2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
5 Copyright (C) Guenther Deschner 2008.
6 Copyright (C) Michael Adam 2008
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/>.
23 #include "libnet/libnet.h"
27 /****************************************************************
28 ****************************************************************/
30 static int keytab_close(struct libnet_keytab_context
*ctx
)
36 if (ctx
->keytab
&& ctx
->context
) {
37 krb5_kt_close(ctx
->context
, ctx
->keytab
);
41 krb5_free_context(ctx
->context
);
45 ads_destroy(&ctx
->ads
);
53 /****************************************************************
54 ****************************************************************/
56 krb5_error_code
libnet_keytab_init(TALLOC_CTX
*mem_ctx
,
57 const char *keytab_name
,
58 struct libnet_keytab_context
**ctx
)
60 krb5_error_code ret
= 0;
61 krb5_context context
= NULL
;
62 krb5_keytab keytab
= NULL
;
63 const char *keytab_string
= NULL
;
65 struct libnet_keytab_context
*r
;
67 r
= TALLOC_ZERO_P(mem_ctx
, struct libnet_keytab_context
);
72 talloc_set_destructor(r
, keytab_close
);
74 initialize_krb5_error_table();
75 ret
= krb5_init_context(&context
);
77 DEBUG(1,("keytab_init: could not krb5_init_context: %s\n",
82 ret
= smb_krb5_open_keytab(context
, keytab_name
, true, &keytab
);
84 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
86 krb5_free_context(context
);
90 ret
= smb_krb5_keytab_name(mem_ctx
, context
, keytab
, &keytab_string
);
92 krb5_kt_close(context
, keytab
);
93 krb5_free_context(context
);
99 r
->keytab_name
= keytab_string
;
100 r
->clean_old_entries
= false;
107 /****************************************************************
108 ****************************************************************/
111 * Remove all entries that have the given principal, kvno and enctype.
113 static krb5_error_code
libnet_keytab_remove_entries(krb5_context context
,
115 const char *principal
,
117 const krb5_enctype enctype
,
121 krb5_kt_cursor cursor
;
122 krb5_keytab_entry kt_entry
;
124 ZERO_STRUCT(kt_entry
);
127 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
132 while (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
) == 0)
135 char *princ_s
= NULL
;
137 if (kt_entry
.vno
!= kvno
&& !ignore_kvno
) {
141 keyp
= KRB5_KT_KEY(&kt_entry
);
143 if (KRB5_KEY_TYPE(keyp
) != enctype
) {
147 ret
= smb_krb5_unparse_name(context
, kt_entry
.principal
,
150 DEBUG(5, ("smb_krb5_unparse_name failed (%s)\n",
151 error_message(ret
)));
155 if (strcmp(principal
, princ_s
) != 0) {
159 /* match found - remove */
161 DEBUG(10, ("found entry for principal %s, kvno %d, "
162 "enctype %d - trying to remove it\n",
163 princ_s
, kt_entry
.vno
, KRB5_KEY_TYPE(keyp
)));
165 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
168 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
169 error_message(ret
)));
173 ret
= krb5_kt_remove_entry(context
, keytab
,
176 DEBUG(5, ("krb5_kt_remove_entry failed (%s)\n",
177 error_message(ret
)));
180 DEBUG(10, ("removed entry for principal %s, kvno %d, "
181 "enctype %d\n", princ_s
, kt_entry
.vno
,
182 KRB5_KEY_TYPE(keyp
)));
184 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
186 DEBUG(5, ("krb5_kt_start_seq_get failed (%s)\n",
187 error_message(ret
)));
192 smb_krb5_kt_free_entry(context
, &kt_entry
);
196 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
198 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
199 error_message(ret
)));
205 static krb5_error_code
libnet_keytab_add_entry(krb5_context context
,
209 krb5_enctype enctype
,
213 krb5_keytab_entry kt_entry
;
216 /* remove duplicates first ... */
217 ret
= libnet_keytab_remove_entries(context
, keytab
, princ_s
, kvno
,
220 DEBUG(1, ("libnet_keytab_remove_entries failed: %s\n",
221 error_message(ret
)));
224 ZERO_STRUCT(kt_entry
);
228 ret
= smb_krb5_parse_name(context
, princ_s
, &kt_entry
.principal
);
230 DEBUG(1, ("smb_krb5_parse_name(%s) failed (%s)\n",
231 princ_s
, error_message(ret
)));
235 keyp
= KRB5_KT_KEY(&kt_entry
);
237 if (create_kerberos_key_from_string(context
, kt_entry
.principal
,
238 &password
, keyp
, enctype
, true))
240 ret
= KRB5KRB_ERR_GENERIC
;
244 ret
= krb5_kt_add_entry(context
, keytab
, &kt_entry
);
246 DEBUG(1, ("adding entry to keytab failed (%s)\n",
247 error_message(ret
)));
251 krb5_free_keyblock_contents(context
, keyp
);
252 krb5_free_principal(context
, kt_entry
.principal
);
253 ZERO_STRUCT(kt_entry
);
254 smb_krb5_kt_free_entry(context
, &kt_entry
);
259 krb5_error_code
libnet_keytab_add(struct libnet_keytab_context
*ctx
)
261 krb5_error_code ret
= 0;
265 if (ctx
->clean_old_entries
) {
266 DEBUG(0, ("cleaning old entries...\n"));
267 for (i
=0; i
< ctx
->count
; i
++) {
268 struct libnet_keytab_entry
*entry
= &ctx
->entries
[i
];
270 ret
= libnet_keytab_remove_entries(ctx
->context
,
277 DEBUG(1,("libnet_keytab_add: Failed to remove "
278 "old entries for %s (enctype %u): %s\n",
279 entry
->principal
, entry
->enctype
,
280 error_message(ret
)));
286 for (i
=0; i
<ctx
->count
; i
++) {
288 struct libnet_keytab_entry
*entry
= &ctx
->entries
[i
];
291 ZERO_STRUCT(password
);
292 password
.data
= (char *)entry
->password
.data
;
293 password
.length
= entry
->password
.length
;
295 ret
= libnet_keytab_add_entry(ctx
->context
,
302 DEBUG(1,("libnet_keytab_add: "
303 "Failed to add entry to keytab file\n"));
311 struct libnet_keytab_entry
*libnet_keytab_search(struct libnet_keytab_context
*ctx
,
312 const char *principal
,
314 const krb5_enctype enctype
,
317 krb5_error_code ret
= 0;
318 krb5_kt_cursor cursor
;
319 krb5_keytab_entry kt_entry
;
320 struct libnet_keytab_entry
*entry
= NULL
;
322 ZERO_STRUCT(kt_entry
);
325 ret
= krb5_kt_start_seq_get(ctx
->context
, ctx
->keytab
, &cursor
);
327 DEBUG(10, ("krb5_kt_start_seq_get failed: %s\n",
328 error_message(ret
)));
332 while (krb5_kt_next_entry(ctx
->context
, ctx
->keytab
, &kt_entry
, &cursor
) == 0)
335 char *princ_s
= NULL
;
337 if (kt_entry
.vno
!= kvno
) {
341 keyp
= KRB5_KT_KEY(&kt_entry
);
343 if (KRB5_KEY_TYPE(keyp
) != 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
, KRB5_KEY_DATA(keyp
),
376 KRB5_KEY_LENGTH(keyp
));
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
);
405 * Helper function to add data to the list
406 * of keytab entries. It builds the prefix from the input.
408 NTSTATUS
libnet_keytab_add_to_keytab_entries(TALLOC_CTX
*mem_ctx
,
409 struct libnet_keytab_context
*ctx
,
413 const krb5_enctype enctype
,
416 struct libnet_keytab_entry entry
;
419 entry
.name
= talloc_strdup(mem_ctx
, name
);
420 entry
.principal
= talloc_asprintf(mem_ctx
, "%s%s%s@%s",
421 prefix
? prefix
: "",
423 name
, ctx
->dns_domain_name
);
424 entry
.enctype
= enctype
;
425 entry
.password
= blob
;
426 NT_STATUS_HAVE_NO_MEMORY(entry
.name
);
427 NT_STATUS_HAVE_NO_MEMORY(entry
.principal
);
428 NT_STATUS_HAVE_NO_MEMORY(entry
.password
.data
);
430 ADD_TO_ARRAY(mem_ctx
, struct libnet_keytab_entry
, entry
,
431 &ctx
->entries
, &ctx
->count
);
432 NT_STATUS_HAVE_NO_MEMORY(ctx
->entries
);
437 #endif /* HAVE_KRB5 */