r5584: add new experimental ldb module
[Samba/aatanasov.git] / source / dsdb / samdb / ldb_modules / samldb.c
blob6b8546e2b83eb8a7809c51a86d547cc2f9f37e6a
1 /*
2 ldb database library
4 Copyright (C) Simo Sorce 2004
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
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.
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.
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
26 * Name: ldb
28 * Component: ldb samldb module
30 * Description: add object timestamping functionality
32 * Author: Simo Sorce
35 #include "includes.h"
36 #include "lib/ldb/include/ldb.h"
37 #include "lib/ldb/include/ldb_private.h"
38 #include <time.h>
40 #define SAM_ACCOUNT_NAME_BASE "$000000-000000000000"
42 struct private_data {
43 const char *error_string;
46 static int samldb_search(struct ldb_module *module, const char *base,
47 enum ldb_scope scope, const char *expression,
48 const char * const *attrs, struct ldb_message ***res)
50 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_search\n");
51 return ldb_next_search(module, base, scope, expression, attrs, res);
54 static int samldb_search_free(struct ldb_module *module, struct ldb_message **res)
56 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_search_free\n");
57 return ldb_next_search_free(module, res);
60 static char *samldb_generate_samAccountName(const void *mem_ctx) {
61 char *name;
63 name = talloc_strdup(mem_ctx, SAM_ACCOUNT_NAME_BASE);
64 /* TODO: randomize name */
66 return name;
69 static BOOL samldb_get_rdn_and_basedn(const void *mem_ctx, const char *dn, char **rdn, char **basedn)
71 char *p;
73 p = strchr(dn, ',');
74 if ( ! p ) {
75 return False;
77 /* clear separator */
78 *p = '\0';
80 *rdn = talloc_strdup(mem_ctx, dn);
82 /* put back separator */
83 *p = ',';
85 if ( ! *rdn) {
86 return False;
89 *basedn = talloc_strdup(mem_ctx, p + 1);
91 if ( ! *basedn) {
92 talloc_free(*rdn);
93 *rdn = NULL;
94 return False;
97 return True;
100 /* if value is not null also check for attribute to have exactly that value */
101 static struct ldb_message_element *samldb_find_attribute(const struct ldb_message *msg, const char *name, const char *value)
103 int i, j;
105 for (i = 0; i < msg->num_elements; i++) {
106 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
107 if (!value) {
108 return &msg->elements[i];
110 for (j = 0; j < msg->elements[i].num_values; j++) {
111 if (strcasecmp(value, msg->elements[i].values[j].data) == 0) {
112 return &msg->elements[i];
118 return NULL;
121 static BOOL samldb_add_attribute(struct ldb_message *msg, const char *name, const char *value)
123 struct ldb_message_element *attr;
124 int i;
126 attr = samldb_find_attribute(msg, name, NULL);
127 if ( ! attr) {
128 msg->num_elements++;
129 msg->elements = talloc_realloc(msg, msg->elements, struct ldb_message_element, msg->num_elements);
130 if ( ! msg->elements ) {
131 return False;
133 attr = &msg->elements[msg->num_elements - 1];
135 attr->name = talloc_strdup(msg, name);
136 if ( ! attr->name ) {
137 return False;
139 attr->flags = 0;
140 attr->num_values = 0;
141 attr->values = NULL;
144 i = attr->num_values;
145 attr->num_values++;
146 attr->values = talloc_realloc(msg, attr->values, struct ldb_val, attr->num_values);
147 if ( ! attr->values ){
148 return False;
151 attr->values[i].data = talloc_strdup(msg, value);
152 attr->values[i].length = strlen(value);
154 if ( ! attr->values[i].data) {
155 return False;
158 return True;
161 static BOOL samldb_find_or_add_attribute(struct ldb_message *msg, const char *name, const char *value, const char *set_value)
163 if (samldb_find_attribute(msg, name, value) == NULL) {
164 if ( ! samldb_add_attribute(msg, name, set_value)) {
165 return False;
168 return True;
171 static struct ldb_message *samldb_manage_group_object(struct ldb_module *module, const struct ldb_message *msg)
173 struct ldb_message *msg2;
174 struct ldb_message_element *attribute;
175 char *rdn, *basedn;
176 int i;
178 if (samldb_find_attribute(msg, "objectclass", "group") == NULL) {
179 return NULL;
182 msg2 = talloc(module, struct ldb_message);
183 if (!msg2) {
184 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_group_object: talloc failed!\n");
185 return NULL;
188 /* build the new msg */
189 msg2->dn = msg->dn;
190 msg2->num_elements = msg->num_elements;
191 msg2->private_data = msg->private_data;
192 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
193 if (! msg2->elements) {
194 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_group_object: talloc_array failed!\n");
195 talloc_free(msg2);
196 return NULL;
198 for (i = 0; i < msg2->num_elements; i++) {
199 msg2->elements[i] = msg->elements[i];
202 if ( ! samldb_get_rdn_and_basedn(msg2, msg2->dn, &rdn, &basedn)) {
203 talloc_free(msg2);
204 return NULL;
206 if (strncasecmp(rdn, "cn", 2) != 0) {
207 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_group_object: Bad RDN (%s) for group!\n", rdn);
208 talloc_free(msg2);
209 return NULL;
212 if (! samldb_find_or_add_attribute(msg2, "objectclass", "top", "top")) {
213 talloc_free(msg2);
214 return NULL;
217 if ((attribute = samldb_find_attribute(msg2, "cn", NULL)) != NULL) {
218 if (strcasecmp(rdn, attribute->values[0].data) != 0) {
219 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_group_object: Bad Attribute Syntax for CN\n");
220 talloc_free(msg2);
221 return NULL;
223 } else { /* FIXME: remove this if ldb supports natively aliasing between the rdn and the "cn" attribute */
224 if ( ! samldb_add_attribute(msg2, "cn", &rdn[3])) {
225 talloc_free(msg2);
226 return NULL;
230 if ((attribute = samldb_find_attribute(msg2, "name", NULL)) != NULL) {
231 if (strcasecmp(rdn, attribute->values[0].data) != 0) {
232 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_group_object: Bad Attribute Syntax for name\n");
233 talloc_free(msg2);
234 return NULL;
236 } else { /* FIXME: remove this if ldb supports natively aliasing between the rdn and the "name" attribute */
237 if ( ! samldb_add_attribute(msg2, "name", &rdn[3])) {
238 talloc_free(msg2);
239 return NULL;
243 if ( ! samldb_find_or_add_attribute(msg2, "instanceType", NULL, "4")) {
244 return NULL;
247 if ( ! samldb_find_or_add_attribute(msg2, "sAMAccountName", NULL, samldb_generate_samAccountName(msg2))) {
248 return NULL;
251 if ( ! samldb_find_or_add_attribute(msg2, "sAMAccountType", NULL, "268435456")) {
252 return NULL;
255 if ( ! samldb_find_or_add_attribute(msg2, "groupType", NULL, "-2147483646")) {
256 return NULL;
259 if ( ! samldb_find_or_add_attribute(msg2, "objectCategory", NULL, "foo")) { /* keep the schema module happy :) */
260 return NULL;
263 if ( ! samldb_find_or_add_attribute(msg2, "objectSid", NULL, "foo")) { /* keep the schema module happy :) */
264 return NULL;
267 /* TODO: objectGUID, objectSid, objectCategory */
268 /* need a way to lock a new Sid */
270 return msg2;
273 static struct ldb_message *samldb_manage_user_object(struct ldb_module *module, const struct ldb_message *msg)
275 struct ldb_message *msg2;
276 struct ldb_message_element *attribute;
277 char *rdn, *basedn;
278 int i;
280 if (samldb_find_attribute(msg, "objectclass", "user") == NULL) {
281 return NULL;
284 msg2 = talloc(module, struct ldb_message);
285 if (!msg2) {
286 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_user_object: talloc failed!\n");
287 return NULL;
290 /* build the new msg */
291 msg2->dn = msg->dn;
292 msg2->num_elements = msg->num_elements;
293 msg2->private_data = msg->private_data;
294 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
295 if (! msg2->elements) {
296 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_user_object: talloc_array failed!\n");
297 talloc_free(msg2);
298 return NULL;
300 for (i = 0; i < msg2->num_elements; i++) {
301 msg2->elements[i] = msg->elements[i];
304 if ( ! samldb_get_rdn_and_basedn(msg2, msg2->dn, &rdn, &basedn)) {
305 talloc_free(msg2);
306 return NULL;
308 if (strncasecmp(rdn, "cn", 2) != 0) {
309 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_group_object: Bad RDN (%s) for group!\n", rdn);
310 talloc_free(msg2);
311 return NULL;
315 if ( ! samldb_find_or_add_attribute(msg2, "objectclass", "top", "top")) {
316 talloc_free(msg2);
317 return NULL;
320 if ( ! samldb_find_or_add_attribute(msg2, "objectclass", "person", "person")) {
321 talloc_free(msg2);
322 return NULL;
325 if ( ! samldb_find_or_add_attribute(msg2, "objectclass", "organizationalPerson", "organizationalPerson")) {
326 talloc_free(msg2);
327 return NULL;
330 if ((attribute = samldb_find_attribute(msg2, "cn", NULL)) != NULL) {
331 if (strcasecmp(rdn, attribute->values[0].data) != 0) {
332 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_user_object: Bad Attribute Syntax for CN\n");
333 talloc_free(msg2);
334 return NULL;
336 } else { /* FIXME: remove this if ldb supports natively aliasing between the rdn and the "cn" attribute */
337 if ( ! samldb_add_attribute(msg2, "cn", &rdn[3])) {
338 talloc_free(msg2);
339 return NULL;
343 if ((attribute = samldb_find_attribute(msg2, "name", NULL)) != NULL) {
344 if (strcasecmp(rdn, attribute->values[0].data) != 0) {
345 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_manage_user_object: Bad Attribute Syntax for name\n");
346 talloc_free(msg2);
347 return NULL;
349 } else { /* FIXME: remove this if ldb supports natively aliasing between the rdn and the "name" attribute */
350 if ( ! samldb_add_attribute(msg2, "name", &rdn[3])) {
351 talloc_free(msg2);
352 return NULL;
356 if ( ! samldb_find_or_add_attribute(msg2, "instanceType", NULL, "4")) {
357 talloc_free(msg2);
358 return NULL;
361 if ( ! samldb_find_or_add_attribute(msg2, "sAMAccountName", NULL, samldb_generate_samAccountName(msg2))) {
362 talloc_free(msg2);
363 return NULL;
366 if ( ! samldb_find_or_add_attribute(msg2, "sAMAccountType", NULL, "805306368")) {
367 talloc_free(msg2);
368 return NULL;
371 if ( ! samldb_find_or_add_attribute(msg2, "objectCategory", NULL, "foo")) { /* keep the schema module happy :) */
372 return NULL;
375 if ( ! samldb_find_or_add_attribute(msg2, "objectSid", NULL, "foo")) { /* keep the schema module happy :) */
376 return NULL;
379 /* TODO: objectGUID, objectSid, objectCategory, userAccountControl, badPwdCount, codePage, countryCode, badPasswordTime, lastLogoff, lastLogon, pwdLastSet, primaryGroupID, accountExpires, logonCount */
381 return msg2;
384 /* add_record */
385 static int samldb_add_record(struct ldb_module *module, const struct ldb_message *msg)
387 struct ldb_message *msg2 = NULL;
388 int ret;
390 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_add_record\n");
392 if (msg->dn[0] == '@') { /* do not manipulate our control entries */
393 return ldb_next_add_record(module, msg);
396 /* is group? add all group relevant missing objects */
397 msg2 = samldb_manage_group_object(module, msg);
399 /* is user? add all user relevant missing objects */
400 if ( ! msg2 ) {
401 msg2 = samldb_manage_user_object(module, msg);
404 if (msg2) {
405 ret = ldb_next_add_record(module, msg2);
406 talloc_free(msg2);
407 } else {
408 ret = ldb_next_add_record(module, msg);
411 return ret;
414 /* modify_record: change modifyTimestamp as well */
415 static int samldb_modify_record(struct ldb_module *module, const struct ldb_message *msg)
417 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_modify_record\n");
418 return ldb_next_modify_record(module, msg);
421 static int samldb_delete_record(struct ldb_module *module, const char *dn)
423 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_delete_record\n");
424 return ldb_next_delete_record(module, dn);
427 static int samldb_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
429 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_rename_record\n");
430 return ldb_next_rename_record(module, olddn, newdn);
433 static int samldb_lock(struct ldb_module *module, const char *lockname)
435 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_lock\n");
436 return ldb_next_named_lock(module, lockname);
439 static int samldb_unlock(struct ldb_module *module, const char *lockname)
441 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_unlock\n");
442 return ldb_next_named_unlock(module, lockname);
445 /* return extended error information */
446 static const char *samldb_errstring(struct ldb_module *module)
448 struct private_data *data = (struct private_data *)module->private_data;
450 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_errstring\n");
451 if (data->error_string) {
452 const char *error;
454 error = data->error_string;
455 data->error_string = NULL;
456 return error;
459 return ldb_next_errstring(module);
462 static int samldb_destructor(void *module_ctx)
464 struct ldb_module *ctx = module_ctx;
465 /* put your clean-up functions here */
466 return 0;
469 static const struct ldb_module_ops samldb_ops = {
470 "samldb",
471 samldb_search,
472 samldb_search_free,
473 samldb_add_record,
474 samldb_modify_record,
475 samldb_delete_record,
476 samldb_rename_record,
477 samldb_lock,
478 samldb_unlock,
479 samldb_errstring
483 /* the init function */
484 #ifdef HAVE_DLOPEN_DISABLED
485 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
486 #else
487 struct ldb_module *samldb_module_init(struct ldb_context *ldb, const char *options[])
488 #endif
490 struct ldb_module *ctx;
491 struct private_data *data;
493 ctx = talloc(ldb, struct ldb_module);
494 if (!ctx)
495 return NULL;
497 data = talloc(ctx, struct private_data);
498 if (!data) {
499 talloc_free(ctx);
500 return NULL;
503 data->error_string = NULL;
504 ctx->private_data = data;
505 ctx->ldb = ldb;
506 ctx->prev = ctx->next = NULL;
507 ctx->ops = &samldb_ops;
509 talloc_set_destructor(ctx, samldb_destructor);
511 return ctx;