]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_passwd.c
51feca18a79099441d914ef321f928293c62032f
[netatalk.git] / etc / uams / uams_dhx_passwd.c
1 /*
2  * $Id: uams_dhx_passwd.c,v 1.21 2003-06-11 22:07:56 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     int rnd_seed[256];
89     for (i = 0; i < sizeof(rnd_seed); i++)
90         rnd_seed[i] = random();
91     RAND_seed(rnd_seed, sizeof rnd_seed);
92 #endif /* TRU64 */
93
94     *rbuflen = 0;
95
96 #ifdef TRU64
97     if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
98                               (void *) &clientname, NULL ) < 0 )
99         return AFPERR_PARAM;
100 #endif /* TRU64 */
101
102     if (( dhxpwd = uam_getname(username, ulen)) == NULL ) {
103         return AFPERR_PARAM;
104     }
105     
106     LOG(log_info, logtype_uams, "dhx login: %s", username);
107     if (uam_checkuser(dhxpwd) < 0)
108       return AFPERR_NOTAUTH;
109
110 #ifdef SHADOWPW
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;
114     }
115     dhxpwd->pw_passwd = sp->sp_pwdp;
116 #endif /* SHADOWPW */
117
118     if (!dhxpwd->pw_passwd)
119       return AFPERR_NOTAUTH;
120
121     /* get the client's public key */
122     if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
123       return AFPERR_PARAM;
124     }
125
126     /* get our primes */
127     if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
128       BN_free(bn);
129       return AFPERR_PARAM;
130     }
131
132     if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
133       BN_free(gbn);
134       BN_free(bn);
135       return AFPERR_PARAM;
136     }
137
138     /* okay, we're ready */
139     if (!(dh = DH_new())) {
140       BN_free(pbn);
141       BN_free(gbn);
142       BN_free(bn);
143       return AFPERR_PARAM;
144     }
145
146     /* generate key and make sure we have enough space */
147     dh->p = pbn;
148     dh->g = gbn;
149     if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
150       goto passwd_fail;
151     }
152
153     /* figure out the key. use rbuf as a temporary buffer. */
154     i = DH_compute_key(rbuf, bn, dh);
155     
156     /* set the key */
157     CAST_set_key(&castkey, i, rbuf);
158     
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);
164     
165     /* send our public key */
166     BN_bn2bin(dh->pub_key, rbuf); 
167     rbuf += KEYSIZE;
168     *rbuflen += KEYSIZE;
169
170     /* buffer to be encrypted */
171     i = sizeof(randbuf);
172     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
173                              &i) < 0) {
174       *rbuflen = 0;
175       goto passwd_fail;
176     }    
177     memcpy(rbuf, &randbuf, sizeof(randbuf));
178
179 #if 0
180     /* get the signature. it's always 16 bytes. */
181     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
182                              (void *) &name, NULL) < 0) {
183       *rbuflen = 0;
184       goto passwd_fail;
185     }
186     memcpy(rbuf + KEYSIZE, name, KEYSIZE); 
187 #else /* 0 */
188     memset(rbuf + KEYSIZE, 0, KEYSIZE);
189 #endif /* 0 */
190
191     /* encrypt using cast */
192     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
193     *rbuflen += CRYPTBUFLEN;
194     BN_free(bn);
195     DH_free(dh);
196     return AFPERR_AUTHCONT;
197
198 passwd_fail:
199     BN_free(bn);
200     DH_free(dh);
201     return AFPERR_PARAM;
202 }
203
204 /* cleartxt login */
205 static int passwd_login(void *obj, struct passwd **uam_pwd,
206                         char *ibuf, int ibuflen,
207                         char *rbuf, int *rbuflen)
208 {
209     char *username;
210     int len, ulen;
211
212     *rbuflen = 0;
213
214     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
215                              (void *) &username, &ulen) < 0)
216         return AFPERR_MISC;
217
218     if (ibuflen <= 1) {
219         return( AFPERR_PARAM );
220     }
221
222     len = (unsigned char) *ibuf++;
223     ibuflen--;
224     if (!len || len > ibuflen || len > ulen ) {
225         return( AFPERR_PARAM );
226     }
227     memcpy(username, ibuf, len );
228     ibuf += len;
229     ibuflen -=len;
230     username[ len ] = '\0';
231
232     if ((unsigned long) ibuf & 1) { /* pad character */
233         ++ibuf;
234         ibuflen--;
235     }
236     return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
237     
238 }
239
240 /* cleartxt login ext 
241  * uname format :
242     byte      3
243     2 bytes   len (network order)
244     len bytes unicode name
245 */
246 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
247                         char *ibuf, int ibuflen,
248                         char *rbuf, int *rbuflen)
249 {
250     char       *username;
251     int        len, ulen;
252     u_int16_t  temp16;
253
254     *rbuflen = 0;
255     
256     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
257                              (void *) &username, &ulen) < 0)
258         return AFPERR_MISC;
259
260     if (*uname != 3)
261         return AFPERR_PARAM;
262     uname++;
263     memcpy(&temp16, uname, sizeof(temp16));
264     len = ntohs(temp16);
265     if (!len || len > ulen ) {
266         return( AFPERR_PARAM );
267     }
268     memcpy(username, uname +2, len );
269     username[ len ] = '\0';
270     return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
271 }
272                         
273 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
274                             char *ibuf, int ibuflen, 
275                             char *rbuf, int *rbuflen)
276 {
277     unsigned char iv[] = "LWallace";
278     BIGNUM *bn1, *bn2, *bn3;
279     u_int16_t sessid;
280     char *p;
281
282     *rbuflen = 0;
283
284     /* check for session id */
285     memcpy(&sessid, ibuf, sizeof(sessid));
286     if (sessid != dhxhash(obj))
287       return AFPERR_PARAM;
288     ibuf += sizeof(sessid);
289    
290     /* use rbuf as scratch space */
291     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
292                      iv, CAST_DECRYPT);
293     
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(rbuf, KEYSIZE, NULL)))
297       return AFPERR_PARAM;
298
299     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
300       BN_free(bn1);
301       return AFPERR_PARAM;
302     }
303       
304     /* zero out the random number */
305     memset(rbuf, 0, sizeof(randbuf));
306     memset(randbuf, 0, sizeof(randbuf));
307     rbuf += KEYSIZE;
308
309     if (!(bn3 = BN_new())) {
310       BN_free(bn2);
311       BN_free(bn1);
312       return AFPERR_PARAM;
313     }
314
315     BN_sub(bn3, bn1, bn2);
316     BN_free(bn2);
317     BN_free(bn1);
318
319     /* okay. is it one more? */
320     if (!BN_is_one(bn3)) {
321       BN_free(bn3);
322       return AFPERR_PARAM;
323     }
324     BN_free(bn3);
325
326     rbuf[PASSWDLEN] = '\0';
327 #ifdef TRU64
328     {
329         int ac;
330         char **av;
331         char hostname[256];
332
333         uam_afp_getcmdline( &ac, &av );
334         sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
335
336         if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
337                                    NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
338             return AFPERR_NOTAUTH;
339
340         memset( rbuf, 0, PASSWDLEN );
341         *uam_pwd = dhxpwd;
342         return AFP_OK;
343     }
344 #else /* TRU64 */
345     p = crypt( rbuf, dhxpwd->pw_passwd );
346     memset(rbuf, 0, PASSWDLEN);
347     if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
348       *uam_pwd = dhxpwd;
349       return AFP_OK;
350     }
351 #endif /* TRU64 */
352
353     return AFPERR_NOTAUTH;
354 }
355
356
357 static int uam_setup(const char *path)
358 {
359   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
360                    passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
361     return -1;
362   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
363     passwd_printer);*/
364
365   return 0;
366 }
367
368 static void uam_cleanup(void)
369 {
370   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
371   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
372 }
373
374 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
375   UAM_MODULE_SERVER,
376   UAM_MODULE_VERSION,
377   uam_setup, uam_cleanup
378 };