2 * $Id: uams_dhx_pam.c,v 1.16 2001-09-05 13:42:16 rufustfirefly 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)
21 #endif /* HAVE_UNISTD_H */
24 #include <security/pam_appl.h>
27 #include <openssl/bn.h>
28 #include <openssl/dh.h>
29 #include <openssl/cast.h>
30 #else /* OPENSSL_DHX */
34 #endif /* OPENSSL_DHX */
36 #include <atalk/afp.h>
37 #include <atalk/uam.h>
41 #define CRYPTBUFLEN (KEYSIZE*2)
42 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
43 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
45 /* hash a number to a 16-bit quantity */
46 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
47 (unsigned long) (a)) & 0xffff)
50 static CAST_KEY castkey;
51 static struct passwd *dhxpwd;
52 static u_int8_t randbuf[KEYSIZE];
54 /* diffie-hellman bits */
55 static unsigned char msg2_iv[] = "CJalbert";
56 static unsigned char msg3_iv[] = "LWallace";
57 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
58 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
59 static const u_int8_t g = 0x07;
62 /* Static variables used to communicate between the conversation function
63 * and the server_login function
65 static pam_handle_t *pamh = NULL;
66 static char *PAM_username;
67 static char *PAM_password;
69 /* PAM conversation function
70 * Here we assume (for now, at least) that echo on means login name, and
71 * echo off means password.
73 static int PAM_conv (int num_msg,
74 const struct pam_message **msg,
75 struct pam_response **resp,
78 struct pam_response *reply;
80 #define COPY_STRING(s) (s) ? strdup(s) : NULL
86 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DHX Conversation Err -- %m");
91 reply = (struct pam_response *)
92 calloc(num_msg, sizeof(struct pam_response));
96 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DHX Conversation Err -- %m");
101 for (count = 0; count < num_msg; count++) {
104 switch (msg[count]->msg_style) {
105 case PAM_PROMPT_ECHO_ON:
106 if (!(string = COPY_STRING(PAM_username))) {
108 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: username failure -- %m");
113 case PAM_PROMPT_ECHO_OFF:
114 if (!(string = COPY_STRING(PAM_password))) {
116 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: passwd failure: --: %m");
122 #ifdef PAM_BINARY_PROMPT
123 case PAM_BINARY_PROMPT:
124 #endif /* PAM_BINARY_PROMPT */
130 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Binary_Prompt -- %m");
136 reply[count].resp_retcode = 0;
137 reply[count].resp = string;
144 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Success -- %m");
149 for (count = 0; count < num_msg; count++) {
150 if (!reply[count].resp)
152 switch (msg[count]->msg_style) {
153 case PAM_PROMPT_ECHO_OFF:
154 case PAM_PROMPT_ECHO_ON:
155 free(reply[count].resp);
161 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DHX Conversation Err -- %m");
166 static struct pam_conv PAM_conversation = {
172 static int dhx_setup(void *obj, char *ibuf, int ibuflen,
173 char *rbuf, int *rbuflen)
177 BIGNUM *bn, *gbn, *pbn;
180 /* TODO: seed dhx_setup properly... this is a hack */
182 /* *SEVERE* hack... fix */
183 RAND_load_file("/var/adm/messages", KEYSIZE);
186 /* get the client's public key */
187 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
189 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Public Key -- %m");
195 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
198 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Primes: GBN -- %m");
203 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
207 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Primes: PBN -- %m");
212 /* okay, we're ready */
213 if (!(dh = DH_new())) {
218 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %m");
223 /* generate key and make sure that we have enough space */
226 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
228 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %m");
233 /* figure out the key. store the key in rbuf for now. */
234 i = DH_compute_key(rbuf, bn, dh);
237 CAST_set_key(&castkey, i, rbuf);
239 /* session id. it's just a hashed version of the object pointer. */
240 sessid = dhxhash(obj);
241 memcpy(rbuf, &sessid, sizeof(sessid));
242 rbuf += sizeof(sessid);
243 *rbuflen += sizeof(sessid);
246 BN_bn2bin(dh->pub_key, rbuf);
250 /* buffer to be encrypted */
252 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
256 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %m");
260 memcpy(rbuf, &randbuf, sizeof(randbuf));
262 /* get the signature. it's always 16 bytes. */
264 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
265 (void *) &buf, NULL) < 0) {
268 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
272 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
274 memset(rbuf + KEYSIZE, 0, KEYSIZE);
277 /* encrypt using cast */
278 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
280 *rbuflen += CRYPTBUFLEN;
283 return AFPERR_AUTHCONT;
289 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %m");
295 /* dhx login: things are done in a slightly bizarre order to avoid
296 * having to clean things up if there's an error. */
297 static int pam_login(void *obj, struct passwd **uam_pwd,
298 char *ibuf, int ibuflen,
299 char *rbuf, int *rbuflen)
306 /* grab some of the options */
307 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
310 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %m");
315 len = (unsigned char) *ibuf++;
318 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
320 return( AFPERR_PARAM );
323 memcpy(buf, ibuf, len );
326 if ((unsigned long) ibuf & 1) /* pad to even boundary */
329 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
331 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: User entered a null value -- %m");
337 syslog( LOG_INFO, "dhx login: %s", buf);
338 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
342 static int pam_logincont(void *obj, struct passwd **uam_pwd,
343 char *ibuf, int ibuflen,
344 char *rbuf, int *rbuflen)
347 BIGNUM *bn1, *bn2, *bn3;
353 /* check for session id */
354 memcpy(&sessid, ibuf, sizeof(sessid));
355 if (sessid != dhxhash(obj)) {
357 syslog(LOG_INFO, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %m");
361 ibuf += sizeof(sessid);
363 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
364 (void *) &hostname, NULL) < 0)
367 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
368 msg3_iv, CAST_DECRYPT);
369 memset(&castkey, 0, sizeof(castkey));
371 /* check to make sure that the random number is the same. we
372 * get sent back an incremented random number. */
373 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
376 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
381 /* zero out the random number */
382 memset(rbuf, 0, sizeof(randbuf));
383 memset(randbuf, 0, sizeof(randbuf));
386 if (!(bn3 = BN_new())) {
392 BN_sub(bn3, bn1, bn2);
396 /* okay. is it one more? */
397 if (!BN_is_one(bn3)) {
403 /* Set these things up for the conv function */
404 rbuf[PASSWDLEN] = '\0';
407 err = AFPERR_NOTAUTH;
408 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
410 if (PAM_error != PAM_SUCCESS) {
412 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh,PAM_error));
417 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
418 pam_set_item(pamh, PAM_TTY, "afpd");
419 pam_set_item(pamh, PAM_RHOST, hostname);
420 PAM_error = pam_authenticate(pamh,0);
421 if (PAM_error != PAM_SUCCESS) {
422 if (PAM_error == PAM_MAXTRIES)
423 err = AFPERR_PWDEXPR;
425 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
430 PAM_error = pam_acct_mgmt(pamh, 0);
431 if (PAM_error != PAM_SUCCESS) {
432 if (PAM_error == PAM_ACCT_EXPIRED)
433 err = AFPERR_PWDEXPR;
434 #ifdef PAM_AUTHTOKEN_REQD
435 else if (PAM_error == PAM_AUTHTOKEN_REQD)
436 err = AFPERR_PWDCHNG;
439 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
444 #ifndef PAM_CRED_ESTABLISH
445 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
447 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
448 if (PAM_error != PAM_SUCCESS) {
450 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
455 PAM_error = pam_open_session(pamh, 0);
456 if (PAM_error != PAM_SUCCESS) {
458 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
463 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
466 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!");
471 pam_end(pamh, PAM_error);
473 memset(rbuf, 0, CRYPT2BUFLEN);
478 static void pam_logout() {
479 pam_close_session(pamh, 0);
485 /* change pw for dhx needs a couple passes to get everything all
486 * right. basically, it's like the login/logincont sequence */
487 static int pam_changepw(void *obj, char *username,
488 struct passwd *pwd, char *ibuf, int ibuflen,
489 char *rbuf, int *rbuflen)
491 BIGNUM *bn1, *bn2, *bn3;
500 memcpy(&sessid, ibuf, sizeof(sessid));
501 ibuf += sizeof(sessid);
503 if (!sessid) { /* no sessid -> initialization phase */
504 PAM_username = username;
505 ibuflen -= sizeof(sessid);
506 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
510 /* otherwise, it's like logincont but different. */
512 /* check out the session id */
513 if (sessid != dhxhash(obj)) {
515 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m");
520 /* we need this for pam */
521 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
522 (void *) &hostname, NULL) < 0) {
524 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m");
529 /* grab the client's nonce, old password, and new password. */
530 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
531 msg3_iv, CAST_DECRYPT);
532 memset(&castkey, 0, sizeof(castkey));
534 /* check to make sure that the random number is the same. we
535 * get sent back an incremented random number. */
536 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
538 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m");
543 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
546 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m");
551 /* zero out the random number */
552 memset(rbuf, 0, sizeof(randbuf));
553 memset(randbuf, 0, sizeof(randbuf));
555 if (!(bn3 = BN_new())) {
559 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m");
564 BN_sub(bn3, bn1, bn2);
568 /* okay. is it one more? */
570 if (!BN_is_one(bn3)) {
573 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m");
580 /* Set these things up for the conv function. the old password
583 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
584 PAM_password = ibuf + PASSWDLEN;
586 PAM_error = pam_start("netatalk", username, &PAM_conversation,
588 if (PAM_error != PAM_SUCCESS) {
590 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m");
594 pam_set_item(lpamh, PAM_TTY, "afpd");
595 pam_set_item(lpamh, PAM_RHOST, hostname);
597 /* we might need to do this as root */
600 PAM_error = pam_authenticate(lpamh, 0);
601 if (PAM_error != PAM_SUCCESS) {
603 pam_end(lpamh, PAM_error);
604 return AFPERR_NOTAUTH;
607 /* clear out old passwd */
608 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
612 ibuf[PASSWDLEN] = '\0';
614 /* this really does need to be done as root */
615 PAM_error = pam_chauthtok(lpamh, 0);
616 seteuid(uid); /* un-root ourselves. */
617 memset(ibuf, 0, PASSWDLEN);
618 if (PAM_error != PAM_SUCCESS) {
619 pam_end(lpamh, PAM_error);
620 return AFPERR_ACCESS;
628 static int uam_setup(const char *path)
630 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
631 pam_logincont, pam_logout) < 0)
634 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
636 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
640 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
646 static void uam_cleanup(void)
648 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
649 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
650 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
653 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
656 uam_setup, uam_cleanup
659 #endif /* USE_PAM && UAM_DHX */