xref: /freebsd/crypto/heimdal/kadmin/kadmin.c (revision b528cefc6b8f9670b31a865051741d946cb37085)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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.26 1999/12/02 17:04:58 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 	"server to contact", "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     {
145 	"list",		list_princs,	"list expression...",
146 	"Lists principals in a terse format. The same as `get -t'."
147     },
148     { "help",		help, "help"},
149     { "?"},
150     { "exit",		exit_kadmin, "exit"},
151     { NULL}
152 };
153 
154 krb5_context context;
155 void *kadm_handle;
156 
157 int
158 help(int argc, char **argv)
159 {
160     sl_help(commands, argc, argv);
161     return 0;
162 }
163 
164 int
165 exit_kadmin (int argc, char **argv)
166 {
167     return 1;
168 }
169 
170 static void
171 usage(int ret)
172 {
173     arg_printusage (args, num_args, NULL, "[command]");
174     exit (ret);
175 }
176 
177 int
178 get_privs(int argc, char **argv)
179 {
180     u_int32_t privs;
181     char str[128];
182     kadm5_ret_t ret;
183 
184     ret = kadm5_get_privs(kadm_handle, &privs);
185     if(ret)
186 	krb5_warn(context, ret, "kadm5_get_privs");
187     else{
188 	ret =_kadm5_privs_to_string(privs, str, sizeof(str));
189 	printf("%s\n", str);
190     }
191     return 0;
192 }
193 
194 int
195 main(int argc, char **argv)
196 {
197     krb5_error_code ret;
198     krb5_config_section *cf = NULL;
199     kadm5_config_params conf;
200     int optind = 0;
201     int e;
202     SL_cmd *cmd;
203 
204     set_progname(argv[0]);
205 
206     krb5_init_context(&context);
207 
208     while((e = getarg(args, num_args, argc, argv, &optind)))
209 	warnx("error at argument `%s'", argv[optind]);
210 
211     if (help_flag)
212 	usage (0);
213 
214     if (version_flag) {
215 	print_version(NULL);
216 	exit(0);
217     }
218 
219     argc -= optind;
220     argv += optind;
221 
222     if (config_file == NULL)
223 	config_file = HDB_DB_DIR "/kdc.conf";
224 
225     if(krb5_config_parse_file(config_file, &cf) == 0) {
226 	const char *p = krb5_config_get_string (context, cf,
227 						"kdc", "key-file", NULL);
228 	if (p)
229 	    keyfile = strdup(p);
230     }
231 
232     memset(&conf, 0, sizeof(conf));
233     if(realm) {
234 	krb5_set_default_realm(context, realm); /* XXX should be fixed
235 						   some other way */
236 	conf.realm = realm;
237 	conf.mask |= KADM5_CONFIG_REALM;
238     }
239 
240     if (admin_server) {
241 	conf.admin_server = admin_server;
242 	conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
243     }
244 
245     if (server_port) {
246 	conf.kadmind_port = htons(server_port);
247 	conf.mask |= KADM5_CONFIG_KADMIND_PORT;
248     }
249 
250     if(local_flag){
251 	ret = kadm5_s_init_with_password_ctx(context,
252 					     KADM5_ADMIN_SERVICE,
253 					     NULL,
254 					     KADM5_ADMIN_SERVICE,
255 					     &conf, 0, 0,
256 					     &kadm_handle);
257 	cmd = commands;
258     } else {
259 	ret = kadm5_c_init_with_password_ctx(context,
260 					     client_name,
261 					     NULL,
262 					     KADM5_ADMIN_SERVICE,
263 					     &conf, 0, 0,
264 					     &kadm_handle);
265 	cmd = commands + 4; /* XXX */
266     }
267 
268     if(ret)
269 	krb5_err(context, 1, ret, "kadm5_init_with_password");
270     if (argc != 0) {
271 	ret = sl_command (cmd, argc, argv);
272 	if(ret == -1)
273 	    krb5_warnx (context, "unrecognized command: %s", argv[0]);
274     } else
275 	ret = sl_loop (cmd, "kadmin> ") != 0;
276 
277     kadm5_destroy(kadm_handle);
278     krb5_config_file_free (context, cf);
279     krb5_free_context(context);
280     return ret;
281 }
282