[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / hdb / print.c
blobfff27d03eb7b6ead6667d1504d44761b31ce6c42
1 /*
2 * Copyright (c) 1999-2005 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 <hex.h>
35 #include <ctype.h>
38 This is the present contents of a dump line. This might change at
39 any time. Fields are separated by white space.
41 principal
42 keyblock
43 kvno
44 keys...
45 mkvno
46 enctype
47 keyvalue
48 salt (- means use normal salt)
49 creation date and principal
50 modification date and principal
51 principal valid from date (not used)
52 principal valid end date (not used)
53 principal key expires (not used)
54 max ticket life
55 max renewable life
56 flags
57 generation number
60 static krb5_error_code
61 append_string(krb5_context context, krb5_storage *sp, const char *fmt, ...)
63 krb5_error_code ret;
64 char *s;
65 va_list ap;
66 va_start(ap, fmt);
67 vasprintf(&s, fmt, ap);
68 va_end(ap);
69 if(s == NULL) {
70 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
71 return ENOMEM;
73 ret = krb5_storage_write(sp, s, strlen(s));
74 free(s);
75 return ret;
78 static krb5_error_code
79 append_hex(krb5_context context, krb5_storage *sp, krb5_data *data)
81 int i, printable = 1;
82 char *p;
84 p = data->data;
85 for(i = 0; i < data->length; i++)
86 if(!isalnum((unsigned char)p[i]) && p[i] != '.'){
87 printable = 0;
88 break;
90 if(printable)
91 return append_string(context, sp, "\"%.*s\"",
92 data->length, data->data);
93 hex_encode(data->data, data->length, &p);
94 append_string(context, sp, "%s", p);
95 free(p);
96 return 0;
99 static char *
100 time2str(time_t t)
102 static char buf[128];
103 strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
104 return buf;
107 static krb5_error_code
108 append_event(krb5_context context, krb5_storage *sp, Event *ev)
110 char *pr = NULL;
111 krb5_error_code ret;
112 if(ev == NULL)
113 return append_string(context, sp, "- ");
114 if (ev->principal != NULL) {
115 ret = krb5_unparse_name(context, ev->principal, &pr);
116 if(ret)
117 return ret;
119 ret = append_string(context, sp, "%s:%s ",
120 time2str(ev->time), pr ? pr : "UNKNOWN");
121 free(pr);
122 return ret;
125 static krb5_error_code
126 entry2string_int (krb5_context context, krb5_storage *sp, hdb_entry *ent)
128 char *p;
129 int i;
130 krb5_error_code ret;
132 /* --- principal */
133 ret = krb5_unparse_name(context, ent->principal, &p);
134 if(ret)
135 return ret;
136 append_string(context, sp, "%s ", p);
137 free(p);
138 /* --- kvno */
139 append_string(context, sp, "%d", ent->kvno);
140 /* --- keys */
141 for(i = 0; i < ent->keys.len; i++){
142 /* --- mkvno, keytype */
143 if(ent->keys.val[i].mkvno)
144 append_string(context, sp, ":%d:%d:",
145 *ent->keys.val[i].mkvno,
146 ent->keys.val[i].key.keytype);
147 else
148 append_string(context, sp, "::%d:",
149 ent->keys.val[i].key.keytype);
150 /* --- keydata */
151 append_hex(context, sp, &ent->keys.val[i].key.keyvalue);
152 append_string(context, sp, ":");
153 /* --- salt */
154 if(ent->keys.val[i].salt){
155 append_string(context, sp, "%u/", ent->keys.val[i].salt->type);
156 append_hex(context, sp, &ent->keys.val[i].salt->salt);
157 }else
158 append_string(context, sp, "-");
160 append_string(context, sp, " ");
161 /* --- created by */
162 append_event(context, sp, &ent->created_by);
163 /* --- modified by */
164 append_event(context, sp, ent->modified_by);
166 /* --- valid start */
167 if(ent->valid_start)
168 append_string(context, sp, "%s ", time2str(*ent->valid_start));
169 else
170 append_string(context, sp, "- ");
172 /* --- valid end */
173 if(ent->valid_end)
174 append_string(context, sp, "%s ", time2str(*ent->valid_end));
175 else
176 append_string(context, sp, "- ");
178 /* --- password ends */
179 if(ent->pw_end)
180 append_string(context, sp, "%s ", time2str(*ent->pw_end));
181 else
182 append_string(context, sp, "- ");
184 /* --- max life */
185 if(ent->max_life)
186 append_string(context, sp, "%d ", *ent->max_life);
187 else
188 append_string(context, sp, "- ");
190 /* --- max renewable life */
191 if(ent->max_renew)
192 append_string(context, sp, "%d ", *ent->max_renew);
193 else
194 append_string(context, sp, "- ");
196 /* --- flags */
197 append_string(context, sp, "%d ", HDBFlags2int(ent->flags));
199 /* --- generation number */
200 if(ent->generation) {
201 append_string(context, sp, "%s:%d:%d ", time2str(ent->generation->time),
202 ent->generation->usec,
203 ent->generation->gen);
204 } else
205 append_string(context, sp, "- ");
207 /* --- extensions */
208 if(ent->extensions && ent->extensions->len > 0) {
209 for(i = 0; i < ent->extensions->len; i++) {
210 void *d;
211 size_t size, sz;
213 ASN1_MALLOC_ENCODE(HDB_extension, d, size,
214 &ent->extensions->val[i], &sz, ret);
215 if (ret) {
216 krb5_clear_error_message(context);
217 return ret;
219 if(size != sz)
220 krb5_abortx(context, "internal asn.1 encoder error");
222 if (hex_encode(d, size, &p) < 0) {
223 free(d);
224 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
225 return ENOMEM;
228 free(d);
229 append_string(context, sp, "%s%s", p,
230 ent->extensions->len - 1 != i ? ":" : "");
231 free(p);
233 } else
234 append_string(context, sp, "-");
237 return 0;
240 krb5_error_code
241 hdb_entry2string (krb5_context context, hdb_entry *ent, char **str)
243 krb5_error_code ret;
244 krb5_data data;
245 krb5_storage *sp;
247 sp = krb5_storage_emem();
248 if(sp == NULL) {
249 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
250 return ENOMEM;
253 ret = entry2string_int(context, sp, ent);
254 if(ret) {
255 krb5_storage_free(sp);
256 return ret;
259 krb5_storage_write(sp, "\0", 1);
260 krb5_storage_to_data(sp, &data);
261 krb5_storage_free(sp);
262 *str = data.data;
263 return 0;
266 /* print a hdb_entry to (FILE*)data; suitable for hdb_foreach */
268 krb5_error_code
269 hdb_print_entry(krb5_context context, HDB *db, hdb_entry_ex *entry, void *data)
271 krb5_error_code ret;
272 krb5_storage *sp;
274 FILE *f = data;
276 fflush(f);
277 sp = krb5_storage_from_fd(fileno(f));
278 if(sp == NULL) {
279 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
280 return ENOMEM;
283 ret = entry2string_int(context, sp, &entry->entry);
284 if(ret) {
285 krb5_storage_free(sp);
286 return ret;
289 krb5_storage_write(sp, "\n", 1);
290 krb5_storage_free(sp);
291 return 0;