Move ad2oLschema and oLschema2ldif into Samba4, out of LDB
[kai/samba.git] / source4 / dsdb / samdb / ldb_modules / schema_fsmo.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the Schema FSMO Role Owner
5    checkings, it also loads the dsdb_schema.
6    
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 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 #include "param/param.h"
34
35 static int schema_fsmo_init(struct ldb_module *module)
36 {
37         TALLOC_CTX *mem_ctx;
38         struct ldb_dn *schema_dn;
39         struct dsdb_schema *schema;
40         struct ldb_result *schema_res;
41         struct ldb_result *a_res;
42         struct ldb_result *c_res;
43         char *error_string = NULL;
44         int ret;
45         static const char *schema_attrs[] = {
46                 "prefixMap",
47                 "schemaInfo",
48                 "fSMORoleOwner",
49                 NULL
50         };
51
52         if (dsdb_get_schema(module->ldb)) {
53                 return ldb_next_init(module);
54         }
55
56         schema_dn = samdb_schema_dn(module->ldb);
57         if (!schema_dn) {
58                 ldb_reset_err_string(module->ldb);
59                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
60                           "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
61                 return ldb_next_init(module);
62         }
63
64         mem_ctx = talloc_new(module);
65         if (!mem_ctx) {
66                 ldb_oom(module->ldb);
67                 return LDB_ERR_OPERATIONS_ERROR;
68         }
69
70         /*
71          * setup the prefix mappings and schema info
72          */
73         ret = ldb_search(module->ldb, schema_dn,
74                          LDB_SCOPE_BASE,
75                          NULL, schema_attrs,
76                          &schema_res);
77         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
78                 ldb_reset_err_string(module->ldb);
79                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
80                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
81                 talloc_free(mem_ctx);
82                 return ldb_next_init(module);
83         } else if (ret != LDB_SUCCESS) {
84                 ldb_asprintf_errstring(module->ldb, 
85                                        "schema_fsmo_init: failed to search the schema head: %s",
86                                        ldb_errstring(module->ldb));
87                 talloc_free(mem_ctx);
88                 return ret;
89         }
90         talloc_steal(mem_ctx, schema_res);
91         if (schema_res->count == 0) {
92                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
93                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
94                 talloc_free(mem_ctx);
95                 return ldb_next_init(module);
96         } else if (schema_res->count > 1) {
97                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
98                               "schema_fsmo_init: [%u] schema heads found on a base search",
99                               schema_res->count);
100                 talloc_free(mem_ctx);
101                 return LDB_ERR_CONSTRAINT_VIOLATION;
102         }
103
104         /*
105          * load the attribute definitions
106          */
107         ret = ldb_search(module->ldb, schema_dn,
108                          LDB_SCOPE_ONELEVEL,
109                          "(objectClass=attributeSchema)", NULL,
110                          &a_res);
111         if (ret != LDB_SUCCESS) {
112                 ldb_asprintf_errstring(module->ldb, 
113                                        "schema_fsmo_init: failed to search attributeSchema objects: %s",
114                                        ldb_errstring(module->ldb));
115                 talloc_free(mem_ctx);
116                 return ret;
117         }
118         talloc_steal(mem_ctx, a_res);
119
120         /*
121          * load the objectClass definitions
122          */
123         ret = ldb_search(module->ldb, schema_dn,
124                          LDB_SCOPE_ONELEVEL,
125                          "(objectClass=classSchema)", NULL,
126                          &c_res);
127         if (ret != LDB_SUCCESS) {
128                 ldb_asprintf_errstring(module->ldb, 
129                                        "schema_fsmo_init: failed to search classSchema objects: %s",
130                                        ldb_errstring(module->ldb));
131                 talloc_free(mem_ctx);
132                 return ret;
133         }
134         talloc_steal(mem_ctx, c_res);
135
136         ret = dsdb_schema_from_ldb_results(mem_ctx, module->ldb,
137                                            lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
138                                            schema_res, a_res, c_res, &schema, &error_string);
139         if (ret != LDB_SUCCESS) {
140                 ldb_asprintf_errstring(module->ldb, 
141                                        "schema_fsmo_init: dsdb_schema load failed: %s",
142                                        error_string);
143                 talloc_free(mem_ctx);
144                 return ret;
145         }
146         
147         /* dsdb_set_schema() steal schema into the ldb_context */
148         ret = dsdb_set_schema(module->ldb, schema);
149         if (ret != LDB_SUCCESS) {
150                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
151                               "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
152                               ret, ldb_strerror(ret));
153                 talloc_free(mem_ctx);
154                 return ret;
155         }
156
157         talloc_free(mem_ctx);
158         return ldb_next_init(module);
159 }
160
161 static int schema_fsmo_add(struct ldb_module *module, struct ldb_request *req)
162 {
163         struct dsdb_schema *schema;
164         const char *attributeID = NULL;
165         const char *governsID = NULL;
166         const char *oid_attr = NULL;
167         const char *oid = NULL;
168         uint32_t id32;
169         WERROR status;
170
171         schema = dsdb_get_schema(module->ldb);
172         if (!schema) {
173                 return ldb_next_request(module, req);
174         }
175
176         if (!schema->fsmo.we_are_master) {
177                 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
178                           "schema_fsmo_add: we are not master: reject request\n");
179                 return LDB_ERR_UNWILLING_TO_PERFORM;
180         }
181
182         attributeID = samdb_result_string(req->op.add.message, "attributeID", NULL);
183         governsID = samdb_result_string(req->op.add.message, "governsID", NULL);
184
185         if (attributeID) {
186                 oid_attr = "attributeID";
187                 oid = attributeID;
188         } else if (governsID) {
189                 oid_attr = "governsID";
190                 oid = governsID;
191         }
192
193         if (!oid) {
194                 return ldb_next_request(module, req);
195         }
196
197         status = dsdb_map_oid2int(schema, oid, &id32);
198         if (W_ERROR_IS_OK(status)) {
199                 return ldb_next_request(module, req);
200         } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
201                 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
202                           "schema_fsmo_add: failed to map %s[%s]: %s\n",
203                           oid_attr, oid, win_errstr(status));
204                 return LDB_ERR_UNWILLING_TO_PERFORM;
205         }
206
207         status = dsdb_create_prefix_mapping(module->ldb, schema, oid);
208         if (!W_ERROR_IS_OK(status)) {
209                 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
210                           "schema_fsmo_add: failed to create prefix mapping for %s[%s]: %s\n",
211                           oid_attr, oid, win_errstr(status));
212                 return LDB_ERR_UNWILLING_TO_PERFORM;
213         }
214
215         return ldb_next_request(module, req);
216 }
217
218 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
219         .name           = "schema_fsmo",
220         .init_context   = schema_fsmo_init,
221         .add            = schema_fsmo_add
222 };