s4-repl: added min_usn to extended replication call
[anatoliy/anatoliy.git] / source4 / dsdb / repl / drepl_fsmo.c
1 /*
2    Unix SMB/CIFS mplementation.
3
4    DSDB replication service - FSMO role change
5
6    Copyright (C) Nadezhda Ivanova 2010
7    Copyright (C) Andrew Tridgell 2010
8    Copyright (C) Andrew Bartlett 2010
9
10    based on drepl_ridalloc.c
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
25 */
26
27 #include "includes.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "smbd/service.h"
30 #include "dsdb/repl/drepl_service.h"
31 #include "param/param.h"
32 #include "lib/messaging/irpc.h"
33 #include "librpc/gen_ndr/ndr_irpc.h"
34
35 static void drepl_role_callback(struct dreplsrv_service *service,
36                                 WERROR werr,
37                                 enum drsuapi_DsExtendedError ext_err,
38                                 void *cb_data)
39 {
40         if (!W_ERROR_IS_OK(werr)) {
41                 DEBUG(0,(__location__ ": Failed role transfer - %s - extended_ret[0x%X]\n",
42                          win_errstr(werr), ext_err));
43         } else {
44                 DEBUG(0,(__location__ ": Successful role transfer\n"));
45         }
46 }
47
48 static bool fsmo_master_cmp(struct ldb_dn *ntds_dn, struct ldb_dn *role_owner_dn)
49 {
50         if (ldb_dn_compare(ntds_dn, role_owner_dn) == 0) {
51                 DEBUG(0,("\nWe are the FSMO master.\n"));
52                 return true;
53         }
54         return false;
55 }
56
57 /*
58   see which role is we are asked to assume, initialize data and send request
59  */
60 WERROR dreplsrv_fsmo_role_check(struct dreplsrv_service *service,
61                                 uint32_t role)
62 {
63         struct ldb_dn *role_owner_dn, *fsmo_role_dn, *ntds_dn;
64         TALLOC_CTX *tmp_ctx = talloc_new(service);
65         struct ldb_context *ldb = service->samdb;
66         int ret;
67         uint64_t fsmo_info = 0;
68         enum drsuapi_DsExtendedOperation extended_op = DRSUAPI_EXOP_NONE;
69         WERROR werr;
70
71         ntds_dn = samdb_ntds_settings_dn(ldb);
72         if (!ntds_dn) {
73                 return WERR_DS_DRA_INTERNAL_ERROR;
74         }
75
76         switch (role) {
77         case DREPL_NAMING_MASTER:
78                 fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx),
79                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
80                 if (ret != LDB_SUCCESS) {
81                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
82                                  ldb_errstring(ldb)));
83                         talloc_free(tmp_ctx);
84                         return WERR_DS_DRA_INTERNAL_ERROR;
85                 }
86                 break;
87         case DREPL_INFRASTRUCTURE_MASTER:
88                 fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
89                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
90                 if (ret != LDB_SUCCESS) {
91                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
92                                  ldb_errstring(ldb)));
93                         talloc_free(tmp_ctx);
94                         return WERR_DS_DRA_INTERNAL_ERROR;
95                 }
96                 extended_op = DRSUAPI_EXOP_FSMO_REQ_ROLE;
97                 break;
98         case DREPL_RID_MASTER:
99                 ret = samdb_rid_manager_dn(ldb, tmp_ctx, &fsmo_role_dn);
100                 if (ret != LDB_SUCCESS) {
101                         DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
102                         talloc_free(tmp_ctx);
103                         return WERR_DS_DRA_INTERNAL_ERROR;
104                 }
105
106                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
107                 if (ret != LDB_SUCCESS) {
108                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
109                                  ldb_errstring(ldb)));
110                         talloc_free(tmp_ctx);
111                         return WERR_DS_DRA_INTERNAL_ERROR;
112                 }
113                 extended_op = DRSUAPI_EXOP_FSMO_RID_REQ_ROLE;
114                 break;
115         case DREPL_SCHEMA_MASTER:
116                 fsmo_role_dn = ldb_get_schema_basedn(ldb);
117                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
118                 if (ret != LDB_SUCCESS) {
119                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
120                                  ldb_errstring(ldb)));
121                         talloc_free(tmp_ctx);
122                         return WERR_DS_DRA_INTERNAL_ERROR;
123                 }
124                 extended_op = DRSUAPI_EXOP_FSMO_REQ_ROLE;
125                 break;
126         case DREPL_PDC_MASTER:
127                 fsmo_role_dn = ldb_get_default_basedn(ldb);
128                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
129                 if (ret != LDB_SUCCESS) {
130                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
131                                  ldb_errstring(ldb)));
132                         talloc_free(tmp_ctx);
133                         return WERR_DS_DRA_INTERNAL_ERROR;
134                 }
135                 extended_op = DRSUAPI_EXOP_FSMO_REQ_PDC;
136                 break;
137         default:
138                 return WERR_DS_DRA_INTERNAL_ERROR;
139         }
140
141         if (fsmo_master_cmp(ntds_dn, role_owner_dn) ||
142             (extended_op == DRSUAPI_EXOP_NONE)) {
143                 DEBUG(0,("FSMO role check failed for DN %s and owner %s ",
144                          ldb_dn_get_linearized(fsmo_role_dn),
145                          ldb_dn_get_linearized(role_owner_dn)));
146                 return WERR_OK;
147         }
148
149         werr = drepl_request_extended_op(service,
150                                          fsmo_role_dn,
151                                          role_owner_dn,
152                                          extended_op,
153                                          fsmo_info,
154                                          0,
155                                          drepl_role_callback,
156                                          NULL);
157         if (W_ERROR_IS_OK(werr)) {
158                 dreplsrv_run_pending_ops(service);
159         } else {
160                 DEBUG(0,("%s: drepl_request_extended_op() failed with %s",
161                          __FUNCTION__, win_errstr(werr)));
162         }
163         return werr;
164 }