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