501008633a09cd1b4ac7a947796f8d99a0dd323e
[citadel.git] / citadel / auth.c
1 /*
2  * system-level password checking for host auth mode
3  * by Nathan Bryant, March 1999
4  * updated by Trey van Riper, June 2005
5  *
6  * Copyright (c) 1999-2009 by the citadel.org team
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
24 #define _XOPEN_SOURCE
25 #define _XOPEN_SOURCE_EXTENDED 1
26 #endif
27
28 #include <pwd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33
34 #include "auth.h"
35 #include "sysdep.h"
36
37 #ifdef HAVE_GETSPNAM
38 #include <shadow.h>
39 #endif
40
41 #ifdef HAVE_PAM_START
42 #include <security/pam_appl.h>
43
44 /*
45  * struct appdata: passed to the conversation function
46  */
47
48 struct appdata
49 {
50   const char *name;
51   const char *pw;
52 };
53
54 /*
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.
60  */
61
62 static int conv(int num_msg, const struct pam_message **msg,
63                 struct pam_response **resp, void *appdata_ptr)
64 {
65   struct pam_response *temp_resp;
66   struct appdata *data = appdata_ptr;
67
68   if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
69     return PAM_CONV_ERR;
70
71   while (num_msg--)
72     {
73       switch ((*msg)[num_msg].msg_style)
74         {
75         case PAM_PROMPT_ECHO_ON:
76           temp_resp[num_msg].resp = strdup(data->name);
77           break;
78         case PAM_PROMPT_ECHO_OFF:
79           temp_resp[num_msg].resp = strdup(data->pw);
80           break;
81         default:
82           temp_resp[num_msg].resp = NULL;
83         }
84       temp_resp[num_msg].resp_retcode = 0;
85     }
86
87   *resp = temp_resp;
88   return PAM_SUCCESS;
89 }
90 #endif /* HAVE_PAM_START */
91
92
93 /*
94  * check that `pass' is the correct password for `uid'
95  * returns zero if no, nonzero if yes
96  */
97
98 int validate_password(uid_t uid, const char *pass)
99 {
100 #ifdef HAVE_PAM_START
101         struct pam_conv pc;
102         struct appdata data;
103         pam_handle_t *ph;
104         int i;
105 #else
106         char *crypted_pwd;
107 #ifdef HAVE_GETSPNAM
108         struct spwd *sp;
109 #endif
110 #endif
111         struct passwd *pw;
112         int retval = 0;
113         int flags = 0;
114
115         flags = 0;      /* silences compiler warning */
116
117 #ifdef PAM_DATA_SILENT
118         flags = ( flags | PAM_DATA_SILENT ) ;
119 #endif /* PAM_DATA_SILENT */
120         if ((pw = getpwuid(uid)) == NULL) {
121                 return retval;
122         }
123
124 #ifdef HAVE_PAM_START
125         pc.conv = conv;
126         pc.appdata_ptr = &data;
127         data.name = pw->pw_name;
128         data.pw = pass;
129         if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
130                 return retval;
131
132         if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS)
133                 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS)
134                         retval = -1;
135
136         pam_end(ph, i | flags);
137 #else
138         crypted_pwd = pw->pw_passwd;
139
140 #ifdef HAVE_GETSPNAM
141         if ((sp = getspnam(pw->pw_name)) != NULL)
142                 crypted_pwd = sp->sp_pwdp;
143 #endif
144
145         if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
146                 retval = -1;
147 #endif                          /* HAVE_PAM_START */
148
149         return retval;
150 }