r15639: fix warnings
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2006
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb objectguid module
30  *
31  *  Description: add a unique objectGUID onto every new record
32  *
33  *  Author: Simo Sorce
34  */
35
36 #include "includes.h"
37 #include "ldb/include/includes.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39
40 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
41 {
42         int i;
43
44         for (i = 0; i < msg->num_elements; i++) {
45                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
46                         return &msg->elements[i];
47                 }
48         }
49
50         return NULL;
51 }
52
53 /* add_record: add objectGUID attribute */
54 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
55 {
56         struct ldb_message *msg = req->op.add.message;
57         struct ldb_val v;
58         struct ldb_message *msg2;
59         struct ldb_message_element *attribute;
60         struct GUID guid;
61         NTSTATUS nt_status;
62         int ret, i;
63
64         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
65
66         if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
67                 return ldb_next_request(module, req);
68         }
69
70         if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
71                 return ldb_next_request(module, req);
72         }
73
74         msg2 = talloc(module, struct ldb_message);
75         if (!msg2) {
76                 return -1;
77         }
78
79         msg2->dn = msg->dn;
80         msg2->num_elements = msg->num_elements;
81         msg2->private_data = msg->private_data;
82         msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
83         for (i = 0; i < msg2->num_elements; i++) {
84                 msg2->elements[i] = msg->elements[i];
85         }
86
87         /* a new GUID */
88         guid = GUID_random();
89
90         nt_status = ndr_push_struct_blob(&v, msg2, &guid, 
91                                          (ndr_push_flags_fn_t)ndr_push_GUID);
92         if (!NT_STATUS_IS_OK(nt_status)) {
93                 return -1;
94         }
95
96         ret = ldb_msg_add_value(msg2, "objectGUID", &v);
97         if (ret) {
98                 return ret;
99         }
100
101         req->op.add.message = msg2;
102         ret = ldb_next_request(module, req);
103         req->op.add.message = msg;
104
105         talloc_free(msg2);
106
107         return ret;
108 }
109
110 static int objectguid_add_async(struct ldb_module *module, struct ldb_request *req)
111 {
112         struct ldb_request *down_req;
113         struct ldb_message_element *attribute;
114         struct ldb_message *msg;
115         struct ldb_val v;
116         struct GUID guid;
117         NTSTATUS nt_status;
118         int ret;
119
120         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
121
122         /* do not manipulate our control entries */
123         if (ldb_dn_is_special(req->op.add.message->dn)) {
124                 return ldb_next_request(module, req);
125         }
126
127         if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
128                 return ldb_next_request(module, req);
129         }
130
131         down_req = talloc(req, struct ldb_request);
132         if (down_req == NULL) {
133                 return LDB_ERR_OPERATIONS_ERROR;
134         }
135
136         /* we have to copy the message as the caller might have it as a const */
137         msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
138         if (msg == NULL) {
139                 return LDB_ERR_OPERATIONS_ERROR;
140         }
141
142         /* a new GUID */
143         guid = GUID_random();
144
145         nt_status = ndr_push_struct_blob(&v, msg, &guid, 
146                                          (ndr_push_flags_fn_t)ndr_push_GUID);
147         if (!NT_STATUS_IS_OK(nt_status)) {
148                 return -1;
149         }
150
151         ret = ldb_msg_add_value(msg, "objectGUID", &v);
152         if (ret) {
153                 return ret;
154         }
155
156         down_req->op.add.message = msg;
157         
158         down_req->controls = req->controls;
159         down_req->creds = req->creds;
160
161         down_req->async.context = req->async.context;
162         down_req->async.callback = req->async.callback;
163         down_req->async.timeout = req->async.timeout;
164
165         /* go on with the call chain */
166         ret = ldb_next_request(module, down_req);
167
168         /* do not free down_req as the call results may be linked to it,
169          * it will be freed when the upper level request get freed */
170         if (ret == LDB_SUCCESS) {
171                 req->async.handle = down_req->async.handle;
172         }
173
174         return ret;
175 }
176
177 static int objectguid_request(struct ldb_module *module, struct ldb_request *req)
178 {
179         switch (req->operation) {
180
181         case LDB_REQ_ADD:
182                 return objectguid_add(module, req);
183
184         case LDB_ASYNC_ADD:
185                 return objectguid_add_async(module, req);
186
187         default:
188                 return ldb_next_request(module, req);
189
190         }
191 }
192
193 static const struct ldb_module_ops objectguid_ops = {
194         .name          = "objectguid",
195         .request       = objectguid_request
196 };
197
198
199 int objectguid_module_init(void)
200 {
201         return ldb_register_module(&objectguid_ops);
202 }