Allocate less memory
[mspang/libpam-csc.git] / pam_csc.c
1 #define PAM_SM_ACCOUNT
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <sys/time.h>
5 #include <time.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <security/pam_modules.h>
11 #include <security/pam_appl.h>
12 #include <ldap.h>
13 #include <sasl/sasl.h>
14 #include <syslog.h>
15 #include <pwd.h>
16
17 #define PAM_CSC_CSC_BASE_DN         "ou=People,dc=csclub,dc=uwaterloo,dc=ca"
18 #define PAM_CSC_CSCF_URI \
19     "ldaps://eponina.student.cs.uwaterloo.ca" \
20     "ldaps://canadenis.student.cs.uwaterloo.ca"
21 #define PAM_CSC_CSCF_BASE_DN        "dc=student,dc=cs,dc=uwateloo,dc=ca"
22 #define PAM_CSC_CSCF_BIND_DN \
23     "uid=TODO,dc=student,dc=cs,dc=uwaterloo,dc=ca"
24 #define PAM_CSC_CSCF_SASL_USER \
25     "dn:uid=TODO,cn=STUDENT.CS.UWATERLOO.CA,cn=DIGEST-MD5,cn=auth"
26 #define PAM_CSC_CSCF_PASSWORD_FILE  "/etc/security/pam_csc_cscf_password"
27 #define PAM_CSC_CSCF_SASL_REALM     "STUDENT.CS.UWATERLOO.CA"
28 #define PAM_CSC_LDAP_TIMEOUT        5
29 #define PAM_CSC_ALLOWED_USERNAMES   {"nobody"}
30 #define PAM_CSC_EXPIRED_MSG \
31     "*****************************************************************************\n" \
32     "*                                                                           *\n" \
33     "*    Your account has expired - please contact the Computer Science Club    *\n" \
34     "*                                                                           *\n" \
35     "*****************************************************************************\n"
36 #define PAM_CSC_CSCF_DISALLOWED_MSG \
37     "You are not registered as a CS student - login denied."
38
39 #define PAM_CSC_SYSLOG_EXPIRED_WARNING \
40     "(pam_csc): %s was not registered for current term or previous term - denying login\n"
41 #define PAM_CSC_SYSLOG_EXPIRED_ERROR \
42     "(pam_csc): %s was not registered for current term but was registered for previous term - permitting login\n"
43 #define PAM_CSC_SYSLOG_NOT_A_MEMBER \
44     "(pam_csc): %s is not a member account - permitting login\n"
45 #define PAM_CSC_SYSLOG_CSCF_DISALLOWED \
46     "(pam_csc): %s is using a CSCF machine but is not enrolled in CS - denying login\n"
47 #define PAM_CSC_SYSLOG_SASL_UNRECOGNIZED_CALLBACK \
48     "(pam_csc): %ld is not a recognized SASL callback option\n"
49
50 /*
51  * User terms are defined as (3 * year + term) where term is:
52  *   0 = Winter, 1 = Spring, 2 = Fall
53  * Term is a string in the form [f|w|s][year]
54  */
55
56 #define HANDLE_WARN \
57 { \
58     syslog(LOG_AUTHPRIV | LOG_WARNING, "pam_csc generated a warning on line %d of %s\n", __LINE__, __FILE__); \
59     retval = PAM_SUCCESS; \
60     goto cleanup; \
61 }
62
63 #define WARN_ZERO(x) \
64     if( (x) == 0 ) HANDLE_WARN
65
66 #define WARN_NEG1(x) \
67     if( (x) == -1 ) HANDLE_WARN
68
69 #define WARN_PAM(x) \
70     if( (x) != PAM_SUCCESS ) HANDLE_WARN
71
72 #define WARN_LDAP(x) \
73     if( (x) != LDAP_SUCCESS ) HANDLE_WARN
74
75 struct pam_csc_sasl_interact_param
76 {
77     const char* realm;
78     const char* user;
79     char pass[100];
80 };
81 typedef struct pam_csc_sasl_interact_param pam_csc_sasl_interact_param_t;
82
83 int pam_csc_sasl_interact(LDAP* ld, unsigned flags, void* def, void* inter)
84 {
85     pam_csc_sasl_interact_param_t* param = (pam_csc_sasl_interact_param_t*)def;
86     sasl_interact_t* interact = (sasl_interact_t*)interact;
87     while(interact->id != SASL_CB_LIST_END)
88     {
89         switch(interact->id)
90         {
91         case SASL_CB_GETREALM:
92             interact->result = param->realm;
93             break;
94         case SASL_CB_USER:
95             interact->result = param->user;
96             break;
97         case SASL_CB_PASS:
98             interact->result = param->pass;
99             break;
100         default:
101             syslog(LOG_AUTHPRIV | LOG_NOTICE,
102                 PAM_CSC_SYSLOG_SASL_UNRECOGNIZED_CALLBACK, interact->id);
103             interact->result = "";
104             break;
105         }
106         interact->len = strlen(interact->result);
107     }
108
109     return LDAP_SUCCESS;
110 }
111
112 char* pam_csc_escape_ldap_string(const char* src)
113 {
114     char *dst, *dst_ptr;
115     int i;
116
117     if(!(dst = malloc(2 * strlen(src) + 1)))
118         return NULL;
119     dst_ptr = dst;
120
121     for(i = 0; i < strlen(src); i++)
122     {
123         if(src[i] == '*' || src[i] == '(' || src[i] == ')' || src[i] == '\\')
124         {
125             dst_ptr[0] = '\\';
126             dst_ptr++;
127         }
128         dst_ptr[0] = src[i];
129         dst_ptr++;
130     }
131     dst_ptr[0] = '\0';
132
133     return dst;
134 }
135
136 int pam_csc_print_message(pam_handle_t* pamh, char* msg, int style)
137 {
138     int retval = PAM_SUCCESS;
139     const struct pam_conv* conv;
140     struct pam_message message;
141     struct pam_message* messages[1];
142     struct pam_response* response;
143
144     /* output message */
145     WARN_PAM( pam_get_item(pamh, PAM_CONV, (const void**)&conv) )
146     if(!conv || !conv->conv)
147         goto cleanup;
148     messages[0] = &message;
149     message.msg_style = style;
150     message.msg = msg;
151     WARN_PAM( conv->conv(1, (const struct pam_message**)messages, 
152         &response, conv->appdata_ptr) )
153
154 cleanup:
155
156     return retval;
157 }
158
159 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t* pamh, int flags, int argc, const char* argv[])
160 {
161     int retval = PAM_SUCCESS;
162     const char* username;
163     struct passwd* pwd;
164     const char* allowed_usernames[] = PAM_CSC_ALLOWED_USERNAMES;
165     int i;
166     time_t cur_time;
167     struct tm* local_time;
168     int long_term;
169     static const char term_chars[] = {'w', 's', 'f'};
170     char cur_term[6], prev_term[6];
171     LDAP *ld_csc = NULL, *ld_cscf = NULL;
172     bool cscf;
173     FILE* pass_file = NULL;
174     char* username_escaped = NULL;
175     char *filter_csc = NULL, *filter_cscf = NULL;
176     char *attrs_csc[] = {"objectClass", "term", "nonMemberTerm", NULL},
177         *attrs_cscf[] = {"objectClass", NULL};
178     bool expired;
179     const char* pam_rhost;
180     int msg_csc, msg_cscf;
181     LDAPMessage *res_csc = NULL, *res_cscf = NULL;
182     struct timeval timeout = {PAM_CSC_LDAP_TIMEOUT, 0};
183     LDAPMessage* entry = NULL;
184     char **values = NULL, **nmvalues = NULL, **values_iter = NULL;
185
186     /* determine username */
187     if((pam_get_user(pamh, &username, NULL) != PAM_SUCCESS) || !username)
188     {
189         return PAM_USER_UNKNOWN;
190     }
191
192     /* check uid range */
193     pwd = getpwnam(username);
194     if(pwd)
195     {
196         /* these ranges are taken from puppet/documents/id-range */
197         if(pwd->pw_uid < 500 || (pwd->pw_uid >= 1000 && pwd->pw_uid < 10000))
198         {
199             return PAM_SUCCESS;
200         }
201     }
202
203     /* check username */
204     for(i = 0; i < sizeof(allowed_usernames) / sizeof(char*); i++)
205     {
206         if(strcmp(allowed_usernames[i], username) == 0)
207         {
208             return PAM_SUCCESS;
209         }
210     }
211
212     /* escape username */
213     WARN_ZERO( username_escaped = pam_csc_escape_ldap_string(username) );
214
215     /* get term info and compute current and previous term */
216     WARN_NEG1( cur_time = time(NULL) )
217     WARN_ZERO( local_time = localtime(&cur_time) )
218     long_term = 3 * (1900 + local_time->tm_year) + (local_time->tm_mon / 4);
219     sprintf(cur_term, "%c%d", term_chars[long_term % 3], long_term / 3);
220     long_term--;
221     sprintf(prev_term, "%c%d", term_chars[long_term % 3], long_term / 3);
222
223     /* connect to CSC */
224     WARN_LDAP( ldap_create(&ld_csc) )
225     WARN_NEG1( ldap_simple_bind(ld_csc, NULL, NULL) )
226
227     /* check if we are logging in from a CSCF teaching thin client */
228     cscf = false;
229     if(pam_get_item(pamh, PAM_RHOST, (const void**)&pam_rhost) && pam_rhost)
230     {
231         /* TODO: check if pam_rhost is tcNNN.student.cs */
232     }
233
234     if(cscf)
235     {
236         pam_csc_sasl_interact_param_t interact_param = {
237             PAM_CSC_CSCF_SASL_REALM,
238             PAM_CSC_CSCF_SASL_USER
239         };
240         int ret;
241
242         /* read password file */
243         WARN_ZERO( pass_file = fopen(PAM_CSC_CSCF_PASSWORD_FILE, "r") )
244         ret = fread(interact_param.pass, sizeof(char), 
245             sizeof(interact_param.pass) - 1, pass_file);
246         interact_param.pass[ret] = '\0';
247         if(ret && interact_param.pass[ret - 1] == '\n')
248             interact_param.pass[ret - 1] = '\0';
249         fclose(pass_file); pass_file = NULL;
250
251         /* connect to CSCF */
252         WARN_LDAP( ldap_initialize(&ld_cscf, PAM_CSC_CSCF_URI) )
253         WARN_NEG1( ldap_sasl_interactive_bind_s(ld_cscf, PAM_CSC_CSCF_BIND_DN,
254             "DIGEST-MD5", NULL, NULL, LDAP_SASL_INTERACTIVE | LDAP_SASL_QUIET,
255             pam_csc_sasl_interact, &interact_param) )
256     }
257
258     /* create CSC request string */
259     WARN_ZERO( filter_csc = malloc(140 + strlen(username_escaped)) )
260     sprintf(filter_csc, "(&(uid=%s)(|(&(objectClass=member)(|(term=%s)(term=%s)(nonMemberTerm=%s)(nonMemberTerm=%s)))(!(objectClass=member))))", username_escaped, cur_term, prev_term, cur_term, prev_term);
261
262     /* issue CSC request */
263     WARN_NEG1( msg_csc = ldap_search(ld_csc, PAM_CSC_CSC_BASE_DN, 
264         LDAP_SCOPE_SUBTREE, filter_csc, attrs_csc, 0) )
265
266     if(cscf)
267     {
268         /* create CSCF request string */
269         WARN_ZERO( filter_cscf = malloc(100 + strlen(username_escaped)) )
270         sprintf(filter_csc, "TODO %s", username_escaped);
271
272         /* issue CSCF request */
273         WARN_NEG1( msg_cscf = ldap_search(ld_cscf, PAM_CSC_CSCF_BASE_DN, 
274             LDAP_SCOPE_SUBTREE, filter_cscf, attrs_cscf, 1) )
275     }
276
277     /* wait for CSC response */
278     WARN_NEG1( ldap_result(ld_csc, msg_csc, 1, &timeout, &res_csc) )
279
280     /* check if we received an entry from CSC */
281     if(ldap_count_entries(ld_csc, res_csc) == 0)
282     {
283         /* show notice and disallow login */
284         pam_csc_print_message(pamh, PAM_CSC_EXPIRED_MSG, PAM_ERROR_MSG);
285         syslog(LOG_AUTHPRIV | LOG_NOTICE, PAM_CSC_SYSLOG_EXPIRED_WARNING, 
286             username);
287         retval = PAM_AUTH_ERR;
288         goto cleanup;
289     }
290
291     /* get CSC entry */
292     WARN_ZERO( entry = ldap_first_entry(ld_csc, res_csc) )
293     values = ldap_get_values(ld_csc, entry, "term");
294     if(!values)
295     {
296         syslog(LOG_AUTHPRIV | LOG_NOTICE, PAM_CSC_SYSLOG_NOT_A_MEMBER, 
297             username);
298         retval = PAM_SUCCESS;
299         goto cleanup;
300     }
301
302     nmvalues = ldap_get_values(ld_csc, entry, "nonMemberTerm");
303
304     /* iterate through term attributes */
305     expired = true;
306     values_iter = values;
307     while(*values_iter)
308     {
309         if(strcmp(*values_iter, cur_term) == 0)
310         {
311             /* user is registered in current term */
312             expired = false;
313             break;
314         }
315         values_iter++;
316     }
317     if (nmvalues) {
318         values_iter = nmvalues;
319         while (*values_iter) {
320             if (strcmp(*values_iter, cur_term) == 0) {
321                 expired = false;
322                 break;
323             }
324             values_iter++;
325         }
326     }
327
328     /* check if account is expired */
329     if(expired)
330     {
331         /* show notice and continue */
332         pam_csc_print_message(pamh, PAM_CSC_EXPIRED_MSG, PAM_TEXT_INFO);
333         syslog(LOG_AUTHPRIV | LOG_NOTICE, PAM_CSC_SYSLOG_EXPIRED_ERROR, 
334             username);
335     }
336
337     if(cscf)
338     {
339         /* wait for CSCF response */
340         WARN_NEG1( ldap_result(ld_cscf, msg_cscf, 1, &timeout, &res_cscf) )
341
342         /* check if we got an entry back from CSCF */
343         if(ldap_count_entries(ld_cscf, res_cscf) == 0)
344         {
345             /* output CSCF disallowed message */
346             pam_csc_print_message(pamh, PAM_CSC_CSCF_DISALLOWED_MSG, 
347                 PAM_ERROR_MSG);
348             syslog(LOG_AUTHPRIV | LOG_NOTICE, PAM_CSC_SYSLOG_CSCF_DISALLOWED, 
349                 username);
350             retval = PAM_AUTH_ERR;
351             goto cleanup;
352         }
353     }
354
355 cleanup:
356
357     if(values) ldap_value_free(values);
358     if(nmvalues) ldap_value_free(nmvalues);
359     if(res_csc) ldap_msgfree(res_csc);
360     if(res_cscf) ldap_msgfree(res_cscf);
361     if(ld_csc) ldap_unbind(ld_csc);
362     if(ld_cscf) ldap_unbind(ld_cscf);
363     if(filter_csc) free(filter_csc);
364     if(filter_cscf) free(filter_cscf);
365     if(username_escaped) free(username_escaped);
366
367     return retval;
368 }