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/>.
22 #include "libnet/libnet.h"
26 /****************************************************************
27 ****************************************************************/
29 static int keytab_close(struct libnet_keytab_context *ctx)
35 if (ctx->keytab && ctx->context) {
36 krb5_kt_close(ctx->context, ctx->keytab);
40 krb5_free_context(ctx->context);
44 ads_destroy(&ctx->ads);
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);
71 talloc_set_destructor(r, keytab_close);
73 initialize_krb5_error_table();
74 ret = krb5_init_context(&context);
76 DEBUG(1,("keytab_init: could not krb5_init_context: %s\n",
81 ret = smb_krb5_open_keytab(context, keytab_name, true, &keytab);
83 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
85 krb5_free_context(context);
89 ret = smb_krb5_keytab_name(mem_ctx, context, keytab, &keytab_string);
91 krb5_kt_close(context, keytab);
92 krb5_free_context(context);
98 r->keytab_name = keytab_string;
99 r->clean_old_entries = false;
106 /****************************************************************
107 ****************************************************************/
110 * Remove all entries that have the given principal, kvno and enctype.
112 static krb5_error_code libnet_keytab_remove_entries(krb5_context context,
114 const char *principal,
116 const krb5_enctype enctype,
120 krb5_kt_cursor cursor;
121 krb5_keytab_entry kt_entry;
123 ZERO_STRUCT(kt_entry);
126 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
131 while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0)
133 char *princ_s = NULL;
135 if (kt_entry.vno != kvno && !ignore_kvno) {
139 if (kt_entry.key.enctype != enctype) {
143 ret = smb_krb5_unparse_name(context, kt_entry.principal,
146 DEBUG(5, ("smb_krb5_unparse_name failed (%s)\n",
147 error_message(ret)));
151 if (strcmp(principal, princ_s) != 0) {
155 /* match found - remove */
157 DEBUG(10, ("found entry for principal %s, kvno %d, "
158 "enctype %d - trying to remove it\n",
159 princ_s, kt_entry.vno, kt_entry.key.enctype));
161 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
164 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
165 error_message(ret)));
169 ret = krb5_kt_remove_entry(context, keytab,
172 DEBUG(5, ("krb5_kt_remove_entry failed (%s)\n",
173 error_message(ret)));
176 DEBUG(10, ("removed entry for principal %s, kvno %d, "
177 "enctype %d\n", princ_s, kt_entry.vno,
178 kt_entry.key.enctype));
180 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
182 DEBUG(5, ("krb5_kt_start_seq_get failed (%s)\n",
183 error_message(ret)));
188 smb_krb5_kt_free_entry(context, &kt_entry);
192 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
194 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
195 error_message(ret)));
201 static krb5_error_code libnet_keytab_add_entry(krb5_context context,
205 krb5_enctype enctype,
209 krb5_keytab_entry kt_entry;
212 /* remove duplicates first ... */
213 ret = libnet_keytab_remove_entries(context, keytab, princ_s, kvno,
216 DEBUG(1, ("libnet_keytab_remove_entries failed: %s\n",
217 error_message(ret)));
220 ZERO_STRUCT(kt_entry);
224 ret = smb_krb5_parse_name(context, princ_s, &kt_entry.principal);
226 DEBUG(1, ("smb_krb5_parse_name(%s) failed (%s)\n",
227 princ_s, error_message(ret)));
231 #if !defined(HAVE_KRB5_KEYTAB_ENTRY_KEY) && !defined(HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK)
232 #error krb5_keytab_entry has no key or keyblock member
234 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY /* MIT */
235 keyp = &kt_entry.key;
237 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK /* Heimdal */
238 keyp = &kt_entry.keyblock;
241 if (create_kerberos_key_from_string(context, kt_entry.principal,
242 &password, keyp, enctype, true))
244 ret = KRB5KRB_ERR_GENERIC;
248 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
250 DEBUG(1, ("adding entry to keytab failed (%s)\n",
251 error_message(ret)));
255 krb5_free_keyblock_contents(context, keyp);
256 krb5_free_principal(context, kt_entry.principal);
257 ZERO_STRUCT(kt_entry);
258 smb_krb5_kt_free_entry(context, &kt_entry);
263 krb5_error_code libnet_keytab_add(struct libnet_keytab_context *ctx)
265 krb5_error_code ret = 0;
269 if (ctx->clean_old_entries) {
270 DEBUG(0, ("cleaning old entries...\n"));
271 for (i=0; i < ctx->count; i++) {
272 struct libnet_keytab_entry *entry = &ctx->entries[i];
274 ret = libnet_keytab_remove_entries(ctx->context,
281 DEBUG(1,("libnet_keytab_add: Failed to remove "
282 "old entries for %s (enctype %u): %s\n",
283 entry->principal, entry->enctype,
284 error_message(ret)));
290 for (i=0; i<ctx->count; i++) {
292 struct libnet_keytab_entry *entry = &ctx->entries[i];
295 password.data = (char *)entry->password.data;
296 password.length = entry->password.length;
298 ret = libnet_keytab_add_entry(ctx->context,
305 DEBUG(1,("libnet_keytab_add: "
306 "Failed to add entry to keytab file\n"));
314 struct libnet_keytab_entry *libnet_keytab_search(struct libnet_keytab_context *ctx,
315 const char *principal,
317 const krb5_enctype enctype,
320 krb5_error_code ret = 0;
321 krb5_kt_cursor cursor;
322 krb5_keytab_entry kt_entry;
323 struct libnet_keytab_entry *entry = NULL;
325 ZERO_STRUCT(kt_entry);
328 ret = krb5_kt_start_seq_get(ctx->context, ctx->keytab, &cursor);
330 DEBUG(10, ("krb5_kt_start_seq_get failed: %s",
331 error_message(ret)));
335 while (krb5_kt_next_entry(ctx->context, ctx->keytab, &kt_entry, &cursor) == 0)
337 char *princ_s = NULL;
339 if (kt_entry.vno != kvno) {
343 if (kt_entry.key.enctype != enctype) {
347 ret = smb_krb5_unparse_name(ctx->context, kt_entry.principal,
353 if (strcmp(principal, princ_s) != 0) {
357 entry = talloc_zero(mem_ctx, struct libnet_keytab_entry);
359 DEBUG(3, ("talloc failed\n"));
363 entry->name = talloc_strdup(entry, princ_s);
365 DEBUG(3, ("talloc_strdup_failed\n"));
369 entry->principal = talloc_strdup(entry, princ_s);
370 if (!entry->principal) {
371 DEBUG(3, ("talloc_strdup_failed\n"));
375 entry->password = data_blob_talloc(entry, kt_entry.key.contents,
376 kt_entry.key.length);
377 if (!entry->password.data) {
378 DEBUG(3, ("data_blob_talloc failed\n"));
382 DEBUG(10, ("found entry\n"));
384 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
389 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
395 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
400 krb5_kt_end_seq_get(ctx->context, ctx->keytab, &cursor);
404 #endif /* HAVE_KRB5 */