xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hdb/print.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: print.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1999-2005 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 KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc  *    specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include "hdb_locl.h"
36ebfedea0SLionel Sambuc #include <krb5/hex.h>
37ebfedea0SLionel Sambuc #include <ctype.h>
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc /*
40ebfedea0SLionel Sambuc    This is the present contents of a dump line. This might change at
41ebfedea0SLionel Sambuc    any time. Fields are separated by white space.
42ebfedea0SLionel Sambuc 
43ebfedea0SLionel Sambuc   principal
44ebfedea0SLionel Sambuc   keyblock
45ebfedea0SLionel Sambuc   	kvno
46ebfedea0SLionel Sambuc 	keys...
47ebfedea0SLionel Sambuc 		mkvno
48ebfedea0SLionel Sambuc 		enctype
49ebfedea0SLionel Sambuc 		keyvalue
50ebfedea0SLionel Sambuc 		salt (- means use normal salt)
51ebfedea0SLionel Sambuc   creation date and principal
52ebfedea0SLionel Sambuc   modification date and principal
53ebfedea0SLionel Sambuc   principal valid from date (not used)
54ebfedea0SLionel Sambuc   principal valid end date (not used)
55ebfedea0SLionel Sambuc   principal key expires (not used)
56ebfedea0SLionel Sambuc   max ticket life
57ebfedea0SLionel Sambuc   max renewable life
58ebfedea0SLionel Sambuc   flags
59ebfedea0SLionel Sambuc   generation number
60ebfedea0SLionel Sambuc   */
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc static krb5_error_code
append_string(krb5_context context,krb5_storage * sp,const char * fmt,...)63ebfedea0SLionel Sambuc append_string(krb5_context context, krb5_storage *sp, const char *fmt, ...)
64ebfedea0SLionel Sambuc {
65ebfedea0SLionel Sambuc     krb5_error_code ret;
66ebfedea0SLionel Sambuc     char *s;
67ebfedea0SLionel Sambuc     va_list ap;
68ebfedea0SLionel Sambuc     va_start(ap, fmt);
69ebfedea0SLionel Sambuc     vasprintf(&s, fmt, ap);
70ebfedea0SLionel Sambuc     va_end(ap);
71ebfedea0SLionel Sambuc     if(s == NULL) {
72ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
73ebfedea0SLionel Sambuc 	return ENOMEM;
74ebfedea0SLionel Sambuc     }
75ebfedea0SLionel Sambuc     ret = krb5_storage_write(sp, s, strlen(s));
76ebfedea0SLionel Sambuc     free(s);
77ebfedea0SLionel Sambuc     return ret;
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc static krb5_error_code
append_hex(krb5_context context,krb5_storage * sp,krb5_data * data)81ebfedea0SLionel Sambuc append_hex(krb5_context context, krb5_storage *sp, krb5_data *data)
82ebfedea0SLionel Sambuc {
83*0a6a1f1dSLionel Sambuc     int printable = 1;
84*0a6a1f1dSLionel Sambuc     size_t i;
85ebfedea0SLionel Sambuc     char *p;
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc     p = data->data;
88ebfedea0SLionel Sambuc     for(i = 0; i < data->length; i++)
89ebfedea0SLionel Sambuc 	if(!isalnum((unsigned char)p[i]) && p[i] != '.'){
90ebfedea0SLionel Sambuc 	    printable = 0;
91ebfedea0SLionel Sambuc 	    break;
92ebfedea0SLionel Sambuc 	}
93ebfedea0SLionel Sambuc     if(printable)
94ebfedea0SLionel Sambuc 	return append_string(context, sp, "\"%.*s\"",
95ebfedea0SLionel Sambuc 			     data->length, data->data);
96ebfedea0SLionel Sambuc     hex_encode(data->data, data->length, &p);
97ebfedea0SLionel Sambuc     append_string(context, sp, "%s", p);
98ebfedea0SLionel Sambuc     free(p);
99ebfedea0SLionel Sambuc     return 0;
100ebfedea0SLionel Sambuc }
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc static char *
time2str(time_t t)103ebfedea0SLionel Sambuc time2str(time_t t)
104ebfedea0SLionel Sambuc {
105ebfedea0SLionel Sambuc     static char buf[128];
106ebfedea0SLionel Sambuc     strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
107ebfedea0SLionel Sambuc     return buf;
108ebfedea0SLionel Sambuc }
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc static krb5_error_code
append_event(krb5_context context,krb5_storage * sp,Event * ev)111ebfedea0SLionel Sambuc append_event(krb5_context context, krb5_storage *sp, Event *ev)
112ebfedea0SLionel Sambuc {
113ebfedea0SLionel Sambuc     char *pr = NULL;
114ebfedea0SLionel Sambuc     krb5_error_code ret;
115ebfedea0SLionel Sambuc     if(ev == NULL)
116ebfedea0SLionel Sambuc 	return append_string(context, sp, "- ");
117ebfedea0SLionel Sambuc     if (ev->principal != NULL) {
118ebfedea0SLionel Sambuc        ret = krb5_unparse_name(context, ev->principal, &pr);
119ebfedea0SLionel Sambuc        if(ret)
120ebfedea0SLionel Sambuc            return ret;
121ebfedea0SLionel Sambuc     }
122ebfedea0SLionel Sambuc     ret = append_string(context, sp, "%s:%s ",
123ebfedea0SLionel Sambuc 			time2str(ev->time), pr ? pr : "UNKNOWN");
124ebfedea0SLionel Sambuc     free(pr);
125ebfedea0SLionel Sambuc     return ret;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc static krb5_error_code
entry2string_int(krb5_context context,krb5_storage * sp,hdb_entry * ent)129ebfedea0SLionel Sambuc entry2string_int (krb5_context context, krb5_storage *sp, hdb_entry *ent)
130ebfedea0SLionel Sambuc {
131ebfedea0SLionel Sambuc     char *p;
132*0a6a1f1dSLionel Sambuc     size_t i;
133ebfedea0SLionel Sambuc     krb5_error_code ret;
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     /* --- principal */
136ebfedea0SLionel Sambuc     ret = krb5_unparse_name(context, ent->principal, &p);
137ebfedea0SLionel Sambuc     if(ret)
138ebfedea0SLionel Sambuc 	return ret;
139ebfedea0SLionel Sambuc     append_string(context, sp, "%s ", p);
140ebfedea0SLionel Sambuc     free(p);
141ebfedea0SLionel Sambuc     /* --- kvno */
142ebfedea0SLionel Sambuc     append_string(context, sp, "%d", ent->kvno);
143ebfedea0SLionel Sambuc     /* --- keys */
144ebfedea0SLionel Sambuc     for(i = 0; i < ent->keys.len; i++){
145ebfedea0SLionel Sambuc 	/* --- mkvno, keytype */
146ebfedea0SLionel Sambuc 	if(ent->keys.val[i].mkvno)
147ebfedea0SLionel Sambuc 	    append_string(context, sp, ":%d:%d:",
148ebfedea0SLionel Sambuc 			  *ent->keys.val[i].mkvno,
149ebfedea0SLionel Sambuc 			  ent->keys.val[i].key.keytype);
150ebfedea0SLionel Sambuc 	else
151ebfedea0SLionel Sambuc 	    append_string(context, sp, "::%d:",
152ebfedea0SLionel Sambuc 			  ent->keys.val[i].key.keytype);
153ebfedea0SLionel Sambuc 	/* --- keydata */
154ebfedea0SLionel Sambuc 	append_hex(context, sp, &ent->keys.val[i].key.keyvalue);
155ebfedea0SLionel Sambuc 	append_string(context, sp, ":");
156ebfedea0SLionel Sambuc 	/* --- salt */
157ebfedea0SLionel Sambuc 	if(ent->keys.val[i].salt){
158ebfedea0SLionel Sambuc 	    append_string(context, sp, "%u/", ent->keys.val[i].salt->type);
159ebfedea0SLionel Sambuc 	    append_hex(context, sp, &ent->keys.val[i].salt->salt);
160ebfedea0SLionel Sambuc 	}else
161ebfedea0SLionel Sambuc 	    append_string(context, sp, "-");
162ebfedea0SLionel Sambuc     }
163ebfedea0SLionel Sambuc     append_string(context, sp, " ");
164ebfedea0SLionel Sambuc     /* --- created by */
165ebfedea0SLionel Sambuc     append_event(context, sp, &ent->created_by);
166ebfedea0SLionel Sambuc     /* --- modified by */
167ebfedea0SLionel Sambuc     append_event(context, sp, ent->modified_by);
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc     /* --- valid start */
170ebfedea0SLionel Sambuc     if(ent->valid_start)
171ebfedea0SLionel Sambuc 	append_string(context, sp, "%s ", time2str(*ent->valid_start));
172ebfedea0SLionel Sambuc     else
173ebfedea0SLionel Sambuc 	append_string(context, sp, "- ");
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc     /* --- valid end */
176ebfedea0SLionel Sambuc     if(ent->valid_end)
177ebfedea0SLionel Sambuc 	append_string(context, sp, "%s ", time2str(*ent->valid_end));
178ebfedea0SLionel Sambuc     else
179ebfedea0SLionel Sambuc 	append_string(context, sp, "- ");
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc     /* --- password ends */
182ebfedea0SLionel Sambuc     if(ent->pw_end)
183ebfedea0SLionel Sambuc 	append_string(context, sp, "%s ", time2str(*ent->pw_end));
184ebfedea0SLionel Sambuc     else
185ebfedea0SLionel Sambuc 	append_string(context, sp, "- ");
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc     /* --- max life */
188ebfedea0SLionel Sambuc     if(ent->max_life)
189ebfedea0SLionel Sambuc 	append_string(context, sp, "%d ", *ent->max_life);
190ebfedea0SLionel Sambuc     else
191ebfedea0SLionel Sambuc 	append_string(context, sp, "- ");
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     /* --- max renewable life */
194ebfedea0SLionel Sambuc     if(ent->max_renew)
195ebfedea0SLionel Sambuc 	append_string(context, sp, "%d ", *ent->max_renew);
196ebfedea0SLionel Sambuc     else
197ebfedea0SLionel Sambuc 	append_string(context, sp, "- ");
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc     /* --- flags */
200ebfedea0SLionel Sambuc     append_string(context, sp, "%d ", HDBFlags2int(ent->flags));
201ebfedea0SLionel Sambuc 
202ebfedea0SLionel Sambuc     /* --- generation number */
203ebfedea0SLionel Sambuc     if(ent->generation) {
204ebfedea0SLionel Sambuc 	append_string(context, sp, "%s:%d:%d ", time2str(ent->generation->time),
205ebfedea0SLionel Sambuc 		      ent->generation->usec,
206ebfedea0SLionel Sambuc 		      ent->generation->gen);
207ebfedea0SLionel Sambuc     } else
208ebfedea0SLionel Sambuc 	append_string(context, sp, "- ");
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc     /* --- extensions */
211ebfedea0SLionel Sambuc     if(ent->extensions && ent->extensions->len > 0) {
212ebfedea0SLionel Sambuc 	for(i = 0; i < ent->extensions->len; i++) {
213ebfedea0SLionel Sambuc 	    void *d;
214*0a6a1f1dSLionel Sambuc 	    size_t size, sz = 0;
215ebfedea0SLionel Sambuc 
216ebfedea0SLionel Sambuc 	    ASN1_MALLOC_ENCODE(HDB_extension, d, size,
217ebfedea0SLionel Sambuc 			       &ent->extensions->val[i], &sz, ret);
218ebfedea0SLionel Sambuc 	    if (ret) {
219ebfedea0SLionel Sambuc 		krb5_clear_error_message(context);
220ebfedea0SLionel Sambuc 		return ret;
221ebfedea0SLionel Sambuc 	    }
222ebfedea0SLionel Sambuc 	    if(size != sz)
223ebfedea0SLionel Sambuc 		krb5_abortx(context, "internal asn.1 encoder error");
224ebfedea0SLionel Sambuc 
225ebfedea0SLionel Sambuc 	    if (hex_encode(d, size, &p) < 0) {
226ebfedea0SLionel Sambuc 		free(d);
227ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
228ebfedea0SLionel Sambuc 		return ENOMEM;
229ebfedea0SLionel Sambuc 	    }
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc 	    free(d);
232ebfedea0SLionel Sambuc 	    append_string(context, sp, "%s%s", p,
233ebfedea0SLionel Sambuc 			  ent->extensions->len - 1 != i ? ":" : "");
234ebfedea0SLionel Sambuc 	    free(p);
235ebfedea0SLionel Sambuc 	}
236ebfedea0SLionel Sambuc     } else
237ebfedea0SLionel Sambuc 	append_string(context, sp, "-");
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc 
240ebfedea0SLionel Sambuc     return 0;
241ebfedea0SLionel Sambuc }
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc krb5_error_code
hdb_entry2string(krb5_context context,hdb_entry * ent,char ** str)244ebfedea0SLionel Sambuc hdb_entry2string (krb5_context context, hdb_entry *ent, char **str)
245ebfedea0SLionel Sambuc {
246ebfedea0SLionel Sambuc     krb5_error_code ret;
247ebfedea0SLionel Sambuc     krb5_data data;
248ebfedea0SLionel Sambuc     krb5_storage *sp;
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc     sp = krb5_storage_emem();
251ebfedea0SLionel Sambuc     if(sp == NULL) {
252ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
253ebfedea0SLionel Sambuc 	return ENOMEM;
254ebfedea0SLionel Sambuc     }
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc     ret = entry2string_int(context, sp, ent);
257ebfedea0SLionel Sambuc     if(ret) {
258ebfedea0SLionel Sambuc 	krb5_storage_free(sp);
259ebfedea0SLionel Sambuc 	return ret;
260ebfedea0SLionel Sambuc     }
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc     krb5_storage_write(sp, "\0", 1);
263ebfedea0SLionel Sambuc     krb5_storage_to_data(sp, &data);
264ebfedea0SLionel Sambuc     krb5_storage_free(sp);
265ebfedea0SLionel Sambuc     *str = data.data;
266ebfedea0SLionel Sambuc     return 0;
267ebfedea0SLionel Sambuc }
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc /* print a hdb_entry to (FILE*)data; suitable for hdb_foreach */
270ebfedea0SLionel Sambuc 
271ebfedea0SLionel Sambuc krb5_error_code
hdb_print_entry(krb5_context context,HDB * db,hdb_entry_ex * entry,void * data)272ebfedea0SLionel Sambuc hdb_print_entry(krb5_context context, HDB *db, hdb_entry_ex *entry, void *data)
273ebfedea0SLionel Sambuc {
274ebfedea0SLionel Sambuc     krb5_error_code ret;
275ebfedea0SLionel Sambuc     krb5_storage *sp;
276ebfedea0SLionel Sambuc 
277ebfedea0SLionel Sambuc     FILE *f = data;
278ebfedea0SLionel Sambuc 
279ebfedea0SLionel Sambuc     fflush(f);
280ebfedea0SLionel Sambuc     sp = krb5_storage_from_fd(fileno(f));
281ebfedea0SLionel Sambuc     if(sp == NULL) {
282ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
283ebfedea0SLionel Sambuc 	return ENOMEM;
284ebfedea0SLionel Sambuc     }
285ebfedea0SLionel Sambuc 
286ebfedea0SLionel Sambuc     ret = entry2string_int(context, sp, &entry->entry);
287ebfedea0SLionel Sambuc     if(ret) {
288ebfedea0SLionel Sambuc 	krb5_storage_free(sp);
289ebfedea0SLionel Sambuc 	return ret;
290ebfedea0SLionel Sambuc     }
291ebfedea0SLionel Sambuc 
292ebfedea0SLionel Sambuc     krb5_storage_write(sp, "\n", 1);
293ebfedea0SLionel Sambuc     krb5_storage_free(sp);
294ebfedea0SLionel Sambuc     return 0;
295ebfedea0SLionel Sambuc }
296