1 // system-level password checking for host auth mode
3 // Copyright (c) 1999-2022 by Nathan Bryant, Trey Van Riper, and the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
7 // The program is distributed without any warranty, expressed or implied.
9 #if defined(__linux) || defined(__sun) // needed for crypt():
11 #define _XOPEN_SOURCE_EXTENDED 1
18 #include <sys/types.h>
22 #include "../server/sysdep.h"
29 #include <security/pam_appl.h>
31 // struct appdata: passed to the conversation function
37 // conv(): the PAM conversation function. this assumes that a
38 // PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
39 // asking for a password. esoteric authentication modules will fail with this
40 // code, but we can't really support them with the existing client protocol
41 // anyway. the failure mode should be to deny access, in any case.
42 static int conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) {
43 struct pam_response *temp_resp;
44 struct appdata *data = appdata_ptr;
47 malloc(sizeof(struct pam_response[num_msg]))) == NULL)
51 switch ((*msg)[num_msg].msg_style) {
52 case PAM_PROMPT_ECHO_ON:
53 temp_resp[num_msg].resp = strdup(data->name);
55 case PAM_PROMPT_ECHO_OFF:
56 temp_resp[num_msg].resp = strdup(data->pw);
59 temp_resp[num_msg].resp = NULL;
61 temp_resp[num_msg].resp_retcode = 0;
67 #endif // HAVE_PAM_START
70 // check that `pass' is the correct password for `uid'
71 // returns zero if no, nonzero if yes
72 int validate_password(uid_t uid, const char *pass) {
96 #ifdef PAM_DATA_SILENT
97 int flags = PAM_DATA_SILENT;
103 pc.appdata_ptr = &data;
104 data.name = pw->pw_name;
106 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
109 if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS) {
110 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS) {
115 pam_end(ph, i | flags);
117 crypted_pwd = pw->pw_passwd;
122 if (pw->pw_name == NULL)
124 if ((sp = getspnam(pw->pw_name)) != NULL) {
125 crypted_pwd = sp->sp_pwdp;
129 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd)) {
132 #endif // HAVE_PAM_START