gdb - Local mods (compile)
[dragonfly.git] / lib / libc / rpc / auth_des.c
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice, 
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice, 
10  *   this list of conditions and the following disclaimer in the documentation 
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its 
13  *   contributors may be used to endorse or promote products derived 
14  *   from this software without specific prior written permission.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)auth_des.c       2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI
29  * $FreeBSD: src/lib/libc/rpc/auth_des.c,v 1.10 2006/02/27 22:10:58 deischen Exp $
30  */
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34 /*
35  * auth_des.c, client-side implementation of DES authentication
36  */
37
38 #include "namespace.h"
39 #include "reentrant.h"
40 #include <err.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <rpc/des_crypt.h>
46 #include <syslog.h>
47 #include <rpc/types.h>
48 #include <rpc/auth.h>
49 #include <rpc/auth_des.h>
50 #include <rpc/clnt.h>
51 #include <rpc/xdr.h>
52 #include <sys/socket.h>
53 #undef NIS
54 #include <rpcsvc/nis.h>
55 #include "un-namespace.h"
56 #include "mt_misc.h"
57
58 #define USEC_PER_SEC            1000000
59 #define RTIME_TIMEOUT           5       /* seconds to wait for sync */
60
61 #define AUTH_PRIVATE(auth)      (struct ad_private *) auth->ah_private
62 #define ALLOC(object_type)      (object_type *) mem_alloc(sizeof(object_type))
63 #define FREE(ptr, size)         mem_free((char *)(ptr), (int) size)
64 #define ATTEMPT(xdr_op)         if (!(xdr_op)) return (FALSE)
65
66 extern bool_t   xdr_authdes_cred(XDR *, struct authdes_cred *);
67 extern bool_t   xdr_authdes_verf(XDR *, struct authdes_verf *);
68 extern int      key_encryptsession_pk(char *, netobj *, des_block *);
69
70 extern bool_t   __rpc_get_time_offset(struct timeval *, nis_server *, char *,
71                                       char **, char **);
72
73
74 /* 
75  * DES authenticator operations vector
76  */
77 static void     authdes_nextverf(AUTH *);
78 static bool_t   authdes_marshal(AUTH *, XDR *);
79 static bool_t   authdes_validate(AUTH *, struct opaque_auth *);
80 static bool_t   authdes_refresh(AUTH *, void *);
81 static void     authdes_destroy(AUTH *);
82
83 static struct auth_ops *authdes_ops(void);
84
85 /*
86  * This struct is pointed to by the ah_private field of an "AUTH *"
87  */
88 struct ad_private {
89         char *ad_fullname;              /* client's full name */
90         u_int ad_fullnamelen;           /* length of name, rounded up */
91         char *ad_servername;            /* server's full name */
92         u_int ad_servernamelen;         /* length of name, rounded up */
93         u_int ad_window;                /* client specified window */
94         bool_t ad_dosync;               /* synchronize? */
95         struct netbuf ad_syncaddr;      /* remote host to synch with */
96         char *ad_timehost;              /* remote host to synch with */
97         struct timeval ad_timediff;     /* server's time - client's time */
98         u_int ad_nickname;              /* server's nickname for client */
99         struct authdes_cred ad_cred;    /* storage for credential */
100         struct authdes_verf ad_verf;    /* storage for verifier */
101         struct timeval ad_timestamp;    /* timestamp sent */
102         des_block ad_xkey;              /* encrypted conversation key */
103         u_char ad_pkey[1024];           /* Server's actual public key */
104         char *ad_netid;                 /* Timehost netid */
105         char *ad_uaddr;                 /* Timehost uaddr */
106         nis_server *ad_nis_srvr;        /* NIS+ server struct */
107 };
108
109 AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *,
110                            const des_block *, nis_server *);
111 /*
112  * documented version of authdes_seccreate
113  */
114 /*
115         servername:     network name of server
116         win:            time to live
117         timehost:       optional hostname to sync with
118         ckey:           optional conversation key to use
119 */
120
121 AUTH *
122 authdes_seccreate(const char *servername, const u_int win,
123                   const char *timehost, const des_block *ckey)
124 {
125         u_char  pkey_data[1024];
126         netobj  pkey;
127         AUTH    *dummy;
128
129         if (! getpublickey(servername, (char *) pkey_data)) {
130                 syslog(LOG_ERR,
131                     "authdes_seccreate: no public key found for %s",
132                     servername);
133                 return (NULL);
134         }
135
136         pkey.n_bytes = (char *) pkey_data;
137         pkey.n_len = (u_int)strlen((char *)pkey_data) + 1;
138         dummy = authdes_pk_seccreate(servername, &pkey, win, timehost,
139             ckey, NULL);
140         return (dummy);
141 }
142
143 /*
144  * Slightly modified version of authdessec_create which takes the public key
145  * of the server principal as an argument. This spares us a call to
146  * getpublickey() which in the nameserver context can cause a deadlock.
147  */
148 AUTH *
149 authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window,
150                      const char *timehost, const des_block *ckey,
151                      nis_server *srvr)
152 {
153         AUTH *auth;
154         struct ad_private *ad;
155         char namebuf[MAXNETNAMELEN+1];
156
157         /*
158          * Allocate everything now
159          */
160         auth = ALLOC(AUTH);
161         if (auth == NULL) {
162                 syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
163                 return (NULL);
164         }
165         ad = ALLOC(struct ad_private);
166         if (ad == NULL) {
167                 syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
168                 goto failed;
169         }
170         ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
171         ad->ad_timehost = NULL;
172         ad->ad_netid = NULL;
173         ad->ad_uaddr = NULL;
174         ad->ad_nis_srvr = NULL;
175         ad->ad_timediff.tv_sec = 0;
176         ad->ad_timediff.tv_usec = 0;
177         memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
178         if (!getnetname(namebuf))
179                 goto failed;
180         ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
181         ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
182         ad->ad_servernamelen = strlen(servername);
183         ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
184
185         if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
186                 syslog(LOG_ERR, "authdes_seccreate: out of memory");
187                 goto failed;
188         }
189         if (timehost != NULL) {
190                 ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
191                 if (ad->ad_timehost == NULL) {
192                         syslog(LOG_ERR, "authdes_seccreate: out of memory");
193                         goto failed;
194                 }
195                 memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
196                 ad->ad_dosync = TRUE;
197         } else if (srvr != NULL) {
198                 ad->ad_nis_srvr = srvr; /* transient */
199                 ad->ad_dosync = TRUE;
200         } else {
201                 ad->ad_dosync = FALSE;
202         }
203         memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
204         memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
205         ad->ad_window = window;
206         if (ckey == NULL) {
207                 if (key_gendes(&auth->ah_key) < 0) {
208                         syslog(LOG_ERR,
209             "authdes_seccreate: keyserv(1m) is unable to generate session key");
210                         goto failed;
211                 }
212         } else {
213                 auth->ah_key = *ckey;
214         }
215
216         /*
217          * Set up auth handle
218          */
219         auth->ah_cred.oa_flavor = AUTH_DES;
220         auth->ah_verf.oa_flavor = AUTH_DES;
221         auth->ah_ops = authdes_ops();
222         auth->ah_private = (caddr_t)ad;
223
224         if (!authdes_refresh(auth, NULL)) {
225                 goto failed;
226         }
227         ad->ad_nis_srvr = NULL; /* not needed any longer */
228         return (auth);
229
230 failed:
231         if (auth)
232                 FREE(auth, sizeof (AUTH));
233         if (ad) {
234                 if (ad->ad_fullname)
235                         FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
236                 if (ad->ad_servername)
237                         FREE(ad->ad_servername, ad->ad_servernamelen + 1);
238                 if (ad->ad_timehost)
239                         FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
240                 if (ad->ad_netid)
241                         FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
242                 if (ad->ad_uaddr)
243                         FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
244                 FREE(ad, sizeof (struct ad_private));
245         }
246         return (NULL);
247 }
248
249 /*
250  * Implement the five authentication operations
251  */
252
253
254 /*
255  * 1. Next Verifier
256  */     
257 /*ARGSUSED*/
258 static void
259 authdes_nextverf(AUTH *auth __unused)
260 {
261         /* what the heck am I supposed to do??? */
262 }
263
264
265 /*
266  * 2. Marshal
267  */
268 static bool_t
269 authdes_marshal(AUTH *auth, XDR *xdrs)
270 {
271 /* LINTED pointer alignment */
272         struct ad_private *ad = AUTH_PRIVATE(auth);
273         struct authdes_cred *cred = &ad->ad_cred;
274         struct authdes_verf *verf = &ad->ad_verf;
275         des_block cryptbuf[2];
276         des_block ivec;
277         int status;
278         int len;
279         rpc_inline_t *ixdr;
280
281         /*
282          * Figure out the "time", accounting for any time difference
283          * with the server if necessary.
284          */
285         gettimeofday(&ad->ad_timestamp, NULL);
286         ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
287         ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
288         while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) {
289                 ad->ad_timestamp.tv_usec -= USEC_PER_SEC;
290                 ad->ad_timestamp.tv_sec++;
291         }
292
293         /*
294          * XDR the timestamp and possibly some other things, then
295          * encrypt them.
296          */
297         ixdr = (rpc_inline_t *)cryptbuf;
298         IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
299         IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
300         if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
301                 IXDR_PUT_U_INT32(ixdr, ad->ad_window);
302                 IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
303                 ivec.key.high = ivec.key.low = 0;       
304                 status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf, 
305                         (u_int) 2 * sizeof (des_block),
306                         DES_ENCRYPT | DES_HW, (char *)&ivec);
307         } else {
308                 status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf, 
309                         (u_int) sizeof (des_block),
310                         DES_ENCRYPT | DES_HW);
311         }
312         if (DES_FAILED(status)) {
313                 syslog(LOG_ERR, "authdes_marshal: DES encryption failure");
314                 return (FALSE);
315         }
316         ad->ad_verf.adv_xtimestamp = cryptbuf[0];
317         if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
318                 ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
319                 ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
320         } else {
321                 ad->ad_cred.adc_nickname = ad->ad_nickname;
322                 ad->ad_verf.adv_winverf = 0;
323         }
324
325         /*
326          * Serialize the credential and verifier into opaque
327          * authentication data.
328          */
329         if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
330                 len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
331         } else {
332                 len = (1 + 1)*BYTES_PER_XDR_UNIT;
333         }
334
335         if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
336                 IXDR_PUT_INT32(ixdr, AUTH_DES);
337                 IXDR_PUT_INT32(ixdr, len);
338         } else {
339                 ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor));
340                 ATTEMPT(xdr_putint32(xdrs, &len));
341         }
342         ATTEMPT(xdr_authdes_cred(xdrs, cred));
343
344         len = (2 + 1)*BYTES_PER_XDR_UNIT; 
345         if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
346                 IXDR_PUT_INT32(ixdr, AUTH_DES);
347                 IXDR_PUT_INT32(ixdr, len);
348         } else {
349                 ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor));
350                 ATTEMPT(xdr_putint32(xdrs, &len));
351         }
352         ATTEMPT(xdr_authdes_verf(xdrs, verf));
353         return (TRUE);
354 }
355
356
357 /*
358  * 3. Validate
359  */
360 static bool_t
361 authdes_validate(AUTH *auth, struct opaque_auth *rverf)
362 {
363 /* LINTED pointer alignment */
364         struct ad_private *ad = AUTH_PRIVATE(auth);
365         struct authdes_verf verf;
366         int status;
367         uint32_t *ixdr;
368         des_block buf;
369
370         if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
371                 return (FALSE);
372         }
373 /* LINTED pointer alignment */
374         ixdr = (uint32_t *)rverf->oa_base;
375         buf.key.high = (uint32_t)*ixdr++;
376         buf.key.low = (uint32_t)*ixdr++;
377         verf.adv_int_u = (uint32_t)*ixdr++;
378
379         /*
380          * Decrypt the timestamp
381          */
382         status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
383                 (u_int)sizeof (des_block), DES_DECRYPT | DES_HW);
384
385         if (DES_FAILED(status)) {
386                 syslog(LOG_ERR, "authdes_validate: DES decryption failure");
387                 return (FALSE);
388         }
389
390         /*
391          * xdr the decrypted timestamp
392          */
393 /* LINTED pointer alignment */
394         ixdr = (uint32_t *)buf.c;
395         verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
396         verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
397
398         /*
399          * validate
400          */
401         if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
402                  sizeof(struct timeval)) != 0) {
403                 syslog(LOG_DEBUG, "authdes_validate: verifier mismatch");
404                 return (FALSE);
405         }
406
407         /*
408          * We have a nickname now, let's use it
409          */
410         ad->ad_nickname = verf.adv_nickname;
411         ad->ad_cred.adc_namekind = ADN_NICKNAME;
412         return (TRUE);
413 }
414
415 /*
416  * 4. Refresh
417  */
418 /*ARGSUSED*/
419 static bool_t
420 authdes_refresh(AUTH *auth, void *dummy __unused)
421 {
422 /* LINTED pointer alignment */
423         struct ad_private *ad = AUTH_PRIVATE(auth);
424         struct authdes_cred *cred = &ad->ad_cred;
425         int             ok;
426         netobj          pkey;
427
428         if (ad->ad_dosync) {
429                 ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr,
430                     ad->ad_timehost, &(ad->ad_uaddr),
431                     &(ad->ad_netid));
432                 if (! ok) {
433                         /*
434                          * Hope the clocks are synced!
435                          */
436                         ad->ad_dosync = 0;
437                         syslog(LOG_DEBUG,
438                             "authdes_refresh: unable to synchronize clock");
439                 }
440         }
441         ad->ad_xkey = auth->ah_key;
442         pkey.n_bytes = (char *)(ad->ad_pkey);
443         pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1;
444         if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
445                 syslog(LOG_INFO,
446                     "authdes_refresh: keyserv(1m) is unable to encrypt session key");
447                 return (FALSE);
448         }
449         cred->adc_fullname.key = ad->ad_xkey;
450         cred->adc_namekind = ADN_FULLNAME;
451         cred->adc_fullname.name = ad->ad_fullname;
452         return (TRUE);
453 }
454
455
456 /*
457  * 5. Destroy
458  */
459 static void
460 authdes_destroy(AUTH *auth)
461 {
462 /* LINTED pointer alignment */
463         struct ad_private *ad = AUTH_PRIVATE(auth);
464
465         FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
466         FREE(ad->ad_servername, ad->ad_servernamelen + 1);
467         if (ad->ad_timehost)
468                 FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
469         if (ad->ad_netid)
470                 FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
471         if (ad->ad_uaddr)
472                 FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
473         FREE(ad, sizeof(struct ad_private));
474         FREE(auth, sizeof(AUTH));
475 }
476
477 static struct auth_ops *
478 authdes_ops(void)
479 {
480         static struct auth_ops ops;
481
482         /* VARIABLES PROTECTED BY ops_lock: ops */
483
484         mutex_lock(&authdes_ops_lock);
485         if (ops.ah_nextverf == NULL) {
486                 ops.ah_nextverf = authdes_nextverf;
487                 ops.ah_marshal = authdes_marshal;
488                 ops.ah_validate = authdes_validate;
489                 ops.ah_refresh = authdes_refresh;
490                 ops.ah_destroy = authdes_destroy;
491         }
492         mutex_unlock(&authdes_ops_lock);
493         return (&ops);
494 }