4 * system-level password checking for autologin
5 * by Nathan Bryant, March 1999
13 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
15 #define _XOPEN_SOURCE_EXTENDED 1
22 #include <sys/types.h>
32 #include <security/pam_appl.h>
35 * struct appdata: passed to the conversation function
45 * conv(): the PAM conversation function. this assumes that a
46 * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
47 * asking for a password. esoteric authentication modules will fail with this
48 * code, but we can't really support them with the existing client protocol
49 * anyway. the failure mode should be to deny access, in any case.
52 static int conv(int num_msg, const struct pam_message **msg,
53 struct pam_response **resp, void *appdata_ptr)
55 struct pam_response *temp_resp;
56 struct appdata *data = appdata_ptr;
58 if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
63 switch ((*msg)[num_msg].msg_style)
65 case PAM_PROMPT_ECHO_ON:
66 temp_resp[num_msg].resp = strdup(data->name);
68 case PAM_PROMPT_ECHO_OFF:
69 temp_resp[num_msg].resp = strdup(data->pw);
72 temp_resp[num_msg].resp = NULL;
74 temp_resp[num_msg].resp_retcode = 0;
80 #endif /* HAVE_PAM_START */
83 * validpw(): check that `pass' is the correct password for `uid'
84 * returns zero if no, nonzero if yes
87 int validpw(uid_t uid, const char *pass)
103 if ((pw = getpwuid(uid)) == NULL)
106 #ifdef HAVE_PAM_START
108 pc.appdata_ptr = &data;
109 data.name = pw->pw_name;
111 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
114 if ((i = pam_authenticate(ph, PAM_SILENT)) == PAM_SUCCESS)
115 if ((i = pam_acct_mgmt(ph, PAM_SILENT)) == PAM_SUCCESS)
118 pam_end(ph, i | PAM_DATA_SILENT);
120 crypted_pwd = pw->pw_passwd;
123 if ((sp = getspnam(pw->pw_name)) != NULL)
124 crypted_pwd = sp->sp_pwdp;
127 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
129 #endif /* HAVE_PAM_START */