s4:drsuapi RPC server - this resembles more "samdb_is_gc"
[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 "libcli/security/security.h"
29 #include "libcli/security/session.h"
30 #include "rpc_server/drsuapi/dcesrv_drsuapi.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                 }
459         }
460         return WERR_UNKNOWN_LEVEL;
461 }
462
463
464 /* 
465   drsuapi_DsRemoveDSServer
466 */
467 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
468                                        struct drsuapi_DsRemoveDSServer *r)
469 {
470         struct drsuapi_bind_state *b_state;
471         struct dcesrv_handle *h;
472         struct ldb_dn *ntds_dn;
473         int ret;
474         bool ok;
475         WERROR status;
476
477         *r->out.level_out = 1;
478
479         status = drs_security_level_check(dce_call, "DsRemoveDSServer", SECURITY_DOMAIN_CONTROLLER, NULL);
480         if (!W_ERROR_IS_OK(status)) {
481                 return status;
482         }
483
484         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
485         b_state = h->data;
486
487         switch (r->in.level) {
488         case 1:
489                 ntds_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, r->in.req->req1.server_dn);
490                 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
491
492                 ok = ldb_dn_validate(ntds_dn);
493                 if (!ok) {
494                         return WERR_FOOBAR;
495                 }
496
497                 /* TODO: it's likely that we need more checks here */
498
499                 ok = ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings");
500                 if (!ok) {
501                         return WERR_FOOBAR;
502                 }
503
504                 if (r->in.req->req1.commit) {
505                         ret = ldb_delete(b_state->sam_ctx, ntds_dn);
506                         if (ret != LDB_SUCCESS) {
507                                 return WERR_FOOBAR;
508                         }
509                 }
510
511                 return WERR_OK;
512         default:
513                 break;
514         }
515
516         return WERR_FOOBAR;
517 }
518
519
520 /* 
521   DRSUAPI_REMOVE_DS_DOMAIN 
522 */
523 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
524                        struct DRSUAPI_REMOVE_DS_DOMAIN *r)
525 {
526         DRSUAPI_UNSUPPORTED(DRSUAPI_REMOVE_DS_DOMAIN);
527 }
528
529 /* Obtain the site name from a server DN */
530 static const char *result_site_name(struct ldb_dn *server_dn)
531 {
532         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
533         const struct ldb_val *val = ldb_dn_get_component_val(server_dn, 2);
534         const char *name = ldb_dn_get_component_name(server_dn, 2);
535
536         if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
537                 /* Ensure this matches the format.  This gives us a
538                  * bit more confidence that a 'cn' value will be a
539                  * ascii string */
540                 return NULL;
541         }
542         if (val) {
543                 return (char *)val->data;
544         }
545         return NULL;
546 }
547
548 /* 
549   drsuapi_DsGetDomainControllerInfo 
550 */
551 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state, 
552                                                 TALLOC_CTX *mem_ctx,
553                                                 struct drsuapi_DsGetDomainControllerInfo *r)
554 {
555         struct ldb_dn *sites_dn;
556         struct ldb_result *res;
557
558         const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
559         const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
560
561         const char *attrs_none[] = { NULL };
562
563         const char *attrs_site[] = { "objectGUID", NULL };
564
565         const char *attrs_ntds[] = { "options", "objectGUID", NULL };
566
567         const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
568         const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
569         const char **attrs;
570
571         struct drsuapi_DsGetDCInfoCtr1 *ctr1;
572         struct drsuapi_DsGetDCInfoCtr2 *ctr2;
573
574         int ret;
575         unsigned int i;
576
577         *r->out.level_out = r->in.req->req1.level;
578         r->out.ctr = talloc(mem_ctx, union drsuapi_DsGetDCInfoCtr);
579         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
580
581         sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
582         if (!sites_dn) {
583                 return WERR_DS_OBJ_NOT_FOUND;
584         }
585
586         switch (*r->out.level_out) {
587         case -1:
588                 /* this level is not like the others */
589                 return WERR_UNKNOWN_LEVEL;
590         case 1:
591                 attrs = attrs_1;
592                 break;
593         case 2:
594                 attrs = attrs_2;
595                 break;
596         default:
597                 return WERR_UNKNOWN_LEVEL;
598         }
599
600         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
601                                  "objectClass=server");
602         
603         if (ret) {
604                 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n", 
605                           ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
606                 return WERR_GENERAL_FAILURE;
607         }
608
609         switch (*r->out.level_out) {
610         case 1:
611                 ctr1 = &r->out.ctr->ctr1;
612                 ctr1->count = res->count;
613                 ctr1->array = talloc_zero_array(mem_ctx, 
614                                                 struct drsuapi_DsGetDCInfo1, 
615                                                 res->count);
616                 for (i=0; i < res->count; i++) {
617                         struct ldb_dn *domain_dn;
618                         struct ldb_result *res_domain;
619                         struct ldb_result *res_account;
620                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
621                         
622                         struct ldb_dn *ref_dn
623                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
624                                                           mem_ctx, res->msgs[i], 
625                                                           "serverReference");
626
627                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
628                                 return WERR_NOMEM;
629                         }
630
631                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
632                                                  LDB_SCOPE_BASE, attrs_account_1, "objectClass=computer");
633                         if (ret == LDB_SUCCESS && res_account->count == 1) {
634                                 const char *errstr;
635                                 ctr1->array[i].dns_name
636                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
637                                 ctr1->array[i].netbios_name
638                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
639                                 ctr1->array[i].computer_dn
640                                         = ldb_dn_get_linearized(res_account->msgs[0]->dn);
641
642                                 /* Determine if this is the PDC */
643                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
644                                                                      mem_ctx, res_account->msgs[0]->dn,
645                                                                      &domain_dn, &errstr);
646                                 
647                                 if (ret == LDB_SUCCESS) {
648                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
649                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
650                                                                  ldb_dn_get_linearized(ntds_dn));
651                                         if (ret) {
652                                                 return WERR_GENERAL_FAILURE;
653                                         }
654                                         if (res_domain->count == 1) {
655                                                 ctr1->array[i].is_pdc = true;
656                                         }
657                                 }
658                         }
659                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
660                                 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n", 
661                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
662                         }
663
664                         /* Look at server DN and extract site component */
665                         ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
666                         ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
667
668
669                         ctr1->array[i].is_enabled = true;
670
671                 }
672                 break;
673         case 2:
674                 ctr2 = &r->out.ctr->ctr2;
675                 ctr2->count = res->count;
676                 ctr2->array = talloc_zero_array(mem_ctx, 
677                                                  struct drsuapi_DsGetDCInfo2, 
678                                                  res->count);
679                 for (i=0; i < res->count; i++) {
680                         struct ldb_dn *domain_dn;
681                         struct ldb_result *res_domain;
682                         struct ldb_result *res_account;
683                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
684                         struct ldb_result *res_ntds;
685                         struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
686                         struct ldb_result *res_site;
687                         struct ldb_dn *ref_dn
688                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
689                                                           mem_ctx, res->msgs[i], 
690                                                           "serverReference");
691
692                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
693                                 return WERR_NOMEM;
694                         }
695
696                         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
697                         if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
698                                 return WERR_NOMEM;
699                         }
700
701                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
702                                                  LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
703                         if (ret == LDB_SUCCESS && res_ntds->count == 1) {
704                                 ctr2->array[i].is_gc
705                                         = (ldb_msg_find_attr_as_uint(res_ntds->msgs[0], "options", 0) & DS_NTDSDSA_OPT_IS_GC);
706                                 ctr2->array[i].ntds_guid 
707                                         = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
708                                 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
709                         }
710                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
711                                 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n", 
712                                           ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
713                         }
714
715                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
716                                                  LDB_SCOPE_BASE, attrs_site, "objectClass=site");
717                         if (ret == LDB_SUCCESS && res_site->count == 1) {
718                                 ctr2->array[i].site_guid 
719                                         = samdb_result_guid(res_site->msgs[0], "objectGUID");
720                                 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
721                         }
722                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
723                                 DEBUG(5, ("warning: searching for site DN %s failed: %s\n", 
724                                           ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
725                         }
726
727                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
728                                                  LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
729                         if (ret == LDB_SUCCESS && res_account->count == 1) {
730                                 const char *errstr;
731                                 ctr2->array[i].dns_name
732                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
733                                 ctr2->array[i].netbios_name
734                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
735                                 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
736                                 ctr2->array[i].computer_guid 
737                                         = samdb_result_guid(res_account->msgs[0], "objectGUID");
738
739                                 /* Determine if this is the PDC */
740                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
741                                                                      mem_ctx, res_account->msgs[0]->dn,
742                                                                      &domain_dn, &errstr);
743                                 
744                                 if (ret == LDB_SUCCESS) {
745                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
746                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
747                                                                  ldb_dn_get_linearized(ntds_dn));
748                                         if (ret == LDB_SUCCESS && res_domain->count == 1) {
749                                                 ctr2->array[i].is_pdc = true;
750                                         }
751                                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
752                                                 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n", 
753                                                           ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
754                                         }
755                                 }
756                         }
757                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
758                                 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n", 
759                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
760                         }
761
762                         /* Look at server DN and extract site component */
763                         ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
764                         ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
765                         ctr2->array[i].server_guid 
766                                 = samdb_result_guid(res->msgs[i], "objectGUID");
767
768                         ctr2->array[i].is_enabled = true;
769
770                 }
771                 break;
772         }
773         return WERR_OK;
774 }
775
776 /* 
777   drsuapi_DsGetDomainControllerInfo 
778 */
779 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
780                                                 struct drsuapi_DsGetDomainControllerInfo *r)
781 {
782         struct dcesrv_handle *h;
783         struct drsuapi_bind_state *b_state;     
784         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
785         b_state = h->data;
786
787         switch (r->in.level) {
788         case 1:
789                 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
790         }
791
792         return WERR_UNKNOWN_LEVEL;
793 }
794
795
796
797 /* 
798   drsuapi_DsExecuteKCC 
799 */
800 static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
801                                   struct drsuapi_DsExecuteKCC *r)
802 {
803         WERROR status;
804         status = drs_security_level_check(dce_call, "DsExecuteKCC", SECURITY_DOMAIN_CONTROLLER, NULL);
805
806         if (!W_ERROR_IS_OK(status)) {
807                 return status;
808         }
809
810         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSEXECUTEKCC,
811                                      &ndr_table_drsuapi, "kccsrv", "DsExecuteKCC",
812                                      IRPC_CALL_TIMEOUT);
813         return WERR_OK;
814 }
815
816
817 /* 
818   drsuapi_DsReplicaGetInfo 
819 */
820 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
821                        struct drsuapi_DsReplicaGetInfo *r)
822 {
823         enum security_user_level level;
824
825         if (!lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
826                          "drs", "disable_sec_check", false)) {
827                 level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
828                 if (level < SECURITY_DOMAIN_CONTROLLER) {
829                         DEBUG(1,(__location__ ": Administrator access required for DsReplicaGetInfo\n"));
830                         security_token_debug(0, 2, dce_call->conn->auth_state.session_info->security_token);
831                         return WERR_DS_DRA_ACCESS_DENIED;
832                 }
833         }
834
835         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICAGETINFO,
836                                      &ndr_table_drsuapi, "kccsrv", "DsReplicaGetInfo",
837                                      IRPC_CALL_TIMEOUT);
838
839         return WERR_OK;
840 }
841
842
843 /* 
844   DRSUAPI_ADD_SID_HISTORY 
845 */
846 static WERROR dcesrv_DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
847                        struct DRSUAPI_ADD_SID_HISTORY *r)
848 {
849         DRSUAPI_UNSUPPORTED(DRSUAPI_ADD_SID_HISTORY);
850 }
851
852 /* 
853   drsuapi_DsGetMemberships2 
854 */
855 static WERROR dcesrv_drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
856                        struct drsuapi_DsGetMemberships2 *r)
857 {
858         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships2);
859 }
860
861 /* 
862   DRSUAPI_REPLICA_VERIFY_OBJECTS 
863 */
864 static WERROR dcesrv_DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
865                        struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
866 {
867         DRSUAPI_UNSUPPORTED(DRSUAPI_REPLICA_VERIFY_OBJECTS);
868 }
869
870
871 /* 
872   DRSUAPI_GET_OBJECT_EXISTENCE 
873 */
874 static WERROR dcesrv_DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
875                        struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
876 {
877         DRSUAPI_UNSUPPORTED(DRSUAPI_GET_OBJECT_EXISTENCE);
878 }
879
880
881 /* 
882   drsuapi_QuerySitesByCost 
883 */
884 static WERROR dcesrv_drsuapi_QuerySitesByCost(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
885                        struct drsuapi_QuerySitesByCost *r)
886 {
887         DRSUAPI_UNSUPPORTED(drsuapi_QuerySitesByCost);
888 }
889
890
891 /* include the generated boilerplate */
892 #include "librpc/gen_ndr/ndr_drsuapi_s.c"