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