2 * $Id: uams_dhx_pam.c,v 1.17 2001-11-13 15:01:38 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_CLIENTNAME,
364 (void *) &hostname, NULL) < 0)
366 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: unable to retrieve client hostname");
370 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
371 msg3_iv, CAST_DECRYPT);
372 memset(&castkey, 0, sizeof(castkey));
374 /* check to make sure that the random number is the same. we
375 * get sent back an incremented random number. */
376 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
379 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
384 /* zero out the random number */
385 memset(rbuf, 0, sizeof(randbuf));
386 memset(randbuf, 0, sizeof(randbuf));
389 if (!(bn3 = BN_new())) {
395 BN_sub(bn3, bn1, bn2);
399 /* okay. is it one more? */
400 if (!BN_is_one(bn3)) {
406 /* Set these things up for the conv function */
407 rbuf[PASSWDLEN] = '\0';
410 err = AFPERR_NOTAUTH;
411 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
413 if (PAM_error != PAM_SUCCESS) {
415 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh,PAM_error));
420 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
421 pam_set_item(pamh, PAM_TTY, "afpd");
422 pam_set_item(pamh, PAM_RHOST, hostname);
423 PAM_error = pam_authenticate(pamh,0);
424 if (PAM_error != PAM_SUCCESS) {
425 if (PAM_error == PAM_MAXTRIES)
426 err = AFPERR_PWDEXPR;
428 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
433 PAM_error = pam_acct_mgmt(pamh, 0);
434 if (PAM_error != PAM_SUCCESS) {
435 if (PAM_error == PAM_ACCT_EXPIRED)
436 err = AFPERR_PWDEXPR;
437 #ifdef PAM_AUTHTOKEN_REQD
438 else if (PAM_error == PAM_AUTHTOKEN_REQD)
439 err = AFPERR_PWDCHNG;
442 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
447 #ifndef PAM_CRED_ESTABLISH
448 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
450 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
451 if (PAM_error != PAM_SUCCESS) {
453 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
458 PAM_error = pam_open_session(pamh, 0);
459 if (PAM_error != PAM_SUCCESS) {
461 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
466 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
469 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!");
474 pam_end(pamh, PAM_error);
476 memset(rbuf, 0, CRYPT2BUFLEN);
481 static void pam_logout() {
482 pam_close_session(pamh, 0);
488 /* change pw for dhx needs a couple passes to get everything all
489 * right. basically, it's like the login/logincont sequence */
490 static int pam_changepw(void *obj, char *username,
491 struct passwd *pwd, char *ibuf, int ibuflen,
492 char *rbuf, int *rbuflen)
494 BIGNUM *bn1, *bn2, *bn3;
503 memcpy(&sessid, ibuf, sizeof(sessid));
504 ibuf += sizeof(sessid);
506 if (!sessid) { /* no sessid -> initialization phase */
507 PAM_username = username;
508 ibuflen -= sizeof(sessid);
509 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
513 /* otherwise, it's like logincont but different. */
515 /* check out the session id */
516 if (sessid != dhxhash(obj)) {
518 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m");
523 /* we need this for pam */
524 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
525 (void *) &hostname, NULL) < 0) {
527 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m");
532 /* grab the client's nonce, old password, and new password. */
533 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
534 msg3_iv, CAST_DECRYPT);
535 memset(&castkey, 0, sizeof(castkey));
537 /* check to make sure that the random number is the same. we
538 * get sent back an incremented random number. */
539 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
541 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m");
546 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
549 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m");
554 /* zero out the random number */
555 memset(rbuf, 0, sizeof(randbuf));
556 memset(randbuf, 0, sizeof(randbuf));
558 if (!(bn3 = BN_new())) {
562 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m");
567 BN_sub(bn3, bn1, bn2);
571 /* okay. is it one more? */
573 if (!BN_is_one(bn3)) {
576 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m");
583 /* Set these things up for the conv function. the old password
586 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
587 PAM_password = ibuf + PASSWDLEN;
589 PAM_error = pam_start("netatalk", username, &PAM_conversation,
591 if (PAM_error != PAM_SUCCESS) {
593 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m");
597 pam_set_item(lpamh, PAM_TTY, "afpd");
598 pam_set_item(lpamh, PAM_RHOST, hostname);
600 /* we might need to do this as root */
603 PAM_error = pam_authenticate(lpamh, 0);
604 if (PAM_error != PAM_SUCCESS) {
606 pam_end(lpamh, PAM_error);
607 return AFPERR_NOTAUTH;
610 /* clear out old passwd */
611 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
615 ibuf[PASSWDLEN] = '\0';
617 /* this really does need to be done as root */
618 PAM_error = pam_chauthtok(lpamh, 0);
619 seteuid(uid); /* un-root ourselves. */
620 memset(ibuf, 0, PASSWDLEN);
621 if (PAM_error != PAM_SUCCESS) {
622 pam_end(lpamh, PAM_error);
623 return AFPERR_ACCESS;
631 static int uam_setup(const char *path)
633 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
634 pam_logincont, pam_logout) < 0)
637 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
639 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
643 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
649 static void uam_cleanup(void)
651 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
652 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
653 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
656 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
659 uam_setup, uam_cleanup
662 #endif /* USE_PAM && UAM_DHX */