librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
blob603370fd6220952bdef1877890bfdb9654dc6f3e
1 /*
2 SAM ldb module
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2011
7 Copyright (C) Matthieu Patou 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Name: ldb
26 * Component: ldb samldb module
28 * Description: various internal DSDB triggers - most for SAM specific objects
30 * Author: Simo Sorce
33 #include "includes.h"
34 #include "libcli/ldap/ldap_ndr.h"
35 #include "ldb_module.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/samdb/ldb_modules/util.h"
38 #include "dsdb/samdb/ldb_modules/ridalloc.h"
39 #include "libcli/security/security.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "ldb_wrap.h"
42 #include "param/param.h"
43 #include "libds/common/flag_mapping.h"
45 struct samldb_ctx;
46 enum samldb_add_type {
47 SAMLDB_TYPE_USER,
48 SAMLDB_TYPE_GROUP,
49 SAMLDB_TYPE_CLASS,
50 SAMLDB_TYPE_ATTRIBUTE
53 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
55 struct samldb_step {
56 struct samldb_step *next;
57 samldb_step_fn_t fn;
60 struct samldb_ctx {
61 struct ldb_module *module;
62 struct ldb_request *req;
64 /* used for add operations */
65 enum samldb_add_type type;
67 /* the resulting message */
68 struct ldb_message *msg;
70 /* used in "samldb_find_for_defaultObjectCategory" */
71 struct ldb_dn *dn, *res_dn;
73 /* all the async steps necessary to complete the operation */
74 struct samldb_step *steps;
75 struct samldb_step *curstep;
77 /* If someone set an ares to forward controls and response back to the caller */
78 struct ldb_reply *ares;
81 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
82 struct ldb_request *req)
84 struct ldb_context *ldb;
85 struct samldb_ctx *ac;
87 ldb = ldb_module_get_ctx(module);
89 ac = talloc_zero(req, struct samldb_ctx);
90 if (ac == NULL) {
91 ldb_oom(ldb);
92 return NULL;
95 ac->module = module;
96 ac->req = req;
98 return ac;
101 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
103 struct samldb_step *step, *stepper;
105 step = talloc_zero(ac, struct samldb_step);
106 if (step == NULL) {
107 return ldb_oom(ldb_module_get_ctx(ac->module));
110 step->fn = fn;
112 if (ac->steps == NULL) {
113 ac->steps = step;
114 ac->curstep = step;
115 } else {
116 if (ac->curstep == NULL)
117 return ldb_operr(ldb_module_get_ctx(ac->module));
118 for (stepper = ac->curstep; stepper->next != NULL;
119 stepper = stepper->next);
120 stepper->next = step;
123 return LDB_SUCCESS;
126 static int samldb_first_step(struct samldb_ctx *ac)
128 if (ac->steps == NULL) {
129 return ldb_operr(ldb_module_get_ctx(ac->module));
132 ac->curstep = ac->steps;
133 return ac->curstep->fn(ac);
136 static int samldb_next_step(struct samldb_ctx *ac)
138 if (ac->curstep->next) {
139 ac->curstep = ac->curstep->next;
140 return ac->curstep->fn(ac);
143 /* We exit the samldb module here. If someone set an "ares" to forward
144 * controls and response back to the caller, use them. */
145 if (ac->ares) {
146 return ldb_module_done(ac->req, ac->ares->controls,
147 ac->ares->response, LDB_SUCCESS);
148 } else {
149 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
154 /* sAMAccountName handling */
156 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
157 struct ldb_message *msg)
159 char *name;
161 /* Format: $000000-000000000000 */
163 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
164 (unsigned int)generate_random(),
165 (unsigned int)generate_random(),
166 (unsigned int)generate_random());
167 if (name == NULL) {
168 return ldb_oom(ldb);
170 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
173 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
175 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
176 const char *name;
177 int ret;
178 struct ldb_result *res;
179 const char * const noattrs[] = { NULL };
181 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
182 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
183 if (ret != LDB_SUCCESS) {
184 return ret;
188 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
189 if (name == NULL) {
190 /* The "sAMAccountName" cannot be nothing */
191 ldb_set_errstring(ldb,
192 "samldb: Empty account names aren't allowed!");
193 return LDB_ERR_CONSTRAINT_VIOLATION;
196 ret = dsdb_module_search(ac->module, ac, &res,
197 ldb_get_default_basedn(ldb), LDB_SCOPE_SUBTREE, noattrs,
198 DSDB_FLAG_NEXT_MODULE,
199 ac->req,
200 "(sAMAccountName=%s)",
201 ldb_binary_encode_string(ac, name));
202 if (ret != LDB_SUCCESS) {
203 return ret;
205 if (res->count != 0) {
206 ldb_asprintf_errstring(ldb,
207 "samldb: Account name (sAMAccountName) '%s' already in use!",
208 name);
209 talloc_free(res);
210 return LDB_ERR_ENTRY_ALREADY_EXISTS;
212 talloc_free(res);
214 return samldb_next_step(ac);
218 static bool samldb_msg_add_sid(struct ldb_message *msg,
219 const char *name,
220 const struct dom_sid *sid)
222 struct ldb_val v;
223 enum ndr_err_code ndr_err;
225 ndr_err = ndr_push_struct_blob(&v, msg, sid,
226 (ndr_push_flags_fn_t)ndr_push_dom_sid);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228 return false;
230 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
234 /* allocate a SID using our RID Set */
235 static int samldb_allocate_sid(struct samldb_ctx *ac)
237 uint32_t rid;
238 struct dom_sid *sid;
239 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
240 int ret;
242 ret = ridalloc_allocate_rid(ac->module, &rid, ac->req);
243 if (ret != LDB_SUCCESS) {
244 return ret;
247 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
248 if (sid == NULL) {
249 return ldb_module_oom(ac->module);
252 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
253 return ldb_operr(ldb);
256 return samldb_next_step(ac);
260 see if a krbtgt_number is available
262 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
263 uint32_t krbtgt_number)
265 TALLOC_CTX *tmp_ctx = talloc_new(ac);
266 struct ldb_result *res;
267 const char * const no_attrs[] = { NULL };
268 int ret;
270 ret = dsdb_module_search(ac->module, tmp_ctx, &res,
271 ldb_get_default_basedn(ldb_module_get_ctx(ac->module)),
272 LDB_SCOPE_SUBTREE, no_attrs,
273 DSDB_FLAG_NEXT_MODULE,
274 ac->req,
275 "(msDC-SecondaryKrbTgtNumber=%u)",
276 krbtgt_number);
277 if (ret == LDB_SUCCESS && res->count == 0) {
278 talloc_free(tmp_ctx);
279 return true;
281 talloc_free(tmp_ctx);
282 return false;
285 /* special handling for add in RODC join */
286 static int samldb_rodc_add(struct samldb_ctx *ac)
288 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
289 uint32_t krbtgt_number, i_start, i;
290 int ret;
291 char *newpass;
292 struct ldb_val newpass_utf16;
294 /* find a unused msDC-SecondaryKrbTgtNumber */
295 i_start = generate_random() & 0xFFFF;
296 if (i_start == 0) {
297 i_start = 1;
300 for (i=i_start; i<=0xFFFF; i++) {
301 if (samldb_krbtgtnumber_available(ac, i)) {
302 krbtgt_number = i;
303 goto found;
306 for (i=1; i<i_start; i++) {
307 if (samldb_krbtgtnumber_available(ac, i)) {
308 krbtgt_number = i;
309 goto found;
313 ldb_asprintf_errstring(ldb,
314 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
315 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
316 return LDB_ERR_OTHER;
318 found:
319 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
320 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
321 if (ret != LDB_SUCCESS) {
322 return ldb_operr(ldb);
325 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
326 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
327 if (ret != LDB_SUCCESS) {
328 return ldb_operr(ldb);
331 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
332 krbtgt_number);
333 if (ret != LDB_SUCCESS) {
334 return ldb_operr(ldb);
337 newpass = generate_random_password(ac->msg, 128, 255);
338 if (newpass == NULL) {
339 return ldb_operr(ldb);
342 if (!convert_string_talloc(ac,
343 CH_UNIX, CH_UTF16,
344 newpass, strlen(newpass),
345 (void *)&newpass_utf16.data,
346 &newpass_utf16.length)) {
347 ldb_asprintf_errstring(ldb,
348 "samldb_rodc_add: "
349 "failed to generate UTF16 password from random password");
350 return LDB_ERR_OPERATIONS_ERROR;
352 ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
353 if (ret != LDB_SUCCESS) {
354 return ldb_operr(ldb);
357 return samldb_next_step(ac);
360 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
362 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
363 struct ldb_result *res;
364 const char * const no_attrs[] = { NULL };
365 int ret;
367 ac->res_dn = NULL;
369 ret = dsdb_module_search(ac->module, ac, &res,
370 ac->dn, LDB_SCOPE_BASE, no_attrs,
371 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
372 | DSDB_FLAG_NEXT_MODULE,
373 ac->req,
374 "(objectClass=classSchema)");
375 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
376 /* Don't be pricky when the DN doesn't exist if we have the */
377 /* RELAX control specified */
378 if (ldb_request_get_control(ac->req,
379 LDB_CONTROL_RELAX_OID) == NULL) {
380 ldb_set_errstring(ldb,
381 "samldb_find_defaultObjectCategory: "
382 "Invalid DN for 'defaultObjectCategory'!");
383 return LDB_ERR_CONSTRAINT_VIOLATION;
386 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
387 return ret;
390 if (ret == LDB_SUCCESS) {
391 /* ensure the defaultObjectCategory has a full GUID */
392 struct ldb_message *m;
393 m = ldb_msg_new(ac->msg);
394 if (m == NULL) {
395 return ldb_oom(ldb);
397 m->dn = ac->msg->dn;
398 if (ldb_msg_add_string(m, "defaultObjectCategory",
399 ldb_dn_get_extended_linearized(m, res->msgs[0]->dn, 1)) !=
400 LDB_SUCCESS) {
401 return ldb_oom(ldb);
403 m->elements[0].flags = LDB_FLAG_MOD_REPLACE;
405 ret = dsdb_module_modify(ac->module, m,
406 DSDB_FLAG_NEXT_MODULE,
407 ac->req);
408 if (ret != LDB_SUCCESS) {
409 return ret;
414 ac->res_dn = ac->dn;
416 return samldb_next_step(ac);
420 * msDS-IntId attributeSchema attribute handling
421 * during LDB_ADD request processing
423 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
425 int ret;
426 bool id_exists;
427 uint32_t msds_intid;
428 int32_t system_flags;
429 struct ldb_context *ldb;
430 struct ldb_result *ldb_res;
431 struct ldb_dn *schema_dn;
432 struct samldb_msds_intid_persistant *msds_intid_struct;
433 struct dsdb_schema *schema;
435 ldb = ldb_module_get_ctx(ac->module);
436 schema_dn = ldb_get_schema_basedn(ldb);
438 /* replicated update should always go through */
439 if (ldb_request_get_control(ac->req,
440 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
441 return LDB_SUCCESS;
444 /* msDS-IntId is handled by system and should never be
445 * passed by clients */
446 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
447 return LDB_ERR_UNWILLING_TO_PERFORM;
450 /* do not generate msDS-IntId if Relax control is passed */
451 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
452 return LDB_SUCCESS;
455 /* check Functional Level */
456 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
457 return LDB_SUCCESS;
460 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
461 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
462 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
463 return LDB_SUCCESS;
465 schema = dsdb_get_schema(ldb, NULL);
466 if (!schema) {
467 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
468 "samldb_schema_info_update: no dsdb_schema loaded");
469 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
470 return ldb_operr(ldb);
473 msds_intid_struct = (struct samldb_msds_intid_persistant*) ldb_get_opaque(ldb, SAMLDB_MSDS_INTID_OPAQUE);
474 if (!msds_intid_struct) {
475 msds_intid_struct = talloc(ldb, struct samldb_msds_intid_persistant);
476 /* Generate new value for msDs-IntId
477 * Value should be in 0x80000000..0xBFFFFFFF range */
478 msds_intid = generate_random() % 0X3FFFFFFF;
479 msds_intid += 0x80000000;
480 msds_intid_struct->msds_intid = msds_intid;
481 msds_intid_struct->usn = schema->loaded_usn;
482 DEBUG(2, ("No samldb_msds_intid_persistant struct, allocating a new one\n"));
483 } else {
484 msds_intid = msds_intid_struct->msds_intid;
487 /* probe id values until unique one is found */
488 do {
489 uint64_t current_usn;
490 msds_intid++;
491 if (msds_intid > 0xBFFFFFFF) {
492 msds_intid = 0x80000001;
495 * Alternative strategy to a costly (even indexed search) to the
496 * database.
497 * We search in the schema if we have already this intid (using dsdb_attribute_by_attributeID_id because
498 * in the range 0x80000000 0xBFFFFFFFF, attributeID is a DSDB_ATTID_TYPE_INTID).
499 * If so generate another random value.
500 * If not check if the highest USN in the database for the schema partition is the
501 * one that we know.
502 * If so it means that's only this ldb context that is touching the schema in the database.
503 * If not it means that's someone else has modified the database while we are doing our changes too
504 * (this case should be very bery rare) in order to be sure do the search in the database.
506 if (dsdb_attribute_by_attributeID_id(schema, msds_intid)) {
507 msds_intid = generate_random() % 0X3FFFFFFF;
508 msds_intid += 0x80000000;
509 continue;
512 ret = dsdb_module_load_partition_usn(ac->module, schema->base_dn, &current_usn, NULL, NULL);
513 if (ret != LDB_SUCCESS) {
514 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
515 __location__": Searching for schema USN failed: %s\n",
516 ldb_errstring(ldb));
517 return ldb_operr(ldb);
520 /* current_usn can be lesser than msds_intid_struct-> if there is
521 * uncommited changes.
523 if (current_usn > msds_intid_struct->usn) {
524 /* oups something has changed, someone/something
525 * else is modifying or has modified the schema
526 * we'd better check this intid is the database directly
529 DEBUG(2, ("Schema has changed, searching the database for the unicity of %d\n",
530 msds_intid));
532 ret = dsdb_module_search(ac->module, ac,
533 &ldb_res,
534 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
535 DSDB_FLAG_NEXT_MODULE,
536 ac->req,
537 "(msDS-IntId=%d)", msds_intid);
538 if (ret != LDB_SUCCESS) {
539 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
540 __location__": Searching for msDS-IntId=%d failed - %s\n",
541 msds_intid,
542 ldb_errstring(ldb));
543 return ldb_operr(ldb);
545 id_exists = (ldb_res->count > 0);
546 talloc_free(ldb_res);
547 } else {
548 id_exists = 0;
551 } while(id_exists);
552 msds_intid_struct->msds_intid = msds_intid;
553 ldb_set_opaque(ldb, SAMLDB_MSDS_INTID_OPAQUE, msds_intid_struct);
555 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
556 msds_intid);
561 * samldb_add_entry (async)
564 static int samldb_add_entry_callback(struct ldb_request *req,
565 struct ldb_reply *ares)
567 struct ldb_context *ldb;
568 struct samldb_ctx *ac;
569 int ret;
571 ac = talloc_get_type(req->context, struct samldb_ctx);
572 ldb = ldb_module_get_ctx(ac->module);
574 if (!ares) {
575 return ldb_module_done(ac->req, NULL, NULL,
576 LDB_ERR_OPERATIONS_ERROR);
579 if (ares->type == LDB_REPLY_REFERRAL) {
580 return ldb_module_send_referral(ac->req, ares->referral);
583 if (ares->error != LDB_SUCCESS) {
584 return ldb_module_done(ac->req, ares->controls,
585 ares->response, ares->error);
587 if (ares->type != LDB_REPLY_DONE) {
588 ldb_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
589 return ldb_module_done(ac->req, NULL, NULL,
590 LDB_ERR_OPERATIONS_ERROR);
593 /* The caller may wish to get controls back from the add */
594 ac->ares = talloc_steal(ac, ares);
596 ret = samldb_next_step(ac);
597 if (ret != LDB_SUCCESS) {
598 return ldb_module_done(ac->req, NULL, NULL, ret);
600 return ret;
603 static int samldb_add_entry(struct samldb_ctx *ac)
605 struct ldb_context *ldb;
606 struct ldb_request *req;
607 int ret;
609 ldb = ldb_module_get_ctx(ac->module);
611 ret = ldb_build_add_req(&req, ldb, ac,
612 ac->msg,
613 ac->req->controls,
614 ac, samldb_add_entry_callback,
615 ac->req);
616 LDB_REQ_SET_LOCATION(req);
617 if (ret != LDB_SUCCESS) {
618 return ret;
621 return ldb_next_request(ac->module, req);
625 * return true if msg carries an attributeSchema that is intended to be RODC
626 * filtered but is also a system-critical attribute.
628 static bool check_rodc_critical_attribute(struct ldb_message *msg)
630 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
632 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
633 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
634 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
635 | SEARCH_FLAG_CONFIDENTIAL);
637 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
638 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
639 return true;
640 } else {
641 return false;
646 static int samldb_fill_object(struct samldb_ctx *ac)
648 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
649 int ret;
651 /* Add information for the different account types */
652 switch(ac->type) {
653 case SAMLDB_TYPE_USER: {
654 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
655 LDB_CONTROL_RODC_DCPROMO_OID);
656 if (rodc_control != NULL) {
657 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
658 rodc_control->critical = false;
659 ret = samldb_add_step(ac, samldb_rodc_add);
660 if (ret != LDB_SUCCESS) return ret;
663 /* check if we have a valid sAMAccountName */
664 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
665 if (ret != LDB_SUCCESS) return ret;
667 ret = samldb_add_step(ac, samldb_add_entry);
668 if (ret != LDB_SUCCESS) return ret;
669 break;
672 case SAMLDB_TYPE_GROUP: {
673 /* check if we have a valid sAMAccountName */
674 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
675 if (ret != LDB_SUCCESS) return ret;
677 ret = samldb_add_step(ac, samldb_add_entry);
678 if (ret != LDB_SUCCESS) return ret;
679 break;
682 case SAMLDB_TYPE_CLASS: {
683 const struct ldb_val *rdn_value, *def_obj_cat_val;
684 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "objectClassCategory", -2);
686 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
687 if (!ldb_msg_find_element(ac->msg, "subClassOf")) {
688 ret = ldb_msg_add_string(ac->msg, "subClassOf", "top");
689 if (ret != LDB_SUCCESS) return ret;
692 ret = samdb_find_or_add_attribute(ldb, ac->msg,
693 "rdnAttId", "cn");
694 if (ret != LDB_SUCCESS) return ret;
696 /* do not allow to mark an attributeSchema as RODC filtered if it
697 * is system-critical */
698 if (check_rodc_critical_attribute(ac->msg)) {
699 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
700 ldb_dn_get_linearized(ac->msg->dn));
701 return LDB_ERR_UNWILLING_TO_PERFORM;
704 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
705 if (rdn_value == NULL) {
706 return ldb_operr(ldb);
708 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
709 /* the RDN has prefix "CN" */
710 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
711 samdb_cn_to_lDAPDisplayName(ac->msg,
712 (const char *) rdn_value->data));
713 if (ret != LDB_SUCCESS) {
714 ldb_oom(ldb);
715 return ret;
719 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
720 struct GUID guid;
721 /* a new GUID */
722 guid = GUID_random();
723 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
724 if (ret != LDB_SUCCESS) {
725 ldb_oom(ldb);
726 return ret;
730 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
731 "defaultObjectCategory");
732 if (def_obj_cat_val != NULL) {
733 /* "defaultObjectCategory" has been set by the caller.
734 * Do some checks for consistency.
735 * NOTE: The real constraint check (that
736 * 'defaultObjectCategory' is the DN of the new
737 * objectclass or any parent of it) is still incomplete.
738 * For now we say that 'defaultObjectCategory' is valid
739 * if it exists and it is of objectclass "classSchema".
741 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
742 if (ac->dn == NULL) {
743 ldb_set_errstring(ldb,
744 "Invalid DN for 'defaultObjectCategory'!");
745 return LDB_ERR_CONSTRAINT_VIOLATION;
747 } else {
748 /* "defaultObjectCategory" has not been set by the
749 * caller. Use the entry DN for it. */
750 ac->dn = ac->msg->dn;
752 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
753 ldb_dn_alloc_linearized(ac->msg, ac->dn));
754 if (ret != LDB_SUCCESS) {
755 ldb_oom(ldb);
756 return ret;
760 ret = samldb_add_step(ac, samldb_add_entry);
761 if (ret != LDB_SUCCESS) return ret;
763 /* Now perform the checks for the 'defaultObjectCategory'. The
764 * lookup DN was already saved in "ac->dn" */
765 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
766 if (ret != LDB_SUCCESS) return ret;
768 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
769 if (v == -2) {
770 /* Windows 2003 does this*/
771 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "objectClassCategory", 0);
772 if (ret != LDB_SUCCESS) {
773 return ret;
776 break;
779 case SAMLDB_TYPE_ATTRIBUTE: {
780 const struct ldb_val *rdn_value;
781 struct ldb_message_element *el;
782 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
783 if (rdn_value == NULL) {
784 return ldb_operr(ldb);
786 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
787 /* the RDN has prefix "CN" */
788 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
789 samdb_cn_to_lDAPDisplayName(ac->msg,
790 (const char *) rdn_value->data));
791 if (ret != LDB_SUCCESS) {
792 ldb_oom(ldb);
793 return ret;
797 /* do not allow to mark an attributeSchema as RODC filtered if it
798 * is system-critical */
799 if (check_rodc_critical_attribute(ac->msg)) {
800 ldb_asprintf_errstring(ldb,
801 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
802 ldb_dn_get_linearized(ac->msg->dn));
803 return LDB_ERR_UNWILLING_TO_PERFORM;
806 ret = samdb_find_or_add_attribute(ldb, ac->msg,
807 "isSingleValued", "FALSE");
808 if (ret != LDB_SUCCESS) return ret;
810 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
811 struct GUID guid;
812 /* a new GUID */
813 guid = GUID_random();
814 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
815 if (ret != LDB_SUCCESS) {
816 ldb_oom(ldb);
817 return ret;
821 el = ldb_msg_find_element(ac->msg, "attributeSyntax");
822 if (el) {
824 * No need to scream if there isn't as we have code later on
825 * that will take care of it.
827 const struct dsdb_syntax *syntax = find_syntax_map_by_ad_oid((const char *)el->values[0].data);
828 if (!syntax) {
829 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
830 (const char *)el->values[0].data));
831 } else {
832 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "oMSyntax", 0);
833 const struct ldb_val *val = ldb_msg_find_ldb_val(ac->msg, "oMObjectClass");
835 if (v == 0) {
836 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "oMSyntax", syntax->oMSyntax);
837 if (ret != LDB_SUCCESS) {
838 return ret;
841 if (!val) {
842 struct ldb_val val2 = ldb_val_dup(ldb, &syntax->oMObjectClass);
843 if (val2.length > 0) {
844 ret = ldb_msg_add_value(ac->msg, "oMObjectClass", &val2, NULL);
845 if (ret != LDB_SUCCESS) {
846 return ret;
853 /* handle msDS-IntID attribute */
854 ret = samldb_add_handle_msDS_IntId(ac);
855 if (ret != LDB_SUCCESS) return ret;
857 ret = samldb_add_step(ac, samldb_add_entry);
858 if (ret != LDB_SUCCESS) return ret;
859 break;
862 default:
863 ldb_asprintf_errstring(ldb, "Invalid entry type!");
864 return LDB_ERR_OPERATIONS_ERROR;
865 break;
868 return samldb_first_step(ac);
871 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
873 struct ldb_context *ldb;
874 const struct ldb_val *rdn_value;
875 struct dom_sid *sid;
876 int ret;
878 ldb = ldb_module_get_ctx(ac->module);
880 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
881 if (sid == NULL) {
882 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
883 if (rdn_value == NULL) {
884 return ldb_operr(ldb);
886 sid = dom_sid_parse_talloc(ac->msg,
887 (const char *)rdn_value->data);
888 if (sid == NULL) {
889 ldb_set_errstring(ldb,
890 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
891 return LDB_ERR_CONSTRAINT_VIOLATION;
893 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
894 return ldb_operr(ldb);
898 /* finally proceed with adding the entry */
899 ret = samldb_add_step(ac, samldb_add_entry);
900 if (ret != LDB_SUCCESS) return ret;
902 return samldb_first_step(ac);
905 static int samldb_schema_info_update(struct samldb_ctx *ac)
907 int ret;
908 struct ldb_context *ldb;
909 struct dsdb_schema *schema;
911 /* replicated update should always go through */
912 if (ldb_request_get_control(ac->req,
913 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
914 return LDB_SUCCESS;
917 /* do not update schemaInfo during provisioning */
918 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
919 return LDB_SUCCESS;
922 ldb = ldb_module_get_ctx(ac->module);
923 schema = dsdb_get_schema(ldb, NULL);
924 if (!schema) {
925 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
926 "samldb_schema_info_update: no dsdb_schema loaded");
927 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
928 return ldb_operr(ldb);
931 ret = dsdb_module_schema_info_update(ac->module, schema,
932 DSDB_FLAG_NEXT_MODULE|
933 DSDB_FLAG_AS_SYSTEM,
934 ac->req);
935 if (ret != LDB_SUCCESS) {
936 ldb_asprintf_errstring(ldb,
937 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
938 ldb_errstring(ldb));
939 return ret;
942 return LDB_SUCCESS;
945 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid);
948 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
950 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
951 * "group" objects.
952 * ac->msg contains the "add"/"modify" message
953 * ac->type contains the object type (main objectclass)
955 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
957 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
958 void *skip_allocate_sids = ldb_get_opaque(ldb,
959 "skip_allocate_sids");
960 struct ldb_message_element *el, *el2;
961 struct dom_sid *sid;
962 int ret;
964 /* make sure that "sAMAccountType" is not specified */
965 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
966 if (el != NULL) {
967 ldb_set_errstring(ldb,
968 "samldb: sAMAccountType must not be specified!");
969 return LDB_ERR_UNWILLING_TO_PERFORM;
972 /* Step 1: objectSid assignment */
974 /* Don't allow the objectSid to be changed. But beside the RELAX
975 * control we have also to guarantee that it can always be set with
976 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
977 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
978 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
979 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
980 ldb_set_errstring(ldb,
981 "samldb: objectSid must not be specified!");
982 return LDB_ERR_UNWILLING_TO_PERFORM;
985 /* but generate a new SID when we do have an add operations */
986 if ((sid == NULL) && (ac->req->operation == LDB_ADD) && !skip_allocate_sids) {
987 ret = samldb_add_step(ac, samldb_allocate_sid);
988 if (ret != LDB_SUCCESS) return ret;
991 switch(ac->type) {
992 case SAMLDB_TYPE_USER: {
993 bool uac_generated = false, uac_add_flags = false;
995 /* Step 1.2: Default values */
996 ret = samdb_find_or_add_attribute(ldb, ac->msg,
997 "accountExpires", "9223372036854775807");
998 if (ret != LDB_SUCCESS) return ret;
999 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1000 "badPasswordTime", "0");
1001 if (ret != LDB_SUCCESS) return ret;
1002 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1003 "badPwdCount", "0");
1004 if (ret != LDB_SUCCESS) return ret;
1005 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1006 "codePage", "0");
1007 if (ret != LDB_SUCCESS) return ret;
1008 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1009 "countryCode", "0");
1010 if (ret != LDB_SUCCESS) return ret;
1011 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1012 "lastLogoff", "0");
1013 if (ret != LDB_SUCCESS) return ret;
1014 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1015 "lastLogon", "0");
1016 if (ret != LDB_SUCCESS) return ret;
1017 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1018 "logonCount", "0");
1019 if (ret != LDB_SUCCESS) return ret;
1020 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1021 "pwdLastSet", "0");
1022 if (ret != LDB_SUCCESS) return ret;
1024 /* On add operations we might need to generate a
1025 * "userAccountControl" (if it isn't specified). */
1026 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1027 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
1028 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1029 "userAccountControl",
1030 UF_NORMAL_ACCOUNT);
1031 if (ret != LDB_SUCCESS) {
1032 return ret;
1034 uac_generated = true;
1035 uac_add_flags = true;
1038 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1039 if (el != NULL) {
1040 uint32_t user_account_control, account_type;
1042 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
1043 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
1044 "userAccountControl",
1046 /* "userAccountControl" = 0 means "UF_NORMAL_ACCOUNT" */
1047 if (user_account_control == 0) {
1048 user_account_control = UF_NORMAL_ACCOUNT;
1049 uac_generated = true;
1053 * As per MS-SAMR 3.1.1.8.10 these flags have not to be set
1055 if ((user_account_control & UF_LOCKOUT) != 0) {
1056 user_account_control &= ~UF_LOCKOUT;
1057 uac_generated = true;
1059 if ((user_account_control & UF_PASSWORD_EXPIRED) != 0) {
1060 user_account_control &= ~UF_PASSWORD_EXPIRED;
1061 uac_generated = true;
1064 /* Temporary duplicate accounts aren't allowed */
1065 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1066 return LDB_ERR_OTHER;
1069 /* Workstation and (read-only) DC objects do need objectclass "computer" */
1070 if ((samdb_find_attribute(ldb, ac->msg,
1071 "objectclass", "computer") == NULL) &&
1072 (user_account_control &
1073 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
1074 ldb_set_errstring(ldb,
1075 "samldb: Requested account type does need objectclass 'computer'!");
1076 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1079 account_type = ds_uf2atype(user_account_control);
1080 if (account_type == 0) {
1081 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1082 return LDB_ERR_UNWILLING_TO_PERFORM;
1084 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1085 "sAMAccountType",
1086 account_type);
1087 if (ret != LDB_SUCCESS) {
1088 return ret;
1090 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1091 el2->flags = LDB_FLAG_MOD_REPLACE;
1093 /* "isCriticalSystemObject" might be set */
1094 if (user_account_control &
1095 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1096 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1097 "TRUE");
1098 if (ret != LDB_SUCCESS) {
1099 return ret;
1101 el2 = ldb_msg_find_element(ac->msg,
1102 "isCriticalSystemObject");
1103 el2->flags = LDB_FLAG_MOD_REPLACE;
1104 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1105 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1106 "FALSE");
1107 if (ret != LDB_SUCCESS) {
1108 return ret;
1110 el2 = ldb_msg_find_element(ac->msg,
1111 "isCriticalSystemObject");
1112 el2->flags = LDB_FLAG_MOD_REPLACE;
1115 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1116 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1117 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1120 * Older AD deployments don't know about the
1121 * RODC group
1123 if (rid == DOMAIN_RID_READONLY_DCS) {
1124 ret = samldb_prim_group_tester(ac, rid);
1125 if (ret != LDB_SUCCESS) {
1126 return ret;
1130 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1131 "primaryGroupID", rid);
1132 if (ret != LDB_SUCCESS) {
1133 return ret;
1135 el2 = ldb_msg_find_element(ac->msg,
1136 "primaryGroupID");
1137 el2->flags = LDB_FLAG_MOD_REPLACE;
1140 /* Step 1.5: Add additional flags when needed */
1141 /* Obviously this is done when the "userAccountControl"
1142 * has been generated here (tested against Windows
1143 * Server) */
1144 if (uac_generated) {
1145 if (uac_add_flags) {
1146 user_account_control |= UF_ACCOUNTDISABLE;
1147 user_account_control |= UF_PASSWD_NOTREQD;
1150 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1151 "userAccountControl",
1152 user_account_control);
1153 if (ret != LDB_SUCCESS) {
1154 return ret;
1158 break;
1161 case SAMLDB_TYPE_GROUP: {
1162 const char *tempstr;
1164 /* Step 2.2: Default values */
1165 tempstr = talloc_asprintf(ac->msg, "%d",
1166 GTYPE_SECURITY_GLOBAL_GROUP);
1167 if (tempstr == NULL) return ldb_operr(ldb);
1168 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1169 "groupType", tempstr);
1170 if (ret != LDB_SUCCESS) return ret;
1172 /* Step 2.3: "groupType" -> "sAMAccountType" */
1173 el = ldb_msg_find_element(ac->msg, "groupType");
1174 if (el != NULL) {
1175 uint32_t group_type, account_type;
1177 group_type = ldb_msg_find_attr_as_uint(ac->msg,
1178 "groupType", 0);
1180 /* The creation of builtin groups requires the
1181 * RELAX control */
1182 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
1183 if (ldb_request_get_control(ac->req,
1184 LDB_CONTROL_RELAX_OID) == NULL) {
1185 return LDB_ERR_UNWILLING_TO_PERFORM;
1189 account_type = ds_gtype2atype(group_type);
1190 if (account_type == 0) {
1191 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1192 return LDB_ERR_UNWILLING_TO_PERFORM;
1194 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1195 "sAMAccountType",
1196 account_type);
1197 if (ret != LDB_SUCCESS) {
1198 return ret;
1200 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1201 el2->flags = LDB_FLAG_MOD_REPLACE;
1203 break;
1206 default:
1207 ldb_asprintf_errstring(ldb,
1208 "Invalid entry type!");
1209 return LDB_ERR_OPERATIONS_ERROR;
1210 break;
1213 return LDB_SUCCESS;
1217 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1219 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1220 * objects.
1221 * ac->msg contains the "add"/"modify" message
1224 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid)
1226 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1227 struct dom_sid *sid;
1228 struct ldb_result *res;
1229 int ret;
1230 const char * const noattrs[] = { NULL };
1232 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1233 if (sid == NULL) {
1234 return ldb_operr(ldb);
1237 ret = dsdb_module_search(ac->module, ac, &res,
1238 ldb_get_default_basedn(ldb),
1239 LDB_SCOPE_SUBTREE,
1240 noattrs, DSDB_FLAG_NEXT_MODULE,
1241 ac->req,
1242 "(objectSid=%s)",
1243 ldap_encode_ndr_dom_sid(ac, sid));
1244 if (ret != LDB_SUCCESS) {
1245 return ret;
1247 if (res->count != 1) {
1248 talloc_free(res);
1249 ldb_asprintf_errstring(ldb,
1250 "Failed to find primary group with RID %u!",
1251 rid);
1252 return LDB_ERR_UNWILLING_TO_PERFORM;
1254 talloc_free(res);
1256 return LDB_SUCCESS;
1259 static int samldb_prim_group_set(struct samldb_ctx *ac)
1261 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1262 uint32_t rid;
1264 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1265 if (rid == (uint32_t) -1) {
1266 /* we aren't affected of any primary group set */
1267 return LDB_SUCCESS;
1269 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1270 ldb_set_errstring(ldb,
1271 "The primary group isn't settable on add operations!");
1272 return LDB_ERR_UNWILLING_TO_PERFORM;
1275 return samldb_prim_group_tester(ac, rid);
1278 static int samldb_prim_group_change(struct samldb_ctx *ac)
1280 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1281 const char * const attrs[] = { "primaryGroupID", "memberOf", NULL };
1282 struct ldb_result *res, *group_res;
1283 struct ldb_message_element *el;
1284 struct ldb_message *msg;
1285 uint32_t prev_rid, new_rid;
1286 struct dom_sid *prev_sid, *new_sid;
1287 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1288 int ret;
1289 const char * const noattrs[] = { NULL };
1291 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1292 ac->req->operation);
1293 if (el == NULL) {
1294 /* we are not affected */
1295 return LDB_SUCCESS;
1298 /* Fetch information from the existing object */
1300 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1301 DSDB_FLAG_NEXT_MODULE, ac->req);
1302 if (ret != LDB_SUCCESS) {
1303 return ret;
1306 /* Finds out the DN of the old primary group */
1308 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1309 (uint32_t) -1);
1310 if (prev_rid == (uint32_t) -1) {
1311 /* User objects do always have a mandatory "primaryGroupID"
1312 * attribute. If this doesn't exist then the object is of the
1313 * wrong type. This is the exact Windows error code */
1314 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1317 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1318 if (prev_sid == NULL) {
1319 return ldb_operr(ldb);
1322 /* Finds out the DN of the new primary group
1323 * Notice: in order to parse the primary group ID correctly we create
1324 * a temporary message here. */
1326 msg = ldb_msg_new(ac->msg);
1327 if (msg == NULL) {
1328 return ldb_module_oom(ac->module);
1330 ret = ldb_msg_add(msg, el, 0);
1331 if (ret != LDB_SUCCESS) {
1332 return ret;
1334 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1335 talloc_free(msg);
1336 if (new_rid == (uint32_t) -1) {
1337 /* we aren't affected of any primary group change */
1338 return LDB_SUCCESS;
1341 if (prev_rid == new_rid) {
1342 return LDB_SUCCESS;
1345 ret = dsdb_module_search(ac->module, ac, &group_res,
1346 ldb_get_default_basedn(ldb),
1347 LDB_SCOPE_SUBTREE,
1348 noattrs, DSDB_FLAG_NEXT_MODULE,
1349 ac->req,
1350 "(objectSid=%s)",
1351 ldap_encode_ndr_dom_sid(ac, prev_sid));
1352 if (ret != LDB_SUCCESS) {
1353 return ret;
1355 if (group_res->count != 1) {
1356 return ldb_operr(ldb);
1358 prev_prim_group_dn = group_res->msgs[0]->dn;
1360 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1361 if (new_sid == NULL) {
1362 return ldb_operr(ldb);
1365 ret = dsdb_module_search(ac->module, ac, &group_res,
1366 ldb_get_default_basedn(ldb),
1367 LDB_SCOPE_SUBTREE,
1368 noattrs, DSDB_FLAG_NEXT_MODULE,
1369 ac->req,
1370 "(objectSid=%s)",
1371 ldap_encode_ndr_dom_sid(ac, new_sid));
1372 if (ret != LDB_SUCCESS) {
1373 return ret;
1375 if (group_res->count != 1) {
1376 /* Here we know if the specified new primary group candidate is
1377 * valid or not. */
1378 return LDB_ERR_UNWILLING_TO_PERFORM;
1380 new_prim_group_dn = group_res->msgs[0]->dn;
1382 /* We need to be already a normal member of the new primary
1383 * group in order to be successful. */
1384 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1385 ldb_dn_get_linearized(new_prim_group_dn));
1386 if (el == NULL) {
1387 return LDB_ERR_UNWILLING_TO_PERFORM;
1390 /* Remove the "member" attribute on the new primary group */
1391 msg = ldb_msg_new(ac->msg);
1392 if (msg == NULL) {
1393 return ldb_module_oom(ac->module);
1395 msg->dn = new_prim_group_dn;
1397 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1398 ldb_dn_get_linearized(ac->msg->dn));
1399 if (ret != LDB_SUCCESS) {
1400 return ret;
1403 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1404 if (ret != LDB_SUCCESS) {
1405 return ret;
1407 talloc_free(msg);
1409 /* Add a "member" attribute for the previous primary group */
1410 msg = ldb_msg_new(ac->msg);
1411 if (msg == NULL) {
1412 return ldb_module_oom(ac->module);
1414 msg->dn = prev_prim_group_dn;
1416 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1417 ldb_dn_get_linearized(ac->msg->dn));
1418 if (ret != LDB_SUCCESS) {
1419 return ret;
1422 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1423 if (ret != LDB_SUCCESS) {
1424 return ret;
1426 talloc_free(msg);
1428 return LDB_SUCCESS;
1431 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1433 int ret;
1435 if (ac->req->operation == LDB_ADD) {
1436 ret = samldb_prim_group_set(ac);
1437 } else {
1438 ret = samldb_prim_group_change(ac);
1441 return ret;
1446 * This function is called on LDB modify operations. It performs some additions/
1447 * replaces on the current LDB message when "userAccountControl" changes.
1449 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1451 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1452 uint32_t user_account_control, old_user_account_control, account_type;
1453 struct ldb_message_element *el;
1454 struct ldb_message *tmp_msg;
1455 int ret;
1456 struct ldb_result *res;
1457 const char * const attrs[] = { "userAccountControl", "objectClass",
1458 "lockoutTime", NULL };
1459 unsigned int i;
1460 bool is_computer = false, uac_generated = false;
1462 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1463 ac->req->operation);
1464 if (el == NULL) {
1465 /* we are not affected */
1466 return LDB_SUCCESS;
1469 /* Create a temporary message for fetching the "userAccountControl" */
1470 tmp_msg = ldb_msg_new(ac->msg);
1471 if (tmp_msg == NULL) {
1472 return ldb_module_oom(ac->module);
1474 ret = ldb_msg_add(tmp_msg, el, 0);
1475 if (ret != LDB_SUCCESS) {
1476 return ret;
1478 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1479 "userAccountControl",
1481 talloc_free(tmp_msg);
1483 /* Temporary duplicate accounts aren't allowed */
1484 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1485 return LDB_ERR_OTHER;
1488 /* Fetch the old "userAccountControl" and "objectClass" */
1489 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1490 DSDB_FLAG_NEXT_MODULE, ac->req);
1491 if (ret != LDB_SUCCESS) {
1492 return ret;
1494 old_user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1495 if (old_user_account_control == 0) {
1496 return ldb_operr(ldb);
1498 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1499 if (el == NULL) {
1500 return ldb_operr(ldb);
1503 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1504 for (i = 0; i < el->num_values; i++) {
1505 if (ldb_attr_cmp((char *)el->values[i].data, "computer") == 0) {
1506 is_computer = true;
1507 break;
1510 if (!is_computer &&
1511 (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT))) {
1512 ldb_set_errstring(ldb,
1513 "samldb: Requested account type does need objectclass 'computer'!");
1514 return LDB_ERR_UNWILLING_TO_PERFORM;
1518 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1519 * detectors for account type changes.
1520 * So if the account type does change then we need to adjust the
1521 * "sAMAccountType", the "isCriticalSystemObject" and the
1522 * "primaryGroupID" attribute.
1524 if ((ds_uf2atype(user_account_control)
1525 == ds_uf2atype(old_user_account_control)) &&
1526 (ds_uf2prim_group_rid(user_account_control)
1527 == ds_uf2prim_group_rid(old_user_account_control))) {
1528 return LDB_SUCCESS;
1531 account_type = ds_uf2atype(user_account_control);
1532 if (account_type == 0) {
1534 * When there is no account type embedded in "userAccountControl"
1535 * fall back to default "UF_NORMAL_ACCOUNT".
1537 if (user_account_control == 0) {
1538 ldb_set_errstring(ldb,
1539 "samldb: Invalid user account control value!");
1540 return LDB_ERR_UNWILLING_TO_PERFORM;
1543 user_account_control |= UF_NORMAL_ACCOUNT;
1544 uac_generated = true;
1545 account_type = ATYPE_NORMAL_ACCOUNT;
1547 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1548 account_type);
1549 if (ret != LDB_SUCCESS) {
1550 return ret;
1552 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1553 el->flags = LDB_FLAG_MOD_REPLACE;
1555 /* As per MS-SAMR 3.1.1.8.10 these flags have not to be set */
1556 if ((user_account_control & UF_LOCKOUT) != 0) {
1557 /* "lockoutTime" reset as per MS-SAMR 3.1.1.8.10 */
1558 uint64_t lockout_time = ldb_msg_find_attr_as_uint64(res->msgs[0],
1559 "lockoutTime",
1561 if (lockout_time != 0) {
1562 ldb_msg_remove_attr(ac->msg, "lockoutTime");
1563 ret = samdb_msg_add_uint64(ldb, ac->msg, ac->msg,
1564 "lockoutTime", (NTTIME)0);
1565 if (ret != LDB_SUCCESS) {
1566 return ret;
1568 el = ldb_msg_find_element(ac->msg, "lockoutTime");
1569 el->flags = LDB_FLAG_MOD_REPLACE;
1572 user_account_control &= ~UF_LOCKOUT;
1573 uac_generated = true;
1575 if ((user_account_control & UF_PASSWORD_EXPIRED) != 0) {
1576 /* "pwdLastSet" reset as password expiration has been forced */
1577 ldb_msg_remove_attr(ac->msg, "pwdLastSet");
1578 ret = samdb_msg_add_uint64(ldb, ac->msg, ac->msg, "pwdLastSet",
1579 (NTTIME)0);
1580 if (ret != LDB_SUCCESS) {
1581 return ret;
1583 el = ldb_msg_find_element(ac->msg, "pwdLastSet");
1584 el->flags = LDB_FLAG_MOD_REPLACE;
1586 user_account_control &= ~UF_PASSWORD_EXPIRED;
1587 uac_generated = true;
1590 /* "isCriticalSystemObject" might be set/changed */
1591 if (user_account_control
1592 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1593 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1594 "TRUE");
1595 if (ret != LDB_SUCCESS) {
1596 return ret;
1598 el = ldb_msg_find_element(ac->msg,
1599 "isCriticalSystemObject");
1600 el->flags = LDB_FLAG_MOD_REPLACE;
1601 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1602 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1603 "FALSE");
1604 if (ret != LDB_SUCCESS) {
1605 return ret;
1607 el = ldb_msg_find_element(ac->msg,
1608 "isCriticalSystemObject");
1609 el->flags = LDB_FLAG_MOD_REPLACE;
1612 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1613 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1615 /* Older AD deployments don't know about the RODC group */
1616 if (rid == DOMAIN_RID_READONLY_DCS) {
1617 ret = samldb_prim_group_tester(ac, rid);
1618 if (ret != LDB_SUCCESS) {
1619 return ret;
1623 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1624 "primaryGroupID", rid);
1625 if (ret != LDB_SUCCESS) {
1626 return ret;
1628 el = ldb_msg_find_element(ac->msg,
1629 "primaryGroupID");
1630 el->flags = LDB_FLAG_MOD_REPLACE;
1633 /* Propagate eventual "userAccountControl" attribute changes */
1634 if (uac_generated) {
1635 char *tempstr = talloc_asprintf(ac->msg, "%d",
1636 user_account_control);
1637 if (tempstr == NULL) {
1638 return ldb_module_oom(ac->module);
1641 /* Overwrite "userAccountControl" correctly */
1642 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1643 ac->req->operation);
1644 el->values[0].data = (uint8_t *) tempstr;
1645 el->values[0].length = strlen(tempstr);
1648 return LDB_SUCCESS;
1651 static int samldb_group_type_change(struct samldb_ctx *ac)
1653 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1654 uint32_t group_type, old_group_type, account_type;
1655 struct ldb_message_element *el;
1656 struct ldb_message *tmp_msg;
1657 int ret;
1658 struct ldb_result *res;
1659 const char * const attrs[] = { "groupType", NULL };
1661 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
1662 ac->req->operation);
1663 if (el == NULL) {
1664 /* we are not affected */
1665 return LDB_SUCCESS;
1668 /* Create a temporary message for fetching the "groupType" */
1669 tmp_msg = ldb_msg_new(ac->msg);
1670 if (tmp_msg == NULL) {
1671 return ldb_module_oom(ac->module);
1673 ret = ldb_msg_add(tmp_msg, el, 0);
1674 if (ret != LDB_SUCCESS) {
1675 return ret;
1677 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1678 talloc_free(tmp_msg);
1680 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1681 DSDB_FLAG_NEXT_MODULE |
1682 DSDB_SEARCH_SHOW_DELETED, ac->req);
1683 if (ret != LDB_SUCCESS) {
1684 return ret;
1686 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
1687 if (old_group_type == 0) {
1688 return ldb_operr(ldb);
1691 /* Group type switching isn't so easy as it seems: We can only
1692 * change in this directions: global <-> universal <-> local
1693 * On each step also the group type itself
1694 * (security/distribution) is variable. */
1696 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
1697 switch (group_type) {
1698 case GTYPE_SECURITY_GLOBAL_GROUP:
1699 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1700 /* change to "universal" allowed */
1701 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1702 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1703 ldb_set_errstring(ldb,
1704 "samldb: Change from security/distribution local group forbidden!");
1705 return LDB_ERR_UNWILLING_TO_PERFORM;
1707 break;
1709 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1710 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1711 /* each change allowed */
1712 break;
1713 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1714 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1715 /* change to "universal" allowed */
1716 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1717 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1718 ldb_set_errstring(ldb,
1719 "samldb: Change from security/distribution global group forbidden!");
1720 return LDB_ERR_UNWILLING_TO_PERFORM;
1722 break;
1724 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1725 default:
1726 /* we don't allow this "groupType" values */
1727 return LDB_ERR_UNWILLING_TO_PERFORM;
1728 break;
1732 account_type = ds_gtype2atype(group_type);
1733 if (account_type == 0) {
1734 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1735 return LDB_ERR_UNWILLING_TO_PERFORM;
1737 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1738 account_type);
1739 if (ret != LDB_SUCCESS) {
1740 return ret;
1742 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1743 el->flags = LDB_FLAG_MOD_REPLACE;
1745 return LDB_SUCCESS;
1748 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1750 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1751 const char * const no_attrs[] = { NULL };
1752 struct ldb_result *res;
1753 const char *sam_accountname, *enc_str;
1754 struct ldb_message_element *el;
1755 struct ldb_message *tmp_msg;
1756 int ret;
1758 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1759 ac->req->operation);
1760 if (el == NULL) {
1761 /* we are not affected */
1762 return LDB_SUCCESS;
1765 /* Create a temporary message for fetching the "sAMAccountName" */
1766 tmp_msg = ldb_msg_new(ac->msg);
1767 if (tmp_msg == NULL) {
1768 return ldb_module_oom(ac->module);
1770 ret = ldb_msg_add(tmp_msg, el, 0);
1771 if (ret != LDB_SUCCESS) {
1772 return ret;
1775 /* We must not steal the original string, it belongs to the caller! */
1776 sam_accountname = talloc_strdup(ac,
1777 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1778 talloc_free(tmp_msg);
1780 if (sam_accountname == NULL) {
1781 /* The "sAMAccountName" cannot be nothing */
1782 ldb_set_errstring(ldb,
1783 "samldb: Empty account names aren't allowed!");
1784 return LDB_ERR_UNWILLING_TO_PERFORM;
1787 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1788 if (enc_str == NULL) {
1789 return ldb_module_oom(ac->module);
1792 /* Make sure that a "sAMAccountName" is only used once */
1794 ret = dsdb_module_search(ac->module, ac, &res,
1795 ldb_get_default_basedn(ldb),
1796 LDB_SCOPE_SUBTREE, no_attrs,
1797 DSDB_FLAG_NEXT_MODULE, ac->req,
1798 "(sAMAccountName=%s)", enc_str);
1799 if (ret != LDB_SUCCESS) {
1800 return ret;
1802 if (res->count > 1) {
1803 return ldb_operr(ldb);
1804 } else if (res->count == 1) {
1805 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1806 ldb_asprintf_errstring(ldb,
1807 "samldb: Account name (sAMAccountName) '%s' already in use!",
1808 sam_accountname);
1809 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1812 talloc_free(res);
1814 return LDB_SUCCESS;
1817 static int samldb_member_check(struct samldb_ctx *ac)
1819 const char * const attrs[] = { "objectSid", NULL };
1820 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1821 struct ldb_message_element *el;
1822 struct ldb_dn *member_dn;
1823 struct dom_sid *sid;
1824 struct ldb_result *res;
1825 struct dom_sid *group_sid;
1826 unsigned int i, j;
1827 int ret;
1829 /* Fetch information from the existing object */
1831 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1832 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req, NULL);
1833 if (ret != LDB_SUCCESS) {
1834 return ret;
1836 if (res->count != 1) {
1837 return ldb_operr(ldb);
1840 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1841 if (group_sid == NULL) {
1842 return ldb_operr(ldb);
1845 /* We've to walk over all modification entries and consider the "member"
1846 * ones. */
1847 for (i = 0; i < ac->msg->num_elements; i++) {
1848 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1849 continue;
1852 el = &ac->msg->elements[i];
1853 for (j = 0; j < el->num_values; j++) {
1854 struct ldb_result *group_res;
1855 const char *group_attrs[] = { "primaryGroupID" , NULL };
1856 uint32_t prim_group_rid;
1858 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
1859 /* Deletes will be handled in
1860 * repl_meta_data, and deletes not
1861 * matching a member will return
1862 * LDB_ERR_UNWILLING_TO_PERFORM
1863 * there */
1864 continue;
1867 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1868 &el->values[j]);
1869 if (!ldb_dn_validate(member_dn)) {
1870 return ldb_operr(ldb);
1873 /* Denies to add "member"s to groups which are primary
1874 * ones for them - in this case return
1875 * ERR_ENTRY_ALREADY_EXISTS. */
1877 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
1878 member_dn, group_attrs,
1879 DSDB_FLAG_NEXT_MODULE, ac->req);
1880 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1881 /* member DN doesn't exist yet */
1882 continue;
1884 if (ret != LDB_SUCCESS) {
1885 return ret;
1887 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
1888 if (prim_group_rid == (uint32_t) -1) {
1889 /* the member hasn't to be a user account ->
1890 * therefore no check needed in this case. */
1891 continue;
1894 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1895 prim_group_rid);
1896 if (sid == NULL) {
1897 return ldb_operr(ldb);
1900 if (dom_sid_equal(group_sid, sid)) {
1901 ldb_asprintf_errstring(ldb,
1902 "samldb: member %s already set via primaryGroupID %u",
1903 ldb_dn_get_linearized(member_dn), prim_group_rid);
1904 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1909 talloc_free(res);
1911 return LDB_SUCCESS;
1914 /* SAM objects have special rules regarding the "description" attribute on
1915 * modify operations. */
1916 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
1918 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1919 const char * const attrs[] = { "objectClass", "description", NULL };
1920 struct ldb_result *res;
1921 unsigned int i;
1922 int ret;
1924 /* Fetch information from the existing object */
1925 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1926 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
1927 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1928 if (ret != LDB_SUCCESS) {
1929 /* don't treat it specially ... let normal error codes
1930 happen from other places */
1931 ldb_reset_err_string(ldb);
1932 return LDB_SUCCESS;
1934 if (res->count == 0) {
1935 /* we didn't match the filter */
1936 talloc_free(res);
1937 return LDB_SUCCESS;
1940 /* We've to walk over all modification entries and consider the
1941 * "description" ones. */
1942 for (i = 0; i < ac->msg->num_elements; i++) {
1943 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
1944 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
1945 *modified = true;
1949 talloc_free(res);
1951 return LDB_SUCCESS;
1954 /* This trigger adapts the "servicePrincipalName" attributes if the
1955 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1956 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1958 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1959 struct ldb_message_element *el = NULL, *el2 = NULL;
1960 struct ldb_message *msg;
1961 const char * const attrs[] = { "servicePrincipalName", NULL };
1962 struct ldb_result *res;
1963 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1964 *sam_accountname = NULL, *old_sam_accountname = NULL;
1965 unsigned int i, j;
1966 int ret;
1968 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
1969 ac->req->operation);
1970 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1971 ac->req->operation);
1972 if ((el == NULL) && (el2 == NULL)) {
1973 /* we are not affected */
1974 return LDB_SUCCESS;
1977 /* Create a temporary message for fetching the "dNSHostName" */
1978 if (el != NULL) {
1979 const char *dns_attrs[] = { "dNSHostName", NULL };
1980 msg = ldb_msg_new(ac->msg);
1981 if (msg == NULL) {
1982 return ldb_module_oom(ac->module);
1984 ret = ldb_msg_add(msg, el, 0);
1985 if (ret != LDB_SUCCESS) {
1986 return ret;
1988 dns_hostname = talloc_strdup(ac,
1989 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1990 if (dns_hostname == NULL) {
1991 return ldb_module_oom(ac->module);
1994 talloc_free(msg);
1996 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
1997 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
1998 if (ret == LDB_SUCCESS) {
1999 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
2003 /* Create a temporary message for fetching the "sAMAccountName" */
2004 if (el2 != NULL) {
2005 char *tempstr, *tempstr2;
2006 const char *acct_attrs[] = { "sAMAccountName", NULL };
2008 msg = ldb_msg_new(ac->msg);
2009 if (msg == NULL) {
2010 return ldb_module_oom(ac->module);
2012 ret = ldb_msg_add(msg, el2, 0);
2013 if (ret != LDB_SUCCESS) {
2014 return ret;
2016 tempstr = talloc_strdup(ac,
2017 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
2018 talloc_free(msg);
2020 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
2021 DSDB_FLAG_NEXT_MODULE, ac->req);
2022 if (ret == LDB_SUCCESS) {
2023 tempstr2 = talloc_strdup(ac,
2024 ldb_msg_find_attr_as_string(res->msgs[0],
2025 "sAMAccountName", NULL));
2029 /* The "sAMAccountName" needs some additional trimming: we need
2030 * to remove the trailing "$"s if they exist. */
2031 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
2032 (tempstr[strlen(tempstr) - 1] == '$')) {
2033 tempstr[strlen(tempstr) - 1] = '\0';
2035 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
2036 (tempstr2[strlen(tempstr2) - 1] == '$')) {
2037 tempstr2[strlen(tempstr2) - 1] = '\0';
2039 sam_accountname = tempstr;
2040 old_sam_accountname = tempstr2;
2043 if (old_dns_hostname == NULL) {
2044 /* we cannot change when the old name is unknown */
2045 dns_hostname = NULL;
2047 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
2048 (strcasecmp_m(old_dns_hostname, dns_hostname) == 0)) {
2049 /* The "dNSHostName" didn't change */
2050 dns_hostname = NULL;
2053 if (old_sam_accountname == NULL) {
2054 /* we cannot change when the old name is unknown */
2055 sam_accountname = NULL;
2057 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
2058 (strcasecmp_m(old_sam_accountname, sam_accountname) == 0)) {
2059 /* The "sAMAccountName" didn't change */
2060 sam_accountname = NULL;
2063 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
2064 /* Well, there are information missing (old name(s)) or the
2065 * names didn't change. We've nothing to do and can exit here */
2066 return LDB_SUCCESS;
2069 /* Potential "servicePrincipalName" changes in the same request have to
2070 * be handled before the update (Windows behaviour). */
2071 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
2072 if (el != NULL) {
2073 msg = ldb_msg_new(ac->msg);
2074 if (msg == NULL) {
2075 return ldb_module_oom(ac->module);
2077 msg->dn = ac->msg->dn;
2079 do {
2080 ret = ldb_msg_add(msg, el, el->flags);
2081 if (ret != LDB_SUCCESS) {
2082 return ret;
2085 ldb_msg_remove_element(ac->msg, el);
2087 el = ldb_msg_find_element(ac->msg,
2088 "servicePrincipalName");
2089 } while (el != NULL);
2091 ret = dsdb_module_modify(ac->module, msg,
2092 DSDB_FLAG_NEXT_MODULE, ac->req);
2093 if (ret != LDB_SUCCESS) {
2094 return ret;
2096 talloc_free(msg);
2099 /* Fetch the "servicePrincipalName"s if any */
2100 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
2101 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
2102 if (ret != LDB_SUCCESS) {
2103 return ret;
2105 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
2106 return ldb_operr(ldb);
2109 if (res->msgs[0]->num_elements == 1) {
2111 * Yes, we do have "servicePrincipalName"s. First we update them
2112 * locally, that means we do always substitute the current
2113 * "dNSHostName" with the new one and/or "sAMAccountName"
2114 * without "$" with the new one and then we append the
2115 * modified "servicePrincipalName"s as a message element
2116 * replace to the modification request (Windows behaviour). We
2117 * need also to make sure that the values remain case-
2118 * insensitively unique.
2121 ret = ldb_msg_add_empty(ac->msg, "servicePrincipalName",
2122 LDB_FLAG_MOD_REPLACE, &el);
2123 if (ret != LDB_SUCCESS) {
2124 return ret;
2127 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
2128 char *old_str, *new_str, *pos;
2129 const char *tok;
2130 struct ldb_val *vals;
2131 bool found = false;
2133 old_str = (char *)
2134 res->msgs[0]->elements[0].values[i].data;
2136 new_str = talloc_strdup(ac->msg,
2137 strtok_r(old_str, "/", &pos));
2138 if (new_str == NULL) {
2139 return ldb_module_oom(ac->module);
2142 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
2143 if ((dns_hostname != NULL) &&
2144 (strcasecmp_m(tok, old_dns_hostname) == 0)) {
2145 tok = dns_hostname;
2147 if ((sam_accountname != NULL) &&
2148 (strcasecmp_m(tok, old_sam_accountname) == 0)) {
2149 tok = sam_accountname;
2152 new_str = talloc_asprintf(ac->msg, "%s/%s",
2153 new_str, tok);
2154 if (new_str == NULL) {
2155 return ldb_module_oom(ac->module);
2159 /* Uniqueness check */
2160 for (j = 0; (!found) && (j < el->num_values); j++) {
2161 if (strcasecmp_m((char *)el->values[j].data,
2162 new_str) == 0) {
2163 found = true;
2166 if (found) {
2167 continue;
2171 * append the new "servicePrincipalName" -
2172 * code derived from ldb_msg_add_value().
2174 * Open coded to make it clear that we must
2175 * append to the MOD_REPLACE el created above.
2177 vals = talloc_realloc(ac->msg, el->values,
2178 struct ldb_val,
2179 el->num_values + 1);
2180 if (vals == NULL) {
2181 return ldb_module_oom(ac->module);
2183 el->values = vals;
2184 el->values[el->num_values] = data_blob_string_const(new_str);
2185 ++(el->num_values);
2189 talloc_free(res);
2191 return LDB_SUCCESS;
2194 /* This checks the "fSMORoleOwner" attributes */
2195 static int samldb_fsmo_role_owner_check(struct samldb_ctx *ac)
2197 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2198 const char * const no_attrs[] = { NULL };
2199 struct ldb_message_element *el;
2200 struct ldb_message *tmp_msg;
2201 struct ldb_dn *res_dn;
2202 struct ldb_result *res;
2203 int ret;
2205 el = dsdb_get_single_valued_attr(ac->msg, "fSMORoleOwner",
2206 ac->req->operation);
2207 if (el == NULL) {
2208 /* we are not affected */
2209 return LDB_SUCCESS;
2212 /* Create a temporary message for fetching the "fSMORoleOwner" */
2213 tmp_msg = ldb_msg_new(ac->msg);
2214 if (tmp_msg == NULL) {
2215 return ldb_module_oom(ac->module);
2217 ret = ldb_msg_add(tmp_msg, el, 0);
2218 if (ret != LDB_SUCCESS) {
2219 return ret;
2221 res_dn = ldb_msg_find_attr_as_dn(ldb, ac, tmp_msg, "fSMORoleOwner");
2222 talloc_free(tmp_msg);
2224 if (res_dn == NULL) {
2225 ldb_set_errstring(ldb,
2226 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2227 if (ac->req->operation == LDB_ADD) {
2228 return LDB_ERR_CONSTRAINT_VIOLATION;
2229 } else {
2230 return LDB_ERR_UNWILLING_TO_PERFORM;
2234 /* Fetched DN has to reference a "nTDSDSA" entry */
2235 ret = dsdb_module_search(ac->module, ac, &res, res_dn, LDB_SCOPE_BASE,
2236 no_attrs,
2237 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2238 ac->req, "(objectClass=nTDSDSA)");
2239 if (ret != LDB_SUCCESS) {
2240 return ret;
2242 if (res->count != 1) {
2243 ldb_set_errstring(ldb,
2244 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2245 return LDB_ERR_UNWILLING_TO_PERFORM;
2248 talloc_free(res);
2250 return LDB_SUCCESS;
2254 /* add */
2255 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
2257 struct ldb_context *ldb;
2258 struct samldb_ctx *ac;
2259 struct ldb_message_element *el;
2260 int ret;
2262 ldb = ldb_module_get_ctx(module);
2263 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
2265 /* do not manipulate our control entries */
2266 if (ldb_dn_is_special(req->op.add.message->dn)) {
2267 return ldb_next_request(module, req);
2270 ac = samldb_ctx_init(module, req);
2271 if (ac == NULL) {
2272 return ldb_operr(ldb);
2275 /* build the new msg */
2276 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
2277 if (ac->msg == NULL) {
2278 talloc_free(ac);
2279 ldb_debug(ldb, LDB_DEBUG_FATAL,
2280 "samldb_add: ldb_msg_copy_shallow failed!\n");
2281 return ldb_operr(ldb);
2284 el = ldb_msg_find_element(ac->msg, "fSMORoleOwner");
2285 if (el != NULL) {
2286 ret = samldb_fsmo_role_owner_check(ac);
2287 if (ret != LDB_SUCCESS) {
2288 return ret;
2292 if (samdb_find_attribute(ldb, ac->msg,
2293 "objectclass", "user") != NULL) {
2294 ac->type = SAMLDB_TYPE_USER;
2296 ret = samldb_prim_group_trigger(ac);
2297 if (ret != LDB_SUCCESS) {
2298 return ret;
2301 ret = samldb_objectclass_trigger(ac);
2302 if (ret != LDB_SUCCESS) {
2303 return ret;
2306 return samldb_fill_object(ac);
2309 if (samdb_find_attribute(ldb, ac->msg,
2310 "objectclass", "group") != NULL) {
2311 ac->type = SAMLDB_TYPE_GROUP;
2313 ret = samldb_objectclass_trigger(ac);
2314 if (ret != LDB_SUCCESS) {
2315 return ret;
2318 return samldb_fill_object(ac);
2321 /* perhaps a foreignSecurityPrincipal? */
2322 if (samdb_find_attribute(ldb, ac->msg,
2323 "objectclass",
2324 "foreignSecurityPrincipal") != NULL) {
2325 return samldb_fill_foreignSecurityPrincipal_object(ac);
2328 if (samdb_find_attribute(ldb, ac->msg,
2329 "objectclass", "classSchema") != NULL) {
2330 ret = samldb_schema_info_update(ac);
2331 if (ret != LDB_SUCCESS) {
2332 talloc_free(ac);
2333 return ret;
2336 ac->type = SAMLDB_TYPE_CLASS;
2337 return samldb_fill_object(ac);
2340 if (samdb_find_attribute(ldb, ac->msg,
2341 "objectclass", "attributeSchema") != NULL) {
2342 ret = samldb_schema_info_update(ac);
2343 if (ret != LDB_SUCCESS) {
2344 talloc_free(ac);
2345 return ret;
2348 ac->type = SAMLDB_TYPE_ATTRIBUTE;
2349 return samldb_fill_object(ac);
2352 talloc_free(ac);
2354 /* nothing matched, go on */
2355 return ldb_next_request(module, req);
2358 /* modify */
2359 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2361 struct ldb_context *ldb;
2362 struct samldb_ctx *ac;
2363 struct ldb_message_element *el, *el2;
2364 bool modified = false;
2365 int ret;
2367 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2368 /* do not manipulate our control entries */
2369 return ldb_next_request(module, req);
2372 ldb = ldb_module_get_ctx(module);
2374 /* make sure that "objectSid" is not specified */
2375 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2376 if (el != NULL) {
2377 if (ldb_request_get_control(req, LDB_CONTROL_PROVISION_OID) == NULL) {
2378 ldb_set_errstring(ldb,
2379 "samldb: objectSid must not be specified!");
2380 return LDB_ERR_UNWILLING_TO_PERFORM;
2383 /* make sure that "sAMAccountType" is not specified */
2384 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2385 if (el != NULL) {
2386 ldb_set_errstring(ldb,
2387 "samldb: sAMAccountType must not be specified!");
2388 return LDB_ERR_UNWILLING_TO_PERFORM;
2390 /* make sure that "isCriticalSystemObject" is not specified */
2391 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2392 if (el != NULL) {
2393 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2394 ldb_set_errstring(ldb,
2395 "samldb: isCriticalSystemObject must not be specified!");
2396 return LDB_ERR_UNWILLING_TO_PERFORM;
2400 /* msDS-IntId is not allowed to be modified
2401 * except when modification comes from replication */
2402 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2403 if (!ldb_request_get_control(req,
2404 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2405 return LDB_ERR_CONSTRAINT_VIOLATION;
2409 ac = samldb_ctx_init(module, req);
2410 if (ac == NULL) {
2411 return ldb_operr(ldb);
2414 /* build the new msg */
2415 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2416 if (ac->msg == NULL) {
2417 talloc_free(ac);
2418 ldb_debug(ldb, LDB_DEBUG_FATAL,
2419 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2420 return ldb_operr(ldb);
2423 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2424 if (el != NULL) {
2425 ret = samldb_prim_group_trigger(ac);
2426 if (ret != LDB_SUCCESS) {
2427 return ret;
2431 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2432 if (el != NULL) {
2433 modified = true;
2434 ret = samldb_user_account_control_change(ac);
2435 if (ret != LDB_SUCCESS) {
2436 return ret;
2440 el = ldb_msg_find_element(ac->msg, "groupType");
2441 if (el != NULL) {
2442 modified = true;
2443 ret = samldb_group_type_change(ac);
2444 if (ret != LDB_SUCCESS) {
2445 return ret;
2449 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2450 if (el != NULL) {
2451 ret = samldb_sam_accountname_check(ac);
2452 if (ret != LDB_SUCCESS) {
2453 return ret;
2457 el = ldb_msg_find_element(ac->msg, "member");
2458 if (el != NULL) {
2459 ret = samldb_member_check(ac);
2460 if (ret != LDB_SUCCESS) {
2461 return ret;
2465 el = ldb_msg_find_element(ac->msg, "description");
2466 if (el != NULL) {
2467 ret = samldb_description_check(ac, &modified);
2468 if (ret != LDB_SUCCESS) {
2469 return ret;
2473 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2474 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2475 if ((el != NULL) || (el2 != NULL)) {
2476 modified = true;
2477 ret = samldb_service_principal_names_change(ac);
2478 if (ret != LDB_SUCCESS) {
2479 return ret;
2483 el = ldb_msg_find_element(ac->msg, "fSMORoleOwner");
2484 if (el != NULL) {
2485 ret = samldb_fsmo_role_owner_check(ac);
2486 if (ret != LDB_SUCCESS) {
2487 return ret;
2491 if (modified) {
2492 struct ldb_request *child_req;
2494 /* Now perform the real modifications as a child request */
2495 ret = ldb_build_mod_req(&child_req, ldb, ac,
2496 ac->msg,
2497 req->controls,
2498 req, dsdb_next_callback,
2499 req);
2500 LDB_REQ_SET_LOCATION(child_req);
2501 if (ret != LDB_SUCCESS) {
2502 return ret;
2505 return ldb_next_request(module, child_req);
2508 talloc_free(ac);
2510 /* no change which interests us, go on */
2511 return ldb_next_request(module, req);
2514 /* delete */
2516 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2518 struct ldb_context *ldb;
2519 struct dom_sid *sid;
2520 uint32_t rid;
2521 NTSTATUS status;
2522 int ret;
2523 struct ldb_result *res;
2524 const char * const attrs[] = { "objectSid", "isDeleted", NULL };
2525 const char * const noattrs[] = { NULL };
2527 ldb = ldb_module_get_ctx(ac->module);
2529 /* Finds out the SID/RID of the SAM object */
2530 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn,
2531 attrs,
2532 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2533 ac->req);
2534 if (ret != LDB_SUCCESS) {
2535 return ret;
2538 if (ldb_msg_check_string_attribute(res->msgs[0], "isDeleted", "TRUE")) {
2539 return LDB_SUCCESS;
2542 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2543 if (sid == NULL) {
2544 /* No SID - it might not be a SAM object - therefore ok */
2545 return LDB_SUCCESS;
2547 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2548 if (!NT_STATUS_IS_OK(status)) {
2549 return ldb_operr(ldb);
2551 if (rid == 0) {
2552 /* Special object (security principal?) */
2553 return LDB_SUCCESS;
2556 /* Deny delete requests from groups which are primary ones */
2557 ret = dsdb_module_search(ac->module, ac, &res,
2558 ldb_get_default_basedn(ldb),
2559 LDB_SCOPE_SUBTREE, noattrs,
2560 DSDB_FLAG_NEXT_MODULE,
2561 ac->req,
2562 "(&(primaryGroupID=%u)(objectClass=user))", rid);
2563 if (ret != LDB_SUCCESS) {
2564 return ret;
2566 if (res->count > 0) {
2567 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2570 return LDB_SUCCESS;
2573 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2575 struct samldb_ctx *ac;
2576 int ret;
2578 if (ldb_dn_is_special(req->op.del.dn)) {
2579 /* do not manipulate our control entries */
2580 return ldb_next_request(module, req);
2583 ac = samldb_ctx_init(module, req);
2584 if (ac == NULL) {
2585 return ldb_operr(ldb_module_get_ctx(module));
2588 ret = samldb_prim_group_users_check(ac);
2589 if (ret != LDB_SUCCESS) {
2590 return ret;
2593 talloc_free(ac);
2595 return ldb_next_request(module, req);
2598 /* extended */
2600 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2602 struct ldb_context *ldb = ldb_module_get_ctx(module);
2603 struct dsdb_fsmo_extended_op *exop;
2604 int ret;
2606 exop = talloc_get_type(req->op.extended.data,
2607 struct dsdb_fsmo_extended_op);
2608 if (!exop) {
2609 ldb_set_errstring(ldb,
2610 "samldb_extended_allocate_rid_pool: invalid extended data");
2611 return LDB_ERR_PROTOCOL_ERROR;
2614 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
2615 if (ret != LDB_SUCCESS) {
2616 return ret;
2619 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2622 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2624 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2625 return samldb_extended_allocate_rid_pool(module, req);
2628 return ldb_next_request(module, req);
2632 static const struct ldb_module_ops ldb_samldb_module_ops = {
2633 .name = "samldb",
2634 .add = samldb_add,
2635 .modify = samldb_modify,
2636 .del = samldb_delete,
2637 .extended = samldb_extended
2641 int ldb_samldb_module_init(const char *version)
2643 LDB_MODULE_CHECK_VERSION(version);
2644 return ldb_register_module(&ldb_samldb_module_ops);