Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / heimdal / lib / kadm5 / dump_log.c
1 /*
2  * Copyright (c) 1997 - 2002 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 "iprop.h"
35 #include "parse_time.h"
36
37 RCSID("$Id: dump_log.c,v 1.12 2002/05/24 15:19:18 joda Exp $");
38
39 static char *op_names[] = {
40     "get",
41     "delete",
42     "create",
43     "rename",
44     "chpass",
45     "modify",
46     "randkey",
47     "get_privs",
48     "get_princs",
49     "chpass_with_key",
50     "nop"
51 };
52
53 static void
54 print_entry(kadm5_server_context *server_context,
55             u_int32_t ver,
56             time_t timestamp,
57             enum kadm_ops op,
58             u_int32_t len,
59             krb5_storage *sp)
60 {
61     char t[256];
62     int32_t mask;
63     hdb_entry ent;
64     krb5_principal source;
65     char *name1, *name2;
66     krb5_data data;
67     krb5_context context = server_context->context;
68
69     off_t end = krb5_storage_seek(sp, 0, SEEK_CUR) + len;
70     
71     krb5_error_code ret;
72
73     strftime(t, sizeof(t), "%Y-%m-%d %H:%M:%S", localtime(&timestamp));
74
75     if(op < kadm_get || op > kadm_nop) {
76         printf("unknown op: %d\n", op);
77         krb5_storage_seek(sp, end, SEEK_SET);
78         return;
79     }
80
81     printf ("%s: ver = %u, timestamp = %s, len = %u\n",
82             op_names[op], ver, t, len);
83     switch(op) {
84     case kadm_delete:
85         krb5_ret_principal(sp, &source);
86         krb5_unparse_name(context, source, &name1);
87         printf("    %s\n", name1);
88         free(name1);
89         krb5_free_principal(context, source);
90         break;
91     case kadm_rename:
92         krb5_data_alloc(&data, len);
93         krb5_ret_principal(sp, &source);
94         krb5_storage_read(sp, data.data, data.length);
95         hdb_value2entry(context, &data, &ent);
96         krb5_unparse_name(context, source, &name1);
97         krb5_unparse_name(context, ent.principal, &name2);
98         printf("    %s -> %s\n", name1, name2);
99         free(name1);
100         free(name2);
101         krb5_free_principal(context, source);
102         hdb_free_entry(context, &ent);
103         break;
104     case kadm_create:
105         krb5_data_alloc(&data, len);
106         krb5_storage_read(sp, data.data, data.length);
107         ret = hdb_value2entry(context, &data, &ent);
108         if(ret)
109             abort();
110         mask = ~0;
111         goto foo;
112     case kadm_modify:
113         krb5_data_alloc(&data, len);
114         krb5_ret_int32(sp, &mask);
115         krb5_storage_read(sp, data.data, data.length);
116         ret = hdb_value2entry(context, &data, &ent);
117         if(ret)
118             abort();
119     foo:
120         if(ent.principal /* mask & KADM5_PRINCIPAL */) {
121             krb5_unparse_name(context, ent.principal, &name1);
122             printf("    principal = %s\n", name1);
123             free(name1);
124         }
125         if(mask & KADM5_PRINC_EXPIRE_TIME) {
126             if(ent.valid_end == NULL) {
127                 strcpy(t, "never");
128             } else {
129                 strftime(t, sizeof(t), "%Y-%m-%d %H:%M:%S", 
130                          localtime(ent.valid_end));
131             }
132             printf("    expires = %s\n", t);
133         }
134         if(mask & KADM5_PW_EXPIRATION) {
135             if(ent.pw_end == NULL) {
136                 strcpy(t, "never");
137             } else {
138                 strftime(t, sizeof(t), "%Y-%m-%d %H:%M:%S", 
139                          localtime(ent.pw_end));
140             }
141             printf("    password exp = %s\n", t);
142         }
143         if(mask & KADM5_LAST_PWD_CHANGE) {
144         }
145         if(mask & KADM5_ATTRIBUTES) {
146             unparse_flags(HDBFlags2int(ent.flags), 
147                           HDBFlags_units, t, sizeof(t));
148             printf("    attributes = %s\n", t);
149         }
150         if(mask & KADM5_MAX_LIFE) {
151             if(ent.max_life == NULL)
152                 strcpy(t, "for ever");
153             else
154                 unparse_time(*ent.max_life, t, sizeof(t));
155             printf("    max life = %s\n", t);
156         }
157         if(mask & KADM5_MAX_RLIFE) {
158             if(ent.max_renew == NULL)
159                 strcpy(t, "for ever");
160             else
161                 unparse_time(*ent.max_renew, t, sizeof(t));
162             printf("    max rlife = %s\n", t);
163         }
164         if(mask & KADM5_MOD_TIME) {
165             printf("    mod time\n");
166         }
167         if(mask & KADM5_MOD_NAME) {
168             printf("    mod name\n");
169         }
170         if(mask & KADM5_KVNO) {
171             printf("    kvno = %d\n", ent.kvno);
172         }
173         if(mask & KADM5_MKVNO) {
174             printf("    mkvno\n");
175         }
176         if(mask & KADM5_AUX_ATTRIBUTES) {
177             printf("    aux attributes\n");
178         }
179         if(mask & KADM5_POLICY) {
180             printf("    policy\n");
181         }
182         if(mask & KADM5_POLICY_CLR) {
183             printf("    mod time\n");
184         }
185         if(mask & KADM5_LAST_SUCCESS) {
186             printf("    last success\n");
187         }
188         if(mask & KADM5_LAST_FAILED) {
189             printf("    last failed\n");
190         }
191         if(mask & KADM5_FAIL_AUTH_COUNT) {
192             printf("    fail auth count\n");
193         }
194         if(mask & KADM5_KEY_DATA) {
195             printf("    key data\n");
196         }
197         if(mask & KADM5_TL_DATA) {
198             printf("    tl data\n");
199         }
200         hdb_free_entry(context, &ent);
201         break;
202     case kadm_nop :
203         break;
204     default:
205         abort();
206     }
207     krb5_storage_seek(sp, end, SEEK_SET);
208 }
209
210 static char *realm;
211 static int version_flag;
212 static int help_flag;
213
214 static struct getargs args[] = {
215     { "realm", 'r', arg_string, &realm },
216     { "version", 0, arg_flag, &version_flag },
217     { "help", 0, arg_flag, &help_flag }
218 };
219 int num_args = sizeof(args) / sizeof(args[0]);
220
221 int
222 main(int argc, char **argv)
223 {
224     krb5_context context;
225     krb5_error_code ret;
226     void *kadm_handle;
227     kadm5_server_context *server_context;
228     kadm5_config_params conf;
229
230     krb5_program_setup(&context, argc, argv, args, num_args, NULL);
231     
232     if(help_flag)
233         krb5_std_usage(0, args, num_args);
234     if(version_flag) {
235         print_version(NULL);
236         exit(0);
237     }
238
239     memset(&conf, 0, sizeof(conf));
240     if(realm) {
241         conf.mask |= KADM5_CONFIG_REALM;
242         conf.realm = realm;
243     }
244     ret = kadm5_init_with_password_ctx (context,
245                                         KADM5_ADMIN_SERVICE,
246                                         NULL,
247                                         KADM5_ADMIN_SERVICE,
248                                         &conf, 0, 0, 
249                                         &kadm_handle);
250     if (ret)
251         krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
252
253     server_context = (kadm5_server_context *)kadm_handle;
254
255     ret = kadm5_log_init (server_context);
256     if (ret)
257         krb5_err (context, 1, ret, "kadm5_log_init");
258
259     ret = kadm5_log_foreach (server_context, print_entry);
260     if(ret)
261         krb5_warn(context, ret, "kadm5_log_foreach");
262
263     ret = kadm5_log_end (server_context);
264     if (ret)
265         krb5_warn(context, ret, "kadm5_log_end");
266     return 0;
267 }