Use hdb_get_dbinfo() to find the realms.

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@25326 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Love Hörnquist Åstrand
2009-07-15 22:17:30 +00:00
parent 9dcdb2c02c
commit 6a24e13678

View File

@@ -121,61 +121,43 @@ hdb_get_name(krb5_context context,
return 0; return 0;
} }
static void
set_config (krb5_context context,
const krb5_config_binding *binding,
const char **dbname,
const char **mkey)
{
*dbname = krb5_config_get_string(context, binding, "dbname", NULL);
*mkey = krb5_config_get_string(context, binding, "mkey_file", NULL);
}
/* /*
* try to figure out the database (`dbname') and master-key (`mkey') * try to figure out the database (`dbname') and master-key (`mkey')
* that should be used for `principal'. * that should be used for `principal'.
*/ */
static void static krb5_error_code
find_db (krb5_context context, find_db (krb5_context context,
const char **dbname, char **dbname,
const char **mkey, char **mkey,
krb5_const_principal principal) krb5_const_principal principal)
{ {
const krb5_config_binding *top_bind = NULL;
const krb5_config_binding *default_binding = NULL;
const krb5_config_binding *db;
krb5_const_realm realm = krb5_principal_get_realm(context, principal); krb5_const_realm realm = krb5_principal_get_realm(context, principal);
krb5_error_code ret;
struct hdb_dbinfo *head, *dbinfo = NULL;
*dbname = *mkey = NULL; *dbname = *mkey = NULL;
while ((db = ret = hdb_get_dbinfo(context, &head);
krb5_config_get_next(context, if (ret)
NULL, return ret;
&top_bind,
krb5_config_list,
"kdc",
"database",
NULL)) != NULL) {
const char *p;
p = krb5_config_get_string (context, db, "realm", NULL); while ((dbinfo = hdb_dbinfo_get_next(head, dbinfo)) != NULL) {
if (p == NULL) { const char *p = hdb_dbinfo_get_realm(context, dbinfo);
if(default_binding) { if (p && strcmp (realm, p) == 0) {
krb5_warnx(context, "WARNING: more than one realm-less " p = hdb_dbinfo_get_dbname(context, dbinfo);
"database specification"); if (p)
krb5_warnx(context, "WARNING: using the first encountered"); *dbname = strdup(p);
} else p = hdb_dbinfo_get_mkey_file(context, dbinfo);
default_binding = db; if (p)
} else if (strcmp (realm, p) == 0) { *mkey = strdup(p);
set_config (context, db, dbname, mkey);
break; break;
} }
} }
if (*dbname == NULL && default_binding != NULL) hdb_free_dbinfo(context, &head);
set_config (context, default_binding, dbname, mkey);
if (*dbname == NULL) if (*dbname == NULL)
*dbname = HDB_DEFAULT_DB; *dbname = strdup(HDB_DEFAULT_DB);
return 0;
} }
/* /*
@@ -194,29 +176,35 @@ hdb_get_entry(krb5_context context,
hdb_entry_ex ent; hdb_entry_ex ent;
krb5_error_code ret; krb5_error_code ret;
struct hdb_data *d = id->data; struct hdb_data *d = id->data;
int i;
HDB *db;
const char *dbname = d->dbname; const char *dbname = d->dbname;
const char *mkey = d->mkey; const char *mkey = d->mkey;
char *fdbname = NULL, *fmkey = NULL;
HDB *db;
int i;
memset(&ent, 0, sizeof(ent)); memset(&ent, 0, sizeof(ent));
if (dbname == NULL) if (dbname == NULL) {
find_db (context, &dbname, &mkey, principal); ret = find_db(context, &fdbname, &fmkey, principal);
if (ret)
return ret;
dbname = fdbname;
mkey = fmkey;
}
ret = hdb_create (context, &db, dbname); ret = hdb_create (context, &db, dbname);
if (ret) if (ret)
return ret; goto out2;
ret = hdb_set_master_keyfile (context, db, mkey); ret = hdb_set_master_keyfile (context, db, mkey);
if (ret) { if (ret) {
(*db->hdb_destroy)(context, db); (*db->hdb_destroy)(context, db);
return ret; goto out2;
} }
ret = (*db->hdb_open)(context, db, O_RDONLY, 0); ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
if (ret) { if (ret) {
(*db->hdb_destroy)(context, db); (*db->hdb_destroy)(context, db);
return ret; goto out2;
} }
ret = (*db->hdb_fetch)(context, db, principal, ret = (*db->hdb_fetch)(context, db, principal,
HDB_F_DECRYPT| HDB_F_DECRYPT|
@@ -250,9 +238,12 @@ hdb_get_entry(krb5_context context,
} }
} }
hdb_free_entry(context, &ent); hdb_free_entry(context, &ent);
out: out:
(*db->hdb_close)(context, db); (*db->hdb_close)(context, db);
(*db->hdb_destroy)(context, db); (*db->hdb_destroy)(context, db);
out2:
free(fdbname);
free(fmkey);
return ret; return ret;
} }