4 * system-level password checking for autologin
5 * by Nathan Bryant, March 1999
9 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
11 #define _XOPEN_SOURCE_EXTENDED 1
18 #include <sys/types.h>
28 #include <security/pam_appl.h>
31 * struct appdata: passed to the conversation function
41 * conv(): the PAM conversation function. this assumes that a
42 * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
43 * asking for a password. esoteric authentication modules will fail with this
44 * code, but we can't really support them with the existing client protocol
45 * anyway. the failure mode should be to deny access, in any case.
48 static int conv(int num_msg, const struct pam_message **msg,
49 struct pam_response **resp, void *appdata_ptr)
51 struct pam_response *temp_resp;
52 struct appdata *data = appdata_ptr;
54 if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
59 switch ((*msg)[num_msg].msg_style)
61 case PAM_PROMPT_ECHO_ON:
62 temp_resp[num_msg].resp = strdup(data->name);
64 case PAM_PROMPT_ECHO_OFF:
65 temp_resp[num_msg].resp = strdup(data->pw);
68 temp_resp[num_msg].resp = NULL;
70 temp_resp[num_msg].resp_retcode = 0;
76 #endif /* HAVE_PAM_START */
79 * validpw(): check that `pass' is the correct password for `uid'
80 * returns zero if no, nonzero if yes
83 int validpw(uid_t uid, const char *pass)
99 if ((pw = getpwuid(uid)) == NULL)
102 #ifdef HAVE_PAM_START
104 pc.appdata_ptr = &data;
105 data.name = pw->pw_name;
107 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
110 if ((i = pam_authenticate(ph, PAM_SILENT)) == PAM_SUCCESS)
111 if ((i = pam_acct_mgmt(ph, PAM_SILENT)) == PAM_SUCCESS)
114 pam_end(ph, i | PAM_DATA_SILENT);
116 crypted_pwd = pw->pw_passwd;
119 if ((sp = getspnam(pw->pw_name)) != NULL)
120 crypted_pwd = sp->sp_pwdp;
123 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
125 #endif /* HAVE_PAM_START */