2a27398fbc04e5424ba139438462e22b541dd23a
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb objectguid module
29  *
30  *  Description: add a unique objectGUID onto every new record
31  *
32  *  Author: Simo Sorce
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39 #include <time.h>
40
41 static int objectguid_search(struct ldb_module *module, const struct ldb_dn *base,
42                                   enum ldb_scope scope, const char *expression,
43                                   const char * const *attrs, struct ldb_message ***res)
44 {
45         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
46         return ldb_next_search(module, base, scope, expression, attrs, res);
47 }
48
49 static int objectguid_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
50                                     enum ldb_scope scope, struct ldb_parse_tree *tree,
51                                     const char * const *attrs, struct ldb_message ***res)
52 {
53         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
54         return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
55 }
56
57 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
58 {
59         int i;
60
61         for (i = 0; i < msg->num_elements; i++) {
62                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
63                         return &msg->elements[i];
64                 }
65         }
66
67         return NULL;
68 }
69
70 /* add_record: add crateTimestamp/modifyTimestamp attributes */
71 static int objectguid_add_record(struct ldb_module *module, const struct ldb_message *msg)
72 {
73         struct ldb_val v;
74         struct ldb_message *msg2;
75         struct ldb_message_element *attribute;
76         struct GUID guid;
77         NTSTATUS nt_status;
78         int ret, i;
79
80         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
81
82         if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
83                 return ldb_next_add_record(module, msg);
84         }
85
86         if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
87                 return ldb_next_add_record(module, msg);
88         }
89
90         msg2 = talloc(module, struct ldb_message);
91         if (!msg2) {
92                 return -1;
93         }
94
95         msg2->dn = msg->dn;
96         msg2->num_elements = msg->num_elements;
97         msg2->private_data = msg->private_data;
98         msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
99         for (i = 0; i < msg2->num_elements; i++) {
100                 msg2->elements[i] = msg->elements[i];
101         }
102
103         /* a new GUID */
104         guid = GUID_random();
105
106         nt_status = ndr_push_struct_blob(&v, msg2, &guid, 
107                                          (ndr_push_flags_fn_t)ndr_push_GUID);
108         if (!NT_STATUS_IS_OK(nt_status)) {
109                 return -1;
110         }
111
112         ret = ldb_msg_add_value(module->ldb, msg2, "objectGUID", &v);
113         if (ret) {
114                 return ret;
115         }
116
117         ret = ldb_next_add_record(module, msg2);
118         talloc_free(msg2);
119
120         return ret;
121 }
122
123 /* modify_record: change modifyTimestamp as well */
124 static int objectguid_modify_record(struct ldb_module *module, const struct ldb_message *msg)
125 {
126         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
127         return ldb_next_modify_record(module, msg);
128 }
129
130 static int objectguid_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
131 {
132         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_delete_record\n");
133         return ldb_next_delete_record(module, dn);
134 }
135
136 static int objectguid_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
137 {
138         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_rename_record\n");
139         return ldb_next_rename_record(module, olddn, newdn);
140 }
141
142 static int objectguid_start_trans(struct ldb_module *module)
143 {
144         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_start_trans\n");
145         return ldb_next_start_trans(module);
146 }
147
148 static int objectguid_end_trans(struct ldb_module *module, int status)
149 {
150         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_end_trans\n");
151         return ldb_next_end_trans(module, status);
152 }
153
154 static int objectguid_destructor(void *module_ctx)
155 {
156         /* struct ldb_module *ctx = module_ctx; */
157         /* put your clean-up functions here */
158         return 0;
159 }
160
161 static const struct ldb_module_ops objectguid_ops = {
162         .name          = "objectguid",
163         .search        = objectguid_search,
164         .search_bytree = objectguid_search_bytree,
165         .add_record    = objectguid_add_record,
166         .modify_record = objectguid_modify_record,
167         .delete_record = objectguid_delete_record,
168         .rename_record = objectguid_rename_record,
169         .start_transaction = objectguid_start_trans,
170         .end_transaction = objectguid_end_trans
171 };
172
173
174 /* the init function */
175 #ifdef HAVE_DLOPEN_DISABLED
176  struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
177 #else
178 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
179 #endif
180 {
181         struct ldb_module *ctx;
182
183         ctx = talloc(ldb, struct ldb_module);
184         if (!ctx)
185                 return NULL;
186
187         ctx->private_data = NULL;
188         ctx->ldb = ldb;
189         ctx->prev = ctx->next = NULL;
190         ctx->ops = &objectguid_ops;
191
192         talloc_set_destructor (ctx, objectguid_destructor);
193
194         return ctx;
195 }