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