2 * $Id: uams_dhx_passwd.c,v 1.8 2001-05-22 19:13:36 rufustfirefly 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.
33 #include <openssl/bn.h>
34 #include <openssl/dh.h>
35 #include <openssl/cast.h>
40 #endif /* OPENSSL_DHX */
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 u_int8_t randbuf[16];
60 #include <sys/types.h>
61 #include <sys/security.h>
65 static int c2security = 0;
69 static int passwd_login(void *obj, struct passwd **uam_pwd,
70 char *ibuf, int ibuflen,
71 char *rbuf, int *rbuflen)
73 unsigned char iv[] = "CJalbert";
74 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
75 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
80 BIGNUM *bn, *gbn, *pbn;
87 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
88 RAND_seed(rnd_seed, sizeof rnd_seed);
93 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
96 len = (unsigned char) *ibuf++;
98 return( AFPERR_PARAM );
101 memcpy(name, ibuf, len );
104 if ((unsigned long) ibuf & 1) /* padding */
107 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
111 syslog( LOG_INFO, "dhx login: %s", name);
112 if (uam_checkuser(dhxpwd) < 0)
113 return AFPERR_NOTAUTH;
116 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
117 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
118 return AFPERR_NOTAUTH;
120 dhxpwd->pw_passwd = sp->sp_pwdp;
123 if (!dhxpwd->pw_passwd)
124 return AFPERR_NOTAUTH;
126 /* get the client's public key */
127 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
132 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
137 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
143 /* okay, we're ready */
144 if (!(dh = DH_new())) {
151 /* generate key and make sure we have enough space */
154 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
158 /* figure out the key. use rbuf as a temporary buffer. */
159 i = DH_compute_key(rbuf, bn, dh);
162 CAST_set_key(&castkey, i, rbuf);
164 /* session id. it's just a hashed version of the object pointer. */
165 sessid = dhxhash(obj);
166 memcpy(rbuf, &sessid, sizeof(sessid));
167 rbuf += sizeof(sessid);
168 *rbuflen += sizeof(sessid);
170 /* send our public key */
171 BN_bn2bin(dh->pub_key, rbuf);
175 /* buffer to be encrypted */
177 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
182 memcpy(rbuf, &randbuf, sizeof(randbuf));
185 /* get the signature. it's always 16 bytes. */
186 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
187 (void *) &name, NULL) < 0) {
191 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
193 memset(rbuf + KEYSIZE, 0, KEYSIZE);
196 /* encrypt using cast */
197 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
198 *rbuflen += CRYPTBUFLEN;
201 return AFPERR_AUTHCONT;
209 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
210 char *ibuf, int ibuflen,
211 char *rbuf, int *rbuflen)
213 unsigned char iv[] = "LWallace";
214 BIGNUM *bn1, *bn2, *bn3;
220 /* check for session id */
221 memcpy(&sessid, ibuf, sizeof(sessid));
222 if (sessid != dhxhash(obj))
224 ibuf += sizeof(sessid);
226 /* use rbuf as scratch space */
227 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
230 /* check to make sure that the random number is the same. we
231 * get sent back an incremented random number. */
232 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
235 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
240 /* zero out the random number */
241 memset(rbuf, 0, sizeof(randbuf));
242 memset(randbuf, 0, sizeof(randbuf));
245 if (!(bn3 = BN_new())) {
251 BN_sub(bn3, bn1, bn2);
255 /* okay. is it one more? */
256 if (!BN_is_one(bn3)) {
262 rbuf[PASSWDLEN] = '\0';
264 if ( c2security == 1 ) {
265 struct pr_passwd *pr = getprpwnam( dhxpwd->pw_name );
267 return AFPERR_NOTAUTH;
268 if ( strcmp( dispcrypt( rbuf, pr->ufld.fd_encrypt,
269 pr->ufld.fd_oldcrypt ), pr->ufld.fd_encrypt ) == 0 ) {
274 p = crypt( rbuf, dhxpwd->pw_passwd );
275 memset(rbuf, 0, PASSWDLEN);
276 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
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)
299 char siad[] = "siad_ses_init=";
301 if ( access( SIAIGOODFILE, F_OK ) == -1 ) {
302 syslog( LOG_ERR, "dhx uam_setup: %s does not exist",
307 if ( ( f = fopen(MATRIX_CONF, "r" ) ) == NULL ) {
308 syslog( LOG_ERR, "dhx uam_setup: %s is unreadable",
313 while ( fgets( buf, sizeof(buf), f ) != NULL ) {
314 if ( strncmp( buf, siad, sizeof(siad) - 1 ) == 0 ) {
315 if ( strstr( buf, "OSFC2" ) != NULL )
323 syslog( LOG_INFO, "dhx uam_setup: security level %s",
324 c2security == 0 ? "BSD" : "OSFC2" );
327 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
328 passwd_login, passwd_logincont, NULL) < 0)
330 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
336 static void uam_cleanup(void)
338 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
339 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
342 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
345 uam_setup, uam_cleanup