xref: /freebsd/lib/libc/rpc/auth_des.c (revision 8fb3f3f68288ae2b1b53dd65e3dd673d83c80f4c)
18360efbdSAlfred Perlstein 
2e8636dfdSBill Paul /*
3e8636dfdSBill Paul  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4e8636dfdSBill Paul  * unrestricted use provided that this legend is included on all tape
5e8636dfdSBill Paul  * media and as a part of the software program in whole or part.  Users
6e8636dfdSBill Paul  * may copy or modify Sun RPC without charge, but are not authorized
7e8636dfdSBill Paul  * to license or distribute it to anyone else except as part of a product or
8e8636dfdSBill Paul  * program developed by the user.
9e8636dfdSBill Paul  *
10e8636dfdSBill Paul  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11e8636dfdSBill Paul  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12e8636dfdSBill Paul  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13e8636dfdSBill Paul  *
14e8636dfdSBill Paul  * Sun RPC is provided with no support and without any obligation on the
15e8636dfdSBill Paul  * part of Sun Microsystems, Inc. to assist in its use, correction,
16e8636dfdSBill Paul  * modification or enhancement.
17e8636dfdSBill Paul  *
18e8636dfdSBill Paul  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19e8636dfdSBill Paul  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20e8636dfdSBill Paul  * OR ANY PART THEREOF.
21e8636dfdSBill Paul  *
22e8636dfdSBill Paul  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23e8636dfdSBill Paul  * or profits or other special, indirect and consequential damages, even if
24e8636dfdSBill Paul  * Sun has been advised of the possibility of such damages.
25e8636dfdSBill Paul  *
26e8636dfdSBill Paul  * Sun Microsystems, Inc.
27e8636dfdSBill Paul  * 2550 Garcia Avenue
28e8636dfdSBill Paul  * Mountain View, California  94043
29e8636dfdSBill Paul  */
30e8636dfdSBill Paul /*
31e8636dfdSBill Paul  * Copyright (c) 1988 by Sun Microsystems, Inc.
32e8636dfdSBill Paul  */
33e8636dfdSBill Paul /*
34e8636dfdSBill Paul  * auth_des.c, client-side implementation of DES authentication
35e8636dfdSBill Paul  */
368360efbdSAlfred Perlstein #include "namespace.h"
379f5afc13SIan Dowse #include "reentrant.h"
388360efbdSAlfred Perlstein #include <err.h>
398360efbdSAlfred Perlstein #include <errno.h>
40e8636dfdSBill Paul #include <string.h>
41e8636dfdSBill Paul #include <stdlib.h>
42e8636dfdSBill Paul #include <unistd.h>
43e8636dfdSBill Paul #include <sys/cdefs.h>
44e8636dfdSBill Paul #include <rpc/des_crypt.h>
458360efbdSAlfred Perlstein #include <syslog.h>
46e8636dfdSBill Paul #include <rpc/types.h>
47e8636dfdSBill Paul #include <rpc/auth.h>
48e8636dfdSBill Paul #include <rpc/auth_des.h>
498360efbdSAlfred Perlstein #include <rpc/clnt.h>
508360efbdSAlfred Perlstein #include <rpc/xdr.h>
51e8636dfdSBill Paul #include <sys/socket.h>
52e8636dfdSBill Paul #undef NIS
53e8636dfdSBill Paul #include <rpcsvc/nis.h>
548360efbdSAlfred Perlstein #include "un-namespace.h"
55e8636dfdSBill Paul 
56e8636dfdSBill Paul #if defined(LIBC_SCCS) && !defined(lint)
57e8636dfdSBill Paul /* from: static char sccsid[] = 	"@(#)auth_des.c	2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI"; */
587f3dea24SPeter Wemm static const char rcsid[] = "$FreeBSD$";
59e8636dfdSBill Paul #endif
60e8636dfdSBill Paul 
618360efbdSAlfred Perlstein #define USEC_PER_SEC		1000000
62e8636dfdSBill Paul #define RTIME_TIMEOUT		5	/* seconds to wait for sync */
63e8636dfdSBill Paul 
64e8636dfdSBill Paul #define AUTH_PRIVATE(auth)	(struct ad_private *) auth->ah_private
65e8636dfdSBill Paul #define ALLOC(object_type)	(object_type *) mem_alloc(sizeof(object_type))
66e8636dfdSBill Paul #define FREE(ptr, size)		mem_free((char *)(ptr), (int) size)
67e8636dfdSBill Paul #define ATTEMPT(xdr_op)		if (!(xdr_op)) return (FALSE)
68e8636dfdSBill Paul 
698360efbdSAlfred Perlstein extern bool_t xdr_authdes_cred( XDR *, struct authdes_cred *);
708360efbdSAlfred Perlstein extern bool_t xdr_authdes_verf( XDR *, struct authdes_verf *);
718360efbdSAlfred Perlstein extern int key_encryptsession_pk();
728360efbdSAlfred Perlstein 
738360efbdSAlfred Perlstein extern bool_t __rpc_get_time_offset(struct timeval *, nis_server *, char *,
748360efbdSAlfred Perlstein 	char **, char **);
75e8636dfdSBill Paul 
76e8636dfdSBill Paul /*
77e8636dfdSBill Paul  * DES authenticator operations vector
78e8636dfdSBill Paul  */
798360efbdSAlfred Perlstein static void	authdes_nextverf(AUTH *);
808360efbdSAlfred Perlstein static bool_t	authdes_marshal(AUTH *, XDR *);
818360efbdSAlfred Perlstein static bool_t	authdes_validate(AUTH *, struct opaque_auth *);
828360efbdSAlfred Perlstein static bool_t	authdes_refresh(AUTH *, void *);
838360efbdSAlfred Perlstein static void	authdes_destroy(AUTH *);
848360efbdSAlfred Perlstein 
858360efbdSAlfred Perlstein static struct auth_ops *authdes_ops(void);
868360efbdSAlfred Perlstein 
87e8636dfdSBill Paul /*
88e8636dfdSBill Paul  * This struct is pointed to by the ah_private field of an "AUTH *"
89e8636dfdSBill Paul  */
90e8636dfdSBill Paul struct ad_private {
91e8636dfdSBill Paul 	char *ad_fullname; 		/* client's full name */
92e8636dfdSBill Paul 	u_int ad_fullnamelen;		/* length of name, rounded up */
93e8636dfdSBill Paul 	char *ad_servername; 		/* server's full name */
94e8636dfdSBill Paul 	u_int ad_servernamelen;		/* length of name, rounded up */
95e8636dfdSBill Paul 	u_int ad_window;	  	/* client specified window */
96e8636dfdSBill Paul 	bool_t ad_dosync;		/* synchronize? */
978360efbdSAlfred Perlstein 	struct netbuf ad_syncaddr;	/* remote host to synch with */
98e8636dfdSBill Paul 	char *ad_timehost;		/* remote host to synch with */
99e8636dfdSBill Paul 	struct timeval ad_timediff;	/* server's time - client's time */
1008360efbdSAlfred Perlstein 	u_int ad_nickname;		/* server's nickname for client */
101e8636dfdSBill Paul 	struct authdes_cred ad_cred;	/* storage for credential */
102e8636dfdSBill Paul 	struct authdes_verf ad_verf;	/* storage for verifier */
103e8636dfdSBill Paul 	struct timeval ad_timestamp;	/* timestamp sent */
104e8636dfdSBill Paul 	des_block ad_xkey;		/* encrypted conversation key */
105e8636dfdSBill Paul 	u_char ad_pkey[1024];		/* Server's actual public key */
106e8636dfdSBill Paul 	char *ad_netid;			/* Timehost netid */
107e8636dfdSBill Paul 	char *ad_uaddr;			/* Timehost uaddr */
108e8636dfdSBill Paul 	nis_server *ad_nis_srvr;	/* NIS+ server struct */
109e8636dfdSBill Paul };
110e8636dfdSBill Paul 
1118360efbdSAlfred Perlstein AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *,
1128360efbdSAlfred Perlstein 	const des_block *, nis_server *);
113e8636dfdSBill Paul 
114e8636dfdSBill Paul /*
1158360efbdSAlfred Perlstein  * documented version of authdes_seccreate
116e8636dfdSBill Paul  */
1178360efbdSAlfred Perlstein /*
1188360efbdSAlfred Perlstein 	servername:	network name of server
1198360efbdSAlfred Perlstein 	win:		time to live
1208360efbdSAlfred Perlstein 	timehost:	optional hostname to sync with
1218360efbdSAlfred Perlstein 	ckey:		optional conversation key to use
1228360efbdSAlfred Perlstein */
1238360efbdSAlfred Perlstein 
124e8636dfdSBill Paul AUTH *
1258360efbdSAlfred Perlstein authdes_seccreate(const char *servername, const u_int win,
1268360efbdSAlfred Perlstein 	const char *timehost, const des_block *ckey)
127e8636dfdSBill Paul {
128e8636dfdSBill Paul 	u_char  pkey_data[1024];
1298360efbdSAlfred Perlstein 	netobj  pkey;
1308360efbdSAlfred Perlstein 	AUTH    *dummy;
131e8636dfdSBill Paul 
1328360efbdSAlfred Perlstein 	if (! getpublickey(servername, (char *) pkey_data)) {
1338360efbdSAlfred Perlstein 		syslog(LOG_ERR,
1348360efbdSAlfred Perlstein 		    "authdes_seccreate: no public key found for %s",
1358360efbdSAlfred Perlstein 		    servername);
136e8636dfdSBill Paul 		return (NULL);
137e8636dfdSBill Paul 	}
138e8636dfdSBill Paul 
1398360efbdSAlfred Perlstein 	pkey.n_bytes = (char *) pkey_data;
1408360efbdSAlfred Perlstein 	pkey.n_len = (u_int)strlen((char *)pkey_data) + 1;
1418360efbdSAlfred Perlstein 	dummy = authdes_pk_seccreate(servername, &pkey, win, timehost,
1428360efbdSAlfred Perlstein 	    ckey, NULL);
1438360efbdSAlfred Perlstein 	return (dummy);
1448360efbdSAlfred Perlstein }
1458360efbdSAlfred Perlstein 
146e8636dfdSBill Paul /*
1478360efbdSAlfred Perlstein  * Slightly modified version of authdessec_create which takes the public key
148e8636dfdSBill Paul  * of the server principal as an argument. This spares us a call to
149e8636dfdSBill Paul  * getpublickey() which in the nameserver context can cause a deadlock.
150e8636dfdSBill Paul  */
151e8636dfdSBill Paul AUTH *
1528360efbdSAlfred Perlstein authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window,
1538360efbdSAlfred Perlstein 	const char *timehost, const des_block *ckey, nis_server *srvr)
154e8636dfdSBill Paul {
155e8636dfdSBill Paul 	AUTH *auth;
156e8636dfdSBill Paul 	struct ad_private *ad;
157e8636dfdSBill Paul 	char namebuf[MAXNETNAMELEN+1];
158e8636dfdSBill Paul 
159e8636dfdSBill Paul 	/*
160e8636dfdSBill Paul 	 * Allocate everything now
161e8636dfdSBill Paul 	 */
162e8636dfdSBill Paul 	auth = ALLOC(AUTH);
163e8636dfdSBill Paul 	if (auth == NULL) {
1648360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
165e8636dfdSBill Paul 		return (NULL);
166e8636dfdSBill Paul 	}
167e8636dfdSBill Paul 	ad = ALLOC(struct ad_private);
168e8636dfdSBill Paul 	if (ad == NULL) {
1698360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
170e8636dfdSBill Paul 		goto failed;
171e8636dfdSBill Paul 	}
172e8636dfdSBill Paul 	ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
173e8636dfdSBill Paul 	ad->ad_timehost = NULL;
174e8636dfdSBill Paul 	ad->ad_netid = NULL;
175e8636dfdSBill Paul 	ad->ad_uaddr = NULL;
176e8636dfdSBill Paul 	ad->ad_nis_srvr = NULL;
177e8636dfdSBill Paul 	ad->ad_timediff.tv_sec = 0;
178e8636dfdSBill Paul 	ad->ad_timediff.tv_usec = 0;
179e8636dfdSBill Paul 	memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
180e8636dfdSBill Paul 	if (!getnetname(namebuf))
181e8636dfdSBill Paul 		goto failed;
182e8636dfdSBill Paul 	ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
183e8636dfdSBill Paul 	ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
184e8636dfdSBill Paul 	ad->ad_servernamelen = strlen(servername);
185e8636dfdSBill Paul 	ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
186e8636dfdSBill Paul 
187e8636dfdSBill Paul 	if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
1888360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_seccreate: out of memory");
189e8636dfdSBill Paul 		goto failed;
190e8636dfdSBill Paul 	}
191e8636dfdSBill Paul 	if (timehost != NULL) {
192e8636dfdSBill Paul 		ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
193e8636dfdSBill Paul 		if (ad->ad_timehost == NULL) {
1948360efbdSAlfred Perlstein 			syslog(LOG_ERR, "authdes_seccreate: out of memory");
195e8636dfdSBill Paul 			goto failed;
196e8636dfdSBill Paul 		}
197e8636dfdSBill Paul 		memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
198e8636dfdSBill Paul 		ad->ad_dosync = TRUE;
199e8636dfdSBill Paul 	} else if (srvr != NULL) {
200e8636dfdSBill Paul 		ad->ad_nis_srvr = srvr;	/* transient */
201e8636dfdSBill Paul 		ad->ad_dosync = TRUE;
202e8636dfdSBill Paul 	} else {
203e8636dfdSBill Paul 		ad->ad_dosync = FALSE;
204e8636dfdSBill Paul 	}
205e8636dfdSBill Paul 	memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
206e8636dfdSBill Paul 	memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
207e8636dfdSBill Paul 	ad->ad_window = window;
208e8636dfdSBill Paul 	if (ckey == NULL) {
209e8636dfdSBill Paul 		if (key_gendes(&auth->ah_key) < 0) {
2108360efbdSAlfred Perlstein 			syslog(LOG_ERR,
2118360efbdSAlfred Perlstein 	    "authdes_seccreate: keyserv(1m) is unable to generate session key");
212e8636dfdSBill Paul 			goto failed;
213e8636dfdSBill Paul 		}
214e8636dfdSBill Paul 	} else {
215e8636dfdSBill Paul 		auth->ah_key = *ckey;
216e8636dfdSBill Paul 	}
217e8636dfdSBill Paul 
218e8636dfdSBill Paul 	/*
219e8636dfdSBill Paul 	 * Set up auth handle
220e8636dfdSBill Paul 	 */
221e8636dfdSBill Paul 	auth->ah_cred.oa_flavor = AUTH_DES;
222e8636dfdSBill Paul 	auth->ah_verf.oa_flavor = AUTH_DES;
2238360efbdSAlfred Perlstein 	auth->ah_ops = authdes_ops();
224e8636dfdSBill Paul 	auth->ah_private = (caddr_t)ad;
225e8636dfdSBill Paul 
2268360efbdSAlfred Perlstein 	if (!authdes_refresh(auth, NULL)) {
227e8636dfdSBill Paul 		goto failed;
228e8636dfdSBill Paul 	}
229e8636dfdSBill Paul 	ad->ad_nis_srvr = NULL; /* not needed any longer */
230e8636dfdSBill Paul 	return (auth);
231e8636dfdSBill Paul 
232e8636dfdSBill Paul failed:
233e8636dfdSBill Paul 	if (auth)
234e8636dfdSBill Paul 		FREE(auth, sizeof (AUTH));
235e8636dfdSBill Paul 	if (ad) {
236e8636dfdSBill Paul 		if (ad->ad_fullname)
237e8636dfdSBill Paul 			FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
238e8636dfdSBill Paul 		if (ad->ad_servername)
239e8636dfdSBill Paul 			FREE(ad->ad_servername, ad->ad_servernamelen + 1);
240e8636dfdSBill Paul 		if (ad->ad_timehost)
241e8636dfdSBill Paul 			FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
242e8636dfdSBill Paul 		if (ad->ad_netid)
2438360efbdSAlfred Perlstein 			FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
244e8636dfdSBill Paul 		if (ad->ad_uaddr)
2458360efbdSAlfred Perlstein 			FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
246e8636dfdSBill Paul 		FREE(ad, sizeof (struct ad_private));
247e8636dfdSBill Paul 	}
248e8636dfdSBill Paul 	return (NULL);
249e8636dfdSBill Paul }
2508360efbdSAlfred Perlstein 
251e8636dfdSBill Paul /*
252e8636dfdSBill Paul  * Implement the five authentication operations
253e8636dfdSBill Paul  */
254e8636dfdSBill Paul 
255e8636dfdSBill Paul 
256e8636dfdSBill Paul /*
257e8636dfdSBill Paul  * 1. Next Verifier
258e8636dfdSBill Paul  */
259e8636dfdSBill Paul /*ARGSUSED*/
260e8636dfdSBill Paul static void
2618360efbdSAlfred Perlstein authdes_nextverf(AUTH *auth)
262e8636dfdSBill Paul {
263e8636dfdSBill Paul 	/* what the heck am I supposed to do??? */
264e8636dfdSBill Paul }
265e8636dfdSBill Paul 
266e8636dfdSBill Paul 
267e8636dfdSBill Paul /*
268e8636dfdSBill Paul  * 2. Marshal
269e8636dfdSBill Paul  */
270e8636dfdSBill Paul static bool_t
2718360efbdSAlfred Perlstein authdes_marshal(AUTH *auth, XDR *xdrs)
272e8636dfdSBill Paul {
2738360efbdSAlfred Perlstein /* LINTED pointer alignment */
274e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
275e8636dfdSBill Paul 	struct authdes_cred *cred = &ad->ad_cred;
276e8636dfdSBill Paul 	struct authdes_verf *verf = &ad->ad_verf;
277e8636dfdSBill Paul 	des_block cryptbuf[2];
278e8636dfdSBill Paul 	des_block ivec;
279e8636dfdSBill Paul 	int status;
2808360efbdSAlfred Perlstein 	int len;
2818fb3f3f6SDavid E. O'Brien 	rpc_inline_t *ixdr;
282e8636dfdSBill Paul 
283e8636dfdSBill Paul 	/*
284e8636dfdSBill Paul 	 * Figure out the "time", accounting for any time difference
285e8636dfdSBill Paul 	 * with the server if necessary.
286e8636dfdSBill Paul 	 */
287e8636dfdSBill Paul 	(void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL);
288e8636dfdSBill Paul 	ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
289e8636dfdSBill Paul 	ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
2908360efbdSAlfred Perlstein 	while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) {
2918360efbdSAlfred Perlstein 		ad->ad_timestamp.tv_usec -= USEC_PER_SEC;
2928360efbdSAlfred Perlstein 		ad->ad_timestamp.tv_sec++;
293e8636dfdSBill Paul 	}
294e8636dfdSBill Paul 
295e8636dfdSBill Paul 	/*
296e8636dfdSBill Paul 	 * XDR the timestamp and possibly some other things, then
297e8636dfdSBill Paul 	 * encrypt them.
298e8636dfdSBill Paul 	 */
2998360efbdSAlfred Perlstein 	ixdr = (rpc_inline_t *)cryptbuf;
3008360efbdSAlfred Perlstein 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
3018360efbdSAlfred Perlstein 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
302e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
3038360efbdSAlfred Perlstein 		IXDR_PUT_U_INT32(ixdr, ad->ad_window);
3048360efbdSAlfred Perlstein 		IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
305e8636dfdSBill Paul 		ivec.key.high = ivec.key.low = 0;
306e8636dfdSBill Paul 		status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
3078360efbdSAlfred Perlstein 			(u_int) 2 * sizeof (des_block),
3088360efbdSAlfred Perlstein 			DES_ENCRYPT | DES_HW, (char *)&ivec);
309e8636dfdSBill Paul 	} else {
310e8636dfdSBill Paul 		status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
3118360efbdSAlfred Perlstein 			(u_int) sizeof (des_block),
3128360efbdSAlfred Perlstein 			DES_ENCRYPT | DES_HW);
313e8636dfdSBill Paul 	}
314e8636dfdSBill Paul 	if (DES_FAILED(status)) {
3158360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_marshal: DES encryption failure");
316e8636dfdSBill Paul 		return (FALSE);
317e8636dfdSBill Paul 	}
318e8636dfdSBill Paul 	ad->ad_verf.adv_xtimestamp = cryptbuf[0];
319e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
320e8636dfdSBill Paul 		ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
321e8636dfdSBill Paul 		ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
322e8636dfdSBill Paul 	} else {
323e8636dfdSBill Paul 		ad->ad_cred.adc_nickname = ad->ad_nickname;
324e8636dfdSBill Paul 		ad->ad_verf.adv_winverf = 0;
325e8636dfdSBill Paul 	}
326e8636dfdSBill Paul 
327e8636dfdSBill Paul 	/*
328e8636dfdSBill Paul 	 * Serialize the credential and verifier into opaque
329e8636dfdSBill Paul 	 * authentication data.
330e8636dfdSBill Paul 	 */
331e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
332e8636dfdSBill Paul 		len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
333e8636dfdSBill Paul 	} else {
334e8636dfdSBill Paul 		len = (1 + 1)*BYTES_PER_XDR_UNIT;
335e8636dfdSBill Paul 	}
336e8636dfdSBill Paul 
337e8636dfdSBill Paul 	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
3388360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, AUTH_DES);
3398360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, len);
340e8636dfdSBill Paul 	} else {
3418360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor));
3428360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, &len));
343e8636dfdSBill Paul 	}
344e8636dfdSBill Paul 	ATTEMPT(xdr_authdes_cred(xdrs, cred));
345e8636dfdSBill Paul 
346e8636dfdSBill Paul 	len = (2 + 1)*BYTES_PER_XDR_UNIT;
347e8636dfdSBill Paul 	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
3488360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, AUTH_DES);
3498360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, len);
350e8636dfdSBill Paul 	} else {
3518360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor));
3528360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, &len));
353e8636dfdSBill Paul 	}
354e8636dfdSBill Paul 	ATTEMPT(xdr_authdes_verf(xdrs, verf));
355e8636dfdSBill Paul 	return (TRUE);
356e8636dfdSBill Paul }
357e8636dfdSBill Paul 
358e8636dfdSBill Paul 
359e8636dfdSBill Paul /*
360e8636dfdSBill Paul  * 3. Validate
361e8636dfdSBill Paul  */
362e8636dfdSBill Paul static bool_t
3638360efbdSAlfred Perlstein authdes_validate(AUTH *auth, struct opaque_auth *rverf)
364e8636dfdSBill Paul {
3658360efbdSAlfred Perlstein /* LINTED pointer alignment */
366e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
367e8636dfdSBill Paul 	struct authdes_verf verf;
368e8636dfdSBill Paul 	int status;
3698fb3f3f6SDavid E. O'Brien 	uint32_t *ixdr;
3708360efbdSAlfred Perlstein 	des_block buf;
371e8636dfdSBill Paul 
372e8636dfdSBill Paul 	if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
373e8636dfdSBill Paul 		return (FALSE);
374e8636dfdSBill Paul 	}
3758360efbdSAlfred Perlstein /* LINTED pointer alignment */
3768360efbdSAlfred Perlstein 	ixdr = (uint32_t *)rverf->oa_base;
3778360efbdSAlfred Perlstein 	buf.key.high = (uint32_t)*ixdr++;
3788360efbdSAlfred Perlstein 	buf.key.low = (uint32_t)*ixdr++;
3798360efbdSAlfred Perlstein 	verf.adv_int_u = (uint32_t)*ixdr++;
380e8636dfdSBill Paul 
381e8636dfdSBill Paul 	/*
382e8636dfdSBill Paul 	 * Decrypt the timestamp
383e8636dfdSBill Paul 	 */
3848360efbdSAlfred Perlstein 	status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
3858360efbdSAlfred Perlstein 		(u_int)sizeof (des_block), DES_DECRYPT | DES_HW);
386e8636dfdSBill Paul 
387e8636dfdSBill Paul 	if (DES_FAILED(status)) {
3888360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_validate: DES decryption failure");
389e8636dfdSBill Paul 		return (FALSE);
390e8636dfdSBill Paul 	}
391e8636dfdSBill Paul 
392e8636dfdSBill Paul 	/*
393e8636dfdSBill Paul 	 * xdr the decrypted timestamp
394e8636dfdSBill Paul 	 */
3958360efbdSAlfred Perlstein /* LINTED pointer alignment */
3968360efbdSAlfred Perlstein 	ixdr = (uint32_t *)buf.c;
3978360efbdSAlfred Perlstein 	verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
3988360efbdSAlfred Perlstein 	verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
399e8636dfdSBill Paul 
400e8636dfdSBill Paul 	/*
401e8636dfdSBill Paul 	 * validate
402e8636dfdSBill Paul 	 */
403e8636dfdSBill Paul 	if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
404e8636dfdSBill Paul 		 sizeof(struct timeval)) != 0) {
4058360efbdSAlfred Perlstein 		syslog(LOG_DEBUG, "authdes_validate: verifier mismatch");
406e8636dfdSBill Paul 		return (FALSE);
407e8636dfdSBill Paul 	}
408e8636dfdSBill Paul 
409e8636dfdSBill Paul 	/*
410e8636dfdSBill Paul 	 * We have a nickname now, let's use it
411e8636dfdSBill Paul 	 */
412e8636dfdSBill Paul 	ad->ad_nickname = verf.adv_nickname;
413e8636dfdSBill Paul 	ad->ad_cred.adc_namekind = ADN_NICKNAME;
414e8636dfdSBill Paul 	return (TRUE);
415e8636dfdSBill Paul }
416e8636dfdSBill Paul 
417e8636dfdSBill Paul /*
418e8636dfdSBill Paul  * 4. Refresh
419e8636dfdSBill Paul  */
4208360efbdSAlfred Perlstein /*ARGSUSED*/
421e8636dfdSBill Paul static bool_t
4228360efbdSAlfred Perlstein authdes_refresh(AUTH *auth, void *dummy)
423e8636dfdSBill Paul {
4248360efbdSAlfred Perlstein /* LINTED pointer alignment */
425e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
426e8636dfdSBill Paul 	struct authdes_cred *cred = &ad->ad_cred;
4278360efbdSAlfred Perlstein 	int		ok;
428e8636dfdSBill Paul 	netobj		pkey;
429e8636dfdSBill Paul 
4308360efbdSAlfred Perlstein 	if (ad->ad_dosync) {
4318360efbdSAlfred Perlstein                 ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr,
432e8636dfdSBill Paul 		    ad->ad_timehost, &(ad->ad_uaddr),
4338360efbdSAlfred Perlstein 		    &(ad->ad_netid));
4348360efbdSAlfred Perlstein 		if (! ok) {
435e8636dfdSBill Paul 			/*
436e8636dfdSBill Paul 			 * Hope the clocks are synced!
437e8636dfdSBill Paul 			 */
438e8636dfdSBill Paul 			ad->ad_dosync = 0;
4398360efbdSAlfred Perlstein 			syslog(LOG_DEBUG,
4408360efbdSAlfred Perlstein 			    "authdes_refresh: unable to synchronize clock");
4418360efbdSAlfred Perlstein 		 }
442e8636dfdSBill Paul 	}
443e8636dfdSBill Paul 	ad->ad_xkey = auth->ah_key;
444e8636dfdSBill Paul 	pkey.n_bytes = (char *)(ad->ad_pkey);
4458360efbdSAlfred Perlstein 	pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1;
446e8636dfdSBill Paul 	if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
4478360efbdSAlfred Perlstein 		syslog(LOG_INFO,
4488360efbdSAlfred Perlstein 		    "authdes_refresh: keyserv(1m) is unable to encrypt session key");
449e8636dfdSBill Paul 		return (FALSE);
450e8636dfdSBill Paul 	}
451e8636dfdSBill Paul 	cred->adc_fullname.key = ad->ad_xkey;
452e8636dfdSBill Paul 	cred->adc_namekind = ADN_FULLNAME;
453e8636dfdSBill Paul 	cred->adc_fullname.name = ad->ad_fullname;
454e8636dfdSBill Paul 	return (TRUE);
455e8636dfdSBill Paul }
456e8636dfdSBill Paul 
457e8636dfdSBill Paul 
458e8636dfdSBill Paul /*
459e8636dfdSBill Paul  * 5. Destroy
460e8636dfdSBill Paul  */
461e8636dfdSBill Paul static void
4628360efbdSAlfred Perlstein authdes_destroy(AUTH *auth)
463e8636dfdSBill Paul {
4648360efbdSAlfred Perlstein /* LINTED pointer alignment */
465e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
466e8636dfdSBill Paul 
467e8636dfdSBill Paul 	FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
468e8636dfdSBill Paul 	FREE(ad->ad_servername, ad->ad_servernamelen + 1);
4698360efbdSAlfred Perlstein 	if (ad->ad_timehost)
4708360efbdSAlfred Perlstein 		FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
4718360efbdSAlfred Perlstein 	if (ad->ad_netid)
4728360efbdSAlfred Perlstein 		FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
4738360efbdSAlfred Perlstein 	if (ad->ad_uaddr)
4748360efbdSAlfred Perlstein 		FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
475e8636dfdSBill Paul 	FREE(ad, sizeof (struct ad_private));
476e8636dfdSBill Paul 	FREE(auth, sizeof(AUTH));
477e8636dfdSBill Paul }
478e8636dfdSBill Paul 
4798360efbdSAlfred Perlstein static struct auth_ops *
4808360efbdSAlfred Perlstein authdes_ops(void)
481e8636dfdSBill Paul {
4828360efbdSAlfred Perlstein 	static struct auth_ops ops;
4838360efbdSAlfred Perlstein 	extern mutex_t authdes_ops_lock;
484e8636dfdSBill Paul 
4858360efbdSAlfred Perlstein 	/* VARIABLES PROTECTED BY ops_lock: ops */
4868360efbdSAlfred Perlstein 
4878360efbdSAlfred Perlstein 	mutex_lock(&authdes_ops_lock);
4888360efbdSAlfred Perlstein 	if (ops.ah_nextverf == NULL) {
4898360efbdSAlfred Perlstein 		ops.ah_nextverf = authdes_nextverf;
4908360efbdSAlfred Perlstein 		ops.ah_marshal = authdes_marshal;
4918360efbdSAlfred Perlstein 		ops.ah_validate = authdes_validate;
4928360efbdSAlfred Perlstein 		ops.ah_refresh = authdes_refresh;
4938360efbdSAlfred Perlstein 		ops.ah_destroy = authdes_destroy;
494e8636dfdSBill Paul         }
4958360efbdSAlfred Perlstein 	mutex_unlock(&authdes_ops_lock);
4968360efbdSAlfred Perlstein 	return (&ops);
497e8636dfdSBill Paul }
498