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