63b831669a9c02779623f842b9fe6c2acfbaafb2
[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         service->role_transfer_in_progress = false;
47 }
48
49 static bool fsmo_master_cmp(struct ldb_dn *ntds_dn, struct ldb_dn *role_owner_dn)
50 {
51         if (ldb_dn_compare(ntds_dn, role_owner_dn) == 0) {
52                 DEBUG(0,("\nWe are the FSMO master.\n"));
53                 return true;
54         }
55         return false;
56 }
57
58 /*
59   see which role is we are asked to assume, initialize data and send request
60  */
61 WERROR dreplsrv_fsmo_role_check(struct dreplsrv_service *service,
62                                 uint32_t role)
63 {
64         struct ldb_dn *role_owner_dn, *fsmo_role_dn, *ntds_dn;
65         TALLOC_CTX *tmp_ctx = talloc_new(service);
66         struct ldb_context *ldb = service->samdb;
67         int ret;
68         uint64_t fsmo_info = 0;
69         enum drsuapi_DsExtendedOperation extended_op = DRSUAPI_EXOP_NONE;
70         WERROR werr;
71
72         if (service->role_transfer_in_progress) {
73                 talloc_free(tmp_ctx);
74                 /* should we allow these in parallel? */
75                 return WERR_DS_DRA_REPL_PENDING;
76         }
77
78         ntds_dn = samdb_ntds_settings_dn(ldb);
79         if (!ntds_dn) {
80                 return WERR_DS_DRA_INTERNAL_ERROR;
81         }
82
83         switch (role) {
84         case DREPL_NAMING_MASTER:
85                 fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx),
86                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
87                 if (ret != LDB_SUCCESS) {
88                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
89                                  ldb_errstring(ldb)));
90                         talloc_free(tmp_ctx);
91                         return WERR_DS_DRA_INTERNAL_ERROR;
92                 }
93                 break;
94         case DREPL_INFRASTRUCTURE_MASTER:
95                 fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
96                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
97                 if (ret != LDB_SUCCESS) {
98                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
99                                  ldb_errstring(ldb)));
100                         talloc_free(tmp_ctx);
101                         return WERR_DS_DRA_INTERNAL_ERROR;
102                 }
103                 extended_op = DRSUAPI_EXOP_FSMO_REQ_ROLE;
104                 break;
105         case DREPL_RID_MASTER:
106                 ret = samdb_rid_manager_dn(ldb, tmp_ctx, &fsmo_role_dn);
107                 if (ret != LDB_SUCCESS) {
108                         DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
109                         talloc_free(tmp_ctx);
110                         return WERR_DS_DRA_INTERNAL_ERROR;
111                 }
112
113                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
114                 if (ret != LDB_SUCCESS) {
115                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
116                                  ldb_errstring(ldb)));
117                         talloc_free(tmp_ctx);
118                         return WERR_DS_DRA_INTERNAL_ERROR;
119                 }
120                 extended_op = DRSUAPI_EXOP_FSMO_RID_REQ_ROLE;
121                 break;
122         case DREPL_SCHEMA_MASTER:
123                 fsmo_role_dn = ldb_get_schema_basedn(ldb);
124                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
125                 if (ret != LDB_SUCCESS) {
126                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
127                                  ldb_errstring(ldb)));
128                         talloc_free(tmp_ctx);
129                         return WERR_DS_DRA_INTERNAL_ERROR;
130                 }
131                 extended_op = DRSUAPI_EXOP_FSMO_REQ_ROLE;
132                 break;
133         case DREPL_PDC_MASTER:
134                 fsmo_role_dn = ldb_get_default_basedn(ldb);
135                 ret = samdb_reference_dn(ldb, tmp_ctx, fsmo_role_dn, "fSMORoleOwner", &role_owner_dn);
136                 if (ret != LDB_SUCCESS) {
137                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
138                                  ldb_errstring(ldb)));
139                         talloc_free(tmp_ctx);
140                         return WERR_DS_DRA_INTERNAL_ERROR;
141                 }
142                 extended_op = DRSUAPI_EXOP_FSMO_REQ_PDC;
143                 break;
144         default:
145                 return WERR_DS_DRA_INTERNAL_ERROR;
146         }
147
148         if (fsmo_master_cmp(ntds_dn, role_owner_dn) ||
149             (extended_op == DRSUAPI_EXOP_NONE)) {
150                 DEBUG(0,("FSMO role check failed for DN %s and owner %s ",
151                          ldb_dn_get_linearized(fsmo_role_dn),
152                          ldb_dn_get_linearized(role_owner_dn)));
153                 return WERR_OK;
154         }
155
156         werr = drepl_request_extended_op(service,
157                                          fsmo_role_dn,
158                                          role_owner_dn,
159                                          extended_op,
160                                          fsmo_info,
161                                          drepl_role_callback);
162         if (W_ERROR_IS_OK(werr)) {
163                 dreplsrv_run_pending_ops(service);
164         } else {
165                 DEBUG(0,("%s: drepl_request_extended_op() failed with %s",
166                          __FUNCTION__, win_errstr(werr)));
167         }
168         return werr;
169 }