diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-07 15:50:30 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-07 15:50:30 +0200 |
commit | 17058a06c4333fc0c492c168c8a971ebd0fd5a5a (patch) | |
tree | a32133b2e5c18be65796283177551f9bcd7e49bb | |
parent | bae8fc4436f9aeb43ef0aaccd1c9b1b35b5a4617 (diff) | |
download | busybox-w32-17058a06c4333fc0c492c168c8a971ebd0fd5a5a.tar.gz busybox-w32-17058a06c4333fc0c492c168c8a971ebd0fd5a5a.tar.bz2 busybox-w32-17058a06c4333fc0c492c168c8a971ebd0fd5a5a.zip |
libbb: switch bb_ask_noecho() to "mallocing" string return API
function old new delta
bb_ask_noecho 313 330 +17
get_cred_or_die 125 115 -10
passwd_main 995 958 -37
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 17/-47) Total: -30 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 7 | ||||
-rw-r--r-- | libbb/bb_askpass.c | 45 | ||||
-rw-r--r-- | loginutils/cryptpw.c | 2 | ||||
-rw-r--r-- | loginutils/passwd.c | 3 | ||||
-rw-r--r-- | mailutils/mail.c | 4 |
5 files changed, 39 insertions, 22 deletions
diff --git a/include/libbb.h b/include/libbb.h index 5388d9d95..ed9a562ff 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1408,10 +1408,11 @@ extern int set_loop(char **devname, const char *file, unsigned long long offset, | |||
1408 | #define BB_LO_FLAGS_READ_ONLY 1 | 1408 | #define BB_LO_FLAGS_READ_ONLY 1 |
1409 | #define BB_LO_FLAGS_AUTOCLEAR 4 | 1409 | #define BB_LO_FLAGS_AUTOCLEAR 4 |
1410 | 1410 | ||
1411 | /* Like bb_ask_noecho below, but asks on stdin with no timeout. */ | 1411 | /* Returns malloced str */ |
1412 | char *bb_ask_noecho(int fd, int timeout, const char *prompt) FAST_FUNC; | ||
1413 | /* Like bb_ask_noecho, but asks on stdin with no timeout. */ | ||
1412 | char *bb_ask_noecho_stdin(const char *prompt) FAST_FUNC; | 1414 | char *bb_ask_noecho_stdin(const char *prompt) FAST_FUNC; |
1413 | //TODO: pass buf pointer or return allocated buf (avoid statics)? | 1415 | |
1414 | char *bb_ask_noecho(const int fd, int timeout, const char *prompt) FAST_FUNC; | ||
1415 | int bb_ask_y_confirmation_FILE(FILE *fp) FAST_FUNC; | 1416 | int bb_ask_y_confirmation_FILE(FILE *fp) FAST_FUNC; |
1416 | int bb_ask_y_confirmation(void) FAST_FUNC; | 1417 | int bb_ask_y_confirmation(void) FAST_FUNC; |
1417 | 1418 | ||
diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c index aadc69108..2dcead35a 100644 --- a/libbb/bb_askpass.c +++ b/libbb/bb_askpass.c | |||
@@ -13,16 +13,9 @@ static void askpass_timeout(int UNUSED_PARAM ignore) | |||
13 | { | 13 | { |
14 | } | 14 | } |
15 | 15 | ||
16 | char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt) | 16 | char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt) |
17 | { | ||
18 | return bb_ask_noecho(STDIN_FILENO, 0, prompt); | ||
19 | } | ||
20 | char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | ||
21 | { | 17 | { |
22 | /* Was static char[BIGNUM] */ | 18 | #define MAX_LINE 0xfff |
23 | enum { sizeof_passwd = 128 }; | ||
24 | |||
25 | char *passwd; | ||
26 | char *ret; | 19 | char *ret; |
27 | int i; | 20 | int i; |
28 | struct sigaction sa, oldsa; | 21 | struct sigaction sa, oldsa; |
@@ -37,7 +30,17 @@ char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | |||
37 | 30 | ||
38 | tcgetattr(fd, &oldtio); | 31 | tcgetattr(fd, &oldtio); |
39 | tio = oldtio; | 32 | tio = oldtio; |
40 | /* Switch off echo */ | 33 | /* Switch off echo. ECHOxyz meaning: |
34 | * ECHO echo input chars | ||
35 | * ECHOE echo BS-SP-BS on erase character | ||
36 | * ECHOK echo kill char specially, not as ^c (ECHOKE controls how exactly) | ||
37 | * ECHOKE erase all input via BS-SP-BS on kill char (else go to next line) | ||
38 | * ECHOCTL Echo ctrl chars as ^c (else echo verbatim: | ||
39 | * e.g. up arrow emits "ESC-something" and thus moves cursor up!) | ||
40 | * ECHONL Echo NL even if ECHO is not set | ||
41 | * ECHOPRT On erase, echo erased chars | ||
42 | * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen] | ||
43 | */ | ||
41 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); | 44 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); |
42 | tcsetattr(fd, TCSANOW, &tio); | 45 | tcsetattr(fd, TCSANOW, &tio); |
43 | 46 | ||
@@ -51,20 +54,30 @@ char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | |||
51 | alarm(timeout); | 54 | alarm(timeout); |
52 | } | 55 | } |
53 | 56 | ||
54 | passwd = auto_string(xmalloc(sizeof_passwd)); | 57 | ret = NULL; |
55 | ret = passwd; | ||
56 | i = 0; | 58 | i = 0; |
57 | while (1) { | 59 | while (1) { |
58 | int r = read(fd, &ret[i], 1); | 60 | int r; |
61 | |||
62 | /* User input is uber-slow, no need to optimize reallocs. | ||
63 | * Grow it on every char. | ||
64 | */ | ||
65 | ret = xrealloc(ret, i + 2); | ||
66 | r = read(fd, &ret[i], 1); | ||
67 | |||
59 | if ((i == 0 && r == 0) /* EOF (^D) with no password */ | 68 | if ((i == 0 && r == 0) /* EOF (^D) with no password */ |
60 | || r < 0 /* read is interrupted by timeout or ^C */ | 69 | || r < 0 /* read is interrupted by timeout or ^C */ |
61 | ) { | 70 | ) { |
71 | ret[i] = '\0'; /* paranoia */ | ||
72 | nuke_str(ret); /* paranoia */ | ||
73 | free(ret); | ||
62 | ret = NULL; | 74 | ret = NULL; |
63 | break; | 75 | break; |
64 | } | 76 | } |
77 | |||
65 | if (r == 0 /* EOF */ | 78 | if (r == 0 /* EOF */ |
66 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ | 79 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ |
67 | || ++i == sizeof_passwd-1 /* line limit */ | 80 | || ++i == MAX_LINE /* line limit */ |
68 | ) { | 81 | ) { |
69 | ret[i] = '\0'; | 82 | ret[i] = '\0'; |
70 | break; | 83 | break; |
@@ -80,3 +93,7 @@ char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | |||
80 | fflush_all(); | 93 | fflush_all(); |
81 | return ret; | 94 | return ret; |
82 | } | 95 | } |
96 | char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt) | ||
97 | { | ||
98 | return bb_ask_noecho(STDIN_FILENO, 0, prompt); | ||
99 | } | ||
diff --git a/loginutils/cryptpw.c b/loginutils/cryptpw.c index 3ca7eda4a..fbb7f0515 100644 --- a/loginutils/cryptpw.c +++ b/loginutils/cryptpw.c | |||
@@ -133,7 +133,7 @@ int cryptpw_main(int argc UNUSED_PARAM, char **argv) | |||
133 | if (!password) { | 133 | if (!password) { |
134 | /* Only mkpasswd, and only from tty, prompts. | 134 | /* Only mkpasswd, and only from tty, prompts. |
135 | * Otherwise it is a plain read. */ | 135 | * Otherwise it is a plain read. */ |
136 | password = (ENABLE_MKPASSWD && isatty(STDIN_FILENO) && applet_name[0] == 'm') | 136 | password = (ENABLE_MKPASSWD && applet_name[0] == 'm' && isatty(STDIN_FILENO)) |
137 | ? bb_ask_noecho_stdin("Password: ") | 137 | ? bb_ask_noecho_stdin("Password: ") |
138 | : xmalloc_fgetline(stdin) | 138 | : xmalloc_fgetline(stdin) |
139 | ; | 139 | ; |
diff --git a/loginutils/passwd.c b/loginutils/passwd.c index 02303b575..d0408d8b4 100644 --- a/loginutils/passwd.c +++ b/loginutils/passwd.c | |||
@@ -65,11 +65,9 @@ static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo | |||
65 | if (ENABLE_FEATURE_CLEAN_UP) | 65 | if (ENABLE_FEATURE_CLEAN_UP) |
66 | free(encrypted); | 66 | free(encrypted); |
67 | } | 67 | } |
68 | orig = xstrdup(orig); /* or else bb_ask_noecho_stdin() will destroy it */ | ||
69 | newp = bb_ask_noecho_stdin("New password: "); /* returns ptr to static */ | 68 | newp = bb_ask_noecho_stdin("New password: "); /* returns ptr to static */ |
70 | if (!newp) | 69 | if (!newp) |
71 | goto err_ret; | 70 | goto err_ret; |
72 | newp = xstrdup(newp); /* we are going to bb_ask_noecho_stdin() again, so save it */ | ||
73 | if (ENABLE_FEATURE_PASSWD_WEAK_CHECK | 71 | if (ENABLE_FEATURE_PASSWD_WEAK_CHECK |
74 | && obscure(orig, newp, pw) | 72 | && obscure(orig, newp, pw) |
75 | && myuid != 0 | 73 | && myuid != 0 |
@@ -99,6 +97,7 @@ static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo | |||
99 | if (ENABLE_FEATURE_CLEAN_UP) free(newp); | 97 | if (ENABLE_FEATURE_CLEAN_UP) free(newp); |
100 | 98 | ||
101 | nuke_str(cp); | 99 | nuke_str(cp); |
100 | if (ENABLE_FEATURE_CLEAN_UP) free(cp); | ||
102 | return ret; | 101 | return ret; |
103 | } | 102 | } |
104 | 103 | ||
diff --git a/mailutils/mail.c b/mailutils/mail.c index 0fc615a7f..7af7edd6c 100644 --- a/mailutils/mail.c +++ b/mailutils/mail.c | |||
@@ -163,8 +163,8 @@ void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol) | |||
163 | void FAST_FUNC get_cred_or_die(int fd) | 163 | void FAST_FUNC get_cred_or_die(int fd) |
164 | { | 164 | { |
165 | if (isatty(fd)) { | 165 | if (isatty(fd)) { |
166 | G.user = xstrdup(bb_ask_noecho(fd, /* timeout: */ 0, "User: ")); | 166 | G.user = bb_ask_noecho(fd, /* timeout: */ 0, "User: "); |
167 | G.pass = xstrdup(bb_ask_noecho(fd, /* timeout: */ 0, "Password: ")); | 167 | G.pass = bb_ask_noecho(fd, /* timeout: */ 0, "Password: "); |
168 | } else { | 168 | } else { |
169 | G.user = xmalloc_reads(fd, /* maxsize: */ NULL); | 169 | G.user = xmalloc_reads(fd, /* maxsize: */ NULL); |
170 | G.pass = xmalloc_reads(fd, /* maxsize: */ NULL); | 170 | G.pass = xmalloc_reads(fd, /* maxsize: */ NULL); |