2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
4 * All Rights Reserved. See COPYRIGHT.
9 #endif /* HAVE_CONFIG_H */
11 #if defined (USE_PAM) && defined (UAM_DHX2)
15 #include <atalk/logger.h>
19 #endif /* HAVE_UNISTD_H */
21 #ifdef HAVE_SECURITY_PAM_APPL_H
22 #include <security/pam_appl.h>
24 #ifdef HAVE_PAM_PAM_APPL_H
25 #include <pam/pam_appl.h>
31 #endif /* HAVE_LIBGCRYPT */
33 #include <atalk/afp.h>
34 #include <atalk/uam.h>
35 #include <atalk/globals.h>
37 /* Number of bits for p which we generate. Everybode out there uses 512, so we beet them */
38 #define PRIMEBITS 1024
40 /* hash a number to a 16-bit quantity */
41 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
42 (unsigned long) (a)) & 0xffff)
44 /* Some parameters need be maintained across calls */
45 static gcry_mpi_t p, g, Ra;
46 static gcry_mpi_t serverNonce;
47 static char *K_MD5hash = NULL;
48 static int K_hash_len;
51 /* The initialization vectors for CAST128 are fixed by Apple. */
52 static unsigned char dhx_c2siv[] = { 'L', 'W', 'a', 'l', 'l', 'a', 'c', 'e' };
53 static unsigned char dhx_s2civ[] = { 'C', 'J', 'a', 'l', 'b', 'e', 'r', 't' };
55 /* Static variables used to communicate between the conversation function
56 * and the server_login function */
57 static pam_handle_t *pamh = NULL;
58 static char *PAM_username;
59 static char *PAM_password;
60 static struct passwd *dhxpwd;
62 /*********************************************************
63 * Crypto helper func to generate p and g for use in DH.
64 * libgcrypt doesn't provide one directly.
65 * Algorithm taken from GNUTLS:gnutls_dh_primes.c
66 *********************************************************/
69 * This function will generate a new pair of prime and generator for use in
70 * the Diffie-Hellman key exchange.
71 * The bits value should be one of 768, 1024, 2048, 3072 or 4096.
73 static int dh_params_generate (unsigned int bits) {
75 int result, times = 0, qbits;
76 gcry_mpi_t *factors = NULL;
79 /* Version check should be the very first call because it
80 makes sure that important subsystems are intialized. */
81 if (!gcry_check_version (GCRYPT_VERSION)) {
82 LOG(log_error, logtype_uams, "PAM DHX2: libgcrypt versions mismatch. Need: %s", GCRYPT_VERSION);
90 qbits = (bits / 40) + 105;
92 if (qbits & 1) /* better have an even number */
95 /* find a prime number of size bits. */
99 gcry_prime_release_factors (factors);
101 err = gcry_prime_generate(&p, bits, qbits, &factors, NULL, NULL,
102 GCRY_STRONG_RANDOM, GCRY_PRIME_FLAG_SPECIAL_FACTOR);
104 result = AFPERR_MISC;
107 err = gcry_prime_check(p, 0);
109 } while (err != 0 && times < 10);
112 result = AFPERR_MISC;
116 /* generate the group generator. */
117 err = gcry_prime_group_generator(&g, p, factors, NULL);
119 result = AFPERR_MISC;
123 gcry_prime_release_factors(factors);
128 gcry_prime_release_factors(factors);
134 /* PAM conversation function
135 * Here we assume (for now, at least) that echo on means login name, and
136 * echo off means password.
138 static int PAM_conv (int num_msg,
139 const struct pam_message **msg,
140 struct pam_response **resp,
141 void *appdata_ptr _U_) {
143 struct pam_response *reply;
145 #define COPY_STRING(s) (s) ? strdup(s) : NULL
151 LOG(log_info, logtype_uams, "PAM DHX2 Conversation Err -- %s",
157 reply = (struct pam_response *)
158 calloc(num_msg, sizeof(struct pam_response));
162 LOG(log_info, logtype_uams, "PAM DHX2: Conversation Err -- %s",
168 for (count = 0; count < num_msg; count++) {
171 switch (msg[count]->msg_style) {
172 case PAM_PROMPT_ECHO_ON:
173 if (!(string = COPY_STRING(PAM_username))) {
175 LOG(log_info, logtype_uams, "PAM DHX2: username failure -- %s",
181 case PAM_PROMPT_ECHO_OFF:
182 if (!(string = COPY_STRING(PAM_password))) {
184 LOG(log_info, logtype_uams, "PAM DHX2: passwd failure: --: %s",
191 #ifdef PAM_BINARY_PROMPT
192 case PAM_BINARY_PROMPT:
193 #endif /* PAM_BINARY_PROMPT */
198 LOG(log_info, logtype_uams, "PAM DHX2: Binary_Prompt -- %s", strerror(errno));
203 reply[count].resp_retcode = 0;
204 reply[count].resp = string;
210 LOG(log_info, logtype_uams, "PAM DHX2: PAM Success");
214 for (count = 0; count < num_msg; count++) {
215 if (!reply[count].resp)
217 switch (msg[count]->msg_style) {
218 case PAM_PROMPT_ECHO_OFF:
219 case PAM_PROMPT_ECHO_ON:
220 free(reply[count].resp);
226 LOG(log_info, logtype_uams, "PAM DHX2: Conversation Err -- %s",
232 static struct pam_conv PAM_conversation = {
238 static int dhx2_setup(void *obj, char *ibuf _U_, size_t ibuflen _U_,
239 char *rbuf, size_t *rbuflen)
244 char *Ra_binary = NULL;
249 Ra = gcry_mpi_new(0);
250 Ma = gcry_mpi_new(0);
252 /* Generate our random number Ra. */
253 Ra_binary = calloc(1, PRIMEBITS/8);
254 if (Ra_binary == NULL) {
258 gcry_randomize(Ra_binary, PRIMEBITS/8, GCRY_STRONG_RANDOM);
259 gcry_mpi_scan(&Ra, GCRYMPI_FMT_USG, Ra_binary, PRIMEBITS/8, NULL);
263 /* Ma = g^Ra mod p. This is our "public" key */
264 gcry_mpi_powm(Ma, g, Ra, p);
266 /* ------- DH Init done ------ */
267 /* Start building reply packet */
269 /* Session ID first */
272 memcpy(rbuf, &uint16, sizeof(uint16_t));
277 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, 4, &nwritten, g);
279 memmove( rbuf+4-nwritten, rbuf, nwritten);
280 memset( rbuf, 0, 4-nwritten);
285 /* len = length of p = PRIMEBITS/8 */
287 uint16 = htons((uint16_t) PRIMEBITS/8);
288 memcpy(rbuf, &uint16, sizeof(uint16_t));
293 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, p);
295 *rbuflen += PRIMEBITS/8;
298 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, &nwritten, Ma);
299 if (nwritten < PRIMEBITS/8) {
300 memmove(rbuf + (PRIMEBITS/8) - nwritten, rbuf, nwritten);
301 memset(rbuf, 0, (PRIMEBITS/8) - nwritten);
304 *rbuflen += PRIMEBITS/8;
306 ret = AFPERR_AUTHCONT;
308 error: /* We exit here anyway */
309 /* We will need Ra later, but mustn't forget to release it ! */
310 gcry_mpi_release(Ma);
314 /* -------------------------------- */
315 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
316 char *ibuf, size_t ibuflen,
317 char *rbuf, size_t *rbuflen)
319 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
320 LOG(log_info, logtype_uams, "DHX2: unknown username");
321 return AFPERR_NOTAUTH;
324 PAM_username = username;
325 LOG(log_info, logtype_uams, "DHX2 login: %s", username);
326 return dhx2_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
329 /* -------------------------------- */
330 /* dhx login: things are done in a slightly bizarre order to avoid
331 * having to clean things up if there's an error. */
332 static int pam_login(void *obj, struct passwd **uam_pwd,
333 char *ibuf, size_t ibuflen,
334 char *rbuf, size_t *rbuflen)
341 /* grab some of the options */
342 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
343 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
348 len = (unsigned char) *ibuf++;
350 LOG(log_info, logtype_uams, "DHX2: Signature Retieval Failure -- %s",
355 memcpy(username, ibuf, len );
357 username[ len ] = '\0';
359 if ((unsigned long) ibuf & 1) /* pad to even boundary */
362 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
365 /* ----------------------------- */
366 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
367 char *ibuf, size_t ibuflen,
368 char *rbuf, size_t *rbuflen)
376 /* grab some of the options */
377 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
378 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
386 memcpy(&temp16, uname, sizeof(temp16));
389 if ( !len || len > ulen ) {
390 LOG(log_info, logtype_uams, "DHX2: Signature Retrieval Failure -- %s",
394 memcpy(username, uname +2, len );
395 username[ len ] = '\0';
397 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
400 /* -------------------------------- */
401 static int logincont1(void *obj _U_, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
405 gcry_mpi_t Mb, K, clientNonce;
406 unsigned char *K_bin = NULL;
407 char serverNonce_bin[16];
408 gcry_cipher_hd_t ctx;
409 gcry_error_t ctxerror;
414 Mb = gcry_mpi_new(0);
416 clientNonce = gcry_mpi_new(0);
417 serverNonce = gcry_mpi_new(0);
419 /* Packet size should be: Session ID + Ma + Encrypted client nonce */
420 if (ibuflen != 2 + PRIMEBITS/8 + 16) {
421 LOG(log_error, logtype_uams, "DHX2: Paket length not correct");
426 /* Skip session id */
429 /* Extract Mb, client's "public" key */
430 gcry_mpi_scan(&Mb, GCRYMPI_FMT_USG, ibuf, PRIMEBITS/8, NULL);
433 /* Now finally generate the Key: K = Mb^Ra mod p */
434 gcry_mpi_powm(K, Mb, Ra, p);
436 /* We need K in binary form in order to ... */
437 K_bin = calloc(1, PRIMEBITS/8);
442 gcry_mpi_print(GCRYMPI_FMT_USG, K_bin, PRIMEBITS/8, &nwritten, K);
443 if (nwritten < PRIMEBITS/8) {
444 memmove(K_bin + PRIMEBITS/8 - nwritten, K_bin, nwritten);
445 memset(K_bin, 0, PRIMEBITS/8 - nwritten);
448 /* ... generate the MD5 hash of K. K_MD5hash is what we actually use ! */
449 K_MD5hash = calloc(1, K_hash_len = gcry_md_get_algo_dlen(GCRY_MD_MD5));
450 if (K_MD5hash == NULL) {
454 gcry_md_hash_buffer(GCRY_MD_MD5, K_MD5hash, K_bin, PRIMEBITS/8);
458 /* FIXME: To support the Reconnect UAM, we need to store this key somewhere */
460 /* Set up our encryption context. */
461 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
462 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
467 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
468 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
472 /* Set the initialization vector for client->server transfer. */
473 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
474 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
478 /* Finally: decrypt client's md5_K(client nonce, C2SIV) inplace */
479 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16, NULL, 0);
480 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
484 /* Pull out clients nonce */
485 gcry_mpi_scan(&clientNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
486 /* Increment nonce */
487 gcry_mpi_add_ui(clientNonce, clientNonce, 1);
489 /* Generate our nonce and remember it for Logincont2 */
490 gcry_create_nonce(serverNonce_bin, 16); /* We'll use this here */
491 gcry_mpi_scan(&serverNonce, GCRYMPI_FMT_USG, serverNonce_bin, 16, NULL); /* For use in Logincont2 */
493 /* ---- Start building reply packet ---- */
495 /* Session ID + 1 first */
496 uint16 = htons(ID+1);
497 memcpy(rbuf, &uint16, sizeof(uint16_t));
501 /* Client nonce + 1 */
502 gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, clientNonce);
504 memcpy(rbuf+16, serverNonce_bin, 16);
506 /* Set the initialization vector for server->client transfer. */
507 ctxerror = gcry_cipher_setiv(ctx, dhx_s2civ, sizeof(dhx_s2civ));
508 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
512 /* Encrypt md5_K(clientNonce+1, serverNonce) inplace */
513 ctxerror = gcry_cipher_encrypt(ctx, rbuf, 32, NULL, 0);
514 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
521 ret = AFPERR_AUTHCONT;
525 gcry_cipher_close(ctx);
527 gcry_mpi_release(serverNonce);
532 gcry_mpi_release(Mb);
533 gcry_mpi_release(Ra);
534 gcry_mpi_release(clientNonce);
539 * Try to authenticate via PAM as "adminauthuser"
541 static int loginasroot(const char *adminauthuser, const char **hostname, int status)
545 if ((PAM_error = pam_end(pamh, status)) != PAM_SUCCESS)
549 if ((PAM_error = pam_start("netatalk", adminauthuser, &PAM_conversation, &pamh)) != PAM_SUCCESS) {
550 LOG(log_info, logtype_uams, "DHX2: PAM_Error: %s", pam_strerror(pamh,PAM_error));
554 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
555 pam_set_item(pamh, PAM_TTY, "afpd");
556 pam_set_item(pamh, PAM_RHOST, *hostname);
557 if ((PAM_error = pam_authenticate(pamh, 0)) != PAM_SUCCESS)
560 LOG(log_warning, logtype_uams, "DHX2: Authenticated as \"%s\"", adminauthuser);
566 static int logincont2(void *obj_in, struct passwd **uam_pwd,
567 char *ibuf, size_t ibuflen,
568 char *rbuf _U_, size_t *rbuflen)
570 AFPObj *obj = obj_in;
571 int ret = AFPERR_MISC;
573 const char *hostname = NULL;
574 gcry_mpi_t retServerNonce;
575 gcry_cipher_hd_t ctx;
576 gcry_error_t ctxerror;
577 char *utfpass = NULL;
581 /* Packet size should be: Session ID + ServerNonce + Passwd buffer (evantually +10 extra bytes, see Apples Docs) */
582 if ((ibuflen != 2 + 16 + 256) && (ibuflen != 2 + 16 + 256 + 10)) {
583 LOG(log_error, logtype_uams, "DHX2: Paket length not correct: %u. Should be 274 or 284.", ibuflen);
588 retServerNonce = gcry_mpi_new(0);
591 uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME, (void *) &hostname, NULL);
593 /* Set up our encryption context. */
594 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
595 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
600 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
601 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
605 /* Set the initialization vector for client->server transfer. */
606 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
607 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
612 /* Skip Session ID */
615 /* Finally: decrypt client's md5_K(serverNonce+1, passwor, C2SIV) inplace */
616 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16+256, NULL, 0);
617 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
621 /* Pull out nonce. Should be serverNonce+1 */
622 gcry_mpi_scan(&retServerNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
623 gcry_mpi_sub_ui(retServerNonce, retServerNonce, 1);
624 if ( gcry_mpi_cmp( serverNonce, retServerNonce) != 0) {
626 ret = AFPERR_NOTAUTH;
631 /* ---- Start authentication with PAM --- */
633 /* The password is in legacy Mac encoding, convert it to host encoding */
634 if (convert_string_allocate(CH_MAC, CH_UNIX, ibuf, -1, &utfpass) == (size_t)-1) {
635 LOG(log_error, logtype_uams, "DHX2: conversion error");
638 PAM_password = utfpass;
641 LOG(log_maxdebug, logtype_default, "DHX2: password: %s", PAM_password);
644 /* Set these things up for the conv function */
646 ret = AFPERR_NOTAUTH;
647 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation, &pamh);
648 if (PAM_error != PAM_SUCCESS) {
649 LOG(log_info, logtype_uams, "DHX2: PAM_Error: %s", pam_strerror(pamh,PAM_error));
653 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
654 pam_set_item(pamh, PAM_TTY, "afpd");
655 pam_set_item(pamh, PAM_RHOST, hostname);
656 pam_set_item(pamh, PAM_RUSER, PAM_username);
658 PAM_error = pam_authenticate(pamh, 0);
659 if (PAM_error != PAM_SUCCESS) {
660 if (PAM_error == PAM_MAXTRIES)
661 ret = AFPERR_PWDEXPR;
662 LOG(log_info, logtype_uams, "DHX2: PAM_Error: %s", pam_strerror(pamh, PAM_error));
664 if (!obj->options.adminauthuser)
666 if (loginasroot(obj->options.adminauthuser, &hostname, PAM_error) != PAM_SUCCESS) {
671 PAM_error = pam_acct_mgmt(pamh, 0);
672 if (PAM_error != PAM_SUCCESS ) {
673 LOG(log_info, logtype_uams, "DHX2: PAM_Error: %s",
674 pam_strerror(pamh, PAM_error));
675 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* password expired */
676 ret = AFPERR_PWDEXPR;
677 #ifdef PAM_AUTHTOKEN_REQD
678 else if (PAM_error == PAM_AUTHTOKEN_REQD)
679 ret = AFPERR_PWDCHNG;
684 #ifndef PAM_CRED_ESTABLISH
685 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
687 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
688 if (PAM_error != PAM_SUCCESS) {
689 LOG(log_info, logtype_uams, "DHX2: PAM_Error: %s",
690 pam_strerror(pamh, PAM_error));
694 PAM_error = pam_open_session(pamh, 0);
695 if (PAM_error != PAM_SUCCESS) {
696 LOG(log_info, logtype_uams, "DHX2: PAM_Error: %s",
697 pam_strerror(pamh, PAM_error));
701 memset(ibuf, 0, 256); /* zero out the password */
703 memset(utfpass, 0, strlen(utfpass));
705 LOG(log_info, logtype_uams, "DHX2: PAM Auth OK!");
710 gcry_cipher_close(ctx);
712 if (utfpass) free(utfpass);
715 gcry_mpi_release(serverNonce);
716 gcry_mpi_release(retServerNonce);
720 static int pam_logincont(void *obj, struct passwd **uam_pwd,
721 char *ibuf, size_t ibuflen,
722 char *rbuf, size_t *rbuflen)
727 /* check for session id */
728 memcpy(&retID, ibuf, sizeof(uint16_t));
729 retID = ntohs(retID);
731 ret = logincont1(obj, ibuf, ibuflen, rbuf, rbuflen);
732 else if (retID == ID+1)
733 ret = logincont2(obj, uam_pwd, ibuf,ibuflen, rbuf, rbuflen);
735 LOG(log_info, logtype_uams, "DHX2: Session ID Mismatch");
743 static void pam_logout(void) {
744 pam_close_session(pamh, 0);
749 /****************************
750 * --- Change pwd stuff --- */
752 static int changepw_1(void *obj, char *uname,
753 char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
757 /* Remember it now, use it in changepw_3 */
758 PAM_username = uname;
759 return( dhx2_setup(obj, ibuf, ibuflen, rbuf, rbuflen) );
762 static int changepw_2(void *obj,
763 char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
765 return( logincont1(obj, ibuf, ibuflen, rbuf, rbuflen) );
768 static int changepw_3(void *obj _U_,
769 char *ibuf, size_t ibuflen _U_,
770 char *rbuf _U_, size_t *rbuflen _U_)
776 const char *hostname = NULL;
777 gcry_mpi_t retServerNonce;
778 gcry_cipher_hd_t ctx;
779 gcry_error_t ctxerror;
783 LOG(log_error, logtype_uams, "DHX2 ChangePW: packet 3 processing");
785 /* Packet size should be: Session ID + ServerNonce + 2*Passwd buffer */
786 if (ibuflen != 2 + 16 + 2*256) {
787 LOG(log_error, logtype_uams, "DHX2: Paket length not correct");
792 retServerNonce = gcry_mpi_new(0);
795 uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME, (void *) &hostname, NULL);
797 /* Set up our encryption context. */
798 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
799 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
804 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
805 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
810 /* Set the initialization vector for client->server transfer. */
811 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
812 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
817 /* Skip Session ID */
820 /* Finally: decrypt client's md5_K(serverNonce+1, 2*password, C2SIV) inplace */
821 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16+2*256, NULL, 0);
822 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
826 /* Pull out nonce. Should be serverNonce+1 */
827 gcry_mpi_scan(&retServerNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
828 gcry_mpi_sub_ui(retServerNonce, retServerNonce, 1);
829 if ( gcry_mpi_cmp( serverNonce, retServerNonce) != 0) {
831 ret = AFPERR_NOTAUTH;
836 /* ---- Start pwd changing with PAM --- */
837 ibuf[255] = '\0'; /* For safety */
840 /* check if new and old password are equal */
841 if (memcmp(ibuf, ibuf + 256, 255) == 0) {
842 LOG(log_info, logtype_uams, "DHX2 Chgpwd: new and old password are equal");
843 ret = AFPERR_PWDSAME;
847 /* Set these things up for the conv function. PAM_username was set in changepw_1 */
848 PAM_password = ibuf + 256;
849 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation, &lpamh);
850 if (PAM_error != PAM_SUCCESS) {
851 LOG(log_info, logtype_uams, "DHX2 Chgpwd: PAM error in pam_start");
855 pam_set_item(lpamh, PAM_TTY, "afpd");
856 uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME, (void *) &hostname, NULL);
857 pam_set_item(lpamh, PAM_RHOST, hostname);
860 PAM_error = pam_authenticate(lpamh,0);
861 if (PAM_error != PAM_SUCCESS) {
862 LOG(log_info, logtype_uams, "DHX2 Chgpwd: error authenticating with PAM");
864 pam_end(lpamh, PAM_error);
865 ret = AFPERR_NOTAUTH;
869 PAM_error = pam_chauthtok(lpamh, 0);
870 seteuid(uid); /* un-root ourselves. */
871 memset(ibuf, 0, 512);
872 if (PAM_error != PAM_SUCCESS) {
873 LOG(log_info, logtype_uams, "DHX2 Chgpwd: error changing pw with PAM");
874 pam_end(lpamh, PAM_error);
882 gcry_cipher_close(ctx);
886 gcry_mpi_release(serverNonce);
887 gcry_mpi_release(retServerNonce);
891 static int dhx2_changepw(void *obj _U_, char *uname,
892 struct passwd *pwd _U_, char *ibuf, size_t ibuflen _U_,
893 char *rbuf _U_, size_t *rbuflen _U_)
895 /* We use this to serialize the three incoming FPChangePassword calls */
896 static int dhx2_changepw_status = 1;
898 int ret = AFPERR_NOTAUTH; /* gcc can't figure out it's always initialized */
900 switch (dhx2_changepw_status) {
902 ret = changepw_1( obj, uname, ibuf, ibuflen, rbuf, rbuflen);
903 if ( ret == AFPERR_AUTHCONT)
904 dhx2_changepw_status = 2;
907 ret = changepw_2( obj, ibuf, ibuflen, rbuf, rbuflen);
908 if ( ret == AFPERR_AUTHCONT)
909 dhx2_changepw_status = 3;
911 dhx2_changepw_status = 1;
914 ret = changepw_3( obj, ibuf, ibuflen, rbuf, rbuflen);
915 dhx2_changepw_status = 1; /* Whether is was succesfull or not: we
922 static int uam_setup(const char *path)
924 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHX2", pam_login,
925 pam_logincont, pam_logout, pam_login_ext) < 0)
927 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHX2", dhx2_changepw) < 0)
930 LOG(log_debug, logtype_uams, "DHX2: generating mersenne primes");
931 /* Generate p and g for DH */
932 if (dh_params_generate(PRIMEBITS) != 0) {
933 LOG(log_error, logtype_uams, "DHX2: Couldn't generate p and g");
940 static void uam_cleanup(void)
942 uam_unregister(UAM_SERVER_LOGIN, "DHX2");
943 uam_unregister(UAM_SERVER_CHANGEPW, "DHX2");
945 LOG(log_debug, logtype_uams, "DHX2: uam_cleanup");
952 UAM_MODULE_EXPORT struct uam_export uams_dhx2 = {
955 uam_setup, uam_cleanup
959 UAM_MODULE_EXPORT struct uam_export uams_dhx2_pam = {
962 uam_setup, uam_cleanup
965 #endif /* USE_PAM && UAM_DHX2 */