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 free 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
24 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
26 #define _XOPEN_SOURCE_EXTENDED 1
33 #include <sys/types.h>
43 #include <security/pam_appl.h>
46 * struct appdata: passed to the conversation function
56 * conv(): the PAM conversation function. this assumes that a
57 * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
58 * asking for a password. esoteric authentication modules will fail with this
59 * code, but we can't really support them with the existing client protocol
60 * anyway. the failure mode should be to deny access, in any case.
63 static int conv(int num_msg, const struct pam_message **msg,
64 struct pam_response **resp, void *appdata_ptr)
66 struct pam_response *temp_resp;
67 struct appdata *data = appdata_ptr;
69 if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
74 switch ((*msg)[num_msg].msg_style)
76 case PAM_PROMPT_ECHO_ON:
77 temp_resp[num_msg].resp = strdup(data->name);
79 case PAM_PROMPT_ECHO_OFF:
80 temp_resp[num_msg].resp = strdup(data->pw);
83 temp_resp[num_msg].resp = NULL;
85 temp_resp[num_msg].resp_retcode = 0;
91 #endif /* HAVE_PAM_START */
95 * check that `pass' is the correct password for `uid'
96 * returns zero if no, nonzero if yes
99 int validate_password(uid_t uid, const char *pass)
101 #ifdef HAVE_PAM_START
116 flags = 0; /* silences compiler warning */
118 #ifdef PAM_DATA_SILENT
119 flags = ( flags | PAM_DATA_SILENT ) ;
120 #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 */