2 * $Id: uams_dhx_pam.c,v 1.33 2010-03-30 10:25:49 franklahm Exp $
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.
11 #endif /* HAVE_CONFIG_H */
13 #if defined(USE_PAM) && defined(UAM_DHX)
17 #include <atalk/logger.h>
21 #endif /* HAVE_UNISTD_H */
23 #ifdef HAVE_SECURITY_PAM_APPL_H
24 #include <security/pam_appl.h>
26 #ifdef HAVE_PAM_PAM_APPL_H
27 #include <pam/pam_appl.h>
29 #include <arpa/inet.h>
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 */
43 #endif /* OPENSSL_DHX */
45 #include <atalk/afp.h>
46 #include <atalk/uam.h>
50 #define CRYPTBUFLEN (KEYSIZE*2)
51 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
52 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
54 /* hash a number to a 16-bit quantity */
55 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
56 (unsigned long) (a)) & 0xffff)
59 static CAST_KEY castkey;
60 static struct passwd *dhxpwd;
61 static uint8_t randbuf[KEYSIZE];
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;
71 /* Static variables used to communicate between the conversation function
72 * and the server_login function
74 static pam_handle_t *pamh = NULL;
75 static char *PAM_username;
76 static char *PAM_password;
78 /* PAM conversation function
79 * Here we assume (for now, at least) that echo on means login name, and
80 * echo off means password.
82 static int PAM_conv (int num_msg,
84 const struct pam_message **msg,
86 struct pam_message **msg,
88 struct pam_response **resp,
89 void *appdata_ptr _U_) {
91 struct pam_response *reply;
93 #define COPY_STRING(s) (s) ? strdup(s) : NULL
99 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
105 reply = (struct pam_response *)
106 calloc(num_msg, sizeof(struct pam_response));
110 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
116 for (count = 0; count < num_msg; count++) {
119 switch (msg[count]->msg_style) {
120 case PAM_PROMPT_ECHO_ON:
121 if (!(string = COPY_STRING(PAM_username))) {
123 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
129 case PAM_PROMPT_ECHO_OFF:
130 if (!(string = COPY_STRING(PAM_password))) {
132 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
139 #ifdef PAM_BINARY_PROMPT
140 case PAM_BINARY_PROMPT:
141 #endif /* PAM_BINARY_PROMPT */
147 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
154 reply[count].resp_retcode = 0;
155 reply[count].resp = string;
162 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
167 for (count = 0; count < num_msg; count++) {
168 if (!reply[count].resp)
170 switch (msg[count]->msg_style) {
171 case PAM_PROMPT_ECHO_OFF:
172 case PAM_PROMPT_ECHO_ON:
173 free(reply[count].resp);
179 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
185 static struct pam_conv PAM_conversation = {
191 static int dhx_setup(void *obj, char *ibuf, size_t ibuflen _U_,
192 char *rbuf, size_t *rbuflen)
196 BIGNUM *bn, *gbn, *pbn;
199 /* get the client's public key */
200 if (!(bn = BN_bin2bn((unsigned char *)ibuf, KEYSIZE, NULL))) {
202 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
209 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
212 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
218 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
222 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
228 /* okay, we're ready */
229 if (!(dh = DH_new())) {
234 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
240 /* generate key and make sure that we have enough space */
243 if (DH_generate_key(dh) == 0) {
244 unsigned long dherror;
247 ERR_load_crypto_strings();
248 dherror = ERR_get_error();
249 ERR_error_string_n(dherror, errbuf, 256);
251 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key (OpenSSL error code: %u, %s)", dherror, errbuf);
256 if (BN_num_bytes(dh->pub_key) > KEYSIZE) {
257 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s", strerror(errno));
261 /* figure out the key. store the key in rbuf for now. */
262 i = DH_compute_key((unsigned char *)rbuf, bn, dh);
265 CAST_set_key(&castkey, i, (unsigned char *)rbuf);
267 /* session id. it's just a hashed version of the object pointer. */
268 sessid = dhxhash(obj);
269 memcpy(rbuf, &sessid, sizeof(sessid));
270 rbuf += sizeof(sessid);
271 *rbuflen += sizeof(sessid);
274 BN_bn2bin(dh->pub_key, (unsigned char *)rbuf);
278 /* buffer to be encrypted */
280 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
284 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
289 memcpy(rbuf, &randbuf, sizeof(randbuf));
291 /* get the signature. it's always 16 bytes. */
293 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
294 (void *) &buf, NULL) < 0) {
297 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
302 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
304 memset(rbuf + KEYSIZE, 0, KEYSIZE);
307 /* encrypt using cast */
308 CAST_cbc_encrypt((unsigned char *)rbuf, (unsigned char *)rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
310 *rbuflen += CRYPTBUFLEN;
313 return AFPERR_AUTHCONT;
319 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
325 /* -------------------------------- */
326 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
327 char *ibuf, size_t ibuflen,
328 char *rbuf, size_t *rbuflen)
330 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
331 LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username [%s]", username);
332 return AFPERR_NOTAUTH;
335 PAM_username = username;
336 LOG(log_info, logtype_uams, "dhx login: %s", username);
337 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
340 /* -------------------------------- */
341 /* dhx login: things are done in a slightly bizarre order to avoid
342 * having to clean things up if there's an error. */
343 static int pam_login(void *obj, struct passwd **uam_pwd,
344 char *ibuf, size_t ibuflen,
345 char *rbuf, size_t *rbuflen)
352 /* grab some of the options */
353 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
354 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
359 len = (unsigned char) *ibuf++;
361 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
366 memcpy(username, ibuf, len );
368 username[ len ] = '\0';
370 if ((unsigned long) ibuf & 1) /* pad to even boundary */
373 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
376 /* ----------------------------- */
377 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
378 char *ibuf, size_t ibuflen,
379 char *rbuf, size_t *rbuflen)
388 /* grab some of the options */
389 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
390 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
398 memcpy(&temp16, uname, sizeof(temp16));
401 if ( !len || len > ulen ) {
402 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
406 memcpy(username, uname +2, len );
407 username[ len ] = '\0';
409 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
412 /* -------------------------------- */
414 static int pam_logincont(void *obj, struct passwd **uam_pwd,
415 char *ibuf, size_t ibuflen _U_,
416 char *rbuf, size_t *rbuflen)
418 const char *hostname;
419 BIGNUM *bn1, *bn2, *bn3;
425 /* check for session id */
426 memcpy(&sessid, ibuf, sizeof(sessid));
427 if (sessid != dhxhash(obj)) {
429 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
434 ibuf += sizeof(sessid);
436 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
437 (void *) &hostname, NULL) < 0)
439 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
443 CAST_cbc_encrypt((unsigned char *)ibuf, (unsigned char *)rbuf, CRYPT2BUFLEN, &castkey,
444 msg3_iv, CAST_DECRYPT);
445 memset(&castkey, 0, sizeof(castkey));
447 /* check to make sure that the random number is the same. we
448 * get sent back an incremented random number. */
449 if (!(bn1 = BN_bin2bn((unsigned char *)rbuf, KEYSIZE, NULL)))
452 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
457 /* zero out the random number */
458 memset(rbuf, 0, sizeof(randbuf));
459 memset(randbuf, 0, sizeof(randbuf));
462 if (!(bn3 = BN_new())) {
468 BN_sub(bn3, bn1, bn2);
472 /* okay. is it one more? */
473 if (!BN_is_one(bn3)) {
479 /* Set these things up for the conv function */
480 rbuf[PASSWDLEN] = '\0';
483 err = AFPERR_NOTAUTH;
484 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
486 if (PAM_error != PAM_SUCCESS) {
488 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
489 pam_strerror(pamh,PAM_error));
494 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
495 pam_set_item(pamh, PAM_TTY, "afpd");
496 pam_set_item(pamh, PAM_RHOST, hostname);
497 PAM_error = pam_authenticate(pamh,0);
498 if (PAM_error != PAM_SUCCESS) {
499 if (PAM_error == PAM_MAXTRIES)
500 err = AFPERR_PWDEXPR;
502 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
503 pam_strerror(pamh, PAM_error));
508 PAM_error = pam_acct_mgmt(pamh, 0);
509 if (PAM_error != PAM_SUCCESS ) {
511 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
512 pam_strerror(pamh, PAM_error));
514 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* password expired */
515 err = AFPERR_PWDEXPR;
516 #ifdef PAM_AUTHTOKEN_REQD
517 else if (PAM_error == PAM_AUTHTOKEN_REQD)
518 err = AFPERR_PWDCHNG;
524 #ifndef PAM_CRED_ESTABLISH
525 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
527 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
528 if (PAM_error != PAM_SUCCESS) {
530 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
531 pam_strerror(pamh, PAM_error));
536 PAM_error = pam_open_session(pamh, 0);
537 if (PAM_error != PAM_SUCCESS) {
539 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
540 pam_strerror(pamh, PAM_error));
545 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
548 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
550 if ( err == AFPERR_PWDEXPR)
555 pam_end(pamh, PAM_error);
557 memset(rbuf, 0, CRYPT2BUFLEN);
562 static void pam_logout(void) {
563 pam_close_session(pamh, 0);
569 /* change pw for dhx needs a couple passes to get everything all
570 * right. basically, it's like the login/logincont sequence */
571 static int pam_changepw(void *obj, char *username,
572 struct passwd *pwd _U_, char *ibuf, size_t ibuflen,
573 char *rbuf, size_t *rbuflen)
575 BIGNUM *bn1, *bn2, *bn3;
583 if (ibuflen < sizeof(sessid)) {
588 memcpy(&sessid, ibuf, sizeof(sessid));
589 ibuf += sizeof(sessid);
591 if (!sessid) { /* no sessid -> initialization phase */
592 PAM_username = username;
593 ibuflen -= sizeof(sessid);
594 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
598 /* otherwise, it's like logincont but different. */
600 /* check out the session id */
601 if (sessid != dhxhash(obj)) {
603 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
609 /* we need this for pam */
610 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
611 (void *) &hostname, NULL) < 0) {
613 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
619 /* grab the client's nonce, old password, and new password. */
620 CAST_cbc_encrypt((unsigned char *)ibuf, (unsigned char *)ibuf, CHANGEPWBUFLEN, &castkey,
621 msg3_iv, CAST_DECRYPT);
622 memset(&castkey, 0, sizeof(castkey));
624 /* check to make sure that the random number is the same. we
625 * get sent back an incremented random number. */
626 if (!(bn1 = BN_bin2bn((unsigned char *)ibuf, KEYSIZE, NULL))) {
628 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
634 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
637 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
643 /* zero out the random number */
644 memset(rbuf, 0, sizeof(randbuf));
645 memset(randbuf, 0, sizeof(randbuf));
647 if (!(bn3 = BN_new())) {
651 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
657 BN_sub(bn3, bn1, bn2);
661 /* okay. is it one more? */
663 if (!BN_is_one(bn3)) {
666 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
674 /* Set these things up for the conv function. the old password
677 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
678 PAM_password = ibuf + PASSWDLEN;
680 PAM_error = pam_start("netatalk", username, &PAM_conversation,
682 if (PAM_error != PAM_SUCCESS) {
684 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
689 pam_set_item(lpamh, PAM_TTY, "afpd");
690 pam_set_item(lpamh, PAM_RHOST, hostname);
692 /* we might need to do this as root */
695 PAM_error = pam_authenticate(lpamh, 0);
696 if (PAM_error != PAM_SUCCESS) {
698 pam_end(lpamh, PAM_error);
699 return AFPERR_NOTAUTH;
702 /* clear out old passwd */
703 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
707 ibuf[PASSWDLEN] = '\0';
709 /* this really does need to be done as root */
710 PAM_error = pam_chauthtok(lpamh, 0);
711 seteuid(uid); /* un-root ourselves. */
712 memset(ibuf, 0, PASSWDLEN);
713 if (PAM_error != PAM_SUCCESS) {
714 pam_end(lpamh, PAM_error);
715 return AFPERR_ACCESS;
723 static int uam_setup(const char *path)
725 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login,
726 pam_logincont, pam_logout, pam_login_ext) < 0)
729 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
731 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
735 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
741 static void uam_cleanup(void)
743 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
744 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
745 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
748 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
751 uam_setup, uam_cleanup
754 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
757 uam_setup, uam_cleanup
760 #endif /* USE_PAM && UAM_DHX */