LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hdb - dbinfo.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 5 130 3.8 %
Date: 2024-04-13 12:30:31 Functions: 1 13 7.7 %

          Line data    Source code
       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           0 : get_dbinfo(krb5_context context,
      49             :            const krb5_config_binding *db_binding,
      50             :            const char *label,
      51             :            struct hdb_dbinfo **db)
      52             : {
      53           0 :     struct hdb_dbinfo *di;
      54           0 :     const char *p;
      55             : 
      56           0 :     *db = NULL;
      57             : 
      58           0 :     p = krb5_config_get_string(context, db_binding, "dbname", NULL);
      59           0 :     if(p == NULL)
      60           0 :         return 0;
      61             : 
      62           0 :     di = calloc(1, sizeof(*di));
      63           0 :     if (di == NULL) {
      64           0 :         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
      65           0 :         return ENOMEM;
      66             :     }
      67           0 :     di->label = strdup(label);
      68           0 :     di->dbname = strdup(p);
      69             : 
      70           0 :     p = krb5_config_get_string(context, db_binding, "realm", NULL);
      71           0 :     if(p)
      72           0 :         di->realm = strdup(p);
      73           0 :     p = krb5_config_get_string(context, db_binding, "mkey_file", NULL);
      74           0 :     if(p)
      75           0 :         di->mkey_file = strdup(p);
      76           0 :     p = krb5_config_get_string(context, db_binding, "acl_file", NULL);
      77           0 :     if(p)
      78           0 :         di->acl_file = strdup(p);
      79           0 :     p = krb5_config_get_string(context, db_binding, "log_file", NULL);
      80           0 :     if(p)
      81           0 :         di->log_file = strdup(p);
      82             : 
      83           0 :     di->binding = db_binding;
      84             : 
      85           0 :     *db = di;
      86           0 :     return 0;
      87             : }
      88             : 
      89             : 
      90             : int
      91           0 : hdb_get_dbinfo(krb5_context context, struct hdb_dbinfo **dbp)
      92             : {
      93           0 :     const krb5_config_binding *db_binding;
      94           0 :     struct hdb_dbinfo *di, **dt, *databases;
      95           0 :     const char *default_dbname = HDB_DEFAULT_DB;
      96           0 :     const char *default_mkey = HDB_DB_DIR "/m-key";
      97           0 :     const char *default_acl = HDB_DB_DIR "/kadmind.acl";
      98           0 :     const char *p;
      99           0 :     int ret;
     100             : 
     101           0 :     *dbp = NULL;
     102           0 :     dt = NULL;
     103           0 :     databases = NULL;
     104             : 
     105           0 :     db_binding = krb5_config_get_list(context, NULL,
     106             :                                       "kdc",
     107             :                                       "database",
     108             :                                       NULL);
     109           0 :     if (db_binding) {
     110             : 
     111           0 :         ret = get_dbinfo(context, db_binding, "default", &databases);
     112           0 :         if (ret == 0 && databases != NULL)
     113           0 :             dt = &databases->next;
     114             : 
     115           0 :         for ( ; db_binding != NULL; db_binding = db_binding->next) {
     116             : 
     117           0 :             if (db_binding->type != krb5_config_list)
     118           0 :                 continue;
     119             : 
     120           0 :             ret = get_dbinfo(context, db_binding->u.list,
     121           0 :                              db_binding->name, &di);
     122           0 :             if (ret)
     123           0 :                 krb5_err(context, 1, ret, "failed getting realm");
     124             : 
     125           0 :             if (di == NULL)
     126           0 :                 continue;
     127             : 
     128           0 :             if (dt)
     129           0 :                 *dt = di;
     130             :             else {
     131           0 :                 hdb_free_dbinfo(context, &databases);
     132           0 :                 databases = di;
     133             :             }
     134           0 :             dt = &di->next;
     135             : 
     136             :         }
     137             :     }
     138             : 
     139           0 :     if (databases == NULL) {
     140             :         /* if there are none specified, create one and use defaults */
     141           0 :         databases = calloc(1, sizeof(*databases));
     142           0 :         databases->label = strdup("default");
     143             :     }
     144             : 
     145           0 :     for (di = databases; di; di = di->next) {
     146           0 :         if (di->dbname == NULL) {
     147           0 :             di->dbname = strdup(default_dbname);
     148           0 :             if (di->mkey_file == NULL)
     149           0 :                 di->mkey_file = strdup(default_mkey);
     150             :         }
     151           0 :         if (di->mkey_file == NULL) {
     152           0 :             p = strrchr(di->dbname, '.');
     153           0 :             if(p == NULL || strchr(p, '/') != NULL)
     154             :                 /* final pathname component does not contain a . */
     155           0 :                 ret = asprintf(&di->mkey_file, "%s.mkey", di->dbname);
     156             :             else
     157             :                 /* the filename is something.else, replace .else with
     158             :                    .mkey */
     159           0 :                 ret = asprintf(&di->mkey_file, "%.*s.mkey",
     160           0 :                                (int)(p - di->dbname), di->dbname);
     161           0 :             if (ret == -1) {
     162           0 :                 hdb_free_dbinfo(context, &databases);
     163           0 :                 return ENOMEM;
     164             :             }
     165             :         }
     166           0 :         if(di->acl_file == NULL)
     167           0 :             di->acl_file = strdup(default_acl);
     168             :     }
     169           0 :     *dbp = databases;
     170           0 :     return 0;
     171             : }
     172             : 
     173             : 
     174             : struct hdb_dbinfo *
     175           0 : hdb_dbinfo_get_next(struct hdb_dbinfo *dbp, struct hdb_dbinfo *dbprevp)
     176             : {
     177           0 :     if (dbprevp == NULL)
     178           0 :         return dbp;
     179             :     else
     180           0 :         return dbprevp->next;
     181             : }
     182             : 
     183             : const char *
     184           0 : hdb_dbinfo_get_label(krb5_context context, struct hdb_dbinfo *dbp)
     185             : {
     186           0 :     return dbp->label;
     187             : }
     188             : 
     189             : const char *
     190           0 : hdb_dbinfo_get_realm(krb5_context context, struct hdb_dbinfo *dbp)
     191             : {
     192           0 :     return dbp->realm;
     193             : }
     194             : 
     195             : const char *
     196           0 : hdb_dbinfo_get_dbname(krb5_context context, struct hdb_dbinfo *dbp)
     197             : {
     198           0 :     return dbp->dbname;
     199             : }
     200             : 
     201             : const char *
     202           0 : hdb_dbinfo_get_mkey_file(krb5_context context, struct hdb_dbinfo *dbp)
     203             : {
     204           0 :     return dbp->mkey_file;
     205             : }
     206             : 
     207             : const char *
     208           0 : hdb_dbinfo_get_acl_file(krb5_context context, struct hdb_dbinfo *dbp)
     209             : {
     210           0 :     return dbp->acl_file;
     211             : }
     212             : 
     213             : const char *
     214           0 : hdb_dbinfo_get_log_file(krb5_context context, struct hdb_dbinfo *dbp)
     215             : {
     216           0 :     return dbp->log_file;
     217             : }
     218             : 
     219             : const krb5_config_binding *
     220           0 : hdb_dbinfo_get_binding(krb5_context context, struct hdb_dbinfo *dbp)
     221             : {
     222           0 :     return dbp->binding;
     223             : }
     224             : 
     225             : void
     226           0 : hdb_free_dbinfo(krb5_context context, struct hdb_dbinfo **dbp)
     227             : {
     228           0 :     struct hdb_dbinfo *di, *ndi;
     229             : 
     230           0 :     for(di = *dbp; di != NULL; di = ndi) {
     231           0 :         ndi = di->next;
     232           0 :         free (di->label);
     233           0 :         free (di->realm);
     234           0 :         free (di->dbname);
     235           0 :         free (di->mkey_file);
     236           0 :         free (di->acl_file);
     237           0 :         free (di->log_file);
     238           0 :         free(di);
     239             :     }
     240           0 :     *dbp = NULL;
     241           0 : }
     242             : 
     243             : /**
     244             :  * Return the directory where the hdb database resides.
     245             :  *
     246             :  * @param context Kerberos 5 context.
     247             :  *
     248             :  * @return string pointing to directory.
     249             :  */
     250             : 
     251             : const char *
     252          54 : hdb_db_dir(krb5_context context)
     253             : {
     254           8 :     const char *p;
     255             : 
     256          54 :     p = krb5_config_get_string(context, NULL, "hdb", "db-dir", NULL);
     257          54 :     if (p)
     258           0 :         return p;
     259             : 
     260          46 :     return HDB_DB_DIR;
     261             : }
     262             : 
     263             : /**
     264             :  * Return the default hdb database resides.
     265             :  *
     266             :  * @param context Kerberos 5 context.
     267             :  *
     268             :  * @return string pointing to directory.
     269             :  */
     270             : 
     271             : const char *
     272           0 : hdb_default_db(krb5_context context)
     273             : {
     274           0 :     static char *default_hdb = NULL;
     275           0 :     struct hdb_dbinfo *dbinfo = NULL;
     276           0 :     struct hdb_dbinfo *d = NULL;
     277           0 :     const char *s;
     278             : 
     279           0 :     if (default_hdb)
     280           0 :         return default_hdb;
     281             : 
     282           0 :     (void) hdb_get_dbinfo(context, &dbinfo);
     283           0 :     while ((d = hdb_dbinfo_get_next(dbinfo, d)) != NULL) {
     284           0 :         if ((s = hdb_dbinfo_get_dbname(context, d)) &&
     285           0 :             (default_hdb = strdup(s)))
     286           0 :             break;
     287             :     }
     288             : 
     289           0 :     hdb_free_dbinfo(context, &dbinfo);
     290           0 :     return default_hdb ? default_hdb : HDB_DEFAULT_DB;
     291             : }

Generated by: LCOV version 1.14