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