r26639: librpc: Pass iconv convenience on from RPC connection to NDR library, so...
[bbaumbach/samba-autobuild/.git] / source4 / dsdb / repl / drepl_partitions.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB replication service
4    
5    Copyright (C) Stefan Metzmacher 2007
6     
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19    
20 */
21
22 #include "includes.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "auth/auth.h"
25 #include "smbd/service.h"
26 #include "lib/events/events.h"
27 #include "lib/messaging/irpc.h"
28 #include "dsdb/repl/drepl_service.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "lib/util/dlinklist.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "librpc/gen_ndr/ndr_drsuapi.h"
33 #include "librpc/gen_ndr/ndr_drsblobs.h"
34 #include "param/param.h"
35
36 static WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s);
37
38 WERROR dreplsrv_load_partitions(struct dreplsrv_service *s)
39 {
40         WERROR status;
41         struct ldb_dn *basedn;
42         struct ldb_result *r;
43         struct ldb_message_element *el;
44         static const char *attrs[] = { "namingContexts", NULL };
45         uint32_t i;
46         int ret;
47
48         basedn = ldb_dn_new(s, s->samdb, NULL);
49         W_ERROR_HAVE_NO_MEMORY(basedn);
50
51         ret = ldb_search(s->samdb, basedn, LDB_SCOPE_BASE, 
52                          "(objectClass=*)", attrs, &r);
53         talloc_free(basedn);
54         if (ret != LDB_SUCCESS) {
55                 return WERR_FOOBAR;
56         } else if (r->count != 1) {
57                 talloc_free(r);
58                 return WERR_FOOBAR;
59         }
60         talloc_steal(s, r);
61
62         el = ldb_msg_find_element(r->msgs[0], "namingContexts");
63         if (!el) {
64                 return WERR_FOOBAR;
65         }
66
67         for (i=0; el && i < el->num_values; i++) {
68                 const char *v = (const char *)el->values[i].data;
69                 struct ldb_dn *pdn;
70                 struct dreplsrv_partition *p;
71
72                 pdn = ldb_dn_new(s, s->samdb, v);
73                 if (!ldb_dn_validate(pdn)) {
74                         return WERR_FOOBAR;
75                 }
76
77                 p = talloc_zero(s, struct dreplsrv_partition);
78                 W_ERROR_HAVE_NO_MEMORY(p);
79
80                 p->dn = talloc_steal(p, pdn);
81
82                 DLIST_ADD(s->partitions, p);
83
84                 DEBUG(2, ("dreplsrv_partition[%s] loaded\n", v));
85         }
86
87         talloc_free(r);
88
89         status = dreplsrv_refresh_partitions(s);
90         W_ERROR_NOT_OK_RETURN(status);
91
92         return WERR_OK;
93 }
94
95 static WERROR dreplsrv_out_connection_attach(struct dreplsrv_service *s,
96                                              const struct repsFromTo1 *rft,
97                                              struct dreplsrv_out_connection **_conn)
98 {
99         struct dreplsrv_out_connection *cur, *conn = NULL;
100         const char *hostname;
101
102         if (!rft->other_info) {
103                 return WERR_FOOBAR;
104         }
105
106         if (!rft->other_info->dns_name) {
107                 return WERR_FOOBAR;
108         }
109
110         hostname = rft->other_info->dns_name;
111
112         for (cur = s->connections; cur; cur = cur->next) {              
113                 if (strcmp(cur->binding->host, hostname) == 0) {
114                         conn = cur;
115                         break;
116                 }
117         }
118
119         if (!conn) {
120                 NTSTATUS nt_status;
121                 char *binding_str;
122
123                 conn = talloc_zero(s, struct dreplsrv_out_connection);
124                 W_ERROR_HAVE_NO_MEMORY(conn);
125
126                 conn->service   = s;
127
128                 binding_str = talloc_asprintf(conn, "ncacn_ip_tcp:%s[krb5,seal]",
129                                               hostname);
130                 W_ERROR_HAVE_NO_MEMORY(binding_str);
131                 nt_status = dcerpc_parse_binding(conn, binding_str, &conn->binding);
132                 talloc_free(binding_str);
133                 if (!NT_STATUS_IS_OK(nt_status)) {
134                         return ntstatus_to_werror(nt_status);
135                 }
136
137                 DLIST_ADD_END(s->connections, conn, struct dreplsrv_out_connection *);
138
139                 DEBUG(2,("dreplsrv_out_connection_attach(%s): create\n", conn->binding->host));
140         } else {
141                 DEBUG(2,("dreplsrv_out_connection_attach(%s): attach\n", conn->binding->host));
142         }
143
144         *_conn = conn;
145         return WERR_OK;
146 }
147
148 static WERROR dreplsrv_partition_add_source_dsa(struct dreplsrv_service *s,
149                                                 struct dreplsrv_partition *p,
150                                                 const struct ldb_val *val)
151 {
152         WERROR status;
153         enum ndr_err_code ndr_err;
154         struct dreplsrv_partition_source_dsa *source;
155
156         source = talloc_zero(p, struct dreplsrv_partition_source_dsa);
157         W_ERROR_HAVE_NO_MEMORY(source);
158
159         ndr_err = ndr_pull_struct_blob(val, source, 
160                                        lp_iconv_convenience(global_loadparm), &source->_repsFromBlob,
161                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
162         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
163                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
164                 return ntstatus_to_werror(nt_status);
165         }
166         /* NDR_PRINT_DEBUG(repsFromToBlob, &source->_repsFromBlob); */
167         if (source->_repsFromBlob.version != 1) {
168                 return WERR_DS_DRA_INTERNAL_ERROR;
169         }
170
171         source->partition       = p;
172         source->repsFrom1       = &source->_repsFromBlob.ctr.ctr1;
173
174         status = dreplsrv_out_connection_attach(s, source->repsFrom1, &source->conn);
175         W_ERROR_NOT_OK_RETURN(status);
176
177         DLIST_ADD_END(p->sources, source, struct dreplsrv_partition_source_dsa *);
178         return WERR_OK;
179 }
180
181 static WERROR dreplsrv_refresh_partition(struct dreplsrv_service *s,
182                                          struct dreplsrv_partition *p,
183                                          TALLOC_CTX *mem_ctx)
184 {
185         WERROR status;
186         const struct ldb_val *ouv_value;
187         struct replUpToDateVectorBlob ouv;
188         struct dom_sid *nc_sid;
189         struct ldb_message_element *orf_el = NULL;
190         struct ldb_result *r;
191         uint32_t i;
192         int ret;
193         static const char *attrs[] = {
194                 "objectSid",
195                 "objectGUID",
196                 "replUpToDateVector",
197                 "repsFrom",
198                 NULL
199         };
200
201         DEBUG(2, ("dreplsrv_refresh_partition(%s)\n",
202                 ldb_dn_get_linearized(p->dn)));
203
204         ret = ldb_search(s->samdb, p->dn, LDB_SCOPE_BASE,
205                          "(objectClass=*)", attrs, &r);
206         if (ret != LDB_SUCCESS) {
207                 return WERR_FOOBAR;
208         } else if (r->count != 1) {
209                 talloc_free(r);
210                 return WERR_FOOBAR;
211         }
212         talloc_steal(mem_ctx, r);
213
214         ZERO_STRUCT(p->nc);
215         p->nc.dn        = ldb_dn_alloc_linearized(p, p->dn);
216         W_ERROR_HAVE_NO_MEMORY(p->nc.dn);
217         p->nc.guid      = samdb_result_guid(r->msgs[0], "objectGUID");
218         nc_sid          = samdb_result_dom_sid(p, r->msgs[0], "objectSid");
219         if (nc_sid) {
220                 p->nc.sid       = *nc_sid;
221         }
222
223         ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
224         if (ouv_value) {
225                 enum ndr_err_code ndr_err;
226                 ndr_err = ndr_pull_struct_blob(ouv_value, mem_ctx, 
227                                                lp_iconv_convenience(global_loadparm), &ouv,
228                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
229                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
230                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
231                         return ntstatus_to_werror(nt_status);
232                 }
233                 /* NDR_PRINT_DEBUG(replUpToDateVectorBlob, &ouv); */
234                 if (ouv.version != 2) {
235                         return WERR_DS_DRA_INTERNAL_ERROR;
236                 }
237
238                 p->uptodatevector.count         = ouv.ctr.ctr2.count;
239                 p->uptodatevector.reserved      = ouv.ctr.ctr2.reserved;
240                 p->uptodatevector.cursors       = talloc_steal(p, ouv.ctr.ctr2.cursors);
241         }
242
243         /*
244          * TODO: add our own uptodatevector cursor
245          */
246
247
248         orf_el = ldb_msg_find_element(r->msgs[0], "repsFrom");
249         if (orf_el) {
250                 for (i=0; i < orf_el->num_values; i++) {
251                         status = dreplsrv_partition_add_source_dsa(s, p, &orf_el->values[i]);
252                         W_ERROR_NOT_OK_RETURN(status);  
253                 }
254         }
255
256         talloc_free(r);
257
258         return WERR_OK;
259 }
260
261 static WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s)
262 {
263         WERROR status;
264         struct dreplsrv_partition *p;
265
266         for (p = s->partitions; p; p = p->next) {
267                 status = dreplsrv_refresh_partition(s, p, p);
268                 W_ERROR_NOT_OK_RETURN(status);
269         }
270
271         return WERR_OK;
272 }