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