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>
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 u_int8_t randbuf[KEYSIZE];
63 /* diffie-hellman bits */
64 static unsigned char msg2_iv[] = "CJalbert";
65 static unsigned char msg3_iv[] = "LWallace";
66 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
67 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
68 static const u_int8_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,
83 const struct pam_message **msg,
84 struct pam_response **resp,
85 void *appdata_ptr _U_) {
87 struct pam_response *reply;
89 #define COPY_STRING(s) (s) ? strdup(s) : NULL
95 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
101 reply = (struct pam_response *)
102 calloc(num_msg, sizeof(struct pam_response));
106 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
112 for (count = 0; count < num_msg; count++) {
115 switch (msg[count]->msg_style) {
116 case PAM_PROMPT_ECHO_ON:
117 if (!(string = COPY_STRING(PAM_username))) {
119 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
125 case PAM_PROMPT_ECHO_OFF:
126 if (!(string = COPY_STRING(PAM_password))) {
128 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
135 #ifdef PAM_BINARY_PROMPT
136 case PAM_BINARY_PROMPT:
137 #endif /* PAM_BINARY_PROMPT */
143 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
150 reply[count].resp_retcode = 0;
151 reply[count].resp = string;
158 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
163 for (count = 0; count < num_msg; count++) {
164 if (!reply[count].resp)
166 switch (msg[count]->msg_style) {
167 case PAM_PROMPT_ECHO_OFF:
168 case PAM_PROMPT_ECHO_ON:
169 free(reply[count].resp);
175 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
181 static struct pam_conv PAM_conversation = {
187 static int dhx_setup(void *obj, char *ibuf, size_t ibuflen _U_,
188 char *rbuf, size_t *rbuflen)
192 BIGNUM *bn, *gbn, *pbn;
195 /* get the client's public key */
196 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
198 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
205 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
208 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
214 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
218 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
224 /* okay, we're ready */
225 if (!(dh = DH_new())) {
230 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
236 /* generate key and make sure that we have enough space */
239 if (DH_generate_key(dh) == 0) {
240 unsigned long dherror;
243 ERR_load_crypto_strings();
244 dherror = ERR_get_error();
245 ERR_error_string_n(dherror, errbuf, 256);
247 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key (OpenSSL error code: %u, %s)", dherror, errbuf);
252 if (BN_num_bytes(dh->pub_key) > KEYSIZE) {
253 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s", strerror(errno));
257 /* figure out the key. store the key in rbuf for now. */
258 i = DH_compute_key(rbuf, bn, dh);
261 CAST_set_key(&castkey, i, rbuf);
263 /* session id. it's just a hashed version of the object pointer. */
264 sessid = dhxhash(obj);
265 memcpy(rbuf, &sessid, sizeof(sessid));
266 rbuf += sizeof(sessid);
267 *rbuflen += sizeof(sessid);
270 BN_bn2bin(dh->pub_key, rbuf);
274 /* buffer to be encrypted */
276 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
280 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
285 memcpy(rbuf, &randbuf, sizeof(randbuf));
287 /* get the signature. it's always 16 bytes. */
289 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
290 (void *) &buf, NULL) < 0) {
293 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
298 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
300 memset(rbuf + KEYSIZE, 0, KEYSIZE);
303 /* encrypt using cast */
304 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
306 *rbuflen += CRYPTBUFLEN;
309 return AFPERR_AUTHCONT;
315 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
321 /* -------------------------------- */
322 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
323 char *ibuf, size_t ibuflen,
324 char *rbuf, size_t *rbuflen)
326 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
327 LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
328 return AFPERR_NOTAUTH;
331 PAM_username = username;
332 LOG(log_info, logtype_uams, "dhx login: %s", username);
333 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
336 /* -------------------------------- */
337 /* dhx login: things are done in a slightly bizarre order to avoid
338 * having to clean things up if there's an error. */
339 static int pam_login(void *obj, struct passwd **uam_pwd,
340 char *ibuf, size_t ibuflen,
341 char *rbuf, size_t *rbuflen)
348 /* grab some of the options */
349 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
350 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
355 len = (unsigned char) *ibuf++;
357 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
362 memcpy(username, ibuf, len );
364 username[ len ] = '\0';
366 if ((unsigned long) ibuf & 1) /* pad to even boundary */
369 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
372 /* ----------------------------- */
373 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
374 char *ibuf, size_t ibuflen,
375 char *rbuf, size_t *rbuflen)
383 /* grab some of the options */
384 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
385 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
393 memcpy(&temp16, uname, sizeof(temp16));
396 if ( !len || len > ulen ) {
397 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
401 memcpy(username, uname +2, len );
402 username[ len ] = '\0';
404 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
407 /* -------------------------------- */
409 static int pam_logincont(void *obj, struct passwd **uam_pwd,
410 char *ibuf, size_t ibuflen _U_,
411 char *rbuf, size_t *rbuflen)
413 const char *hostname;
414 BIGNUM *bn1, *bn2, *bn3;
420 /* check for session id */
421 memcpy(&sessid, ibuf, sizeof(sessid));
422 if (sessid != dhxhash(obj)) {
424 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
429 ibuf += sizeof(sessid);
431 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
432 (void *) &hostname, NULL) < 0)
434 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
438 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
439 msg3_iv, CAST_DECRYPT);
440 memset(&castkey, 0, sizeof(castkey));
442 /* check to make sure that the random number is the same. we
443 * get sent back an incremented random number. */
444 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
447 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
452 /* zero out the random number */
453 memset(rbuf, 0, sizeof(randbuf));
454 memset(randbuf, 0, sizeof(randbuf));
457 if (!(bn3 = BN_new())) {
463 BN_sub(bn3, bn1, bn2);
467 /* okay. is it one more? */
468 if (!BN_is_one(bn3)) {
474 /* Set these things up for the conv function */
475 rbuf[PASSWDLEN] = '\0';
478 err = AFPERR_NOTAUTH;
479 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
481 if (PAM_error != PAM_SUCCESS) {
483 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
484 pam_strerror(pamh,PAM_error));
489 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
490 pam_set_item(pamh, PAM_TTY, "afpd");
491 pam_set_item(pamh, PAM_RHOST, hostname);
492 PAM_error = pam_authenticate(pamh,0);
493 if (PAM_error != PAM_SUCCESS) {
494 if (PAM_error == PAM_MAXTRIES)
495 err = AFPERR_PWDEXPR;
497 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
498 pam_strerror(pamh, PAM_error));
503 PAM_error = pam_acct_mgmt(pamh, 0);
504 if (PAM_error != PAM_SUCCESS ) {
506 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
507 pam_strerror(pamh, PAM_error));
509 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* password expired */
510 err = AFPERR_PWDEXPR;
511 #ifdef PAM_AUTHTOKEN_REQD
512 else if (PAM_error == PAM_AUTHTOKEN_REQD)
513 err = AFPERR_PWDCHNG;
519 #ifndef PAM_CRED_ESTABLISH
520 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
522 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
523 if (PAM_error != PAM_SUCCESS) {
525 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
526 pam_strerror(pamh, PAM_error));
531 PAM_error = pam_open_session(pamh, 0);
532 if (PAM_error != PAM_SUCCESS) {
534 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
535 pam_strerror(pamh, PAM_error));
540 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
543 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
545 if ( err == AFPERR_PWDEXPR)
550 pam_end(pamh, PAM_error);
552 memset(rbuf, 0, CRYPT2BUFLEN);
557 static void pam_logout(void) {
558 pam_close_session(pamh, 0);
564 /* change pw for dhx needs a couple passes to get everything all
565 * right. basically, it's like the login/logincont sequence */
566 static int pam_changepw(void *obj, char *username,
567 struct passwd *pwd _U_, char *ibuf, size_t ibuflen,
568 char *rbuf, size_t *rbuflen)
570 BIGNUM *bn1, *bn2, *bn3;
578 if (ibuflen < sizeof(sessid)) {
583 memcpy(&sessid, ibuf, sizeof(sessid));
584 ibuf += sizeof(sessid);
586 if (!sessid) { /* no sessid -> initialization phase */
587 PAM_username = username;
588 ibuflen -= sizeof(sessid);
589 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
593 /* otherwise, it's like logincont but different. */
595 /* check out the session id */
596 if (sessid != dhxhash(obj)) {
598 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
604 /* we need this for pam */
605 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
606 (void *) &hostname, NULL) < 0) {
608 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
614 /* grab the client's nonce, old password, and new password. */
615 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
616 msg3_iv, CAST_DECRYPT);
617 memset(&castkey, 0, sizeof(castkey));
619 /* check to make sure that the random number is the same. we
620 * get sent back an incremented random number. */
621 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
623 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
629 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
632 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
638 /* zero out the random number */
639 memset(rbuf, 0, sizeof(randbuf));
640 memset(randbuf, 0, sizeof(randbuf));
642 if (!(bn3 = BN_new())) {
646 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
652 BN_sub(bn3, bn1, bn2);
656 /* okay. is it one more? */
658 if (!BN_is_one(bn3)) {
661 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
669 /* Set these things up for the conv function. the old password
672 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
673 PAM_password = ibuf + PASSWDLEN;
675 PAM_error = pam_start("netatalk", username, &PAM_conversation,
677 if (PAM_error != PAM_SUCCESS) {
679 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
684 pam_set_item(lpamh, PAM_TTY, "afpd");
685 pam_set_item(lpamh, PAM_RHOST, hostname);
687 /* we might need to do this as root */
690 PAM_error = pam_authenticate(lpamh, 0);
691 if (PAM_error != PAM_SUCCESS) {
693 pam_end(lpamh, PAM_error);
694 return AFPERR_NOTAUTH;
697 /* clear out old passwd */
698 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
702 ibuf[PASSWDLEN] = '\0';
704 /* this really does need to be done as root */
705 PAM_error = pam_chauthtok(lpamh, 0);
706 seteuid(uid); /* un-root ourselves. */
707 memset(ibuf, 0, PASSWDLEN);
708 if (PAM_error != PAM_SUCCESS) {
709 pam_end(lpamh, PAM_error);
710 return AFPERR_ACCESS;
718 static int uam_setup(const char *path)
720 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login,
721 pam_logincont, pam_logout, pam_login_ext) < 0)
724 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
726 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
730 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
736 static void uam_cleanup(void)
738 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
739 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
740 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
743 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
746 uam_setup, uam_cleanup
749 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
752 uam_setup, uam_cleanup
755 #endif /* USE_PAM && UAM_DHX */