smbldap: Fix typo in debug message.
[Samba.git] / source / libnet / libnet_keytab.c
blob02c2b6f76153d6fe7b2df2ecacc7c65c25c631e0
1 /*
2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
5 Copyright (C) Guenther Deschner 2008.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "libnet/libnet.h"
24 #ifdef HAVE_KRB5
26 /****************************************************************
27 ****************************************************************/
29 static int keytab_close(struct libnet_keytab_context *ctx)
31 if (!ctx) {
32 return 0;
35 if (ctx->keytab && ctx->context) {
36 krb5_kt_close(ctx->context, ctx->keytab);
39 if (ctx->context) {
40 krb5_free_context(ctx->context);
43 if (ctx->ads) {
44 ads_destroy(&ctx->ads);
47 TALLOC_FREE(ctx);
49 return 0;
52 /****************************************************************
53 ****************************************************************/
55 krb5_error_code libnet_keytab_init(TALLOC_CTX *mem_ctx,
56 const char *keytab_name,
57 struct libnet_keytab_context **ctx)
59 krb5_error_code ret = 0;
60 krb5_context context = NULL;
61 krb5_keytab keytab = NULL;
62 const char *keytab_string = NULL;
64 struct libnet_keytab_context *r;
66 r = TALLOC_ZERO_P(mem_ctx, struct libnet_keytab_context);
67 if (!r) {
68 return ENOMEM;
71 talloc_set_destructor(r, keytab_close);
73 initialize_krb5_error_table();
74 ret = krb5_init_context(&context);
75 if (ret) {
76 DEBUG(1,("keytab_init: could not krb5_init_context: %s\n",
77 error_message(ret)));
78 return ret;
81 ret = smb_krb5_open_keytab(context, keytab_name, true, &keytab);
82 if (ret) {
83 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
84 error_message(ret)));
85 krb5_free_context(context);
86 return ret;
89 ret = smb_krb5_keytab_name(mem_ctx, context, keytab, &keytab_string);
90 if (ret) {
91 krb5_kt_close(context, keytab);
92 krb5_free_context(context);
93 return ret;
96 r->context = context;
97 r->keytab = keytab;
98 r->keytab_name = keytab_string;
100 *ctx = r;
102 return 0;
105 /****************************************************************
106 ****************************************************************/
108 krb5_error_code libnet_keytab_add(struct libnet_keytab_context *ctx)
110 #if defined(ENCTYPE_ARCFOUR_HMAC)
111 krb5_error_code ret = 0;
112 krb5_enctype enctypes[2] = { ENCTYPE_ARCFOUR_HMAC, 0 };
113 int i;
115 for (i=0; i<ctx->count; i++) {
117 struct libnet_keytab_entry *entry = &ctx->entries[i];
118 krb5_data password;
120 password.data = (char *)entry->password.data;
121 password.length = entry->password.length;
123 ret = smb_krb5_kt_add_entry_ext(ctx->context,
124 ctx->keytab,
125 entry->kvno,
126 entry->principal,
127 enctypes,
128 password,
129 true,
130 true);
131 if (ret) {
132 DEBUG(1,("libnet_keytab_add: "
133 "Failed to add entry to keytab file\n"));
134 return ret;
138 return ret;
139 #else
140 return -1;
141 #endif /* defined(ENCTYPE_ARCFOUR_HMAC) */
144 #endif /* HAVE_KRB5 */