17c478bd9Sstevel@tonic-gate /*
24e2a441bSPeter Shoults  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #include <stdio.h>
77c478bd9Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
87c478bd9Sstevel@tonic-gate #include <signal.h>
97c478bd9Sstevel@tonic-gate #include <sys/types.h>
107c478bd9Sstevel@tonic-gate #include <memory.h>
117c478bd9Sstevel@tonic-gate #include <stropts.h>
127c478bd9Sstevel@tonic-gate #include <netconfig.h>
137c478bd9Sstevel@tonic-gate #include <sys/resource.h> /* rlimit */
147c478bd9Sstevel@tonic-gate #include <syslog.h>
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include <kadm5/admin.h>
177c478bd9Sstevel@tonic-gate #include <kadm5/kadm_rpc.h>
187c478bd9Sstevel@tonic-gate #include <kadm5/server_internal.h>
197c478bd9Sstevel@tonic-gate #include <server_acl.h>
207c478bd9Sstevel@tonic-gate #include <krb5/adm_proto.h>
217c478bd9Sstevel@tonic-gate #include <string.h>
227c478bd9Sstevel@tonic-gate #include <gssapi_krb5.h>
237c478bd9Sstevel@tonic-gate #include <sys/socket.h>
247c478bd9Sstevel@tonic-gate #include <netinet/in.h>
257c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
267c478bd9Sstevel@tonic-gate #include <netdb.h>
277c478bd9Sstevel@tonic-gate #include <libintl.h>
287c478bd9Sstevel@tonic-gate #include <kdb/kdb_log.h>
297c478bd9Sstevel@tonic-gate #include "misc.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate extern int setup_gss_names(struct svc_req *, char **, char **);
327c478bd9Sstevel@tonic-gate extern gss_name_t get_clnt_name(struct svc_req *);
337c478bd9Sstevel@tonic-gate extern char *client_addr(struct svc_req *, char *);
347c478bd9Sstevel@tonic-gate extern void *global_server_handle;
357c478bd9Sstevel@tonic-gate extern int nofork;
367c478bd9Sstevel@tonic-gate extern short l_port;
377c478bd9Sstevel@tonic-gate static char abuf[33];
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static char *reply_ok_str	= "UPDATE_OK";
407c478bd9Sstevel@tonic-gate static char *reply_err_str	= "UPDATE_ERROR";
417c478bd9Sstevel@tonic-gate static char *reply_fr_str	= "UPDATE_FULL_RESYNC_NEEDED";
427c478bd9Sstevel@tonic-gate static char *reply_busy_str	= "UPDATE_BUSY";
437c478bd9Sstevel@tonic-gate static char *reply_nil_str	= "UPDATE_NIL";
447c478bd9Sstevel@tonic-gate static char *reply_perm_str	= "UPDATE_PERM_DENIED";
457c478bd9Sstevel@tonic-gate static char *reply_unknown_str	= "<UNKNOWN_CODE>";
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	LOG_UNAUTH  gettext("Unauthorized request: %s, %s, " \
487c478bd9Sstevel@tonic-gate 			"client=%s, service=%s, addr=%s")
497c478bd9Sstevel@tonic-gate #define	LOG_DONE    gettext("Request: %s, %s, %s, client=%s, " \
507c478bd9Sstevel@tonic-gate 			"service=%s, addr=%s")
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	KDB5_UTIL_DUMP_STR "/usr/sbin/kdb5_util dump -i "
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef	DPRINT
557c478bd9Sstevel@tonic-gate #undef	DPRINT
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate #define	DPRINT(i) if (nofork) printf i
587c478bd9Sstevel@tonic-gate 
594e2a441bSPeter Shoults #ifdef POSIX_SIGNALS
604e2a441bSPeter Shoults static struct sigaction s_action;
614e2a441bSPeter Shoults #endif /* POSIX_SIGNALS */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static void
debprret(char * w,update_status_t ret,kdb_sno_t sno)647c478bd9Sstevel@tonic-gate debprret(char *w, update_status_t ret, kdb_sno_t sno)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	switch (ret) {
677c478bd9Sstevel@tonic-gate 	case UPDATE_OK:
687c478bd9Sstevel@tonic-gate 		printf("%s: end (OK, sno=%u)\n",
697c478bd9Sstevel@tonic-gate 		    w, sno);
707c478bd9Sstevel@tonic-gate 		break;
717c478bd9Sstevel@tonic-gate 	case UPDATE_ERROR:
727c478bd9Sstevel@tonic-gate 		printf("%s: end (ERROR)\n", w);
737c478bd9Sstevel@tonic-gate 		break;
747c478bd9Sstevel@tonic-gate 	case UPDATE_FULL_RESYNC_NEEDED:
757c478bd9Sstevel@tonic-gate 		printf("%s: end (FR NEEDED)\n", w);
767c478bd9Sstevel@tonic-gate 		break;
777c478bd9Sstevel@tonic-gate 	case UPDATE_BUSY:
787c478bd9Sstevel@tonic-gate 		printf("%s: end (BUSY)\n", w);
797c478bd9Sstevel@tonic-gate 		break;
807c478bd9Sstevel@tonic-gate 	case UPDATE_NIL:
817c478bd9Sstevel@tonic-gate 		printf("%s: end (NIL)\n", w);
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	case UPDATE_PERM_DENIED:
847c478bd9Sstevel@tonic-gate 		printf("%s: end (PERM)\n", w);
857c478bd9Sstevel@tonic-gate 		break;
867c478bd9Sstevel@tonic-gate 	default:
877c478bd9Sstevel@tonic-gate 		printf("%s: end (UNKNOWN return code (%d))\n", w, ret);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static char *
replystr(update_status_t ret)927c478bd9Sstevel@tonic-gate replystr(update_status_t ret)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	switch (ret) {
957c478bd9Sstevel@tonic-gate 	case UPDATE_OK:
967c478bd9Sstevel@tonic-gate 		return (reply_ok_str);
977c478bd9Sstevel@tonic-gate 	case UPDATE_ERROR:
987c478bd9Sstevel@tonic-gate 		return (reply_err_str);
997c478bd9Sstevel@tonic-gate 	case UPDATE_FULL_RESYNC_NEEDED:
1007c478bd9Sstevel@tonic-gate 		return (reply_fr_str);
1017c478bd9Sstevel@tonic-gate 	case UPDATE_BUSY:
1027c478bd9Sstevel@tonic-gate 		return (reply_busy_str);
1037c478bd9Sstevel@tonic-gate 	case UPDATE_NIL:
1047c478bd9Sstevel@tonic-gate 		return (reply_nil_str);
1057c478bd9Sstevel@tonic-gate 	case UPDATE_PERM_DENIED:
1067c478bd9Sstevel@tonic-gate 		return (reply_perm_str);
1077c478bd9Sstevel@tonic-gate 	default:
1087c478bd9Sstevel@tonic-gate 		return (reply_unknown_str);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate kdb_incr_result_t *
iprop_get_updates_1(kdb_last_t * arg,struct svc_req * rqstp)1137c478bd9Sstevel@tonic-gate iprop_get_updates_1(kdb_last_t *arg, struct svc_req *rqstp)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	static kdb_incr_result_t ret;
1167c478bd9Sstevel@tonic-gate 	char *whoami = "iprop_get_updates_1";
1177c478bd9Sstevel@tonic-gate 	int kret;
1187c478bd9Sstevel@tonic-gate 	kadm5_server_handle_t handle = global_server_handle;
1197c478bd9Sstevel@tonic-gate 	char *client_name = NULL, *service_name = NULL;
1207c478bd9Sstevel@tonic-gate 	gss_name_t name = NULL;
1217c478bd9Sstevel@tonic-gate 	OM_uint32 min_stat;
1227c478bd9Sstevel@tonic-gate 	char obuf[256] = {0};
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* default return code */
1257c478bd9Sstevel@tonic-gate 	ret.ret = UPDATE_ERROR;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	DPRINT(("%s: start, last_sno=%u\n", whoami, (ulong_t)arg->last_sno));
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (!handle) {
1307c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
1317c478bd9Sstevel@tonic-gate 				gettext("%s: server handle is NULL"),
1327c478bd9Sstevel@tonic-gate 					whoami);
1337c478bd9Sstevel@tonic-gate 		goto out;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
1377c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
1387c478bd9Sstevel@tonic-gate 			gettext("%s: setup_gss_names failed"),
1397c478bd9Sstevel@tonic-gate 			whoami);
1407c478bd9Sstevel@tonic-gate 		goto out;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	DPRINT(("%s: clprinc=`%s'\n\tsvcprinc=`%s'\n",
1447c478bd9Sstevel@tonic-gate 		whoami, client_name, service_name));
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
1477c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
1487c478bd9Sstevel@tonic-gate 			gettext("%s: Couldn't obtain client's name"),
1497c478bd9Sstevel@tonic-gate 			whoami);
1507c478bd9Sstevel@tonic-gate 		goto out;
1517c478bd9Sstevel@tonic-gate 	}
15256a424ccSmp 	if (!kadm5int_acl_check(handle->context,
1537c478bd9Sstevel@tonic-gate 		    name,
1547c478bd9Sstevel@tonic-gate 		    ACL_IPROP,
1557c478bd9Sstevel@tonic-gate 		    NULL,
1567c478bd9Sstevel@tonic-gate 		    NULL)) {
1577c478bd9Sstevel@tonic-gate 		ret.ret = UPDATE_PERM_DENIED;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
1607c478bd9Sstevel@tonic-gate 				    whoami,
1617c478bd9Sstevel@tonic-gate 				    "<null>", client_name);
1627c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_NOTICE, LOG_UNAUTH, whoami,
1637c478bd9Sstevel@tonic-gate 				"<null>", client_name, service_name,
1647c478bd9Sstevel@tonic-gate 				client_addr(rqstp, abuf));
1657c478bd9Sstevel@tonic-gate 		goto out;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	kret = ulog_get_entries(handle->context, *arg, &ret);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (ret.ret == UPDATE_OK) {
1717c478bd9Sstevel@tonic-gate 		(void) snprintf(obuf, sizeof (obuf),
1727c478bd9Sstevel@tonic-gate 		gettext("%s; Incoming SerialNo=%u; Outgoing SerialNo=%u"),
1737c478bd9Sstevel@tonic-gate 				replystr(ret.ret),
1747c478bd9Sstevel@tonic-gate 				(ulong_t)arg->last_sno,
1757c478bd9Sstevel@tonic-gate 				(ulong_t)ret.lastentry.last_sno);
1767c478bd9Sstevel@tonic-gate 	} else {
1777c478bd9Sstevel@tonic-gate 		(void) snprintf(obuf, sizeof (obuf),
1787c478bd9Sstevel@tonic-gate 		gettext("%s; Incoming SerialNo=%u; Outgoing SerialNo=N/A"),
1797c478bd9Sstevel@tonic-gate 				replystr(ret.ret),
1807c478bd9Sstevel@tonic-gate 				(ulong_t)arg->last_sno);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	audit_kadmind_auth(rqstp->rq_xprt, l_port,
1847c478bd9Sstevel@tonic-gate 			whoami,
1857c478bd9Sstevel@tonic-gate 			obuf, client_name, kret);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	krb5_klog_syslog(LOG_NOTICE, LOG_DONE, whoami,
1887c478bd9Sstevel@tonic-gate 			obuf,
1897c478bd9Sstevel@tonic-gate 			((kret == 0) ? "success" : error_message(kret)),
1907c478bd9Sstevel@tonic-gate 			client_name, service_name,
1917c478bd9Sstevel@tonic-gate 			client_addr(rqstp, abuf));
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate out:
1947c478bd9Sstevel@tonic-gate 	if (nofork)
1957c478bd9Sstevel@tonic-gate 		debprret(whoami, ret.ret, ret.lastentry.last_sno);
1967c478bd9Sstevel@tonic-gate 	if (client_name)
1977c478bd9Sstevel@tonic-gate 		free(client_name);
1987c478bd9Sstevel@tonic-gate 	if (service_name)
1997c478bd9Sstevel@tonic-gate 		free(service_name);
2007c478bd9Sstevel@tonic-gate 	if (name)
2017c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
2027c478bd9Sstevel@tonic-gate 	return (&ret);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Given a client princ (foo/fqdn@R), copy (in arg cl) the fqdn substring.
2087c478bd9Sstevel@tonic-gate  * Return arg cl str ptr on success, else NULL.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate static char *
getclhoststr(char * clprinc,char * cl,int len)2117c478bd9Sstevel@tonic-gate getclhoststr(char *clprinc, char *cl, int len)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	char *s;
2147c478bd9Sstevel@tonic-gate 	if (s = strchr(clprinc, '/')) {
2157c478bd9Sstevel@tonic-gate 		if (!++s || strlcpy(cl, s, len) >= len) {
2167c478bd9Sstevel@tonic-gate 			return (NULL);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 		if (s = strchr(cl, '@')) {
2197c478bd9Sstevel@tonic-gate 			*s = '\0';
2207c478bd9Sstevel@tonic-gate 			return (cl); /* success */
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (NULL);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate kdb_fullresync_result_t *
iprop_full_resync_1(void * argp,struct svc_req * rqstp)2287c478bd9Sstevel@tonic-gate iprop_full_resync_1(
2297c478bd9Sstevel@tonic-gate 	/* LINTED */
2307c478bd9Sstevel@tonic-gate 	void *argp,
2317c478bd9Sstevel@tonic-gate 	struct svc_req *rqstp)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	static kdb_fullresync_result_t ret;
2347c478bd9Sstevel@tonic-gate 	char tmpf[MAX_FILENAME] = {0};
2357c478bd9Sstevel@tonic-gate 	char ubuf[MAX_FILENAME + sizeof (KDB5_UTIL_DUMP_STR)] = {0};
2367c478bd9Sstevel@tonic-gate 	char clhost[MAXHOSTNAMELEN] = {0};
2377c478bd9Sstevel@tonic-gate 	int pret, fret;
2387c478bd9Sstevel@tonic-gate 	kadm5_server_handle_t handle = global_server_handle;
2397c478bd9Sstevel@tonic-gate 	OM_uint32 min_stat;
2407c478bd9Sstevel@tonic-gate 	gss_name_t name = NULL;
2417c478bd9Sstevel@tonic-gate 	char *client_name = NULL, *service_name = NULL;
2427c478bd9Sstevel@tonic-gate 	char *whoami = "iprop_full_resync_1";
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* default return code */
2457c478bd9Sstevel@tonic-gate 	ret.ret = UPDATE_ERROR;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (!handle) {
2487c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
2497c478bd9Sstevel@tonic-gate 				gettext("%s: server handle is NULL"),
2507c478bd9Sstevel@tonic-gate 					whoami);
2517c478bd9Sstevel@tonic-gate 		goto out;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	DPRINT(("%s: start\n", whoami));
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
2577c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
2587c478bd9Sstevel@tonic-gate 			gettext("%s: setup_gss_names failed"),
2597c478bd9Sstevel@tonic-gate 			whoami);
2607c478bd9Sstevel@tonic-gate 		goto out;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	DPRINT(("%s: clprinc=`%s'\n\tsvcprinc=`%s'\n",
2647c478bd9Sstevel@tonic-gate 		whoami, client_name, service_name));
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
2677c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
2687c478bd9Sstevel@tonic-gate 			gettext("%s: Couldn't obtain client's name"),
2697c478bd9Sstevel@tonic-gate 			whoami);
2707c478bd9Sstevel@tonic-gate 		goto out;
2717c478bd9Sstevel@tonic-gate 	}
27256a424ccSmp 	if (!kadm5int_acl_check(handle->context,
2737c478bd9Sstevel@tonic-gate 		    name,
2747c478bd9Sstevel@tonic-gate 		    ACL_IPROP,
2757c478bd9Sstevel@tonic-gate 		    NULL,
2767c478bd9Sstevel@tonic-gate 		    NULL)) {
2777c478bd9Sstevel@tonic-gate 		ret.ret = UPDATE_PERM_DENIED;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
2807c478bd9Sstevel@tonic-gate 				    whoami,
2817c478bd9Sstevel@tonic-gate 				    "<null>", client_name);
2827c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_NOTICE, LOG_UNAUTH, whoami,
2837c478bd9Sstevel@tonic-gate 				"<null>", client_name, service_name,
2847c478bd9Sstevel@tonic-gate 				client_addr(rqstp, abuf));
2857c478bd9Sstevel@tonic-gate 		goto out;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (!getclhoststr(client_name, clhost, sizeof (clhost))) {
2897c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
2907c478bd9Sstevel@tonic-gate 			gettext("%s: getclhoststr failed"),
2917c478bd9Sstevel@tonic-gate 			whoami);
2927c478bd9Sstevel@tonic-gate 		goto out;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * construct db dump file name; kprop style name + clnt fqdn
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpf, "/var/krb5/slave_datatrans_");
2997c478bd9Sstevel@tonic-gate 	if (strlcat(tmpf, clhost, sizeof (tmpf)) >= sizeof (tmpf)) {
3007c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
3017c478bd9Sstevel@tonic-gate 		gettext("%s: db dump file name too long; max length=%d"),
3027c478bd9Sstevel@tonic-gate 				whoami,
3037c478bd9Sstevel@tonic-gate 				(sizeof (tmpf) - 1));
3047c478bd9Sstevel@tonic-gate 		goto out;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * note the -i; modified version of kdb5_util dump format
3097c478bd9Sstevel@tonic-gate 	 * to include sno (serial number)
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (strlcpy(ubuf, KDB5_UTIL_DUMP_STR, sizeof (ubuf)) >=
3127c478bd9Sstevel@tonic-gate 	    sizeof (ubuf)) {
3137c478bd9Sstevel@tonic-gate 		goto out;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	if (strlcat(ubuf, tmpf, sizeof (ubuf)) >= sizeof (ubuf)) {
3167c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
3177c478bd9Sstevel@tonic-gate 		gettext("%s: kdb5 util dump string too long; max length=%d"),
3187c478bd9Sstevel@tonic-gate 				whoami,
3197c478bd9Sstevel@tonic-gate 				(sizeof (ubuf) - 1));
3207c478bd9Sstevel@tonic-gate 		goto out;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * Fork to dump the db and xfer it to the slave.
3257c478bd9Sstevel@tonic-gate 	 * (the fork allows parent to return quickly and the child
3267c478bd9Sstevel@tonic-gate 	 * acts like a callback to the slave).
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 	fret = fork();
3297c478bd9Sstevel@tonic-gate 	DPRINT(("%s: fork=%d (%d)\n", whoami, fret, getpid()));
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	switch (fret) {
3327c478bd9Sstevel@tonic-gate 	case -1: /* error */
3337c478bd9Sstevel@tonic-gate 		if (nofork) {
3347c478bd9Sstevel@tonic-gate 			perror(whoami);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
3377c478bd9Sstevel@tonic-gate 				gettext("%s: fork failed: %s"),
3387c478bd9Sstevel@tonic-gate 				whoami,
3397c478bd9Sstevel@tonic-gate 				error_message(errno));
3407c478bd9Sstevel@tonic-gate 		goto out;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	case 0: /* child */
3437c478bd9Sstevel@tonic-gate 		DPRINT(("%s: run `%s' ...\n", whoami, ubuf));
3444e2a441bSPeter Shoults #ifdef POSIX_SIGNALS
3454e2a441bSPeter Shoults 		(void) sigemptyset(&s_action.sa_mask);
3464e2a441bSPeter Shoults 		s_action.sa_handler = SIG_DFL;
3474e2a441bSPeter Shoults 		(void) sigaction(SIGCHLD, &s_action, (struct sigaction *) NULL);
3484e2a441bSPeter Shoults #else
3497c478bd9Sstevel@tonic-gate 		(void) signal(SIGCHLD, SIG_DFL);
3504e2a441bSPeter Shoults #endif /* POSIX_SIGNALS */
351*bbf21555SRichard Lowe 		/* run kdb5_util(8) dump for IProp */
3527c478bd9Sstevel@tonic-gate 		pret = pclose(popen(ubuf, "w"));
3537c478bd9Sstevel@tonic-gate 		DPRINT(("%s: pclose=%d\n", whoami, pret));
3547c478bd9Sstevel@tonic-gate 		if (pret == -1) {
3557c478bd9Sstevel@tonic-gate 			if (nofork) {
3567c478bd9Sstevel@tonic-gate 				perror(whoami);
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			krb5_klog_syslog(LOG_ERR,
3597c478bd9Sstevel@tonic-gate 				gettext("%s: pclose(popen) failed: %s"),
3607c478bd9Sstevel@tonic-gate 					whoami,
3617c478bd9Sstevel@tonic-gate 					error_message(errno));
3627c478bd9Sstevel@tonic-gate 			goto out;
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		DPRINT(("%s: exec `kprop -f %s %s' ...\n",
3667c478bd9Sstevel@tonic-gate 			whoami, tmpf, clhost));
3677c478bd9Sstevel@tonic-gate 		pret = execl("/usr/lib/krb5/kprop", "kprop", "-f", tmpf,
3687c478bd9Sstevel@tonic-gate 			    clhost, NULL);
3697c478bd9Sstevel@tonic-gate 		if (pret == -1) {
3707c478bd9Sstevel@tonic-gate 			if (nofork) {
3717c478bd9Sstevel@tonic-gate 				perror(whoami);
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 			krb5_klog_syslog(LOG_ERR,
3747c478bd9Sstevel@tonic-gate 					gettext("%s: exec failed: %s"),
3757c478bd9Sstevel@tonic-gate 					whoami,
3767c478bd9Sstevel@tonic-gate 					error_message(errno));
3777c478bd9Sstevel@tonic-gate 			goto out;
3787c478bd9Sstevel@tonic-gate 		}
379674a0a21SToomas Soome 		/* NOTREACHED */
380674a0a21SToomas Soome 		break;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	default: /* parent */
3837c478bd9Sstevel@tonic-gate 		ret.ret = UPDATE_OK;
3847c478bd9Sstevel@tonic-gate 		/* not used by slave (sno is retrieved from kdb5_util dump) */
3857c478bd9Sstevel@tonic-gate 		ret.lastentry.last_sno = 0;
3867c478bd9Sstevel@tonic-gate 		ret.lastentry.last_time.seconds = 0;
3877c478bd9Sstevel@tonic-gate 		ret.lastentry.last_time.useconds = 0;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
3907c478bd9Sstevel@tonic-gate 				whoami,
3917c478bd9Sstevel@tonic-gate 				"<null>", client_name, 0);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_NOTICE, LOG_DONE, whoami,
3947c478bd9Sstevel@tonic-gate 				"<null>",
3957c478bd9Sstevel@tonic-gate 				"success",
3967c478bd9Sstevel@tonic-gate 				client_name, service_name,
3977c478bd9Sstevel@tonic-gate 				client_addr(rqstp, abuf));
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		goto out;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate out:
4037c478bd9Sstevel@tonic-gate 	if (nofork)
4047c478bd9Sstevel@tonic-gate 		debprret(whoami, ret.ret, 0);
4057c478bd9Sstevel@tonic-gate 	if (client_name)
4067c478bd9Sstevel@tonic-gate 		free(client_name);
4077c478bd9Sstevel@tonic-gate 	if (service_name)
4087c478bd9Sstevel@tonic-gate 		free(service_name);
4097c478bd9Sstevel@tonic-gate 	if (name)
4107c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
4117c478bd9Sstevel@tonic-gate 	return (&ret);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate void
krb5_iprop_prog_1(struct svc_req * rqstp,register SVCXPRT * transp)4157c478bd9Sstevel@tonic-gate krb5_iprop_prog_1(
4167c478bd9Sstevel@tonic-gate 	struct svc_req *rqstp,
4177c478bd9Sstevel@tonic-gate 	register SVCXPRT *transp)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	union {
4207c478bd9Sstevel@tonic-gate 		kdb_last_t iprop_get_updates_1_arg;
4217c478bd9Sstevel@tonic-gate 	} argument;
4227c478bd9Sstevel@tonic-gate 	char *result;
4237c478bd9Sstevel@tonic-gate 	bool_t (*_xdr_argument)(), (*_xdr_result)();
4247c478bd9Sstevel@tonic-gate 	char *(*local)();
4257c478bd9Sstevel@tonic-gate 	char *whoami = "krb5_iprop_prog_1";
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_proc) {
4287c478bd9Sstevel@tonic-gate 	case NULLPROC:
4297c478bd9Sstevel@tonic-gate 		(void) svc_sendreply(transp, xdr_void,
4307c478bd9Sstevel@tonic-gate 			(char *)NULL);
4317c478bd9Sstevel@tonic-gate 		return;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	case IPROP_GET_UPDATES:
4347c478bd9Sstevel@tonic-gate 		_xdr_argument = xdr_kdb_last_t;
4357c478bd9Sstevel@tonic-gate 		_xdr_result = xdr_kdb_incr_result_t;
4367c478bd9Sstevel@tonic-gate 		local = (char *(*)()) iprop_get_updates_1;
4377c478bd9Sstevel@tonic-gate 		break;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	case IPROP_FULL_RESYNC:
4407c478bd9Sstevel@tonic-gate 		_xdr_argument = xdr_void;
4417c478bd9Sstevel@tonic-gate 		_xdr_result = xdr_kdb_fullresync_result_t;
4427c478bd9Sstevel@tonic-gate 		local = (char *(*)()) iprop_full_resync_1;
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	default:
4467c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
4477c478bd9Sstevel@tonic-gate 				gettext("RPC unknown request: %d (%s)"),
4487c478bd9Sstevel@tonic-gate 				rqstp->rq_proc, whoami);
4497c478bd9Sstevel@tonic-gate 		svcerr_noproc(transp);
4507c478bd9Sstevel@tonic-gate 		return;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	(void) memset((char *)&argument, 0, sizeof (argument));
4537c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, _xdr_argument, (caddr_t)&argument)) {
4547c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
4557c478bd9Sstevel@tonic-gate 				gettext("RPC svc_getargs failed (%s)"),
4567c478bd9Sstevel@tonic-gate 				whoami);
4577c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
4587c478bd9Sstevel@tonic-gate 		return;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	result = (*local)(&argument, rqstp);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (_xdr_result && result != NULL &&
4637c478bd9Sstevel@tonic-gate 	    !svc_sendreply(transp, _xdr_result, result)) {
4647c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
4657c478bd9Sstevel@tonic-gate 				gettext("RPC svc_sendreply failed (%s)"),
4667c478bd9Sstevel@tonic-gate 				whoami);
4677c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, _xdr_argument, (caddr_t)&argument)) {
4707c478bd9Sstevel@tonic-gate 		krb5_klog_syslog(LOG_ERR,
4717c478bd9Sstevel@tonic-gate 				gettext("RPC svc_freeargs failed (%s)"),
4727c478bd9Sstevel@tonic-gate 				whoami);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		exit(1);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (rqstp->rq_proc == IPROP_GET_UPDATES) {
4787c478bd9Sstevel@tonic-gate 		/* LINTED */
4797c478bd9Sstevel@tonic-gate 		kdb_incr_result_t *r = (kdb_incr_result_t *)result;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		if (r->ret == UPDATE_OK) {
4827c478bd9Sstevel@tonic-gate 			ulog_free_entries(r->updates.kdb_ulog_t_val,
4837c478bd9Sstevel@tonic-gate 					r->updates.kdb_ulog_t_len);
4847c478bd9Sstevel@tonic-gate 			r->updates.kdb_ulog_t_val = NULL;
4857c478bd9Sstevel@tonic-gate 			r->updates.kdb_ulog_t_len = 0;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * Get the host base service name for the kiprop principal. Returns
4937c478bd9Sstevel@tonic-gate  * KADM5_OK on success. Caller must free the storage allocated for
4947c478bd9Sstevel@tonic-gate  * host_service_name.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate kadm5_ret_t
kiprop_get_adm_host_srv_name(krb5_context context,const char * realm,char ** host_service_name)4977c478bd9Sstevel@tonic-gate kiprop_get_adm_host_srv_name(
4987c478bd9Sstevel@tonic-gate 	krb5_context context,
4997c478bd9Sstevel@tonic-gate 	const char *realm,
5007c478bd9Sstevel@tonic-gate 	char **host_service_name)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	kadm5_ret_t ret;
5037c478bd9Sstevel@tonic-gate 	char *name;
5047c478bd9Sstevel@tonic-gate 	char *host;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (ret = kadm5_get_master(context, realm, &host))
5077c478bd9Sstevel@tonic-gate 		return (ret);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	name = malloc(strlen(KIPROP_SVC_NAME)+ strlen(host) + 2);
5107c478bd9Sstevel@tonic-gate 	if (name == NULL) {
5117c478bd9Sstevel@tonic-gate 		free(host);
5127c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "%s@%s", KIPROP_SVC_NAME, host);
5157c478bd9Sstevel@tonic-gate 	free(host);
5167c478bd9Sstevel@tonic-gate 	*host_service_name = name;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	return (KADM5_OK);
5197c478bd9Sstevel@tonic-gate }
520