xref: /freebsd/crypto/heimdal/kadmin/kadmin.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "kadmin_locl.h"
35 #include <sl.h>
36 
37 RCSID("$Id: kadmin.c,v 1.34 2001/01/26 22:20:52 joda Exp $");
38 
39 static char *config_file;
40 static char *keyfile;
41 static int local_flag;
42 static int help_flag;
43 static int version_flag;
44 static char *realm;
45 static char *admin_server;
46 static int server_port = 0;
47 static char *client_name;
48 
49 static struct getargs args[] = {
50     {	"principal", 	'p',	arg_string,	&client_name,
51 	"principal to authenticate as" },
52     {
53 	"config-file",	'c',	arg_string,	&config_file,
54 	"location of config file",	"file"
55     },
56     {
57 	"key-file",	'k',	arg_string, &keyfile,
58 	"location of master key file", "file"
59     },
60     {
61 	"realm",	'r',	arg_string,   &realm,
62 	"realm to use", "realm"
63     },
64     {
65 	"admin-server",	'a',	arg_string,   &admin_server,
66 	"server to contact", "host"
67     },
68     {
69 	"server-port",	's',	arg_integer,   &server_port,
70 	"port to use", "port number"
71     },
72     {	"local", 'l', arg_flag, &local_flag, "local admin mode" },
73     {	"help",		'h',	arg_flag,   &help_flag },
74     {	"version",	'v',	arg_flag,   &version_flag }
75 };
76 
77 static int num_args = sizeof(args) / sizeof(args[0]);
78 
79 static SL_cmd commands[] = {
80     /* commands that are only available with `-l' */
81     {
82 	"dump",		dump,		"dump [file]",
83 	"Dumps the database in a human readable format to the\n"
84 	"specified file, or the standard out."
85     },
86     {
87 	"load",		load,		"load file",
88 	"Loads a previously dumped file."
89     },
90     {
91 	"merge",	merge,		"merge file" ,
92 	"Merges the contents of a dump file into the database."
93     },
94     {
95 	"init",		init,		"init realm...",
96 	"Initializes the default principals for a realm.\n"
97 	"Creates the database if necessary."
98     },
99     /* common commands */
100     {
101 	"add",	add_new_key, 	"add principal" ,
102 	"Adds a principal to the database."
103     },
104     { "add_new_key"},
105     { "ank"},
106     {
107 	"passwd",	cpw_entry, 	"passwd expression..." ,
108 	"Changes the password of one or more principals\n"
109 	"matching the expressions."
110     },
111     { "change_password"},
112     { "cpw"},
113     {
114 	"delete",	del_entry, 	"delete expression...",
115 	"Deletes all principals matching the expressions."
116     },
117     { "del_entry" },
118     {
119 	"del_enctype",	del_enctype,	"del_enctype principal enctype...",
120 	"Delete all the mentioned enctypes for principal."
121     },
122     {
123 	"ext_keytab",	ext_keytab, 	"ext_keytab expression...",
124 	"Extracts the keys of all principals matching the expressions,\n"
125 	"and stores them in a keytab."
126     },
127     {
128 	"get",		get_entry, 	"get expression...",
129 	"Shows information about principals matching the expressions."
130     },
131     { "get_entry" },
132     {
133 	"rename",	rename_entry, 	"rename source target",
134 	"Renames `source' to `target'."
135     },
136     {
137 	"modify",	mod_entry, 	"modify principal",
138 	"Modifies some attributes of the specified principal."
139     },
140     {
141 	"privileges",	get_privs,	"privileges",
142 	"Shows which kinds of operations you are allowed to perform."
143     },
144     { "privs" },
145     {
146 	"list",		list_princs,	"list expression...",
147 	"Lists principals in a terse format. The same as `get -t'."
148     },
149     { "help",		help, "help"},
150     { "?"},
151     { "exit",		exit_kadmin, "exit"},
152     { "quit" },
153     { NULL}
154 };
155 
156 krb5_context context;
157 void *kadm_handle;
158 
159 static SL_cmd *actual_cmds;
160 
161 int
162 help(int argc, char **argv)
163 {
164     sl_help(actual_cmds, argc, argv);
165     return 0;
166 }
167 
168 int
169 exit_kadmin (int argc, char **argv)
170 {
171     return 1;
172 }
173 
174 static void
175 usage(int ret)
176 {
177     arg_printusage (args, num_args, NULL, "[command]");
178     exit (ret);
179 }
180 
181 int
182 get_privs(int argc, char **argv)
183 {
184     u_int32_t privs;
185     char str[128];
186     kadm5_ret_t ret;
187 
188     int help_flag = 0;
189     struct getargs args[] = {
190 	{ "help",	'h',	arg_flag,	NULL }
191     };
192     int num_args = sizeof(args) / sizeof(args[0]);
193     int optind = 0;
194 
195     args[0].value = &help_flag;
196 
197     if(getarg(args, num_args, argc, argv, &optind)) {
198 	arg_printusage (args, num_args, "privileges", NULL);
199 	return 0;
200     }
201     if(help_flag) {
202 	arg_printusage (args, num_args, "privileges", NULL);
203 	return 0;
204     }
205 
206     ret = kadm5_get_privs(kadm_handle, &privs);
207     if(ret)
208 	krb5_warn(context, ret, "kadm5_get_privs");
209     else{
210 	ret =_kadm5_privs_to_string(privs, str, sizeof(str));
211 	printf("%s\n", str);
212     }
213     return 0;
214 }
215 
216 int
217 main(int argc, char **argv)
218 {
219     krb5_error_code ret;
220     krb5_config_section *cf = NULL;
221     kadm5_config_params conf;
222     int optind = 0;
223     int e;
224 
225     set_progname(argv[0]);
226 
227     ret = krb5_init_context(&context);
228     if (ret)
229 	errx (1, "krb5_init_context failed: %d", ret);
230 
231     while((e = getarg(args, num_args, argc, argv, &optind)))
232 	errx(1, "error at argument `%s'", argv[optind]);
233 
234     if (help_flag)
235 	usage (0);
236 
237     if (version_flag) {
238 	print_version(NULL);
239 	exit(0);
240     }
241 
242     argc -= optind;
243     argv += optind;
244 
245     if (config_file == NULL)
246 	config_file = HDB_DB_DIR "/kdc.conf";
247 
248     if(krb5_config_parse_file(config_file, &cf) == 0) {
249 	const char *p = krb5_config_get_string (context, cf,
250 						"kdc", "key-file", NULL);
251 	if (p)
252 	    keyfile = strdup(p);
253     }
254 
255     memset(&conf, 0, sizeof(conf));
256     if(realm) {
257 	krb5_set_default_realm(context, realm); /* XXX should be fixed
258 						   some other way */
259 	conf.realm = realm;
260 	conf.mask |= KADM5_CONFIG_REALM;
261     }
262 
263     if (admin_server) {
264 	conf.admin_server = admin_server;
265 	conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
266     }
267 
268     if (server_port) {
269 	conf.kadmind_port = htons(server_port);
270 	conf.mask |= KADM5_CONFIG_KADMIND_PORT;
271     }
272 
273     if(local_flag){
274 	ret = kadm5_s_init_with_password_ctx(context,
275 					     KADM5_ADMIN_SERVICE,
276 					     NULL,
277 					     KADM5_ADMIN_SERVICE,
278 					     &conf, 0, 0,
279 					     &kadm_handle);
280 	actual_cmds = commands;
281     } else {
282 	ret = kadm5_c_init_with_password_ctx(context,
283 					     client_name,
284 					     NULL,
285 					     KADM5_ADMIN_SERVICE,
286 					     &conf, 0, 0,
287 					     &kadm_handle);
288 	actual_cmds = commands + 4; /* XXX */
289     }
290 
291     if(ret)
292 	krb5_err(context, 1, ret, "kadm5_init_with_password");
293 
294     signal(SIGINT, SIG_IGN); /* ignore signals for now, the sl command
295                                 parser will handle SIGINT its own way;
296                                 we should really take care of this in
297                                 each function, f.i `get' might be
298                                 interruptable, but not `create' */
299     if (argc != 0) {
300 	ret = sl_command (actual_cmds, argc, argv);
301 	if(ret == -1)
302 	    krb5_warnx (context, "unrecognized command: %s", argv[0]);
303     } else
304 	ret = sl_loop (actual_cmds, "kadmin> ") != 0;
305 
306     kadm5_destroy(kadm_handle);
307     krb5_config_file_free (context, cf);
308     krb5_free_context(context);
309     return ret;
310 }
311