2 * $Id: uams_dhx_passwd.c,v 1.18 2003-02-11 16:41:56 didg 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 */
18 #endif /* HAVE_UNISTD_H */
21 #endif /* ! NO_CRYPT_H */
23 #include <atalk/logger.h>
29 #if defined(GNUTLS_DHX)
30 #include <gnutls/openssl.h>
31 #elif defined(OPENSSL_DHX)
32 #include <openssl/bn.h>
33 #include <openssl/dh.h>
34 #include <openssl/cast.h>
35 #else /* OPENSSL_DHX */
39 #endif /* OPENSSL_DHX */
41 #include <atalk/afp.h>
42 #include <atalk/uam.h>
46 #define CRYPTBUFLEN (KEYSIZE*2)
47 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
49 /* hash a number to a 16-bit quantity */
50 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
51 (unsigned long) (a)) & 0xffff)
54 static CAST_KEY castkey;
55 static struct passwd *dhxpwd;
56 static u_int8_t randbuf[16];
62 static char *clientname;
66 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
67 char *ibuf, int ibuflen,
68 char *rbuf, int *rbuflen)
70 unsigned char iv[] = "CJalbert";
71 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
72 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
77 BIGNUM *bn, *gbn, *pbn;
84 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
85 RAND_seed(rnd_seed, sizeof rnd_seed);
91 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
92 (void *) &clientname, NULL ) < 0 )
96 if (( dhxpwd = uam_getname(username, ulen)) == NULL ) {
100 LOG(log_info, logtype_uams, "dhx login: %s", username);
101 if (uam_checkuser(dhxpwd) < 0)
102 return AFPERR_NOTAUTH;
105 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
106 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
107 return AFPERR_NOTAUTH;
109 dhxpwd->pw_passwd = sp->sp_pwdp;
110 #endif /* SHADOWPW */
112 if (!dhxpwd->pw_passwd)
113 return AFPERR_NOTAUTH;
115 /* get the client's public key */
116 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
121 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
126 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
132 /* okay, we're ready */
133 if (!(dh = DH_new())) {
140 /* generate key and make sure we have enough space */
143 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
147 /* figure out the key. use rbuf as a temporary buffer. */
148 i = DH_compute_key(rbuf, bn, dh);
151 CAST_set_key(&castkey, i, rbuf);
153 /* session id. it's just a hashed version of the object pointer. */
154 sessid = dhxhash(obj);
155 memcpy(rbuf, &sessid, sizeof(sessid));
156 rbuf += sizeof(sessid);
157 *rbuflen += sizeof(sessid);
159 /* send our public key */
160 BN_bn2bin(dh->pub_key, rbuf);
164 /* buffer to be encrypted */
166 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
171 memcpy(rbuf, &randbuf, sizeof(randbuf));
174 /* get the signature. it's always 16 bytes. */
175 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
176 (void *) &name, NULL) < 0) {
180 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
182 memset(rbuf + KEYSIZE, 0, KEYSIZE);
185 /* encrypt using cast */
186 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
187 *rbuflen += CRYPTBUFLEN;
190 return AFPERR_AUTHCONT;
199 static int passwd_login(void *obj, struct passwd **uam_pwd,
200 char *ibuf, int ibuflen,
201 char *rbuf, int *rbuflen)
208 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
209 (void *) &username, &ulen) < 0)
213 return( AFPERR_PARAM );
216 len = (unsigned char) *ibuf++;
218 if (!len || len > ibuflen || len > ulen ) {
219 return( AFPERR_PARAM );
221 memcpy(username, ibuf, len );
224 username[ len ] = '\0';
226 if ((unsigned long) ibuf & 1) { /* pad character */
230 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
234 /* cleartxt login ext
237 2 bytes len (network order)
238 len bytes unicode name
240 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
241 char *ibuf, int ibuflen,
242 char *rbuf, int *rbuflen)
250 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
251 (void *) &username, &ulen) < 0)
257 memcpy(&temp16, uname, sizeof(temp16));
259 if (!len || len > ulen ) {
260 return( AFPERR_PARAM );
262 memcpy(username, uname +2, len );
263 username[ len ] = '\0';
264 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
267 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
268 char *ibuf, int ibuflen,
269 char *rbuf, int *rbuflen)
271 unsigned char iv[] = "LWallace";
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 /* use rbuf as scratch space */
285 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
288 /* check to make sure that the random number is the same. we
289 * get sent back an incremented random number. */
290 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
293 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
298 /* zero out the random number */
299 memset(rbuf, 0, sizeof(randbuf));
300 memset(randbuf, 0, sizeof(randbuf));
303 if (!(bn3 = BN_new())) {
309 BN_sub(bn3, bn1, bn2);
313 /* okay. is it one more? */
314 if (!BN_is_one(bn3)) {
320 rbuf[PASSWDLEN] = '\0';
327 uam_afp_getcmdline( &ac, &av );
328 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
330 if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
331 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
332 return AFPERR_NOTAUTH;
334 memset( rbuf, 0, PASSWDLEN );
339 p = crypt( rbuf, dhxpwd->pw_passwd );
340 memset(rbuf, 0, PASSWDLEN);
341 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
347 return AFPERR_NOTAUTH;
351 static int uam_setup(const char *path)
353 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
354 passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
356 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
362 static void uam_cleanup(void)
364 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
365 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
368 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
371 uam_setup, uam_cleanup