s4-source4/dsdb/samdb/ldb_modules/samldb.c: Use DSDB_FLAG_NEXT_MODULE flag
[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" and
69          * "samldb_find_for_defaultObjectCategory" */
70         struct ldb_dn *res_dn;
71
72         /* all the async steps necessary to complete the operation */
73         struct samldb_step *steps;
74         struct samldb_step *curstep;
75
76         /* If someone set an ares to forward controls and response back to the caller */
77         struct ldb_reply *ares;
78 };
79
80 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
81                                           struct ldb_request *req)
82 {
83         struct ldb_context *ldb;
84         struct samldb_ctx *ac;
85
86         ldb = ldb_module_get_ctx(module);
87
88         ac = talloc_zero(req, struct samldb_ctx);
89         if (ac == NULL) {
90                 ldb_oom(ldb);
91                 return NULL;
92         }
93
94         ac->module = module;
95         ac->req = req;
96
97         return ac;
98 }
99
100 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
101 {
102         struct samldb_step *step, *stepper;
103
104         step = talloc_zero(ac, struct samldb_step);
105         if (step == NULL) {
106                 return LDB_ERR_OPERATIONS_ERROR;
107         }
108
109         step->fn = fn;
110
111         if (ac->steps == NULL) {
112                 ac->steps = step;
113                 ac->curstep = step;
114         } else {
115                 if (ac->curstep == NULL)
116                         return LDB_ERR_OPERATIONS_ERROR;
117                 for (stepper = ac->curstep; stepper->next != NULL;
118                         stepper = stepper->next);
119                 stepper->next = step;
120         }
121
122         return LDB_SUCCESS;
123 }
124
125 static int samldb_first_step(struct samldb_ctx *ac)
126 {
127         if (ac->steps == NULL) {
128                 return LDB_ERR_OPERATIONS_ERROR;
129         }
130
131         ac->curstep = ac->steps;
132         return ac->curstep->fn(ac);
133 }
134
135 static int samldb_next_step(struct samldb_ctx *ac)
136 {
137         if (ac->curstep->next) {
138                 ac->curstep = ac->curstep->next;
139                 return ac->curstep->fn(ac);
140         }
141
142         /* we exit the samldb module here */
143         /* If someone set an ares to forward controls and response back to the caller, use them */
144         if (ac->ares) {
145                 return ldb_module_done(ac->req, ac->ares->controls,
146                                        ac->ares->response, LDB_SUCCESS);
147         } else {
148                 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
149         }
150 }
151
152 static int samldb_generate_samAccountName(struct ldb_message *msg)
153 {
154         char *name;
155
156         /* Format: $000000-000000000000 */
157
158         name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
159                                 (unsigned int)generate_random(),
160                                 (unsigned int)generate_random(),
161                                 (unsigned int)generate_random());
162         if (name == NULL) {
163                 return LDB_ERR_OPERATIONS_ERROR;
164         }
165         return ldb_msg_add_steal_string(msg, "samAccountName", name);
166 }
167
168 /*
169  * samldb_check_samAccountName (async)
170  */
171
172 static int samldb_check_samAccountName_callback(struct ldb_request *req,
173                                                 struct ldb_reply *ares)
174 {
175         struct samldb_ctx *ac;
176         int ret;
177
178         ac = talloc_get_type(req->context, struct samldb_ctx);
179
180         if (ares->error != LDB_SUCCESS) {
181                 return ldb_module_done(ac->req, ares->controls,
182                                        ares->response, ares->error);
183         }
184
185         switch (ares->type) {
186         case LDB_REPLY_ENTRY:
187                 /* if we get an entry it means this samAccountName
188                  * already exists */
189                 return ldb_module_done(ac->req, NULL, NULL,
190                                        LDB_ERR_ENTRY_ALREADY_EXISTS);
191
192         case LDB_REPLY_REFERRAL:
193                 /* ignore */
194                 talloc_free(ares);
195                 ret = LDB_SUCCESS;
196                 break;
197
198         case LDB_REPLY_DONE:
199                 /* not found, go on */
200                 talloc_free(ares);
201                 ret = samldb_next_step(ac);
202                 break;
203         }
204
205         if (ret != LDB_SUCCESS) {
206                 return ldb_module_done(ac->req, NULL, NULL, ret);
207         }
208
209         return LDB_SUCCESS;
210 }
211
212 static int samldb_check_samAccountName(struct samldb_ctx *ac)
213 {
214         struct ldb_context *ldb;
215         struct ldb_request *req;
216         const char *name;
217         char *filter;
218         int ret;
219
220         ldb = ldb_module_get_ctx(ac->module);
221
222         if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
223                 ret = samldb_generate_samAccountName(ac->msg);
224                 if (ret != LDB_SUCCESS) {
225                         return ret;
226                 }
227         }
228
229         name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
230         if (name == NULL) {
231                 return LDB_ERR_OPERATIONS_ERROR;
232         }
233         filter = talloc_asprintf(ac, "samAccountName=%s",
234                                  ldb_binary_encode_string(ac, name));
235         if (filter == NULL) {
236                 return LDB_ERR_OPERATIONS_ERROR;
237         }
238
239         ret = ldb_build_search_req(&req, ldb, ac,
240                                    ldb_get_default_basedn(ldb),
241                                    LDB_SCOPE_SUBTREE,
242                                    filter, NULL,
243                                    NULL,
244                                    ac, samldb_check_samAccountName_callback,
245                                    ac->req);
246         talloc_free(filter);
247         if (ret != LDB_SUCCESS) {
248                 return ret;
249         }
250         return ldb_next_request(ac->module, req);
251 }
252
253
254 static int samldb_check_samAccountType(struct samldb_ctx *ac)
255 {
256         struct ldb_context *ldb;
257         unsigned int account_type;
258         unsigned int group_type;
259         unsigned int uac;
260         int ret;
261
262         ldb = ldb_module_get_ctx(ac->module);
263
264         /* make sure sAMAccountType is not specified */
265         if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
266                 ldb_asprintf_errstring(ldb,
267                         "sAMAccountType must not be specified!");
268                 return LDB_ERR_UNWILLING_TO_PERFORM;
269         }
270
271         if (strcmp("user", ac->type) == 0) {
272                 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
273                 if (uac == 0) {
274                         ldb_asprintf_errstring(ldb,
275                                 "userAccountControl invalid!");
276                         return LDB_ERR_UNWILLING_TO_PERFORM;
277                 } else {
278                         account_type = ds_uf2atype(uac);
279                         ret = samdb_msg_add_uint(ldb,
280                                                  ac->msg, ac->msg,
281                                                  "sAMAccountType",
282                                                  account_type);
283                         if (ret != LDB_SUCCESS) {
284                                 return ret;
285                         }
286                 }
287         } else
288         if (strcmp("group", ac->type) == 0) {
289
290                 group_type = samdb_result_uint(ac->msg, "groupType", 0);
291                 if (group_type == 0) {
292                         ldb_asprintf_errstring(ldb,
293                                 "groupType invalid!\n");
294                         return LDB_ERR_UNWILLING_TO_PERFORM;
295                 } else {
296                         account_type = ds_gtype2atype(group_type);
297                         ret = samdb_msg_add_uint(ldb,
298                                                  ac->msg, ac->msg,
299                                                  "sAMAccountType",
300                                                  account_type);
301                         if (ret != LDB_SUCCESS) {
302                                 return ret;
303                         }
304                 }
305         }
306
307         return samldb_next_step(ac);
308 }
309
310 static bool samldb_msg_add_sid(struct ldb_message *msg,
311                                 const char *name,
312                                 const struct dom_sid *sid)
313 {
314         struct ldb_val v;
315         enum ndr_err_code ndr_err;
316
317         ndr_err = ndr_push_struct_blob(&v, msg, sid,
318                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
319         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
320                 return false;
321         }
322         return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
323 }
324
325
326 /* allocate a SID using our RID Set */
327 static int samldb_allocate_sid(struct samldb_ctx *ac)
328 {
329         uint32_t rid;
330         int ret;
331         struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
332
333         ret = ridalloc_allocate_rid(ac->module, &rid);
334         if (ret != LDB_SUCCESS) {
335                 return ret;
336         }
337
338         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
339         if (ac->sid == NULL) {
340                 ldb_module_oom(ac->module);
341                 return LDB_ERR_OPERATIONS_ERROR;
342         }
343
344         if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
345                 return LDB_ERR_OPERATIONS_ERROR;
346         }
347
348         return samldb_next_step(ac);
349 }
350
351 /*
352  * samldb_dn_from_sid (async)
353  */
354
355 static int samldb_dn_from_sid(struct samldb_ctx *ac);
356
357 static int samldb_dn_from_sid_callback(struct ldb_request *req,
358         struct ldb_reply *ares)
359 {
360         struct ldb_context *ldb;
361         struct samldb_ctx *ac;
362         int ret;
363
364         ac = talloc_get_type(req->context, struct samldb_ctx);
365         ldb = ldb_module_get_ctx(ac->module);
366
367         if (!ares) {
368                 ret = LDB_ERR_OPERATIONS_ERROR;
369                 goto done;
370         }
371         if (ares->error != LDB_SUCCESS) {
372                 return ldb_module_done(ac->req, ares->controls,
373                                         ares->response, ares->error);
374         }
375
376         switch (ares->type) {
377         case LDB_REPLY_ENTRY:
378                 /* save entry */
379                 if (ac->res_dn != NULL) {
380                         /* one too many! */
381                         ldb_set_errstring(ldb,
382                                 "Invalid number of results while searching "
383                                 "for domain objects!");
384                         ret = LDB_ERR_OPERATIONS_ERROR;
385                         break;
386                 }
387                 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
388
389                 talloc_free(ares);
390                 ret = LDB_SUCCESS;
391                 break;
392
393         case LDB_REPLY_REFERRAL:
394                 /* ignore */
395                 talloc_free(ares);
396                 ret = LDB_SUCCESS;
397                 break;
398
399         case LDB_REPLY_DONE:
400                 talloc_free(ares);
401
402                 /* found or not found, go on */
403                 ret = samldb_next_step(ac);
404                 break;
405         }
406
407 done:
408         if (ret != LDB_SUCCESS) {
409                 return ldb_module_done(ac->req, NULL, NULL, ret);
410         }
411
412         return LDB_SUCCESS;
413 }
414
415 /* Finds the DN "res_dn" of an object with a given SID "sid" */
416 static int samldb_dn_from_sid(struct samldb_ctx *ac)
417 {
418         struct ldb_context *ldb;
419         static const char * const attrs[] = { NULL };
420         struct ldb_request *req;
421         char *filter;
422         int ret;
423
424         ldb = ldb_module_get_ctx(ac->module);
425
426         if (ac->sid == NULL)
427                 return LDB_ERR_OPERATIONS_ERROR;
428
429         filter = talloc_asprintf(ac, "(objectSid=%s)",
430                 ldap_encode_ndr_dom_sid(ac, ac->sid));
431         if (filter == NULL)
432                 return LDB_ERR_OPERATIONS_ERROR;
433
434         ret = ldb_build_search_req(&req, ldb, ac,
435                                 ldb_get_default_basedn(ldb),
436                                 LDB_SCOPE_SUBTREE,
437                                 filter, attrs,
438                                 NULL,
439                                 ac, samldb_dn_from_sid_callback,
440                                 ac->req);
441         if (ret != LDB_SUCCESS)
442                 return ret;
443
444         return ldb_next_request(ac->module, req);
445 }
446
447
448 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
449 {
450         struct ldb_context *ldb;
451         uint32_t rid;
452
453         ldb = ldb_module_get_ctx(ac->module);
454
455         rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
456         ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
457         if (ac->sid == NULL)
458                 return LDB_ERR_OPERATIONS_ERROR;
459         ac->res_dn = NULL;
460
461         return samldb_next_step(ac);
462 }
463
464 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
465 {
466         if (ac->res_dn == NULL) {
467                 struct ldb_context *ldb;
468                 ldb = ldb_module_get_ctx(ac->module);
469                 ldb_asprintf_errstring(ldb,
470                                        "Failed to find group sid %s!",
471                                        dom_sid_string(ac->sid, ac->sid));
472                 return LDB_ERR_UNWILLING_TO_PERFORM;
473         }
474
475         return samldb_next_step(ac);
476 }
477
478
479 /*
480  * samldb_find_for_defaultObjectCategory (async)
481  */
482
483 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
484                                                           struct ldb_reply *ares)
485 {
486         struct ldb_context *ldb;
487         struct samldb_ctx *ac;
488         int ret;
489
490         ac = talloc_get_type(req->context, struct samldb_ctx);
491         ldb = ldb_module_get_ctx(ac->module);
492
493         if (!ares) {
494                 ret = LDB_ERR_OPERATIONS_ERROR;
495                 goto done;
496         }
497         if (ares->error != LDB_SUCCESS) {
498                 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
499                         if (ldb_request_get_control(ac->req,
500                                                     LDB_CONTROL_RELAX_OID) != NULL) {
501                                 /* Don't be pricky when the DN doesn't exist */
502                                 /* if we have the RELAX control specified */
503                                 ac->res_dn = req->op.search.base;
504                                 return samldb_next_step(ac);
505                         } else {
506                                 ldb_set_errstring(ldb,
507                                         "samldb_find_defaultObjectCategory: "
508                                         "Invalid DN for 'defaultObjectCategory'!");
509                                 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
510                         }
511                 }
512
513                 return ldb_module_done(ac->req, ares->controls,
514                                        ares->response, ares->error);
515         }
516
517         switch (ares->type) {
518         case LDB_REPLY_ENTRY:
519                 ac->res_dn = talloc_steal(ac, ares->message->dn);
520
521                 ret = LDB_SUCCESS;
522                 break;
523
524         case LDB_REPLY_REFERRAL:
525                 /* ignore */
526                 talloc_free(ares);
527                 ret = LDB_SUCCESS;
528                 break;
529
530         case LDB_REPLY_DONE:
531                 talloc_free(ares);
532
533                 if (ac->res_dn != NULL) {
534                         /* when found go on */
535                         ret = samldb_next_step(ac);
536                 } else {
537                         ret = LDB_ERR_OPERATIONS_ERROR;
538                 }
539                 break;
540         }
541
542 done:
543         if (ret != LDB_SUCCESS) {
544                 return ldb_module_done(ac->req, NULL, NULL, ret);
545         }
546
547         return LDB_SUCCESS;
548 }
549
550 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
551 {
552         struct ldb_context *ldb;
553         struct ldb_request *req;
554         static const char *no_attrs[] = { NULL };
555         int ret;
556
557         ldb = ldb_module_get_ctx(ac->module);
558
559         ac->res_dn = NULL;
560
561         ret = ldb_build_search_req(&req, ldb, ac,
562                                    ac->dn, LDB_SCOPE_BASE,
563                                    "(objectClass=classSchema)", no_attrs,
564                                    NULL, ac,
565                                    samldb_find_for_defaultObjectCategory_callback,
566                                    ac->req);
567         if (ret != LDB_SUCCESS) {
568                 return ret;
569         }
570
571         ret = dsdb_request_add_controls(req,
572                                         DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
573         if (ret != LDB_SUCCESS) {
574                 return ret;
575         }
576
577         return ldb_next_request(ac->module, req);
578 }
579
580 /**
581  * msDS-IntId attributeSchema attribute handling
582  * during LDB_ADD request processing
583  */
584 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
585 {
586         int ret;
587         bool id_exists;
588         uint32_t msds_intid;
589         uint32_t system_flags;
590         struct ldb_context *ldb;
591         struct ldb_result *ldb_res;
592         struct ldb_dn *schema_dn;
593
594         ldb = ldb_module_get_ctx(ac->module);
595         schema_dn = ldb_get_schema_basedn(ldb);
596
597         /* replicated update should always go through */
598         if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
599                 return LDB_SUCCESS;
600         }
601
602         /* msDS-IntId is handled by system and should never be
603          * passed by clients */
604         if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
605                 return LDB_ERR_UNWILLING_TO_PERFORM;
606         }
607
608         /* do not generate msDS-IntId if Relax control is passed */
609         if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
610                 return LDB_SUCCESS;
611         }
612
613         /* check Functional Level */
614         if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
615                 return LDB_SUCCESS;
616         }
617
618         /* check systemFlags for SCHEMA_BASE_OBJECT flag */
619         system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
620         if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
621                 return LDB_SUCCESS;
622         }
623
624         /* Generate new value for msDs-IntId
625          * Value should be in 0x80000000..0xBFFFFFFF range */
626         msds_intid = generate_random() % 0X3FFFFFFF;
627         msds_intid += 0x80000000;
628
629         /* probe id values until unique one is found */
630         do {
631                 msds_intid++;
632                 if (msds_intid > 0xBFFFFFFF) {
633                         msds_intid = 0x80000001;
634                 }
635
636                 ret = dsdb_module_search(ac->module, ac,
637                                          &ldb_res,
638                                          schema_dn, LDB_SCOPE_ONELEVEL, NULL,
639                                          DSDB_FLAG_NEXT_MODULE,
640                                          "(msDS-IntId=%d)", msds_intid);
641                 if (ret != LDB_SUCCESS) {
642                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
643                                       __location__": Searching for msDS-IntId=%d failed - %s\n",
644                                       msds_intid,
645                                       ldb_errstring(ldb));
646                         return LDB_ERR_OPERATIONS_ERROR;
647                 }
648                 id_exists = (ldb_res->count > 0);
649
650                 talloc_free(ldb_res);
651         } while(id_exists);
652
653         return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
654 }
655
656
657 /*
658  * samldb_add_entry (async)
659  */
660
661 static int samldb_add_entry_callback(struct ldb_request *req,
662                                         struct ldb_reply *ares)
663 {
664         struct ldb_context *ldb;
665         struct samldb_ctx *ac;
666         int ret;
667
668         ac = talloc_get_type(req->context, struct samldb_ctx);
669         ldb = ldb_module_get_ctx(ac->module);
670
671         if (!ares) {
672                 return ldb_module_done(ac->req, NULL, NULL,
673                                         LDB_ERR_OPERATIONS_ERROR);
674         }
675
676         if (ares->type == LDB_REPLY_REFERRAL) {
677                 return ldb_module_send_referral(ac->req, ares->referral);
678         }
679
680         if (ares->error != LDB_SUCCESS) {
681                 return ldb_module_done(ac->req, ares->controls,
682                                         ares->response, ares->error);
683         }
684         if (ares->type != LDB_REPLY_DONE) {
685                 ldb_set_errstring(ldb,
686                         "Invalid reply type!\n");
687                 return ldb_module_done(ac->req, NULL, NULL,
688                                         LDB_ERR_OPERATIONS_ERROR);
689         }
690
691         /* The caller may wish to get controls back from the add */
692         ac->ares = talloc_steal(ac, ares);
693
694         ret = samldb_next_step(ac);
695         if (ret != LDB_SUCCESS) {
696                 return ldb_module_done(ac->req, NULL, NULL, ret);
697         }
698         return ret;
699 }
700
701 static int samldb_add_entry(struct samldb_ctx *ac)
702 {
703         struct ldb_context *ldb;
704         struct ldb_request *req;
705         int ret;
706
707         ldb = ldb_module_get_ctx(ac->module);
708
709         ret = ldb_build_add_req(&req, ldb, ac,
710                                 ac->msg,
711                                 ac->req->controls,
712                                 ac, samldb_add_entry_callback,
713                                 ac->req);
714         if (ret != LDB_SUCCESS) {
715                 return ret;
716         }
717
718         return ldb_next_request(ac->module, req);
719 }
720
721 /*
722  * return true if msg carries an attributeSchema that is intended to be RODC
723  * filtered but is also a system-critical attribute.
724  */
725 static bool check_rodc_critical_attribute(struct ldb_message *msg)
726 {
727         uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
728
729         schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
730         searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
731         rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
732
733         if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
734                 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
735                 return true;
736         } else {
737                 return false;
738         }
739 }
740
741
742 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
743 {
744         struct ldb_context *ldb;
745         struct loadparm_context *lp_ctx;
746         enum sid_generator sid_generator;
747         int ret;
748
749         ldb = ldb_module_get_ctx(ac->module);
750
751         /* Add informations for the different account types */
752         ac->type = type;
753         if (strcmp(ac->type, "user") == 0) {
754                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
755                         "userAccountControl", "546");
756                 if (ret != LDB_SUCCESS) return ret;
757                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
758                         "badPwdCount", "0");
759                 if (ret != LDB_SUCCESS) return ret;
760                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
761                         "codePage", "0");
762                 if (ret != LDB_SUCCESS) return ret;
763                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
764                         "countryCode", "0");
765                 if (ret != LDB_SUCCESS) return ret;
766                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
767                         "badPasswordTime", "0");
768                 if (ret != LDB_SUCCESS) return ret;
769                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
770                         "lastLogoff", "0");
771                 if (ret != LDB_SUCCESS) return ret;
772                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
773                         "lastLogon", "0");
774                 if (ret != LDB_SUCCESS) return ret;
775                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
776                         "pwdLastSet", "0");
777                 if (ret != LDB_SUCCESS) return ret;
778                 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
779                         ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
780                                                  "primaryGroupID", DOMAIN_RID_USERS);
781                         if (ret != LDB_SUCCESS) return ret;
782                 }
783                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
784                         "accountExpires", "9223372036854775807");
785                 if (ret != LDB_SUCCESS) return ret;
786                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
787                         "logonCount", "0");
788                 if (ret != LDB_SUCCESS) return ret;
789         } else if (strcmp(ac->type, "group") == 0) {
790                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
791                         "groupType", "-2147483646");
792                 if (ret != LDB_SUCCESS) return ret;
793         } else if (strcmp(ac->type, "classSchema") == 0) {
794                 const struct ldb_val *rdn_value, *def_obj_cat_val;
795
796                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
797                                                   "rdnAttId", "cn");
798                 if (ret != LDB_SUCCESS) return ret;
799
800                 /* do not allow to mark an attributeSchema as RODC filtered if it
801                  * is system-critical */
802                 if (check_rodc_critical_attribute(ac->msg)) {
803                         ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
804                                                ldb_dn_get_linearized(ac->msg->dn));
805                         return LDB_ERR_UNWILLING_TO_PERFORM;
806                 }
807
808
809                 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
810                 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
811                         /* the RDN has prefix "CN" */
812                         ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
813                                 samdb_cn_to_lDAPDisplayName(ac,
814                                         (const char *) rdn_value->data));
815                         if (ret != LDB_SUCCESS) {
816                                 ldb_oom(ldb);
817                                 return ret;
818                         }
819                 }
820
821                 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
822                         struct GUID guid;
823                         /* a new GUID */
824                         guid = GUID_random();
825                         ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
826                         if (ret != LDB_SUCCESS) {
827                                 ldb_oom(ldb);
828                                 return ret;
829                         }
830                 }
831
832                 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
833                                                        "defaultObjectCategory");
834                 if (def_obj_cat_val != NULL) {
835                         /* "defaultObjectCategory" has been set by the caller.
836                          * Do some checks for consistency.
837                          * NOTE: The real constraint check (that
838                          * 'defaultObjectCategory' is the DN of the new
839                          * objectclass or any parent of it) is still incomplete.
840                          * For now we say that 'defaultObjectCategory' is valid
841                          * if it exists and it is of objectclass "classSchema".
842                          */
843                         ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
844                         if (ac->dn == NULL) {
845                                 ldb_set_errstring(ldb,
846                                                   "Invalid DN for 'defaultObjectCategory'!");
847                                 return LDB_ERR_CONSTRAINT_VIOLATION;
848                         }
849                 } else {
850                         /* "defaultObjectCategory" has not been set by the
851                          * caller. Use the entry DN for it. */
852                         ac->dn = ac->msg->dn;
853
854                         ret = samdb_msg_add_string(ldb, ac, ac->msg,
855                                                    "defaultObjectCategory",
856                                                    ldb_dn_get_linearized(ac->dn));
857                         if (ret != LDB_SUCCESS) {
858                                 ldb_oom(ldb);
859                                 return ret;
860                         }
861                 }
862
863                 ret = samldb_add_step(ac, samldb_add_entry);
864                 if (ret != LDB_SUCCESS) return ret;
865
866                 /* Now perform the checks for the 'defaultObjectCategory'. The
867                  * lookup DN was already saved in "ac->dn" */
868                 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
869                 if (ret != LDB_SUCCESS) return ret;
870
871                 return samldb_first_step(ac);
872         } else if (strcmp(ac->type, "attributeSchema") == 0) {
873                 const struct ldb_val *rdn_value;
874                 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
875                 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
876                         /* the RDN has prefix "CN" */
877                         ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
878                                 samdb_cn_to_lDAPDisplayName(ac,
879                                         (const char *) rdn_value->data));
880                         if (ret != LDB_SUCCESS) {
881                                 ldb_oom(ldb);
882                                 return ret;
883                         }
884                 }
885
886                 /* do not allow to mark an attributeSchema as RODC filtered if it
887                  * is system-critical */
888                 if (check_rodc_critical_attribute(ac->msg)) {
889                         ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
890                                                ldb_dn_get_linearized(ac->msg->dn));
891                         return LDB_ERR_UNWILLING_TO_PERFORM;
892                 }
893
894                 ret = samdb_find_or_add_attribute(ldb, ac->msg,
895                                                   "isSingleValued", "FALSE");
896                 if (ret != LDB_SUCCESS) return ret;
897
898                 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
899                         struct GUID guid;
900                         /* a new GUID */
901                         guid = GUID_random();
902                         ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
903                         if (ret != LDB_SUCCESS) {
904                                 ldb_oom(ldb);
905                                 return ret;
906                         }
907                 }
908
909                 /* handle msDS-IntID attribute */
910                 ret = samldb_add_handle_msDS_IntId(ac);
911                 if (ret != LDB_SUCCESS) return ret;
912
913                 ret = samldb_add_step(ac, samldb_add_entry);
914                 if (ret != LDB_SUCCESS) return ret;
915
916                 return samldb_first_step(ac);
917         } else {
918                 ldb_asprintf_errstring(ldb,
919                         "Invalid entry type!");
920                 return LDB_ERR_OPERATIONS_ERROR;
921         }
922
923         /* check if we have a valid samAccountName */
924         ret = samldb_add_step(ac, samldb_check_samAccountName);
925         if (ret != LDB_SUCCESS) return ret;
926
927         /* check account_type/group_type */
928         ret = samldb_add_step(ac, samldb_check_samAccountType);
929         if (ret != LDB_SUCCESS) return ret;
930
931         /* check if we have a valid primary group ID */
932         if (strcmp(ac->type, "user") == 0) {
933                 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
934                 if (ret != LDB_SUCCESS) return ret;
935                 ret = samldb_add_step(ac, samldb_dn_from_sid);
936                 if (ret != LDB_SUCCESS) return ret;
937                 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
938                 if (ret != LDB_SUCCESS) return ret;
939         }
940
941         lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
942                  struct loadparm_context);
943
944         /* don't allow objectSID to be specified without the RELAX control */
945         ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
946         if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
947             !dsdb_module_am_system(ac->module)) {
948                 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
949                                        ldb_dn_get_linearized(ac->msg->dn));
950                 return LDB_ERR_UNWILLING_TO_PERFORM;
951         }
952
953         if ( ! ac->sid) {
954                 sid_generator = lp_sid_generator(lp_ctx);
955                 if (sid_generator == SID_GENERATOR_INTERNAL) {
956                         ret = samldb_add_step(ac, samldb_allocate_sid);
957                         if (ret != LDB_SUCCESS) return ret;
958                 }
959         }
960
961         /* finally proceed with adding the entry */
962         ret = samldb_add_step(ac, samldb_add_entry);
963         if (ret != LDB_SUCCESS) return ret;
964
965         return samldb_first_step(ac);
966 }
967
968 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
969 {
970         struct ldb_context *ldb;
971         int ret;
972
973         ldb = ldb_module_get_ctx(ac->module);
974
975         ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
976         if (ac->sid == NULL) {
977                 ac->sid = dom_sid_parse_talloc(ac->msg,
978                            (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
979                 if (!ac->sid) {
980                         ldb_set_errstring(ldb,
981                                         "No valid SID found in "
982                                         "ForeignSecurityPrincipal CN!");
983                         talloc_free(ac);
984                         return LDB_ERR_CONSTRAINT_VIOLATION;
985                 }
986                 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
987                         talloc_free(ac);
988                         return LDB_ERR_OPERATIONS_ERROR;
989                 }
990         }
991
992         /* finally proceed with adding the entry */
993         ret = samldb_add_step(ac, samldb_add_entry);
994         if (ret != LDB_SUCCESS) return ret;
995
996         return samldb_first_step(ac);
997 }
998
999 static int samldb_schema_info_update(struct samldb_ctx *ac)
1000 {
1001         WERROR werr;
1002         struct ldb_context *ldb;
1003         struct dsdb_schema *schema;
1004
1005         /* replicated update should always go through */
1006         if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1007                 return LDB_SUCCESS;
1008         }
1009
1010         /* do not update schemaInfo during provisioning */
1011         if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1012                 return LDB_SUCCESS;
1013         }
1014
1015         ldb = ldb_module_get_ctx(ac->module);
1016         schema = dsdb_get_schema(ldb, NULL);
1017         if (!schema) {
1018                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1019                               "samldb_schema_info_update: no dsdb_schema loaded");
1020                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1021                 return LDB_ERR_OPERATIONS_ERROR;
1022         }
1023
1024         werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
1025         if (!W_ERROR_IS_OK(werr)) {
1026                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1027                               "samldb_schema_info_update: "
1028                               "dsdb_module_schema_info_update failed with %s",
1029                               win_errstr(werr));
1030                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1031                 return LDB_ERR_OPERATIONS_ERROR;
1032         }
1033
1034         return LDB_SUCCESS;
1035 }
1036
1037
1038 static int samldb_prim_group_change(struct samldb_ctx *ac)
1039 {
1040         struct ldb_context *ldb;
1041         const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1042         struct ldb_result *res;
1043         struct ldb_message_element *el;
1044         struct ldb_message *msg;
1045         uint32_t rid;
1046         struct dom_sid *sid;
1047         struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1048         int ret;
1049
1050         ldb = ldb_module_get_ctx(ac->module);
1051
1052         /* Fetch informations from the existing object */
1053
1054         ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1055                          NULL);
1056         if (ret != LDB_SUCCESS) {
1057                 return ret;
1058         }
1059
1060         /* Finds out the DN of the old primary group */
1061
1062         rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1063         if (rid == (uint32_t) -1) {
1064                 /* User objects do always have a mandatory "primaryGroupID"
1065                  * attribute. If this doesn't exist then the object is of the
1066                  * wrong type. This is the exact Windows error code */
1067                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1068         }
1069
1070         sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1071         if (sid == NULL) {
1072                 return LDB_ERR_OPERATIONS_ERROR;
1073         }
1074
1075         prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1076                                              dom_sid_string(ac, sid));
1077         if (prev_prim_group_dn == NULL) {
1078                 return LDB_ERR_OPERATIONS_ERROR;
1079         }
1080
1081         /* Finds out the DN of the new primary group */
1082
1083         rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1084         if (rid == (uint32_t) -1) {
1085                 /* we aren't affected of any primary group change */
1086                 return LDB_SUCCESS;
1087         }
1088
1089         sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1090         if (sid == NULL) {
1091                 return LDB_ERR_OPERATIONS_ERROR;
1092         }
1093
1094         new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1095                                             dom_sid_string(ac, sid));
1096         if (new_prim_group_dn == NULL) {
1097                 /* Here we know if the specified new primary group candidate is
1098                  * valid or not. */
1099                 return LDB_ERR_UNWILLING_TO_PERFORM;
1100         }
1101
1102         /* Only update the "member" attributes when we really do have a change */
1103         if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1104                 /* We need to be already a normal member of the new primary
1105                  * group in order to be successful. */
1106                 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1107                                           ldb_dn_get_linearized(new_prim_group_dn));
1108                 if (el == NULL) {
1109                         return LDB_ERR_UNWILLING_TO_PERFORM;
1110                 }
1111
1112                 /* Remove the "member" attribute on the new primary group */
1113                 msg = talloc_zero(ac, struct ldb_message);
1114                 msg->dn = new_prim_group_dn;
1115
1116                 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
1117                                            ldb_dn_get_linearized(ac->msg->dn));
1118                 if (ret != LDB_SUCCESS) {
1119                         return ret;
1120                 }
1121
1122                 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1123                 if (ret != LDB_SUCCESS) {
1124                         return ret;
1125                 }
1126
1127                 /* Add a "member" attribute for the previous primary group */
1128                 msg = talloc_zero(ac, struct ldb_message);
1129                 msg->dn = prev_prim_group_dn;
1130
1131                 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1132                                            ldb_dn_get_linearized(ac->msg->dn));
1133                 if (ret != LDB_SUCCESS) {
1134                         return ret;
1135                 }
1136
1137                 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1138                 if (ret != LDB_SUCCESS) {
1139                         return ret;
1140                 }
1141         }
1142
1143         return LDB_SUCCESS;
1144 }
1145
1146
1147 static int samldb_member_check(struct samldb_ctx *ac)
1148 {
1149         struct ldb_context *ldb;
1150         struct ldb_message_element *el;
1151         struct ldb_dn *member_dn, *group_dn;
1152         uint32_t prim_group_rid;
1153         struct dom_sid *sid;
1154         unsigned int i;
1155
1156         ldb = ldb_module_get_ctx(ac->module);
1157
1158         el = ldb_msg_find_element(ac->msg, "member");
1159         if (el == NULL) {
1160                 /* we aren't affected */
1161                 return LDB_SUCCESS;
1162         }
1163
1164         for (i = 0; i < el->num_values; i++) {
1165                 /* Denies to add "member"s to groups which are primary ones
1166                  * for them */
1167                 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1168                 if (!ldb_dn_validate(member_dn)) {
1169                         return LDB_ERR_OPERATIONS_ERROR;
1170                 }
1171
1172                 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1173                                                    member_dn, "primaryGroupID",
1174                                                    NULL);
1175                 if (prim_group_rid == (uint32_t) -1) {
1176                         /* the member hasn't to be a user account -> therefore
1177                          * no check needed in this case. */
1178                         continue;
1179                 }
1180
1181                 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1182                                       prim_group_rid);
1183                 if (sid == NULL) {
1184                         return LDB_ERR_OPERATIONS_ERROR;
1185                 }
1186
1187                 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1188                                            dom_sid_string(ac, sid));
1189                 if (group_dn == NULL) {
1190                         return LDB_ERR_OPERATIONS_ERROR;
1191                 }
1192
1193                 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1194                         return LDB_ERR_ENTRY_ALREADY_EXISTS;
1195                 }
1196         }
1197
1198         return LDB_SUCCESS;
1199 }
1200
1201
1202 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1203 {
1204         struct ldb_context *ldb;
1205         struct dom_sid *sid;
1206         uint32_t rid;
1207         NTSTATUS status;
1208         int count;
1209
1210         ldb = ldb_module_get_ctx(ac->module);
1211
1212         /* Finds out the SID/RID of the SAM object */
1213         sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1214                                    NULL);
1215         if (sid == NULL) {
1216                 /* No SID - it might not be a SAM object - therefore ok */
1217                 return LDB_SUCCESS;
1218         }
1219         status = dom_sid_split_rid(ac, sid, NULL, &rid);
1220         if (!NT_STATUS_IS_OK(status)) {
1221                 return LDB_ERR_OPERATIONS_ERROR;
1222         }
1223         if (rid == 0) {
1224                 /* Special object (security principal?) */
1225                 return LDB_SUCCESS;
1226         }
1227
1228         /* Deny delete requests from groups which are primary ones */
1229         count = samdb_search_count(ldb, NULL,
1230                                    "(&(primaryGroupID=%u)(objectClass=user))",
1231                                    rid);
1232         if (count < 0) {
1233                 return LDB_ERR_OPERATIONS_ERROR;
1234         }
1235         if (count > 0) {
1236                 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1237         }
1238
1239         return LDB_SUCCESS;
1240 }
1241
1242
1243 /* add */
1244 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1245 {
1246         struct ldb_context *ldb;
1247         struct samldb_ctx *ac;
1248         int ret;
1249
1250         ldb = ldb_module_get_ctx(module);
1251         ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1252
1253         /* do not manipulate our control entries */
1254         if (ldb_dn_is_special(req->op.add.message->dn)) {
1255                 return ldb_next_request(module, req);
1256         }
1257
1258         ac = samldb_ctx_init(module, req);
1259         if (ac == NULL) {
1260                 return LDB_ERR_OPERATIONS_ERROR;
1261         }
1262
1263         /* build the new msg */
1264         ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1265         if (!ac->msg) {
1266                 talloc_free(ac);
1267                 ldb_debug(ldb, LDB_DEBUG_FATAL,
1268                           "samldb_add: ldb_msg_copy failed!\n");
1269                 return LDB_ERR_OPERATIONS_ERROR;
1270         }
1271
1272         if (samdb_find_attribute(ldb, ac->msg,
1273                                  "objectclass", "user") != NULL) {
1274                 return samldb_fill_object(ac, "user");
1275         }
1276
1277         if (samdb_find_attribute(ldb, ac->msg,
1278                                  "objectclass", "group") != NULL) {
1279                 return samldb_fill_object(ac, "group");
1280         }
1281
1282         /* perhaps a foreignSecurityPrincipal? */
1283         if (samdb_find_attribute(ldb, ac->msg,
1284                                  "objectclass",
1285                                  "foreignSecurityPrincipal") != NULL) {
1286                 return samldb_fill_foreignSecurityPrincipal_object(ac);
1287         }
1288
1289         if (samdb_find_attribute(ldb, ac->msg,
1290                                  "objectclass", "classSchema") != NULL) {
1291                 ret = samldb_schema_info_update(ac);
1292                 if (ret != LDB_SUCCESS) {
1293                         talloc_free(ac);
1294                         return ret;
1295                 }
1296
1297                 return samldb_fill_object(ac, "classSchema");
1298         }
1299
1300         if (samdb_find_attribute(ldb, ac->msg,
1301                                  "objectclass", "attributeSchema") != NULL) {
1302                 ret = samldb_schema_info_update(ac);
1303                 if (ret != LDB_SUCCESS) {
1304                         talloc_free(ac);
1305                         return ret;
1306                 }
1307
1308                 return samldb_fill_object(ac, "attributeSchema");
1309         }
1310
1311         talloc_free(ac);
1312
1313         /* nothing matched, go on */
1314         return ldb_next_request(module, req);
1315 }
1316
1317 /* modify */
1318 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1319 {
1320         struct ldb_context *ldb;
1321         struct samldb_ctx *ac;
1322         struct ldb_message *msg;
1323         struct ldb_message_element *el, *el2;
1324         int ret;
1325         uint32_t account_type;
1326
1327         if (ldb_dn_is_special(req->op.mod.message->dn)) {
1328                 /* do not manipulate our control entries */
1329                 return ldb_next_request(module, req);
1330         }
1331
1332         ldb = ldb_module_get_ctx(module);
1333
1334         if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1335                 ldb_asprintf_errstring(ldb,
1336                         "sAMAccountType must not be specified!");
1337                 return LDB_ERR_UNWILLING_TO_PERFORM;
1338         }
1339
1340         /* msDS-IntId is not allowed to be modified
1341          * except when modification comes from replication */
1342         if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1343                 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1344                         return LDB_ERR_CONSTRAINT_VIOLATION;
1345                 }
1346         }
1347
1348         ac = samldb_ctx_init(module, req);
1349         if (ac == NULL) {
1350                 return LDB_ERR_OPERATIONS_ERROR;
1351         }
1352
1353         /* TODO: do not modify original request, create a new one */
1354
1355         el = ldb_msg_find_element(req->op.mod.message, "groupType");
1356         if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1357                 uint32_t group_type;
1358
1359                 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1360                         req->op.mod.message);
1361
1362                 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1363                 account_type =  ds_gtype2atype(group_type);
1364                 ret = samdb_msg_add_uint(ldb, msg, msg,
1365                                          "sAMAccountType",
1366                                          account_type);
1367                 if (ret != LDB_SUCCESS) {
1368                         return ret;
1369                 }
1370                 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1371                 el2->flags = LDB_FLAG_MOD_REPLACE;
1372         }
1373         if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
1374                 return LDB_ERR_UNWILLING_TO_PERFORM;
1375         }
1376
1377         el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1378         if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1379                 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1380                         req->op.mod.message);
1381
1382                 ret = samldb_prim_group_change(ac);
1383                 if (ret != LDB_SUCCESS) {
1384                         return ret;
1385                 }
1386         }
1387         if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
1388                 return LDB_ERR_UNWILLING_TO_PERFORM;
1389         }
1390
1391         el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1392         if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1393                 uint32_t user_account_control;
1394
1395                 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1396                         req->op.mod.message);
1397
1398                 user_account_control = strtoul((const char *)el->values[0].data,
1399                         NULL, 0);
1400                 account_type = ds_uf2atype(user_account_control);
1401                 ret = samdb_msg_add_uint(ldb, msg, msg,
1402                                          "sAMAccountType",
1403                                          account_type);
1404                 if (ret != LDB_SUCCESS) {
1405                         return ret;
1406                 }
1407                 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1408                 el2->flags = LDB_FLAG_MOD_REPLACE;
1409
1410                 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1411                         ret = samdb_msg_add_string(ldb, msg, msg,
1412                                                    "isCriticalSystemObject", "TRUE");
1413                         if (ret != LDB_SUCCESS) {
1414                                 return ret;
1415                         }
1416                         el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1417                         el2->flags = LDB_FLAG_MOD_REPLACE;
1418
1419                         /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1420                         if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1421                                 ret = samdb_msg_add_uint(ldb, msg, msg,
1422                                                          "primaryGroupID", DOMAIN_RID_DCS);
1423                                 if (ret != LDB_SUCCESS) {
1424                                         return ret;
1425                                 }
1426                                 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1427                                 el2->flags = LDB_FLAG_MOD_REPLACE;
1428                         }
1429                 }
1430         }
1431         if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
1432                 return LDB_ERR_UNWILLING_TO_PERFORM;
1433         }
1434
1435         el = ldb_msg_find_element(req->op.mod.message, "member");
1436         if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1437                 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1438                         req->op.mod.message);
1439
1440                 ret = samldb_member_check(ac);
1441                 if (ret != LDB_SUCCESS) {
1442                         return ret;
1443                 }
1444         }
1445
1446         return ldb_next_request(module, req);
1447 }
1448
1449 /* delete */
1450 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1451 {
1452         struct samldb_ctx *ac;
1453         int ret;
1454
1455         if (ldb_dn_is_special(req->op.del.dn)) {
1456                 /* do not manipulate our control entries */
1457                 return ldb_next_request(module, req);
1458         }
1459
1460         ac = samldb_ctx_init(module, req);
1461         if (ac == NULL)
1462                 return LDB_ERR_OPERATIONS_ERROR;
1463
1464         ret = samldb_prim_group_users_check(ac);
1465         if (ret != LDB_SUCCESS) {
1466                 return ret;
1467         }
1468
1469         return ldb_next_request(module, req);
1470 }
1471
1472 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1473 {
1474         struct ldb_context *ldb = ldb_module_get_ctx(module);
1475         struct dsdb_fsmo_extended_op *exop;
1476         int ret;
1477
1478         exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1479         if (!exop) {
1480                 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1481                 return LDB_ERR_PROTOCOL_ERROR;
1482         }
1483
1484         ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1485         if (ret != LDB_SUCCESS) {
1486                 return ret;
1487         }
1488
1489         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1490 }
1491
1492 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1493 {
1494         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1495                 return samldb_extended_allocate_rid_pool(module, req);
1496         }
1497
1498         return ldb_next_request(module, req);
1499 }
1500
1501
1502 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1503         .name          = "samldb",
1504         .add           = samldb_add,
1505         .modify        = samldb_modify,
1506         .del           = samldb_delete,
1507         .extended      = samldb_extended
1508 };
1509