sqlite: Use 0600
[heimdal.git] / kadmin / load.c
blob971c24793c05d11d5efb1a386b9a70a07f30ac27
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 <limits.h>
36 #include "kadmin_locl.h"
37 #include "kadmin-commands.h"
38 #include <kadm5/private.h>
40 struct entry {
41 char *principal;
42 char *key;
43 char *max_life;
44 char *max_renew;
45 char *created;
46 char *modified;
47 char *valid_start;
48 char *valid_end;
49 char *pw_end;
50 char *flags;
51 char *generation;
52 char *extensions;
55 static char *
56 skip_next(char *p)
58 while(*p && !isspace((unsigned char)*p))
59 p++;
60 *p++ = 0;
61 while(*p && isspace((unsigned char)*p))
62 p++;
63 return p;
67 * Parse the time in `s', returning:
68 * -1 if error parsing
69 * 0 if none present
70 * 1 if parsed ok
73 static int
74 parse_time_string(time_t *t, const char *s)
76 int year, month, date, hour, minute, second;
77 struct tm tm;
79 if(strcmp(s, "-") == 0)
80 return 0;
81 if(sscanf(s, "%04d%02d%02d%02d%02d%02d",
82 &year, &month, &date, &hour, &minute, &second) != 6)
83 return -1;
84 tm.tm_year = year - 1900;
85 tm.tm_mon = month - 1;
86 tm.tm_mday = date;
87 tm.tm_hour = hour;
88 tm.tm_min = minute;
89 tm.tm_sec = second;
90 tm.tm_isdst = 0;
91 *t = timegm(&tm);
92 return 1;
96 * parse time, allocating space in *t if it's there
99 static int
100 parse_time_string_alloc (time_t **t, const char *s)
102 time_t tmp;
103 int ret;
105 *t = NULL;
106 ret = parse_time_string (&tmp, s);
107 if (ret == 1) {
108 *t = malloc (sizeof (**t));
109 if (*t == NULL)
110 krb5_errx (context, 1, "malloc: out of memory");
111 **t = tmp;
113 return ret;
117 * see parse_time_string for calling convention
120 static int
121 parse_integer(unsigned int *u, const char *s)
123 if(strcmp(s, "-") == 0)
124 return 0;
125 if (sscanf(s, "%u", u) != 1)
126 return -1;
127 return 1;
130 static int
131 parse_integer_alloc (unsigned int **u, const char *s)
133 unsigned int tmp;
134 int ret;
136 *u = NULL;
137 ret = parse_integer (&tmp, s);
138 if (ret == 1) {
139 *u = malloc (sizeof (**u));
140 if (*u == NULL)
141 krb5_errx (context, 1, "malloc: out of memory");
142 **u = tmp;
144 return ret;
148 * Parse dumped keys in `str' and store them in `ent'
149 * return -1 if parsing failed
152 static int
153 parse_keys(hdb_entry *ent, char *str)
155 krb5_error_code ret;
156 int tmp;
157 char *p;
158 size_t i;
160 p = strsep(&str, ":");
161 if (sscanf(p, "%d", &tmp) != 1)
162 return 1;
163 ent->kvno = tmp;
164 p = strsep(&str, ":");
165 while(p){
166 Key *key;
167 key = realloc(ent->keys.val,
168 (ent->keys.len + 1) * sizeof(*ent->keys.val));
169 if(key == NULL)
170 krb5_errx (context, 1, "realloc: out of memory");
171 ent->keys.val = key;
172 key = ent->keys.val + ent->keys.len;
173 ent->keys.len++;
174 memset(key, 0, sizeof(*key));
175 if(sscanf(p, "%d", &tmp) == 1) {
176 key->mkvno = malloc(sizeof(*key->mkvno));
177 *key->mkvno = tmp;
178 } else
179 key->mkvno = NULL;
180 p = strsep(&str, ":");
181 if (sscanf(p, "%d", &tmp) != 1)
182 return 1;
183 key->key.keytype = tmp;
184 p = strsep(&str, ":");
185 ret = krb5_data_alloc(&key->key.keyvalue, (strlen(p) - 1) / 2 + 1);
186 if (ret)
187 krb5_err (context, 1, ret, "krb5_data_alloc");
188 for(i = 0; i < strlen(p); i += 2) {
189 if(sscanf(p + i, "%02x", &tmp) != 1)
190 return 1;
191 ((u_char*)key->key.keyvalue.data)[i / 2] = tmp;
193 p = strsep(&str, ":");
194 if(strcmp(p, "-") != 0){
195 unsigned type;
196 size_t p_len;
198 if(sscanf(p, "%u/", &type) != 1)
199 return 1;
200 p = strchr(p, '/');
201 if(p == NULL)
202 return 1;
203 p++;
204 p_len = strlen(p);
206 key->salt = calloc(1, sizeof(*key->salt));
207 if (key->salt == NULL)
208 krb5_errx (context, 1, "malloc: out of memory");
209 key->salt->type = type;
211 if (p_len) {
212 if(*p == '\"') {
213 ret = krb5_data_copy(&key->salt->salt, p + 1, p_len - 2);
214 if (ret)
215 krb5_err (context, 1, ret, "krb5_data_copy");
216 } else {
217 ret = krb5_data_alloc(&key->salt->salt,
218 (p_len - 1) / 2 + 1);
219 if (ret)
220 krb5_err (context, 1, ret, "krb5_data_alloc");
221 for(i = 0; i < p_len; i += 2){
222 if (sscanf(p + i, "%02x", &tmp) != 1)
223 return 1;
224 ((u_char*)key->salt->salt.data)[i / 2] = tmp;
227 } else
228 krb5_data_zero (&key->salt->salt);
230 p = strsep(&str, ":");
232 return 0;
236 * see parse_time_string for calling convention
239 static int
240 parse_event(Event *ev, char *s)
242 krb5_error_code ret;
243 char *p;
245 if(strcmp(s, "-") == 0)
246 return 0;
247 memset(ev, 0, sizeof(*ev));
248 p = strsep(&s, ":");
249 if(parse_time_string(&ev->time, p) != 1)
250 return -1;
251 p = strsep(&s, ":");
252 ret = krb5_parse_name(context, p, &ev->principal);
253 if (ret)
254 return -1;
255 return 1;
258 static int
259 parse_event_alloc (Event **ev, char *s)
261 Event tmp;
262 int ret;
264 *ev = NULL;
265 ret = parse_event (&tmp, s);
266 if (ret == 1) {
267 *ev = malloc (sizeof (**ev));
268 if (*ev == NULL)
269 krb5_errx (context, 1, "malloc: out of memory");
270 **ev = tmp;
272 return ret;
275 static int
276 parse_hdbflags2int(HDBFlags *f, const char *s)
278 int ret;
279 unsigned int tmp;
281 ret = parse_integer (&tmp, s);
282 if (ret == 1)
283 *f = int2HDBFlags (tmp);
284 return ret;
287 static int
288 parse_generation(char *str, GENERATION **gen)
290 char *p;
291 int v;
293 if(strcmp(str, "-") == 0 || *str == '\0') {
294 *gen = NULL;
295 return 0;
297 *gen = calloc(1, sizeof(**gen));
299 p = strsep(&str, ":");
300 if(parse_time_string(&(*gen)->time, p) != 1)
301 return -1;
302 p = strsep(&str, ":");
303 if(sscanf(p, "%d", &v) != 1)
304 return -1;
305 (*gen)->usec = v;
306 p = strsep(&str, ":");
307 if(sscanf(p, "%d", &v) != 1)
308 return -1;
309 (*gen)->gen = v - 1; /* XXX gets bumped in _hdb_store */
310 return 0;
313 /* On error modify strp to point to the problem element */
314 static int
315 parse_extensions(char **strp, HDB_extensions **e)
317 char *str = *strp;
318 char *p;
319 int ret;
321 if(strcmp(str, "-") == 0 || *str == '\0') {
322 *e = NULL;
323 return 0;
325 *e = calloc(1, sizeof(**e));
327 p = strsep(&str, ":");
329 while (p) {
330 HDB_extension ext;
331 ssize_t len;
332 void *d;
334 len = strlen(p);
335 d = emalloc(len);
337 len = hex_decode(p, d, len);
338 if (len < 0) {
339 free(d);
340 *strp = p;
341 return -1;
344 ret = decode_HDB_extension(d, len, &ext, NULL);
345 free(d);
346 if (ret) {
347 *strp = p;
348 return -1;
350 d = realloc((*e)->val, ((*e)->len + 1) * sizeof((*e)->val[0]));
351 if (d == NULL)
352 abort();
353 (*e)->val = d;
354 (*e)->val[(*e)->len] = ext;
355 (*e)->len++;
357 p = strsep(&str, ":");
360 return 0;
363 /* XXX: Principal names with '\n' cannot be dumped or loaded */
364 static int
365 my_fgetln(FILE *f, char **bufp, size_t *szp, size_t *lenp)
367 size_t len;
368 size_t sz = *szp;
369 char *buf = *bufp;
370 char *p, *n;
372 if (!buf) {
373 buf = malloc(sz ? sz : 8192);
374 if (!buf)
375 return ENOMEM;
376 if (!sz)
377 sz = 8192;
380 len = 0;
381 while ((p = fgets(&buf[len], sz-len, f)) != NULL) {
382 len += strlen(&buf[len]);
383 if (buf[len-1] == '\n')
384 break;
385 if (feof(f))
386 break;
387 if (sz > SIZE_MAX/2 ||
388 (n = realloc(buf, sz += 1 + (sz >> 1))) == NULL) {
389 free(buf);
390 *bufp = NULL;
391 *szp = 0;
392 *lenp = 0;
393 return ENOMEM;
395 buf = n;
397 *bufp = buf;
398 *szp = sz;
399 *lenp = len;
400 return 0; /* *len == 0 || no EOL -> EOF */
404 * Parse the dump file in `filename' and create the database (merging
405 * iff merge)
408 static int
409 doit(const char *filename, int mergep)
411 krb5_error_code ret = 0;
412 krb5_error_code ret2 = 0;
413 FILE *f;
414 char *line = NULL;
415 size_t linesz = 0;
416 size_t linelen = 0;
417 char *p;
418 int lineno;
419 int flags = O_RDWR;
420 struct entry e;
421 hdb_entry_ex ent;
422 HDB *db = _kadm5_s_get_db(kadm_handle);
424 f = fopen(filename, "r");
425 if (f == NULL) {
426 krb5_warn(context, errno, "fopen(%s)", filename);
427 return 1;
430 * We don't have a version number in the dump, so we don't know which iprop
431 * log entries to keep, if any. We throw the log away.
433 * We could merge the ipropd-master/slave dump/load here as an option, in
434 * which case we would first load the dump.
436 * If we're merging, first recover unconfirmed records in the existing log.
438 if (mergep)
439 ret = kadm5_log_init(kadm_handle);
440 if (ret == 0)
441 ret = kadm5_log_reinit(kadm_handle, 0);
442 if (ret) {
443 fclose (f);
444 krb5_warn(context, ret, "kadm5_log_reinit");
445 return 1;
448 if (!mergep)
449 flags |= O_CREAT | O_TRUNC;
450 ret = db->hdb_open(context, db, flags, 0600);
451 if (ret){
452 krb5_warn(context, ret, "hdb_open");
453 fclose(f);
454 return 1;
456 (void) db->hdb_set_sync(context, db, 0);
457 for (lineno = 1;
458 (ret2 = my_fgetln(f, &line, &linesz, &linelen)) == 0 && linelen > 0;
459 ++lineno) {
460 p = line;
461 while (isspace((unsigned char)*p))
462 p++;
464 e.principal = p;
465 for (p = line; *p; p++){
466 if (*p == '\\') /* Support '\n' escapes??? */
467 p++;
468 else if (isspace((unsigned char)*p)) {
469 *p = 0;
470 break;
473 p = skip_next(p);
475 e.key = p;
476 p = skip_next(p);
478 e.created = p;
479 p = skip_next(p);
481 e.modified = p;
482 p = skip_next(p);
484 e.valid_start = p;
485 p = skip_next(p);
487 e.valid_end = p;
488 p = skip_next(p);
490 e.pw_end = p;
491 p = skip_next(p);
493 e.max_life = p;
494 p = skip_next(p);
496 e.max_renew = p;
497 p = skip_next(p);
499 e.flags = p;
500 p = skip_next(p);
502 e.generation = p;
503 p = skip_next(p);
505 e.extensions = p;
506 skip_next(p);
508 memset(&ent, 0, sizeof(ent));
509 ret2 = krb5_parse_name(context, e.principal, &ent.entry.principal);
510 if (ret2) {
511 const char *msg = krb5_get_error_message(context, ret);
512 fprintf(stderr, "%s:%d:%s (%s)\n",
513 filename, lineno, msg, e.principal);
514 krb5_free_error_message(context, msg);
515 ret = 1;
516 continue;
519 if (parse_keys(&ent.entry, e.key)) {
520 fprintf (stderr, "%s:%d:error parsing keys (%s)\n",
521 filename, lineno, e.key);
522 hdb_free_entry (context, &ent);
523 ret = 1;
524 continue;
527 if (parse_event(&ent.entry.created_by, e.created) == -1) {
528 fprintf (stderr, "%s:%d:error parsing created event (%s)\n",
529 filename, lineno, e.created);
530 hdb_free_entry (context, &ent);
531 ret = 1;
532 continue;
534 if (parse_event_alloc (&ent.entry.modified_by, e.modified) == -1) {
535 fprintf (stderr, "%s:%d:error parsing event (%s)\n",
536 filename, lineno, e.modified);
537 hdb_free_entry (context, &ent);
538 ret = 1;
539 continue;
541 if (parse_time_string_alloc (&ent.entry.valid_start, e.valid_start) == -1) {
542 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
543 filename, lineno, e.valid_start);
544 hdb_free_entry (context, &ent);
545 ret = 1;
546 continue;
548 if (parse_time_string_alloc (&ent.entry.valid_end, e.valid_end) == -1) {
549 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
550 filename, lineno, e.valid_end);
551 hdb_free_entry (context, &ent);
552 ret = 1;
553 continue;
555 if (parse_time_string_alloc (&ent.entry.pw_end, e.pw_end) == -1) {
556 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
557 filename, lineno, e.pw_end);
558 hdb_free_entry (context, &ent);
559 ret = 1;
560 continue;
563 if (parse_integer_alloc (&ent.entry.max_life, e.max_life) == -1) {
564 fprintf (stderr, "%s:%d:error parsing lifetime (%s)\n",
565 filename, lineno, e.max_life);
566 hdb_free_entry (context, &ent);
567 ret = 1;
568 continue;
571 if (parse_integer_alloc (&ent.entry.max_renew, e.max_renew) == -1) {
572 fprintf (stderr, "%s:%d:error parsing lifetime (%s)\n",
573 filename, lineno, e.max_renew);
574 hdb_free_entry (context, &ent);
575 ret = 1;
576 continue;
579 if (parse_hdbflags2int (&ent.entry.flags, e.flags) != 1) {
580 fprintf (stderr, "%s:%d:error parsing flags (%s)\n",
581 filename, lineno, e.flags);
582 hdb_free_entry (context, &ent);
583 ret = 1;
584 continue;
587 if(parse_generation(e.generation, &ent.entry.generation) == -1) {
588 fprintf (stderr, "%s:%d:error parsing generation (%s)\n",
589 filename, lineno, e.generation);
590 hdb_free_entry (context, &ent);
591 ret = 1;
592 continue;
595 if (parse_extensions(&e.extensions, &ent.entry.extensions) == -1) {
596 fprintf (stderr, "%s:%d:error parsing extension (%s)\n",
597 filename, lineno, e.extensions);
598 hdb_free_entry (context, &ent);
599 ret = 1;
600 continue;
603 ret2 = db->hdb_store(context, db, HDB_F_REPLACE, &ent);
604 hdb_free_entry (context, &ent);
605 if (ret2) {
606 krb5_warn(context, ret2, "db_store");
607 break;
610 free(line);
611 if (ret2)
612 ret = ret2;
613 ret2 = db->hdb_set_sync(context, db, 1);
614 if (ret2) {
615 krb5_err(context, 1, ret2, "failed to sync the HDB");
616 ret = ret2;
618 (void) kadm5_log_end(kadm_handle);
619 ret2 = db->hdb_close(context, db);
620 if (ret2)
621 ret = ret2;
622 fclose(f);
623 return ret != 0;
627 extern int local_flag;
629 static int
630 loadit(int mergep, const char *name, int argc, char **argv)
632 if(!local_flag) {
633 krb5_warnx(context, "%s is only available in local (-l) mode", name);
634 return 0;
637 return doit(argv[0], mergep);
641 load(void *opt, int argc, char **argv)
643 return loadit(0, "load", argc, argv);
647 merge(void *opt, int argc, char **argv)
649 return loadit(1, "merge", argc, argv);