Skip to content

Commit 2425d7f

Browse files
committed
check PAM user against previous user, not pw_name
Avoids early fatal() if the user doesn't exist. Reported by Viswesh Narayanan; ok dtucker@
1 parent 7e2f89b commit 2425d7f

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

auth-pam.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ pthread_join(sp_pthread_t thread, void **value)
237237

238238

239239
static pam_handle_t *sshpam_handle = NULL;
240+
static char *sshpam_initial_user;
240241
static int sshpam_err = 0;
241242
static int sshpam_authenticated = 0;
242243
static int sshpam_session_open = 0;
@@ -485,10 +486,11 @@ check_pam_user(Authctxt *authctxt)
485486
return PAM_USER_UNKNOWN;
486487
}
487488

488-
if (strcmp(authctxt->pw->pw_name, pam_user) != 0) {
489-
debug("PAM user \"%s\" does not match expected \"%s\"",
490-
pam_user, authctxt->pw->pw_name);
491-
return PAM_USER_UNKNOWN;
489+
if (sshpam_initial_user == NULL)
490+
fatal_f("internal error: sshpam_initial_user NULL");
491+
if (strcmp(sshpam_initial_user, pam_user) != 0) {
492+
error_f("PAM user \"%s\" does not match previous \"%s\"",
493+
pam_user, sshpam_initial_user);
492494
}
493495
return PAM_SUCCESS;
494496
}
@@ -709,6 +711,8 @@ sshpam_cleanup(void)
709711
sshpam_authenticated = 0;
710712
pam_end(sshpam_handle, sshpam_err);
711713
sshpam_handle = NULL;
714+
free(sshpam_initial_user);
715+
sshpam_initial_user = NULL;
712716
}
713717

714718
static int
@@ -725,12 +729,8 @@ sshpam_init(struct ssh *ssh, Authctxt *authctxt)
725729
fatal("Username too long from %s port %d",
726730
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
727731
#endif
728-
if (sshpam_handle == NULL) {
729-
if (ssh == NULL) {
730-
fatal("%s: called initially with no "
731-
"packet context", __func__);
732-
}
733-
}
732+
if (sshpam_handle == NULL && ssh == NULL)
733+
fatal("%s: called initially with no packet context", __func__);
734734
if (sshpam_handle != NULL) {
735735
/* We already have a PAM context; check if the user matches */
736736
if ((sshpam_err = check_pam_user(authctxt)) != PAM_SUCCESS)
@@ -741,6 +741,7 @@ sshpam_init(struct ssh *ssh, Authctxt *authctxt)
741741
options.pam_service_name);
742742
sshpam_err = pam_start(options.pam_service_name, user,
743743
&store_conv, &sshpam_handle);
744+
sshpam_initial_user = xstrdup(user);
744745
sshpam_authctxt = authctxt;
745746

746747
if (sshpam_err != PAM_SUCCESS) {

0 commit comments

Comments
 (0)