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 ) {
298 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: User entered a null value -- %m");
304 syslog( LOG_INFO, "dhx login: %s", buf);
305 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
309 static int pam_logincont(void *obj, struct passwd **uam_pwd,
310 char *ibuf, int ibuflen,
311 char *rbuf, int *rbuflen)
314 BIGNUM *bn1, *bn2, *bn3;
320 /* check for session id */
321 memcpy(&sessid, ibuf, sizeof(sessid));
322 if (sessid != dhxhash(obj))
324 ibuf += sizeof(sessid);
326 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
327 (void *) &hostname, NULL) < 0)
330 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
331 msg3_iv, CAST_DECRYPT);
332 memset(&castkey, 0, sizeof(castkey));
334 /* check to make sure that the random number is the same. we
335 * get sent back an incremented random number. */
336 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
339 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
344 /* zero out the random number */
345 memset(rbuf, 0, sizeof(randbuf));
346 memset(randbuf, 0, sizeof(randbuf));
349 if (!(bn3 = BN_new())) {
355 BN_sub(bn3, bn1, bn2);
359 /* okay. is it one more? */
360 if (!BN_is_one(bn3)) {
366 /* Set these things up for the conv function */
367 rbuf[PASSWDLEN] = '\0';
370 err = AFPERR_NOTAUTH;
371 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
373 if (PAM_error != PAM_SUCCESS)
375 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
379 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
380 pam_set_item(pamh, PAM_TTY, "afpd");
381 pam_set_item(pamh, PAM_RHOST, hostname);
382 PAM_error = pam_authenticate(pamh,0);
383 if (PAM_error != PAM_SUCCESS) {
384 if (PAM_error == PAM_MAXTRIES)
385 err = AFPERR_PWDEXPR;
387 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
392 PAM_error = pam_acct_mgmt(pamh, 0);
393 if (PAM_error != PAM_SUCCESS) {
394 if (PAM_error == PAM_ACCT_EXPIRED)
395 err = AFPERR_PWDEXPR;
396 #ifdef PAM_AUTHTOKEN_REQD
397 else if (PAM_error == PAM_AUTHTOKEN_REQD)
398 err = AFPERR_PWDCHNG;
401 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
406 #ifndef PAM_CRED_ESTABLISH
407 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
409 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
410 if (PAM_error != PAM_SUCCESS)
412 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
416 PAM_error = pam_open_session(pamh, 0);
417 if (PAM_error != PAM_SUCCESS)
419 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
423 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
426 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!: %s -- %m", AFP_OK);
431 pam_end(pamh, PAM_error);
433 memset(rbuf, 0, CRYPT2BUFLEN);
438 static void pam_logout() {
439 pam_close_session(pamh, 0);
445 /* change pw for dhx needs a couple passes to get everything all
446 * right. basically, it's like the login/logincont sequence */
447 static int pam_changepw(void *obj, char *username,
448 struct passwd *pwd, char *ibuf, int ibuflen,
449 char *rbuf, int *rbuflen)
451 BIGNUM *bn1, *bn2, *bn3;
460 memcpy(&sessid, ibuf, sizeof(sessid));
461 ibuf += sizeof(sessid);
463 if (!sessid) { /* no sessid -> initialization phase */
464 PAM_username = username;
465 ibuflen -= sizeof(sessid);
466 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
470 /* otherwise, it's like logincont but different. */
472 /* check out the session id */
473 if (sessid != dhxhash(obj))
475 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m",);
479 /* we need this for pam */
480 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
481 (void *) &hostname, NULL) < 0)
483 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m",);
487 /* grab the client's nonce, old password, and new password. */
488 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
489 msg3_iv, CAST_DECRYPT);
490 memset(&castkey, 0, sizeof(castkey));
492 /* check to make sure that the random number is the same. we
493 * get sent back an incremented random number. */
494 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
496 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m",);
500 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
503 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m",);
508 /* zero out the random number */
509 memset(rbuf, 0, sizeof(randbuf));
510 memset(randbuf, 0, sizeof(randbuf));
512 if (!(bn3 = BN_new())) {
516 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m",);
521 BN_sub(bn3, bn1, bn2);
525 /* okay. is it one more? */
527 if (!BN_is_one(bn3)) {
530 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m",);
537 /* Set these things up for the conv function. the old password
540 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
541 PAM_password = ibuf + PASSWDLEN;
543 PAM_error = pam_start("netatalk", username, &PAM_conversation,
545 if (PAM_error != PAM_SUCCESS)
547 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m",);
550 pam_set_item(lpamh, PAM_TTY, "afpd");
551 pam_set_item(lpamh, PAM_RHOST, hostname);
553 /* we might need to do this as root */
556 PAM_error = pam_authenticate(lpamh, 0);
557 if (PAM_error != PAM_SUCCESS) {
559 pam_end(lpamh, PAM_error);
560 return AFPERR_NOTAUTH;
563 /* clear out old passwd */
564 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
568 ibuf[PASSWDLEN] = '\0';
570 /* this really does need to be done as root */
571 PAM_error = pam_chauthtok(lpamh, 0);
572 seteuid(uid); /* un-root ourselves. */
573 memset(ibuf, 0, PASSWDLEN);
574 if (PAM_error != PAM_SUCCESS) {
575 pam_end(lpamh, PAM_error);
576 return AFPERR_ACCESS;
584 static int uam_setup(const char *path)
586 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
587 pam_logincont, pam_logout) < 0)
590 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
592 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
596 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
602 static void uam_cleanup(void)
604 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
605 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
606 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
609 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
612 uam_setup, uam_cleanup
615 #endif /* USE_PAM && UAM_DHX */