2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
4 * All Rights Reserved. See COPYRIGHT.
9 #endif /* HAVE_CONFIG_H */
11 #include <atalk/standards.h>
19 #endif /* ! HAVE_CRYPT_H */
23 #include <arpa/inet.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/logger.h>
42 #include <atalk/afp.h>
43 #include <atalk/uam.h>
47 #define CRYPTBUFLEN (KEYSIZE*2)
48 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
50 /* hash a number to a 16-bit quantity */
51 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
52 (unsigned long) (a)) & 0xffff)
55 static CAST_KEY castkey;
56 static struct passwd *dhxpwd;
57 static uint8_t randbuf[16];
63 static const char *clientname;
67 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
68 char *ibuf, size_t ibuflen _U_,
69 char *rbuf, size_t *rbuflen)
71 unsigned char iv[] = "CJalbert";
72 uint8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
73 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
78 BIGNUM *bn, *gbn, *pbn;
85 for (i = 0; i < 256; i++)
86 rnd_seed[i] = random();
87 RAND_seed(rnd_seed, sizeof(rnd_seed));
93 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
94 (void *) &clientname, NULL ) < 0 )
98 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
99 return AFPERR_NOTAUTH;
102 LOG(log_info, logtype_uams, "dhx login: %s", username);
103 if (uam_checkuser(dhxpwd) < 0)
104 return AFPERR_NOTAUTH;
107 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
108 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
109 return AFPERR_NOTAUTH;
111 dhxpwd->pw_passwd = sp->sp_pwdp;
112 #endif /* SHADOWPW */
114 if (!dhxpwd->pw_passwd)
115 return AFPERR_NOTAUTH;
117 /* get the client's public key */
118 if (!(bn = BN_bin2bn((unsigned char *)ibuf, KEYSIZE, NULL))) {
123 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
128 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
134 /* okay, we're ready */
135 if (!(dh = DH_new())) {
142 /* generate key and make sure we have enough space */
145 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
149 /* figure out the key. use rbuf as a temporary buffer. */
150 i = DH_compute_key((unsigned char *)rbuf, bn, dh);
153 CAST_set_key(&castkey, i, (unsigned char *)rbuf);
155 /* session id. it's just a hashed version of the object pointer. */
156 sessid = dhxhash(obj);
157 memcpy(rbuf, &sessid, sizeof(sessid));
158 rbuf += sizeof(sessid);
159 *rbuflen += sizeof(sessid);
161 /* send our public key */
162 BN_bn2bin(dh->pub_key, (unsigned char *)rbuf);
166 /* buffer to be encrypted */
168 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
173 memcpy(rbuf, &randbuf, sizeof(randbuf));
176 /* get the signature. it's always 16 bytes. */
177 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
178 (void *) &name, NULL) < 0) {
182 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
184 memset(rbuf + KEYSIZE, 0, KEYSIZE);
187 /* encrypt using cast */
188 CAST_cbc_encrypt((unsigned char *)rbuf, (unsigned char *)rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
189 *rbuflen += CRYPTBUFLEN;
192 return AFPERR_AUTHCONT;
201 static int passwd_login(void *obj, struct passwd **uam_pwd,
202 char *ibuf, size_t ibuflen,
203 char *rbuf, size_t *rbuflen)
210 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
211 (void *) &username, &ulen) < 0)
215 return( AFPERR_PARAM );
218 len = (unsigned char) *ibuf++;
220 if (!len || len > ibuflen || len > ulen ) {
221 return( AFPERR_PARAM );
223 memcpy(username, ibuf, len );
226 username[ len ] = '\0';
228 if ((unsigned long) ibuf & 1) { /* pad character */
232 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
236 /* cleartxt login ext
239 2 bytes len (network order)
242 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
243 char *ibuf, size_t ibuflen,
244 char *rbuf, size_t *rbuflen)
252 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
253 (void *) &username, &ulen) < 0)
259 memcpy(&temp16, uname, sizeof(temp16));
261 if (!len || len > ulen ) {
262 return( AFPERR_PARAM );
264 memcpy(username, uname +2, len );
265 username[ len ] = '\0';
266 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
269 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
270 char *ibuf, size_t ibuflen _U_,
271 char *rbuf, size_t *rbuflen)
275 #endif /* SHADOWPW */
276 unsigned char iv[] = "LWallace";
277 BIGNUM *bn1, *bn2, *bn3;
280 int err = AFPERR_NOTAUTH;
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((unsigned char *)ibuf, (unsigned char *)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((unsigned char *)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 ) {
352 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
353 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", dhxpwd->pw_name);
354 return (AFPERR_NOTAUTH);
357 /* check for expired password */
358 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
359 time_t now = time(NULL) / (60*60*24);
360 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
361 if ( expire_days < 0 ) {
362 LOG(log_info, logtype_uams, "password for user %s expired", dhxpwd->pw_name);
363 err = AFPERR_PWDEXPR;
366 #endif /* SHADOWPW */
370 return AFPERR_NOTAUTH;
374 static int uam_setup(const char *path)
376 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
377 passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
379 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
385 static void uam_cleanup(void)
387 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
388 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
391 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
394 uam_setup, uam_cleanup
397 UAM_MODULE_EXPORT struct uam_export uams_dhx_passwd = {
400 uam_setup, uam_cleanup