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 */
12 #define _XOPEN_SOURCE 500 /* for crypt() */
15 #define _XOPEN_SOURCE /* for crypt() */
24 #endif /* ! HAVE_CRYPT_H */
33 #if defined(GNUTLS_DHX)
34 #include <gnutls/openssl.h>
35 #elif defined(OPENSSL_DHX)
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/logger.h>
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
51 #define CRYPTBUFLEN (KEYSIZE*2)
52 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
54 /* hash a number to a 16-bit quantity */
55 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
56 (unsigned long) (a)) & 0xffff)
59 static CAST_KEY castkey;
60 static struct passwd *dhxpwd;
61 static u_int8_t randbuf[16];
67 static const char *clientname;
71 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
72 char *ibuf, size_t ibuflen _U_,
73 char *rbuf, size_t *rbuflen)
75 unsigned char iv[] = "CJalbert";
76 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
77 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
82 BIGNUM *bn, *gbn, *pbn;
89 for (i = 0; i < 256; i++)
90 rnd_seed[i] = random();
91 RAND_seed(rnd_seed, sizeof(rnd_seed));
97 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
98 (void *) &clientname, NULL ) < 0 )
102 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
103 return AFPERR_NOTAUTH;
106 LOG(log_info, logtype_uams, "dhx login: %s", username);
107 if (uam_checkuser(dhxpwd) < 0)
108 return AFPERR_NOTAUTH;
111 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
112 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
113 return AFPERR_NOTAUTH;
115 dhxpwd->pw_passwd = sp->sp_pwdp;
116 #endif /* SHADOWPW */
118 if (!dhxpwd->pw_passwd)
119 return AFPERR_NOTAUTH;
121 /* get the client's public key */
122 if (!(bn = BN_bin2bn((unsigned char *)ibuf, KEYSIZE, NULL))) {
127 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
132 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
138 /* okay, we're ready */
139 if (!(dh = DH_new())) {
146 /* generate key and make sure we have enough space */
149 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
153 /* figure out the key. use rbuf as a temporary buffer. */
154 i = DH_compute_key((unsigned char *)rbuf, bn, dh);
157 CAST_set_key(&castkey, i, (unsigned char *)rbuf);
159 /* session id. it's just a hashed version of the object pointer. */
160 sessid = dhxhash(obj);
161 memcpy(rbuf, &sessid, sizeof(sessid));
162 rbuf += sizeof(sessid);
163 *rbuflen += sizeof(sessid);
165 /* send our public key */
166 BN_bn2bin(dh->pub_key, (unsigned char *)rbuf);
170 /* buffer to be encrypted */
172 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
177 memcpy(rbuf, &randbuf, sizeof(randbuf));
180 /* get the signature. it's always 16 bytes. */
181 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
182 (void *) &name, NULL) < 0) {
186 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
188 memset(rbuf + KEYSIZE, 0, KEYSIZE);
191 /* encrypt using cast */
192 CAST_cbc_encrypt((unsigned char *)rbuf, (unsigned char *)rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
193 *rbuflen += CRYPTBUFLEN;
196 return AFPERR_AUTHCONT;
205 static int passwd_login(void *obj, struct passwd **uam_pwd,
206 char *ibuf, size_t ibuflen,
207 char *rbuf, size_t *rbuflen)
214 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
215 (void *) &username, &ulen) < 0)
219 return( AFPERR_PARAM );
222 len = (unsigned char) *ibuf++;
224 if (!len || len > ibuflen || len > ulen ) {
225 return( AFPERR_PARAM );
227 memcpy(username, ibuf, len );
230 username[ len ] = '\0';
232 if ((unsigned long) ibuf & 1) { /* pad character */
236 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
240 /* cleartxt login ext
243 2 bytes len (network order)
246 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
247 char *ibuf, size_t ibuflen,
248 char *rbuf, size_t *rbuflen)
256 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
257 (void *) &username, &ulen) < 0)
263 memcpy(&temp16, uname, sizeof(temp16));
265 if (!len || len > ulen ) {
266 return( AFPERR_PARAM );
268 memcpy(username, uname +2, len );
269 username[ len ] = '\0';
270 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
273 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
274 char *ibuf, size_t ibuflen _U_,
275 char *rbuf, size_t *rbuflen)
279 #endif /* SHADOWPW */
280 unsigned char iv[] = "LWallace";
281 BIGNUM *bn1, *bn2, *bn3;
284 int err = AFPERR_NOTAUTH;
288 /* check for session id */
289 memcpy(&sessid, ibuf, sizeof(sessid));
290 if (sessid != dhxhash(obj))
292 ibuf += sizeof(sessid);
294 /* use rbuf as scratch space */
295 CAST_cbc_encrypt((unsigned char *)ibuf, (unsigned char *)rbuf, CRYPT2BUFLEN, &castkey,
298 /* check to make sure that the random number is the same. we
299 * get sent back an incremented random number. */
300 if (!(bn1 = BN_bin2bn((unsigned char *)rbuf, KEYSIZE, NULL)))
303 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
308 /* zero out the random number */
309 memset(rbuf, 0, sizeof(randbuf));
310 memset(randbuf, 0, sizeof(randbuf));
313 if (!(bn3 = BN_new())) {
319 BN_sub(bn3, bn1, bn2);
323 /* okay. is it one more? */
324 if (!BN_is_one(bn3)) {
330 rbuf[PASSWDLEN] = '\0';
337 uam_afp_getcmdline( &ac, &av );
338 sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
340 if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
341 NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
342 return AFPERR_NOTAUTH;
344 memset( rbuf, 0, PASSWDLEN );
349 p = crypt( rbuf, dhxpwd->pw_passwd );
350 memset(rbuf, 0, PASSWDLEN);
351 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
356 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
357 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", dhxpwd->pw_name);
358 return (AFPERR_NOTAUTH);
361 /* check for expired password */
362 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
363 time_t now = time(NULL) / (60*60*24);
364 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
365 if ( expire_days < 0 ) {
366 LOG(log_info, logtype_uams, "password for user %s expired", dhxpwd->pw_name);
367 err = AFPERR_PWDEXPR;
370 #endif /* SHADOWPW */
374 return AFPERR_NOTAUTH;
378 static int uam_setup(const char *path)
380 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
381 passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
383 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
389 static void uam_cleanup(void)
391 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
392 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
395 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
398 uam_setup, uam_cleanup
401 UAM_MODULE_EXPORT struct uam_export uams_dhx_passwd = {
404 uam_setup, uam_cleanup