s4-libcli/security Use seperate subsystem for session related functions
[abartlet/samba.git/.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 = samdb_result_uint(ntds_res->msgs[0], "ms-DS-ReplicationEpoch", 0);
139
140         /*
141          * The "process identifier" of the client.
142          * According to the WSPP docs, sectin 5.35, this is
143          * for informational and debugging purposes only.
144          * The assignment is implementation specific.
145          */
146         pid = 0;
147
148         /*
149          * store the clients bind_guid
150          */
151         if (r->in.bind_guid) {
152                 b_state->remote_bind_guid = *r->in.bind_guid;
153         }
154
155         /*
156          * store the clients bind_info
157          */
158         if (r->in.bind_info) {
159                 switch (r->in.bind_info->length) {
160                 case 24: {
161                         struct drsuapi_DsBindInfo24 *info24;
162                         info24 = &r->in.bind_info->info.info24;
163                         b_state->remote_info28.supported_extensions     = info24->supported_extensions;
164                         b_state->remote_info28.site_guid                = info24->site_guid;
165                         b_state->remote_info28.pid                      = info24->pid;
166                         b_state->remote_info28.repl_epoch               = 0;
167                         break;
168                 }
169                 case 28:
170                         b_state->remote_info28 = r->in.bind_info->info.info28;
171                         break;
172                 }
173         }
174
175         /*
176          * fill in our local bind info 28
177          */
178         b_state->local_info28.supported_extensions      = 0;
179         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
180         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
181         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
182         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
183 #if 0 /* we don't support MSZIP compression (only decompression) */
184         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
185 #endif
186         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
187         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
188         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
189         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
190         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
191         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
192         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
193         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
194         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
195         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
196         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
197         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
198         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
199         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
200         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
201         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
202         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
203         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
204         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
205         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
206         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
207         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
208         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
209         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
210 #if 0 /* we don't support XPRESS compression yet */
211         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
212 #endif
213         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10;
214         b_state->local_info28.site_guid                 = site_guid;
215         b_state->local_info28.pid                       = pid;
216         b_state->local_info28.repl_epoch                = repl_epoch;
217
218         /*
219          * allocate the return bind_info
220          */
221         bind_info = talloc(mem_ctx, struct drsuapi_DsBindInfoCtr);
222         W_ERROR_HAVE_NO_MEMORY(bind_info);
223
224         bind_info->length       = 28;
225         bind_info->info.info28  = b_state->local_info28;
226
227         /*
228          * allocate a bind handle
229          */
230         handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
231         W_ERROR_HAVE_NO_MEMORY(handle);
232         handle->data = talloc_steal(handle, b_state);
233
234         /*
235          * prepare reply
236          */
237         r->out.bind_info = bind_info;
238         *r->out.bind_handle = handle->wire_handle;
239
240         return WERR_OK;
241 }
242
243
244 /* 
245   drsuapi_DsUnbind 
246 */
247 static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
248                                struct drsuapi_DsUnbind *r)
249 {
250         struct dcesrv_handle *h;
251
252         *r->out.bind_handle = *r->in.bind_handle;
253
254         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
255
256         talloc_free(h);
257
258         ZERO_STRUCTP(r->out.bind_handle);
259
260         return WERR_OK;
261 }
262
263
264 /* 
265   drsuapi_DsReplicaSync 
266 */
267 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
268                                            struct drsuapi_DsReplicaSync *r)
269 {
270         WERROR status;
271         uint32_t timeout;
272
273         status = drs_security_level_check(dce_call, "DsReplicaSync", SECURITY_DOMAIN_CONTROLLER, NULL);
274         if (!W_ERROR_IS_OK(status)) {
275                 return status;
276         }
277
278         if (r->in.level != 1) {
279                 DEBUG(0,("DsReplicaSync called with unsupported level %d\n", r->in.level));
280                 return WERR_DS_DRA_INVALID_PARAMETER;
281         }
282
283         if (r->in.req->req1.options & DRSUAPI_DRS_ASYNC_OP) {
284                 timeout = IRPC_CALL_TIMEOUT;
285         } else {
286                 /*
287                  * use Infinite time for timeout in case
288                  * the caller made a sync call
289                  */
290                 timeout = IRPC_CALL_TIMEOUT_INF;
291         }
292
293         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
294                                      r, NDR_DRSUAPI_DSREPLICASYNC,
295                                      &ndr_table_drsuapi,
296                                      "dreplsrv", "DsReplicaSync",
297                                      timeout);
298
299         return WERR_OK;
300 }
301
302
303 /* 
304   drsuapi_DsReplicaAdd 
305 */
306 static WERROR dcesrv_drsuapi_DsReplicaAdd(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
307                                           struct drsuapi_DsReplicaAdd *r)
308 {
309         WERROR status;
310
311         status = drs_security_level_check(dce_call, "DsReplicaAdd", SECURITY_DOMAIN_CONTROLLER, NULL);
312         if (!W_ERROR_IS_OK(status)) {
313                 return status;
314         }
315
316         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
317                                      r, NDR_DRSUAPI_DSREPLICAADD,
318                                      &ndr_table_drsuapi,
319                                      "dreplsrv", "DsReplicaAdd",
320                                      IRPC_CALL_TIMEOUT);
321
322         return WERR_OK;
323 }
324
325
326 /* 
327   drsuapi_DsReplicaDel 
328 */
329 static WERROR dcesrv_drsuapi_DsReplicaDel(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
330                                           struct drsuapi_DsReplicaDel *r)
331 {
332         WERROR status;
333
334         status = drs_security_level_check(dce_call, "DsReplicaDel", SECURITY_DOMAIN_CONTROLLER, NULL);
335         if (!W_ERROR_IS_OK(status)) {
336                 return status;
337         }
338
339         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
340                                      r, NDR_DRSUAPI_DSREPLICADEL,
341                                      &ndr_table_drsuapi,
342                                      "dreplsrv", "DsReplicaDel",
343                                      IRPC_CALL_TIMEOUT);
344
345         return WERR_OK;
346 }
347
348
349 /* 
350   drsuapi_DsReplicaModify 
351 */
352 static WERROR dcesrv_drsuapi_DsReplicaMod(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
353                                           struct drsuapi_DsReplicaMod *r)
354 {
355         WERROR status;
356
357         status = drs_security_level_check(dce_call, "DsReplicaMod", SECURITY_DOMAIN_CONTROLLER, NULL);
358         if (!W_ERROR_IS_OK(status)) {
359                 return status;
360         }
361
362         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
363                                      r, NDR_DRSUAPI_DSREPLICAMOD,
364                                      &ndr_table_drsuapi,
365                                      "dreplsrv", "DsReplicaMod",
366                                      IRPC_CALL_TIMEOUT);
367
368         return WERR_OK;
369 }
370
371
372 /* 
373   DRSUAPI_VERIFY_NAMES 
374 */
375 static WERROR dcesrv_DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
376                        struct DRSUAPI_VERIFY_NAMES *r)
377 {
378         DRSUAPI_UNSUPPORTED(DRSUAPI_VERIFY_NAMES);
379 }
380
381
382 /* 
383   drsuapi_DsGetMemberships 
384 */
385 static WERROR dcesrv_drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
386                        struct drsuapi_DsGetMemberships *r)
387 {
388         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships);
389 }
390
391
392 /* 
393   DRSUAPI_INTER_DOMAIN_MOVE 
394 */
395 static WERROR dcesrv_DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
396                        struct DRSUAPI_INTER_DOMAIN_MOVE *r)
397 {
398         DRSUAPI_UNSUPPORTED(DRSUAPI_INTER_DOMAIN_MOVE);
399 }
400
401
402 /* 
403   drsuapi_DsGetNT4ChangeLog 
404 */
405 static WERROR dcesrv_drsuapi_DsGetNT4ChangeLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
406                        struct drsuapi_DsGetNT4ChangeLog *r)
407 {
408         DRSUAPI_UNSUPPORTED(drsuapi_DsGetNT4ChangeLog);
409 }
410
411 /* 
412   drsuapi_DsCrackNames 
413 */
414 static WERROR dcesrv_drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
415                             struct drsuapi_DsCrackNames *r)
416 {
417         struct drsuapi_bind_state *b_state;
418         struct dcesrv_handle *h;
419
420         *r->out.level_out = r->in.level;
421
422         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
423         b_state = h->data;
424
425         r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsNameCtr);
426         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
427
428         switch (r->in.level) {
429                 case 1: {
430                         switch(r->in.req->req1.format_offered){
431                         case DRSUAPI_DS_NAME_FORMAT_UPN_AND_ALTSECID:
432                         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT_NAME_SANS_DOMAIN_EX:
433                         case DRSUAPI_DS_NAME_FORMAT_LIST_GLOBAL_CATALOG_SERVERS:
434                         case DRSUAPI_DS_NAME_FORMAT_UPN_FOR_LOGON:
435                         case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_WITH_DCS_IN_SITE:
436                         case DRSUAPI_DS_NAME_FORMAT_STRING_SID_NAME:
437                         case DRSUAPI_DS_NAME_FORMAT_ALT_SECURITY_IDENTITIES_NAME:
438                         case DRSUAPI_DS_NAME_FORMAT_LIST_NCS:
439                         case DRSUAPI_DS_NAME_FORMAT_LIST_DOMAINS:
440                         case DRSUAPI_DS_NAME_FORMAT_MAP_SCHEMA_GUID:
441                         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT_NAME_SANS_DOMAIN:
442                         case DRSUAPI_DS_NAME_FORMAT_LIST_INFO_FOR_SERVER:
443                         case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_FOR_DOMAIN_IN_SITE:
444                         case DRSUAPI_DS_NAME_FORMAT_LIST_DOMAINS_IN_SITE:
445                         case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_IN_SITE:
446                         case DRSUAPI_DS_NAME_FORMAT_LIST_SITES:
447                                 DEBUG(0, ("DsCrackNames: Unsupported operation requested: %X",
448                                           r->in.req->req1.format_offered));
449                                 return WERR_OK;
450                         case DRSUAPI_DS_NAME_FORMAT_LIST_ROLES:
451                                 return dcesrv_drsuapi_ListRoles(b_state->sam_ctx, mem_ctx,
452                                                                 &r->in.req->req1, &r->out.ctr->ctr1);
453                         default:/* format_offered is in the enum drsuapi_DsNameFormat*/
454                                 return dcesrv_drsuapi_CrackNamesByNameFormat(b_state->sam_ctx, mem_ctx,
455                                                                              &r->in.req->req1, &r->out.ctr->ctr1);
456                         }
457                         return WERR_OK;
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_int(res_ntds->msgs[0], "options", 0) == 1);
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_ADMINISTRATOR) {
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"