1 /* $NetBSD: iprop-log.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <krb5/parse_time.h>
39 #include "iprop-commands.h"
43 static krb5_context context
;
45 static kadm5_server_context
*
46 get_kadmin_context(const char *config_file
, char *realm
)
48 kadm5_config_params conf
;
53 if (config_file
== NULL
) {
55 asprintf(&file
, "%s/kdc.conf", hdb_db_dir(context
));
57 errx(1, "out of memory");
61 ret
= krb5_prepend_config_files_default(config_file
, &files
);
63 krb5_err(context
, 1, ret
, "getting configuration files");
65 ret
= krb5_set_config_files(context
, files
);
66 krb5_free_config_files(files
);
68 krb5_err(context
, 1, ret
, "reading configuration files");
70 memset(&conf
, 0, sizeof(conf
));
72 conf
.mask
|= KADM5_CONFIG_REALM
;
76 ret
= kadm5_init_with_password_ctx (context
,
83 krb5_err (context
, 1, ret
, "kadm5_init_with_password_ctx");
85 return (kadm5_server_context
*)kadm_handle
;
92 static const char *op_names
[] = {
107 print_entry(kadm5_server_context
*server_context
,
118 krb5_principal source
;
121 krb5_context scontext
= server_context
->context
;
123 off_t end
= krb5_storage_seek(sp
, 0, SEEK_CUR
) + len
;
127 strftime(t
, sizeof(t
), "%Y-%m-%d %H:%M:%S", localtime(×tamp
));
129 if((int)op
< (int)kadm_get
|| (int)op
> (int)kadm_nop
) {
130 printf("unknown op: %d\n", op
);
131 krb5_storage_seek(sp
, end
, SEEK_SET
);
135 printf ("%s: ver = %u, timestamp = %s, len = %u\n",
136 op_names
[op
], ver
, t
, len
);
139 krb5_ret_principal(sp
, &source
);
140 krb5_unparse_name(scontext
, source
, &name1
);
141 printf(" %s\n", name1
);
143 krb5_free_principal(scontext
, source
);
146 ret
= krb5_data_alloc(&data
, len
);
148 krb5_err (scontext
, 1, ret
, "kadm_rename: data alloc: %d", len
);
149 krb5_ret_principal(sp
, &source
);
150 krb5_storage_read(sp
, data
.data
, data
.length
);
151 hdb_value2entry(scontext
, &data
, &ent
);
152 krb5_unparse_name(scontext
, source
, &name1
);
153 krb5_unparse_name(scontext
, ent
.principal
, &name2
);
154 printf(" %s -> %s\n", name1
, name2
);
157 krb5_free_principal(scontext
, source
);
158 free_hdb_entry(&ent
);
161 ret
= krb5_data_alloc(&data
, len
);
163 krb5_err (scontext
, 1, ret
, "kadm_create: data alloc: %d", len
);
164 krb5_storage_read(sp
, data
.data
, data
.length
);
165 ret
= hdb_value2entry(scontext
, &data
, &ent
);
171 ret
= krb5_data_alloc(&data
, len
);
173 krb5_err (scontext
, 1, ret
, "kadm_modify: data alloc: %d", len
);
174 krb5_ret_int32(sp
, &mask
);
175 krb5_storage_read(sp
, data
.data
, data
.length
);
176 ret
= hdb_value2entry(scontext
, &data
, &ent
);
180 if(ent
.principal
/* mask & KADM5_PRINCIPAL */) {
181 krb5_unparse_name(scontext
, ent
.principal
, &name1
);
182 printf(" principal = %s\n", name1
);
185 if(mask
& KADM5_PRINC_EXPIRE_TIME
) {
186 if(ent
.valid_end
== NULL
) {
187 strlcpy(t
, "never", sizeof(t
));
189 strftime(t
, sizeof(t
), "%Y-%m-%d %H:%M:%S",
190 localtime(ent
.valid_end
));
192 printf(" expires = %s\n", t
);
194 if(mask
& KADM5_PW_EXPIRATION
) {
195 if(ent
.pw_end
== NULL
) {
196 strlcpy(t
, "never", sizeof(t
));
198 strftime(t
, sizeof(t
), "%Y-%m-%d %H:%M:%S",
199 localtime(ent
.pw_end
));
201 printf(" password exp = %s\n", t
);
203 if(mask
& KADM5_LAST_PWD_CHANGE
) {
205 if(mask
& KADM5_ATTRIBUTES
) {
206 unparse_flags(HDBFlags2int(ent
.flags
),
207 asn1_HDBFlags_units(), t
, sizeof(t
));
208 printf(" attributes = %s\n", t
);
210 if(mask
& KADM5_MAX_LIFE
) {
211 if(ent
.max_life
== NULL
)
212 strlcpy(t
, "for ever", sizeof(t
));
214 unparse_time(*ent
.max_life
, t
, sizeof(t
));
215 printf(" max life = %s\n", t
);
217 if(mask
& KADM5_MAX_RLIFE
) {
218 if(ent
.max_renew
== NULL
)
219 strlcpy(t
, "for ever", sizeof(t
));
221 unparse_time(*ent
.max_renew
, t
, sizeof(t
));
222 printf(" max rlife = %s\n", t
);
224 if(mask
& KADM5_MOD_TIME
) {
225 printf(" mod time\n");
227 if(mask
& KADM5_MOD_NAME
) {
228 printf(" mod name\n");
230 if(mask
& KADM5_KVNO
) {
231 printf(" kvno = %d\n", ent
.kvno
);
233 if(mask
& KADM5_MKVNO
) {
236 if(mask
& KADM5_AUX_ATTRIBUTES
) {
237 printf(" aux attributes\n");
239 if(mask
& KADM5_POLICY
) {
242 if(mask
& KADM5_POLICY_CLR
) {
243 printf(" mod time\n");
245 if(mask
& KADM5_LAST_SUCCESS
) {
246 printf(" last success\n");
248 if(mask
& KADM5_LAST_FAILED
) {
249 printf(" last failed\n");
251 if(mask
& KADM5_FAIL_AUTH_COUNT
) {
252 printf(" fail auth count\n");
254 if(mask
& KADM5_KEY_DATA
) {
255 printf(" key data\n");
257 if(mask
& KADM5_TL_DATA
) {
258 printf(" tl data\n");
260 free_hdb_entry(&ent
);
267 krb5_storage_seek(sp
, end
, SEEK_SET
);
271 iprop_dump(struct dump_options
*opt
, int argc
, char **argv
)
273 kadm5_server_context
*server_context
;
276 server_context
= get_kadmin_context(opt
->config_file_string
,
279 ret
= kadm5_log_init (server_context
);
281 krb5_err (context
, 1, ret
, "kadm5_log_init");
283 ret
= kadm5_log_foreach (server_context
, print_entry
, NULL
);
285 krb5_warn(context
, ret
, "kadm5_log_foreach");
287 ret
= kadm5_log_end (server_context
);
289 krb5_warn(context
, ret
, "kadm5_log_end");
294 iprop_truncate(struct truncate_options
*opt
, int argc
, char **argv
)
296 kadm5_server_context
*server_context
;
299 server_context
= get_kadmin_context(opt
->config_file_string
,
302 ret
= kadm5_log_truncate (server_context
);
304 krb5_err (context
, 1, ret
, "kadm5_log_truncate");
310 last_version(struct last_version_options
*opt
, int argc
, char **argv
)
312 kadm5_server_context
*server_context
;
316 server_context
= get_kadmin_context(opt
->config_file_string
,
319 ret
= kadm5_log_init (server_context
);
321 krb5_err (context
, 1, ret
, "kadm5_log_init");
323 ret
= kadm5_log_get_version (server_context
, &version
);
325 krb5_err (context
, 1, ret
, "kadm5_log_get_version");
327 ret
= kadm5_log_end (server_context
);
329 krb5_warn(context
, ret
, "kadm5_log_end");
331 printf("version: %lu\n", (unsigned long)version
);
340 int start_version
= -1;
341 int end_version
= -1;
344 apply_entry(kadm5_server_context
*server_context
,
352 struct replay_options
*opt
= ctx
;
355 if((opt
->start_version_integer
!= -1 && ver
< (uint32_t)opt
->start_version_integer
) ||
356 (opt
->end_version_integer
!= -1 && ver
> (uint32_t)opt
->end_version_integer
)) {
357 /* XXX skip this entry */
358 krb5_storage_seek(sp
, len
, SEEK_CUR
);
361 printf ("ver %u... ", ver
);
364 ret
= kadm5_log_replay (server_context
,
367 krb5_warn (server_context
->context
, ret
, "kadm5_log_replay");
373 iprop_replay(struct replay_options
*opt
, int argc
, char **argv
)
375 kadm5_server_context
*server_context
;
378 server_context
= get_kadmin_context(opt
->config_file_string
,
381 ret
= server_context
->db
->hdb_open(context
,
383 O_RDWR
| O_CREAT
, 0600);
385 krb5_err (context
, 1, ret
, "db->open");
387 ret
= kadm5_log_init (server_context
);
389 krb5_err (context
, 1, ret
, "kadm5_log_init");
391 ret
= kadm5_log_foreach (server_context
, apply_entry
, opt
);
393 krb5_warn(context
, ret
, "kadm5_log_foreach");
394 ret
= kadm5_log_end (server_context
);
396 krb5_warn(context
, ret
, "kadm5_log_end");
397 ret
= server_context
->db
->hdb_close (context
, server_context
->db
);
399 krb5_err (context
, 1, ret
, "db->close");
404 static int help_flag
;
405 static int version_flag
;
407 static struct getargs args
[] = {
408 { "version", 0, arg_flag
, &version_flag
,
411 { "help", 'h', arg_flag
, &help_flag
,
416 static int num_args
= sizeof(args
) / sizeof(args
[0]);
419 help(void *opt
, int argc
, char **argv
)
422 sl_help(commands
, 1, argv
- 1 /* XXX */);
424 SL_cmd
*c
= sl_match (commands
, argv
[0], 0);
426 fprintf (stderr
, "No such command: %s. "
427 "Try \"help\" for a list of commands\n",
431 static char shelp
[] = "--help";
437 fprintf(stderr
, "\n");
439 if(c
->help
&& *c
->help
)
440 fprintf (stderr
, "%s\n", c
->help
);
441 if((++c
)->name
&& c
->func
== NULL
) {
443 fprintf (stderr
, "Synonyms:");
444 while (c
->name
&& c
->func
== NULL
) {
445 fprintf (stderr
, "%s%s", f
? ", " : " ", (c
++)->name
);
448 fprintf (stderr
, "\n");
458 arg_printusage(args
, num_args
, NULL
, "command");
463 main(int argc
, char **argv
)
468 setprogname(argv
[0]);
470 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
483 ret
= krb5_init_context(&context
);
485 errx(1, "krb5_init_context failed with: %d\n", ret
);
487 ret
= sl_command(commands
, argc
, argv
);
489 warnx ("unrecognized command: %s", argv
[0]);