2 * $Id: uams_dhx_pam.c,v 1.26 2008-12-02 18:24:55 morgana 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>
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>
38 #include <openssl/rand.h>
40 #else /* OPENSSL_DHX */
44 #endif /* OPENSSL_DHX */
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
51 #define CRYPTBUFLEN (KEYSIZE*2)
52 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
53 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
55 /* hash a number to a 16-bit quantity */
56 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
57 (unsigned long) (a)) & 0xffff)
60 static CAST_KEY castkey;
61 static struct passwd *dhxpwd;
62 static u_int8_t randbuf[KEYSIZE];
64 /* diffie-hellman bits */
65 static unsigned char msg2_iv[] = "CJalbert";
66 static unsigned char msg3_iv[] = "LWallace";
67 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
68 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
69 static const u_int8_t g = 0x07;
72 /* Static variables used to communicate between the conversation function
73 * and the server_login function
75 static pam_handle_t *pamh = NULL;
76 static char *PAM_username;
77 static char *PAM_password;
79 /* PAM conversation function
80 * Here we assume (for now, at least) that echo on means login name, and
81 * echo off means password.
83 static int PAM_conv (int num_msg,
84 const struct pam_message **msg,
85 struct pam_response **resp,
86 void *appdata_ptr _U_) {
88 struct pam_response *reply;
90 #define COPY_STRING(s) (s) ? strdup(s) : NULL
96 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
102 reply = (struct pam_response *)
103 calloc(num_msg, sizeof(struct pam_response));
107 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
113 for (count = 0; count < num_msg; count++) {
116 switch (msg[count]->msg_style) {
117 case PAM_PROMPT_ECHO_ON:
118 if (!(string = COPY_STRING(PAM_username))) {
120 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
126 case PAM_PROMPT_ECHO_OFF:
127 if (!(string = COPY_STRING(PAM_password))) {
129 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
136 #ifdef PAM_BINARY_PROMPT
137 case PAM_BINARY_PROMPT:
138 #endif /* PAM_BINARY_PROMPT */
144 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
151 reply[count].resp_retcode = 0;
152 reply[count].resp = string;
159 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
164 for (count = 0; count < num_msg; count++) {
165 if (!reply[count].resp)
167 switch (msg[count]->msg_style) {
168 case PAM_PROMPT_ECHO_OFF:
169 case PAM_PROMPT_ECHO_ON:
170 free(reply[count].resp);
176 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
182 static struct pam_conv PAM_conversation = {
188 static int dhx_setup(void *obj, char *ibuf, int ibuflen _U_,
189 char *rbuf, int *rbuflen)
193 BIGNUM *bn, *gbn, *pbn;
196 /* TODO: seed dhx_setup properly... this is a hack */
198 /* *SEVERE* hack... fix */
199 RAND_load_file("/var/adm/messages", KEYSIZE);
202 /* get the client's public key */
203 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
205 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
212 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
215 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
221 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
225 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
231 /* okay, we're ready */
232 if (!(dh = DH_new())) {
237 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
243 /* generate key and make sure that we have enough space */
246 if (DH_generate_key(dh) == 0) {
247 unsigned long dherror;
250 ERR_load_crypto_strings();
251 dherror = ERR_get_error();
252 ERR_error_string_n(dherror, errbuf, 256);
254 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key (OpenSSL error code: %u, %s)", dherror, errbuf);
259 if (BN_num_bytes(dh->pub_key) > KEYSIZE) {
260 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s", strerror(errno));
264 /* figure out the key. store the key in rbuf for now. */
265 i = DH_compute_key(rbuf, bn, dh);
268 CAST_set_key(&castkey, i, rbuf);
270 /* session id. it's just a hashed version of the object pointer. */
271 sessid = dhxhash(obj);
272 memcpy(rbuf, &sessid, sizeof(sessid));
273 rbuf += sizeof(sessid);
274 *rbuflen += sizeof(sessid);
277 BN_bn2bin(dh->pub_key, rbuf);
281 /* buffer to be encrypted */
283 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
287 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
292 memcpy(rbuf, &randbuf, sizeof(randbuf));
294 /* get the signature. it's always 16 bytes. */
296 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
297 (void *) &buf, NULL) < 0) {
300 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
305 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
307 memset(rbuf + KEYSIZE, 0, KEYSIZE);
310 /* encrypt using cast */
311 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
313 *rbuflen += CRYPTBUFLEN;
316 return AFPERR_AUTHCONT;
322 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
328 /* -------------------------------- */
329 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
330 char *ibuf, int ibuflen,
331 char *rbuf, int *rbuflen)
333 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
334 LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
338 PAM_username = username;
339 LOG(log_info, logtype_uams, "dhx login: %s", username);
340 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
343 /* -------------------------------- */
344 /* dhx login: things are done in a slightly bizarre order to avoid
345 * having to clean things up if there's an error. */
346 static int pam_login(void *obj, struct passwd **uam_pwd,
347 char *ibuf, int ibuflen,
348 char *rbuf, int *rbuflen)
355 /* grab some of the options */
356 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
357 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
362 len = (unsigned char) *ibuf++;
364 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
369 memcpy(username, ibuf, len );
371 username[ len ] = '\0';
373 if ((unsigned long) ibuf & 1) /* pad to even boundary */
376 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
379 /* ----------------------------- */
380 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
381 char *ibuf, int ibuflen,
382 char *rbuf, int *rbuflen)
390 /* grab some of the options */
391 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
392 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
400 memcpy(&temp16, uname, sizeof(temp16));
403 if ( !len || len > ulen ) {
404 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
408 memcpy(username, uname +2, len );
409 username[ len ] = '\0';
411 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
414 /* -------------------------------- */
416 static int pam_logincont(void *obj, struct passwd **uam_pwd,
417 char *ibuf, int ibuflen _U_,
418 char *rbuf, int *rbuflen)
421 BIGNUM *bn1, *bn2, *bn3;
427 /* check for session id */
428 memcpy(&sessid, ibuf, sizeof(sessid));
429 if (sessid != dhxhash(obj)) {
431 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
436 ibuf += sizeof(sessid);
438 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
439 (void *) &hostname, NULL) < 0)
441 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
445 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
446 msg3_iv, CAST_DECRYPT);
447 memset(&castkey, 0, sizeof(castkey));
449 /* check to make sure that the random number is the same. we
450 * get sent back an incremented random number. */
451 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
454 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
459 /* zero out the random number */
460 memset(rbuf, 0, sizeof(randbuf));
461 memset(randbuf, 0, sizeof(randbuf));
464 if (!(bn3 = BN_new())) {
470 BN_sub(bn3, bn1, bn2);
474 /* okay. is it one more? */
475 if (!BN_is_one(bn3)) {
481 /* Set these things up for the conv function */
482 rbuf[PASSWDLEN] = '\0';
485 err = AFPERR_NOTAUTH;
486 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
488 if (PAM_error != PAM_SUCCESS) {
490 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
491 pam_strerror(pamh,PAM_error));
496 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
497 pam_set_item(pamh, PAM_TTY, "afpd");
498 pam_set_item(pamh, PAM_RHOST, hostname);
499 PAM_error = pam_authenticate(pamh,0);
500 if (PAM_error != PAM_SUCCESS) {
501 if (PAM_error == PAM_MAXTRIES)
502 err = AFPERR_PWDEXPR;
504 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
505 pam_strerror(pamh, PAM_error));
510 PAM_error = pam_acct_mgmt(pamh, 0);
511 if (PAM_error != PAM_SUCCESS ) {
513 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
514 pam_strerror(pamh, PAM_error));
516 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* password expired */
517 err = AFPERR_PWDEXPR;
518 #ifdef PAM_AUTHTOKEN_REQD
519 else if (PAM_error == PAM_AUTHTOKEN_REQD)
520 err = AFPERR_PWDCHNG;
526 #ifndef PAM_CRED_ESTABLISH
527 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
529 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
530 if (PAM_error != PAM_SUCCESS) {
532 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
533 pam_strerror(pamh, PAM_error));
538 PAM_error = pam_open_session(pamh, 0);
539 if (PAM_error != PAM_SUCCESS) {
541 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
542 pam_strerror(pamh, PAM_error));
547 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
550 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
552 if ( err == AFPERR_PWDEXPR)
557 pam_end(pamh, PAM_error);
559 memset(rbuf, 0, CRYPT2BUFLEN);
564 static void pam_logout() {
565 pam_close_session(pamh, 0);
571 /* change pw for dhx needs a couple passes to get everything all
572 * right. basically, it's like the login/logincont sequence */
573 static int pam_changepw(void *obj, char *username,
574 struct passwd *pwd _U_, char *ibuf, int ibuflen,
575 char *rbuf, int *rbuflen)
577 BIGNUM *bn1, *bn2, *bn3;
586 memcpy(&sessid, ibuf, sizeof(sessid));
587 ibuf += sizeof(sessid);
589 if (!sessid) { /* no sessid -> initialization phase */
590 PAM_username = username;
591 ibuflen -= sizeof(sessid);
592 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
596 /* otherwise, it's like logincont but different. */
598 /* check out the session id */
599 if (sessid != dhxhash(obj)) {
601 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
607 /* we need this for pam */
608 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
609 (void *) &hostname, NULL) < 0) {
611 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
617 /* grab the client's nonce, old password, and new password. */
618 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
619 msg3_iv, CAST_DECRYPT);
620 memset(&castkey, 0, sizeof(castkey));
622 /* check to make sure that the random number is the same. we
623 * get sent back an incremented random number. */
624 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
626 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
632 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
635 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
641 /* zero out the random number */
642 memset(rbuf, 0, sizeof(randbuf));
643 memset(randbuf, 0, sizeof(randbuf));
645 if (!(bn3 = BN_new())) {
649 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
655 BN_sub(bn3, bn1, bn2);
659 /* okay. is it one more? */
661 if (!BN_is_one(bn3)) {
664 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
672 /* Set these things up for the conv function. the old password
675 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
676 PAM_password = ibuf + PASSWDLEN;
678 PAM_error = pam_start("netatalk", username, &PAM_conversation,
680 if (PAM_error != PAM_SUCCESS) {
682 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
687 pam_set_item(lpamh, PAM_TTY, "afpd");
688 pam_set_item(lpamh, PAM_RHOST, hostname);
690 /* we might need to do this as root */
693 PAM_error = pam_authenticate(lpamh, 0);
694 if (PAM_error != PAM_SUCCESS) {
696 pam_end(lpamh, PAM_error);
697 return AFPERR_NOTAUTH;
700 /* clear out old passwd */
701 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
705 ibuf[PASSWDLEN] = '\0';
707 /* this really does need to be done as root */
708 PAM_error = pam_chauthtok(lpamh, 0);
709 seteuid(uid); /* un-root ourselves. */
710 memset(ibuf, 0, PASSWDLEN);
711 if (PAM_error != PAM_SUCCESS) {
712 pam_end(lpamh, PAM_error);
713 return AFPERR_ACCESS;
721 static int uam_setup(const char *path)
723 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login,
724 pam_logincont, pam_logout, pam_login_ext) < 0)
727 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
729 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
733 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
739 static void uam_cleanup(void)
741 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
742 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
743 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
746 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
749 uam_setup, uam_cleanup
752 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
755 uam_setup, uam_cleanup
758 #endif /* USE_PAM && UAM_DHX */