4 Copyright (C) Simo Sorce 2004
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 * NOTICE: this module is NOT released under the GNU LGPL license as
8 * other ldb code. This module is release under the GNU GPL v2 or
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program 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
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * Component: ldb samldb module
30 * Description: add embedded user/group creation functionality
36 #include "libcli/ldap/ldap.h"
37 #include "lib/ldb/include/ldb_errors.h"
38 #include "lib/ldb/include/ldb_private.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "libcli/security/security.h"
41 #include "librpc/gen_ndr/ndr_security.h"
42 #include "util/util_ldb.h"
44 int samldb_notice_sid(struct ldb_module *module,
45 TALLOC_CTX *mem_ctx, const struct dom_sid *sid);
47 static bool samldb_msg_add_sid(struct ldb_module *module, struct ldb_message *msg, const char *name, const struct dom_sid *sid)
50 enum ndr_err_code ndr_err;
52 ndr_err = ndr_push_struct_blob(&v, msg, sid,
53 (ndr_push_flags_fn_t)ndr_push_dom_sid);
54 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
57 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
61 allocate a new id, attempting to do it atomically
62 return 0 on failure, the id on success
64 static int samldb_set_next_rid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
65 struct ldb_dn *dn, uint32_t old_id, uint32_t new_id)
67 struct ldb_message msg;
69 struct ldb_val vals[2];
70 struct ldb_message_element els[2];
74 ldb_debug(ldb, LDB_DEBUG_FATAL, "Are we out of valid IDs ?\n");
75 return LDB_ERR_OPERATIONS_ERROR;
78 /* we do a delete and add as a single operation. That prevents
79 a race, in case we are not actually on a transaction db */
81 msg.dn = ldb_dn_copy(mem_ctx, dn);
83 return LDB_ERR_OPERATIONS_ERROR;
88 els[0].num_values = 1;
89 els[0].values = &vals[0];
90 els[0].flags = LDB_FLAG_MOD_DELETE;
91 els[0].name = talloc_strdup(mem_ctx, "nextRid");
93 return LDB_ERR_OPERATIONS_ERROR;
96 els[1].num_values = 1;
97 els[1].values = &vals[1];
98 els[1].flags = LDB_FLAG_MOD_ADD;
99 els[1].name = els[0].name;
101 vals[0].data = (uint8_t *)talloc_asprintf(mem_ctx, "%u", old_id);
103 return LDB_ERR_OPERATIONS_ERROR;
105 vals[0].length = strlen((char *)vals[0].data);
107 vals[1].data = (uint8_t *)talloc_asprintf(mem_ctx, "%u", new_id);
109 return LDB_ERR_OPERATIONS_ERROR;
111 vals[1].length = strlen((char *)vals[1].data);
113 ret = ldb_modify(ldb, &msg);
118 allocate a new id, attempting to do it atomically
119 return 0 on failure, the id on success
121 static int samldb_find_next_rid(struct ldb_module *module, TALLOC_CTX *mem_ctx,
122 struct ldb_dn *dn, uint32_t *old_rid)
124 const char * const attrs[2] = { "nextRid", NULL };
125 struct ldb_result *res = NULL;
129 ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, "nextRid=*", attrs, &res);
130 if (ret != LDB_SUCCESS) {
133 if (res->count != 1) {
135 return LDB_ERR_OPERATIONS_ERROR;
138 str = ldb_msg_find_attr_as_string(res->msgs[0], "nextRid", NULL);
140 ldb_asprintf_errstring(module->ldb,
141 "attribute nextRid not found in %s\n",
142 ldb_dn_get_linearized(dn));
144 return LDB_ERR_OPERATIONS_ERROR;
147 *old_rid = strtol(str, NULL, 0);
152 static int samldb_allocate_next_rid(struct ldb_module *module, TALLOC_CTX *mem_ctx,
153 struct ldb_dn *dn, const struct dom_sid *dom_sid,
154 struct dom_sid **new_sid)
156 struct dom_sid *obj_sid;
160 ret = samldb_find_next_rid(module, mem_ctx, dn, &old_rid);
165 /* return the new object sid */
166 obj_sid = dom_sid_add_rid(mem_ctx, dom_sid, old_rid);
168 *new_sid = dom_sid_add_rid(mem_ctx, dom_sid, old_rid + 1);
170 return LDB_ERR_OPERATIONS_ERROR;
173 ret = samldb_notice_sid(module, mem_ctx, *new_sid);
175 /* gah, there are conflicting sids.
176 * This is a critical situation it means that someone messed up with
177 * the DB and nextRid is not returning free RIDs, report an error
178 * and refuse to create any user until the problem is fixed */
179 ldb_asprintf_errstring(module->ldb,
180 "Critical Error: unconsistent DB, unable to retireve an unique RID to generate a new SID: %s",
181 ldb_errstring(module->ldb));
187 /* search the domain related to the provided dn
188 allocate a new RID for the domain
189 return the new sid string
191 static int samldb_get_new_sid(struct ldb_module *module,
192 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
193 struct ldb_dn *dom_dn,
194 struct dom_sid **sid)
196 const char * const attrs[2] = { "objectSid", NULL };
197 struct ldb_result *res = NULL;
199 struct dom_sid *dom_sid;
201 /* get the domain component part of the provided dn */
203 /* find the domain sid */
205 ret = ldb_search(module->ldb, dom_dn, LDB_SCOPE_BASE, "objectSid=*", attrs, &res);
206 if (ret != LDB_SUCCESS) {
207 ldb_asprintf_errstring(module->ldb,
208 "samldb_get_new_sid: error retrieving domain sid from %s: %s!\n",
209 ldb_dn_get_linearized(dom_dn),
210 ldb_errstring(module->ldb));
215 if (res->count != 1) {
216 ldb_asprintf_errstring(module->ldb,
217 "samldb_get_new_sid: error retrieving domain sid from %s: not found!\n",
218 ldb_dn_get_linearized(dom_dn));
220 return LDB_ERR_CONSTRAINT_VIOLATION;
223 dom_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
224 if (dom_sid == NULL) {
225 ldb_set_errstring(module->ldb, "samldb_get_new_sid: error parsing domain sid!\n");
227 return LDB_ERR_CONSTRAINT_VIOLATION;
230 /* allocate a new Rid for the domain */
231 ret = samldb_allocate_next_rid(module, mem_ctx, dom_dn, dom_sid, sid);
233 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Failed to increment nextRid of %s: %s\n", ldb_dn_get_linearized(dom_dn), ldb_errstring(module->ldb));
243 /* If we are adding new users/groups, we need to update the nextRid
244 * attribute to be 'above' all incoming users RIDs. This tries to
245 * avoid clashes in future */
247 int samldb_notice_sid(struct ldb_module *module,
248 TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
251 struct ldb_dn *dom_dn;
252 struct dom_sid *dom_sid;
253 const char *attrs[] = { NULL };
254 struct ldb_result *dom_res;
255 struct ldb_result *res;
258 /* find if this SID already exists */
259 ret = ldb_search_exp_fmt(module->ldb, mem_ctx, &res,
260 NULL, LDB_SCOPE_SUBTREE, attrs,
261 "(objectSid=%s)", ldap_encode_ndr_dom_sid(mem_ctx, sid));
262 if (ret == LDB_SUCCESS) {
263 if (res->count > 0) {
265 ldb_asprintf_errstring(module->ldb,
266 "Attempt to add record with SID %s rejected,"
267 " because this SID is already in the database",
268 dom_sid_string(mem_ctx, sid));
269 /* We have a duplicate SID, we must reject the add */
270 return LDB_ERR_CONSTRAINT_VIOLATION;
274 ldb_asprintf_errstring(module->ldb,
275 "samldb_notice_sid: error searching to see if sid %s is in use: %s\n",
276 dom_sid_string(mem_ctx, sid),
277 ldb_errstring(module->ldb));
281 dom_sid = dom_sid_dup(mem_ctx, sid);
283 return LDB_ERR_OPERATIONS_ERROR;
285 /* get the domain component part of the provided SID */
286 dom_sid->num_auths--;
288 /* find the domain DN */
289 ret = ldb_search_exp_fmt(module->ldb, mem_ctx, &dom_res,
290 NULL, LDB_SCOPE_SUBTREE, attrs,
291 "(&(objectSid=%s)(objectclass=domain))",
292 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
293 if (ret == LDB_SUCCESS) {
294 if (dom_res->count == 0) {
295 talloc_free(dom_res);
296 /* This isn't an operation on a domain we know about, so nothing to update */
300 if (dom_res->count > 1) {
301 talloc_free(dom_res);
302 ldb_asprintf_errstring(module->ldb,
303 "samldb_notice_sid: error retrieving domain from sid: duplicate (found %d) domain: %s!\n",
304 dom_res->count, dom_sid_string(dom_res, dom_sid));
305 return LDB_ERR_OPERATIONS_ERROR;
308 ldb_asprintf_errstring(module->ldb,
309 "samldb_notice_sid: error retrieving domain from sid: %s: %s\n",
310 dom_sid_string(dom_res, dom_sid),
311 ldb_errstring(module->ldb));
315 dom_dn = dom_res->msgs[0]->dn;
317 ret = samldb_find_next_rid(module, mem_ctx,
320 talloc_free(dom_res);
324 if (old_rid <= sid->sub_auths[sid->num_auths - 1]) {
325 ret = samldb_set_next_rid(module->ldb, mem_ctx, dom_dn, old_rid,
326 sid->sub_auths[sid->num_auths - 1] + 1);
328 talloc_free(dom_res);
332 static int samldb_handle_sid(struct ldb_module *module,
333 TALLOC_CTX *mem_ctx, struct ldb_message *msg2,
334 struct ldb_dn *parent_dn)
338 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg2, "objectSid");
340 ret = samldb_get_new_sid(module, msg2, msg2->dn, parent_dn, &sid);
345 if ( ! samldb_msg_add_sid(module, msg2, "objectSid", sid)) {
347 return LDB_ERR_OPERATIONS_ERROR;
352 ret = samldb_notice_sid(module, msg2, sid);
357 static int samldb_generate_samAccountName(struct ldb_module *module, TALLOC_CTX *mem_ctx,
358 struct ldb_dn *dom_dn, char **name)
360 const char *attrs[] = { NULL };
361 struct ldb_result *res;
364 /* Format: $000000-000000000000 */
367 *name = talloc_asprintf(mem_ctx, "$%.6X-%.6X%.6X", (unsigned int)random(), (unsigned int)random(), (unsigned int)random());
368 /* TODO: Figure out exactly what this is meant to conflict with */
369 ret = ldb_search_exp_fmt(module->ldb,
370 mem_ctx, &res, dom_dn, LDB_SCOPE_SUBTREE, attrs,
372 ldb_binary_encode_string(mem_ctx, *name));
373 if (ret != LDB_SUCCESS) {
374 ldb_asprintf_errstring(module->ldb, "samldb: Failure searching to determine if samAccountName %s is unique: %s",
375 *name, ldb_errstring(module->ldb));
379 if (res->count == 0) {
381 /* Great. There are no conflicting users/groups/etc */
385 /* gah, there is a conflicting name, lets move around the loop again... */
390 static int samldb_fill_group_object(struct ldb_module *module, const struct ldb_message *msg,
391 struct ldb_message **ret_msg)
395 struct ldb_message *msg2;
396 struct ldb_dn *dom_dn;
397 const char *rdn_name;
398 TALLOC_CTX *mem_ctx = talloc_new(msg);
401 return LDB_ERR_OPERATIONS_ERROR;
404 /* build the new msg */
405 msg2 = ldb_msg_copy(mem_ctx, msg);
407 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: ldb_msg_copy failed!\n");
408 talloc_free(mem_ctx);
409 return LDB_ERR_OPERATIONS_ERROR;
412 ret = samdb_copy_template(module->ldb, msg2,
417 talloc_free(mem_ctx);
421 rdn_name = ldb_dn_get_rdn_name(msg2->dn);
423 if (strcasecmp(rdn_name, "cn") != 0) {
424 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_group_object: Bad RDN (%s) for group!\n", rdn_name);
425 talloc_free(mem_ctx);
426 return LDB_ERR_CONSTRAINT_VIOLATION;
429 ret = samdb_search_for_parent_domain(module->ldb, mem_ctx, msg2->dn, &dom_dn, &errstr);
430 if (ret != LDB_SUCCESS) {
431 ldb_asprintf_errstring(module->ldb,
432 "samldb_fill_group_object: %s", errstr);
436 /* Generate a random name, if no samAccountName was supplied */
437 if (ldb_msg_find_element(msg2, "samAccountName") == NULL) {
438 ret = samldb_generate_samAccountName(module, mem_ctx, dom_dn, &name);
439 if (ret != LDB_SUCCESS) {
440 talloc_free(mem_ctx);
443 ret = samdb_find_or_add_attribute(module->ldb, msg2, "sAMAccountName", name);
445 talloc_free(mem_ctx);
450 /* Manage SID allocation, conflicts etc */
451 ret = samldb_handle_sid(module, mem_ctx, msg2, dom_dn);
453 if (ret == LDB_SUCCESS) {
454 talloc_steal(msg, msg2);
457 talloc_free(mem_ctx);
461 static int samldb_fill_user_or_computer_object(struct ldb_module *module, const struct ldb_message *msg,
462 struct ldb_message **ret_msg)
466 struct ldb_message *msg2;
467 struct ldb_dn *dom_dn;
468 const char *rdn_name;
469 TALLOC_CTX *mem_ctx = talloc_new(msg);
472 return LDB_ERR_OPERATIONS_ERROR;
475 /* build the new msg */
476 msg2 = ldb_msg_copy(mem_ctx, msg);
478 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_user_or_computer_object: ldb_msg_copy failed!\n");
479 talloc_free(mem_ctx);
480 return LDB_ERR_OPERATIONS_ERROR;
483 if (samdb_find_attribute(module->ldb, msg, "objectclass", "computer") != NULL) {
485 ret = samdb_copy_template(module->ldb, msg2,
489 ldb_asprintf_errstring(module->ldb,
490 "samldb_fill_user_or_computer_object: "
491 "Error copying computer template: %s",
493 talloc_free(mem_ctx);
497 ret = samdb_copy_template(module->ldb, msg2,
501 ldb_asprintf_errstring(module->ldb,
502 "samldb_fill_user_or_computer_object: Error copying user template: %s\n",
504 talloc_free(mem_ctx);
507 /* readd user objectclass */
508 ret = samdb_find_or_add_value(module->ldb, msg2, "objectclass", "user");
510 talloc_free(mem_ctx);
515 rdn_name = ldb_dn_get_rdn_name(msg2->dn);
517 if (strcasecmp(rdn_name, "cn") != 0) {
518 ldb_asprintf_errstring(module->ldb, "Bad RDN (%s=) for user/computer, should be CN=!\n", rdn_name);
519 talloc_free(mem_ctx);
520 return LDB_ERR_CONSTRAINT_VIOLATION;
523 ret = samdb_search_for_parent_domain(module->ldb, mem_ctx, msg2->dn, &dom_dn, &errstr);
524 if (ret != LDB_SUCCESS) {
525 ldb_asprintf_errstring(module->ldb,
526 "samldb_fill_user_or_computer_object: %s", errstr);
530 if (ldb_msg_find_element(msg2, "samAccountName") == NULL) {
531 ret = samldb_generate_samAccountName(module, mem_ctx, dom_dn, &name);
532 if (ret != LDB_SUCCESS) {
533 talloc_free(mem_ctx);
536 ret = samdb_find_or_add_attribute(module->ldb, msg2, "sAMAccountName", name);
538 talloc_free(mem_ctx);
544 TODO: useraccountcontrol: setting value 0 gives 0x200 for users
547 /* Manage SID allocation, conflicts etc */
548 ret = samldb_handle_sid(module, mem_ctx, msg2, dom_dn);
550 /* TODO: objectCategory, userAccountControl, badPwdCount, codePage, countryCode, badPasswordTime, lastLogoff, lastLogon, pwdLastSet, primaryGroupID, accountExpires, logonCount */
554 talloc_steal(msg, msg2);
556 talloc_free(mem_ctx);
560 static int samldb_fill_foreignSecurityPrincipal_object(struct ldb_module *module, const struct ldb_message *msg,
561 struct ldb_message **ret_msg)
563 struct ldb_message *msg2;
564 const char *rdn_name;
565 struct dom_sid *dom_sid;
567 const char *dom_attrs[] = { "name", NULL };
568 struct ldb_message **dom_msgs;
572 TALLOC_CTX *mem_ctx = talloc_new(msg);
574 return LDB_ERR_OPERATIONS_ERROR;
577 /* build the new msg */
578 msg2 = ldb_msg_copy(mem_ctx, msg);
580 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_foreignSecurityPrincipal_object: ldb_msg_copy failed!\n");
581 talloc_free(mem_ctx);
582 return LDB_ERR_OPERATIONS_ERROR;
585 ret = samdb_copy_template(module->ldb, msg2,
586 "ForeignSecurityPrincipal",
589 ldb_asprintf_errstring(module->ldb,
590 "samldb_fill_foreignSecurityPrincipal_object: "
591 "Error copying template: %s",
593 talloc_free(mem_ctx);
597 rdn_name = ldb_dn_get_rdn_name(msg2->dn);
599 if (strcasecmp(rdn_name, "cn") != 0) {
600 ldb_asprintf_errstring(module->ldb, "Bad RDN (%s=) for ForeignSecurityPrincipal, should be CN=!", rdn_name);
601 talloc_free(mem_ctx);
602 return LDB_ERR_CONSTRAINT_VIOLATION;
605 sid = samdb_result_dom_sid(msg2, msg, "objectSid");
607 /* Slightly different for the foreign sids. We don't want
608 * domain SIDs ending up there, it would cause all sorts of
611 sid = dom_sid_parse_talloc(msg2, (const char *)ldb_dn_get_rdn_val(msg2->dn)->data);
613 ldb_set_errstring(module->ldb, "No valid found SID in ForeignSecurityPrincipal CN!");
614 talloc_free(mem_ctx);
615 return LDB_ERR_CONSTRAINT_VIOLATION;
618 if ( ! samldb_msg_add_sid(module, msg2, "objectSid", sid)) {
620 return LDB_ERR_OPERATIONS_ERROR;
623 dom_sid = dom_sid_dup(mem_ctx, sid);
625 talloc_free(mem_ctx);
626 return LDB_ERR_OPERATIONS_ERROR;
628 /* get the domain component part of the provided SID */
629 dom_sid->num_auths--;
631 /* find the domain DN */
633 ret = gendb_search(module->ldb,
634 mem_ctx, NULL, &dom_msgs, dom_attrs,
635 "(&(objectSid=%s)(objectclass=domain))",
636 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
638 /* We don't really like the idea of foreign sids that are not foreign, but it happens */
639 const char *name = samdb_result_string(dom_msgs[0], "name", NULL);
640 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "NOTE (strange but valid): Adding foreign SID record with SID %s, but this domian (%s) is already in the database",
641 dom_sid_string(mem_ctx, sid), name);
642 } else if (ret == -1) {
643 ldb_asprintf_errstring(module->ldb,
644 "samldb_fill_foreignSecurityPrincipal_object: error searching for a domain with this sid: %s\n",
645 dom_sid_string(mem_ctx, dom_sid));
646 talloc_free(dom_msgs);
647 return LDB_ERR_OPERATIONS_ERROR;
651 /* This isn't an operation on a domain we know about, so just
652 * check for the SID, looking for duplicates via the common
654 ret = samldb_notice_sid(module, msg2, sid);
656 talloc_steal(msg, msg2);
668 * Actually this module is not async at all as it does a number of sync searches
669 * in the process. It still to be decided how to deal with it properly so it is
670 * left SYNC for now until we think of a good solution.
673 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
675 const struct ldb_message *msg = req->op.add.message;
676 struct ldb_message *msg2 = NULL;
677 struct ldb_request *down_req;
680 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_add_record\n");
682 if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
683 return ldb_next_request(module, req);
686 /* is user or computer? */
687 if ((samdb_find_attribute(module->ldb, msg, "objectclass", "user") != NULL) ||
688 (samdb_find_attribute(module->ldb, msg, "objectclass", "computer") != NULL)) {
689 /* add all relevant missing objects */
690 ret = samldb_fill_user_or_computer_object(module, msg, &msg2);
696 /* is group? add all relevant missing objects */
698 if (samdb_find_attribute(module->ldb, msg, "objectclass", "group") != NULL) {
699 ret = samldb_fill_group_object(module, msg, &msg2);
706 /* perhaps a foreignSecurityPrincipal? */
708 if (samdb_find_attribute(module->ldb, msg, "objectclass", "foreignSecurityPrincipal") != NULL) {
709 ret = samldb_fill_foreignSecurityPrincipal_object(module, msg, &msg2);
717 return ldb_next_request(module, req);
720 down_req = talloc(req, struct ldb_request);
721 if (down_req == NULL) {
722 return LDB_ERR_OPERATIONS_ERROR;
727 down_req->op.add.message = talloc_steal(down_req, msg2);
729 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
731 /* go on with the call chain */
732 ret = ldb_next_request(module, down_req);
734 /* do not free down_req as the call results may be linked to it,
735 * it will be freed when the upper level request get freed */
736 if (ret == LDB_SUCCESS) {
737 req->handle = down_req->handle;
743 static int samldb_init(struct ldb_module *module)
745 return ldb_next_init(module);
748 static const struct ldb_module_ops samldb_ops = {
750 .init_context = samldb_init,
755 int samldb_module_init(void)
757 return ldb_register_module(&samldb_ops);