summaryrefslogtreecommitdiff
path: root/source3/libnet/libnet_dssync_keytab.c
blob: 132a58d353de90000daf8063885523b7169dde01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
   Unix SMB/CIFS implementation.

   Copyright (C) Guenther Deschner <gd@samba.org> 2008

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libnet/libnet.h"

#if defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC)

/****************************************************************
****************************************************************/

static NTSTATUS parse_object(TALLOC_CTX *mem_ctx,
			     struct libnet_keytab_context *ctx,
			     struct drsuapi_DsReplicaObjectListItemEx *cur)
{
	NTSTATUS status = NT_STATUS_OK;
	uchar nt_passwd[16];
	struct libnet_keytab_entry entry;
	DATA_BLOB *blob;
	int i = 0;
	struct drsuapi_DsReplicaAttribute *attr;
	bool got_pwd = false;

	char *upn = NULL;
	char *name = NULL;
	uint32_t kvno = 0;
	uint32_t uacc = 0;
	uint32_t sam_type = 0;

	uint32_t pwd_history_len = 0;
	uint8_t *pwd_history = NULL;

	ZERO_STRUCT(nt_passwd);

	for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {

		attr = &cur->object.attribute_ctr.attributes[i];

		if (attr->value_ctr.num_values != 1) {
			continue;
		}

		if (!attr->value_ctr.values[0].blob) {
			continue;
		}

		blob = attr->value_ctr.values[0].blob;

		switch (attr->attid) {
			case DRSUAPI_ATTRIBUTE_unicodePwd:

				if (blob->length != 16) {
					break;
				}

				memcpy(&nt_passwd, blob->data, 16);
				got_pwd = true;

				/* pick the kvno from the meta_data version,
				 * thanks, metze, for explaining this */

				if (!cur->meta_data_ctr) {
					break;
				}
				if (cur->meta_data_ctr->count !=
				    cur->object.attribute_ctr.num_attributes) {
					break;
				}
				kvno = cur->meta_data_ctr->meta_data[i].version;
				break;
			case DRSUAPI_ATTRIBUTE_ntPwdHistory:
				pwd_history_len = blob->length / 16;
				pwd_history = blob->data;
				break;
			case DRSUAPI_ATTRIBUTE_msDS_KeyVersionNumber:
				kvno = IVAL(blob->data, 0);
				break;
			case DRSUAPI_ATTRIBUTE_userPrincipalName:
				pull_string_talloc(mem_ctx, NULL, 0, &upn,
						   blob->data, blob->length,
						   STR_UNICODE);
				break;
			case DRSUAPI_ATTRIBUTE_sAMAccountName:
				pull_string_talloc(mem_ctx, NULL, 0, &name,
						   blob->data, blob->length,
						   STR_UNICODE);
				break;
			case DRSUAPI_ATTRIBUTE_sAMAccountType:
				sam_type = IVAL(blob->data, 0);
				break;
			case DRSUAPI_ATTRIBUTE_userAccountControl:
				uacc = IVAL(blob->data, 0);
				break;
			default:
				break;
		}
	}

	if (!got_pwd || !name) {
		return NT_STATUS_OK;
	}

	DEBUG(1,("#%02d: %s:%d, ", ctx->count, name, kvno));
	DEBUGADD(1,("sAMAccountType: 0x%08x, userAccountControl: 0x%08x ",
		sam_type, uacc));
	if (upn) {
		DEBUGADD(1,("upn: %s", upn));
	}
	DEBUGADD(1,("\n"));

	entry.kvno = kvno;
	entry.name = talloc_strdup(mem_ctx, name);
	entry.principal = talloc_asprintf(mem_ctx, "%s@%s",
					  name, ctx->dns_domain_name);
	entry.password = data_blob_talloc(mem_ctx, nt_passwd, 16);
	NT_STATUS_HAVE_NO_MEMORY(entry.name);
	NT_STATUS_HAVE_NO_MEMORY(entry.principal);
	NT_STATUS_HAVE_NO_MEMORY(entry.password.data);

	ADD_TO_ARRAY(mem_ctx, struct libnet_keytab_entry, entry,
		     &ctx->entries, &ctx->count);

	if ((kvno < 0) && (kvno < pwd_history_len)) {
		return status;
	}

	/* add password history */

	/* skip first entry */
	if (got_pwd) {
		kvno--;
		i = 1;
	} else {
		i = 0;
	}

	for (; i<pwd_history_len; i++) {

		entry.kvno = kvno--;
		entry.name = talloc_strdup(mem_ctx, name);
		entry.principal = talloc_asprintf(mem_ctx, "%s@%s",
						  name, ctx->dns_domain_name);
		entry.password = data_blob_talloc(mem_ctx, &pwd_history[i*16], 16);
		NT_STATUS_HAVE_NO_MEMORY(entry.name);
		NT_STATUS_HAVE_NO_MEMORY(entry.principal);
		NT_STATUS_HAVE_NO_MEMORY(entry.password.data);

		ADD_TO_ARRAY(mem_ctx, struct libnet_keytab_entry, entry,
			     &ctx->entries, &ctx->count);
	}

	return status;
}

/****************************************************************
****************************************************************/

NTSTATUS libnet_dssync_dump_keytab(TALLOC_CTX *mem_ctx,
				   struct drsuapi_DsReplicaObjectListItemEx *cur,
				   struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
				   bool last_query,
				   struct dssync_context *ctx)
{
	NTSTATUS status = NT_STATUS_OK;
	krb5_error_code ret = 0;
	static struct libnet_keytab_context *keytab_ctx = NULL;

	if (!keytab_ctx) {
		ret = libnet_keytab_init(mem_ctx,
					 ctx->output_filename,
					 &keytab_ctx);
		if (ret) {
			status = krb5_to_nt_status(ret);
			goto out;
		}

		keytab_ctx->dns_domain_name = ctx->dns_domain_name;
	}

	for (; cur; cur = cur->next_object) {
		status = parse_object(mem_ctx, keytab_ctx, cur);
		if (!NT_STATUS_IS_OK(status)) {
			goto out;
		}
	}

	if (last_query) {

		ret = libnet_keytab_add(keytab_ctx);
		if (ret) {
			status = krb5_to_nt_status(ret);
			ctx->error_message = talloc_asprintf(mem_ctx,
				"Failed to add entries to keytab %s: %s",
				keytab_ctx->keytab_name, error_message(ret));
			goto out;
		}

		ctx->result_message = talloc_asprintf(mem_ctx,
			"Vampired %d accounts to keytab %s",
			keytab_ctx->count,
			keytab_ctx->keytab_name);

		TALLOC_FREE(keytab_ctx);
	}

	return NT_STATUS_OK;
 out:
	TALLOC_FREE(keytab_ctx);

	return status;
}

#else

NTSTATUS libnet_dssync_dump_keytab(TALLOC_CTX *mem_ctx,
				   struct drsuapi_DsReplicaObjectListItemEx *cur,
				   struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
				   bool last_query,
				   struct dssync_context *ctx)
{
	return NT_STATUS_NOT_SUPPORTED;
}

#endif /* defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC) */