2 Unix SMB/CIFS mplementation.
3 DSDB replication service
5 Copyright (C) Stefan Metzmacher 2007
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.
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.
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/>.
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"
36 static WERROR
dreplsrv_refresh_partitions(struct dreplsrv_service
*s
);
38 WERROR
dreplsrv_load_partitions(struct dreplsrv_service
*s
)
41 struct ldb_dn
*basedn
;
43 struct ldb_message_element
*el
;
44 static const char *attrs
[] = { "namingContexts", NULL
};
48 basedn
= ldb_dn_new(s
, s
->samdb
, NULL
);
49 W_ERROR_HAVE_NO_MEMORY(basedn
);
51 ret
= ldb_search(s
->samdb
, s
, &r
, basedn
, LDB_SCOPE_BASE
, attrs
,
54 if (ret
!= LDB_SUCCESS
) {
56 } else if (r
->count
!= 1) {
61 el
= ldb_msg_find_element(r
->msgs
[0], "namingContexts");
66 for (i
=0; el
&& i
< el
->num_values
; i
++) {
67 const char *v
= (const char *)el
->values
[i
].data
;
69 struct dreplsrv_partition
*p
;
71 pdn
= ldb_dn_new(s
, s
->samdb
, v
);
72 if (!ldb_dn_validate(pdn
)) {
76 p
= talloc_zero(s
, struct dreplsrv_partition
);
77 W_ERROR_HAVE_NO_MEMORY(p
);
79 p
->dn
= talloc_steal(p
, pdn
);
81 DLIST_ADD(s
->partitions
, p
);
83 DEBUG(2, ("dreplsrv_partition[%s] loaded\n", v
));
88 status
= dreplsrv_refresh_partitions(s
);
89 W_ERROR_NOT_OK_RETURN(status
);
94 static WERROR
dreplsrv_out_connection_attach(struct dreplsrv_service
*s
,
95 const struct repsFromTo1
*rft
,
96 struct dreplsrv_out_connection
**_conn
)
98 struct dreplsrv_out_connection
*cur
, *conn
= NULL
;
101 if (!rft
->other_info
) {
105 if (!rft
->other_info
->dns_name
) {
109 hostname
= rft
->other_info
->dns_name
;
111 for (cur
= s
->connections
; cur
; cur
= cur
->next
) {
112 if (strcmp(cur
->binding
->host
, hostname
) == 0) {
122 conn
= talloc_zero(s
, struct dreplsrv_out_connection
);
123 W_ERROR_HAVE_NO_MEMORY(conn
);
127 binding_str
= talloc_asprintf(conn
, "ncacn_ip_tcp:%s[krb5,seal]",
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
);
136 DLIST_ADD_END(s
->connections
, conn
, struct dreplsrv_out_connection
*);
138 DEBUG(2,("dreplsrv_out_connection_attach(%s): create\n", conn
->binding
->host
));
140 DEBUG(2,("dreplsrv_out_connection_attach(%s): attach\n", conn
->binding
->host
));
147 static WERROR
dreplsrv_partition_add_source_dsa(struct dreplsrv_service
*s
,
148 struct dreplsrv_partition
*p
,
149 const struct ldb_val
*val
)
152 enum ndr_err_code ndr_err
;
153 struct dreplsrv_partition_source_dsa
*source
;
155 source
= talloc_zero(p
, struct dreplsrv_partition_source_dsa
);
156 W_ERROR_HAVE_NO_MEMORY(source
);
158 ndr_err
= ndr_pull_struct_blob(val
, source
,
159 lp_iconv_convenience(s
->task
->lp_ctx
), &source
->_repsFromBlob
,
160 (ndr_pull_flags_fn_t
)ndr_pull_repsFromToBlob
);
161 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
162 NTSTATUS nt_status
= ndr_map_error2ntstatus(ndr_err
);
163 return ntstatus_to_werror(nt_status
);
165 /* NDR_PRINT_DEBUG(repsFromToBlob, &source->_repsFromBlob); */
166 if (source
->_repsFromBlob
.version
!= 1) {
167 return WERR_DS_DRA_INTERNAL_ERROR
;
170 source
->partition
= p
;
171 source
->repsFrom1
= &source
->_repsFromBlob
.ctr
.ctr1
;
173 status
= dreplsrv_out_connection_attach(s
, source
->repsFrom1
, &source
->conn
);
174 W_ERROR_NOT_OK_RETURN(status
);
176 DLIST_ADD_END(p
->sources
, source
, struct dreplsrv_partition_source_dsa
*);
180 static WERROR
dreplsrv_refresh_partition(struct dreplsrv_service
*s
,
181 struct dreplsrv_partition
*p
,
185 const struct ldb_val
*ouv_value
;
186 struct replUpToDateVectorBlob ouv
;
187 struct dom_sid
*nc_sid
;
188 struct ldb_message_element
*orf_el
= NULL
;
189 struct ldb_result
*r
;
192 static const char *attrs
[] = {
195 "replUpToDateVector",
200 DEBUG(2, ("dreplsrv_refresh_partition(%s)\n",
201 ldb_dn_get_linearized(p
->dn
)));
203 ret
= ldb_search(s
->samdb
, mem_ctx
, &r
, p
->dn
, LDB_SCOPE_BASE
, attrs
,
205 if (ret
!= LDB_SUCCESS
) {
207 } else if (r
->count
!= 1) {
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");
221 ouv_value
= ldb_msg_find_ldb_val(r
->msgs
[0], "replUpToDateVector");
223 enum ndr_err_code ndr_err
;
224 ndr_err
= ndr_pull_struct_blob(ouv_value
, mem_ctx
,
225 lp_iconv_convenience(s
->task
->lp_ctx
), &ouv
,
226 (ndr_pull_flags_fn_t
)ndr_pull_replUpToDateVectorBlob
);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
228 NTSTATUS nt_status
= ndr_map_error2ntstatus(ndr_err
);
229 return ntstatus_to_werror(nt_status
);
231 /* NDR_PRINT_DEBUG(replUpToDateVectorBlob, &ouv); */
232 if (ouv
.version
!= 2) {
233 return WERR_DS_DRA_INTERNAL_ERROR
;
236 p
->uptodatevector
.count
= ouv
.ctr
.ctr2
.count
;
237 p
->uptodatevector
.reserved
= ouv
.ctr
.ctr2
.reserved
;
238 p
->uptodatevector
.cursors
= talloc_steal(p
, ouv
.ctr
.ctr2
.cursors
);
242 * TODO: add our own uptodatevector cursor
246 orf_el
= ldb_msg_find_element(r
->msgs
[0], "repsFrom");
248 for (i
=0; i
< orf_el
->num_values
; i
++) {
249 status
= dreplsrv_partition_add_source_dsa(s
, p
, &orf_el
->values
[i
]);
250 W_ERROR_NOT_OK_RETURN(status
);
259 static WERROR
dreplsrv_refresh_partitions(struct dreplsrv_service
*s
)
262 struct dreplsrv_partition
*p
;
264 for (p
= s
->partitions
; p
; p
= p
->next
) {
265 status
= dreplsrv_refresh_partition(s
, p
, p
);
266 W_ERROR_NOT_OK_RETURN(status
);