Skip to content

Commit

Permalink
Retry up to 3 times on password authentication failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Rehan committed Dec 9, 2023
1 parent b96106b commit 2712da2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 2 additions & 0 deletions doas.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ char **prepenv(const struct rule *, const struct passwd *,
#define PERSIST 0x4
#define NOLOG 0x8

#define AUTH_RETRIES 3

#ifdef USE_PAM
void pamauth(const char *, const char *, int, int, int);
#endif
Expand Down
19 changes: 14 additions & 5 deletions pam.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,20 @@ pamauth(const char *user, const char *myname, int interactive, int nopass, int p
"\rdoas (%.32s@%.32s) password: ", myname, host);

/* authenticate */
ret = pam_authenticate(pamh, 0);
if (ret != PAM_SUCCESS) {
pamcleanup(ret, sess, cred);
syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
errx(1, "Authentication failed");
for (int i = 0; i < AUTH_RETRIES; i++) {
ret = pam_authenticate(pamh, 0);
if (ret != PAM_SUCCESS) {
syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);

if (i == AUTH_RETRIES - 1) {
pamcleanup(ret, sess, cred);
errx(1, "Authentication failed");
}
else
warnx("Authentication failed");
}
else
break;
}
}

Expand Down
39 changes: 22 additions & 17 deletions shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,28 @@ shadowauth(const char *myname, int persist)
snprintf(cbuf, sizeof(cbuf),
"\rdoas (%.32s@%.32s) password: ", myname, host);
challenge = cbuf;

response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
if (response == NULL && errno == ENOTTY) {
syslog(LOG_AUTHPRIV | LOG_NOTICE,
"tty required for %s", myname);
errx(1, "a tty is required");
}
if (response == NULL)
err(1, "readpassphrase");
if ((encrypted = crypt(response, hash)) == NULL) {
explicit_bzero(rbuf, sizeof(rbuf));
errx(1, "Authentication failed");
}
explicit_bzero(rbuf, sizeof(rbuf));
if (strcmp(encrypted, hash) != 0) {
syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
errx(1, "Authentication failed");
for (int i = 0; i < AUTH_RETRIES; i++) {
response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
if (response == NULL && errno == ENOTTY) {
syslog(LOG_AUTHPRIV | LOG_NOTICE,
"tty required for %s", myname);
errx(1, "a tty is required");
}
if (response == NULL)
err(1, "readpassphrase");
if ((encrypted = crypt(response, hash)) == NULL) {
explicit_bzero(rbuf, sizeof(rbuf));
(i == AUTH_RETRIES - 1) ? errx(1, "Authentication failed") : warnx("Authentication failed");
}
else {
explicit_bzero(rbuf, sizeof(rbuf));
if (strcmp(encrypted, hash) != 0) {
syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
(i == AUTH_RETRIES - 1) ? errx(1, "Authentication failed") : warnx("Authentication failed");
}
else
break;
}
}

#ifdef USE_TIMESTAMP
Expand Down

0 comments on commit 2712da2

Please sign in to comment.