xref: /freebsd/crypto/heimdal/kadmin/kadmind.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "kadmin_locl.h"
35 
36 RCSID("$Id: kadmind.c,v 1.16 1999/12/02 17:04:58 joda Exp $");
37 
38 static char *config_file;
39 static char *keyfile;
40 static char *keytab_str = "HDB:";
41 static int help_flag;
42 static int version_flag;
43 static int debug_flag;
44 static int debug_port;
45 char *realm;
46 
47 static struct getargs args[] = {
48     {
49 	"config-file",	'c',	arg_string,	&config_file,
50 	"location of config file",	"file"
51     },
52     {
53 	"key-file",	'k',	arg_string, &keyfile,
54 	"location of master key file", "file"
55     },
56     {
57 	"keytab",	0,	arg_string, &keytab_str,
58 	"what keytab to use", "keytab"
59     },
60     {	"realm",	'r',	arg_string,   &realm,
61 	"realm to use", "realm"
62     },
63     {	"debug",	'd',	arg_flag,   &debug_flag,
64 	"enable debugging"
65     },
66     {	"debug-port",	'p',	arg_integer,&debug_port,
67 	"port to use with debug", "port" },
68     {	"help",		'h',	arg_flag,   &help_flag },
69     {	"version",	'v',	arg_flag,   &version_flag }
70 };
71 
72 static int num_args = sizeof(args) / sizeof(args[0]);
73 
74 krb5_context context;
75 
76 static void
77 usage(int ret)
78 {
79     arg_printusage (args, num_args, NULL, "");
80     exit (ret);
81 }
82 
83 krb5_error_code
84 kadmind_loop (krb5_context, krb5_auth_context, krb5_keytab, int);
85 
86 int
87 main(int argc, char **argv)
88 {
89     krb5_error_code ret;
90     krb5_config_section *cf;
91     int optind = 0;
92     int e;
93     krb5_log_facility *logf;
94     krb5_keytab keytab;
95 
96     set_progname(argv[0]);
97 
98     krb5_init_context(&context);
99 
100     ret = krb5_openlog(context, "kadmind", &logf);
101     ret = krb5_set_warn_dest(context, logf);
102 
103     while((e = getarg(args, num_args, argc, argv, &optind)))
104 	warnx("error at argument `%s'", argv[optind]);
105 
106     if (help_flag)
107 	usage (0);
108 
109     if (version_flag) {
110 	print_version(NULL);
111 	exit(0);
112     }
113 
114     argc -= optind;
115     argv += optind;
116 
117     ret = krb5_kt_register(context, &hdb_kt_ops);
118     if(ret)
119 	krb5_err(context, 1, ret, "krb5_kt_register");
120 
121     if (config_file == NULL)
122 	config_file = HDB_DB_DIR "/kdc.conf";
123 
124     if(krb5_config_parse_file(config_file, &cf) == 0) {
125 	const char *p = krb5_config_get_string (context, cf,
126 						"kdc", "key-file", NULL);
127 	if (p)
128 	    keyfile = strdup(p);
129     }
130 
131     ret = krb5_kt_resolve(context, keytab_str, &keytab);
132     if(ret)
133 	krb5_err(context, 1, ret, "krb5_kt_resolve");
134 
135     {
136 	int fd = 0;
137 	krb5_auth_context ac = NULL;
138 	if(debug_flag){
139 	    if(debug_port == 0)
140 		debug_port = krb5_getportbyname (context, "kerberos-adm",
141 						 "tcp", 749);
142 	    else
143 		debug_port = htons(debug_port);
144 	    mini_inetd(debug_port);
145 	}
146 	if(realm)
147 	    krb5_set_default_realm(context, realm); /* XXX */
148 	kadmind_loop(context, ac, keytab, fd);
149     }
150     return 0;
151 }
152