This commit was manufactured by cvs2svn to create tag
[heimdal.git] / lib / hdb / print.c
blob5a3133c998c7d287e42f1bc221437c78ac29c6d7
1 /*
2 * Copyright (c) 1999 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. 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.
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. */
33 #include "hdb_locl.h"
34 #include <ctype.h>
36 RCSID("$Id$");
38 /*
39 This is the present contents of a dump line. This might change at
40 any time. Fields are separated by white space.
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
60 static void
61 append_hex(char *str, krb5_data *data)
63 int i, s = 1;
64 char *p;
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;
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]);
82 strcat(str, p);
83 free(p);
86 static char *
87 time2str(time_t t)
89 static char buf[128];
90 strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
91 return buf;
94 static krb5_error_code
95 event2string(krb5_context context, Event *ev, char **str)
97 char *p;
98 char *pr;
99 krb5_error_code ret;
100 if(ev == NULL){
101 *str = strdup("-");
102 return (*str == NULL) ? ENOMEM : 0;
104 ret = krb5_unparse_name(context, ev->principal, &pr);
105 if(ret)
106 return ret;
107 ret = asprintf(&p, "%s:%s", time2str(ev->time), pr);
108 free(pr);
109 if(ret < 0)
110 return ENOMEM;
111 *str = p;
112 return 0;
115 krb5_error_code
116 hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
118 char *p;
119 char buf[1024] = "";
120 int i;
121 krb5_error_code ret;
123 /* --- principal */
124 ret = krb5_unparse_name(context, ent->principal, &p);
125 if(ret)
126 return ret;
127 strcat_truncate(buf, p, sizeof(buf));
128 strcat_truncate(buf, " ", sizeof(buf));
129 free(p);
130 /* --- kvno */
131 asprintf(&p, "%d", ent->kvno);
132 strcat_truncate(buf, p, sizeof(buf));
133 free(p);
134 /* --- keys */
135 for(i = 0; i < ent->keys.len; i++){
136 /* --- mkvno, keytype */
137 if(ent->keys.val[i].mkvno)
138 asprintf(&p, ":%d:%d:",
139 *ent->keys.val[i].mkvno,
140 ent->keys.val[i].key.keytype);
141 else
142 asprintf(&p, "::%d:",
143 ent->keys.val[i].key.keytype);
144 strcat_truncate(buf, p, sizeof(buf));
145 free(p);
146 /* --- keydata */
147 append_hex(buf, &ent->keys.val[i].key.keyvalue);
148 strcat_truncate(buf, ":", sizeof(buf));
149 /* --- salt */
150 if(ent->keys.val[i].salt){
151 asprintf(&p, "%u/", ent->keys.val[i].salt->type);
152 strcat_truncate(buf, p, sizeof(buf));
153 free(p);
154 append_hex(buf, &ent->keys.val[i].salt->salt);
155 }else
156 strcat_truncate(buf, "-", sizeof(buf));
158 strcat_truncate(buf, " ", sizeof(buf));
159 /* --- created by */
160 event2string(context, &ent->created_by, &p);
161 strcat_truncate(buf, p, sizeof(buf));
162 strcat_truncate(buf, " ", sizeof(buf));
163 free(p);
164 /* --- modified by */
165 event2string(context, ent->modified_by, &p);
166 strcat_truncate(buf, p, sizeof(buf));
167 strcat_truncate(buf, " ", sizeof(buf));
168 free(p);
170 /* --- valid start */
171 if(ent->valid_start)
172 strcat_truncate(buf, time2str(*ent->valid_start), sizeof(buf));
173 else
174 strcat_truncate(buf, "-", sizeof(buf));
175 strcat_truncate(buf, " ", sizeof(buf));
177 /* --- valid end */
178 if(ent->valid_end)
179 strcat_truncate(buf, time2str(*ent->valid_end), sizeof(buf));
180 else
181 strcat_truncate(buf, "-", sizeof(buf));
182 strcat_truncate(buf, " ", sizeof(buf));
184 /* --- password ends */
185 if(ent->pw_end)
186 strcat_truncate(buf, time2str(*ent->pw_end), sizeof(buf));
187 else
188 strcat_truncate(buf, "-", sizeof(buf));
189 strcat_truncate(buf, " ", sizeof(buf));
191 /* --- max life */
192 if(ent->max_life){
193 asprintf(&p, "%d", *ent->max_life);
194 strcat_truncate(buf, p, sizeof(buf));
195 free(p);
196 }else
197 strcat_truncate(buf, "-", sizeof(buf));
198 strcat_truncate(buf, " ", sizeof(buf));
200 /* --- max renewable life */
201 if(ent->max_renew){
202 asprintf(&p, "%d", *ent->max_renew);
203 strcat_truncate(buf, p, sizeof(buf));
204 free(p);
205 }else
206 strcat_truncate(buf, "-", sizeof(buf));
208 strcat_truncate(buf, " ", sizeof(buf));
210 /* --- flags */
211 asprintf(&p, "%d", HDBFlags2int(ent->flags));
212 strcat_truncate(buf, p, sizeof(buf));
213 free(p);
215 *str = strdup(buf);
217 return 0;
220 /* print a hdb_entry to (FILE*)data; suitable for hdb_foreach */
222 krb5_error_code
223 hdb_print_entry(krb5_context context, HDB *db, hdb_entry *entry, void *data)
225 char *p;
226 hdb_entry2string(context, entry, &p);
227 fprintf((FILE*)data, "%s\n", p);
228 free(p);
229 return 0;