xref: /titanic_41/usr/src/cmd/krb5/kadmin/server/ovsec_kadmd.c (revision af3025fd013117424096c94bd9e116ecc538cc6b)
1 /*
2  * Copyright 2007 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  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10  *
11  *	Openvision retains the copyright to derivative works of
12  *	this source code.	Do *NOT* create a derivative of this
13  *	source code before consulting with your legal department.
14  *	Do *NOT* integrate *ANY* of this source code into another
15  *	product before consulting with your legal department.
16  *
17  *	For further information, read the top-level Openvision
18  *	copyright which is contained in the top-level MIT Kerberos
19  *	copyright.
20  *
21  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
22  *
23  */
24 
25 /*
26  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
27  */
28 
29 /*
30  * Copyright (C) 1998 by the FundsXpress, INC.
31  *
32  * All rights reserved.
33  *
34  * Export of this software from the United States of America may require
35  * a specific license from the United States Government.  It is the
36  * responsibility of any person or organization contemplating export to
37  * obtain such a license before exporting.
38  *
39  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
40  * distribute this software and its documentation for any purpose and
41  * without fee is hereby granted, provided that the above copyright
42  * notice appear in all copies and that both that copyright notice and
43  * this permission notice appear in supporting documentation, and that
44  * the name of FundsXpress. not be used in advertising or publicity pertaining
45  * to distribution of the software without specific, written prior
46  * permission.  FundsXpress makes no representations about the suitability of
47  * this software for any purpose.  It is provided "as is" without express
48  * or implied warranty.
49  *
50  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
51  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
52  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
53  */
54 
55 
56 /*
57  * SUNWresync121 XXX
58  * Beware future resyncers, this file is much diff from MIT (1.0...)
59  */
60 
61 #include    <stdio.h>
62 #include    <stdio_ext.h>
63 #include    <signal.h>
64 #include    <syslog.h>
65 #include    <sys/types.h>
66 #ifdef _AIX
67 #include    <sys/select.h>
68 #endif
69 #include    <sys/time.h>
70 #include    <sys/socket.h>
71 #include    <unistd.h>
72 #include    <netinet/in.h>
73 #include    <arpa/inet.h>  /* inet_ntoa */
74 #include    <gssapi/gssapi.h>
75 #include    <rpc/rpc.h>
76 #include    <kadm5/admin.h>
77 #include    <kadm5/kadm_rpc.h>
78 #include    <server_acl.h>
79 #include    <krb5/adm_proto.h>
80 #include    <string.h>
81 #include    <kadm5/server_internal.h>
82 #include    <gssapi_krb5.h>
83 #include    <libintl.h>
84 #include    <locale.h>
85 #include    <sys/resource.h>
86 #include    <kdb/kdb_log.h>
87 #include    <kdb/kdb_kt.h>
88 
89 #include <rpc/rpcsec_gss.h>
90 #include    "misc.h"
91 
92 #ifndef	FD_SETSIZE
93 #define	FD_SETSIZE	256
94 #endif
95 
96 #ifndef MAX
97 #define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
98 #endif
99 
100 #if defined(NEED_DAEMON_PROTO)
101 extern int daemon(int, int);
102 #endif
103 
104 
105 
106 static int signal_request_exit = 0;
107 static int schpw;
108 kadm5_config_params chgpw_params;
109 void kadm_svc_run(void);
110 void setup_signal_handlers(iprop_role iproprole);
111 void sig_exit(int);
112 void sig_pipe(int);
113 krb5_error_code log_kt_error(char*, char*);
114 
115 #ifdef POSIX_SIGNALS
116 static struct sigaction s_action;
117 #endif /* POSIX_SIGNALS */
118 
119 
120 #define	TIMEOUT	15
121 
122 typedef struct _auth_gssapi_name {
123 	char *name;
124 	gss_OID type;
125 } auth_gssapi_name;
126 
127 gss_name_t gss_changepw_name = NULL, gss_oldchangepw_name = NULL;
128 void *global_server_handle;
129 
130 /*
131  * This is a kludge, but the server needs these constants to be
132  * compatible with old clients.  They are defined in <kadm5/admin.h>,
133  * but only if USE_KADM5_API_VERSION == 1.
134  */
135 #define	OVSEC_KADM_ADMIN_SERVICE_P	"ovsec_adm@admin"
136 #define	OVSEC_KADM_CHANGEPW_SERVICE_P	"ovsec_adm@changepw"
137 
138 /*
139  * This enables us to set the keytab that gss_acquire_cred uses, but
140  * it also restricts us to linking against the Kv5 GSS-API library.
141  * Since this is *k*admind, that shouldn't be a problem.
142  */
143 extern char *krb5_overridekeyname;
144 
145 extern void krb5_iprop_prog_1();
146 extern kadm5_ret_t kiprop_get_adm_host_srv_name(
147 	krb5_context,
148 	const char *,
149 	char **);
150 
151 static krb5_context context;  /* XXX yuck.  the signal handlers need this */
152 
153 static krb5_context hctx;
154 
155 in_port_t l_port = 0;	/* global local port num, for BSM audits */
156 
157 int nofork = 0; /* global; don't fork (debug mode) */
158 
159 
160 /*
161  * Function: usage
162  *
163  * Purpose: print out the server usage message
164  *
165  * Arguments:
166  * Requires:
167  * Effects:
168  * Modifies:
169  */
170 
171 static void usage()
172 {
173 	fprintf(stderr, gettext("Usage: kadmind [-r realm] [-m] [-d] "
174 	    "[-p port-number]\n"));
175 	exit(1);
176 }
177 
178 /*
179  * Function: display_status
180  *
181  * Purpose: displays GSS-API messages
182  *
183  * Arguments:
184  *
185  * 	msg		a string to be displayed with the message
186  * 	maj_stat	the GSS-API major status code
187  * 	min_stat	the GSS-API minor status code
188  *
189  * Effects:
190  *
191  * The GSS-API messages associated with maj_stat and min_stat are
192  * displayed on stderr, each preceeded by "GSS-API error <msg>: " and
193  * followed by a newline.
194  */
195 static void display_status_1(char *, OM_uint32, int);
196 
197 static void display_status(msg, maj_stat, min_stat)
198      char *msg;
199      OM_uint32 maj_stat;
200      OM_uint32 min_stat;
201 {
202      display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
203      display_status_1(msg, min_stat, GSS_C_MECH_CODE);
204 }
205 
206 static void display_status_1(m, code, type)
207      char *m;
208      OM_uint32 code;
209      int type;
210 {
211 	OM_uint32 maj_stat, min_stat;
212 	gss_buffer_desc msg;
213 	OM_uint32 msg_ctx;
214 
215 	msg_ctx = 0;
216 	while (1) {
217 		maj_stat = gss_display_status(&min_stat, code,
218 					      type, GSS_C_NULL_OID,
219 					      &msg_ctx, &msg);
220 		fprintf(stderr, "GSS-API error %s: %s\n", m,
221 			(char *)msg.value);
222 		(void) gss_release_buffer(&min_stat, &msg);
223 
224 		if (!msg_ctx)
225 			break;
226 	}
227 }
228 
229 
230 /*
231  * Solaris Kerberos: the following prototypes are needed because these are
232  * private interfaces that do not have prototypes in any .h
233  */
234 
235 extern struct hostent   *res_getipnodebyaddr(const void *, size_t, int, int *);
236 extern void             res_freehostent(struct hostent *);
237 
238 static void
239 freedomnames(char **npp)
240 {
241 	char **tpp;
242 
243 	if (npp) {
244 		tpp = npp;
245 		while (*tpp++) {
246 			free(*(tpp-1));
247 		}
248 		free(npp);
249 	}
250 }
251 
252 /*
253  * Construct a list of uniq FQDNs of all the net interfaces (except
254  * krb5.conf master dups) and return it in arg 'dnames'.
255  *
256  * On successful return (0), caller must call freedomnames()
257  * to free memory.
258  */
259 static int
260 getdomnames(krb5_context ctx, char *realm, char ***dnames)
261 {
262 	krb5_address **addresses = NULL;
263 	krb5_address *a = NULL;
264 	struct hostent *hp = NULL;
265 	int ret, i, result=0, error;
266 	char **npp = NULL, **tpp=NULL;
267 	int dup=0, n = 0;
268 	char *cfhost = NULL; /* krb5 conf file master hostname */
269 
270 	if (ret = kadm5_get_master(ctx, realm, &cfhost)) {
271 		return (ret);
272 	}
273 
274 	ret = krb5_os_localaddr(ctx, &addresses);
275 	if (ret != 0) {
276 		if (nofork)
277 			(void) fprintf(stderr,
278 				    "kadmind: get localaddrs failed: %s",
279 				    error_message(ret));
280 		result = ret;
281 		goto err;
282 	}
283 
284 
285 	for (i=0; addresses[i]; i++) {
286 		a = addresses[i];
287 		hp = res_getipnodebyaddr(a->contents, a->length,
288 					a->addrtype == ADDRTYPE_INET
289 					? AF_INET : AF_INET6,
290 					&error);
291 		if (hp != NULL) {
292 
293 			/* skip master host in krb5.conf */
294 			if (strcasecmp(cfhost, hp->h_name) == 0) {
295 				res_freehostent(hp);
296 				hp = NULL;
297 				continue;
298 			}
299 
300 			dup = 0;
301 			tpp = npp;
302 			/* skip if hostname already exists in list */
303 			while (tpp && *tpp++) {
304 				if (strcasecmp(*(tpp-1), hp->h_name) == 0) {
305 					dup++;
306 					break;
307 				}
308 			}
309 
310 			if (dup) {
311 				res_freehostent(hp);
312 				hp = NULL;
313 				continue;
314 			}
315 
316 			npp = realloc(npp, sizeof(char *) * (n + 2));
317 			if (!npp) {
318 				result = ENOMEM;
319 				goto err;
320 			}
321 			npp[n] = strdup(hp->h_name);
322 			if (!npp[n]) {
323 				result = ENOMEM;
324 				goto err;
325 			}
326 			npp[n+1] = NULL;
327 			n++;
328 
329 			res_freehostent(hp);
330 			hp = NULL;
331 			result = 0;
332 		}
333 
334 	}
335 
336 #ifdef DEBUG
337 	printf("getdomnames: n=%d, i=%d, npp=%p\n", n, i, npp);
338 	tpp = npp;
339 	while (tpp && *tpp++) {
340 		printf("tpp=%s\n", *(tpp-1));
341 	}
342 #endif
343 
344 	goto out;
345 
346 err:
347 	if (npp) {
348 		freedomnames(npp);
349 		npp = NULL;
350 	}
351 
352 	if (hp) {
353 		res_freehostent(hp);
354 		hp = NULL;
355 	}
356 
357 out:
358 	if (cfhost) {
359 		free (cfhost);
360 		cfhost = NULL;
361 	}
362 	if (addresses) {
363 		krb5_free_addresses(ctx, addresses);
364 		addresses = NULL;
365 	}
366 
367 	if (result == 0)
368 		*dnames = npp;
369 
370 	return (result);
371 }
372 
373 /*
374  * Set the rpcsec_gss svc names for all net interfaces.
375  */
376 static void
377 set_svc_domnames(char *svcname, char **dnames,
378 		u_int program, u_int version)
379 {
380 	bool_t ret;
381 	char **tpp = dnames;
382 
383 	if (!tpp)
384 		return;
385 
386 	while (*tpp++) {
387 		/* MAX_NAME_LEN from rpc/rpcsec_gss.h */
388 		char name[MAXHOSTNAMELEN+MAX_NAME_LEN+2] = {0};
389 		(void) snprintf(name, sizeof(name), "%s@%s",
390 				svcname, *(tpp-1));
391 		ret = rpc_gss_set_svc_name(name,
392 					"kerberos_v5", 0,
393 					program, version);
394 		if (nofork && ret)
395 			(void) fprintf(stderr,
396 				    "rpc_gss_set_svc_name success: %s\n",
397 				    name);
398 	}
399 }
400 
401 
402 
403 
404 int
405 main(int argc, char *argv[])
406 {
407 	SVCXPRT *transp;
408 	extern char *optarg;
409 	extern int optind, opterr;
410 	int ret, rlen, oldnames = 0;
411 	OM_uint32 OMret, major_status, minor_status;
412 	char *whoami;
413 	FILE *acl_file;
414 	gss_buffer_desc in_buf;
415 	struct servent *srv;
416 	struct sockaddr_in addr;
417 	struct sockaddr_in *sin;
418 	int s;
419 	int optchar;
420 	struct netconfig *nconf;
421 	void *handlep;
422 	int fd;
423 	struct t_info tinfo;
424 	struct t_bind tbindstr, *tres;
425 
426 	struct t_optmgmt req, resp;
427 	struct opthdr *opt;
428 	char reqbuf[128];
429 	struct rlimit rl;
430 
431 	char *kiprop_name = NULL; /* IProp svc name */
432 	kdb_log_context *log_ctx;
433 	kadm5_server_handle_t handle;
434 	krb5_context ctx;
435 
436 	kadm5_config_params params;
437 	auth_gssapi_name names[6];
438      	gss_buffer_desc gssbuf;
439      	gss_OID nt_krb5_name_oid;
440 
441 	char **dnames = NULL;
442 	int retdn;
443 
444 	/* This is OID value the Krb5_Name NameType */
445      	gssbuf.value = "{1 2 840 113554 1 2 2 1}";
446      	gssbuf.length = strlen(gssbuf.value);
447      	major_status = gss_str_to_oid(&minor_status, &gssbuf,
448 				    &nt_krb5_name_oid);
449      	if (major_status != GSS_S_COMPLETE) {
450 		fprintf(stderr,
451 			gettext("Couldn't create KRB5 Name NameType OID\n"));
452 		display_status("str_to_oid", major_status, minor_status);
453 		exit(1);
454      	}
455 
456 	names[0].name = names[1].name = names[2].name =
457 		names[3].name = names[4].name  = names[5].name =NULL;
458 	names[0].type = names[1].type = names[2].type =
459 		names[3].type = names[4].type = names[5].type =
460 		(gss_OID) nt_krb5_name_oid;
461 
462 	whoami = (strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0]);
463 
464 	(void) setlocale(LC_ALL, "");
465 
466 #if !defined(TEXT_DOMAIN)  /* Should be defined by cc -D */
467 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
468 #endif
469 
470 	(void) textdomain(TEXT_DOMAIN);
471 
472 	nofork = 0;
473 
474 	memset((char *) &params, 0, sizeof (params));
475 
476 	while ((optchar = getopt(argc, argv, "r:mdp:")) != EOF) {
477 		switch (optchar) {
478 		case 'r':
479 			if (!optarg)
480 				usage();
481 			params.realm = optarg;
482 			params.mask |= KADM5_CONFIG_REALM;
483 			break;
484 		case 'm':
485 			params.mkey_from_kbd = 1;
486 			params.mask |= KADM5_CONFIG_MKEY_FROM_KBD;
487 			break;
488 		case 'd':
489 			nofork = 1;
490 			break;
491 		case 'p':
492 			if (!optarg)
493 				usage();
494 			params.kadmind_port = atoi(optarg);
495 			params.mask |= KADM5_CONFIG_KADMIND_PORT;
496 			break;
497 		case '?':
498 		default:
499 			usage();
500 		}
501 	}
502 
503 
504 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
505 		rl.rlim_cur = rl.rlim_max = MAX(rl.rlim_max, FD_SETSIZE);
506 		(void) setrlimit(RLIMIT_NOFILE, &rl);
507 		(void) enable_extended_FILE_stdio(-1, -1);
508 	}
509 
510 	if (ret = krb5_init_context(&context)) {
511 		fprintf(stderr,
512 		    gettext("%s: %s while initializing context, aborting\n"),
513 		    whoami, error_message(ret));
514 		exit(1);
515 	}
516 
517 	krb5_klog_init(context, "admin_server", whoami, 1);
518     /* SUNW14resync */
519 #if 0
520     krb5_klog_syslog(LOG_INFO, "Seeding random number generator");
521           ret = krb5_c_random_os_entropy(context, 1, NULL);
522 	if(ret) {
523 	krb5_klog_syslog(LOG_ERR, "Error getting random seed: %s, aborting",
524 			     error_message(ret));
525 	exit(1);
526 	}
527 #endif
528 
529 	/*
530 	 * When using the Horowitz/IETF protocol for
531 	 * password changing, the default port is 464
532 	 * (officially recognized by IANA)
533 	 *
534 	 * DEFAULT_KPASSWD_PORT -> 464
535 	 */
536 	chgpw_params.kpasswd_port = DEFAULT_KPASSWD_PORT;
537 	chgpw_params.mask |= KADM5_CONFIG_KPASSWD_PORT;
538 	chgpw_params.kpasswd_protocol = KRB5_CHGPWD_CHANGEPW_V2;
539 	chgpw_params.mask |= KADM5_CONFIG_KPASSWD_PROTOCOL;
540 
541 	if (ret = kadm5_get_config_params(context, NULL, NULL, &chgpw_params,
542 		&chgpw_params)) {
543 		krb5_klog_syslog(LOG_ERR, gettext("%s: %s while initializing,"
544 				" aborting"), whoami, error_message(ret));
545 		fprintf(stderr,
546 		    gettext("%s: %s while initializing, aborting\n"),
547 		    whoami, error_message(ret));
548 		krb5_klog_close(context);
549 		exit(1);
550 	}
551 
552 	/*
553 	 * We now setup the socket and bind() to port 464, so that
554 	 * kadmind can now listen to and process change-pwd requests
555 	 * from non-Solaris Kerberos V5 clients such as Microsoft,
556 	 * MIT, AIX, HP etc
557 	 */
558 	if ((schpw = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
559 		krb5_klog_syslog(LOG_ERR, gettext( "cannot create simple "
560 				"chpw socket: %s"), error_message(errno));
561 		fprintf(stderr, gettext("Cannot create simple chpw "
562 					"socket: %s"), error_message(errno));
563 		krb5_klog_close(context);
564 		exit(1);
565 	}
566 
567 	memset(&addr, 0, sizeof(addr));
568 	addr.sin_family = AF_INET;
569 	addr.sin_addr.s_addr = INADDR_ANY;
570 	addr.sin_port = htons(chgpw_params.kpasswd_port);
571 
572 	if (bind(schpw, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
573 		char portbuf[32];
574 		int oerrno = errno;
575 		fprintf(stderr, gettext("%s: Cannot bind socket.\n"), whoami);
576 		fprintf(stderr, gettext("bind: %s\n"), error_message(oerrno));
577 		errno = oerrno;
578 		(void) snprintf(portbuf, sizeof (portbuf), "%d",
579 				ntohs(addr.sin_port));
580 		krb5_klog_syslog(LOG_ERR, gettext("cannot bind simple "
581 				"chpw socket: %s"), error_message(oerrno));
582 		if(oerrno == EADDRINUSE) {
583 			char *w = strrchr(whoami, '/');
584 			if (w) {
585 				w++;
586 			}
587 			else {
588 				w = whoami;
589 			}
590 			fprintf(stderr, gettext(
591 				"This probably means that another %s process\n"
592 				"is already running, or that another program\n"
593 				"is using the server port (number %d).\n"
594 				"If another %s is already running, you should\n"
595 				"kill it before restarting the server.\n"),
596 				w, ntohs(addr.sin_port), w);
597 		}
598 		krb5_klog_close(context);
599 		exit(1);
600 	}
601 
602 	if (ret = kadm5_get_config_params(context, NULL, NULL, &params,
603 		&params)) {
604 		krb5_klog_syslog(LOG_ERR, gettext("%s: %s while initializing,"
605 				" aborting"), whoami, error_message(ret));
606 		fprintf(stderr,
607 		    gettext("%s: %s while initializing, aborting\n"),
608 		    whoami, error_message(ret));
609 		krb5_klog_close(context);
610 		exit(1);
611 	}
612 #define	REQUIRED_PARAMS (KADM5_CONFIG_REALM | KADM5_CONFIG_ACL_FILE)
613 
614 	if ((params.mask & REQUIRED_PARAMS) != REQUIRED_PARAMS) {
615 		krb5_klog_syslog(LOG_ERR,
616 		    gettext("%s: Missing required configuration values "
617 			"while initializing, aborting"), whoami,
618 		    (params.mask & REQUIRED_PARAMS) ^ REQUIRED_PARAMS);
619 		fprintf(stderr,
620 		    gettext("%s: Missing required configuration values "
621 			"(%lx) while initializing, aborting\n"), whoami,
622 		    (params.mask & REQUIRED_PARAMS) ^ REQUIRED_PARAMS);
623 		krb5_klog_close(context);
624 		exit(1);
625 	}
626 	memset((char *) &addr, 0, sizeof (struct sockaddr_in));
627 	addr.sin_family = AF_INET;
628 	addr.sin_addr.s_addr = INADDR_ANY;
629 	l_port = addr.sin_port = htons(params.kadmind_port);
630 	sin = &addr;
631 
632 	if ((handlep = setnetconfig()) == (void *) NULL) {
633 		(void) krb5_klog_syslog(LOG_ERR,
634 		    gettext("cannot get any transport information"));
635 		krb5_klog_close(context);
636 		exit(1);
637 	}
638 	while (nconf = getnetconfig(handlep)) {
639 		if ((nconf->nc_semantics == NC_TPI_COTS_ORD) &&
640 		    (strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
641 		    (strcmp(nconf->nc_proto, NC_TCP) == 0))
642 			break;
643 	}
644 
645 	if (nconf == (struct netconfig *) NULL) {
646 		(void) endnetconfig(handlep);
647 		krb5_klog_close(context);
648 		exit(1);
649 	}
650 	fd = t_open(nconf->nc_device, O_RDWR, &tinfo);
651 	if (fd == -1) {
652 		krb5_klog_syslog(LOG_ERR,
653 		    gettext("unable to open connection for ADMIN server"));
654 		krb5_klog_close(context);
655 		exit(1);
656 	}
657 	/* LINTED */
658 	opt = (struct opthdr *) reqbuf;
659 	opt->level = SOL_SOCKET;
660 	opt->name = SO_REUSEADDR;
661 	opt->len = sizeof (int);
662 
663 	/*
664 	 * The option value is "1".  This will allow the server to restart
665 	 * whilst the previous process is cleaning up after itself in a
666 	 * FIN_WAIT_2 or TIME_WAIT state.  If another process is started
667 	 * outside of smf(5) then bind will fail anyway, which is what we want.
668 	 */
669 	reqbuf[sizeof (struct opthdr)] = 1;
670 
671 	req.flags = T_NEGOTIATE;
672 	req.opt.len = sizeof (struct opthdr) + opt->len;
673 	req.opt.buf = (char *) opt;
674 
675 	resp.flags = 0;
676 	resp.opt.buf = reqbuf;
677 	resp.opt.maxlen = sizeof (reqbuf);
678 
679 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
680 		t_error("t_optmgmt");
681 		exit(1);
682 	}
683 	/* Transform addr to netbuf */
684 
685 	tres = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
686 	if (tres == NULL) {
687 		(void) t_close(fd);
688 		(void) krb5_klog_syslog(LOG_ERR,
689 					gettext("cannot allocate netbuf"));
690 		krb5_klog_close(context);
691 		exit(1);
692 	}
693 	tbindstr.qlen = 8;
694 	tbindstr.addr.buf = (char *) sin;
695 	tbindstr.addr.len = tbindstr.addr.maxlen = __rpc_get_a_size(tinfo.addr);
696 	sin = (struct sockaddr_in *) tbindstr.addr.buf;
697 	/* SUNWresync121 XXX (void) memset(&addr, 0, sizeof(addr)); */
698 
699 	if (t_bind(fd, &tbindstr, tres) < 0) {
700 		int oerrno = errno;
701 		fprintf(stderr, gettext("%s: Cannot bind socket.\n"), whoami);
702 		fprintf(stderr, gettext("bind: %s\n"), error_message(oerrno));
703 		errno = oerrno;
704 		krb5_klog_syslog(LOG_ERR, gettext("Cannot bind socket: %s"),
705 		    error_message(errno));
706 		if (oerrno == EADDRINUSE) {
707 			char *w = strrchr(whoami, '/');
708 
709 			if (w) {
710 				w++;
711 			} else {
712 				w = whoami;
713 			}
714 			fprintf(stderr, gettext(
715 				"This probably means that another %s "
716 				"process is already\n"
717 				"running, or that another program is using "
718 				"the server port (number %d)\n"
719 				"after being assigned it by the RPC "
720 				"portmap deamon.	If another\n"
721 				"%s is already running, you should kill "
722 				"it before\n"
723 				"restarting the server. If, on the other hand, "
724 				"another program is\n"
725 				"using the server port, you should kill it "
726 				"before running\n"
727 				"%s, and ensure that the conflict does "
728 				"not occur in the\n"
729 				"future by making sure that %s is started "
730 				"on reboot\n"
731 				"before portmap.\n"),
732 			    w, ntohs(addr.sin_port), w, w, w);
733 			krb5_klog_syslog(LOG_ERR,
734 			    gettext("Check for already-running %s or for "
735 				"another process using port %d"), w,
736 			    htons(addr.sin_port));
737 		}
738 		krb5_klog_close(context);
739 		exit(1);
740 	}
741 	transp = svc_tli_create(fd, nconf, NULL, 0, 0);
742 	(void) t_free((char *) tres, T_BIND);
743 	if (transp == NULL) {
744 		fprintf(stderr, gettext("%s: Cannot create RPC service.\n"),
745 			whoami);
746 		krb5_klog_syslog(LOG_ERR, gettext("Cannot create RPC service: %m"));
747 		krb5_klog_close(context);
748 		exit(1);
749 	}
750 	if (!svc_register(transp, KADM, KADMVERS, kadm_1, 0)) {
751 		fprintf(stderr,
752 		    gettext("%s: Cannot register RPC service.\n"), whoami);
753 		krb5_klog_syslog(LOG_ERR,
754 		    gettext("Cannot register RPC service, failing."));
755 		krb5_klog_close(context);
756 		exit(1);
757 	}
758 
759 	/*
760 	 * XXX krb5_defkeyname is an internal library global and should go
761 	 * away
762 	 */
763 	krb5_overridekeyname = params.admin_keytab;
764 
765 	/* Solaris Kerberos:
766 	 * The only service principals which matter here are
767 	 *  -> names[0].name (kadmin/<fqdn>)
768 	 *  -> names[1].name (changepw/<fqdn>)
769 	 * KADM5_ADMIN_SERVICE_P, KADM5_CHANGEPW_SERVICE_P,
770 	 * OVSEC_KADM_ADMIN_SERVICE_P, OVSEC_KADM_CHANGEPW_SERVICE_P
771 	 * are all legacy service princs and calls to rpc_gss_set_svc_name()
772 	 * using these principals will always fail as they are not host
773 	 * based principals.
774 	 */
775 
776 	(void) kadm5_get_adm_host_srv_name(context,
777 					   params.realm, &names[0].name);
778 	(void) kadm5_get_cpw_host_srv_name(context,
779 					   params.realm, &names[1].name);
780 	names[2].name = KADM5_ADMIN_SERVICE_P;
781 	names[3].name = KADM5_CHANGEPW_SERVICE_P;
782 	names[4].name = OVSEC_KADM_ADMIN_SERVICE_P;
783 	names[5].name = OVSEC_KADM_CHANGEPW_SERVICE_P;
784 
785 	if (names[0].name == NULL || names[1].name == NULL ||
786 	    names[2].name == NULL || names[3].name == NULL ||
787 	    names[4].name == NULL || names[5].name == NULL) {
788 		krb5_klog_syslog(LOG_ERR,
789 		    gettext("Cannot initialize GSS-API authentication, "
790 			"failing."));
791 		fprintf(stderr,
792 		    gettext("%s: Cannot initialize "
793 			"GSS-API authentication.\n"),
794 		    whoami);
795 		krb5_klog_close(context);
796 		exit(1);
797 	}
798 
799 
800 	/*
801 	 * Try to acquire creds for the old OV services as well as the new
802 	 * names, but if that fails just fall back on the new names.
803 	 */
804 
805 	if (rpc_gss_set_svc_name(names[5].name,
806 				"kerberos_v5", 0, KADM, KADMVERS) &&
807 	    rpc_gss_set_svc_name(names[4].name,
808 				"kerberos_v5", 0, KADM, KADMVERS))
809 		oldnames++;
810 	if (rpc_gss_set_svc_name(names[3].name,
811 				"kerberos_v5", 0, KADM, KADMVERS))
812 		oldnames++;
813 	if (rpc_gss_set_svc_name(names[2].name,
814 				"kerberos_v5", 0, KADM, KADMVERS))
815 		oldnames++;
816 
817     /* If rpc_gss_set_svc_name() fails for either kadmin/<fqdn> or
818      * for changepw/<fqdn> then try to determine if this is caused
819      * by a missing keytab file or entry. If so, log it and continue.
820      */
821 	if (rpc_gss_set_svc_name(names[0].name,
822 				"kerberos_v5", 0, KADM, KADMVERS))
823 		oldnames++;
824 	else
825 		log_kt_error(names[0].name, whoami);
826 	if (rpc_gss_set_svc_name(names[1].name,
827 				"kerberos_v5", 0, KADM, KADMVERS))
828 		oldnames++;
829 	else
830 		log_kt_error(names[1].name, whoami);
831 
832 	retdn = getdomnames(context, params.realm, &dnames);
833 	if (retdn == 0 && dnames) {
834 		/*
835 		 * Multi-homed KDCs sometimes may need to set svc names
836 		 * for multiple net interfaces so we set them for
837 		 * all interfaces just in case.
838 		 */
839 		set_svc_domnames(KADM5_ADMIN_HOST_SERVICE,
840 				dnames, KADM, KADMVERS);
841 		set_svc_domnames(KADM5_CHANGEPW_HOST_SERVICE,
842 				dnames, KADM, KADMVERS);
843 	}
844 
845 	/* if set_names succeeded, this will too */
846 	in_buf.value = names[1].name;
847 	in_buf.length = strlen(names[1].name) + 1;
848 	(void) gss_import_name(&OMret, &in_buf, (gss_OID) nt_krb5_name_oid,
849 	    &gss_changepw_name);
850 	if (oldnames) {
851 		in_buf.value = names[3].name;
852 		in_buf.length = strlen(names[3].name) + 1;
853 		(void) gss_import_name(&OMret, &in_buf,
854 					(gss_OID) nt_krb5_name_oid,
855 					&gss_oldchangepw_name);
856 	}
857 	if (ret = kadm5int_acl_init(context, 0, params.acl_file)) {
858 		krb5_klog_syslog(LOG_ERR, gettext("Cannot initialize acl file: %s"),
859 		    error_message(ret));
860 		fprintf(stderr, gettext("%s: Cannot initialize acl file: %s\n"),
861 		    whoami, error_message(ret));
862 		krb5_klog_close(context);
863 		exit(1);
864 	}
865 	if ((ret = kadm5_init("kadmind", NULL,
866 		    NULL, &params,
867 		    KADM5_STRUCT_VERSION,
868 		    KADM5_API_VERSION_2,
869 		    &global_server_handle)) != KADM5_OK) {
870 		krb5_klog_syslog(LOG_ERR,
871 		    gettext("%s while initializing, aborting"),
872 		    error_message(ret));
873 		fprintf(stderr,
874 		    gettext("%s: %s while initializing, aborting\n"),
875 		    whoami, error_message(ret));
876 		krb5_klog_close(context);
877 		exit(1);
878 	}
879 
880 	if (!nofork && (ret = daemon(0, 0))) {
881 		ret = errno;
882 		krb5_klog_syslog(LOG_ERR,
883 		    gettext("Cannot detach from tty: %s"),
884 		    error_message(ret));
885 		fprintf(stderr, gettext("%s: Cannot detach from tty: %s\n"),
886 		    whoami, error_message(ret));
887 		krb5_klog_close(context);
888 		exit(1);
889 	}
890 
891 	handle = global_server_handle;
892 	ctx = handle->context;
893 	if (params.iprop_enabled == TRUE)
894 		ulog_set_role(ctx, IPROP_MASTER);
895 	else
896 		ulog_set_role(ctx, IPROP_NULL);
897 
898 	log_ctx = ctx->kdblog_context;
899 
900 	if (log_ctx && (log_ctx->iproprole == IPROP_MASTER)) {
901 		/*
902 		 * IProp is enabled, so let's map in the update log
903 		 * and setup the service.
904 		 */
905 		if (ret = ulog_map(ctx, &params, FKADMIND)) {
906 			fprintf(stderr,
907 				gettext("%s: %s while mapping update log "
908 				"(`%s.ulog')\n"), whoami, error_message(ret),
909 				params.dbname);
910 			krb5_klog_syslog(LOG_ERR,
911 				gettext("%s while mapping update log "
912 				"(`%s.ulog')"), error_message(ret),
913 				params.dbname);
914 			krb5_klog_close(ctx);
915 			exit(1);
916 		}
917 
918 
919 		if (nofork)
920 			fprintf(stderr,
921 				"%s: create IPROP svc (PROG=%d, VERS=%d)\n",
922 				whoami, KRB5_IPROP_PROG, KRB5_IPROP_VERS);
923 
924 		if (!svc_create(krb5_iprop_prog_1,
925 				KRB5_IPROP_PROG, KRB5_IPROP_VERS,
926 				"circuit_v")) {
927 			fprintf(stderr,
928     gettext("%s: Cannot create IProp RPC service (PROG=%d, VERS=%d)\n"),
929 				whoami,
930 				KRB5_IPROP_PROG, KRB5_IPROP_VERS);
931 			krb5_klog_syslog(LOG_ERR,
932     gettext("Cannot create IProp RPC service (PROG=%d, VERS=%d), failing."),
933 					KRB5_IPROP_PROG, KRB5_IPROP_VERS);
934 			krb5_klog_close(ctx);
935 			exit(1);
936 		}
937 
938 		if (ret = kiprop_get_adm_host_srv_name(ctx,
939 							params.realm,
940 							&kiprop_name)) {
941 			krb5_klog_syslog(LOG_ERR,
942 			gettext("%s while getting IProp svc name, failing"),
943 					error_message(ret));
944 			fprintf(stderr,
945 		gettext("%s: %s while getting IProp svc name, failing\n"),
946 				whoami, error_message(ret));
947 			krb5_klog_close(ctx);
948 			exit(1);
949 		}
950 
951 		if (!rpc_gss_set_svc_name(kiprop_name, "kerberos_v5", 0,
952 					KRB5_IPROP_PROG, KRB5_IPROP_VERS)) {
953 			rpc_gss_error_t err;
954 			(void) rpc_gss_get_error(&err);
955 
956 		/* Try to determine if the error was caused by a missing keytab or
957 		 * missing keytab entries (and log it).
958 		 */
959 			log_kt_error(kiprop_name, whoami);
960 			krb5_klog_syslog(LOG_ERR,
961     gettext("Unable to set RPCSEC_GSS service name (`%s'), failing."),
962 					kiprop_name ? kiprop_name : "<null>");
963 			fprintf(stderr,
964     gettext("%s: Unable to set RPCSEC_GSS service name (`%s'), failing.\n"),
965 				whoami,
966 				kiprop_name ? kiprop_name : "<null>");
967 
968 			if (nofork) {
969 				fprintf(stderr,
970 			"%s: set svc name (rpcsec err=%d, sys err=%d)\n",
971 					whoami,
972 					err.rpc_gss_error,
973 					err.system_error);
974 			}
975 
976 			exit(1);
977 		}
978 		free(kiprop_name);
979 
980 		if (retdn == 0 && dnames) {
981 			set_svc_domnames(KADM5_KIPROP_HOST_SERVICE,
982 					dnames,
983 					KRB5_IPROP_PROG, KRB5_IPROP_VERS);
984 		}
985 
986 	} else {
987 		if (!oldnames) {
988 		/* rpc_gss_set_svc_name failed for both kadmin/<fqdn> and
989 		 * changepw/<fqdn>.
990 		 */
991 			krb5_klog_syslog(LOG_ERR,
992 					gettext("Unable to set RPCSEC_GSS service names "
993 						"('%s, %s')"),
994 					names[0].name, names[1].name);
995 			fprintf(stderr,
996 					gettext("%s: Unable to set RPCSEC_GSS service names "
997 						"('%s, %s')\n"),
998 					whoami,
999 					names[0].name, names[1].name);
1000 			krb5_klog_close(context);
1001 			exit(1);
1002 		}
1003 	}
1004 
1005 	if (dnames)
1006 		freedomnames(dnames);
1007 
1008 	setup_signal_handlers(log_ctx->iproprole);
1009 	krb5_klog_syslog(LOG_INFO, gettext("starting"));
1010 	if (nofork)
1011 		fprintf(stderr, "%s: starting...\n", whoami);
1012 
1013 
1014 	/*
1015 	 * We now call our own customized async event processing
1016 	 * function kadm_svc_run(), as opposed to svc_run() earlier,
1017 	 * since this enables kadmind to also listen-to/process
1018 	 * non-RPCSEC_GSS based change-pwd requests apart from the
1019 	 * regular, RPCSEC_GSS kpasswd requests from Solaris Krb5 clients.
1020 	 */
1021 	kadm_svc_run();
1022 
1023 	krb5_klog_syslog(LOG_INFO, gettext("finished, exiting"));
1024 	kadm5_destroy(global_server_handle);
1025 	t_close(fd);
1026 	krb5_klog_close(context);
1027 	exit(0);
1028 }
1029 
1030 
1031 /*
1032  * Function: kadm_svc_run
1033  *
1034  * Purpose: modified version of sunrpc svc_run.
1035  *	    which closes the database every TIMEOUT seconds.
1036  *
1037  * Arguments:
1038  * Requires:
1039  * Effects:
1040  * Modifies:
1041  */
1042 void
1043 kadm_svc_run(void)
1044 {
1045 	struct pollfd	*rfd = 0;
1046 	struct	timeval	    timeout;
1047 	int pollret;
1048 	int nfds = 0;
1049 	int i;
1050 
1051 	while(signal_request_exit == 0) {
1052 		timeout.tv_sec = TIMEOUT;
1053 		timeout.tv_usec = 0;
1054 
1055 		if (nfds != svc_max_pollfd) {
1056 			rfd = realloc(rfd, sizeof (pollfd_t) * svc_max_pollfd);
1057 			nfds = svc_max_pollfd;
1058 		}
1059 
1060 		(void) memcpy(rfd, svc_pollfd,
1061 			sizeof (pollfd_t) * svc_max_pollfd);
1062 
1063 		for (i = 0; i < nfds; i++) {
1064 			if (rfd[i].fd == -1) {
1065 				rfd[i].fd = schpw;
1066 				rfd[i].events = POLLIN;
1067 				break;
1068 			}
1069 		}
1070 
1071 		switch(pollret = poll(rfd, nfds,
1072 				__rpc_timeval_to_msec(&timeout))) {
1073 		case -1:
1074 			if(errno == EINTR)
1075 				continue;
1076 			perror("poll");
1077 			return;
1078 		case 0:
1079 			continue;
1080 		default:
1081 			for (i = 0; i < nfds; i++) {
1082 				if (rfd[i].revents & POLLIN) {
1083 					if (rfd[i].fd == schpw)
1084 						handle_chpw(context, schpw,
1085 							global_server_handle,
1086 							&chgpw_params);
1087 					else
1088 						svc_getreq_poll(rfd, pollret);
1089 					break;
1090 				} else {
1091 					if (i == (nfds - 1))
1092 						perror("poll");
1093 				}
1094 			}
1095 			break;
1096 		}
1097 	}
1098 }
1099 
1100 
1101 /*
1102  * Function: setup_signal_handlers
1103  *
1104  * Purpose: Setup signal handling functions with System V's signal().
1105  */
1106 void setup_signal_handlers(iprop_role iproprole) {
1107 	signal(SIGINT, sig_exit);
1108 	signal(SIGTERM, sig_exit);
1109 	signal(SIGQUIT, sig_exit);
1110 	signal(SIGPIPE, sig_pipe);
1111 
1112 	/*
1113 	 * IProp will fork for a full-resync, we don't want to
1114 	 * wait on it and we don't want the living dead procs either.
1115 	 */
1116 	if (iproprole == IPROP_MASTER)
1117 		(void) signal(SIGCHLD, SIG_IGN);
1118 
1119 	return;
1120 }
1121 
1122 
1123 /*
1124  * Function: sig_exit
1125  *
1126  * Purpose: sets flags saying the server got a signal and that it
1127  *          should exit when convenient.
1128  *
1129  * Effects:
1130  *      Modifies signal_request_exit which ideally makes the server exit
1131  *      at some point.
1132  *
1133  * Modifies:
1134  *      Signal_request_exit
1135  */
1136 void sig_exit(int signum)
1137 {
1138 	krb5_klog_syslog(LOG_NOTICE, gettext("Got signal to request exit"));
1139 	signal_request_exit = 1;
1140 	return;
1141 }
1142 
1143 
1144 /*
1145  * Function: sig_pipe
1146  *
1147  * Purpose: SIGPIPE handler
1148  *
1149  * Effects: krb5_klog_syslog a message that a SIGPIPE occurred and returns,
1150  * thus causing the read() or write() to fail and, presumable, the RPC
1151  * to recover.	Otherwise, the process aborts.
1152  */
1153 void
1154 sig_pipe(int unused)
1155 {
1156 	krb5_klog_syslog(LOG_NOTICE, gettext("Warning: Received a SIGPIPE; "
1157 		"probably a client aborted.  Continuing."));
1158 }
1159 
1160 
1161 /*
1162  * Given a service name (s_name) determine if the keytab file exists
1163  * and if the keytab entry is present. Log missing keytab
1164  * at LOG_ERR and log missing keytab entries at LOG_WARNING.
1165  * If any of krb5_* (or strdup) fail it will return the failure.
1166  */
1167 krb5_error_code log_kt_error(char *s_name, char *whoami) {
1168 	krb5_keytab kt;
1169 	krb5_principal princ;
1170 	krb5_keytab_entry entry;
1171 	krb5_error_code	code = 0;
1172 	char kt_name[MAX_KEYTAB_NAME_LEN];
1173 	char *service;
1174 	char *host;
1175 
1176 	service = strdup(s_name);
1177 	if(!service)
1178 		return ENOMEM;
1179 
1180 	host = strchr(service, '@');
1181 	*host++ = '\0';
1182 	if (code = krb5_sname_to_principal(context, host,
1183 				service, KRB5_NT_SRV_HST, &princ)) {
1184 		krb5_klog_syslog(LOG_ERR,
1185 				gettext("krb5_sname_to_principal failed: %s"),
1186 				error_message(code));
1187 		fprintf(stderr,
1188 				gettext("%s: krb5_sname_to_principal failed: %s"),
1189 				whoami, error_message(code));
1190 		free(service);
1191 		return code;
1192 	}
1193 
1194 	if (code = krb5_kt_default_name(context, kt_name, sizeof (kt_name))) {
1195 		krb5_klog_syslog(LOG_ERR,
1196 				gettext("krb5_kt_default_name failed: %s"),
1197 				error_message(code));
1198 		fprintf(stderr,
1199 				gettext("%s: krb5_kt_default_name failed: %s"),
1200 				whoami, error_message(code));
1201 		krb5_free_principal(context, princ);
1202 		free(service);
1203 		return code;
1204 	}
1205 
1206 	if (code = krb5_kt_default(context, &kt)) {
1207 		krb5_klog_syslog(LOG_ERR,
1208 				gettext("krb5_kt_default failed: %s"),
1209 				error_message(code));
1210 		fprintf(stderr,
1211 				gettext("%s: krb5_kt_default failed: %s"),
1212 				whoami, error_message(code));
1213 		krb5_free_principal(context, princ);
1214 		free(service);
1215 		return code;
1216 	}
1217 
1218 	code = krb5_kt_get_entry(context, kt, princ, 0, 0, &entry);
1219 
1220 	switch (code) {
1221 		case 0:
1222 			krb5_kt_free_entry(context, &entry);
1223 			break;
1224 		case KRB5_KT_NOTFOUND:
1225 			krb5_klog_syslog(LOG_WARNING,
1226 					gettext("Keytab entry \"%s/%s\" is missing from \"%s\""),
1227 					service, host,
1228 					kt_name);
1229 			fprintf(stderr,
1230 					gettext("%s: Keytab entry \"%s/%s\" is missing from \"%s\".\n"),
1231 					whoami,
1232 					service, host,
1233 					kt_name);
1234 			break;
1235 		case ENOENT:
1236 			krb5_klog_syslog(LOG_ERR,
1237 					gettext("Keytab file \"%s\" does not exist"),
1238 					kt_name);
1239 			fprintf(stderr,
1240 					gettext("%s: Keytab file \"%s\" does not exist.\n"),
1241 					whoami,
1242 					kt_name);
1243 			break;
1244 	}
1245 	krb5_kt_close(context,kt);
1246 	krb5_free_principal(context, princ);
1247 	free(service);
1248 	return code;
1249 }
1250 
1251