Until the new ldb changes land, make ldb_wait set the error string.
[jelmer/samba4-debian.git] / source / dsdb / samdb / ldb_modules / instancetype.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2006
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      ** NOTE! The following LGPL license applies to the ldb
10      ** library. This does NOT imply that all of Samba is released
11      ** under the LGPL
12    
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 3 of the License, or (at your option) any later version.
17
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 */
26
27 /*
28  *  Name: ldb
29  *
30  *  Component: ldb instancetype module
31  *
32  *  Description: add an instanceType onto every new record
33  *
34  *  Author: Andrew Bartlett
35  */
36
37 #include "includes.h"
38 #include "ldb/include/ldb_includes.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/samdb.h"
42 #include "dsdb/common/flags.h"
43
44 /* add_record: add instancetype attribute */
45 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
46 {
47         struct ldb_request *down_req;
48         struct ldb_message *msg;
49         uint32_t instance_type;
50         int ret;
51         const struct ldb_control *partition_ctrl;
52         const struct dsdb_control_current_partition *partition;
53  
54
55         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
56
57         /* do not manipulate our control entries */
58         if (ldb_dn_is_special(req->op.add.message->dn)) {
59                 return ldb_next_request(module, req);
60         }
61
62         partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
63         if (!partition_ctrl) {
64                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
65                               "instancetype_add: no current partition control found");
66                 return LDB_ERR_CONSTRAINT_VIOLATION;
67         }
68
69         partition = talloc_get_type(partition_ctrl->data,
70                                     struct dsdb_control_current_partition);
71         SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
72
73         down_req = talloc(req, struct ldb_request);
74         if (down_req == NULL) {
75                 ldb_oom(module->ldb);
76                 return LDB_ERR_OPERATIONS_ERROR;
77         }
78
79         *down_req = *req;
80
81         /* we have to copy the message as the caller might have it as a const */
82         down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
83         if (msg == NULL) {
84                 talloc_free(down_req);
85                 ldb_oom(module->ldb);
86                 return LDB_ERR_OPERATIONS_ERROR;
87         }
88
89         /*
90          * TODO: calculate correct instance type
91          */
92         instance_type = INSTANCE_TYPE_WRITE;
93         if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
94                 instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
95                 if (ldb_dn_compare(msg->dn, samdb_base_dn(module->ldb)) != 0) {
96                         instance_type |= INSTANCE_TYPE_NC_ABOVE;
97                 }
98         }
99
100         ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
101         if (ret != LDB_SUCCESS) {
102                 talloc_free(down_req);
103                 ldb_oom(module->ldb);
104                 return LDB_ERR_OPERATIONS_ERROR;
105         }
106
107         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
108
109         /* go on with the call chain */
110         ret = ldb_next_request(module, down_req);
111
112         /* do not free down_req as the call results may be linked to it,
113          * it will be freed when the upper level request get freed */
114         if (ret == LDB_SUCCESS) {
115                 req->handle = down_req->handle;
116         }
117
118         return ret;
119 }
120
121 static const struct ldb_module_ops instancetype_ops = {
122         .name          = "instancetype",
123         .add           = instancetype_add,
124 };
125
126
127 int ldb_instancetype_init(void)
128 {
129         return ldb_register_module(&instancetype_ops);
130 }