xref: /minix3/crypto/external/bsd/heimdal/dist/kadmin/get.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: get.c,v 1.3 2014/04/24 13:45:33 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadmin_locl.h"
37ebfedea0SLionel Sambuc #include "kadmin-commands.h"
38ebfedea0SLionel Sambuc #include <krb5/parse_units.h>
39ebfedea0SLionel Sambuc #include <krb5/rtbl.h>
40ebfedea0SLionel Sambuc 
41ebfedea0SLionel Sambuc static struct field_name {
42ebfedea0SLionel Sambuc     const char *fieldname;
43ebfedea0SLionel Sambuc     unsigned int fieldvalue;
44ebfedea0SLionel Sambuc     unsigned int subvalue;
45ebfedea0SLionel Sambuc     uint32_t extra_mask;
46ebfedea0SLionel Sambuc     const char *default_header;
47ebfedea0SLionel Sambuc     const char *def_longheader;
48ebfedea0SLionel Sambuc     unsigned int flags;
49ebfedea0SLionel Sambuc } field_names[] = {
50ebfedea0SLionel Sambuc     { "principal", KADM5_PRINCIPAL, 0, 0, "Principal", "Principal", 0 },
51ebfedea0SLionel Sambuc     { "princ_expire_time", KADM5_PRINC_EXPIRE_TIME, 0, 0, "Expiration", "Principal expires", 0 },
52ebfedea0SLionel Sambuc     { "pw_expiration", KADM5_PW_EXPIRATION, 0, 0, "PW-exp", "Password expires", 0 },
53ebfedea0SLionel Sambuc     { "last_pwd_change", KADM5_LAST_PWD_CHANGE, 0, 0, "PW-change", "Last password change", 0 },
54ebfedea0SLionel Sambuc     { "max_life", KADM5_MAX_LIFE, 0, 0, "Max life", "Max ticket life", 0 },
55ebfedea0SLionel Sambuc     { "max_rlife", KADM5_MAX_RLIFE, 0, 0, "Max renew", "Max renewable life", 0 },
56ebfedea0SLionel Sambuc     { "mod_time", KADM5_MOD_TIME, 0, 0, "Mod time", "Last modified", 0 },
57ebfedea0SLionel Sambuc     { "mod_name", KADM5_MOD_NAME, 0, 0, "Modifier", "Modifier", 0 },
58ebfedea0SLionel Sambuc     { "attributes", KADM5_ATTRIBUTES, 0, 0, "Attributes", "Attributes", 0 },
59ebfedea0SLionel Sambuc     { "kvno", KADM5_KVNO, 0, 0, "Kvno", "Kvno", RTBL_ALIGN_RIGHT  },
60ebfedea0SLionel Sambuc     { "mkvno", KADM5_MKVNO, 0, 0, "Mkvno", "Mkvno", RTBL_ALIGN_RIGHT },
61ebfedea0SLionel Sambuc     { "last_success", KADM5_LAST_SUCCESS, 0, 0, "Last login", "Last successful login", 0 },
62ebfedea0SLionel Sambuc     { "last_failed", KADM5_LAST_FAILED, 0, 0, "Last fail", "Last failed login", 0 },
63ebfedea0SLionel Sambuc     { "fail_auth_count", KADM5_FAIL_AUTH_COUNT, 0, 0, "Fail count", "Failed login count", RTBL_ALIGN_RIGHT },
64ebfedea0SLionel Sambuc     { "policy", KADM5_POLICY, 0, 0, "Policy", "Policy", 0 },
65ebfedea0SLionel Sambuc     { "keytypes", KADM5_KEY_DATA, 0, KADM5_PRINCIPAL, "Keytypes", "Keytypes", 0 },
66ebfedea0SLionel Sambuc     { "password", KADM5_TL_DATA, KRB5_TL_PASSWORD, KADM5_KEY_DATA, "Password", "Password", 0 },
67ebfedea0SLionel Sambuc     { "pkinit-acl", KADM5_TL_DATA, KRB5_TL_PKINIT_ACL, 0, "PK-INIT ACL", "PK-INIT ACL", 0 },
68ebfedea0SLionel Sambuc     { "aliases", KADM5_TL_DATA, KRB5_TL_ALIASES, 0, "Aliases", "Aliases", 0 },
69ebfedea0SLionel Sambuc     { .fieldname = NULL }
70ebfedea0SLionel Sambuc };
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc struct field_info {
73ebfedea0SLionel Sambuc     struct field_name *ff;
74ebfedea0SLionel Sambuc     char *header;
75ebfedea0SLionel Sambuc     struct field_info *next;
76ebfedea0SLionel Sambuc };
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc struct get_entry_data {
79ebfedea0SLionel Sambuc     void (*format)(struct get_entry_data*, kadm5_principal_ent_t);
80ebfedea0SLionel Sambuc     rtbl_t table;
81ebfedea0SLionel Sambuc     uint32_t mask;
82ebfedea0SLionel Sambuc     uint32_t extra_mask;
83ebfedea0SLionel Sambuc     struct field_info *chead, **ctail;
84ebfedea0SLionel Sambuc };
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc static int
add_column(struct get_entry_data * data,struct field_name * ff,const char * header)87ebfedea0SLionel Sambuc add_column(struct get_entry_data *data, struct field_name *ff, const char *header)
88ebfedea0SLionel Sambuc {
89ebfedea0SLionel Sambuc     struct field_info *f = malloc(sizeof(*f));
90ebfedea0SLionel Sambuc     if (f == NULL)
91ebfedea0SLionel Sambuc 	return ENOMEM;
92ebfedea0SLionel Sambuc     f->ff = ff;
93ebfedea0SLionel Sambuc     if(header)
94ebfedea0SLionel Sambuc 	f->header = strdup(header);
95ebfedea0SLionel Sambuc     else
96ebfedea0SLionel Sambuc 	f->header = NULL;
97ebfedea0SLionel Sambuc     f->next = NULL;
98ebfedea0SLionel Sambuc     *data->ctail = f;
99ebfedea0SLionel Sambuc     data->ctail = &f->next;
100ebfedea0SLionel Sambuc     data->mask |= ff->fieldvalue;
101ebfedea0SLionel Sambuc     data->extra_mask |= ff->extra_mask;
102ebfedea0SLionel Sambuc     if(data->table != NULL)
103ebfedea0SLionel Sambuc 	rtbl_add_column_by_id(data->table, ff->fieldvalue,
104ebfedea0SLionel Sambuc 			      header ? header : ff->default_header, ff->flags);
105ebfedea0SLionel Sambuc     return 0;
106ebfedea0SLionel Sambuc }
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc /*
109ebfedea0SLionel Sambuc  * return 0 iff `salt' actually is the same as the current salt in `k'
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc static int
cmp_salt(const krb5_salt * salt,const krb5_key_data * k)113ebfedea0SLionel Sambuc cmp_salt (const krb5_salt *salt, const krb5_key_data *k)
114ebfedea0SLionel Sambuc {
115ebfedea0SLionel Sambuc     if (salt->salttype != (size_t)k->key_data_type[1])
116ebfedea0SLionel Sambuc 	return 1;
117ebfedea0SLionel Sambuc     if (salt->saltvalue.length != (size_t)k->key_data_length[1])
118ebfedea0SLionel Sambuc 	return 1;
119ebfedea0SLionel Sambuc     return memcmp (salt->saltvalue.data, k->key_data_contents[1],
120ebfedea0SLionel Sambuc 		   salt->saltvalue.length);
121ebfedea0SLionel Sambuc }
122ebfedea0SLionel Sambuc 
123ebfedea0SLionel Sambuc static void
format_keytype(krb5_key_data * k,krb5_salt * def_salt,char * buf,size_t buf_len)124ebfedea0SLionel Sambuc format_keytype(krb5_key_data *k, krb5_salt *def_salt, char *buf, size_t buf_len)
125ebfedea0SLionel Sambuc {
126ebfedea0SLionel Sambuc     krb5_error_code ret;
127ebfedea0SLionel Sambuc     char *s;
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc     ret = krb5_enctype_to_string (context,
130ebfedea0SLionel Sambuc 				  k->key_data_type[0],
131ebfedea0SLionel Sambuc 				  &s);
132ebfedea0SLionel Sambuc     if (ret)
133ebfedea0SLionel Sambuc 	asprintf (&s, "unknown(%d)", k->key_data_type[0]);
134ebfedea0SLionel Sambuc     strlcpy(buf, s, buf_len);
135ebfedea0SLionel Sambuc     free(s);
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     strlcat(buf, "(", buf_len);
138ebfedea0SLionel Sambuc 
139ebfedea0SLionel Sambuc     ret = krb5_salttype_to_string (context,
140ebfedea0SLionel Sambuc 				   k->key_data_type[0],
141ebfedea0SLionel Sambuc 				   k->key_data_type[1],
142ebfedea0SLionel Sambuc 				   &s);
143ebfedea0SLionel Sambuc     if (ret)
144ebfedea0SLionel Sambuc 	asprintf (&s, "unknown(%d)", k->key_data_type[1]);
145ebfedea0SLionel Sambuc     strlcat(buf, s, buf_len);
146ebfedea0SLionel Sambuc     free(s);
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc     if (cmp_salt(def_salt, k) == 0)
149ebfedea0SLionel Sambuc 	s = strdup("");
150ebfedea0SLionel Sambuc     else if(k->key_data_length[1] == 0)
151ebfedea0SLionel Sambuc 	s = strdup("()");
152ebfedea0SLionel Sambuc     else
153ebfedea0SLionel Sambuc 	asprintf (&s, "(%.*s)", k->key_data_length[1],
154ebfedea0SLionel Sambuc 		  (char *)k->key_data_contents[1]);
155ebfedea0SLionel Sambuc     strlcat(buf, s, buf_len);
156ebfedea0SLionel Sambuc     free(s);
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc     strlcat(buf, ")", buf_len);
159ebfedea0SLionel Sambuc }
160ebfedea0SLionel Sambuc 
161ebfedea0SLionel Sambuc static void
format_field(kadm5_principal_ent_t princ,unsigned int field,unsigned int subfield,char * buf,size_t buf_len,int condensed)162ebfedea0SLionel Sambuc format_field(kadm5_principal_ent_t princ, unsigned int field,
163ebfedea0SLionel Sambuc 	     unsigned int subfield, char *buf, size_t buf_len, int condensed)
164ebfedea0SLionel Sambuc {
165ebfedea0SLionel Sambuc     switch(field) {
166ebfedea0SLionel Sambuc     case KADM5_PRINCIPAL:
167ebfedea0SLionel Sambuc 	if(condensed)
168ebfedea0SLionel Sambuc 	    krb5_unparse_name_fixed_short(context, princ->principal, buf, buf_len);
169ebfedea0SLionel Sambuc 	else
170ebfedea0SLionel Sambuc 	    krb5_unparse_name_fixed(context, princ->principal, buf, buf_len);
171ebfedea0SLionel Sambuc 	break;
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc     case KADM5_PRINC_EXPIRE_TIME:
174ebfedea0SLionel Sambuc 	time_t2str(princ->princ_expire_time, buf, buf_len, !condensed);
175ebfedea0SLionel Sambuc 	break;
176ebfedea0SLionel Sambuc 
177ebfedea0SLionel Sambuc     case KADM5_PW_EXPIRATION:
178ebfedea0SLionel Sambuc 	time_t2str(princ->pw_expiration, buf, buf_len, !condensed);
179ebfedea0SLionel Sambuc 	break;
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc     case KADM5_LAST_PWD_CHANGE:
182ebfedea0SLionel Sambuc 	time_t2str(princ->last_pwd_change, buf, buf_len, !condensed);
183ebfedea0SLionel Sambuc 	break;
184ebfedea0SLionel Sambuc 
185ebfedea0SLionel Sambuc     case KADM5_MAX_LIFE:
186ebfedea0SLionel Sambuc 	deltat2str(princ->max_life, buf, buf_len);
187ebfedea0SLionel Sambuc 	break;
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc     case KADM5_MAX_RLIFE:
190ebfedea0SLionel Sambuc 	deltat2str(princ->max_renewable_life, buf, buf_len);
191ebfedea0SLionel Sambuc 	break;
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     case KADM5_MOD_TIME:
194ebfedea0SLionel Sambuc 	time_t2str(princ->mod_date, buf, buf_len, !condensed);
195ebfedea0SLionel Sambuc 	break;
196ebfedea0SLionel Sambuc 
197ebfedea0SLionel Sambuc     case KADM5_MOD_NAME:
198ebfedea0SLionel Sambuc 	if (princ->mod_name == NULL)
199ebfedea0SLionel Sambuc 	    strlcpy(buf, "unknown", buf_len);
200ebfedea0SLionel Sambuc 	else if(condensed)
201ebfedea0SLionel Sambuc 	    krb5_unparse_name_fixed_short(context, princ->mod_name, buf, buf_len);
202ebfedea0SLionel Sambuc 	else
203ebfedea0SLionel Sambuc 	    krb5_unparse_name_fixed(context, princ->mod_name, buf, buf_len);
204ebfedea0SLionel Sambuc 	break;
205ebfedea0SLionel Sambuc     case KADM5_ATTRIBUTES:
206ebfedea0SLionel Sambuc 	attributes2str (princ->attributes, buf, buf_len);
207ebfedea0SLionel Sambuc 	break;
208ebfedea0SLionel Sambuc     case KADM5_KVNO:
209ebfedea0SLionel Sambuc 	snprintf(buf, buf_len, "%d", princ->kvno);
210ebfedea0SLionel Sambuc 	break;
211ebfedea0SLionel Sambuc     case KADM5_MKVNO:
212ebfedea0SLionel Sambuc 	/* XXX libkadm5srv decrypts the keys, so mkvno is always 0. */
213ebfedea0SLionel Sambuc 	strlcpy(buf, "unknown", buf_len);
214ebfedea0SLionel Sambuc 	break;
215ebfedea0SLionel Sambuc     case KADM5_LAST_SUCCESS:
216ebfedea0SLionel Sambuc 	time_t2str(princ->last_success, buf, buf_len, !condensed);
217ebfedea0SLionel Sambuc 	break;
218ebfedea0SLionel Sambuc     case KADM5_LAST_FAILED:
219ebfedea0SLionel Sambuc 	time_t2str(princ->last_failed, buf, buf_len, !condensed);
220ebfedea0SLionel Sambuc 	break;
221ebfedea0SLionel Sambuc     case KADM5_FAIL_AUTH_COUNT:
222ebfedea0SLionel Sambuc 	snprintf(buf, buf_len, "%d", princ->fail_auth_count);
223ebfedea0SLionel Sambuc 	break;
224ebfedea0SLionel Sambuc     case KADM5_POLICY:
225ebfedea0SLionel Sambuc 	if(princ->policy != NULL)
226ebfedea0SLionel Sambuc 	    strlcpy(buf, princ->policy, buf_len);
227ebfedea0SLionel Sambuc 	else
228ebfedea0SLionel Sambuc 	    strlcpy(buf, "none", buf_len);
229ebfedea0SLionel Sambuc 	break;
230ebfedea0SLionel Sambuc     case KADM5_KEY_DATA:{
231ebfedea0SLionel Sambuc 	krb5_salt def_salt;
232ebfedea0SLionel Sambuc 	int i;
233ebfedea0SLionel Sambuc 	char buf2[1024];
234ebfedea0SLionel Sambuc 	krb5_get_pw_salt (context, princ->principal, &def_salt);
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc 	*buf = '\0';
237ebfedea0SLionel Sambuc 	for (i = 0; i < princ->n_key_data; ++i) {
238ebfedea0SLionel Sambuc 	    format_keytype(&princ->key_data[i], &def_salt, buf2, sizeof(buf2));
239ebfedea0SLionel Sambuc 	    if(i > 0)
240ebfedea0SLionel Sambuc 		strlcat(buf, ", ", buf_len);
241ebfedea0SLionel Sambuc 	    strlcat(buf, buf2, buf_len);
242ebfedea0SLionel Sambuc 	}
243ebfedea0SLionel Sambuc 	krb5_free_salt (context, def_salt);
244ebfedea0SLionel Sambuc 	break;
245ebfedea0SLionel Sambuc     }
246ebfedea0SLionel Sambuc     case KADM5_TL_DATA: {
247ebfedea0SLionel Sambuc 	krb5_tl_data *tl;
248ebfedea0SLionel Sambuc 
249ebfedea0SLionel Sambuc 	for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next)
250ebfedea0SLionel Sambuc 	    if ((unsigned)tl->tl_data_type == subfield)
251ebfedea0SLionel Sambuc 		break;
252ebfedea0SLionel Sambuc 	if (tl == NULL) {
253ebfedea0SLionel Sambuc 	    strlcpy(buf, "", buf_len);
254ebfedea0SLionel Sambuc 	    break;
255ebfedea0SLionel Sambuc 	}
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc 	switch (subfield) {
258ebfedea0SLionel Sambuc 	case KRB5_TL_PASSWORD:
259ebfedea0SLionel Sambuc 	    snprintf(buf, buf_len, "\"%.*s\"",
260ebfedea0SLionel Sambuc 		     (int)tl->tl_data_length,
261ebfedea0SLionel Sambuc 		     (const char *)tl->tl_data_contents);
262ebfedea0SLionel Sambuc 	    break;
263ebfedea0SLionel Sambuc 	case KRB5_TL_PKINIT_ACL: {
264ebfedea0SLionel Sambuc 	    HDB_Ext_PKINIT_acl acl;
265ebfedea0SLionel Sambuc 	    size_t size;
266ebfedea0SLionel Sambuc 	    int ret;
267ebfedea0SLionel Sambuc 	    size_t i;
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc 	    ret = decode_HDB_Ext_PKINIT_acl(tl->tl_data_contents,
270ebfedea0SLionel Sambuc 					    tl->tl_data_length,
271ebfedea0SLionel Sambuc 					    &acl,
272ebfedea0SLionel Sambuc 					    &size);
273ebfedea0SLionel Sambuc 	    if (ret) {
274ebfedea0SLionel Sambuc 		snprintf(buf, buf_len, "failed to decode ACL");
275ebfedea0SLionel Sambuc 		break;
276ebfedea0SLionel Sambuc 	    }
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc 	    buf[0] = '\0';
279ebfedea0SLionel Sambuc 	    for (i = 0; i < acl.len; i++) {
280ebfedea0SLionel Sambuc 		strlcat(buf, "subject: ", buf_len);
281ebfedea0SLionel Sambuc 		strlcat(buf, acl.val[i].subject, buf_len);
282ebfedea0SLionel Sambuc 		if (acl.val[i].issuer) {
283ebfedea0SLionel Sambuc 		    strlcat(buf, " issuer:", buf_len);
284ebfedea0SLionel Sambuc 		    strlcat(buf, *acl.val[i].issuer, buf_len);
285ebfedea0SLionel Sambuc 		}
286ebfedea0SLionel Sambuc 		if (acl.val[i].anchor) {
287ebfedea0SLionel Sambuc 		    strlcat(buf, " anchor:", buf_len);
288ebfedea0SLionel Sambuc 		    strlcat(buf, *acl.val[i].anchor, buf_len);
289ebfedea0SLionel Sambuc 		}
290ebfedea0SLionel Sambuc 		if (i + 1 < acl.len)
291ebfedea0SLionel Sambuc 		    strlcat(buf, ", ", buf_len);
292ebfedea0SLionel Sambuc 	    }
293ebfedea0SLionel Sambuc 	    free_HDB_Ext_PKINIT_acl(&acl);
294ebfedea0SLionel Sambuc 	    break;
295ebfedea0SLionel Sambuc 	}
296ebfedea0SLionel Sambuc 	case KRB5_TL_ALIASES: {
297ebfedea0SLionel Sambuc 	    HDB_Ext_Aliases alias;
298ebfedea0SLionel Sambuc 	    size_t size;
299ebfedea0SLionel Sambuc 	    int ret;
300ebfedea0SLionel Sambuc 	    size_t i;
301ebfedea0SLionel Sambuc 
302ebfedea0SLionel Sambuc 	    ret = decode_HDB_Ext_Aliases(tl->tl_data_contents,
303ebfedea0SLionel Sambuc 					 tl->tl_data_length,
304ebfedea0SLionel Sambuc 					 &alias,
305ebfedea0SLionel Sambuc 					 &size);
306ebfedea0SLionel Sambuc 	    if (ret) {
307ebfedea0SLionel Sambuc 		snprintf(buf, buf_len, "failed to decode alias");
308ebfedea0SLionel Sambuc 		break;
309ebfedea0SLionel Sambuc 	    }
310ebfedea0SLionel Sambuc 	    buf[0] = '\0';
311ebfedea0SLionel Sambuc 	    for (i = 0; i < alias.aliases.len; i++) {
312ebfedea0SLionel Sambuc 		char *p;
313ebfedea0SLionel Sambuc 		ret = krb5_unparse_name(context, &alias.aliases.val[i], &p);
314ebfedea0SLionel Sambuc 		if (ret)
315ebfedea0SLionel Sambuc 		    break;
316ebfedea0SLionel Sambuc 		if (i > 0)
317ebfedea0SLionel Sambuc 		    strlcat(buf, " ", buf_len);
318ebfedea0SLionel Sambuc 		strlcat(buf, p, buf_len);
319ebfedea0SLionel Sambuc 		free(p);
320ebfedea0SLionel Sambuc 	    }
321ebfedea0SLionel Sambuc 	    free_HDB_Ext_Aliases(&alias);
322ebfedea0SLionel Sambuc 	    break;
323ebfedea0SLionel Sambuc 	}
324ebfedea0SLionel Sambuc 	default:
325ebfedea0SLionel Sambuc 	    snprintf(buf, buf_len, "unknown type %d", subfield);
326ebfedea0SLionel Sambuc 	    break;
327ebfedea0SLionel Sambuc 	}
328ebfedea0SLionel Sambuc 	break;
329ebfedea0SLionel Sambuc     }
330ebfedea0SLionel Sambuc     default:
331ebfedea0SLionel Sambuc 	strlcpy(buf, "<unknown>", buf_len);
332ebfedea0SLionel Sambuc 	break;
333ebfedea0SLionel Sambuc     }
334ebfedea0SLionel Sambuc }
335ebfedea0SLionel Sambuc 
336ebfedea0SLionel Sambuc static void
print_entry_short(struct get_entry_data * data,kadm5_principal_ent_t princ)337ebfedea0SLionel Sambuc print_entry_short(struct get_entry_data *data, kadm5_principal_ent_t princ)
338ebfedea0SLionel Sambuc {
339ebfedea0SLionel Sambuc     char buf[1024];
340ebfedea0SLionel Sambuc     struct field_info *f;
341ebfedea0SLionel Sambuc 
342ebfedea0SLionel Sambuc     for(f = data->chead; f != NULL; f = f->next) {
343ebfedea0SLionel Sambuc 	format_field(princ, f->ff->fieldvalue, f->ff->subvalue, buf, sizeof(buf), 1);
344ebfedea0SLionel Sambuc 	rtbl_add_column_entry_by_id(data->table, f->ff->fieldvalue, buf);
345ebfedea0SLionel Sambuc     }
346ebfedea0SLionel Sambuc }
347ebfedea0SLionel Sambuc 
348ebfedea0SLionel Sambuc static void
print_entry_long(struct get_entry_data * data,kadm5_principal_ent_t princ)349ebfedea0SLionel Sambuc print_entry_long(struct get_entry_data *data, kadm5_principal_ent_t princ)
350ebfedea0SLionel Sambuc {
351ebfedea0SLionel Sambuc     char buf[1024];
352ebfedea0SLionel Sambuc     struct field_info *f;
353ebfedea0SLionel Sambuc     int width = 0;
354ebfedea0SLionel Sambuc 
355ebfedea0SLionel Sambuc     for(f = data->chead; f != NULL; f = f->next) {
356ebfedea0SLionel Sambuc 	int w = strlen(f->header ? f->header : f->ff->def_longheader);
357ebfedea0SLionel Sambuc 	if(w > width)
358ebfedea0SLionel Sambuc 	    width = w;
359ebfedea0SLionel Sambuc     }
360ebfedea0SLionel Sambuc     for(f = data->chead; f != NULL; f = f->next) {
361ebfedea0SLionel Sambuc 	format_field(princ, f->ff->fieldvalue, f->ff->subvalue, buf, sizeof(buf), 0);
362ebfedea0SLionel Sambuc 	printf("%*s: %s\n", width, f->header ? f->header : f->ff->def_longheader, buf);
363ebfedea0SLionel Sambuc     }
364ebfedea0SLionel Sambuc     printf("\n");
365ebfedea0SLionel Sambuc }
366ebfedea0SLionel Sambuc 
367ebfedea0SLionel Sambuc static int
do_get_entry(krb5_principal principal,void * data)368ebfedea0SLionel Sambuc do_get_entry(krb5_principal principal, void *data)
369ebfedea0SLionel Sambuc {
370ebfedea0SLionel Sambuc     kadm5_principal_ent_rec princ;
371ebfedea0SLionel Sambuc     krb5_error_code ret;
372ebfedea0SLionel Sambuc     struct get_entry_data *e = data;
373ebfedea0SLionel Sambuc 
374ebfedea0SLionel Sambuc     memset(&princ, 0, sizeof(princ));
375ebfedea0SLionel Sambuc     ret = kadm5_get_principal(kadm_handle, principal,
376ebfedea0SLionel Sambuc 			      &princ,
377ebfedea0SLionel Sambuc 			      e->mask | e->extra_mask);
378ebfedea0SLionel Sambuc     if(ret)
379ebfedea0SLionel Sambuc 	return ret;
380ebfedea0SLionel Sambuc     else {
381ebfedea0SLionel Sambuc 	(e->format)(e, &princ);
382ebfedea0SLionel Sambuc 	kadm5_free_principal_ent(kadm_handle, &princ);
383ebfedea0SLionel Sambuc     }
384ebfedea0SLionel Sambuc     return 0;
385ebfedea0SLionel Sambuc }
386ebfedea0SLionel Sambuc 
387ebfedea0SLionel Sambuc static void
free_columns(struct get_entry_data * data)388ebfedea0SLionel Sambuc free_columns(struct get_entry_data *data)
389ebfedea0SLionel Sambuc {
390ebfedea0SLionel Sambuc     struct field_info *f, *next;
391ebfedea0SLionel Sambuc     for(f = data->chead; f != NULL; f = next) {
392ebfedea0SLionel Sambuc 	free(f->header);
393ebfedea0SLionel Sambuc 	next = f->next;
394ebfedea0SLionel Sambuc 	free(f);
395ebfedea0SLionel Sambuc     }
396ebfedea0SLionel Sambuc     data->chead = NULL;
397ebfedea0SLionel Sambuc     data->ctail = &data->chead;
398ebfedea0SLionel Sambuc }
399ebfedea0SLionel Sambuc 
400ebfedea0SLionel Sambuc static int
setup_columns(struct get_entry_data * data,const char * column_info)401ebfedea0SLionel Sambuc setup_columns(struct get_entry_data *data, const char *column_info)
402ebfedea0SLionel Sambuc {
403ebfedea0SLionel Sambuc     char buf[1024], *q;
404ebfedea0SLionel Sambuc     char *field, *header;
405ebfedea0SLionel Sambuc     struct field_name *f;
406ebfedea0SLionel Sambuc 
407ebfedea0SLionel Sambuc     while(strsep_copy(&column_info, ",", buf, sizeof(buf)) != -1) {
408ebfedea0SLionel Sambuc 	q = buf;
409ebfedea0SLionel Sambuc 	field = strsep(&q, "=");
410ebfedea0SLionel Sambuc 	header = strsep(&q, "=");
411ebfedea0SLionel Sambuc 	for(f = field_names; f->fieldname != NULL; f++) {
412ebfedea0SLionel Sambuc 	    if(strcasecmp(field, f->fieldname) == 0) {
413ebfedea0SLionel Sambuc 		add_column(data, f, header);
414ebfedea0SLionel Sambuc 		break;
415ebfedea0SLionel Sambuc 	    }
416ebfedea0SLionel Sambuc 	}
417ebfedea0SLionel Sambuc 	if(f->fieldname == NULL) {
418ebfedea0SLionel Sambuc 	    krb5_warnx(context, "unknown field name \"%s\"", field);
419ebfedea0SLionel Sambuc 	    free_columns(data);
420ebfedea0SLionel Sambuc 	    return -1;
421ebfedea0SLionel Sambuc 	}
422ebfedea0SLionel Sambuc     }
423ebfedea0SLionel Sambuc     return 0;
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc 
426ebfedea0SLionel Sambuc static int
do_list_entry(krb5_principal principal,void * data)427ebfedea0SLionel Sambuc do_list_entry(krb5_principal principal, void *data)
428ebfedea0SLionel Sambuc {
429ebfedea0SLionel Sambuc     char buf[1024];
430ebfedea0SLionel Sambuc     krb5_error_code ret;
431ebfedea0SLionel Sambuc 
432ebfedea0SLionel Sambuc     ret = krb5_unparse_name_fixed_short(context, principal, buf, sizeof(buf));
433ebfedea0SLionel Sambuc     if (ret != 0)
434ebfedea0SLionel Sambuc         return ret;
435ebfedea0SLionel Sambuc     printf("%s\n", buf);
436ebfedea0SLionel Sambuc     return 0;
437ebfedea0SLionel Sambuc }
438ebfedea0SLionel Sambuc 
439ebfedea0SLionel Sambuc static int
listit(const char * funcname,int argc,char ** argv)440ebfedea0SLionel Sambuc listit(const char *funcname, int argc, char **argv)
441ebfedea0SLionel Sambuc {
442ebfedea0SLionel Sambuc     int i;
443ebfedea0SLionel Sambuc     krb5_error_code ret, saved_ret = 0;
444ebfedea0SLionel Sambuc 
445ebfedea0SLionel Sambuc     for (i = 0; i < argc; i++) {
446ebfedea0SLionel Sambuc 	ret = foreach_principal(argv[i], do_list_entry, funcname, NULL);
447ebfedea0SLionel Sambuc         if (saved_ret == 0 && ret != 0)
448ebfedea0SLionel Sambuc             saved_ret = ret;
449ebfedea0SLionel Sambuc     }
450ebfedea0SLionel Sambuc     return saved_ret != 0;
451ebfedea0SLionel Sambuc }
452ebfedea0SLionel Sambuc 
453ebfedea0SLionel Sambuc #define DEFAULT_COLUMNS_SHORT "principal,princ_expire_time,pw_expiration,last_pwd_change,max_life,max_rlife"
454ebfedea0SLionel Sambuc #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,keytypes,pkinit-acl,aliases"
455ebfedea0SLionel Sambuc 
456ebfedea0SLionel Sambuc static int
getit(struct get_options * opt,const char * name,int argc,char ** argv)457ebfedea0SLionel Sambuc getit(struct get_options *opt, const char *name, int argc, char **argv)
458ebfedea0SLionel Sambuc {
459ebfedea0SLionel Sambuc     int i;
460ebfedea0SLionel Sambuc     krb5_error_code ret;
461ebfedea0SLionel Sambuc     struct get_entry_data data;
462ebfedea0SLionel Sambuc 
463ebfedea0SLionel Sambuc     if(opt->long_flag == -1 && (opt->short_flag == 1 || opt->terse_flag == 1))
464ebfedea0SLionel Sambuc 	opt->long_flag = 0;
465ebfedea0SLionel Sambuc     if(opt->short_flag == -1 && (opt->long_flag == 1 || opt->terse_flag == 1))
466ebfedea0SLionel Sambuc 	opt->short_flag = 0;
467ebfedea0SLionel Sambuc     if(opt->terse_flag == -1 && (opt->long_flag == 1 || opt->short_flag == 1))
468ebfedea0SLionel Sambuc 	opt->terse_flag = 0;
469ebfedea0SLionel Sambuc     if(opt->long_flag == 0 && opt->short_flag == 0 && opt->terse_flag == 0)
470ebfedea0SLionel Sambuc 	opt->short_flag = 1;
471ebfedea0SLionel Sambuc 
472ebfedea0SLionel Sambuc     if (opt->terse_flag)
473ebfedea0SLionel Sambuc         return listit(name, argc, argv);
474ebfedea0SLionel Sambuc 
475ebfedea0SLionel Sambuc     data.table = NULL;
476ebfedea0SLionel Sambuc     data.chead = NULL;
477ebfedea0SLionel Sambuc     data.ctail = &data.chead;
478ebfedea0SLionel Sambuc     data.mask = 0;
479ebfedea0SLionel Sambuc     data.extra_mask = 0;
480ebfedea0SLionel Sambuc 
481ebfedea0SLionel Sambuc     if(opt->short_flag) {
482ebfedea0SLionel Sambuc 	data.table = rtbl_create();
483ebfedea0SLionel Sambuc 	rtbl_set_separator(data.table, "  ");
484ebfedea0SLionel Sambuc 	data.format = print_entry_short;
485ebfedea0SLionel Sambuc     } else
486ebfedea0SLionel Sambuc 	data.format = print_entry_long;
487ebfedea0SLionel Sambuc     if(opt->column_info_string == NULL) {
488ebfedea0SLionel Sambuc 	if(opt->long_flag)
489ebfedea0SLionel Sambuc 	    ret = setup_columns(&data, DEFAULT_COLUMNS_LONG);
490ebfedea0SLionel Sambuc 	else
491ebfedea0SLionel Sambuc 	    ret = setup_columns(&data, DEFAULT_COLUMNS_SHORT);
492ebfedea0SLionel Sambuc     } else
493ebfedea0SLionel Sambuc 	ret = setup_columns(&data, opt->column_info_string);
494ebfedea0SLionel Sambuc 
495ebfedea0SLionel Sambuc     if(ret != 0) {
496ebfedea0SLionel Sambuc 	if(data.table != NULL)
497ebfedea0SLionel Sambuc 	    rtbl_destroy(data.table);
498ebfedea0SLionel Sambuc 	return 0;
499ebfedea0SLionel Sambuc     }
500ebfedea0SLionel Sambuc 
501ebfedea0SLionel Sambuc     for(i = 0; i < argc; i++)
502ebfedea0SLionel Sambuc 	ret = foreach_principal(argv[i], do_get_entry, name, &data);
503ebfedea0SLionel Sambuc 
504ebfedea0SLionel Sambuc     if(data.table != NULL) {
505ebfedea0SLionel Sambuc 	rtbl_format(data.table, stdout);
506ebfedea0SLionel Sambuc 	rtbl_destroy(data.table);
507ebfedea0SLionel Sambuc     }
508ebfedea0SLionel Sambuc     free_columns(&data);
509ebfedea0SLionel Sambuc     return ret != 0;
510ebfedea0SLionel Sambuc }
511ebfedea0SLionel Sambuc 
512ebfedea0SLionel Sambuc int
get_entry(struct get_options * opt,int argc,char ** argv)513ebfedea0SLionel Sambuc get_entry(struct get_options *opt, int argc, char **argv)
514ebfedea0SLionel Sambuc {
515ebfedea0SLionel Sambuc     return getit(opt, "get", argc, argv);
516ebfedea0SLionel Sambuc }
517ebfedea0SLionel Sambuc 
518ebfedea0SLionel Sambuc int
list_princs(struct list_options * opt,int argc,char ** argv)519ebfedea0SLionel Sambuc list_princs(struct list_options *opt, int argc, char **argv)
520ebfedea0SLionel Sambuc {
521ebfedea0SLionel Sambuc     if(sizeof(struct get_options) != sizeof(struct list_options)) {
522ebfedea0SLionel Sambuc 	krb5_warnx(context, "programmer error: sizeof(struct get_options) != sizeof(struct list_options)");
523ebfedea0SLionel Sambuc 	return 0;
524ebfedea0SLionel Sambuc     }
525ebfedea0SLionel Sambuc     return getit((struct get_options*)opt, "list", argc, argv);
526ebfedea0SLionel Sambuc }
527