1*1c9681d1Schristos /*	$NetBSD: set_dbinfo.c,v 1.2 2017/01/28 21:31:44 christos Exp $	*/
2f59d82ffSelric 
3f59d82ffSelric /*
4f59d82ffSelric  * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
5f59d82ffSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric  * All rights reserved.
7f59d82ffSelric  *
8f59d82ffSelric  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9f59d82ffSelric  *
10f59d82ffSelric  * Redistribution and use in source and binary forms, with or without
11f59d82ffSelric  * modification, are permitted provided that the following conditions
12f59d82ffSelric  * are met:
13f59d82ffSelric  *
14f59d82ffSelric  * 1. Redistributions of source code must retain the above copyright
15f59d82ffSelric  *    notice, this list of conditions and the following disclaimer.
16f59d82ffSelric  *
17f59d82ffSelric  * 2. Redistributions in binary form must reproduce the above copyright
18f59d82ffSelric  *    notice, this list of conditions and the following disclaimer in the
19f59d82ffSelric  *    documentation and/or other materials provided with the distribution.
20f59d82ffSelric  *
21f59d82ffSelric  * 3. Neither the name of the Institute nor the names of its contributors
22f59d82ffSelric  *    may be used to endorse or promote products derived from this software
23f59d82ffSelric  *    without specific prior written permission.
24f59d82ffSelric  *
25f59d82ffSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26f59d82ffSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27f59d82ffSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28f59d82ffSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29f59d82ffSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30f59d82ffSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31f59d82ffSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32f59d82ffSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33f59d82ffSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34f59d82ffSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35f59d82ffSelric  * SUCH DAMAGE.
36f59d82ffSelric  */
37f59d82ffSelric 
38f59d82ffSelric #include "kdc_locl.h"
39f59d82ffSelric 
40f59d82ffSelric static krb5_error_code
add_db(krb5_context context,struct krb5_kdc_configuration * c,const char * conf,const char * master_key)41f59d82ffSelric add_db(krb5_context context, struct krb5_kdc_configuration *c,
42f59d82ffSelric        const char *conf, const char *master_key)
43f59d82ffSelric {
44f59d82ffSelric     krb5_error_code ret;
45f59d82ffSelric     void *ptr;
46f59d82ffSelric 
47f59d82ffSelric     ptr = realloc(c->db, (c->num_db + 1) * sizeof(*c->db));
48f59d82ffSelric     if (ptr == NULL) {
49f59d82ffSelric 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
50f59d82ffSelric 	return ENOMEM;
51f59d82ffSelric     }
52f59d82ffSelric     c->db = ptr;
53f59d82ffSelric 
54f59d82ffSelric     ret = hdb_create(context, &c->db[c->num_db], conf);
55f59d82ffSelric     if(ret)
56f59d82ffSelric 	return ret;
57f59d82ffSelric 
58f59d82ffSelric     c->num_db++;
59f59d82ffSelric 
60f59d82ffSelric     if (master_key) {
61f59d82ffSelric 	ret = hdb_set_master_keyfile(context, c->db[c->num_db - 1], master_key);
62f59d82ffSelric 	if (ret)
63f59d82ffSelric 	    return ret;
64f59d82ffSelric     }
65f59d82ffSelric 
66f59d82ffSelric     return 0;
67f59d82ffSelric }
68f59d82ffSelric 
69f59d82ffSelric krb5_error_code
krb5_kdc_set_dbinfo(krb5_context context,struct krb5_kdc_configuration * c)70f59d82ffSelric krb5_kdc_set_dbinfo(krb5_context context, struct krb5_kdc_configuration *c)
71f59d82ffSelric {
72f59d82ffSelric     struct hdb_dbinfo *info, *d;
73f59d82ffSelric     krb5_error_code ret;
74f59d82ffSelric     int i;
75f59d82ffSelric 
76f59d82ffSelric     /* fetch the databases */
77f59d82ffSelric     ret = hdb_get_dbinfo(context, &info);
78f59d82ffSelric     if (ret)
79f59d82ffSelric 	return ret;
80f59d82ffSelric 
81f59d82ffSelric     d = NULL;
82f59d82ffSelric     while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
83f59d82ffSelric 
84f59d82ffSelric 	ret = add_db(context, c,
85f59d82ffSelric 		     hdb_dbinfo_get_dbname(context, d),
86f59d82ffSelric 		     hdb_dbinfo_get_mkey_file(context, d));
87f59d82ffSelric 	if (ret)
88f59d82ffSelric 	    goto out;
89f59d82ffSelric 
90f59d82ffSelric 	kdc_log(context, c, 0, "label: %s",
91f59d82ffSelric 		hdb_dbinfo_get_label(context, d));
92f59d82ffSelric 	kdc_log(context, c, 0, "\tdbname: %s",
93f59d82ffSelric 		hdb_dbinfo_get_dbname(context, d));
94f59d82ffSelric 	kdc_log(context, c, 0, "\tmkey_file: %s",
95f59d82ffSelric 		hdb_dbinfo_get_mkey_file(context, d));
96f59d82ffSelric 	kdc_log(context, c, 0, "\tacl_file: %s",
97f59d82ffSelric 		hdb_dbinfo_get_acl_file(context, d));
98f59d82ffSelric     }
99f59d82ffSelric     hdb_free_dbinfo(context, &info);
100f59d82ffSelric 
101f59d82ffSelric     return 0;
102f59d82ffSelric out:
103f59d82ffSelric     for (i = 0; i < c->num_db; i++)
104f59d82ffSelric 	if (c->db[i] && c->db[i]->hdb_destroy)
105f59d82ffSelric 	    (*c->db[i]->hdb_destroy)(context, c->db[i]);
106f59d82ffSelric     c->num_db = 0;
107f59d82ffSelric     free(c->db);
108f59d82ffSelric     c->db = NULL;
109f59d82ffSelric 
110f59d82ffSelric     hdb_free_dbinfo(context, &info);
111f59d82ffSelric 
112f59d82ffSelric     return ret;
113f59d82ffSelric }
114f59d82ffSelric 
115f59d82ffSelric 
116