2 * $Id: uams_dhx_passwd.c,v 1.15 2002-02-13 16:44:59 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.
11 #endif /* HAVE_CONFIG_H */
18 #endif /* HAVE_UNISTD_H */
21 #endif /* ! NO_CRYPT_H */
23 #include <atalk/logger.h>
30 #include <openssl/bn.h>
31 #include <openssl/dh.h>
32 #include <openssl/cast.h>
33 #else /* OPENSSL_DHX */
37 #endif /* OPENSSL_DHX */
39 #include <atalk/afp.h>
40 #include <atalk/uam.h>
44 #define CRYPTBUFLEN (KEYSIZE*2)
45 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
47 /* hash a number to a 16-bit quantity */
48 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
49 (unsigned long) (a)) & 0xffff)
52 static CAST_KEY castkey;
53 static struct passwd *dhxpwd;
54 static u_int8_t randbuf[16];
60 static char *clientname;
64 static int passwd_login(void *obj, struct passwd **uam_pwd,
65 char *ibuf, int ibuflen,
66 char *rbuf, int *rbuflen)
68 unsigned char iv[] = "CJalbert";
69 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
70 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
75 BIGNUM *bn, *gbn, *pbn;
82 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
83 RAND_seed(rnd_seed, sizeof rnd_seed);
88 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
92 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
93 (void *) &clientname, NULL ) < 0 )
97 len = (unsigned char) *ibuf++;
99 return( AFPERR_PARAM );
102 memcpy(name, ibuf, len );
105 if ((unsigned long) ibuf & 1) /* padding */
108 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
112 LOG(log_info, logtype_default, "dhx login: %s", name);
113 if (uam_checkuser(dhxpwd) < 0)
114 return AFPERR_NOTAUTH;
117 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
118 LOG(log_info, logtype_default, "no shadow passwd entry for %s", name);
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(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(rbuf, bn, dh);
163 CAST_set_key(&castkey, i, 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, 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(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
199 *rbuflen += CRYPTBUFLEN;
202 return AFPERR_AUTHCONT;
210 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
211 char *ibuf, int ibuflen,
212 char *rbuf, int *rbuflen)
214 unsigned char iv[] = "LWallace";
215 BIGNUM *bn1, *bn2, *bn3;
221 /* check for session id */
222 memcpy(&sessid, ibuf, sizeof(sessid));
223 if (sessid != dhxhash(obj))
225 ibuf += sizeof(sessid);
227 /* use rbuf as scratch space */
228 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
231 /* check to make sure that the random number is the same. we
232 * get sent back an incremented random number. */
233 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
236 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
241 /* zero out the random number */
242 memset(rbuf, 0, sizeof(randbuf));
243 memset(randbuf, 0, sizeof(randbuf));
246 if (!(bn3 = BN_new())) {
252 BN_sub(bn3, bn1, bn2);
256 /* okay. is it one more? */
257 if (!BN_is_one(bn3)) {
263 rbuf[PASSWDLEN] = '\0';
270 uam_afp_getcmdline( &ac, &av );
271 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
273 if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
274 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
275 return AFPERR_NOTAUTH;
277 memset( rbuf, 0, PASSWDLEN );
282 p = crypt( rbuf, dhxpwd->pw_passwd );
283 memset(rbuf, 0, PASSWDLEN);
284 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
290 return AFPERR_NOTAUTH;
294 static int uam_setup(const char *path)
296 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
297 passwd_login, passwd_logincont, NULL) < 0)
299 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
305 static void uam_cleanup(void)
307 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
308 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
311 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
314 uam_setup, uam_cleanup