r26539: Remove unnecessary statics.
[ira/wip.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
35 static WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s);
36
37 WERROR dreplsrv_load_partitions(struct dreplsrv_service *s)
38 {
39         WERROR status;
40         struct ldb_dn *basedn;
41         struct ldb_result *r;
42         struct ldb_message_element *el;
43         const char *attrs[] = { "namingContexts", NULL };
44         uint32_t i;
45         int ret;
46
47         basedn = ldb_dn_new(s, s->samdb, NULL);
48         W_ERROR_HAVE_NO_MEMORY(basedn);
49
50         ret = ldb_search(s->samdb, basedn, LDB_SCOPE_BASE, 
51                          "(objectClass=*)", attrs, &r);
52         talloc_free(basedn);
53         if (ret != LDB_SUCCESS) {
54                 return WERR_FOOBAR;
55         } else if (r->count != 1) {
56                 talloc_free(r);
57                 return WERR_FOOBAR;
58         }
59         talloc_steal(s, r);
60
61         el = ldb_msg_find_element(r->msgs[0], "namingContexts");
62         if (!el) {
63                 return WERR_FOOBAR;
64         }
65
66         for (i=0; el && i < el->num_values; i++) {
67                 const char *v = (const char *)el->values[i].data;
68                 struct ldb_dn *pdn;
69                 struct dreplsrv_partition *p;
70
71                 pdn = ldb_dn_new(s, s->samdb, v);
72                 if (!ldb_dn_validate(pdn)) {
73                         return WERR_FOOBAR;
74                 }
75
76                 p = talloc_zero(s, struct dreplsrv_partition);
77                 W_ERROR_HAVE_NO_MEMORY(p);
78
79                 p->dn = talloc_steal(p, pdn);
80
81                 DLIST_ADD(s->partitions, p);
82
83                 DEBUG(2, ("dreplsrv_partition[%s] loaded\n", v));
84         }
85
86         talloc_free(r);
87
88         status = dreplsrv_refresh_partitions(s);
89         W_ERROR_NOT_OK_RETURN(status);
90
91         return WERR_OK;
92 }
93
94 static WERROR dreplsrv_out_connection_attach(struct dreplsrv_service *s,
95                                              const struct repsFromTo1 *rft,
96                                              struct dreplsrv_out_connection **_conn)
97 {
98         struct dreplsrv_out_connection *cur, *conn = NULL;
99         const char *hostname;
100
101         if (!rft->other_info) {
102                 return WERR_FOOBAR;
103         }
104
105         if (!rft->other_info->dns_name) {
106                 return WERR_FOOBAR;
107         }
108
109         hostname = rft->other_info->dns_name;
110
111         for (cur = s->connections; cur; cur = cur->next) {              
112                 if (strcmp(cur->binding->host, hostname) == 0) {
113                         conn = cur;
114                         break;
115                 }
116         }
117
118         if (!conn) {
119                 NTSTATUS nt_status;
120                 char *binding_str;
121
122                 conn = talloc_zero(s, struct dreplsrv_out_connection);
123                 W_ERROR_HAVE_NO_MEMORY(conn);
124
125                 conn->service   = s;
126
127                 binding_str = talloc_asprintf(conn, "ncacn_ip_tcp:%s[krb5,seal]",
128                                               hostname);
129                 W_ERROR_HAVE_NO_MEMORY(binding_str);
130                 nt_status = dcerpc_parse_binding(conn, binding_str, &conn->binding);
131                 talloc_free(binding_str);
132                 if (!NT_STATUS_IS_OK(nt_status)) {
133                         return ntstatus_to_werror(nt_status);
134                 }
135
136                 DLIST_ADD_END(s->connections, conn, struct dreplsrv_out_connection *);
137
138                 DEBUG(2,("dreplsrv_out_connection_attach(%s): create\n", conn->binding->host));
139         } else {
140                 DEBUG(2,("dreplsrv_out_connection_attach(%s): attach\n", conn->binding->host));
141         }
142
143         *_conn = conn;
144         return WERR_OK;
145 }
146
147 static WERROR dreplsrv_partition_add_source_dsa(struct dreplsrv_service *s,
148                                                 struct dreplsrv_partition *p,
149                                                 const struct ldb_val *val)
150 {
151         WERROR status;
152         enum ndr_err_code ndr_err;
153         struct dreplsrv_partition_source_dsa *source;
154
155         source = talloc_zero(p, struct dreplsrv_partition_source_dsa);
156         W_ERROR_HAVE_NO_MEMORY(source);
157
158         ndr_err = ndr_pull_struct_blob(val, source, &source->_repsFromBlob,
159                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
160         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
161                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
162                 return ntstatus_to_werror(nt_status);
163         }
164         /* NDR_PRINT_DEBUG(repsFromToBlob, &source->_repsFromBlob); */
165         if (source->_repsFromBlob.version != 1) {
166                 return WERR_DS_DRA_INTERNAL_ERROR;
167         }
168
169         source->partition       = p;
170         source->repsFrom1       = &source->_repsFromBlob.ctr.ctr1;
171
172         status = dreplsrv_out_connection_attach(s, source->repsFrom1, &source->conn);
173         W_ERROR_NOT_OK_RETURN(status);
174
175         DLIST_ADD_END(p->sources, source, struct dreplsrv_partition_source_dsa *);
176         return WERR_OK;
177 }
178
179 static WERROR dreplsrv_refresh_partition(struct dreplsrv_service *s,
180                                          struct dreplsrv_partition *p,
181                                          TALLOC_CTX *mem_ctx)
182 {
183         WERROR status;
184         const struct ldb_val *ouv_value;
185         struct replUpToDateVectorBlob ouv;
186         struct dom_sid *nc_sid;
187         struct ldb_message_element *orf_el = NULL;
188         struct ldb_result *r;
189         uint32_t i;
190         int ret;
191         const char *attrs[] = {
192                 "objectSid",
193                 "objectGUID",
194                 "replUpToDateVector",
195                 "repsFrom",
196                 NULL
197         };
198
199         DEBUG(2, ("dreplsrv_refresh_partition(%s)\n",
200                 ldb_dn_get_linearized(p->dn)));
201
202         ret = ldb_search(s->samdb, p->dn, LDB_SCOPE_BASE,
203                          "(objectClass=*)", attrs, &r);
204         if (ret != LDB_SUCCESS) {
205                 return WERR_FOOBAR;
206         } else if (r->count != 1) {
207                 talloc_free(r);
208                 return WERR_FOOBAR;
209         }
210         talloc_steal(mem_ctx, r);
211
212         ZERO_STRUCT(p->nc);
213         p->nc.dn        = ldb_dn_alloc_linearized(p, p->dn);
214         W_ERROR_HAVE_NO_MEMORY(p->nc.dn);
215         p->nc.guid      = samdb_result_guid(r->msgs[0], "objectGUID");
216         nc_sid          = samdb_result_dom_sid(p, r->msgs[0], "objectSid");
217         if (nc_sid) {
218                 p->nc.sid       = *nc_sid;
219         }
220
221         ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
222         if (ouv_value) {
223                 enum ndr_err_code ndr_err;
224                 ndr_err = ndr_pull_struct_blob(ouv_value, mem_ctx, &ouv,
225                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
226                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
227                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
228                         return ntstatus_to_werror(nt_status);
229                 }
230                 /* NDR_PRINT_DEBUG(replUpToDateVectorBlob, &ouv); */
231                 if (ouv.version != 2) {
232                         return WERR_DS_DRA_INTERNAL_ERROR;
233                 }
234
235                 p->uptodatevector.count         = ouv.ctr.ctr2.count;
236                 p->uptodatevector.reserved      = ouv.ctr.ctr2.reserved;
237                 p->uptodatevector.cursors       = talloc_steal(p, ouv.ctr.ctr2.cursors);
238         }
239
240         /*
241          * TODO: add our own uptodatevector cursor
242          */
243
244
245         orf_el = ldb_msg_find_element(r->msgs[0], "repsFrom");
246         if (orf_el) {
247                 for (i=0; i < orf_el->num_values; i++) {
248                         status = dreplsrv_partition_add_source_dsa(s, p, &orf_el->values[i]);
249                         W_ERROR_NOT_OK_RETURN(status);  
250                 }
251         }
252
253         talloc_free(r);
254
255         return WERR_OK;
256 }
257
258 static WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s)
259 {
260         WERROR status;
261         struct dreplsrv_partition *p;
262
263         for (p = s->partitions; p; p = p->next) {
264                 status = dreplsrv_refresh_partition(s, p, p);
265                 W_ERROR_NOT_OK_RETURN(status);
266         }
267
268         return WERR_OK;
269 }