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/>.
26 #include "libnet/libnet_keytab.h"
30 /****************************************************************
31 ****************************************************************/
33 static int keytab_close(struct libnet_keytab_context
*ctx
)
39 if (ctx
->keytab
&& ctx
->context
) {
40 krb5_kt_close(ctx
->context
, ctx
->keytab
);
44 krb5_free_context(ctx
->context
);
47 TALLOC_FREE(ctx
->ads
);
54 /****************************************************************
55 ****************************************************************/
57 krb5_error_code
libnet_keytab_init(TALLOC_CTX
*mem_ctx
,
58 const char *keytab_name
,
59 struct libnet_keytab_context
**ctx
)
61 krb5_error_code ret
= 0;
62 krb5_context context
= NULL
;
63 krb5_keytab keytab
= NULL
;
64 const char *keytab_string
= NULL
;
66 struct libnet_keytab_context
*r
;
68 r
= talloc_zero(mem_ctx
, struct libnet_keytab_context
);
73 talloc_set_destructor(r
, keytab_close
);
75 ret
= smb_krb5_init_context_common(&context
);
77 DBG_ERR("kerberos init context failed (%s)\n",
82 ret
= smb_krb5_kt_open_relative(context
,
84 true, /* write_access */
87 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
89 krb5_free_context(context
);
93 ret
= smb_krb5_kt_get_name(mem_ctx
, context
, keytab
, &keytab_string
);
95 krb5_kt_close(context
, keytab
);
96 krb5_free_context(context
);
100 r
->context
= context
;
102 r
->keytab_name
= keytab_string
;
103 r
->clean_old_entries
= false;
110 /****************************************************************
111 ****************************************************************/
114 * Remove all entries that have the given principal, kvno and enctype.
116 static krb5_error_code
libnet_keytab_remove_entries(krb5_context context
,
118 const char *principal
,
120 const krb5_enctype enctype
,
124 krb5_kt_cursor cursor
;
125 krb5_keytab_entry kt_entry
;
127 ZERO_STRUCT(kt_entry
);
130 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
135 while (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
) == 0)
138 char *princ_s
= NULL
;
140 if (kt_entry
.vno
!= kvno
&& !ignore_kvno
) {
144 keyp
= KRB5_KT_KEY(&kt_entry
);
146 if (KRB5_KEY_TYPE(keyp
) != enctype
) {
150 ret
= smb_krb5_unparse_name(talloc_tos(), context
, kt_entry
.principal
,
153 DEBUG(5, ("smb_krb5_unparse_name failed (%s)\n",
154 error_message(ret
)));
158 if (strcmp(principal
, princ_s
) != 0) {
162 /* match found - remove */
164 DEBUG(10, ("found entry for principal %s, kvno %d, "
165 "enctype %d - trying to remove it\n",
166 princ_s
, kt_entry
.vno
, KRB5_KEY_TYPE(keyp
)));
168 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
171 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
172 error_message(ret
)));
176 ret
= krb5_kt_remove_entry(context
, keytab
,
179 DEBUG(5, ("krb5_kt_remove_entry failed (%s)\n",
180 error_message(ret
)));
183 DEBUG(10, ("removed entry for principal %s, kvno %d, "
184 "enctype %d\n", princ_s
, kt_entry
.vno
,
185 KRB5_KEY_TYPE(keyp
)));
187 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
189 DEBUG(5, ("krb5_kt_start_seq_get failed (%s)\n",
190 error_message(ret
)));
195 smb_krb5_kt_free_entry(context
, &kt_entry
);
196 TALLOC_FREE(princ_s
);
199 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
201 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
202 error_message(ret
)));
208 static krb5_error_code
libnet_keytab_add_entry(krb5_context context
,
212 krb5_enctype enctype
,
216 krb5_keytab_entry kt_entry
;
218 krb5_principal salt_princ
= NULL
;
221 /* remove duplicates first ... */
222 ret
= libnet_keytab_remove_entries(context
, keytab
, princ_s
, kvno
,
225 DEBUG(1, ("libnet_keytab_remove_entries failed: %s\n",
226 error_message(ret
)));
229 ZERO_STRUCT(kt_entry
);
233 ret
= smb_krb5_parse_name(context
, princ_s
, &kt_entry
.principal
);
235 DEBUG(1, ("smb_krb5_parse_name(%s) failed (%s)\n",
236 princ_s
, error_message(ret
)));
240 keyp
= KRB5_KT_KEY(&kt_entry
);
242 salt_princ_s
= kerberos_secrets_fetch_salt_princ();
243 if (salt_princ_s
== NULL
) {
244 ret
= KRB5KRB_ERR_GENERIC
;
248 ret
= krb5_parse_name(context
, salt_princ_s
, &salt_princ
);
249 SAFE_FREE(salt_princ_s
);
251 ret
= KRB5KRB_ERR_GENERIC
;
255 ret
= create_kerberos_key_from_string(context
,
262 krb5_free_principal(context
, salt_princ
);
264 ret
= KRB5KRB_ERR_GENERIC
;
268 ret
= krb5_kt_add_entry(context
, keytab
, &kt_entry
);
270 DEBUG(1, ("adding entry to keytab failed (%s)\n",
271 error_message(ret
)));
275 krb5_free_keyblock_contents(context
, keyp
);
276 krb5_free_principal(context
, kt_entry
.principal
);
277 ZERO_STRUCT(kt_entry
);
278 smb_krb5_kt_free_entry(context
, &kt_entry
);
283 krb5_error_code
libnet_keytab_add(struct libnet_keytab_context
*ctx
)
285 krb5_error_code ret
= 0;
289 if (ctx
->clean_old_entries
) {
290 DEBUG(0, ("cleaning old entries...\n"));
291 for (i
=0; i
< ctx
->count
; i
++) {
292 struct libnet_keytab_entry
*entry
= &ctx
->entries
[i
];
294 ret
= libnet_keytab_remove_entries(ctx
->context
,
301 DEBUG(1,("libnet_keytab_add: Failed to remove "
302 "old entries for %s (enctype %u): %s\n",
303 entry
->principal
, entry
->enctype
,
304 error_message(ret
)));
310 for (i
=0; i
<ctx
->count
; i
++) {
312 struct libnet_keytab_entry
*entry
= &ctx
->entries
[i
];
315 ZERO_STRUCT(password
);
316 password
.data
= (char *)entry
->password
.data
;
317 password
.length
= entry
->password
.length
;
319 ret
= libnet_keytab_add_entry(ctx
->context
,
326 DEBUG(1,("libnet_keytab_add: "
327 "Failed to add entry to keytab file\n"));
335 struct libnet_keytab_entry
*libnet_keytab_search(struct libnet_keytab_context
*ctx
,
336 const char *principal
,
338 const krb5_enctype enctype
,
341 krb5_error_code ret
= 0;
342 krb5_kt_cursor cursor
;
343 krb5_keytab_entry kt_entry
;
344 struct libnet_keytab_entry
*entry
= NULL
;
346 ZERO_STRUCT(kt_entry
);
349 ret
= krb5_kt_start_seq_get(ctx
->context
, ctx
->keytab
, &cursor
);
351 DEBUG(10, ("krb5_kt_start_seq_get failed: %s\n",
352 error_message(ret
)));
356 while (krb5_kt_next_entry(ctx
->context
, ctx
->keytab
, &kt_entry
, &cursor
) == 0)
359 char *princ_s
= NULL
;
363 if (kt_entry
.vno
!= kvno
) {
367 keyp
= KRB5_KT_KEY(&kt_entry
);
369 if (KRB5_KEY_TYPE(keyp
) != enctype
) {
373 entry
= talloc_zero(mem_ctx
, struct libnet_keytab_entry
);
375 DEBUG(3, ("talloc failed\n"));
379 ret
= smb_krb5_unparse_name(entry
, ctx
->context
, kt_entry
.principal
,
385 if (strcmp(principal
, princ_s
) != 0) {
389 entry
->principal
= talloc_strdup(entry
, princ_s
);
390 if (!entry
->principal
) {
391 DEBUG(3, ("talloc_strdup_failed\n"));
395 entry
->name
= talloc_move(entry
, &princ_s
);
397 entry
->password
= data_blob_talloc(entry
, KRB5_KEY_DATA(keyp
),
398 KRB5_KEY_LENGTH(keyp
));
399 if (!entry
->password
.data
) {
400 DEBUG(3, ("data_blob_talloc failed\n"));
404 DEBUG(10, ("found entry\n"));
406 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
410 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
415 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
420 krb5_kt_end_seq_get(ctx
->context
, ctx
->keytab
, &cursor
);
425 * Helper function to add data to the list
426 * of keytab entries. It builds the prefix from the input.
428 NTSTATUS
libnet_keytab_add_to_keytab_entries(TALLOC_CTX
*mem_ctx
,
429 struct libnet_keytab_context
*ctx
,
433 const krb5_enctype enctype
,
436 struct libnet_keytab_entry entry
;
439 entry
.name
= talloc_strdup(mem_ctx
, name
);
440 entry
.principal
= talloc_asprintf(mem_ctx
, "%s%s%s@%s",
441 prefix
? prefix
: "",
443 name
, ctx
->dns_domain_name
);
444 entry
.enctype
= enctype
;
445 entry
.password
= blob
;
446 NT_STATUS_HAVE_NO_MEMORY(entry
.name
);
447 NT_STATUS_HAVE_NO_MEMORY(entry
.principal
);
448 NT_STATUS_HAVE_NO_MEMORY(entry
.password
.data
);
450 ADD_TO_ARRAY(mem_ctx
, struct libnet_keytab_entry
, entry
,
451 &ctx
->entries
, &ctx
->count
);
452 NT_STATUS_HAVE_NO_MEMORY(ctx
->entries
);
457 #endif /* HAVE_KRB5 */