]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_passwd.c
Merge master
[netatalk.git] / etc / uams / uams_dhx_passwd.c
1 /*
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.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif /* HAVE_CONFIG_H */
10
11 #ifdef NETBSD
12 #define _XOPEN_SOURCE 500 /* for crypt() */
13 #endif
14 #ifdef FREEBSD
15 #define _XOPEN_SOURCE /* for crypt() */
16 #endif
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #ifdef HAVE_CRYPT_H
23 #include <crypt.h>
24 #endif /* ! HAVE_CRYPT_H */
25 #include <sys/time.h>
26 #include <time.h>
27 #include <pwd.h>
28
29 #ifdef SHADOWPW
30 #include <shadow.h>
31 #endif /* SHADOWPW */
32
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 */
40 #include <bn.h>
41 #include <dh.h>
42 #include <cast.h>
43 #endif /* OPENSSL_DHX */
44
45 #include <atalk/logger.h>
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
48
49 #define KEYSIZE 16
50 #define PASSWDLEN 64
51 #define CRYPTBUFLEN  (KEYSIZE*2)
52 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
53
54 /* hash a number to a 16-bit quantity */
55 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
56                      (unsigned long) (a)) & 0xffff)
57
58 /* the secret key */
59 static CAST_KEY castkey;
60 static struct passwd *dhxpwd;
61 static u_int8_t randbuf[16];
62
63 #ifdef TRU64
64 #include <sia.h>
65 #include <siad.h>
66
67 static const char *clientname;
68 #endif /* TRU64 */
69
70 /* dhx passwd */
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)
74 {
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 };
78     u_int8_t g = 0x07;
79 #ifdef SHADOWPW
80     struct spwd *sp;
81 #endif /* SHADOWPW */
82     BIGNUM *bn, *gbn, *pbn;
83     u_int16_t sessid;
84     size_t i;
85     DH *dh;
86
87 #ifdef TRU64
88     int rnd_seed[256];
89     for (i = 0; i < 256; 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(obj, username, ulen)) == NULL ) {
103         return AFPERR_NOTAUTH;
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((unsigned char *)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((unsigned char *)rbuf, bn, dh);
155     
156     /* set the key */
157     CAST_set_key(&castkey, i, (unsigned char *)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, (unsigned char *)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((unsigned char *)rbuf, (unsigned char *)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, size_t ibuflen,
207                         char *rbuf, size_t *rbuflen)
208 {
209     char *username;
210     size_t 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 < 2) {
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 utf8 name
245 */
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)
249 {
250     char       *username;
251     size_t     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, size_t ibuflen _U_, 
275                             char *rbuf, size_t *rbuflen)
276 {
277 #ifdef SHADOWPW
278     struct spwd *sp;
279 #endif /* SHADOWPW */
280     unsigned char iv[] = "LWallace";
281     BIGNUM *bn1, *bn2, *bn3;
282     u_int16_t sessid;
283     char *p;
284     int err = AFPERR_NOTAUTH;
285
286     *rbuflen = 0;
287
288     /* check for session id */
289     memcpy(&sessid, ibuf, sizeof(sessid));
290     if (sessid != dhxhash(obj))
291       return AFPERR_PARAM;
292     ibuf += sizeof(sessid);
293    
294     /* use rbuf as scratch space */
295     CAST_cbc_encrypt((unsigned char *)ibuf, (unsigned char *)rbuf, CRYPT2BUFLEN, &castkey,
296                      iv, CAST_DECRYPT);
297     
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)))
301       return AFPERR_PARAM;
302
303     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
304       BN_free(bn1);
305       return AFPERR_PARAM;
306     }
307       
308     /* zero out the random number */
309     memset(rbuf, 0, sizeof(randbuf));
310     memset(randbuf, 0, sizeof(randbuf));
311     rbuf += KEYSIZE;
312
313     if (!(bn3 = BN_new())) {
314       BN_free(bn2);
315       BN_free(bn1);
316       return AFPERR_PARAM;
317     }
318
319     BN_sub(bn3, bn1, bn2);
320     BN_free(bn2);
321     BN_free(bn1);
322
323     /* okay. is it one more? */
324     if (!BN_is_one(bn3)) {
325       BN_free(bn3);
326       return AFPERR_PARAM;
327     }
328     BN_free(bn3);
329
330     rbuf[PASSWDLEN] = '\0';
331 #ifdef TRU64
332     {
333         int ac;
334         char **av;
335         char hostname[256];
336
337         uam_afp_getcmdline( &ac, &av );
338         sprintf( hostname, "%s@%s", dhxpwd->pw_name, clientname );
339
340         if( uam_sia_validate_user( NULL, ac, av, hostname, dhxpwd->pw_name,
341                                    NULL, FALSE, NULL, rbuf ) != SIASUCCESS )
342             return AFPERR_NOTAUTH;
343
344         memset( rbuf, 0, PASSWDLEN );
345         *uam_pwd = dhxpwd;
346         return AFP_OK;
347     }
348 #else /* TRU64 */
349     p = crypt( rbuf, dhxpwd->pw_passwd );
350     memset(rbuf, 0, PASSWDLEN);
351     if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
352       *uam_pwd = dhxpwd;
353       err = AFP_OK;
354     }
355 #ifdef SHADOWPW
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);
359     }
360
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;
368         }
369     }
370 #endif /* SHADOWPW */
371     return err;
372 #endif /* TRU64 */
373
374     return AFPERR_NOTAUTH;
375 }
376
377
378 static int uam_setup(const char *path)
379 {
380   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHCAST128",
381                    passwd_login, passwd_logincont, NULL, passwd_login_ext) < 0)
382     return -1;
383   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
384     passwd_printer);*/
385
386   return 0;
387 }
388
389 static void uam_cleanup(void)
390 {
391   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
392   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
393 }
394
395 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
396   UAM_MODULE_SERVER,
397   UAM_MODULE_VERSION,
398   uam_setup, uam_cleanup
399 };
400
401 UAM_MODULE_EXPORT struct uam_export uams_dhx_passwd = {
402   UAM_MODULE_SERVER,
403   UAM_MODULE_VERSION,
404   uam_setup, uam_cleanup
405 };