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