Path: blob/main/crypto/heimdal/lib/hdb/hdb-keytab.c
34878 views
/*1* Copyright (c) 2009 Kungliga Tekniska H�gskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "hdb_locl.h"36#include <assert.h>3738typedef struct {39char *path;40krb5_keytab keytab;41} *hdb_keytab;4243/*44*45*/4647static krb5_error_code48hkt_close(krb5_context context, HDB *db)49{50hdb_keytab k = (hdb_keytab)db->hdb_db;51krb5_error_code ret;5253assert(k->keytab);5455ret = krb5_kt_close(context, k->keytab);56k->keytab = NULL;5758return ret;59}6061static krb5_error_code62hkt_destroy(krb5_context context, HDB *db)63{64hdb_keytab k = (hdb_keytab)db->hdb_db;65krb5_error_code ret;6667ret = hdb_clear_master_key (context, db);6869free(k->path);70free(k);7172free(db->hdb_name);73free(db);74return ret;75}7677static krb5_error_code78hkt_lock(krb5_context context, HDB *db, int operation)79{80return 0;81}8283static krb5_error_code84hkt_unlock(krb5_context context, HDB *db)85{86return 0;87}8889static krb5_error_code90hkt_firstkey(krb5_context context, HDB *db,91unsigned flags, hdb_entry_ex *entry)92{93return HDB_ERR_DB_INUSE;94}9596static krb5_error_code97hkt_nextkey(krb5_context context, HDB * db, unsigned flags,98hdb_entry_ex * entry)99{100return HDB_ERR_DB_INUSE;101}102103static krb5_error_code104hkt_open(krb5_context context, HDB * db, int flags, mode_t mode)105{106hdb_keytab k = (hdb_keytab)db->hdb_db;107krb5_error_code ret;108109assert(k->keytab == NULL);110111ret = krb5_kt_resolve(context, k->path, &k->keytab);112if (ret)113return ret;114115return 0;116}117118static krb5_error_code119hkt_fetch_kvno(krb5_context context, HDB * db, krb5_const_principal principal,120unsigned flags, krb5_kvno kvno, hdb_entry_ex * entry)121{122hdb_keytab k = (hdb_keytab)db->hdb_db;123krb5_error_code ret;124krb5_keytab_entry ktentry;125126if (!(flags & HDB_F_KVNO_SPECIFIED)) {127/* Preserve previous behaviour if no kvno specified */128kvno = 0;129}130131memset(&ktentry, 0, sizeof(ktentry));132133entry->entry.flags.server = 1;134entry->entry.flags.forwardable = 1;135entry->entry.flags.renewable = 1;136137/* Not recorded in the OD backend, make something up */138ret = krb5_parse_name(context, "hdb/keytab@WELL-KNOWN:KEYTAB-BACKEND",139&entry->entry.created_by.principal);140if (ret)141goto out;142143/*144* XXX really needs to try all enctypes and just not pick the145* first one, even if that happens to be des3-cbc-sha1 (ie best146* enctype) in the Apple case. A while loop over all known147* enctypes should work.148*/149150ret = krb5_kt_get_entry(context, k->keytab, principal, kvno, 0, &ktentry);151if (ret) {152ret = HDB_ERR_NOENTRY;153goto out;154}155156ret = krb5_copy_principal(context, principal, &entry->entry.principal);157if (ret)158goto out;159160ret = _hdb_keytab2hdb_entry(context, &ktentry, entry);161162out:163if (ret) {164free_hdb_entry(&entry->entry);165memset(&entry->entry, 0, sizeof(entry->entry));166}167krb5_kt_free_entry(context, &ktentry);168169return ret;170}171172static krb5_error_code173hkt_store(krb5_context context, HDB * db, unsigned flags,174hdb_entry_ex * entry)175{176return HDB_ERR_DB_INUSE;177}178179180krb5_error_code181hdb_keytab_create(krb5_context context, HDB ** db, const char *arg)182{183hdb_keytab k;184185*db = calloc(1, sizeof(**db));186if (*db == NULL) {187krb5_set_error_message(context, ENOMEM, "malloc: out of memory");188return ENOMEM;189}190memset(*db, 0, sizeof(**db));191192k = calloc(1, sizeof(*k));193if (k == NULL) {194free(*db);195*db = NULL;196krb5_set_error_message(context, ENOMEM, "malloc: out of memory");197return ENOMEM;198}199200k->path = strdup(arg);201if (k->path == NULL) {202free(k);203free(*db);204*db = NULL;205krb5_set_error_message(context, ENOMEM, "malloc: out of memory");206return ENOMEM;207}208209210(*db)->hdb_db = k;211212(*db)->hdb_master_key_set = 0;213(*db)->hdb_openp = 0;214(*db)->hdb_open = hkt_open;215(*db)->hdb_close = hkt_close;216(*db)->hdb_fetch_kvno = hkt_fetch_kvno;217(*db)->hdb_store = hkt_store;218(*db)->hdb_remove = NULL;219(*db)->hdb_firstkey = hkt_firstkey;220(*db)->hdb_nextkey = hkt_nextkey;221(*db)->hdb_lock = hkt_lock;222(*db)->hdb_unlock = hkt_unlock;223(*db)->hdb_rename = NULL;224(*db)->hdb__get = NULL;225(*db)->hdb__put = NULL;226(*db)->hdb__del = NULL;227(*db)->hdb_destroy = hkt_destroy;228229return 0;230}231232233