70f3e8ddfdaef39c83bbcda45eaa70a9dbe95a5a
[samba.git] / source4 / dsdb / samdb / ldb_modules / naming_fsmo.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the Domain Naming FSMO Role Owner
5    checkings
6    
7    Copyright (C) Stefan Metzmacher 2007
8     
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    
22 */
23
24 #include "includes.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "librpc/gen_ndr/ndr_drsuapi.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "lib/util/dlinklist.h"
33
34 static int naming_fsmo_init(struct ldb_module *module)
35 {
36         TALLOC_CTX *mem_ctx;
37         struct ldb_dn *naming_dn;
38         struct dsdb_naming_fsmo *naming_fsmo;
39         struct ldb_result *naming_res;
40         int ret;
41         static const char *naming_attrs[] = {
42                 "fSMORoleOwner",
43                 NULL
44         };
45
46         mem_ctx = talloc_new(module);
47         if (!mem_ctx) {
48                 ldb_oom(module->ldb);
49                 return LDB_ERR_OPERATIONS_ERROR;
50         }
51
52         naming_dn = samdb_partitions_dn(module->ldb, mem_ctx);
53         if (!naming_dn) {
54                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
55                           "naming_fsmo_init: no partitions dn present: (skip loading of naming contexts details)\n");
56                 talloc_free(mem_ctx);
57                 return ldb_next_init(module);
58         }
59
60         naming_fsmo = talloc_zero(mem_ctx, struct dsdb_naming_fsmo);
61         if (!naming_fsmo) {
62                 ldb_oom(module->ldb);
63                 return LDB_ERR_OPERATIONS_ERROR;
64         }
65         module->private_data = naming_fsmo;
66
67         ret = ldb_search(module->ldb, mem_ctx, &naming_res,
68                          naming_dn, LDB_SCOPE_BASE,
69                          naming_attrs, NULL);
70         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
71                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
72                           "naming_fsmo_init: no partitions dn present: (skip loading of naming contexts details)\n");
73                 talloc_free(mem_ctx);
74                 return ldb_next_init(module);
75         }
76         if (ret != LDB_SUCCESS) {
77                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
78                               "naming_fsmo_init: failed to search the cross-ref container: %s: %s",
79                               ldb_strerror(ret), ldb_errstring(module->ldb));
80                 talloc_free(mem_ctx);
81                 return ret;
82         }
83         if (naming_res->count == 0) {
84                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
85                           "naming_fsmo_init: no cross-ref container present: (skip loading of naming contexts details)\n");
86                 talloc_free(mem_ctx);
87                 return ldb_next_init(module);
88         } else if (naming_res->count > 1) {
89                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
90                               "naming_fsmo_init: [%u] cross-ref containers found on a base search",
91                               naming_res->count);
92                 talloc_free(mem_ctx);
93                 return LDB_ERR_CONSTRAINT_VIOLATION;
94         }
95
96         naming_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, naming_fsmo, naming_res->msgs[0], "fSMORoleOwner");
97         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), naming_fsmo->master_dn) == 0) {
98                 naming_fsmo->we_are_master = true;
99         } else {
100                 naming_fsmo->we_are_master = false;
101         }
102
103         if (ldb_set_opaque(module->ldb, "dsdb_naming_fsmo", naming_fsmo) != LDB_SUCCESS) {
104                 ldb_oom(module->ldb);
105                 return LDB_ERR_OPERATIONS_ERROR;
106         }
107
108         talloc_steal(module, naming_fsmo);
109
110         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
111                           "naming_fsmo_init: we are master: %s\n",
112                           (naming_fsmo->we_are_master?"yes":"no"));
113
114         talloc_free(mem_ctx);
115         return ldb_next_init(module);
116 }
117
118 _PUBLIC_ const struct ldb_module_ops ldb_naming_fsmo_module_ops = {
119         .name           = "naming_fsmo",
120         .init_context   = naming_fsmo_init
121 };