d33131348a8ae73d7906889ec5e7dc32ad869c62
[samba.git] / source4 / rpc_server / drsuapi / dcesrv_drsuapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the drsuapi pipe
5
6    Copyright (C) Stefan Metzmacher 2004
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_drsuapi.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "rpc_server/common/common.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "dsdb/common/util.h"
29 #include "libcli/security/security.h"
30 #include "libcli/security/session.h"
31 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
32 #include "auth/auth.h"
33 #include "param/param.h"
34 #include "lib/messaging/irpc.h"
35
36 #define DRSUAPI_UNSUPPORTED(fname) do { \
37         DEBUG(1,(__location__ ": Unsupported DRS call %s\n", #fname)); \
38         if (DEBUGLVL(2)) NDR_PRINT_IN_DEBUG(fname, r); \
39         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR); \
40 } while (0)
41
42 /* 
43   drsuapi_DsBind 
44 */
45 static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
46                        struct drsuapi_DsBind *r)
47 {
48         struct drsuapi_bind_state *b_state;
49         struct dcesrv_handle *handle;
50         struct drsuapi_DsBindInfoCtr *bind_info;
51         struct GUID site_guid;
52         struct ldb_result *site_res;
53         struct ldb_dn *server_site_dn;
54         static const char *site_attrs[] = { "objectGUID", NULL };
55         struct ldb_result *ntds_res;
56         struct ldb_dn *ntds_dn;
57         static const char *ntds_attrs[] = { "ms-DS-ReplicationEpoch", NULL };
58         uint32_t pid;
59         uint32_t repl_epoch;
60         int ret;
61         struct auth_session_info *auth_info;
62         WERROR werr;
63         bool connected_as_system = false;
64
65         r->out.bind_info = NULL;
66         ZERO_STRUCTP(r->out.bind_handle);
67
68         b_state = talloc_zero(mem_ctx, struct drsuapi_bind_state);
69         W_ERROR_HAVE_NO_MEMORY(b_state);
70
71         /* if this is a DC connecting, give them system level access */
72         werr = drs_security_level_check(dce_call, NULL, SECURITY_DOMAIN_CONTROLLER, NULL);
73         if (W_ERROR_IS_OK(werr)) {
74                 DEBUG(3,(__location__ ": doing DsBind with system_session\n"));
75                 auth_info = system_session(dce_call->conn->dce_ctx->lp_ctx);
76                 connected_as_system = true;
77         } else {
78                 auth_info = dce_call->conn->auth_state.session_info;
79         }
80
81         /*
82          * connect to the samdb
83          */
84         b_state->sam_ctx = samdb_connect(b_state, dce_call->event_ctx, 
85                                          dce_call->conn->dce_ctx->lp_ctx, auth_info, 0);
86         if (!b_state->sam_ctx) {
87                 return WERR_FOOBAR;
88         }
89
90         if (connected_as_system) {
91                 b_state->sam_ctx_system = b_state->sam_ctx;
92         } else {
93                 /* an RODC also needs system samdb access for secret
94                    attribute replication */
95                 werr = drs_security_level_check(dce_call, NULL, SECURITY_RO_DOMAIN_CONTROLLER,
96                                                 samdb_domain_sid(b_state->sam_ctx));
97                 if (W_ERROR_IS_OK(werr)) {
98                         b_state->sam_ctx_system = samdb_connect(b_state, dce_call->event_ctx,
99                                                                 dce_call->conn->dce_ctx->lp_ctx,
100                                                                 system_session(dce_call->conn->dce_ctx->lp_ctx), 0);
101                         if (!b_state->sam_ctx_system) {
102                                 return WERR_FOOBAR;
103                         }
104                 }
105         }
106
107         /*
108          * find out the guid of our own site
109          */
110         server_site_dn = samdb_server_site_dn(b_state->sam_ctx, mem_ctx);
111         W_ERROR_HAVE_NO_MEMORY(server_site_dn);
112
113         ret = ldb_search(b_state->sam_ctx, mem_ctx, &site_res,
114                                  server_site_dn, LDB_SCOPE_BASE, site_attrs,
115                                  "(objectClass=*)");
116         if (ret != LDB_SUCCESS) {
117                 return WERR_DS_DRA_INTERNAL_ERROR;
118         }
119         if (site_res->count != 1) {
120                 return WERR_DS_DRA_INTERNAL_ERROR;
121         }
122         site_guid = samdb_result_guid(site_res->msgs[0], "objectGUID");
123
124         /*
125          * lookup the local servers Replication Epoch
126          */
127         ntds_dn = samdb_ntds_settings_dn(b_state->sam_ctx, mem_ctx);
128         W_ERROR_HAVE_NO_MEMORY(ntds_dn);
129
130         ret = ldb_search(b_state->sam_ctx, mem_ctx, &ntds_res,
131                                  ntds_dn, LDB_SCOPE_BASE, ntds_attrs,
132                                  "(objectClass=*)");
133         if (ret != LDB_SUCCESS) {
134                 return WERR_DS_DRA_INTERNAL_ERROR;
135         }
136         if (ntds_res->count != 1) {
137                 return WERR_DS_DRA_INTERNAL_ERROR;
138         }
139         repl_epoch = ldb_msg_find_attr_as_uint(ntds_res->msgs[0],
140                                                "ms-DS-ReplicationEpoch", 0);
141
142         /*
143          * The "process identifier" of the client.
144          * According to the WSPP docs, sectin 5.35, this is
145          * for informational and debugging purposes only.
146          * The assignment is implementation specific.
147          */
148         pid = 0;
149
150         /*
151          * store the clients bind_guid
152          */
153         if (r->in.bind_guid) {
154                 b_state->remote_bind_guid = *r->in.bind_guid;
155         }
156
157         /*
158          * store the clients bind_info
159          */
160         if (r->in.bind_info) {
161                 switch (r->in.bind_info->length) {
162                 case 24: {
163                         struct drsuapi_DsBindInfo24 *info24;
164                         info24 = &r->in.bind_info->info.info24;
165                         b_state->remote_info28.supported_extensions     = info24->supported_extensions;
166                         b_state->remote_info28.site_guid                = info24->site_guid;
167                         b_state->remote_info28.pid                      = info24->pid;
168                         b_state->remote_info28.repl_epoch               = 0;
169                         break;
170                 }
171                 case 28:
172                         b_state->remote_info28 = r->in.bind_info->info.info28;
173                         break;
174                 }
175         }
176
177         /*
178          * fill in our local bind info 28
179          */
180         b_state->local_info28.supported_extensions      = 0;
181         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
182         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
183         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
184         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
185 #if 0 /* we don't support MSZIP compression (only decompression) */
186         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
187 #endif
188         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
189         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
190         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
191         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
192         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
193         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
194         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
195         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
196         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
197         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
198         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
199         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
200         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
201         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
202         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
203         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
204         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
205         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
206         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
207         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
208         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
209         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
210         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
211         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
212 #if 0 /* we don't support XPRESS compression yet */
213         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
214 #endif
215         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10;
216         b_state->local_info28.site_guid                 = site_guid;
217         b_state->local_info28.pid                       = pid;
218         b_state->local_info28.repl_epoch                = repl_epoch;
219
220         /*
221          * allocate the return bind_info
222          */
223         bind_info = talloc_zero(mem_ctx, struct drsuapi_DsBindInfoCtr);
224         W_ERROR_HAVE_NO_MEMORY(bind_info);
225
226         bind_info->length       = 28;
227         bind_info->info.info28  = b_state->local_info28;
228
229         /*
230          * allocate a bind handle
231          */
232         handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
233         W_ERROR_HAVE_NO_MEMORY(handle);
234         handle->data = talloc_steal(handle, b_state);
235
236         /*
237          * prepare reply
238          */
239         r->out.bind_info = bind_info;
240         *r->out.bind_handle = handle->wire_handle;
241
242         return WERR_OK;
243 }
244
245
246 /* 
247   drsuapi_DsUnbind 
248 */
249 static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
250                                struct drsuapi_DsUnbind *r)
251 {
252         struct dcesrv_handle *h;
253
254         *r->out.bind_handle = *r->in.bind_handle;
255
256         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
257
258         talloc_free(h);
259
260         ZERO_STRUCTP(r->out.bind_handle);
261
262         return WERR_OK;
263 }
264
265
266 /* 
267   drsuapi_DsReplicaSync 
268 */
269 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
270                                            struct drsuapi_DsReplicaSync *r)
271 {
272         WERROR status;
273         uint32_t timeout;
274
275         status = drs_security_level_check(dce_call, "DsReplicaSync", SECURITY_DOMAIN_CONTROLLER, NULL);
276         if (!W_ERROR_IS_OK(status)) {
277                 return status;
278         }
279
280         if (r->in.level != 1) {
281                 DEBUG(0,("DsReplicaSync called with unsupported level %d\n", r->in.level));
282                 return WERR_DS_DRA_INVALID_PARAMETER;
283         }
284
285         if (r->in.req->req1.options & DRSUAPI_DRS_ASYNC_OP) {
286                 timeout = IRPC_CALL_TIMEOUT;
287         } else {
288                 /*
289                  * use Infinite time for timeout in case
290                  * the caller made a sync call
291                  */
292                 timeout = IRPC_CALL_TIMEOUT_INF;
293         }
294
295         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
296                                      r, NDR_DRSUAPI_DSREPLICASYNC,
297                                      &ndr_table_drsuapi,
298                                      "dreplsrv", "DsReplicaSync",
299                                      timeout);
300
301         return WERR_OK;
302 }
303
304
305 /* 
306   drsuapi_DsReplicaAdd 
307 */
308 static WERROR dcesrv_drsuapi_DsReplicaAdd(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
309                                           struct drsuapi_DsReplicaAdd *r)
310 {
311         WERROR status;
312
313         status = drs_security_level_check(dce_call, "DsReplicaAdd", SECURITY_DOMAIN_CONTROLLER, NULL);
314         if (!W_ERROR_IS_OK(status)) {
315                 return status;
316         }
317
318         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
319                                      r, NDR_DRSUAPI_DSREPLICAADD,
320                                      &ndr_table_drsuapi,
321                                      "dreplsrv", "DsReplicaAdd",
322                                      IRPC_CALL_TIMEOUT);
323
324         return WERR_OK;
325 }
326
327
328 /* 
329   drsuapi_DsReplicaDel 
330 */
331 static WERROR dcesrv_drsuapi_DsReplicaDel(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
332                                           struct drsuapi_DsReplicaDel *r)
333 {
334         WERROR status;
335
336         status = drs_security_level_check(dce_call, "DsReplicaDel", SECURITY_DOMAIN_CONTROLLER, NULL);
337         if (!W_ERROR_IS_OK(status)) {
338                 return status;
339         }
340
341         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
342                                      r, NDR_DRSUAPI_DSREPLICADEL,
343                                      &ndr_table_drsuapi,
344                                      "dreplsrv", "DsReplicaDel",
345                                      IRPC_CALL_TIMEOUT);
346
347         return WERR_OK;
348 }
349
350
351 /* 
352   drsuapi_DsReplicaModify 
353 */
354 static WERROR dcesrv_drsuapi_DsReplicaMod(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
355                                           struct drsuapi_DsReplicaMod *r)
356 {
357         WERROR status;
358
359         status = drs_security_level_check(dce_call, "DsReplicaMod", SECURITY_DOMAIN_CONTROLLER, NULL);
360         if (!W_ERROR_IS_OK(status)) {
361                 return status;
362         }
363
364         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
365                                      r, NDR_DRSUAPI_DSREPLICAMOD,
366                                      &ndr_table_drsuapi,
367                                      "dreplsrv", "DsReplicaMod",
368                                      IRPC_CALL_TIMEOUT);
369
370         return WERR_OK;
371 }
372
373
374 /* 
375   DRSUAPI_VERIFY_NAMES 
376 */
377 static WERROR dcesrv_DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
378                        struct DRSUAPI_VERIFY_NAMES *r)
379 {
380         DRSUAPI_UNSUPPORTED(DRSUAPI_VERIFY_NAMES);
381 }
382
383
384 /* 
385   drsuapi_DsGetMemberships 
386 */
387 static WERROR dcesrv_drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
388                        struct drsuapi_DsGetMemberships *r)
389 {
390         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships);
391 }
392
393
394 /* 
395   DRSUAPI_INTER_DOMAIN_MOVE 
396 */
397 static WERROR dcesrv_DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
398                        struct DRSUAPI_INTER_DOMAIN_MOVE *r)
399 {
400         DRSUAPI_UNSUPPORTED(DRSUAPI_INTER_DOMAIN_MOVE);
401 }
402
403
404 /* 
405   drsuapi_DsGetNT4ChangeLog 
406 */
407 static WERROR dcesrv_drsuapi_DsGetNT4ChangeLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
408                        struct drsuapi_DsGetNT4ChangeLog *r)
409 {
410         DRSUAPI_UNSUPPORTED(drsuapi_DsGetNT4ChangeLog);
411 }
412
413 /* 
414   drsuapi_DsCrackNames 
415 */
416 static WERROR dcesrv_drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
417                             struct drsuapi_DsCrackNames *r)
418 {
419         struct drsuapi_bind_state *b_state;
420         struct dcesrv_handle *h;
421
422         *r->out.level_out = r->in.level;
423
424         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
425         b_state = h->data;
426
427         r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsNameCtr);
428         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
429
430         switch (r->in.level) {
431                 case 1: {
432                         switch(r->in.req->req1.format_offered){
433                         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT_NAME_SANS_DOMAIN_EX:
434                         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT_NAME_SANS_DOMAIN:
435                         case DRSUAPI_DS_NAME_FORMAT_STRING_SID_NAME:
436                         case DRSUAPI_DS_NAME_FORMAT_ALT_SECURITY_IDENTITIES_NAME:
437                         case DRSUAPI_DS_NAME_FORMAT_MAP_SCHEMA_GUID:
438                         case DRSUAPI_DS_NAME_FORMAT_LIST_NCS:
439                         case DRSUAPI_DS_NAME_FORMAT_LIST_DOMAINS:
440                         case DRSUAPI_DS_NAME_FORMAT_LIST_GLOBAL_CATALOG_SERVERS:
441                         case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_WITH_DCS_IN_SITE:
442                         case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_FOR_DOMAIN_IN_SITE:
443                         case DRSUAPI_DS_NAME_FORMAT_LIST_DOMAINS_IN_SITE:
444                         case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_IN_SITE:
445                         case DRSUAPI_DS_NAME_FORMAT_LIST_SITES:
446                         case DRSUAPI_DS_NAME_FORMAT_UPN_AND_ALTSECID:
447                         case DRSUAPI_DS_NAME_FORMAT_UPN_FOR_LOGON:
448                                 DEBUG(0, ("DsCrackNames: Unsupported operation requested: %X",
449                                           r->in.req->req1.format_offered));
450                                 return WERR_OK;
451                         case DRSUAPI_DS_NAME_FORMAT_LIST_INFO_FOR_SERVER:
452                                 return dcesrv_drsuapi_ListInfoServer(b_state->sam_ctx, mem_ctx, &r->in.req->req1, &r->out.ctr->ctr1);
453                         case DRSUAPI_DS_NAME_FORMAT_LIST_ROLES:
454                                 return dcesrv_drsuapi_ListRoles(b_state->sam_ctx, mem_ctx,
455                                                                 &r->in.req->req1, &r->out.ctr->ctr1);
456                         default:/* format_offered is in the enum drsuapi_DsNameFormat*/
457                                 return dcesrv_drsuapi_CrackNamesByNameFormat(b_state->sam_ctx, mem_ctx,
458                                                                              &r->in.req->req1, &r->out.ctr->ctr1);
459                         }
460                 }
461         }
462         return WERR_UNKNOWN_LEVEL;
463 }
464
465
466 /* 
467   drsuapi_DsRemoveDSServer
468 */
469 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
470                                        struct drsuapi_DsRemoveDSServer *r)
471 {
472         struct drsuapi_bind_state *b_state;
473         struct dcesrv_handle *h;
474         struct ldb_dn *ntds_dn;
475         int ret;
476         bool ok;
477         WERROR status;
478
479         *r->out.level_out = 1;
480
481         status = drs_security_level_check(dce_call, "DsRemoveDSServer", SECURITY_DOMAIN_CONTROLLER, NULL);
482         if (!W_ERROR_IS_OK(status)) {
483                 return status;
484         }
485
486         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
487         b_state = h->data;
488
489         switch (r->in.level) {
490         case 1:
491                 ntds_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, r->in.req->req1.server_dn);
492                 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
493
494                 ok = ldb_dn_validate(ntds_dn);
495                 if (!ok) {
496                         return WERR_FOOBAR;
497                 }
498
499                 /* TODO: it's likely that we need more checks here */
500
501                 ok = ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings");
502                 if (!ok) {
503                         return WERR_FOOBAR;
504                 }
505
506                 if (r->in.req->req1.commit) {
507                         ret = dsdb_delete(b_state->sam_ctx, ntds_dn, DSDB_TREE_DELETE);
508                         if (ret != LDB_SUCCESS) {
509                                 return WERR_FOOBAR;
510                         }
511                 }
512
513                 return WERR_OK;
514         default:
515                 break;
516         }
517
518         return WERR_FOOBAR;
519 }
520
521
522 /* 
523   DRSUAPI_REMOVE_DS_DOMAIN 
524 */
525 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
526                        struct DRSUAPI_REMOVE_DS_DOMAIN *r)
527 {
528         DRSUAPI_UNSUPPORTED(DRSUAPI_REMOVE_DS_DOMAIN);
529 }
530
531 /* Obtain the site name from a server DN */
532 static const char *result_site_name(struct ldb_dn *server_dn)
533 {
534         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
535         const struct ldb_val *val = ldb_dn_get_component_val(server_dn, 2);
536         const char *name = ldb_dn_get_component_name(server_dn, 2);
537
538         if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
539                 /* Ensure this matches the format.  This gives us a
540                  * bit more confidence that a 'cn' value will be a
541                  * ascii string */
542                 return NULL;
543         }
544         if (val) {
545                 return (char *)val->data;
546         }
547         return NULL;
548 }
549
550 /* 
551   drsuapi_DsGetDomainControllerInfo 
552 */
553 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state, 
554                                                 TALLOC_CTX *mem_ctx,
555                                                 struct drsuapi_DsGetDomainControllerInfo *r)
556 {
557         struct ldb_dn *sites_dn;
558         struct ldb_result *res;
559
560         const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
561         const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
562
563         const char *attrs_none[] = { NULL };
564
565         const char *attrs_site[] = { "objectGUID", NULL };
566
567         const char *attrs_ntds[] = { "options", "objectGUID", NULL };
568
569         const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
570         const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
571         const char **attrs;
572
573         struct drsuapi_DsGetDCInfoCtr1 *ctr1;
574         struct drsuapi_DsGetDCInfoCtr2 *ctr2;
575
576         int ret;
577         unsigned int i;
578
579         *r->out.level_out = r->in.req->req1.level;
580         r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsGetDCInfoCtr);
581         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
582
583         switch (*r->out.level_out) {
584         case -1:
585                 /* this level is not like the others */
586                 return WERR_UNKNOWN_LEVEL;
587         case 1:
588                 attrs = attrs_1;
589                 break;
590         case 2:
591                 attrs = attrs_2;
592                 break;
593         default:
594                 return WERR_UNKNOWN_LEVEL;
595         }
596
597         sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
598         if (!sites_dn) {
599                 return WERR_DS_OBJ_NOT_FOUND;
600         }
601
602         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
603                                  "(&(objectClass=server)(serverReference=*))");
604         
605         if (ret) {
606                 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n", 
607                           ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
608                 return WERR_GENERAL_FAILURE;
609         }
610
611         switch (*r->out.level_out) {
612         case 1:
613                 ctr1 = &r->out.ctr->ctr1;
614                 ctr1->count = res->count;
615                 ctr1->array = talloc_zero_array(mem_ctx, 
616                                                 struct drsuapi_DsGetDCInfo1, 
617                                                 res->count);
618                 for (i=0; i < res->count; i++) {
619                         struct ldb_dn *domain_dn;
620                         struct ldb_result *res_domain;
621                         struct ldb_result *res_account;
622                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
623                         
624                         struct ldb_dn *ref_dn
625                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
626                                                           mem_ctx, res->msgs[i], 
627                                                           "serverReference");
628
629                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
630                                 return WERR_NOMEM;
631                         }
632
633                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
634                                                  LDB_SCOPE_BASE, attrs_account_1,
635                                                 "(&(objectClass=computer)(userAccountControl:1.2.840.113556.1.4.803:=%u))",
636                                                 UF_SERVER_TRUST_ACCOUNT);
637                         if (ret == LDB_SUCCESS && res_account->count == 1) {
638                                 const char *errstr;
639                                 ctr1->array[i].dns_name
640                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
641                                 ctr1->array[i].netbios_name
642                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
643                                 ctr1->array[i].computer_dn
644                                         = ldb_dn_get_linearized(res_account->msgs[0]->dn);
645
646                                 /* Determine if this is the PDC */
647                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
648                                                                      mem_ctx, res_account->msgs[0]->dn,
649                                                                      &domain_dn, &errstr);
650                                 
651                                 if (ret == LDB_SUCCESS) {
652                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
653                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
654                                                                  ldb_dn_get_linearized(ntds_dn));
655                                         if (ret) {
656                                                 return WERR_GENERAL_FAILURE;
657                                         }
658                                         if (res_domain->count == 1) {
659                                                 ctr1->array[i].is_pdc = true;
660                                         }
661                                 }
662                         }
663                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
664                                 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n", 
665                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
666                         }
667
668                         /* Look at server DN and extract site component */
669                         ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
670                         ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
671
672
673                         ctr1->array[i].is_enabled = true;
674
675                 }
676                 break;
677         case 2:
678                 ctr2 = &r->out.ctr->ctr2;
679                 ctr2->count = res->count;
680                 ctr2->array = talloc_zero_array(mem_ctx, 
681                                                  struct drsuapi_DsGetDCInfo2, 
682                                                  res->count);
683                 for (i=0; i < res->count; i++) {
684                         struct ldb_dn *domain_dn;
685                         struct ldb_result *res_domain;
686                         struct ldb_result *res_account;
687                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
688                         struct ldb_result *res_ntds;
689                         struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
690                         struct ldb_result *res_site;
691                         struct ldb_dn *ref_dn
692                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
693                                                           mem_ctx, res->msgs[i], 
694                                                           "serverReference");
695
696                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
697                                 return WERR_NOMEM;
698                         }
699
700                         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
701                         if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
702                                 return WERR_NOMEM;
703                         }
704
705                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
706                                                  LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
707                         if (ret == LDB_SUCCESS && res_ntds->count == 1) {
708                                 ctr2->array[i].is_gc
709                                         = (ldb_msg_find_attr_as_uint(res_ntds->msgs[0], "options", 0) & DS_NTDSDSA_OPT_IS_GC);
710                                 ctr2->array[i].ntds_guid 
711                                         = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
712                                 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
713                         }
714                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
715                                 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n", 
716                                           ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
717                         }
718
719                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
720                                                  LDB_SCOPE_BASE, attrs_site, "objectClass=site");
721                         if (ret == LDB_SUCCESS && res_site->count == 1) {
722                                 ctr2->array[i].site_guid 
723                                         = samdb_result_guid(res_site->msgs[0], "objectGUID");
724                                 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
725                         }
726                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
727                                 DEBUG(5, ("warning: searching for site DN %s failed: %s\n", 
728                                           ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
729                         }
730
731                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
732                                                  LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
733                         if (ret == LDB_SUCCESS && res_account->count == 1) {
734                                 const char *errstr;
735                                 ctr2->array[i].dns_name
736                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
737                                 ctr2->array[i].netbios_name
738                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
739                                 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
740                                 ctr2->array[i].computer_guid 
741                                         = samdb_result_guid(res_account->msgs[0], "objectGUID");
742
743                                 /* Determine if this is the PDC */
744                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
745                                                                      mem_ctx, res_account->msgs[0]->dn,
746                                                                      &domain_dn, &errstr);
747                                 
748                                 if (ret == LDB_SUCCESS) {
749                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
750                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
751                                                                  ldb_dn_get_linearized(ntds_dn));
752                                         if (ret == LDB_SUCCESS && res_domain->count == 1) {
753                                                 ctr2->array[i].is_pdc = true;
754                                         }
755                                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
756                                                 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n", 
757                                                           ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
758                                         }
759                                 }
760                         }
761                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
762                                 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n", 
763                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
764                         }
765
766                         /* Look at server DN and extract site component */
767                         ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
768                         ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
769                         ctr2->array[i].server_guid 
770                                 = samdb_result_guid(res->msgs[i], "objectGUID");
771
772                         ctr2->array[i].is_enabled = true;
773
774                 }
775                 break;
776         default:
777                 return WERR_UNKNOWN_LEVEL;
778         }
779         return WERR_OK;
780 }
781
782 /* 
783   drsuapi_DsGetDomainControllerInfo 
784 */
785 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
786                                                 struct drsuapi_DsGetDomainControllerInfo *r)
787 {
788         struct dcesrv_handle *h;
789         struct drsuapi_bind_state *b_state;     
790         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
791         b_state = h->data;
792
793         switch (r->in.level) {
794         case 1:
795                 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
796         }
797
798         return WERR_UNKNOWN_LEVEL;
799 }
800
801
802
803 /* 
804   drsuapi_DsExecuteKCC 
805 */
806 static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
807                                   struct drsuapi_DsExecuteKCC *r)
808 {
809         WERROR status;
810         uint32_t timeout;
811         status = drs_security_level_check(dce_call, "DsExecuteKCC", SECURITY_DOMAIN_CONTROLLER, NULL);
812
813         if (!W_ERROR_IS_OK(status)) {
814                 return status;
815         }
816         if (r->in.req->ctr1.taskID != 0) {
817                 return WERR_INVALID_PARAM;
818         }
819         if (r->in.req->ctr1.flags & DRSUAPI_DS_EXECUTE_KCC_ASYNCHRONOUS_OPERATION) {
820                 timeout = IRPC_CALL_TIMEOUT;
821         } else {
822                 /*
823                  * use Infinite time for timeout in case
824                  * the caller made a sync call
825                  */
826                 timeout = IRPC_CALL_TIMEOUT_INF;
827         }
828
829         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSEXECUTEKCC,
830                                      &ndr_table_drsuapi, "kccsrv", "DsExecuteKCC",
831                                      timeout);
832         DEBUG(0, ("Forwarded the call to execute the KCC\n"));
833         return WERR_OK;
834 }
835
836
837 /* 
838   drsuapi_DsReplicaGetInfo 
839 */
840 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
841                        struct drsuapi_DsReplicaGetInfo *r)
842 {
843         enum security_user_level level;
844
845         if (!lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
846                          "drs", "disable_sec_check", false)) {
847                 level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
848                 if (level < SECURITY_DOMAIN_CONTROLLER) {
849                         DEBUG(1,(__location__ ": Administrator access required for DsReplicaGetInfo\n"));
850                         security_token_debug(0, 2, dce_call->conn->auth_state.session_info->security_token);
851                         return WERR_DS_DRA_ACCESS_DENIED;
852                 }
853         }
854
855         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICAGETINFO,
856                                      &ndr_table_drsuapi, "kccsrv", "DsReplicaGetInfo",
857                                      IRPC_CALL_TIMEOUT);
858
859         return WERR_OK;
860 }
861
862
863 /* 
864   DRSUAPI_ADD_SID_HISTORY 
865 */
866 static WERROR dcesrv_DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
867                        struct DRSUAPI_ADD_SID_HISTORY *r)
868 {
869         DRSUAPI_UNSUPPORTED(DRSUAPI_ADD_SID_HISTORY);
870 }
871
872 /* 
873   drsuapi_DsGetMemberships2 
874 */
875 static WERROR dcesrv_drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
876                        struct drsuapi_DsGetMemberships2 *r)
877 {
878         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships2);
879 }
880
881 /* 
882   DRSUAPI_REPLICA_VERIFY_OBJECTS 
883 */
884 static WERROR dcesrv_DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
885                        struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
886 {
887         DRSUAPI_UNSUPPORTED(DRSUAPI_REPLICA_VERIFY_OBJECTS);
888 }
889
890
891 /* 
892   DRSUAPI_GET_OBJECT_EXISTENCE 
893 */
894 static WERROR dcesrv_DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
895                        struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
896 {
897         DRSUAPI_UNSUPPORTED(DRSUAPI_GET_OBJECT_EXISTENCE);
898 }
899
900
901 /* 
902   drsuapi_QuerySitesByCost 
903 */
904 static WERROR dcesrv_drsuapi_QuerySitesByCost(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
905                        struct drsuapi_QuerySitesByCost *r)
906 {
907         DRSUAPI_UNSUPPORTED(drsuapi_QuerySitesByCost);
908 }
909
910
911 /* include the generated boilerplate */
912 #include "librpc/gen_ndr/ndr_drsuapi_s.c"