xref: /titanic_52/usr/src/cmd/krb5/krb5kdc/main.c (revision 56a424cca6b3f91f31bdab72a4626c48c779fe8b)
17c478bd9Sstevel@tonic-gate /*
2*56a424ccSmp153739  * Copyright 2006 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 
56*56a424ccSmp153739 #ifdef KRB5_KRB4_COMPAT
57*56a424ccSmp153739 #include <des.h>
58*56a424ccSmp153739 #endif
59*56a424ccSmp153739 
60*56a424ccSmp153739 #if defined(NEED_DAEMON_PROTO)
61*56a424ccSmp153739 extern int daemon(int, int);
62*56a424ccSmp153739 #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 *
93*56a424ccSmp153739 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
105*56a424ccSmp153739 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
124*56a424ccSmp153739 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     }
150*56a424ccSmp153739     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*56a424ccSmp153739 init_realm(char *progname, kdc_realm_t *rdp, char *realm, char *def_dbname,
163*56a424ccSmp153739 	   char *def_mpname, krb5_enctype def_enctype, char *def_udp_ports,
164*56a424ccSmp153739 	   char *def_tcp_ports, krb5_boolean def_manual)
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;
1777c478bd9Sstevel@tonic-gate     kret = krb5_init_context(&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 database name */
1967c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_dbname)
1977c478bd9Sstevel@tonic-gate 	rdp->realm_dbname = strdup(rparams->realm_dbname);
1987c478bd9Sstevel@tonic-gate     else
1997c478bd9Sstevel@tonic-gate 	rdp->realm_dbname = (def_dbname) ? strdup(def_dbname) :
2007c478bd9Sstevel@tonic-gate 	    strdup(DEFAULT_KDB_FILE);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate     /* Handle master key name */
2037c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_mkey_name)
2047c478bd9Sstevel@tonic-gate 	rdp->realm_mpname = strdup(rparams->realm_mkey_name);
2057c478bd9Sstevel@tonic-gate     else
2067c478bd9Sstevel@tonic-gate 	rdp->realm_mpname = (def_mpname) ? strdup(def_mpname) :
2077c478bd9Sstevel@tonic-gate 	    strdup(KRB5_KDB_M_NAME);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate     /* Handle KDC ports */
2107c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_kdc_ports)
2117c478bd9Sstevel@tonic-gate 	rdp->realm_ports = strdup(rparams->realm_kdc_ports);
2127c478bd9Sstevel@tonic-gate     else
2137c478bd9Sstevel@tonic-gate 	rdp->realm_ports = strdup(def_udp_ports);
2147c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_kdc_tcp_ports)
2157c478bd9Sstevel@tonic-gate 	rdp->realm_tcp_ports = strdup(rparams->realm_kdc_tcp_ports);
2167c478bd9Sstevel@tonic-gate     else
2177c478bd9Sstevel@tonic-gate 	rdp->realm_tcp_ports = strdup(def_tcp_ports);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate     /* Handle stash file */
2207c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_stash_file) {
2217c478bd9Sstevel@tonic-gate 	rdp->realm_stash = strdup(rparams->realm_stash_file);
2227c478bd9Sstevel@tonic-gate 	manual = FALSE;
2237c478bd9Sstevel@tonic-gate     } else
2247c478bd9Sstevel@tonic-gate 	manual = def_manual;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate     /* Handle master key type */
2277c478bd9Sstevel@tonic-gate     if (rparams && rparams->realm_enctype_valid)
2287c478bd9Sstevel@tonic-gate 	rdp->realm_mkey.enctype = (krb5_enctype) rparams->realm_enctype;
2297c478bd9Sstevel@tonic-gate     else
2307c478bd9Sstevel@tonic-gate 	rdp->realm_mkey.enctype = manual ? def_enctype : ENCTYPE_UNKNOWN;
231*56a424ccSmp153739 
232*56a424ccSmp153739     /* Handle reject-bad-transit flag */
233*56a424ccSmp153739     if (rparams && rparams->realm_reject_bad_transit_valid)
234*56a424ccSmp153739 	rdp->realm_reject_bad_transit = rparams->realm_reject_bad_transit;
235*56a424ccSmp153739     else
236*56a424ccSmp153739 	rdp->realm_reject_bad_transit = 1;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate     /* Handle ticket maximum life */
239*56a424ccSmp153739     rdp->realm_maxlife = (rparams && rparams->realm_max_life_valid) ?
240*56a424ccSmp153739 	rparams->realm_max_life : KRB5_KDB_MAX_LIFE;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate     /* Handle ticket renewable maximum life */
243*56a424ccSmp153739     rdp->realm_maxrlife = (rparams && rparams->realm_max_rlife_valid) ?
244*56a424ccSmp153739 	rparams->realm_max_rlife : KRB5_KDB_MAX_RLIFE;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate     if (rparams)
2477c478bd9Sstevel@tonic-gate 	krb5_free_realm_params(rdp->realm_context, rparams);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate     /*
2507c478bd9Sstevel@tonic-gate      * We've got our parameters, now go and setup our realm context.
2517c478bd9Sstevel@tonic-gate      */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate     /* Set the default realm of this context */
2547c478bd9Sstevel@tonic-gate     if ((kret = krb5_set_default_realm(rdp->realm_context, realm))) {
2557c478bd9Sstevel@tonic-gate 	com_err(progname, kret, gettext("while setting default realm to %s"),
2567c478bd9Sstevel@tonic-gate 		realm);
2577c478bd9Sstevel@tonic-gate 	goto whoops;
2587c478bd9Sstevel@tonic-gate     }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate     /* Assemble and parse the master key name */
2617c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_setup_mkey_name(rdp->realm_context, rdp->realm_mpname,
2627c478bd9Sstevel@tonic-gate 					rdp->realm_name, (char **) NULL,
2637c478bd9Sstevel@tonic-gate 					&rdp->realm_mprinc))) {
2647c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2657c478bd9Sstevel@tonic-gate 		gettext("while setting up master key name %s for realm %s"),
2667c478bd9Sstevel@tonic-gate 		rdp->realm_mpname, realm);
2677c478bd9Sstevel@tonic-gate 	goto whoops;
2687c478bd9Sstevel@tonic-gate     }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate     /*
2717c478bd9Sstevel@tonic-gate      * Get the master key.
2727c478bd9Sstevel@tonic-gate      */
2737c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_fetch_mkey(rdp->realm_context, rdp->realm_mprinc,
2747c478bd9Sstevel@tonic-gate 				   rdp->realm_mkey.enctype, manual,
2757c478bd9Sstevel@tonic-gate 				   FALSE, rdp->realm_stash,
2767c478bd9Sstevel@tonic-gate 				   0, &rdp->realm_mkey))) {
2777c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2787c478bd9Sstevel@tonic-gate 		gettext("while fetching master key %s for realm %s"),
2797c478bd9Sstevel@tonic-gate 		rdp->realm_mpname, realm);
2807c478bd9Sstevel@tonic-gate 	goto whoops;
2817c478bd9Sstevel@tonic-gate     }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate     /* Set and open the database. */
2847c478bd9Sstevel@tonic-gate     if (rdp->realm_dbname &&
2857c478bd9Sstevel@tonic-gate 	(kret = krb5_db_set_name(rdp->realm_context, rdp->realm_dbname))) {
2867c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2877c478bd9Sstevel@tonic-gate 		gettext("while setting database name to %s for realm %s"),
2887c478bd9Sstevel@tonic-gate 		rdp->realm_dbname, realm);
2897c478bd9Sstevel@tonic-gate 	goto whoops;
2907c478bd9Sstevel@tonic-gate     }
2917c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_init(rdp->realm_context))) {
2927c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
2937c478bd9Sstevel@tonic-gate 		gettext("while initializing database "),
2947c478bd9Sstevel@tonic-gate 		gettext("for realm %s"), realm);
2957c478bd9Sstevel@tonic-gate 	goto whoops;
296*56a424ccSmp153739     }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate     /* Verify the master key */
2997c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_verify_master_key(rdp->realm_context,
3007c478bd9Sstevel@tonic-gate 					  rdp->realm_mprinc,
3017c478bd9Sstevel@tonic-gate 					  &rdp->realm_mkey))) {
3027c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3037c478bd9Sstevel@tonic-gate 		gettext("while verifying master key for realm %s"),
3047c478bd9Sstevel@tonic-gate 		realm);
3057c478bd9Sstevel@tonic-gate 	goto whoops;
3067c478bd9Sstevel@tonic-gate     }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate     if ((kret = krb5_db_set_mkey(rdp->realm_context, &rdp->realm_mkey))) {
3097c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3107c478bd9Sstevel@tonic-gate 		gettext("while processing master key for realm %s"),
3117c478bd9Sstevel@tonic-gate 		realm);
3127c478bd9Sstevel@tonic-gate 	goto whoops;
3137c478bd9Sstevel@tonic-gate     }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate     /* Set up the keytab */
316*56a424ccSmp153739     if ((kret = krb5_ktkdb_resolve(rdp->realm_context, NULL,
3177c478bd9Sstevel@tonic-gate 				   &rdp->realm_keytab))) {
3187c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3197c478bd9Sstevel@tonic-gate 		gettext("while resolving kdb keytab for realm %s"),
3207c478bd9Sstevel@tonic-gate 		realm);
3217c478bd9Sstevel@tonic-gate 	goto whoops;
3227c478bd9Sstevel@tonic-gate     }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate     /* Preformat the TGS name */
3257c478bd9Sstevel@tonic-gate     if ((kret = krb5_build_principal(rdp->realm_context, &rdp->realm_tgsprinc,
3267c478bd9Sstevel@tonic-gate 				     strlen(realm), realm, KRB5_TGS_NAME,
3277c478bd9Sstevel@tonic-gate 				     realm, (char *) NULL))) {
3287c478bd9Sstevel@tonic-gate 	com_err(progname, kret,
3297c478bd9Sstevel@tonic-gate 		gettext("while building TGS name for realm %s"),
3307c478bd9Sstevel@tonic-gate 		realm);
3317c478bd9Sstevel@tonic-gate 	goto whoops;
3327c478bd9Sstevel@tonic-gate     }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate     if (!rkey_init_done) {
3357c478bd9Sstevel@tonic-gate 	krb5_data seed;
3367c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
3377c478bd9Sstevel@tonic-gate 	krb5_keyblock temp_key;
3387c478bd9Sstevel@tonic-gate #endif
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * If all that worked, then initialize the random key
3417c478bd9Sstevel@tonic-gate 	 * generators.
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	seed.length = rdp->realm_mkey.length;
3457c478bd9Sstevel@tonic-gate 	seed.data = (char *)rdp->realm_mkey.contents;
346*56a424ccSmp153739 /* SUNW14resync - XXX */
347*56a424ccSmp153739 #if 0
348*56a424ccSmp153739 	if ((kret = krb5_c_random_add_entropy(rdp->realm_context,
349*56a424ccSmp153739 					     KRB5_C_RANDSOURCE_TRUSTEDPARTY, &seed)))
3507c478bd9Sstevel@tonic-gate 	    goto whoops;
351*56a424ccSmp153739 #endif
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
3547c478bd9Sstevel@tonic-gate 	if ((kret = krb5_c_make_random_key(rdp->realm_context,
3557c478bd9Sstevel@tonic-gate 					   ENCTYPE_DES_CBC_CRC, &temp_key))) {
3567c478bd9Sstevel@tonic-gate 	    com_err(progname, kret,
3577c478bd9Sstevel@tonic-gate 		    "while initializing V4 random key generator");
3587c478bd9Sstevel@tonic-gate 	    goto whoops;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	(void) des_init_random_number_generator(temp_key.contents);
3627c478bd9Sstevel@tonic-gate 	krb5_free_keyblock_contents(rdp->realm_context, &temp_key);
3637c478bd9Sstevel@tonic-gate #endif
3647c478bd9Sstevel@tonic-gate 	rkey_init_done = 1;
3657c478bd9Sstevel@tonic-gate     }
3667c478bd9Sstevel@tonic-gate  whoops:
3677c478bd9Sstevel@tonic-gate     /*
3687c478bd9Sstevel@tonic-gate      * If we choked, then clean up any dirt we may have dropped on the floor.
3697c478bd9Sstevel@tonic-gate      */
3707c478bd9Sstevel@tonic-gate     if (kret) {
371*56a424ccSmp153739 
3727c478bd9Sstevel@tonic-gate 	finish_realm(rdp);
3737c478bd9Sstevel@tonic-gate     }
3747c478bd9Sstevel@tonic-gate     return(kret);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate krb5_sigtype
378*56a424ccSmp153739 request_exit(int signo)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate     signal_requests_exit = 1;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGTYPE
3837c478bd9Sstevel@tonic-gate     return;
3847c478bd9Sstevel@tonic-gate #else
3857c478bd9Sstevel@tonic-gate     return(0);
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate krb5_sigtype
390*56a424ccSmp153739 request_hup(int signo)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate     signal_requests_hup = 1;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGTYPE
3957c478bd9Sstevel@tonic-gate     return;
3967c478bd9Sstevel@tonic-gate #else
3977c478bd9Sstevel@tonic-gate     return(0);
3987c478bd9Sstevel@tonic-gate #endif
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate void
402*56a424ccSmp153739 setup_signal_handlers(void)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate #ifdef POSIX_SIGNALS
4057c478bd9Sstevel@tonic-gate     (void) sigemptyset(&s_action.sa_mask);
4067c478bd9Sstevel@tonic-gate     s_action.sa_flags = 0;
4077c478bd9Sstevel@tonic-gate     s_action.sa_handler = request_exit;
4087c478bd9Sstevel@tonic-gate     (void) sigaction(SIGINT, &s_action, (struct sigaction *) NULL);
4097c478bd9Sstevel@tonic-gate     (void) sigaction(SIGTERM, &s_action, (struct sigaction *) NULL);
4107c478bd9Sstevel@tonic-gate     s_action.sa_handler = request_hup;
4117c478bd9Sstevel@tonic-gate     (void) sigaction(SIGHUP, &s_action, (struct sigaction *) NULL);
4127c478bd9Sstevel@tonic-gate #else  /* POSIX_SIGNALS */
4137c478bd9Sstevel@tonic-gate     signal(SIGINT, request_exit);
4147c478bd9Sstevel@tonic-gate     signal(SIGTERM, request_exit);
4157c478bd9Sstevel@tonic-gate     signal(SIGHUP, request_hup);
4167c478bd9Sstevel@tonic-gate #endif /* POSIX_SIGNALS */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate     return;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate krb5_error_code
422*56a424ccSmp153739 setup_sam(void)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate     return krb5_c_make_random_key(kdc_context, ENCTYPE_DES_CBC_MD5, &psr_key);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate void
428*56a424ccSmp153739 usage(char *name)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@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);
4317c478bd9Sstevel@tonic-gate     return;
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate void
435*56a424ccSmp153739 initialize_realms(krb5_context kcontext, int argc, char **argv)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate     int 		c;
4387c478bd9Sstevel@tonic-gate     char		*db_name = (char *) NULL;
4397c478bd9Sstevel@tonic-gate     char		*mkey_name = (char *) NULL;
4407c478bd9Sstevel@tonic-gate     char		*rcname = KDCRCACHE;
4417c478bd9Sstevel@tonic-gate     char		*lrealm;
4427c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
4437c478bd9Sstevel@tonic-gate     krb5_enctype	menctype = ENCTYPE_UNKNOWN;
4447c478bd9Sstevel@tonic-gate     kdc_realm_t		*rdatap;
4457c478bd9Sstevel@tonic-gate     krb5_boolean	manual = FALSE;
4467c478bd9Sstevel@tonic-gate     char		*default_udp_ports = 0;
4477c478bd9Sstevel@tonic-gate     char		*default_tcp_ports = 0;
4487c478bd9Sstevel@tonic-gate     krb5_pointer	aprof;
4497c478bd9Sstevel@tonic-gate     const char		*hierarchy[3];
4507c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
4517c478bd9Sstevel@tonic-gate     char                *v4mode = 0;
4527c478bd9Sstevel@tonic-gate #endif
4537c478bd9Sstevel@tonic-gate     extern char *optarg;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate     if (!krb5_aprof_init(DEFAULT_KDC_PROFILE, KDC_PROFILE_ENV, &aprof)) {
4567c478bd9Sstevel@tonic-gate 	hierarchy[0] = "kdcdefaults";
4577c478bd9Sstevel@tonic-gate 	hierarchy[1] = "kdc_ports";
4587c478bd9Sstevel@tonic-gate 	hierarchy[2] = (char *) NULL;
4597c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_string(aprof, hierarchy, TRUE, &default_udp_ports))
4607c478bd9Sstevel@tonic-gate 	    default_udp_ports = 0;
4617c478bd9Sstevel@tonic-gate 	hierarchy[1] = "kdc_tcp_ports";
4627c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_string(aprof, hierarchy, TRUE, &default_tcp_ports))
4637c478bd9Sstevel@tonic-gate 	    default_tcp_ports = 0;
4647c478bd9Sstevel@tonic-gate 	hierarchy[1] = "kdc_max_tcp_connections";
4657c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_int32(aprof, hierarchy, TRUE,
4667c478bd9Sstevel@tonic-gate 		&max_tcp_data_connections)) {
4677c478bd9Sstevel@tonic-gate 	    max_tcp_data_connections = DEFAULT_KDC_TCP_CONNECTIONS;
4687c478bd9Sstevel@tonic-gate 	} else if (max_tcp_data_connections < MIN_KDC_TCP_CONNECTIONS) {
4697c478bd9Sstevel@tonic-gate 	    max_tcp_data_connections = DEFAULT_KDC_TCP_CONNECTIONS;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
4727c478bd9Sstevel@tonic-gate 	hierarchy[1] = "v4_mode";
4737c478bd9Sstevel@tonic-gate 	if (krb5_aprof_get_string(aprof, hierarchy, TRUE, &v4mode))
4747c478bd9Sstevel@tonic-gate 	    v4mode = 0;
4757c478bd9Sstevel@tonic-gate #endif
4767c478bd9Sstevel@tonic-gate 	/* aprof_init can return 0 with aprof == NULL */
4777c478bd9Sstevel@tonic-gate 	if (aprof)
4787c478bd9Sstevel@tonic-gate 	     krb5_aprof_finish(aprof);
4797c478bd9Sstevel@tonic-gate     }
4807c478bd9Sstevel@tonic-gate     if (default_udp_ports == 0)
4817c478bd9Sstevel@tonic-gate 	default_udp_ports = strdup(DEFAULT_KDC_UDP_PORTLIST);
4827c478bd9Sstevel@tonic-gate     if (default_tcp_ports == 0)
4837c478bd9Sstevel@tonic-gate 	default_tcp_ports = strdup(DEFAULT_KDC_TCP_PORTLIST);
4847c478bd9Sstevel@tonic-gate     /*
4857c478bd9Sstevel@tonic-gate      * Loop through the option list.  Each time we encounter a realm name,
4867c478bd9Sstevel@tonic-gate      * use the previously scanned options to fill in for defaults.
4877c478bd9Sstevel@tonic-gate      */
4887c478bd9Sstevel@tonic-gate     while ((c = getopt(argc, argv, "r:d:mM:k:R:e:p:s:n")) != -1) { /* SUNW */
4897c478bd9Sstevel@tonic-gate 	switch(c) {
4907c478bd9Sstevel@tonic-gate 	case 'r':			/* realm name for db */
4917c478bd9Sstevel@tonic-gate 	    if (!find_realm_data(optarg, (krb5_ui_4) strlen(optarg))) {
4927c478bd9Sstevel@tonic-gate 		if ((rdatap = (kdc_realm_t *) malloc(sizeof(kdc_realm_t)))) {
4937c478bd9Sstevel@tonic-gate 		    if ((retval = init_realm(argv[0], rdatap, optarg, db_name,
4947c478bd9Sstevel@tonic-gate 					     mkey_name, menctype,
4957c478bd9Sstevel@tonic-gate 					     default_udp_ports,
4967c478bd9Sstevel@tonic-gate 					     default_tcp_ports, manual))) {
4977c478bd9Sstevel@tonic-gate 			fprintf(stderr,gettext("%s: cannot initialize realm %s\n"),
4987c478bd9Sstevel@tonic-gate 				argv[0], optarg);
4997c478bd9Sstevel@tonic-gate 			exit(1);
5007c478bd9Sstevel@tonic-gate 		    }
5017c478bd9Sstevel@tonic-gate 		    kdc_realmlist[kdc_numrealms] = rdatap;
5027c478bd9Sstevel@tonic-gate 		    kdc_numrealms++;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 	    }
5057c478bd9Sstevel@tonic-gate 	    break;
5067c478bd9Sstevel@tonic-gate 	case 'd':			/* pathname for db */
5077c478bd9Sstevel@tonic-gate 	    db_name = optarg;
5087c478bd9Sstevel@tonic-gate 	    break;
5097c478bd9Sstevel@tonic-gate 	case 'm':			/* manual type-in of master key */
5107c478bd9Sstevel@tonic-gate 	    manual = TRUE;
5117c478bd9Sstevel@tonic-gate 	    if (menctype == ENCTYPE_UNKNOWN)
5127c478bd9Sstevel@tonic-gate 		menctype = ENCTYPE_DES_CBC_CRC;
5137c478bd9Sstevel@tonic-gate 	    break;
5147c478bd9Sstevel@tonic-gate 	case 'M':			/* master key name in DB */
5157c478bd9Sstevel@tonic-gate 	    mkey_name = optarg;
5167c478bd9Sstevel@tonic-gate 	    break;
5177c478bd9Sstevel@tonic-gate 	case 'n':
5187c478bd9Sstevel@tonic-gate 	    nofork++;			/* don't detach from terminal */
5197c478bd9Sstevel@tonic-gate 	    break;
5207c478bd9Sstevel@tonic-gate 	case 'k':			/* enctype for master key */
5217c478bd9Sstevel@tonic-gate 	    if (krb5_string_to_enctype(optarg, &menctype))
5227c478bd9Sstevel@tonic-gate 		com_err(argv[0], 0,
5237c478bd9Sstevel@tonic-gate 		gettext("invalid enctype %s"), optarg);
5247c478bd9Sstevel@tonic-gate 	    break;
5257c478bd9Sstevel@tonic-gate 	case 'R':
5267c478bd9Sstevel@tonic-gate 	    rcname = optarg;
5277c478bd9Sstevel@tonic-gate 	    break;
5287c478bd9Sstevel@tonic-gate 	case 'p':
5297c478bd9Sstevel@tonic-gate 	    if (default_udp_ports)
5307c478bd9Sstevel@tonic-gate 		free(default_udp_ports);
5317c478bd9Sstevel@tonic-gate 	    default_udp_ports = strdup(optarg);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	    if (default_tcp_ports)
5347c478bd9Sstevel@tonic-gate 		free(default_tcp_ports);
5357c478bd9Sstevel@tonic-gate 	    default_tcp_ports = strdup(optarg);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	    break;
5387c478bd9Sstevel@tonic-gate 	case '4':
5397c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
5407c478bd9Sstevel@tonic-gate 	    if (v4mode)
5417c478bd9Sstevel@tonic-gate 		free(v4mode);
5427c478bd9Sstevel@tonic-gate 	    v4mode = strdup(optarg);
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate 	    break;
545*56a424ccSmp153739 	case 'X':
546*56a424ccSmp153739 #ifdef KRB5_KRB4_COMPAT
547*56a424ccSmp153739 		enable_v4_crossrealm(argv[0]);
5487c478bd9Sstevel@tonic-gate #endif
549*56a424ccSmp153739 		break;
5507c478bd9Sstevel@tonic-gate 	case '?':
5517c478bd9Sstevel@tonic-gate 	default:
5527c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
5537c478bd9Sstevel@tonic-gate 	    exit(1);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate     }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
5587c478bd9Sstevel@tonic-gate     /*
5597c478bd9Sstevel@tonic-gate      * Setup the v4 mode
5607c478bd9Sstevel@tonic-gate      */
5617c478bd9Sstevel@tonic-gate     process_v4_mode(argv[0], v4mode);
5627c478bd9Sstevel@tonic-gate #endif
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate     /*
5657c478bd9Sstevel@tonic-gate      * Check to see if we processed any realms.
5667c478bd9Sstevel@tonic-gate      */
5677c478bd9Sstevel@tonic-gate     if (kdc_numrealms == 0) {
5687c478bd9Sstevel@tonic-gate 	/* no realm specified, use default realm */
5697c478bd9Sstevel@tonic-gate 	if ((retval = krb5_get_default_realm(kcontext, &lrealm))) {
5707c478bd9Sstevel@tonic-gate 	    com_err(argv[0], retval,
5717c478bd9Sstevel@tonic-gate 		gettext("while attempting to retrieve default realm"));
572*56a424ccSmp153739 	    fprintf (stderr, "%s: %s, %s", argv[0], error_message (retval),
573*56a424ccSmp153739 	        gettext("attempting to retrieve default realm\n"));
5747c478bd9Sstevel@tonic-gate 	    exit(1);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	if ((rdatap = (kdc_realm_t *) malloc(sizeof(kdc_realm_t)))) {
5777c478bd9Sstevel@tonic-gate 	    if ((retval = init_realm(argv[0], rdatap, lrealm, db_name,
5787c478bd9Sstevel@tonic-gate 				     mkey_name, menctype, default_udp_ports,
5797c478bd9Sstevel@tonic-gate 				     default_tcp_ports, manual))) {
5807c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5817c478bd9Sstevel@tonic-gate 			gettext("%s: cannot initialize realm %s\n"),
5827c478bd9Sstevel@tonic-gate 			argv[0], lrealm);
5837c478bd9Sstevel@tonic-gate 		exit(1);
5847c478bd9Sstevel@tonic-gate 	    }
5857c478bd9Sstevel@tonic-gate 	    kdc_realmlist[0] = rdatap;
5867c478bd9Sstevel@tonic-gate 	    kdc_numrealms++;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate     }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate #ifdef USE_RCACHE
5917c478bd9Sstevel@tonic-gate     /*
5927c478bd9Sstevel@tonic-gate      * Now handle the replay cache.
5937c478bd9Sstevel@tonic-gate      */
5947c478bd9Sstevel@tonic-gate     if ((retval = kdc_initialize_rcache(kcontext, rcname))) {
595*56a424ccSmp153739 	com_err(argv[0], retval, gettext("while initializing KDC replay cache '%s'"),
596*56a424ccSmp153739 		rcname);
5977c478bd9Sstevel@tonic-gate 	exit(1);
5987c478bd9Sstevel@tonic-gate     }
5997c478bd9Sstevel@tonic-gate #endif
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate     /* Ensure that this is set for our first request. */
6027c478bd9Sstevel@tonic-gate     kdc_active_realm = kdc_realmlist[0];
6037c478bd9Sstevel@tonic-gate     if (default_udp_ports)
6047c478bd9Sstevel@tonic-gate 	free(default_udp_ports);
6057c478bd9Sstevel@tonic-gate     if (default_tcp_ports)
6067c478bd9Sstevel@tonic-gate 	free(default_tcp_ports);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate     return;
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate void
612*56a424ccSmp153739 finish_realms(char *prog)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate     int i;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate     for (i = 0; i < kdc_numrealms; i++) {
6177c478bd9Sstevel@tonic-gate 	finish_realm(kdc_realmlist[i]);
6187c478bd9Sstevel@tonic-gate 	kdc_realmlist[i] = 0;
6197c478bd9Sstevel@tonic-gate     }
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  outline:
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate  process args & setup
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate  initialize database access (fetch master key, open DB)
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate  initialize network
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate  loop:
6327c478bd9Sstevel@tonic-gate  	listen for packet
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	determine packet type, dispatch to handling routine
6357c478bd9Sstevel@tonic-gate 		(AS or TGS (or V4?))
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	reflect response
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	exit on signal
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate  clean up secrets, close db
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate  shut down network
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate  exit
6467c478bd9Sstevel@tonic-gate  */
6477c478bd9Sstevel@tonic-gate 
648*56a424ccSmp153739 int main(int argc, char **argv)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
6517c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
6527c478bd9Sstevel@tonic-gate     int errout = 0;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL, "");
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
6577c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"KRB5KDC_TEST"	/* Use this only if it weren't */
6587c478bd9Sstevel@tonic-gate #endif
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate     (void) textdomain(TEXT_DOMAIN);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate     if (strrchr(argv[0], '/'))
6637c478bd9Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/')+1;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate     if (!(kdc_realmlist = (kdc_realm_t **) malloc(sizeof(kdc_realm_t *) *
6667c478bd9Sstevel@tonic-gate 						  KRB5_KDC_MAX_REALMS))) {
6677c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%s: cannot get memory for realm list\n"), argv[0]);
6687c478bd9Sstevel@tonic-gate 	exit(1);
6697c478bd9Sstevel@tonic-gate     }
6707c478bd9Sstevel@tonic-gate     memset((char *) kdc_realmlist, 0,
6717c478bd9Sstevel@tonic-gate 	   (size_t) (sizeof(kdc_realm_t *) * KRB5_KDC_MAX_REALMS));
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate     /*
6747c478bd9Sstevel@tonic-gate      * A note about Kerberos contexts: This context, "kcontext", is used
6757c478bd9Sstevel@tonic-gate      * for the KDC operations, i.e. setup, network connection and error
6767c478bd9Sstevel@tonic-gate      * reporting.  The per-realm operations use the "realm_context"
6777c478bd9Sstevel@tonic-gate      * associated with each realm.
6787c478bd9Sstevel@tonic-gate      */
6797c478bd9Sstevel@tonic-gate     retval = krb5_init_context(&kcontext);
6807c478bd9Sstevel@tonic-gate     if (retval) {
6817c478bd9Sstevel@tonic-gate 	    com_err(argv[0], retval, gettext("while initializing krb5"));
6827c478bd9Sstevel@tonic-gate 	    exit(1);
6837c478bd9Sstevel@tonic-gate     }
6847c478bd9Sstevel@tonic-gate     krb5_klog_init(kcontext, "kdc", argv[0], 1);
6857c478bd9Sstevel@tonic-gate     /* initialize_kdc5_error_table();  SUNWresync121 XXX */
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate     /*
6887c478bd9Sstevel@tonic-gate      * Scan through the argument list
6897c478bd9Sstevel@tonic-gate      */
6907c478bd9Sstevel@tonic-gate     initialize_realms(kcontext, argc, argv);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate     setup_signal_handlers();
6937c478bd9Sstevel@tonic-gate 
694*56a424ccSmp153739     retval = setup_sam();
695*56a424ccSmp153739     if (retval) {
6967c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while initializing SAM"));
6977c478bd9Sstevel@tonic-gate 	finish_realms(argv[0]);
6987c478bd9Sstevel@tonic-gate 	return 1;
6997c478bd9Sstevel@tonic-gate     }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate     if ((retval = setup_network(argv[0]))) {
7027c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while initializing network"));
7037c478bd9Sstevel@tonic-gate 	finish_realms(argv[0]);
7047c478bd9Sstevel@tonic-gate 	return 1;
7057c478bd9Sstevel@tonic-gate     }
7067c478bd9Sstevel@tonic-gate     if (!nofork && daemon(0, 0)) {
7077c478bd9Sstevel@tonic-gate 	com_err(argv[0], errno, gettext("while detaching from tty"));
7087c478bd9Sstevel@tonic-gate 	finish_realms(argv[0]);
7097c478bd9Sstevel@tonic-gate 	return 1;
7107c478bd9Sstevel@tonic-gate     }
7117c478bd9Sstevel@tonic-gate     if (retval = krb5_klog_syslog(LOG_INFO, "commencing operation")) {
7127c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while logging message"));
7137c478bd9Sstevel@tonic-gate 	errout++;
7147c478bd9Sstevel@tonic-gate 	};
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate     if ((retval = listen_and_process(argv[0]))) {
7177c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while processing network requests"));
7187c478bd9Sstevel@tonic-gate 	errout++;
7197c478bd9Sstevel@tonic-gate     }
7207c478bd9Sstevel@tonic-gate     if ((retval = closedown_network(argv[0]))) {
7217c478bd9Sstevel@tonic-gate 	com_err(argv[0], retval, gettext("while shutting down network"));
7227c478bd9Sstevel@tonic-gate 	errout++;
7237c478bd9Sstevel@tonic-gate     }
7247c478bd9Sstevel@tonic-gate     krb5_klog_syslog(LOG_INFO, "shutting down");
7257c478bd9Sstevel@tonic-gate     krb5_klog_close(kdc_context);
7267c478bd9Sstevel@tonic-gate     finish_realms(argv[0]);
727*56a424ccSmp153739     if (kdc_realmlist)
728*56a424ccSmp153739       free(kdc_realmlist);
729*56a424ccSmp153739 #ifdef USE_RCACHE
730*56a424ccSmp153739     (void) krb5_rc_close(kcontext, kdc_rcache);
731*56a424ccSmp153739 #endif
732*56a424ccSmp153739 #ifndef NOCACHE
733*56a424ccSmp153739     kdc_free_lookaside(kcontext);
734*56a424ccSmp153739 #endif
7357c478bd9Sstevel@tonic-gate     krb5_free_context(kcontext);
7367c478bd9Sstevel@tonic-gate     return errout;
7377c478bd9Sstevel@tonic-gate }
738*56a424ccSmp153739 
739*56a424ccSmp153739 
740*56a424ccSmp153739 
741*56a424ccSmp153739 
742