2 * $Id: uams_dhx_pam.c,v 1.23 2003-01-01 13:19:24 srittau 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 */
24 #include <security/pam_appl.h>
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 */
36 #endif /* OPENSSL_DHX */
38 #include <atalk/afp.h>
39 #include <atalk/uam.h>
43 #define CRYPTBUFLEN (KEYSIZE*2)
44 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
45 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
47 /* hash a number to a 16-bit quantity */
48 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
49 (unsigned long) (a)) & 0xffff)
52 static CAST_KEY castkey;
53 static struct passwd *dhxpwd;
54 static u_int8_t randbuf[KEYSIZE];
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;
64 /* Static variables used to communicate between the conversation function
65 * and the server_login function
67 static pam_handle_t *pamh = NULL;
68 static char *PAM_username;
69 static char *PAM_password;
71 /* PAM conversation function
72 * Here we assume (for now, at least) that echo on means login name, and
73 * echo off means password.
75 static int PAM_conv (int num_msg,
76 const struct pam_message **msg,
77 struct pam_response **resp,
80 struct pam_response *reply;
82 #define COPY_STRING(s) (s) ? strdup(s) : NULL
88 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
94 reply = (struct pam_response *)
95 calloc(num_msg, sizeof(struct pam_response));
99 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
105 for (count = 0; count < num_msg; count++) {
108 switch (msg[count]->msg_style) {
109 case PAM_PROMPT_ECHO_ON:
110 if (!(string = COPY_STRING(PAM_username))) {
112 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
118 case PAM_PROMPT_ECHO_OFF:
119 if (!(string = COPY_STRING(PAM_password))) {
121 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
128 #ifdef PAM_BINARY_PROMPT
129 case PAM_BINARY_PROMPT:
130 #endif /* PAM_BINARY_PROMPT */
136 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
143 reply[count].resp_retcode = 0;
144 reply[count].resp = string;
151 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
156 for (count = 0; count < num_msg; count++) {
157 if (!reply[count].resp)
159 switch (msg[count]->msg_style) {
160 case PAM_PROMPT_ECHO_OFF:
161 case PAM_PROMPT_ECHO_ON:
162 free(reply[count].resp);
168 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
174 static struct pam_conv PAM_conversation = {
180 static int dhx_setup(void *obj, char *ibuf, int ibuflen,
181 char *rbuf, int *rbuflen)
185 BIGNUM *bn, *gbn, *pbn;
188 /* TODO: seed dhx_setup properly... this is a hack */
190 /* *SEVERE* hack... fix */
191 RAND_load_file("/var/adm/messages", KEYSIZE);
194 /* get the client's public key */
195 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
197 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
204 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
207 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
213 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
217 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
223 /* okay, we're ready */
224 if (!(dh = DH_new())) {
229 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
235 /* generate key and make sure that we have enough space */
238 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
240 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s",
246 /* figure out the key. store the key in rbuf for now. */
247 i = DH_compute_key(rbuf, bn, dh);
250 CAST_set_key(&castkey, i, rbuf);
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);
259 BN_bn2bin(dh->pub_key, rbuf);
263 /* buffer to be encrypted */
265 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
269 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
274 memcpy(rbuf, &randbuf, sizeof(randbuf));
276 /* get the signature. it's always 16 bytes. */
278 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
279 (void *) &buf, NULL) < 0) {
282 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
287 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
289 memset(rbuf + KEYSIZE, 0, KEYSIZE);
292 /* encrypt using cast */
293 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
295 *rbuflen += CRYPTBUFLEN;
298 return AFPERR_AUTHCONT;
304 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
311 /* dhx login: things are done in a slightly bizarre order to avoid
312 * having to clean things up if there's an error. */
313 static int pam_login(void *obj, struct passwd **uam_pwd,
314 char *ibuf, int ibuflen,
315 char *rbuf, int *rbuflen)
322 /* grab some of the options */
323 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
326 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
332 len = (unsigned char) *ibuf++;
335 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
338 return( AFPERR_PARAM );
341 memcpy(buf, ibuf, len );
344 if ((unsigned long) ibuf & 1) /* pad to even boundary */
347 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
349 LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
355 LOG(log_info, logtype_uams, "dhx login: %s", buf);
356 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
360 static int pam_logincont(void *obj, struct passwd **uam_pwd,
361 char *ibuf, int ibuflen,
362 char *rbuf, int *rbuflen)
365 BIGNUM *bn1, *bn2, *bn3;
371 /* check for session id */
372 memcpy(&sessid, ibuf, sizeof(sessid));
373 if (sessid != dhxhash(obj)) {
375 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
380 ibuf += sizeof(sessid);
382 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
383 (void *) &hostname, NULL) < 0)
385 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
389 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
390 msg3_iv, CAST_DECRYPT);
391 memset(&castkey, 0, sizeof(castkey));
393 /* check to make sure that the random number is the same. we
394 * get sent back an incremented random number. */
395 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
398 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
403 /* zero out the random number */
404 memset(rbuf, 0, sizeof(randbuf));
405 memset(randbuf, 0, sizeof(randbuf));
408 if (!(bn3 = BN_new())) {
414 BN_sub(bn3, bn1, bn2);
418 /* okay. is it one more? */
419 if (!BN_is_one(bn3)) {
425 /* Set these things up for the conv function */
426 rbuf[PASSWDLEN] = '\0';
429 err = AFPERR_NOTAUTH;
430 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
432 if (PAM_error != PAM_SUCCESS) {
434 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
435 pam_strerror(pamh,PAM_error));
440 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
441 pam_set_item(pamh, PAM_TTY, "afpd");
442 pam_set_item(pamh, PAM_RHOST, hostname);
443 PAM_error = pam_authenticate(pamh,0);
444 if (PAM_error != PAM_SUCCESS) {
445 if (PAM_error == PAM_MAXTRIES)
446 err = AFPERR_PWDEXPR;
448 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
449 pam_strerror(pamh, PAM_error));
454 PAM_error = pam_acct_mgmt(pamh, 0);
455 if (PAM_error != PAM_SUCCESS) {
456 if (PAM_error == PAM_ACCT_EXPIRED)
457 err = AFPERR_PWDEXPR;
458 #ifdef PAM_AUTHTOKEN_REQD
459 else if (PAM_error == PAM_AUTHTOKEN_REQD)
460 err = AFPERR_PWDCHNG;
463 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
464 pam_strerror(pamh, PAM_error));
469 #ifndef PAM_CRED_ESTABLISH
470 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
472 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
473 if (PAM_error != PAM_SUCCESS) {
475 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
476 pam_strerror(pamh, PAM_error));
481 PAM_error = pam_open_session(pamh, 0);
482 if (PAM_error != PAM_SUCCESS) {
484 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
485 pam_strerror(pamh, PAM_error));
490 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
493 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
498 pam_end(pamh, PAM_error);
500 memset(rbuf, 0, CRYPT2BUFLEN);
505 static void pam_logout() {
506 pam_close_session(pamh, 0);
512 /* change pw for dhx needs a couple passes to get everything all
513 * right. basically, it's like the login/logincont sequence */
514 static int pam_changepw(void *obj, char *username,
515 struct passwd *pwd, char *ibuf, int ibuflen,
516 char *rbuf, int *rbuflen)
518 BIGNUM *bn1, *bn2, *bn3;
527 memcpy(&sessid, ibuf, sizeof(sessid));
528 ibuf += sizeof(sessid);
530 if (!sessid) { /* no sessid -> initialization phase */
531 PAM_username = username;
532 ibuflen -= sizeof(sessid);
533 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
537 /* otherwise, it's like logincont but different. */
539 /* check out the session id */
540 if (sessid != dhxhash(obj)) {
542 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
548 /* we need this for pam */
549 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
550 (void *) &hostname, NULL) < 0) {
552 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
558 /* grab the client's nonce, old password, and new password. */
559 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
560 msg3_iv, CAST_DECRYPT);
561 memset(&castkey, 0, sizeof(castkey));
563 /* check to make sure that the random number is the same. we
564 * get sent back an incremented random number. */
565 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
567 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
573 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
576 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
582 /* zero out the random number */
583 memset(rbuf, 0, sizeof(randbuf));
584 memset(randbuf, 0, sizeof(randbuf));
586 if (!(bn3 = BN_new())) {
590 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
596 BN_sub(bn3, bn1, bn2);
600 /* okay. is it one more? */
602 if (!BN_is_one(bn3)) {
605 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
613 /* Set these things up for the conv function. the old password
616 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
617 PAM_password = ibuf + PASSWDLEN;
619 PAM_error = pam_start("netatalk", username, &PAM_conversation,
621 if (PAM_error != PAM_SUCCESS) {
623 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
628 pam_set_item(lpamh, PAM_TTY, "afpd");
629 pam_set_item(lpamh, PAM_RHOST, hostname);
631 /* we might need to do this as root */
634 PAM_error = pam_authenticate(lpamh, 0);
635 if (PAM_error != PAM_SUCCESS) {
637 pam_end(lpamh, PAM_error);
638 return AFPERR_NOTAUTH;
641 /* clear out old passwd */
642 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
646 ibuf[PASSWDLEN] = '\0';
648 /* this really does need to be done as root */
649 PAM_error = pam_chauthtok(lpamh, 0);
650 seteuid(uid); /* un-root ourselves. */
651 memset(ibuf, 0, PASSWDLEN);
652 if (PAM_error != PAM_SUCCESS) {
653 pam_end(lpamh, PAM_error);
654 return AFPERR_ACCESS;
662 static int uam_setup(const char *path)
664 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
665 pam_logincont, pam_logout) < 0)
668 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
670 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
674 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
680 static void uam_cleanup(void)
682 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
683 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
684 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
687 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
690 uam_setup, uam_cleanup
693 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
696 uam_setup, uam_cleanup
699 #endif /* USE_PAM && UAM_DHX */