r23718: Make Samba4 work against the LDAP backend again.
[ira/wip.git] / source / 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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "lib/ldb/include/ldb_private.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "librpc/gen_ndr/ndr_drsuapi.h"
32 #include "librpc/gen_ndr/ndr_drsblobs.h"
33 #include "lib/util/dlinklist.h"
34
35 static int naming_fsmo_init(struct ldb_module *module)
36 {
37         TALLOC_CTX *mem_ctx;
38         struct ldb_dn *naming_dn;
39         struct dsdb_naming_fsmo *naming_fsmo;
40         struct ldb_result *naming_res;
41         int ret;
42         static const char *naming_attrs[] = {
43                 "fSMORoleOwner",
44                 NULL
45         };
46
47         mem_ctx = talloc_new(module);
48         if (!mem_ctx) {
49                 ldb_oom(module->ldb);
50                 return LDB_ERR_OPERATIONS_ERROR;
51         }
52
53         naming_dn = samdb_partitions_dn(module->ldb, mem_ctx);
54         if (!naming_dn) {
55                 ldb_debug(module->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(module->ldb);
64                 return LDB_ERR_OPERATIONS_ERROR;
65         }
66         module->private_data = naming_fsmo;
67
68         ret = ldb_search(module->ldb, naming_dn,
69                          LDB_SCOPE_BASE,
70                          NULL, naming_attrs,
71                          &naming_res);
72         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
73                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
74                           "naming_fsmo_init: no partitions dn present: (skip loading of naming contexts details)\n");
75                 talloc_free(mem_ctx);
76                 return ldb_next_init(module);
77         }
78         if (ret != LDB_SUCCESS) {
79                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
80                               "naming_fsmo_init: failed to search the cross-ref container: %s: %s\n",
81                               ldb_strerror(ret), ldb_errstring(module->ldb));
82                 talloc_free(mem_ctx);
83                 return ret;
84         }
85         talloc_steal(mem_ctx, naming_res);
86         if (naming_res->count == 0) {
87                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
88                           "naming_fsmo_init: no cross-ref container present: (skip loading of naming contexts details)\n");
89                 talloc_free(mem_ctx);
90                 return ldb_next_init(module);
91         } else if (naming_res->count > 1) {
92                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
93                               "naming_fsmo_init: [%u] cross-ref containers found on a base search\n",
94                               naming_res->count);
95                 talloc_free(mem_ctx);
96                 return LDB_ERR_CONSTRAINT_VIOLATION;
97         }
98
99         naming_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, naming_fsmo, naming_res->msgs[0], "fSMORoleOwner");
100         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), naming_fsmo->master_dn) == 0) {
101                 naming_fsmo->we_are_master = true;
102         } else {
103                 naming_fsmo->we_are_master = false;
104         }
105
106         if (ldb_set_opaque(module->ldb, "dsdb_naming_fsmo", naming_fsmo) != LDB_SUCCESS) {
107                 ldb_oom(module->ldb);
108                 return LDB_ERR_OPERATIONS_ERROR;
109         }
110
111         talloc_steal(module, naming_fsmo);
112
113         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
114                           "naming_fsmo_init: we are master: %s\n",
115                           (naming_fsmo->we_are_master?"yes":"no"));
116
117         talloc_free(mem_ctx);
118         return ldb_next_init(module);
119 }
120
121 static const struct ldb_module_ops naming_fsmo_ops = {
122         .name           = "naming_fsmo",
123         .init_context   = naming_fsmo_init
124 };
125
126 int naming_fsmo_module_init(void)
127 {
128         return ldb_register_module(&naming_fsmo_ops);
129 }