(base64_encode): check return value from malloc
[heimdal.git] / kuser / klist.c
blob09361a5db96c19d560136ba31ebcbfa3b38cb2ea
1 /*
2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include "kuser_locl.h"
41 RCSID("$Id$");
43 static char*
44 printable_time(time_t t)
46 static char s[128];
47 strcpy(s, ctime(&t)+ 4);
48 s[15] = 0;
49 return s;
52 static char*
53 printable_time_long(time_t t)
55 static char s[128];
56 strcpy(s, ctime(&t)+ 4);
57 s[20] = 0;
58 return s;
61 static void
62 print_cred(krb5_context context, krb5_creds *cred)
64 char *str;
65 krb5_error_code ret;
66 int32_t sec;
68 krb5_timeofday (context, &sec);
70 if(cred->times.starttime)
71 printf ("%s ", printable_time(cred->times.starttime));
72 else
73 printf ("%s ", printable_time(cred->times.authtime));
75 if(cred->times.endtime > sec)
76 printf ("%s ", printable_time(cred->times.endtime));
77 else
78 printf ("%-15s ", ">>>Expired<<<");
79 ret = krb5_unparse_name (context, cred->server, &str);
80 if (ret)
81 krb5_err(context, 1, ret, "krb5_unparse_name");
82 printf ("%s\n", str);
83 free (str);
86 static void
87 print_cred_verbose(krb5_context context, krb5_creds *cred)
89 int j;
90 char *str;
91 krb5_error_code ret;
92 int first_flag;
93 int32_t sec;
95 krb5_timeofday (context, &sec);
97 ret = krb5_unparse_name(context, cred->server, &str);
98 if(ret)
99 exit(1);
100 printf("Server: %s\n", str);
101 free (str);
103 Ticket t;
104 size_t len;
105 char *s;
106 decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len);
107 krb5_etype_to_string(context, t.enc_part.etype, &s);
108 printf("Ticket etype: %s", s);
109 free(s);
110 if(t.enc_part.kvno)
111 printf(", kvno %d", *t.enc_part.kvno);
112 printf("\n");
113 free_Ticket(&t);
115 krb5_keytype_to_string(context, cred->session.keytype, &str);
116 printf("Session key: %s\n", str);
117 free(str);
118 printf("Auth time: %s\n", printable_time_long(cred->times.authtime));
119 if(cred->times.authtime != cred->times.starttime)
120 printf("Start time: %s\n", printable_time_long(cred->times.starttime));
121 printf("End time: %s", printable_time_long(cred->times.endtime));
122 if(sec > cred->times.endtime)
123 printf(" (expired)");
124 printf("\n");
125 if(cred->flags.b.renewable)
126 printf("Renew till: %s\n",
127 printable_time_long(cred->times.renew_till));
128 printf("Ticket flags: ");
129 #define PRINT_FLAG2(f, s) if(cred->flags.b.f) { if(!first_flag) printf(", "); printf("%s", #s); first_flag = 0; }
130 #define PRINT_FLAG(f) PRINT_FLAG2(f, f)
131 first_flag = 1;
132 PRINT_FLAG(forwardable);
133 PRINT_FLAG(forwarded);
134 PRINT_FLAG(proxiable);
135 PRINT_FLAG(proxy);
136 PRINT_FLAG2(may_postdate, may-postdate);
137 PRINT_FLAG(postdated);
138 PRINT_FLAG(invalid);
139 PRINT_FLAG(renewable);
140 PRINT_FLAG(initial);
141 PRINT_FLAG2(pre_authent, pre-authenticated);
142 PRINT_FLAG2(hw_authent, hw-authenticated);
143 PRINT_FLAG2(transited_policy_checked, transited-policy-checked);
144 PRINT_FLAG2(ok_as_delegate, ok-as-delegate);
145 PRINT_FLAG(anonymous);
146 printf("\n");
147 printf("Addresses: ");
148 for(j = 0; j < cred->addresses.len; j++){
149 if(j) printf(", ");
150 switch(cred->addresses.val[j].addr_type){
151 case KRB5_ADDRESS_INET :
152 printf("IPv4: %s", inet_ntoa(*(struct in_addr*)cred->addresses.val[j].address.data));
153 break;
154 #if defined(AF_INET6) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
155 case KRB5_ADDRESS_INET6: {
156 char foo[INET6_ADDRSTRLEN];
158 printf("IPv6: %s", inet_ntop(AF_INET6,
159 cred->addresses.val[j].address.data,
160 foo, sizeof(foo)));
161 break;
163 #endif
164 default:{
165 char *s;
166 int i;
167 krb5_address *a = &cred->addresses.val[j];
168 s = malloc(a->address.length * 2 + 1);
169 for(i = 0; i < a->address.length; i++)
170 sprintf(s + 2*i, "%02x", ((char*)a->address.data)[i]);
171 printf("%d/%s", cred->addresses.val[j].addr_type, s);
172 free(s);
176 printf("\n\n");
179 static int version_flag = 0;
180 static int help_flag = 0;
181 static int do_verbose = 0;
183 static struct getargs args[] = {
184 { "verbose", 'v', arg_flag, &do_verbose,
185 "Verbose output", NULL },
186 { "version", 0, arg_flag, &version_flag,
187 "print version", NULL },
188 { "help", 0, arg_flag, &help_flag,
189 NULL, NULL}
192 static int
193 usage (int ret)
195 arg_printusage (args,
196 sizeof(args)/sizeof(*args),
197 "");
198 exit (ret);
202 main (int argc, char **argv)
204 krb5_error_code ret;
205 krb5_context context;
206 krb5_ccache ccache;
207 krb5_principal principal;
208 krb5_cc_cursor cursor;
209 krb5_creds creds;
210 char *str;
211 int optind = 0;
213 set_progname (argv[0]);
215 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
216 usage(1);
218 if (help_flag)
219 usage (0);
221 if(version_flag){
222 printf("%s (%s-%s)\n", __progname, PACKAGE, VERSION);
223 exit(0);
226 argc -= optind;
227 argv += optind;
229 if (argc != 0)
230 usage (1);
232 ret = krb5_init_context (&context);
233 if (ret)
234 krb5_err(context, 1, ret, "krb5_init_context");
236 ret = krb5_cc_default (context, &ccache);
237 if (ret)
238 krb5_err (context, 1, ret, "krb5_cc_default");
240 ret = krb5_cc_get_principal (context, ccache, &principal);
241 if(ret == ENOENT)
242 krb5_errx(context, 1, "No ticket file: %s",
243 krb5_cc_get_name(context, ccache));
244 if (ret)
245 krb5_err (context, 1, ret, "krb5_cc_get_principal");
247 ret = krb5_unparse_name (context, principal, &str);
248 if (ret)
249 krb5_err (context, 1, ret, "krb5_unparse_name");
251 printf ("Credentials cache: %s\n", krb5_cc_get_name(context, ccache));
252 printf ("\tPrincipal: %s\n\n", str);
253 free (str);
255 if (do_verbose && context->kdc_sec_offset) {
256 char buf[BUFSIZ];
257 int val;
258 int sig;
260 val = context->kdc_sec_offset;
261 sig = 1;
262 if (val < 0) {
263 sig = -1;
264 val = -val;
267 unparse_time (val, buf, sizeof(buf));
269 printf ("\tKDC time offset: %s%s\n",
270 sig == -1 ? "-" : "", buf);
273 ret = krb5_cc_start_seq_get (context, ccache, &cursor);
274 if (ret)
275 krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
277 if(!do_verbose)
278 printf(" %-15s %-15s %s\n", "Issued", "Expires", "Principal");
280 while (krb5_cc_next_cred (context,
281 ccache,
282 &creds,
283 &cursor) == 0) {
284 if(do_verbose){
285 print_cred_verbose(context, &creds);
286 }else{
287 print_cred(context, &creds);
289 krb5_free_creds_contents (context, &creds);
291 ret = krb5_cc_end_seq_get (context, ccache, &cursor);
292 if (ret)
293 krb5_err (context, 1, ret, "krb5_cc_end_seq_get");
295 ret = krb5_cc_close (context, ccache);
296 if (ret)
297 krb5_err (context, 1, ret, "krb5_cc_close");
299 krb5_free_principal (context, principal);
300 krb5_free_context (context);
301 return 0;