1178825Sdfr/*
2178825Sdfr * Copyright (c) 1997-2007 Kungliga Tekniska H�gskolan
3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr *
5178825Sdfr * All rights reserved.
6178825Sdfr *
7178825Sdfr * Redistribution and use in source and binary forms, with or without
8178825Sdfr * modification, are permitted provided that the following conditions
9178825Sdfr * are met:
10178825Sdfr *
11178825Sdfr * 1. Redistributions of source code must retain the above copyright
12178825Sdfr *    notice, this list of conditions and the following disclaimer.
13178825Sdfr *
14178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
15178825Sdfr *    notice, this list of conditions and the following disclaimer in the
16178825Sdfr *    documentation and/or other materials provided with the distribution.
17178825Sdfr *
18178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors
19178825Sdfr *    may be used to endorse or promote products derived from this software
20178825Sdfr *    without specific prior written permission.
21178825Sdfr *
22178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32178825Sdfr * SUCH DAMAGE.
33178825Sdfr */
34178825Sdfr
35178825Sdfr#include "kdc_locl.h"
36178825Sdfr
37178825SdfrRCSID("$Id: default_config.c 21296 2007-06-25 14:49:11Z lha $");
38178825Sdfr
39178825Sdfrkrb5_error_code
40178825Sdfrkrb5_kdc_set_dbinfo(krb5_context context, struct krb5_kdc_configuration *c)
41178825Sdfr{
42178825Sdfr    struct hdb_dbinfo *info, *d;
43178825Sdfr    krb5_error_code ret;
44178825Sdfr    int i;
45178825Sdfr
46178825Sdfr    /* fetch the databases */
47178825Sdfr    ret = hdb_get_dbinfo(context, &info);
48178825Sdfr    if (ret)
49178825Sdfr	return ret;
50178825Sdfr
51178825Sdfr    d = NULL;
52178825Sdfr    while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
53178825Sdfr	void *ptr;
54178825Sdfr
55178825Sdfr	ptr = realloc(c->db, (c->num_db + 1) * sizeof(*c->db));
56178825Sdfr	if (ptr == NULL) {
57178825Sdfr	    ret = ENOMEM;
58178825Sdfr	    krb5_set_error_string(context, "out of memory");
59178825Sdfr	    goto out;
60178825Sdfr	}
61178825Sdfr	c->db = ptr;
62178825Sdfr
63178825Sdfr	ret = hdb_create(context, &c->db[c->num_db],
64178825Sdfr			 hdb_dbinfo_get_dbname(context, d));
65178825Sdfr	if(ret)
66178825Sdfr	    goto out;
67178825Sdfr
68178825Sdfr	ret = hdb_set_master_keyfile(context, c->db[c->num_db],
69178825Sdfr				     hdb_dbinfo_get_mkey_file(context, d));
70178825Sdfr	if (ret)
71178825Sdfr	    goto out;
72178825Sdfr
73178825Sdfr	c->num_db++;
74178825Sdfr
75178825Sdfr	kdc_log(context, c, 0, "label: %s",
76178825Sdfr		hdb_dbinfo_get_label(context, d));
77178825Sdfr	kdc_log(context, c, 0, "\tdbname: %s",
78178825Sdfr		hdb_dbinfo_get_dbname(context, d));
79178825Sdfr	kdc_log(context, c, 0, "\tmkey_file: %s",
80178825Sdfr		hdb_dbinfo_get_mkey_file(context, d));
81178825Sdfr	kdc_log(context, c, 0, "\tacl_file: %s",
82178825Sdfr		hdb_dbinfo_get_acl_file(context, d));
83178825Sdfr    }
84178825Sdfr    hdb_free_dbinfo(context, &info);
85178825Sdfr
86178825Sdfr    return 0;
87178825Sdfrout:
88178825Sdfr    for (i = 0; i < c->num_db; i++)
89178825Sdfr	if (c->db[i] && c->db[i]->hdb_destroy)
90178825Sdfr	    (*c->db[i]->hdb_destroy)(context, c->db[i]);
91178825Sdfr    c->num_db = 0;
92178825Sdfr    free(c->db);
93178825Sdfr    c->db = NULL;
94178825Sdfr
95178825Sdfr    hdb_free_dbinfo(context, &info);
96178825Sdfr
97178825Sdfr    return ret;
98178825Sdfr}
99178825Sdfr
100178825Sdfr
101