xref: /freebsd/crypto/heimdal/lib/hdb/dbinfo.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * Copyright (c) 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "hdb_locl.h"
35 
36 struct hdb_dbinfo {
37     char *label;
38     char *realm;
39     char *dbname;
40     char *mkey_file;
41     char *acl_file;
42     char *log_file;
43     const krb5_config_binding *binding;
44     struct hdb_dbinfo *next;
45 };
46 
47 static int
48 get_dbinfo(krb5_context context,
49 	   const krb5_config_binding *db_binding,
50 	   const char *label,
51 	   struct hdb_dbinfo **db)
52 {
53     struct hdb_dbinfo *di;
54     const char *p;
55 
56     *db = NULL;
57 
58     p = krb5_config_get_string(context, db_binding, "dbname", NULL);
59     if(p == NULL)
60 	return 0;
61 
62     di = calloc(1, sizeof(*di));
63     if (di == NULL) {
64 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
65 	return ENOMEM;
66     }
67     di->label = strdup(label);
68     di->dbname = strdup(p);
69 
70     p = krb5_config_get_string(context, db_binding, "realm", NULL);
71     if(p)
72 	di->realm = strdup(p);
73     p = krb5_config_get_string(context, db_binding, "mkey_file", NULL);
74     if(p)
75 	di->mkey_file = strdup(p);
76     p = krb5_config_get_string(context, db_binding, "acl_file", NULL);
77     if(p)
78 	di->acl_file = strdup(p);
79     p = krb5_config_get_string(context, db_binding, "log_file", NULL);
80     if(p)
81 	di->log_file = strdup(p);
82 
83     di->binding = db_binding;
84 
85     *db = di;
86     return 0;
87 }
88 
89 
90 int
91 hdb_get_dbinfo(krb5_context context, struct hdb_dbinfo **dbp)
92 {
93     const krb5_config_binding *db_binding;
94     struct hdb_dbinfo *di, **dt, *databases;
95     const char *default_dbname = HDB_DEFAULT_DB;
96     const char *default_mkey = HDB_DB_DIR "/m-key";
97     const char *default_acl = HDB_DB_DIR "/kadmind.acl";
98     const char *p;
99     int ret;
100 
101     *dbp = NULL;
102     dt = NULL;
103     databases = NULL;
104 
105     db_binding = krb5_config_get_list(context, NULL,
106 				      "kdc",
107 				      "database",
108 				      NULL);
109     if (db_binding) {
110 
111 	ret = get_dbinfo(context, db_binding, "default", &di);
112 	if (ret == 0 && di) {
113 	    databases = di;
114 	    dt = &di->next;
115 	}
116 
117 	for ( ; db_binding != NULL; db_binding = db_binding->next) {
118 
119 	    if (db_binding->type != krb5_config_list)
120 		continue;
121 
122 	    ret = get_dbinfo(context, db_binding->u.list,
123 			     db_binding->name, &di);
124 	    if (ret)
125 		krb5_err(context, 1, ret, "failed getting realm");
126 
127 	    if (di == NULL)
128 		continue;
129 
130 	    if (dt)
131 		*dt = di;
132 	    else
133 		databases = di;
134 	    dt = &di->next;
135 
136 	}
137     }
138 
139     if(databases == NULL) {
140 	/* if there are none specified, create one and use defaults */
141 	di = calloc(1, sizeof(*di));
142 	databases = di;
143 	di->label = strdup("default");
144     }
145 
146     for(di = databases; di; di = di->next) {
147 	if(di->dbname == NULL) {
148 	    di->dbname = strdup(default_dbname);
149 	    if (di->mkey_file == NULL)
150 		di->mkey_file = strdup(default_mkey);
151 	}
152 	if(di->mkey_file == NULL) {
153 	    p = strrchr(di->dbname, '.');
154 	    if(p == NULL || strchr(p, '/') != NULL)
155 		/* final pathname component does not contain a . */
156 		asprintf(&di->mkey_file, "%s.mkey", di->dbname);
157 	    else
158 		/* the filename is something.else, replace .else with
159                    .mkey */
160 		asprintf(&di->mkey_file, "%.*s.mkey",
161 			 (int)(p - di->dbname), di->dbname);
162 	}
163 	if(di->acl_file == NULL)
164 	    di->acl_file = strdup(default_acl);
165     }
166     *dbp = databases;
167     return 0;
168 }
169 
170 
171 struct hdb_dbinfo *
172 hdb_dbinfo_get_next(struct hdb_dbinfo *dbp, struct hdb_dbinfo *dbprevp)
173 {
174     if (dbprevp == NULL)
175 	return dbp;
176     else
177 	return dbprevp->next;
178 }
179 
180 const char *
181 hdb_dbinfo_get_label(krb5_context context, struct hdb_dbinfo *dbp)
182 {
183     return dbp->label;
184 }
185 
186 const char *
187 hdb_dbinfo_get_realm(krb5_context context, struct hdb_dbinfo *dbp)
188 {
189     return dbp->realm;
190 }
191 
192 const char *
193 hdb_dbinfo_get_dbname(krb5_context context, struct hdb_dbinfo *dbp)
194 {
195     return dbp->dbname;
196 }
197 
198 const char *
199 hdb_dbinfo_get_mkey_file(krb5_context context, struct hdb_dbinfo *dbp)
200 {
201     return dbp->mkey_file;
202 }
203 
204 const char *
205 hdb_dbinfo_get_acl_file(krb5_context context, struct hdb_dbinfo *dbp)
206 {
207     return dbp->acl_file;
208 }
209 
210 const char *
211 hdb_dbinfo_get_log_file(krb5_context context, struct hdb_dbinfo *dbp)
212 {
213     return dbp->log_file;
214 }
215 
216 const krb5_config_binding *
217 hdb_dbinfo_get_binding(krb5_context context, struct hdb_dbinfo *dbp)
218 {
219     return dbp->binding;
220 }
221 
222 void
223 hdb_free_dbinfo(krb5_context context, struct hdb_dbinfo **dbp)
224 {
225     struct hdb_dbinfo *di, *ndi;
226 
227     for(di = *dbp; di != NULL; di = ndi) {
228 	ndi = di->next;
229 	free (di->label);
230 	free (di->realm);
231 	free (di->dbname);
232 	free (di->mkey_file);
233 	free (di->acl_file);
234 	free (di->log_file);
235 	free(di);
236     }
237     *dbp = NULL;
238 }
239 
240 /**
241  * Return the directory where the hdb database resides.
242  *
243  * @param context Kerberos 5 context.
244  *
245  * @return string pointing to directory.
246  */
247 
248 const char *
249 hdb_db_dir(krb5_context context)
250 {
251     return HDB_DB_DIR;
252 }
253 
254 /**
255  * Return the default hdb database resides.
256  *
257  * @param context Kerberos 5 context.
258  *
259  * @return string pointing to directory.
260  */
261 
262 const char *
263 hdb_default_db(krb5_context context)
264 {
265     return HDB_DEFAULT_DB;
266 }
267