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