xref: /freebsd/crypto/heimdal/kadmin/get.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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 "kadmin_locl.h"
35 #include <parse_units.h>
36 
37 RCSID("$Id: get.c,v 1.8 1999/12/02 17:04:58 joda Exp $");
38 
39 struct get_entry_data {
40     void (*header)(void);
41     void (*format)(kadm5_principal_ent_t);
42 };
43 
44 static void
45 print_entry_terse(kadm5_principal_ent_t princ)
46 {
47     char *p;
48     krb5_unparse_name(context, princ->principal, &p);
49     printf("  %s\n", p);
50     free(p);
51 }
52 
53 static void
54 print_header_short(void)
55 {
56     printf("%-20s ", "Principal");
57 
58     printf("%-10s ", "Expires");
59 
60     printf("%-10s ", "PW-exp");
61 
62     printf("%-10s ", "PW-change");
63 
64     printf("%-9s ", "Max life");
65 
66     printf("%-9s ", "Max renew");
67 
68     printf("\n");
69 }
70 
71 static void
72 print_entry_short(kadm5_principal_ent_t princ)
73 {
74     char buf[1024];
75 
76     krb5_unparse_name_fixed_short(context, princ->principal, buf, sizeof(buf));
77     printf("%-20s ", buf);
78 
79     time_t2str(princ->princ_expire_time, buf, sizeof(buf), 0);
80     printf("%-10s ", buf);
81 
82     time_t2str(princ->pw_expiration, buf, sizeof(buf), 0);
83     printf("%-10s ", buf);
84 
85     time_t2str(princ->last_pwd_change, buf, sizeof(buf), 0);
86     printf("%-10s ", buf);
87 
88     deltat2str(princ->max_life, buf, sizeof(buf));
89     printf("%-9s ", buf);
90 
91     deltat2str(princ->max_renewable_life, buf, sizeof(buf));
92     printf("%-9s ", buf);
93 
94 #if 0
95     time_t2str(princ->mod_date, buf, sizeof(buf), 0);
96     printf("%-10s ", buf);
97 
98     krb5_unparse_name_fixed(context, princ->mod_name, buf, sizeof(buf));
99     printf("%-24s", buf);
100 #endif
101 
102     printf("\n");
103 }
104 
105 static void
106 print_entry_long(kadm5_principal_ent_t princ)
107 {
108     char buf[1024];
109     int i;
110 
111     krb5_unparse_name_fixed(context, princ->principal, buf, sizeof(buf));
112     printf("%24s: %s\n", "Principal", buf);
113     time_t2str(princ->princ_expire_time, buf, sizeof(buf), 1);
114     printf("%24s: %s\n", "Principal expires", buf);
115 
116     time_t2str(princ->pw_expiration, buf, sizeof(buf), 1);
117     printf("%24s: %s\n", "Password expires", buf);
118 
119     time_t2str(princ->last_pwd_change, buf, sizeof(buf), 1);
120     printf("%24s: %s\n", "Last password change", buf);
121 
122     deltat2str(princ->max_life, buf, sizeof(buf));
123     printf("%24s: %s\n", "Max ticket life", buf);
124 
125     deltat2str(princ->max_renewable_life, buf, sizeof(buf));
126     printf("%24s: %s\n", "Max renewable life", buf);
127     printf("%24s: %d\n", "Kvno", princ->kvno);
128     printf("%24s: %d\n", "Mkvno", princ->mkvno);
129     printf("%24s: %s\n", "Policy", princ->policy ? princ->policy : "none");
130     time_t2str(princ->last_success, buf, sizeof(buf), 1);
131     printf("%24s: %s\n", "Last successful login", buf);
132     time_t2str(princ->last_failed, buf, sizeof(buf), 1);
133     printf("%24s: %s\n", "Last failed login", buf);
134     printf("%24s: %d\n", "Failed login count", princ->fail_auth_count);
135     time_t2str(princ->mod_date, buf, sizeof(buf), 1);
136     printf("%24s: %s\n", "Last modified", buf);
137     krb5_unparse_name_fixed(context, princ->mod_name, buf, sizeof(buf));
138     printf("%24s: %s\n", "Modifier", buf);
139     attributes2str (princ->attributes, buf, sizeof(buf));
140     printf("%24s: %s\n", "Attributes", buf);
141 
142     printf("%24s: ", "Keytypes(salts)");
143 
144     for (i = 0; i < princ->n_key_data; ++i) {
145 	krb5_key_data *k = &princ->key_data[i];
146 	krb5_error_code ret;
147 	char *e_string, *s_string;
148 
149 	ret = krb5_enctype_to_string (context,
150 				      k->key_data_type[0],
151 				      &e_string);
152 	if (ret)
153 	    asprintf (&e_string, "unknown(%d)", k->key_data_type[0]);
154 
155 	ret = krb5_salttype_to_string (context,
156 				       k->key_data_type[0],
157 				       k->key_data_type[1],
158 				       &s_string);
159 	if (ret)
160 	    asprintf (&s_string, "unknown(%d)", k->key_data_type[1]);
161 
162 	printf ("%s%s(%s)", (i != 0) ? ", " : "", e_string, s_string);
163 	free (e_string);
164 	free (s_string);
165     }
166     printf("\n\n");
167 }
168 
169 static int
170 do_get_entry(krb5_principal principal, void *data)
171 {
172     kadm5_principal_ent_rec princ;
173     krb5_error_code ret;
174     struct get_entry_data *e = data;
175 
176     memset(&princ, 0, sizeof(princ));
177     ret = kadm5_get_principal(kadm_handle, principal,
178 			      &princ,
179 			      KADM5_PRINCIPAL_NORMAL_MASK|KADM5_KEY_DATA);
180     if(ret)
181 	return ret;
182     else {
183 	if(e->header) {
184 	    (*e->header)();
185 	    e->header = NULL; /* XXX only once */
186 	}
187 	(e->format)(&princ);
188 	kadm5_free_principal_ent(kadm_handle, &princ);
189     }
190     return 0;
191 }
192 
193 int
194 get_entry(int argc, char **argv)
195 {
196     int i;
197     krb5_error_code ret;
198     struct get_entry_data data;
199     struct getargs args[] = {
200 	{ "long",	'l',	arg_flag,	NULL, "long format" },
201 	{ "terse",	't',	arg_flag,	NULL, "terse format" },
202     };
203     int num_args = sizeof(args) / sizeof(args[0]);
204     int optind = 0;
205     int long_flag = 0;
206     int terse_flag = 0;
207 
208     args[0].value = &long_flag;
209     args[1].value = &terse_flag;
210     if(getarg(args, num_args, argc, argv, &optind))
211 	goto usage;
212     if(optind == argc)
213 	goto usage;
214 
215     if(long_flag) {
216 	data.format = print_entry_long;
217 	data.header = NULL;
218     } else if(terse_flag) {
219 	data.format = print_entry_terse;
220 	data.header = NULL;
221     } else {
222 	data.format = print_entry_short;
223 	data.header = print_header_short;
224     }
225 
226     argc -= optind;
227     argv += optind;
228 
229     for(i = 0; i < argc; i++)
230 	ret = foreach_principal(argv[i], do_get_entry, &data);
231     return 0;
232 usage:
233     arg_printusage (args, num_args, "get", "principal...");
234     return 0;
235 }
236 
237 int
238 list_princs(int argc, char **argv)
239 {
240     int i;
241     krb5_error_code ret;
242     struct get_entry_data data;
243 
244     data.format = print_entry_terse;
245     data.header = NULL;
246 
247     for(i = 1; i < argc; i++)
248 	ret = foreach_principal(argv[i], do_get_entry, &data);
249     return 0;
250 }
251