s4-drs: open samdb with system credentials when authorised
[ira/wip.git] / source4 / rpc_server / drsuapi / dcesrv_drsuapi.c
index a5418a1a938db9cc27b04c4676a90947959fa412..f11cc232f084127667b173bbb0b38f318630e613 100644 (file)
 #include "rpc_server/dcerpc_server.h"
 #include "rpc_server/common/common.h"
 #include "dsdb/samdb/samdb.h"
-#include "lib/ldb/include/ldb_errors.h"
-#include "param/param.h"
-#include "librpc/gen_ndr/ndr_drsblobs.h"
-#include "messaging/irpc.h"
 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
+#include "libcli/security/security.h"
+#include "auth/auth.h"
 
 /* 
   drsuapi_DsBind 
@@ -50,6 +48,8 @@ static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_C
        uint32_t pid;
        uint32_t repl_epoch;
        int ret;
+       struct auth_session_info *auth_info;
+       WERROR werr;
 
        r->out.bind_info = NULL;
        ZERO_STRUCTP(r->out.bind_handle);
@@ -57,10 +57,20 @@ static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_C
        b_state = talloc_zero(mem_ctx, struct drsuapi_bind_state);
        W_ERROR_HAVE_NO_MEMORY(b_state);
 
+       /* if this is a DC connecting, give them system level access */
+       werr = drs_security_level_check(dce_call, NULL);
+       if (W_ERROR_IS_OK(werr)) {
+               DEBUG(0,(__location__ ": doing DsBind with system_session\n"));
+               auth_info = system_session(b_state, dce_call->conn->dce_ctx->lp_ctx);
+       } else {
+               auth_info = dce_call->conn->auth_state.session_info;
+       }
+
        /*
         * connect to the samdb
         */
-       b_state->sam_ctx = samdb_connect(b_state, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info); 
+       b_state->sam_ctx = samdb_connect(b_state, dce_call->event_ctx, 
+                                        dce_call->conn->dce_ctx->lp_ctx, auth_info); 
        if (!b_state->sam_ctx) {
                return WERR_FOOBAR;
        }
@@ -231,33 +241,21 @@ static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC
 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                                           struct drsuapi_DsReplicaSync *r)
 {
-       struct server_id *repld;
-       struct irpc_request *ireq;
-
-       if (DEBUGLVL(4)) {
-               NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r);
-       }
-
-       repld = irpc_servers_byname(dce_call->msg_ctx, mem_ctx, "dreplsrv");
-       if (repld == NULL || repld[0].id == 0) {
-               DEBUG(0,("DsReplicaSync: Unable to find dreplsrv task\n"));
-               return WERR_DS_DRA_INTERNAL_ERROR;
-       }
+       WERROR status;
 
-       ireq = IRPC_CALL_SEND(dce_call->msg_ctx, repld[0],
-                             drsuapi, DRSUAPI_DSREPLICASYNC,
-                             r, mem_ctx);
-       if (ireq == NULL) {
-               DEBUG(0,("DsReplicaSync: Failed to forward request to dreplsrv task\n"));
-               return WERR_DS_DRA_INTERNAL_ERROR;
+       status = drs_security_level_check(dce_call, "DsReplicaSync");
+       if (!W_ERROR_IS_OK(status)) {
+               return status;
        }
 
-       /* we are not interested in a reply */
-       talloc_free(ireq);
+       dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICASYNC,
+                                    &ndr_table_drsuapi,
+                                    "dreplsrv", "DsReplicaSync");
 
        return WERR_OK;
 }
 
+
 /* 
   drsuapi_DsReplicaAdd 
 */
@@ -470,10 +468,16 @@ static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call
        struct ldb_dn *ntds_dn;
        int ret;
        bool ok;
+       WERROR status;
 
        ZERO_STRUCT(r->out.res);
        *r->out.level_out = 1;
 
+       status = drs_security_level_check(dce_call, "DsRemoveDSServer");
+       if (!W_ERROR_IS_OK(status)) {
+               return status;
+       }
+
        DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
        b_state = h->data;
 
@@ -787,10 +791,10 @@ static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state
 
 
 /* 
-  DRSUAPI_EXECUTE_KCC 
+  drsuapi_DsExecuteKCC 
 */
-static WERROR dcesrv_DRSUAPI_EXECUTE_KCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct DRSUAPI_EXECUTE_KCC *r)
+static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
+                                 struct drsuapi_DsExecuteKCC *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
 }