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