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