/*1* Copyright (c) 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "hprop.h"3435extern krb5_error_code _hdb_mdb_value2entry(krb5_context context,36krb5_data *data,37krb5_kvno target_kvno,38hdb_entry *entry);3940extern int _hdb_mit_dump2mitdb_entry(krb5_context context,41char *line,42krb5_storage *sp);43444546/*47can have any number of princ stanzas.48format is as follows (only \n indicates newlines)49princ\t%d\t (%d is KRB5_KDB_V1_BASE_LENGTH, always 38)50%d\t (strlen of principal e.g. shadow/[email protected])51%d\t (number of tl_data)52%d\t (number of key data, e.g. how many keys for this user)53%d\t (extra data length)54%s\t (principal name)55%d\t (attributes)56%d\t (max lifetime, seconds)57%d\t (max renewable life, seconds)58%d\t (expiration, seconds since epoch or 2145830400 for never)59%d\t (password expiration, seconds, 0 for never)60%d\t (last successful auth, seconds since epoch)61%d\t (last failed auth, per above)62%d\t (failed auth count)63foreach tl_data 0 to number of tl_data - 1 as above64%d\t%d\t (data type, data length)65foreach tl_data 0 to length-166%02x (tl data contents[element n])67except if tl_data length is 068%d (always -1)69\t70foreach key 0 to number of keys - 1 as above71%d\t%d\t (key data version, kvno)72foreach version 0 to key data version - 1 (a key or a salt)73%d\t%d\t(data type for this key, data length for this key)74foreach key data length 0 to length-175%02x (key data contents[element n])76except if key_data length is 077%d (always -1)78\t79foreach extra data length 0 to length - 180%02x (extra data part)81unless no extra data82%d (always -1)83;\n8485*/8687static char *88nexttoken(char **p)89{90char *q;91do {92q = strsep(p, " \t");93} while(q && *q == '\0');94return q;95}9697#include <kadm5/admin.h>9899static int100my_fgetln(FILE *f, char **buf, size_t *sz, size_t *len)101{102char *p, *n;103104if (!*buf) {105*buf = malloc(*sz ? *sz : 2048);106if (!*buf)107return ENOMEM;108if (!*sz)109*sz = 2048;110}111*len = 0;112while ((p = fgets(&(*buf)[*len], *sz, f))) {113if (strcspn(*buf, "\r\n") || feof(f)) {114*len = strlen(*buf);115return 0;116}117*len += strlen(&(*buf)[*len]); /* *len should be == *sz */118n = realloc(*buf, *sz + (*sz >> 1));119if (!n) {120free(*buf);121*buf = NULL;122*sz = 0;123*len = 0;124return ENOMEM;125}126*buf = n;127*sz += *sz >> 1;128}129return 0; /* *len == 0 || no EOL -> EOF */130}131132int133mit_prop_dump(void *arg, const char *file)134{135krb5_error_code ret;136size_t line_bufsz = 0;137size_t line_len = 0;138char *line = NULL;139int lineno = 0;140FILE *f;141struct hdb_entry_ex ent;142struct prop_data *pd = arg;143krb5_storage *sp = NULL;144krb5_data kdb_ent;145146memset(&ent, 0, sizeof (ent));147f = fopen(file, "r");148if (f == NULL)149return errno;150151ret = ENOMEM;152sp = krb5_storage_emem();153if (!sp)154goto out;155while ((ret = my_fgetln(f, &line, &line_bufsz, &line_len)) == 0) {156char *p = line;157char *q;158lineno++;159160if(strncmp(line, "kdb5_util", strlen("kdb5_util")) == 0) {161int major;162q = nexttoken(&p);163if (strcmp(q, "kdb5_util"))164errx(1, "line %d: unknown version", lineno);165q = nexttoken(&p); /* load_dump */166if (strcmp(q, "load_dump"))167errx(1, "line %d: unknown version", lineno);168q = nexttoken(&p); /* load_dump */169if (strcmp(q, "version"))170errx(1, "line %d: unknown version", lineno);171q = nexttoken(&p); /* x.0 */172if (sscanf(q, "%d", &major) != 1)173errx(1, "line %d: unknown version", lineno);174if (major != 4 && major != 5 && major != 6)175errx(1, "unknown dump file format, got %d, expected 4-6",176major);177continue;178} else if(strncmp(p, "policy", strlen("policy")) == 0) {179warnx("line: %d: ignoring policy (not supported)", lineno);180continue;181} else if(strncmp(p, "princ", strlen("princ")) != 0) {182warnx("line %d: not a principal", lineno);183continue;184}185krb5_storage_truncate(sp, 0);186ret = _hdb_mit_dump2mitdb_entry(pd->context, line, sp);187if (ret) break;188ret = krb5_storage_to_data(sp, &kdb_ent);189if (ret) break;190ret = _hdb_mdb_value2entry(pd->context, &kdb_ent, 0, &ent.entry);191krb5_data_free(&kdb_ent);192if (ret) break;193ret = v5_prop(pd->context, NULL, &ent, arg);194hdb_free_entry(pd->context, &ent);195if (ret) break;196}197198out:199fclose(f);200free(line);201if (sp)202krb5_storage_free(sp);203if (ret && ret == ENOMEM)204errx(1, "out of memory");205if (ret)206errx(1, "line %d: problem parsing dump line", lineno);207return ret;208}209210211212