xref: /freebsd/crypto/heimdal/lib/hdb/dbinfo.c (revision c19800e8cd5640693f36f2040db4ab5e8d738146)
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 RCSID("$Id: dbinfo.c 22306 2007-12-14 12:22:38Z lha $");
37 
38 struct hdb_dbinfo {
39     char *label;
40     char *realm;
41     char *dbname;
42     char *mkey_file;
43     char *acl_file;
44     char *log_file;
45     const krb5_config_binding *binding;
46     struct hdb_dbinfo *next;
47 };
48 
49 static int
50 get_dbinfo(krb5_context context,
51 	   const krb5_config_binding *db_binding,
52 	   const char *label,
53 	   struct hdb_dbinfo **db)
54 {
55     struct hdb_dbinfo *di;
56     const char *p;
57 
58     *db = NULL;
59 
60     p = krb5_config_get_string(context, db_binding, "dbname", NULL);
61     if(p == NULL)
62 	return 0;
63 
64     di = calloc(1, sizeof(*di));
65     if (di == NULL) {
66 	krb5_set_error_string(context, "malloc: out of memory");
67 	return ENOMEM;
68     }
69     di->label = strdup(label);
70     di->dbname = strdup(p);
71 
72     p = krb5_config_get_string(context, db_binding, "realm", NULL);
73     if(p)
74 	di->realm = strdup(p);
75     p = krb5_config_get_string(context, db_binding, "mkey_file", NULL);
76     if(p)
77 	di->mkey_file = strdup(p);
78     p = krb5_config_get_string(context, db_binding, "acl_file", NULL);
79     if(p)
80 	di->acl_file = strdup(p);
81     p = krb5_config_get_string(context, db_binding, "log_file", NULL);
82     if(p)
83 	di->log_file = strdup(p);
84 
85     di->binding = db_binding;
86 
87     *db = di;
88     return 0;
89 }
90 
91 
92 int
93 hdb_get_dbinfo(krb5_context context, struct hdb_dbinfo **dbp)
94 {
95     const krb5_config_binding *db_binding;
96     struct hdb_dbinfo *di, **dt, *databases;
97     const char *default_dbname = HDB_DEFAULT_DB;
98     const char *default_mkey = HDB_DB_DIR "/m-key";
99     const char *default_acl = HDB_DB_DIR "/kadmind.acl";
100     const char *p;
101     int ret;
102 
103     *dbp = NULL;
104     dt = NULL;
105     databases = NULL;
106 
107     db_binding = krb5_config_get(context, NULL, krb5_config_list,
108 				 "kdc",
109 				 "database",
110 				 NULL);
111     if (db_binding) {
112 
113 	ret = get_dbinfo(context, db_binding, "default", &di);
114 	if (ret == 0 && di) {
115 	    databases = di;
116 	    dt = &di->next;
117 	}
118 
119 	for ( ; db_binding != NULL; db_binding = db_binding->next) {
120 
121 	    if (db_binding->type != krb5_config_list)
122 		continue;
123 
124 	    ret = get_dbinfo(context, db_binding->u.list,
125 			     db_binding->name, &di);
126 	    if (ret)
127 		krb5_err(context, 1, ret, "failed getting realm");
128 
129 	    if (di == NULL)
130 		continue;
131 
132 	    if (dt)
133 		*dt = di;
134 	    else
135 		databases = di;
136 	    dt = &di->next;
137 
138 	}
139     }
140 
141     if(databases == NULL) {
142 	/* if there are none specified, create one and use defaults */
143 	di = calloc(1, sizeof(*di));
144 	databases = di;
145 	di->label = strdup("default");
146     }
147 
148     for(di = databases; di; di = di->next) {
149 	if(di->dbname == NULL) {
150 	    di->dbname = strdup(default_dbname);
151 	    if (di->mkey_file == NULL)
152 		di->mkey_file = strdup(default_mkey);
153 	}
154 	if(di->mkey_file == NULL) {
155 	    p = strrchr(di->dbname, '.');
156 	    if(p == NULL || strchr(p, '/') != NULL)
157 		/* final pathname component does not contain a . */
158 		asprintf(&di->mkey_file, "%s.mkey", di->dbname);
159 	    else
160 		/* the filename is something.else, replace .else with
161                    .mkey */
162 		asprintf(&di->mkey_file, "%.*s.mkey",
163 			 (int)(p - di->dbname), di->dbname);
164 	}
165 	if(di->acl_file == NULL)
166 	    di->acl_file = strdup(default_acl);
167     }
168     *dbp = databases;
169     return 0;
170 }
171 
172 
173 struct hdb_dbinfo *
174 hdb_dbinfo_get_next(struct hdb_dbinfo *dbp, struct hdb_dbinfo *dbprevp)
175 {
176     if (dbprevp == NULL)
177 	return dbp;
178     else
179 	return dbprevp->next;
180 }
181 
182 const char *
183 hdb_dbinfo_get_label(krb5_context context, struct hdb_dbinfo *dbp)
184 {
185     return dbp->label;
186 }
187 
188 const char *
189 hdb_dbinfo_get_realm(krb5_context context, struct hdb_dbinfo *dbp)
190 {
191     return dbp->realm;
192 }
193 
194 const char *
195 hdb_dbinfo_get_dbname(krb5_context context, struct hdb_dbinfo *dbp)
196 {
197     return dbp->dbname;
198 }
199 
200 const char *
201 hdb_dbinfo_get_mkey_file(krb5_context context, struct hdb_dbinfo *dbp)
202 {
203     return dbp->mkey_file;
204 }
205 
206 const char *
207 hdb_dbinfo_get_acl_file(krb5_context context, struct hdb_dbinfo *dbp)
208 {
209     return dbp->acl_file;
210 }
211 
212 const char *
213 hdb_dbinfo_get_log_file(krb5_context context, struct hdb_dbinfo *dbp)
214 {
215     return dbp->log_file;
216 }
217 
218 const krb5_config_binding *
219 hdb_dbinfo_get_binding(krb5_context context, struct hdb_dbinfo *dbp)
220 {
221     return dbp->binding;
222 }
223 
224 void
225 hdb_free_dbinfo(krb5_context context, struct hdb_dbinfo **dbp)
226 {
227     struct hdb_dbinfo *di, *ndi;
228 
229     for(di = *dbp; di != NULL; di = ndi) {
230 	ndi = di->next;
231 	free (di->realm);
232 	free (di->dbname);
233 	if (di->mkey_file)
234 	    free (di->mkey_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