s4:role transfer - use always type "enum drepl_role_master" for role specifications
[garming/samba-autobuild/.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    Copyright (C) Anatoliy Atanasov 2010
10
11    based on drepl_ridalloc.c
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 3 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26 */
27
28 #include "includes.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "smbd/service.h"
31 #include "dsdb/repl/drepl_service.h"
32 #include "param/param.h"
33 #include "librpc/gen_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                                 enum drepl_role_master role)
62 {
63         struct ldb_dn *role_owner_dn, *fsmo_role_dn, *ntds_dn;
64         TALLOC_CTX *tmp_ctx = talloc_new(service);
65         uint64_t fsmo_info = 0;
66         enum drsuapi_DsExtendedOperation extended_op = DRSUAPI_EXOP_NONE;
67         WERROR werr;
68
69         ntds_dn = samdb_ntds_settings_dn(service->samdb);
70         if (!ntds_dn) {
71                 return WERR_DS_DRA_INTERNAL_ERROR;
72         }
73
74         werr = dsdb_get_fsmo_role_info(tmp_ctx, service->samdb, role,
75                                        &fsmo_role_dn, &role_owner_dn);
76         if (!W_ERROR_IS_OK(werr)) {
77                 return werr;
78         }
79
80         switch (role) {
81         case DREPL_NAMING_MASTER:
82         case DREPL_INFRASTRUCTURE_MASTER:
83         case DREPL_SCHEMA_MASTER:
84                 extended_op = DRSUAPI_EXOP_FSMO_REQ_ROLE;
85                 break;
86         case DREPL_RID_MASTER:
87                 extended_op = DRSUAPI_EXOP_FSMO_RID_REQ_ROLE;
88                 break;
89         case DREPL_PDC_MASTER:
90                 extended_op = DRSUAPI_EXOP_FSMO_REQ_PDC;
91                 break;
92         default:
93                 return WERR_DS_DRA_INTERNAL_ERROR;
94         }
95
96         if (fsmo_master_cmp(ntds_dn, role_owner_dn) ||
97             (extended_op == DRSUAPI_EXOP_NONE)) {
98                 DEBUG(0,("FSMO role check failed for DN %s and owner %s ",
99                          ldb_dn_get_linearized(fsmo_role_dn),
100                          ldb_dn_get_linearized(role_owner_dn)));
101                 return WERR_OK;
102         }
103
104         werr = drepl_request_extended_op(service,
105                                          fsmo_role_dn,
106                                          role_owner_dn,
107                                          extended_op,
108                                          fsmo_info,
109                                          0,
110                                          drepl_role_callback,
111                                          NULL);
112         if (W_ERROR_IS_OK(werr)) {
113                 dreplsrv_run_pending_ops(service);
114         } else {
115                 DEBUG(0,("%s: drepl_request_extended_op() failed with %s",
116                          __FUNCTION__, win_errstr(werr)));
117         }
118         return werr;
119 }