4 * system-level password checking for autologin
5 * by Nathan Bryant, March 1999
6 * updated by Trey van Riper, June 2005
14 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
16 #define _XOPEN_SOURCE_EXTENDED 1
23 #include <sys/types.h>
33 #include <security/pam_appl.h>
36 * struct appdata: passed to the conversation function
46 * conv(): the PAM conversation function. this assumes that a
47 * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
48 * asking for a password. esoteric authentication modules will fail with this
49 * code, but we can't really support them with the existing client protocol
50 * anyway. the failure mode should be to deny access, in any case.
53 static int conv(int num_msg, const struct pam_message **msg,
54 struct pam_response **resp, void *appdata_ptr)
56 struct pam_response *temp_resp;
57 struct appdata *data = appdata_ptr;
59 if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
64 switch ((*msg)[num_msg].msg_style)
66 case PAM_PROMPT_ECHO_ON:
67 temp_resp[num_msg].resp = strdup(data->name);
69 case PAM_PROMPT_ECHO_OFF:
70 temp_resp[num_msg].resp = strdup(data->pw);
73 temp_resp[num_msg].resp = NULL;
75 temp_resp[num_msg].resp_retcode = 0;
81 #endif /* HAVE_PAM_START */
85 * validpw(): check that `pass' is the correct password for `uid'
86 * returns zero if no, nonzero if yes
89 int validpw(uid_t uid, const char *pass)
106 #ifdef PAM_DATA_SILENT
107 flags = flags | PAM_DATA_SILENT
108 #endif /* PAM_DATA_SILENT */
109 if ((pw = getpwuid(uid)) == NULL)
112 #ifdef HAVE_PAM_START
114 pc.appdata_ptr = &data;
115 data.name = pw->pw_name;
117 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
120 if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS)
121 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS)
124 pam_end(ph, i | flags);
126 crypted_pwd = pw->pw_passwd;
129 if ((sp = getspnam(pw->pw_name)) != NULL)
130 crypted_pwd = sp->sp_pwdp;
133 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
135 #endif /* HAVE_PAM_START */