2 Unix SMB/CIFS implementation.
4 Copyright (C) Guenther Deschner <gd@samba.org> 2008
5 Copyright (C) Michael Adam 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/>.
24 #include "libnet/libnet_dssync.h"
25 #include "libnet/libnet_keytab.h"
26 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
31 struct replUpToDateVectorBlob **pold_utdv)
33 krb5_error_code ret = 0;
34 struct libnet_keytab_context *keytab_ctx;
35 struct libnet_keytab_entry *entry;
36 struct replUpToDateVectorBlob *old_utdv = NULL;
39 ret = libnet_keytab_init(mem_ctx, ctx->output_filename, &keytab_ctx);
41 return krb5_to_nt_status(ret);
44 keytab_ctx->dns_domain_name = ctx->dns_domain_name;
45 keytab_ctx->clean_old_entries = ctx->clean_old_entries;
46 ctx->private_data = keytab_ctx;
48 principal = talloc_asprintf(mem_ctx, "UTDV/%s@%s",
49 ctx->nc_dn, ctx->dns_domain_name);
50 NT_STATUS_HAVE_NO_MEMORY(principal);
52 entry = libnet_keytab_search(keytab_ctx, principal, 0, ENCTYPE_NULL,
55 enum ndr_err_code ndr_err;
56 old_utdv = talloc(mem_ctx, struct replUpToDateVectorBlob);
58 ndr_err = ndr_pull_struct_blob(&entry->password, old_utdv, old_utdv,
59 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
60 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
61 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
62 ctx->error_message = talloc_asprintf(ctx,
63 "Failed to pull UpToDateVector: %s",
68 if (DEBUGLEVEL >= 10) {
69 NDR_PRINT_DEBUG(replUpToDateVectorBlob, old_utdv);
74 *pold_utdv = old_utdv;
80 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
81 struct replUpToDateVectorBlob *new_utdv)
83 NTSTATUS status = NT_STATUS_OK;
84 krb5_error_code ret = 0;
85 struct libnet_keytab_context *keytab_ctx =
86 (struct libnet_keytab_context *)ctx->private_data;
89 enum ndr_err_code ndr_err;
92 if (DEBUGLEVEL >= 10) {
93 NDR_PRINT_DEBUG(replUpToDateVectorBlob, new_utdv);
96 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, new_utdv,
97 (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
98 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
99 status = ndr_map_error2ntstatus(ndr_err);
100 ctx->error_message = talloc_asprintf(ctx,
101 "Failed to push UpToDateVector: %s",
106 status = libnet_keytab_add_to_keytab_entries(mem_ctx, keytab_ctx, 0,
110 if (!NT_STATUS_IS_OK(status)) {
115 ret = libnet_keytab_add(keytab_ctx);
117 status = krb5_to_nt_status(ret);
118 ctx->error_message = talloc_asprintf(ctx,
119 "Failed to add entries to keytab %s: %s",
120 keytab_ctx->keytab_name, error_message(ret));
124 ctx->result_message = talloc_asprintf(ctx,
125 "Vampired %d accounts to keytab %s",
127 keytab_ctx->keytab_name);
130 TALLOC_FREE(keytab_ctx);
134 /****************************************************************
135 ****************************************************************/
137 static NTSTATUS parse_supplemental_credentials(TALLOC_CTX *mem_ctx,
138 const DATA_BLOB *blob,
139 struct package_PrimaryKerberosCtr3 **pkb3,
140 struct package_PrimaryKerberosCtr4 **pkb4)
143 enum ndr_err_code ndr_err;
144 struct supplementalCredentialsBlob scb;
145 struct supplementalCredentialsPackage *scpk = NULL;
147 struct package_PrimaryKerberosBlob *pkb;
148 bool newer_keys = false;
151 ndr_err = ndr_pull_struct_blob_all(blob, mem_ctx, &scb,
152 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
153 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
154 status = ndr_map_error2ntstatus(ndr_err);
157 if (scb.sub.signature !=
158 SUPPLEMENTAL_CREDENTIALS_SIGNATURE)
160 if (DEBUGLEVEL >= 10) {
161 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
163 status = NT_STATUS_INVALID_PARAMETER;
166 for (j=0; j < scb.sub.num_packages; j++) {
167 if (strcmp("Primary:Kerberos-Newer-Keys",
168 scb.sub.packages[j].name) == 0)
170 scpk = &scb.sub.packages[j];
171 if (!scpk->data || !scpk->data[0]) {
177 } else if (strcmp("Primary:Kerberos",
178 scb.sub.packages[j].name) == 0)
181 * grab this but don't break here:
182 * there might still be newer-keys ...
184 scpk = &scb.sub.packages[j];
185 if (!scpk->data || !scpk->data[0]) {
193 status = NT_STATUS_OK;
197 scpk_blob = strhex_to_data_blob(mem_ctx, scpk->data);
198 if (!scpk_blob.data) {
199 status = NT_STATUS_NO_MEMORY;
203 pkb = talloc_zero(mem_ctx, struct package_PrimaryKerberosBlob);
205 status = NT_STATUS_NO_MEMORY;
208 ndr_err = ndr_pull_struct_blob(&scpk_blob, mem_ctx, pkb,
209 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
210 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
211 status = ndr_map_error2ntstatus(ndr_err);
215 if (!newer_keys && pkb->version != 3) {
216 status = NT_STATUS_INVALID_PARAMETER;
220 if (newer_keys && pkb->version != 4) {
221 status = NT_STATUS_INVALID_PARAMETER;
225 if (pkb->version == 4 && pkb4) {
226 *pkb4 = &pkb->ctr.ctr4;
227 } else if (pkb->version == 3 && pkb3) {
228 *pkb3 = &pkb->ctr.ctr3;
231 status = NT_STATUS_OK;
237 static NTSTATUS parse_object(TALLOC_CTX *mem_ctx,
238 struct libnet_keytab_context *ctx,
239 struct drsuapi_DsReplicaObjectListItemEx *cur)
241 NTSTATUS status = NT_STATUS_OK;
245 struct drsuapi_DsReplicaAttribute *attr;
246 bool got_pwd = false;
248 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
249 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
251 char *object_dn = NULL;
254 uint32_t num_spns = 0;
258 uint32_t sam_type = 0;
260 uint32_t pwd_history_len = 0;
261 uint8_t *pwd_history = NULL;
263 ZERO_STRUCT(nt_passwd);
265 object_dn = talloc_strdup(mem_ctx, cur->object.identifier->dn);
267 return NT_STATUS_NO_MEMORY;
270 DEBUG(3, ("parsing object '%s'\n", object_dn));
272 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
274 attr = &cur->object.attribute_ctr.attributes[i];
276 if (attr->attid == DRSUAPI_ATTID_servicePrincipalName) {
278 num_spns = attr->value_ctr.num_values;
279 spn = talloc_array(mem_ctx, char *, num_spns);
280 for (count = 0; count < num_spns; count++) {
281 blob = attr->value_ctr.values[count].blob;
282 pull_string_talloc(spn, NULL, 0,
284 blob->data, blob->length,
289 if (attr->value_ctr.num_values != 1) {
293 if (!attr->value_ctr.values[0].blob) {
297 blob = attr->value_ctr.values[0].blob;
299 switch (attr->attid) {
300 case DRSUAPI_ATTID_unicodePwd:
302 if (blob->length != 16) {
306 memcpy(&nt_passwd, blob->data, 16);
309 /* pick the kvno from the meta_data version,
310 * thanks, metze, for explaining this */
312 if (!cur->meta_data_ctr) {
315 if (cur->meta_data_ctr->count !=
316 cur->object.attribute_ctr.num_attributes) {
319 kvno = cur->meta_data_ctr->meta_data[i].version;
321 case DRSUAPI_ATTID_ntPwdHistory:
322 pwd_history_len = blob->length / 16;
323 pwd_history = blob->data;
325 case DRSUAPI_ATTID_userPrincipalName:
326 pull_string_talloc(mem_ctx, NULL, 0, &upn,
327 blob->data, blob->length,
330 case DRSUAPI_ATTID_sAMAccountName:
331 pull_string_talloc(mem_ctx, NULL, 0, &name,
332 blob->data, blob->length,
335 case DRSUAPI_ATTID_sAMAccountType:
336 sam_type = IVAL(blob->data, 0);
338 case DRSUAPI_ATTID_userAccountControl:
339 uacc = IVAL(blob->data, 0);
341 case DRSUAPI_ATTID_supplementalCredentials:
342 status = parse_supplemental_credentials(mem_ctx,
346 if (!NT_STATUS_IS_OK(status)) {
347 DEBUG(2, ("parsing of supplemental "
348 "credentials failed: %s\n",
358 DEBUG(10, ("no password (unicodePwd) found - skipping.\n"));
363 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, 0, object_dn,
366 data_blob_talloc(mem_ctx, name,
368 if (!NT_STATUS_IS_OK(status)) {
372 /* look into keytab ... */
373 struct libnet_keytab_entry *entry = NULL;
374 char *principal = NULL;
376 DEBUG(10, ("looking for SAMACCOUNTNAME/%s@%s in keytayb...\n",
377 object_dn, ctx->dns_domain_name));
379 principal = talloc_asprintf(mem_ctx, "%s/%s@%s",
382 ctx->dns_domain_name);
384 DEBUG(1, ("talloc failed\n"));
385 return NT_STATUS_NO_MEMORY;
387 entry = libnet_keytab_search(ctx, principal, 0, ENCTYPE_NULL,
390 name = (char *)TALLOC_MEMDUP(mem_ctx,
391 entry->password.data,
392 entry->password.length);
394 DEBUG(1, ("talloc failed!"));
395 return NT_STATUS_NO_MEMORY;
397 DEBUG(10, ("found name %s\n", name));
401 DEBUG(10, ("entry not found\n"));
403 TALLOC_FREE(principal);
407 DEBUG(10, ("no name (sAMAccountName) found - skipping.\n"));
411 DEBUG(1,("#%02d: %s:%d, ", ctx->count, name, kvno));
412 DEBUGADD(1,("sAMAccountType: 0x%08x, userAccountControl: 0x%08x",
415 DEBUGADD(1,(", upn: %s", upn));
418 DEBUGADD(1, (", spns: ["));
419 for (i = 0; i < num_spns; i++) {
420 DEBUGADD(1, ("%s%s", spn[i],
421 (i+1 == num_spns)?"]":", "));
426 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno, name, NULL,
427 ENCTYPE_ARCFOUR_HMAC,
428 data_blob_talloc(mem_ctx, nt_passwd, 16));
430 if (!NT_STATUS_IS_OK(status)) {
434 /* add kerberos keys (if any) */
437 for (i=0; i < pkb4->num_keys; i++) {
438 if (!pkb4->keys[i].value) {
441 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno,
444 pkb4->keys[i].keytype,
445 *pkb4->keys[i].value);
446 if (!NT_STATUS_IS_OK(status)) {
450 for (i=0; i < pkb4->num_old_keys; i++) {
451 if (!pkb4->old_keys[i].value) {
454 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno - 1,
457 pkb4->old_keys[i].keytype,
458 *pkb4->old_keys[i].value);
459 if (!NT_STATUS_IS_OK(status)) {
463 for (i=0; i < pkb4->num_older_keys; i++) {
464 if (!pkb4->older_keys[i].value) {
467 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno - 2,
470 pkb4->older_keys[i].keytype,
471 *pkb4->older_keys[i].value);
472 if (!NT_STATUS_IS_OK(status)) {
479 for (i=0; i < pkb3->num_keys; i++) {
480 if (!pkb3->keys[i].value) {
483 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno, name,
485 pkb3->keys[i].keytype,
486 *pkb3->keys[i].value);
487 if (!NT_STATUS_IS_OK(status)) {
491 for (i=0; i < pkb3->num_old_keys; i++) {
492 if (!pkb3->old_keys[i].value) {
495 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno - 1,
498 pkb3->old_keys[i].keytype,
499 *pkb3->old_keys[i].value);
500 if (!NT_STATUS_IS_OK(status)) {
506 if ((kvno < 0) && (kvno < pwd_history_len)) {
510 /* add password history */
512 /* skip first entry */
520 for (; i<pwd_history_len; i++) {
521 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx, kvno--, name, NULL,
522 ENCTYPE_ARCFOUR_HMAC,
523 data_blob_talloc(mem_ctx, &pwd_history[i*16], 16));
524 if (!NT_STATUS_IS_OK(status)) {
532 static bool dn_is_in_object_list(struct dssync_context *ctx,
537 if (ctx->object_count == 0) {
541 for (count = 0; count < ctx->object_count; count++) {
542 if (strequal(ctx->object_dns[count], dn)) {
550 /****************************************************************
551 ****************************************************************/
553 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
555 struct drsuapi_DsReplicaObjectListItemEx *cur,
556 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
558 NTSTATUS status = NT_STATUS_OK;
559 struct libnet_keytab_context *keytab_ctx =
560 (struct libnet_keytab_context *)ctx->private_data;
562 for (; cur; cur = cur->next_object) {
564 * When not in single object replication mode,
565 * the object_dn list is used as a positive write filter.
567 if (!ctx->single_object_replication &&
568 !dn_is_in_object_list(ctx, cur->object.identifier->dn))
573 status = parse_object(mem_ctx, keytab_ctx, cur);
574 if (!NT_STATUS_IS_OK(status)) {
585 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
586 struct replUpToDateVectorBlob **pold_utdv)
588 return NT_STATUS_NOT_SUPPORTED;
591 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
592 struct replUpToDateVectorBlob *new_utdv)
594 return NT_STATUS_NOT_SUPPORTED;
597 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
599 struct drsuapi_DsReplicaObjectListItemEx *cur,
600 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
602 return NT_STATUS_NOT_SUPPORTED;
604 #endif /* defined(HAVE_ADS) */
606 const struct dssync_ops libnet_dssync_keytab_ops = {
607 .startup = keytab_startup,
608 .process_objects = keytab_process_objects,
609 .finish = keytab_finish,