LCOV - code coverage report
Current view: top level - source4/kdc - ktutil.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 33 50 66.0 %
Date: 2021-09-23 10:06:22 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Minimal ktutil for selftest
       5             : 
       6             :    Copyright (C) Ralph Boehme <slow@samba.org> 2016
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "krb5_wrap/krb5_samba.h"
      24             : 
      25           0 : static void smb_krb5_err(TALLOC_CTX *mem_ctx,
      26             :                          krb5_context context,
      27             :                          int exit_code,
      28             :                          krb5_error_code code,
      29             :                          const char *msg)
      30             : {
      31           0 :         char *krb5_err_str = smb_get_krb5_error_message(context,
      32             :                                                         code,
      33             :                                                         mem_ctx);
      34           0 :         printf("%s: %s\n", msg, krb5_err_str ? krb5_err_str : "UNKOWN");
      35             : 
      36           0 :         talloc_free(mem_ctx);
      37           0 :         exit(exit_code);
      38             : }
      39             : 
      40           7 : int main (int argc, char **argv)
      41             : {
      42           7 :         TALLOC_CTX *mem_ctx = talloc_init("ktutil");
      43             :         krb5_context context;
      44             :         krb5_keytab keytab;
      45             :         krb5_kt_cursor cursor;
      46             :         krb5_keytab_entry entry;
      47             :         krb5_error_code ret;
      48           7 :         char *keytab_name = NULL;
      49             : 
      50           7 :         if (mem_ctx == NULL) {
      51           0 :                 printf("talloc_init() failed\n");
      52           0 :                 exit(1);
      53             :         }
      54             : 
      55           7 :         if (argc != 2) {
      56           0 :                 printf("Usage: %s KEYTAB\n", argv[0]);
      57           0 :                 exit(1);
      58             :         }
      59             : 
      60           7 :         keytab_name = argv[1];
      61             : 
      62           7 :         ret = smb_krb5_init_context_common(&context);
      63           7 :         if (ret) {
      64           0 :                 DBG_ERR("kerberos init context failed (%s)\n",
      65             :                         error_message(ret));
      66           0 :                 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_context");
      67             :         }
      68             : 
      69           7 :         ret = smb_krb5_kt_open_relative(context, keytab_name, false, &keytab);
      70           7 :         if (ret) {
      71           0 :                 smb_krb5_err(mem_ctx, context, 1, ret, "open keytab");
      72             :         }
      73             : 
      74           7 :         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
      75           7 :         if (ret) {
      76           0 :                 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_start_seq_get");
      77             :         }
      78             : 
      79         101 :         for (ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
      80             :              ret == 0;
      81          87 :              ret = krb5_kt_next_entry(context, keytab, &entry, &cursor))
      82             :         {
      83          87 :                 char *principal = NULL;
      84          87 :                 char *enctype_str = NULL;
      85          87 :                 krb5_enctype enctype = smb_krb5_kt_get_enctype_from_entry(&entry);
      86             : 
      87          87 :                 ret = smb_krb5_unparse_name(mem_ctx,
      88             :                                             context,
      89          87 :                                             entry.principal,
      90             :                                             &principal);
      91          87 :                 if (ret) {
      92           0 :                         smb_krb5_err(mem_ctx, context, 1, ret, "krb5_enctype_to_string");
      93             :                 }
      94             : 
      95          87 :                 ret = smb_krb5_enctype_to_string(context,
      96             :                                                  enctype,
      97             :                                                  &enctype_str);
      98          87 :                 if (ret) {
      99           0 :                         printf("%s (%d)\n", principal, (int)enctype);
     100             :                 } else {
     101          87 :                         printf("%s (%s)\n", principal, enctype_str);
     102             :                 }
     103             : 
     104          87 :                 TALLOC_FREE(principal);
     105          87 :                 SAFE_FREE(enctype_str);
     106          87 :                 smb_krb5_kt_free_entry(context, &entry);
     107             :         }
     108             : 
     109           7 :         ret = krb5_kt_end_seq_get(context, keytab, &cursor);
     110           7 :         if (ret) {
     111           0 :                 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_end_seq_get");
     112             :         }
     113             : 
     114           7 :         ret = krb5_kt_close(context, keytab);
     115           7 :         if (ret) {
     116           0 :                 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_close");
     117             :         }
     118             : 
     119           7 :         krb5_free_context(context);
     120           7 :         talloc_free(mem_ctx);
     121           7 :         return 0;
     122             : }

Generated by: LCOV version 1.13