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
78 reply = (struct pam_response *)
79 calloc(num_msg, sizeof(struct pam_response));
84 for (count = 0; count < num_msg; count++) {
87 switch (msg[count]->msg_style) {
88 case PAM_PROMPT_ECHO_ON:
89 if (!(string = COPY_STRING(PAM_username)))
92 case PAM_PROMPT_ECHO_OFF:
93 if (!(string = COPY_STRING(PAM_password)))
97 #ifdef PAM_BINARY_PROMPT
98 case PAM_BINARY_PROMPT:
108 reply[count].resp_retcode = 0;
109 reply[count].resp = string;
118 for (count = 0; count < num_msg; count++) {
119 if (!reply[count].resp)
121 switch (msg[count]->msg_style) {
122 case PAM_PROMPT_ECHO_OFF:
123 case PAM_PROMPT_ECHO_ON:
124 free(reply[count].resp);
132 static struct pam_conv PAM_conversation = {
138 static int dhx_setup(void *obj, char *ibuf, int ibuflen,
139 char *rbuf, int *rbuflen)
143 BIGNUM *bn, *gbn, *pbn;
146 /* get the client's public key */
147 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL)))
151 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
156 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
162 /* okay, we're ready */
163 if (!(dh = DH_new())) {
170 /* generate key and make sure that we have enough space */
173 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
177 /* figure out the key. store the key in rbuf for now. */
178 i = DH_compute_key(rbuf, bn, dh);
181 CAST_set_key(&castkey, i, rbuf);
183 /* session id. it's just a hashed version of the object pointer. */
184 sessid = dhxhash(obj);
185 memcpy(rbuf, &sessid, sizeof(sessid));
186 rbuf += sizeof(sessid);
187 *rbuflen += sizeof(sessid);
190 BN_bn2bin(dh->pub_key, rbuf);
194 /* buffer to be encrypted */
196 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
201 memcpy(rbuf, &randbuf, sizeof(randbuf));
203 /* get the signature. it's always 16 bytes. */
205 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
206 (void *) &buf, NULL) < 0) {
210 memcpy(rbuf + KEYSIZE, buf, KEYSIZE);
212 memset(rbuf + KEYSIZE, 0, KEYSIZE);
215 /* encrypt using cast */
216 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv,
218 *rbuflen += CRYPTBUFLEN;
221 return AFPERR_AUTHCONT;
230 /* dhx login: things are done in a slightly bizarre order to avoid
231 * having to clean things up if there's an error. */
232 static int pam_login(void *obj, struct passwd **uam_pwd,
233 char *ibuf, int ibuflen,
234 char *rbuf, int *rbuflen)
241 /* grab some of the options */
242 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
246 len = (unsigned char) *ibuf++;
248 return( AFPERR_PARAM );
251 memcpy(buf, ibuf, len );
254 if ((unsigned long) ibuf & 1) /* pad to even boundary */
257 if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
262 syslog( LOG_INFO, "dhx login: %s", buf);
263 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
267 static int pam_logincont(void *obj, struct passwd **uam_pwd,
268 char *ibuf, int ibuflen,
269 char *rbuf, int *rbuflen)
272 BIGNUM *bn1, *bn2, *bn3;
278 /* check for session id */
279 memcpy(&sessid, ibuf, sizeof(sessid));
280 if (sessid != dhxhash(obj))
282 ibuf += sizeof(sessid);
284 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
285 (void *) &hostname, NULL) < 0)
288 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
289 msg3_iv, CAST_DECRYPT);
290 memset(&castkey, 0, sizeof(castkey));
292 /* check to make sure that the random number is the same. we
293 * get sent back an incremented random number. */
294 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
297 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
302 /* zero out the random number */
303 memset(rbuf, 0, sizeof(randbuf));
304 memset(randbuf, 0, sizeof(randbuf));
307 if (!(bn3 = BN_new())) {
313 BN_sub(bn3, bn1, bn2);
317 /* okay. is it one more? */
318 if (!BN_is_one(bn3)) {
324 /* Set these things up for the conv function */
325 rbuf[PASSWDLEN] = '\0';
328 err = AFPERR_NOTAUTH;
329 PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
331 if (PAM_error != PAM_SUCCESS)
334 /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
335 pam_set_item(pamh, PAM_TTY, "afpd");
336 pam_set_item(pamh, PAM_RHOST, hostname);
337 PAM_error = pam_authenticate(pamh,0);
338 if (PAM_error != PAM_SUCCESS) {
339 if (PAM_error == PAM_MAXTRIES)
340 err = AFPERR_PWDEXPR;
344 PAM_error = pam_acct_mgmt(pamh, 0);
345 if (PAM_error != PAM_SUCCESS) {
346 if (PAM_error == PAM_ACCT_EXPIRED)
347 err = AFPERR_PWDEXPR;
348 #ifdef PAM_AUTHTOKEN_REQD
349 else if (PAM_error == PAM_AUTHTOKEN_REQD)
350 err = AFPERR_PWDCHNG;
355 #ifndef PAM_CRED_ESTABLISH
356 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
358 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
359 if (PAM_error != PAM_SUCCESS)
362 PAM_error = pam_open_session(pamh, 0);
363 if (PAM_error != PAM_SUCCESS)
366 memset(rbuf, 0, PASSWDLEN); /* zero out the password */
371 pam_end(pamh, PAM_error);
373 memset(rbuf, 0, CRYPT2BUFLEN);
378 static void pam_logout() {
379 pam_close_session(pamh, 0);
385 /* change pw for dhx needs a couple passes to get everything all
386 * right. basically, it's like the login/logincont sequence */
387 static int pam_changepw(void *obj, char *username,
388 struct passwd *pwd, char *ibuf, int ibuflen,
389 char *rbuf, int *rbuflen)
391 BIGNUM *bn1, *bn2, *bn3;
400 memcpy(&sessid, ibuf, sizeof(sessid));
401 ibuf += sizeof(sessid);
403 if (!sessid) { /* no sessid -> initialization phase */
404 PAM_username = username;
405 ibuflen -= sizeof(sessid);
406 return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
410 /* otherwise, it's like logincont but different. */
412 /* check out the session id */
413 if (sessid != dhxhash(obj))
416 /* we need this for pam */
417 if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
418 (void *) &hostname, NULL) < 0)
421 /* grab the client's nonce, old password, and new password. */
422 CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
423 msg3_iv, CAST_DECRYPT);
424 memset(&castkey, 0, sizeof(castkey));
426 /* check to make sure that the random number is the same. we
427 * get sent back an incremented random number. */
428 if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
431 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
436 /* zero out the random number */
437 memset(rbuf, 0, sizeof(randbuf));
438 memset(randbuf, 0, sizeof(randbuf));
440 if (!(bn3 = BN_new())) {
446 BN_sub(bn3, bn1, bn2);
450 /* okay. is it one more? */
452 if (!BN_is_one(bn3)) {
459 /* Set these things up for the conv function. the old password
462 ibuf[PASSWDLEN + PASSWDLEN] = '\0';
463 PAM_password = ibuf + PASSWDLEN;
465 PAM_error = pam_start("netatalk", username, &PAM_conversation,
467 if (PAM_error != PAM_SUCCESS)
469 pam_set_item(lpamh, PAM_TTY, "afpd");
470 pam_set_item(lpamh, PAM_RHOST, hostname);
472 /* we might need to do this as root */
475 PAM_error = pam_authenticate(lpamh, 0);
476 if (PAM_error != PAM_SUCCESS) {
478 pam_end(lpamh, PAM_error);
479 return AFPERR_NOTAUTH;
482 /* clear out old passwd */
483 memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
487 ibuf[PASSWDLEN] = '\0';
489 /* this really does need to be done as root */
490 PAM_error = pam_chauthtok(lpamh, 0);
491 seteuid(uid); /* un-root ourselves. */
492 memset(ibuf, 0, PASSWDLEN);
493 if (PAM_error != PAM_SUCCESS) {
494 pam_end(lpamh, PAM_error);
495 return AFPERR_ACCESS;
503 static int uam_setup(const char *path)
505 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login,
506 pam_logincont, pam_logout) < 0)
509 if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128",
511 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
515 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
521 static void uam_cleanup(void)
523 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
524 uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
525 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
528 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
531 uam_setup, uam_cleanup
534 #endif /* USE_PAM && UAM_DHX */