2 * $Id: uams_dhx_pam.c,v 1.15 2001-06-25 20:13:45 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:
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 /* get the client's public key */
181 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
183 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Public Key -- %m");
189 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
192 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Primes: GBN -- %m");
197 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
201 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Primes: PBN -- %m");
206 /* okay, we're ready */
207 if (!(dh = DH_new())) {
212 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %m");
217 /* generate key and make sure that we have enough space */
220 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
222 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %m");
227 /* figure out the key. store the key in rbuf for now. */
228 i = DH_compute_key(rbuf, bn, dh);
231 CAST_set_key(&castkey, i, rbuf);
233 /* session id. it's just a hashed version of the object pointer. */
234 sessid = dhxhash(obj);
235 memcpy(rbuf, &sessid, sizeof(sessid));
236 rbuf += sizeof(sessid);
237 *rbuflen += sizeof(sessid);
240 BN_bn2bin(dh->pub_key, rbuf);
244 /* buffer to be encrypted */
246 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
250 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %m");
254 memcpy(rbuf, &randbuf, sizeof(randbuf));
256 /* get the signature. it's always 16 bytes. */
258 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
259 (void *) &buf, NULL) < 0) {
262 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
266 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
268 memset(rbuf + KEYSIZE, 0, KEYSIZE);
271 /* encrypt using cast */
272 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
274 *rbuflen += CRYPTBUFLEN;
277 return AFPERR_AUTHCONT;
283 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %m");
289 /* dhx login: things are done in a slightly bizarre order to avoid
290 * having to clean things up if there's an error. */
291 static int pam_login(void *obj, struct passwd **uam_pwd,
292 char *ibuf, int ibuflen,
293 char *rbuf, int *rbuflen)
300 /* grab some of the options */
301 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
304 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %m");
309 len = (unsigned char) *ibuf++;
312 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
314 return( AFPERR_PARAM );
317 memcpy(buf, ibuf, len );
320 if ((unsigned long) ibuf & 1) /* pad to even boundary */
323 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
325 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: User entered a null value -- %m");
331 syslog( LOG_INFO, "dhx login: %s", buf);
332 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
336 static int pam_logincont(void *obj, struct passwd **uam_pwd,
337 char *ibuf, int ibuflen,
338 char *rbuf, int *rbuflen)
341 BIGNUM *bn1, *bn2, *bn3;
347 /* check for session id */
348 memcpy(&sessid, ibuf, sizeof(sessid));
349 if (sessid != dhxhash(obj)) {
351 syslog(LOG_INFO, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %m");
355 ibuf += sizeof(sessid);
357 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
358 (void *) &hostname, NULL) < 0)
361 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
362 msg3_iv, CAST_DECRYPT);
363 memset(&castkey, 0, sizeof(castkey));
365 /* check to make sure that the random number is the same. we
366 * get sent back an incremented random number. */
367 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
370 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
375 /* zero out the random number */
376 memset(rbuf, 0, sizeof(randbuf));
377 memset(randbuf, 0, sizeof(randbuf));
380 if (!(bn3 = BN_new())) {
386 BN_sub(bn3, bn1, bn2);
390 /* okay. is it one more? */
391 if (!BN_is_one(bn3)) {
397 /* Set these things up for the conv function */
398 rbuf[PASSWDLEN] = '\0';
401 err = AFPERR_NOTAUTH;
402 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
404 if (PAM_error != PAM_SUCCESS) {
406 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh,PAM_error));
411 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
412 pam_set_item(pamh, PAM_TTY, "afpd");
413 pam_set_item(pamh, PAM_RHOST, hostname);
414 PAM_error = pam_authenticate(pamh,0);
415 if (PAM_error != PAM_SUCCESS) {
416 if (PAM_error == PAM_MAXTRIES)
417 err = AFPERR_PWDEXPR;
419 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
424 PAM_error = pam_acct_mgmt(pamh, 0);
425 if (PAM_error != PAM_SUCCESS) {
426 if (PAM_error == PAM_ACCT_EXPIRED)
427 err = AFPERR_PWDEXPR;
428 #ifdef PAM_AUTHTOKEN_REQD
429 else if (PAM_error == PAM_AUTHTOKEN_REQD)
430 err = AFPERR_PWDCHNG;
433 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
438 #ifndef PAM_CRED_ESTABLISH
439 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
441 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
442 if (PAM_error != PAM_SUCCESS) {
444 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
449 PAM_error = pam_open_session(pamh, 0);
450 if (PAM_error != PAM_SUCCESS) {
452 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", pam_strerror(pamh, PAM_error));
457 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
460 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!");
465 pam_end(pamh, PAM_error);
467 memset(rbuf, 0, CRYPT2BUFLEN);
472 static void pam_logout() {
473 pam_close_session(pamh, 0);
479 /* change pw for dhx needs a couple passes to get everything all
480 * right. basically, it's like the login/logincont sequence */
481 static int pam_changepw(void *obj, char *username,
482 struct passwd *pwd, char *ibuf, int ibuflen,
483 char *rbuf, int *rbuflen)
485 BIGNUM *bn1, *bn2, *bn3;
494 memcpy(&sessid, ibuf, sizeof(sessid));
495 ibuf += sizeof(sessid);
497 if (!sessid) { /* no sessid -> initialization phase */
498 PAM_username = username;
499 ibuflen -= sizeof(sessid);
500 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
504 /* otherwise, it's like logincont but different. */
506 /* check out the session id */
507 if (sessid != dhxhash(obj)) {
509 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m");
514 /* we need this for pam */
515 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
516 (void *) &hostname, NULL) < 0) {
518 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m");
523 /* grab the client's nonce, old password, and new password. */
524 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
525 msg3_iv, CAST_DECRYPT);
526 memset(&castkey, 0, sizeof(castkey));
528 /* check to make sure that the random number is the same. we
529 * get sent back an incremented random number. */
530 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
532 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m");
537 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
540 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m");
545 /* zero out the random number */
546 memset(rbuf, 0, sizeof(randbuf));
547 memset(randbuf, 0, sizeof(randbuf));
549 if (!(bn3 = BN_new())) {
553 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m");
558 BN_sub(bn3, bn1, bn2);
562 /* okay. is it one more? */
564 if (!BN_is_one(bn3)) {
567 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m");
574 /* Set these things up for the conv function. the old password
577 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
578 PAM_password = ibuf + PASSWDLEN;
580 PAM_error = pam_start("netatalk", username, &PAM_conversation,
582 if (PAM_error != PAM_SUCCESS) {
584 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m");
588 pam_set_item(lpamh, PAM_TTY, "afpd");
589 pam_set_item(lpamh, PAM_RHOST, hostname);
591 /* we might need to do this as root */
594 PAM_error = pam_authenticate(lpamh, 0);
595 if (PAM_error != PAM_SUCCESS) {
597 pam_end(lpamh, PAM_error);
598 return AFPERR_NOTAUTH;
601 /* clear out old passwd */
602 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
606 ibuf[PASSWDLEN] = '\0';
608 /* this really does need to be done as root */
609 PAM_error = pam_chauthtok(lpamh, 0);
610 seteuid(uid); /* un-root ourselves. */
611 memset(ibuf, 0, PASSWDLEN);
612 if (PAM_error != PAM_SUCCESS) {
613 pam_end(lpamh, PAM_error);
614 return AFPERR_ACCESS;
622 static int uam_setup(const char *path)
624 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
625 pam_logincont, pam_logout) < 0)
628 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
630 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
634 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
640 static void uam_cleanup(void)
642 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
643 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
644 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
647 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
650 uam_setup, uam_cleanup
653 #endif /* USE_PAM && UAM_DHX */