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