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