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