Path: blob/main/crypto/heimdal/lib/krb5/context.c
105220 views
/*1* Copyright (c) 1997 - 2010 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 "krb5_locl.h"36#include <assert.h>37#include <com_err.h>3839#define INIT_FIELD(C, T, E, D, F) \40(C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D), \41"libdefaults", F, NULL)4243#define INIT_FLAG(C, O, V, D, F) \44do { \45if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \46(C)->O |= V; \47} \48} while(0)4950/*51* Set the list of etypes `ret_etypes' from the configuration variable52* `name'53*/5455static krb5_error_code56set_etypes (krb5_context context,57const char *name,58krb5_enctype **ret_enctypes)59{60char **etypes_str;61krb5_enctype *etypes = NULL;6263etypes_str = krb5_config_get_strings(context, NULL, "libdefaults",64name, NULL);65if(etypes_str){66int i, j, k;67for(i = 0; etypes_str[i]; i++);68etypes = malloc((i+1) * sizeof(*etypes));69if (etypes == NULL) {70krb5_config_free_strings (etypes_str);71krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));72return ENOMEM;73}74for(j = 0, k = 0; j < i; j++) {75krb5_enctype e;76if(krb5_string_to_enctype(context, etypes_str[j], &e) != 0)77continue;78if (krb5_enctype_valid(context, e) != 0)79continue;80etypes[k++] = e;81}82etypes[k] = ETYPE_NULL;83krb5_config_free_strings(etypes_str);84}85*ret_enctypes = etypes;86return 0;87}8889/*90* read variables from the configuration file and set in `context'91*/9293static krb5_error_code94init_context_from_config_file(krb5_context context)95{96krb5_error_code ret;97const char * tmp;98char **s;99krb5_enctype *tmptypes = NULL;100101INIT_FIELD(context, time, max_skew, 5 * 60, "clockskew");102INIT_FIELD(context, time, kdc_timeout, 3, "kdc_timeout");103INIT_FIELD(context, int, max_retries, 3, "max_retries");104105INIT_FIELD(context, string, http_proxy, NULL, "http_proxy");106107ret = krb5_config_get_bool_default(context, NULL, FALSE,108"libdefaults",109"allow_weak_crypto", NULL);110if (ret) {111krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);112krb5_enctype_enable(context, ETYPE_DES_CBC_MD4);113krb5_enctype_enable(context, ETYPE_DES_CBC_MD5);114krb5_enctype_enable(context, ETYPE_DES_CBC_NONE);115krb5_enctype_enable(context, ETYPE_DES_CFB64_NONE);116krb5_enctype_enable(context, ETYPE_DES_PCBC_NONE);117}118119ret = set_etypes (context, "default_etypes", &tmptypes);120if(ret)121return ret;122free(context->etypes);123context->etypes = tmptypes;124125ret = set_etypes (context, "default_etypes_des", &tmptypes);126if(ret)127return ret;128free(context->etypes_des);129context->etypes_des = tmptypes;130131ret = set_etypes (context, "default_as_etypes", &tmptypes);132if(ret)133return ret;134free(context->as_etypes);135context->as_etypes = tmptypes;136137ret = set_etypes (context, "default_tgs_etypes", &tmptypes);138if(ret)139return ret;140free(context->tgs_etypes);141context->tgs_etypes = tmptypes;142143ret = set_etypes (context, "permitted_enctypes", &tmptypes);144if(ret)145return ret;146free(context->permitted_enctypes);147context->permitted_enctypes = tmptypes;148149/* default keytab name */150tmp = NULL;151if(!issuid())152tmp = getenv("KRB5_KTNAME");153if(tmp != NULL)154context->default_keytab = tmp;155else156INIT_FIELD(context, string, default_keytab,157KEYTAB_DEFAULT, "default_keytab_name");158159INIT_FIELD(context, string, default_keytab_modify,160NULL, "default_keytab_modify_name");161162INIT_FIELD(context, string, time_fmt,163"%Y-%m-%dT%H:%M:%S", "time_format");164165INIT_FIELD(context, string, date_fmt,166"%Y-%m-%d", "date_format");167168INIT_FIELD(context, bool, log_utc,169FALSE, "log_utc");170171172173/* init dns-proxy slime */174tmp = krb5_config_get_string(context, NULL, "libdefaults",175"dns_proxy", NULL);176if(tmp)177roken_gethostby_setup(context->http_proxy, tmp);178krb5_free_host_realm (context, context->default_realms);179context->default_realms = NULL;180181{182krb5_addresses addresses;183char **adr, **a;184185krb5_set_extra_addresses(context, NULL);186adr = krb5_config_get_strings(context, NULL,187"libdefaults",188"extra_addresses",189NULL);190memset(&addresses, 0, sizeof(addresses));191for(a = adr; a && *a; a++) {192ret = krb5_parse_address(context, *a, &addresses);193if (ret == 0) {194krb5_add_extra_addresses(context, &addresses);195krb5_free_addresses(context, &addresses);196}197}198krb5_config_free_strings(adr);199200krb5_set_ignore_addresses(context, NULL);201adr = krb5_config_get_strings(context, NULL,202"libdefaults",203"ignore_addresses",204NULL);205memset(&addresses, 0, sizeof(addresses));206for(a = adr; a && *a; a++) {207ret = krb5_parse_address(context, *a, &addresses);208if (ret == 0) {209krb5_add_ignore_addresses(context, &addresses);210krb5_free_addresses(context, &addresses);211}212}213krb5_config_free_strings(adr);214}215216INIT_FIELD(context, bool, scan_interfaces, TRUE, "scan_interfaces");217INIT_FIELD(context, int, fcache_vno, 0, "fcache_version");218/* prefer dns_lookup_kdc over srv_lookup. */219INIT_FIELD(context, bool, srv_lookup, TRUE, "srv_lookup");220INIT_FIELD(context, bool, srv_lookup, context->srv_lookup, "dns_lookup_kdc");221INIT_FIELD(context, int, large_msg_size, 1400, "large_message_size");222INIT_FLAG(context, flags, KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME, TRUE, "dns_canonicalize_hostname");223INIT_FLAG(context, flags, KRB5_CTX_F_CHECK_PAC, TRUE, "check_pac");224context->default_cc_name = NULL;225context->default_cc_name_set = 0;226227s = krb5_config_get_strings(context, NULL, "logging", "krb5", NULL);228if(s) {229char **p;230krb5_initlog(context, "libkrb5", &context->debug_dest);231for(p = s; *p; p++)232krb5_addlog_dest(context, context->debug_dest, *p);233krb5_config_free_strings(s);234}235236tmp = krb5_config_get_string(context, NULL, "libdefaults",237"check-rd-req-server", NULL);238if (tmp == NULL && !issuid())239tmp = getenv("KRB5_CHECK_RD_REQ_SERVER");240if(tmp) {241if (strcasecmp(tmp, "ignore") == 0)242context->flags |= KRB5_CTX_F_RD_REQ_IGNORE;243}244245return 0;246}247248static krb5_error_code249cc_ops_register(krb5_context context)250{251context->cc_ops = NULL;252context->num_cc_ops = 0;253254#ifndef KCM_IS_API_CACHE255krb5_cc_register(context, &krb5_acc_ops, TRUE);256#endif257krb5_cc_register(context, &krb5_fcc_ops, TRUE);258krb5_cc_register(context, &krb5_mcc_ops, TRUE);259#ifdef HAVE_SCC260krb5_cc_register(context, &krb5_scc_ops, TRUE);261#endif262#ifdef HAVE_KCM263#ifdef KCM_IS_API_CACHE264krb5_cc_register(context, &krb5_akcm_ops, TRUE);265#endif266krb5_cc_register(context, &krb5_kcm_ops, TRUE);267#endif268_krb5_load_ccache_plugins(context);269return 0;270}271272static krb5_error_code273cc_ops_copy(krb5_context context, const krb5_context src_context)274{275const krb5_cc_ops **cc_ops;276277context->cc_ops = NULL;278context->num_cc_ops = 0;279280if (src_context->num_cc_ops == 0)281return 0;282283cc_ops = malloc(sizeof(cc_ops[0]) * src_context->num_cc_ops);284if (cc_ops == NULL) {285krb5_set_error_message(context, KRB5_CC_NOMEM,286N_("malloc: out of memory", ""));287return KRB5_CC_NOMEM;288}289290memcpy(rk_UNCONST(cc_ops), src_context->cc_ops,291sizeof(cc_ops[0]) * src_context->num_cc_ops);292context->cc_ops = cc_ops;293context->num_cc_ops = src_context->num_cc_ops;294295return 0;296}297298static krb5_error_code299kt_ops_register(krb5_context context)300{301context->num_kt_types = 0;302context->kt_types = NULL;303304krb5_kt_register (context, &krb5_fkt_ops);305krb5_kt_register (context, &krb5_wrfkt_ops);306krb5_kt_register (context, &krb5_javakt_ops);307krb5_kt_register (context, &krb5_mkt_ops);308#ifndef HEIMDAL_SMALLER309krb5_kt_register (context, &krb5_akf_ops);310#endif311krb5_kt_register (context, &krb5_any_ops);312return 0;313}314315static krb5_error_code316kt_ops_copy(krb5_context context, const krb5_context src_context)317{318context->num_kt_types = 0;319context->kt_types = NULL;320321if (src_context->num_kt_types == 0)322return 0;323324context->kt_types = malloc(sizeof(context->kt_types[0]) * src_context->num_kt_types);325if (context->kt_types == NULL) {326krb5_set_error_message(context, ENOMEM,327N_("malloc: out of memory", ""));328return ENOMEM;329}330331context->num_kt_types = src_context->num_kt_types;332memcpy(context->kt_types, src_context->kt_types,333sizeof(context->kt_types[0]) * src_context->num_kt_types);334335return 0;336}337338static const char *sysplugin_dirs[] = {339LIBDIR "/plugin/krb5",340#ifdef __APPLE__341"/Library/KerberosPlugins/KerberosFrameworkPlugins",342"/System/Library/KerberosPlugins/KerberosFrameworkPlugins",343#endif344NULL345};346347static void348init_context_once(void *ctx)349{350krb5_context context = ctx;351352_krb5_load_plugins(context, "krb5", sysplugin_dirs);353354bindtextdomain(HEIMDAL_TEXTDOMAIN, HEIMDAL_LOCALEDIR);355}356357358/**359* Initializes the context structure and reads the configuration file360* /etc/krb5.conf. The structure should be freed by calling361* krb5_free_context() when it is no longer being used.362*363* @param context pointer to returned context364*365* @return Returns 0 to indicate success. Otherwise an errno code is366* returned. Failure means either that something bad happened during367* initialization (typically ENOMEM) or that Kerberos should not be368* used ENXIO.369*370* @ingroup krb5371*/372373KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL374krb5_init_context(krb5_context *context)375{376static heim_base_once_t init_context = HEIM_BASE_ONCE_INIT;377krb5_context p;378krb5_error_code ret;379char **files;380381*context = NULL;382383p = calloc(1, sizeof(*p));384if(!p)385return ENOMEM;386387p->mutex = malloc(sizeof(HEIMDAL_MUTEX));388if (p->mutex == NULL) {389free(p);390return ENOMEM;391}392HEIMDAL_MUTEX_init(p->mutex);393394ret = fbsd_ossl_provider_load();395if(ret)396goto out;397398p->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;399400ret = krb5_get_default_config_files(&files);401if(ret)402goto out;403ret = krb5_set_config_files(p, files);404krb5_free_config_files(files);405if(ret)406goto out;407408/* init error tables */409krb5_init_ets(p);410cc_ops_register(p);411kt_ops_register(p);412413#ifdef PKINIT414ret = hx509_context_init(&p->hx509ctx);415if (ret)416goto out;417#endif418if (rk_SOCK_INIT())419p->flags |= KRB5_CTX_F_SOCKETS_INITIALIZED;420421out:422if(ret) {423krb5_free_context(p);424p = NULL;425} else {426heim_base_once_f(&init_context, p, init_context_once);427}428*context = p;429return ret;430}431432#ifndef HEIMDAL_SMALLER433434KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL435krb5_get_permitted_enctypes(krb5_context context,436krb5_enctype **etypes)437{438return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, etypes);439}440441/*442*443*/444445static krb5_error_code446copy_etypes (krb5_context context,447krb5_enctype *enctypes,448krb5_enctype **ret_enctypes)449{450unsigned int i;451452for (i = 0; enctypes[i]; i++)453;454i++;455456*ret_enctypes = malloc(sizeof(ret_enctypes[0]) * i);457if (*ret_enctypes == NULL) {458krb5_set_error_message(context, ENOMEM,459N_("malloc: out of memory", ""));460return ENOMEM;461}462memcpy(*ret_enctypes, enctypes, sizeof(ret_enctypes[0]) * i);463return 0;464}465466/**467* Make a copy for the Kerberos 5 context, the new krb5_context shoud468* be freed with krb5_free_context().469*470* @param context the Kerberos context to copy471* @param out the copy of the Kerberos, set to NULL error.472*473* @return Returns 0 to indicate success. Otherwise an kerberos et474* error code is returned, see krb5_get_error_message().475*476* @ingroup krb5477*/478479KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL480krb5_copy_context(krb5_context context, krb5_context *out)481{482krb5_error_code ret;483krb5_context p;484485*out = NULL;486487p = calloc(1, sizeof(*p));488if (p == NULL) {489krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));490return ENOMEM;491}492493p->mutex = malloc(sizeof(HEIMDAL_MUTEX));494if (p->mutex == NULL) {495krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));496free(p);497return ENOMEM;498}499HEIMDAL_MUTEX_init(p->mutex);500501502if (context->default_cc_name)503p->default_cc_name = strdup(context->default_cc_name);504if (context->default_cc_name_env)505p->default_cc_name_env = strdup(context->default_cc_name_env);506507if (context->etypes) {508ret = copy_etypes(context, context->etypes, &p->etypes);509if (ret)510goto out;511}512if (context->etypes_des) {513ret = copy_etypes(context, context->etypes_des, &p->etypes_des);514if (ret)515goto out;516}517518if (context->default_realms) {519ret = krb5_copy_host_realm(context,520context->default_realms, &p->default_realms);521if (ret)522goto out;523}524525ret = _krb5_config_copy(context, context->cf, &p->cf);526if (ret)527goto out;528529/* XXX should copy */530krb5_init_ets(p);531532cc_ops_copy(p, context);533kt_ops_copy(p, context);534535#if 0 /* XXX */536if(context->warn_dest != NULL)537;538if(context->debug_dest != NULL)539;540#endif541542ret = krb5_set_extra_addresses(p, context->extra_addresses);543if (ret)544goto out;545ret = krb5_set_extra_addresses(p, context->ignore_addresses);546if (ret)547goto out;548549ret = _krb5_copy_send_to_kdc_func(p, context);550if (ret)551goto out;552553*out = p;554555return 0;556557out:558krb5_free_context(p);559return ret;560}561562#endif563564/**565* Frees the krb5_context allocated by krb5_init_context().566*567* @param context context to be freed.568*569* @ingroup krb5570*/571572KRB5_LIB_FUNCTION void KRB5_LIB_CALL573krb5_free_context(krb5_context context)574{575if (context->default_cc_name)576free(context->default_cc_name);577if (context->default_cc_name_env)578free(context->default_cc_name_env);579free(context->etypes);580free(context->etypes_des);581krb5_free_host_realm (context, context->default_realms);582krb5_config_file_free (context, context->cf);583free_error_table (context->et_list);584free(rk_UNCONST(context->cc_ops));585free(context->kt_types);586krb5_clear_error_message(context);587if(context->warn_dest != NULL)588krb5_closelog(context, context->warn_dest);589if(context->debug_dest != NULL)590krb5_closelog(context, context->debug_dest);591krb5_set_extra_addresses(context, NULL);592krb5_set_ignore_addresses(context, NULL);593krb5_set_send_to_kdc_func(context, NULL, NULL);594595#ifdef PKINIT596if (context->hx509ctx)597hx509_context_free(&context->hx509ctx);598#endif599600HEIMDAL_MUTEX_destroy(context->mutex);601free(context->mutex);602if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) {603rk_SOCK_EXIT();604}605606memset(context, 0, sizeof(*context));607free(context);608}609610/**611* Reinit the context from a new set of filenames.612*613* @param context context to add configuration too.614* @param filenames array of filenames, end of list is indicated with a NULL filename.615*616* @return Returns 0 to indicate success. Otherwise an kerberos et617* error code is returned, see krb5_get_error_message().618*619* @ingroup krb5620*/621622KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL623krb5_set_config_files(krb5_context context, char **filenames)624{625krb5_error_code ret;626krb5_config_binding *tmp = NULL;627while(filenames != NULL && *filenames != NULL && **filenames != '\0') {628ret = krb5_config_parse_file_multi(context, *filenames, &tmp);629if(ret != 0 && ret != ENOENT && ret != EACCES && ret != EPERM) {630krb5_config_file_free(context, tmp);631return ret;632}633filenames++;634}635#if 0636/* with this enabled and if there are no config files, Kerberos is637considererd disabled */638if(tmp == NULL)639return ENXIO;640#endif641642#ifdef _WIN32643_krb5_load_config_from_registry(context, &tmp);644#endif645646krb5_config_file_free(context, context->cf);647context->cf = tmp;648ret = init_context_from_config_file(context);649return ret;650}651652static krb5_error_code653add_file(char ***pfilenames, int *len, char *file)654{655char **pp = *pfilenames;656int i;657658for(i = 0; i < *len; i++) {659if(strcmp(pp[i], file) == 0) {660free(file);661return 0;662}663}664665pp = realloc(*pfilenames, (*len + 2) * sizeof(*pp));666if (pp == NULL) {667free(file);668return ENOMEM;669}670671pp[*len] = file;672pp[*len + 1] = NULL;673*pfilenames = pp;674*len += 1;675return 0;676}677678/*679* `pq' isn't free, it's up the the caller680*/681682KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL683krb5_prepend_config_files(const char *filelist, char **pq, char ***ret_pp)684{685krb5_error_code ret;686const char *p, *q;687char **pp;688int len;689char *fn;690691pp = NULL;692693len = 0;694p = filelist;695while(1) {696ssize_t l;697q = p;698l = strsep_copy(&q, PATH_SEP, NULL, 0);699if(l == -1)700break;701fn = malloc(l + 1);702if(fn == NULL) {703krb5_free_config_files(pp);704return ENOMEM;705}706(void)strsep_copy(&p, PATH_SEP, fn, l + 1);707ret = add_file(&pp, &len, fn);708if (ret) {709krb5_free_config_files(pp);710return ret;711}712}713714if (pq != NULL) {715int i;716717for (i = 0; pq[i] != NULL; i++) {718fn = strdup(pq[i]);719if (fn == NULL) {720krb5_free_config_files(pp);721return ENOMEM;722}723ret = add_file(&pp, &len, fn);724if (ret) {725krb5_free_config_files(pp);726return ret;727}728}729}730731*ret_pp = pp;732return 0;733}734735/**736* Prepend the filename to the global configuration list.737*738* @param filelist a filename to add to the default list of filename739* @param pfilenames return array of filenames, should be freed with krb5_free_config_files().740*741* @return Returns 0 to indicate success. Otherwise an kerberos et742* error code is returned, see krb5_get_error_message().743*744* @ingroup krb5745*/746747KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL748krb5_prepend_config_files_default(const char *filelist, char ***pfilenames)749{750krb5_error_code ret;751char **defpp, **pp = NULL;752753ret = krb5_get_default_config_files(&defpp);754if (ret)755return ret;756757ret = krb5_prepend_config_files(filelist, defpp, &pp);758krb5_free_config_files(defpp);759if (ret) {760return ret;761}762*pfilenames = pp;763return 0;764}765766#ifdef _WIN32767768/**769* Checks the registry for configuration file location770*771* Kerberos for Windows and other legacy Kerberos applications expect772* to find the configuration file location in the773* SOFTWARE\MIT\Kerberos registry key under the value "config".774*/775char *776_krb5_get_default_config_config_files_from_registry()777{778static const char * KeyName = "Software\\MIT\\Kerberos";779char *config_file = NULL;780LONG rcode;781HKEY key;782783rcode = RegOpenKeyEx(HKEY_CURRENT_USER, KeyName, 0, KEY_READ, &key);784if (rcode == ERROR_SUCCESS) {785config_file = _krb5_parse_reg_value_as_multi_string(NULL, key, "config",786REG_NONE, 0, PATH_SEP);787RegCloseKey(key);788}789790if (config_file)791return config_file;792793rcode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyName, 0, KEY_READ, &key);794if (rcode == ERROR_SUCCESS) {795config_file = _krb5_parse_reg_value_as_multi_string(NULL, key, "config",796REG_NONE, 0, PATH_SEP);797RegCloseKey(key);798}799800return config_file;801}802803#endif804805/**806* Get the global configuration list.807*808* @param pfilenames return array of filenames, should be freed with krb5_free_config_files().809*810* @return Returns 0 to indicate success. Otherwise an kerberos et811* error code is returned, see krb5_get_error_message().812*813* @ingroup krb5814*/815816KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL817krb5_get_default_config_files(char ***pfilenames)818{819const char *files = NULL;820821if (pfilenames == NULL)822return EINVAL;823if(!issuid())824files = getenv("KRB5_CONFIG");825826#ifdef _WIN32827if (files == NULL) {828char * reg_files;829reg_files = _krb5_get_default_config_config_files_from_registry();830if (reg_files != NULL) {831krb5_error_code code;832833code = krb5_prepend_config_files(reg_files, NULL, pfilenames);834free(reg_files);835836return code;837}838}839#endif840841if (files == NULL)842files = krb5_config_file;843844return krb5_prepend_config_files(files, NULL, pfilenames);845}846847/**848* Free a list of configuration files.849*850* @param filenames list, terminated with a NULL pointer, to be851* freed. NULL is an valid argument.852*853* @return Returns 0 to indicate success. Otherwise an kerberos et854* error code is returned, see krb5_get_error_message().855*856* @ingroup krb5857*/858859KRB5_LIB_FUNCTION void KRB5_LIB_CALL860krb5_free_config_files(char **filenames)861{862char **p;863for(p = filenames; p && *p != NULL; p++)864free(*p);865free(filenames);866}867868/**869* Returns the list of Kerberos encryption types sorted in order of870* most preferred to least preferred encryption type. Note that some871* encryption types might be disabled, so you need to check with872* krb5_enctype_valid() before using the encryption type.873*874* @return list of enctypes, terminated with ETYPE_NULL. Its a static875* array completed into the Kerberos library so the content doesn't876* need to be freed.877*878* @ingroup krb5879*/880881KRB5_LIB_FUNCTION const krb5_enctype * KRB5_LIB_CALL882krb5_kerberos_enctypes(krb5_context context)883{884static const krb5_enctype p[] = {885ETYPE_AES256_CTS_HMAC_SHA1_96,886ETYPE_AES128_CTS_HMAC_SHA1_96,887ETYPE_DES3_CBC_SHA1,888ETYPE_DES3_CBC_MD5,889ETYPE_ARCFOUR_HMAC_MD5,890ETYPE_DES_CBC_MD5,891ETYPE_DES_CBC_MD4,892ETYPE_DES_CBC_CRC,893ETYPE_NULL894};895return p;896}897898/*899*900*/901902static krb5_error_code903copy_enctypes(krb5_context context,904const krb5_enctype *in,905krb5_enctype **out)906{907krb5_enctype *p = NULL;908size_t m, n;909910for (n = 0; in[n]; n++)911;912n++;913ALLOC(p, n);914if(p == NULL)915return krb5_enomem(context);916for (n = 0, m = 0; in[n]; n++) {917if (krb5_enctype_valid(context, in[n]) != 0)918continue;919p[m++] = in[n];920}921p[m] = KRB5_ENCTYPE_NULL;922if (m == 0) {923free(p);924krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,925N_("no valid enctype set", ""));926return KRB5_PROG_ETYPE_NOSUPP;927}928*out = p;929return 0;930}931932933/*934* set `etype' to a malloced list of the default enctypes935*/936937static krb5_error_code938default_etypes(krb5_context context, krb5_enctype **etype)939{940const krb5_enctype *p = krb5_kerberos_enctypes(context);941return copy_enctypes(context, p, etype);942}943944/**945* Set the default encryption types that will be use in communcation946* with the KDC, clients and servers.947*948* @param context Kerberos 5 context.949* @param etypes Encryption types, array terminated with ETYPE_NULL (0).950*951* @return Returns 0 to indicate success. Otherwise an kerberos et952* error code is returned, see krb5_get_error_message().953*954* @ingroup krb5955*/956957KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL958krb5_set_default_in_tkt_etypes(krb5_context context,959const krb5_enctype *etypes)960{961krb5_error_code ret;962krb5_enctype *p = NULL;963964if(etypes) {965ret = copy_enctypes(context, etypes, &p);966if (ret)967return ret;968}969if(context->etypes)970free(context->etypes);971context->etypes = p;972return 0;973}974975/**976* Get the default encryption types that will be use in communcation977* with the KDC, clients and servers.978*979* @param context Kerberos 5 context.980* @param etypes Encryption types, array terminated with981* ETYPE_NULL(0), caller should free array with krb5_xfree():982*983* @return Returns 0 to indicate success. Otherwise an kerberos et984* error code is returned, see krb5_get_error_message().985*986* @ingroup krb5987*/988989KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL990krb5_get_default_in_tkt_etypes(krb5_context context,991krb5_pdu pdu_type,992krb5_enctype **etypes)993{994krb5_enctype *enctypes = NULL;995krb5_error_code ret;996krb5_enctype *p;997998heim_assert(pdu_type == KRB5_PDU_AS_REQUEST ||999pdu_type == KRB5_PDU_TGS_REQUEST ||1000pdu_type == KRB5_PDU_NONE, "pdu contant not as expected");10011002if (pdu_type == KRB5_PDU_AS_REQUEST && context->as_etypes != NULL)1003enctypes = context->as_etypes;1004else if (pdu_type == KRB5_PDU_TGS_REQUEST && context->tgs_etypes != NULL)1005enctypes = context->tgs_etypes;1006else if (context->etypes != NULL)1007enctypes = context->etypes;10081009if (enctypes != NULL) {1010ret = copy_enctypes(context, enctypes, &p);1011if (ret)1012return ret;1013} else {1014ret = default_etypes(context, &p);1015if (ret)1016return ret;1017}1018*etypes = p;1019return 0;1020}10211022/**1023* Init the built-in ets in the Kerberos library.1024*1025* @param context kerberos context to add the ets too1026*1027* @ingroup krb51028*/10291030KRB5_LIB_FUNCTION void KRB5_LIB_CALL1031krb5_init_ets(krb5_context context)1032{1033if(context->et_list == NULL){1034krb5_add_et_list(context, initialize_krb5_error_table_r);1035krb5_add_et_list(context, initialize_asn1_error_table_r);1036krb5_add_et_list(context, initialize_heim_error_table_r);10371038krb5_add_et_list(context, initialize_k524_error_table_r);10391040#ifdef COM_ERR_BINDDOMAIN_krb51041bindtextdomain(COM_ERR_BINDDOMAIN_krb5, HEIMDAL_LOCALEDIR);1042bindtextdomain(COM_ERR_BINDDOMAIN_asn1, HEIMDAL_LOCALEDIR);1043bindtextdomain(COM_ERR_BINDDOMAIN_heim, HEIMDAL_LOCALEDIR);1044bindtextdomain(COM_ERR_BINDDOMAIN_k524, HEIMDAL_LOCALEDIR);1045#endif10461047#ifdef PKINIT1048krb5_add_et_list(context, initialize_hx_error_table_r);1049#ifdef COM_ERR_BINDDOMAIN_hx1050bindtextdomain(COM_ERR_BINDDOMAIN_hx, HEIMDAL_LOCALEDIR);1051#endif1052#endif1053}1054}10551056/**1057* Make the kerberos library default to the admin KDC.1058*1059* @param context Kerberos 5 context.1060* @param flag boolean flag to select if the use the admin KDC or not.1061*1062* @ingroup krb51063*/10641065KRB5_LIB_FUNCTION void KRB5_LIB_CALL1066krb5_set_use_admin_kdc (krb5_context context, krb5_boolean flag)1067{1068context->use_admin_kdc = flag;1069}10701071/**1072* Make the kerberos library default to the admin KDC.1073*1074* @param context Kerberos 5 context.1075*1076* @return boolean flag to telling the context will use admin KDC as the default KDC.1077*1078* @ingroup krb51079*/10801081KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL1082krb5_get_use_admin_kdc (krb5_context context)1083{1084return context->use_admin_kdc;1085}10861087/**1088* Add extra address to the address list that the library will add to1089* the client's address list when communicating with the KDC.1090*1091* @param context Kerberos 5 context.1092* @param addresses addreses to add1093*1094* @return Returns 0 to indicate success. Otherwise an kerberos et1095* error code is returned, see krb5_get_error_message().1096*1097* @ingroup krb51098*/10991100KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1101krb5_add_extra_addresses(krb5_context context, krb5_addresses *addresses)1102{11031104if(context->extra_addresses)1105return krb5_append_addresses(context,1106context->extra_addresses, addresses);1107else1108return krb5_set_extra_addresses(context, addresses);1109}11101111/**1112* Set extra address to the address list that the library will add to1113* the client's address list when communicating with the KDC.1114*1115* @param context Kerberos 5 context.1116* @param addresses addreses to set1117*1118* @return Returns 0 to indicate success. Otherwise an kerberos et1119* error code is returned, see krb5_get_error_message().1120*1121* @ingroup krb51122*/11231124KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1125krb5_set_extra_addresses(krb5_context context, const krb5_addresses *addresses)1126{1127if(context->extra_addresses)1128krb5_free_addresses(context, context->extra_addresses);11291130if(addresses == NULL) {1131if(context->extra_addresses != NULL) {1132free(context->extra_addresses);1133context->extra_addresses = NULL;1134}1135return 0;1136}1137if(context->extra_addresses == NULL) {1138context->extra_addresses = malloc(sizeof(*context->extra_addresses));1139if(context->extra_addresses == NULL) {1140krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));1141return ENOMEM;1142}1143}1144return krb5_copy_addresses(context, addresses, context->extra_addresses);1145}11461147/**1148* Get extra address to the address list that the library will add to1149* the client's address list when communicating with the KDC.1150*1151* @param context Kerberos 5 context.1152* @param addresses addreses to set1153*1154* @return Returns 0 to indicate success. Otherwise an kerberos et1155* error code is returned, see krb5_get_error_message().1156*1157* @ingroup krb51158*/11591160KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1161krb5_get_extra_addresses(krb5_context context, krb5_addresses *addresses)1162{1163if(context->extra_addresses == NULL) {1164memset(addresses, 0, sizeof(*addresses));1165return 0;1166}1167return krb5_copy_addresses(context,context->extra_addresses, addresses);1168}11691170/**1171* Add extra addresses to ignore when fetching addresses from the1172* underlaying operating system.1173*1174* @param context Kerberos 5 context.1175* @param addresses addreses to ignore1176*1177* @return Returns 0 to indicate success. Otherwise an kerberos et1178* error code is returned, see krb5_get_error_message().1179*1180* @ingroup krb51181*/11821183KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1184krb5_add_ignore_addresses(krb5_context context, krb5_addresses *addresses)1185{11861187if(context->ignore_addresses)1188return krb5_append_addresses(context,1189context->ignore_addresses, addresses);1190else1191return krb5_set_ignore_addresses(context, addresses);1192}11931194/**1195* Set extra addresses to ignore when fetching addresses from the1196* underlaying operating system.1197*1198* @param context Kerberos 5 context.1199* @param addresses addreses to ignore1200*1201* @return Returns 0 to indicate success. Otherwise an kerberos et1202* error code is returned, see krb5_get_error_message().1203*1204* @ingroup krb51205*/12061207KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1208krb5_set_ignore_addresses(krb5_context context, const krb5_addresses *addresses)1209{1210if(context->ignore_addresses)1211krb5_free_addresses(context, context->ignore_addresses);1212if(addresses == NULL) {1213if(context->ignore_addresses != NULL) {1214free(context->ignore_addresses);1215context->ignore_addresses = NULL;1216}1217return 0;1218}1219if(context->ignore_addresses == NULL) {1220context->ignore_addresses = malloc(sizeof(*context->ignore_addresses));1221if(context->ignore_addresses == NULL) {1222krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));1223return ENOMEM;1224}1225}1226return krb5_copy_addresses(context, addresses, context->ignore_addresses);1227}12281229/**1230* Get extra addresses to ignore when fetching addresses from the1231* underlaying operating system.1232*1233* @param context Kerberos 5 context.1234* @param addresses list addreses ignored1235*1236* @return Returns 0 to indicate success. Otherwise an kerberos et1237* error code is returned, see krb5_get_error_message().1238*1239* @ingroup krb51240*/12411242KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1243krb5_get_ignore_addresses(krb5_context context, krb5_addresses *addresses)1244{1245if(context->ignore_addresses == NULL) {1246memset(addresses, 0, sizeof(*addresses));1247return 0;1248}1249return krb5_copy_addresses(context, context->ignore_addresses, addresses);1250}12511252/**1253* Set version of fcache that the library should use.1254*1255* @param context Kerberos 5 context.1256* @param version version number.1257*1258* @return Returns 0 to indicate success. Otherwise an kerberos et1259* error code is returned, see krb5_get_error_message().1260*1261* @ingroup krb51262*/12631264KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1265krb5_set_fcache_version(krb5_context context, int version)1266{1267context->fcache_vno = version;1268return 0;1269}12701271/**1272* Get version of fcache that the library should use.1273*1274* @param context Kerberos 5 context.1275* @param version version number.1276*1277* @return Returns 0 to indicate success. Otherwise an kerberos et1278* error code is returned, see krb5_get_error_message().1279*1280* @ingroup krb51281*/12821283KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1284krb5_get_fcache_version(krb5_context context, int *version)1285{1286*version = context->fcache_vno;1287return 0;1288}12891290/**1291* Runtime check if the Kerberos library was complied with thread support.1292*1293* @return TRUE if the library was compiled with thread support, FALSE if not.1294*1295* @ingroup krb51296*/129712981299KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL1300krb5_is_thread_safe(void)1301{1302#ifdef ENABLE_PTHREAD_SUPPORT1303return TRUE;1304#else1305return FALSE;1306#endif1307}13081309/**1310* Set if the library should use DNS to canonicalize hostnames.1311*1312* @param context Kerberos 5 context.1313* @param flag if its dns canonicalizion is used or not.1314*1315* @ingroup krb51316*/13171318KRB5_LIB_FUNCTION void KRB5_LIB_CALL1319krb5_set_dns_canonicalize_hostname (krb5_context context, krb5_boolean flag)1320{1321if (flag)1322context->flags |= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;1323else1324context->flags &= ~KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;1325}13261327/**1328* Get if the library uses DNS to canonicalize hostnames.1329*1330* @param context Kerberos 5 context.1331*1332* @return return non zero if the library uses DNS to canonicalize hostnames.1333*1334* @ingroup krb51335*/13361337KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL1338krb5_get_dns_canonicalize_hostname (krb5_context context)1339{1340return (context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) ? 1 : 0;1341}13421343/**1344* Get current offset in time to the KDC.1345*1346* @param context Kerberos 5 context.1347* @param sec seconds part of offset.1348* @param usec micro seconds part of offset.1349*1350* @return returns zero1351*1352* @ingroup krb51353*/13541355KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1356krb5_get_kdc_sec_offset (krb5_context context, int32_t *sec, int32_t *usec)1357{1358if (sec)1359*sec = context->kdc_sec_offset;1360if (usec)1361*usec = context->kdc_usec_offset;1362return 0;1363}13641365/**1366* Set current offset in time to the KDC.1367*1368* @param context Kerberos 5 context.1369* @param sec seconds part of offset.1370* @param usec micro seconds part of offset.1371*1372* @return returns zero1373*1374* @ingroup krb51375*/13761377KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1378krb5_set_kdc_sec_offset (krb5_context context, int32_t sec, int32_t usec)1379{1380context->kdc_sec_offset = sec;1381if (usec >= 0)1382context->kdc_usec_offset = usec;1383return 0;1384}13851386/**1387* Get max time skew allowed.1388*1389* @param context Kerberos 5 context.1390*1391* @return timeskew in seconds.1392*1393* @ingroup krb51394*/13951396KRB5_LIB_FUNCTION time_t KRB5_LIB_CALL1397krb5_get_max_time_skew (krb5_context context)1398{1399return context->max_skew;1400}14011402/**1403* Set max time skew allowed.1404*1405* @param context Kerberos 5 context.1406* @param t timeskew in seconds.1407*1408* @ingroup krb51409*/14101411KRB5_LIB_FUNCTION void KRB5_LIB_CALL1412krb5_set_max_time_skew (krb5_context context, time_t t)1413{1414context->max_skew = t;1415}14161417/*1418* Init encryption types in len, val with etypes.1419*1420* @param context Kerberos 5 context.1421* @param pdu_type type of pdu1422* @param len output length of val.1423* @param val output array of enctypes.1424* @param etypes etypes to set val and len to, if NULL, use default enctypes.14251426* @return Returns 0 to indicate success. Otherwise an kerberos et1427* error code is returned, see krb5_get_error_message().1428*1429* @ingroup krb51430*/14311432KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL1433_krb5_init_etype(krb5_context context,1434krb5_pdu pdu_type,1435unsigned *len,1436krb5_enctype **val,1437const krb5_enctype *etypes)1438{1439krb5_error_code ret;14401441if (etypes == NULL)1442ret = krb5_get_default_in_tkt_etypes(context, pdu_type, val);1443else1444ret = copy_enctypes(context, etypes, val);1445if (ret)1446return ret;14471448if (len) {1449*len = 0;1450while ((*val)[*len] != KRB5_ENCTYPE_NULL)1451(*len)++;1452}1453return 0;1454}14551456/*1457* Allow homedir accces1458*/14591460static HEIMDAL_MUTEX homedir_mutex = HEIMDAL_MUTEX_INITIALIZER;1461static krb5_boolean allow_homedir = TRUE;14621463krb5_boolean1464_krb5_homedir_access(krb5_context context)1465{1466krb5_boolean allow;14671468#ifdef HAVE_GETEUID1469/* is never allowed for root */1470if (geteuid() == 0)1471return FALSE;1472#endif14731474if (context && (context->flags & KRB5_CTX_F_HOMEDIR_ACCESS) == 0)1475return FALSE;14761477HEIMDAL_MUTEX_lock(&homedir_mutex);1478allow = allow_homedir;1479HEIMDAL_MUTEX_unlock(&homedir_mutex);1480return allow;1481}14821483/**1484* Enable and disable home directory access on either the global state1485* or the krb5_context state. By calling krb5_set_home_dir_access()1486* with context set to NULL, the global state is configured otherwise1487* the state for the krb5_context is modified.1488*1489* For home directory access to be allowed, both the global state and1490* the krb5_context state have to be allowed.1491*1492* Administrator (root user), never uses the home directory.1493*1494* @param context a Kerberos 5 context or NULL1495* @param allow allow if TRUE home directory1496* @return the old value1497*1498* @ingroup krb51499*/15001501KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL1502krb5_set_home_dir_access(krb5_context context, krb5_boolean allow)1503{1504krb5_boolean old;1505if (context) {1506old = (context->flags & KRB5_CTX_F_HOMEDIR_ACCESS) ? TRUE : FALSE;1507if (allow)1508context->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;1509else1510context->flags &= ~KRB5_CTX_F_HOMEDIR_ACCESS;1511} else {1512HEIMDAL_MUTEX_lock(&homedir_mutex);1513old = allow_homedir;1514allow_homedir = allow;1515HEIMDAL_MUTEX_unlock(&homedir_mutex);1516}15171518return old;1519}152015211522