selftest: Add the trusted domain realms to krb5.conf
[Samba.git] / source4 / kdc / ktutil.c
blob59aa1cf377fa8fcfdf03f3a11a585055d2b09c5c
1 /*
2 Unix SMB/CIFS implementation.
4 Minimal ktutil for selftest
6 Copyright (C) Ralph Boehme <slow@samba.org> 2016
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.
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.
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/>.
22 #include "includes.h"
23 #include "krb5_wrap/krb5_samba.h"
25 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)
31 char *krb5_err_str = smb_get_krb5_error_message(context,
32 code,
33 mem_ctx);
34 printf("%s: %s\n", msg, krb5_err_str ? krb5_err_str : "UNKOWN");
36 talloc_free(mem_ctx);
37 exit(exit_code);
40 int main (int argc, char **argv)
42 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 char *keytab_name = NULL;
50 if (mem_ctx == NULL) {
51 printf("talloc_init() failed\n");
52 exit(1);
55 if (argc != 2) {
56 printf("Usage: %s KEYTAB\n", argv[0]);
57 exit(1);
60 keytab_name = argv[1];
62 ret = smb_krb5_init_context_common(&context);
63 if (ret) {
64 DBG_ERR("kerberos init context failed (%s)\n",
65 error_message(ret));
66 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_context");
69 ret = smb_krb5_kt_open_relative(context, keytab_name, false, &keytab);
70 if (ret) {
71 smb_krb5_err(mem_ctx, context, 1, ret, "open keytab");
74 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
75 if (ret) {
76 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_start_seq_get");
79 for (ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
80 ret == 0;
81 ret = krb5_kt_next_entry(context, keytab, &entry, &cursor))
83 char *principal = NULL;
84 char *enctype_str = NULL;
85 krb5_enctype enctype = smb_krb5_kt_get_enctype_from_entry(&entry);
87 ret = smb_krb5_unparse_name(mem_ctx,
88 context,
89 entry.principal,
90 &principal);
91 if (ret) {
92 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_enctype_to_string");
95 ret = smb_krb5_enctype_to_string(context,
96 enctype,
97 &enctype_str);
98 if (ret) {
99 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_enctype_to_string");
102 printf("%s (%s)\n", principal, enctype_str);
104 TALLOC_FREE(principal);
105 SAFE_FREE(enctype_str);
106 smb_krb5_kt_free_entry(context, &entry);
109 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
110 if (ret) {
111 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_end_seq_get");
114 ret = krb5_kt_close(context, keytab);
115 if (ret) {
116 smb_krb5_err(mem_ctx, context, 1, ret, "krb5_kt_close");
119 krb5_free_context(context);
120 talloc_free(mem_ctx);
121 return 0;