libbb: bb_do_delay(3) -> pause_after_failed_login(), and stop looping there

function                                             old     new   delta
pause_after_failed_login                               -       9      +9
vlock_main                                           358     353      -5
sulogin_main                                         252     247      -5
su_main                                              484     479      -5
passwd_main                                          936     931      -5
login_main                                           967     962      -5
bb_do_delay                                           68       -     -68
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/5 up/down: 9/-93)             Total: -84 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-11-29 11:32:48 +01:00
parent 32a8f70ac1
commit 87bd558f3f
8 changed files with 27 additions and 16 deletions

View File

@ -1604,9 +1604,10 @@ char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC;
#ifndef LOGIN_FAIL_DELAY #ifndef LOGIN_FAIL_DELAY
#define LOGIN_FAIL_DELAY 3 #define LOGIN_FAIL_DELAY 3
#endif #endif
extern void bb_do_delay(int seconds) FAST_FUNC; void pause_after_failed_login(void) FAST_FUNC;
extern void change_identity(const struct passwd *pw) FAST_FUNC; void bb_do_delay(int seconds) FAST_FUNC;
extern void run_shell(const char *shell, int loginshell, const char **args) NORETURN FAST_FUNC; void change_identity(const struct passwd *pw) FAST_FUNC;
void run_shell(const char *shell, int loginshell, const char **args) NORETURN FAST_FUNC;
/* Returns $SHELL, getpwuid(getuid())->pw_shell, or DEFAULT_SHELL. /* Returns $SHELL, getpwuid(getuid())->pw_shell, or DEFAULT_SHELL.
* Note that getpwuid result might need xstrdup'ing * Note that getpwuid result might need xstrdup'ing

View File

@ -8,13 +8,23 @@
*/ */
#include "libbb.h" #include "libbb.h"
void FAST_FUNC bb_do_delay(int seconds) /* void FAST_FUNC bb_do_delay(int seconds) { ... } - no users yet */
{
time_t start, now;
start = time(NULL); #ifndef LOGIN_FAIL_DELAY
#define LOGIN_FAIL_DELAY 3
#endif
void FAST_FUNC pause_after_failed_login(void)
{
#if 0 /* over-engineered madness */
time_t end, diff;
end = time(NULL) + LOGIN_FAIL_DELAY;
diff = LOGIN_FAIL_DELAY;
do { do {
sleep(seconds); sleep(diff);
now = time(NULL); diff = end - time(NULL);
} while ((now - start) < seconds); } while (diff > 0);
#else
sleep(LOGIN_FAIL_DELAY);
#endif
} }

View File

@ -505,7 +505,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
#endif /* ENABLE_PAM */ #endif /* ENABLE_PAM */
auth_failed: auth_failed:
opt &= ~LOGIN_OPT_f; opt &= ~LOGIN_OPT_f;
bb_do_delay(LOGIN_FAIL_DELAY); pause_after_failed_login();
/* TODO: doesn't sound like correct English phrase to me */ /* TODO: doesn't sound like correct English phrase to me */
puts("Login incorrect"); puts("Login incorrect");
if (++count == 3) { if (++count == 3) {

View File

@ -57,7 +57,7 @@ static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo
encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */ encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
if (strcmp(encrypted, pw->pw_passwd) != 0) { if (strcmp(encrypted, pw->pw_passwd) != 0) {
syslog(LOG_WARNING, "incorrect password for %s", pw->pw_name); syslog(LOG_WARNING, "incorrect password for %s", pw->pw_name);
bb_do_delay(LOGIN_FAIL_DELAY); pause_after_failed_login();
puts("Incorrect password"); puts("Incorrect password");
goto err_ret; goto err_ret;
} }

View File

@ -146,7 +146,7 @@ int su_main(int argc UNUSED_PARAM, char **argv)
if (ENABLE_FEATURE_SU_SYSLOG) if (ENABLE_FEATURE_SU_SYSLOG)
syslog(LOG_NOTICE, "%c %s %s:%s", syslog(LOG_NOTICE, "%c %s %s:%s",
'-', tty, old_user, opt_username); '-', tty, old_user, opt_username);
bb_do_delay(LOGIN_FAIL_DELAY); pause_after_failed_login();
bb_simple_error_msg_and_die("incorrect password"); bb_simple_error_msg_and_die("incorrect password");
} }

View File

@ -74,7 +74,7 @@ int sulogin_main(int argc UNUSED_PARAM, char **argv)
if (r > 0) { if (r > 0) {
break; break;
} }
bb_do_delay(LOGIN_FAIL_DELAY); pause_after_failed_login();
bb_simple_info_msg("Login incorrect"); bb_simple_info_msg("Login incorrect");
} }

View File

@ -120,7 +120,7 @@ int vlock_main(int argc UNUSED_PARAM, char **argv)
if (ask_and_check_password(pw) > 0) { if (ask_and_check_password(pw) > 0) {
break; break;
} }
bb_do_delay(LOGIN_FAIL_DELAY); pause_after_failed_login();
puts("Incorrect password"); puts("Incorrect password");
} }

View File

@ -139,7 +139,7 @@ int pscan_main(int argc UNUSED_PARAM, char **argv)
* We check rtt BEFORE we usleep, otherwise * We check rtt BEFORE we usleep, otherwise
* on localhost we'll have no writes done (!) * on localhost we'll have no writes done (!)
* before we exceed (rather small) rtt */ * before we exceed (rather small) rtt */
usleep(rtt_4/8); usleep(rtt_4 / 8);
open: open:
diff = MONOTONIC_US() - start; diff = MONOTONIC_US() - start;
DMSG("write to port %u @%u", port, diff - start); DMSG("write to port %u @%u", port, diff - start);