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.
8 #if defined(__linux) || defined(__sun) // needed for crypt():
10 #define _XOPEN_SOURCE_EXTENDED 1
17 #include <sys/types.h>
21 #include "../server/sysdep.h"
28 #include <security/pam_appl.h>
30 // struct appdata: passed to the conversation function
36 // conv(): the PAM conversation function. this assumes that a
37 // PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
38 // asking for a password. esoteric authentication modules will fail with this
39 // code, but we can't really support them with the existing client protocol
40 // anyway. the failure mode should be to deny access, in any case.
41 static int conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) {
42 struct pam_response *temp_resp;
43 struct appdata *data = appdata_ptr;
46 malloc(sizeof(struct pam_response[num_msg]))) == NULL)
50 switch ((*msg)[num_msg].msg_style) {
51 case PAM_PROMPT_ECHO_ON:
52 temp_resp[num_msg].resp = strdup(data->name);
54 case PAM_PROMPT_ECHO_OFF:
55 temp_resp[num_msg].resp = strdup(data->pw);
58 temp_resp[num_msg].resp = NULL;
60 temp_resp[num_msg].resp_retcode = 0;
66 #endif // HAVE_PAM_START
69 // check that `pass' is the correct password for `uid'
70 // returns zero if no, nonzero if yes
71 int validate_password(uid_t uid, const char *pass) {
95 #ifdef PAM_DATA_SILENT
96 int flags = PAM_DATA_SILENT;
102 pc.appdata_ptr = &data;
103 data.name = pw->pw_name;
105 if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
108 if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS) {
109 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS) {
114 pam_end(ph, i | flags);
116 crypted_pwd = pw->pw_passwd;
121 if (pw->pw_name == NULL)
123 if ((sp = getspnam(pw->pw_name)) != NULL) {
124 crypted_pwd = sp->sp_pwdp;
128 if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd)) {
131 #endif // HAVE_PAM_START