xref: /titanic_50/usr/src/cmd/krb5/krb5kdc/main.c (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
17c478bd9Sstevel@tonic-gate /*
2*54925bf6Swillf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * kdc/main.c
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Copyright 1990,2001 by the Massachusetts Institute of Technology.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
147c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
157c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
167c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
197c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
207c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
217c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
227c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
237c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
247c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
257c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
267c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
277c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
287c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
297c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
307c478bd9Sstevel@tonic-gate  * or implied warranty.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Main procedure body for the KDC server process.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <netdb.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "k5-int.h"
437c478bd9Sstevel@tonic-gate #include "com_err.h"
447c478bd9Sstevel@tonic-gate #include "adm.h"
457c478bd9Sstevel@tonic-gate #include "adm_proto.h"
467c478bd9Sstevel@tonic-gate #include "kdc_util.h"
477c478bd9Sstevel@tonic-gate #include "extern.h"
487c478bd9Sstevel@tonic-gate #include "kdc5_err.h"
497c478bd9Sstevel@tonic-gate #include <libintl.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
537c478bd9Sstevel@tonic-gate #include <netinet/in.h>
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate 
5656a424ccSmp153739 #ifdef KRB5_KRB4_COMPAT
5756a424ccSmp153739 #include <des.h>
5856a424ccSmp153739 #endif
5956a424ccSmp153739 
6056a424ccSmp153739 #if defined(NEED_DAEMON_PROTO)
6156a424ccSmp153739 extern int daemon(int, int);
6256a424ccSmp153739 #endif
637c478bd9Sstevel@tonic-gate 
64505d05c7Sgtb void usage (char *);
657c478bd9Sstevel@tonic-gate 
66505d05c7Sgtb krb5_sigtype request_exit (int);
67505d05c7Sgtb krb5_sigtype request_hup  (int);
687c478bd9Sstevel@tonic-gate 
69505d05c7Sgtb void setup_signal_handlers (void);
707c478bd9Sstevel@tonic-gate 
71505d05c7Sgtb krb5_error_code setup_sam (void);
727c478bd9Sstevel@tonic-gate 
73505d05c7Sgtb void initialize_realms (krb5_context, int, char **);
747c478bd9Sstevel@tonic-gate 
75505d05c7Sgtb void finish_realms (char *);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int nofork = 0;
787c478bd9Sstevel@tonic-gate static int rkey_init_done = 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* Solaris Kerberos: global here that other functions access */
817c478bd9Sstevel@tonic-gate int max_tcp_data_connections;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGNALS
847c478bd9Sstevel@tonic-gate static struct sigaction s_action;
857c478bd9Sstevel@tonic-gate #endif /* POSIX_SIGNALS */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	KRB5_KDC_MAX_REALMS	32
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Find the realm entry for a given realm.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate kdc_realm_t *
9356a424ccSmp153739 find_realm_data(char *rname, krb5_ui_4 rsize)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate     int i;
967c478bd9Sstevel@tonic-gate     for (i=0; i<kdc_numrealms; i++) {
977c478bd9Sstevel@tonic-gate 	if ((rsize == strlen(kdc_realmlist[i]->realm_name)) &&
987c478bd9Sstevel@tonic-gate 	    !strncmp(rname, kdc_realmlist[i]->realm_name, rsize))
997c478bd9Sstevel@tonic-gate 	    return(kdc_realmlist[i]);
1007c478bd9Sstevel@tonic-gate     }
1017c478bd9Sstevel@tonic-gate     return((kdc_realm_t *) NULL);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate krb5_error_code
10556a424ccSmp153739 setup_server_realm(krb5_principal sprinc)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
1087c478bd9Sstevel@tonic-gate     kdc_realm_t		*newrealm;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate     kret = 0;
1117c478bd9Sstevel@tonic-gate     if (kdc_numrealms > 1) {
1127c478bd9Sstevel@tonic-gate 	if (!(newrealm = find_realm_data(sprinc->realm.data,
1137c478bd9Sstevel@tonic-gate 					 (krb5_ui_4) sprinc->realm.length)))
1147c478bd9Sstevel@tonic-gate 	    kret = ENOENT;
1157c478bd9Sstevel@tonic-gate 	else
1167c478bd9Sstevel@tonic-gate 	    kdc_active_realm = newrealm;
1177c478bd9Sstevel@tonic-gate     }
1187c478bd9Sstevel@tonic-gate     else
1197c478bd9Sstevel@tonic-gate 	kdc_active_realm = kdc_realmlist[0];
1207c478bd9Sstevel@tonic-gate     return(kret);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static void
12456a424ccSmp153739 finish_realm(kdc_realm_t *rdp)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate     if (rdp->realm_dbname)
1277c478bd9Sstevel@tonic-gate 	free(rdp->realm_dbname);
1287c478bd9Sstevel@tonic-gate     if (rdp->realm_mpname)
1297c478bd9Sstevel@tonic-gate 	free(rdp->realm_mpname);
1307c478bd9Sstevel@tonic-gate     if (rdp->realm_stash)
1317c478bd9Sstevel@tonic-gate 	free(rdp->realm_stash);
1327c478bd9Sstevel@tonic-gate     if (rdp->realm_ports)
1337c478bd9Sstevel@tonic-gate 	free(rdp->realm_ports);
1347c478bd9Sstevel@tonic-gate     if (rdp->realm_tcp_ports)
1357c478bd9Sstevel@tonic-gate 	free(rdp->realm_tcp_ports);
1367c478bd9Sstevel@tonic-gate     if (rdp->realm_keytab)
1377c478bd9Sstevel@tonic-gate 	krb5_kt_close(rdp->realm_context, rdp->realm_keytab);
1387c478bd9Sstevel@tonic-gate     if (rdp->realm_context) {
1397c478bd9Sstevel@tonic-gate 	if (rdp->realm_mprinc)
1407c478bd9Sstevel@tonic-gate 	    krb5_free_principal(rdp->realm_context, rdp->realm_mprinc);
1417c478bd9Sstevel@tonic-gate 	if (rdp->realm_mkey.length && rdp->realm_mkey.contents) {
1427c478bd9Sstevel@tonic-gate 	    memset(rdp->realm_mkey.contents, 0, rdp->realm_mkey.length);
1437c478bd9Sstevel@tonic-gate 	    free(rdp->realm_mkey.contents);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	krb5_db_fini(rdp->realm_context);
1467c478bd9Sstevel@tonic-gate 	if (rdp->realm_tgsprinc)
1477c478bd9Sstevel@tonic-gate 	    krb5_free_principal(rdp->realm_context, rdp->realm_tgsprinc);
1487c478bd9Sstevel@tonic-gate 	krb5_free_context(rdp->realm_context);
1497c478bd9Sstevel@tonic-gate     }
15056a424ccSmp153739     memset((char *) rdp, 0, sizeof(*rdp));
1517c478bd9Sstevel@tonic-gate     free(rdp);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Initialize a realm control structure from the alternate profile or from
1567c478bd9Sstevel@tonic-gate  * the specified defaults.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * After we're complete here, the essence of the realm is embodied in the
1597c478bd9Sstevel@tonic-gate  * realm data and we should be all set to begin operation for that realm.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static krb5_error_code
162*54925bf6Swillf init_realm(char *progname, kdc_realm_t *rdp, char *realm,
16356a424ccSmp153739 	   char *def_mpname, krb5_enctype def_enctype, char *def_udp_ports,
164*54925bf6Swillf 	   char *def_tcp_ports, krb5_boolean def_manual, char **db_args)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
1677c478bd9Sstevel@tonic-gate     krb5_boolean	manual;
1687c478bd9Sstevel@tonic-gate     krb5_realm_params	*rparams;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate     memset((char *) rdp, 0, sizeof(kdc_realm_t));
1717c478bd9Sstevel@tonic-gate     if (!realm) {
1727c478bd9Sstevel@tonic-gate 	kret = EINVAL;
1737c478bd9Sstevel@tonic-gate 	goto whoops;
1747c478bd9Sstevel@tonic-gate     }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate     rdp->realm_name = realm;
177*54925bf6Swillf     kret = krb5int_init_context_kdc(&rdp->realm_context);
1787c478bd9Sstevel@tonic-gate     if (kret) {
1797c478bd9Sstevel@tonic-gate 	com_err(progname, kret, gettext("while getting context for realm %s"),
1807c478bd9Sstevel@tonic-gate 		realm);
1817c478bd9Sstevel@tonic-gate 	goto whoops;
1827c478bd9Sstevel@tonic-gate     }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate     kret = krb5_read_realm_params(rdp->realm_context, rdp->realm_name,
1857c478bd9Sstevel@tonic-gate 				  (char *) NULL, (char *) NULL, &rparams);
1867c478bd9Sstevel@tonic-gate     if (kret) {
1877c478bd9Sstevel@tonic-gate 	com_err(progname, kret, gettext("while reading realm parameters"));
1887c478bd9Sstevel@tonic-gate 	goto whoops;
1897c478bd9Sstevel@tonic-gate     }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate     /* Handle profile file name */
1927c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_profile)
1937c478bd9Sstevel@tonic-gate 	rdp->realm_profile = strdup(rparams->realm_profile);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate     /* Handle master key name */
1967c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_mkey_name)
1977c478bd9Sstevel@tonic-gate 	rdp->realm_mpname = strdup(rparams->realm_mkey_name);
1987c478bd9Sstevel@tonic-gate     else
1997c478bd9Sstevel@tonic-gate 	rdp->realm_mpname = (def_mpname) ? strdup(def_mpname) :
2007c478bd9Sstevel@tonic-gate 	    strdup(KRB5_KDB_M_NAME);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate     /* Handle KDC ports */
2037c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_kdc_ports)
2047c478bd9Sstevel@tonic-gate 	rdp->realm_ports = strdup(rparams->realm_kdc_ports);
2057c478bd9Sstevel@tonic-gate     else
2067c478bd9Sstevel@tonic-gate 	rdp->realm_ports = strdup(def_udp_ports);
2077c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_kdc_tcp_ports)
2087c478bd9Sstevel@tonic-gate 	rdp->realm_tcp_ports = strdup(rparams->realm_kdc_tcp_ports);
2097c478bd9Sstevel@tonic-gate     else
2107c478bd9Sstevel@tonic-gate 	rdp->realm_tcp_ports = strdup(def_tcp_ports);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate     /* Handle stash file */
2137c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_stash_file) {
2147c478bd9Sstevel@tonic-gate 	rdp->realm_stash = strdup(rparams->realm_stash_file);
2157c478bd9Sstevel@tonic-gate 	manual = FALSE;
2167c478bd9Sstevel@tonic-gate     } else
2177c478bd9Sstevel@tonic-gate 	manual = def_manual;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate     /* Handle master key type */
2207c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_enctype_valid)
2217c478bd9Sstevel@tonic-gate 	rdp->realm_mkey.enctype = (krb5_enctype) rparams->realm_enctype;
2227c478bd9Sstevel@tonic-gate     else
2237c478bd9Sstevel@tonic-gate 	rdp->realm_mkey.enctype = manual ? def_enctype : ENCTYPE_UNKNOWN;
22456a424ccSmp153739 
22556a424ccSmp153739     /* Handle reject-bad-transit flag */
22656a424ccSmp153739     if (rparams && rparams->realm_reject_bad_transit_valid)
22756a424ccSmp153739 	rdp->realm_reject_bad_transit = rparams->realm_reject_bad_transit;
22856a424ccSmp153739     else
22956a424ccSmp153739 	rdp->realm_reject_bad_transit = 1;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate     /* Handle ticket maximum life */
23256a424ccSmp153739     rdp->realm_maxlife = (rparams && rparams->realm_max_life_valid) ?
23356a424ccSmp153739 	rparams->realm_max_life : KRB5_KDB_MAX_LIFE;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate     /* Handle ticket renewable maximum life */
23656a424ccSmp153739     rdp->realm_maxrlife = (rparams && rparams->realm_max_rlife_valid) ?
23756a424ccSmp153739 	rparams->realm_max_rlife : KRB5_KDB_MAX_RLIFE;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate     if (rparams)
2407c478bd9Sstevel@tonic-gate 	krb5_free_realm_params(rdp->realm_context, rparams);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate     /*
2437c478bd9Sstevel@tonic-gate      * We've got our parameters, now go and setup our realm context.
2447c478bd9Sstevel@tonic-gate      */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate     /* Set the default realm of this context */
2477c478bd9Sstevel@tonic-gate     if ((kret = krb5_set_default_realm(rdp->realm_context, realm))) {
2487c478bd9Sstevel@tonic-gate 	com_err(progname, kret, gettext("while setting default realm to %s"),
2497c478bd9Sstevel@tonic-gate 		realm);
2507c478bd9Sstevel@tonic-gate 	goto whoops;
2517c478bd9Sstevel@tonic-gate     }
2527c478bd9Sstevel@tonic-gate 
253*54925bf6Swillf     /* first open the database  before doing anything */
254*54925bf6Swillf #ifdef KRBCONF_KDC_MODIFIES_KDB
255*54925bf6Swillf     if ((kret = krb5_db_open(rdp->realm_context, db_args,
256*54925bf6Swillf 			     KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_KDC))) {
257*54925bf6Swillf #else
258*54925bf6Swillf     if ((kret = krb5_db_open(rdp->realm_context, db_args,
259*54925bf6Swillf 			     KRB5_KDB_OPEN_RO | KRB5_KDB_SRV_TYPE_KDC))) {
260*54925bf6Swillf #endif
261*54925bf6Swillf 	com_err(progname, kret,
262*54925bf6Swillf 		"while initializing database for realm %s", realm);
263*54925bf6Swillf 	goto whoops;
264*54925bf6Swillf     }
265*54925bf6Swillf 
2667c478bd9Sstevel@tonic-gate     /* Assemble and parse the master key name */
2677c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_setup_mkey_name(rdp->realm_context, rdp->realm_mpname,
2687c478bd9Sstevel@tonic-gate 					rdp->realm_name, (char **) NULL,
2697c478bd9Sstevel@tonic-gate 					&rdp->realm_mprinc))) {
2707c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2717c478bd9Sstevel@tonic-gate 		gettext("while setting up master key name %s for realm %s"),
2727c478bd9Sstevel@tonic-gate 		rdp->realm_mpname, realm);
2737c478bd9Sstevel@tonic-gate 	goto whoops;
2747c478bd9Sstevel@tonic-gate     }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate     /*
2777c478bd9Sstevel@tonic-gate      * Get the master key.
2787c478bd9Sstevel@tonic-gate      */
2797c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_fetch_mkey(rdp->realm_context, rdp->realm_mprinc,
2807c478bd9Sstevel@tonic-gate 				   rdp->realm_mkey.enctype, manual,
2817c478bd9Sstevel@tonic-gate 				   FALSE, rdp->realm_stash,
2827c478bd9Sstevel@tonic-gate 				   0, &rdp->realm_mkey))) {
2837c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2847c478bd9Sstevel@tonic-gate 		gettext("while fetching master key %s for realm %s"),
2857c478bd9Sstevel@tonic-gate 		rdp->realm_mpname, realm);
2867c478bd9Sstevel@tonic-gate 	goto whoops;
2877c478bd9Sstevel@tonic-gate     }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate     /* Verify the master key */
2907c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_verify_master_key(rdp->realm_context,
2917c478bd9Sstevel@tonic-gate 					  rdp->realm_mprinc,
2927c478bd9Sstevel@tonic-gate 					  &rdp->realm_mkey))) {
2937c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2947c478bd9Sstevel@tonic-gate 		gettext("while verifying master key for realm %s"),
2957c478bd9Sstevel@tonic-gate 		realm);
2967c478bd9Sstevel@tonic-gate 	goto whoops;
2977c478bd9Sstevel@tonic-gate     }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_set_mkey(rdp->realm_context, &rdp->realm_mkey))) {
3007c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3017c478bd9Sstevel@tonic-gate 		gettext("while processing master key for realm %s"),
3027c478bd9Sstevel@tonic-gate 		realm);
3037c478bd9Sstevel@tonic-gate 	goto whoops;
3047c478bd9Sstevel@tonic-gate     }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate     /* Set up the keytab */
30756a424ccSmp153739     if ((kret = krb5_ktkdb_resolve(rdp->realm_context, NULL,
3087c478bd9Sstevel@tonic-gate 				   &rdp->realm_keytab))) {
3097c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3107c478bd9Sstevel@tonic-gate 		gettext("while resolving kdb keytab for realm %s"),
3117c478bd9Sstevel@tonic-gate 		realm);
3127c478bd9Sstevel@tonic-gate 	goto whoops;
3137c478bd9Sstevel@tonic-gate     }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate     /* Preformat the TGS name */
3167c478bd9Sstevel@tonic-gate     if ((kret = krb5_build_principal(rdp->realm_context, &rdp->realm_tgsprinc,
3177c478bd9Sstevel@tonic-gate 				     strlen(realm), realm, KRB5_TGS_NAME,
3187c478bd9Sstevel@tonic-gate 				     realm, (char *) NULL))) {
3197c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3207c478bd9Sstevel@tonic-gate 		gettext("while building TGS name for realm %s"),
3217c478bd9Sstevel@tonic-gate 		realm);
3227c478bd9Sstevel@tonic-gate 	goto whoops;
3237c478bd9Sstevel@tonic-gate     }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate     if (!rkey_init_done) {
3267c478bd9Sstevel@tonic-gate 	krb5_data seed;
3277c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
3287c478bd9Sstevel@tonic-gate 	krb5_keyblock temp_key;
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * If all that worked, then initialize the random key
3327c478bd9Sstevel@tonic-gate 	 * generators.
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	seed.length = rdp->realm_mkey.length;
3367c478bd9Sstevel@tonic-gate 	seed.data = (char *)rdp->realm_mkey.contents;
33756a424ccSmp153739 /* SUNW14resync - XXX */
33856a424ccSmp153739 #if 0
33956a424ccSmp153739 	if ((kret = krb5_c_random_add_entropy(rdp->realm_context,
34056a424ccSmp153739 					     KRB5_C_RANDSOURCE_TRUSTEDPARTY, &seed)))
3417c478bd9Sstevel@tonic-gate 	    goto whoops;
34256a424ccSmp153739 #endif
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
3457c478bd9Sstevel@tonic-gate 	if ((kret = krb5_c_make_random_key(rdp->realm_context,
3467c478bd9Sstevel@tonic-gate 					   ENCTYPE_DES_CBC_CRC, &temp_key))) {
3477c478bd9Sstevel@tonic-gate 	    com_err(progname, kret,
3487c478bd9Sstevel@tonic-gate 		    "while initializing V4 random key generator");
3497c478bd9Sstevel@tonic-gate 	    goto whoops;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	(void) des_init_random_number_generator(temp_key.contents);
3537c478bd9Sstevel@tonic-gate 	krb5_free_keyblock_contents(rdp->realm_context, &temp_key);
3547c478bd9Sstevel@tonic-gate #endif
3557c478bd9Sstevel@tonic-gate 	rkey_init_done = 1;
3567c478bd9Sstevel@tonic-gate     }
3577c478bd9Sstevel@tonic-gate  whoops:
3587c478bd9Sstevel@tonic-gate     /*
3597c478bd9Sstevel@tonic-gate      * If we choked, then clean up any dirt we may have dropped on the floor.
3607c478bd9Sstevel@tonic-gate      */
3617c478bd9Sstevel@tonic-gate     if (kret) {
36256a424ccSmp153739 
3637c478bd9Sstevel@tonic-gate 	finish_realm(rdp);
3647c478bd9Sstevel@tonic-gate     }
3657c478bd9Sstevel@tonic-gate     return(kret);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate krb5_sigtype
36956a424ccSmp153739 request_exit(int signo)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate     signal_requests_exit = 1;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGTYPE
3747c478bd9Sstevel@tonic-gate     return;
3757c478bd9Sstevel@tonic-gate #else
3767c478bd9Sstevel@tonic-gate     return(0);
3777c478bd9Sstevel@tonic-gate #endif
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate krb5_sigtype
38156a424ccSmp153739 request_hup(int signo)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate     signal_requests_hup = 1;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGTYPE
3867c478bd9Sstevel@tonic-gate     return;
3877c478bd9Sstevel@tonic-gate #else
3887c478bd9Sstevel@tonic-gate     return(0);
3897c478bd9Sstevel@tonic-gate #endif
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate void
39356a424ccSmp153739 setup_signal_handlers(void)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGNALS
3967c478bd9Sstevel@tonic-gate     (void) sigemptyset(&s_action.sa_mask);
3977c478bd9Sstevel@tonic-gate     s_action.sa_flags = 0;
3987c478bd9Sstevel@tonic-gate     s_action.sa_handler = request_exit;
3997c478bd9Sstevel@tonic-gate     (void) sigaction(SIGINT, &s_action, (struct sigaction *) NULL);
4007c478bd9Sstevel@tonic-gate     (void) sigaction(SIGTERM, &s_action, (struct sigaction *) NULL);
4017c478bd9Sstevel@tonic-gate     s_action.sa_handler = request_hup;
4027c478bd9Sstevel@tonic-gate     (void) sigaction(SIGHUP, &s_action, (struct sigaction *) NULL);
403*54925bf6Swillf     s_action.sa_handler = SIG_IGN;
404*54925bf6Swillf     (void) sigaction(SIGPIPE, &s_action, (struct sigaction *) NULL);
4057c478bd9Sstevel@tonic-gate #else  /* POSIX_SIGNALS */
4067c478bd9Sstevel@tonic-gate     signal(SIGINT, request_exit);
4077c478bd9Sstevel@tonic-gate     signal(SIGTERM, request_exit);
4087c478bd9Sstevel@tonic-gate     signal(SIGHUP, request_hup);
409*54925bf6Swillf     signal(SIGPIPE, SIG_IGN);
4107c478bd9Sstevel@tonic-gate #endif /* POSIX_SIGNALS */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate     return;
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate krb5_error_code
41656a424ccSmp153739 setup_sam(void)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate     return krb5_c_make_random_key(kdc_context, ENCTYPE_DES_CBC_MD5, &psr_key);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate void
42256a424ccSmp153739 usage(char *name)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("usage: %s [-d dbpathname] [-r dbrealmname] [-R replaycachename ]\n\t[-m] [-k masterenctype] [-M masterkeyname] [-p port] [-n]\n"), name);
425*54925bf6Swillf     fprintf(stderr, "usage: %s [-x db_args]* [-d dbpathname] [-r dbrealmname] [-R replaycachename ]\n\t[-m] [-k masterenctype] [-M masterkeyname] [-p port] [-X] [-n]\n"
426*54925bf6Swillf 	    "\nwhere,\n\t[-x db_args]* - any number of database specific arguments.\n"
427*54925bf6Swillf 	    "\t\t\tLook at each database documentation for supported arguments\n",
428*54925bf6Swillf 	    name);
4297c478bd9Sstevel@tonic-gate     return;
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate void
43356a424ccSmp153739 initialize_realms(krb5_context kcontext, int argc, char **argv)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate     int 		c;
4367c478bd9Sstevel@tonic-gate     char		*db_name = (char *) NULL;
4377c478bd9Sstevel@tonic-gate     char		*mkey_name = (char *) NULL;
4387c478bd9Sstevel@tonic-gate     char		*rcname = KDCRCACHE;
4397c478bd9Sstevel@tonic-gate     char		*lrealm;
4407c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
4417c478bd9Sstevel@tonic-gate     krb5_enctype	menctype = ENCTYPE_UNKNOWN;
4427c478bd9Sstevel@tonic-gate     kdc_realm_t		*rdatap;
4437c478bd9Sstevel@tonic-gate     krb5_boolean	manual = FALSE;
4447c478bd9Sstevel@tonic-gate     char		*default_udp_ports = 0;
4457c478bd9Sstevel@tonic-gate     char		*default_tcp_ports = 0;
4467c478bd9Sstevel@tonic-gate     krb5_pointer	aprof;
4477c478bd9Sstevel@tonic-gate     const char		*hierarchy[3];
448*54925bf6Swillf     char               **db_args      = NULL;
449*54925bf6Swillf     int                  db_args_size = 0;
450*54925bf6Swillf 
4517c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
4527c478bd9Sstevel@tonic-gate     char                *v4mode = 0;
4537c478bd9Sstevel@tonic-gate #endif
4547c478bd9Sstevel@tonic-gate     extern char *optarg;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate     if (!krb5_aprof_init(DEFAULT_KDC_PROFILE, KDC_PROFILE_ENV, &aprof)) {
4577c478bd9Sstevel@tonic-gate 	hierarchy[0] = "kdcdefaults";
4587c478bd9Sstevel@tonic-gate 	hierarchy[1] = "kdc_ports";
4597c478bd9Sstevel@tonic-gate 	hierarchy[2] = (char *) NULL;
4607c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_string(aprof, hierarchy, TRUE, &default_udp_ports))
4617c478bd9Sstevel@tonic-gate 	    default_udp_ports = 0;
4627c478bd9Sstevel@tonic-gate 	hierarchy[1] = "kdc_tcp_ports";
4637c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_string(aprof, hierarchy, TRUE, &default_tcp_ports))
4647c478bd9Sstevel@tonic-gate 	    default_tcp_ports = 0;
4657c478bd9Sstevel@tonic-gate 	hierarchy[1] = "kdc_max_tcp_connections";
4667c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_int32(aprof, hierarchy, TRUE,
4677c478bd9Sstevel@tonic-gate 		&max_tcp_data_connections)) {
4687c478bd9Sstevel@tonic-gate 	    max_tcp_data_connections = DEFAULT_KDC_TCP_CONNECTIONS;
4697c478bd9Sstevel@tonic-gate 	} else if (max_tcp_data_connections < MIN_KDC_TCP_CONNECTIONS) {
4707c478bd9Sstevel@tonic-gate 	    max_tcp_data_connections = DEFAULT_KDC_TCP_CONNECTIONS;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
4737c478bd9Sstevel@tonic-gate 	hierarchy[1] = "v4_mode";
4747c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_string(aprof, hierarchy, TRUE, &v4mode))
4757c478bd9Sstevel@tonic-gate 	    v4mode = 0;
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate 	/* aprof_init can return 0 with aprof == NULL */
4787c478bd9Sstevel@tonic-gate 	if (aprof)
4797c478bd9Sstevel@tonic-gate 	     krb5_aprof_finish(aprof);
4807c478bd9Sstevel@tonic-gate     }
4817c478bd9Sstevel@tonic-gate     if (default_udp_ports == 0)
4827c478bd9Sstevel@tonic-gate 	default_udp_ports = strdup(DEFAULT_KDC_UDP_PORTLIST);
4837c478bd9Sstevel@tonic-gate     if (default_tcp_ports == 0)
4847c478bd9Sstevel@tonic-gate 	default_tcp_ports = strdup(DEFAULT_KDC_TCP_PORTLIST);
4857c478bd9Sstevel@tonic-gate     /*
4867c478bd9Sstevel@tonic-gate      * Loop through the option list.  Each time we encounter a realm name,
4877c478bd9Sstevel@tonic-gate      * use the previously scanned options to fill in for defaults.
4887c478bd9Sstevel@tonic-gate      */
489*54925bf6Swillf     while ((c = getopt(argc, argv, "x:r:d:mM:k:R:e:p:s:n4:X3")) != -1) {
4907c478bd9Sstevel@tonic-gate 	switch(c) {
491*54925bf6Swillf 	case 'x':
492*54925bf6Swillf 	    db_args_size++;
493*54925bf6Swillf 	    {
494*54925bf6Swillf 		char **temp = realloc( db_args, sizeof(char*) * (db_args_size+1)); /* one for NULL */
495*54925bf6Swillf 		if( temp == NULL )
496*54925bf6Swillf 		{
497*54925bf6Swillf 		    fprintf(stderr, gettext("%s: KDC cannot initialize. Not enough memory\n"),
498*54925bf6Swillf 			    argv[0]);
499*54925bf6Swillf 		    exit(1);
500*54925bf6Swillf 		}
501*54925bf6Swillf 
502*54925bf6Swillf 		db_args = temp;
503*54925bf6Swillf 	    }
504*54925bf6Swillf 	    db_args[db_args_size-1] = optarg;
505*54925bf6Swillf 	    db_args[db_args_size]   = NULL;
506*54925bf6Swillf 	  break;
507*54925bf6Swillf 
5087c478bd9Sstevel@tonic-gate 	case 'r':			/* realm name for db */
5097c478bd9Sstevel@tonic-gate 	    if (!find_realm_data(optarg, (krb5_ui_4) strlen(optarg))) {
5107c478bd9Sstevel@tonic-gate 		if ((rdatap = (kdc_realm_t *) malloc(sizeof(kdc_realm_t)))) {
511*54925bf6Swillf 		    if ((retval = init_realm(argv[0], rdatap, optarg,
5127c478bd9Sstevel@tonic-gate 					     mkey_name, menctype,
5137c478bd9Sstevel@tonic-gate 					     default_udp_ports,
514*54925bf6Swillf 					     default_tcp_ports, manual, db_args))) {
5157c478bd9Sstevel@tonic-gate 			fprintf(stderr,gettext("%s: cannot initialize realm %s\n"),
5167c478bd9Sstevel@tonic-gate 				argv[0], optarg);
5177c478bd9Sstevel@tonic-gate 			exit(1);
5187c478bd9Sstevel@tonic-gate 		    }
5197c478bd9Sstevel@tonic-gate 		    kdc_realmlist[kdc_numrealms] = rdatap;
5207c478bd9Sstevel@tonic-gate 		    kdc_numrealms++;
521*54925bf6Swillf 		    free(db_args), db_args=NULL, db_args_size = 0;
522*54925bf6Swillf 		}
523*54925bf6Swillf 		else
524*54925bf6Swillf 		{
525*54925bf6Swillf 			fprintf(stderr, gettext("%s: cannot initialize realm %s. Not enough memory\n"),
526*54925bf6Swillf 				argv[0], optarg);
527*54925bf6Swillf 			exit(1);
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 	    }
5307c478bd9Sstevel@tonic-gate 	    break;
5317c478bd9Sstevel@tonic-gate 	case 'd':			/* pathname for db */
532*54925bf6Swillf 	    /* now db_name is not a seperate argument. It has to be passed as part of the db_args */
533*54925bf6Swillf 	    if( db_name == NULL )
534*54925bf6Swillf 	    {
535*54925bf6Swillf 		db_name = malloc(sizeof("dbname=") + strlen(optarg));
536*54925bf6Swillf 		if( db_name == NULL )
537*54925bf6Swillf 		{
538*54925bf6Swillf 			fprintf(stderr, gettext("%s: KDC cannot initialize. Not enough memory\n"),
539*54925bf6Swillf 				argv[0] );
540*54925bf6Swillf 			exit(1);
541*54925bf6Swillf 		}
542*54925bf6Swillf 
543*54925bf6Swillf 		sprintf( db_name, "dbname=%s", optarg);
544*54925bf6Swillf 	    }
545*54925bf6Swillf 
546*54925bf6Swillf 	    db_args_size++;
547*54925bf6Swillf 	    {
548*54925bf6Swillf 		char **temp = realloc( db_args, sizeof(char*) * (db_args_size+1)); /* one for NULL */
549*54925bf6Swillf 		if( temp == NULL )
550*54925bf6Swillf 		{
551*54925bf6Swillf 		    fprintf(stderr, gettext("%s: KDC cannot initialize. Not enough memory\n"),
552*54925bf6Swillf 			    argv[0]);
553*54925bf6Swillf 		    exit(1);
554*54925bf6Swillf 		}
555*54925bf6Swillf 
556*54925bf6Swillf 		db_args = temp;
557*54925bf6Swillf 	    }
558*54925bf6Swillf 	    db_args[db_args_size-1] = db_name;
559*54925bf6Swillf 	    db_args[db_args_size]   = NULL;
5607c478bd9Sstevel@tonic-gate 	    break;
5617c478bd9Sstevel@tonic-gate 	case 'm':			/* manual type-in of master key */
5627c478bd9Sstevel@tonic-gate 	    manual = TRUE;
5637c478bd9Sstevel@tonic-gate 	    if (menctype == ENCTYPE_UNKNOWN)
5647c478bd9Sstevel@tonic-gate 		menctype = ENCTYPE_DES_CBC_CRC;
5657c478bd9Sstevel@tonic-gate 	    break;
5667c478bd9Sstevel@tonic-gate 	case 'M':			/* master key name in DB */
5677c478bd9Sstevel@tonic-gate 	    mkey_name = optarg;
5687c478bd9Sstevel@tonic-gate 	    break;
5697c478bd9Sstevel@tonic-gate 	case 'n':
5707c478bd9Sstevel@tonic-gate 	    nofork++;			/* don't detach from terminal */
5717c478bd9Sstevel@tonic-gate 	    break;
5727c478bd9Sstevel@tonic-gate 	case 'k':			/* enctype for master key */
5737c478bd9Sstevel@tonic-gate 	    if (krb5_string_to_enctype(optarg, &menctype))
5747c478bd9Sstevel@tonic-gate 		com_err(argv[0], 0,
5757c478bd9Sstevel@tonic-gate 		gettext("invalid enctype %s"), optarg);
5767c478bd9Sstevel@tonic-gate 	    break;
5777c478bd9Sstevel@tonic-gate 	case 'R':
5787c478bd9Sstevel@tonic-gate 	    rcname = optarg;
5797c478bd9Sstevel@tonic-gate 	    break;
5807c478bd9Sstevel@tonic-gate 	case 'p':
5817c478bd9Sstevel@tonic-gate 	    if (default_udp_ports)
5827c478bd9Sstevel@tonic-gate 		free(default_udp_ports);
5837c478bd9Sstevel@tonic-gate 	    default_udp_ports = strdup(optarg);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	    if (default_tcp_ports)
5867c478bd9Sstevel@tonic-gate 		free(default_tcp_ports);
5877c478bd9Sstevel@tonic-gate 	    default_tcp_ports = strdup(optarg);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	    break;
5907c478bd9Sstevel@tonic-gate 	case '4':
5917c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
5927c478bd9Sstevel@tonic-gate 	    if (v4mode)
5937c478bd9Sstevel@tonic-gate 		free(v4mode);
5947c478bd9Sstevel@tonic-gate 	    v4mode = strdup(optarg);
5957c478bd9Sstevel@tonic-gate #endif
5967c478bd9Sstevel@tonic-gate 	    break;
59756a424ccSmp153739 	case 'X':
59856a424ccSmp153739 #ifdef KRB5_KRB4_COMPAT
59956a424ccSmp153739 		enable_v4_crossrealm(argv[0]);
6007c478bd9Sstevel@tonic-gate #endif
60156a424ccSmp153739 		break;
6027c478bd9Sstevel@tonic-gate 	case '?':
6037c478bd9Sstevel@tonic-gate 	default:
6047c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
6057c478bd9Sstevel@tonic-gate 	    exit(1);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate     }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
6107c478bd9Sstevel@tonic-gate     /*
6117c478bd9Sstevel@tonic-gate      * Setup the v4 mode
6127c478bd9Sstevel@tonic-gate      */
6137c478bd9Sstevel@tonic-gate     process_v4_mode(argv[0], v4mode);
614*54925bf6Swillf     free(v4mode);
6157c478bd9Sstevel@tonic-gate #endif
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate     /*
6187c478bd9Sstevel@tonic-gate      * Check to see if we processed any realms.
6197c478bd9Sstevel@tonic-gate      */
6207c478bd9Sstevel@tonic-gate     if (kdc_numrealms == 0) {
6217c478bd9Sstevel@tonic-gate 	/* no realm specified, use default realm */
6227c478bd9Sstevel@tonic-gate 	if ((retval = krb5_get_default_realm(kcontext, &lrealm))) {
6237c478bd9Sstevel@tonic-gate 	    com_err(argv[0], retval,
6247c478bd9Sstevel@tonic-gate 		gettext("while attempting to retrieve default realm"));
62556a424ccSmp153739 	    fprintf (stderr, "%s: %s, %s", argv[0], error_message (retval),
62656a424ccSmp153739 	        gettext("attempting to retrieve default realm\n"));
6277c478bd9Sstevel@tonic-gate 	    exit(1);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	if ((rdatap = (kdc_realm_t *) malloc(sizeof(kdc_realm_t)))) {
630*54925bf6Swillf 	    if ((retval = init_realm(argv[0], rdatap, lrealm,
6317c478bd9Sstevel@tonic-gate 				     mkey_name, menctype, default_udp_ports,
632*54925bf6Swillf 				     default_tcp_ports, manual, db_args))) {
6337c478bd9Sstevel@tonic-gate 		fprintf(stderr,
6347c478bd9Sstevel@tonic-gate 			gettext("%s: cannot initialize realm %s\n"),
6357c478bd9Sstevel@tonic-gate 			argv[0], lrealm);
6367c478bd9Sstevel@tonic-gate 		exit(1);
6377c478bd9Sstevel@tonic-gate 	    }
6387c478bd9Sstevel@tonic-gate 	    kdc_realmlist[0] = rdatap;
6397c478bd9Sstevel@tonic-gate 	    kdc_numrealms++;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate     }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate #ifdef USE_RCACHE
6447c478bd9Sstevel@tonic-gate     /*
6457c478bd9Sstevel@tonic-gate      * Now handle the replay cache.
6467c478bd9Sstevel@tonic-gate      */
6477c478bd9Sstevel@tonic-gate     if ((retval = kdc_initialize_rcache(kcontext, rcname))) {
64856a424ccSmp153739 	com_err(argv[0], retval, gettext("while initializing KDC replay cache '%s'"),
64956a424ccSmp153739 		rcname);
6507c478bd9Sstevel@tonic-gate 	exit(1);
6517c478bd9Sstevel@tonic-gate     }
6527c478bd9Sstevel@tonic-gate #endif
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate     /* Ensure that this is set for our first request. */
6557c478bd9Sstevel@tonic-gate     kdc_active_realm = kdc_realmlist[0];
6567c478bd9Sstevel@tonic-gate     if (default_udp_ports)
6577c478bd9Sstevel@tonic-gate 	free(default_udp_ports);
6587c478bd9Sstevel@tonic-gate     if (default_tcp_ports)
6597c478bd9Sstevel@tonic-gate 	free(default_tcp_ports);
660*54925bf6Swillf     if (db_args)
661*54925bf6Swillf 	free(db_args);
662*54925bf6Swillf     if (db_name)
663*54925bf6Swillf 	free(db_name);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate     return;
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate void
66956a424ccSmp153739 finish_realms(char *prog)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate     int i;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate     for (i = 0; i < kdc_numrealms; i++) {
6747c478bd9Sstevel@tonic-gate 	finish_realm(kdc_realmlist[i]);
6757c478bd9Sstevel@tonic-gate 	kdc_realmlist[i] = 0;
6767c478bd9Sstevel@tonic-gate     }
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  outline:
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate  process args & setup
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate  initialize database access (fetch master key, open DB)
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate  initialize network
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate  loop:
6897c478bd9Sstevel@tonic-gate  	listen for packet
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	determine packet type, dispatch to handling routine
6927c478bd9Sstevel@tonic-gate 		(AS or TGS (or V4?))
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	reflect response
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	exit on signal
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate  clean up secrets, close db
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate  shut down network
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate  exit
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate 
70556a424ccSmp153739 int main(int argc, char **argv)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
7087c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
7097c478bd9Sstevel@tonic-gate     int errout = 0;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL, "");
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
7147c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"KRB5KDC_TEST"	/* Use this only if it weren't */
7157c478bd9Sstevel@tonic-gate #endif
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate     (void) textdomain(TEXT_DOMAIN);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate     if (strrchr(argv[0], '/'))
7207c478bd9Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/')+1;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate     if (!(kdc_realmlist = (kdc_realm_t **) malloc(sizeof(kdc_realm_t *) *
7237c478bd9Sstevel@tonic-gate 						  KRB5_KDC_MAX_REALMS))) {
7247c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%s: cannot get memory for realm list\n"), argv[0]);
7257c478bd9Sstevel@tonic-gate 	exit(1);
7267c478bd9Sstevel@tonic-gate     }
7277c478bd9Sstevel@tonic-gate     memset((char *) kdc_realmlist, 0,
7287c478bd9Sstevel@tonic-gate 	   (size_t) (sizeof(kdc_realm_t *) * KRB5_KDC_MAX_REALMS));
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate     /*
7317c478bd9Sstevel@tonic-gate      * A note about Kerberos contexts: This context, "kcontext", is used
7327c478bd9Sstevel@tonic-gate      * for the KDC operations, i.e. setup, network connection and error
7337c478bd9Sstevel@tonic-gate      * reporting.  The per-realm operations use the "realm_context"
7347c478bd9Sstevel@tonic-gate      * associated with each realm.
7357c478bd9Sstevel@tonic-gate      */
736*54925bf6Swillf     retval = krb5int_init_context_kdc(&kcontext);
7377c478bd9Sstevel@tonic-gate     if (retval) {
7387c478bd9Sstevel@tonic-gate 	    com_err(argv[0], retval, gettext("while initializing krb5"));
7397c478bd9Sstevel@tonic-gate 	    exit(1);
7407c478bd9Sstevel@tonic-gate     }
7417c478bd9Sstevel@tonic-gate     krb5_klog_init(kcontext, "kdc", argv[0], 1);
7427c478bd9Sstevel@tonic-gate     /* initialize_kdc5_error_table();  SUNWresync121 XXX */
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate     /*
7457c478bd9Sstevel@tonic-gate      * Scan through the argument list
7467c478bd9Sstevel@tonic-gate      */
7477c478bd9Sstevel@tonic-gate     initialize_realms(kcontext, argc, argv);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate     setup_signal_handlers();
7507c478bd9Sstevel@tonic-gate 
75156a424ccSmp153739     retval = setup_sam();
75256a424ccSmp153739     if (retval) {
7537c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while initializing SAM"));
7547c478bd9Sstevel@tonic-gate 	finish_realms(argv[0]);
7557c478bd9Sstevel@tonic-gate 	return 1;
7567c478bd9Sstevel@tonic-gate     }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate     if ((retval = setup_network(argv[0]))) {
7597c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while initializing network"));
7607c478bd9Sstevel@tonic-gate 	finish_realms(argv[0]);
7617c478bd9Sstevel@tonic-gate 	return 1;
7627c478bd9Sstevel@tonic-gate     }
7637c478bd9Sstevel@tonic-gate     if (!nofork && daemon(0, 0)) {
7647c478bd9Sstevel@tonic-gate 	com_err(argv[0], errno, gettext("while detaching from tty"));
7657c478bd9Sstevel@tonic-gate 	finish_realms(argv[0]);
7667c478bd9Sstevel@tonic-gate 	return 1;
7677c478bd9Sstevel@tonic-gate     }
7687c478bd9Sstevel@tonic-gate     if (retval = krb5_klog_syslog(LOG_INFO, "commencing operation")) {
7697c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while logging message"));
7707c478bd9Sstevel@tonic-gate 	errout++;
7717c478bd9Sstevel@tonic-gate 	};
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate     if ((retval = listen_and_process(argv[0]))) {
7747c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while processing network requests"));
7757c478bd9Sstevel@tonic-gate 	errout++;
7767c478bd9Sstevel@tonic-gate     }
7777c478bd9Sstevel@tonic-gate     if ((retval = closedown_network(argv[0]))) {
7787c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while shutting down network"));
7797c478bd9Sstevel@tonic-gate 	errout++;
7807c478bd9Sstevel@tonic-gate     }
7817c478bd9Sstevel@tonic-gate     krb5_klog_syslog(LOG_INFO, "shutting down");
7827c478bd9Sstevel@tonic-gate     krb5_klog_close(kdc_context);
7837c478bd9Sstevel@tonic-gate     finish_realms(argv[0]);
78456a424ccSmp153739     if (kdc_realmlist)
78556a424ccSmp153739       free(kdc_realmlist);
78656a424ccSmp153739 #ifdef USE_RCACHE
78756a424ccSmp153739     (void) krb5_rc_close(kcontext, kdc_rcache);
78856a424ccSmp153739 #endif
78956a424ccSmp153739 #ifndef NOCACHE
79056a424ccSmp153739     kdc_free_lookaside(kcontext);
79156a424ccSmp153739 #endif
7927c478bd9Sstevel@tonic-gate     krb5_free_context(kcontext);
7937c478bd9Sstevel@tonic-gate     return errout;
7947c478bd9Sstevel@tonic-gate }
79556a424ccSmp153739 
79656a424ccSmp153739 
79756a424ccSmp153739 
79856a424ccSmp153739 
799