1178825Sdfr/*
2233294Sstas * Copyright (c) 1997-2007 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7178825Sdfr *
8233294Sstas * Redistribution and use in source and binary forms, with or without
9233294Sstas * modification, are permitted provided that the following conditions
10233294Sstas * are met:
11178825Sdfr *
12233294Sstas * 1. Redistributions of source code must retain the above copyright
13233294Sstas *    notice, this list of conditions and the following disclaimer.
14178825Sdfr *
15233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer in the
17233294Sstas *    documentation and/or other materials provided with the distribution.
18178825Sdfr *
19233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
20233294Sstas *    may be used to endorse or promote products derived from this software
21233294Sstas *    without specific prior written permission.
22178825Sdfr *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233294Sstas * SUCH DAMAGE.
34178825Sdfr */
35178825Sdfr
36178825Sdfr#include "kdc_locl.h"
37178825Sdfr
38233294Sstasstatic krb5_error_code
39233294Sstasadd_db(krb5_context context, struct krb5_kdc_configuration *c,
40233294Sstas       const char *conf, const char *master_key)
41233294Sstas{
42233294Sstas    krb5_error_code ret;
43233294Sstas    void *ptr;
44178825Sdfr
45233294Sstas    ptr = realloc(c->db, (c->num_db + 1) * sizeof(*c->db));
46233294Sstas    if (ptr == NULL) {
47233294Sstas	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
48233294Sstas	return ENOMEM;
49233294Sstas    }
50233294Sstas    c->db = ptr;
51233294Sstas
52233294Sstas    ret = hdb_create(context, &c->db[c->num_db], conf);
53233294Sstas    if(ret)
54233294Sstas	return ret;
55233294Sstas
56233294Sstas    c->num_db++;
57233294Sstas
58233294Sstas    if (master_key) {
59233294Sstas	ret = hdb_set_master_keyfile(context, c->db[c->num_db - 1], master_key);
60233294Sstas	if (ret)
61233294Sstas	    return ret;
62233294Sstas    }
63233294Sstas
64233294Sstas    return 0;
65233294Sstas}
66233294Sstas
67178825Sdfrkrb5_error_code
68178825Sdfrkrb5_kdc_set_dbinfo(krb5_context context, struct krb5_kdc_configuration *c)
69178825Sdfr{
70178825Sdfr    struct hdb_dbinfo *info, *d;
71178825Sdfr    krb5_error_code ret;
72178825Sdfr    int i;
73178825Sdfr
74178825Sdfr    /* fetch the databases */
75178825Sdfr    ret = hdb_get_dbinfo(context, &info);
76178825Sdfr    if (ret)
77178825Sdfr	return ret;
78233294Sstas
79178825Sdfr    d = NULL;
80178825Sdfr    while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
81233294Sstas
82233294Sstas	ret = add_db(context, c,
83233294Sstas		     hdb_dbinfo_get_dbname(context, d),
84233294Sstas		     hdb_dbinfo_get_mkey_file(context, d));
85178825Sdfr	if (ret)
86178825Sdfr	    goto out;
87178825Sdfr
88178825Sdfr	kdc_log(context, c, 0, "label: %s",
89178825Sdfr		hdb_dbinfo_get_label(context, d));
90178825Sdfr	kdc_log(context, c, 0, "\tdbname: %s",
91178825Sdfr		hdb_dbinfo_get_dbname(context, d));
92178825Sdfr	kdc_log(context, c, 0, "\tmkey_file: %s",
93178825Sdfr		hdb_dbinfo_get_mkey_file(context, d));
94178825Sdfr	kdc_log(context, c, 0, "\tacl_file: %s",
95178825Sdfr		hdb_dbinfo_get_acl_file(context, d));
96178825Sdfr    }
97178825Sdfr    hdb_free_dbinfo(context, &info);
98178825Sdfr
99178825Sdfr    return 0;
100178825Sdfrout:
101178825Sdfr    for (i = 0; i < c->num_db; i++)
102178825Sdfr	if (c->db[i] && c->db[i]->hdb_destroy)
103178825Sdfr	    (*c->db[i]->hdb_destroy)(context, c->db[i]);
104178825Sdfr    c->num_db = 0;
105178825Sdfr    free(c->db);
106178825Sdfr    c->db = NULL;
107233294Sstas
108178825Sdfr    hdb_free_dbinfo(context, &info);
109178825Sdfr
110178825Sdfr    return ret;
111178825Sdfr}
112178825Sdfr
113178825Sdfr
114