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