2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "kadmin_locl.h"
35 #include <kadm5/private.h>
37 RCSID("$Id: load.c,v 1.44 2002/09/04 20:44:35 joda Exp $");
56 while(*p && !isspace((unsigned char)*p))
59 while(*p && isspace((unsigned char)*p))
65 * Parse the time in `s', returning:
72 parse_time_string(time_t *t, const char *s)
74 int year, month, date, hour, minute, second;
77 if(strcmp(s, "-") == 0)
79 if(sscanf(s, "%04d%02d%02d%02d%02d%02d",
80 &year, &month, &date, &hour, &minute, &second) != 6)
82 tm.tm_year = year - 1900;
83 tm.tm_mon = month - 1;
94 * parse time, allocating space in *t if it's there
98 parse_time_string_alloc (time_t **t, const char *s)
104 ret = parse_time_string (&tmp, s);
106 *t = malloc (sizeof (**t));
108 krb5_errx (context, 1, "malloc: out of memory");
115 * see parse_time_string for calling convention
119 parse_integer(unsigned *u, const char *s)
121 if(strcmp(s, "-") == 0)
123 if (sscanf(s, "%u", u) != 1)
129 parse_integer_alloc (int **u, const char *s)
135 ret = parse_integer (&tmp, s);
137 *u = malloc (sizeof (**u));
139 krb5_errx (context, 1, "malloc: out of memory");
146 * Parse dumped keys in `str' and store them in `ent'
147 * return -1 if parsing failed
151 parse_keys(hdb_entry *ent, char *str)
158 p = strsep(&str, ":");
159 if (sscanf(p, "%d", &tmp) != 1)
162 p = strsep(&str, ":");
165 key = realloc(ent->keys.val,
166 (ent->keys.len + 1) * sizeof(*ent->keys.val));
168 krb5_errx (context, 1, "realloc: out of memory");
170 key = ent->keys.val + ent->keys.len;
172 memset(key, 0, sizeof(*key));
173 if(sscanf(p, "%d", &tmp) == 1) {
174 key->mkvno = malloc(sizeof(*key->mkvno));
178 p = strsep(&str, ":");
179 if (sscanf(p, "%d", &tmp) != 1)
181 key->key.keytype = tmp;
182 p = strsep(&str, ":");
183 ret = krb5_data_alloc(&key->key.keyvalue, (strlen(p) - 1) / 2 + 1);
185 krb5_err (context, 1, ret, "krb5_data_alloc");
186 for(i = 0; i < strlen(p); i += 2) {
187 if(sscanf(p + i, "%02x", &tmp) != 1)
189 ((u_char*)key->key.keyvalue.data)[i / 2] = tmp;
191 p = strsep(&str, ":");
192 if(strcmp(p, "-") != 0){
196 if(sscanf(p, "%u/", &type) != 1)
204 key->salt = malloc(sizeof(*key->salt));
205 if (key->salt == NULL)
206 krb5_errx (context, 1, "malloc: out of memory");
207 key->salt->type = type;
211 ret = krb5_data_copy(&key->salt->salt, p + 1, p_len - 2);
213 krb5_err (context, 1, ret, "krb5_data_copy");
215 ret = krb5_data_alloc(&key->salt->salt,
216 (p_len - 1) / 2 + 1);
218 krb5_err (context, 1, ret, "krb5_data_alloc");
219 for(i = 0; i < p_len; i += 2){
220 if (sscanf(p + i, "%02x", &tmp) != 1)
222 ((u_char*)key->salt->salt.data)[i / 2] = tmp;
226 krb5_data_zero (&key->salt->salt);
228 p = strsep(&str, ":");
234 * see parse_time_string for calling convention
238 parse_event(Event *ev, char *s)
243 if(strcmp(s, "-") == 0)
245 memset(ev, 0, sizeof(*ev));
247 if(parse_time_string(&ev->time, p) != 1)
250 ret = krb5_parse_name(context, p, &ev->principal);
257 parse_event_alloc (Event **ev, char *s)
263 ret = parse_event (&tmp, s);
265 *ev = malloc (sizeof (**ev));
267 krb5_errx (context, 1, "malloc: out of memory");
274 parse_hdbflags2int(HDBFlags *f, const char *s)
279 ret = parse_integer (&tmp, s);
281 *f = int2HDBFlags (tmp);
286 parse_generation(char *str, GENERATION **gen)
291 if(strcmp(str, "-") == 0 || *str == '\0') {
295 *gen = calloc(1, sizeof(**gen));
297 p = strsep(&str, ":");
298 if(parse_time_string(&(*gen)->time, p) != 1)
300 p = strsep(&str, ":");
301 if(sscanf(p, "%d", &v) != 1)
304 p = strsep(&str, ":");
305 if(sscanf(p, "%d", &v) != 1)
307 (*gen)->gen = v - 1; /* XXX gets bumped in _hdb_store */
313 * Parse the dump file in `filename' and create the database (merging
318 doit(const char *filename, int merge)
322 char s[8192]; /* XXX should fix this properly */
328 HDB *db = _kadm5_s_get_db(kadm_handle);
330 f = fopen(filename, "r");
332 krb5_warn(context, errno, "fopen(%s)", filename);
335 ret = kadm5_log_truncate (kadm_handle);
338 krb5_warn(context, ret, "kadm5_log_truncate");
343 flags |= O_CREAT | O_TRUNC;
344 ret = db->open(context, db, flags, 0600);
346 krb5_warn(context, ret, "hdb_open");
352 while(fgets(s, sizeof(s), f) != NULL) {
359 else if(isspace((unsigned char)*p)) {
396 memset(&ent, 0, sizeof(ent));
397 ret = krb5_parse_name(context, e.principal, &ent.principal);
399 fprintf(stderr, "%s:%d:%s (%s)\n",
402 krb5_get_err_text(context, ret),
407 if (parse_keys(&ent, e.key)) {
408 fprintf (stderr, "%s:%d:error parsing keys (%s)\n",
409 filename, line, e.key);
410 hdb_free_entry (context, &ent);
414 if (parse_event(&ent.created_by, e.created) == -1) {
415 fprintf (stderr, "%s:%d:error parsing created event (%s)\n",
416 filename, line, e.created);
417 hdb_free_entry (context, &ent);
420 if (parse_event_alloc (&ent.modified_by, e.modified) == -1) {
421 fprintf (stderr, "%s:%d:error parsing event (%s)\n",
422 filename, line, e.modified);
423 hdb_free_entry (context, &ent);
426 if (parse_time_string_alloc (&ent.valid_start, e.valid_start) == -1) {
427 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
428 filename, line, e.valid_start);
429 hdb_free_entry (context, &ent);
432 if (parse_time_string_alloc (&ent.valid_end, e.valid_end) == -1) {
433 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
434 filename, line, e.valid_end);
435 hdb_free_entry (context, &ent);
438 if (parse_time_string_alloc (&ent.pw_end, e.pw_end) == -1) {
439 fprintf (stderr, "%s:%d:error parsing time (%s)\n",
440 filename, line, e.pw_end);
441 hdb_free_entry (context, &ent);
445 if (parse_integer_alloc (&ent.max_life, e.max_life) == -1) {
446 fprintf (stderr, "%s:%d:error parsing lifetime (%s)\n",
447 filename, line, e.max_life);
448 hdb_free_entry (context, &ent);
452 if (parse_integer_alloc (&ent.max_renew, e.max_renew) == -1) {
453 fprintf (stderr, "%s:%d:error parsing lifetime (%s)\n",
454 filename, line, e.max_renew);
455 hdb_free_entry (context, &ent);
459 if (parse_hdbflags2int (&ent.flags, e.flags) != 1) {
460 fprintf (stderr, "%s:%d:error parsing flags (%s)\n",
461 filename, line, e.flags);
462 hdb_free_entry (context, &ent);
466 if(parse_generation(e.generation, &ent.generation) == -1) {
467 fprintf (stderr, "%s:%d:error parsing generation (%s)\n",
468 filename, line, e.generation);
469 hdb_free_entry (context, &ent);
473 ret = db->store(context, db, HDB_F_REPLACE, &ent);
474 hdb_free_entry (context, &ent);
476 krb5_warn(context, ret, "db_store");
480 db->close(context, db);
486 static struct getargs args[] = {
487 { "help", 'h', arg_flag, NULL }
490 static int num_args = sizeof(args) / sizeof(args[0]);
493 usage(const char *name)
495 arg_printusage (args, num_args, name, "file");
501 load(int argc, char **argv)
506 args[0].value = &help_flag;
508 if(getarg(args, num_args, argc, argv, &optind)) {
512 if(argc - optind != 1 || help_flag) {
517 doit(argv[optind], 0);
522 merge(int argc, char **argv)
527 args[0].value = &help_flag;
529 if(getarg(args, num_args, argc, argv, &optind)) {
533 if(argc - optind != 1 || help_flag) {
538 doit(argv[optind], 1);