2 Unix SMB/CIFS implementation.
4 Copyright (C) Guenther Deschner <gd@samba.org> 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libnet/libnet.h"
23 #if defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC)
25 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx)
27 krb5_error_code ret = 0;
28 struct libnet_keytab_context *keytab_ctx;
30 ret = libnet_keytab_init(mem_ctx, ctx->output_filename, &keytab_ctx);
32 return krb5_to_nt_status(ret);
35 keytab_ctx->dns_domain_name = ctx->dns_domain_name;
36 ctx->private_data = keytab_ctx;
41 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx)
43 NTSTATUS status = NT_STATUS_OK;
44 krb5_error_code ret = 0;
45 struct libnet_keytab_context *keytab_ctx =
46 (struct libnet_keytab_context *)ctx->private_data;
48 ret = libnet_keytab_add(keytab_ctx);
50 status = krb5_to_nt_status(ret);
51 ctx->error_message = talloc_asprintf(mem_ctx,
52 "Failed to add entries to keytab %s: %s",
53 keytab_ctx->keytab_name, error_message(ret));
57 ctx->result_message = talloc_asprintf(mem_ctx,
58 "Vampired %d accounts to keytab %s",
60 keytab_ctx->keytab_name);
63 TALLOC_FREE(keytab_ctx);
67 /****************************************************************
68 ****************************************************************/
70 static NTSTATUS parse_object(TALLOC_CTX *mem_ctx,
71 struct libnet_keytab_context *ctx,
72 struct drsuapi_DsReplicaObjectListItemEx *cur)
74 NTSTATUS status = NT_STATUS_OK;
76 struct libnet_keytab_entry entry;
79 struct drsuapi_DsReplicaAttribute *attr;
86 uint32_t sam_type = 0;
88 uint32_t pwd_history_len = 0;
89 uint8_t *pwd_history = NULL;
91 ZERO_STRUCT(nt_passwd);
93 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
95 attr = &cur->object.attribute_ctr.attributes[i];
97 if (attr->value_ctr.num_values != 1) {
101 if (!attr->value_ctr.values[0].blob) {
105 blob = attr->value_ctr.values[0].blob;
107 switch (attr->attid) {
108 case DRSUAPI_ATTRIBUTE_unicodePwd:
110 if (blob->length != 16) {
114 memcpy(&nt_passwd, blob->data, 16);
117 /* pick the kvno from the meta_data version,
118 * thanks, metze, for explaining this */
120 if (!cur->meta_data_ctr) {
123 if (cur->meta_data_ctr->count !=
124 cur->object.attribute_ctr.num_attributes) {
127 kvno = cur->meta_data_ctr->meta_data[i].version;
129 case DRSUAPI_ATTRIBUTE_ntPwdHistory:
130 pwd_history_len = blob->length / 16;
131 pwd_history = blob->data;
133 case DRSUAPI_ATTRIBUTE_msDS_KeyVersionNumber:
134 kvno = IVAL(blob->data, 0);
136 case DRSUAPI_ATTRIBUTE_userPrincipalName:
137 pull_string_talloc(mem_ctx, NULL, 0, &upn,
138 blob->data, blob->length,
141 case DRSUAPI_ATTRIBUTE_sAMAccountName:
142 pull_string_talloc(mem_ctx, NULL, 0, &name,
143 blob->data, blob->length,
146 case DRSUAPI_ATTRIBUTE_sAMAccountType:
147 sam_type = IVAL(blob->data, 0);
149 case DRSUAPI_ATTRIBUTE_userAccountControl:
150 uacc = IVAL(blob->data, 0);
157 if (!got_pwd || !name) {
161 DEBUG(1,("#%02d: %s:%d, ", ctx->count, name, kvno));
162 DEBUGADD(1,("sAMAccountType: 0x%08x, userAccountControl: 0x%08x ",
165 DEBUGADD(1,("upn: %s", upn));
170 entry.name = talloc_strdup(mem_ctx, name);
171 entry.principal = talloc_asprintf(mem_ctx, "%s@%s",
172 name, ctx->dns_domain_name);
173 entry.password = data_blob_talloc(mem_ctx, nt_passwd, 16);
174 NT_STATUS_HAVE_NO_MEMORY(entry.name);
175 NT_STATUS_HAVE_NO_MEMORY(entry.principal);
176 NT_STATUS_HAVE_NO_MEMORY(entry.password.data);
178 ADD_TO_ARRAY(mem_ctx, struct libnet_keytab_entry, entry,
179 &ctx->entries, &ctx->count);
181 if ((kvno < 0) && (kvno < pwd_history_len)) {
185 /* add password history */
187 /* skip first entry */
195 for (; i<pwd_history_len; i++) {
198 entry.name = talloc_strdup(mem_ctx, name);
199 entry.principal = talloc_asprintf(mem_ctx, "%s@%s",
200 name, ctx->dns_domain_name);
201 entry.password = data_blob_talloc(mem_ctx, &pwd_history[i*16], 16);
202 NT_STATUS_HAVE_NO_MEMORY(entry.name);
203 NT_STATUS_HAVE_NO_MEMORY(entry.principal);
204 NT_STATUS_HAVE_NO_MEMORY(entry.password.data);
206 ADD_TO_ARRAY(mem_ctx, struct libnet_keytab_entry, entry,
207 &ctx->entries, &ctx->count);
213 /****************************************************************
214 ****************************************************************/
216 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
218 struct drsuapi_DsReplicaObjectListItemEx *cur,
219 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
221 NTSTATUS status = NT_STATUS_OK;
222 struct libnet_keytab_context *keytab_ctx =
223 (struct libnet_keytab_context *)ctx->private_data;
225 for (; cur; cur = cur->next_object) {
226 status = parse_object(mem_ctx, keytab_ctx, cur);
227 if (!NT_STATUS_IS_OK(status)) {
238 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx)
240 return NT_STATUS_NOT_SUPPORTED;
243 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx)
245 return NT_STATUS_NOT_SUPPORTED;
248 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
250 struct drsuapi_DsReplicaObjectListItemEx *cur,
251 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
253 return NT_STATUS_NOT_SUPPORTED;
255 #endif /* defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC) */
257 const struct dssync_ops libnet_dssync_keytab_ops = {
258 .startup = keytab_startup,
259 .process_objects = keytab_process_objects,
260 .finish = keytab_finish,