s4:samldb LDB module - make "samldb_prim_group_users_check" synchronous again
[samba.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
1 /*
2    SAM ldb module
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5    Copyright (C) Simo Sorce  2004-2008
6    Copyright (C) Matthias Dieter Wallnöfer 2009-2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23  *  Name: ldb
24  *
25  *  Component: ldb samldb module
26  *
27  *  Description: add embedded user/group creation functionality
28  *
29  *  Author: Simo Sorce
30  */
31
32 #include "includes.h"
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "ldb_module.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37 #include "libcli/security/security.h"
38 #include "librpc/gen_ndr/ndr_security.h"
39 #include "../lib/util/util_ldb.h"
40 #include "ldb_wrap.h"
41 #include "param/param.h"
42
43 struct samldb_ctx;
44
45 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
46
47 struct samldb_step {
48         struct samldb_step *next;
49         samldb_step_fn_t fn;
50 };
51
52 struct samldb_ctx {
53         struct ldb_module *module;
54         struct ldb_request *req;
55
56         /* used for add operations */
57         const char *type;
58
59         /* the resulting message */
60         struct ldb_message *msg;
61
62         /* holds the entry SID */
63         struct dom_sid *sid;
64
65         /* holds a generic dn */
66         struct ldb_dn *dn;
67
68         /* used in conjunction with "sid" in "samldb_dn_from_sid" */
69         struct ldb_dn *res_dn;
70
71         /* used in conjunction with "dn" in "samldb_sid_from_dn" */
72         struct dom_sid *res_sid;
73
74         /* used in "samldb_user_dn_to_prim_group_rid" */
75         uint32_t prim_group_rid;
76
77         /* used in "samldb_group_add_member" and "samldb_group_del_member" */
78         struct ldb_dn *group_dn;
79         struct ldb_dn *member_dn;
80
81         /* used in "samldb_primary_group_change" */
82         struct ldb_dn *user_dn;
83         struct ldb_dn *old_prim_group_dn, *new_prim_group_dn;
84
85         /* generic counter - used in "samldb_member_check" */
86         unsigned int cnt;
87
88         /* all the async steps necessary to complete the operation */
89         struct samldb_step *steps;
90         struct samldb_step *curstep;
91
92         /* If someone set an ares to forward controls and response back to the caller */
93         struct ldb_reply *ares;
94 };
95
96 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
97                                           struct ldb_request *req)
98 {
99         struct ldb_context *ldb;
100         struct samldb_ctx *ac;
101
102         ldb = ldb_module_get_ctx(module);
103
104         ac = talloc_zero(req, struct samldb_ctx);
105         if (ac == NULL) {
106                 ldb_oom(ldb);
107                 return NULL;
108         }
109
110         ac->module = module;
111         ac->req = req;
112
113         return ac;
114 }
115
116 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
117 {
118         struct samldb_step *step, *stepper;
119
120         step = talloc_zero(ac, struct samldb_step);
121         if (step == NULL) {
122                 return LDB_ERR_OPERATIONS_ERROR;
123         }
124
125         step->fn = fn;
126
127         if (ac->steps == NULL) {
128                 ac->steps = step;
129                 ac->curstep = step;
130         } else {
131                 if (ac->curstep == NULL)
132                         return LDB_ERR_OPERATIONS_ERROR;
133                 for (stepper = ac->curstep; stepper->next != NULL;
134                         stepper = stepper->next);
135                 stepper->next = step;
136         }
137
138         return LDB_SUCCESS;
139 }
140
141 static int samldb_first_step(struct samldb_ctx *ac)
142 {
143         if (ac->steps == NULL) {
144                 return LDB_ERR_OPERATIONS_ERROR;
145         }
146
147         ac->curstep = ac->steps;
148         return ac->curstep->fn(ac);
149 }
150
151 static int samldb_next_step(struct samldb_ctx *ac)
152 {
153         if (ac->curstep->next) {
154                 ac->curstep = ac->curstep->next;
155                 return ac->curstep->fn(ac);
156         }
157
158         /* we exit the samldb module here */
159         /* If someone set an ares to forward controls and response back to the caller, use them */
160         if (ac->ares) {
161                 return ldb_module_done(ac->req, ac->ares->controls,
162                                        ac->ares->response, LDB_SUCCESS);
163         } else {
164                 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
165         }
166 }
167
168 static int samldb_generate_samAccountName(struct ldb_message *msg)
169 {
170         char *name;
171
172         /* Format: $000000-000000000000 */
173
174         name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
175                                 (unsigned int)generate_random(),
176                                 (unsigned int)generate_random(),
177                                 (unsigned int)generate_random());
178         if (name == NULL) {
179                 return LDB_ERR_OPERATIONS_ERROR;
180         }
181         return ldb_msg_add_steal_string(msg, "samAccountName", name);
182 }
183
184 /*
185  * samldb_check_samAccountName (async)
186  */
187
188 static int samldb_check_samAccountName_callback(struct ldb_request *req,
189                                                 struct ldb_reply *ares)
190 {
191         struct samldb_ctx *ac;
192         int ret;
193
194         ac = talloc_get_type(req->context, struct samldb_ctx);
195
196         if (ares->error != LDB_SUCCESS) {
197                 return ldb_module_done(ac->req, ares->controls,
198                                        ares->response, ares->error);
199         }
200
201         switch (ares->type) {
202         case LDB_REPLY_ENTRY:
203                 /* if we get an entry it means this samAccountName
204                  * already exists */
205                 return ldb_module_done(ac->req, NULL, NULL,
206                                        LDB_ERR_ENTRY_ALREADY_EXISTS);
207
208         case LDB_REPLY_REFERRAL:
209                 /* ignore */
210                 talloc_free(ares);
211                 ret = LDB_SUCCESS;
212                 break;
213
214         case LDB_REPLY_DONE:
215                 /* not found, go on */
216                 talloc_free(ares);
217                 ret = samldb_next_step(ac);
218                 break;
219         }
220
221         if (ret != LDB_SUCCESS) {
222                 return ldb_module_done(ac->req, NULL, NULL, ret);
223         }
224
225         return LDB_SUCCESS;
226 }
227
228 static int samldb_check_samAccountName(struct samldb_ctx *ac)
229 {
230         struct ldb_context *ldb;
231         struct ldb_request *req;
232         const char *name;
233         char *filter;
234         int ret;
235
236         ldb = ldb_module_get_ctx(ac->module);
237
238         if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
239                 ret = samldb_generate_samAccountName(ac->msg);
240                 if (ret != LDB_SUCCESS) {
241                         return ret;
242                 }
243         }
244
245         name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
246         if (name == NULL) {
247                 return LDB_ERR_OPERATIONS_ERROR;
248         }
249         filter = talloc_asprintf(ac, "samAccountName=%s",
250                                  ldb_binary_encode_string(ac, name));
251         if (filter == NULL) {
252                 return LDB_ERR_OPERATIONS_ERROR;
253         }
254
255         ret = ldb_build_search_req(&req, ldb, ac,
256                                    ldb_get_default_basedn(ldb),
257                                    LDB_SCOPE_SUBTREE,
258                                    filter, NULL,
259                                    NULL,
260                                    ac, samldb_check_samAccountName_callback,
261                                    ac->req);
262         talloc_free(filter);
263         if (ret != LDB_SUCCESS) {
264                 return ret;
265         }
266         return ldb_next_request(ac->module, req);
267 }
268
269
270 static int samldb_check_samAccountType(struct samldb_ctx *ac)
271 {
272         struct ldb_context *ldb;
273         unsigned int account_type;
274         unsigned int group_type;
275         unsigned int uac;
276         int ret;
277
278         ldb = ldb_module_get_ctx(ac->module);
279
280         /* make sure sAMAccountType is not specified */
281         if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
282                 ldb_asprintf_errstring(ldb,
283                         "sAMAccountType must not be specified!");
284                 return LDB_ERR_UNWILLING_TO_PERFORM;
285         }
286
287         if (strcmp("user", ac->type) == 0) {
288                 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
289                 if (uac == 0) {
290                         ldb_asprintf_errstring(ldb,
291                                 "userAccountControl invalid!");
292                         return LDB_ERR_UNWILLING_TO_PERFORM;
293                 } else {
294                         account_type = ds_uf2atype(uac);
295                         ret = samdb_msg_add_uint(ldb,
296                                                  ac->msg, ac->msg,
297                                                  "sAMAccountType",
298                                                  account_type);
299                         if (ret != LDB_SUCCESS) {
300                                 return ret;
301                         }
302                 }
303         } else
304         if (strcmp("group", ac->type) == 0) {
305
306                 group_type = samdb_result_uint(ac->msg, "groupType", 0);
307                 if (group_type == 0) {
308                         ldb_asprintf_errstring(ldb,
309                                 "groupType invalid!\n");
310                         return LDB_ERR_UNWILLING_TO_PERFORM;
311                 } else {
312                         account_type = ds_gtype2atype(group_type);
313                         ret = samdb_msg_add_uint(ldb,
314                                                  ac->msg, ac->msg,
315                                                  "sAMAccountType",
316                                                  account_type);
317                         if (ret != LDB_SUCCESS) {
318                                 return ret;
319                         }
320                 }
321         }
322
323         return samldb_next_step(ac);
324 }
325
326 static bool samldb_msg_add_sid(struct ldb_message *msg,
327                                 const char *name,
328                                 const struct dom_sid *sid)
329 {
330         struct ldb_val v;
331         enum ndr_err_code ndr_err;
332
333         ndr_err = ndr_push_struct_blob(&v, msg, NULL, sid,
334                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
335         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
336                 return false;
337         }
338         return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
339 }
340
341
342 /* allocate a SID using our RID Set */
343 static int samldb_allocate_sid(struct samldb_ctx *ac)
344 {
345         uint32_t rid;
346         int ret;
347         struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
348
349         ret = ridalloc_allocate_rid(ac->module, &rid);
350         if (ret != LDB_SUCCESS) {
351                 return ret;
352         }
353
354         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
355         if (ac->sid == NULL) {
356                 ldb_module_oom(ac->module);
357                 return LDB_ERR_OPERATIONS_ERROR;
358         }
359
360         if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
361                 return LDB_ERR_OPERATIONS_ERROR;
362         }
363
364         return samldb_next_step(ac);
365 }
366
367 /*
368  * samldb_dn_from_sid (async)
369  */
370
371 static int samldb_dn_from_sid(struct samldb_ctx *ac);
372
373 static int samldb_dn_from_sid_callback(struct ldb_request *req,
374         struct ldb_reply *ares)
375 {
376         struct ldb_context *ldb;
377         struct samldb_ctx *ac;
378         int ret;
379
380         ac = talloc_get_type(req->context, struct samldb_ctx);
381         ldb = ldb_module_get_ctx(ac->module);
382
383         if (!ares) {
384                 ret = LDB_ERR_OPERATIONS_ERROR;
385                 goto done;
386         }
387         if (ares->error != LDB_SUCCESS) {
388                 return ldb_module_done(ac->req, ares->controls,
389                                         ares->response, ares->error);
390         }
391
392         switch (ares->type) {
393         case LDB_REPLY_ENTRY:
394                 /* save entry */
395                 if (ac->res_dn != NULL) {
396                         /* one too many! */
397                         ldb_set_errstring(ldb,
398                                 "Invalid number of results while searching "
399                                 "for domain objects!");
400                         ret = LDB_ERR_OPERATIONS_ERROR;
401                         break;
402                 }
403                 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
404
405                 talloc_free(ares);
406                 ret = LDB_SUCCESS;
407                 break;
408
409         case LDB_REPLY_REFERRAL:
410                 /* ignore */
411                 talloc_free(ares);
412                 ret = LDB_SUCCESS;
413                 break;
414
415         case LDB_REPLY_DONE:
416                 talloc_free(ares);
417
418                 /* found or not found, go on */
419                 ret = samldb_next_step(ac);
420                 break;
421         }
422
423 done:
424         if (ret != LDB_SUCCESS) {
425                 return ldb_module_done(ac->req, NULL, NULL, ret);
426         }
427
428         return LDB_SUCCESS;
429 }
430
431 /* Finds the DN "res_dn" of an object with a given SID "sid" */
432 static int samldb_dn_from_sid(struct samldb_ctx *ac)
433 {
434         struct ldb_context *ldb;
435         static const char * const attrs[] = { NULL };
436         struct ldb_request *req;
437         char *filter;
438         int ret;
439
440         ldb = ldb_module_get_ctx(ac->module);
441
442         if (ac->sid == NULL)
443                 return LDB_ERR_OPERATIONS_ERROR;
444
445         filter = talloc_asprintf(ac, "(objectSid=%s)",
446                 ldap_encode_ndr_dom_sid(ac, ac->sid));
447         if (filter == NULL)
448                 return LDB_ERR_OPERATIONS_ERROR;
449
450         ret = ldb_build_search_req(&req, ldb, ac,
451                                 ldb_get_default_basedn(ldb),
452                                 LDB_SCOPE_SUBTREE,
453                                 filter, attrs,
454                                 NULL,
455                                 ac, samldb_dn_from_sid_callback,
456                                 ac->req);
457         if (ret != LDB_SUCCESS)
458                 return ret;
459
460         return ldb_next_request(ac->module, req);
461 }
462
463
464 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
465 {
466         struct ldb_context *ldb;
467         uint32_t rid;
468
469         ldb = ldb_module_get_ctx(ac->module);
470
471         rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
472         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
473         if (ac->sid == NULL)
474                 return LDB_ERR_OPERATIONS_ERROR;
475         ac->res_dn = NULL;
476
477         return samldb_next_step(ac);
478 }
479
480 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
481 {
482         if (ac->res_dn == NULL) {
483                 struct ldb_context *ldb;
484                 ldb = ldb_module_get_ctx(ac->module);
485                 ldb_asprintf_errstring(ldb,
486                                        "Failed to find group sid %s!",
487                                        dom_sid_string(ac->sid, ac->sid));
488                 return LDB_ERR_UNWILLING_TO_PERFORM;
489         }
490
491         return samldb_next_step(ac);
492 }
493
494
495 /*
496  * samldb_set_defaultObjectCategory_callback (async)
497  */
498
499 static int samldb_set_defaultObjectCategory_callback(struct ldb_request *req,
500                                                      struct ldb_reply *ares)
501 {
502         struct ldb_context *ldb;
503         struct samldb_ctx *ac;
504         int ret;
505
506         ac = talloc_get_type(req->context, struct samldb_ctx);
507         ldb = ldb_module_get_ctx(ac->module);
508
509         if (!ares) {
510                 ret = LDB_ERR_OPERATIONS_ERROR;
511                 goto done;
512         }
513
514         if (ares->type == LDB_REPLY_REFERRAL) {
515                 return ldb_module_send_referral(ac->req, ares->referral);
516         }
517
518         if (ares->error != LDB_SUCCESS) {
519                 return ldb_module_done(ac->req, ares->controls,
520                                         ares->response, ares->error);
521         }
522         if (ares->type != LDB_REPLY_DONE) {
523                 ldb_set_errstring(ldb,
524                         "Invalid reply type!");
525                 ret = LDB_ERR_OPERATIONS_ERROR;
526                 goto done;
527         }
528
529         ret = samldb_next_step(ac);
530
531 done:
532         if (ret != LDB_SUCCESS) {
533                 return ldb_module_done(ac->req, NULL, NULL, ret);
534         }
535
536         return LDB_SUCCESS;
537 }
538
539 static int samldb_set_defaultObjectCategory(struct samldb_ctx *ac)
540 {
541         struct ldb_context *ldb;
542         struct ldb_message *msg;
543         struct ldb_request *req;
544         int ret;
545
546         ldb = ldb_module_get_ctx(ac->module);
547
548         /* (Re)set the default object category to have it set to the DN in the
549          * storage format */
550         msg = ldb_msg_new(ac);
551         msg->dn = ac->msg->dn;
552         ldb_msg_add_empty(msg, "defaultObjectCategory",
553                           LDB_FLAG_MOD_REPLACE, NULL);
554         ldb_msg_add_steal_string(msg, "defaultObjectCategory",
555                                  ldb_dn_alloc_linearized(msg, ac->dn));
556
557         ret = ldb_build_mod_req(&req, ldb, ac,
558                                 msg, NULL,
559                                 ac,
560                                 samldb_set_defaultObjectCategory_callback,
561                                 ac->req);
562         if (ret != LDB_SUCCESS) {
563                 talloc_free(msg);
564                 return ret;
565         }
566
567         return ldb_next_request(ac->module, req);
568 }
569
570 /*
571  * samldb_find_for_defaultObjectCategory (async)
572  */
573
574 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
575                                                           struct ldb_reply *ares)
576 {
577         struct ldb_context *ldb;
578         struct samldb_ctx *ac;
579         int ret;
580
581         ac = talloc_get_type(req->context, struct samldb_ctx);
582         ldb = ldb_module_get_ctx(ac->module);
583
584         if (!ares) {
585                 ret = LDB_ERR_OPERATIONS_ERROR;
586                 goto done;
587         }
588         if (ares->error != LDB_SUCCESS) {
589                 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
590                         if (ldb_request_get_control(ac->req,
591                                                     LDB_CONTROL_RELAX_OID) != NULL) {
592                                 /* Don't be pricky when the DN doesn't exist */
593                                 /* if we have the RELAX control specified */
594                                 ac->dn = req->op.search.base;
595                                 return samldb_next_step(ac);
596                         } else {
597                                 ldb_set_errstring(ldb,
598                                         "samldb_find_defaultObjectCategory: "
599                                         "Invalid DN for 'defaultObjectCategory'!");
600                                 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
601                         }
602                 }
603
604                 return ldb_module_done(ac->req, ares->controls,
605                                        ares->response, ares->error);
606         }
607
608         switch (ares->type) {
609         case LDB_REPLY_ENTRY:
610                 ac->dn = talloc_steal(ac, ares->message->dn);
611
612                 ret = LDB_SUCCESS;
613                 break;
614
615         case LDB_REPLY_REFERRAL:
616                 /* ignore */
617                 talloc_free(ares);
618                 ret = LDB_SUCCESS;
619                 break;
620
621         case LDB_REPLY_DONE:
622                 talloc_free(ares);
623
624                 if (ac->dn != NULL) {
625                         /* when found go on */
626                         ret = samldb_next_step(ac);
627                 } else {
628                         ret = LDB_ERR_OPERATIONS_ERROR;
629                 }
630                 break;
631         }
632
633 done:
634         if (ret != LDB_SUCCESS) {
635                 return ldb_module_done(ac->req, NULL, NULL, ret);
636         }
637
638         return LDB_SUCCESS;
639 }
640
641 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
642 {
643         struct ldb_context *ldb;
644         struct ldb_request *req;
645         static const char *no_attrs[] = { NULL };
646         int ret;
647         const struct ldb_val *val;
648         struct ldb_dn *def_obj_cat_dn;
649
650         ldb = ldb_module_get_ctx(ac->module);
651
652         ac->dn = NULL;
653
654         val = ldb_msg_find_ldb_val(ac->msg, "defaultObjectCategory");
655         if (val != NULL) {
656                 /* "defaultObjectCategory" has been set by the caller. Do some
657                  * checks for consistency.
658                  * NOTE: The real constraint check (that 'defaultObjectCategory'
659                  * is the DN of the new objectclass or any parent of it) is
660                  * still incomplete.
661                  * For now we say that 'defaultObjectCategory' is valid if it
662                  * exists and it is of objectclass "classSchema". */
663                 def_obj_cat_dn = ldb_dn_from_ldb_val(ac, ldb, val);
664                 if (def_obj_cat_dn == NULL) {
665                         ldb_set_errstring(ldb,
666                                 "samldb_find_defaultObjectCategory: Invalid DN "
667                                 "for 'defaultObjectCategory'!");
668                         return LDB_ERR_CONSTRAINT_VIOLATION;
669                 }
670         } else {
671                 /* "defaultObjectCategory" has not been set by the caller. Use
672                  * the entry DN for it. */
673                 def_obj_cat_dn = ac->msg->dn;
674         }
675
676         ret = ldb_build_search_req(&req, ldb, ac,
677                                    def_obj_cat_dn, LDB_SCOPE_BASE,
678                                    "objectClass=classSchema", no_attrs,
679                                    NULL,
680                                    ac, samldb_find_for_defaultObjectCategory_callback,
681                                    ac->req);
682         if (ret != LDB_SUCCESS) {
683                 return ret;
684         }
685
686         ret = dsdb_request_add_controls(req,
687                                         DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
688         if (ret != LDB_SUCCESS) {
689                 return ret;
690         }
691
692         return ldb_next_request(ac->module, req);
693 }
694
695 /**
696  * msDS-IntId attributeSchema attribute handling
697  * during LDB_ADD request processing
698  */
699 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
700 {
701         int ret;
702         bool id_exists;
703         uint32_t msds_intid;
704         uint32_t system_flags;
705         struct ldb_context *ldb;
706         struct ldb_result *ldb_res;
707         struct ldb_dn *schema_dn;
708
709         ldb = ldb_module_get_ctx(ac->module);
710         schema_dn = ldb_get_schema_basedn(ldb);
711
712         /* replicated update should always go through */
713         if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
714                 return LDB_SUCCESS;
715         }
716
717         /* msDS-IntId is handled by system and should never be
718          * passed by clients */
719         if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
720                 return LDB_ERR_UNWILLING_TO_PERFORM;
721         }
722
723         /* do not generate msDS-IntId if Relax control is passed */
724         if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
725                 return LDB_SUCCESS;
726         }
727
728         /* check Functional Level */
729         if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
730                 return LDB_SUCCESS;
731         }
732
733         /* check systemFlags for SCHEMA_BASE_OBJECT flag */
734         system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
735         if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
736                 return LDB_SUCCESS;
737         }
738
739         /* Generate new value for msDs-IntId
740          * Value should be in 0x80000000..0xBFFFFFFF range */
741         msds_intid = generate_random() % 0X3FFFFFFF;
742         msds_intid += 0x80000000;
743
744         /* probe id values until unique one is found */
745         do {
746                 msds_intid++;
747                 if (msds_intid > 0xBFFFFFFF) {
748                         msds_intid = 0x80000001;
749                 }
750
751                 ret = dsdb_module_search(ac->module, ac,
752                                          &ldb_res,
753                                          schema_dn, LDB_SCOPE_ONELEVEL, NULL, 0,
754                                          "(msDS-IntId=%d)", msds_intid);
755                 if (ret != LDB_SUCCESS) {
756                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
757                                       __location__": Searching for msDS-IntId=%d failed - %s\n",
758                                       msds_intid,
759                                       ldb_errstring(ldb));
760                         return LDB_ERR_OPERATIONS_ERROR;
761                 }
762                 id_exists = (ldb_res->count > 0);
763
764                 talloc_free(ldb_res);
765         } while(id_exists);
766
767         return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
768 }
769
770
771 /*
772  * samldb_add_entry (async)
773  */
774
775 static int samldb_add_entry_callback(struct ldb_request *req,
776                                         struct ldb_reply *ares)
777 {
778         struct ldb_context *ldb;
779         struct samldb_ctx *ac;
780         int ret;
781
782         ac = talloc_get_type(req->context, struct samldb_ctx);
783         ldb = ldb_module_get_ctx(ac->module);
784
785         if (!ares) {
786                 return ldb_module_done(ac->req, NULL, NULL,
787                                         LDB_ERR_OPERATIONS_ERROR);
788         }
789
790         if (ares->type == LDB_REPLY_REFERRAL) {
791                 return ldb_module_send_referral(ac->req, ares->referral);
792         }
793
794         if (ares->error != LDB_SUCCESS) {
795                 return ldb_module_done(ac->req, ares->controls,
796                                         ares->response, ares->error);
797         }
798         if (ares->type != LDB_REPLY_DONE) {
799                 ldb_set_errstring(ldb,
800                         "Invalid reply type!\n");
801                 return ldb_module_done(ac->req, NULL, NULL,
802                                         LDB_ERR_OPERATIONS_ERROR);
803         }
804
805         /* The caller may wish to get controls back from the add */
806         ac->ares = talloc_steal(ac, ares);
807
808         ret = samldb_next_step(ac);
809         if (ret != LDB_SUCCESS) {
810                 return ldb_module_done(ac->req, NULL, NULL, ret);
811         }
812         return ret;
813 }
814
815 static int samldb_add_entry(struct samldb_ctx *ac)
816 {
817         struct ldb_context *ldb;
818         struct ldb_request *req;
819         int ret;
820
821         ldb = ldb_module_get_ctx(ac->module);
822
823         ret = ldb_build_add_req(&req, ldb, ac,
824                                 ac->msg,
825                                 ac->req->controls,
826                                 ac, samldb_add_entry_callback,
827                                 ac->req);
828         if (ret != LDB_SUCCESS) {
829                 return ret;
830         }
831
832         return ldb_next_request(ac->module, req);
833 }
834
835 /*
836  * return true if msg carries an attributeSchema that is intended to be RODC
837  * filtered but is also a system-critical attribute.
838  */
839 static bool check_rodc_critical_attribute(struct ldb_message *msg)
840 {
841         uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
842
843         schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
844         searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
845         rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
846
847         if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
848                 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
849                 return true;
850         } else {
851                 return false;
852         }
853 }
854
855
856 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
857 {
858         struct ldb_context *ldb;
859         struct loadparm_context *lp_ctx;
860         enum sid_generator sid_generator;
861         int ret;
862
863         ldb = ldb_module_get_ctx(ac->module);
864
865         /* Add informations for the different account types */
866         ac->type = type;
867         if (strcmp(ac->type, "user") == 0) {
868                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
869                         "userAccountControl", "546");
870                 if (ret != LDB_SUCCESS) return ret;
871                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
872                         "badPwdCount", "0");
873                 if (ret != LDB_SUCCESS) return ret;
874                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
875                         "codePage", "0");
876                 if (ret != LDB_SUCCESS) return ret;
877                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
878                         "countryCode", "0");
879                 if (ret != LDB_SUCCESS) return ret;
880                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
881                         "badPasswordTime", "0");
882                 if (ret != LDB_SUCCESS) return ret;
883                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
884                         "lastLogoff", "0");
885                 if (ret != LDB_SUCCESS) return ret;
886                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
887                         "lastLogon", "0");
888                 if (ret != LDB_SUCCESS) return ret;
889                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
890                         "pwdLastSet", "0");
891                 if (ret != LDB_SUCCESS) return ret;
892                 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
893                         ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
894                                                  "primaryGroupID", DOMAIN_RID_USERS);
895                         if (ret != LDB_SUCCESS) return ret;
896                 }
897                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
898                         "accountExpires", "9223372036854775807");
899                 if (ret != LDB_SUCCESS) return ret;
900                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
901                         "logonCount", "0");
902                 if (ret != LDB_SUCCESS) return ret;
903         } else if (strcmp(ac->type, "group") == 0) {
904                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
905                         "groupType", "-2147483646");
906                 if (ret != LDB_SUCCESS) return ret;
907         } else if (strcmp(ac->type, "classSchema") == 0) {
908                 const struct ldb_val *rdn_value;
909
910                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
911                                                   "rdnAttId", "cn");
912                 if (ret != LDB_SUCCESS) return ret;
913
914                 /* do not allow to mark an attributeSchema as RODC filtered if it
915                  * is system-critical */
916                 if (check_rodc_critical_attribute(ac->msg)) {
917                         ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
918                                                ldb_dn_get_linearized(ac->msg->dn));
919                         return LDB_ERR_UNWILLING_TO_PERFORM;
920                 }
921
922
923                 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
924                 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
925                         /* the RDN has prefix "CN" */
926                         ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
927                                 samdb_cn_to_lDAPDisplayName(ac,
928                                         (const char *) rdn_value->data));
929                         if (ret != LDB_SUCCESS) {
930                                 ldb_oom(ldb);
931                                 return ret;
932                         }
933                 }
934
935                 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
936                         struct GUID guid;
937                         /* a new GUID */
938                         guid = GUID_random();
939                         ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
940                         if (ret != LDB_SUCCESS) {
941                                 ldb_oom(ldb);
942                                 return ret;
943                         }
944                 }
945
946                 ret = samldb_add_step(ac, samldb_add_entry);
947                 if (ret != LDB_SUCCESS) return ret;
948
949                 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
950                 if (ret != LDB_SUCCESS) return ret;
951
952                 ret = samldb_add_step(ac, samldb_set_defaultObjectCategory);
953                 if (ret != LDB_SUCCESS) return ret;
954
955                 return samldb_first_step(ac);
956         } else if (strcmp(ac->type, "attributeSchema") == 0) {
957                 const struct ldb_val *rdn_value;
958                 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
959                 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
960                         /* the RDN has prefix "CN" */
961                         ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
962                                 samdb_cn_to_lDAPDisplayName(ac,
963                                         (const char *) rdn_value->data));
964                         if (ret != LDB_SUCCESS) {
965                                 ldb_oom(ldb);
966                                 return ret;
967                         }
968                 }
969
970                 /* do not allow to mark an attributeSchema as RODC filtered if it
971                  * is system-critical */
972                 if (check_rodc_critical_attribute(ac->msg)) {
973                         ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
974                                                ldb_dn_get_linearized(ac->msg->dn));
975                         return LDB_ERR_UNWILLING_TO_PERFORM;
976                 }
977
978                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
979                                                   "isSingleValued", "FALSE");
980                 if (ret != LDB_SUCCESS) return ret;
981
982                 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
983                         struct GUID guid;
984                         /* a new GUID */
985                         guid = GUID_random();
986                         ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
987                         if (ret != LDB_SUCCESS) {
988                                 ldb_oom(ldb);
989                                 return ret;
990                         }
991                 }
992
993                 /* handle msDS-IntID attribute */
994                 ret = samldb_add_handle_msDS_IntId(ac);
995                 if (ret != LDB_SUCCESS) return ret;
996
997                 ret = samldb_add_step(ac, samldb_add_entry);
998                 if (ret != LDB_SUCCESS) return ret;
999
1000                 return samldb_first_step(ac);
1001         } else {
1002                 ldb_asprintf_errstring(ldb,
1003                         "Invalid entry type!");
1004                 return LDB_ERR_OPERATIONS_ERROR;
1005         }
1006
1007         /* check if we have a valid samAccountName */
1008         ret = samldb_add_step(ac, samldb_check_samAccountName);
1009         if (ret != LDB_SUCCESS) return ret;
1010
1011         /* check account_type/group_type */
1012         ret = samldb_add_step(ac, samldb_check_samAccountType);
1013         if (ret != LDB_SUCCESS) return ret;
1014
1015         /* check if we have a valid primary group ID */
1016         if (strcmp(ac->type, "user") == 0) {
1017                 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
1018                 if (ret != LDB_SUCCESS) return ret;
1019                 ret = samldb_add_step(ac, samldb_dn_from_sid);
1020                 if (ret != LDB_SUCCESS) return ret;
1021                 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
1022                 if (ret != LDB_SUCCESS) return ret;
1023         }
1024
1025         lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
1026                  struct loadparm_context);
1027
1028         /* don't allow objectSID to be specified without the RELAX control */
1029         ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
1030         if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
1031             !dsdb_module_am_system(ac->module)) {
1032                 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
1033                                        ldb_dn_get_linearized(ac->msg->dn));
1034                 return LDB_ERR_UNWILLING_TO_PERFORM;
1035         }
1036
1037         if ( ! ac->sid) {
1038                 sid_generator = lp_sid_generator(lp_ctx);
1039                 if (sid_generator == SID_GENERATOR_INTERNAL) {
1040                         ret = samldb_add_step(ac, samldb_allocate_sid);
1041                         if (ret != LDB_SUCCESS) return ret;
1042                 }
1043         }
1044
1045         /* finally proceed with adding the entry */
1046         ret = samldb_add_step(ac, samldb_add_entry);
1047         if (ret != LDB_SUCCESS) return ret;
1048
1049         return samldb_first_step(ac);
1050 }
1051
1052 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
1053 {
1054         struct ldb_context *ldb;
1055         int ret;
1056
1057         ldb = ldb_module_get_ctx(ac->module);
1058
1059         ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1060         if (ac->sid == NULL) {
1061                 ac->sid = dom_sid_parse_talloc(ac->msg,
1062                            (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1063                 if (!ac->sid) {
1064                         ldb_set_errstring(ldb,
1065                                         "No valid SID found in "
1066                                         "ForeignSecurityPrincipal CN!");
1067                         talloc_free(ac);
1068                         return LDB_ERR_CONSTRAINT_VIOLATION;
1069                 }
1070                 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
1071                         talloc_free(ac);
1072                         return LDB_ERR_OPERATIONS_ERROR;
1073                 }
1074         }
1075
1076         /* finally proceed with adding the entry */
1077         ret = samldb_add_step(ac, samldb_add_entry);
1078         if (ret != LDB_SUCCESS) return ret;
1079
1080         return samldb_first_step(ac);
1081 }
1082
1083 static int samldb_check_rdn(struct ldb_module *module, struct ldb_dn *dn)
1084 {
1085         struct ldb_context *ldb;
1086         const char *rdn_name;
1087
1088         ldb = ldb_module_get_ctx(module);
1089         rdn_name = ldb_dn_get_rdn_name(dn);
1090
1091         if (strcasecmp(rdn_name, "cn") != 0) {
1092                 ldb_asprintf_errstring(ldb,
1093                                         "Bad RDN (%s=) for samldb object, "
1094                                         "should be CN=!", rdn_name);
1095                 return LDB_ERR_CONSTRAINT_VIOLATION;
1096         }
1097
1098         return LDB_SUCCESS;
1099 }
1100
1101 static int samldb_schema_info_update(struct samldb_ctx *ac)
1102 {
1103         WERROR werr;
1104         struct ldb_context *ldb;
1105         struct dsdb_schema *schema;
1106
1107         /* replicated update should always go through */
1108         if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1109                 return LDB_SUCCESS;
1110         }
1111
1112         /* do not update schemaInfo during provisioning */
1113         if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1114                 return LDB_SUCCESS;
1115         }
1116
1117         ldb = ldb_module_get_ctx(ac->module);
1118         schema = dsdb_get_schema(ldb, NULL);
1119         if (!schema) {
1120                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1121                               "samldb_schema_info_update: no dsdb_schema loaded");
1122                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1123                 return LDB_ERR_OPERATIONS_ERROR;
1124         }
1125
1126         werr = dsdb_module_schema_info_update(ac->module, schema, 0);
1127         if (!W_ERROR_IS_OK(werr)) {
1128                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1129                               "samldb_schema_info_update: "
1130                               "dsdb_module_schema_info_update failed with %s",
1131                               win_errstr(werr));
1132                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1133                 return LDB_ERR_OPERATIONS_ERROR;
1134         }
1135
1136         return LDB_SUCCESS;
1137 }
1138
1139 /*
1140  * samldb_user_dn_to_prim_group_rid (async)
1141  */
1142
1143 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac);
1144
1145 static int samldb_user_dn_to_prim_group_rid_callback(struct ldb_request *req,
1146         struct ldb_reply *ares)
1147 {
1148         struct ldb_context *ldb;
1149         struct samldb_ctx *ac;
1150         int ret;
1151
1152         ac = talloc_get_type(req->context, struct samldb_ctx);
1153         ldb = ldb_module_get_ctx(ac->module);
1154
1155         if (!ares) {
1156                 ret = LDB_ERR_OPERATIONS_ERROR;
1157                 goto done;
1158         }
1159         if (ares->error != LDB_SUCCESS) {
1160                 return ldb_module_done(ac->req, ares->controls,
1161                                         ares->response, ares->error);
1162         }
1163
1164         switch (ares->type) {
1165         case LDB_REPLY_ENTRY:
1166                 /* save entry */
1167                 if (ac->prim_group_rid != 0) {
1168                         /* one too many! */
1169                         ldb_set_errstring(ldb,
1170                                 "Invalid number of results while searching "
1171                                 "for domain objects!");
1172                         ret = LDB_ERR_OPERATIONS_ERROR;
1173                         break;
1174                 }
1175                 ac->prim_group_rid = samdb_result_uint(ares->message,
1176                         "primaryGroupID", ~0);
1177
1178                 talloc_free(ares);
1179                 ret = LDB_SUCCESS;
1180                 break;
1181
1182         case LDB_REPLY_REFERRAL:
1183                 /* ignore */
1184                 talloc_free(ares);
1185                 ret = LDB_SUCCESS;
1186                 break;
1187
1188         case LDB_REPLY_DONE:
1189                 talloc_free(ares);
1190                 if (ac->prim_group_rid == 0) {
1191                         ldb_asprintf_errstring(ldb,
1192                                 "Unable to get the primary group RID!");
1193                         ret = LDB_ERR_OPERATIONS_ERROR;
1194                         break;
1195                 }
1196
1197                 /* found, go on */
1198                 ret = samldb_next_step(ac);
1199                 break;
1200         }
1201
1202 done:
1203         if (ret != LDB_SUCCESS) {
1204                 return ldb_module_done(ac->req, NULL, NULL, ret);
1205         }
1206
1207         return LDB_SUCCESS;
1208 }
1209
1210 /* Locates the "primaryGroupID" attribute from a certain user specified as
1211  * "user_dn". Saves the result in "prim_group_rid". */
1212 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac)
1213 {
1214         struct ldb_context *ldb;
1215         static const char * const attrs[] = { "primaryGroupID", NULL };
1216         struct ldb_request *req;
1217         int ret;
1218
1219         ldb = ldb_module_get_ctx(ac->module);
1220
1221         if (ac->user_dn == NULL)
1222                 return LDB_ERR_OPERATIONS_ERROR;
1223
1224         ret = ldb_build_search_req(&req, ldb, ac,
1225                                 ac->user_dn,
1226                                 LDB_SCOPE_BASE,
1227                                 NULL, attrs,
1228                                 NULL,
1229                                 ac, samldb_user_dn_to_prim_group_rid_callback,
1230                                 ac->req);
1231         if (ret != LDB_SUCCESS)
1232                 return ret;
1233
1234         return ldb_next_request(ac->module, req);
1235 }
1236
1237 /*
1238  * samldb_group_add_member (async)
1239  * samldb_group_del_member (async)
1240  */
1241
1242 static int samldb_group_add_del_member_callback(struct ldb_request *req,
1243         struct ldb_reply *ares)
1244 {
1245         struct ldb_context *ldb;
1246         struct samldb_ctx *ac;
1247         int ret;
1248
1249         ac = talloc_get_type(req->context, struct samldb_ctx);
1250         ldb = ldb_module_get_ctx(ac->module);
1251
1252         if (!ares) {
1253                 ret = LDB_ERR_OPERATIONS_ERROR;
1254                 goto done;
1255         }
1256
1257         if (ares->type == LDB_REPLY_REFERRAL) {
1258                 return ldb_module_send_referral(ac->req, ares->referral);
1259         }
1260
1261         if (ares->error != LDB_SUCCESS) {
1262                 if (ares->error == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1263                         /* On error "NO_SUCH_ATTRIBUTE" (delete of an invalid
1264                          * "member" attribute) return "UNWILLING_TO_PERFORM" */
1265                         ares->error = LDB_ERR_UNWILLING_TO_PERFORM;
1266                 }
1267                 return ldb_module_done(ac->req, ares->controls,
1268                                         ares->response, ares->error);
1269         }
1270         if (ares->type != LDB_REPLY_DONE) {
1271                 ldb_set_errstring(ldb,
1272                         "Invalid reply type!");
1273                 ret = LDB_ERR_OPERATIONS_ERROR;
1274                 goto done;
1275         }
1276
1277         ret = samldb_next_step(ac);
1278
1279 done:
1280         if (ret != LDB_SUCCESS) {
1281                 return ldb_module_done(ac->req, NULL, NULL, ret);
1282         }
1283
1284         return LDB_SUCCESS;
1285 }
1286
1287 /* Adds a member with DN "member_dn" to a group with DN "group_dn" */
1288 static int samldb_group_add_member(struct samldb_ctx *ac)
1289 {
1290         struct ldb_context *ldb;
1291         struct ldb_request *req;
1292         struct ldb_message *msg;
1293         int ret;
1294
1295         ldb = ldb_module_get_ctx(ac->module);
1296
1297         if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1298                 return LDB_ERR_OPERATIONS_ERROR;
1299
1300         msg = ldb_msg_new(ac);
1301         msg->dn = ac->group_dn;
1302         samdb_msg_add_addval(ldb, ac, msg, "member",
1303                 ldb_dn_get_linearized(ac->member_dn));
1304
1305         ret = ldb_build_mod_req(&req, ldb, ac,
1306                                 msg, NULL,
1307                                 ac, samldb_group_add_del_member_callback,
1308                                 ac->req);
1309         if (ret != LDB_SUCCESS)
1310                 return ret;
1311
1312         return ldb_next_request(ac->module, req);
1313 }
1314
1315 /* Removes a member with DN "member_dn" from a group with DN "group_dn" */
1316 static int samldb_group_del_member(struct samldb_ctx *ac)
1317 {
1318         struct ldb_context *ldb;
1319         struct ldb_request *req;
1320         struct ldb_message *msg;
1321         int ret;
1322
1323         ldb = ldb_module_get_ctx(ac->module);
1324
1325         if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1326                 return LDB_ERR_OPERATIONS_ERROR;
1327
1328         msg = ldb_msg_new(ac);
1329         msg->dn = ac->group_dn;
1330         samdb_msg_add_delval(ldb, ac, msg, "member",
1331                 ldb_dn_get_linearized(ac->member_dn));
1332
1333         ret = ldb_build_mod_req(&req, ldb, ac,
1334                                 msg, NULL,
1335                                 ac, samldb_group_add_del_member_callback,
1336                                 ac->req);
1337         if (ret != LDB_SUCCESS)
1338                 return ret;
1339
1340         return ldb_next_request(ac->module, req);
1341 }
1342
1343
1344 static int samldb_prim_group_change_1(struct samldb_ctx *ac)
1345 {
1346         struct ldb_context *ldb;
1347         uint32_t rid;
1348
1349         ldb = ldb_module_get_ctx(ac->module);
1350
1351         ac->user_dn = ac->msg->dn;
1352
1353         rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
1354         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1355         if (ac->sid == NULL)
1356                 return LDB_ERR_OPERATIONS_ERROR;
1357         ac->res_dn = NULL;
1358
1359         ac->prim_group_rid = 0;
1360
1361         return samldb_next_step(ac);
1362 }
1363
1364 static int samldb_prim_group_change_2(struct samldb_ctx *ac)
1365 {
1366         struct ldb_context *ldb;
1367
1368         ldb = ldb_module_get_ctx(ac->module);
1369
1370         if (ac->res_dn != NULL)
1371                 ac->new_prim_group_dn = ac->res_dn;
1372         else
1373                 return LDB_ERR_UNWILLING_TO_PERFORM;
1374
1375         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1376                 ac->prim_group_rid);
1377         if (ac->sid == NULL)
1378                 return LDB_ERR_OPERATIONS_ERROR;
1379         ac->res_dn = NULL;
1380
1381         return samldb_next_step(ac);
1382 }
1383
1384 static int samldb_prim_group_change_4(struct samldb_ctx *ac);
1385 static int samldb_prim_group_change_5(struct samldb_ctx *ac);
1386 static int samldb_prim_group_change_6(struct samldb_ctx *ac);
1387
1388 static int samldb_prim_group_change_3(struct samldb_ctx *ac)
1389 {
1390         int ret;
1391
1392         if (ac->res_dn != NULL)
1393                 ac->old_prim_group_dn = ac->res_dn;
1394         else
1395                 return LDB_ERR_UNWILLING_TO_PERFORM;
1396
1397         /* Only update when the primary group changed */
1398         if (ldb_dn_compare(ac->old_prim_group_dn, ac->new_prim_group_dn) != 0) {
1399                 ac->member_dn = ac->user_dn;
1400                 /* Remove the "member" attribute of the actual (new) primary
1401                  * group */
1402
1403                 ret = samldb_add_step(ac, samldb_prim_group_change_4);
1404                 if (ret != LDB_SUCCESS) return ret;
1405
1406                 ret = samldb_add_step(ac, samldb_group_del_member);
1407                 if (ret != LDB_SUCCESS) return ret;
1408
1409                 /* Add a "member" attribute for the previous primary group */
1410
1411                 ret = samldb_add_step(ac, samldb_prim_group_change_5);
1412                 if (ret != LDB_SUCCESS) return ret;
1413
1414                 ret = samldb_add_step(ac, samldb_group_add_member);
1415                 if (ret != LDB_SUCCESS) return ret;
1416         }
1417
1418         ret = samldb_add_step(ac, samldb_prim_group_change_6);
1419         if (ret != LDB_SUCCESS) return ret;
1420
1421         return samldb_next_step(ac);
1422 }
1423
1424 static int samldb_prim_group_change_4(struct samldb_ctx *ac)
1425 {
1426         ac->group_dn = ac->new_prim_group_dn;
1427
1428         return samldb_next_step(ac);
1429 }
1430
1431 static int samldb_prim_group_change_5(struct samldb_ctx *ac)
1432 {
1433         ac->group_dn = ac->old_prim_group_dn;
1434
1435         return samldb_next_step(ac);
1436 }
1437
1438 static int samldb_prim_group_change_6(struct samldb_ctx *ac)
1439 {
1440         return ldb_next_request(ac->module, ac->req);
1441 }
1442
1443 static int samldb_prim_group_change(struct samldb_ctx *ac)
1444 {
1445         int ret;
1446
1447         /* Finds out the DN of the new primary group */
1448
1449         ret = samldb_add_step(ac, samldb_prim_group_change_1);
1450         if (ret != LDB_SUCCESS) return ret;
1451
1452         ret = samldb_add_step(ac, samldb_dn_from_sid);
1453         if (ret != LDB_SUCCESS) return ret;
1454
1455         ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1456         if (ret != LDB_SUCCESS) return ret;
1457
1458         /* Finds out the DN of the old primary group */
1459
1460         ret = samldb_add_step(ac, samldb_prim_group_change_2);
1461         if (ret != LDB_SUCCESS) return ret;
1462
1463         ret = samldb_add_step(ac, samldb_dn_from_sid);
1464         if (ret != LDB_SUCCESS) return ret;
1465
1466         ret = samldb_add_step(ac, samldb_prim_group_change_3);
1467         if (ret != LDB_SUCCESS) return ret;
1468
1469         return samldb_first_step(ac);
1470 }
1471
1472
1473 static int samldb_member_check_1(struct samldb_ctx *ac)
1474 {
1475         struct ldb_context *ldb;
1476         struct ldb_message_element *el;
1477
1478         ldb = ldb_module_get_ctx(ac->module);
1479
1480         el = ldb_msg_find_element(ac->msg, "member");
1481
1482         ac->user_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[ac->cnt]);
1483         if (!ldb_dn_validate(ac->user_dn))
1484                 return LDB_ERR_OPERATIONS_ERROR;
1485         ac->prim_group_rid = 0;
1486
1487         return samldb_next_step(ac);
1488 }
1489
1490 static int samldb_member_check_2(struct samldb_ctx *ac)
1491 {
1492         struct ldb_context *ldb;
1493
1494         ldb = ldb_module_get_ctx(ac->module);
1495
1496         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1497                 ac->prim_group_rid);
1498         if (ac->sid == NULL)
1499                 return LDB_ERR_OPERATIONS_ERROR;
1500         ac->res_dn = NULL;
1501
1502         return samldb_next_step(ac);
1503 }
1504
1505 static int samldb_member_check_3(struct samldb_ctx *ac)
1506 {
1507         if (ldb_dn_compare(ac->res_dn, ac->msg->dn) == 0)
1508                 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1509
1510         ++(ac->cnt);
1511
1512         return samldb_next_step(ac);
1513 }
1514
1515 static int samldb_member_check_4(struct samldb_ctx *ac)
1516 {
1517         return ldb_next_request(ac->module, ac->req);
1518 }
1519
1520 static int samldb_member_check(struct samldb_ctx *ac)
1521 {
1522         struct ldb_message_element *el;
1523         int i, ret;
1524
1525         el = ldb_msg_find_element(ac->msg, "member");
1526         ac->cnt = 0;
1527         for (i = 0; i < el->num_values; i++) {
1528                 /* Denies to add "member"s to groups which are primary ones
1529                  * for them */
1530                 ret = samldb_add_step(ac, samldb_member_check_1);
1531                 if (ret != LDB_SUCCESS) return ret;
1532
1533                 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1534                 if (ret != LDB_SUCCESS) return ret;
1535
1536                 ret = samldb_add_step(ac, samldb_member_check_2);
1537                 if (ret != LDB_SUCCESS) return ret;
1538
1539                 ret = samldb_add_step(ac, samldb_dn_from_sid);
1540                 if (ret != LDB_SUCCESS) return ret;
1541
1542                 ret = samldb_add_step(ac, samldb_member_check_3);
1543                 if (ret != LDB_SUCCESS) return ret;
1544         }
1545
1546         ret = samldb_add_step(ac, samldb_member_check_4);
1547         if (ret != LDB_SUCCESS) return ret;
1548
1549         return samldb_first_step(ac);
1550 }
1551
1552
1553 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1554 {
1555         struct ldb_context *ldb;
1556         struct dom_sid *sid;
1557         uint32_t rid;
1558         NTSTATUS status;
1559         int count;
1560
1561         ldb = ldb_module_get_ctx(ac->module);
1562
1563         /* Finds out the SID/RID of the SAM object */
1564         sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1565                                    NULL);
1566         if (sid == NULL) {
1567                 /* No SID - it might not be a SAM object - therefore ok */
1568                 return ldb_next_request(ac->module, ac->req);
1569         }
1570         status = dom_sid_split_rid(ac, sid, NULL, &rid);
1571         if (!NT_STATUS_IS_OK(status)) {
1572                 return LDB_ERR_OPERATIONS_ERROR;
1573         }
1574         if (rid == 0) {
1575                 /* Special object (security principal?) */
1576                 return ldb_next_request(ac->module, ac->req);
1577         }
1578
1579         /* Deny delete requests from groups which are primary ones */
1580         count = samdb_search_count(ldb, NULL,
1581                                    "(&(primaryGroupID=%u)(objectClass=user))",
1582                                    rid);
1583         if (count < 0) {
1584                 return LDB_ERR_OPERATIONS_ERROR;
1585         }
1586         if (count > 0) {
1587                 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1588         }
1589
1590         return ldb_next_request(ac->module, ac->req);
1591 }
1592
1593
1594 /* add */
1595 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1596 {
1597         struct ldb_context *ldb;
1598         struct samldb_ctx *ac;
1599         int ret;
1600
1601         ldb = ldb_module_get_ctx(module);
1602         ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1603
1604         /* do not manipulate our control entries */
1605         if (ldb_dn_is_special(req->op.add.message->dn)) {
1606                 return ldb_next_request(module, req);
1607         }
1608
1609         ac = samldb_ctx_init(module, req);
1610         if (ac == NULL) {
1611                 return LDB_ERR_OPERATIONS_ERROR;
1612         }
1613
1614         /* build the new msg */
1615         ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1616         if (!ac->msg) {
1617                 talloc_free(ac);
1618                 ldb_debug(ldb, LDB_DEBUG_FATAL,
1619                           "samldb_add: ldb_msg_copy failed!\n");
1620                 return LDB_ERR_OPERATIONS_ERROR;
1621         }
1622
1623         if (samdb_find_attribute(ldb, ac->msg,
1624                                  "objectclass", "computer") != NULL) {
1625
1626                 /* make sure the computer object also has the 'user'
1627                  * objectclass so it will be handled by the next call */
1628                 ret = samdb_find_or_add_value(ldb, ac->msg,
1629                                                 "objectclass", "user");
1630                 if (ret != LDB_SUCCESS) {
1631                         talloc_free(ac);
1632                         return ret;
1633                 }
1634         }
1635
1636         if (samdb_find_attribute(ldb, ac->msg,
1637                                  "objectclass", "user") != NULL) {
1638
1639                 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1640                 if (ret != LDB_SUCCESS) {
1641                         talloc_free(ac);
1642                         return ret;
1643                 }
1644
1645                 return samldb_fill_object(ac, "user");
1646         }
1647
1648         if (samdb_find_attribute(ldb, ac->msg,
1649                                  "objectclass", "group") != NULL) {
1650
1651                 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1652                 if (ret != LDB_SUCCESS) {
1653                         talloc_free(ac);
1654                         return ret;
1655                 }
1656
1657                 return samldb_fill_object(ac, "group");
1658         }
1659
1660         /* perhaps a foreignSecurityPrincipal? */
1661         if (samdb_find_attribute(ldb, ac->msg,
1662                                  "objectclass",
1663                                  "foreignSecurityPrincipal") != NULL) {
1664
1665                 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1666                 if (ret != LDB_SUCCESS) {
1667                         talloc_free(ac);
1668                         return ret;
1669                 }
1670
1671                 return samldb_fill_foreignSecurityPrincipal_object(ac);
1672         }
1673
1674         if (samdb_find_attribute(ldb, ac->msg,
1675                                  "objectclass", "classSchema") != NULL) {
1676
1677                 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1678                 if (ret != LDB_SUCCESS) {
1679                         talloc_free(ac);
1680                         return ret;
1681                 }
1682
1683                 ret = samldb_schema_info_update(ac);
1684                 if (ret != LDB_SUCCESS) {
1685                         talloc_free(ac);
1686                         return ret;
1687                 }
1688
1689                 return samldb_fill_object(ac, "classSchema");
1690         }
1691
1692         if (samdb_find_attribute(ldb, ac->msg,
1693                                  "objectclass", "attributeSchema") != NULL) {
1694
1695                 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1696                 if (ret != LDB_SUCCESS) {
1697                         talloc_free(ac);
1698                         return ret;
1699                 }
1700
1701                 ret = samldb_schema_info_update(ac);
1702                 if (ret != LDB_SUCCESS) {
1703                         talloc_free(ac);
1704                         return ret;
1705                 }
1706
1707                 return samldb_fill_object(ac, "attributeSchema");
1708         }
1709
1710         talloc_free(ac);
1711
1712         /* nothing matched, go on */
1713         return ldb_next_request(module, req);
1714 }
1715
1716 /* modify */
1717 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1718 {
1719         struct ldb_context *ldb;
1720         struct ldb_message *msg;
1721         struct ldb_message_element *el, *el2;
1722         int ret;
1723         uint32_t account_type;
1724
1725         if (ldb_dn_is_special(req->op.mod.message->dn)) {
1726                 /* do not manipulate our control entries */
1727                 return ldb_next_request(module, req);
1728         }
1729
1730         ldb = ldb_module_get_ctx(module);
1731
1732         if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1733                 ldb_asprintf_errstring(ldb,
1734                         "sAMAccountType must not be specified!");
1735                 return LDB_ERR_UNWILLING_TO_PERFORM;
1736         }
1737
1738         /* msDS-IntId is not allowed to be modified
1739          * except when modification comes from replication */
1740         if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1741                 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1742                         return LDB_ERR_CONSTRAINT_VIOLATION;
1743                 }
1744         }
1745
1746         /* TODO: do not modify original request, create a new one */
1747
1748         el = ldb_msg_find_element(req->op.mod.message, "groupType");
1749         if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1750                 uint32_t group_type;
1751
1752                 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1753                         req->op.mod.message);
1754
1755                 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1756                 account_type =  ds_gtype2atype(group_type);
1757                 ret = samdb_msg_add_uint(ldb, msg, msg,
1758                                          "sAMAccountType",
1759                                          account_type);
1760                 if (ret != LDB_SUCCESS) {
1761                         return ret;
1762                 }
1763                 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1764                 el2->flags = LDB_FLAG_MOD_REPLACE;
1765         }
1766
1767         el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1768         if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1769                 struct samldb_ctx *ac;
1770
1771                 ac = samldb_ctx_init(module, req);
1772                 if (ac == NULL)
1773                         return LDB_ERR_OPERATIONS_ERROR;
1774
1775                 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1776                         req->op.mod.message);
1777
1778                 return samldb_prim_group_change(ac);
1779         }
1780
1781         el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1782         if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1783                 uint32_t user_account_control;
1784
1785                 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1786                         req->op.mod.message);
1787
1788                 user_account_control = strtoul((const char *)el->values[0].data,
1789                         NULL, 0);
1790                 account_type = ds_uf2atype(user_account_control);
1791                 ret = samdb_msg_add_uint(ldb, msg, msg,
1792                                          "sAMAccountType",
1793                                          account_type);
1794                 if (ret != LDB_SUCCESS) {
1795                         return ret;
1796                 }
1797                 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1798                 el2->flags = LDB_FLAG_MOD_REPLACE;
1799
1800                 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1801                         ret = samdb_msg_add_string(ldb, msg, msg,
1802                                                    "isCriticalSystemObject", "TRUE");
1803                         if (ret != LDB_SUCCESS) {
1804                                 return ret;
1805                         }
1806                         el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1807                         el2->flags = LDB_FLAG_MOD_REPLACE;
1808
1809                         /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1810                         if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1811                                 ret = samdb_msg_add_uint(ldb, msg, msg,
1812                                                          "primaryGroupID", DOMAIN_RID_DCS);
1813                                 if (ret != LDB_SUCCESS) {
1814                                         return ret;
1815                                 }
1816                                 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1817                                 el2->flags = LDB_FLAG_MOD_REPLACE;
1818                         }
1819                 }
1820         }
1821
1822         el = ldb_msg_find_element(req->op.mod.message, "member");
1823         if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1824                 struct samldb_ctx *ac;
1825
1826                 ac = samldb_ctx_init(module, req);
1827                 if (ac == NULL)
1828                         return LDB_ERR_OPERATIONS_ERROR;
1829
1830                 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1831                         req->op.mod.message);
1832
1833                 return samldb_member_check(ac);
1834         }
1835
1836         /* nothing matched, go on */
1837         return ldb_next_request(module, req);
1838 }
1839
1840 /* delete */
1841 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1842 {
1843         struct samldb_ctx *ac;
1844
1845         if (ldb_dn_is_special(req->op.del.dn)) {
1846                 /* do not manipulate our control entries */
1847                 return ldb_next_request(module, req);
1848         }
1849
1850         ac = samldb_ctx_init(module, req);
1851         if (ac == NULL)
1852                 return LDB_ERR_OPERATIONS_ERROR;
1853
1854         return samldb_prim_group_users_check(ac);
1855 }
1856
1857 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1858 {
1859         struct ldb_context *ldb = ldb_module_get_ctx(module);
1860         struct dsdb_fsmo_extended_op *exop;
1861         int ret;
1862
1863         exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1864         if (!exop) {
1865                 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1866                 return LDB_ERR_PROTOCOL_ERROR;
1867         }
1868
1869         ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1870         if (ret != LDB_SUCCESS) {
1871                 return ret;
1872         }
1873
1874         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1875 }
1876
1877 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1878 {
1879         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1880                 return samldb_extended_allocate_rid_pool(module, req);
1881         }
1882
1883         return ldb_next_request(module, req);
1884 }
1885
1886
1887 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1888         .name          = "samldb",
1889         .add           = samldb_add,
1890         .modify        = samldb_modify,
1891         .del           = samldb_delete,
1892         .extended      = samldb_extended
1893 };
1894