]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_pam.c
implemented config.h
[netatalk.git] / etc / uams / uams_dhx_pam.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu) 
4  * All Rights Reserved.  See COPYRIGHT.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #if defined(USE_PAM) && defined(UAM_DHX)
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <syslog.h>
16
17 #include <security/pam_appl.h>
18
19 #include <bn.h>
20 #include <dh.h>
21 #include <cast.h>
22
23 #include <atalk/afp.h>
24 #include <atalk/uam.h>
25
26 #define KEYSIZE 16
27 #define PASSWDLEN 64
28 #define CRYPTBUFLEN  (KEYSIZE*2)
29 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
30 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
31
32 /* hash a number to a 16-bit quantity */
33 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
34                      (unsigned long) (a)) & 0xffff)
35
36 /* the secret key */
37 static CAST_KEY castkey;
38 static struct passwd *dhxpwd;
39 static u_int8_t randbuf[KEYSIZE];
40
41 /* diffie-hellman bits */
42 static unsigned char msg2_iv[] = "CJalbert";
43 static unsigned char msg3_iv[] = "LWallace";
44 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
45                              0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
46 static const u_int8_t g = 0x07;
47
48
49 /* Static variables used to communicate between the conversation function
50  * and the server_login function
51  */
52 static pam_handle_t *pamh = NULL; 
53 static char *PAM_username;
54 static char *PAM_password;
55
56 /* PAM conversation function
57  * Here we assume (for now, at least) that echo on means login name, and
58  * echo off means password.
59  */
60 static int PAM_conv (int num_msg,
61                      const struct pam_message **msg,
62                      struct pam_response **resp,
63                      void *appdata_ptr) {
64   int count = 0;
65   struct pam_response *reply;
66   
67 #define COPY_STRING(s) (s) ? strdup(s) : NULL
68   
69   if (num_msg < 1)
70     return PAM_CONV_ERR;
71
72   reply = (struct pam_response *) 
73     calloc(num_msg, sizeof(struct pam_response));
74
75   if (!reply)
76     return PAM_CONV_ERR;
77
78   for (count = 0; count < num_msg; count++) {
79     char *string = NULL;
80
81     switch (msg[count]->msg_style) {
82     case PAM_PROMPT_ECHO_ON:
83       if (!(string = COPY_STRING(PAM_username)))
84         goto pam_fail_conv;
85       break;
86     case PAM_PROMPT_ECHO_OFF:
87       if (!(string = COPY_STRING(PAM_password)))
88         goto pam_fail_conv;
89       break;
90     case PAM_TEXT_INFO:
91 #ifdef PAM_BINARY_PROMPT
92     case PAM_BINARY_PROMPT:
93 #endif
94       /* ignore it... */
95       break;
96     case PAM_ERROR_MSG:
97     default:
98       goto pam_fail_conv;
99     }
100
101     if (string) {  
102       reply[count].resp_retcode = 0;
103       reply[count].resp = string;
104       string = NULL;
105     }
106   }
107
108   *resp = reply;
109   return PAM_SUCCESS;
110
111 pam_fail_conv:
112   for (count = 0; count < num_msg; count++) {
113     if (!reply[count].resp)
114         continue;
115     switch (msg[count]->msg_style) {
116     case PAM_PROMPT_ECHO_OFF:
117     case PAM_PROMPT_ECHO_ON:
118       free(reply[count].resp);
119       break;      
120     }
121   }
122   free(reply);
123   return PAM_CONV_ERR;
124 }
125
126 static struct pam_conv PAM_conversation = {
127   &PAM_conv,
128   NULL
129 };
130
131
132 static int dhx_setup(void *obj, char *ibuf, int ibuflen, 
133                      char *rbuf, int *rbuflen)
134 {
135     u_int16_t sessid;
136     int i;
137     BIGNUM *bn, *gbn, *pbn;
138     DH *dh;
139
140     /* get the client's public key */
141     if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL)))
142       return AFPERR_PARAM;
143
144     /* get our primes */
145     if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
146       BN_clear_free(bn);
147       return AFPERR_PARAM;
148     }
149
150     if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
151       BN_free(gbn);
152       BN_clear_free(bn);
153       return AFPERR_PARAM;
154     }
155
156     /* okay, we're ready */
157     if (!(dh = DH_new())) {
158       BN_free(pbn);
159       BN_free(gbn);
160       BN_clear_free(bn);
161       return AFPERR_PARAM;
162     }
163
164     /* generate key and make sure that we have enough space */
165     dh->p = pbn;
166     dh->g = gbn;
167     if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
168       goto pam_fail;
169     }
170
171     /* figure out the key. store the key in rbuf for now. */
172     i = DH_compute_key(rbuf, bn, dh);
173     
174     /* set the key */
175     CAST_set_key(&castkey, i, rbuf);
176     
177     /* session id. it's just a hashed version of the object pointer. */
178     sessid = dhxhash(obj);
179     memcpy(rbuf, &sessid, sizeof(sessid));
180     rbuf += sizeof(sessid);
181     *rbuflen += sizeof(sessid);
182     
183     /* public key */
184     BN_bn2bin(dh->pub_key, rbuf); 
185     rbuf += KEYSIZE;
186     *rbuflen += KEYSIZE;
187
188     /* buffer to be encrypted */
189     i = sizeof(randbuf);
190     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
191                              &i) < 0) {
192       *rbuflen = 0;
193       goto pam_fail;
194     }    
195     memcpy(rbuf, &randbuf, sizeof(randbuf));
196
197     /* get the signature. it's always 16 bytes. */
198 #if 0
199     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
200                              (void *) &buf, NULL) < 0) {
201       *rbuflen = 0;
202       goto pam_fail;
203     }
204     memcpy(rbuf + KEYSIZE, buf, KEYSIZE); 
205 #else
206     memset(rbuf + KEYSIZE, 0, KEYSIZE); 
207 #endif
208
209     /* encrypt using cast */
210     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv, 
211                      CAST_ENCRYPT);
212     *rbuflen += CRYPTBUFLEN;
213     BN_free(bn);
214     DH_free(dh);
215     return AFPERR_AUTHCONT;
216
217 pam_fail:
218     BN_free(bn);
219     DH_free(dh);
220     return AFPERR_PARAM;
221 }
222
223
224 /* dhx login: things are done in a slightly bizarre order to avoid
225  * having to clean things up if there's an error. */
226 static int pam_login(void *obj, struct passwd **uam_pwd,
227                      char *ibuf, int ibuflen,
228                      char *rbuf, int *rbuflen)
229 {
230     char *buf;
231     int len, i;
232
233     *rbuflen = 0;
234
235     /* grab some of the options */
236     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
237                              &i) < 0)
238       return AFPERR_PARAM;
239
240     len = (unsigned char) *ibuf++;
241     if ( len > i ) {
242         return( AFPERR_PARAM );
243     }
244
245     memcpy(buf, ibuf, len );
246     ibuf += len;
247     buf[ len ] = '\0';
248     if ((unsigned long) ibuf & 1) /* pad to even boundary */
249       ++ibuf;
250
251     if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
252         return AFPERR_PARAM;
253     }
254
255     PAM_username = buf;
256     syslog( LOG_INFO, "dhx login: %s", buf);
257     return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
258 }
259
260
261 static int pam_logincont(void *obj, struct passwd **uam_pwd,
262                          char *ibuf, int ibuflen, 
263                          char *rbuf, int *rbuflen)
264 {
265     char *hostname;
266     BIGNUM *bn1, *bn2, *bn3;
267     u_int16_t sessid;
268     int err, PAM_error;
269
270     *rbuflen = 0;
271
272     /* check for session id */
273     memcpy(&sessid, ibuf, sizeof(sessid));
274     if (sessid != dhxhash(obj))
275       return AFPERR_PARAM;
276     ibuf += sizeof(sessid);
277     
278     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
279                              (void *) &hostname, NULL) < 0)
280       return AFPERR_MISC;
281
282     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
283                      msg3_iv, CAST_DECRYPT);
284     memset(&castkey, 0, sizeof(castkey));
285
286     /* check to make sure that the random number is the same. we
287      * get sent back an incremented random number. */
288     if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
289       return AFPERR_PARAM;
290
291     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
292       BN_free(bn1);
293       return AFPERR_PARAM;
294     }
295       
296     /* zero out the random number */
297     memset(rbuf, 0, sizeof(randbuf));
298     memset(randbuf, 0, sizeof(randbuf));
299     rbuf += KEYSIZE;
300
301     if (!(bn3 = BN_new())) {
302       BN_free(bn2);
303       BN_free(bn1);
304       return AFPERR_PARAM;
305     }
306
307     BN_sub(bn3, bn1, bn2);
308     BN_free(bn2);
309     BN_free(bn1);
310
311     /* okay. is it one more? */
312     if (!BN_is_one(bn3)) {
313       BN_free(bn3);
314       return AFPERR_PARAM;
315     }
316     BN_free(bn3);
317
318     /* Set these things up for the conv function */
319     rbuf[PASSWDLEN] = '\0';
320     PAM_password = rbuf;
321
322     err = AFPERR_NOTAUTH;
323     PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
324                           &pamh);
325     if (PAM_error != PAM_SUCCESS)
326       goto logincont_err;
327
328     /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
329     pam_set_item(pamh, PAM_TTY, "afpd");
330     pam_set_item(pamh, PAM_RHOST, hostname);
331     PAM_error = pam_authenticate(pamh,0);
332     if (PAM_error != PAM_SUCCESS) {
333       if (PAM_error == PAM_MAXTRIES) 
334         err = AFPERR_PWDEXPR;
335       goto logincont_err;
336     }      
337
338     PAM_error = pam_acct_mgmt(pamh, 0);
339     if (PAM_error != PAM_SUCCESS) {
340       if (PAM_error == PAM_ACCT_EXPIRED)
341         err = AFPERR_PWDEXPR;
342 #ifdef PAM_AUTHTOKEN_REQD
343       else if (PAM_error == PAM_AUTHTOKEN_REQD) 
344         err = AFPERR_PWDCHNG;
345 #endif
346       goto logincont_err;
347     }
348
349 #ifndef PAM_CRED_ESTABLISH
350 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
351 #endif
352     PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
353     if (PAM_error != PAM_SUCCESS)
354       goto logincont_err;
355
356     PAM_error = pam_open_session(pamh, 0);
357     if (PAM_error != PAM_SUCCESS)
358       goto logincont_err;
359
360     memset(rbuf, 0, PASSWDLEN); /* zero out the password */
361     *uam_pwd = dhxpwd;
362     return AFP_OK;
363
364 logincont_err:
365     pam_end(pamh, PAM_error);
366     pamh = NULL;
367     memset(rbuf, 0, CRYPT2BUFLEN);
368     return err;
369 }
370
371 /* logout */
372 static void pam_logout() {
373     pam_close_session(pamh, 0);
374     pam_end(pamh, 0);
375     pamh = NULL;
376 }
377
378
379 /* change pw for dhx needs a couple passes to get everything all
380  * right. basically, it's like the login/logincont sequence */
381 static int pam_changepw(void *obj, char *username,
382                         struct passwd *pwd, char *ibuf, int ibuflen,
383                         char *rbuf, int *rbuflen)
384 {
385     BIGNUM *bn1, *bn2, *bn3;
386
387     char *hostname;
388     pam_handle_t *lpamh;
389     uid_t uid;
390     u_int16_t sessid;
391     int PAM_error;
392
393     /* grab the id */
394     memcpy(&sessid, ibuf, sizeof(sessid));
395     ibuf += sizeof(sessid);
396     
397     if (!sessid) {  /* no sessid -> initialization phase */
398       PAM_username = username;
399       ibuflen -= sizeof(sessid);
400       return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
401     }
402
403
404     /* otherwise, it's like logincont but different. */
405
406     /* check out the session id */
407     if (sessid != dhxhash(obj))
408       return AFPERR_PARAM;
409
410     /* we need this for pam */
411     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
412                              (void *) &hostname, NULL) < 0)
413       return AFPERR_MISC;
414
415     /* grab the client's nonce, old password, and new password. */
416     CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
417                      msg3_iv, CAST_DECRYPT);
418     memset(&castkey, 0, sizeof(castkey));
419
420     /* check to make sure that the random number is the same. we
421      * get sent back an incremented random number. */
422     if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
423       return AFPERR_PARAM;
424
425     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
426       BN_free(bn1);
427       return AFPERR_PARAM;
428     }
429       
430     /* zero out the random number */
431     memset(rbuf, 0, sizeof(randbuf));
432     memset(randbuf, 0, sizeof(randbuf));
433
434     if (!(bn3 = BN_new())) {
435       BN_free(bn2);
436       BN_free(bn1);
437       return AFPERR_PARAM;
438     }
439
440     BN_sub(bn3, bn1, bn2);
441     BN_free(bn2);
442     BN_free(bn1);
443
444     /* okay. is it one more? */
445 #if 0
446     if (!BN_is_one(bn3)) {
447       BN_free(bn3);
448       return AFPERR_PARAM;
449     }
450 #endif
451     BN_free(bn3);
452
453     /* Set these things up for the conv function. the old password
454      * is at the end. */
455     ibuf += KEYSIZE;
456     ibuf[PASSWDLEN + PASSWDLEN] = '\0';
457     PAM_password = ibuf + PASSWDLEN;
458
459     PAM_error = pam_start("netatalk", username, &PAM_conversation,
460                           &lpamh);
461     if (PAM_error != PAM_SUCCESS) 
462       return AFPERR_PARAM;
463     pam_set_item(lpamh, PAM_TTY, "afpd");
464     pam_set_item(lpamh, PAM_RHOST, hostname);
465
466     /* we might need to do this as root */
467     uid = geteuid();
468     seteuid(0);
469     PAM_error = pam_authenticate(lpamh, 0);
470     if (PAM_error != PAM_SUCCESS) {
471       seteuid(uid);
472       pam_end(lpamh, PAM_error);
473       return AFPERR_NOTAUTH;
474     }
475
476     /* clear out old passwd */
477     memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
478
479     /* new password */
480     PAM_password = ibuf;
481     ibuf[PASSWDLEN] = '\0';
482
483     /* this really does need to be done as root */
484     PAM_error = pam_chauthtok(lpamh, 0);
485     seteuid(uid); /* un-root ourselves. */
486     memset(ibuf, 0, PASSWDLEN);
487     if (PAM_error != PAM_SUCCESS) {
488       pam_end(lpamh, PAM_error);
489       return AFPERR_ACCESS;
490     }
491
492     pam_end(lpamh, 0);
493     return AFP_OK;
494 }
495
496
497 static int uam_setup(const char *path)
498 {
499   if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login, 
500                    pam_logincont, pam_logout) < 0)
501     return -1;
502
503   if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128", 
504                    pam_changepw) < 0) {
505     uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
506     return -1;
507   }
508
509   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
510     pam_printer);*/
511
512   return 0;
513 }
514
515 static void uam_cleanup(void)
516 {
517   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
518   uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
519   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
520 }
521
522 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
523   UAM_MODULE_SERVER,
524   UAM_MODULE_VERSION,
525   uam_setup, uam_cleanup
526 };
527
528 #endif /* USE_PAM && UAM_DHX */