s4:new_partition LDB module - fix comments
[rusty/samba.git] / source4 / dsdb / samdb / ldb_modules / new_partition.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    Copyright (C) Andrew Tridgell 2005
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  *  Name: ldb
25  *
26  *  Component: ldb new partition module
27  *
28  *  Description: Handle the add of new partitions
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include "ldb.h"
35 #include "ldb_module.h"
36 #include "librpc/gen_ndr/ndr_misc.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "../libds/common/flags.h"
39
40 struct np_context {
41         struct ldb_module *module;
42         struct ldb_request *req;
43         struct ldb_request *search_req;
44         struct ldb_request *part_add;
45 };
46
47 static int np_part_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
48 {
49         struct ldb_context *ldb;
50         struct np_context *ac;
51
52         ac = talloc_get_type(req->context, struct np_context);
53         ldb = ldb_module_get_ctx(ac->module);
54
55         if (!ares) {
56                 return ldb_module_done(ac->req, NULL, NULL,
57                                         LDB_ERR_OPERATIONS_ERROR);
58         }
59
60         /* We just want to update the @PARTITIONS record if the value does not exist */
61         if (ares->error != LDB_SUCCESS && ares->error != LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
62                 return ldb_module_done(ac->req, ares->controls,
63                                         ares->response, ares->error);
64         }
65
66         if (ares->type != LDB_REPLY_DONE) {
67                 ldb_set_errstring(ldb, "Invalid reply type!");
68                 return ldb_module_done(ac->req, NULL, NULL,
69                                         LDB_ERR_OPERATIONS_ERROR);
70         }
71
72         ldb_reset_err_string(ldb);
73
74         /* Do the original add */
75         return ldb_next_request(ac->module, ac->req);
76 }
77
78 static int np_part_search_callback(struct ldb_request *req, struct ldb_reply *ares)
79 {
80         struct ldb_context *ldb;
81         struct np_context *ac;
82         struct dsdb_create_partition_exop *ex_op;
83         int ret;
84
85         ac = talloc_get_type(req->context, struct np_context);
86         ldb = ldb_module_get_ctx(ac->module);
87
88         if (!ares) {
89                 return ldb_module_done(ac->req, NULL, NULL,
90                                         LDB_ERR_OPERATIONS_ERROR);
91         }
92
93         /* If this already exists, we really don't want to create a
94          * partition - it would allow a duplicate entry to be
95          * created */
96         if (ares->error != LDB_ERR_NO_SUCH_OBJECT) {
97                 if (ares->error == LDB_SUCCESS) {
98                         return ldb_module_done(ac->req, ares->controls,
99                                                ares->response, LDB_ERR_ENTRY_ALREADY_EXISTS);
100                 } else {
101                         return ldb_module_done(ac->req, ares->controls,
102                                                ares->response, ares->error);
103                 }
104         }
105
106         if (ares->type != LDB_REPLY_DONE) {
107                 ldb_set_errstring(ldb, "Invalid reply type - we must not get a result here!");
108                 return ldb_module_done(ac->req, NULL, NULL,
109                                         LDB_ERR_OPERATIONS_ERROR);
110         }
111
112         ldb_reset_err_string(ldb);
113
114         /* Now that we know it does not exist, we can try and create the partition */
115         ex_op = talloc(ac, struct dsdb_create_partition_exop);
116         if (ex_op == NULL) {
117                 return ldb_oom(ldb);
118         }
119         
120         ex_op->new_dn = ac->req->op.add.message->dn;
121         
122         ret = ldb_build_extended_req(&ac->part_add, 
123                                      ldb, ac, DSDB_EXTENDED_CREATE_PARTITION_OID, ex_op, 
124                                      NULL, ac, np_part_mod_callback, req);
125         
126         LDB_REQ_SET_LOCATION(ac->part_add);
127         if (ret != LDB_SUCCESS) {
128                 return ret;
129         }
130         
131         return ldb_next_request(ac->module, ac->part_add);
132 }
133
134 /* add_record: add instancetype attribute */
135 static int new_partition_add(struct ldb_module *module, struct ldb_request *req)
136 {
137         struct ldb_context *ldb;
138         struct np_context *ac;
139         int ret;
140
141         ldb = ldb_module_get_ctx(module);
142
143         ldb_debug(ldb, LDB_DEBUG_TRACE, "new_partition_add\n");
144
145         /* do not manipulate our control entries */
146         if (ldb_dn_is_special(req->op.add.message->dn)) {
147                 return ldb_next_request(module, req);
148         }
149
150         if (ldb_msg_find_element(req->op.add.message, "instanceType")) {
151                 /* This needs to be 'static' to ensure it does not move, and is not on the stack */
152                 static const char *no_attrs[] = { NULL };
153                 unsigned int instanceType = ldb_msg_find_attr_as_uint(req->op.add.message, "instanceType", 0);
154                 if (!(instanceType & INSTANCE_TYPE_IS_NC_HEAD) ||
155                     (instanceType & INSTANCE_TYPE_UNINSTANT)) {
156                         return ldb_next_request(module, req);
157                 }
158
159                 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
160                         DEBUG(0,(__location__ ": Skipping uninstantiated partition %s\n",
161                                  ldb_dn_get_linearized(req->op.add.message->dn)));
162                         return ldb_next_request(module, req);
163                 }
164
165                 if (ldb_msg_find_attr_as_bool(req->op.add.message, "isDeleted", false)) {
166                         DEBUG(0,(__location__ ": Skipping deleted partition %s\n",
167                                  ldb_dn_get_linearized(req->op.add.message->dn)));
168                         return ldb_next_request(module, req);           
169                 }
170
171                 /* Create an @PARTITIONS record for this partition -
172                  * by asking the partitions module to do so via an
173                  * extended operation, after first checking if the
174                  * record already exists */
175                 ac = talloc(req, struct np_context);
176                 if (ac == NULL) {
177                         return ldb_oom(ldb);
178                 }
179                 ac->module = module;
180                 ac->req = req;
181                 
182                 ret = ldb_build_search_req(&ac->search_req, ldb, ac, req->op.add.message->dn, 
183                                            LDB_SCOPE_BASE, NULL, no_attrs, req->controls, ac, 
184                                            np_part_search_callback,
185                                            req);
186                 LDB_REQ_SET_LOCATION(ac->search_req);
187                 if (ret != LDB_SUCCESS) {
188                         return ret;
189                 }
190                 
191                 return ldb_next_request(module, ac->search_req);
192         }
193
194         /* go on with the call chain */
195         return ldb_next_request(module, req);
196 }
197
198 static const struct ldb_module_ops ldb_new_partition_module_ops = {
199         .name          = "new_partition",
200         .add           = new_partition_add,
201 };
202
203 int ldb_new_partition_module_init(const char *version)
204 {
205         LDB_MODULE_CHECK_VERSION(version);
206         return ldb_register_module(&ldb_new_partition_module_ops);
207 }