4 Copyright (C) Simo Sorce 2004
6 * NOTICE: this module is NOT released under the GNU LGPL license as
7 * other ldb code. This module is release under the GNU GPL v2 or
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program 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
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Component: ldb samldb module
30 * Description: add embedded user/group creation functionality
36 #include "lib/ldb/include/ldb.h"
37 #include "lib/ldb/include/ldb_private.h"
38 #include "system/time.h"
39 #include "librpc/gen_ndr/ndr_security.h"
41 #define SAM_ACCOUNT_NAME_BASE "$000000-000000000000"
44 const char *error_string;
47 static int samldb_search(struct ldb_module *module, const struct ldb_dn *base,
48 enum ldb_scope scope, const char *expression,
49 const char * const *attrs, struct ldb_message ***res)
51 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_search\n");
52 return ldb_next_search(module, base, scope, expression, attrs, res);
55 static int samldb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
56 enum ldb_scope scope, struct ldb_parse_tree *tree,
57 const char * const *attrs, struct ldb_message ***res)
59 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_search\n");
60 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
64 allocate a new id, attempting to do it atomically
65 return 0 on failure, the id on success
67 static int samldb_allocate_next_rid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
68 const struct ldb_dn *dn, uint32_t *id)
70 const char * const attrs[2] = { "nextRid", NULL };
71 struct ldb_message **res = NULL;
72 struct ldb_message msg;
75 struct ldb_val vals[2];
76 struct ldb_message_element els[2];
78 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, "nextRid=*", attrs, &res);
80 if (res) talloc_free(res);
83 str = ldb_msg_find_string(res[0], "nextRid", NULL);
85 ldb_debug(ldb, LDB_DEBUG_FATAL, "attribute nextRid not found in %s\n", ldb_dn_linearize(res, dn));
90 *id = strtol(str, NULL, 0);
93 ldb_debug(ldb, LDB_DEBUG_FATAL, "Are we out of valid IDs ?\n");
99 /* we do a delete and add as a single operation. That prevents
102 msg.dn = ldb_dn_copy(mem_ctx, dn);
106 msg.num_elements = 2;
109 els[0].num_values = 1;
110 els[0].values = &vals[0];
111 els[0].flags = LDB_FLAG_MOD_DELETE;
112 els[0].name = talloc_strdup(mem_ctx, "nextRid");
117 els[1].num_values = 1;
118 els[1].values = &vals[1];
119 els[1].flags = LDB_FLAG_MOD_ADD;
120 els[1].name = els[0].name;
122 vals[0].data = talloc_asprintf(mem_ctx, "%u", *id);
126 vals[0].length = strlen(vals[0].data);
128 vals[1].data = talloc_asprintf(mem_ctx, "%u", (*id)+1);
132 vals[1].length = strlen(vals[1].data);
134 ret = ldb_modify(ldb, &msg);
144 static struct ldb_dn *samldb_search_domain(struct ldb_module *module, TALLOC_CTX *mem_ctx, const struct ldb_dn *dn)
146 TALLOC_CTX *local_ctx;
148 struct ldb_message **res = NULL;
151 local_ctx = talloc_named(mem_ctx, 0, "samldb_search_domain memory conext");
152 if (local_ctx == NULL) return NULL;
154 sdn = ldb_dn_copy(local_ctx, dn);
156 ret = ldb_search(module->ldb, sdn, LDB_SCOPE_BASE, "objectClass=domain", NULL, &res);
162 } while ((sdn = ldb_dn_get_parent(local_ctx, sdn)));
165 talloc_free(local_ctx);
169 talloc_steal(mem_ctx, sdn);
170 talloc_free(local_ctx);
175 /* search the domain related to the provided dn
176 allocate a new RID for the domain
177 return the new sid string
179 static struct dom_sid *samldb_get_new_sid(struct ldb_module *module,
180 TALLOC_CTX *mem_ctx, const struct ldb_dn *obj_dn)
182 const char * const attrs[2] = { "objectSid", NULL };
183 struct ldb_message **res = NULL;
184 const struct ldb_dn *dom_dn;
187 struct dom_sid *dom_sid, *obj_sid;
189 /* get the domain component part of the provided dn */
191 /* FIXME: quick search here, I think we should use something like
192 ldap_parse_dn here to be 100% sure we get the right domain dn */
194 /* FIXME: "dc=" is probably not utf8 safe either,
195 we need a multibyte safe substring search function here */
197 dom_dn = samldb_search_domain(module, mem_ctx, obj_dn);
198 if (dom_dn == NULL) {
199 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Invalid dn (%s) not child of a domain object!\n", ldb_dn_linearize(mem_ctx, obj_dn));
203 /* find the domain sid */
205 ret = ldb_search(module->ldb, dom_dn, LDB_SCOPE_BASE, "objectSid=*", attrs, &res);
207 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_get_new_sid: error retrieving domain sid!\n");
212 dom_sid = samdb_result_dom_sid(res, res[0], "objectSid");
213 if (dom_sid == NULL) {
214 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_get_new_sid: error retrieving domain sid!\n");
219 /* allocate a new Rid for the domain */
221 /* we need to try multiple times to cope with two account
222 creations at the same time */
224 ret = samldb_allocate_next_rid(module->ldb, mem_ctx, dom_dn, &rid);
230 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Failed to increment nextRid of %s\n", ldb_dn_linearize(mem_ctx, dom_dn));
235 /* return the new object sid */
236 obj_sid = dom_sid_add_rid(mem_ctx, dom_sid, rid);
243 static char *samldb_generate_samAccountName(const void *mem_ctx) {
246 name = talloc_strdup(mem_ctx, SAM_ACCOUNT_NAME_BASE);
247 /* TODO: randomize name */
252 /* if value is not null also check for attribute to have exactly that value */
253 static struct ldb_message_element *samldb_find_attribute(const struct ldb_message *msg, const char *name, const char *value)
257 for (i = 0; i < msg->num_elements; i++) {
258 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
260 return &msg->elements[i];
262 for (j = 0; j < msg->elements[i].num_values; j++) {
263 if (strcasecmp(value, msg->elements[i].values[j].data) == 0) {
264 return &msg->elements[i];
273 static BOOL samldb_msg_add_string(struct ldb_module *module, struct ldb_message *msg, const char *name, const char *value)
275 char *aname = talloc_strdup(msg, name);
276 char *aval = talloc_strdup(msg, value);
278 if (aname == NULL || aval == NULL) {
279 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_msg_add_string: talloc_strdup failed!\n");
283 if (ldb_msg_add_string(module->ldb, msg, aname, aval) != 0) {
290 static BOOL samldb_msg_add_sid(struct ldb_module *module, struct ldb_message *msg, const char *name, const struct dom_sid *sid)
294 status = ndr_push_struct_blob(&v, msg, sid,
295 (ndr_push_flags_fn_t)ndr_push_dom_sid);
296 if (!NT_STATUS_IS_OK(status)) {
299 return (ldb_msg_add_value(module->ldb, msg, name, &v) == 0);
302 static BOOL samldb_find_or_add_attribute(struct ldb_module *module, struct ldb_message *msg, const char *name, const char *value, const char *set_value)
304 if (samldb_find_attribute(msg, name, value) == NULL) {
305 return samldb_msg_add_string(module, msg, name, set_value);
310 static int samldb_copy_template(struct ldb_module *module, struct ldb_message *msg, const char *filter)
312 struct ldb_message **res, *t;
316 /* pull the template record */
317 ret = ldb_search(module->ldb, NULL, LDB_SCOPE_SUBTREE, filter, NULL, &res);
319 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb: ERROR: template '%s' matched %d records\n", filter, ret);
324 for (i = 0; i < t->num_elements; i++) {
325 struct ldb_message_element *el = &t->elements[i];
326 /* some elements should not be copied from the template */
327 if (strcasecmp(el->name, "cn") == 0 ||
328 strcasecmp(el->name, "name") == 0 ||
329 strcasecmp(el->name, "sAMAccountName") == 0 ||
330 strcasecmp(el->name, "objectGUID") == 0) {
333 for (j = 0; j < el->num_values; j++) {
334 if (strcasecmp(el->name, "objectClass") == 0 &&
335 (strcasecmp((char *)el->values[j].data, "Template") == 0 ||
336 strcasecmp((char *)el->values[j].data, "userTemplate") == 0 ||
337 strcasecmp((char *)el->values[j].data, "groupTemplate") == 0 ||
338 strcasecmp((char *)el->values[j].data, "foreignSecurityPrincipalTemplate") == 0 ||
339 strcasecmp((char *)el->values[j].data, "aliasTemplate") == 0 ||
340 strcasecmp((char *)el->values[j].data, "trustedDomainTemplate") == 0 ||
341 strcasecmp((char *)el->values[j].data, "secretTemplate") == 0)) {
344 if ( ! samldb_find_or_add_attribute(module, msg, el->name,
346 (char *)el->values[j].data)) {
347 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Attribute adding failed...\n");
359 static struct ldb_message *samldb_fill_group_object(struct ldb_module *module, const struct ldb_message *msg)
361 struct ldb_message *msg2;
362 struct ldb_message_element *attribute;
363 struct ldb_dn_component *rdn;
365 if (samldb_find_attribute(msg, "objectclass", "group") == NULL) {
369 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_fill_group_object\n");
371 /* build the new msg */
372 msg2 = ldb_msg_copy(module->ldb, msg);
374 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: ldb_msg_copy failed!\n");
378 if (samldb_copy_template(module, msg2, "(&(CN=TemplateGroup)(objectclass=groupTemplate))") != 0) {
379 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb_fill_group_object: Error copying template!\n");
383 if ((rdn = ldb_dn_get_rdn(msg2, msg2->dn)) == NULL) {
384 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: Bad DN (%s)!\n", ldb_dn_linearize(msg2, msg2->dn));
387 if (strcasecmp(rdn->name, "cn") != 0) {
388 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: Bad RDN (%s) for group!\n", rdn->name);
392 if ((attribute = samldb_find_attribute(msg2, "objectSid", NULL)) == NULL ) {
393 struct dom_sid *sid = samldb_get_new_sid(module, msg2, msg2->dn);
395 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: internal error! Can't generate new sid\n");
399 if (!samldb_msg_add_sid(module, msg2, "objectSid", sid)) {
406 if ( ! samldb_find_or_add_attribute(module, msg2, "sAMAccountName", NULL, samldb_generate_samAccountName(msg2))) {
410 talloc_steal(msg, msg2);
415 static struct ldb_message *samldb_fill_user_or_computer_object(struct ldb_module *module, const struct ldb_message *msg)
417 struct ldb_message *msg2;
418 struct ldb_message_element *attribute;
419 struct ldb_dn_component *rdn;
421 if ((samldb_find_attribute(msg, "objectclass", "user") == NULL) &&
422 (samldb_find_attribute(msg, "objectclass", "computer") == NULL)) {
426 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_fill_user_or_computer_object\n");
428 /* build the new msg */
429 msg2 = ldb_msg_copy(module->ldb, msg);
431 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: ldb_msg_copy failed!\n");
435 if (samldb_find_attribute(msg, "objectclass", "computer") != NULL) {
436 if (samldb_copy_template(module, msg2, "(&(CN=TemplateMemberServer)(objectclass=userTemplate))") != 0) {
437 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb_fill_user_or_computer_object: Error copying computer template!\n");
441 if (samldb_copy_template(module, msg2, "(&(CN=TemplateUser)(objectclass=userTemplate))") != 0) {
442 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb_fill_user_or_computer_object: Error copying user template!\n");
447 if ((rdn = ldb_dn_get_rdn(msg2, msg2->dn)) == NULL) {
450 if (strcasecmp(rdn->name, "cn") != 0) {
451 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_user_or_computer_object: Bad RDN (%s) for user/computer!\n", rdn->name);
455 /* if the only attribute was: "objectclass: computer", then make sure we also add "user" objectclass */
456 if ( ! samldb_find_or_add_attribute(module, msg2, "objectclass", "user", "user")) {
460 if ((attribute = samldb_find_attribute(msg2, "objectSid", NULL)) == NULL ) {
462 sid = samldb_get_new_sid(module, msg2, msg2->dn);
464 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_user_or_computer_object: internal error! Can't generate new sid\n");
468 if ( ! samldb_msg_add_sid(module, msg2, "objectSid", sid)) {
475 if ( ! samldb_find_or_add_attribute(module, msg2, "sAMAccountName", NULL, samldb_generate_samAccountName(msg2))) {
479 /* TODO: objectCategory, userAccountControl, badPwdCount, codePage, countryCode, badPasswordTime, lastLogoff, lastLogon, pwdLastSet, primaryGroupID, accountExpires, logonCount */
484 static struct ldb_message *samldb_fill_foreignSecurityPrincipal_object(struct ldb_module *module, const struct ldb_message *msg)
486 struct ldb_message *msg2;
487 struct ldb_message_element *attribute;
488 struct ldb_dn_component *rdn;
490 if (samldb_find_attribute(msg, "objectclass", "foreignSecurityPrincipal") == NULL) {
494 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_fill_foreignSecurityPrincipal_object\n");
496 /* build the new msg */
497 msg2 = ldb_msg_copy(module->ldb, msg);
499 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_foreignSecurityPrincpal_object: ldb_msg_copy failed!\n");
503 talloc_steal(msg, msg2);
505 if (samldb_copy_template(module, msg2, "(&(CN=TemplateForeignSecurityPrincipal)(objectclass=foreignSecurityPrincipalTemplate))") != 0) {
506 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb_fill_foreignSecurityPrincipal_object: Error copying template!\n");
510 if ((rdn = ldb_dn_get_rdn(msg2, msg2->dn)) == NULL) {
511 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_foreignSecurityPrincipal_object: Bad DN (%s)!\n", ldb_dn_linearize(msg2, msg2->dn));
514 if (strcasecmp(rdn->name, "cn") != 0) {
515 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_foreignSecurityPrincipal_object: Bad RDN (%s) for foreignSecurityPrincpal!\n", rdn->name);
519 if ((attribute = samldb_find_attribute(msg2, "objectSid", NULL)) == NULL ) {
520 struct dom_sid *sid = dom_sid_parse_talloc(msg2, rdn->value.data);
522 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_foreignSecurityPrincipal_object: internal error! Can't parse sid in CN\n");
526 if (!samldb_msg_add_sid(module, msg2, "objectSid", sid)) {
537 static int samldb_add_record(struct ldb_module *module, const struct ldb_message *msg)
539 struct ldb_message *msg2 = NULL;
542 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_add_record\n");
545 if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
546 return ldb_next_add_record(module, msg);
549 /* is user or computer? add all relevant missing objects */
550 msg2 = samldb_fill_user_or_computer_object(module, msg);
552 /* is group? add all relevant missing objects */
554 msg2 = samldb_fill_group_object(module, msg);
557 /* perhaps a foreignSecurityPrincipal? */
559 msg2 = samldb_fill_foreignSecurityPrincipal_object(module, msg);
563 ret = ldb_next_add_record(module, msg2);
565 ret = ldb_next_add_record(module, msg);
571 /* modify_record: change modifyTimestamp as well */
572 static int samldb_modify_record(struct ldb_module *module, const struct ldb_message *msg)
574 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_modify_record\n");
575 return ldb_next_modify_record(module, msg);
578 static int samldb_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
580 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_delete_record\n");
581 return ldb_next_delete_record(module, dn);
584 static int samldb_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
586 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_rename_record\n");
587 return ldb_next_rename_record(module, olddn, newdn);
590 static int samldb_lock(struct ldb_module *module, const char *lockname)
592 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_lock\n");
593 return ldb_next_named_lock(module, lockname);
596 static int samldb_unlock(struct ldb_module *module, const char *lockname)
598 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_unlock\n");
599 return ldb_next_named_unlock(module, lockname);
602 /* return extended error information */
603 static const char *samldb_errstring(struct ldb_module *module)
605 struct private_data *data = (struct private_data *)module->private_data;
607 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_errstring\n");
608 if (data->error_string) {
611 error = data->error_string;
612 data->error_string = NULL;
616 return ldb_next_errstring(module);
619 static int samldb_destructor(void *module_ctx)
621 /* struct ldb_module *ctx = module_ctx; */
622 /* put your clean-up functions here */
626 static const struct ldb_module_ops samldb_ops = {
628 .search = samldb_search,
629 .search_bytree = samldb_search_bytree,
630 .add_record = samldb_add_record,
631 .modify_record = samldb_modify_record,
632 .delete_record = samldb_delete_record,
633 .rename_record = samldb_rename_record,
634 .named_lock = samldb_lock,
635 .named_unlock = samldb_unlock,
636 .errstring = samldb_errstring
640 /* the init function */
641 #ifdef HAVE_DLOPEN_DISABLED
642 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
644 struct ldb_module *samldb_module_init(struct ldb_context *ldb, const char *options[])
647 struct ldb_module *ctx;
648 struct private_data *data;
650 ctx = talloc(ldb, struct ldb_module);
654 data = talloc(ctx, struct private_data);
660 data->error_string = NULL;
661 ctx->private_data = data;
663 ctx->prev = ctx->next = NULL;
664 ctx->ops = &samldb_ops;
666 talloc_set_destructor(ctx, samldb_destructor);