1b528cefcSMark Murray /*
2*ae771770SStanislav 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"
35c19800e8SDoug Rabson #include "kadmin-commands.h"
36b528cefcSMark Murray #include <sl.h>
37b528cefcSMark Murray
38b528cefcSMark Murray static char *config_file;
39b528cefcSMark Murray static char *keyfile;
40c19800e8SDoug Rabson int local_flag;
41c19800e8SDoug Rabson static int ad_flag;
42b528cefcSMark Murray static int help_flag;
43b528cefcSMark Murray static int version_flag;
44b528cefcSMark Murray static char *realm;
45b528cefcSMark Murray static char *admin_server;
46b528cefcSMark Murray static int server_port = 0;
47b528cefcSMark Murray static char *client_name;
48adb0ddaeSAssar Westerlund static char *keytab;
49c19800e8SDoug Rabson static char *check_library = NULL;
50c19800e8SDoug Rabson static char *check_function = NULL;
51c19800e8SDoug Rabson static getarg_strings policy_libraries = { 0, NULL };
52b528cefcSMark Murray
53b528cefcSMark Murray static struct getargs args[] = {
54b528cefcSMark Murray { "principal", 'p', arg_string, &client_name,
55*ae771770SStanislav Sedov "principal to authenticate as", NULL },
56adb0ddaeSAssar Westerlund { "keytab", 'K', arg_string, &keytab,
57*ae771770SStanislav Sedov "keytab for authentication principal", NULL },
58b528cefcSMark Murray {
59b528cefcSMark Murray "config-file", 'c', arg_string, &config_file,
60b528cefcSMark Murray "location of config file", "file"
61b528cefcSMark Murray },
62b528cefcSMark Murray {
63b528cefcSMark Murray "key-file", 'k', arg_string, &keyfile,
64b528cefcSMark Murray "location of master key file", "file"
65b528cefcSMark Murray },
66b528cefcSMark Murray {
67b528cefcSMark Murray "realm", 'r', arg_string, &realm,
68b528cefcSMark Murray "realm to use", "realm"
69b528cefcSMark Murray },
70b528cefcSMark Murray {
71b528cefcSMark Murray "admin-server", 'a', arg_string, &admin_server,
72b528cefcSMark Murray "server to contact", "host"
73b528cefcSMark Murray },
74b528cefcSMark Murray {
75b528cefcSMark Murray "server-port", 's', arg_integer, &server_port,
7613e3f4d6SMark Murray "port to use", "port number"
77b528cefcSMark Murray },
78*ae771770SStanislav Sedov { "ad", 0, arg_flag, &ad_flag, "active directory admin mode",
79*ae771770SStanislav Sedov NULL },
80c19800e8SDoug Rabson #ifdef HAVE_DLOPEN
81c19800e8SDoug Rabson { "check-library", 0, arg_string, &check_library,
82c19800e8SDoug Rabson "library to load password check function from", "library" },
83c19800e8SDoug Rabson { "check-function", 0, arg_string, &check_function,
84c19800e8SDoug Rabson "password check function to load", "function" },
85c19800e8SDoug Rabson { "policy-libraries", 0, arg_strings, &policy_libraries,
86c19800e8SDoug Rabson "password check function to load", "function" },
87c19800e8SDoug Rabson #endif
88*ae771770SStanislav Sedov { "local", 'l', arg_flag, &local_flag, "local admin mode", NULL },
89*ae771770SStanislav Sedov { "help", 'h', arg_flag, &help_flag, NULL, NULL },
90*ae771770SStanislav Sedov { "version", 'v', arg_flag, &version_flag, NULL, NULL }
91b528cefcSMark Murray };
92b528cefcSMark Murray
93b528cefcSMark Murray static int num_args = sizeof(args) / sizeof(args[0]);
94b528cefcSMark Murray
95b528cefcSMark Murray
96b528cefcSMark Murray krb5_context context;
97b528cefcSMark Murray void *kadm_handle;
98b528cefcSMark Murray
99b528cefcSMark Murray int
help(void * opt,int argc,char ** argv)100c19800e8SDoug Rabson help(void *opt, int argc, char **argv)
101b528cefcSMark Murray {
102c19800e8SDoug Rabson sl_slc_help(commands, argc, argv);
103b528cefcSMark Murray return 0;
104b528cefcSMark Murray }
105b528cefcSMark Murray
106c19800e8SDoug Rabson static int exit_seen = 0;
107c19800e8SDoug Rabson
108b528cefcSMark Murray int
exit_kadmin(void * opt,int argc,char ** argv)109c19800e8SDoug Rabson exit_kadmin (void *opt, int argc, char **argv)
110b528cefcSMark Murray {
111c19800e8SDoug Rabson exit_seen = 1;
112c19800e8SDoug Rabson return 0;
113b528cefcSMark Murray }
114b528cefcSMark Murray
115b528cefcSMark Murray static void
usage(int ret)116b528cefcSMark Murray usage(int ret)
117b528cefcSMark Murray {
118b528cefcSMark Murray arg_printusage (args, num_args, NULL, "[command]");
119b528cefcSMark Murray exit (ret);
120b528cefcSMark Murray }
121b528cefcSMark Murray
122b528cefcSMark Murray int
get_privs(void * opt,int argc,char ** argv)123c19800e8SDoug Rabson get_privs(void *opt, int argc, char **argv)
124b528cefcSMark Murray {
125c19800e8SDoug Rabson uint32_t privs;
126b528cefcSMark Murray char str[128];
127b528cefcSMark Murray kadm5_ret_t ret;
128b528cefcSMark Murray
129b528cefcSMark Murray ret = kadm5_get_privs(kadm_handle, &privs);
130b528cefcSMark Murray if(ret)
131b528cefcSMark Murray krb5_warn(context, ret, "kadm5_get_privs");
132b528cefcSMark Murray else{
133b528cefcSMark Murray ret =_kadm5_privs_to_string(privs, str, sizeof(str));
134*ae771770SStanislav Sedov if (ret == 0)
135b528cefcSMark Murray printf("%s\n", str);
136*ae771770SStanislav Sedov else
137*ae771770SStanislav Sedov printf("privs: 0x%x\n", (unsigned int)privs);
138b528cefcSMark Murray }
139b528cefcSMark Murray return 0;
140b528cefcSMark Murray }
141b528cefcSMark Murray
142b528cefcSMark Murray int
main(int argc,char ** argv)143b528cefcSMark Murray main(int argc, char **argv)
144b528cefcSMark Murray {
145b528cefcSMark Murray krb5_error_code ret;
146c19800e8SDoug Rabson char **files;
147b528cefcSMark Murray kadm5_config_params conf;
148c19800e8SDoug Rabson int optidx = 0;
149c19800e8SDoug Rabson int exit_status = 0;
150b528cefcSMark Murray
151adb0ddaeSAssar Westerlund setprogname(argv[0]);
152b528cefcSMark Murray
1535e9cd1aeSAssar Westerlund ret = krb5_init_context(&context);
1545e9cd1aeSAssar Westerlund if (ret)
1555e9cd1aeSAssar Westerlund errx (1, "krb5_init_context failed: %d", ret);
156b528cefcSMark Murray
157c19800e8SDoug Rabson if(getarg(args, num_args, argc, argv, &optidx))
1584137ff4cSJacques Vidrine usage(1);
159b528cefcSMark Murray
160b528cefcSMark Murray if (help_flag)
161b528cefcSMark Murray usage (0);
162b528cefcSMark Murray
163b528cefcSMark Murray if (version_flag) {
164b528cefcSMark Murray print_version(NULL);
165b528cefcSMark Murray exit(0);
166b528cefcSMark Murray }
167b528cefcSMark Murray
168c19800e8SDoug Rabson argc -= optidx;
169c19800e8SDoug Rabson argv += optidx;
170b528cefcSMark Murray
171c19800e8SDoug Rabson if (config_file == NULL) {
172c19800e8SDoug Rabson asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
173b528cefcSMark Murray if (config_file == NULL)
174c19800e8SDoug Rabson errx(1, "out of memory");
175b528cefcSMark Murray }
176c19800e8SDoug Rabson
177c19800e8SDoug Rabson ret = krb5_prepend_config_files_default(config_file, &files);
178c19800e8SDoug Rabson if (ret)
179c19800e8SDoug Rabson krb5_err(context, 1, ret, "getting configuration files");
180c19800e8SDoug Rabson
181c19800e8SDoug Rabson ret = krb5_set_config_files(context, files);
182c19800e8SDoug Rabson krb5_free_config_files(files);
183c19800e8SDoug Rabson if(ret)
184c19800e8SDoug Rabson krb5_err(context, 1, ret, "reading configuration files");
185b528cefcSMark Murray
186b528cefcSMark Murray memset(&conf, 0, sizeof(conf));
187b528cefcSMark Murray if(realm) {
188b528cefcSMark Murray krb5_set_default_realm(context, realm); /* XXX should be fixed
189b528cefcSMark Murray some other way */
190b528cefcSMark Murray conf.realm = realm;
191b528cefcSMark Murray conf.mask |= KADM5_CONFIG_REALM;
192b528cefcSMark Murray }
193b528cefcSMark Murray
194b528cefcSMark Murray if (admin_server) {
195b528cefcSMark Murray conf.admin_server = admin_server;
196b528cefcSMark Murray conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
197b528cefcSMark Murray }
198b528cefcSMark Murray
199b528cefcSMark Murray if (server_port) {
200b528cefcSMark Murray conf.kadmind_port = htons(server_port);
201b528cefcSMark Murray conf.mask |= KADM5_CONFIG_KADMIND_PORT;
202b528cefcSMark Murray }
203b528cefcSMark Murray
204c19800e8SDoug Rabson if (keyfile) {
205c19800e8SDoug Rabson conf.stash_file = keyfile;
206c19800e8SDoug Rabson conf.mask |= KADM5_CONFIG_STASH_FILE;
207c19800e8SDoug Rabson }
208c19800e8SDoug Rabson
209b528cefcSMark Murray if(local_flag) {
210c19800e8SDoug Rabson int i;
211c19800e8SDoug Rabson
212c19800e8SDoug Rabson kadm5_setup_passwd_quality_check (context,
213c19800e8SDoug Rabson check_library, check_function);
214c19800e8SDoug Rabson
215c19800e8SDoug Rabson for (i = 0; i < policy_libraries.num_strings; i++) {
216c19800e8SDoug Rabson ret = kadm5_add_passwd_quality_verifier(context,
217c19800e8SDoug Rabson policy_libraries.strings[i]);
218c19800e8SDoug Rabson if (ret)
219c19800e8SDoug Rabson krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
220c19800e8SDoug Rabson }
221c19800e8SDoug Rabson ret = kadm5_add_passwd_quality_verifier(context, NULL);
222c19800e8SDoug Rabson if (ret)
223c19800e8SDoug Rabson krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
224c19800e8SDoug Rabson
225b528cefcSMark Murray ret = kadm5_s_init_with_password_ctx(context,
226b528cefcSMark Murray KADM5_ADMIN_SERVICE,
227b528cefcSMark Murray NULL,
228b528cefcSMark Murray KADM5_ADMIN_SERVICE,
229b528cefcSMark Murray &conf, 0, 0,
230b528cefcSMark Murray &kadm_handle);
231c19800e8SDoug Rabson } else if (ad_flag) {
232c19800e8SDoug Rabson if (client_name == NULL)
233c19800e8SDoug Rabson krb5_errx(context, 1, "keytab mode require principal name");
234c19800e8SDoug Rabson ret = kadm5_ad_init_with_password_ctx(context,
235c19800e8SDoug Rabson client_name,
236c19800e8SDoug Rabson NULL,
237c19800e8SDoug Rabson KADM5_ADMIN_SERVICE,
238c19800e8SDoug Rabson &conf, 0, 0,
239c19800e8SDoug Rabson &kadm_handle);
240adb0ddaeSAssar Westerlund } else if (keytab) {
241c19800e8SDoug Rabson if (client_name == NULL)
242c19800e8SDoug Rabson krb5_errx(context, 1, "keytab mode require principal name");
243adb0ddaeSAssar Westerlund ret = kadm5_c_init_with_skey_ctx(context,
244adb0ddaeSAssar Westerlund client_name,
245adb0ddaeSAssar Westerlund keytab,
246adb0ddaeSAssar Westerlund KADM5_ADMIN_SERVICE,
247adb0ddaeSAssar Westerlund &conf, 0, 0,
248adb0ddaeSAssar Westerlund &kadm_handle);
249c19800e8SDoug Rabson } else
250b528cefcSMark Murray ret = kadm5_c_init_with_password_ctx(context,
251b528cefcSMark Murray client_name,
252b528cefcSMark Murray NULL,
253b528cefcSMark Murray KADM5_ADMIN_SERVICE,
254b528cefcSMark Murray &conf, 0, 0,
255b528cefcSMark Murray &kadm_handle);
256b528cefcSMark Murray
257b528cefcSMark Murray if(ret)
258b528cefcSMark Murray krb5_err(context, 1, ret, "kadm5_init_with_password");
2595e9cd1aeSAssar Westerlund
2605e9cd1aeSAssar Westerlund signal(SIGINT, SIG_IGN); /* ignore signals for now, the sl command
2615e9cd1aeSAssar Westerlund parser will handle SIGINT its own way;
2625e9cd1aeSAssar Westerlund we should really take care of this in
2635e9cd1aeSAssar Westerlund each function, f.i `get' might be
2645e9cd1aeSAssar Westerlund interruptable, but not `create' */
265b528cefcSMark Murray if (argc != 0) {
266c19800e8SDoug Rabson ret = sl_command (commands, argc, argv);
267b528cefcSMark Murray if(ret == -1)
268b528cefcSMark Murray krb5_warnx (context, "unrecognized command: %s", argv[0]);
269c19800e8SDoug Rabson else if (ret == -2)
270c19800e8SDoug Rabson ret = 0;
271c19800e8SDoug Rabson if(ret != 0)
272c19800e8SDoug Rabson exit_status = 1;
273c19800e8SDoug Rabson } else {
274c19800e8SDoug Rabson while(!exit_seen) {
275c19800e8SDoug Rabson ret = sl_command_loop(commands, "kadmin> ", NULL);
276c19800e8SDoug Rabson if (ret == -2)
277c19800e8SDoug Rabson exit_seen = 1;
278c19800e8SDoug Rabson else if (ret != 0)
279c19800e8SDoug Rabson exit_status = 1;
280c19800e8SDoug Rabson }
281c19800e8SDoug Rabson }
282b528cefcSMark Murray
283b528cefcSMark Murray kadm5_destroy(kadm_handle);
284b528cefcSMark Murray krb5_free_context(context);
285c19800e8SDoug Rabson return exit_status;
286b528cefcSMark Murray }
287