2 * $Id: uams_dhx_passwd.c,v 1.21 2003-06-11 22:07:56 srittau 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.
9 #define _XOPEN_SOURCE /* for crypt() */
13 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_UNISTD_H */
23 #endif /* ! NO_CRYPT_H */
25 #include <atalk/logger.h>
31 #if defined(GNUTLS_DHX)
32 #include <gnutls/openssl.h>
33 #elif defined(OPENSSL_DHX)
34 #include <openssl/bn.h>
35 #include <openssl/dh.h>
36 #include <openssl/cast.h>
37 #else /* OPENSSL_DHX */
41 #endif /* OPENSSL_DHX */
43 #include <atalk/afp.h>
44 #include <atalk/uam.h>
48 #define CRYPTBUFLEN (KEYSIZE*2)
49 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
51 /* hash a number to a 16-bit quantity */
52 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
53 (unsigned long) (a)) & 0xffff)
56 static CAST_KEY castkey;
57 static struct passwd *dhxpwd;
58 static u_int8_t randbuf[16];
64 static char *clientname;
68 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
69 char *ibuf, int ibuflen,
70 char *rbuf, int *rbuflen)
72 unsigned char iv[] = "CJalbert";
73 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
74 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
79 BIGNUM *bn, *gbn, *pbn;
89 for (i = 0; i < sizeof(rnd_seed); i++)
90 rnd_seed[i] = random();
91 RAND_seed(rnd_seed, sizeof rnd_seed);
97 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
98 (void *) &clientname, NULL ) < 0 )
102 if (( dhxpwd = uam_getname(username, ulen)) == NULL ) {
106 LOG(log_info, logtype_uams, "dhx login: %s", username);
107 if (uam_checkuser(dhxpwd) < 0)
108 return AFPERR_NOTAUTH;
111 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
112 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
113 return AFPERR_NOTAUTH;
115 dhxpwd->pw_passwd = sp->sp_pwdp;
116 #endif /* SHADOWPW */
118 if (!dhxpwd->pw_passwd)
119 return AFPERR_NOTAUTH;
121 /* get the client's public key */
122 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
127 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
132 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
138 /* okay, we're ready */
139 if (!(dh = DH_new())) {
146 /* generate key and make sure we have enough space */
149 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
153 /* figure out the key. use rbuf as a temporary buffer. */
154 i = DH_compute_key(rbuf, bn, dh);
157 CAST_set_key(&castkey, i, rbuf);
159 /* session id. it's just a hashed version of the object pointer. */
160 sessid = dhxhash(obj);
161 memcpy(rbuf, &sessid, sizeof(sessid));
162 rbuf += sizeof(sessid);
163 *rbuflen += sizeof(sessid);
165 /* send our public key */
166 BN_bn2bin(dh->pub_key, rbuf);
170 /* buffer to be encrypted */
172 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
177 memcpy(rbuf, &randbuf, sizeof(randbuf));
180 /* get the signature. it's always 16 bytes. */
181 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
182 (void *) &name, NULL) < 0) {
186 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
188 memset(rbuf + KEYSIZE, 0, KEYSIZE);
191 /* encrypt using cast */
192 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
193 *rbuflen += CRYPTBUFLEN;
196 return AFPERR_AUTHCONT;
205 static int passwd_login(void *obj, struct passwd **uam_pwd,
206 char *ibuf, int ibuflen,
207 char *rbuf, int *rbuflen)
214 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
215 (void *) &username, &ulen) < 0)
219 return( AFPERR_PARAM );
222 len = (unsigned char) *ibuf++;
224 if (!len || len > ibuflen || len > ulen ) {
225 return( AFPERR_PARAM );
227 memcpy(username, ibuf, len );
230 username[ len ] = '\0';
232 if ((unsigned long) ibuf & 1) { /* pad character */
236 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
240 /* cleartxt login ext
243 2 bytes len (network order)
244 len bytes unicode name
246 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
247 char *ibuf, int ibuflen,
248 char *rbuf, int *rbuflen)
256 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
257 (void *) &username, &ulen) < 0)
263 memcpy(&temp16, uname, sizeof(temp16));
265 if (!len || len > ulen ) {
266 return( AFPERR_PARAM );
268 memcpy(username, uname +2, len );
269 username[ len ] = '\0';
270 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
273 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
274 char *ibuf, int ibuflen,
275 char *rbuf, int *rbuflen)
277 unsigned char iv[] = "LWallace";
278 BIGNUM *bn1, *bn2, *bn3;
284 /* check for session id */
285 memcpy(&sessid, ibuf, sizeof(sessid));
286 if (sessid != dhxhash(obj))
288 ibuf += sizeof(sessid);
290 /* use rbuf as scratch space */
291 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
294 /* check to make sure that the random number is the same. we
295 * get sent back an incremented random number. */
296 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
299 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
304 /* zero out the random number */
305 memset(rbuf, 0, sizeof(randbuf));
306 memset(randbuf, 0, sizeof(randbuf));
309 if (!(bn3 = BN_new())) {
315 BN_sub(bn3, bn1, bn2);
319 /* okay. is it one more? */
320 if (!BN_is_one(bn3)) {
326 rbuf[PASSWDLEN] = '\0';
333 uam_afp_getcmdline( &ac, &av );
334 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
336 if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
337 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
338 return AFPERR_NOTAUTH;
340 memset( rbuf, 0, PASSWDLEN );
345 p = crypt( rbuf, dhxpwd->pw_passwd );
346 memset(rbuf, 0, PASSWDLEN);
347 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
353 return AFPERR_NOTAUTH;
357 static int uam_setup(const char *path)
359 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
360 passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
362 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
368 static void uam_cleanup(void)
370 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
371 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
374 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
377 uam_setup, uam_cleanup