2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
43 const krb5_config_binding
*binding
;
44 struct hdb_dbinfo
*next
;
48 get_dbinfo(krb5_context context
,
49 const krb5_config_binding
*db_binding
,
51 struct hdb_dbinfo
**db
)
53 struct hdb_dbinfo
*di
;
58 p
= krb5_config_get_string(context
, db_binding
, "dbname", NULL
);
62 di
= calloc(1, sizeof(*di
));
64 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
67 di
->label
= strdup(label
);
68 di
->dbname
= strdup(p
);
70 p
= krb5_config_get_string(context
, db_binding
, "realm", NULL
);
72 di
->realm
= strdup(p
);
73 p
= krb5_config_get_string(context
, db_binding
, "mkey_file", NULL
);
75 di
->mkey_file
= strdup(p
);
76 p
= krb5_config_get_string(context
, db_binding
, "acl_file", NULL
);
78 di
->acl_file
= strdup(p
);
79 p
= krb5_config_get_string(context
, db_binding
, "log_file", NULL
);
81 di
->log_file
= strdup(p
);
83 di
->binding
= db_binding
;
91 hdb_get_dbinfo(krb5_context context
, struct hdb_dbinfo
**dbp
)
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";
105 db_binding
= krb5_config_get_list(context
, NULL
,
111 ret
= get_dbinfo(context
, db_binding
, "default", &databases
);
112 if (ret
== 0 && databases
!= NULL
)
113 dt
= &databases
->next
;
115 for ( ; db_binding
!= NULL
; db_binding
= db_binding
->next
) {
117 if (db_binding
->type
!= krb5_config_list
)
120 ret
= get_dbinfo(context
, db_binding
->u
.list
,
121 db_binding
->name
, &di
);
123 krb5_err(context
, 1, ret
, "failed getting realm");
131 hdb_free_dbinfo(context
, &databases
);
139 if (databases
== NULL
) {
140 /* if there are none specified, create one and use defaults */
141 databases
= calloc(1, sizeof(*databases
));
142 databases
->label
= strdup("default");
145 for (di
= databases
; di
; di
= di
->next
) {
146 if (di
->dbname
== NULL
) {
147 di
->dbname
= strdup(default_dbname
);
148 if (di
->mkey_file
== NULL
)
149 di
->mkey_file
= strdup(default_mkey
);
151 if (di
->mkey_file
== NULL
) {
152 p
= strrchr(di
->dbname
, '.');
153 if(p
== NULL
|| strchr(p
, '/') != NULL
)
154 /* final pathname component does not contain a . */
155 ret
= asprintf(&di
->mkey_file
, "%s.mkey", di
->dbname
);
157 /* the filename is something.else, replace .else with
159 ret
= asprintf(&di
->mkey_file
, "%.*s.mkey",
160 (int)(p
- di
->dbname
), di
->dbname
);
162 hdb_free_dbinfo(context
, &databases
);
166 if(di
->acl_file
== NULL
)
167 di
->acl_file
= strdup(default_acl
);
175 hdb_dbinfo_get_next(struct hdb_dbinfo
*dbp
, struct hdb_dbinfo
*dbprevp
)
180 return dbprevp
->next
;
184 hdb_dbinfo_get_label(krb5_context context
, struct hdb_dbinfo
*dbp
)
190 hdb_dbinfo_get_realm(krb5_context context
, struct hdb_dbinfo
*dbp
)
196 hdb_dbinfo_get_dbname(krb5_context context
, struct hdb_dbinfo
*dbp
)
202 hdb_dbinfo_get_mkey_file(krb5_context context
, struct hdb_dbinfo
*dbp
)
204 return dbp
->mkey_file
;
208 hdb_dbinfo_get_acl_file(krb5_context context
, struct hdb_dbinfo
*dbp
)
210 return dbp
->acl_file
;
214 hdb_dbinfo_get_log_file(krb5_context context
, struct hdb_dbinfo
*dbp
)
216 return dbp
->log_file
;
219 const krb5_config_binding
*
220 hdb_dbinfo_get_binding(krb5_context context
, struct hdb_dbinfo
*dbp
)
226 hdb_free_dbinfo(krb5_context context
, struct hdb_dbinfo
**dbp
)
228 struct hdb_dbinfo
*di
, *ndi
;
230 for(di
= *dbp
; di
!= NULL
; di
= ndi
) {
235 free (di
->mkey_file
);
244 * Return the directory where the hdb database resides.
246 * @param context Kerberos 5 context.
248 * @return string pointing to directory.
252 hdb_db_dir(krb5_context context
)
256 p
= krb5_config_get_string(context
, NULL
, "hdb", "db-dir", NULL
);
264 * Return the default hdb database resides.
266 * @param context Kerberos 5 context.
268 * @return string pointing to directory.
272 hdb_default_db(krb5_context context
)
274 static char *default_hdb
= NULL
;
275 struct hdb_dbinfo
*dbinfo
= NULL
;
276 struct hdb_dbinfo
*d
= NULL
;
282 (void) hdb_get_dbinfo(context
, &dbinfo
);
283 while ((d
= hdb_dbinfo_get_next(dbinfo
, d
)) != NULL
) {
284 if ((s
= hdb_dbinfo_get_dbname(context
, d
)) &&
285 (default_hdb
= strdup(s
)))
289 hdb_free_dbinfo(context
, &dbinfo
);
290 return default_hdb
? default_hdb
: HDB_DEFAULT_DB
;