kdc: _kdc_find_etype prevent NULL dereference
[heimdal.git] / kadmin / get.c
bloba884e11e96be481fb40cdc39708871d5c24702f9
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 uint32_t n;
89 static int
90 add_column(struct get_entry_data *data, struct field_name *ff, const char *header)
92 struct field_info *f = malloc(sizeof(*f));
93 if (f == NULL)
94 return ENOMEM;
95 f->ff = ff;
96 if(header)
97 f->header = strdup(header);
98 else
99 f->header = NULL;
100 f->next = NULL;
101 *data->ctail = f;
102 data->ctail = &f->next;
103 data->mask |= ff->fieldvalue;
104 data->extra_mask |= ff->extra_mask;
105 if(data->table != NULL)
106 rtbl_add_column_by_id(data->table, ff->fieldvalue,
107 header ? header : ff->default_header, ff->flags);
108 return 0;
112 * return 0 iff `salt' actually is the same as the current salt in `k'
115 static int
116 cmp_salt (const krb5_salt *salt, const krb5_key_data *k)
118 if (salt->salttype != (size_t)k->key_data_type[1])
119 return 1;
120 if (salt->saltvalue.length != (size_t)k->key_data_length[1])
121 return 1;
122 return memcmp (salt->saltvalue.data, k->key_data_contents[1],
123 salt->saltvalue.length);
126 static void
127 format_keytype(krb5_key_data *k, krb5_salt *def_salt, char *buf, size_t buf_len)
129 krb5_error_code ret;
130 char *s;
131 int aret;
133 buf[0] = '\0';
134 ret = krb5_enctype_to_string (context,
135 k->key_data_type[0],
136 &s);
137 if (ret) {
138 aret = asprintf (&s, "unknown(%d)", k->key_data_type[0]);
139 if (aret == -1)
140 return; /* Nothing to do here, we have no way to pass the err */
142 strlcpy(buf, s, buf_len);
143 free(s);
145 strlcat(buf, "(", buf_len);
147 ret = krb5_salttype_to_string (context,
148 k->key_data_type[0],
149 k->key_data_type[1],
150 &s);
151 if (ret) {
152 aret = asprintf (&s, "unknown(%d)", k->key_data_type[1]);
153 if (aret == -1)
154 return; /* Again, nothing else to do... */
156 strlcat(buf, s, buf_len);
157 free(s);
159 aret = 0;
160 if (cmp_salt(def_salt, k) == 0)
161 s = strdup("");
162 else if(k->key_data_length[1] == 0)
163 s = strdup("()");
164 else
165 aret = asprintf (&s, "(%.*s)", k->key_data_length[1],
166 (char *)k->key_data_contents[1]);
167 if (aret == -1 || s == NULL)
168 return; /* Again, nothing else we can do... */
169 strlcat(buf, s, buf_len);
170 free(s);
171 aret = asprintf (&s, "[%d]", k->key_data_kvno);
172 if (aret == -1)
173 return;
174 strlcat(buf, ")", buf_len);
176 strlcat(buf, s, buf_len);
177 free(s);
180 static int
181 is_special_file(const char *fname)
183 #ifdef WIN32
184 if (strcasecmp(fname, "con") == 0 || strcasecmp(fname, "nul") == 0 ||
185 strcasecmp(fname, "aux") == 0 || strcasecmp(fname, "prn") == 0)
186 return 1;
187 if ((strncasecmp(fname, "com", sizeof("com") - 1) == 0 ||
188 strncasecmp(fname, "lpt", sizeof("lpt") - 1) == 0) &&
189 fname[sizeof("lpt")] >= '0' && fname[sizeof("lpt")] <= '9' &&
190 fname[sizeof("lpt") + 1] == '\0')
191 return 1;
192 #else
193 if (strncmp(fname, "/dev/", sizeof("/dev/") - 1) == 0)
194 return 1;
195 #endif
196 return 0;
199 static char *
200 write_krb5_config(krb5_tl_data *tl,
201 const char *fn,
202 uint32_t i)
204 char *s = NULL;
205 FILE *f = NULL;
207 if (fn == NULL)
208 return NULL;
209 if (i == 0 || is_special_file(fn))
210 s = strdup(fn);
211 else if (asprintf(&s, "%s-%u", fn, i) == -1)
212 s = NULL;
213 if (s == NULL)
214 krb5_err(context, 1, errno, "Out of memory");
216 /* rk_dumpdata() doesn't allow error checking :( */
217 if ((f = fopen(s, "w")) &&
218 fwrite(tl->tl_data_contents, tl->tl_data_length, 1, f) != 1)
219 krb5_warn(context, errno, "Could not write to %s", fn);
220 if (f && fclose(f))
221 krb5_warn(context, errno, "Could not write to %s", fn);
222 return s;
225 static void
226 format_field(struct get_entry_data *data,
227 kadm5_principal_ent_t princ,
228 unsigned int field,
229 unsigned int subfield,
230 char *buf,
231 size_t buf_len,
232 int condensed)
234 switch(field) {
235 case KADM5_PRINCIPAL:
236 if(condensed)
237 krb5_unparse_name_fixed_short(context, princ->principal, buf, buf_len);
238 else
239 krb5_unparse_name_fixed(context, princ->principal, buf, buf_len);
240 break;
242 case KADM5_PRINC_EXPIRE_TIME:
243 time_t2str(princ->princ_expire_time, buf, buf_len, !condensed);
244 break;
246 case KADM5_PW_EXPIRATION:
247 time_t2str(princ->pw_expiration, buf, buf_len, !condensed);
248 break;
250 case KADM5_LAST_PWD_CHANGE:
251 time_t2str(princ->last_pwd_change, buf, buf_len, !condensed);
252 break;
254 case KADM5_MAX_LIFE:
255 deltat2str(princ->max_life, buf, buf_len);
256 break;
258 case KADM5_MAX_RLIFE:
259 deltat2str(princ->max_renewable_life, buf, buf_len);
260 break;
262 case KADM5_MOD_TIME:
263 time_t2str(princ->mod_date, buf, buf_len, !condensed);
264 break;
266 case KADM5_MOD_NAME:
267 if (princ->mod_name == NULL)
268 strlcpy(buf, "unknown", buf_len);
269 else if(condensed)
270 krb5_unparse_name_fixed_short(context, princ->mod_name, buf, buf_len);
271 else
272 krb5_unparse_name_fixed(context, princ->mod_name, buf, buf_len);
273 break;
274 case KADM5_ATTRIBUTES:
275 attributes2str (princ->attributes, buf, buf_len);
276 break;
277 case KADM5_KVNO:
278 snprintf(buf, buf_len, "%d", princ->kvno);
279 break;
280 case KADM5_MKVNO:
281 /* XXX libkadm5srv decrypts the keys, so mkvno is always 0. */
282 strlcpy(buf, "unknown", buf_len);
283 break;
284 case KADM5_LAST_SUCCESS:
285 time_t2str(princ->last_success, buf, buf_len, !condensed);
286 break;
287 case KADM5_LAST_FAILED:
288 time_t2str(princ->last_failed, buf, buf_len, !condensed);
289 break;
290 case KADM5_FAIL_AUTH_COUNT:
291 snprintf(buf, buf_len, "%d", princ->fail_auth_count);
292 break;
293 case KADM5_POLICY:
294 if(princ->policy != NULL)
295 strlcpy(buf, princ->policy, buf_len);
296 else
297 strlcpy(buf, "none", buf_len);
298 break;
299 case KADM5_KEY_DATA:{
300 krb5_salt def_salt;
301 int i;
302 char buf2[1024];
303 krb5_get_pw_salt (context, princ->principal, &def_salt);
305 *buf = '\0';
306 for (i = 0; i < princ->n_key_data; ++i) {
307 format_keytype(&princ->key_data[i], &def_salt, buf2, sizeof(buf2));
308 if(i > 0)
309 strlcat(buf, ", ", buf_len);
310 strlcat(buf, buf2, buf_len);
312 krb5_free_salt (context, def_salt);
313 break;
315 case KADM5_TL_DATA: {
316 krb5_tl_data *tl;
318 for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next)
319 if ((unsigned)tl->tl_data_type == subfield)
320 break;
321 if (tl == NULL) {
322 strlcpy(buf, "", buf_len);
323 break;
326 switch (subfield) {
327 case KRB5_TL_PASSWORD:
328 snprintf(buf, buf_len, "\"%.*s\"",
329 (int)tl->tl_data_length,
330 (const char *)tl->tl_data_contents);
331 break;
332 case KRB5_TL_ETYPES: {
333 HDB_EncTypeList etypes;
334 size_t i, size;
335 char *str;
336 int ret;
338 ret = decode_HDB_EncTypeList(tl->tl_data_contents,
339 tl->tl_data_length,
340 &etypes, &size);
341 if (ret) {
342 snprintf(buf, buf_len, "failed to decode server etypes");
343 break;
345 buf[0] = '\0';
346 for (i = 0; i < etypes.len; i++) {
347 ret = krb5_enctype_to_string(context, etypes.val[i], &str);
348 if (ret == 0) {
349 if (i)
350 strlcat(buf, ",", buf_len);
351 strlcat(buf, str, buf_len);
352 krb5_xfree(str);
355 free_HDB_EncTypeList(&etypes);
356 break;
358 case KRB5_TL_PKINIT_ACL: {
359 HDB_Ext_PKINIT_acl acl;
360 size_t size;
361 int ret;
362 size_t i;
364 ret = decode_HDB_Ext_PKINIT_acl(tl->tl_data_contents,
365 tl->tl_data_length,
366 &acl,
367 &size);
368 if (ret) {
369 snprintf(buf, buf_len, "failed to decode ACL");
370 break;
373 buf[0] = '\0';
374 for (i = 0; i < acl.len; i++) {
375 strlcat(buf, "subject: ", buf_len);
376 strlcat(buf, acl.val[i].subject, buf_len);
377 if (acl.val[i].issuer) {
378 strlcat(buf, " issuer:", buf_len);
379 strlcat(buf, *acl.val[i].issuer, buf_len);
381 if (acl.val[i].anchor) {
382 strlcat(buf, " anchor:", buf_len);
383 strlcat(buf, *acl.val[i].anchor, buf_len);
385 if (i + 1 < acl.len)
386 strlcat(buf, ", ", buf_len);
388 free_HDB_Ext_PKINIT_acl(&acl);
389 break;
391 case KRB5_TL_KRB5_CONFIG: {
392 char *fname;
394 fname = write_krb5_config(tl, data->krb5_config_fname, data->n);
395 if (fname) {
396 strlcat(buf, fname, buf_len);
397 free(fname);
399 break;
401 case KRB5_TL_ALIASES: {
402 HDB_Ext_Aliases alias;
403 size_t size;
404 int ret;
405 size_t i;
407 ret = decode_HDB_Ext_Aliases(tl->tl_data_contents,
408 tl->tl_data_length,
409 &alias,
410 &size);
411 if (ret) {
412 snprintf(buf, buf_len, "failed to decode alias");
413 break;
415 buf[0] = '\0';
416 for (i = 0; i < alias.aliases.len; i++) {
417 char *p;
418 ret = krb5_unparse_name(context, &alias.aliases.val[i], &p);
419 if (ret)
420 break;
421 if (i > 0)
422 strlcat(buf, " ", buf_len);
423 strlcat(buf, p, buf_len);
424 free(p);
426 free_HDB_Ext_Aliases(&alias);
427 break;
429 default:
430 snprintf(buf, buf_len, "unknown type %d", subfield);
431 break;
433 break;
435 default:
436 strlcpy(buf, "<unknown>", buf_len);
437 break;
441 static void
442 print_entry_short(struct get_entry_data *data, kadm5_principal_ent_t princ)
444 char buf[1024];
445 struct field_info *f;
447 for(f = data->chead; f != NULL; f = f->next) {
448 format_field(data, princ, f->ff->fieldvalue, f->ff->subvalue, buf,
449 sizeof(buf), 1);
450 rtbl_add_column_entry_by_id(data->table, f->ff->fieldvalue, buf);
454 static void
455 print_entry_long(struct get_entry_data *data, kadm5_principal_ent_t princ)
457 char buf[1024];
458 struct field_info *f;
459 int width = 0;
461 for(f = data->chead; f != NULL; f = f->next) {
462 int w = strlen(f->header ? f->header : f->ff->def_longheader);
463 if(w > width)
464 width = w;
466 for(f = data->chead; f != NULL; f = f->next) {
467 format_field(data, princ, f->ff->fieldvalue, f->ff->subvalue, buf,
468 sizeof(buf), 0);
469 printf("%*s: %s\n", width, f->header ? f->header : f->ff->def_longheader, buf);
471 printf("\n");
474 static int
475 do_get_entry(krb5_principal principal, void *data)
477 kadm5_principal_ent_rec princ;
478 krb5_error_code ret;
479 struct get_entry_data *e = data;
481 memset(&princ, 0, sizeof(princ));
482 ret = kadm5_get_principal(kadm_handle, principal,
483 &princ,
484 e->mask | e->extra_mask);
485 if (ret == 0) {
486 (e->format)(e, &princ);
487 kadm5_free_principal_ent(kadm_handle, &princ);
490 e->n++;
491 return ret;
494 static void
495 free_columns(struct get_entry_data *data)
497 struct field_info *f, *next;
498 for(f = data->chead; f != NULL; f = next) {
499 free(f->header);
500 next = f->next;
501 free(f);
503 data->chead = NULL;
504 data->ctail = &data->chead;
507 static int
508 setup_columns(struct get_entry_data *data, const char *column_info)
510 char buf[1024], *q;
511 char *field, *header;
512 struct field_name *f;
514 while(strsep_copy(&column_info, ",", buf, sizeof(buf)) != -1) {
515 q = buf;
516 field = strsep(&q, "=");
517 header = strsep(&q, "=");
518 for(f = field_names; f->fieldname != NULL; f++) {
519 if(strcasecmp(field, f->fieldname) == 0) {
520 add_column(data, f, header);
521 break;
524 if(f->fieldname == NULL) {
525 krb5_warnx(context, "unknown field name \"%s\"", field);
526 free_columns(data);
527 return -1;
530 return 0;
533 static int
534 do_list_entry(krb5_principal principal, void *data)
536 char buf[1024];
537 krb5_error_code ret;
539 ret = krb5_unparse_name_fixed_short(context, principal, buf, sizeof(buf));
540 if (ret != 0)
541 return ret;
542 printf("%s\n", buf);
543 return 0;
546 static int
547 listit(const char *funcname, int argc, char **argv)
549 int i;
550 krb5_error_code ret, saved_ret = 0;
552 for (i = 0; i < argc; i++) {
553 ret = foreach_principal(argv[i], do_list_entry, funcname, NULL);
554 if (saved_ret == 0 && ret != 0)
555 saved_ret = ret;
557 return saved_ret != 0;
560 #define DEFAULT_COLUMNS_SHORT "principal,princ_expire_time,pw_expiration,last_pwd_change,max_life,max_rlife"
561 #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"
563 static int
564 getit(struct get_options *opt, const char *name, int argc, char **argv)
566 int i;
567 krb5_error_code ret;
568 struct get_entry_data data;
570 if(opt->long_flag == -1 && (opt->short_flag == 1 || opt->terse_flag == 1))
571 opt->long_flag = 0;
572 if(opt->short_flag == -1 && (opt->long_flag == 1 || opt->terse_flag == 1))
573 opt->short_flag = 0;
574 if(opt->terse_flag == -1 && (opt->long_flag == 1 || opt->short_flag == 1))
575 opt->terse_flag = 0;
576 if(opt->long_flag == 0 && opt->short_flag == 0 && opt->terse_flag == 0)
577 opt->short_flag = 1;
579 if (opt->terse_flag)
580 return listit(name, argc, argv);
582 data.table = NULL;
583 data.chead = NULL;
584 data.ctail = &data.chead;
585 data.mask = 0;
586 data.extra_mask = 0;
587 data.krb5_config_fname = opt->krb5_config_file_string;
588 data.n = 0;
590 if(opt->short_flag) {
591 data.table = rtbl_create();
592 rtbl_set_separator(data.table, " ");
593 data.format = print_entry_short;
594 } else
595 data.format = print_entry_long;
596 if(opt->column_info_string == NULL) {
597 if(opt->long_flag)
598 ret = setup_columns(&data, DEFAULT_COLUMNS_LONG);
599 else
600 ret = setup_columns(&data, DEFAULT_COLUMNS_SHORT);
601 } else
602 ret = setup_columns(&data, opt->column_info_string);
604 if(ret != 0) {
605 if(data.table != NULL)
606 rtbl_destroy(data.table);
607 return 0;
610 for(i = 0; i < argc; i++)
611 ret = foreach_principal(argv[i], do_get_entry, name, &data);
613 if(data.table != NULL) {
614 rtbl_format(data.table, stdout);
615 rtbl_destroy(data.table);
617 free_columns(&data);
618 return ret != 0;
622 get_entry(struct get_options *opt, int argc, char **argv)
624 return getit(opt, "get", argc, argv);
628 list_princs(struct list_options *opt, int argc, char **argv)
630 struct get_options get_opt;
632 if(sizeof(struct get_options) != sizeof(struct list_options)) {
633 krb5_warnx(context, "programmer error: sizeof(struct get_options) != sizeof(struct list_options)");
634 return 0;
636 memset(&get_opt, 0, sizeof(get_opt));
637 get_opt.long_flag = opt->long_flag;
638 get_opt.short_flag = opt->short_flag;
639 get_opt.terse_flag = opt->terse_flag;
640 get_opt.column_info_string = opt->column_info_string;
641 return getit(&get_opt, "list", argc, argv);