xref: /freebsd/crypto/heimdal/lib/hdb/print.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*
2  * Copyright (c) 1999-2001 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 KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32 
33 #include "hdb_locl.h"
34 #include <ctype.h>
35 
36 RCSID("$Id: print.c,v 1.5 2001/01/26 15:08:36 joda Exp $");
37 
38 /*
39    This is the present contents of a dump line. This might change at
40    any time. Fields are separated by white space.
41 
42   principal
43   keyblock
44   	kvno
45 	keys...
46 		mkvno
47 		enctype
48 		keyvalue
49 		salt (- means use normal salt)
50   creation date and principal
51   modification date and principal
52   principal valid from date (not used)
53   principal valid end date (not used)
54   principal key expires (not used)
55   max ticket life
56   max renewable life
57   flags
58   */
59 
60 static void
61 append_hex(char *str, krb5_data *data)
62 {
63     int i, s = 1;
64     char *p;
65 
66     p = data->data;
67     for(i = 0; i < data->length; i++)
68 	if(!isalnum((unsigned char)p[i]) && p[i] != '.'){
69 	    s = 0;
70 	    break;
71 	}
72     if(s){
73 	p = calloc(1, data->length + 2 + 1);
74 	p[0] = '\"';
75 	p[data->length + 1] = '\"';
76 	memcpy(p + 1, data->data, data->length);
77     }else{
78 	const char *xchars = "0123456789abcdef";
79 	char *q = p = malloc(data->length * 2 + 1);
80 	for(i = 0; i < data->length; i++) {
81 	    unsigned char c = ((u_char*)data->data)[i];
82 	    *q++ = xchars[(c & 0xf0) >> 4];
83 	    *q++ = xchars[(c & 0xf)];
84 	}
85 	*q = '\0';
86     }
87     strcat(str, p);
88     free(p);
89 }
90 
91 static char *
92 time2str(time_t t)
93 {
94     static char buf[128];
95     strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
96     return buf;
97 }
98 
99 static krb5_error_code
100 event2string(krb5_context context, Event *ev, char **str)
101 {
102     char *p;
103     char *pr;
104     krb5_error_code ret;
105     if(ev == NULL){
106 	*str = strdup("-");
107 	return (*str == NULL) ? ENOMEM : 0;
108     }
109     if (ev->principal == NULL) {
110        pr = strdup("UNKNOWN");
111        if (pr == NULL)
112 	   return ENOMEM;
113     } else {
114        ret = krb5_unparse_name(context, ev->principal, &pr);
115        if(ret)
116            return ret;
117     }
118     ret = asprintf(&p, "%s:%s", time2str(ev->time), pr);
119     free(pr);
120     if(ret < 0)
121 	return ENOMEM;
122     *str = p;
123     return 0;
124 }
125 
126 krb5_error_code
127 hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
128 {
129     char *p;
130     char buf[1024] = "";
131     char tmp[32];
132     int i;
133     krb5_error_code ret;
134 
135     /* --- principal */
136     ret = krb5_unparse_name(context, ent->principal, &p);
137     if(ret)
138 	return ret;
139     strlcat(buf, p, sizeof(buf));
140     strlcat(buf, " ", sizeof(buf));
141     free(p);
142     /* --- kvno */
143     snprintf(tmp, sizeof(tmp), "%d", ent->kvno);
144     strlcat(buf, tmp, sizeof(buf));
145     /* --- keys */
146     for(i = 0; i < ent->keys.len; i++){
147 	/* --- mkvno, keytype */
148 	if(ent->keys.val[i].mkvno)
149 	    snprintf(tmp, sizeof(tmp), ":%d:%d:",
150 		     *ent->keys.val[i].mkvno,
151 		     ent->keys.val[i].key.keytype);
152 	else
153 	    snprintf(tmp, sizeof(tmp), "::%d:",
154 		     ent->keys.val[i].key.keytype);
155 	strlcat(buf, tmp, sizeof(buf));
156 	/* --- keydata */
157 	append_hex(buf, &ent->keys.val[i].key.keyvalue);
158 	strlcat(buf, ":", sizeof(buf));
159 	/* --- salt */
160 	if(ent->keys.val[i].salt){
161 	    snprintf(tmp, sizeof(tmp), "%u/", ent->keys.val[i].salt->type);
162 	    strlcat(buf, tmp, sizeof(buf));
163 	    append_hex(buf, &ent->keys.val[i].salt->salt);
164 	}else
165 	    strlcat(buf, "-", sizeof(buf));
166     }
167     strlcat(buf, " ", sizeof(buf));
168     /* --- created by */
169     event2string(context, &ent->created_by, &p);
170     strlcat(buf, p, sizeof(buf));
171     strlcat(buf, " ", sizeof(buf));
172     free(p);
173     /* --- modified by */
174     event2string(context, ent->modified_by, &p);
175     strlcat(buf, p, sizeof(buf));
176     strlcat(buf, " ", sizeof(buf));
177     free(p);
178 
179     /* --- valid start */
180     if(ent->valid_start)
181 	strlcat(buf, time2str(*ent->valid_start), sizeof(buf));
182     else
183 	strlcat(buf, "-", sizeof(buf));
184     strlcat(buf, " ", sizeof(buf));
185 
186     /* --- valid end */
187     if(ent->valid_end)
188 	strlcat(buf, time2str(*ent->valid_end), sizeof(buf));
189     else
190 	strlcat(buf, "-", sizeof(buf));
191     strlcat(buf, " ", sizeof(buf));
192 
193     /* --- password ends */
194     if(ent->pw_end)
195 	strlcat(buf, time2str(*ent->pw_end), sizeof(buf));
196     else
197 	strlcat(buf, "-", sizeof(buf));
198     strlcat(buf, " ", sizeof(buf));
199 
200     /* --- max life */
201     if(ent->max_life){
202 	snprintf(tmp, sizeof(tmp), "%d", *ent->max_life);
203 	strlcat(buf, tmp, sizeof(buf));
204     }else
205 	strlcat(buf, "-", sizeof(buf));
206     strlcat(buf, " ", sizeof(buf));
207 
208     /* --- max renewable life */
209     if(ent->max_renew){
210 	snprintf(tmp, sizeof(tmp), "%d", *ent->max_renew);
211 	strlcat(buf, tmp, sizeof(buf));
212     }else
213 	strlcat(buf, "-", sizeof(buf));
214 
215     strlcat(buf, " ", sizeof(buf));
216 
217     /* --- flags */
218     snprintf(tmp, sizeof(tmp), "%d", HDBFlags2int(ent->flags));
219     strlcat(buf, tmp, sizeof(buf));
220 
221     *str = strdup(buf);
222 
223     return 0;
224 }
225 
226 /* print a hdb_entry to (FILE*)data; suitable for hdb_foreach */
227 
228 krb5_error_code
229 hdb_print_entry(krb5_context context, HDB *db, hdb_entry *entry, void *data)
230 {
231     char *p;
232     hdb_entry2string(context, entry, &p);
233     fprintf((FILE*)data, "%s\n", p);
234     free(p);
235     return 0;
236 }
237