]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_pam.c
Fix several warnings, remove const
[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                      struct pam_message **msg,
84                      struct pam_response **resp,
85                      void *appdata_ptr _U_) {
86   int count = 0;
87   struct pam_response *reply;
88   
89 #define COPY_STRING(s) (s) ? strdup(s) : NULL
90   
91   errno = 0;
92
93   if (num_msg < 1) {
94     /* Log Entry */
95            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
96                   strerror(errno));
97     /* Log Entry */
98     return PAM_CONV_ERR;
99   }
100
101   reply = (struct pam_response *) 
102     calloc(num_msg, sizeof(struct pam_response));
103
104   if (!reply) {
105     /* Log Entry */
106            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
107                   strerror(errno));
108     /* Log Entry */
109     return PAM_CONV_ERR;
110   }
111
112   for (count = 0; count < num_msg; count++) {
113     char *string = NULL;
114
115     switch (msg[count]->msg_style) {
116     case PAM_PROMPT_ECHO_ON:
117       if (!(string = COPY_STRING(PAM_username))) {
118     /* Log Entry */
119            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
120                   strerror(errno));
121     /* Log Entry */
122         goto pam_fail_conv;
123       }
124       break;
125     case PAM_PROMPT_ECHO_OFF:
126       if (!(string = COPY_STRING(PAM_password))) {
127     /* Log Entry */
128            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
129                   strerror(errno));
130     /* Log Entry */
131         goto pam_fail_conv;
132       }
133       break;
134     case PAM_TEXT_INFO:
135 #ifdef PAM_BINARY_PROMPT
136     case PAM_BINARY_PROMPT:
137 #endif /* PAM_BINARY_PROMPT */
138       /* ignore it... */
139       break;
140     case PAM_ERROR_MSG:
141     default:
142     /* Log Entry */
143            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
144                   strerror(errno));
145     /* Log Entry */
146       goto pam_fail_conv;
147     }
148
149     if (string) {  
150       reply[count].resp_retcode = 0;
151       reply[count].resp = string;
152       string = NULL;
153     }
154   }
155
156   *resp = reply;
157     /* Log Entry */
158            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
159     /* Log Entry */
160   return PAM_SUCCESS;
161
162 pam_fail_conv:
163   for (count = 0; count < num_msg; count++) {
164     if (!reply[count].resp)
165         continue;
166     switch (msg[count]->msg_style) {
167     case PAM_PROMPT_ECHO_OFF:
168     case PAM_PROMPT_ECHO_ON:
169       free(reply[count].resp);
170       break;      
171     }
172   }
173   free(reply);
174     /* Log Entry */
175            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
176                   strerror(errno));
177     /* Log Entry */
178     return PAM_CONV_ERR;
179 }
180
181 static struct pam_conv PAM_conversation = {
182   &PAM_conv,
183   NULL
184 };
185
186
187 static int dhx_setup(void *obj, char *ibuf, size_t ibuflen _U_, 
188                      char *rbuf, size_t *rbuflen)
189 {
190     uint16_t sessid;
191     size_t i;
192     BIGNUM *bn, *gbn, *pbn;
193     DH *dh;
194
195     /* get the client's public key */
196     if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
197     /* Log Entry */
198            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
199                   strerror(errno));
200     /* Log Entry */
201       return AFPERR_PARAM;
202     }
203
204     /* get our primes */
205     if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
206       BN_clear_free(bn);
207     /* Log Entry */
208            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
209                   strerror(errno));
210     /* Log Entry */
211       return AFPERR_PARAM;
212     }
213
214     if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
215       BN_free(gbn);
216       BN_clear_free(bn);
217     /* Log Entry */
218            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
219                   strerror(errno));
220     /* Log Entry */
221       return AFPERR_PARAM;
222     }
223
224     /* okay, we're ready */
225     if (!(dh = DH_new())) {
226       BN_free(pbn);
227       BN_free(gbn);
228       BN_clear_free(bn);
229     /* Log Entry */
230            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
231                   strerror(errno));
232     /* Log Entry */
233       return AFPERR_PARAM;
234     }
235
236     /* generate key and make sure that we have enough space */
237     dh->p = pbn;
238     dh->g = gbn;
239     if (DH_generate_key(dh) == 0) {
240         unsigned long dherror;
241         char errbuf[256];
242
243         ERR_load_crypto_strings();
244         dherror = ERR_get_error();
245         ERR_error_string_n(dherror, errbuf, 256);
246
247         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key (OpenSSL error code: %u, %s)", dherror, errbuf);
248
249         ERR_free_strings();
250         goto pam_fail;
251     }
252     if (BN_num_bytes(dh->pub_key) > KEYSIZE) {
253         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s", strerror(errno));
254         goto pam_fail;
255     }
256
257     /* figure out the key. store the key in rbuf for now. */
258     i = DH_compute_key(rbuf, bn, dh);
259     
260     /* set the key */
261     CAST_set_key(&castkey, i, rbuf);
262     
263     /* session id. it's just a hashed version of the object pointer. */
264     sessid = dhxhash(obj);
265     memcpy(rbuf, &sessid, sizeof(sessid));
266     rbuf += sizeof(sessid);
267     *rbuflen += sizeof(sessid);
268     
269     /* public key */
270     BN_bn2bin(dh->pub_key, rbuf); 
271     rbuf += KEYSIZE;
272     *rbuflen += KEYSIZE;
273
274     /* buffer to be encrypted */
275     i = sizeof(randbuf);
276     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
277                              &i) < 0) {
278       *rbuflen = 0;
279     /* Log Entry */
280            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
281                   strerror(errno));
282     /* Log Entry */
283       goto pam_fail;
284     }    
285     memcpy(rbuf, &randbuf, sizeof(randbuf));
286
287     /* get the signature. it's always 16 bytes. */
288 #if 0
289     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
290                              (void *) &buf, NULL) < 0) {
291       *rbuflen = 0;
292     /* Log Entry */
293            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
294                   strerror(errno));
295     /* Log Entry */
296       goto pam_fail;
297     }
298     memcpy(rbuf + KEYSIZE, buf, KEYSIZE); 
299 #else /* 0 */
300     memset(rbuf + KEYSIZE, 0, KEYSIZE); 
301 #endif /* 0 */
302
303     /* encrypt using cast */
304     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv, 
305                      CAST_ENCRYPT);
306     *rbuflen += CRYPTBUFLEN;
307     BN_free(bn);
308     DH_free(dh);
309     return AFPERR_AUTHCONT;
310
311 pam_fail:
312     BN_free(bn);
313     DH_free(dh);
314     /* Log Entry */
315            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
316                   strerror(errno));
317     /* Log Entry */
318     return AFPERR_PARAM;
319 }
320
321 /* -------------------------------- */
322 static int login(void *obj, char *username, int ulen,  struct passwd **uam_pwd _U_,
323                      char *ibuf, size_t ibuflen,
324                      char *rbuf, size_t *rbuflen)
325 {
326     if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
327         LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username [%s]", username);
328         return AFPERR_NOTAUTH;
329     }
330
331     PAM_username = username;
332     LOG(log_info, logtype_uams, "dhx login: %s", username);
333     return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
334 }
335
336 /* -------------------------------- */
337 /* dhx login: things are done in a slightly bizarre order to avoid
338  * having to clean things up if there's an error. */
339 static int pam_login(void *obj, struct passwd **uam_pwd,
340                      char *ibuf, size_t ibuflen,
341                      char *rbuf, size_t *rbuflen)
342 {
343     char *username;
344     size_t len, ulen;
345
346     *rbuflen = 0;
347
348     /* grab some of the options */
349     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
350         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username  -- %s",
351                   strerror(errno));
352         return AFPERR_PARAM;
353     }
354
355     len = (unsigned char) *ibuf++;
356     if ( len > ulen ) {
357         LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
358                   strerror(errno));
359         return AFPERR_PARAM;
360     }
361
362     memcpy(username, ibuf, len );
363     ibuf += len;
364     username[ len ] = '\0';
365
366     if ((unsigned long) ibuf & 1) /* pad to even boundary */
367       ++ibuf;
368
369     return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
370 }
371
372 /* ----------------------------- */
373 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
374                      char *ibuf, size_t ibuflen,
375                      char *rbuf, size_t *rbuflen)
376 {
377     char *username;
378     int len;
379     size_t ulen;
380     uint16_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, size_t ibuflen _U_, 
412                          char *rbuf, size_t *rbuflen)
413 {
414     const char *hostname;
415     BIGNUM *bn1, *bn2, *bn3;
416     uint16_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(void) {
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, size_t ibuflen,
569                         char *rbuf, size_t *rbuflen)
570 {
571     BIGNUM *bn1, *bn2, *bn3;
572
573     char *hostname;
574     pam_handle_t *lpamh;
575     uid_t uid;
576     uint16_t sessid;
577     int PAM_error;
578
579     if (ibuflen < sizeof(sessid)) {
580       return AFPERR_PARAM;
581     }
582
583     /* grab the id */
584     memcpy(&sessid, ibuf, sizeof(sessid));
585     ibuf += sizeof(sessid);
586     
587     if (!sessid) {  /* no sessid -> initialization phase */
588       PAM_username = username;
589       ibuflen -= sizeof(sessid);
590       return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
591     }
592
593
594     /* otherwise, it's like logincont but different. */
595
596     /* check out the session id */
597     if (sessid != dhxhash(obj)) {
598     /* Log Entry */
599            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
600                   strerror(errno));
601     /* Log Entry */
602       return AFPERR_PARAM;
603     }
604
605     /* we need this for pam */
606     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
607                              (void *) &hostname, NULL) < 0) {
608     /* Log Entry */
609            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
610                   strerror(errno));
611     /* Log Entry */
612       return AFPERR_MISC;
613     }
614
615     /* grab the client's nonce, old password, and new password. */
616     CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
617                      msg3_iv, CAST_DECRYPT);
618     memset(&castkey, 0, sizeof(castkey));
619
620     /* check to make sure that the random number is the same. we
621      * get sent back an incremented random number. */
622     if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
623     /* Log Entry */
624            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
625                   strerror(errno));
626     /* Log Entry */
627       return AFPERR_PARAM;
628     }
629
630     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
631       BN_free(bn1);
632     /* Log Entry */
633            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
634                   strerror(errno));
635     /* Log Entry */
636       return AFPERR_PARAM;
637     }
638       
639     /* zero out the random number */
640     memset(rbuf, 0, sizeof(randbuf));
641     memset(randbuf, 0, sizeof(randbuf));
642
643     if (!(bn3 = BN_new())) {
644       BN_free(bn2);
645       BN_free(bn1);
646     /* Log Entry */
647            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
648                   strerror(errno));
649     /* Log Entry */
650       return AFPERR_PARAM;
651     }
652
653     BN_sub(bn3, bn1, bn2);
654     BN_free(bn2);
655     BN_free(bn1);
656
657     /* okay. is it one more? */
658 #if 0
659     if (!BN_is_one(bn3)) {
660       BN_free(bn3);
661     /* Log Entry */
662            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
663                   strerror(errno));
664     /* Log Entry */
665       return AFPERR_PARAM;
666     }
667 #endif
668     BN_free(bn3);
669
670     /* Set these things up for the conv function. the old password
671      * is at the end. */
672     ibuf += KEYSIZE;
673     ibuf[PASSWDLEN + PASSWDLEN] = '\0';
674     PAM_password = ibuf + PASSWDLEN;
675
676     PAM_error = pam_start("netatalk", username, &PAM_conversation,
677                           &lpamh);
678     if (PAM_error != PAM_SUCCESS) {
679     /* Log Entry */
680            LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
681                   strerror(errno));
682     /* Log Entry */
683       return AFPERR_PARAM;
684     }
685     pam_set_item(lpamh, PAM_TTY, "afpd");
686     pam_set_item(lpamh, PAM_RHOST, hostname);
687
688     /* we might need to do this as root */
689     uid = geteuid();
690     seteuid(0);
691     PAM_error = pam_authenticate(lpamh, 0);
692     if (PAM_error != PAM_SUCCESS) {
693       seteuid(uid);
694       pam_end(lpamh, PAM_error);
695       return AFPERR_NOTAUTH;
696     }
697
698     /* clear out old passwd */
699     memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
700
701     /* new password */
702     PAM_password = ibuf;
703     ibuf[PASSWDLEN] = '\0';
704
705     /* this really does need to be done as root */
706     PAM_error = pam_chauthtok(lpamh, 0);
707     seteuid(uid); /* un-root ourselves. */
708     memset(ibuf, 0, PASSWDLEN);
709     if (PAM_error != PAM_SUCCESS) {
710       pam_end(lpamh, PAM_error);
711       return AFPERR_ACCESS;
712     }
713
714     pam_end(lpamh, 0);
715     return AFP_OK;
716 }
717
718
719 static int uam_setup(const char *path)
720 {
721   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login, 
722                    pam_logincont, pam_logout, pam_login_ext) < 0)
723     return -1;
724
725   if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128", 
726                    pam_changepw) < 0) {
727     uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
728     return -1;
729   }
730
731   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
732     pam_printer);*/
733
734   return 0;
735 }
736
737 static void uam_cleanup(void)
738 {
739   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
740   uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
741   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
742 }
743
744 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
745   UAM_MODULE_SERVER,
746   UAM_MODULE_VERSION,
747   uam_setup, uam_cleanup
748 };
749
750 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
751   UAM_MODULE_SERVER,
752   UAM_MODULE_VERSION,
753   uam_setup, uam_cleanup
754 };
755
756 #endif /* USE_PAM && UAM_DHX */
757