No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / load.c
blob91f65283da7cb70e114855fd525c8f8c59be0c86
1 /*
2 * Copyright (c) 1997-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 the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kadmin_locl.h"
35 #include "kadmin-commands.h"
36 #include <kadm5/private.h>
38 __RCSID("$Heimdal: load.c 16658 2006-01-25 12:29:46Z lha $"
39 "$NetBSD$");
41 struct entry {
42 char *principal;
43 char *key;
44 char *max_life;
45 char *max_renew;
46 char *created;
47 char *modified;
48 char *valid_start;
49 char *valid_end;
50 char *pw_end;
51 char *flags;
52 char *generation;
53 char *extensions;
56 static char *
57 skip_next(char *p)
59 while(*p && !isspace((unsigned char)*p))
60 p++;
61 *p++ = 0;
62 while(*p && isspace((unsigned char)*p))
63 p++;
64 return p;
68 * Parse the time in `s', returning:
69 * -1 if error parsing
70 * 0 if none present
71 * 1 if parsed ok
74 static int
75 parse_time_string(time_t *t, const char *s)
77 int year, month, date, hour, minute, second;
78 struct tm tm;
80 if(strcmp(s, "-") == 0)
81 return 0;
82 if(sscanf(s, "%04d%02d%02d%02d%02d%02d",
83 &year, &month, &date, &hour, &minute, &second) != 6)
84 return -1;
85 tm.tm_year = year - 1900;
86 tm.tm_mon = month - 1;
87 tm.tm_mday = date;
88 tm.tm_hour = hour;
89 tm.tm_min = minute;
90 tm.tm_sec = second;
91 tm.tm_isdst = 0;
92 *t = timegm(&tm);
93 return 1;
97 * parse time, allocating space in *t if it's there
100 static int
101 parse_time_string_alloc (time_t **t, const char *s)
103 time_t tmp;
104 int ret;
106 *t = NULL;
107 ret = parse_time_string (&tmp, s);
108 if (ret == 1) {
109 *t = malloc (sizeof (**t));
110 if (*t == NULL)
111 krb5_errx (context, 1, "malloc: out of memory");
112 **t = tmp;
114 return ret;
118 * see parse_time_string for calling convention
121 static int
122 parse_integer(unsigned int *u, const char *s)
124 if(strcmp(s, "-") == 0)
125 return 0;
126 if (sscanf(s, "%u", u) != 1)
127 return -1;
128 return 1;
131 static int
132 parse_integer_alloc (unsigned int **u, const char *s)
134 unsigned int tmp;
135 int ret;
137 *u = NULL;
138 ret = parse_integer (&tmp, s);
139 if (ret == 1) {
140 *u = malloc (sizeof (**u));
141 if (*u == NULL)
142 krb5_errx (context, 1, "malloc: out of memory");
143 **u = tmp;
145 return ret;
149 * Parse dumped keys in `str' and store them in `ent'
150 * return -1 if parsing failed
153 static int
154 parse_keys(hdb_entry *ent, char *str)
156 krb5_error_code ret;
157 int tmp;
158 char *p;
159 int i;
161 p = strsep(&str, ":");
162 if (sscanf(p, "%d", &tmp) != 1)
163 return 1;
164 ent->kvno = tmp;
165 p = strsep(&str, ":");
166 while(p){
167 Key *key;
168 key = realloc(ent->keys.val,
169 (ent->keys.len + 1) * sizeof(*ent->keys.val));
170 if(key == NULL)
171 krb5_errx (context, 1, "realloc: out of memory");
172 ent->keys.val = key;
173 key = ent->keys.val + ent->keys.len;
174 ent->keys.len++;
175 memset(key, 0, sizeof(*key));
176 if(sscanf(p, "%d", &tmp) == 1) {
177 key->mkvno = malloc(sizeof(*key->mkvno));
178 *key->mkvno = tmp;
179 } else
180 key->mkvno = NULL;
181 p = strsep(&str, ":");
182 if (sscanf(p, "%d", &tmp) != 1)
183 return 1;
184 key->key.keytype = tmp;
185 p = strsep(&str, ":");
186 ret = krb5_data_alloc(&key->key.keyvalue, (strlen(p) - 1) / 2 + 1);
187 if (ret)
188 krb5_err (context, 1, ret, "krb5_data_alloc");
189 for(i = 0; i < strlen(p); i += 2) {
190 if(sscanf(p + i, "%02x", &tmp) != 1)
191 return 1;
192 ((u_char*)key->key.keyvalue.data)[i / 2] = tmp;
194 p = strsep(&str, ":");
195 if(strcmp(p, "-") != 0){
196 unsigned type;
197 size_t p_len;
199 if(sscanf(p, "%u/", &type) != 1)
200 return 1;
201 p = strchr(p, '/');
202 if(p == NULL)
203 return 1;
204 p++;
205 p_len = strlen(p);
207 key->salt = malloc(sizeof(*key->salt));
208 if (key->salt == NULL)
209 krb5_errx (context, 1, "malloc: out of memory");
210 key->salt->type = type;
212 if (p_len) {
213 if(*p == '\"') {
214 ret = krb5_data_copy(&key->salt->salt, p + 1, p_len - 2);
215 if (ret)
216 krb5_err (context, 1, ret, "krb5_data_copy");
217 } else {
218 ret = krb5_data_alloc(&key->salt->salt,
219 (p_len - 1) / 2 + 1);
220 if (ret)
221 krb5_err (context, 1, ret, "krb5_data_alloc");
222 for(i = 0; i < p_len; i += 2){
223 if (sscanf(p + i, "%02x", &tmp) != 1)
224 return 1;
225 ((u_char*)key->salt->salt.data)[i / 2] = tmp;
228 } else
229 krb5_data_zero (&key->salt->salt);
231 p = strsep(&str, ":");
233 return 0;
237 * see parse_time_string for calling convention
240 static int
241 parse_event(Event *ev, char *s)
243 krb5_error_code ret;
244 char *p;
246 if(strcmp(s, "-") == 0)
247 return 0;
248 memset(ev, 0, sizeof(*ev));
249 p = strsep(&s, ":");
250 if(parse_time_string(&ev->time, p) != 1)
251 return -1;
252 p = strsep(&s, ":");
253 ret = krb5_parse_name(context, p, &ev->principal);
254 if (ret)
255 return -1;
256 return 1;
259 static int
260 parse_event_alloc (Event **ev, char *s)
262 Event tmp;
263 int ret;
265 *ev = NULL;
266 ret = parse_event (&tmp, s);
267 if (ret == 1) {
268 *ev = malloc (sizeof (**ev));
269 if (*ev == NULL)
270 krb5_errx (context, 1, "malloc: out of memory");
271 **ev = tmp;
273 return ret;
276 static int
277 parse_hdbflags2int(HDBFlags *f, const char *s)
279 int ret;
280 unsigned int tmp;
282 ret = parse_integer (&tmp, s);
283 if (ret == 1)
284 *f = int2HDBFlags (tmp);
285 return ret;
288 static int
289 parse_generation(char *str, GENERATION **gen)
291 char *p;
292 int v;
294 if(strcmp(str, "-") == 0 || *str == '\0') {
295 *gen = NULL;
296 return 0;
298 *gen = calloc(1, sizeof(**gen));
300 p = strsep(&str, ":");
301 if(parse_time_string(&(*gen)->time, p) != 1)
302 return -1;
303 p = strsep(&str, ":");
304 if(sscanf(p, "%d", &v) != 1)
305 return -1;
306 (*gen)->usec = v;
307 p = strsep(&str, ":");
308 if(sscanf(p, "%d", &v) != 1)
309 return -1;
310 (*gen)->gen = v - 1; /* XXX gets bumped in _hdb_store */
311 return 0;
314 static int
315 parse_extensions(char *str, HDB_extensions **e)
317 char *p;
318 int ret;
320 if(strcmp(str, "-") == 0 || *str == '\0') {
321 *e = NULL;
322 return 0;
324 *e = calloc(1, sizeof(**e));
326 p = strsep(&str, ":");
328 while (p) {
329 HDB_extension ext;
330 ssize_t len;
331 void *d;
333 len = strlen(p);
334 d = malloc(len);
336 len = hex_decode(p, d, len);
337 if (len < 0)
338 return -1;
340 ret = decode_HDB_extension(d, len, &ext, NULL);
341 free(d);
342 if (ret)
343 return -1;
344 d = realloc((*e)->val, ((*e)->len + 1) * sizeof((*e)->val[0]));
345 if (d == NULL)
346 abort();
347 (*e)->val = d;
348 (*e)->val[(*e)->len] = ext;
349 (*e)->len++;
351 p = strsep(&str, ":");
354 return 0;
359 * Parse the dump file in `filename' and create the database (merging
360 * iff merge)
363 static int
364 doit(const char *filename, int mergep)
366 krb5_error_code ret;
367 FILE *f;
368 char s[8192]; /* XXX should fix this properly */
369 char *p;
370 int line;
371 int flags = O_RDWR;
372 struct entry e;
373 hdb_entry_ex ent;
374 HDB *db = _kadm5_s_get_db(kadm_handle);
376 f = fopen(filename, "r");
377 if(f == NULL){
378 krb5_warn(context, errno, "fopen(%s)", filename);
379 return 1;
381 ret = kadm5_log_truncate (kadm_handle);
382 if (ret) {
383 fclose (f);
384 krb5_warn(context, ret, "kadm5_log_truncate");
385 return 1;
388 if(!mergep)
389 flags |= O_CREAT | O_TRUNC;
390 ret = db->hdb_open(context, db, flags, 0600);
391 if(ret){
392 krb5_warn(context, ret, "hdb_open");
393 fclose(f);
394 return 1;
396 line = 0;
397 ret = 0;
398 while(fgets(s, sizeof(s), f) != NULL) {
399 ret = 0;
400 line++;
402 p = s;
403 while (isspace((unsigned char)*p))
404 p++;
406 e.principal = p;
407 for(p = s; *p; p++){
408 if(*p == '\\')
409 p++;
410 else if(isspace((unsigned char)*p)) {
411 *p = 0;
412 break;
415 p = skip_next(p);
417 e.key = p;
418 p = skip_next(p);
420 e.created = p;
421 p = skip_next(p);
423 e.modified = p;
424 p = skip_next(p);
426 e.valid_start = p;
427 p = skip_next(p);
429 e.valid_end = p;
430 p = skip_next(p);
432 e.pw_end = p;
433 p = skip_next(p);
435 e.max_life = p;
436 p = skip_next(p);
438 e.max_renew = p;
439 p = skip_next(p);
441 e.flags = p;
442 p = skip_next(p);
444 e.generation = p;
445 p = skip_next(p);
447 e.extensions = p;
448 p = skip_next(p);
450 memset(&ent, 0, sizeof(ent));
451 ret = krb5_parse_name(context, e.principal, &ent.entry.principal);
452 if(ret) {
453 fprintf(stderr, "%s:%d:%s (%s)\n",
454 filename,
455 line,
456 krb5_get_err_text(context, ret),
457 e.principal);
458 continue;
461 if (parse_keys(&ent.entry, e.key)) {
462 fprintf (stderr, "%s:%d:error parsing keys (%s)\n",
463 filename, line, e.key);
464 hdb_free_entry (context, &ent);
465 continue;
468 if (parse_event(&ent.entry.created_by, e.created) == -1) {
469 fprintf (stderr, "%s:%d:error parsing created event (%s)\n",
470 filename, line, e.created);
471 hdb_free_entry (context, &ent);
472 continue;
474 if (parse_event_alloc (&ent.entry.modified_by, e.modified) == -1) {
475 fprintf (stderr, "%s:%d:error parsing event (%s)\n",
476 filename, line, e.modified);
477 hdb_free_entry (context, &ent);
478 continue;
480 if (parse_time_string_alloc (&ent.entry.valid_start, e.valid_start) == -1) {
481 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
482 filename, line, e.valid_start);
483 hdb_free_entry (context, &ent);
484 continue;
486 if (parse_time_string_alloc (&ent.entry.valid_end, e.valid_end) == -1) {
487 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
488 filename, line, e.valid_end);
489 hdb_free_entry (context, &ent);
490 continue;
492 if (parse_time_string_alloc (&ent.entry.pw_end, e.pw_end) == -1) {
493 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
494 filename, line, e.pw_end);
495 hdb_free_entry (context, &ent);
496 continue;
499 if (parse_integer_alloc (&ent.entry.max_life, e.max_life) == -1) {
500 fprintf (stderr, "%s:%d:error parsing lifetime (%s)\n",
501 filename, line, e.max_life);
502 hdb_free_entry (context, &ent);
503 continue;
506 if (parse_integer_alloc (&ent.entry.max_renew, e.max_renew) == -1) {
507 fprintf (stderr, "%s:%d:error parsing lifetime (%s)\n",
508 filename, line, e.max_renew);
509 hdb_free_entry (context, &ent);
510 continue;
513 if (parse_hdbflags2int (&ent.entry.flags, e.flags) != 1) {
514 fprintf (stderr, "%s:%d:error parsing flags (%s)\n",
515 filename, line, e.flags);
516 hdb_free_entry (context, &ent);
517 continue;
520 if(parse_generation(e.generation, &ent.entry.generation) == -1) {
521 fprintf (stderr, "%s:%d:error parsing generation (%s)\n",
522 filename, line, e.generation);
523 hdb_free_entry (context, &ent);
524 continue;
527 if(parse_extensions(e.extensions, &ent.entry.extensions) == -1) {
528 fprintf (stderr, "%s:%d:error parsing extension (%s)\n",
529 filename, line, e.extensions);
530 hdb_free_entry (context, &ent);
531 continue;
534 ret = db->hdb_store(context, db, HDB_F_REPLACE, &ent);
535 hdb_free_entry (context, &ent);
536 if (ret) {
537 krb5_warn(context, ret, "db_store");
538 break;
541 db->hdb_close(context, db);
542 fclose(f);
543 return ret != 0;
547 extern int local_flag;
549 static int
550 loadit(int mergep, const char *name, int argc, char **argv)
552 if(!local_flag) {
553 krb5_warnx(context, "%s is only available in local (-l) mode", name);
554 return 0;
557 return doit(argv[0], mergep);
561 load(void *opt, int argc, char **argv)
563 return loadit(0, "load", argc, argv);
567 merge(void *opt, int argc, char **argv)
569 return loadit(1, "merge", argc, argv);