* auth.c: applied fleeb's patch to validpw() to clean up FreeBSD compatibility
[citadel.git] / citadel / auth.c
1 /*
2  * $Id$
3  *
4  * system-level password checking for autologin
5  * by Nathan Bryant, March 1999
6  * updated by Trey van Riper, June 2005
7  *
8  */
9
10 #ifdef DLL_EXPORT
11 #define IN_LIBCIT
12 #endif
13
14 #if defined(__linux) || defined(__sun) /* needed for crypt(): */
15 #define _XOPEN_SOURCE
16 #define _XOPEN_SOURCE_EXTENDED 1
17 #endif
18
19 #include <pwd.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24
25 #include "auth.h"
26 #include "sysdep.h"
27
28 #ifdef HAVE_GETSPNAM
29 #include <shadow.h>
30 #endif
31
32 #ifdef HAVE_PAM_START
33 #include <security/pam_appl.h>
34
35 /*
36  * struct appdata: passed to the conversation function
37  */
38
39 struct appdata
40 {
41   const char *name;
42   const char *pw;
43 };
44
45 /*
46  * conv(): the PAM conversation function. this assumes that a
47  * PAM_PROMPT_ECHO_ON is asking for a username, and a PAM_PROMPT_ECHO_OFF is
48  * asking for a password. esoteric authentication modules will fail with this
49  * code, but we can't really support them with the existing client protocol
50  * anyway. the failure mode should be to deny access, in any case.
51  */
52
53 static int conv(int num_msg, const struct pam_message **msg,
54                 struct pam_response **resp, void *appdata_ptr)
55 {
56   struct pam_response *temp_resp;
57   struct appdata *data = appdata_ptr;
58
59   if ((temp_resp = malloc(sizeof(struct pam_response[num_msg]))) == NULL)
60     return PAM_CONV_ERR;
61
62   while (num_msg--)
63     {
64       switch ((*msg)[num_msg].msg_style)
65         {
66         case PAM_PROMPT_ECHO_ON:
67           temp_resp[num_msg].resp = strdup(data->name);
68           break;
69         case PAM_PROMPT_ECHO_OFF:
70           temp_resp[num_msg].resp = strdup(data->pw);
71           break;
72         default:
73           temp_resp[num_msg].resp = NULL;
74         }
75       temp_resp[num_msg].resp_retcode = 0;
76     }
77
78   *resp = temp_resp;
79   return PAM_SUCCESS;
80 }
81 #endif /* HAVE_PAM_START */
82
83
84 /*
85  * validpw(): check that `pass' is the correct password for `uid'
86  *            returns zero if no, nonzero if yes
87  */
88
89 int validpw(uid_t uid, const char *pass)
90 {
91 #ifdef HAVE_PAM_START
92         struct pam_conv pc;
93         struct appdata data;
94         pam_handle_t *ph;
95         int i;
96 #else
97         char *crypted_pwd;
98 #ifdef HAVE_GETSPNAM
99         struct spwd *sp;
100 #endif
101 #endif
102         struct passwd *pw;
103         int retval = 0;
104         int flags = 0;
105
106 #ifdef PAM_DATA_SILENT
107         flags = flags | PAM_DATA_SILENT
108 #endif /* PAM_DATA_SILENT */
109             if ((pw = getpwuid(uid)) == NULL)
110                 return retval;
111
112 #ifdef HAVE_PAM_START
113         pc.conv = conv;
114         pc.appdata_ptr = &data;
115         data.name = pw->pw_name;
116         data.pw = pass;
117         if (pam_start("citadel", pw->pw_name, &pc, &ph) != PAM_SUCCESS)
118                 return retval;
119
120         if ((i = pam_authenticate(ph, flags)) == PAM_SUCCESS)
121                 if ((i = pam_acct_mgmt(ph, flags)) == PAM_SUCCESS)
122                         retval = -1;
123
124         pam_end(ph, i | flags);
125 #else
126         crypted_pwd = pw->pw_passwd;
127
128 #ifdef HAVE_GETSPNAM
129         if ((sp = getspnam(pw->pw_name)) != NULL)
130                 crypted_pwd = sp->sp_pwdp;
131 #endif
132
133         if (!strcmp(crypt(pass, crypted_pwd), crypted_pwd))
134                 retval = -1;
135 #endif                          /* HAVE_PAM_START */
136
137         return retval;
138 }
139