summaryrefslogtreecommitdiff
path: root/source4/dsdb/repl/drepl_out_pull.c
blob: c62a605dcc0f2e8df9737057460c9f1b93199316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* 
   Unix SMB/CIFS mplementation.
   DSDB replication service outgoing Pull-Replication
   
   Copyright (C) Stefan Metzmacher 2007
    
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
*/

#include "includes.h"
#include "dsdb/samdb/samdb.h"
#include "auth/auth.h"
#include "smbd/service.h"
#include "lib/events/events.h"
#include "dsdb/repl/drepl_service.h"
#include "lib/ldb/include/ldb_errors.h"
#include "../lib/util/dlinklist.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "librpc/gen_ndr/ndr_drsuapi.h"
#include "librpc/gen_ndr/ndr_drsblobs.h"
#include "libcli/composite/composite.h"
#include "libcli/security/security.h"

/*
  update repsFrom/repsTo error information
 */
void drepl_reps_update(struct dreplsrv_service *s, const char *reps_attr,
		       struct ldb_dn *dn,
		       struct GUID *source_dsa_obj_guid, WERROR status)
{
	struct repsFromToBlob *reps;
	uint32_t count, i;
	WERROR werr;
	TALLOC_CTX *tmp_ctx = talloc_new(s);
	time_t t;
	NTTIME now;

	t = time(NULL);
	unix_to_nt_time(&now, t);

	werr = dsdb_loadreps(s->samdb, tmp_ctx, dn, reps_attr, &reps, &count);
	if (!W_ERROR_IS_OK(werr)) {
		talloc_free(tmp_ctx);
		return;
	}

	for (i=0; i<count; i++) {
		if (GUID_compare(source_dsa_obj_guid,
				 &reps[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
			break;
		}
	}

	if (i == count) {
		/* no record to update */
		talloc_free(tmp_ctx);
		return;
	}

	/* only update the status fields */
	reps[i].ctr.ctr1.last_attempt = now;
	reps[i].ctr.ctr1.result_last_attempt = status;
	if (W_ERROR_IS_OK(status)) {
		reps[i].ctr.ctr1.last_success = now;
		reps[i].ctr.ctr1.consecutive_sync_failures = 0;
	} else {
		reps[i].ctr.ctr1.consecutive_sync_failures++;
	}

	werr = dsdb_savereps(s->samdb, tmp_ctx, dn, reps_attr, reps, count);
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(2,("drepl_reps_update: Failed to save %s for %s: %s\n",
			 reps_attr, ldb_dn_get_linearized(dn), win_errstr(werr)));
	}
	talloc_free(tmp_ctx);
}

WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
					       struct dreplsrv_partition_source_dsa *source,
					       enum drsuapi_DsExtendedOperation extended_op,
					       uint64_t fsmo_info,
					       dreplsrv_extended_callback_t callback,
					       void *cb_data)
{
	struct dreplsrv_out_operation *op;

	op = talloc_zero(s, struct dreplsrv_out_operation);
	W_ERROR_HAVE_NO_MEMORY(op);

	op->service	= s;
	op->source_dsa	= source;
	op->extended_op = extended_op;
	op->fsmo_info   = fsmo_info;
	op->callback    = callback;
	op->cb_data	= cb_data;
	op->schedule_time = time(NULL);

	DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);

	return WERR_OK;
}

static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
					       struct dreplsrv_partition *p,
					       TALLOC_CTX *mem_ctx)
{
	WERROR status;
	struct dreplsrv_partition_source_dsa *cur;

	for (cur = p->sources; cur; cur = cur->next) {
		status = dreplsrv_schedule_partition_pull_source(s, cur,
		                                                 DRSUAPI_EXOP_NONE, 0,
		                                                 NULL, NULL);
		W_ERROR_NOT_OK_RETURN(status);
	}

	return WERR_OK;
}

WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
{
	WERROR status;
	struct dreplsrv_partition *p;

	for (p = s->partitions; p; p = p->next) {
		status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
		W_ERROR_NOT_OK_RETURN(status);
	}

	return WERR_OK;
}


static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
{
	struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
					    struct dreplsrv_out_operation);
	struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
	struct dreplsrv_service *s = op->service;
	WERROR werr;

	werr = dreplsrv_op_pull_source_recv(subreq);
	TALLOC_FREE(subreq);

	DEBUG(4,("dreplsrv_op_pull_source(%s) for %s\n", win_errstr(werr),
		 ldb_dn_get_linearized(op->source_dsa->partition->dn)));

	if (op->extended_op == DRSUAPI_EXOP_NONE) {
		drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
				  &rf->source_dsa_obj_guid, werr);
	}

	if (op->callback) {
		op->callback(s, werr, op->extended_ret, op->cb_data);
	}
	talloc_free(op);
	s->ops.current = NULL;
	dreplsrv_run_pending_ops(s);
}

void dreplsrv_run_pull_ops(struct dreplsrv_service *s)
{
	struct dreplsrv_out_operation *op;
	time_t t;
	NTTIME now;
	struct tevent_req *subreq;

	if (s->ops.current) {
		/* if there's still one running, we're done */
		return;
	}

	if (!s->ops.pending) {
		/* if there're no pending operations, we're done */
		return;
	}

	t = time(NULL);
	unix_to_nt_time(&now, t);

	op = s->ops.pending;
	s->ops.current = op;
	DLIST_REMOVE(s->ops.pending, op);

	op->source_dsa->repsFrom1->last_attempt = now;

	subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
	if (!subreq) {
		struct repsFromTo1 *rf = op->source_dsa->repsFrom1;

		if (op->extended_op == DRSUAPI_EXOP_NONE) {
			drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
					  &rf->source_dsa_obj_guid, WERR_NOMEM);
		}
		s->ops.current = NULL;

		/*
		 * call the callback (if any) so it gets the chance
		 * to do its job just like in any other failure situation
		 */
		if (op->callback) {
			op->callback(s, WERR_NOMEM, op->extended_ret, op->cb_data);
		}
		return;
	}
	tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
}