xref: /netbsd-src/crypto/external/bsd/heimdal/dist/kadmin/kadmind.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: kadmind.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "kadmin_locl.h"
37 
38 static char *check_library  = NULL;
39 static char *check_function = NULL;
40 static getarg_strings policy_libraries = { 0, NULL };
41 static char *config_file;
42 static char sHDB[] = "HDB:";
43 static char *keytab_str = sHDB;
44 static int help_flag;
45 static int version_flag;
46 static int debug_flag;
47 static char *port_str;
48 char *realm;
49 
50 static struct getargs args[] = {
51     {
52 	"config-file",	'c',	arg_string,	&config_file,
53 	"location of config file",	"file"
54     },
55     {
56 	"keytab",	0,	arg_string, &keytab_str,
57 	"what keytab to use", "keytab"
58     },
59     {	"realm",	'r',	arg_string,   &realm,
60 	"realm to use", "realm"
61     },
62 #ifdef HAVE_DLOPEN
63     { "check-library", 0, arg_string, &check_library,
64       "library to load password check function from", "library" },
65     { "check-function", 0, arg_string, &check_function,
66       "password check function to load", "function" },
67     { "policy-libraries", 0, arg_strings, &policy_libraries,
68       "password check function to load", "function" },
69 #endif
70     {	"debug",	'd',	arg_flag,   &debug_flag,
71 	"enable debugging", NULL
72     },
73     {	"ports",	'p',	arg_string, &port_str,
74 	"ports to listen to", "port" },
75     {	"help",		'h',	arg_flag,   &help_flag, NULL, NULL },
76     {	"version",	'v',	arg_flag,   &version_flag, NULL, NULL }
77 };
78 
79 static int num_args = sizeof(args) / sizeof(args[0]);
80 
81 krb5_context context;
82 
83 static void
84 usage(int ret)
85 {
86     arg_printusage (args, num_args, NULL, "");
87     exit (ret);
88 }
89 
90 int
91 main(int argc, char **argv)
92 {
93     krb5_error_code ret;
94     char **files;
95     int optidx = 0;
96     int i;
97     krb5_log_facility *logfacility;
98     krb5_keytab keytab;
99     krb5_socket_t sfd = rk_INVALID_SOCKET;
100 
101     setprogname(argv[0]);
102 
103     ret = krb5_init_context(&context);
104     if (ret)
105 	errx (1, "krb5_init_context failed: %d", ret);
106 
107     if (getarg(args, num_args, argc, argv, &optidx)) {
108 	warnx("error at argument `%s'", argv[optidx]);
109 	usage(1);
110     }
111 
112     if (help_flag)
113 	usage (0);
114 
115     if (version_flag) {
116 	print_version(NULL);
117 	exit(0);
118     }
119 
120     argc -= optidx;
121     argv += optidx;
122 
123     if (config_file == NULL) {
124 	asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
125 	if (config_file == NULL)
126 	    errx(1, "out of memory");
127     }
128 
129     ret = krb5_prepend_config_files_default(config_file, &files);
130     if (ret)
131 	krb5_err(context, 1, ret, "getting configuration files");
132 
133     ret = krb5_set_config_files(context, files);
134     krb5_free_config_files(files);
135     if(ret)
136 	krb5_err(context, 1, ret, "reading configuration files");
137 
138     ret = krb5_openlog(context, "kadmind", &logfacility);
139     if (ret)
140 	krb5_err(context, 1, ret, "krb5_openlog");
141     ret = krb5_set_warn_dest(context, logfacility);
142     if (ret)
143 	krb5_err(context, 1, ret, "krb5_set_warn_dest");
144 
145     ret = krb5_kt_register(context, &hdb_kt_ops);
146     if(ret)
147 	krb5_err(context, 1, ret, "krb5_kt_register");
148 
149     ret = krb5_kt_resolve(context, keytab_str, &keytab);
150     if(ret)
151 	krb5_err(context, 1, ret, "krb5_kt_resolve");
152 
153     kadm5_setup_passwd_quality_check (context, check_library, check_function);
154 
155     for (i = 0; i < policy_libraries.num_strings; i++) {
156 	ret = kadm5_add_passwd_quality_verifier(context,
157 						policy_libraries.strings[i]);
158 	if (ret)
159 	    krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
160     }
161     ret = kadm5_add_passwd_quality_verifier(context, NULL);
162     if (ret)
163 	krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
164 
165     if(debug_flag) {
166 	int debug_port;
167 
168 	if(port_str == NULL)
169 	    debug_port = krb5_getportbyname (context, "kerberos-adm",
170 					     "tcp", 749);
171 	else
172 	    debug_port = htons(atoi(port_str));
173 	mini_inetd(debug_port, &sfd);
174     } else {
175 #ifdef _WIN32
176 	pidfile(NULL);
177 	start_server(context, port_str);
178 #else
179 	struct sockaddr_storage __ss;
180 	struct sockaddr *sa = (struct sockaddr *)&__ss;
181 	socklen_t sa_size = sizeof(__ss);
182 
183 	/*
184 	 * Check if we are running inside inetd or not, if not, start
185 	 * our own server.
186 	 */
187 
188 	if(roken_getsockname(STDIN_FILENO, sa, &sa_size) < 0 &&
189 	   rk_SOCK_ERRNO == ENOTSOCK) {
190 	    pidfile(NULL);
191 	    start_server(context, port_str);
192 	}
193 #endif /* _WIN32 */
194 	sfd = STDIN_FILENO;
195     }
196 
197     if(realm)
198 	krb5_set_default_realm(context, realm); /* XXX */
199 
200     kadmind_loop(context, keytab, sfd);
201 
202     return 0;
203 }
204