LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - aname_to_localname.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 0 184 0.0 %
Date: 2024-06-13 04:01:37 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 1999, 2002 - 2003 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 <string.h>
      35             : #include "krb5_locl.h"
      36             : #include "an2ln_plugin.h"
      37             : #include "db_plugin.h"
      38             : 
      39             : /* Default plugin (DB using binary search of sorted text file) follows */
      40             : static krb5_error_code KRB5_LIB_CALL an2ln_def_plug_init(krb5_context, void **);
      41             : static void KRB5_LIB_CALL an2ln_def_plug_fini(void *);
      42             : static krb5_error_code KRB5_LIB_CALL an2ln_def_plug_an2ln(void *, krb5_context, const char *,
      43             :                                             krb5_const_principal, set_result_f,
      44             :                                             void *);
      45             : 
      46             : static krb5plugin_an2ln_ftable an2ln_def_plug = {
      47             :     0,
      48             :     an2ln_def_plug_init,
      49             :     an2ln_def_plug_fini,
      50             :     an2ln_def_plug_an2ln,
      51             : };
      52             : 
      53             : /* Plugin engine code follows */
      54             : struct plctx {
      55             :     krb5_const_principal aname;
      56             :     heim_string_t luser;
      57             :     const char *rule;
      58             : };
      59             : 
      60             : static krb5_error_code KRB5_LIB_CALL
      61           0 : set_res(void *userctx, const char *res)
      62             : {
      63           0 :     struct plctx *plctx = userctx;
      64           0 :     plctx->luser = heim_string_create(res);
      65           0 :     if (plctx->luser == NULL)
      66           0 :         return ENOMEM;
      67           0 :     return 0;
      68             : }
      69             : 
      70             : static krb5_error_code KRB5_LIB_CALL
      71           0 : plcallback(krb5_context context,
      72             :            const void *plug, void *plugctx, void *userctx)
      73             : {
      74           0 :     const krb5plugin_an2ln_ftable *locate = plug;
      75           0 :     struct plctx *plctx = userctx;
      76             : 
      77           0 :     if (plctx->luser)
      78           0 :         return 0;
      79             :     
      80           0 :     return locate->an2ln(plugctx, context, plctx->rule, plctx->aname, set_res, plctx);
      81             : }
      82             : 
      83             : static const char *an2ln_plugin_deps[] = { "krb5", NULL };
      84             : 
      85             : static struct heim_plugin_data
      86             : an2ln_plugin_data = {
      87             :     "krb5",
      88             :     KRB5_PLUGIN_AN2LN,
      89             :     KRB5_PLUGIN_AN2LN_VERSION_0,
      90             :     an2ln_plugin_deps,
      91             :     krb5_get_instance
      92             : };
      93             : 
      94             : static krb5_error_code
      95           0 : an2ln_plugin(krb5_context context, const char *rule, krb5_const_principal aname,
      96             :              size_t lnsize, char *lname)
      97             : {
      98             :     krb5_error_code ret;
      99             :     struct plctx ctx;
     100             : 
     101           0 :     ctx.rule = rule;
     102           0 :     ctx.aname = aname;
     103           0 :     ctx.luser = NULL;
     104             : 
     105             :     /*
     106             :      * Order of plugin invocation is non-deterministic, but there should
     107             :      * really be no more than one plugin that can handle any given kind
     108             :      * rule, so the effect should be deterministic anyways.
     109             :      */
     110           0 :     ret = _krb5_plugin_run_f(context, &an2ln_plugin_data,
     111             :                              0, &ctx, plcallback);
     112           0 :     if (ret != 0) {
     113           0 :         heim_release(ctx.luser);
     114           0 :         return ret;
     115             :     }
     116             : 
     117           0 :     if (ctx.luser == NULL)
     118           0 :         return KRB5_PLUGIN_NO_HANDLE;
     119             : 
     120           0 :     if (strlcpy(lname, heim_string_get_utf8(ctx.luser), lnsize) >= lnsize)
     121           0 :         ret = KRB5_CONFIG_NOTENUFSPACE;
     122             : 
     123           0 :     heim_release(ctx.luser);
     124           0 :     return ret;
     125             : }
     126             : 
     127             : static void
     128           0 : reg_def_plugins_once(void *ctx)
     129             : {
     130           0 :     krb5_context context = ctx;
     131             : 
     132           0 :     krb5_plugin_register(context, PLUGIN_TYPE_DATA, KRB5_PLUGIN_AN2LN,
     133             :                          &an2ln_def_plug);
     134           0 : }
     135             : 
     136             : static int
     137           0 : princ_realm_is_default(krb5_context context,
     138             :                        krb5_const_principal aname)
     139             : {
     140             :     krb5_error_code ret;
     141           0 :     krb5_realm *lrealms = NULL;
     142             :     krb5_realm *r;
     143             :     int valid;
     144             : 
     145           0 :     ret = krb5_get_default_realms(context, &lrealms);
     146           0 :     if (ret)
     147           0 :         return 0;
     148             : 
     149           0 :     valid = 0;
     150           0 :     for (r = lrealms; *r != NULL; ++r) {
     151           0 :         if (strcmp (*r, aname->realm) == 0) {
     152           0 :             valid = 1;
     153           0 :             break;
     154             :         }
     155             :     }
     156           0 :     krb5_free_host_realm (context, lrealms);
     157           0 :     return valid;
     158             : }
     159             : 
     160             : /*
     161             :  * This function implements MIT's auth_to_local_names configuration for
     162             :  * configuration compatibility.  Specifically:
     163             :  *
     164             :  * [realms]
     165             :  *     <realm-name> = {
     166             :  *         auth_to_local_names = {
     167             :  *             <unparsed-principal-name> = <username>
     168             :  *         }
     169             :  *     }
     170             :  *
     171             :  * If multiple usernames are configured then the last one is taken.
     172             :  *
     173             :  * The configuration can only be expected to hold a relatively small
     174             :  * number of mappings.  For lots of mappings use a DB.
     175             :  */
     176             : static krb5_error_code
     177           0 : an2ln_local_names(krb5_context context,
     178             :                   krb5_const_principal aname,
     179             :                   size_t lnsize,
     180             :                   char *lname)
     181             : {
     182             :     krb5_error_code ret;
     183             :     char *unparsed;
     184             :     char **values;
     185             :     char *res;
     186             :     size_t i;
     187             : 
     188           0 :     if (!princ_realm_is_default(context, aname))
     189           0 :         return KRB5_PLUGIN_NO_HANDLE;
     190             : 
     191           0 :     ret = krb5_unparse_name_flags(context, aname,
     192             :                                   KRB5_PRINCIPAL_UNPARSE_NO_REALM,
     193             :                                   &unparsed);
     194           0 :     if (ret)
     195           0 :         return ret;
     196             : 
     197           0 :     ret = KRB5_PLUGIN_NO_HANDLE;
     198           0 :     values = krb5_config_get_strings(context, NULL, "realms", aname->realm,
     199             :                                      "auth_to_local_names", unparsed, NULL);
     200           0 :     free(unparsed);
     201           0 :     if (!values)
     202           0 :         return ret;
     203             :     /* Take the last value, just like MIT */
     204           0 :     for (res = NULL, i = 0; values[i]; i++)
     205           0 :         res = values[i];
     206           0 :     if (res) {
     207           0 :         ret = 0;
     208           0 :         if (strlcpy(lname, res, lnsize) >= lnsize)
     209           0 :             ret = KRB5_CONFIG_NOTENUFSPACE;
     210             : 
     211           0 :         if (!*res || strcmp(res, ":") == 0)
     212           0 :             ret = KRB5_NO_LOCALNAME;
     213             :     }
     214             : 
     215           0 :     krb5_config_free_strings(values);
     216           0 :     return ret;
     217             : }
     218             : 
     219             : /*
     220             :  * Heimdal's default aname2lname mapping.
     221             :  */
     222             : static krb5_error_code
     223           0 : an2ln_default(krb5_context context,
     224             :               char *rule,
     225             :               krb5_const_principal aname,
     226             :               size_t lnsize, char *lname)
     227             : {
     228             :     krb5_error_code ret;
     229             :     const char *res;
     230             :     int root_princs_ok;
     231             : 
     232           0 :     if (strcmp(rule, "NONE") == 0)
     233           0 :         return KRB5_NO_LOCALNAME;
     234             : 
     235           0 :     if (strcmp(rule, "DEFAULT") == 0)
     236           0 :         root_princs_ok = 0;
     237           0 :     else if (strcmp(rule, "HEIMDAL_DEFAULT") == 0)
     238           0 :         root_princs_ok = 1;
     239             :     else
     240           0 :         return KRB5_PLUGIN_NO_HANDLE;
     241             : 
     242           0 :     if (!princ_realm_is_default(context, aname))
     243           0 :         return KRB5_PLUGIN_NO_HANDLE;
     244             : 
     245           0 :     if (aname->name.name_string.len == 1) {
     246             :         /*
     247             :          * One component principal names in default realm -> the one
     248             :          * component is the username.
     249             :          */
     250           0 :         res = aname->name.name_string.val[0];
     251           0 :     } else if (root_princs_ok && aname->name.name_string.len == 2 &&
     252           0 :                strcmp (aname->name.name_string.val[1], "root") == 0) {
     253             :         /*
     254             :          * Two-component principal names in default realm where the
     255             :          * first component is "root" -> root IFF the principal is in
     256             :          * root's .k5login (or whatever krb5_kuserok() does).
     257             :          */
     258             :         krb5_principal rootprinc;
     259             :         krb5_boolean userok;
     260             : 
     261           0 :         res = "root";
     262             : 
     263           0 :         ret = krb5_copy_principal(context, aname, &rootprinc);
     264           0 :         if (ret)
     265           0 :             return ret;
     266             : 
     267           0 :         userok = _krb5_kuserok(context, rootprinc, res, FALSE);
     268           0 :         krb5_free_principal(context, rootprinc);
     269           0 :         if (!userok)
     270           0 :             return KRB5_NO_LOCALNAME;
     271             :     } else {
     272           0 :         return KRB5_PLUGIN_NO_HANDLE;
     273             :     }
     274             : 
     275           0 :     if (strlcpy(lname, res, lnsize) >= lnsize)
     276           0 :         return KRB5_CONFIG_NOTENUFSPACE;
     277             : 
     278           0 :     return 0;
     279             : }
     280             : 
     281             : /**
     282             :  * Map a principal name to a local username.
     283             :  *
     284             :  * Returns 0 on success, KRB5_NO_LOCALNAME if no mapping was found, or
     285             :  * some Kerberos or system error.
     286             :  *
     287             :  * Inputs:
     288             :  *
     289             :  * @param context    A krb5_context
     290             :  * @param aname      A principal name
     291             :  * @param lnsize     The size of the buffer into which the username will be written
     292             :  * @param lname      The buffer into which the username will be written
     293             :  *
     294             :  * @ingroup krb5_support
     295             :  */
     296             : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     297           0 : krb5_aname_to_localname(krb5_context context,
     298             :                         krb5_const_principal aname,
     299             :                         size_t lnsize,
     300             :                         char *lname)
     301             : {
     302             :     static heim_base_once_t reg_def_plugins = HEIM_BASE_ONCE_INIT;
     303             :     krb5_error_code ret;
     304             :     krb5_realm realm;
     305             :     size_t i;
     306           0 :     char **rules = NULL;
     307             :     char *rule;
     308             : 
     309           0 :     if (lnsize)
     310           0 :         lname[0] = '\0';
     311             : 
     312           0 :     heim_base_once_f(&reg_def_plugins, context, reg_def_plugins_once);
     313             : 
     314             :     /* Try MIT's auth_to_local_names config first */
     315           0 :     ret = an2ln_local_names(context, aname, lnsize, lname);
     316           0 :     if (ret != KRB5_PLUGIN_NO_HANDLE)
     317           0 :         return ret;
     318             : 
     319           0 :     ret = krb5_get_default_realm(context, &realm);
     320           0 :     if (ret)
     321           0 :         return ret;
     322             : 
     323           0 :     rules = krb5_config_get_strings(context, NULL, "realms", realm,
     324             :                                     "auth_to_local", NULL);
     325           0 :     krb5_xfree(realm);
     326           0 :     if (!rules) {
     327             :         /* Heimdal's default rule */
     328           0 :         ret = an2ln_default(context, "HEIMDAL_DEFAULT", aname, lnsize, lname);
     329           0 :         if (ret == KRB5_PLUGIN_NO_HANDLE)
     330           0 :             return KRB5_NO_LOCALNAME;
     331           0 :         return ret;
     332             :     }
     333             : 
     334             :     /*
     335             :      * MIT rules.
     336             :      *
     337             :      * Note that RULEs and DBs only have white-list functionality,
     338             :      * thus RULEs and DBs that we don't understand we simply ignore.
     339             :      *
     340             :      * This means that plugins that implement black-lists are
     341             :      * dangerous: if a black-list plugin isn't found, the black-list
     342             :      * won't be enforced.  But black-lists are dangerous anyways.
     343             :      */
     344           0 :     for (ret = KRB5_PLUGIN_NO_HANDLE, i = 0; rules[i]; i++) {
     345           0 :         rule = rules[i];
     346             : 
     347             :         /* Try NONE, DEFAULT, and HEIMDAL_DEFAULT rules */
     348           0 :         ret = an2ln_default(context, rule, aname, lnsize, lname);
     349           0 :         if (ret == KRB5_PLUGIN_NO_HANDLE)
     350             :             /* Try DB, RULE, ... plugins */
     351           0 :             ret = an2ln_plugin(context, rule, aname, lnsize, lname);
     352             : 
     353           0 :         if (ret == 0 && lnsize && !lname[0])
     354           0 :             continue; /* Success but no lname?!  lies! */
     355           0 :         else if (ret != KRB5_PLUGIN_NO_HANDLE)
     356           0 :             break;
     357             :     }
     358             : 
     359           0 :     if (ret == KRB5_PLUGIN_NO_HANDLE) {
     360           0 :         if (lnsize)
     361           0 :             lname[0] = '\0';
     362           0 :         ret = KRB5_NO_LOCALNAME;
     363             :     }
     364             : 
     365           0 :     krb5_config_free_strings(rules);
     366           0 :     return ret;
     367             : }
     368             : 
     369             : static krb5_error_code KRB5_LIB_CALL
     370           0 : an2ln_def_plug_init(krb5_context context, void **ctx)
     371             : {
     372           0 :     *ctx = NULL;
     373           0 :     return 0;
     374             : }
     375             : 
     376             : static void KRB5_LIB_CALL
     377           0 : an2ln_def_plug_fini(void *ctx)
     378             : {
     379           0 : }
     380             : 
     381             : static heim_base_once_t sorted_text_db_init_once = HEIM_BASE_ONCE_INIT;
     382             : 
     383             : static void
     384           0 : sorted_text_db_init_f(void *arg)
     385             : {
     386           0 :     (void) heim_db_register("sorted-text", NULL, &heim_sorted_text_file_dbtype);
     387           0 : }
     388             : 
     389             : static krb5_error_code KRB5_LIB_CALL
     390           0 : an2ln_def_plug_an2ln(void *plug_ctx, krb5_context context,
     391             :                      const char *rule,
     392             :                      krb5_const_principal aname,
     393             :                      set_result_f set_res_f, void *set_res_ctx)
     394             : {
     395             :     krb5_error_code ret;
     396             :     const char *an2ln_db_fname;
     397           0 :     heim_db_t dbh = NULL;
     398             :     heim_dict_t db_options;
     399             :     heim_data_t k, v;
     400             :     heim_error_t error;
     401           0 :     char *unparsed = NULL;
     402           0 :     char *value = NULL;
     403             : 
     404           0 :     _krb5_load_db_plugins(context);
     405           0 :     heim_base_once_f(&sorted_text_db_init_once, NULL, sorted_text_db_init_f);
     406             : 
     407           0 :     if (strncmp(rule, "DB:", strlen("DB:")) != 0)
     408           0 :         return KRB5_PLUGIN_NO_HANDLE;
     409             : 
     410           0 :     an2ln_db_fname = &rule[strlen("DB:")];
     411           0 :     if (!*an2ln_db_fname)
     412           0 :         return KRB5_PLUGIN_NO_HANDLE;
     413             : 
     414           0 :     ret = krb5_unparse_name(context, aname, &unparsed);
     415           0 :     if (ret)
     416           0 :         return ret;
     417             : 
     418           0 :     db_options = heim_dict_create(11);
     419           0 :     if (db_options != NULL)
     420           0 :         heim_dict_set_value(db_options, HSTR("read-only"),
     421           0 :                             heim_number_create(1));
     422           0 :     dbh = heim_db_create(NULL, an2ln_db_fname, db_options, &error);
     423           0 :     if (dbh == NULL) {
     424           0 :         krb5_set_error_message(context, heim_error_get_code(error),
     425           0 :                                N_("Couldn't open aname2lname-text-db", ""));
     426           0 :         ret = KRB5_PLUGIN_NO_HANDLE;
     427           0 :         goto cleanup;
     428             :     }
     429             : 
     430             :     /* Binary search; file should be sorted (in C locale) */
     431           0 :     k = heim_data_ref_create(unparsed, strlen(unparsed), NULL);
     432           0 :     if (k == NULL) {
     433           0 :         ret = krb5_enomem(context);
     434           0 :         goto cleanup;
     435             :     }
     436           0 :     v = heim_db_copy_value(dbh, NULL, k, &error);
     437           0 :     heim_release(k);
     438           0 :     if (v == NULL && error != NULL) {
     439           0 :         krb5_set_error_message(context, heim_error_get_code(error),
     440           0 :                                N_("Lookup in aname2lname-text-db failed", ""));
     441           0 :         ret = heim_error_get_code(error);
     442           0 :         goto cleanup;
     443           0 :     } else if (v == NULL) {
     444           0 :         ret = KRB5_PLUGIN_NO_HANDLE;
     445           0 :         goto cleanup;
     446             :     } else {
     447             :         /* found */
     448           0 :         if (heim_data_get_length(v) == 0) {
     449           0 :             krb5_set_error_message(context, ret,
     450           0 :                                    N_("Principal mapped to empty username", ""));
     451           0 :             ret = KRB5_NO_LOCALNAME;
     452           0 :             goto cleanup;
     453             :         }
     454           0 :         value = strndup(heim_data_get_ptr(v), heim_data_get_length(v));
     455           0 :         heim_release(v);
     456           0 :         if (value == NULL) {
     457           0 :             ret = krb5_enomem(context);
     458           0 :             goto cleanup;
     459             :         }
     460           0 :         ret = set_res_f(set_res_ctx, value);
     461             :     }
     462             : 
     463           0 : cleanup:
     464           0 :     heim_release(dbh);
     465           0 :     free(unparsed);
     466           0 :     free(value);
     467           0 :     return ret;
     468             : }
     469             : 

Generated by: LCOV version 1.13