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 as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
25 #define _XOPEN_SOURCE_EXTENDED 1
32 #include <sys/types.h>
42 #include <security/pam_appl.h>
45 * struct appdata: passed to the conversation function
55 * conv(): the PAM conversation function. this assumes that a
56 * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
57 * asking for a password. esoteric authentication modules will fail with this
58 * code, but we can't really support them with the existing client protocol
59 * anyway. the failure mode should be to deny access, in any case.
62 static int conv(int num_msg, const struct pam_message **msg,
63 struct pam_response **resp, void *appdata_ptr)
65 struct pam_response *temp_resp;
66 struct appdata *data = appdata_ptr;
68 if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
73 switch ((*msg)[num_msg].msg_style)
75 case PAM_PROMPT_ECHO_ON:
76 temp_resp[num_msg].resp = strdup(data->name);
78 case PAM_PROMPT_ECHO_OFF:
79 temp_resp[num_msg].resp = strdup(data->pw);
82 temp_resp[num_msg].resp = NULL;
84 temp_resp[num_msg].resp_retcode = 0;
90 #endif /* HAVE_PAM_START */
94 * check that `pass' is the correct password for `uid'
95 * returns zero if no, nonzero if yes
98 int validate_password(uid_t uid, const char *pass)
100 #ifdef HAVE_PAM_START
115 #ifdef PAM_DATA_SILENT
116 flags = PAM_DATA_SILENT;
119 #endif /* PAM_DATA_SILENT */
121 if ((pw = getpwuid(uid)) == NULL) {
125 #ifdef HAVE_PAM_START
127 pc.appdata_ptr = &data;
128 data.name = pw->pw_name;
130 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
133 if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS)
134 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS)
137 pam_end(ph, i | flags);
139 crypted_pwd = pw->pw_passwd;
142 if ((sp = getspnam(pw->pw_name)) != NULL)
143 crypted_pwd = sp->sp_pwdp;
146 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
148 #endif /* HAVE_PAM_START */