s4:dsdb/repl: fix the usage of 'GC/' prefixed principal names
[samba.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
34 struct fsmo_role_state {
35         struct irpc_message *msg;
36         struct drepl_takeFSMORole *r;
37 };
38
39 static void drepl_role_callback(struct dreplsrv_service *service,
40                                 WERROR werr,
41                                 enum drsuapi_DsExtendedError ext_err,
42                                 void *cb_data)
43 {
44         struct fsmo_role_state *fsmo = talloc_get_type_abort(cb_data, struct fsmo_role_state);
45         if (!W_ERROR_IS_OK(werr)) {
46                 DEBUG(2,(__location__ ": Failed role transfer - %s - extended_ret[0x%X]\n",
47                          win_errstr(werr), ext_err));
48         } else {
49                 DEBUG(2,(__location__ ": Successful role transfer\n"));
50         }
51         fsmo->r->out.result = werr;
52         irpc_send_reply(fsmo->msg, NT_STATUS_OK);
53 }
54
55 /*
56   see which role is we are asked to assume, initialize data and send request
57  */
58 NTSTATUS drepl_take_FSMO_role(struct irpc_message *msg,
59                               struct drepl_takeFSMORole *r)
60 {
61         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
62                                                            struct dreplsrv_service);
63         struct ldb_dn *role_owner_dn, *fsmo_role_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         enum drepl_role_master role = r->in.role;
69         struct fsmo_role_state *fsmo;
70         bool is_us;
71         int ret;
72
73         werr = dsdb_get_fsmo_role_info(tmp_ctx, service->samdb, role,
74                                        &fsmo_role_dn, &role_owner_dn);
75         if (!W_ERROR_IS_OK(werr)) {
76                 talloc_free(tmp_ctx);
77                 r->out.result = werr;
78                 return NT_STATUS_OK;
79         }
80
81         switch (role) {
82         case DREPL_NAMING_MASTER:
83         case DREPL_INFRASTRUCTURE_MASTER:
84         case DREPL_SCHEMA_MASTER:
85                 extended_op = DRSUAPI_EXOP_FSMO_REQ_ROLE;
86                 break;
87         case DREPL_RID_MASTER:
88                 extended_op = DRSUAPI_EXOP_FSMO_RID_REQ_ROLE;
89                 break;
90         case DREPL_PDC_MASTER:
91                 extended_op = DRSUAPI_EXOP_FSMO_REQ_PDC;
92                 break;
93         default:
94                 DEBUG(2,("Unknown role %u in role transfer\n",
95                          (unsigned)role));
96                 r->out.result = WERR_DS_DRA_INTERNAL_ERROR;
97                 talloc_free(tmp_ctx);
98                 return NT_STATUS_OK;
99         }
100
101         ret = samdb_dn_is_our_ntdsa(service->samdb, role_owner_dn, &is_us);
102         if (ret != LDB_SUCCESS) {
103                 DEBUG(0,("FSMO role check failed (failed to confirm if our ntdsDsa) for DN %s and owner %s \n",
104                          ldb_dn_get_linearized(fsmo_role_dn),
105                          ldb_dn_get_linearized(role_owner_dn)));
106                 talloc_free(tmp_ctx);
107                 r->out.result = WERR_DS_DRA_INTERNAL_ERROR;
108                 return NT_STATUS_OK;
109         }
110         
111         if (is_us || 
112             (extended_op == DRSUAPI_EXOP_NONE)) {
113                 DEBUG(0,("FSMO role check failed for DN %s and owner %s \n",
114                          ldb_dn_get_linearized(fsmo_role_dn),
115                          ldb_dn_get_linearized(role_owner_dn)));
116                 r->out.result = WERR_OK;
117                 talloc_free(tmp_ctx);
118                 return NT_STATUS_OK;
119         }
120
121         fsmo = talloc(msg, struct fsmo_role_state);
122         NT_STATUS_HAVE_NO_MEMORY(fsmo);
123
124         fsmo->msg = msg;
125         fsmo->r   = r;
126
127         werr = drepl_request_extended_op(service,
128                                          fsmo_role_dn,
129                                          role_owner_dn,
130                                          extended_op,
131                                          fsmo_info,
132                                          0,
133                                          drepl_role_callback,
134                                          fsmo);
135         if (!W_ERROR_IS_OK(werr)) {
136                 r->out.result = werr;
137                 talloc_free(tmp_ctx);
138                 return NT_STATUS_OK;
139         }
140
141         /* mark this message to be answered later */
142         msg->defer_reply = true;
143         dreplsrv_run_pending_ops(service);
144         talloc_free(tmp_ctx);
145         return NT_STATUS_OK;
146 }