set_dbinfo.c revision 256281
1323124Sdes/*
257429Smarkm * Copyright (c) 1997-2007 Kungliga Tekniska H��gskolan
357429Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
457429Smarkm * All rights reserved.
557429Smarkm *
657429Smarkm * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
760576Skris *
865674Skris * Redistribution and use in source and binary forms, with or without
965674Skris * modification, are permitted provided that the following conditions
1065674Skris * are met:
1165674Skris *
1265674Skris * 1. Redistributions of source code must retain the above copyright
1357429Smarkm *    notice, this list of conditions and the following disclaimer.
1457429Smarkm *
1557429Smarkm * 2. Redistributions in binary form must reproduce the above copyright
16162856Sdes *    notice, this list of conditions and the following disclaimer in the
1757429Smarkm *    documentation and/or other materials provided with the distribution.
18162856Sdes *
19162856Sdes * 3. Neither the name of the Institute nor the names of its contributors
20162856Sdes *    may be used to endorse or promote products derived from this software
21181918Sdes *    without specific prior written permission.
22262566Sdes *
23295367Sdes * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24162856Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25162856Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26221420Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27221420Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28264377Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29162856Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30162856Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31162856Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32262566Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33295367Sdes * SUCH DAMAGE.
34162856Sdes */
35262566Sdes
36262566Sdes#include "kdc_locl.h"
37262566Sdes
38262566Sdesstatic krb5_error_code
39162856Sdesadd_db(krb5_context context, struct krb5_kdc_configuration *c,
40162856Sdes       const char *conf, const char *master_key)
41162856Sdes{
42162856Sdes    krb5_error_code ret;
43162856Sdes    void *ptr;
44323124Sdes
45323124Sdes    ptr = realloc(c->db, (c->num_db + 1) * sizeof(*c->db));
46323124Sdes    if (ptr == NULL) {
47323124Sdes	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
48323124Sdes	return ENOMEM;
49255767Sdes    }
50255767Sdes    c->db = ptr;
51255767Sdes
52295367Sdes    ret = hdb_create(context, &c->db[c->num_db], conf);
53295367Sdes    if(ret)
54295367Sdes	return ret;
55162856Sdes
56162856Sdes    c->num_db++;
5757429Smarkm
5876262Sgreen    if (master_key) {
5976262Sgreen	ret = hdb_set_master_keyfile(context, c->db[c->num_db - 1], master_key);
6076262Sgreen	if (ret)
6176262Sgreen	    return ret;
62295367Sdes    }
63295367Sdes
6457429Smarkm    return 0;
6560576Skris}
6676262Sgreen
6776262Sgreenkrb5_error_code
68262566Sdeskrb5_kdc_set_dbinfo(krb5_context context, struct krb5_kdc_configuration *c)
69295367Sdes{
70295367Sdes    struct hdb_dbinfo *info, *d;
71192595Sdes    krb5_error_code ret;
7257429Smarkm    int i;
7357429Smarkm
7457429Smarkm    /* fetch the databases */
7557429Smarkm    ret = hdb_get_dbinfo(context, &info);
7657429Smarkm    if (ret)
7757429Smarkm	return ret;
7857429Smarkm
7957429Smarkm    d = NULL;
8057429Smarkm    while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
8157429Smarkm
8257429Smarkm	ret = add_db(context, c,
8357429Smarkm		     hdb_dbinfo_get_dbname(context, d),
8457429Smarkm		     hdb_dbinfo_get_mkey_file(context, d));
8557429Smarkm	if (ret)
8657429Smarkm	    goto out;
8757429Smarkm
8898684Sdes	kdc_log(context, c, 0, "label: %s",
8957429Smarkm		hdb_dbinfo_get_label(context, d));
9057429Smarkm	kdc_log(context, c, 0, "\tdbname: %s",
9157429Smarkm		hdb_dbinfo_get_dbname(context, d));
9257429Smarkm	kdc_log(context, c, 0, "\tmkey_file: %s",
9357429Smarkm		hdb_dbinfo_get_mkey_file(context, d));
9457429Smarkm	kdc_log(context, c, 0, "\tacl_file: %s",
9557429Smarkm		hdb_dbinfo_get_acl_file(context, d));
9657429Smarkm    }
9757429Smarkm    hdb_free_dbinfo(context, &info);
9857429Smarkm
9957429Smarkm    return 0;
10057429Smarkmout:
10157429Smarkm    for (i = 0; i < c->num_db; i++)
10257429Smarkm	if (c->db[i] && c->db[i]->hdb_destroy)
10357429Smarkm	    (*c->db[i]->hdb_destroy)(context, c->db[i]);
10457429Smarkm    c->num_db = 0;
10557429Smarkm    free(c->db);
10657429Smarkm    c->db = NULL;
10757429Smarkm
10857429Smarkm    hdb_free_dbinfo(context, &info);
10957429Smarkm
11057429Smarkm    return ret;
11198684Sdes}
11257429Smarkm
11357429Smarkm
11457429Smarkm