LCOV - code coverage report
Current view: top level - third_party/heimdal/kuser - kgetcred.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 78 160 48.8 %
Date: 2024-04-21 15:09:00 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2008 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 "kuser_locl.h"
      35             : 
      36             : static char *cache_str;
      37             : static char *out_cache_str;
      38             : static char *delegation_cred_str;
      39             : static char *etype_str;
      40             : static int transit_flag = 1;
      41             : static int forwardable_flag;
      42             : static int canonicalize_flag;
      43             : static int is_hostbased_flag;
      44             : static int is_canonical_flag;
      45             : static char *impersonate_str;
      46             : static char *nametype_str;
      47             : static int store_flag = 1;
      48             : static int cached_only_flag;
      49             : static int anonymous_flag;
      50             : static int debug_flag;
      51             : static int version_flag;
      52             : static int help_flag;
      53             : 
      54             : struct getargs args[] = {
      55             :     { "cache",                'c', arg_string, &cache_str,
      56             :       NP_("credential cache to use", ""), "cache"},
      57             :     { "out-cache",    0,   arg_string, &out_cache_str,
      58             :       NP_("credential cache to store credential in", ""), "cache"},
      59             :     { "delegation-credential-cache",0,arg_string, &delegation_cred_str,
      60             :       NP_("where to find the ticket use for delegation", ""), "cache"},
      61             :     { "canonicalize", 0, arg_flag, &canonicalize_flag,
      62             :       NP_("canonicalize the principal (chase referrals)", ""), NULL },
      63             :     { "canonical",    0,   arg_flag, &is_canonical_flag,
      64             :       NP_("the name components are canonical", ""), NULL },
      65             :     { "forwardable",  0,   arg_flag, &forwardable_flag,
      66             :       NP_("forwardable ticket requested", ""), NULL},
      67             :     { "transit-check",        0,   arg_negative_flag, &transit_flag, NULL, NULL },
      68             :     { "enctype",      'e', arg_string, &etype_str,
      69             :       NP_("encryption type to use", ""), "enctype"},
      70             :     { "impersonate",  0,   arg_string, &impersonate_str,
      71             :       NP_("client to impersonate", ""), "principal"},
      72             :     { "name-type",    0,   arg_string, &nametype_str,
      73             :       NP_("Kerberos name type", ""), NULL },
      74             :     { "hostbased",    'H', arg_flag, &is_hostbased_flag,
      75             :       NP_("indicate that the name is a host-based service name", ""), NULL },
      76             :     { "store",                0,   arg_negative_flag, &store_flag,
      77             :       NP_("don't store the tickets obtained in the cache", ""), NULL },
      78             :     { "cached-only",          0,   arg_flag, &cached_only_flag,
      79             :       NP_("don't talk to the KDC, just search the cache", ""), NULL },
      80             :     { "anonymous",      'n',   arg_flag, &anonymous_flag,
      81             :       NP_("request an anonymous ticket", ""), NULL },
      82             :     { "debug",                0,   arg_flag, &debug_flag, NULL, NULL },
      83             :     { "version",      0,   arg_flag, &version_flag, NULL, NULL },
      84             :     { "help",         0,   arg_flag, &help_flag, NULL, NULL }
      85             : };
      86             : 
      87             : static void
      88           0 : usage(int ret)
      89             : {
      90           0 :     arg_printusage(args,
      91             :                    sizeof(args)/sizeof(*args),
      92             :                    NULL,
      93             :                    "service");
      94           0 :     exit (ret);
      95             : }
      96             : 
      97             : int
      98          12 : main(int argc, char **argv)
      99             : {
     100             :     krb5_error_code ret;
     101             :     krb5_context context;
     102             :     krb5_ccache cache;
     103             :     krb5_creds *out;
     104          12 :     int optidx = 0;
     105          12 :     int32_t nametype = KRB5_NT_UNKNOWN;
     106             :     krb5_get_creds_opt opt;
     107          12 :     krb5_principal server = NULL;
     108             :     krb5_principal impersonate;
     109             : 
     110          12 :     setprogname(argv[0]);
     111             : 
     112          12 :     ret = krb5_init_context(&context);
     113          12 :     if (ret)
     114           0 :         errx(1, "krb5_init_context failed: %d", ret);
     115             : 
     116          12 :     if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
     117           0 :         usage(1);
     118             : 
     119          12 :     if (help_flag)
     120           0 :         usage (0);
     121             : 
     122          12 :     if (version_flag) {
     123           0 :         print_version(NULL);
     124           0 :         exit(0);
     125             :     }
     126             : 
     127          12 :     argc -= optidx;
     128          12 :     argv += optidx;
     129             : 
     130          12 :     if (debug_flag) {
     131           0 :         ret = krb5_set_debug_dest(context, getprogname(), "STDERR");
     132           0 :         if (ret)
     133           0 :             krb5_warn(context, ret, "krb5_set_debug_dest");
     134             :     }
     135             : 
     136          12 :     if (cache_str) {
     137           3 :         ret = krb5_cc_resolve(context, cache_str, &cache);
     138           3 :         if (ret)
     139           0 :             krb5_err(context, 1, ret, "%s", cache_str);
     140             :     } else {
     141           9 :         ret = krb5_cc_default (context, &cache);
     142           9 :         if (ret)
     143           0 :             krb5_err(context, 1, ret, "krb5_cc_resolve");
     144             :     }
     145             : 
     146          12 :     ret = krb5_get_creds_opt_alloc(context, &opt);
     147          12 :     if (ret)
     148           0 :         krb5_err(context, 1, ret, "krb5_get_creds_opt_alloc");
     149             : 
     150          12 :     if (etype_str) {
     151             :         krb5_enctype enctype;
     152             : 
     153           0 :         ret = krb5_string_to_enctype(context, etype_str, &enctype);
     154           0 :         if (ret)
     155           0 :             krb5_errx(context, 1, N_("unrecognized enctype: %s", ""),
     156             :                       etype_str);
     157           0 :         krb5_get_creds_opt_set_enctype(context, opt, enctype);
     158             :     }
     159             : 
     160          12 :     if (impersonate_str) {
     161           3 :         ret = krb5_parse_name(context, impersonate_str, &impersonate);
     162           3 :         if (ret)
     163           0 :             krb5_err(context, 1, ret, "krb5_parse_name %s", impersonate_str);
     164           3 :         krb5_get_creds_opt_set_impersonate(context, opt, impersonate);
     165           3 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
     166           3 :         krb5_free_principal(context, impersonate);
     167             :     }
     168             : 
     169          12 :     if (out_cache_str)
     170           9 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
     171             : 
     172          12 :     if (forwardable_flag)
     173           6 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
     174          12 :     if (!transit_flag)
     175           0 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_TRANSIT_CHECK);
     176          12 :     if (canonicalize_flag)
     177           0 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CANONICALIZE);
     178          12 :     if (!store_flag)
     179           0 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
     180          12 :     if (cached_only_flag)
     181           0 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CACHED);
     182          12 :     if (anonymous_flag)
     183           0 :         krb5_get_creds_opt_add_options(context, opt, KRB5_GC_ANONYMOUS);
     184             : 
     185          12 :     if (delegation_cred_str) {
     186             :         krb5_ccache id;
     187             :         krb5_creds c, mc;
     188             :         Ticket ticket;
     189             : 
     190           6 :         krb5_cc_clear_mcred(&mc);
     191           6 :         ret = krb5_cc_get_principal(context, cache, &mc.server);
     192           6 :         if (ret)
     193           0 :             krb5_err(context, 1, ret, "krb5_cc_get_principal");
     194             : 
     195           6 :         ret = krb5_cc_resolve(context, delegation_cred_str, &id);
     196           6 :         if(ret)
     197           0 :             krb5_err(context, 1, ret, "krb5_cc_resolve");
     198             : 
     199           6 :         ret = krb5_cc_retrieve_cred(context, id, 0, &mc, &c);
     200           6 :         if(ret)
     201           0 :             krb5_err(context, 1, ret, "krb5_cc_retrieve_cred");
     202             : 
     203           6 :         ret = decode_Ticket(c.ticket.data, c.ticket.length, &ticket, NULL);
     204           6 :         if (ret) {
     205           0 :             krb5_clear_error_message(context);
     206           0 :             krb5_err(context, 1, ret, "decode_Ticket");
     207             :         }
     208           6 :         krb5_free_cred_contents(context, &c);
     209             : 
     210           6 :         ret = krb5_get_creds_opt_set_ticket(context, opt, &ticket);
     211           6 :         if(ret)
     212           0 :             krb5_err(context, 1, ret, "krb5_get_creds_opt_set_ticket");
     213           6 :         free_Ticket(&ticket);
     214             : 
     215           6 :         krb5_cc_close(context, id);
     216           6 :         krb5_free_principal(context, mc.server);
     217             : 
     218           6 :         krb5_get_creds_opt_add_options(context, opt,
     219             :                                        KRB5_GC_CONSTRAINED_DELEGATION);
     220             :     }
     221             : 
     222          12 :     if (nametype_str != NULL) {
     223           0 :         ret = krb5_parse_nametype(context, nametype_str, &nametype);
     224           0 :         if (ret)
     225           0 :             krb5_err(context, 1, ret, "krb5_parse_nametype");
     226             :     }
     227             : 
     228          12 :     if (nametype == KRB5_NT_SRV_HST ||
     229          12 :         nametype == KRB5_NT_SRV_HST_NEEDS_CANON)
     230           0 :         is_hostbased_flag = 1;
     231             : 
     232          12 :     if (is_hostbased_flag) {
     233           0 :         const char *sname = NULL;
     234           0 :         const char *hname = NULL;
     235             : 
     236           0 :         if (nametype_str != NULL &&
     237           0 :             nametype != KRB5_NT_SRV_HST &&
     238           0 :             nametype != KRB5_NT_SRV_HST_NEEDS_CANON)
     239           0 :             krb5_errx(context, 1, "--hostbased not compatible with "
     240             :                       "non-hostbased --name-type");
     241             : 
     242           0 :         if (is_canonical_flag)
     243           0 :             nametype = KRB5_NT_SRV_HST;
     244             :         else
     245           0 :             nametype = KRB5_NT_SRV_HST_NEEDS_CANON;
     246             : 
     247             :         /*
     248             :          * Host-based service names can have more than one component.
     249             :          *
     250             :          * RFC5179 did not, but should have, assign a Kerberos name-type
     251             :          * corresponding to GSS_C_NT_DOMAINBASED.  But it's basically a
     252             :          * host-based service name type with one additional component.
     253             :          *
     254             :          * So that's how we're treating host-based service names here:
     255             :          * two or more components.
     256             :          */
     257             : 
     258           0 :         if (argc == 0) {
     259           0 :             usage(1);
     260           0 :         } else if (argc == 1) {
     261             :             krb5_principal server2;
     262             : 
     263             :             /*
     264             :              * In this case the one argument is a principal name, not the
     265             :              * service name.
     266             :              *
     267             :              * We parse the argument as a principal name, extract the service
     268             :              * and hostname components, use krb5_sname_to_principal(), then
     269             :              * extract the service and hostname components from that.
     270             :              */
     271             : 
     272           0 :             ret = krb5_parse_name(context, argv[0], &server);
     273           0 :             if (ret)
     274           0 :                 krb5_err(context, 1, ret, "krb5_parse_name %s", argv[0]);
     275           0 :             sname = krb5_principal_get_comp_string(context, server, 0);
     276             : 
     277             :             /*
     278             :              * If a single-component principal name is given, then we'll
     279             :              * default the hostname, as krb5_principal_get_comp_string()
     280             :              * returns NULL in this case.
     281             :              */
     282           0 :             hname = krb5_principal_get_comp_string(context, server, 1);
     283             : 
     284           0 :             ret = krb5_sname_to_principal(context, hname, sname,
     285             :                                            KRB5_NT_SRV_HST, &server2);
     286           0 :             if (ret)
     287           0 :                 krb5_err(context, 1, ret, "krb5_sname_to_principal %s %s",
     288             :                          sname, hname);
     289           0 :             sname = krb5_principal_get_comp_string(context, server2, 0);
     290           0 :             hname = krb5_principal_get_comp_string(context, server2, 1);
     291             : 
     292             :             /*
     293             :              * Modify the original with the new sname/hname.  This way we
     294             :              * retain any additional principal name components from the given
     295             :              * principal name.
     296             :              *
     297             :              * The name-type is set further below.
     298             :              */
     299           0 :             ret = krb5_principal_set_comp_string(context, server, 0, sname);
     300           0 :             if (ret)
     301           0 :                 krb5_err(context, 1, ret, "krb5_principal_set_comp_string %s", argv[0]);
     302           0 :             ret = krb5_principal_set_comp_string(context, server, 1, hname);
     303           0 :             if (ret)
     304           0 :                 krb5_err(context, 1, ret, "krb5_principal_set_comp_string %s", argv[0]);
     305           0 :             krb5_free_principal(context, server2);
     306             :         } else {
     307             :             size_t i;
     308             : 
     309             :             /*
     310             :              * In this case the arguments are principal name components.
     311             :              *
     312             :              * The service and hostname components can be defaulted by passing
     313             :              * empty strings.
     314             :              */
     315           0 :             sname = argv[0];
     316           0 :             if (*sname == '\0')
     317           0 :                 sname = NULL;
     318           0 :             hname = argv[1];
     319           0 :             if (hname == NULL || *hname == '\0')
     320           0 :                 hname = NULL;
     321           0 :             ret = krb5_sname_to_principal(context, hname, sname,
     322             :                                           KRB5_NT_SRV_HST, &server);
     323           0 :             if (ret)
     324           0 :                 krb5_err(context, 1, ret, "krb5_sname_to_principal");
     325             : 
     326           0 :             for (i = 2; i < argc; i++) {
     327           0 :                 ret = krb5_principal_set_comp_string(context, server, i, argv[i]);
     328           0 :                 if (ret)
     329           0 :                     krb5_err(context, 1, ret, "krb5_principal_set_comp_string");
     330             :             }
     331             :         }
     332          12 :     } else if (argc == 1) {
     333          12 :         ret = krb5_parse_name(context, argv[0], &server);
     334          12 :         if (ret)
     335           0 :             krb5_err(context, 1, ret, "krb5_parse_name %s", argv[0]);
     336             :     } else {
     337           0 :         usage(1);
     338             :     }
     339             : 
     340          12 :     if (nametype != KRB5_NT_UNKNOWN)
     341           0 :         server->name.name_type = (NAME_TYPE)nametype;
     342             : 
     343          12 :     ret = krb5_get_creds(context, opt, cache, server, &out);
     344          12 :     if (ret)
     345           2 :         krb5_err(context, 1, ret, "krb5_get_creds");
     346             : 
     347          10 :     if (out_cache_str) {
     348             :         krb5_ccache id;
     349             : 
     350           7 :         ret = krb5_cc_resolve(context, out_cache_str, &id);
     351           7 :         if(ret)
     352           0 :             krb5_err(context, 1, ret, "krb5_cc_resolve");
     353             : 
     354           7 :         ret = krb5_cc_initialize(context, id, out->client);
     355           7 :         if(ret)
     356           0 :             krb5_err(context, 1, ret, "krb5_cc_initialize");
     357             : 
     358           7 :         ret = krb5_cc_store_cred(context, id, out);
     359           7 :         if(ret)
     360           0 :             krb5_err(context, 1, ret, "krb5_cc_store_cred");
     361           7 :         krb5_cc_close(context, id);
     362             :     }
     363             : 
     364          10 :     krb5_free_creds(context, out);
     365          10 :     krb5_free_principal(context, server);
     366          10 :     krb5_get_creds_opt_free(context, opt);
     367          10 :     krb5_cc_close (context, cache);
     368          10 :     krb5_free_context (context);
     369             : 
     370          10 :     return 0;
     371             : }

Generated by: LCOV version 1.14