2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "kadmin_locl.h"
37 RCSID("$Id: kadmin.c,v 1.42 2003/03/31 10:20:19 lha Exp $");
39 static char *config_file;
41 static int local_flag;
43 static int version_flag;
45 static char *admin_server;
46 static int server_port = 0;
47 static char *client_name;
50 static struct getargs args[] = {
51 { "principal", 'p', arg_string, &client_name,
52 "principal to authenticate as" },
53 { "keytab", 'K', arg_string, &keytab,
54 "keytab for authentication principal" },
56 "config-file", 'c', arg_string, &config_file,
57 "location of config file", "file"
60 "key-file", 'k', arg_string, &keyfile,
61 "location of master key file", "file"
64 "realm", 'r', arg_string, &realm,
65 "realm to use", "realm"
68 "admin-server", 'a', arg_string, &admin_server,
69 "server to contact", "host"
72 "server-port", 's', arg_integer, &server_port,
73 "port to use", "port number"
75 { "local", 'l', arg_flag, &local_flag, "local admin mode" },
76 { "help", 'h', arg_flag, &help_flag },
77 { "version", 'v', arg_flag, &version_flag }
80 static int num_args = sizeof(args) / sizeof(args[0]);
82 static SL_cmd commands[] = {
83 /* commands that are only available with `-l' */
85 "dump", dump, "dump [file]",
86 "Dumps the database in a human readable format to the\n"
87 "specified file, or the standard out."
90 "load", load, "load file",
91 "Loads a previously dumped file."
94 "merge", merge, "merge file" ,
95 "Merges the contents of a dump file into the database."
98 "init", init, "init realm...",
99 "Initializes the default principals for a realm.\n"
100 "Creates the database if necessary."
102 /* common commands */
104 "add", add_new_key, "add principal" ,
105 "Adds a principal to the database."
110 "passwd", cpw_entry, "passwd expression..." ,
111 "Changes the password of one or more principals\n"
112 "matching the expressions."
114 { "change_password"},
117 "delete", del_entry, "delete expression...",
118 "Deletes all principals matching the expressions."
123 "del_enctype", del_enctype, "del_enctype principal enctype...",
124 "Delete all the mentioned enctypes for principal."
127 "ext_keytab", ext_keytab, "ext_keytab expression...",
128 "Extracts the keys of all principals matching the expressions,\n"
129 "and stores them in a keytab."
132 "get", get_entry, "get expression...",
133 "Shows information about principals matching the expressions."
137 "rename", rename_entry, "rename source target",
138 "Renames `source' to `target'."
141 "modify", mod_entry, "modify principal",
142 "Modifies some attributes of the specified principal."
145 "privileges", get_privs, "privileges",
146 "Shows which kinds of operations you are allowed to perform."
150 "list", list_princs, "list expression...",
151 "Lists principals in a terse format. The same as `get -t'."
153 { "help", help, "help"},
155 { "exit", exit_kadmin, "exit"},
160 krb5_context context;
163 static SL_cmd *actual_cmds;
166 help(int argc, char **argv)
168 sl_help(actual_cmds, argc, argv);
173 exit_kadmin (int argc, char **argv)
181 arg_printusage (args, num_args, NULL, "[command]");
186 get_privs(int argc, char **argv)
193 struct getargs args[] = {
194 { "help", 'h', arg_flag, NULL }
196 int num_args = sizeof(args) / sizeof(args[0]);
199 args[0].value = &help_flag;
201 if(getarg(args, num_args, argc, argv, &optind)) {
202 arg_printusage (args, num_args, "privileges", NULL);
206 arg_printusage (args, num_args, "privileges", NULL);
210 ret = kadm5_get_privs(kadm_handle, &privs);
212 krb5_warn(context, ret, "kadm5_get_privs");
214 ret =_kadm5_privs_to_string(privs, str, sizeof(str));
221 main(int argc, char **argv)
224 krb5_config_section *cf = NULL;
225 kadm5_config_params conf;
228 setprogname(argv[0]);
230 ret = krb5_init_context(&context);
232 errx (1, "krb5_init_context failed: %d", ret);
234 if(getarg(args, num_args, argc, argv, &optind))
248 if (config_file == NULL)
249 config_file = HDB_DB_DIR "/kdc.conf";
251 if(krb5_config_parse_file(context, config_file, &cf) == 0) {
252 const char *p = krb5_config_get_string (context, cf,
253 "kdc", "key-file", NULL);
257 krb5_clear_error_string (context);
259 memset(&conf, 0, sizeof(conf));
261 krb5_set_default_realm(context, realm); /* XXX should be fixed
264 conf.mask |= KADM5_CONFIG_REALM;
268 conf.admin_server = admin_server;
269 conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
273 conf.kadmind_port = htons(server_port);
274 conf.mask |= KADM5_CONFIG_KADMIND_PORT;
278 ret = kadm5_s_init_with_password_ctx(context,
284 actual_cmds = commands;
286 ret = kadm5_c_init_with_skey_ctx(context,
292 actual_cmds = commands + 4; /* XXX */
294 ret = kadm5_c_init_with_password_ctx(context,
300 actual_cmds = commands + 4; /* XXX */
304 krb5_err(context, 1, ret, "kadm5_init_with_password");
306 signal(SIGINT, SIG_IGN); /* ignore signals for now, the sl command
307 parser will handle SIGINT its own way;
308 we should really take care of this in
309 each function, f.i `get' might be
310 interruptable, but not `create' */
312 ret = sl_command (actual_cmds, argc, argv);
314 krb5_warnx (context, "unrecognized command: %s", argv[0]);
316 ret = sl_loop (actual_cmds, "kadmin> ") != 0;
318 kadm5_destroy(kadm_handle);
319 krb5_config_file_free (context, cf);
320 krb5_free_context(context);