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