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