2 * $Id: uams_dhx_pam.c,v 1.28 2008-12-03 19:15:06 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>
37 #else /* OPENSSL_DHX */
41 #endif /* OPENSSL_DHX */
43 #include <atalk/afp.h>
44 #include <atalk/uam.h>
48 #define CRYPTBUFLEN (KEYSIZE*2)
49 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
50 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
52 /* hash a number to a 16-bit quantity */
53 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
54 (unsigned long) (a)) & 0xffff)
57 static CAST_KEY castkey;
58 static struct passwd *dhxpwd;
59 static u_int8_t randbuf[KEYSIZE];
61 /* diffie-hellman bits */
62 static unsigned char msg2_iv[] = "CJalbert";
63 static unsigned char msg3_iv[] = "LWallace";
64 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
65 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
66 static const u_int8_t g = 0x07;
69 /* Static variables used to communicate between the conversation function
70 * and the server_login function
72 static pam_handle_t *pamh = NULL;
73 static char *PAM_username;
74 static char *PAM_password;
76 /* PAM conversation function
77 * Here we assume (for now, at least) that echo on means login name, and
78 * echo off means password.
80 static int PAM_conv (int num_msg,
81 const struct pam_message **msg,
82 struct pam_response **resp,
83 void *appdata_ptr _U_) {
85 struct pam_response *reply;
87 #define COPY_STRING(s) (s) ? strdup(s) : NULL
93 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
99 reply = (struct pam_response *)
100 calloc(num_msg, sizeof(struct pam_response));
104 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
110 for (count = 0; count < num_msg; count++) {
113 switch (msg[count]->msg_style) {
114 case PAM_PROMPT_ECHO_ON:
115 if (!(string = COPY_STRING(PAM_username))) {
117 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: username failure -- %s",
123 case PAM_PROMPT_ECHO_OFF:
124 if (!(string = COPY_STRING(PAM_password))) {
126 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: passwd failure: --: %s",
133 #ifdef PAM_BINARY_PROMPT
134 case PAM_BINARY_PROMPT:
135 #endif /* PAM_BINARY_PROMPT */
141 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Binary_Prompt -- %s",
148 reply[count].resp_retcode = 0;
149 reply[count].resp = string;
156 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Success");
161 for (count = 0; count < num_msg; count++) {
162 if (!reply[count].resp)
164 switch (msg[count]->msg_style) {
165 case PAM_PROMPT_ECHO_OFF:
166 case PAM_PROMPT_ECHO_ON:
167 free(reply[count].resp);
173 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DHX Conversation Err -- %s",
179 static struct pam_conv PAM_conversation = {
185 static int dhx_setup(void *obj, char *ibuf, int ibuflen _U_,
186 char *rbuf, int *rbuflen)
190 BIGNUM *bn, *gbn, *pbn;
193 /* get the client's public key */
194 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
196 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Public Key -- %s",
203 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
206 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: GBN -- %s",
212 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
216 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM No Primes: PBN -- %s",
222 /* okay, we're ready */
223 if (!(dh = DH_new())) {
228 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %s",
234 /* generate key and make sure that we have enough space */
237 if (DH_generate_key(dh) == 0) {
238 unsigned long dherror;
241 ERR_load_crypto_strings();
242 dherror = ERR_get_error();
243 ERR_error_string_n(dherror, errbuf, 256);
245 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key (OpenSSL error code: %u, %s)", dherror, errbuf);
250 if (BN_num_bytes(dh->pub_key) > KEYSIZE) {
251 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %s", strerror(errno));
255 /* figure out the key. store the key in rbuf for now. */
256 i = DH_compute_key(rbuf, bn, dh);
259 CAST_set_key(&castkey, i, rbuf);
261 /* session id. it's just a hashed version of the object pointer. */
262 sessid = dhxhash(obj);
263 memcpy(rbuf, &sessid, sizeof(sessid));
264 rbuf += sizeof(sessid);
265 *rbuflen += sizeof(sessid);
268 BN_bn2bin(dh->pub_key, rbuf);
272 /* buffer to be encrypted */
274 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
278 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %s",
283 memcpy(rbuf, &randbuf, sizeof(randbuf));
285 /* get the signature. it's always 16 bytes. */
287 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
288 (void *) &buf, NULL) < 0) {
291 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
296 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
298 memset(rbuf + KEYSIZE, 0, KEYSIZE);
301 /* encrypt using cast */
302 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
304 *rbuflen += CRYPTBUFLEN;
307 return AFPERR_AUTHCONT;
313 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %s",
319 /* -------------------------------- */
320 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
321 char *ibuf, int ibuflen,
322 char *rbuf, int *rbuflen)
324 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
325 LOG(log_info, logtype_uams, "uams_dhx_pam.c: unknown username");
329 PAM_username = username;
330 LOG(log_info, logtype_uams, "dhx login: %s", username);
331 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
334 /* -------------------------------- */
335 /* dhx login: things are done in a slightly bizarre order to avoid
336 * having to clean things up if there's an error. */
337 static int pam_login(void *obj, struct passwd **uam_pwd,
338 char *ibuf, int ibuflen,
339 char *rbuf, int *rbuflen)
346 /* grab some of the options */
347 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
348 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
353 len = (unsigned char) *ibuf++;
355 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %s",
360 memcpy(username, ibuf, len );
362 username[ len ] = '\0';
364 if ((unsigned long) ibuf & 1) /* pad to even boundary */
367 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
370 /* ----------------------------- */
371 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
372 char *ibuf, int ibuflen,
373 char *rbuf, int *rbuflen)
381 /* grab some of the options */
382 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
383 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %s",
391 memcpy(&temp16, uname, sizeof(temp16));
394 if ( !len || len > ulen ) {
395 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Signature Retrieval Failure -- %s",
399 memcpy(username, uname +2, len );
400 username[ len ] = '\0';
402 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
405 /* -------------------------------- */
407 static int pam_logincont(void *obj, struct passwd **uam_pwd,
408 char *ibuf, int ibuflen _U_,
409 char *rbuf, int *rbuflen)
412 BIGNUM *bn1, *bn2, *bn3;
418 /* check for session id */
419 memcpy(&sessid, ibuf, sizeof(sessid));
420 if (sessid != dhxhash(obj)) {
422 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %s",
427 ibuf += sizeof(sessid);
429 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
430 (void *) &hostname, NULL) < 0)
432 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
436 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
437 msg3_iv, CAST_DECRYPT);
438 memset(&castkey, 0, sizeof(castkey));
440 /* check to make sure that the random number is the same. we
441 * get sent back an incremented random number. */
442 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
445 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
450 /* zero out the random number */
451 memset(rbuf, 0, sizeof(randbuf));
452 memset(randbuf, 0, sizeof(randbuf));
455 if (!(bn3 = BN_new())) {
461 BN_sub(bn3, bn1, bn2);
465 /* okay. is it one more? */
466 if (!BN_is_one(bn3)) {
472 /* Set these things up for the conv function */
473 rbuf[PASSWDLEN] = '\0';
476 err = AFPERR_NOTAUTH;
477 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
479 if (PAM_error != PAM_SUCCESS) {
481 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
482 pam_strerror(pamh,PAM_error));
487 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
488 pam_set_item(pamh, PAM_TTY, "afpd");
489 pam_set_item(pamh, PAM_RHOST, hostname);
490 PAM_error = pam_authenticate(pamh,0);
491 if (PAM_error != PAM_SUCCESS) {
492 if (PAM_error == PAM_MAXTRIES)
493 err = AFPERR_PWDEXPR;
495 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
496 pam_strerror(pamh, PAM_error));
501 PAM_error = pam_acct_mgmt(pamh, 0);
502 if (PAM_error != PAM_SUCCESS ) {
504 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
505 pam_strerror(pamh, PAM_error));
507 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* password expired */
508 err = AFPERR_PWDEXPR;
509 #ifdef PAM_AUTHTOKEN_REQD
510 else if (PAM_error == PAM_AUTHTOKEN_REQD)
511 err = AFPERR_PWDCHNG;
517 #ifndef PAM_CRED_ESTABLISH
518 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
520 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
521 if (PAM_error != PAM_SUCCESS) {
523 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM_Error: %s",
524 pam_strerror(pamh, PAM_error));
529 PAM_error = pam_open_session(pamh, 0);
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 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
541 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: PAM Auth OK!");
543 if ( err == AFPERR_PWDEXPR)
548 pam_end(pamh, PAM_error);
550 memset(rbuf, 0, CRYPT2BUFLEN);
555 static void pam_logout() {
556 pam_close_session(pamh, 0);
562 /* change pw for dhx needs a couple passes to get everything all
563 * right. basically, it's like the login/logincont sequence */
564 static int pam_changepw(void *obj, char *username,
565 struct passwd *pwd _U_, char *ibuf, int ibuflen,
566 char *rbuf, int *rbuflen)
568 BIGNUM *bn1, *bn2, *bn3;
577 memcpy(&sessid, ibuf, sizeof(sessid));
578 ibuf += sizeof(sessid);
580 if (!sessid) { /* no sessid -> initialization phase */
581 PAM_username = username;
582 ibuflen -= sizeof(sessid);
583 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
587 /* otherwise, it's like logincont but different. */
589 /* check out the session id */
590 if (sessid != dhxhash(obj)) {
592 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %s",
598 /* we need this for pam */
599 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
600 (void *) &hostname, NULL) < 0) {
602 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Hostname Null?? -- %s",
608 /* grab the client's nonce, old password, and new password. */
609 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
610 msg3_iv, CAST_DECRYPT);
611 memset(&castkey, 0, sizeof(castkey));
613 /* check to make sure that the random number is the same. we
614 * get sent back an incremented random number. */
615 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
617 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %s",
623 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
626 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %s",
632 /* zero out the random number */
633 memset(rbuf, 0, sizeof(randbuf));
634 memset(randbuf, 0, sizeof(randbuf));
636 if (!(bn3 = BN_new())) {
640 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %s",
646 BN_sub(bn3, bn1, bn2);
650 /* okay. is it one more? */
652 if (!BN_is_one(bn3)) {
655 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %s",
663 /* Set these things up for the conv function. the old password
666 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
667 PAM_password = ibuf + PASSWDLEN;
669 PAM_error = pam_start("netatalk", username, &PAM_conversation,
671 if (PAM_error != PAM_SUCCESS) {
673 LOG(log_info, logtype_uams, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %s",
678 pam_set_item(lpamh, PAM_TTY, "afpd");
679 pam_set_item(lpamh, PAM_RHOST, hostname);
681 /* we might need to do this as root */
684 PAM_error = pam_authenticate(lpamh, 0);
685 if (PAM_error != PAM_SUCCESS) {
687 pam_end(lpamh, PAM_error);
688 return AFPERR_NOTAUTH;
691 /* clear out old passwd */
692 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
696 ibuf[PASSWDLEN] = '\0';
698 /* this really does need to be done as root */
699 PAM_error = pam_chauthtok(lpamh, 0);
700 seteuid(uid); /* un-root ourselves. */
701 memset(ibuf, 0, PASSWDLEN);
702 if (PAM_error != PAM_SUCCESS) {
703 pam_end(lpamh, PAM_error);
704 return AFPERR_ACCESS;
712 static int uam_setup(const char *path)
714 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128", pam_login,
715 pam_logincont, pam_logout, pam_login_ext) < 0)
718 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
720 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
724 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
730 static void uam_cleanup(void)
732 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
733 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
734 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
737 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
740 uam_setup, uam_cleanup
743 UAM_MODULE_EXPORT struct uam_export uams_dhx_pam = {
746 uam_setup, uam_cleanup
749 #endif /* USE_PAM && UAM_DHX */