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