]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_pam.c
Correctly handle errors in DH_generate_key(). Partial fix for bug #1079670.
[netatalk.git] / etc / uams / uams_dhx_pam.c
1 /*
2  * $Id: uams_dhx_pam.c,v 1.26 2008-12-02 18:24:55 morgana Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu) 
6  * All Rights Reserved.  See COPYRIGHT.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #if defined(USE_PAM) && defined(UAM_DHX)
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <atalk/logger.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif /* HAVE_UNISTD_H */
22 #include <errno.h>
23 #ifdef HAVE_SECURITY_PAM_APPL_H
24 #include <security/pam_appl.h>
25 #endif
26 #ifdef HAVE_PAM_PAM_APPL_H
27 #include <pam/pam_appl.h>
28 #endif
29
30
31 #if defined(GNUTLS_DHX)
32 #include <gnutls/openssl.h>
33 #elif defined(OPENSSL_DHX)
34 #include <openssl/bn.h>
35 #include <openssl/dh.h>
36 #include <openssl/cast.h>
37 #ifdef sun
38 #include <openssl/rand.h>
39 #endif
40 #else /* OPENSSL_DHX */
41 #include <bn.h>
42 #include <dh.h>
43 #include <cast.h>
44 #endif /* OPENSSL_DHX */
45
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
48
49 #define KEYSIZE 16
50 #define PASSWDLEN 64
51 #define CRYPTBUFLEN  (KEYSIZE*2)
52 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
53 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
54
55 /* hash a number to a 16-bit quantity */
56 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
57                      (unsigned long) (a)) & 0xffff)
58
59 /* the secret key */
60 static CAST_KEY castkey;
61 static struct passwd *dhxpwd;
62 static u_int8_t randbuf[KEYSIZE];
63
64 /* diffie-hellman bits */
65 static unsigned char msg2_iv[] = "CJalbert";
66 static unsigned char msg3_iv[] = "LWallace";
67 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
68                              0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
69 static const u_int8_t g = 0x07;
70
71
72 /* Static variables used to communicate between the conversation function
73  * and the server_login function
74  */
75 static pam_handle_t *pamh = NULL; 
76 static char *PAM_username;
77 static char *PAM_password;
78
79 /* PAM conversation function
80  * Here we assume (for now, at least) that echo on means login name, and
81  * echo off means password.
82  */
83 static int PAM_conv (int num_msg,
84                      const struct pam_message **msg,
85                      struct pam_response **resp,
86                      void *appdata_ptr _U_) {
87   int count = 0;
88   struct pam_response *reply;
89   
90 #define COPY_STRING(s) (s) ? strdup(s) : NULL
91   
92   errno = 0;
93
94   if (num_msg < 1) {
95     /* Log Entry */
96            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
97                   strerror(errno));
98     /* Log Entry */
99     return PAM_CONV_ERR;
100   }
101
102   reply = (struct pam_response *) 
103     calloc(num_msg, sizeof(struct pam_response));
104
105   if (!reply) {
106     /* Log Entry */
107            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
108                   strerror(errno));
109     /* Log Entry */
110     return PAM_CONV_ERR;
111   }
112
113   for (count = 0; count < num_msg; count++) {
114     char *string = NULL;
115
116     switch (msg[count]->msg_style) {
117     case PAM_PROMPT_ECHO_ON:
118       if (!(string = COPY_STRING(PAM_username))) {
119     /* Log Entry */
120            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
121                   strerror(errno));
122     /* Log Entry */
123         goto pam_fail_conv;
124       }
125       break;
126     case PAM_PROMPT_ECHO_OFF:
127       if (!(string = COPY_STRING(PAM_password))) {
128     /* Log Entry */
129            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
130                   strerror(errno));
131     /* Log Entry */
132         goto pam_fail_conv;
133       }
134       break;
135     case PAM_TEXT_INFO:
136 #ifdef PAM_BINARY_PROMPT
137     case PAM_BINARY_PROMPT:
138 #endif /* PAM_BINARY_PROMPT */
139       /* ignore it... */
140       break;
141     case PAM_ERROR_MSG:
142     default:
143     /* Log Entry */
144            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
145                   strerror(errno));
146     /* Log Entry */
147       goto pam_fail_conv;
148     }
149
150     if (string) {  
151       reply[count].resp_retcode = 0;
152       reply[count].resp = string;
153       string = NULL;
154     }
155   }
156
157   *resp = reply;
158     /* Log Entry */
159            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
160     /* Log Entry */
161   return PAM_SUCCESS;
162
163 pam_fail_conv:
164   for (count = 0; count < num_msg; count++) {
165     if (!reply[count].resp)
166         continue;
167     switch (msg[count]->msg_style) {
168     case PAM_PROMPT_ECHO_OFF:
169     case PAM_PROMPT_ECHO_ON:
170       free(reply[count].resp);
171       break;      
172     }
173   }
174   free(reply);
175     /* Log Entry */
176            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
177                   strerror(errno));
178     /* Log Entry */
179     return PAM_CONV_ERR;
180 }
181
182 static struct pam_conv PAM_conversation = {
183   &PAM_conv,
184   NULL
185 };
186
187
188 static int dhx_setup(void *obj, char *ibuf, int ibuflen _U_, 
189                      char *rbuf, int *rbuflen)
190 {
191     u_int16_t sessid;
192     int i;
193     BIGNUM *bn, *gbn, *pbn;
194     DH *dh;
195
196     /* TODO: seed dhx_setup properly... this is a hack */
197 #ifdef sun
198         /* *SEVERE* hack... fix */
199         RAND_load_file("/var/adm/messages", KEYSIZE);
200 #endif /* sun */
201
202     /* get the client's public key */
203     if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
204     /* Log Entry */
205            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
206                   strerror(errno));
207     /* Log Entry */
208       return AFPERR_PARAM;
209     }
210
211     /* get our primes */
212     if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
213       BN_clear_free(bn);
214     /* Log Entry */
215            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
216                   strerror(errno));
217     /* Log Entry */
218       return AFPERR_PARAM;
219     }
220
221     if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
222       BN_free(gbn);
223       BN_clear_free(bn);
224     /* Log Entry */
225            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
226                   strerror(errno));
227     /* Log Entry */
228       return AFPERR_PARAM;
229     }
230
231     /* okay, we're ready */
232     if (!(dh = DH_new())) {
233       BN_free(pbn);
234       BN_free(gbn);
235       BN_clear_free(bn);
236     /* Log Entry */
237            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
238                   strerror(errno));
239     /* Log Entry */
240       return AFPERR_PARAM;
241     }
242
243     /* generate key and make sure that we have enough space */
244     dh->p = pbn;
245     dh->g = gbn;
246     if (DH_generate_key(dh) == 0) {
247         unsigned long dherror;
248         char errbuf[256];
249
250         ERR_load_crypto_strings();
251         dherror = ERR_get_error();
252         ERR_error_string_n(dherror, errbuf, 256);
253
254         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key (OpenSSL error code: %u, %s)", dherror, errbuf);
255
256         ERR_free_strings();
257         goto pam_fail;
258     }
259     if (BN_num_bytes(dh->pub_key) > KEYSIZE) {
260         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s", strerror(errno));
261         goto pam_fail;
262     }
263
264     /* figure out the key. store the key in rbuf for now. */
265     i = DH_compute_key(rbuf, bn, dh);
266     
267     /* set the key */
268     CAST_set_key(&castkey, i, rbuf);
269     
270     /* session id. it's just a hashed version of the object pointer. */
271     sessid = dhxhash(obj);
272     memcpy(rbuf, &sessid, sizeof(sessid));
273     rbuf += sizeof(sessid);
274     *rbuflen += sizeof(sessid);
275     
276     /* public key */
277     BN_bn2bin(dh->pub_key, rbuf); 
278     rbuf += KEYSIZE;
279     *rbuflen += KEYSIZE;
280
281     /* buffer to be encrypted */
282     i = sizeof(randbuf);
283     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
284                              &i) < 0) {
285       *rbuflen = 0;
286     /* Log Entry */
287            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
288                   strerror(errno));
289     /* Log Entry */
290       goto pam_fail;
291     }    
292     memcpy(rbuf, &randbuf, sizeof(randbuf));
293
294     /* get the signature. it's always 16 bytes. */
295 #if 0
296     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
297                              (void *) &buf, NULL) < 0) {
298       *rbuflen = 0;
299     /* Log Entry */
300            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
301                   strerror(errno));
302     /* Log Entry */
303       goto pam_fail;
304     }
305     memcpy(rbuf + KEYSIZE, buf, KEYSIZE); 
306 #else /* 0 */
307     memset(rbuf + KEYSIZE, 0, KEYSIZE); 
308 #endif /* 0 */
309
310     /* encrypt using cast */
311     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv, 
312                      CAST_ENCRYPT);
313     *rbuflen += CRYPTBUFLEN;
314     BN_free(bn);
315     DH_free(dh);
316     return AFPERR_AUTHCONT;
317
318 pam_fail:
319     BN_free(bn);
320     DH_free(dh);
321     /* Log Entry */
322            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
323                   strerror(errno));
324     /* Log Entry */
325     return AFPERR_PARAM;
326 }
327
328 /* -------------------------------- */
329 static int login(void *obj, char *username, int ulen,  struct passwd **uam_pwd _U_,
330                      char *ibuf, int ibuflen,
331                      char *rbuf, int *rbuflen)
332 {
333     if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
334         LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
335         return AFPERR_PARAM;
336     }
337
338     PAM_username = username;
339     LOG(log_info, logtype_uams, "dhx login: %s", username);
340     return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
341 }
342
343 /* -------------------------------- */
344 /* dhx login: things are done in a slightly bizarre order to avoid
345  * having to clean things up if there's an error. */
346 static int pam_login(void *obj, struct passwd **uam_pwd,
347                      char *ibuf, int ibuflen,
348                      char *rbuf, int *rbuflen)
349 {
350     char *username;
351     int len, ulen;
352
353     *rbuflen = 0;
354
355     /* grab some of the options */
356     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
357         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username  -- %s",
358                   strerror(errno));
359         return AFPERR_PARAM;
360     }
361
362     len = (unsigned char) *ibuf++;
363     if ( len > ulen ) {
364         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
365                   strerror(errno));
366         return AFPERR_PARAM;
367     }
368
369     memcpy(username, ibuf, len );
370     ibuf += len;
371     username[ len ] = '\0';
372
373     if ((unsigned long) ibuf & 1) /* pad to even boundary */
374       ++ibuf;
375
376     return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
377 }
378
379 /* ----------------------------- */
380 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
381                      char *ibuf, int ibuflen,
382                      char *rbuf, int *rbuflen)
383 {
384     char *username;
385     int len, ulen;
386     u_int16_t  temp16;
387
388     *rbuflen = 0;
389
390     /* grab some of the options */
391     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
392         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username  -- %s",
393                   strerror(errno));
394         return AFPERR_PARAM;
395     }
396
397     if (*uname != 3)
398         return AFPERR_PARAM;
399     uname++;
400     memcpy(&temp16, uname, sizeof(temp16));
401     len = ntohs(temp16);
402
403     if ( !len || len > ulen ) {
404         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
405                   strerror(errno));
406         return AFPERR_PARAM;
407     }
408     memcpy(username, uname +2, len );
409     username[ len ] = '\0';
410
411     return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
412 }
413
414 /* -------------------------------- */
415
416 static int pam_logincont(void *obj, struct passwd **uam_pwd,
417                          char *ibuf, int ibuflen _U_, 
418                          char *rbuf, int *rbuflen)
419 {
420     char *hostname;
421     BIGNUM *bn1, *bn2, *bn3;
422     u_int16_t sessid;
423     int err, PAM_error;
424
425     *rbuflen = 0;
426
427     /* check for session id */
428     memcpy(&sessid, ibuf, sizeof(sessid));
429     if (sessid != dhxhash(obj)) {
430     /* Log Entry */
431            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
432                   strerror(errno));
433     /* Log Entry */
434       return AFPERR_PARAM;
435     }
436     ibuf += sizeof(sessid);
437     
438     if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
439                              (void *) &hostname, NULL) < 0)
440         {
441         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
442         hostname = NULL;
443         }
444
445     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
446                      msg3_iv, CAST_DECRYPT);
447     memset(&castkey, 0, sizeof(castkey));
448
449     /* check to make sure that the random number is the same. we
450      * get sent back an incremented random number. */
451     if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
452       return AFPERR_PARAM;
453
454     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
455       BN_free(bn1);
456       return AFPERR_PARAM;
457     }
458       
459     /* zero out the random number */
460     memset(rbuf, 0, sizeof(randbuf));
461     memset(randbuf, 0, sizeof(randbuf));
462     rbuf += KEYSIZE;
463
464     if (!(bn3 = BN_new())) {
465       BN_free(bn2);
466       BN_free(bn1);
467       return AFPERR_PARAM;
468     }
469
470     BN_sub(bn3, bn1, bn2);
471     BN_free(bn2);
472     BN_free(bn1);
473
474     /* okay. is it one more? */
475     if (!BN_is_one(bn3)) {
476       BN_free(bn3);
477       return AFPERR_PARAM;
478     }
479     BN_free(bn3);
480
481     /* Set these things up for the conv function */
482     rbuf[PASSWDLEN] = '\0';
483     PAM_password = rbuf;
484
485     err = AFPERR_NOTAUTH;
486     PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
487                           &pamh);
488     if (PAM_error != PAM_SUCCESS) {
489     /* Log Entry */
490            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
491                   pam_strerror(pamh,PAM_error));
492     /* Log Entry */
493       goto logincont_err;
494     }
495
496     /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
497     pam_set_item(pamh, PAM_TTY, "afpd");
498     pam_set_item(pamh, PAM_RHOST, hostname);
499     PAM_error = pam_authenticate(pamh,0);
500     if (PAM_error != PAM_SUCCESS) {
501       if (PAM_error == PAM_MAXTRIES) 
502         err = AFPERR_PWDEXPR;
503     /* Log Entry */
504            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
505                   pam_strerror(pamh, PAM_error));
506     /* Log Entry */
507       goto logincont_err;
508     }      
509
510     PAM_error = pam_acct_mgmt(pamh, 0);
511     if (PAM_error != PAM_SUCCESS ) {
512       /* Log Entry */
513       LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
514           pam_strerror(pamh, PAM_error));
515       /* Log Entry */
516       if (PAM_error == PAM_NEW_AUTHTOK_REQD)    /* password expired */
517         err = AFPERR_PWDEXPR;
518 #ifdef PAM_AUTHTOKEN_REQD
519       else if (PAM_error == PAM_AUTHTOKEN_REQD) 
520         err = AFPERR_PWDCHNG;
521 #endif
522       else
523         goto logincont_err;
524     }
525
526 #ifndef PAM_CRED_ESTABLISH
527 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
528 #endif
529     PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
530     if (PAM_error != PAM_SUCCESS) {
531     /* Log Entry */
532            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
533                   pam_strerror(pamh, PAM_error));
534     /* Log Entry */
535       goto logincont_err;
536     }
537
538     PAM_error = pam_open_session(pamh, 0);
539     if (PAM_error != PAM_SUCCESS) {
540     /* Log Entry */
541            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
542                   pam_strerror(pamh, PAM_error));
543     /* Log Entry */
544       goto logincont_err;
545     }
546
547     memset(rbuf, 0, PASSWDLEN); /* zero out the password */
548     *uam_pwd = dhxpwd;
549     /* Log Entry */
550            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
551     /* Log Entry */
552     if ( err == AFPERR_PWDEXPR)
553         return err;
554     return AFP_OK;
555
556 logincont_err:
557     pam_end(pamh, PAM_error);
558     pamh = NULL;
559     memset(rbuf, 0, CRYPT2BUFLEN);
560     return err;
561 }
562
563 /* logout */
564 static void pam_logout() {
565     pam_close_session(pamh, 0);
566     pam_end(pamh, 0);
567     pamh = NULL;
568 }
569
570
571 /* change pw for dhx needs a couple passes to get everything all
572  * right. basically, it's like the login/logincont sequence */
573 static int pam_changepw(void *obj, char *username,
574                         struct passwd *pwd _U_, char *ibuf, int ibuflen,
575                         char *rbuf, int *rbuflen)
576 {
577     BIGNUM *bn1, *bn2, *bn3;
578
579     char *hostname;
580     pam_handle_t *lpamh;
581     uid_t uid;
582     u_int16_t sessid;
583     int PAM_error;
584
585     /* grab the id */
586     memcpy(&sessid, ibuf, sizeof(sessid));
587     ibuf += sizeof(sessid);
588     
589     if (!sessid) {  /* no sessid -> initialization phase */
590       PAM_username = username;
591       ibuflen -= sizeof(sessid);
592       return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
593     }
594
595
596     /* otherwise, it's like logincont but different. */
597
598     /* check out the session id */
599     if (sessid != dhxhash(obj)) {
600     /* Log Entry */
601            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
602                   strerror(errno));
603     /* Log Entry */
604       return AFPERR_PARAM;
605     }
606
607     /* we need this for pam */
608     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
609                              (void *) &hostname, NULL) < 0) {
610     /* Log Entry */
611            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
612                   strerror(errno));
613     /* Log Entry */
614       return AFPERR_MISC;
615     }
616
617     /* grab the client's nonce, old password, and new password. */
618     CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
619                      msg3_iv, CAST_DECRYPT);
620     memset(&castkey, 0, sizeof(castkey));
621
622     /* check to make sure that the random number is the same. we
623      * get sent back an incremented random number. */
624     if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
625     /* Log Entry */
626            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
627                   strerror(errno));
628     /* Log Entry */
629       return AFPERR_PARAM;
630     }
631
632     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
633       BN_free(bn1);
634     /* Log Entry */
635            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
636                   strerror(errno));
637     /* Log Entry */
638       return AFPERR_PARAM;
639     }
640       
641     /* zero out the random number */
642     memset(rbuf, 0, sizeof(randbuf));
643     memset(randbuf, 0, sizeof(randbuf));
644
645     if (!(bn3 = BN_new())) {
646       BN_free(bn2);
647       BN_free(bn1);
648     /* Log Entry */
649            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
650                   strerror(errno));
651     /* Log Entry */
652       return AFPERR_PARAM;
653     }
654
655     BN_sub(bn3, bn1, bn2);
656     BN_free(bn2);
657     BN_free(bn1);
658
659     /* okay. is it one more? */
660 #if 0
661     if (!BN_is_one(bn3)) {
662       BN_free(bn3);
663     /* Log Entry */
664            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
665                   strerror(errno));
666     /* Log Entry */
667       return AFPERR_PARAM;
668     }
669 #endif
670     BN_free(bn3);
671
672     /* Set these things up for the conv function. the old password
673      * is at the end. */
674     ibuf += KEYSIZE;
675     ibuf[PASSWDLEN + PASSWDLEN] = '\0';
676     PAM_password = ibuf + PASSWDLEN;
677
678     PAM_error = pam_start("netatalk", username, &PAM_conversation,
679                           &lpamh);
680     if (PAM_error != PAM_SUCCESS) {
681     /* Log Entry */
682            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
683                   strerror(errno));
684     /* Log Entry */
685       return AFPERR_PARAM;
686     }
687     pam_set_item(lpamh, PAM_TTY, "afpd");
688     pam_set_item(lpamh, PAM_RHOST, hostname);
689
690     /* we might need to do this as root */
691     uid = geteuid();
692     seteuid(0);
693     PAM_error = pam_authenticate(lpamh, 0);
694     if (PAM_error != PAM_SUCCESS) {
695       seteuid(uid);
696       pam_end(lpamh, PAM_error);
697       return AFPERR_NOTAUTH;
698     }
699
700     /* clear out old passwd */
701     memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
702
703     /* new password */
704     PAM_password = ibuf;
705     ibuf[PASSWDLEN] = '\0';
706
707     /* this really does need to be done as root */
708     PAM_error = pam_chauthtok(lpamh, 0);
709     seteuid(uid); /* un-root ourselves. */
710     memset(ibuf, 0, PASSWDLEN);
711     if (PAM_error != PAM_SUCCESS) {
712       pam_end(lpamh, PAM_error);
713       return AFPERR_ACCESS;
714     }
715
716     pam_end(lpamh, 0);
717     return AFP_OK;
718 }
719
720
721 static int uam_setup(const char *path)
722 {
723   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login, 
724                    pam_logincont, pam_logout, pam_login_ext) < 0)
725     return -1;
726
727   if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128", 
728                    pam_changepw) < 0) {
729     uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
730     return -1;
731   }
732
733   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
734     pam_printer);*/
735
736   return 0;
737 }
738
739 static void uam_cleanup(void)
740 {
741   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
742   uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
743   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
744 }
745
746 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
747   UAM_MODULE_SERVER,
748   UAM_MODULE_VERSION,
749   uam_setup, uam_cleanup
750 };
751
752 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
753   UAM_MODULE_SERVER,
754   UAM_MODULE_VERSION,
755   uam_setup, uam_cleanup
756 };
757
758 #endif /* USE_PAM && UAM_DHX */
759