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;
105 /****************************************************************
106 ****************************************************************/
109 * Remove all entries that have the given principal, kvno and enctype.
111 static krb5_error_code libnet_keytab_remove_entries(krb5_context context,
113 const char *principal,
115 const krb5_enctype enctype)
118 krb5_kt_cursor cursor;
119 krb5_keytab_entry kt_entry;
121 ZERO_STRUCT(kt_entry);
124 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
129 while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0)
131 char *princ_s = NULL;
133 if (kt_entry.vno != kvno) {
137 if (kt_entry.key.enctype != enctype) {
141 ret = smb_krb5_unparse_name(context, kt_entry.principal,
144 DEBUG(5, ("smb_krb5_unparse_name failed (%s)\n",
145 error_message(ret)));
149 if (strcmp(principal, princ_s) != 0) {
153 /* match found - remove */
155 DEBUG(10, ("found entry for principal %s, kvno %d, "
156 "enctype %d - trying to remove it\n",
157 princ_s, kt_entry.vno, kt_entry.key.enctype));
159 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
162 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
163 error_message(ret)));
167 ret = krb5_kt_remove_entry(context, keytab,
170 DEBUG(5, ("krb5_kt_remove_entry failed (%s)\n",
171 error_message(ret)));
174 DEBUG(10, ("removed entry for principal %s, kvno %d, "
175 "enctype %d\n", princ_s, kt_entry.vno,
176 kt_entry.key.enctype));
178 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
180 DEBUG(5, ("krb5_kt_start_seq_get failed (%s)\n",
181 error_message(ret)));
186 smb_krb5_kt_free_entry(context, &kt_entry);
190 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
192 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
193 error_message(ret)));
199 static krb5_error_code libnet_keytab_add_entry(krb5_context context,
203 krb5_enctype enctype,
207 krb5_keytab_entry kt_entry;
210 /* remove duplicates first ... */
211 ret = libnet_keytab_remove_entries(context, keytab, princ_s, kvno,
214 DEBUG(1, ("libnet_keytab_remove_entries failed: %s\n",
215 error_message(ret)));
218 ZERO_STRUCT(kt_entry);
222 ret = smb_krb5_parse_name(context, princ_s, &kt_entry.principal);
224 DEBUG(1, ("smb_krb5_parse_name(%s) failed (%s)\n",
225 princ_s, error_message(ret)));
229 #if !defined(HAVE_KRB5_KEYTAB_ENTRY_KEY) && !defined(HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK)
230 #error krb5_keytab_entry has no key or keyblock member
232 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY /* MIT */
233 keyp = &kt_entry.key;
235 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK /* Heimdal */
236 keyp = &kt_entry.keyblock;
239 if (create_kerberos_key_from_string(context, kt_entry.principal,
240 &password, keyp, enctype, true))
242 ret = KRB5KRB_ERR_GENERIC;
246 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
248 DEBUG(1, ("adding entry to keytab failed (%s)\n",
249 error_message(ret)));
253 krb5_free_keyblock_contents(context, keyp);
254 krb5_free_principal(context, kt_entry.principal);
255 ZERO_STRUCT(kt_entry);
256 smb_krb5_kt_free_entry(context, &kt_entry);
261 krb5_error_code libnet_keytab_add(struct libnet_keytab_context *ctx)
263 krb5_error_code ret = 0;
266 for (i=0; i<ctx->count; i++) {
268 struct libnet_keytab_entry *entry = &ctx->entries[i];
271 password.data = (char *)entry->password.data;
272 password.length = entry->password.length;
274 ret = libnet_keytab_add_entry(ctx->context,
281 DEBUG(1,("libnet_keytab_add: "
282 "Failed to add entry to keytab file\n"));
290 struct libnet_keytab_entry *libnet_keytab_search(struct libnet_keytab_context *ctx,
291 const char *principal,
293 const krb5_enctype enctype,
296 krb5_error_code ret = 0;
297 krb5_kt_cursor cursor;
298 krb5_keytab_entry kt_entry;
299 struct libnet_keytab_entry *entry = NULL;
301 ZERO_STRUCT(kt_entry);
304 ret = krb5_kt_start_seq_get(ctx->context, ctx->keytab, &cursor);
306 DEBUG(10, ("krb5_kt_start_seq_get failed: %s",
307 error_message(ret)));
311 while (krb5_kt_next_entry(ctx->context, ctx->keytab, &kt_entry, &cursor) == 0)
313 char *princ_s = NULL;
315 if (kt_entry.vno != kvno) {
319 if (kt_entry.key.enctype != enctype) {
323 ret = smb_krb5_unparse_name(ctx->context, kt_entry.principal,
329 if (strcmp(principal, princ_s) != 0) {
333 entry = talloc_zero(mem_ctx, struct libnet_keytab_entry);
335 DEBUG(3, ("talloc failed\n"));
339 entry->name = talloc_strdup(entry, princ_s);
341 DEBUG(3, ("talloc_strdup_failed\n"));
345 entry->principal = talloc_strdup(entry, princ_s);
346 if (!entry->principal) {
347 DEBUG(3, ("talloc_strdup_failed\n"));
351 entry->password = data_blob_talloc(entry, kt_entry.key.contents,
352 kt_entry.key.length);
353 if (!entry->password.data) {
354 DEBUG(3, ("data_blob_talloc failed\n"));
358 DEBUG(10, ("found entry\n"));
360 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
365 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
371 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
376 krb5_kt_end_seq_get(ctx->context, ctx->keytab, &cursor);
380 #endif /* HAVE_KRB5 */