xref: /onnv-gate/usr/src/cmd/krb5/slave/kprop.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * slave/kprop.c
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
12*0Sstevel@tonic-gate  * All Rights Reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Export of this software from the United States of America may
15*0Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*0Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*0Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*0Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*0Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*0Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*0Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*0Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*0Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*0Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
27*0Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
28*0Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
29*0Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
30*0Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
31*0Sstevel@tonic-gate  * or implied warranty.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <ctype.h>
41*0Sstevel@tonic-gate #include <sys/file.h>
42*0Sstevel@tonic-gate #include <signal.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <sys/types.h>
45*0Sstevel@tonic-gate #include <sys/time.h>
46*0Sstevel@tonic-gate #include <sys/stat.h>
47*0Sstevel@tonic-gate #include <sys/socket.h>
48*0Sstevel@tonic-gate #include <netinet/in.h>
49*0Sstevel@tonic-gate #include <sys/param.h>
50*0Sstevel@tonic-gate #include <netdb.h>
51*0Sstevel@tonic-gate #include <fcntl.h>
52*0Sstevel@tonic-gate #include <libintl.h>
53*0Sstevel@tonic-gate #include <locale.h>
54*0Sstevel@tonic-gate #include <k5-int.h>
55*0Sstevel@tonic-gate #include "com_err.h"
56*0Sstevel@tonic-gate #include "kprop.h"
57*0Sstevel@tonic-gate static char *kprop_version = KPROP_PROT_VERSION;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate char	*progname = 0;
60*0Sstevel@tonic-gate int     debug = 0;
61*0Sstevel@tonic-gate char	*srvtab = 0;
62*0Sstevel@tonic-gate char	*slave_host;
63*0Sstevel@tonic-gate char	*realm = 0;
64*0Sstevel@tonic-gate char	*file = KPROP_DEFAULT_FILE;
65*0Sstevel@tonic-gate short	port = 0;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate krb5_principal	my_principal;		/* The Kerberos principal we'll be */
68*0Sstevel@tonic-gate 				/* running under, initialized in */
69*0Sstevel@tonic-gate 				/* get_tickets() */
70*0Sstevel@tonic-gate krb5_ccache	ccache;		/* Credentials cache which we'll be using */
71*0Sstevel@tonic-gate /* krb5_creds 	my_creds;	/* My credentials */
72*0Sstevel@tonic-gate krb5_creds	creds;
73*0Sstevel@tonic-gate krb5_address	sender_addr;
74*0Sstevel@tonic-gate krb5_address	receiver_addr;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate void	PRS
77*0Sstevel@tonic-gate 	 (int, char **);
78*0Sstevel@tonic-gate void	get_tickets
79*0Sstevel@tonic-gate 	 (krb5_context);
80*0Sstevel@tonic-gate static void usage
81*0Sstevel@tonic-gate 	 (void);
82*0Sstevel@tonic-gate krb5_error_code open_connection
83*0Sstevel@tonic-gate 	(char *, int *, char *, int);
84*0Sstevel@tonic-gate void	kerberos_authenticate
85*0Sstevel@tonic-gate 	 (krb5_context, krb5_auth_context *,
86*0Sstevel@tonic-gate 		   int, krb5_principal, krb5_creds **);
87*0Sstevel@tonic-gate int	open_database
88*0Sstevel@tonic-gate 	 (krb5_context, char *, int *);
89*0Sstevel@tonic-gate void	close_database
90*0Sstevel@tonic-gate 	 (krb5_context, int);
91*0Sstevel@tonic-gate void	xmit_database
92*0Sstevel@tonic-gate 	 (krb5_context, krb5_auth_context, krb5_creds *,
93*0Sstevel@tonic-gate 		   int, int, int);
94*0Sstevel@tonic-gate void	send_error
95*0Sstevel@tonic-gate 	 (krb5_context, krb5_creds *, int, char *, krb5_error_code);
96*0Sstevel@tonic-gate void	update_last_prop_file
97*0Sstevel@tonic-gate 	 (char *, char *);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static void usage()
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	fprintf(stderr,
102*0Sstevel@tonic-gate 		gettext
103*0Sstevel@tonic-gate 		("\nUsage: %s [-r realm] [-f file] [-d] [-P port] [-s srvtab] slave_host\n\n"),
104*0Sstevel@tonic-gate 		progname);
105*0Sstevel@tonic-gate 	exit(1);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate int
109*0Sstevel@tonic-gate main(argc, argv)
110*0Sstevel@tonic-gate 	int	argc;
111*0Sstevel@tonic-gate 	char	**argv;
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	int	fd, database_fd, database_size;
114*0Sstevel@tonic-gate 	krb5_error_code	retval;
115*0Sstevel@tonic-gate 	krb5_context context;
116*0Sstevel@tonic-gate 	krb5_creds *my_creds;
117*0Sstevel@tonic-gate 	krb5_auth_context auth_context;
118*0Sstevel@tonic-gate #define	ERRMSGSIZ	256
119*0Sstevel@tonic-gate 	char	Errmsg[ERRMSGSIZ];
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
124*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"KPROP_TEST"	/* Use this only if it weren't */
125*0Sstevel@tonic-gate #endif
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	retval = krb5_init_context(&context);
130*0Sstevel@tonic-gate 	if (retval) {
131*0Sstevel@tonic-gate 		com_err(argv[0], retval, gettext("while initializing krb5"));
132*0Sstevel@tonic-gate 		exit(1);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	PRS(argc, argv);
135*0Sstevel@tonic-gate 	get_tickets(context);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	database_fd = open_database(context, file, &database_size);
138*0Sstevel@tonic-gate 	if (retval = open_connection(slave_host, &fd, Errmsg, sizeof(Errmsg))) {
139*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("%s while opening connection to %s"),
140*0Sstevel@tonic-gate 			Errmsg, slave_host);
141*0Sstevel@tonic-gate 		exit(1);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 	if (fd < 0) {
144*0Sstevel@tonic-gate 		fprintf(stderr,
145*0Sstevel@tonic-gate 			gettext("%s: %s while opening connection to %s\n"),
146*0Sstevel@tonic-gate 			progname, Errmsg, slave_host);
147*0Sstevel@tonic-gate 		exit(1);
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 	kerberos_authenticate(context, &auth_context, fd, my_principal,
150*0Sstevel@tonic-gate 			      &my_creds);
151*0Sstevel@tonic-gate 	xmit_database(context, auth_context, my_creds, fd, database_fd,
152*0Sstevel@tonic-gate 		      database_size);
153*0Sstevel@tonic-gate 	update_last_prop_file(slave_host, file);
154*0Sstevel@tonic-gate 	printf(gettext("Database propagation to %s: SUCCEEDED\n"), slave_host);
155*0Sstevel@tonic-gate 	krb5_free_cred_contents(context, my_creds);
156*0Sstevel@tonic-gate 	close_database(context, database_fd);
157*0Sstevel@tonic-gate 	exit(0);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate void PRS(argc, argv)
160*0Sstevel@tonic-gate 	int	argc;
161*0Sstevel@tonic-gate 	char	**argv;
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	int c;
164*0Sstevel@tonic-gate 	register char	*word, ch;
165*0Sstevel@tonic-gate 	extern int optind;
166*0Sstevel@tonic-gate 	extern char *optarg;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	progname = argv[0];
169*0Sstevel@tonic-gate 	while ((c= getopt(argc, argv, "r:f:dP:s:h:")) != EOF) {
170*0Sstevel@tonic-gate 		switch (c) {
171*0Sstevel@tonic-gate 				case 'r':
172*0Sstevel@tonic-gate 			realm = optarg;
173*0Sstevel@tonic-gate 					if (!realm)
174*0Sstevel@tonic-gate 						usage();
175*0Sstevel@tonic-gate 					break;
176*0Sstevel@tonic-gate 				case 'f':
177*0Sstevel@tonic-gate 			file = optarg;
178*0Sstevel@tonic-gate 					if (!file)
179*0Sstevel@tonic-gate 						usage();
180*0Sstevel@tonic-gate 					break;
181*0Sstevel@tonic-gate 				case 'd':
182*0Sstevel@tonic-gate 					debug++;
183*0Sstevel@tonic-gate 					break;
184*0Sstevel@tonic-gate 				case 'P':
185*0Sstevel@tonic-gate 			port = htons(atoi(optarg));
186*0Sstevel@tonic-gate 					if (!port)
187*0Sstevel@tonic-gate 						usage();
188*0Sstevel@tonic-gate 					break;
189*0Sstevel@tonic-gate 				case 's':
190*0Sstevel@tonic-gate 			srvtab = optarg;
191*0Sstevel@tonic-gate 					if (!srvtab)
192*0Sstevel@tonic-gate 						usage();
193*0Sstevel@tonic-gate 					break;
194*0Sstevel@tonic-gate 		case '?':
195*0Sstevel@tonic-gate 				default:
196*0Sstevel@tonic-gate 			printf (gettext("Error \n"));
197*0Sstevel@tonic-gate 					usage();
198*0Sstevel@tonic-gate 				}
199*0Sstevel@tonic-gate 			}
200*0Sstevel@tonic-gate 	argc -= optind;
201*0Sstevel@tonic-gate 	argv = &argv[optind];
202*0Sstevel@tonic-gate 	if (*argv)
203*0Sstevel@tonic-gate 		slave_host = *argv;
204*0Sstevel@tonic-gate 			else
205*0Sstevel@tonic-gate 		usage();
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate void get_tickets(context)
210*0Sstevel@tonic-gate     krb5_context context;
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	char   my_host_name[MAXHOSTNAMELEN];
213*0Sstevel@tonic-gate 	char   buf[BUFSIZ];
214*0Sstevel@tonic-gate 	char   *cp;
215*0Sstevel@tonic-gate 	struct hostent *hp;
216*0Sstevel@tonic-gate 	krb5_error_code retval;
217*0Sstevel@tonic-gate 	static char tkstring[] = "/tmp/kproptktXXXXXX";
218*0Sstevel@tonic-gate 	krb5_keytab keytab = NULL;
219*0Sstevel@tonic-gate 	krb5_get_init_creds_opt opt;
220*0Sstevel@tonic-gate 	char *svcname = NULL;
221*0Sstevel@tonic-gate 	char *def_realm = NULL;
222*0Sstevel@tonic-gate 	char *master_host = NULL;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	/*
226*0Sstevel@tonic-gate 	 * Figure out what tickets we'll be using to send stuff
227*0Sstevel@tonic-gate 	 */
228*0Sstevel@tonic-gate 	if (realm) {
229*0Sstevel@tonic-gate 	    if ((def_realm = strdup(realm)) == NULL) {
230*0Sstevel@tonic-gate 	      com_err(progname, ENOMEM,
231*0Sstevel@tonic-gate 		      gettext("while allocating default realm name '%s'"),
232*0Sstevel@tonic-gate 		      realm);
233*0Sstevel@tonic-gate 	      exit(1);
234*0Sstevel@tonic-gate 	    }
235*0Sstevel@tonic-gate 	} else {
236*0Sstevel@tonic-gate 	    retval = krb5_get_default_realm(context, &def_realm);
237*0Sstevel@tonic-gate 	    if (retval) {
238*0Sstevel@tonic-gate 	        com_err(progname, retval,
239*0Sstevel@tonic-gate 			gettext("while getting default realm"));
240*0Sstevel@tonic-gate 	        exit(1);
241*0Sstevel@tonic-gate 	    }
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	/*
245*0Sstevel@tonic-gate 	 * Always pick up the master hostname from krb5.conf, as
246*0Sstevel@tonic-gate 	 * opposed to picking up the localhost, so we do not get bit
247*0Sstevel@tonic-gate 	 * if the master KDC is HA and hence points to a logicalhost.
248*0Sstevel@tonic-gate 	 */
249*0Sstevel@tonic-gate 	retval = kadm5_get_master(context, def_realm, &master_host);
250*0Sstevel@tonic-gate 	if (retval) {
251*0Sstevel@tonic-gate 	    free(def_realm);
252*0Sstevel@tonic-gate 	    com_err(progname, retval,
253*0Sstevel@tonic-gate 		gettext("while getting admin server fqdn"));
254*0Sstevel@tonic-gate 	    exit(1);
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	retval = krb5_sname_to_principal(context, master_host, NULL,
258*0Sstevel@tonic-gate 					 KRB5_NT_SRV_HST, &my_principal);
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	free(def_realm);
261*0Sstevel@tonic-gate 	free(master_host);
262*0Sstevel@tonic-gate 	if (retval) {
263*0Sstevel@tonic-gate 	    com_err(progname, errno, gettext("while setting client principal name"));
264*0Sstevel@tonic-gate 	    exit(1);
265*0Sstevel@tonic-gate 	}
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	if (realm) {
268*0Sstevel@tonic-gate 	    (void) krb5_xfree(krb5_princ_realm(context, my_principal)->data);
269*0Sstevel@tonic-gate 	    krb5_princ_set_realm_length(context, my_principal, strlen(realm));
270*0Sstevel@tonic-gate 	    krb5_princ_set_realm_data(context, my_principal, strdup(realm));
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate #if 0
273*0Sstevel@tonic-gate 	krb5_princ_type(context, my_principal) = KRB5_NT_PRINCIPAL;
274*0Sstevel@tonic-gate #endif
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	/*
277*0Sstevel@tonic-gate 	 * Initialize cache file which we're going to be using
278*0Sstevel@tonic-gate 	 */
279*0Sstevel@tonic-gate 	(void) mktemp(tkstring);
280*0Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), gettext("FILE:%s"), tkstring);
281*0Sstevel@tonic-gate 	if (retval = krb5_cc_resolve(context, buf, &ccache)) {
282*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("while opening credential cache %s"),
283*0Sstevel@tonic-gate 			buf);
284*0Sstevel@tonic-gate 		exit(1);
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 	if (retval = krb5_cc_initialize(context, ccache, my_principal)) {
287*0Sstevel@tonic-gate 		com_err (progname, retval, gettext("when initializing cache %s"),
288*0Sstevel@tonic-gate 			 buf);
289*0Sstevel@tonic-gate 		exit(1);
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	/*
293*0Sstevel@tonic-gate 	 * Get the tickets we'll need.
294*0Sstevel@tonic-gate 	 *
295*0Sstevel@tonic-gate 	 * Construct the principal name for the slave host.
296*0Sstevel@tonic-gate 	 */
297*0Sstevel@tonic-gate 	memset((char *)&creds, 0, sizeof(creds));
298*0Sstevel@tonic-gate 	retval = krb5_sname_to_principal(context,
299*0Sstevel@tonic-gate 					 slave_host, KPROP_SERVICE_NAME,
300*0Sstevel@tonic-gate 					 KRB5_NT_SRV_HST, &creds.server);
301*0Sstevel@tonic-gate 	if (retval) {
302*0Sstevel@tonic-gate 	    com_err(progname, errno, gettext("while setting server principal name"));
303*0Sstevel@tonic-gate 	    (void) krb5_cc_destroy(context, ccache);
304*0Sstevel@tonic-gate 	    exit(1);
305*0Sstevel@tonic-gate 	}
306*0Sstevel@tonic-gate 	if (realm) {
307*0Sstevel@tonic-gate 	    (void) krb5_xfree(krb5_princ_realm(context, creds.server)->data);
308*0Sstevel@tonic-gate 	    krb5_princ_set_realm_length(context, creds.server, strlen(realm));
309*0Sstevel@tonic-gate 	    krb5_princ_set_realm_data(context, creds.server, strdup(realm));
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	/*
313*0Sstevel@tonic-gate 	 * Now fill in the client....
314*0Sstevel@tonic-gate 	 */
315*0Sstevel@tonic-gate 	if (retval = krb5_copy_principal(context, my_principal, &creds.client)) {
316*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("While copying client principal"));
317*0Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
318*0Sstevel@tonic-gate 		exit(1);
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 	if (srvtab) {
321*0Sstevel@tonic-gate 		if (retval = krb5_kt_resolve(context, srvtab, &keytab)) {
322*0Sstevel@tonic-gate 			com_err(progname, retval, gettext("while resolving keytab"));
323*0Sstevel@tonic-gate 			(void) krb5_cc_destroy(context, ccache);
324*0Sstevel@tonic-gate 			exit(1);
325*0Sstevel@tonic-gate 		}
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 	(void) memset(&opt, 0, sizeof (opt));
328*0Sstevel@tonic-gate 	krb5_get_init_creds_opt_init(&opt);
329*0Sstevel@tonic-gate 	retval = krb5_unparse_name(context,  creds.server, &svcname);
330*0Sstevel@tonic-gate 	if (retval) {
331*0Sstevel@tonic-gate 		com_err(progname, errno, gettext("while parsing svc principal name"));
332*0Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
333*0Sstevel@tonic-gate 		exit (1);
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 	retval = krb5_get_init_creds_keytab(context, &creds, creds.client,
336*0Sstevel@tonic-gate 				keytab,  0, svcname, &opt);
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	if (svcname)
339*0Sstevel@tonic-gate 		free(svcname);
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	if (retval) {
342*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("while getting initial ticket\n"));
343*0Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
344*0Sstevel@tonic-gate 		exit(1);
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	if (keytab)
348*0Sstevel@tonic-gate 	    (void) krb5_kt_close(context, keytab);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	/*
351*0Sstevel@tonic-gate 	 * Now destroy the cache right away --- the credentials we
352*0Sstevel@tonic-gate 	 * need will be in my_creds.
353*0Sstevel@tonic-gate 	 */
354*0Sstevel@tonic-gate 	if (retval = krb5_cc_destroy(context, ccache)) {
355*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("while destroying ticket cache"));
356*0Sstevel@tonic-gate 		exit(1);
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate krb5_error_code
361*0Sstevel@tonic-gate open_connection(host, fd, Errmsg, ErrmsgSz)
362*0Sstevel@tonic-gate 	char	*host;
363*0Sstevel@tonic-gate 	int	*fd;
364*0Sstevel@tonic-gate 	char	*Errmsg;
365*0Sstevel@tonic-gate 	int	 ErrmsgSz;
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate 	int	s;
368*0Sstevel@tonic-gate 	krb5_error_code	retval;
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	int	socket_length;
371*0Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
372*0Sstevel@tonic-gate 	struct sockaddr_storage	  ss;
373*0Sstevel@tonic-gate 	char serv_or_port[NI_MAXSERV];
374*0Sstevel@tonic-gate 	enum err_types {SOCKET, CONNECT};
375*0Sstevel@tonic-gate 	int which_err;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
378*0Sstevel@tonic-gate 	hints.ai_family = AF_UNSPEC;    /* go for either IPv4 or v6 */
379*0Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	if (port != 0)
382*0Sstevel@tonic-gate 		(void) snprintf(serv_or_port, sizeof(serv_or_port), ("%d"),
383*0Sstevel@tonic-gate 				port);
384*0Sstevel@tonic-gate 	else
385*0Sstevel@tonic-gate 		strncpy(serv_or_port, KPROP_SERVICE, sizeof(serv_or_port));
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	if (getaddrinfo(host, serv_or_port, &hints, &aitop) != 0) {
388*0Sstevel@tonic-gate 		(void) snprintf(Errmsg, ERRMSGSIZ, gettext("%s: unknown host"),
389*0Sstevel@tonic-gate 				host);
390*0Sstevel@tonic-gate 		*fd = -1;
391*0Sstevel@tonic-gate 		return(0);
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
395*0Sstevel@tonic-gate 		if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
396*0Sstevel@tonic-gate 			continue;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 		s = socket(ai->ai_family, SOCK_STREAM, 0);
399*0Sstevel@tonic-gate 		if (s < 0) {
400*0Sstevel@tonic-gate 			which_err = SOCKET;
401*0Sstevel@tonic-gate 			retval = errno;
402*0Sstevel@tonic-gate 			continue;
403*0Sstevel@tonic-gate 		}
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
406*0Sstevel@tonic-gate 		    errno != EINPROGRESS) {
407*0Sstevel@tonic-gate 			which_err = CONNECT;
408*0Sstevel@tonic-gate 			retval = errno;
409*0Sstevel@tonic-gate 			close(s);
410*0Sstevel@tonic-gate 			continue;	/* fail -- try next */
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 		break; /* success */
414*0Sstevel@tonic-gate 	}
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	if (ai == NULL) {
417*0Sstevel@tonic-gate 		switch (which_err) {
418*0Sstevel@tonic-gate 			case SOCKET:
419*0Sstevel@tonic-gate 				(void) snprintf(Errmsg, ERRMSGSIZ,
420*0Sstevel@tonic-gate 						gettext("in call to socket"));
421*0Sstevel@tonic-gate 				break;
422*0Sstevel@tonic-gate 			case CONNECT:
423*0Sstevel@tonic-gate 				(void) snprintf(Errmsg, ERRMSGSIZ,
424*0Sstevel@tonic-gate 						gettext("in call to connect"));
425*0Sstevel@tonic-gate 				break;
426*0Sstevel@tonic-gate 			default :
427*0Sstevel@tonic-gate 				retval = -1; /* generic error */
428*0Sstevel@tonic-gate 				(void) snprintf(Errmsg, ERRMSGSIZ,
429*0Sstevel@tonic-gate 					gettext("could not setup network"));
430*0Sstevel@tonic-gate 				break;
431*0Sstevel@tonic-gate 		}
432*0Sstevel@tonic-gate 		if (aitop != NULL)
433*0Sstevel@tonic-gate 			freeaddrinfo(aitop);
434*0Sstevel@tonic-gate 		return(retval);
435*0Sstevel@tonic-gate 	}
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	*fd = s;
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	/*
440*0Sstevel@tonic-gate 	 * Set receiver_addr and sender_addr.
441*0Sstevel@tonic-gate 	 */
442*0Sstevel@tonic-gate 	if (cvtkaddr((struct sockaddr_storage *)ai->ai_addr, &receiver_addr)
443*0Sstevel@tonic-gate 			== NULL) {
444*0Sstevel@tonic-gate 		retval = errno;
445*0Sstevel@tonic-gate 		com_err(progname, errno,
446*0Sstevel@tonic-gate 			gettext("while converting socket address"));
447*0Sstevel@tonic-gate 		if (aitop != NULL)
448*0Sstevel@tonic-gate 			freeaddrinfo(aitop);
449*0Sstevel@tonic-gate 		return(retval);
450*0Sstevel@tonic-gate 	}
451*0Sstevel@tonic-gate 	if (aitop != NULL)
452*0Sstevel@tonic-gate 		freeaddrinfo(aitop);
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	socket_length = sizeof(ss);
455*0Sstevel@tonic-gate 	if (getsockname(s, (struct sockaddr *)&ss, &socket_length) < 0) {
456*0Sstevel@tonic-gate 		retval = errno;
457*0Sstevel@tonic-gate 		close(s);
458*0Sstevel@tonic-gate 		(void) snprintf(Errmsg, ERRMSGSIZ,
459*0Sstevel@tonic-gate 				gettext("in call to getsockname"));
460*0Sstevel@tonic-gate 		return(retval);
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	if (cvtkaddr(&ss, &sender_addr) == NULL) {
464*0Sstevel@tonic-gate 		retval = errno;
465*0Sstevel@tonic-gate 		com_err(progname, errno,
466*0Sstevel@tonic-gate 			gettext("while converting socket address"));
467*0Sstevel@tonic-gate 		return(retval);
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	return(0);
471*0Sstevel@tonic-gate }
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate void kerberos_authenticate(context, auth_context, fd, me, new_creds)
475*0Sstevel@tonic-gate     krb5_context context;
476*0Sstevel@tonic-gate     krb5_auth_context *auth_context;
477*0Sstevel@tonic-gate     int	fd;
478*0Sstevel@tonic-gate     krb5_principal me;
479*0Sstevel@tonic-gate     krb5_creds ** new_creds;
480*0Sstevel@tonic-gate {
481*0Sstevel@tonic-gate 	krb5_error_code	retval;
482*0Sstevel@tonic-gate 	krb5_error	*error = NULL;
483*0Sstevel@tonic-gate 	krb5_ap_rep_enc_part	*rep_result;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate     if (retval = krb5_auth_con_init(context, auth_context))
486*0Sstevel@tonic-gate 	exit(1);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate     krb5_auth_con_setflags(context, *auth_context,
489*0Sstevel@tonic-gate 			   KRB5_AUTH_CONTEXT_DO_SEQUENCE);
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate     if (retval = krb5_auth_con_setaddrs(context, *auth_context, &sender_addr,
492*0Sstevel@tonic-gate 				        &receiver_addr)) {
493*0Sstevel@tonic-gate 	com_err(progname, retval, gettext("in krb5_auth_con_setaddrs"));
494*0Sstevel@tonic-gate 	exit(1);
495*0Sstevel@tonic-gate     }
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	if (retval = krb5_sendauth(context, auth_context, (void *)&fd,
498*0Sstevel@tonic-gate 				   kprop_version, me, creds.server,
499*0Sstevel@tonic-gate 				   AP_OPTS_MUTUAL_REQUIRED, NULL, &creds, NULL,
500*0Sstevel@tonic-gate 				   &error, &rep_result, new_creds)) {
501*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("while authenticating to server"));
502*0Sstevel@tonic-gate 		if (error) {
503*0Sstevel@tonic-gate 			if (error->error == KRB_ERR_GENERIC) {
504*0Sstevel@tonic-gate 				if (error->text.data)
505*0Sstevel@tonic-gate 					fprintf(stderr,
506*0Sstevel@tonic-gate 						gettext("Generic remote error: %s\n"),
507*0Sstevel@tonic-gate 						error->text.data);
508*0Sstevel@tonic-gate 			} else if (error->error) {
509*0Sstevel@tonic-gate 				com_err(progname,
510*0Sstevel@tonic-gate 					error->error + ERROR_TABLE_BASE_krb5,
511*0Sstevel@tonic-gate 					gettext("signalled from server"));
512*0Sstevel@tonic-gate 				if (error->text.data)
513*0Sstevel@tonic-gate 					fprintf(stderr,
514*0Sstevel@tonic-gate 					gettext("Error text from server: %s\n"),
515*0Sstevel@tonic-gate 						error->text.data);
516*0Sstevel@tonic-gate 			}
517*0Sstevel@tonic-gate 			krb5_free_error(context, error);
518*0Sstevel@tonic-gate 		}
519*0Sstevel@tonic-gate 		exit(1);
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate 	krb5_free_ap_rep_enc_part(context, rep_result);
522*0Sstevel@tonic-gate }
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate char * dbpathname;
525*0Sstevel@tonic-gate /*
526*0Sstevel@tonic-gate  * Open the Kerberos database dump file.  Takes care of locking it
527*0Sstevel@tonic-gate  * and making sure that the .ok file is more recent that the database
528*0Sstevel@tonic-gate  * dump file itself.
529*0Sstevel@tonic-gate  *
530*0Sstevel@tonic-gate  * Returns the file descriptor of the database dump file.  Also fills
531*0Sstevel@tonic-gate  * in the size of the database file.
532*0Sstevel@tonic-gate  */
533*0Sstevel@tonic-gate int
534*0Sstevel@tonic-gate open_database(context, data_fn, size)
535*0Sstevel@tonic-gate     krb5_context context;
536*0Sstevel@tonic-gate     char *data_fn;
537*0Sstevel@tonic-gate     int	*size;
538*0Sstevel@tonic-gate {
539*0Sstevel@tonic-gate 	int		fd;
540*0Sstevel@tonic-gate 	int		err;
541*0Sstevel@tonic-gate 	struct stat 	stbuf, stbuf_ok;
542*0Sstevel@tonic-gate 	char		*data_ok_fn;
543*0Sstevel@tonic-gate 	static char ok[] = ".dump_ok";
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 	dbpathname = strdup(data_fn);
546*0Sstevel@tonic-gate 	if (!dbpathname) {
547*0Sstevel@tonic-gate  	    com_err(progname, ENOMEM, gettext("allocating database file name '%s'"),
548*0Sstevel@tonic-gate  		    data_fn);
549*0Sstevel@tonic-gate  	    exit(1);
550*0Sstevel@tonic-gate  	}
551*0Sstevel@tonic-gate 	if ((fd = open(dbpathname, O_RDONLY)) < 0) {
552*0Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to open %s"),
553*0Sstevel@tonic-gate 			dbpathname);
554*0Sstevel@tonic-gate 		exit(1);
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	err = krb5_lock_file(context, fd,
558*0Sstevel@tonic-gate 			     KRB5_LOCKMODE_SHARED|KRB5_LOCKMODE_DONTBLOCK);
559*0Sstevel@tonic-gate 	if (err == EAGAIN || err == EWOULDBLOCK || errno == EACCES) {
560*0Sstevel@tonic-gate 	    com_err(progname, 0, gettext("database locked"));
561*0Sstevel@tonic-gate 	    exit(1);
562*0Sstevel@tonic-gate 	} else if (err) {
563*0Sstevel@tonic-gate 	    com_err(progname, err, gettext("while trying to lock '%s'"), dbpathname);
564*0Sstevel@tonic-gate 	    exit(1);
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 	if (fstat(fd, &stbuf)) {
567*0Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to stat %s"),
568*0Sstevel@tonic-gate 			data_fn);
569*0Sstevel@tonic-gate 		exit(1);
570*0Sstevel@tonic-gate 	}
571*0Sstevel@tonic-gate 	if ((data_ok_fn = (char *) malloc(strlen(data_fn)+strlen(ok)+1))
572*0Sstevel@tonic-gate 	    == NULL) {
573*0Sstevel@tonic-gate 		com_err(progname, ENOMEM, gettext("while trying to malloc data_ok_fn"));
574*0Sstevel@tonic-gate 		exit(1);
575*0Sstevel@tonic-gate 	}
576*0Sstevel@tonic-gate 	strcpy(data_ok_fn, data_fn);
577*0Sstevel@tonic-gate 	strcat(data_ok_fn, ok);
578*0Sstevel@tonic-gate 	if (stat(data_ok_fn, &stbuf_ok)) {
579*0Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to stat %s"),
580*0Sstevel@tonic-gate 			data_ok_fn);
581*0Sstevel@tonic-gate 		free(data_ok_fn);
582*0Sstevel@tonic-gate 		exit(1);
583*0Sstevel@tonic-gate 	}
584*0Sstevel@tonic-gate 	free(data_ok_fn);
585*0Sstevel@tonic-gate 	if (stbuf.st_mtime > stbuf_ok.st_mtime) {
586*0Sstevel@tonic-gate 		com_err(progname, 0, gettext("'%s' more recent than '%s'."),
587*0Sstevel@tonic-gate 			data_fn, data_ok_fn);
588*0Sstevel@tonic-gate 		exit(1);
589*0Sstevel@tonic-gate 	}
590*0Sstevel@tonic-gate 	*size = stbuf.st_size;
591*0Sstevel@tonic-gate 	return(fd);
592*0Sstevel@tonic-gate }
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate void
595*0Sstevel@tonic-gate close_database(context, fd)
596*0Sstevel@tonic-gate     krb5_context context;
597*0Sstevel@tonic-gate     int fd;
598*0Sstevel@tonic-gate {
599*0Sstevel@tonic-gate     int err;
600*0Sstevel@tonic-gate     if (err = krb5_lock_file(context, fd, KRB5_LOCKMODE_UNLOCK))
601*0Sstevel@tonic-gate 	com_err(progname, err, gettext("while unlocking database '%s'"), dbpathname);
602*0Sstevel@tonic-gate     free(dbpathname);
603*0Sstevel@tonic-gate     (void)close(fd);
604*0Sstevel@tonic-gate     return;
605*0Sstevel@tonic-gate }
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate /*
608*0Sstevel@tonic-gate  * Now we send over the database.  We use the following protocol:
609*0Sstevel@tonic-gate  * Send over a KRB_SAFE message with the size.  Then we send over the
610*0Sstevel@tonic-gate  * database in blocks of KPROP_BLKSIZE, encrypted using KRB_PRIV.
611*0Sstevel@tonic-gate  * Then we expect to see a KRB_SAFE message with the size sent back.
612*0Sstevel@tonic-gate  *
613*0Sstevel@tonic-gate  * At any point in the protocol, we may send a KRB_ERROR message; this
614*0Sstevel@tonic-gate  * will abort the entire operation.
615*0Sstevel@tonic-gate  */
616*0Sstevel@tonic-gate void
617*0Sstevel@tonic-gate xmit_database(context, auth_context, my_creds, fd, database_fd, database_size)
618*0Sstevel@tonic-gate     krb5_context context;
619*0Sstevel@tonic-gate     krb5_auth_context auth_context;
620*0Sstevel@tonic-gate     krb5_creds *my_creds;
621*0Sstevel@tonic-gate     int	fd;
622*0Sstevel@tonic-gate     int	database_fd;
623*0Sstevel@tonic-gate     int	database_size;
624*0Sstevel@tonic-gate {
625*0Sstevel@tonic-gate 	krb5_int32	send_size, sent_size, n;
626*0Sstevel@tonic-gate 	krb5_data	inbuf, outbuf;
627*0Sstevel@tonic-gate 	char		buf[KPROP_BUFSIZ];
628*0Sstevel@tonic-gate 	krb5_error_code	retval;
629*0Sstevel@tonic-gate 	krb5_error	*error;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 	/*
632*0Sstevel@tonic-gate 	 * Send over the size
633*0Sstevel@tonic-gate 	 */
634*0Sstevel@tonic-gate 	send_size = htonl(database_size);
635*0Sstevel@tonic-gate 	inbuf.data = (char *) &send_size;
636*0Sstevel@tonic-gate 	inbuf.length = sizeof(send_size); /* must be 4, really */
637*0Sstevel@tonic-gate 	/* KPROP_CKSUMTYPE */
638*0Sstevel@tonic-gate 	if (retval = krb5_mk_safe(context, auth_context, &inbuf,
639*0Sstevel@tonic-gate 				  &outbuf, NULL)) {
640*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("while encoding database size"));
641*0Sstevel@tonic-gate 		send_error(context, my_creds, fd, gettext("while encoding database size"), retval);
642*0Sstevel@tonic-gate 		exit(1);
643*0Sstevel@tonic-gate 	}
644*0Sstevel@tonic-gate 	if (retval = krb5_write_message(context, (void *) &fd, &outbuf)) {
645*0Sstevel@tonic-gate 		krb5_free_data_contents(context, &outbuf);
646*0Sstevel@tonic-gate 		com_err(progname, retval, gettext("while sending database size"));
647*0Sstevel@tonic-gate 		exit(1);
648*0Sstevel@tonic-gate 	}
649*0Sstevel@tonic-gate 	krb5_free_data_contents(context, &outbuf);
650*0Sstevel@tonic-gate     /*
651*0Sstevel@tonic-gate      * Initialize the initial vector.
652*0Sstevel@tonic-gate      */
653*0Sstevel@tonic-gate     if (retval = krb5_auth_con_initivector(context, auth_context)) {
654*0Sstevel@tonic-gate 	send_error(context, my_creds, fd,
655*0Sstevel@tonic-gate 		   gettext("failed while initializing i_vector"), retval);
656*0Sstevel@tonic-gate 	com_err(progname, retval, gettext("while allocating i_vector"));
657*0Sstevel@tonic-gate 	exit(1);
658*0Sstevel@tonic-gate     }
659*0Sstevel@tonic-gate 	/*
660*0Sstevel@tonic-gate 	 * Send over the file, block by block....
661*0Sstevel@tonic-gate 	 */
662*0Sstevel@tonic-gate 	inbuf.data = buf;
663*0Sstevel@tonic-gate 	sent_size = 0;
664*0Sstevel@tonic-gate 	while (n = read(database_fd, buf, sizeof(buf))) {
665*0Sstevel@tonic-gate 		inbuf.length = n;
666*0Sstevel@tonic-gate 		if (retval = krb5_mk_priv(context, auth_context, &inbuf,
667*0Sstevel@tonic-gate 					  &outbuf, NULL)) {
668*0Sstevel@tonic-gate 			snprintf(buf, sizeof (buf),
669*0Sstevel@tonic-gate 				gettext("while encoding database block starting at %d"),
670*0Sstevel@tonic-gate 				sent_size);
671*0Sstevel@tonic-gate 			com_err(progname, retval, buf);
672*0Sstevel@tonic-gate 			send_error(context, my_creds, fd, buf, retval);
673*0Sstevel@tonic-gate 			exit(1);
674*0Sstevel@tonic-gate 		}
675*0Sstevel@tonic-gate 		if (retval = krb5_write_message(context, (void *)&fd,&outbuf)) {
676*0Sstevel@tonic-gate 			krb5_free_data_contents(context, &outbuf);
677*0Sstevel@tonic-gate 			com_err(progname, retval,
678*0Sstevel@tonic-gate 				gettext("while sending database block starting at %d"),
679*0Sstevel@tonic-gate 				sent_size);
680*0Sstevel@tonic-gate 			exit(1);
681*0Sstevel@tonic-gate 		}
682*0Sstevel@tonic-gate 		krb5_free_data_contents(context, &outbuf);
683*0Sstevel@tonic-gate 		sent_size += n;
684*0Sstevel@tonic-gate 		if (debug)
685*0Sstevel@tonic-gate 			printf(gettext("%d bytes sent.\n"), sent_size);
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 	if (sent_size != database_size) {
688*0Sstevel@tonic-gate 		com_err(progname, 0, gettext("Premature EOF found for database file!"));
689*0Sstevel@tonic-gate 		send_error(context, my_creds, fd,gettext("Premature EOF found for database file!"),
690*0Sstevel@tonic-gate 			   KRB5KRB_ERR_GENERIC);
691*0Sstevel@tonic-gate 		exit(1);
692*0Sstevel@tonic-gate 	}
693*0Sstevel@tonic-gate 	/*
694*0Sstevel@tonic-gate 	 * OK, we've sent the database; now let's wait for a success
695*0Sstevel@tonic-gate 	 * indication from the remote end.
696*0Sstevel@tonic-gate 	 */
697*0Sstevel@tonic-gate 	if (retval = krb5_read_message(context, (void *) &fd, &inbuf)) {
698*0Sstevel@tonic-gate 		com_err(progname, retval,
699*0Sstevel@tonic-gate 			gettext("while reading response from server"));
700*0Sstevel@tonic-gate 		exit(1);
701*0Sstevel@tonic-gate 	}
702*0Sstevel@tonic-gate 	/*
703*0Sstevel@tonic-gate 	 * If we got an error response back from the server, display
704*0Sstevel@tonic-gate 	 * the error message
705*0Sstevel@tonic-gate 	 */
706*0Sstevel@tonic-gate 	if (krb5_is_krb_error(&inbuf)) {
707*0Sstevel@tonic-gate 		if (retval = krb5_rd_error(context, &inbuf, &error)) {
708*0Sstevel@tonic-gate 			com_err(progname, retval,
709*0Sstevel@tonic-gate 				gettext("while decoding error response from server"));
710*0Sstevel@tonic-gate 			exit(1);
711*0Sstevel@tonic-gate 		}
712*0Sstevel@tonic-gate 		if (error->error == KRB_ERR_GENERIC) {
713*0Sstevel@tonic-gate 			if (error->text.data)
714*0Sstevel@tonic-gate 				fprintf(stderr,
715*0Sstevel@tonic-gate 				gettext("Generic remote error: %s\n"),
716*0Sstevel@tonic-gate 					error->text.data);
717*0Sstevel@tonic-gate 		} else if (error->error) {
718*0Sstevel@tonic-gate 			com_err(progname, error->error + ERROR_TABLE_BASE_krb5,
719*0Sstevel@tonic-gate 				gettext("signalled from server"));
720*0Sstevel@tonic-gate 			if (error->text.data)
721*0Sstevel@tonic-gate 				fprintf(stderr,
722*0Sstevel@tonic-gate 				gettext("Error text from server: %s\n"),
723*0Sstevel@tonic-gate 					error->text.data);
724*0Sstevel@tonic-gate 		}
725*0Sstevel@tonic-gate 		krb5_free_error(context, error);
726*0Sstevel@tonic-gate 		exit(1);
727*0Sstevel@tonic-gate 	}
728*0Sstevel@tonic-gate 	if (retval = krb5_rd_safe(context,auth_context,&inbuf,&outbuf,NULL)) {
729*0Sstevel@tonic-gate 		com_err(progname, retval,
730*0Sstevel@tonic-gate 			gettext("while decoding final size packet from server"));
731*0Sstevel@tonic-gate 		exit(1);
732*0Sstevel@tonic-gate 	}
733*0Sstevel@tonic-gate 	memcpy((char *)&send_size, outbuf.data, sizeof(send_size));
734*0Sstevel@tonic-gate 	send_size = ntohl(send_size);
735*0Sstevel@tonic-gate 	if (send_size != database_size) {
736*0Sstevel@tonic-gate 		com_err(progname, 0,
737*0Sstevel@tonic-gate 			gettext("Kpropd sent database size %d, expecting %d"),
738*0Sstevel@tonic-gate 			send_size, database_size);
739*0Sstevel@tonic-gate 		exit(1);
740*0Sstevel@tonic-gate 	}
741*0Sstevel@tonic-gate 	free(outbuf.data);
742*0Sstevel@tonic-gate 	free(inbuf.data);
743*0Sstevel@tonic-gate }
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate void
746*0Sstevel@tonic-gate send_error(context, my_creds, fd, err_text, err_code)
747*0Sstevel@tonic-gate     krb5_context context;
748*0Sstevel@tonic-gate     krb5_creds *my_creds;
749*0Sstevel@tonic-gate     int	fd;
750*0Sstevel@tonic-gate     char	*err_text;
751*0Sstevel@tonic-gate     krb5_error_code	err_code;
752*0Sstevel@tonic-gate {
753*0Sstevel@tonic-gate 	krb5_error	error;
754*0Sstevel@tonic-gate 	const char	*text;
755*0Sstevel@tonic-gate 	krb5_data	outbuf;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	memset((char *)&error, 0, sizeof(error));
758*0Sstevel@tonic-gate 	krb5_us_timeofday(context, &error.ctime, &error.cusec);
759*0Sstevel@tonic-gate 	error.server = my_creds->server;
760*0Sstevel@tonic-gate 	error.client = my_principal;
761*0Sstevel@tonic-gate 	error.error = err_code - ERROR_TABLE_BASE_krb5;
762*0Sstevel@tonic-gate 	if (error.error > 127)
763*0Sstevel@tonic-gate 		error.error = KRB_ERR_GENERIC;
764*0Sstevel@tonic-gate 	if (err_text)
765*0Sstevel@tonic-gate 		text = err_text;
766*0Sstevel@tonic-gate 	else
767*0Sstevel@tonic-gate 		text = error_message(err_code);
768*0Sstevel@tonic-gate 	error.text.length = strlen(text) + 1;
769*0Sstevel@tonic-gate 	if (error.text.data = malloc(error.text.length)) {
770*0Sstevel@tonic-gate 		strcpy(error.text.data, text);
771*0Sstevel@tonic-gate 		if (!krb5_mk_error(context, &error, &outbuf)) {
772*0Sstevel@tonic-gate 			(void) krb5_write_message(context, (void *)&fd,&outbuf);
773*0Sstevel@tonic-gate 			krb5_free_data_contents(context, &outbuf);
774*0Sstevel@tonic-gate 		}
775*0Sstevel@tonic-gate 		free(error.text.data);
776*0Sstevel@tonic-gate 	}
777*0Sstevel@tonic-gate }
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate void update_last_prop_file(hostname, file_name)
780*0Sstevel@tonic-gate 	char *hostname;
781*0Sstevel@tonic-gate 	char *file_name;
782*0Sstevel@tonic-gate {
783*0Sstevel@tonic-gate 	/* handle slave locking/failure stuff */
784*0Sstevel@tonic-gate 	char *file_last_prop;
785*0Sstevel@tonic-gate 	int fd;
786*0Sstevel@tonic-gate 	static char last_prop[]=".last_prop";
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	if ((file_last_prop = (char *)malloc(strlen(file_name) +
789*0Sstevel@tonic-gate 					     strlen(hostname) + 1 +
790*0Sstevel@tonic-gate 					     strlen(last_prop) + 1)) == NULL) {
791*0Sstevel@tonic-gate 		com_err(progname, ENOMEM,
792*0Sstevel@tonic-gate 			gettext("while allocating filename for update_last_prop_file"));
793*0Sstevel@tonic-gate 		return;
794*0Sstevel@tonic-gate 	}
795*0Sstevel@tonic-gate 	strcpy(file_last_prop, file_name);
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	/*
798*0Sstevel@tonic-gate 	 * If a nondefault file name was specified then we should not add an
799*0Sstevel@tonic-gate 	 * extraneous host name to the file name given that a file name could
800*0Sstevel@tonic-gate 	 * have already specified a host name and therefore would be redundant.
801*0Sstevel@tonic-gate 	 */
802*0Sstevel@tonic-gate 	if (strcmp(file_name, KPROP_DEFAULT_FILE) == 0) {
803*0Sstevel@tonic-gate 		strcat(file_last_prop, ".");
804*0Sstevel@tonic-gate 		strcat(file_last_prop, hostname);
805*0Sstevel@tonic-gate 	}
806*0Sstevel@tonic-gate 	strcat(file_last_prop, last_prop);
807*0Sstevel@tonic-gate 	if ((fd = THREEPARAMOPEN(file_last_prop, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
808*0Sstevel@tonic-gate 		com_err(progname, errno,
809*0Sstevel@tonic-gate 			gettext("while creating 'last_prop' file, '%s'"),
810*0Sstevel@tonic-gate 			file_last_prop);
811*0Sstevel@tonic-gate 		free(file_last_prop);
812*0Sstevel@tonic-gate 		return;
813*0Sstevel@tonic-gate 	}
814*0Sstevel@tonic-gate 	write(fd, "", 1);
815*0Sstevel@tonic-gate 	free(file_last_prop);
816*0Sstevel@tonic-gate 	close(fd);
817*0Sstevel@tonic-gate 	return;
818*0Sstevel@tonic-gate }
819