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