2 * $Id: uams_dhx_passwd.c,v 1.12 2001-10-24 16:21:33 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 */
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 passwd_login(void *obj, 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);
90 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
94 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
95 (void *) &clientname, NULL ) < 0 )
99 len = (unsigned char) *ibuf++;
101 return( AFPERR_PARAM );
104 memcpy(name, ibuf, len );
107 if ((unsigned long) ibuf & 1) /* padding */
110 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
114 syslog( LOG_INFO, "dhx login: %s", name);
115 if (uam_checkuser(dhxpwd) < 0)
116 return AFPERR_NOTAUTH;
119 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
120 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
121 return AFPERR_NOTAUTH;
123 dhxpwd->pw_passwd = sp->sp_pwdp;
124 #endif /* SHADOWPW */
126 if (!dhxpwd->pw_passwd)
127 return AFPERR_NOTAUTH;
129 /* get the client's public key */
130 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
135 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
140 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
146 /* okay, we're ready */
147 if (!(dh = DH_new())) {
154 /* generate key and make sure we have enough space */
157 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
161 /* figure out the key. use rbuf as a temporary buffer. */
162 i = DH_compute_key(rbuf, bn, dh);
165 CAST_set_key(&castkey, i, rbuf);
167 /* session id. it's just a hashed version of the object pointer. */
168 sessid = dhxhash(obj);
169 memcpy(rbuf, &sessid, sizeof(sessid));
170 rbuf += sizeof(sessid);
171 *rbuflen += sizeof(sessid);
173 /* send our public key */
174 BN_bn2bin(dh->pub_key, rbuf);
178 /* buffer to be encrypted */
180 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
185 memcpy(rbuf, &randbuf, sizeof(randbuf));
188 /* get the signature. it's always 16 bytes. */
189 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
190 (void *) &name, NULL) < 0) {
194 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
196 memset(rbuf + KEYSIZE, 0, KEYSIZE);
199 /* encrypt using cast */
200 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
201 *rbuflen += CRYPTBUFLEN;
204 return AFPERR_AUTHCONT;
212 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
213 char *ibuf, int ibuflen,
214 char *rbuf, int *rbuflen)
216 unsigned char iv[] = "LWallace";
217 BIGNUM *bn1, *bn2, *bn3;
223 /* check for session id */
224 memcpy(&sessid, ibuf, sizeof(sessid));
225 if (sessid != dhxhash(obj))
227 ibuf += sizeof(sessid);
229 /* use rbuf as scratch space */
230 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
233 /* check to make sure that the random number is the same. we
234 * get sent back an incremented random number. */
235 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
238 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
243 /* zero out the random number */
244 memset(rbuf, 0, sizeof(randbuf));
245 memset(randbuf, 0, sizeof(randbuf));
248 if (!(bn3 = BN_new())) {
254 BN_sub(bn3, bn1, bn2);
258 /* okay. is it one more? */
259 if (!BN_is_one(bn3)) {
265 rbuf[PASSWDLEN] = '\0';
272 uam_afp_getcmdline( &ac, &av );
273 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
275 if( sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
276 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
277 return AFPERR_NOTAUTH;
279 memset( rbuf, 0, PASSWDLEN );
284 p = crypt( rbuf, dhxpwd->pw_passwd );
285 memset(rbuf, 0, PASSWDLEN);
286 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
292 return AFPERR_NOTAUTH;
296 static int uam_setup(const char *path)
298 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
299 passwd_login, passwd_logincont, NULL) < 0)
301 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
307 static void uam_cleanup(void)
309 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
310 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
313 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
316 uam_setup, uam_cleanup