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)))
172 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
177 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
183 /* okay, we're ready */
184 if (!(dh = DH_new())) {
191 /* generate key and make sure that we have enough space */
194 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
196 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Err Generating Key -- Not enough Space? -- %m");
201 /* figure out the key. store the key in rbuf for now. */
202 i = DH_compute_key(rbuf, bn, dh);
205 CAST_set_key(&castkey, i, rbuf);
207 /* session id. it's just a hashed version of the object pointer. */
208 sessid = dhxhash(obj);
209 memcpy(rbuf, &sessid, sizeof(sessid));
210 rbuf += sizeof(sessid);
211 *rbuflen += sizeof(sessid);
214 BN_bn2bin(dh->pub_key, rbuf);
218 /* buffer to be encrypted */
220 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
224 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Buffer Encryption Err. -- %m");
228 memcpy(rbuf, &randbuf, sizeof(randbuf));
230 /* get the signature. it's always 16 bytes. */
232 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
233 (void *) &buf, NULL) < 0) {
236 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
240 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
242 memset(rbuf + KEYSIZE, 0, KEYSIZE);
245 /* encrypt using cast */
246 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
248 *rbuflen += CRYPTBUFLEN;
251 return AFPERR_AUTHCONT;
257 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Fail - Cast Encryption -- %m");
263 /* dhx login: things are done in a slightly bizarre order to avoid
264 * having to clean things up if there's an error. */
265 static int pam_login(void *obj, struct passwd **uam_pwd,
266 char *ibuf, int ibuflen,
267 char *rbuf, int *rbuflen)
274 /* grab some of the options */
275 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
278 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: uam_afpserver_option didn't meet uam_option_username -- %m");
282 len = (unsigned char) *ibuf++;
285 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Signature Retieval Failure -- %m");
287 return( AFPERR_PARAM );
290 memcpy(buf, ibuf, len );
293 if ((unsigned long) ibuf & 1) /* pad to even boundary */
296 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
301 syslog( LOG_INFO, "dhx login: %s", buf);
302 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
306 static int pam_logincont(void *obj, struct passwd **uam_pwd,
307 char *ibuf, int ibuflen,
308 char *rbuf, int *rbuflen)
311 BIGNUM *bn1, *bn2, *bn3;
317 /* check for session id */
318 memcpy(&sessid, ibuf, sizeof(sessid));
319 if (sessid != dhxhash(obj))
321 ibuf += sizeof(sessid);
323 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
324 (void *) &hostname, NULL) < 0)
327 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
328 msg3_iv, CAST_DECRYPT);
329 memset(&castkey, 0, sizeof(castkey));
331 /* check to make sure that the random number is the same. we
332 * get sent back an incremented random number. */
333 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
336 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
341 /* zero out the random number */
342 memset(rbuf, 0, sizeof(randbuf));
343 memset(randbuf, 0, sizeof(randbuf));
346 if (!(bn3 = BN_new())) {
352 BN_sub(bn3, bn1, bn2);
356 /* okay. is it one more? */
357 if (!BN_is_one(bn3)) {
363 /* Set these things up for the conv function */
364 rbuf[PASSWDLEN] = '\0';
367 err = AFPERR_NOTAUTH;
368 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
370 if (PAM_error != PAM_SUCCESS)
372 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
376 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
377 pam_set_item(pamh, PAM_TTY, "afpd");
378 pam_set_item(pamh, PAM_RHOST, hostname);
379 PAM_error = pam_authenticate(pamh,0);
380 if (PAM_error != PAM_SUCCESS) {
381 if (PAM_error == PAM_MAXTRIES)
382 err = AFPERR_PWDEXPR;
384 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
389 PAM_error = pam_acct_mgmt(pamh, 0);
390 if (PAM_error != PAM_SUCCESS) {
391 if (PAM_error == PAM_ACCT_EXPIRED)
392 err = AFPERR_PWDEXPR;
393 #ifdef PAM_AUTHTOKEN_REQD
394 else if (PAM_error == PAM_AUTHTOKEN_REQD)
395 err = AFPERR_PWDCHNG;
398 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
403 #ifndef PAM_CRED_ESTABLISH
404 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
406 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
407 if (PAM_error != PAM_SUCCESS)
409 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
413 PAM_error = pam_open_session(pamh, 0);
414 if (PAM_error != PAM_SUCCESS)
416 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
420 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
423 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!: %s -- %m", AFP_OK);
428 pam_end(pamh, PAM_error);
430 memset(rbuf, 0, CRYPT2BUFLEN);
435 static void pam_logout() {
436 pam_close_session(pamh, 0);
442 /* change pw for dhx needs a couple passes to get everything all
443 * right. basically, it's like the login/logincont sequence */
444 static int pam_changepw(void *obj, char *username,
445 struct passwd *pwd, char *ibuf, int ibuflen,
446 char *rbuf, int *rbuflen)
448 BIGNUM *bn1, *bn2, *bn3;
457 memcpy(&sessid, ibuf, sizeof(sessid));
458 ibuf += sizeof(sessid);
460 if (!sessid) { /* no sessid -> initialization phase */
461 PAM_username = username;
462 ibuflen -= sizeof(sessid);
463 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
467 /* otherwise, it's like logincont but different. */
469 /* check out the session id */
470 if (sessid != dhxhash(obj))
472 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m",);
476 /* we need this for pam */
477 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
478 (void *) &hostname, NULL) < 0)
480 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m",);
484 /* grab the client's nonce, old password, and new password. */
485 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
486 msg3_iv, CAST_DECRYPT);
487 memset(&castkey, 0, sizeof(castkey));
489 /* check to make sure that the random number is the same. we
490 * get sent back an incremented random number. */
491 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
493 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m",);
497 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
500 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m",);
505 /* zero out the random number */
506 memset(rbuf, 0, sizeof(randbuf));
507 memset(randbuf, 0, sizeof(randbuf));
509 if (!(bn3 = BN_new())) {
513 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m",);
518 BN_sub(bn3, bn1, bn2);
522 /* okay. is it one more? */
524 if (!BN_is_one(bn3)) {
527 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m",);
534 /* Set these things up for the conv function. the old password
537 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
538 PAM_password = ibuf + PASSWDLEN;
540 PAM_error = pam_start("netatalk", username, &PAM_conversation,
542 if (PAM_error != PAM_SUCCESS)
544 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m",);
547 pam_set_item(lpamh, PAM_TTY, "afpd");
548 pam_set_item(lpamh, PAM_RHOST, hostname);
550 /* we might need to do this as root */
553 PAM_error = pam_authenticate(lpamh, 0);
554 if (PAM_error != PAM_SUCCESS) {
556 pam_end(lpamh, PAM_error);
557 return AFPERR_NOTAUTH;
560 /* clear out old passwd */
561 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
565 ibuf[PASSWDLEN] = '\0';
567 /* this really does need to be done as root */
568 PAM_error = pam_chauthtok(lpamh, 0);
569 seteuid(uid); /* un-root ourselves. */
570 memset(ibuf, 0, PASSWDLEN);
571 if (PAM_error != PAM_SUCCESS) {
572 pam_end(lpamh, PAM_error);
573 return AFPERR_ACCESS;
581 static int uam_setup(const char *path)
583 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
584 pam_logincont, pam_logout) < 0)
587 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
589 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
593 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
599 static void uam_cleanup(void)
601 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
602 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
603 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
606 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
609 uam_setup, uam_cleanup
612 #endif /* USE_PAM && UAM_DHX */