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;
260 /* dhx login: things are done in a slightly bizarre order to avoid
261 * having to clean things up if there's an error. */
262 static int pam_login(void *obj, struct passwd **uam_pwd,
263 char *ibuf, int ibuflen,
264 char *rbuf, int *rbuflen)
271 /* grab some of the options */
272 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
276 len = (unsigned char) *ibuf++;
278 return( AFPERR_PARAM );
281 memcpy(buf, ibuf, len );
284 if ((unsigned long) ibuf & 1) /* pad to even boundary */
287 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
292 syslog( LOG_INFO, "dhx login: %s", buf);
293 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
297 static int pam_logincont(void *obj, struct passwd **uam_pwd,
298 char *ibuf, int ibuflen,
299 char *rbuf, int *rbuflen)
302 BIGNUM *bn1, *bn2, *bn3;
308 /* check for session id */
309 memcpy(&sessid, ibuf, sizeof(sessid));
310 if (sessid != dhxhash(obj))
312 ibuf += sizeof(sessid);
314 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
315 (void *) &hostname, NULL) < 0)
318 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
319 msg3_iv, CAST_DECRYPT);
320 memset(&castkey, 0, sizeof(castkey));
322 /* check to make sure that the random number is the same. we
323 * get sent back an incremented random number. */
324 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
327 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
332 /* zero out the random number */
333 memset(rbuf, 0, sizeof(randbuf));
334 memset(randbuf, 0, sizeof(randbuf));
337 if (!(bn3 = BN_new())) {
343 BN_sub(bn3, bn1, bn2);
347 /* okay. is it one more? */
348 if (!BN_is_one(bn3)) {
354 /* Set these things up for the conv function */
355 rbuf[PASSWDLEN] = '\0';
358 err = AFPERR_NOTAUTH;
359 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
361 if (PAM_error != PAM_SUCCESS)
363 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
367 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
368 pam_set_item(pamh, PAM_TTY, "afpd");
369 pam_set_item(pamh, PAM_RHOST, hostname);
370 PAM_error = pam_authenticate(pamh,0);
371 if (PAM_error != PAM_SUCCESS) {
372 if (PAM_error == PAM_MAXTRIES)
373 err = AFPERR_PWDEXPR;
375 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
380 PAM_error = pam_acct_mgmt(pamh, 0);
381 if (PAM_error != PAM_SUCCESS) {
382 if (PAM_error == PAM_ACCT_EXPIRED)
383 err = AFPERR_PWDEXPR;
384 #ifdef PAM_AUTHTOKEN_REQD
385 else if (PAM_error == PAM_AUTHTOKEN_REQD)
386 err = AFPERR_PWDCHNG;
389 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
394 #ifndef PAM_CRED_ESTABLISH
395 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
397 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
398 if (PAM_error != PAM_SUCCESS)
400 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
404 PAM_error = pam_open_session(pamh, 0);
405 if (PAM_error != PAM_SUCCESS)
407 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM_Error: %s -- %m", PAM_error);
411 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
414 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: PAM Auth OK!: %s -- %m", AFP_OK);
419 pam_end(pamh, PAM_error);
421 memset(rbuf, 0, CRYPT2BUFLEN);
426 static void pam_logout() {
427 pam_close_session(pamh, 0);
433 /* change pw for dhx needs a couple passes to get everything all
434 * right. basically, it's like the login/logincont sequence */
435 static int pam_changepw(void *obj, char *username,
436 struct passwd *pwd, char *ibuf, int ibuflen,
437 char *rbuf, int *rbuflen)
439 BIGNUM *bn1, *bn2, *bn3;
448 memcpy(&sessid, ibuf, sizeof(sessid));
449 ibuf += sizeof(sessid);
451 if (!sessid) { /* no sessid -> initialization phase */
452 PAM_username = username;
453 ibuflen -= sizeof(sessid);
454 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
458 /* otherwise, it's like logincont but different. */
460 /* check out the session id */
461 if (sessid != dhxhash(obj))
463 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Session ID not Equal to DHX Hash -- %m",);
467 /* we need this for pam */
468 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
469 (void *) &hostname, NULL) < 0)
471 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Hostname Null?? -- %m",);
475 /* grab the client's nonce, old password, and new password. */
476 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
477 msg3_iv, CAST_DECRYPT);
478 memset(&castkey, 0, sizeof(castkey));
480 /* check to make sure that the random number is the same. we
481 * get sent back an incremented random number. */
482 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
484 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented-- %m",);
488 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
491 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number Not the same or not incremented -- %m",);
496 /* zero out the random number */
497 memset(rbuf, 0, sizeof(randbuf));
498 memset(randbuf, 0, sizeof(randbuf));
500 if (!(bn3 = BN_new())) {
504 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Random Number did not Zero -- %m",);
509 BN_sub(bn3, bn1, bn2);
513 /* okay. is it one more? */
515 if (!BN_is_one(bn3)) {
518 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: After Random Number not Zero, is it one more? -- %m",);
525 /* Set these things up for the conv function. the old password
528 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
529 PAM_password = ibuf + PASSWDLEN;
531 PAM_error = pam_start("netatalk", username, &PAM_conversation,
533 if (PAM_error != PAM_SUCCESS)
535 syslog(LOG_INFO, "uams_dhx_pam.c :PAM: Needless to say, PAM_error is != to PAM_SUCCESS -- %m",);
538 pam_set_item(lpamh, PAM_TTY, "afpd");
539 pam_set_item(lpamh, PAM_RHOST, hostname);
541 /* we might need to do this as root */
544 PAM_error = pam_authenticate(lpamh, 0);
545 if (PAM_error != PAM_SUCCESS) {
547 pam_end(lpamh, PAM_error);
548 return AFPERR_NOTAUTH;
551 /* clear out old passwd */
552 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
556 ibuf[PASSWDLEN] = '\0';
558 /* this really does need to be done as root */
559 PAM_error = pam_chauthtok(lpamh, 0);
560 seteuid(uid); /* un-root ourselves. */
561 memset(ibuf, 0, PASSWDLEN);
562 if (PAM_error != PAM_SUCCESS) {
563 pam_end(lpamh, PAM_error);
564 return AFPERR_ACCESS;
572 static int uam_setup(const char *path)
574 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
575 pam_logincont, pam_logout) < 0)
578 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
580 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
584 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
590 static void uam_cleanup(void)
592 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
593 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
594 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
597 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
600 uam_setup, uam_cleanup
603 #endif /* USE_PAM && UAM_DHX */