use gss_krb5_get_subkey() instead of gss_krb5_get_{local,remote}key()
[heimdal.git] / kadmin / kadmin.c
blobabc1f526e9acb9a9678c36d462130669bb7d0603
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "kadmin_locl.h"
35 #include "kadmin-commands.h"
36 #include <sl.h>
38 RCSID("$Id$");
40 static char *config_file;
41 static char *keyfile;
42 int local_flag;
43 static int ad_flag;
44 static int help_flag;
45 static int version_flag;
46 static char *realm;
47 static char *admin_server;
48 static int server_port = 0;
49 static char *client_name;
50 static char *keytab;
52 static struct getargs args[] = {
53 { "principal", 'p', arg_string, &client_name,
54 "principal to authenticate as" },
55 { "keytab", 'K', arg_string, &keytab,
56 "keytab for authentication principal" },
58 "config-file", 'c', arg_string, &config_file,
59 "location of config file", "file"
62 "key-file", 'k', arg_string, &keyfile,
63 "location of master key file", "file"
66 "realm", 'r', arg_string, &realm,
67 "realm to use", "realm"
70 "admin-server", 'a', arg_string, &admin_server,
71 "server to contact", "host"
74 "server-port", 's', arg_integer, &server_port,
75 "port to use", "port number"
77 { "ad", 0, arg_flag, &ad_flag, "active directory admin mode" },
78 { "local", 'l', arg_flag, &local_flag, "local admin mode" },
79 { "help", 'h', arg_flag, &help_flag },
80 { "version", 'v', arg_flag, &version_flag }
83 static int num_args = sizeof(args) / sizeof(args[0]);
86 krb5_context context;
87 void *kadm_handle;
89 int
90 help(void *opt, int argc, char **argv)
92 if(argc == 0) {
93 sl_help(commands, 1, argv - 1 /* XXX */);
94 } else {
95 SL_cmd *c = sl_match (commands, argv[0], 0);
96 if(c == NULL) {
97 fprintf (stderr, "No such command: %s. "
98 "Try \"help\" for a list of commands\n",
99 argv[0]);
100 } else {
101 if(c->func) {
102 char *fake[] = { argv[0], "--help", NULL };
103 (*c->func)(2, fake);
104 fprintf(stderr, "\n");
106 if(c->help && *c->help)
107 fprintf (stderr, "%s\n", c->help);
108 if((++c)->name && c->func == NULL) {
109 int f = 0;
110 fprintf (stderr, "Synonyms:");
111 while (c->name && c->func == NULL) {
112 fprintf (stderr, "%s%s", f ? ", " : " ", (c++)->name);
113 f = 1;
115 fprintf (stderr, "\n");
119 return 0;
122 static int exit_seen = 0;
125 exit_kadmin (void *opt, int argc, char **argv)
127 exit_seen = 1;
128 return 0;
131 static void
132 usage(int ret)
134 arg_printusage (args, num_args, NULL, "[command]");
135 exit (ret);
139 get_privs(void *opt, int argc, char **argv)
141 u_int32_t privs;
142 char str[128];
143 kadm5_ret_t ret;
145 ret = kadm5_get_privs(kadm_handle, &privs);
146 if(ret)
147 krb5_warn(context, ret, "kadm5_get_privs");
148 else{
149 ret =_kadm5_privs_to_string(privs, str, sizeof(str));
150 printf("%s\n", str);
152 return 0;
156 main(int argc, char **argv)
158 krb5_error_code ret;
159 char **files;
160 kadm5_config_params conf;
161 int optind = 0;
162 int exit_status = 0;
164 setprogname(argv[0]);
166 ret = krb5_init_context(&context);
167 if (ret)
168 errx (1, "krb5_init_context failed: %d", ret);
170 if(getarg(args, num_args, argc, argv, &optind))
171 usage(1);
173 if (help_flag)
174 usage (0);
176 if (version_flag) {
177 print_version(NULL);
178 exit(0);
181 argc -= optind;
182 argv += optind;
184 if (config_file == NULL)
185 config_file = HDB_DB_DIR "/kdc.conf";
187 ret = krb5_prepend_config_files_default(config_file, &files);
188 if (ret)
189 krb5_err(context, 1, ret, "getting configuration files");
191 ret = krb5_set_config_files(context, files);
192 krb5_free_config_files(files);
193 if(ret)
194 krb5_err(context, 1, ret, "reading configuration files");
196 memset(&conf, 0, sizeof(conf));
197 if(realm) {
198 krb5_set_default_realm(context, realm); /* XXX should be fixed
199 some other way */
200 conf.realm = realm;
201 conf.mask |= KADM5_CONFIG_REALM;
204 if (admin_server) {
205 conf.admin_server = admin_server;
206 conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
209 if (server_port) {
210 conf.kadmind_port = htons(server_port);
211 conf.mask |= KADM5_CONFIG_KADMIND_PORT;
214 if (keyfile) {
215 conf.stash_file = keyfile;
216 conf.mask |= KADM5_CONFIG_STASH_FILE;
219 if(local_flag)
220 ret = kadm5_s_init_with_password_ctx(context,
221 KADM5_ADMIN_SERVICE,
222 NULL,
223 KADM5_ADMIN_SERVICE,
224 &conf, 0, 0,
225 &kadm_handle);
226 else if (ad_flag) {
227 if (client_name == NULL)
228 krb5_errx(context, 1, "keytab mode require principal name");
229 ret = kadm5_ad_init_with_password_ctx(context,
230 client_name,
231 NULL,
232 KADM5_ADMIN_SERVICE,
233 &conf, 0, 0,
234 &kadm_handle);
235 } else if (keytab) {
236 if (client_name == NULL)
237 krb5_errx(context, 1, "keytab mode require principal name");
238 ret = kadm5_c_init_with_skey_ctx(context,
239 client_name,
240 keytab,
241 KADM5_ADMIN_SERVICE,
242 &conf, 0, 0,
243 &kadm_handle);
244 } else
245 ret = kadm5_c_init_with_password_ctx(context,
246 client_name,
247 NULL,
248 KADM5_ADMIN_SERVICE,
249 &conf, 0, 0,
250 &kadm_handle);
252 if(ret)
253 krb5_err(context, 1, ret, "kadm5_init_with_password");
255 signal(SIGINT, SIG_IGN); /* ignore signals for now, the sl command
256 parser will handle SIGINT its own way;
257 we should really take care of this in
258 each function, f.i `get' might be
259 interruptable, but not `create' */
260 if (argc != 0) {
261 ret = sl_command (commands, argc, argv);
262 if(ret == -1)
263 krb5_warnx (context, "unrecognized command: %s", argv[0]);
264 if(ret != 0)
265 exit_status = 1;
266 } else {
267 while(!exit_seen) {
268 ret = sl_command_loop(commands, "kadmin> ", NULL);
269 if(ret != 0)
270 exit_status = 1;
274 kadm5_destroy(kadm_handle);
275 krb5_free_context(context);
276 return exit_status;