2 * $Id: uams_dhx_passwd.c,v 1.11 2001-10-24 16:19:12 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 */
20 #endif /* HAVE_UNISTD_H */
23 #endif /* ! NO_CRYPT_H */
36 #include <openssl/bn.h>
37 #include <openssl/dh.h>
38 #include <openssl/cast.h>
39 #else /* OPENSSL_DHX */
43 #endif /* OPENSSL_DHX */
45 #include <atalk/afp.h>
46 #include <atalk/uam.h>
50 #define CRYPTBUFLEN (KEYSIZE*2)
51 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
53 /* hash a number to a 16-bit quantity */
54 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
55 (unsigned long) (a)) & 0xffff)
58 static CAST_KEY castkey;
59 static struct passwd *dhxpwd;
60 static u_int8_t randbuf[16];
66 static char *clientname;
70 static int passwd_login(void *obj, struct passwd **uam_pwd,
71 char *ibuf, int ibuflen,
72 char *rbuf, int *rbuflen)
74 unsigned char iv[] = "CJalbert";
75 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
76 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
81 BIGNUM *bn, *gbn, *pbn;
88 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
89 RAND_seed(rnd_seed, sizeof rnd_seed);
94 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
98 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
99 (void *) &clientname, NULL ) < 0 )
103 len = (unsigned char) *ibuf++;
105 return( AFPERR_PARAM );
108 memcpy(name, ibuf, len );
111 if ((unsigned long) ibuf & 1) /* padding */
114 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
118 syslog( LOG_INFO, "dhx login: %s", name);
119 if (uam_checkuser(dhxpwd) < 0)
120 return AFPERR_NOTAUTH;
123 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
124 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
125 return AFPERR_NOTAUTH;
127 dhxpwd->pw_passwd = sp->sp_pwdp;
128 #endif /* SHADOWPW */
130 if (!dhxpwd->pw_passwd)
131 return AFPERR_NOTAUTH;
133 /* get the client's public key */
134 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
139 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
144 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
150 /* okay, we're ready */
151 if (!(dh = DH_new())) {
158 /* generate key and make sure we have enough space */
161 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
165 /* figure out the key. use rbuf as a temporary buffer. */
166 i = DH_compute_key(rbuf, bn, dh);
169 CAST_set_key(&castkey, i, rbuf);
171 /* session id. it's just a hashed version of the object pointer. */
172 sessid = dhxhash(obj);
173 memcpy(rbuf, &sessid, sizeof(sessid));
174 rbuf += sizeof(sessid);
175 *rbuflen += sizeof(sessid);
177 /* send our public key */
178 BN_bn2bin(dh->pub_key, rbuf);
182 /* buffer to be encrypted */
184 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
189 memcpy(rbuf, &randbuf, sizeof(randbuf));
192 /* get the signature. it's always 16 bytes. */
193 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
194 (void *) &name, NULL) < 0) {
198 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
200 memset(rbuf + KEYSIZE, 0, KEYSIZE);
203 /* encrypt using cast */
204 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
205 *rbuflen += CRYPTBUFLEN;
208 return AFPERR_AUTHCONT;
216 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
217 char *ibuf, int ibuflen,
218 char *rbuf, int *rbuflen)
220 unsigned char iv[] = "LWallace";
221 BIGNUM *bn1, *bn2, *bn3;
227 /* check for session id */
228 memcpy(&sessid, ibuf, sizeof(sessid));
229 if (sessid != dhxhash(obj))
231 ibuf += sizeof(sessid);
233 /* use rbuf as scratch space */
234 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
237 /* check to make sure that the random number is the same. we
238 * get sent back an incremented random number. */
239 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
242 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
247 /* zero out the random number */
248 memset(rbuf, 0, sizeof(randbuf));
249 memset(randbuf, 0, sizeof(randbuf));
252 if (!(bn3 = BN_new())) {
258 BN_sub(bn3, bn1, bn2);
262 /* okay. is it one more? */
263 if (!BN_is_one(bn3)) {
269 rbuf[PASSWDLEN] = '\0';
276 uam_afp_getcmdline( &ac, &av );
277 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
279 if( sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
280 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
281 return AFPERR_NOTAUTH;
283 memset( rbuf, 0, PASSWDLEN );
288 p = crypt( rbuf, dhxpwd->pw_passwd );
289 memset(rbuf, 0, PASSWDLEN);
290 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
296 return AFPERR_NOTAUTH;
300 static int uam_setup(const char *path)
302 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
303 passwd_login, passwd_logincont, NULL) < 0)
305 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
311 static void uam_cleanup(void)
313 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
314 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
317 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
320 uam_setup, uam_cleanup