2 * $Id: uams_dhx_passwd.c,v 1.26 2009-10-22 13:40:11 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
11 #endif /* HAVE_CONFIG_H */
14 #define _XOPEN_SOURCE 500 /* for crypt() */
17 #define _XOPEN_SOURCE /* for crypt() */
25 #endif /* HAVE_UNISTD_H */
28 #endif /* ! HAVE_CRYPT_H */
29 #ifdef HAVE_SYS_TIME_H
39 #if defined(GNUTLS_DHX)
40 #include <gnutls/openssl.h>
41 #elif defined(OPENSSL_DHX)
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/cast.h>
45 #else /* OPENSSL_DHX */
49 #endif /* OPENSSL_DHX */
51 #include <atalk/logger.h>
52 #include <atalk/afp.h>
53 #include <atalk/uam.h>
57 #define CRYPTBUFLEN (KEYSIZE*2)
58 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
60 /* hash a number to a 16-bit quantity */
61 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
62 (unsigned long) (a)) & 0xffff)
65 static CAST_KEY castkey;
66 static struct passwd *dhxpwd;
67 static u_int8_t randbuf[16];
73 static char *clientname;
77 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
78 char *ibuf, size_t ibuflen _U_,
79 char *rbuf, size_t *rbuflen)
81 unsigned char iv[] = "CJalbert";
82 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
83 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
88 BIGNUM *bn, *gbn, *pbn;
95 for (i = 0; i < 256; i++)
96 rnd_seed[i] = random();
97 RAND_seed(rnd_seed, sizeof(rnd_seed));
103 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
104 (void *) &clientname, NULL ) < 0 )
108 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
112 LOG(log_info, logtype_uams, "dhx login: %s", username);
113 if (uam_checkuser(dhxpwd) < 0)
114 return AFPERR_NOTAUTH;
117 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
118 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
119 return AFPERR_NOTAUTH;
121 dhxpwd->pw_passwd = sp->sp_pwdp;
122 #endif /* SHADOWPW */
124 if (!dhxpwd->pw_passwd)
125 return AFPERR_NOTAUTH;
127 /* get the client's public key */
128 if (!(bn = BN_bin2bn((unsigned char *)ibuf, KEYSIZE, NULL))) {
133 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
138 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
144 /* okay, we're ready */
145 if (!(dh = DH_new())) {
152 /* generate key and make sure we have enough space */
155 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
159 /* figure out the key. use rbuf as a temporary buffer. */
160 i = DH_compute_key((unsigned char *)rbuf, bn, dh);
163 CAST_set_key(&castkey, i, (unsigned char *)rbuf);
165 /* session id. it's just a hashed version of the object pointer. */
166 sessid = dhxhash(obj);
167 memcpy(rbuf, &sessid, sizeof(sessid));
168 rbuf += sizeof(sessid);
169 *rbuflen += sizeof(sessid);
171 /* send our public key */
172 BN_bn2bin(dh->pub_key, (unsigned char *)rbuf);
176 /* buffer to be encrypted */
178 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
183 memcpy(rbuf, &randbuf, sizeof(randbuf));
186 /* get the signature. it's always 16 bytes. */
187 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
188 (void *) &name, NULL) < 0) {
192 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
194 memset(rbuf + KEYSIZE, 0, KEYSIZE);
197 /* encrypt using cast */
198 CAST_cbc_encrypt((unsigned char *)rbuf, (unsigned char *)rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
199 *rbuflen += CRYPTBUFLEN;
202 return AFPERR_AUTHCONT;
211 static int passwd_login(void *obj, struct passwd **uam_pwd,
212 char *ibuf, size_t ibuflen,
213 char *rbuf, size_t *rbuflen)
220 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
221 (void *) &username, &ulen) < 0)
225 return( AFPERR_PARAM );
228 len = (unsigned char) *ibuf++;
230 if (!len || len > ibuflen || len > ulen ) {
231 return( AFPERR_PARAM );
233 memcpy(username, ibuf, len );
236 username[ len ] = '\0';
238 if ((unsigned long) ibuf & 1) { /* pad character */
242 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
246 /* cleartxt login ext
249 2 bytes len (network order)
252 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
253 char *ibuf, size_t ibuflen,
254 char *rbuf, size_t *rbuflen)
262 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
263 (void *) &username, &ulen) < 0)
269 memcpy(&temp16, uname, sizeof(temp16));
271 if (!len || len > ulen ) {
272 return( AFPERR_PARAM );
274 memcpy(username, uname +2, len );
275 username[ len ] = '\0';
276 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
279 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
280 char *ibuf, size_t ibuflen _U_,
281 char *rbuf, size_t *rbuflen)
285 #endif /* SHADOWPW */
286 unsigned char iv[] = "LWallace";
287 BIGNUM *bn1, *bn2, *bn3;
290 int err = AFPERR_NOTAUTH;
294 /* check for session id */
295 memcpy(&sessid, ibuf, sizeof(sessid));
296 if (sessid != dhxhash(obj))
298 ibuf += sizeof(sessid);
300 /* use rbuf as scratch space */
301 CAST_cbc_encrypt((unsigned char *)ibuf, (unsigned char *)rbuf, CRYPT2BUFLEN, &castkey,
304 /* check to make sure that the random number is the same. we
305 * get sent back an incremented random number. */
306 if (!(bn1 = BN_bin2bn((unsigned char *)rbuf, KEYSIZE, NULL)))
309 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
314 /* zero out the random number */
315 memset(rbuf, 0, sizeof(randbuf));
316 memset(randbuf, 0, sizeof(randbuf));
319 if (!(bn3 = BN_new())) {
325 BN_sub(bn3, bn1, bn2);
329 /* okay. is it one more? */
330 if (!BN_is_one(bn3)) {
336 rbuf[PASSWDLEN] = '\0';
343 uam_afp_getcmdline( &ac, &av );
344 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
346 if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
347 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
348 return AFPERR_NOTAUTH;
350 memset( rbuf, 0, PASSWDLEN );
355 p = crypt( rbuf, dhxpwd->pw_passwd );
356 memset(rbuf, 0, PASSWDLEN);
357 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
362 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
363 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", dhxpwd->pw_name);
364 return (AFPERR_NOTAUTH);
367 /* check for expired password */
368 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
369 time_t now = time(NULL) / (60*60*24);
370 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
371 if ( expire_days < 0 ) {
372 LOG(log_info, logtype_uams, "password for user %s expired", dhxpwd->pw_name);
373 err = AFPERR_PWDEXPR;
376 #endif /* SHADOWPW */
380 return AFPERR_NOTAUTH;
384 static int uam_setup(const char *path)
386 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
387 passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
389 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
395 static void uam_cleanup(void)
397 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
398 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
401 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
404 uam_setup, uam_cleanup
407 UAM_MODULE_EXPORT struct uam_export uams_dhx_passwd = {
410 uam_setup, uam_cleanup