|
| 1 | +From 02f8fcddca83e8a4e9954d4f1e4087b777c03126 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Renuka Manavalan < [email protected]> |
| 3 | +Date: Sat, 19 Feb 2022 03:34:35 +0000 |
| 4 | +Subject: [PATCH] handle bad password set by sshd |
| 5 | + |
| 6 | +--- |
| 7 | + pam_tacplus.c | 7 +++++++ |
| 8 | + support.c | 37 +++++++++++++++++++++++++++++++++++++ |
| 9 | + support.h | 1 + |
| 10 | + tacc.c | 4 ++-- |
| 11 | + 4 files changed, 47 insertions(+), 2 deletions(-) |
| 12 | + |
| 13 | +diff --git a/pam_tacplus.c b/pam_tacplus.c |
| 14 | +index 38e2a70..bf06519 100644 |
| 15 | +--- a/pam_tacplus.c |
| 16 | ++++ b/pam_tacplus.c |
| 17 | +@@ -251,6 +251,13 @@ int pam_sm_authenticate (pam_handle_t * pamh, int flags, |
| 18 | + return PAM_CRED_INSUFFICIENT; |
| 19 | + } |
| 20 | + |
| 21 | ++ if (validate_not_sshd_bad_pass(pass) != PAM_SUCCESS) { |
| 22 | ++ syslog(LOG_LOCAL0|LOG_ERR, "auth fail: Password incorrect. user: %s", user); |
| 23 | ++ memset(pass, 0, strlen (pass)); |
| 24 | ++ free(pass); |
| 25 | ++ return PAM_AUTH_ERR; |
| 26 | ++ } |
| 27 | ++ |
| 28 | + retval = pam_set_item (pamh, PAM_AUTHTOK, pass); |
| 29 | + if (retval != PAM_SUCCESS) { |
| 30 | + _pam_log(LOG_ERR, "unable to set password"); |
| 31 | +diff --git a/support.c b/support.c |
| 32 | +index 7c00618..a34d061 100644 |
| 33 | +--- a/support.c |
| 34 | ++++ b/support.c |
| 35 | +@@ -106,6 +106,43 @@ int converse(pam_handle_t * pamh, int nargs, const struct pam_message *message, |
| 36 | + return retval; |
| 37 | + } |
| 38 | + |
| 39 | ++/* |
| 40 | ++ * Ref: From <https://groups.google.com/g/mailing.unix.openssh-dev/c/ViHvtciKYh0> |
| 41 | ++ * For future archive searchers: |
| 42 | ++ * > Why does OpenSSH replaces the password entered by the user with the |
| 43 | ++ * > bad password - "\b\n\r\177INCORRECT |
| 44 | ++ * |
| 45 | ++ * There are some situations where sshd determines a user can't log in. |
| 46 | ++ * Typical samples of that are DenyUsers or PermitRootLogin. |
| 47 | ++ * In those cases sshd *still* calls PAM, so that delays set by it are |
| 48 | ++ * still performed to the user (without leaking info about accounts |
| 49 | ++ * existing, disabled, etc.). But in order to ensure it can't succeed, |
| 50 | ++ * replaces the password with that impossible one. |
| 51 | ++ * |
| 52 | ++ */ |
| 53 | ++int validate_not_sshd_bad_pass(const char *pass) |
| 54 | ++{ |
| 55 | ++ const char *SSHD_BAD_PASS = "\010\012\015\177INCORRECT"; |
| 56 | ++ const int SSHD_BAD_PASS_LEN = strlen(SSHD_BAD_PASS); |
| 57 | ++ |
| 58 | ++ int len = strlen(pass); |
| 59 | ++ const char *p = pass; |
| 60 | ++ |
| 61 | ++ if (len == 0) |
| 62 | ++ return PAM_SUCCESS; |
| 63 | ++ |
| 64 | ++ while (len > 0) { |
| 65 | ++ int l = len < SSHD_BAD_PASS_LEN ? len : SSHD_BAD_PASS_LEN; |
| 66 | ++ |
| 67 | ++ if (strncmp(p, SSHD_BAD_PASS, l) != 0) |
| 68 | ++ return PAM_SUCCESS; |
| 69 | ++ |
| 70 | ++ len -= l; |
| 71 | ++ p += l; |
| 72 | ++ } |
| 73 | ++ return PAM_AUTH_ERR; |
| 74 | ++} |
| 75 | ++ |
| 76 | + /* stolen from pam_stress */ |
| 77 | + int tacacs_get_password (pam_handle_t * pamh, int flags |
| 78 | + ,int ctrl, char **password) { |
| 79 | +diff --git a/support.h b/support.h |
| 80 | +index 9cbd040..d7845d3 100644 |
| 81 | +--- a/support.h |
| 82 | ++++ b/support.h |
| 83 | +@@ -41,6 +41,7 @@ extern char tac_prompt[64]; |
| 84 | + int _pam_parse (int, const char **); |
| 85 | + unsigned long _resolve_name (char *); |
| 86 | + unsigned long _getserveraddr (char *serv); |
| 87 | ++int validate_not_sshd_bad_pass(const char *pass); |
| 88 | + int tacacs_get_password (pam_handle_t *, int, int, char **); |
| 89 | + int converse (pam_handle_t *, int, const struct pam_message *, struct pam_response **); |
| 90 | + void _pam_log (int, const char *, ...); |
| 91 | +diff --git a/tacc.c b/tacc.c |
| 92 | +index fcc7d8c..bf0f2a3 100644 |
| 93 | +--- a/tacc.c |
| 94 | ++++ b/tacc.c |
| 95 | +@@ -181,7 +181,7 @@ int main(int argc, char **argv) { |
| 96 | + break; |
| 97 | + case 'L': |
| 98 | + // tac_login is a global variable initialized in libtac |
| 99 | +- bzero(tac_login, sizeof(tac_login)); |
| 100 | ++ memset(tac_login, 0, sizeof(tac_login)); |
| 101 | + strncpy(tac_login, optarg, sizeof(tac_login) - 1); |
| 102 | + break; |
| 103 | + case 'p': |
| 104 | +@@ -312,7 +312,7 @@ int main(int argc, char **argv) { |
| 105 | + } |
| 106 | + |
| 107 | + /* we no longer need the password in our address space */ |
| 108 | +- bzero(pass, strlen(pass)); |
| 109 | ++ memset(pass, 0, strlen(pass)); |
| 110 | + pass = NULL; |
| 111 | + |
| 112 | + if (do_account) { |
| 113 | +-- |
| 114 | +2.17.1 |
| 115 | + |
0 commit comments