]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_pam.c
249cc7318b96885abb5436d95dc749ed9f8257bd
[netatalk.git] / etc / uams / uams_dhx_pam.c
1 /*
2  * $Id: uams_dhx_pam.c,v 1.24.6.5.2.1 2005-09-27 10:40:41 didg 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) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
247     /* Log Entry */
248            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s",
249                   strerror(errno));
250     /* Log Entry */
251     goto pam_fail;
252     }
253
254     /* figure out the key. store the key in rbuf for now. */
255     i = DH_compute_key(rbuf, bn, dh);
256     
257     /* set the key */
258     CAST_set_key(&castkey, i, rbuf);
259     
260     /* session id. it's just a hashed version of the object pointer. */
261     sessid = dhxhash(obj);
262     memcpy(rbuf, &sessid, sizeof(sessid));
263     rbuf += sizeof(sessid);
264     *rbuflen += sizeof(sessid);
265     
266     /* public key */
267     BN_bn2bin(dh->pub_key, rbuf); 
268     rbuf += KEYSIZE;
269     *rbuflen += KEYSIZE;
270
271     /* buffer to be encrypted */
272     i = sizeof(randbuf);
273     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
274                              &i) < 0) {
275       *rbuflen = 0;
276     /* Log Entry */
277            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
278                   strerror(errno));
279     /* Log Entry */
280       goto pam_fail;
281     }    
282     memcpy(rbuf, &randbuf, sizeof(randbuf));
283
284     /* get the signature. it's always 16 bytes. */
285 #if 0
286     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
287                              (void *) &buf, NULL) < 0) {
288       *rbuflen = 0;
289     /* Log Entry */
290            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
291                   strerror(errno));
292     /* Log Entry */
293       goto pam_fail;
294     }
295     memcpy(rbuf + KEYSIZE, buf, KEYSIZE); 
296 #else /* 0 */
297     memset(rbuf + KEYSIZE, 0, KEYSIZE); 
298 #endif /* 0 */
299
300     /* encrypt using cast */
301     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv, 
302                      CAST_ENCRYPT);
303     *rbuflen += CRYPTBUFLEN;
304     BN_free(bn);
305     DH_free(dh);
306     return AFPERR_AUTHCONT;
307
308 pam_fail:
309     BN_free(bn);
310     DH_free(dh);
311     /* Log Entry */
312            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
313                   strerror(errno));
314     /* Log Entry */
315     return AFPERR_PARAM;
316 }
317
318 /* -------------------------------- */
319 static int login(void *obj, char *username, int ulen,  struct passwd **uam_pwd _U_,
320                      char *ibuf, int ibuflen,
321                      char *rbuf, int *rbuflen)
322 {
323     if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
324         LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
325         return AFPERR_PARAM;
326     }
327
328     PAM_username = username;
329     LOG(log_info, logtype_uams, "dhx login: %s", username);
330     return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
331 }
332
333 /* -------------------------------- */
334 /* dhx login: things are done in a slightly bizarre order to avoid
335  * having to clean things up if there's an error. */
336 static int pam_login(void *obj, struct passwd **uam_pwd,
337                      char *ibuf, int ibuflen,
338                      char *rbuf, int *rbuflen)
339 {
340     char *username;
341     int len, ulen;
342
343     *rbuflen = 0;
344
345     /* grab some of the options */
346     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
347         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username  -- %s",
348                   strerror(errno));
349         return AFPERR_PARAM;
350     }
351
352     len = (unsigned char) *ibuf++;
353     if ( len > ulen ) {
354         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
355                   strerror(errno));
356         return AFPERR_PARAM;
357     }
358
359     memcpy(username, ibuf, len );
360     ibuf += len;
361     username[ len ] = '\0';
362
363     if ((unsigned long) ibuf & 1) /* pad to even boundary */
364       ++ibuf;
365
366     return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
367 }
368
369 /* ----------------------------- */
370 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
371                      char *ibuf, int ibuflen,
372                      char *rbuf, int *rbuflen)
373 {
374     char *username;
375     int len, ulen;
376     u_int16_t  temp16;
377
378     *rbuflen = 0;
379
380     /* grab some of the options */
381     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
382         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username  -- %s",
383                   strerror(errno));
384         return AFPERR_PARAM;
385     }
386
387     if (*uname != 3)
388         return AFPERR_PARAM;
389     uname++;
390     memcpy(&temp16, uname, sizeof(temp16));
391     len = ntohs(temp16);
392
393     if ( !len || len > ulen ) {
394         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
395                   strerror(errno));
396         return AFPERR_PARAM;
397     }
398     memcpy(username, uname +2, len );
399     username[ len ] = '\0';
400
401     return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
402 }
403
404 /* -------------------------------- */
405
406 static int pam_logincont(void *obj, struct passwd **uam_pwd,
407                          char *ibuf, int ibuflen _U_, 
408                          char *rbuf, int *rbuflen)
409 {
410     char *hostname;
411     BIGNUM *bn1, *bn2, *bn3;
412     u_int16_t sessid;
413     int err, PAM_error;
414
415     *rbuflen = 0;
416
417     /* check for session id */
418     memcpy(&sessid, ibuf, sizeof(sessid));
419     if (sessid != dhxhash(obj)) {
420     /* Log Entry */
421            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
422                   strerror(errno));
423     /* Log Entry */
424       return AFPERR_PARAM;
425     }
426     ibuf += sizeof(sessid);
427     
428     if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
429                              (void *) &hostname, NULL) < 0)
430         {
431         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
432         hostname = NULL;
433         }
434
435     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
436                      msg3_iv, CAST_DECRYPT);
437     memset(&castkey, 0, sizeof(castkey));
438
439     /* check to make sure that the random number is the same. we
440      * get sent back an incremented random number. */
441     if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
442       return AFPERR_PARAM;
443
444     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
445       BN_free(bn1);
446       return AFPERR_PARAM;
447     }
448       
449     /* zero out the random number */
450     memset(rbuf, 0, sizeof(randbuf));
451     memset(randbuf, 0, sizeof(randbuf));
452     rbuf += KEYSIZE;
453
454     if (!(bn3 = BN_new())) {
455       BN_free(bn2);
456       BN_free(bn1);
457       return AFPERR_PARAM;
458     }
459
460     BN_sub(bn3, bn1, bn2);
461     BN_free(bn2);
462     BN_free(bn1);
463
464     /* okay. is it one more? */
465     if (!BN_is_one(bn3)) {
466       BN_free(bn3);
467       return AFPERR_PARAM;
468     }
469     BN_free(bn3);
470
471     /* Set these things up for the conv function */
472     rbuf[PASSWDLEN] = '\0';
473     PAM_password = rbuf;
474
475     err = AFPERR_NOTAUTH;
476     PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
477                           &pamh);
478     if (PAM_error != PAM_SUCCESS) {
479     /* Log Entry */
480            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
481                   pam_strerror(pamh,PAM_error));
482     /* Log Entry */
483       goto logincont_err;
484     }
485
486     /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
487     pam_set_item(pamh, PAM_TTY, "afpd");
488     pam_set_item(pamh, PAM_RHOST, hostname);
489     PAM_error = pam_authenticate(pamh,0);
490     if (PAM_error != PAM_SUCCESS) {
491       if (PAM_error == PAM_MAXTRIES) 
492         err = AFPERR_PWDEXPR;
493     /* Log Entry */
494            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
495                   pam_strerror(pamh, PAM_error));
496     /* Log Entry */
497       goto logincont_err;
498     }      
499
500     PAM_error = pam_acct_mgmt(pamh, 0);
501     if (PAM_error != PAM_SUCCESS ) {
502       /* Log Entry */
503       LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
504           pam_strerror(pamh, PAM_error));
505       /* Log Entry */
506       if (PAM_error == PAM_NEW_AUTHTOK_REQD)    /* password expired */
507         err = AFPERR_PWDEXPR;
508 #ifdef PAM_AUTHTOKEN_REQD
509       else if (PAM_error == PAM_AUTHTOKEN_REQD) 
510         err = AFPERR_PWDCHNG;
511 #endif
512       else
513         goto logincont_err;
514     }
515
516 #ifndef PAM_CRED_ESTABLISH
517 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
518 #endif
519     PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
520     if (PAM_error != PAM_SUCCESS) {
521     /* Log Entry */
522            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
523                   pam_strerror(pamh, PAM_error));
524     /* Log Entry */
525       goto logincont_err;
526     }
527
528     PAM_error = pam_open_session(pamh, 0);
529     if (PAM_error != PAM_SUCCESS) {
530     /* Log Entry */
531            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
532                   pam_strerror(pamh, PAM_error));
533     /* Log Entry */
534       goto logincont_err;
535     }
536
537     memset(rbuf, 0, PASSWDLEN); /* zero out the password */
538     *uam_pwd = dhxpwd;
539     /* Log Entry */
540            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
541     /* Log Entry */
542     if ( err == AFPERR_PWDEXPR)
543         return err;
544     return AFP_OK;
545
546 logincont_err:
547     pam_end(pamh, PAM_error);
548     pamh = NULL;
549     memset(rbuf, 0, CRYPT2BUFLEN);
550     return err;
551 }
552
553 /* logout */
554 static void pam_logout() {
555     pam_close_session(pamh, 0);
556     pam_end(pamh, 0);
557     pamh = NULL;
558 }
559
560
561 /* change pw for dhx needs a couple passes to get everything all
562  * right. basically, it's like the login/logincont sequence */
563 static int pam_changepw(void *obj, char *username,
564                         struct passwd *pwd _U_, char *ibuf, int ibuflen,
565                         char *rbuf, int *rbuflen)
566 {
567     BIGNUM *bn1, *bn2, *bn3;
568
569     char *hostname;
570     pam_handle_t *lpamh;
571     uid_t uid;
572     u_int16_t sessid;
573     int PAM_error;
574
575     /* grab the id */
576     memcpy(&sessid, ibuf, sizeof(sessid));
577     ibuf += sizeof(sessid);
578     
579     if (!sessid) {  /* no sessid -> initialization phase */
580       PAM_username = username;
581       ibuflen -= sizeof(sessid);
582       return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
583     }
584
585
586     /* otherwise, it's like logincont but different. */
587
588     /* check out the session id */
589     if (sessid != dhxhash(obj)) {
590     /* Log Entry */
591            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
592                   strerror(errno));
593     /* Log Entry */
594       return AFPERR_PARAM;
595     }
596
597     /* we need this for pam */
598     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
599                              (void *) &hostname, NULL) < 0) {
600     /* Log Entry */
601            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
602                   strerror(errno));
603     /* Log Entry */
604       return AFPERR_MISC;
605     }
606
607     /* grab the client's nonce, old password, and new password. */
608     CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
609                      msg3_iv, CAST_DECRYPT);
610     memset(&castkey, 0, sizeof(castkey));
611
612     /* check to make sure that the random number is the same. we
613      * get sent back an incremented random number. */
614     if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
615     /* Log Entry */
616            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
617                   strerror(errno));
618     /* Log Entry */
619       return AFPERR_PARAM;
620     }
621
622     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
623       BN_free(bn1);
624     /* Log Entry */
625            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
626                   strerror(errno));
627     /* Log Entry */
628       return AFPERR_PARAM;
629     }
630       
631     /* zero out the random number */
632     memset(rbuf, 0, sizeof(randbuf));
633     memset(randbuf, 0, sizeof(randbuf));
634
635     if (!(bn3 = BN_new())) {
636       BN_free(bn2);
637       BN_free(bn1);
638     /* Log Entry */
639            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
640                   strerror(errno));
641     /* Log Entry */
642       return AFPERR_PARAM;
643     }
644
645     BN_sub(bn3, bn1, bn2);
646     BN_free(bn2);
647     BN_free(bn1);
648
649     /* okay. is it one more? */
650 #if 0
651     if (!BN_is_one(bn3)) {
652       BN_free(bn3);
653     /* Log Entry */
654            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
655                   strerror(errno));
656     /* Log Entry */
657       return AFPERR_PARAM;
658     }
659 #endif
660     BN_free(bn3);
661
662     /* Set these things up for the conv function. the old password
663      * is at the end. */
664     ibuf += KEYSIZE;
665     ibuf[PASSWDLEN + PASSWDLEN] = '\0';
666     PAM_password = ibuf + PASSWDLEN;
667
668     PAM_error = pam_start("netatalk", username, &PAM_conversation,
669                           &lpamh);
670     if (PAM_error != PAM_SUCCESS) {
671     /* Log Entry */
672            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
673                   strerror(errno));
674     /* Log Entry */
675       return AFPERR_PARAM;
676     }
677     pam_set_item(lpamh, PAM_TTY, "afpd");
678     pam_set_item(lpamh, PAM_RHOST, hostname);
679
680     /* we might need to do this as root */
681     uid = geteuid();
682     seteuid(0);
683     PAM_error = pam_authenticate(lpamh, 0);
684     if (PAM_error != PAM_SUCCESS) {
685       seteuid(uid);
686       pam_end(lpamh, PAM_error);
687       return AFPERR_NOTAUTH;
688     }
689
690     /* clear out old passwd */
691     memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
692
693     /* new password */
694     PAM_password = ibuf;
695     ibuf[PASSWDLEN] = '\0';
696
697     /* this really does need to be done as root */
698     PAM_error = pam_chauthtok(lpamh, 0);
699     seteuid(uid); /* un-root ourselves. */
700     memset(ibuf, 0, PASSWDLEN);
701     if (PAM_error != PAM_SUCCESS) {
702       pam_end(lpamh, PAM_error);
703       return AFPERR_ACCESS;
704     }
705
706     pam_end(lpamh, 0);
707     return AFP_OK;
708 }
709
710
711 static int uam_setup(const char *path)
712 {
713   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login, 
714                    pam_logincont, pam_logout, pam_login_ext) < 0)
715     return -1;
716
717   if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128", 
718                    pam_changepw) < 0) {
719     uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
720     return -1;
721   }
722
723   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
724     pam_printer);*/
725
726   return 0;
727 }
728
729 static void uam_cleanup(void)
730 {
731   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
732   uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
733   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
734 }
735
736 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
737   UAM_MODULE_SERVER,
738   UAM_MODULE_VERSION,
739   uam_setup, uam_cleanup
740 };
741
742 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
743   UAM_MODULE_SERVER,
744   UAM_MODULE_VERSION,
745   uam_setup, uam_cleanup
746 };
747
748 #endif /* USE_PAM && UAM_DHX */
749