dssync keytab: add parsing and logging of servicePrincipalName-s
[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, ENCTYPE_ARCFOUR_HMAC,
77                                      mem_ctx);
78         if (entry) {
79                 enum ndr_err_code ndr_err;
80                 old_utdv = talloc(mem_ctx, struct replUpToDateVectorBlob);
81
82                 ndr_err = ndr_pull_struct_blob(&entry->password, old_utdv,
83                                 old_utdv,
84                                 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
85                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
86                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
87                         ctx->error_message = talloc_asprintf(mem_ctx,
88                                         "Failed to pull UpToDateVector: %s",
89                                         nt_errstr(status));
90                         return status;
91                 }
92
93                 if (DEBUGLEVEL >= 10) {
94                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, old_utdv);
95                 }
96         }
97
98         if (pold_utdv) {
99                 *pold_utdv = old_utdv;
100         }
101
102         return NT_STATUS_OK;
103 }
104
105 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
106                               struct replUpToDateVectorBlob *new_utdv)
107 {
108         NTSTATUS status = NT_STATUS_OK;
109         krb5_error_code ret = 0;
110         struct libnet_keytab_context *keytab_ctx =
111                 (struct libnet_keytab_context *)ctx->private_data;
112
113         if (new_utdv) {
114                 enum ndr_err_code ndr_err;
115                 DATA_BLOB blob;
116
117                 if (DEBUGLEVEL >= 10) {
118                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, new_utdv);
119                 }
120
121                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, new_utdv,
122                                 (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
123                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
124                         status = ndr_map_error2ntstatus(ndr_err);
125                         ctx->error_message = talloc_asprintf(mem_ctx,
126                                         "Failed to push UpToDateVector: %s",
127                                         nt_errstr(status));
128                         goto done;
129                 }
130
131                 status = add_to_keytab_entries(mem_ctx, keytab_ctx, 0,
132                                                ctx->nc_dn, "UTDV",
133                                                ENCTYPE_ARCFOUR_HMAC,
134                                                blob);
135                 if (!NT_STATUS_IS_OK(status)) {
136                         goto done;
137                 }
138         }
139
140         ret = libnet_keytab_add(keytab_ctx);
141         if (ret) {
142                 status = krb5_to_nt_status(ret);
143                 ctx->error_message = talloc_asprintf(mem_ctx,
144                         "Failed to add entries to keytab %s: %s",
145                         keytab_ctx->keytab_name, error_message(ret));
146                 goto done;
147         }
148
149         ctx->result_message = talloc_asprintf(mem_ctx,
150                 "Vampired %d accounts to keytab %s",
151                 keytab_ctx->count,
152                 keytab_ctx->keytab_name);
153
154 done:
155         TALLOC_FREE(keytab_ctx);
156         return status;
157 }
158
159 /****************************************************************
160 ****************************************************************/
161
162 static NTSTATUS parse_object(TALLOC_CTX *mem_ctx,
163                              struct libnet_keytab_context *ctx,
164                              struct drsuapi_DsReplicaObjectListItemEx *cur)
165 {
166         NTSTATUS status = NT_STATUS_OK;
167         uchar nt_passwd[16];
168         DATA_BLOB *blob;
169         int i = 0;
170         struct drsuapi_DsReplicaAttribute *attr;
171         bool got_pwd = false;
172
173         char *upn = NULL;
174         char **spn = NULL;
175         uint32_t num_spns = 0;
176         char *name = NULL;
177         uint32_t kvno = 0;
178         uint32_t uacc = 0;
179         uint32_t sam_type = 0;
180
181         uint32_t pwd_history_len = 0;
182         uint8_t *pwd_history = NULL;
183
184         ZERO_STRUCT(nt_passwd);
185
186         for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
187
188                 attr = &cur->object.attribute_ctr.attributes[i];
189
190                 if (attr->attid == DRSUAPI_ATTRIBUTE_servicePrincipalName) {
191                         uint32_t count;
192                         num_spns = attr->value_ctr.num_values;
193                         spn = TALLOC_ARRAY(mem_ctx, char *, num_spns);
194                         for (count = 0; count < num_spns; count++) {
195                                 blob = attr->value_ctr.values[count].blob;
196                                 pull_string_talloc(spn, NULL, 0,
197                                                    &spn[count],
198                                                    blob->data, blob->length,
199                                                    STR_UNICODE);
200                         }
201                 }
202
203                 if (attr->value_ctr.num_values != 1) {
204                         continue;
205                 }
206
207                 if (!attr->value_ctr.values[0].blob) {
208                         continue;
209                 }
210
211                 blob = attr->value_ctr.values[0].blob;
212
213                 switch (attr->attid) {
214                         case DRSUAPI_ATTRIBUTE_unicodePwd:
215
216                                 if (blob->length != 16) {
217                                         break;
218                                 }
219
220                                 memcpy(&nt_passwd, blob->data, 16);
221                                 got_pwd = true;
222
223                                 /* pick the kvno from the meta_data version,
224                                  * thanks, metze, for explaining this */
225
226                                 if (!cur->meta_data_ctr) {
227                                         break;
228                                 }
229                                 if (cur->meta_data_ctr->count !=
230                                     cur->object.attribute_ctr.num_attributes) {
231                                         break;
232                                 }
233                                 kvno = cur->meta_data_ctr->meta_data[i].version;
234                                 break;
235                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
236                                 pwd_history_len = blob->length / 16;
237                                 pwd_history = blob->data;
238                                 break;
239                         case DRSUAPI_ATTRIBUTE_userPrincipalName:
240                                 pull_string_talloc(mem_ctx, NULL, 0, &upn,
241                                                    blob->data, blob->length,
242                                                    STR_UNICODE);
243                                 break;
244                         case DRSUAPI_ATTRIBUTE_sAMAccountName:
245                                 pull_string_talloc(mem_ctx, NULL, 0, &name,
246                                                    blob->data, blob->length,
247                                                    STR_UNICODE);
248                                 break;
249                         case DRSUAPI_ATTRIBUTE_sAMAccountType:
250                                 sam_type = IVAL(blob->data, 0);
251                                 break;
252                         case DRSUAPI_ATTRIBUTE_userAccountControl:
253                                 uacc = IVAL(blob->data, 0);
254                                 break;
255                         default:
256                                 break;
257                 }
258         }
259
260         if (!name) {
261                 DEBUG(10, ("no name (sAMAccountName) found - skipping.\n"));
262                 return NT_STATUS_OK;
263         }
264
265         if (!got_pwd) {
266                 DEBUG(10, ("no password (unicodePwd) found - skipping.\n"));
267                 return NT_STATUS_OK;
268         }
269
270         DEBUG(1,("#%02d: %s:%d, ", ctx->count, name, kvno));
271         DEBUGADD(1,("sAMAccountType: 0x%08x, userAccountControl: 0x%08x",
272                 sam_type, uacc));
273         if (upn) {
274                 DEBUGADD(1,(", upn: %s", upn));
275         }
276         if (num_spns > 0) {
277                 DEBUGADD(1, (", spns: ["));
278                 for (i = 0; i < num_spns; i++) {
279                         DEBUGADD(1, ("%s%s", spn[i],
280                                      (i+1 == num_spns)?"]":", "));
281                 }
282         }
283         DEBUGADD(1,("\n"));
284
285         status = add_to_keytab_entries(mem_ctx, ctx, kvno, name, NULL,
286                                        ENCTYPE_ARCFOUR_HMAC,
287                                        data_blob_talloc(mem_ctx, nt_passwd, 16));
288
289         if (!NT_STATUS_IS_OK(status)) {
290                 return status;
291         }
292
293         if ((kvno < 0) && (kvno < pwd_history_len)) {
294                 return status;
295         }
296
297         /* add password history */
298
299         /* skip first entry */
300         if (got_pwd) {
301                 kvno--;
302                 i = 1;
303         } else {
304                 i = 0;
305         }
306
307         for (; i<pwd_history_len; i++) {
308                 status = add_to_keytab_entries(mem_ctx, ctx, kvno--, name, NULL,
309                                 ENCTYPE_ARCFOUR_HMAC,
310                                 data_blob_talloc(mem_ctx, &pwd_history[i*16], 16));
311                 if (!NT_STATUS_IS_OK(status)) {
312                         break;
313                 }
314         }
315
316         return status;
317 }
318
319 /****************************************************************
320 ****************************************************************/
321
322 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
323                                        TALLOC_CTX *mem_ctx,
324                                        struct drsuapi_DsReplicaObjectListItemEx *cur,
325                                        struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
326 {
327         NTSTATUS status = NT_STATUS_OK;
328         struct libnet_keytab_context *keytab_ctx =
329                 (struct libnet_keytab_context *)ctx->private_data;
330
331         for (; cur; cur = cur->next_object) {
332                 status = parse_object(mem_ctx, keytab_ctx, cur);
333                 if (!NT_STATUS_IS_OK(status)) {
334                         goto out;
335                 }
336         }
337
338  out:
339         return status;
340 }
341
342 #else
343
344 static NTSTATUS keytab_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
345                                struct replUpToDateVectorBlob **pold_utdv)
346 {
347         return NT_STATUS_NOT_SUPPORTED;
348 }
349
350 static NTSTATUS keytab_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
351                               struct replUpToDateVectorBlob *new_utdv)
352 {
353         return NT_STATUS_NOT_SUPPORTED;
354 }
355
356 static NTSTATUS keytab_process_objects(struct dssync_context *ctx,
357                                        TALLOC_CTX *mem_ctx,
358                                        struct drsuapi_DsReplicaObjectListItemEx *cur,
359                                        struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
360 {
361         return NT_STATUS_NOT_SUPPORTED;
362 }
363 #endif /* defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC) */
364
365 const struct dssync_ops libnet_dssync_keytab_ops = {
366         .startup                = keytab_startup,
367         .process_objects        = keytab_process_objects,
368         .finish                 = keytab_finish,
369 };