xref: /freebsd/crypto/heimdal/lib/hdb/print.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 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 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.4 1999/12/26 13:50:22 assar 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 	p = calloc(1, data->length * 2 + 1);
79 	for(i = 0; i < data->length; i++)
80 	    sprintf(p + 2 * i, "%02x", ((u_char*)data->data)[i]);
81     }
82     strcat(str, p);
83     free(p);
84 }
85 
86 static char *
87 time2str(time_t t)
88 {
89     static char buf[128];
90     strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
91     return buf;
92 }
93 
94 static krb5_error_code
95 event2string(krb5_context context, Event *ev, char **str)
96 {
97     char *p;
98     char *pr;
99     krb5_error_code ret;
100     if(ev == NULL){
101 	*str = strdup("-");
102 	return (*str == NULL) ? ENOMEM : 0;
103     }
104     if (ev->principal == NULL) {
105        pr = strdup("UNKNOWN");
106        if (pr == NULL)
107 	   return ENOMEM;
108     } else {
109        ret = krb5_unparse_name(context, ev->principal, &pr);
110        if(ret)
111            return ret;
112     }
113     ret = asprintf(&p, "%s:%s", time2str(ev->time), pr);
114     free(pr);
115     if(ret < 0)
116 	return ENOMEM;
117     *str = p;
118     return 0;
119 }
120 
121 krb5_error_code
122 hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
123 {
124     char *p;
125     char buf[1024] = "";
126     int i;
127     krb5_error_code ret;
128 
129     /* --- principal */
130     ret = krb5_unparse_name(context, ent->principal, &p);
131     if(ret)
132 	return ret;
133     strlcat(buf, p, sizeof(buf));
134     strlcat(buf, " ", sizeof(buf));
135     free(p);
136     /* --- kvno */
137     asprintf(&p, "%d", ent->kvno);
138     strlcat(buf, p, sizeof(buf));
139     free(p);
140     /* --- keys */
141     for(i = 0; i < ent->keys.len; i++){
142 	/* --- mkvno, keytype */
143 	if(ent->keys.val[i].mkvno)
144 	    asprintf(&p, ":%d:%d:",
145 		     *ent->keys.val[i].mkvno,
146 		     ent->keys.val[i].key.keytype);
147 	else
148 	    asprintf(&p, "::%d:",
149 		     ent->keys.val[i].key.keytype);
150 	strlcat(buf, p, sizeof(buf));
151 	free(p);
152 	/* --- keydata */
153 	append_hex(buf, &ent->keys.val[i].key.keyvalue);
154 	strlcat(buf, ":", sizeof(buf));
155 	/* --- salt */
156 	if(ent->keys.val[i].salt){
157 	    asprintf(&p, "%u/", ent->keys.val[i].salt->type);
158 	    strlcat(buf, p, sizeof(buf));
159 	    free(p);
160 	    append_hex(buf, &ent->keys.val[i].salt->salt);
161 	}else
162 	    strlcat(buf, "-", sizeof(buf));
163     }
164     strlcat(buf, " ", sizeof(buf));
165     /* --- created by */
166     event2string(context, &ent->created_by, &p);
167     strlcat(buf, p, sizeof(buf));
168     strlcat(buf, " ", sizeof(buf));
169     free(p);
170     /* --- modified by */
171     event2string(context, ent->modified_by, &p);
172     strlcat(buf, p, sizeof(buf));
173     strlcat(buf, " ", sizeof(buf));
174     free(p);
175 
176     /* --- valid start */
177     if(ent->valid_start)
178 	strlcat(buf, time2str(*ent->valid_start), sizeof(buf));
179     else
180 	strlcat(buf, "-", sizeof(buf));
181     strlcat(buf, " ", sizeof(buf));
182 
183     /* --- valid end */
184     if(ent->valid_end)
185 	strlcat(buf, time2str(*ent->valid_end), sizeof(buf));
186     else
187 	strlcat(buf, "-", sizeof(buf));
188     strlcat(buf, " ", sizeof(buf));
189 
190     /* --- password ends */
191     if(ent->pw_end)
192 	strlcat(buf, time2str(*ent->pw_end), sizeof(buf));
193     else
194 	strlcat(buf, "-", sizeof(buf));
195     strlcat(buf, " ", sizeof(buf));
196 
197     /* --- max life */
198     if(ent->max_life){
199 	asprintf(&p, "%d", *ent->max_life);
200 	strlcat(buf, p, sizeof(buf));
201 	free(p);
202     }else
203 	strlcat(buf, "-", sizeof(buf));
204     strlcat(buf, " ", sizeof(buf));
205 
206     /* --- max renewable life */
207     if(ent->max_renew){
208 	asprintf(&p, "%d", *ent->max_renew);
209 	strlcat(buf, p, sizeof(buf));
210 	free(p);
211     }else
212 	strlcat(buf, "-", sizeof(buf));
213 
214     strlcat(buf, " ", sizeof(buf));
215 
216     /* --- flags */
217     asprintf(&p, "%d", HDBFlags2int(ent->flags));
218     strlcat(buf, p, sizeof(buf));
219     free(p);
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