spelling fixes (underun prefered relase encyption confunder)
[heimdal.git] / kadmin / get.c
blob1942d63894e56c5d7c81ca5e2cf0be38ab127bcb
1 /*
2 * Copyright (c) 1997-2006 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 <parse_units.h>
37 #include <rtbl.h>
39 static struct field_name {
40 const char *fieldname;
41 unsigned int fieldvalue;
42 unsigned int subvalue;
43 uint32_t extra_mask;
44 const char *default_header;
45 const char *def_longheader;
46 unsigned int flags;
47 } field_names[] = {
48 { "principal", KADM5_PRINCIPAL, 0, 0, "Principal", "Principal", 0 },
49 { "princ_expire_time", KADM5_PRINC_EXPIRE_TIME, 0, 0, "Expiration", "Principal expires", 0 },
50 { "pw_expiration", KADM5_PW_EXPIRATION, 0, 0, "PW-exp", "Password expires", 0 },
51 { "last_pwd_change", KADM5_LAST_PWD_CHANGE, 0, 0, "PW-change", "Last password change", 0 },
52 { "max_life", KADM5_MAX_LIFE, 0, 0, "Max life", "Max ticket life", 0 },
53 { "max_rlife", KADM5_MAX_RLIFE, 0, 0, "Max renew", "Max renewable life", 0 },
54 { "mod_time", KADM5_MOD_TIME, 0, 0, "Mod time", "Last modified", 0 },
55 { "mod_name", KADM5_MOD_NAME, 0, 0, "Modifier", "Modifier", 0 },
56 { "attributes", KADM5_ATTRIBUTES, 0, 0, "Attributes", "Attributes", 0 },
57 { "kvno", KADM5_KVNO, 0, 0, "Kvno", "Kvno", RTBL_ALIGN_RIGHT },
58 { "mkvno", KADM5_MKVNO, 0, 0, "Mkvno", "Mkvno", RTBL_ALIGN_RIGHT },
59 { "last_success", KADM5_LAST_SUCCESS, 0, 0, "Last login", "Last successful login", 0 },
60 { "last_failed", KADM5_LAST_FAILED, 0, 0, "Last fail", "Last failed login", 0 },
61 { "fail_auth_count", KADM5_FAIL_AUTH_COUNT, 0, 0, "Fail count", "Failed login count", RTBL_ALIGN_RIGHT },
62 { "policy", KADM5_POLICY, 0, 0, "Policy", "Policy", 0 },
63 { "keytypes", KADM5_KEY_DATA, 0, KADM5_PRINCIPAL | KADM5_KVNO, "Keytypes", "Keytypes", 0 },
64 { "server-keytypes", KADM5_TL_DATA, KRB5_TL_ETYPES, 0, "Server keytypes", "Supported keytypes (servers)", 0 },
65 { "password", KADM5_TL_DATA, KRB5_TL_PASSWORD, KADM5_KEY_DATA, "Password", "Password", 0 },
66 { "pkinit-acl", KADM5_TL_DATA, KRB5_TL_PKINIT_ACL, 0, "PK-INIT ACL", "PK-INIT ACL", 0 },
67 { "aliases", KADM5_TL_DATA, KRB5_TL_ALIASES, 0, "Aliases", "Aliases", 0 },
68 { "hist-kvno-diff-clnt", KADM5_TL_DATA, KRB5_TL_HIST_KVNO_DIFF_CLNT, 0, "Clnt hist keys", "Historic keys allowed for client", 0 },
69 { "hist-kvno-diff-svc", KADM5_TL_DATA, KRB5_TL_HIST_KVNO_DIFF_SVC, 0, "Svc hist keys", "Historic keys allowed for service", 0 },
70 { NULL, 0, 0, 0, NULL, NULL, 0 }
73 struct field_info {
74 struct field_name *ff;
75 char *header;
76 struct field_info *next;
79 struct get_entry_data {
80 void (*format)(struct get_entry_data*, kadm5_principal_ent_t);
81 rtbl_t table;
82 uint32_t mask;
83 uint32_t extra_mask;
84 struct field_info *chead, **ctail;
85 const char *krb5_config_fname;
86 void *kadm_handle;
87 uint32_t n;
88 int upto;
91 static int
92 add_column(struct get_entry_data *data, struct field_name *ff, const char *header)
94 struct field_info *f = malloc(sizeof(*f));
95 if (f == NULL)
96 return ENOMEM;
97 f->ff = ff;
98 if(header)
99 f->header = strdup(header);
100 else
101 f->header = NULL;
102 f->next = NULL;
103 *data->ctail = f;
104 data->ctail = &f->next;
105 data->mask |= ff->fieldvalue;
106 data->extra_mask |= ff->extra_mask;
107 if(data->table != NULL)
108 rtbl_add_column_by_id(data->table, ff->fieldvalue,
109 header ? header : ff->default_header, ff->flags);
110 return 0;
114 * return 0 iff `salt' actually is the same as the current salt in `k'
117 static int
118 cmp_salt (const krb5_salt *salt, const krb5_key_data *k)
120 if (salt->salttype != (size_t)k->key_data_type[1])
121 return 1;
122 if (salt->saltvalue.length != (size_t)k->key_data_length[1])
123 return 1;
124 return memcmp (salt->saltvalue.data, k->key_data_contents[1],
125 salt->saltvalue.length);
128 static void
129 format_keytype(krb5_key_data *k, krb5_salt *def_salt, char *buf, size_t buf_len)
131 krb5_error_code ret;
132 char *s;
133 int aret;
135 buf[0] = '\0';
136 ret = krb5_enctype_to_string (context,
137 k->key_data_type[0],
138 &s);
139 if (ret) {
140 aret = asprintf (&s, "unknown(%d)", k->key_data_type[0]);
141 if (aret == -1)
142 return; /* Nothing to do here, we have no way to pass the err */
144 strlcpy(buf, s, buf_len);
145 free(s);
147 strlcat(buf, "(", buf_len);
149 ret = krb5_salttype_to_string (context,
150 k->key_data_type[0],
151 k->key_data_type[1],
152 &s);
153 if (ret) {
154 aret = asprintf (&s, "unknown(%d)", k->key_data_type[1]);
155 if (aret == -1)
156 return; /* Again, nothing else to do... */
158 strlcat(buf, s, buf_len);
159 free(s);
161 aret = 0;
162 if (cmp_salt(def_salt, k) == 0)
163 s = strdup("");
164 else if(k->key_data_length[1] == 0)
165 s = strdup("()");
166 else
167 aret = asprintf (&s, "(%.*s)", k->key_data_length[1],
168 (char *)k->key_data_contents[1]);
169 if (aret == -1 || s == NULL)
170 return; /* Again, nothing else we can do... */
171 strlcat(buf, s, buf_len);
172 free(s);
173 aret = asprintf (&s, "[%d]", k->key_data_kvno);
174 if (aret == -1)
175 return;
176 strlcat(buf, ")", buf_len);
178 strlcat(buf, s, buf_len);
179 free(s);
182 static int
183 is_special_file(const char *fname)
185 #ifdef WIN32
186 if (strcasecmp(fname, "con") == 0 || strcasecmp(fname, "nul") == 0 ||
187 strcasecmp(fname, "aux") == 0 || strcasecmp(fname, "prn") == 0)
188 return 1;
189 if ((strncasecmp(fname, "com", sizeof("com") - 1) == 0 ||
190 strncasecmp(fname, "lpt", sizeof("lpt") - 1) == 0) &&
191 fname[sizeof("lpt")] >= '0' && fname[sizeof("lpt")] <= '9' &&
192 fname[sizeof("lpt") + 1] == '\0')
193 return 1;
194 #else
195 if (strncmp(fname, "/dev/", sizeof("/dev/") - 1) == 0)
196 return 1;
197 #endif
198 return 0;
201 static char *
202 write_krb5_config(krb5_tl_data *tl,
203 const char *fn,
204 uint32_t i)
206 char *s = NULL;
207 FILE *f = NULL;
209 if (fn == NULL)
210 return NULL;
211 if (i == 0 || is_special_file(fn))
212 s = strdup(fn);
213 else if (asprintf(&s, "%s-%u", fn, i) == -1)
214 s = NULL;
215 if (s == NULL)
216 krb5_err(context, 1, errno, "Out of memory");
218 /* rk_dumpdata() doesn't allow error checking :( */
219 if ((f = fopen(s, "w")) &&
220 fwrite(tl->tl_data_contents, tl->tl_data_length, 1, f) != 1)
221 krb5_warn(context, errno, "Could not write to %s", fn);
222 if (f && fclose(f))
223 krb5_warn(context, errno, "Could not write to %s", fn);
224 return s;
227 static void
228 format_field(struct get_entry_data *data,
229 kadm5_principal_ent_t princ,
230 unsigned int field,
231 unsigned int subfield,
232 char *buf,
233 size_t buf_len,
234 int condensed)
236 krb5_error_code ret;
238 switch(field) {
239 case KADM5_PRINCIPAL:
240 if(condensed)
241 krb5_unparse_name_fixed_short(context, princ->principal, buf, buf_len);
242 else
243 krb5_unparse_name_fixed(context, princ->principal, buf, buf_len);
244 break;
246 case KADM5_PRINC_EXPIRE_TIME:
247 time_t2str(princ->princ_expire_time, buf, buf_len, !condensed);
248 break;
250 case KADM5_PW_EXPIRATION:
251 time_t2str(princ->pw_expiration, buf, buf_len, !condensed);
252 break;
254 case KADM5_LAST_PWD_CHANGE:
255 time_t2str(princ->last_pwd_change, buf, buf_len, !condensed);
256 break;
258 case KADM5_MAX_LIFE:
259 deltat2str(princ->max_life, buf, buf_len);
260 break;
262 case KADM5_MAX_RLIFE:
263 deltat2str(princ->max_renewable_life, buf, buf_len);
264 break;
266 case KADM5_MOD_TIME:
267 time_t2str(princ->mod_date, buf, buf_len, !condensed);
268 break;
270 case KADM5_MOD_NAME:
271 if (princ->mod_name == NULL)
272 strlcpy(buf, "unknown", buf_len);
273 else if(condensed)
274 krb5_unparse_name_fixed_short(context, princ->mod_name, buf, buf_len);
275 else
276 krb5_unparse_name_fixed(context, princ->mod_name, buf, buf_len);
277 break;
278 case KADM5_ATTRIBUTES:
279 attributes2str (princ->attributes, buf, buf_len);
280 break;
281 case KADM5_KVNO:
282 snprintf(buf, buf_len, "%d", princ->kvno);
283 break;
284 case KADM5_MKVNO:
285 /* XXX libkadm5srv decrypts the keys, so mkvno is always 0. */
286 strlcpy(buf, "unknown", buf_len);
287 break;
288 case KADM5_LAST_SUCCESS:
289 time_t2str(princ->last_success, buf, buf_len, !condensed);
290 break;
291 case KADM5_LAST_FAILED:
292 time_t2str(princ->last_failed, buf, buf_len, !condensed);
293 break;
294 case KADM5_FAIL_AUTH_COUNT:
295 snprintf(buf, buf_len, "%d", princ->fail_auth_count);
296 break;
297 case KADM5_POLICY:
298 if(princ->policy != NULL)
299 strlcpy(buf, princ->policy, buf_len);
300 else
301 strlcpy(buf, "none", buf_len);
302 break;
303 case KADM5_KEY_DATA:{
304 krb5_salt def_salt;
305 int i;
306 char buf2[1024];
308 ret = krb5_get_pw_salt(context, princ->principal, &def_salt);
309 if (ret)
310 krb5_err(context, 1, ret, "krb5_get_pw_salt");
312 *buf = '\0';
313 for (i = 0; i < princ->n_key_data; ++i) {
314 format_keytype(&princ->key_data[i], &def_salt, buf2, sizeof(buf2));
315 if(i > 0)
316 strlcat(buf, ", ", buf_len);
317 strlcat(buf, buf2, buf_len);
319 krb5_free_salt (context, def_salt);
320 break;
322 case KADM5_TL_DATA: {
323 krb5_tl_data *tl;
325 for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next)
326 if ((unsigned)tl->tl_data_type == subfield)
327 break;
328 if (tl == NULL) {
329 strlcpy(buf, "", buf_len);
330 break;
333 switch (subfield) {
334 case KRB5_TL_PASSWORD:
335 snprintf(buf, buf_len, "\"%.*s\"",
336 (int)tl->tl_data_length,
337 (const char *)tl->tl_data_contents);
338 break;
339 case KRB5_TL_ETYPES: {
340 HDB_EncTypeList etypes;
341 size_t i, size;
342 char *str;
344 ret = decode_HDB_EncTypeList(tl->tl_data_contents,
345 tl->tl_data_length,
346 &etypes, &size);
347 if (ret) {
348 snprintf(buf, buf_len, "failed to decode server etypes");
349 break;
351 buf[0] = '\0';
352 for (i = 0; i < etypes.len; i++) {
353 ret = krb5_enctype_to_string(context, etypes.val[i], &str);
354 if (ret == 0) {
355 if (i)
356 strlcat(buf, ",", buf_len);
357 strlcat(buf, str, buf_len);
358 krb5_xfree(str);
361 free_HDB_EncTypeList(&etypes);
362 break;
364 case KRB5_TL_PKINIT_ACL: {
365 HDB_Ext_PKINIT_acl acl;
366 size_t size;
367 size_t i;
369 ret = decode_HDB_Ext_PKINIT_acl(tl->tl_data_contents,
370 tl->tl_data_length,
371 &acl,
372 &size);
373 if (ret) {
374 snprintf(buf, buf_len, "failed to decode ACL");
375 break;
378 buf[0] = '\0';
379 for (i = 0; i < acl.len; i++) {
380 strlcat(buf, "subject: ", buf_len);
381 strlcat(buf, acl.val[i].subject, buf_len);
382 if (acl.val[i].issuer) {
383 strlcat(buf, " issuer:", buf_len);
384 strlcat(buf, *acl.val[i].issuer, buf_len);
386 if (acl.val[i].anchor) {
387 strlcat(buf, " anchor:", buf_len);
388 strlcat(buf, *acl.val[i].anchor, buf_len);
390 if (i + 1 < acl.len)
391 strlcat(buf, ", ", buf_len);
393 free_HDB_Ext_PKINIT_acl(&acl);
394 break;
396 case KRB5_TL_KRB5_CONFIG: {
397 char *fname;
399 fname = write_krb5_config(tl, data->krb5_config_fname, data->n);
400 if (fname) {
401 strlcat(buf, fname, buf_len);
402 free(fname);
404 break;
406 case KRB5_TL_ALIASES: {
407 HDB_Ext_Aliases alias;
408 size_t size;
409 size_t i;
411 ret = decode_HDB_Ext_Aliases(tl->tl_data_contents,
412 tl->tl_data_length,
413 &alias,
414 &size);
415 if (ret) {
416 snprintf(buf, buf_len, "failed to decode alias");
417 break;
419 buf[0] = '\0';
420 for (i = 0; i < alias.aliases.len; i++) {
421 char *p;
422 ret = krb5_unparse_name(context, &alias.aliases.val[i], &p);
423 if (ret)
424 break;
425 if (i > 0)
426 strlcat(buf, " ", buf_len);
427 strlcat(buf, p, buf_len);
428 free(p);
430 free_HDB_Ext_Aliases(&alias);
431 break;
433 default:
434 snprintf(buf, buf_len, "unknown type %d", subfield);
435 break;
437 break;
439 default:
440 strlcpy(buf, "<unknown>", buf_len);
441 break;
445 static void
446 print_entry_short(struct get_entry_data *data, kadm5_principal_ent_t princ)
448 char buf[1024];
449 struct field_info *f;
451 for(f = data->chead; f != NULL; f = f->next) {
452 format_field(data, princ, f->ff->fieldvalue, f->ff->subvalue, buf,
453 sizeof(buf), 1);
454 rtbl_add_column_entry_by_id(data->table, f->ff->fieldvalue, buf);
458 static void
459 print_entry_long(struct get_entry_data *data, kadm5_principal_ent_t princ)
461 char buf[1024];
462 struct field_info *f;
463 int width = 0;
465 for(f = data->chead; f != NULL; f = f->next) {
466 int w = strlen(f->header ? f->header : f->ff->def_longheader);
467 if(w > width)
468 width = w;
470 for(f = data->chead; f != NULL; f = f->next) {
471 format_field(data, princ, f->ff->fieldvalue, f->ff->subvalue, buf,
472 sizeof(buf), 0);
473 printf("%*s: %s\n", width, f->header ? f->header : f->ff->def_longheader, buf);
475 printf("\n");
478 static int
479 do_get_entry(krb5_principal principal, void *data)
481 kadm5_principal_ent_rec princ;
482 krb5_error_code ret;
483 struct get_entry_data *e = data;
485 if (e->upto == 0)
486 return EINTR;
487 if (e->upto > 0)
488 e->upto--;
490 memset(&princ, 0, sizeof(princ));
491 ret = kadm5_get_principal(e->kadm_handle, principal,
492 &princ,
493 e->mask | e->extra_mask);
494 if (ret == 0) {
495 (e->format)(e, &princ);
496 kadm5_free_principal_ent(e->kadm_handle, &princ);
499 e->n++;
500 return ret;
503 static void
504 free_columns(struct get_entry_data *data)
506 struct field_info *f, *next;
507 for(f = data->chead; f != NULL; f = next) {
508 free(f->header);
509 next = f->next;
510 free(f);
512 data->chead = NULL;
513 data->ctail = &data->chead;
516 static int
517 setup_columns(struct get_entry_data *data, const char *column_info)
519 char buf[1024], *q;
520 char *field, *header;
521 struct field_name *f;
523 while(strsep_copy(&column_info, ",", buf, sizeof(buf)) != -1) {
524 q = buf;
525 field = strsep(&q, "=");
526 header = strsep(&q, "=");
527 for(f = field_names; f->fieldname != NULL; f++) {
528 if(strcasecmp(field, f->fieldname) == 0) {
529 add_column(data, f, header);
530 break;
533 if(f->fieldname == NULL) {
534 krb5_warnx(context, "unknown field name \"%s\"", field);
535 free_columns(data);
536 return -1;
539 return 0;
542 static int
543 do_list_entry(krb5_principal principal, void *data)
545 char buf[1024];
546 int *upto = data;
547 krb5_error_code ret;
549 if (*upto == 0)
550 return EINTR;
551 if (*upto > 0)
552 (*upto)--;
554 ret = krb5_unparse_name_fixed_short(context, principal, buf, sizeof(buf));
555 if (ret != 0)
556 return ret;
557 printf("%s\n", buf);
558 return 0;
561 static int
562 listit(const char *funcname, int upto, int argc, char **argv)
564 int i;
565 krb5_error_code ret, saved_ret = 0;
567 for (i = 0; i < argc; i++) {
568 ret = foreach_principal(argv[i], do_list_entry, funcname, &upto);
569 if (saved_ret == 0 && ret != 0)
570 saved_ret = ret;
572 return saved_ret != 0;
575 #define DEFAULT_COLUMNS_SHORT "principal,princ_expire_time,pw_expiration,last_pwd_change,max_life,max_rlife"
576 #define DEFAULT_COLUMNS_LONG "principal,princ_expire_time,pw_expiration,last_pwd_change,max_life,max_rlife,kvno,mkvno,last_success,last_failed,fail_auth_count,mod_time,mod_name,attributes,server-keytypes,keytypes,pkinit-acl,aliases"
578 static int
579 getit(struct get_options *opt, const char *name, int argc, char **argv)
581 int i;
582 krb5_error_code ret;
583 struct get_entry_data data;
585 if(opt->long_flag == -1 && (opt->short_flag == 1 || opt->terse_flag == 1))
586 opt->long_flag = 0;
587 if(opt->short_flag == -1 && (opt->long_flag == 1 || opt->terse_flag == 1))
588 opt->short_flag = 0;
589 if(opt->terse_flag == -1 && (opt->long_flag == 1 || opt->short_flag == 1))
590 opt->terse_flag = 0;
591 if(opt->long_flag == 0 && opt->short_flag == 0 && opt->terse_flag == 0)
592 opt->short_flag = 1;
594 if (opt->terse_flag)
595 return listit(name, opt->upto_integer, argc, argv);
597 data.kadm_handle = NULL;
598 ret = kadm5_dup_context(kadm_handle, &data.kadm_handle);
599 if (ret)
600 krb5_err(context, 1, ret, "Could not duplicate kadmin connection");
601 data.table = NULL;
602 data.chead = NULL;
603 data.ctail = &data.chead;
604 data.mask = 0;
605 data.extra_mask = 0;
606 data.krb5_config_fname = opt->krb5_config_file_string;
607 data.upto = opt->upto_integer;
608 data.n = 0;
610 if(opt->short_flag) {
611 data.table = rtbl_create();
612 rtbl_set_separator(data.table, " ");
613 data.format = print_entry_short;
614 } else
615 data.format = print_entry_long;
616 if(opt->column_info_string == NULL) {
617 if(opt->long_flag)
618 ret = setup_columns(&data, DEFAULT_COLUMNS_LONG);
619 else
620 ret = setup_columns(&data, DEFAULT_COLUMNS_SHORT);
621 } else
622 ret = setup_columns(&data, opt->column_info_string);
624 if(ret != 0) {
625 if(data.table != NULL)
626 rtbl_destroy(data.table);
627 return 0;
630 for(i = 0; i < argc; i++)
631 ret = foreach_principal(argv[i], do_get_entry, name, &data);
633 kadm5_destroy(data.kadm_handle);
635 if(data.table != NULL) {
636 rtbl_format(data.table, stdout);
637 rtbl_destroy(data.table);
639 free_columns(&data);
640 return ret != 0;
644 get_entry(struct get_options *opt, int argc, char **argv)
646 return getit(opt, "get", argc, argv);
650 list_princs(struct list_options *opt, int argc, char **argv)
652 struct get_options get_opt;
654 if(sizeof(struct get_options) != sizeof(struct list_options)) {
655 krb5_warnx(context, "programmer error: sizeof(struct get_options) != sizeof(struct list_options)");
656 return 0;
658 memset(&get_opt, 0, sizeof(get_opt));
659 get_opt.long_flag = opt->long_flag;
660 get_opt.short_flag = opt->short_flag;
661 get_opt.terse_flag = opt->terse_flag;
662 get_opt.column_info_string = opt->column_info_string;
663 get_opt.upto_integer = opt->upto_integer;
664 return getit(&get_opt, "list", argc, argv);