2 * $Id: uams_dhx_pam.c,v 1.24.6.5.2.1 2005-09-27 10:40:41 didg 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) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
248 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s",
254 /* figure out the key. store the key in rbuf for now. */
255 i = DH_compute_key(rbuf, bn, dh);
258 CAST_set_key(&castkey, i, rbuf);
260 /* session id. it's just a hashed version of the object pointer. */
261 sessid = dhxhash(obj);
262 memcpy(rbuf, &sessid, sizeof(sessid));
263 rbuf += sizeof(sessid);
264 *rbuflen += sizeof(sessid);
267 BN_bn2bin(dh->pub_key, rbuf);
271 /* buffer to be encrypted */
273 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
277 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
282 memcpy(rbuf, &randbuf, sizeof(randbuf));
284 /* get the signature. it's always 16 bytes. */
286 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
287 (void *) &buf, NULL) < 0) {
290 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
295 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
297 memset(rbuf + KEYSIZE, 0, KEYSIZE);
300 /* encrypt using cast */
301 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
303 *rbuflen += CRYPTBUFLEN;
306 return AFPERR_AUTHCONT;
312 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
318 /* -------------------------------- */
319 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
320 char *ibuf, int ibuflen,
321 char *rbuf, int *rbuflen)
323 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
324 LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
328 PAM_username = username;
329 LOG(log_info, logtype_uams, "dhx login: %s", username);
330 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
333 /* -------------------------------- */
334 /* dhx login: things are done in a slightly bizarre order to avoid
335 * having to clean things up if there's an error. */
336 static int pam_login(void *obj, struct passwd **uam_pwd,
337 char *ibuf, int ibuflen,
338 char *rbuf, int *rbuflen)
345 /* grab some of the options */
346 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
347 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
352 len = (unsigned char) *ibuf++;
354 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
359 memcpy(username, ibuf, len );
361 username[ len ] = '\0';
363 if ((unsigned long) ibuf & 1) /* pad to even boundary */
366 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
369 /* ----------------------------- */
370 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
371 char *ibuf, int ibuflen,
372 char *rbuf, int *rbuflen)
380 /* grab some of the options */
381 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
382 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
390 memcpy(&temp16, uname, sizeof(temp16));
393 if ( !len || len > ulen ) {
394 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
398 memcpy(username, uname +2, len );
399 username[ len ] = '\0';
401 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
404 /* -------------------------------- */
406 static int pam_logincont(void *obj, struct passwd **uam_pwd,
407 char *ibuf, int ibuflen _U_,
408 char *rbuf, int *rbuflen)
411 BIGNUM *bn1, *bn2, *bn3;
417 /* check for session id */
418 memcpy(&sessid, ibuf, sizeof(sessid));
419 if (sessid != dhxhash(obj)) {
421 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
426 ibuf += sizeof(sessid);
428 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
429 (void *) &hostname, NULL) < 0)
431 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
435 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
436 msg3_iv, CAST_DECRYPT);
437 memset(&castkey, 0, sizeof(castkey));
439 /* check to make sure that the random number is the same. we
440 * get sent back an incremented random number. */
441 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
444 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
449 /* zero out the random number */
450 memset(rbuf, 0, sizeof(randbuf));
451 memset(randbuf, 0, sizeof(randbuf));
454 if (!(bn3 = BN_new())) {
460 BN_sub(bn3, bn1, bn2);
464 /* okay. is it one more? */
465 if (!BN_is_one(bn3)) {
471 /* Set these things up for the conv function */
472 rbuf[PASSWDLEN] = '\0';
475 err = AFPERR_NOTAUTH;
476 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
478 if (PAM_error != PAM_SUCCESS) {
480 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
481 pam_strerror(pamh,PAM_error));
486 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
487 pam_set_item(pamh, PAM_TTY, "afpd");
488 pam_set_item(pamh, PAM_RHOST, hostname);
489 PAM_error = pam_authenticate(pamh,0);
490 if (PAM_error != PAM_SUCCESS) {
491 if (PAM_error == PAM_MAXTRIES)
492 err = AFPERR_PWDEXPR;
494 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
495 pam_strerror(pamh, PAM_error));
500 PAM_error = pam_acct_mgmt(pamh, 0);
501 if (PAM_error != PAM_SUCCESS ) {
503 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
504 pam_strerror(pamh, PAM_error));
506 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* password expired */
507 err = AFPERR_PWDEXPR;
508 #ifdef PAM_AUTHTOKEN_REQD
509 else if (PAM_error == PAM_AUTHTOKEN_REQD)
510 err = AFPERR_PWDCHNG;
516 #ifndef PAM_CRED_ESTABLISH
517 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
519 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
520 if (PAM_error != PAM_SUCCESS) {
522 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
523 pam_strerror(pamh, PAM_error));
528 PAM_error = pam_open_session(pamh, 0);
529 if (PAM_error != PAM_SUCCESS) {
531 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
532 pam_strerror(pamh, PAM_error));
537 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
540 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
542 if ( err == AFPERR_PWDEXPR)
547 pam_end(pamh, PAM_error);
549 memset(rbuf, 0, CRYPT2BUFLEN);
554 static void pam_logout() {
555 pam_close_session(pamh, 0);
561 /* change pw for dhx needs a couple passes to get everything all
562 * right. basically, it's like the login/logincont sequence */
563 static int pam_changepw(void *obj, char *username,
564 struct passwd *pwd _U_, char *ibuf, int ibuflen,
565 char *rbuf, int *rbuflen)
567 BIGNUM *bn1, *bn2, *bn3;
576 memcpy(&sessid, ibuf, sizeof(sessid));
577 ibuf += sizeof(sessid);
579 if (!sessid) { /* no sessid -> initialization phase */
580 PAM_username = username;
581 ibuflen -= sizeof(sessid);
582 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
586 /* otherwise, it's like logincont but different. */
588 /* check out the session id */
589 if (sessid != dhxhash(obj)) {
591 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
597 /* we need this for pam */
598 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
599 (void *) &hostname, NULL) < 0) {
601 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
607 /* grab the client's nonce, old password, and new password. */
608 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
609 msg3_iv, CAST_DECRYPT);
610 memset(&castkey, 0, sizeof(castkey));
612 /* check to make sure that the random number is the same. we
613 * get sent back an incremented random number. */
614 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
616 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
622 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
625 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
631 /* zero out the random number */
632 memset(rbuf, 0, sizeof(randbuf));
633 memset(randbuf, 0, sizeof(randbuf));
635 if (!(bn3 = BN_new())) {
639 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
645 BN_sub(bn3, bn1, bn2);
649 /* okay. is it one more? */
651 if (!BN_is_one(bn3)) {
654 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
662 /* Set these things up for the conv function. the old password
665 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
666 PAM_password = ibuf + PASSWDLEN;
668 PAM_error = pam_start("netatalk", username, &PAM_conversation,
670 if (PAM_error != PAM_SUCCESS) {
672 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
677 pam_set_item(lpamh, PAM_TTY, "afpd");
678 pam_set_item(lpamh, PAM_RHOST, hostname);
680 /* we might need to do this as root */
683 PAM_error = pam_authenticate(lpamh, 0);
684 if (PAM_error != PAM_SUCCESS) {
686 pam_end(lpamh, PAM_error);
687 return AFPERR_NOTAUTH;
690 /* clear out old passwd */
691 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
695 ibuf[PASSWDLEN] = '\0';
697 /* this really does need to be done as root */
698 PAM_error = pam_chauthtok(lpamh, 0);
699 seteuid(uid); /* un-root ourselves. */
700 memset(ibuf, 0, PASSWDLEN);
701 if (PAM_error != PAM_SUCCESS) {
702 pam_end(lpamh, PAM_error);
703 return AFPERR_ACCESS;
711 static int uam_setup(const char *path)
713 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login,
714 pam_logincont, pam_logout, pam_login_ext) < 0)
717 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
719 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
723 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
729 static void uam_cleanup(void)
731 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
732 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
733 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
736 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
739 uam_setup, uam_cleanup
742 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
745 uam_setup, uam_cleanup
748 #endif /* USE_PAM && UAM_DHX */