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