2 * system-level password checking for host auth mode
3 * by Nathan Bryant, March 1999
4 * updated by Trey van Riper, June 2005
6 * Copyright (c) 1999-2009 by the citadel.org team
8 * This program is open source software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License, version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
19 #define _XOPEN_SOURCE_EXTENDED 1
26 #include <sys/types.h>
36 #include <security/pam_appl.h>
39 * struct appdata: passed to the conversation function
49 * conv(): the PAM conversation function. this assumes that a
50 * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
51 * asking for a password. esoteric authentication modules will fail with this
52 * code, but we can't really support them with the existing client protocol
53 * anyway. the failure mode should be to deny access, in any case.
56 static int conv(int num_msg, const struct pam_message **msg,
57 struct pam_response **resp, void *appdata_ptr)
59 struct pam_response *temp_resp;
60 struct appdata *data = appdata_ptr;
62 if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
67 switch ((*msg)[num_msg].msg_style)
69 case PAM_PROMPT_ECHO_ON:
70 temp_resp[num_msg].resp = strdup(data->name);
72 case PAM_PROMPT_ECHO_OFF:
73 temp_resp[num_msg].resp = strdup(data->pw);
76 temp_resp[num_msg].resp = NULL;
78 temp_resp[num_msg].resp_retcode = 0;
84 #endif /* HAVE_PAM_START */
88 * check that `pass' is the correct password for `uid'
89 * returns zero if no, nonzero if yes
92 int validate_password(uid_t uid, const char *pass)
108 if ((pw = getpwuid(uid)) == NULL) {
112 #ifdef HAVE_PAM_START
114 #ifdef PAM_DATA_SILENT
115 int flags = PAM_DATA_SILENT;
118 #endif /* PAM_DATA_SILENT */
121 pc.appdata_ptr = &data;
122 data.name = pw->pw_name;
124 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
127 if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS)
128 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS)
131 pam_end(ph, i | flags);
133 crypted_pwd = pw->pw_passwd;
136 if ((sp = getspnam(pw->pw_name)) != NULL)
137 crypted_pwd = sp->sp_pwdp;
140 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
142 #endif /* HAVE_PAM_START */