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