1 /*	$NetBSD: kadmind.c,v 1.3 2023/06/19 21:41:41 christos 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[] = "HDBGET:";
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 int detach_from_console = -1;
51 int daemon_child = -1;
52 
53 static struct getargs args[] = {
54     {
55 	"config-file",	'c',	arg_string,	&config_file,
56 	"location of config file",	"file"
57     },
58     {
59 	"keytab",	0,	arg_string, &keytab_str,
60 	"what keytab to use", "keytab"
61     },
62     {	"realm",	'r',	arg_string,   &realm,
63 	"realm to use", "realm"
64     },
65 #ifdef HAVE_DLOPEN
66     { "check-library", 0, arg_string, &check_library,
67       "library to load password check function from", "library" },
68     { "check-function", 0, arg_string, &check_function,
69       "password check function to load", "function" },
70     { "policy-libraries", 0, arg_strings, &policy_libraries,
71       "password check function to load", "function" },
72 #endif
73     {	"debug",	'd',	arg_flag,   &debug_flag,
74 	"enable debugging", NULL
75     },
76     {
77         "detach",       0 ,      arg_flag, &detach_from_console,
78         "detach from console", NULL
79     },
80     {
81         "daemon-child",       0 ,      arg_integer, &daemon_child,
82         "private argument, do not use", NULL
83     },
84     {	"ports",	'p',	arg_string, &port_str,
85 	"ports to listen to", "port" },
86     {	"help",		'h',	arg_flag,   &help_flag, NULL, NULL },
87     {	"version",	'v',	arg_flag,   &version_flag, NULL, NULL }
88 };
89 
90 static int num_args = sizeof(args) / sizeof(args[0]);
91 
92 krb5_context context;
93 
94 static void
usage(int ret)95 usage(int ret)
96 {
97     arg_printusage (args, num_args, NULL, "");
98     exit (ret);
99 }
100 
101 int
main(int argc,char ** argv)102 main(int argc, char **argv)
103 {
104     krb5_error_code ret;
105     char **files;
106     int optidx = 0;
107     int i;
108     krb5_log_facility *logfacility;
109     krb5_keytab keytab;
110     krb5_socket_t sfd = rk_INVALID_SOCKET;
111 
112     setprogname(argv[0]);
113 
114     if (getarg(args, num_args, argc, argv, &optidx)) {
115 	warnx("error at argument `%s'", argv[optidx]);
116 	usage(1);
117     }
118 
119     if (help_flag)
120 	usage (0);
121 
122     if (version_flag) {
123 	print_version(NULL);
124 	exit(0);
125     }
126 
127     if (detach_from_console > 0 && daemon_child == -1)
128         roken_detach_prep(argc, argv, "--daemon-child");
129 
130     ret = krb5_init_context(&context);
131     if (ret)
132 	errx (1, "krb5_init_context failed: %d", ret);
133 
134     argc -= optidx;
135 #ifndef __clang_analyzer__
136     argv += optidx;
137 #endif
138     if (argc != 0)
139         usage(1);
140 
141     if (config_file == NULL) {
142 	int aret;
143 
144 	aret = asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
145 	if (aret == -1)
146 	    errx(1, "out of memory");
147     }
148 
149     ret = krb5_prepend_config_files_default(config_file, &files);
150     if (ret)
151 	krb5_err(context, 1, ret, "getting configuration files");
152 
153     ret = krb5_set_config_files(context, files);
154     krb5_free_config_files(files);
155     if(ret)
156 	krb5_err(context, 1, ret, "reading configuration files");
157 
158     ret = krb5_openlog(context, "kadmind", &logfacility);
159     if (ret)
160 	krb5_err(context, 1, ret, "krb5_openlog");
161     ret = krb5_set_warn_dest(context, logfacility);
162     if (ret)
163 	krb5_err(context, 1, ret, "krb5_set_warn_dest");
164 
165     ret = krb5_kt_register(context, &hdb_get_kt_ops);
166     if(ret)
167 	krb5_err(context, 1, ret, "krb5_kt_register");
168 
169     ret = krb5_kt_resolve(context, keytab_str, &keytab);
170     if(ret)
171 	krb5_err(context, 1, ret, "krb5_kt_resolve");
172 
173     kadm5_setup_passwd_quality_check (context, check_library, check_function);
174 
175     for (i = 0; i < policy_libraries.num_strings; i++) {
176 	ret = kadm5_add_passwd_quality_verifier(context,
177 						policy_libraries.strings[i]);
178 	if (ret)
179 	    krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
180     }
181     ret = kadm5_add_passwd_quality_verifier(context, NULL);
182     if (ret)
183 	krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
184 
185     if(debug_flag) {
186 	int debug_port;
187 
188 	if(port_str == NULL)
189 	    debug_port = krb5_getportbyname (context, "kerberos-adm",
190 					     "tcp", 749);
191 	else
192 	    debug_port = htons(atoi(port_str));
193 	mini_inetd(debug_port, &sfd);
194     } else {
195 #ifdef _WIN32
196 	start_server(context, port_str);
197 #else
198 	struct sockaddr_storage __ss;
199 	struct sockaddr *sa = (struct sockaddr *)&__ss;
200 	socklen_t sa_size = sizeof(__ss);
201 
202 	/*
203 	 * Check if we are running inside inetd or not, if not, start
204 	 * our own server.
205 	 */
206 
207 	if(roken_getsockname(STDIN_FILENO, sa, &sa_size) < 0 &&
208 	   rk_SOCK_ERRNO == ENOTSOCK) {
209 	    start_server(context, port_str);
210 	}
211 #endif /* _WIN32 */
212 	sfd = STDIN_FILENO;
213     }
214 
215     if(realm)
216 	krb5_set_default_realm(context, realm); /* XXX */
217 
218     kadmind_loop(context, keytab, sfd);
219 
220     return 0;
221 }
222