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