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