No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / kadmin.c
blob8e12a7dbdf4eec293a01af4d07ff9c68222dfdc1
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("$Heimdal: kadmin.c 22253 2007-12-09 06:00:00Z lha $"
39 "$NetBSD$");
41 static char *config_file;
42 static char *keyfile;
43 int local_flag;
44 static int ad_flag;
45 static int help_flag;
46 static int version_flag;
47 static char *realm;
48 static char *admin_server;
49 static int server_port = 0;
50 static char *client_name;
51 static char *keytab;
52 static char *check_library = NULL;
53 static char *check_function = NULL;
54 static getarg_strings policy_libraries = { 0, NULL };
56 static struct getargs args[] = {
57 { "principal", 'p', arg_string, &client_name,
58 "principal to authenticate as" },
59 { "keytab", 'K', arg_string, &keytab,
60 "keytab for authentication principal" },
62 "config-file", 'c', arg_string, &config_file,
63 "location of config file", "file"
66 "key-file", 'k', arg_string, &keyfile,
67 "location of master key file", "file"
70 "realm", 'r', arg_string, &realm,
71 "realm to use", "realm"
74 "admin-server", 'a', arg_string, &admin_server,
75 "server to contact", "host"
78 "server-port", 's', arg_integer, &server_port,
79 "port to use", "port number"
81 { "ad", 0, arg_flag, &ad_flag, "active directory admin mode" },
82 #ifdef HAVE_DLOPEN
83 { "check-library", 0, arg_string, &check_library,
84 "library to load password check function from", "library" },
85 { "check-function", 0, arg_string, &check_function,
86 "password check function to load", "function" },
87 { "policy-libraries", 0, arg_strings, &policy_libraries,
88 "password check function to load", "function" },
89 #endif
90 { "local", 'l', arg_flag, &local_flag, "local admin mode" },
91 { "help", 'h', arg_flag, &help_flag },
92 { "version", 'v', arg_flag, &version_flag }
95 static int num_args = sizeof(args) / sizeof(args[0]);
98 krb5_context context;
99 void *kadm_handle;
102 help(void *opt, int argc, char **argv)
104 sl_slc_help(commands, argc, argv);
105 return 0;
108 static int exit_seen = 0;
111 exit_kadmin (void *opt, int argc, char **argv)
113 exit_seen = 1;
114 return 0;
117 static void
118 usage(int ret)
120 arg_printusage (args, num_args, NULL, "[command]");
121 exit (ret);
125 get_privs(void *opt, int argc, char **argv)
127 uint32_t privs;
128 char str[128];
129 kadm5_ret_t ret;
131 ret = kadm5_get_privs(kadm_handle, &privs);
132 if(ret)
133 krb5_warn(context, ret, "kadm5_get_privs");
134 else{
135 ret =_kadm5_privs_to_string(privs, str, sizeof(str));
136 printf("%s\n", str);
138 return 0;
142 main(int argc, char **argv)
144 krb5_error_code ret;
145 char **files;
146 kadm5_config_params conf;
147 int optidx = 0;
148 int exit_status = 0;
150 setprogname(argv[0]);
152 ret = krb5_init_context(&context);
153 if (ret)
154 errx (1, "krb5_init_context failed: %d", ret);
156 if(getarg(args, num_args, argc, argv, &optidx))
157 usage(1);
159 if (help_flag)
160 usage (0);
162 if (version_flag) {
163 print_version(NULL);
164 exit(0);
167 argc -= optidx;
168 argv += optidx;
170 if (config_file == NULL) {
171 asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
172 if (config_file == NULL)
173 errx(1, "out of memory");
176 ret = krb5_prepend_config_files_default(config_file, &files);
177 if (ret)
178 krb5_err(context, 1, ret, "getting configuration files");
180 ret = krb5_set_config_files(context, files);
181 krb5_free_config_files(files);
182 if(ret)
183 krb5_err(context, 1, ret, "reading configuration files");
185 memset(&conf, 0, sizeof(conf));
186 if(realm) {
187 krb5_set_default_realm(context, realm); /* XXX should be fixed
188 some other way */
189 conf.realm = realm;
190 conf.mask |= KADM5_CONFIG_REALM;
193 if (admin_server) {
194 conf.admin_server = admin_server;
195 conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
198 if (server_port) {
199 conf.kadmind_port = htons(server_port);
200 conf.mask |= KADM5_CONFIG_KADMIND_PORT;
203 if (keyfile) {
204 conf.stash_file = keyfile;
205 conf.mask |= KADM5_CONFIG_STASH_FILE;
208 if(local_flag) {
209 int i;
211 kadm5_setup_passwd_quality_check (context,
212 check_library, check_function);
214 for (i = 0; i < policy_libraries.num_strings; i++) {
215 ret = kadm5_add_passwd_quality_verifier(context,
216 policy_libraries.strings[i]);
217 if (ret)
218 krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
220 ret = kadm5_add_passwd_quality_verifier(context, NULL);
221 if (ret)
222 krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
224 ret = kadm5_s_init_with_password_ctx(context,
225 KADM5_ADMIN_SERVICE,
226 NULL,
227 KADM5_ADMIN_SERVICE,
228 &conf, 0, 0,
229 &kadm_handle);
230 } else if (ad_flag) {
231 if (client_name == NULL)
232 krb5_errx(context, 1, "keytab mode require principal name");
233 ret = kadm5_ad_init_with_password_ctx(context,
234 client_name,
235 NULL,
236 KADM5_ADMIN_SERVICE,
237 &conf, 0, 0,
238 &kadm_handle);
239 } else if (keytab) {
240 if (client_name == NULL)
241 krb5_errx(context, 1, "keytab mode require principal name");
242 ret = kadm5_c_init_with_skey_ctx(context,
243 client_name,
244 keytab,
245 KADM5_ADMIN_SERVICE,
246 &conf, 0, 0,
247 &kadm_handle);
248 } else
249 ret = kadm5_c_init_with_password_ctx(context,
250 client_name,
251 NULL,
252 KADM5_ADMIN_SERVICE,
253 &conf, 0, 0,
254 &kadm_handle);
256 if(ret)
257 krb5_err(context, 1, ret, "kadm5_init_with_password");
259 signal(SIGINT, SIG_IGN); /* ignore signals for now, the sl command
260 parser will handle SIGINT its own way;
261 we should really take care of this in
262 each function, f.i `get' might be
263 interruptable, but not `create' */
264 if (argc != 0) {
265 ret = sl_command (commands, argc, argv);
266 if(ret == -1)
267 krb5_warnx (context, "unrecognized command: %s", argv[0]);
268 else if (ret == -2)
269 ret = 0;
270 if(ret != 0)
271 exit_status = 1;
272 } else {
273 while(!exit_seen) {
274 ret = sl_command_loop(commands, "kadmin> ", NULL);
275 if (ret == -2)
276 exit_seen = 1;
277 else if (ret != 0)
278 exit_status = 1;
282 kadm5_destroy(kadm_handle);
283 krb5_free_context(context);
284 return exit_status;