dssync keytab: add store enctypes in the libnet_keytype_entry structs.
[samba.git] / source3 / libnet / libnet_dssync_keytab.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Guenther Deschner <gd@samba.org> 2008
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "libnet/libnet.h"
22 #include "librpc/gen_ndr/ndr_drsblobs.h"
23
24 #if defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC)
25
26 static NTSTATUS add_to_keytab_entries(TALLOC_CTX *mem_ctx,
27                                       struct libnet_keytab_context *ctx,
28                                       uint32_t kvno,
29                                       const char *name,
30                                       const char *prefix,
31                                       const krb5_enctype enctype,
32                                       DATA_BLOB blob)
33 {
34         struct libnet_keytab_entry entry;
35
36         entry.kvno = kvno;
37         entry.name = talloc_strdup(mem_ctx, name);
38         entry.principal = talloc_asprintf(mem_ctx, "%s%s%s@%s",
39                                           prefix ? prefix : "",
40                                           prefix ? "/" : "",
41                                           name, ctx->dns_domain_name);
42         entry.enctype = enctype;
43         entry.password = blob;
44         NT_STATUS_HAVE_NO_MEMORY(entry.name);
45         NT_STATUS_HAVE_NO_MEMORY(entry.principal);
46         NT_STATUS_HAVE_NO_MEMORY(entry.password.data);
47
48         ADD_TO_ARRAY(mem_ctx, struct libnet_keytab_entry, entry,
49                      &ctx->entries, &ctx->count);
50         NT_STATUS_HAVE_NO_MEMORY(ctx->entries);
51
52         return NT_STATUS_OK;
53 }
54
55 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
56                                struct replUpToDateVectorBlob **pold_utdv)
57 {
58         krb5_error_code ret = 0;
59         struct libnet_keytab_context *keytab_ctx;
60         struct libnet_keytab_entry *entry;
61         struct replUpToDateVectorBlob *old_utdv = NULL;
62         char *principal;
63
64         ret = libnet_keytab_init(mem_ctx, ctx->output_filename, &keytab_ctx);
65         if (ret) {
66                 return krb5_to_nt_status(ret);
67         }
68
69         keytab_ctx->dns_domain_name = ctx->dns_domain_name;
70         ctx->private_data = keytab_ctx;
71
72         principal = talloc_asprintf(mem_ctx, "UTDV/%s@%s",
73                                     ctx->nc_dn, ctx->dns_domain_name);
74         NT_STATUS_HAVE_NO_MEMORY(principal);
75
76         entry = libnet_keytab_search(keytab_ctx, principal, 0, mem_ctx);
77         if (entry) {
78                 enum ndr_err_code ndr_err;
79                 old_utdv = talloc(mem_ctx, struct replUpToDateVectorBlob);
80
81                 ndr_err = ndr_pull_struct_blob(&entry->password, old_utdv,
82                                 old_utdv,
83                                 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
84                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
85                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
86                         ctx->error_message = talloc_asprintf(mem_ctx,
87                                         "Failed to pull UpToDateVector: %s",
88                                         nt_errstr(status));
89                         return status;
90                 }
91
92                 if (DEBUGLEVEL >= 10) {
93                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, old_utdv);
94                 }
95         }
96
97         if (pold_utdv) {
98                 *pold_utdv = old_utdv;
99         }
100
101         return NT_STATUS_OK;
102 }
103
104 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
105                               struct replUpToDateVectorBlob *new_utdv)
106 {
107         NTSTATUS status = NT_STATUS_OK;
108         krb5_error_code ret = 0;
109         struct libnet_keytab_context *keytab_ctx =
110                 (struct libnet_keytab_context *)ctx->private_data;
111
112         if (new_utdv) {
113                 enum ndr_err_code ndr_err;
114                 DATA_BLOB blob;
115
116                 if (DEBUGLEVEL >= 10) {
117                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, new_utdv);
118                 }
119
120                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, new_utdv,
121                                 (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
122                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
123                         status = ndr_map_error2ntstatus(ndr_err);
124                         ctx->error_message = talloc_asprintf(mem_ctx,
125                                         "Failed to push UpToDateVector: %s",
126                                         nt_errstr(status));
127                         goto done;
128                 }
129
130                 status = add_to_keytab_entries(mem_ctx, keytab_ctx, 0,
131                                                ctx->nc_dn, "UTDV",
132                                                ENCTYPE_NULL, blob);
133                 if (!NT_STATUS_IS_OK(status)) {
134                         goto done;
135                 }
136         }
137
138         ret = libnet_keytab_add(keytab_ctx);
139         if (ret) {
140                 status = krb5_to_nt_status(ret);
141                 ctx->error_message = talloc_asprintf(mem_ctx,
142                         "Failed to add entries to keytab %s: %s",
143                         keytab_ctx->keytab_name, error_message(ret));
144                 goto done;
145         }
146
147         ctx->result_message = talloc_asprintf(mem_ctx,
148                 "Vampired %d accounts to keytab %s",
149                 keytab_ctx->count,
150                 keytab_ctx->keytab_name);
151
152 done:
153         TALLOC_FREE(keytab_ctx);
154         return status;
155 }
156
157 /****************************************************************
158 ****************************************************************/
159
160 static NTSTATUS parse_object(TALLOC_CTX *mem_ctx,
161                              struct libnet_keytab_context *ctx,
162                              struct drsuapi_DsReplicaObjectListItemEx *cur)
163 {
164         NTSTATUS status = NT_STATUS_OK;
165         uchar nt_passwd[16];
166         DATA_BLOB *blob;
167         int i = 0;
168         struct drsuapi_DsReplicaAttribute *attr;
169         bool got_pwd = false;
170
171         char *upn = NULL;
172         char *name = NULL;
173         uint32_t kvno = 0;
174         uint32_t uacc = 0;
175         uint32_t sam_type = 0;
176
177         uint32_t pwd_history_len = 0;
178         uint8_t *pwd_history = NULL;
179
180         ZERO_STRUCT(nt_passwd);
181
182         for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
183
184                 attr = &cur->object.attribute_ctr.attributes[i];
185
186                 if (attr->value_ctr.num_values != 1) {
187                         continue;
188                 }
189
190                 if (!attr->value_ctr.values[0].blob) {
191                         continue;
192                 }
193
194                 blob = attr->value_ctr.values[0].blob;
195
196                 switch (attr->attid) {
197                         case DRSUAPI_ATTRIBUTE_unicodePwd:
198
199                                 if (blob->length != 16) {
200                                         break;
201                                 }
202
203                                 memcpy(&nt_passwd, blob->data, 16);
204                                 got_pwd = true;
205
206                                 /* pick the kvno from the meta_data version,
207                                  * thanks, metze, for explaining this */
208
209                                 if (!cur->meta_data_ctr) {
210                                         break;
211                                 }
212                                 if (cur->meta_data_ctr->count !=
213                                     cur->object.attribute_ctr.num_attributes) {
214                                         break;
215                                 }
216                                 kvno = cur->meta_data_ctr->meta_data[i].version;
217                                 break;
218                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
219                                 pwd_history_len = blob->length / 16;
220                                 pwd_history = blob->data;
221                                 break;
222                         case DRSUAPI_ATTRIBUTE_userPrincipalName:
223                                 pull_string_talloc(mem_ctx, NULL, 0, &upn,
224                                                    blob->data, blob->length,
225                                                    STR_UNICODE);
226                                 break;
227                         case DRSUAPI_ATTRIBUTE_sAMAccountName:
228                                 pull_string_talloc(mem_ctx, NULL, 0, &name,
229                                                    blob->data, blob->length,
230                                                    STR_UNICODE);
231                                 break;
232                         case DRSUAPI_ATTRIBUTE_sAMAccountType:
233                                 sam_type = IVAL(blob->data, 0);
234                                 break;
235                         case DRSUAPI_ATTRIBUTE_userAccountControl:
236                                 uacc = IVAL(blob->data, 0);
237                                 break;
238                         default:
239                                 break;
240                 }
241         }
242
243         if (!got_pwd || !name) {
244                 return NT_STATUS_OK;
245         }
246
247         DEBUG(1,("#%02d: %s:%d, ", ctx->count, name, kvno));
248         DEBUGADD(1,("sAMAccountType: 0x%08x, userAccountControl: 0x%08x ",
249                 sam_type, uacc));
250         if (upn) {
251                 DEBUGADD(1,("upn: %s", upn));
252         }
253         DEBUGADD(1,("\n"));
254
255         status = add_to_keytab_entries(mem_ctx, ctx, kvno, name, NULL,
256                                        ENCTYPE_ARCFOUR_HMAC,
257                                        data_blob_talloc(mem_ctx, nt_passwd, 16));
258
259         if (!NT_STATUS_IS_OK(status)) {
260                 return status;
261         }
262
263         if ((kvno < 0) && (kvno < pwd_history_len)) {
264                 return status;
265         }
266
267         /* add password history */
268
269         /* skip first entry */
270         if (got_pwd) {
271                 kvno--;
272                 i = 1;
273         } else {
274                 i = 0;
275         }
276
277         for (; i<pwd_history_len; i++) {
278                 status = add_to_keytab_entries(mem_ctx, ctx, kvno--, name, NULL,
279                                 ENCTYPE_ARCFOUR_HMAC,
280                                 data_blob_talloc(mem_ctx, &pwd_history[i*16], 16));
281                 if (!NT_STATUS_IS_OK(status)) {
282                         break;
283                 }
284         }
285
286         return status;
287 }
288
289 /****************************************************************
290 ****************************************************************/
291
292 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
293                                        TALLOC_CTX *mem_ctx,
294                                        struct drsuapi_DsReplicaObjectListItemEx *cur,
295                                        struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
296 {
297         NTSTATUS status = NT_STATUS_OK;
298         struct libnet_keytab_context *keytab_ctx =
299                 (struct libnet_keytab_context *)ctx->private_data;
300
301         for (; cur; cur = cur->next_object) {
302                 status = parse_object(mem_ctx, keytab_ctx, cur);
303                 if (!NT_STATUS_IS_OK(status)) {
304                         goto out;
305                 }
306         }
307
308  out:
309         return status;
310 }
311
312 #else
313
314 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
315                                struct replUpToDateVectorBlob **pold_utdv)
316 {
317         return NT_STATUS_NOT_SUPPORTED;
318 }
319
320 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
321                               struct replUpToDateVectorBlob *new_utdv)
322 {
323         return NT_STATUS_NOT_SUPPORTED;
324 }
325
326 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
327                                        TALLOC_CTX *mem_ctx,
328                                        struct drsuapi_DsReplicaObjectListItemEx *cur,
329                                        struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
330 {
331         return NT_STATUS_NOT_SUPPORTED;
332 }
333 #endif /* defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC) */
334
335 const struct dssync_ops libnet_dssync_keytab_ops = {
336         .startup                = keytab_startup,
337         .process_objects        = keytab_process_objects,
338         .finish                 = keytab_finish,
339 };