r11567: Ldb API change patch.
[kai/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004
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/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40
41 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
42 {
43         int i;
44
45         for (i = 0; i < msg->num_elements; i++) {
46                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
47                         return &msg->elements[i];
48                 }
49         }
50
51         return NULL;
52 }
53
54 /* add_record: add objectGUID attribute */
55 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
56 {
57         const struct ldb_message *msg = req->op.add.message;
58         struct ldb_val v;
59         struct ldb_message *msg2;
60         struct ldb_message_element *attribute;
61         struct GUID guid;
62         NTSTATUS nt_status;
63         int ret, i;
64
65         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
66
67         if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
68                 return ldb_next_request(module, req);
69         }
70
71         if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
72                 return ldb_next_request(module, req);
73         }
74
75         msg2 = talloc(module, struct ldb_message);
76         if (!msg2) {
77                 return -1;
78         }
79
80         msg2->dn = msg->dn;
81         msg2->num_elements = msg->num_elements;
82         msg2->private_data = msg->private_data;
83         msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
84         for (i = 0; i < msg2->num_elements; i++) {
85                 msg2->elements[i] = msg->elements[i];
86         }
87
88         /* a new GUID */
89         guid = GUID_random();
90
91         nt_status = ndr_push_struct_blob(&v, msg2, &guid, 
92                                          (ndr_push_flags_fn_t)ndr_push_GUID);
93         if (!NT_STATUS_IS_OK(nt_status)) {
94                 return -1;
95         }
96
97         ret = ldb_msg_add_value(msg2, "objectGUID", &v);
98         if (ret) {
99                 return ret;
100         }
101
102         req->op.add.message = msg2;
103         ret = ldb_next_request(module, req);
104         req->op.add.message = msg;
105
106         talloc_free(msg2);
107
108         return ret;
109 }
110
111 static int objectguid_request(struct ldb_module *module, struct ldb_request *req)
112 {
113         switch (req->operation) {
114
115         case LDB_REQ_ADD:
116                 return objectguid_add(module, req);
117
118         default:
119                 return ldb_next_request(module, req);
120
121         }
122 }
123
124 static const struct ldb_module_ops objectguid_ops = {
125         .name          = "objectguid",
126         .request       = objectguid_request
127 };
128
129
130 /* the init function */
131 #ifdef HAVE_DLOPEN_DISABLED
132  struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
133 #else
134 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
135 #endif
136 {
137         struct ldb_module *ctx;
138
139         ctx = talloc(ldb, struct ldb_module);
140         if (!ctx)
141                 return NULL;
142
143         ctx->private_data = NULL;
144         ctx->ldb = ldb;
145         ctx->prev = ctx->next = NULL;
146         ctx->ops = &objectguid_ops;
147
148         return ctx;
149 }