s4-drs: lock down key DRS calls
[nivanova/samba-autobuild/.git] / source4 / rpc_server / drsuapi / updaterefs.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DRSUpdateRefs call
5
6    Copyright (C) Andrew Tridgell 2009
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_drsuapi.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/common/common.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "param/param.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "auth/auth.h"
31 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
32 #include "libcli/security/security.h"
33
34 struct repsTo {
35         uint32_t count;
36         struct repsFromToBlob *r;
37 };
38
39 /*
40   add a replication destination for a given partition GUID
41  */
42 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
43                             struct ldb_dn *dn, struct repsFromTo1 *dest)
44 {
45         struct repsTo reps;
46         WERROR werr;
47
48         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
49         if (!W_ERROR_IS_OK(werr)) {
50                 return werr;
51         }
52
53         reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
54         if (reps.r == NULL) {
55                 return WERR_DS_DRA_INTERNAL_ERROR;
56         }
57         ZERO_STRUCT(reps.r[reps.count]);
58         reps.r[reps.count].version = 1;
59         reps.r[reps.count].ctr.ctr1 = *dest;
60         reps.count++;
61
62         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
63         if (!W_ERROR_IS_OK(werr)) {
64                 return werr;
65         }
66
67         return WERR_OK; 
68 }
69
70 /*
71   delete a replication destination for a given partition GUID
72  */
73 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
74                             struct ldb_dn *dn, struct GUID *dest_guid)
75 {
76         struct repsTo reps;
77         WERROR werr;
78         int i;
79
80         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
81         if (!W_ERROR_IS_OK(werr)) {
82                 return werr;
83         }
84
85         for (i=0; i<reps.count; i++) {
86                 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
87                         if (i+1 < reps.count) {
88                                 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
89                         }
90                         reps.count--;
91                 }
92         }
93
94         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
95         if (!W_ERROR_IS_OK(werr)) {
96                 return werr;
97         }
98
99         return WERR_OK; 
100 }
101
102 /* 
103   drsuapi_DsReplicaUpdateRefs
104 */
105 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
106                                           struct drsuapi_DsReplicaUpdateRefs *r)
107 {
108         struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
109         struct ldb_context *sam_ctx;
110         WERROR werr;
111         struct ldb_dn *dn;
112
113         if (security_session_user_level(dce_call->conn->auth_state.session_info) <
114             SECURITY_DOMAIN_CONTROLLER) {
115                 DEBUG(0,("DsReplicaUpdateRefs refused for security token\n"));
116                 return WERR_DS_DRA_ACCESS_DENIED;
117         }
118
119         if (r->in.level != 1) {
120                 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
121                 return WERR_DS_DRA_INVALID_PARAMETER;
122         }
123
124         req = &r->in.req.req1;
125         DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
126                  req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
127                  req->options,
128                  drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
129
130         /* TODO: We need to authenticate this operation pretty carefully */
131         sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, 
132                                 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
133         if (!sam_ctx) {
134                 return WERR_DS_DRA_INTERNAL_ERROR;              
135         }
136
137         dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
138         if (dn == NULL) {
139                 talloc_free(sam_ctx);           
140                 return WERR_DS_INVALID_DN_SYNTAX;
141         }
142
143         if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
144                 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
145                 talloc_free(sam_ctx);
146                 return WERR_DS_DRA_INTERNAL_ERROR;              
147         }
148
149         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
150                 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid);
151                 if (!W_ERROR_IS_OK(werr)) {
152                         DEBUG(0,("Failed to delete repsTo for %s\n",
153                                  GUID_string(dce_call, &req->dest_dsa_guid)));
154                         goto failed;
155                 }
156         }
157
158         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
159                 struct repsFromTo1 dest;
160                 struct repsFromTo1OtherInfo oi;
161                 
162                 ZERO_STRUCT(dest);
163                 ZERO_STRUCT(oi);
164
165                 oi.dns_name = req->dest_dsa_dns_name;
166                 dest.other_info          = &oi;
167                 dest.source_dsa_obj_guid = req->dest_dsa_guid;
168                 dest.replica_flags       = req->options;
169
170                 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest);
171                 if (!W_ERROR_IS_OK(werr)) {
172                         DEBUG(0,("Failed to delete repsTo for %s\n",
173                                  GUID_string(dce_call, &dest.source_dsa_obj_guid)));
174                         goto failed;
175                 }
176         }
177
178         if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
179                 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
180                 return WERR_DS_DRA_INTERNAL_ERROR;              
181         }
182
183         talloc_free(sam_ctx);
184         return WERR_OK;
185
186 failed:
187         ldb_transaction_cancel(sam_ctx);
188         talloc_free(sam_ctx);
189         return werr;
190 }