]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_passwd.c
e79047b13ebd3a8d09f81b421311b693ae1f420f
[netatalk.git] / etc / uams / uams_dhx_passwd.c
1 /*
2  * $Id: uams_dhx_passwd.c,v 1.19 2003-06-11 07:16:14 srittau Exp $
3  *
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.
7  */
8
9 #define _XOPEN_SOURCE /* for crypt() */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif /* HAVE_UNISTD_H */
21 #ifndef NO_CRYPT_H
22 #include <crypt.h>
23 #endif /* ! NO_CRYPT_H */
24 #include <pwd.h>
25 #include <atalk/logger.h>
26
27 #ifdef SHADOWPW
28 #include <shadow.h>
29 #endif /* SHADOWPW */
30
31 #if defined(GNUTLS_DHX)
32 #include <gnutls/openssl.h>
33 #elif defined(OPENSSL_DHX)
34 #include <openssl/bn.h>
35 #include <openssl/dh.h>
36 #include <openssl/cast.h>
37 #else /* OPENSSL_DHX */
38 #include <bn.h>
39 #include <dh.h>
40 #include <cast.h>
41 #endif /* OPENSSL_DHX */
42
43 #include <atalk/afp.h>
44 #include <atalk/uam.h>
45
46 #define KEYSIZE 16
47 #define PASSWDLEN 64
48 #define CRYPTBUFLEN  (KEYSIZE*2)
49 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
50
51 /* hash a number to a 16-bit quantity */
52 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
53                      (unsigned long) (a)) & 0xffff)
54
55 /* the secret key */
56 static CAST_KEY castkey;
57 static struct passwd *dhxpwd;
58 static u_int8_t randbuf[16];
59
60 #ifdef TRU64
61 #include <sia.h>
62 #include <siad.h>
63
64 static char *clientname;
65 #endif /* TRU64 */
66
67 /* dhx passwd */
68 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
69                         char *ibuf, int ibuflen,
70                         char *rbuf, int *rbuflen)
71 {
72     unsigned char iv[] = "CJalbert";
73     u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
74                     0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
75     u_int8_t g = 0x07;
76 #ifdef SHADOWPW
77     struct spwd *sp;
78 #endif /* SHADOWPW */
79     BIGNUM *bn, *gbn, *pbn;
80     u_int16_t sessid;
81     int i;
82 #if 0
83     char *name;
84 #endif
85     DH *dh;
86
87 #ifdef TRU64
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);
90 #endif /* TRU64 */
91
92     *rbuflen = 0;
93
94 #ifdef TRU64
95     if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
96                               (void *) &clientname, NULL ) < 0 )
97         return AFPERR_PARAM;
98 #endif /* TRU64 */
99
100     if (( dhxpwd = uam_getname(username, ulen)) == NULL ) {
101         return AFPERR_PARAM;
102     }
103     
104     LOG(log_info, logtype_uams, "dhx login: %s", username);
105     if (uam_checkuser(dhxpwd) < 0)
106       return AFPERR_NOTAUTH;
107
108 #ifdef SHADOWPW
109     if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
110         LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
111         return AFPERR_NOTAUTH;
112     }
113     dhxpwd->pw_passwd = sp->sp_pwdp;
114 #endif /* SHADOWPW */
115
116     if (!dhxpwd->pw_passwd)
117       return AFPERR_NOTAUTH;
118
119     /* get the client's public key */
120     if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
121       return AFPERR_PARAM;
122     }
123
124     /* get our primes */
125     if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
126       BN_free(bn);
127       return AFPERR_PARAM;
128     }
129
130     if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
131       BN_free(gbn);
132       BN_free(bn);
133       return AFPERR_PARAM;
134     }
135
136     /* okay, we're ready */
137     if (!(dh = DH_new())) {
138       BN_free(pbn);
139       BN_free(gbn);
140       BN_free(bn);
141       return AFPERR_PARAM;
142     }
143
144     /* generate key and make sure we have enough space */
145     dh->p = pbn;
146     dh->g = gbn;
147     if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
148       goto passwd_fail;
149     }
150
151     /* figure out the key. use rbuf as a temporary buffer. */
152     i = DH_compute_key(rbuf, bn, dh);
153     
154     /* set the key */
155     CAST_set_key(&castkey, i, rbuf);
156     
157     /* session id. it's just a hashed version of the object pointer. */
158     sessid = dhxhash(obj);
159     memcpy(rbuf, &sessid, sizeof(sessid));
160     rbuf += sizeof(sessid);
161     *rbuflen += sizeof(sessid);
162     
163     /* send our public key */
164     BN_bn2bin(dh->pub_key, rbuf); 
165     rbuf += KEYSIZE;
166     *rbuflen += KEYSIZE;
167
168     /* buffer to be encrypted */
169     i = sizeof(randbuf);
170     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
171                              &i) < 0) {
172       *rbuflen = 0;
173       goto passwd_fail;
174     }    
175     memcpy(rbuf, &randbuf, sizeof(randbuf));
176
177 #if 0
178     /* get the signature. it's always 16 bytes. */
179     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
180                              (void *) &name, NULL) < 0) {
181       *rbuflen = 0;
182       goto passwd_fail;
183     }
184     memcpy(rbuf + KEYSIZE, name, KEYSIZE); 
185 #else /* 0 */
186     memset(rbuf + KEYSIZE, 0, KEYSIZE);
187 #endif /* 0 */
188
189     /* encrypt using cast */
190     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
191     *rbuflen += CRYPTBUFLEN;
192     BN_free(bn);
193     DH_free(dh);
194     return AFPERR_AUTHCONT;
195
196 passwd_fail:
197     BN_free(bn);
198     DH_free(dh);
199     return AFPERR_PARAM;
200 }
201
202 /* cleartxt login */
203 static int passwd_login(void *obj, struct passwd **uam_pwd,
204                         char *ibuf, int ibuflen,
205                         char *rbuf, int *rbuflen)
206 {
207     char *username;
208     int len, ulen;
209
210     *rbuflen = 0;
211
212     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
213                              (void *) &username, &ulen) < 0)
214         return AFPERR_MISC;
215
216     if (ibuflen <= 1) {
217         return( AFPERR_PARAM );
218     }
219
220     len = (unsigned char) *ibuf++;
221     ibuflen--;
222     if (!len || len > ibuflen || len > ulen ) {
223         return( AFPERR_PARAM );
224     }
225     memcpy(username, ibuf, len );
226     ibuf += len;
227     ibuflen -=len;
228     username[ len ] = '\0';
229
230     if ((unsigned long) ibuf & 1) { /* pad character */
231         ++ibuf;
232         ibuflen--;
233     }
234     return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
235     
236 }
237
238 /* cleartxt login ext 
239  * uname format :
240     byte      3
241     2 bytes   len (network order)
242     len bytes unicode name
243 */
244 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
245                         char *ibuf, int ibuflen,
246                         char *rbuf, int *rbuflen)
247 {
248     char       *username;
249     int        len, ulen;
250     u_int16_t  temp16;
251
252     *rbuflen = 0;
253     
254     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
255                              (void *) &username, &ulen) < 0)
256         return AFPERR_MISC;
257
258     if (*uname != 3)
259         return AFPERR_PARAM;
260     uname++;
261     memcpy(&temp16, uname, sizeof(temp16));
262     len = ntohs(temp16);
263     if (!len || len > ulen ) {
264         return( AFPERR_PARAM );
265     }
266     memcpy(username, uname +2, len );
267     username[ len ] = '\0';
268     return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
269 }
270                         
271 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
272                             char *ibuf, int ibuflen, 
273                             char *rbuf, int *rbuflen)
274 {
275     unsigned char iv[] = "LWallace";
276     BIGNUM *bn1, *bn2, *bn3;
277     u_int16_t sessid;
278     char *p;
279
280     *rbuflen = 0;
281
282     /* check for session id */
283     memcpy(&sessid, ibuf, sizeof(sessid));
284     if (sessid != dhxhash(obj))
285       return AFPERR_PARAM;
286     ibuf += sizeof(sessid);
287    
288     /* use rbuf as scratch space */
289     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
290                      iv, CAST_DECRYPT);
291     
292     /* check to make sure that the random number is the same. we
293      * get sent back an incremented random number. */
294     if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
295       return AFPERR_PARAM;
296
297     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
298       BN_free(bn1);
299       return AFPERR_PARAM;
300     }
301       
302     /* zero out the random number */
303     memset(rbuf, 0, sizeof(randbuf));
304     memset(randbuf, 0, sizeof(randbuf));
305     rbuf += KEYSIZE;
306
307     if (!(bn3 = BN_new())) {
308       BN_free(bn2);
309       BN_free(bn1);
310       return AFPERR_PARAM;
311     }
312
313     BN_sub(bn3, bn1, bn2);
314     BN_free(bn2);
315     BN_free(bn1);
316
317     /* okay. is it one more? */
318     if (!BN_is_one(bn3)) {
319       BN_free(bn3);
320       return AFPERR_PARAM;
321     }
322     BN_free(bn3);
323
324     rbuf[PASSWDLEN] = '\0';
325 #ifdef TRU64
326     {
327         int ac;
328         char **av;
329         char hostname[256];
330
331         uam_afp_getcmdline( &ac, &av );
332         sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
333
334         if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
335                                    NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
336             return AFPERR_NOTAUTH;
337
338         memset( rbuf, 0, PASSWDLEN );
339         *uam_pwd = dhxpwd;
340         return AFP_OK;
341     }
342 #else /* TRU64 */
343     p = crypt( rbuf, dhxpwd->pw_passwd );
344     memset(rbuf, 0, PASSWDLEN);
345     if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
346       *uam_pwd = dhxpwd;
347       return AFP_OK;
348     }
349 #endif /* TRU64 */
350
351     return AFPERR_NOTAUTH;
352 }
353
354
355 static int uam_setup(const char *path)
356 {
357   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
358                    passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
359     return -1;
360   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
361     passwd_printer);*/
362
363   return 0;
364 }
365
366 static void uam_cleanup(void)
367 {
368   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
369   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
370 }
371
372 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
373   UAM_MODULE_SERVER,
374   UAM_MODULE_VERSION,
375   uam_setup, uam_cleanup
376 };