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 : "UNKNOWN");
35 :
36 0 : talloc_free(mem_ctx);
37 0 : exit(exit_code);
38 : }
39 :
40 14 : int main (int argc, char **argv)
41 : {
42 14 : 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 14 : char *keytab_name = NULL;
49 :
50 14 : if (mem_ctx == NULL) {
51 0 : printf("talloc_init() failed\n");
52 0 : exit(1);
53 : }
54 :
55 14 : if (argc != 2) {
56 0 : printf("Usage: %s KEYTAB\n", argv[0]);
57 0 : exit(1);
58 : }
59 :
60 14 : keytab_name = argv[1];
61 :
62 14 : ret = smb_krb5_init_context_common(&context);
63 14 : 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 14 : ret = smb_krb5_kt_open_relative(context, keytab_name, false, &keytab);
70 14 : if (ret) {
71 0 : smb_krb5_err(mem_ctx, context, 1, ret, "open keytab");
72 : }
73 :
74 14 : ret = krb5_kt_start_seq_get(context, keytab, &cursor);
75 14 : if (ret) {
76 0 : smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_start_seq_get");
77 : }
78 :
79 14 : for (ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
80 184 : ret == 0;
81 170 : ret = krb5_kt_next_entry(context, keytab, &entry, &cursor))
82 : {
83 170 : char *principal = NULL;
84 170 : char *enctype_str = NULL;
85 170 : krb5_enctype enctype = smb_krb5_kt_get_enctype_from_entry(&entry);
86 :
87 170 : ret = smb_krb5_unparse_name(mem_ctx,
88 : context,
89 170 : entry.principal,
90 : &principal);
91 170 : if (ret) {
92 0 : smb_krb5_err(mem_ctx, context, 1, ret, "krb5_enctype_to_string");
93 : }
94 :
95 170 : ret = smb_krb5_enctype_to_string(context,
96 : enctype,
97 : &enctype_str);
98 170 : if (ret) {
99 0 : printf("%s (%d)\n", principal, (int)enctype);
100 : } else {
101 170 : printf("%s (%s)\n", principal, enctype_str);
102 : }
103 :
104 170 : TALLOC_FREE(principal);
105 170 : SAFE_FREE(enctype_str);
106 170 : smb_krb5_kt_free_entry(context, &entry);
107 : }
108 :
109 14 : ret = krb5_kt_end_seq_get(context, keytab, &cursor);
110 14 : if (ret) {
111 0 : smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_end_seq_get");
112 : }
113 :
114 14 : ret = krb5_kt_close(context, keytab);
115 14 : if (ret) {
116 0 : smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_close");
117 : }
118 :
119 14 : krb5_free_context(context);
120 14 : talloc_free(mem_ctx);
121 14 : return 0;
122 : }
|