2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
4 * All Rights Reserved. See COPYRIGHT.
11 #if defined(USE_PAM) && defined(UAM_DHX)
17 #include <security/pam_appl.h>
20 #include <openssl/bn.h>
21 #include <openssl/dh.h>
22 #include <openssl/cast.h>
29 #include <atalk/afp.h>
30 #include <atalk/uam.h>
34 #define CRYPTBUFLEN (KEYSIZE*2)
35 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
36 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
38 /* hash a number to a 16-bit quantity */
39 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
40 (unsigned long) (a)) & 0xffff)
43 static CAST_KEY castkey;
44 static struct passwd *dhxpwd;
45 static u_int8_t randbuf[KEYSIZE];
47 /* diffie-hellman bits */
48 static unsigned char msg2_iv[] = "CJalbert";
49 static unsigned char msg3_iv[] = "LWallace";
50 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
51 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
52 static const u_int8_t g = 0x07;
55 /* Static variables used to communicate between the conversation function
56 * and the server_login function
58 static pam_handle_t *pamh = NULL;
59 static char *PAM_username;
60 static char *PAM_password;
62 /* PAM conversation function
63 * Here we assume (for now, at least) that echo on means login name, and
64 * echo off means password.
66 static int PAM_conv (int num_msg,
67 const struct pam_message **msg,
68 struct pam_response **resp,
71 struct pam_response *reply;
73 #define COPY_STRING(s) (s) ? strdup(s) : NULL
77 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DHX Conversation Err -- %m");
81 reply = (struct pam_response *)
82 calloc(num_msg, sizeof(struct pam_response));
86 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DHX Conversation Err -- %m");
90 for (count = 0; count < num_msg; count++) {
93 switch (msg[count]->msg_style) {
94 case PAM_PROMPT_ECHO_ON:
95 if (!(string = COPY_STRING(PAM_username)))
97 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: username failure -- %m");
101 case PAM_PROMPT_ECHO_OFF:
102 if (!(string = COPY_STRING(PAM_password)))
104 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: passwd failure: --: %m");
109 #ifdef PAM_BINARY_PROMPT
110 case PAM_BINARY_PROMPT:
117 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Binary_Prompt -- %m");
123 reply[count].resp_retcode = 0;
124 reply[count].resp = string;
131 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Success -- %m");
136 for (count = 0; count < num_msg; count++) {
137 if (!reply[count].resp)
139 switch (msg[count]->msg_style) {
140 case PAM_PROMPT_ECHO_OFF:
141 case PAM_PROMPT_ECHO_ON:
142 free(reply[count].resp);
148 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DHX Conversation Err -- %m");
153 static struct pam_conv PAM_conversation = {
159 static int dhx_setup(void *obj, char *ibuf, int ibuflen,
160 char *rbuf, int *rbuflen)
164 BIGNUM *bn, *gbn, *pbn;
167 /* get the client's public key */
168 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL)))
170 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Public Key -- %m");
175 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
178 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Primes: GBN -- %m");
183 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
187 syslog(LOG_INFO, "uams_dhx_pam.c :PAM No Primes: PBN -- %m");
192 /* okay, we're ready */
193 if (!(dh = DH_new())) {
198 syslog(LOG_INFO, "uams_dhx_pam.c :PAM DH was equal to DH_New... Go figure... -- %m");
203 /* generate key and make sure that we have enough space */
206 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
208 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %m");
213 /* figure out the key. store the key in rbuf for now. */
214 i = DH_compute_key(rbuf, bn, dh);
217 CAST_set_key(&castkey, i, rbuf);
219 /* session id. it's just a hashed version of the object pointer. */
220 sessid = dhxhash(obj);
221 memcpy(rbuf, &sessid, sizeof(sessid));
222 rbuf += sizeof(sessid);
223 *rbuflen += sizeof(sessid);
226 BN_bn2bin(dh->pub_key, rbuf);
230 /* buffer to be encrypted */
232 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
236 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %m");
240 memcpy(rbuf, &randbuf, sizeof(randbuf));
242 /* get the signature. it's always 16 bytes. */
244 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
245 (void *) &buf, NULL) < 0) {
248 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
252 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
254 memset(rbuf + KEYSIZE, 0, KEYSIZE);
257 /* encrypt using cast */
258 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
260 *rbuflen += CRYPTBUFLEN;
263 return AFPERR_AUTHCONT;
269 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %m");
275 /* dhx login: things are done in a slightly bizarre order to avoid
276 * having to clean things up if there's an error. */
277 static int pam_login(void *obj, struct passwd **uam_pwd,
278 char *ibuf, int ibuflen,
279 char *rbuf, int *rbuflen)
286 /* grab some of the options */
287 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
290 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %m");
294 len = (unsigned char) *ibuf++;
297 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
299 return( AFPERR_PARAM );
302 memcpy(buf, ibuf, len );
305 if ((unsigned long) ibuf & 1) /* pad to even boundary */
308 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
310 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: User entered a null value -- %m");
316 syslog( LOG_INFO, "dhx login: %s", buf);
317 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
321 static int pam_logincont(void *obj, struct passwd **uam_pwd,
322 char *ibuf, int ibuflen,
323 char *rbuf, int *rbuflen)
326 BIGNUM *bn1, *bn2, *bn3;
332 /* check for session id */
333 memcpy(&sessid, ibuf, sizeof(sessid));
334 if (sessid != dhxhash(obj))
336 syslog(LOG_INFO, "uams_dhx_pam.c :PAM Session ID - DHXHash Mismatch -- %m");
339 ibuf += sizeof(sessid);
341 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
342 (void *) &hostname, NULL) < 0)
345 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
346 msg3_iv, CAST_DECRYPT);
347 memset(&castkey, 0, sizeof(castkey));
349 /* check to make sure that the random number is the same. we
350 * get sent back an incremented random number. */
351 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
354 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
359 /* zero out the random number */
360 memset(rbuf, 0, sizeof(randbuf));
361 memset(randbuf, 0, sizeof(randbuf));
364 if (!(bn3 = BN_new())) {
370 BN_sub(bn3, bn1, bn2);
374 /* okay. is it one more? */
375 if (!BN_is_one(bn3)) {
381 /* Set these things up for the conv function */
382 rbuf[PASSWDLEN] = '\0';
385 err = AFPERR_NOTAUTH;
386 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
388 if (PAM_error != PAM_SUCCESS)
390 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
394 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
395 pam_set_item(pamh, PAM_TTY, "afpd");
396 pam_set_item(pamh, PAM_RHOST, hostname);
397 PAM_error = pam_authenticate(pamh,0);
398 if (PAM_error != PAM_SUCCESS) {
399 if (PAM_error == PAM_MAXTRIES)
400 err = AFPERR_PWDEXPR;
402 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
407 PAM_error = pam_acct_mgmt(pamh, 0);
408 if (PAM_error != PAM_SUCCESS) {
409 if (PAM_error == PAM_ACCT_EXPIRED)
410 err = AFPERR_PWDEXPR;
411 #ifdef PAM_AUTHTOKEN_REQD
412 else if (PAM_error == PAM_AUTHTOKEN_REQD)
413 err = AFPERR_PWDCHNG;
416 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
421 #ifndef PAM_CRED_ESTABLISH
422 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
424 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
425 if (PAM_error != PAM_SUCCESS)
427 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
431 PAM_error = pam_open_session(pamh, 0);
432 if (PAM_error != PAM_SUCCESS)
434 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
438 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
441 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!: %s -- %m", AFP_OK);
446 pam_end(pamh, PAM_error);
448 memset(rbuf, 0, CRYPT2BUFLEN);
453 static void pam_logout() {
454 pam_close_session(pamh, 0);
460 /* change pw for dhx needs a couple passes to get everything all
461 * right. basically, it's like the login/logincont sequence */
462 static int pam_changepw(void *obj, char *username,
463 struct passwd *pwd, char *ibuf, int ibuflen,
464 char *rbuf, int *rbuflen)
466 BIGNUM *bn1, *bn2, *bn3;
475 memcpy(&sessid, ibuf, sizeof(sessid));
476 ibuf += sizeof(sessid);
478 if (!sessid) { /* no sessid -> initialization phase */
479 PAM_username = username;
480 ibuflen -= sizeof(sessid);
481 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
485 /* otherwise, it's like logincont but different. */
487 /* check out the session id */
488 if (sessid != dhxhash(obj))
490 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m");
494 /* we need this for pam */
495 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
496 (void *) &hostname, NULL) < 0)
498 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m");
502 /* grab the client's nonce, old password, and new password. */
503 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
504 msg3_iv, CAST_DECRYPT);
505 memset(&castkey, 0, sizeof(castkey));
507 /* check to make sure that the random number is the same. we
508 * get sent back an incremented random number. */
509 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
511 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m");
515 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
518 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m");
523 /* zero out the random number */
524 memset(rbuf, 0, sizeof(randbuf));
525 memset(randbuf, 0, sizeof(randbuf));
527 if (!(bn3 = BN_new())) {
531 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m");
536 BN_sub(bn3, bn1, bn2);
540 /* okay. is it one more? */
542 if (!BN_is_one(bn3)) {
545 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m");
552 /* Set these things up for the conv function. the old password
555 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
556 PAM_password = ibuf + PASSWDLEN;
558 PAM_error = pam_start("netatalk", username, &PAM_conversation,
560 if (PAM_error != PAM_SUCCESS)
562 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m");
565 pam_set_item(lpamh, PAM_TTY, "afpd");
566 pam_set_item(lpamh, PAM_RHOST, hostname);
568 /* we might need to do this as root */
571 PAM_error = pam_authenticate(lpamh, 0);
572 if (PAM_error != PAM_SUCCESS) {
574 pam_end(lpamh, PAM_error);
575 return AFPERR_NOTAUTH;
578 /* clear out old passwd */
579 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
583 ibuf[PASSWDLEN] = '\0';
585 /* this really does need to be done as root */
586 PAM_error = pam_chauthtok(lpamh, 0);
587 seteuid(uid); /* un-root ourselves. */
588 memset(ibuf, 0, PASSWDLEN);
589 if (PAM_error != PAM_SUCCESS) {
590 pam_end(lpamh, PAM_error);
591 return AFPERR_ACCESS;
599 static int uam_setup(const char *path)
601 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
602 pam_logincont, pam_logout) < 0)
605 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
607 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
611 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
617 static void uam_cleanup(void)
619 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
620 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
621 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
624 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
627 uam_setup, uam_cleanup
630 #endif /* USE_PAM && UAM_DHX */