s4-samdb: internal s4 ldb modules should be GPL not LGPL
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / instancetype.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 instancetype module
27  *
28  *  Description: add an instanceType onto every new record
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include "ldb_module.h"
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "../libds/common/flags.h"
38
39 struct it_context {
40         struct ldb_module *module;
41         struct ldb_request *req;
42 };
43
44 static int it_callback(struct ldb_request *req, struct ldb_reply *ares)
45 {
46         struct ldb_context *ldb;
47         struct it_context *ac;
48
49         ac = talloc_get_type(req->context, struct it_context);
50         ldb = ldb_module_get_ctx(ac->module);
51
52         if (!ares) {
53                 return ldb_module_done(ac->req, NULL, NULL,
54                                         LDB_ERR_OPERATIONS_ERROR);
55         }
56         if (ares->error != LDB_SUCCESS) {
57                 return ldb_module_done(ac->req, ares->controls,
58                                         ares->response, ares->error);
59         }
60
61         if (ares->type != LDB_REPLY_DONE) {
62                 ldb_set_errstring(ldb, "Invalid reply type!");
63                 return ldb_module_done(ac->req, NULL, NULL,
64                                         LDB_ERR_OPERATIONS_ERROR);
65         }
66
67         return ldb_module_done(ac->req, ares->controls,
68                                         ares->response, ares->error);
69 }
70
71 /* add_record: add instancetype attribute */
72 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
73 {
74         struct ldb_context *ldb;
75         struct ldb_request *down_req;
76         struct ldb_message *msg;
77         struct it_context *ac;
78         uint32_t instance_type;
79         int ret;
80         const struct ldb_control *partition_ctrl;
81         const struct dsdb_control_current_partition *partition;
82
83         ldb = ldb_module_get_ctx(module);
84
85         ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
86
87         /* do not manipulate our control entries */
88         if (ldb_dn_is_special(req->op.add.message->dn)) {
89                 return ldb_next_request(module, req);
90         }
91
92         partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
93         if (!partition_ctrl) {
94                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
95                               "instancetype_add: no current partition control found");
96                 return LDB_ERR_CONSTRAINT_VIOLATION;
97         }
98
99         partition = talloc_get_type(partition_ctrl->data,
100                                     struct dsdb_control_current_partition);
101         SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
102
103         ac = talloc(req, struct it_context);
104         if (ac == NULL) {
105                 return LDB_ERR_OPERATIONS_ERROR;
106         }
107         ac->module = module;
108         ac->req = req;
109
110         /* we have to copy the message as the caller might have it as a const */
111         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
112         if (msg == NULL) {
113                 ldb_oom(ldb);
114                 return LDB_ERR_OPERATIONS_ERROR;
115         }
116
117         /*
118          * TODO: calculate correct instance type
119          */
120         instance_type = INSTANCE_TYPE_WRITE;
121         if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
122                 instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
123                 if (ldb_dn_compare(msg->dn, samdb_base_dn(ldb)) != 0) {
124                         instance_type |= INSTANCE_TYPE_NC_ABOVE;
125                 }
126         }
127
128         ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
129         if (ret != LDB_SUCCESS) {
130                 ldb_oom(ldb);
131                 return LDB_ERR_OPERATIONS_ERROR;
132         }
133
134         ret = ldb_build_add_req(&down_req, ldb, ac,
135                                 msg,
136                                 req->controls,
137                                 ac, it_callback,
138                                 req);
139         if (ret != LDB_SUCCESS) {
140                 return ret;
141         }
142
143         /* go on with the call chain */
144         return ldb_next_request(module, down_req);
145 }
146
147 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
148         .name          = "instancetype",
149         .add           = instancetype_add,
150 };