more include minimisation
[mat/samba.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 "rpc_server/dcerpc_server.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
27 #include "libcli/security/security.h"
28
29 struct repsTo {
30         uint32_t count;
31         struct repsFromToBlob *r;
32 };
33
34 /*
35   add a replication destination for a given partition GUID
36  */
37 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
38                             struct ldb_dn *dn, struct repsFromTo1 *dest)
39 {
40         struct repsTo reps;
41         WERROR werr;
42
43         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
44         if (!W_ERROR_IS_OK(werr)) {
45                 return werr;
46         }
47
48         reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
49         if (reps.r == NULL) {
50                 return WERR_DS_DRA_INTERNAL_ERROR;
51         }
52         ZERO_STRUCT(reps.r[reps.count]);
53         reps.r[reps.count].version = 1;
54         reps.r[reps.count].ctr.ctr1 = *dest;
55         reps.count++;
56
57         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
58         if (!W_ERROR_IS_OK(werr)) {
59                 return werr;
60         }
61
62         return WERR_OK; 
63 }
64
65 /*
66   delete a replication destination for a given partition GUID
67  */
68 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
69                             struct ldb_dn *dn, struct GUID *dest_guid)
70 {
71         struct repsTo reps;
72         WERROR werr;
73         int i;
74
75         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
76         if (!W_ERROR_IS_OK(werr)) {
77                 return werr;
78         }
79
80         for (i=0; i<reps.count; i++) {
81                 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
82                         if (i+1 < reps.count) {
83                                 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
84                         }
85                         reps.count--;
86                 }
87         }
88
89         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
90         if (!W_ERROR_IS_OK(werr)) {
91                 return werr;
92         }
93
94         return WERR_OK; 
95 }
96
97 /* 
98   drsuapi_DsReplicaUpdateRefs
99 */
100 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
101                                           struct drsuapi_DsReplicaUpdateRefs *r)
102 {
103         struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
104         struct ldb_context *sam_ctx;
105         WERROR werr;
106         struct ldb_dn *dn;
107
108         if (security_session_user_level(dce_call->conn->auth_state.session_info) <
109             SECURITY_DOMAIN_CONTROLLER) {
110                 DEBUG(0,("DsReplicaUpdateRefs refused for security token\n"));
111                 return WERR_DS_DRA_ACCESS_DENIED;
112         }
113
114         if (r->in.level != 1) {
115                 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
116                 return WERR_DS_DRA_INVALID_PARAMETER;
117         }
118
119         req = &r->in.req.req1;
120         DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
121                  req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
122                  req->options,
123                  drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
124
125         /* TODO: We need to authenticate this operation pretty carefully */
126         sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, 
127                                 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
128         if (!sam_ctx) {
129                 return WERR_DS_DRA_INTERNAL_ERROR;              
130         }
131
132         dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
133         if (dn == NULL) {
134                 talloc_free(sam_ctx);           
135                 return WERR_DS_INVALID_DN_SYNTAX;
136         }
137
138         if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
139                 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
140                 talloc_free(sam_ctx);
141                 return WERR_DS_DRA_INTERNAL_ERROR;              
142         }
143
144         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
145                 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid);
146                 if (!W_ERROR_IS_OK(werr)) {
147                         DEBUG(0,("Failed to delete repsTo for %s\n",
148                                  GUID_string(dce_call, &req->dest_dsa_guid)));
149                         goto failed;
150                 }
151         }
152
153         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
154                 struct repsFromTo1 dest;
155                 struct repsFromTo1OtherInfo oi;
156                 
157                 ZERO_STRUCT(dest);
158                 ZERO_STRUCT(oi);
159
160                 oi.dns_name = req->dest_dsa_dns_name;
161                 dest.other_info          = &oi;
162                 dest.source_dsa_obj_guid = req->dest_dsa_guid;
163                 dest.replica_flags       = req->options;
164
165                 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest);
166                 if (!W_ERROR_IS_OK(werr)) {
167                         DEBUG(0,("Failed to delete repsTo for %s\n",
168                                  GUID_string(dce_call, &dest.source_dsa_obj_guid)));
169                         goto failed;
170                 }
171         }
172
173         if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
174                 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
175                 return WERR_DS_DRA_INTERNAL_ERROR;              
176         }
177
178         talloc_free(sam_ctx);
179         return WERR_OK;
180
181 failed:
182         ldb_transaction_cancel(sam_ctx);
183         talloc_free(sam_ctx);
184         return werr;
185 }