607bf054d22c319aa6858a9f5cb56e99d5d221c7
[kai/samba-autobuild/.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 "ldb_module.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "../lib/util/dlinklist.h"
31
32 static int naming_fsmo_init(struct ldb_module *module)
33 {
34         struct ldb_context *ldb;
35         TALLOC_CTX *mem_ctx;
36         struct ldb_dn *naming_dn;
37         struct dsdb_naming_fsmo *naming_fsmo;
38         struct ldb_result *naming_res;
39         int ret;
40         static const char *naming_attrs[] = {
41                 "fSMORoleOwner",
42                 NULL
43         };
44
45         ldb = ldb_module_get_ctx(module);
46
47         mem_ctx = talloc_new(module);
48         if (!mem_ctx) {
49                 ldb_oom(ldb);
50                 return LDB_ERR_OPERATIONS_ERROR;
51         }
52
53         naming_dn = samdb_partitions_dn(ldb, mem_ctx);
54         if (!naming_dn) {
55                 ldb_debug(ldb, LDB_DEBUG_WARNING,
56                           "naming_fsmo_init: no partitions dn present: (skip loading of naming contexts details)\n");
57                 talloc_free(mem_ctx);
58                 return ldb_next_init(module);
59         }
60
61         naming_fsmo = talloc_zero(mem_ctx, struct dsdb_naming_fsmo);
62         if (!naming_fsmo) {
63                 ldb_oom(ldb);
64                 return LDB_ERR_OPERATIONS_ERROR;
65         }
66         ldb_module_set_private(module, naming_fsmo);
67
68         ret = ldb_search(ldb, mem_ctx, &naming_res,
69                          naming_dn, LDB_SCOPE_BASE,
70                          naming_attrs, NULL);
71         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
72                 ldb_debug(ldb, LDB_DEBUG_WARNING,
73                           "naming_fsmo_init: no partitions dn present: (skip loading of naming contexts details)\n");
74                 talloc_free(mem_ctx);
75                 return ldb_next_init(module);
76         }
77         if (ret != LDB_SUCCESS) {
78                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
79                               "naming_fsmo_init: failed to search the cross-ref container: %s: %s",
80                               ldb_strerror(ret), ldb_errstring(ldb));
81                 talloc_free(mem_ctx);
82                 return ret;
83         }
84         if (naming_res->count == 0) {
85                 ldb_debug(ldb, LDB_DEBUG_WARNING,
86                           "naming_fsmo_init: no cross-ref container present: (skip loading of naming contexts details)\n");
87                 talloc_free(mem_ctx);
88                 return ldb_next_init(module);
89         } else if (naming_res->count > 1) {
90                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
91                               "naming_fsmo_init: [%u] cross-ref containers found on a base search",
92                               naming_res->count);
93                 talloc_free(mem_ctx);
94                 return LDB_ERR_CONSTRAINT_VIOLATION;
95         }
96
97         naming_fsmo->master_dn = ldb_msg_find_attr_as_dn(ldb, naming_fsmo, naming_res->msgs[0], "fSMORoleOwner");
98         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), naming_fsmo->master_dn) == 0) {
99                 naming_fsmo->we_are_master = true;
100         } else {
101                 naming_fsmo->we_are_master = false;
102         }
103
104         if (ldb_set_opaque(ldb, "dsdb_naming_fsmo", naming_fsmo) != LDB_SUCCESS) {
105                 ldb_oom(ldb);
106                 return LDB_ERR_OPERATIONS_ERROR;
107         }
108
109         talloc_steal(module, naming_fsmo);
110
111         ldb_debug(ldb, LDB_DEBUG_TRACE,
112                           "naming_fsmo_init: we are master: %s\n",
113                           (naming_fsmo->we_are_master?"yes":"no"));
114
115         talloc_free(mem_ctx);
116         return ldb_next_init(module);
117 }
118
119 _PUBLIC_ const struct ldb_module_ops ldb_naming_fsmo_module_ops = {
120         .name           = "naming_fsmo",
121         .init_context   = naming_fsmo_init
122 };