libndr: Avoid assigning duplicate versions to symbols
[amitay/samba.git] / source4 / dsdb / common / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4
5    Copyright (C) Andrew Tridgell 2004
6    Copyright (C) Volker Lendecke 2004
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "events/events.h"
26 #include "ldb.h"
27 #include "ldb_module.h"
28 #include "ldb_errors.h"
29 #include "../lib/util/util_ldb.h"
30 #include "../lib/crypto/crypto.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "librpc/gen_ndr/ndr_misc.h"
35 #include "../libds/common/flags.h"
36 #include "dsdb/common/proto.h"
37 #include "libcli/ldap/ldap_ndr.h"
38 #include "param/param.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "librpc/gen_ndr/ndr_drsblobs.h"
41 #include "system/locale.h"
42 #include "system/filesys.h"
43 #include "lib/util/tsort.h"
44 #include "dsdb/common/util.h"
45 #include "lib/socket/socket.h"
46 #include "librpc/gen_ndr/irpc.h"
47 #include "libds/common/flag_mapping.h"
48 #include "lib/util/access.h"
49 #include "lib/util/util_str_hex.h"
50 #include "lib/util/sys_rw_data.h"
51 #include "libcli/util/ntstatus.h"
52 #include "lib/util/smb_strtox.h"
53
54 #undef strncasecmp
55 #undef strcasecmp
56
57 /*
58  * This included to allow us to handle DSDB_FLAG_REPLICATED_UPDATE in
59  * dsdb_request_add_controls()
60  */
61 #include "dsdb/samdb/ldb_modules/util.h"
62
63 /* default is 30 minutes: -1e7 * 30 * 60 */
64 #define DEFAULT_OBSERVATION_WINDOW              -18000000000
65
66 /*
67   search the sam for the specified attributes in a specific domain, filter on
68   objectSid being in domain_sid.
69 */
70 int samdb_search_domain(struct ldb_context *sam_ldb,
71                         TALLOC_CTX *mem_ctx, 
72                         struct ldb_dn *basedn,
73                         struct ldb_message ***res,
74                         const char * const *attrs,
75                         const struct dom_sid *domain_sid,
76                         const char *format, ...)  _PRINTF_ATTRIBUTE(7,8)
77 {
78         va_list ap;
79         int i, count;
80
81         va_start(ap, format);
82         count = gendb_search_v(sam_ldb, mem_ctx, basedn,
83                                res, attrs, format, ap);
84         va_end(ap);
85
86         i=0;
87
88         while (i<count) {
89                 struct dom_sid *entry_sid;
90
91                 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
92
93                 if ((entry_sid == NULL) ||
94                     (!dom_sid_in_domain(domain_sid, entry_sid))) {
95                         /* Delete that entry from the result set */
96                         (*res)[i] = (*res)[count-1];
97                         count -= 1;
98                         talloc_free(entry_sid);
99                         continue;
100                 }
101                 talloc_free(entry_sid);
102                 i += 1;
103         }
104
105         return count;
106 }
107
108 /*
109   search the sam for a single string attribute in exactly 1 record
110 */
111 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
112                                   TALLOC_CTX *mem_ctx,
113                                   struct ldb_dn *basedn,
114                                   const char *attr_name,
115                                   const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
116 {
117         int count;
118         const char *attrs[2] = { NULL, NULL };
119         struct ldb_message **res = NULL;
120
121         attrs[0] = attr_name;
122
123         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
124         if (count > 1) {                
125                 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n", 
126                          attr_name, format, count));
127         }
128         if (count != 1) {
129                 talloc_free(res);
130                 return NULL;
131         }
132
133         return ldb_msg_find_attr_as_string(res[0], attr_name, NULL);
134 }
135
136 /*
137   search the sam for a single string attribute in exactly 1 record
138 */
139 const char *samdb_search_string(struct ldb_context *sam_ldb,
140                                 TALLOC_CTX *mem_ctx,
141                                 struct ldb_dn *basedn,
142                                 const char *attr_name,
143                                 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
144 {
145         va_list ap;
146         const char *str;
147
148         va_start(ap, format);
149         str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
150         va_end(ap);
151
152         return str;
153 }
154
155 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
156                                TALLOC_CTX *mem_ctx,
157                                struct ldb_dn *basedn,
158                                const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
159 {
160         va_list ap;
161         struct ldb_dn *ret;
162         struct ldb_message **res = NULL;
163         int count;
164
165         va_start(ap, format);
166         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
167         va_end(ap);
168
169         if (count != 1) return NULL;
170
171         ret = talloc_steal(mem_ctx, res[0]->dn);
172         talloc_free(res);
173
174         return ret;
175 }
176
177 /*
178   search the sam for a dom_sid attribute in exactly 1 record
179 */
180 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
181                                      TALLOC_CTX *mem_ctx,
182                                      struct ldb_dn *basedn,
183                                      const char *attr_name,
184                                      const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
185 {
186         va_list ap;
187         int count;
188         struct ldb_message **res;
189         const char *attrs[2] = { NULL, NULL };
190         struct dom_sid *sid;
191
192         attrs[0] = attr_name;
193
194         va_start(ap, format);
195         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
196         va_end(ap);
197         if (count > 1) {                
198                 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n", 
199                          attr_name, format, count));
200         }
201         if (count != 1) {
202                 talloc_free(res);
203                 return NULL;
204         }
205         sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
206         talloc_free(res);
207         return sid;     
208 }
209
210 /*
211   search the sam for a single integer attribute in exactly 1 record
212 */
213 unsigned int samdb_search_uint(struct ldb_context *sam_ldb,
214                          TALLOC_CTX *mem_ctx,
215                          unsigned int default_value,
216                          struct ldb_dn *basedn,
217                          const char *attr_name,
218                          const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
219 {
220         va_list ap;
221         int count;
222         struct ldb_message **res;
223         const char *attrs[2] = { NULL, NULL };
224
225         attrs[0] = attr_name;
226
227         va_start(ap, format);
228         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
229         va_end(ap);
230
231         if (count != 1) {
232                 return default_value;
233         }
234
235         return ldb_msg_find_attr_as_uint(res[0], attr_name, default_value);
236 }
237
238 /*
239   search the sam for a single signed 64 bit integer attribute in exactly 1 record
240 */
241 int64_t samdb_search_int64(struct ldb_context *sam_ldb,
242                            TALLOC_CTX *mem_ctx,
243                            int64_t default_value,
244                            struct ldb_dn *basedn,
245                            const char *attr_name,
246                            const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
247 {
248         va_list ap;
249         int count;
250         struct ldb_message **res;
251         const char *attrs[2] = { NULL, NULL };
252
253         attrs[0] = attr_name;
254
255         va_start(ap, format);
256         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
257         va_end(ap);
258
259         if (count != 1) {
260                 return default_value;
261         }
262
263         return ldb_msg_find_attr_as_int64(res[0], attr_name, default_value);
264 }
265
266 /*
267   search the sam for multipe records each giving a single string attribute
268   return the number of matches, or -1 on error
269 */
270 int samdb_search_string_multiple(struct ldb_context *sam_ldb,
271                                  TALLOC_CTX *mem_ctx,
272                                  struct ldb_dn *basedn,
273                                  const char ***strs,
274                                  const char *attr_name,
275                                  const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
276 {
277         va_list ap;
278         int count, i;
279         const char *attrs[2] = { NULL, NULL };
280         struct ldb_message **res = NULL;
281
282         attrs[0] = attr_name;
283
284         va_start(ap, format);
285         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
286         va_end(ap);
287
288         if (count <= 0) {
289                 return count;
290         }
291
292         /* make sure its single valued */
293         for (i=0;i<count;i++) {
294                 if (res[i]->num_elements != 1) {
295                         DEBUG(1,("samdb: search for %s %s not single valued\n", 
296                                  attr_name, format));
297                         talloc_free(res);
298                         return -1;
299                 }
300         }
301
302         *strs = talloc_array(mem_ctx, const char *, count+1);
303         if (! *strs) {
304                 talloc_free(res);
305                 return -1;
306         }
307
308         for (i=0;i<count;i++) {
309                 (*strs)[i] = ldb_msg_find_attr_as_string(res[i], attr_name, NULL);
310         }
311         (*strs)[count] = NULL;
312
313         return count;
314 }
315
316 struct ldb_dn *samdb_result_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
317                                const char *attr, struct ldb_dn *default_value)
318 {
319         struct ldb_dn *ret_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
320         if (!ret_dn) {
321                 return default_value;
322         }
323         return ret_dn;
324 }
325
326 /*
327   pull a rid from a objectSid in a result set. 
328 */
329 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, 
330                                    const char *attr, uint32_t default_value)
331 {
332         struct dom_sid *sid;
333         uint32_t rid;
334
335         sid = samdb_result_dom_sid(mem_ctx, msg, attr);
336         if (sid == NULL) {
337                 return default_value;
338         }
339         rid = sid->sub_auths[sid->num_auths-1];
340         talloc_free(sid);
341         return rid;
342 }
343
344 /*
345   pull a dom_sid structure from a objectSid in a result set. 
346 */
347 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, 
348                                      const char *attr)
349 {
350         ssize_t ret;
351         const struct ldb_val *v;
352         struct dom_sid *sid;
353         v = ldb_msg_find_ldb_val(msg, attr);
354         if (v == NULL) {
355                 return NULL;
356         }
357         sid = talloc(mem_ctx, struct dom_sid);
358         if (sid == NULL) {
359                 return NULL;
360         }
361         ret = sid_parse(v->data, v->length, sid);
362         if (ret == -1) {
363                 talloc_free(sid);
364                 return NULL;
365         }
366         return sid;
367 }
368
369 /*
370   pull a guid structure from a objectGUID in a result set. 
371 */
372 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
373 {
374         const struct ldb_val *v;
375         struct GUID guid;
376         NTSTATUS status;
377
378         v = ldb_msg_find_ldb_val(msg, attr);
379         if (!v) return GUID_zero();
380
381         status = GUID_from_ndr_blob(v, &guid);
382         if (!NT_STATUS_IS_OK(status)) {
383                 return GUID_zero();
384         }
385
386         return guid;
387 }
388
389 /*
390   pull a sid prefix from a objectSid in a result set. 
391   this is used to find the domain sid for a user
392 */
393 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, 
394                                         const char *attr)
395 {
396         struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
397         if (!sid || sid->num_auths < 1) return NULL;
398         sid->num_auths--;
399         return sid;
400 }
401
402 /*
403   pull a NTTIME in a result set. 
404 */
405 NTTIME samdb_result_nttime(const struct ldb_message *msg, const char *attr,
406                            NTTIME default_value)
407 {
408         return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
409 }
410
411 /*
412  * Windows stores 0 for lastLogoff.
413  * But when a MS DC return the lastLogoff (as Logoff Time)
414  * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
415  * cause windows 2008 and newer version to fail for SMB requests
416  */
417 NTTIME samdb_result_last_logoff(const struct ldb_message *msg)
418 {
419         NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "lastLogoff",0);
420
421         if (ret == 0)
422                 ret = 0x7FFFFFFFFFFFFFFFULL;
423
424         return ret;
425 }
426
427 /*
428  * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
429  * indicate an account doesn't expire.
430  *
431  * When Windows initially creates an account, it sets
432  * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF).  However,
433  * when changing from an account having a specific expiration date to
434  * that account never expiring, it sets accountExpires = 0.
435  *
436  * Consolidate that logic here to allow clearer logic for account expiry in
437  * the rest of the code.
438  */
439 NTTIME samdb_result_account_expires(const struct ldb_message *msg)
440 {
441         NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "accountExpires",
442                                                  0);
443
444         if (ret == 0)
445                 ret = 0x7FFFFFFFFFFFFFFFULL;
446
447         return ret;
448 }
449
450 /*
451   construct the allow_password_change field from the PwdLastSet attribute and the 
452   domain password settings
453 */
454 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb, 
455                                           TALLOC_CTX *mem_ctx, 
456                                           struct ldb_dn *domain_dn, 
457                                           struct ldb_message *msg, 
458                                           const char *attr)
459 {
460         uint64_t attr_time = ldb_msg_find_attr_as_uint64(msg, attr, 0);
461         int64_t minPwdAge;
462
463         if (attr_time == 0) {
464                 return 0;
465         }
466
467         minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
468
469         /* yes, this is a -= not a += as minPwdAge is stored as the negative
470            of the number of 100-nano-seconds */
471         attr_time -= minPwdAge;
472
473         return attr_time;
474 }
475
476 /*
477   pull a samr_Password structutre from a result set. 
478 */
479 struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, const char *attr)
480 {
481         struct samr_Password *hash = NULL;
482         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
483         if (val && (val->length >= sizeof(hash->hash))) {
484                 hash = talloc(mem_ctx, struct samr_Password);
485                 memcpy(hash->hash, val->data, MIN(val->length, sizeof(hash->hash)));
486         }
487         return hash;
488 }
489
490 /*
491   pull an array of samr_Password structures from a result set.
492 */
493 unsigned int samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
494                            const char *attr, struct samr_Password **hashes)
495 {
496         unsigned int count, i;
497         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
498
499         *hashes = NULL;
500         if (!val) {
501                 return 0;
502         }
503         count = val->length / 16;
504         if (count == 0) {
505                 return 0;
506         }
507
508         *hashes = talloc_array(mem_ctx, struct samr_Password, count);
509         if (! *hashes) {
510                 return 0;
511         }
512
513         for (i=0;i<count;i++) {
514                 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
515         }
516
517         return count;
518 }
519
520 NTSTATUS samdb_result_passwords_from_history(TALLOC_CTX *mem_ctx,
521                                              struct loadparm_context *lp_ctx,
522                                              struct ldb_message *msg,
523                                              unsigned int idx,
524                                              struct samr_Password **lm_pwd,
525                                              struct samr_Password **nt_pwd)
526 {
527         struct samr_Password *lmPwdHash, *ntPwdHash;
528
529         if (nt_pwd) {
530                 unsigned int num_nt;
531                 num_nt = samdb_result_hashes(mem_ctx, msg, "ntPwdHistory", &ntPwdHash);
532                 if (num_nt <= idx) {
533                         *nt_pwd = NULL;
534                 } else {
535                         *nt_pwd = &ntPwdHash[idx];
536                 }
537         }
538         if (lm_pwd) {
539                 /* Ensure that if we have turned off LM
540                  * authentication, that we never use the LM hash, even
541                  * if we store it */
542                 if (lpcfg_lanman_auth(lp_ctx)) {
543                         unsigned int num_lm;
544                         num_lm = samdb_result_hashes(mem_ctx, msg, "lmPwdHistory", &lmPwdHash);
545                         if (num_lm <= idx) {
546                                 *lm_pwd = NULL;
547                         } else {
548                                 *lm_pwd = &lmPwdHash[idx];
549                         }
550                 } else {
551                         *lm_pwd = NULL;
552                 }
553         }
554         return NT_STATUS_OK;
555 }
556
557 NTSTATUS samdb_result_passwords_no_lockout(TALLOC_CTX *mem_ctx,
558                                            struct loadparm_context *lp_ctx,
559                                            const struct ldb_message *msg,
560                                            struct samr_Password **lm_pwd,
561                                            struct samr_Password **nt_pwd)
562 {
563         struct samr_Password *lmPwdHash, *ntPwdHash;
564
565         if (nt_pwd) {
566                 unsigned int num_nt;
567                 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
568                 if (num_nt == 0) {
569                         *nt_pwd = NULL;
570                 } else if (num_nt > 1) {
571                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
572                 } else {
573                         *nt_pwd = &ntPwdHash[0];
574                 }
575         }
576         if (lm_pwd) {
577                 /* Ensure that if we have turned off LM
578                  * authentication, that we never use the LM hash, even
579                  * if we store it */
580                 if (lpcfg_lanman_auth(lp_ctx)) {
581                         unsigned int num_lm;
582                         num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
583                         if (num_lm == 0) {
584                                 *lm_pwd = NULL;
585                         } else if (num_lm > 1) {
586                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
587                         } else {
588                                 *lm_pwd = &lmPwdHash[0];
589                         }
590                 } else {
591                         *lm_pwd = NULL;
592                 }
593         }
594         return NT_STATUS_OK;
595 }
596
597 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx,
598                                 struct loadparm_context *lp_ctx,
599                                 const struct ldb_message *msg,
600                                 struct samr_Password **lm_pwd,
601                                 struct samr_Password **nt_pwd)
602 {
603         uint16_t acct_flags;
604
605         acct_flags = samdb_result_acct_flags(msg,
606                                              "msDS-User-Account-Control-Computed");
607         /* Quit if the account was locked out. */
608         if (acct_flags & ACB_AUTOLOCK) {
609                 DEBUG(3,("samdb_result_passwords: Account for user %s was locked out.\n",
610                          ldb_dn_get_linearized(msg->dn)));
611                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
612         }
613
614         return samdb_result_passwords_no_lockout(mem_ctx, lp_ctx, msg,
615                                                  lm_pwd, nt_pwd);
616 }
617
618 /*
619   pull a samr_LogonHours structutre from a result set. 
620 */
621 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
622 {
623         struct samr_LogonHours hours;
624         size_t units_per_week = 168;
625         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
626
627         ZERO_STRUCT(hours);
628
629         if (val) {
630                 units_per_week = val->length * 8;
631         }
632
633         hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
634         if (!hours.bits) {
635                 return hours;
636         }
637         hours.units_per_week = units_per_week;
638         memset(hours.bits, 0xFF, units_per_week/8);
639         if (val) {
640                 memcpy(hours.bits, val->data, val->length);
641         }
642
643         return hours;
644 }
645
646 /*
647   pull a set of account_flags from a result set. 
648
649   Naturally, this requires that userAccountControl and
650   (if not null) the attributes 'attr' be already
651   included in msg
652 */
653 uint32_t samdb_result_acct_flags(const struct ldb_message *msg, const char *attr)
654 {
655         uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
656         uint32_t attr_flags = 0;
657         uint32_t acct_flags = ds_uf2acb(userAccountControl);
658         if (attr) {
659                 attr_flags = ldb_msg_find_attr_as_uint(msg, attr, UF_ACCOUNTDISABLE);
660                 if (attr_flags == UF_ACCOUNTDISABLE) {
661                         DEBUG(0, ("Attribute %s not found, disabling account %s!\n", attr,
662                                   ldb_dn_get_linearized(msg->dn)));
663                 }
664                 acct_flags |= ds_uf2acb(attr_flags);
665         }
666
667         return acct_flags;
668 }
669
670 NTSTATUS samdb_result_parameters(TALLOC_CTX *mem_ctx,
671                                  struct ldb_message *msg,
672                                  const char *attr,
673                                  struct lsa_BinaryString *s)
674 {
675         int i;
676         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
677
678         ZERO_STRUCTP(s);
679
680         if (!val) {
681                 return NT_STATUS_OK;
682         }
683
684         if ((val->length % 2) != 0) {
685                 /*
686                  * If the on-disk data is not even in length, we know
687                  * it is corrupt, and can not be safely pushed.  We
688                  * would either truncate, send either a un-initilaised
689                  * byte or send a forced zero byte
690                  */
691                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
692         }
693
694         s->array = talloc_array(mem_ctx, uint16_t, val->length/2);
695         if (!s->array) {
696                 return NT_STATUS_NO_MEMORY;
697         }
698         s->length = s->size = val->length;
699
700         /* The on-disk format is the 'network' format, being UTF16LE (sort of) */
701         for (i = 0; i < s->length / 2; i++) {
702                 s->array[i] = SVAL(val->data, i * 2);
703         }
704
705         return NT_STATUS_OK;
706 }
707
708 /* Find an attribute, with a particular value */
709
710 /* The current callers of this function expect a very specific
711  * behaviour: In particular, objectClass subclass equivalence is not
712  * wanted.  This means that we should not lookup the schema for the
713  * comparison function */
714 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb, 
715                                                  const struct ldb_message *msg, 
716                                                  const char *name, const char *value)
717 {
718         unsigned int i;
719         struct ldb_message_element *el = ldb_msg_find_element(msg, name);
720
721         if (!el) {
722                 return NULL;
723         }
724
725         for (i=0;i<el->num_values;i++) {
726                 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
727                         return el;
728                 }
729         }
730
731         return NULL;
732 }
733
734 static int samdb_find_or_add_attribute_ex(struct ldb_context *ldb,
735                                           struct ldb_message *msg,
736                                           const char *name,
737                                           const char *set_value,
738                                           unsigned attr_flags,
739                                           bool *added)
740 {
741         int ret;
742         struct ldb_message_element *el;
743
744         SMB_ASSERT(attr_flags != 0);
745
746         el = ldb_msg_find_element(msg, name);
747         if (el) {
748                 if (added != NULL) {
749                         *added = false;
750                 }
751
752                 return LDB_SUCCESS;
753         }
754
755         ret = ldb_msg_add_empty(msg, name,
756                                 attr_flags,
757                                 &el);
758         if (ret != LDB_SUCCESS) {
759                 return ret;
760         }
761
762         if (set_value != NULL) {
763                 ret = ldb_msg_add_string(msg, name, set_value);
764                 if (ret != LDB_SUCCESS) {
765                         return ret;
766                 }
767         }
768
769         if (added != NULL) {
770                 *added = true;
771         }
772         return LDB_SUCCESS;
773 }
774
775 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
776 {
777         return samdb_find_or_add_attribute_ex(ldb, msg, name, set_value, LDB_FLAG_MOD_ADD, NULL);
778 }
779
780 /*
781   add a dom_sid element to a message
782 */
783 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
784                           const char *attr_name, const struct dom_sid *sid)
785 {
786         struct ldb_val v;
787         enum ndr_err_code ndr_err;
788
789         ndr_err = ndr_push_struct_blob(&v, mem_ctx, 
790                                        sid,
791                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
792         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
793                 return ldb_operr(sam_ldb);
794         }
795         return ldb_msg_add_value(msg, attr_name, &v, NULL);
796 }
797
798
799 /*
800   add a delete element operation to a message
801 */
802 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
803                          const char *attr_name)
804 {
805         /* we use an empty replace rather than a delete, as it allows for 
806            dsdb_replace() to be used everywhere */
807         return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
808 }
809
810 /*
811   add an add attribute value to a message or enhance an existing attribute
812   which has the same name and the add flag set.
813 */
814 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
815                          struct ldb_message *msg, const char *attr_name,
816                          const char *value)
817 {
818         struct ldb_message_element *el;
819         struct ldb_val val, *vals;
820         char *v;
821         unsigned int i;
822         bool found = false;
823         int ret;
824
825         v = talloc_strdup(mem_ctx, value);
826         if (v == NULL) {
827                 return ldb_oom(sam_ldb);
828         }
829
830         val.data = (uint8_t *) v;
831         val.length = strlen(v);
832
833         if (val.length == 0) {
834                 /* allow empty strings as non-existent attributes */
835                 return LDB_SUCCESS;
836         }
837
838         for (i = 0; i < msg->num_elements; i++) {
839                 el = &msg->elements[i];
840                 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
841                     (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
842                         found = true;
843                         break;
844                 }
845         }
846         if (!found) {
847                 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
848                                         &el);
849                 if (ret != LDB_SUCCESS) {
850                         return ret;
851                 }
852         }
853
854         vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
855                               el->num_values + 1);
856         if (vals == NULL) {
857                 return ldb_oom(sam_ldb);
858         }
859         el->values = vals;
860         el->values[el->num_values] = val;
861         ++(el->num_values);
862
863         return LDB_SUCCESS;
864 }
865
866 /*
867   add a delete attribute value to a message or enhance an existing attribute
868   which has the same name and the delete flag set.
869 */
870 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
871                          struct ldb_message *msg, const char *attr_name,
872                          const char *value)
873 {
874         struct ldb_message_element *el;
875         struct ldb_val val, *vals;
876         char *v;
877         unsigned int i;
878         bool found = false;
879         int ret;
880
881         v = talloc_strdup(mem_ctx, value);
882         if (v == NULL) {
883                 return ldb_oom(sam_ldb);
884         }
885
886         val.data = (uint8_t *) v;
887         val.length = strlen(v);
888
889         if (val.length == 0) {
890                 /* allow empty strings as non-existent attributes */
891                 return LDB_SUCCESS;
892         }
893
894         for (i = 0; i < msg->num_elements; i++) {
895                 el = &msg->elements[i];
896                 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
897                     (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
898                         found = true;
899                         break;
900                 }
901         }
902         if (!found) {
903                 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
904                                         &el);
905                 if (ret != LDB_SUCCESS) {
906                         return ret;
907                 }
908         }
909
910         vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
911                               el->num_values + 1);
912         if (vals == NULL) {
913                 return ldb_oom(sam_ldb);
914         }
915         el->values = vals;
916         el->values[el->num_values] = val;
917         ++(el->num_values);
918
919         return LDB_SUCCESS;
920 }
921
922 /*
923   add a int element to a message
924 */
925 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
926                        const char *attr_name, int v)
927 {
928         const char *s = talloc_asprintf(mem_ctx, "%d", v);
929         if (s == NULL) {
930                 return ldb_oom(sam_ldb);
931         }
932         return ldb_msg_add_string(msg, attr_name, s);
933 }
934
935 /*
936  * Add an unsigned int element to a message
937  *
938  * The issue here is that we have not yet first cast to int32_t explicitly,
939  * before we cast to an signed int to printf() into the %d or cast to a
940  * int64_t before we then cast to a long long to printf into a %lld.
941  *
942  * There are *no* unsigned integers in Active Directory LDAP, even the RID
943  * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
944  * (See the schema, and the syntax definitions in schema_syntax.c).
945  *
946  */
947 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
948                        const char *attr_name, unsigned int v)
949 {
950         return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
951 }
952
953 /*
954   add a (signed) int64_t element to a message
955 */
956 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
957                         const char *attr_name, int64_t v)
958 {
959         const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
960         if (s == NULL) {
961                 return ldb_oom(sam_ldb);
962         }
963         return ldb_msg_add_string(msg, attr_name, s);
964 }
965
966 /*
967  * Add an unsigned int64_t (uint64_t) element to a message
968  *
969  * The issue here is that we have not yet first cast to int32_t explicitly,
970  * before we cast to an signed int to printf() into the %d or cast to a
971  * int64_t before we then cast to a long long to printf into a %lld.
972  *
973  * There are *no* unsigned integers in Active Directory LDAP, even the RID
974  * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
975  * (See the schema, and the syntax definitions in schema_syntax.c).
976  *
977  */
978 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
979                         const char *attr_name, uint64_t v)
980 {
981         return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
982 }
983
984 /*
985   add a samr_Password element to a message
986 */
987 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
988                        const char *attr_name, const struct samr_Password *hash)
989 {
990         struct ldb_val val;
991         val.data = talloc_memdup(mem_ctx, hash->hash, 16);
992         if (!val.data) {
993                 return ldb_oom(sam_ldb);
994         }
995         val.length = 16;
996         return ldb_msg_add_value(msg, attr_name, &val, NULL);
997 }
998
999 /*
1000   add a samr_Password array to a message
1001 */
1002 int samdb_msg_add_hashes(struct ldb_context *ldb,
1003                          TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1004                          const char *attr_name, struct samr_Password *hashes,
1005                          unsigned int count)
1006 {
1007         struct ldb_val val;
1008         unsigned int i;
1009         val.data = talloc_array_size(mem_ctx, 16, count);
1010         val.length = count*16;
1011         if (!val.data) {
1012                 return ldb_oom(ldb);
1013         }
1014         for (i=0;i<count;i++) {
1015                 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
1016         }
1017         return ldb_msg_add_value(msg, attr_name, &val, NULL);
1018 }
1019
1020 /*
1021   add a acct_flags element to a message
1022 */
1023 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1024                              const char *attr_name, uint32_t v)
1025 {
1026         return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
1027 }
1028
1029 /*
1030   add a logon_hours element to a message
1031 */
1032 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1033                               const char *attr_name, struct samr_LogonHours *hours)
1034 {
1035         struct ldb_val val;
1036         val.length = hours->units_per_week / 8;
1037         val.data = hours->bits;
1038         return ldb_msg_add_value(msg, attr_name, &val, NULL);
1039 }
1040
1041 /*
1042   add a parameters element to a message
1043 */
1044 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1045                              const char *attr_name, struct lsa_BinaryString *parameters)
1046 {
1047         int i;
1048         struct ldb_val val;
1049         if ((parameters->length % 2) != 0) {
1050                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
1051         }
1052
1053         val.data = talloc_array(mem_ctx, uint8_t, parameters->length);
1054         if (val.data == NULL) {
1055                 return LDB_ERR_OPERATIONS_ERROR;
1056         }
1057         val.length = parameters->length;
1058         for (i = 0; i < parameters->length / 2; i++) {
1059                 /*
1060                  * The on-disk format needs to be in the 'network'
1061                  * format, parmeters->array is a uint16_t array of
1062                  * length parameters->length / 2
1063                  */
1064                 SSVAL(val.data, i * 2, parameters->array[i]);
1065         }
1066         return ldb_msg_add_steal_value(msg, attr_name, &val);
1067 }
1068
1069 /*
1070  * Sets an unsigned int element in a message
1071  *
1072  * The issue here is that we have not yet first cast to int32_t explicitly,
1073  * before we cast to an signed int to printf() into the %d or cast to a
1074  * int64_t before we then cast to a long long to printf into a %lld.
1075  *
1076  * There are *no* unsigned integers in Active Directory LDAP, even the RID
1077  * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1078  * (See the schema, and the syntax definitions in schema_syntax.c).
1079  *
1080  */
1081 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1082                        struct ldb_message *msg, const char *attr_name,
1083                        unsigned int v)
1084 {
1085         struct ldb_message_element *el;
1086
1087         el = ldb_msg_find_element(msg, attr_name);
1088         if (el) {
1089                 el->num_values = 0;
1090         }
1091         return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1092 }
1093
1094 /*
1095  * Handle ldb_request in transaction
1096  */
1097 int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1098                                  struct ldb_request *req)
1099 {
1100         int ret;
1101
1102         ret = ldb_transaction_start(sam_ldb);
1103         if (ret != LDB_SUCCESS) {
1104                 return ret;
1105         }
1106
1107         ret = ldb_request(sam_ldb, req);
1108         if (ret == LDB_SUCCESS) {
1109                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1110         }
1111
1112         if (ret == LDB_SUCCESS) {
1113                 return ldb_transaction_commit(sam_ldb);
1114         }
1115         ldb_transaction_cancel(sam_ldb);
1116
1117         return ret;
1118 }
1119
1120 /*
1121   return a default security descriptor
1122 */
1123 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1124 {
1125         struct security_descriptor *sd;
1126
1127         sd = security_descriptor_initialise(mem_ctx);
1128
1129         return sd;
1130 }
1131
1132 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx) 
1133 {
1134         struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1135         struct ldb_dn *aggregate_dn;
1136         if (!schema_dn) {
1137                 return NULL;
1138         }
1139
1140         aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1141         if (!aggregate_dn) {
1142                 return NULL;
1143         }
1144         if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1145                 return NULL;
1146         }
1147         return aggregate_dn;
1148 }
1149
1150 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1151 {
1152         struct ldb_dn *new_dn;
1153
1154         new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1155         if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1156                 talloc_free(new_dn);
1157                 return NULL;
1158         }
1159         return new_dn;
1160 }
1161
1162 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1163 {
1164        struct ldb_dn *new_dn;
1165
1166        new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1167        if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1168                talloc_free(new_dn);
1169                return NULL;
1170        }
1171        return new_dn;
1172 }
1173
1174 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1175 {
1176         struct ldb_dn *new_dn;
1177
1178         new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1179         if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1180                 talloc_free(new_dn);
1181                 return NULL;
1182         }
1183         return new_dn;
1184 }
1185
1186 /*
1187   work out the domain sid for the current open ldb
1188 */
1189 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1190 {
1191         TALLOC_CTX *tmp_ctx;
1192         const struct dom_sid *domain_sid;
1193         const char *attrs[] = {
1194                 "objectSid",
1195                 NULL
1196         };
1197         struct ldb_result *res;
1198         int ret;
1199
1200         /* see if we have a cached copy */
1201         domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1202         if (domain_sid) {
1203                 return domain_sid;
1204         }
1205
1206         tmp_ctx = talloc_new(ldb);
1207         if (tmp_ctx == NULL) {
1208                 goto failed;
1209         }
1210
1211         ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1212
1213         if (ret != LDB_SUCCESS) {
1214                 goto failed;
1215         }
1216
1217         if (res->count != 1) {
1218                 goto failed;
1219         }
1220
1221         domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1222         if (domain_sid == NULL) {
1223                 goto failed;
1224         }
1225
1226         /* cache the domain_sid in the ldb */
1227         if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1228                 goto failed;
1229         }
1230
1231         talloc_steal(ldb, domain_sid);
1232         talloc_free(tmp_ctx);
1233
1234         return domain_sid;
1235
1236 failed:
1237         talloc_free(tmp_ctx);
1238         return NULL;
1239 }
1240
1241 /*
1242   get domain sid from cache
1243 */
1244 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1245 {
1246         return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1247 }
1248
1249 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1250 {
1251         TALLOC_CTX *tmp_ctx;
1252         struct dom_sid *dom_sid_new;
1253         struct dom_sid *dom_sid_old;
1254
1255         /* see if we have a cached copy */
1256         dom_sid_old = talloc_get_type(ldb_get_opaque(ldb, 
1257                                                      "cache.domain_sid"), struct dom_sid);
1258
1259         tmp_ctx = talloc_new(ldb);
1260         if (tmp_ctx == NULL) {
1261                 goto failed;
1262         }
1263
1264         dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1265         if (!dom_sid_new) {
1266                 goto failed;
1267         }
1268
1269         /* cache the domain_sid in the ldb */
1270         if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1271                 goto failed;
1272         }
1273
1274         talloc_steal(ldb, dom_sid_new);
1275         talloc_free(tmp_ctx);
1276         talloc_free(dom_sid_old);
1277
1278         return true;
1279
1280 failed:
1281         DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1282         talloc_free(tmp_ctx);
1283         return false;
1284 }
1285
1286 /*
1287   work out the domain guid for the current open ldb
1288 */
1289 const struct GUID *samdb_domain_guid(struct ldb_context *ldb)
1290 {
1291         TALLOC_CTX *tmp_ctx = NULL;
1292         struct GUID *domain_guid = NULL;
1293         const char *attrs[] = {
1294                 "objectGUID",
1295                 NULL
1296         };
1297         struct ldb_result *res = NULL;
1298         int ret;
1299
1300         /* see if we have a cached copy */
1301         domain_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.domain_guid");
1302         if (domain_guid) {
1303                 return domain_guid;
1304         }
1305
1306         tmp_ctx = talloc_new(ldb);
1307         if (tmp_ctx == NULL) {
1308                 goto failed;
1309         }
1310
1311         ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectGUID=*");
1312         if (ret != LDB_SUCCESS) {
1313                 goto failed;
1314         }
1315
1316         if (res->count != 1) {
1317                 goto failed;
1318         }
1319
1320         domain_guid = talloc(tmp_ctx, struct GUID);
1321         if (domain_guid == NULL) {
1322                 goto failed;
1323         }
1324         *domain_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1325
1326         /* cache the domain_sid in the ldb */
1327         if (ldb_set_opaque(ldb, "cache.domain_guid", domain_guid) != LDB_SUCCESS) {
1328                 goto failed;
1329         }
1330
1331         talloc_steal(ldb, domain_guid);
1332         talloc_free(tmp_ctx);
1333
1334         return domain_guid;
1335
1336 failed:
1337         talloc_free(tmp_ctx);
1338         return NULL;
1339 }
1340
1341 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1342 {
1343         TALLOC_CTX *tmp_ctx;
1344         struct ldb_dn *ntds_settings_dn_new;
1345         struct ldb_dn *ntds_settings_dn_old;
1346
1347         /* see if we have a forced copy from provision */
1348         ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb, 
1349                                                               "forced.ntds_settings_dn"), struct ldb_dn);
1350
1351         tmp_ctx = talloc_new(ldb);
1352         if (tmp_ctx == NULL) {
1353                 goto failed;
1354         }
1355
1356         ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1357         if (!ntds_settings_dn_new) {
1358                 goto failed;
1359         }
1360
1361         /* set the DN in the ldb to avoid lookups during provision */
1362         if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1363                 goto failed;
1364         }
1365
1366         talloc_steal(ldb, ntds_settings_dn_new);
1367         talloc_free(tmp_ctx);
1368         talloc_free(ntds_settings_dn_old);
1369
1370         return true;
1371
1372 failed:
1373         DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1374         talloc_free(tmp_ctx);
1375         return false;
1376 }
1377
1378 /*
1379   work out the ntds settings dn for the current open ldb
1380 */
1381 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1382 {
1383         TALLOC_CTX *tmp_ctx;
1384         const char *root_attrs[] = { "dsServiceName", NULL };
1385         int ret;
1386         struct ldb_result *root_res;
1387         struct ldb_dn *settings_dn;
1388
1389         /* see if we have a cached copy */
1390         settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1391         if (settings_dn) {
1392                 return ldb_dn_copy(mem_ctx, settings_dn);
1393         }
1394
1395         tmp_ctx = talloc_new(mem_ctx);
1396         if (tmp_ctx == NULL) {
1397                 goto failed;
1398         }
1399
1400         ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1401         if (ret != LDB_SUCCESS) {
1402                 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n", 
1403                          ldb_errstring(ldb)));
1404                 goto failed;
1405         }
1406
1407         if (root_res->count != 1) {
1408                 goto failed;
1409         }
1410
1411         settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1412
1413         /* note that we do not cache the DN here, as that would mean
1414          * we could not handle server renames at runtime. Only
1415          * provision sets up forced.ntds_settings_dn */
1416
1417         talloc_steal(mem_ctx, settings_dn);
1418         talloc_free(tmp_ctx);
1419
1420         return settings_dn;
1421
1422 failed:
1423         DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1424         talloc_free(tmp_ctx);
1425         return NULL;
1426 }
1427
1428 /*
1429   work out the ntds settings invocationID/objectGUID for the current open ldb
1430 */
1431 static const struct GUID *samdb_ntds_GUID(struct ldb_context *ldb,
1432                                           const char *attribute,
1433                                           const char *cache_name)
1434 {
1435         TALLOC_CTX *tmp_ctx;
1436         const char *attrs[] = { attribute, NULL };
1437         int ret;
1438         struct ldb_result *res;
1439         struct GUID *ntds_guid;
1440         struct ldb_dn *ntds_settings_dn = NULL;
1441         const char *errstr = NULL;
1442
1443         /* see if we have a cached copy */
1444         ntds_guid = (struct GUID *)ldb_get_opaque(ldb, cache_name);
1445         if (ntds_guid != NULL) {
1446                 return ntds_guid;
1447         }
1448
1449         tmp_ctx = talloc_new(ldb);
1450         if (tmp_ctx == NULL) {
1451                 goto failed;
1452         }
1453
1454         ntds_settings_dn = samdb_ntds_settings_dn(ldb, tmp_ctx);
1455         if (ntds_settings_dn == NULL) {
1456                 errstr = "samdb_ntds_settings_dn() returned NULL";
1457                 goto failed;
1458         }
1459
1460         ret = ldb_search(ldb, tmp_ctx, &res, ntds_settings_dn,
1461                          LDB_SCOPE_BASE, attrs, NULL);
1462         if (ret) {
1463                 errstr = ldb_errstring(ldb);
1464                 goto failed;
1465         }
1466
1467         if (res->count != 1) {
1468                 errstr = "incorrect number of results from base search";
1469                 goto failed;
1470         }
1471
1472         ntds_guid = talloc(tmp_ctx, struct GUID);
1473         if (ntds_guid == NULL) {
1474                 goto failed;
1475         }
1476
1477         *ntds_guid = samdb_result_guid(res->msgs[0], attribute);
1478
1479         if (GUID_all_zero(ntds_guid)) {
1480                 if (ldb_msg_find_ldb_val(res->msgs[0], attribute)) {
1481                         errstr = "failed to find the GUID attribute";
1482                 } else {
1483                         errstr = "failed to parse the GUID";
1484                 }
1485                 goto failed;
1486         }
1487
1488         /* cache the domain_sid in the ldb */
1489         if (ldb_set_opaque(ldb, cache_name, ntds_guid) != LDB_SUCCESS) {
1490                 errstr = "ldb_set_opaque() failed";
1491                 goto failed;
1492         }
1493
1494         talloc_steal(ldb, ntds_guid);
1495         talloc_free(tmp_ctx);
1496
1497         return ntds_guid;
1498
1499 failed:
1500         DBG_WARNING("Failed to find our own NTDS Settings %s in the ldb: %s!\n",
1501                     attribute, errstr);
1502         talloc_free(tmp_ctx);
1503         return NULL;
1504 }
1505
1506 /*
1507   work out the ntds settings objectGUID for the current open ldb
1508 */
1509 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1510 {
1511         return samdb_ntds_GUID(ldb, "objectGUID", "cache.ntds_guid");
1512 }
1513
1514 /*
1515   work out the ntds settings invocationId for the current open ldb
1516 */
1517 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1518 {
1519         return samdb_ntds_GUID(ldb, "invocationId", "cache.invocation_id");
1520 }
1521
1522 static bool samdb_set_ntds_GUID(struct ldb_context *ldb,
1523                                 const struct GUID *ntds_guid_in,
1524                                 const char *attribute,
1525                                 const char *cache_name)
1526 {
1527         TALLOC_CTX *tmp_ctx;
1528         struct GUID *ntds_guid_new;
1529         struct GUID *ntds_guid_old;
1530
1531         /* see if we have a cached copy */
1532         ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, cache_name);
1533
1534         tmp_ctx = talloc_new(ldb);
1535         if (tmp_ctx == NULL) {
1536                 goto failed;
1537         }
1538
1539         ntds_guid_new = talloc(tmp_ctx, struct GUID);
1540         if (!ntds_guid_new) {
1541                 goto failed;
1542         }
1543
1544         *ntds_guid_new = *ntds_guid_in;
1545
1546         /* cache the domain_sid in the ldb */
1547         if (ldb_set_opaque(ldb, cache_name, ntds_guid_new) != LDB_SUCCESS) {
1548                 goto failed;
1549         }
1550
1551         talloc_steal(ldb, ntds_guid_new);
1552         talloc_free(tmp_ctx);
1553         talloc_free(ntds_guid_old);
1554
1555         return true;
1556
1557 failed:
1558         DBG_WARNING("Failed to set our own cached %s in the ldb!\n",
1559                     attribute);
1560         talloc_free(tmp_ctx);
1561         return false;
1562 }
1563
1564 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1565 {
1566         return samdb_set_ntds_GUID(ldb,
1567                                    ntds_guid_in,
1568                                    "objectGUID",
1569                                    "cache.ntds_guid");
1570 }
1571
1572 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1573 {
1574         return samdb_set_ntds_GUID(ldb,
1575                                    invocation_id_in,
1576                                    "invocationId",
1577                                    "cache.invocation_id");
1578 }
1579
1580 /*
1581   work out the server dn for the current open ldb
1582 */
1583 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1584 {
1585         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1586         struct ldb_dn *dn;
1587         if (!tmp_ctx) {
1588                 return NULL;
1589         }
1590         dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1591         talloc_free(tmp_ctx);
1592         return dn;
1593         
1594 }
1595
1596 /*
1597   work out the server dn for the current open ldb
1598 */
1599 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1600 {
1601         struct ldb_dn *server_dn;
1602         struct ldb_dn *servers_dn;
1603         struct ldb_dn *server_site_dn;
1604
1605         /* TODO: there must be a saner way to do this!! */
1606         server_dn = samdb_server_dn(ldb, mem_ctx);
1607         if (!server_dn) return NULL;
1608
1609         servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1610         talloc_free(server_dn);
1611         if (!servers_dn) return NULL;
1612
1613         server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1614         talloc_free(servers_dn);
1615
1616         return server_site_dn;
1617 }
1618
1619 /*
1620   find the site name from a computers DN record
1621  */
1622 int samdb_find_site_for_computer(struct ldb_context *ldb,
1623                                  TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1624                                  const char **site_name)
1625 {
1626         int ret;
1627         struct ldb_dn *dn;
1628         const struct ldb_val *rdn_val;
1629
1630         *site_name = NULL;
1631
1632         ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1633         if (ret != LDB_SUCCESS) {
1634                 return ret;
1635         }
1636
1637         if (!ldb_dn_remove_child_components(dn, 2)) {
1638                 talloc_free(dn);
1639                 return LDB_ERR_INVALID_DN_SYNTAX;
1640         }
1641
1642         rdn_val = ldb_dn_get_rdn_val(dn);
1643         if (rdn_val == NULL) {
1644                 return LDB_ERR_OPERATIONS_ERROR;
1645         }
1646
1647         (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1648         talloc_free(dn);
1649         if (!*site_name) {
1650                 return LDB_ERR_OPERATIONS_ERROR;
1651         }
1652         return LDB_SUCCESS;
1653 }
1654
1655 /*
1656   find the NTDS GUID from a computers DN record
1657  */
1658 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1659                                      struct GUID *ntds_guid)
1660 {
1661         int ret;
1662         struct ldb_dn *dn;
1663
1664         *ntds_guid = GUID_zero();
1665
1666         ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1667         if (ret != LDB_SUCCESS) {
1668                 return ret;
1669         }
1670
1671         if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1672                 talloc_free(dn);
1673                 return LDB_ERR_OPERATIONS_ERROR;
1674         }
1675
1676         ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1677         talloc_free(dn);
1678         return ret;
1679 }
1680
1681 /*
1682   find a 'reference' DN that points at another object
1683   (eg. serverReference, rIDManagerReference etc)
1684  */
1685 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1686                        const char *attribute, struct ldb_dn **dn)
1687 {
1688         const char *attrs[2];
1689         struct ldb_result *res;
1690         int ret;
1691
1692         attrs[0] = attribute;
1693         attrs[1] = NULL;
1694
1695         ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1696         if (ret != LDB_SUCCESS) {
1697                 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1698                                        ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1699                 return ret;
1700         }
1701
1702         *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1703         if (!*dn) {
1704                 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1705                         ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1706                                                ldb_dn_get_linearized(base));
1707                 } else {
1708                         ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1709                                                ldb_dn_get_linearized(base));
1710                 }
1711                 talloc_free(res);
1712                 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1713         }
1714
1715         talloc_free(res);
1716         return LDB_SUCCESS;
1717 }
1718
1719 /*
1720   find if a DN (must have GUID component!) is our ntdsDsa
1721  */
1722 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1723 {
1724         NTSTATUS status;
1725         struct GUID dn_guid;
1726         const struct GUID *our_ntds_guid;
1727         status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1728         if (!NT_STATUS_IS_OK(status)) {
1729                 return LDB_ERR_OPERATIONS_ERROR;
1730         }
1731
1732         our_ntds_guid = samdb_ntds_objectGUID(ldb);
1733         if (!our_ntds_guid) {
1734                 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1735                 return LDB_ERR_OPERATIONS_ERROR;
1736         }
1737
1738         *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1739         return LDB_SUCCESS;
1740 }
1741
1742 /*
1743   find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1744  */
1745 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1746                                     const char *attribute, bool *is_ntdsa)
1747 {
1748         int ret;
1749         struct ldb_dn *referenced_dn;
1750         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1751         if (tmp_ctx == NULL) {
1752                 return LDB_ERR_OPERATIONS_ERROR;
1753         }
1754         ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1755         if (ret != LDB_SUCCESS) {
1756                 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1757                 return ret;
1758         }
1759
1760         ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1761         
1762         talloc_free(tmp_ctx);
1763         return ret;
1764 }
1765
1766 /*
1767   find our machine account via the serverReference attribute in the
1768   server DN
1769  */
1770 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1771 {
1772         struct ldb_dn *server_dn;
1773         int ret;
1774
1775         server_dn = samdb_server_dn(ldb, mem_ctx);
1776         if (server_dn == NULL) {
1777                 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
1778         }
1779
1780         ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1781         talloc_free(server_dn);
1782
1783         return ret;
1784 }
1785
1786 /*
1787   find the RID Manager$ DN via the rIDManagerReference attribute in the
1788   base DN
1789  */
1790 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1791 {
1792         return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1793                                   "rIDManagerReference", dn);
1794 }
1795
1796 /*
1797   find the RID Set DN via the rIDSetReferences attribute in our
1798   machine account DN
1799  */
1800 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1801 {
1802         struct ldb_dn *server_ref_dn = NULL;
1803         int ret;
1804
1805         ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1806         if (ret != LDB_SUCCESS) {
1807                 return ret;
1808         }
1809         ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1810         talloc_free(server_ref_dn);
1811         return ret;
1812 }
1813
1814 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1815 {
1816         const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1817                                                                             mem_ctx));
1818
1819         if (val == NULL) {
1820                 return NULL;
1821         }
1822
1823         return (const char *) val->data;
1824 }
1825
1826 /*
1827  * Finds the client site by using the client's IP address.
1828  * The "subnet_name" returns the name of the subnet if parameter != NULL
1829  *
1830  * Has a Windows-based fallback to provide the only site available, or an empty
1831  * string if there are multiple sites.
1832  */
1833 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1834                                    const char *ip_address, char **subnet_name,
1835                                    bool fallback)
1836 {
1837         const char *attrs[] = { "cn", "siteObject", NULL };
1838         struct ldb_dn *sites_container_dn = NULL;
1839         struct ldb_dn *subnets_dn = NULL;
1840         struct ldb_dn *sites_dn = NULL;
1841         struct ldb_result *res = NULL;
1842         const struct ldb_val *val = NULL;
1843         const char *site_name = NULL;
1844         const char *l_subnet_name = NULL;
1845         const char *allow_list[2] = { NULL, NULL };
1846         unsigned int i, count;
1847         int ret;
1848
1849         /*
1850          * if we don't have a client ip e.g. ncalrpc
1851          * the server site is the client site
1852          */
1853         if (ip_address == NULL) {
1854                 return samdb_server_site_name(ldb, mem_ctx);
1855         }
1856
1857         sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1858         if (sites_container_dn == NULL) {
1859                 goto exit;
1860         }
1861
1862         subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1863         if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1864                 goto exit;
1865         }
1866
1867         ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1868                          attrs, NULL);
1869         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1870                 count = 0;
1871         } else if (ret != LDB_SUCCESS) {
1872                 goto exit;
1873         } else {
1874                 count = res->count;
1875         }
1876
1877         for (i = 0; i < count; i++) {
1878                 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1879                                                             NULL);
1880
1881                 allow_list[0] = l_subnet_name;
1882
1883                 if (allow_access_nolog(NULL, allow_list, "", ip_address)) {
1884                         sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1885                                                            res->msgs[i],
1886                                                            "siteObject");
1887                         if (sites_dn == NULL) {
1888                                 /* No reference, maybe another subnet matches */
1889                                 continue;
1890                         }
1891
1892                         /* "val" cannot be NULL here since "sites_dn" != NULL */
1893                         val = ldb_dn_get_rdn_val(sites_dn);
1894                         site_name = talloc_strdup(mem_ctx,
1895                                                   (const char *) val->data);
1896
1897                         TALLOC_FREE(sites_dn);
1898
1899                         break;
1900                 }
1901         }
1902
1903         if (site_name == NULL && fallback) {
1904                 /* This is the Windows Server fallback rule: when no subnet
1905                  * exists and we have only one site available then use it (it
1906                  * is for sure the same as our server site). If more sites do
1907                  * exist then we don't know which one to use and set the site
1908                  * name to "". */
1909                 size_t cnt = 0;
1910                 ret = dsdb_domain_count(
1911                         ldb,
1912                         &cnt,
1913                         sites_container_dn,
1914                         NULL,
1915                         LDB_SCOPE_SUBTREE,
1916                         "(objectClass=site)");
1917                 if (ret != LDB_SUCCESS) {
1918                         goto exit;
1919                 }
1920                 if (cnt == 1) {
1921                         site_name = samdb_server_site_name(ldb, mem_ctx);
1922                 } else {
1923                         site_name = talloc_strdup(mem_ctx, "");
1924                 }
1925                 l_subnet_name = NULL;
1926         }
1927
1928         if (subnet_name != NULL) {
1929                 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1930         }
1931
1932 exit:
1933         TALLOC_FREE(sites_container_dn);
1934         TALLOC_FREE(subnets_dn);
1935         TALLOC_FREE(res);
1936
1937         return site_name;
1938 }
1939
1940 /*
1941   work out if we are the PDC for the domain of the current open ldb
1942 */
1943 bool samdb_is_pdc(struct ldb_context *ldb)
1944 {
1945         int ret;
1946         bool is_pdc;
1947
1948         ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner", 
1949                                               &is_pdc);
1950         if (ret != LDB_SUCCESS) {
1951                 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n", 
1952                          ldb_dn_get_linearized(ldb_get_default_basedn(ldb)), 
1953                          ldb_errstring(ldb)));
1954                 return false;
1955         }
1956
1957         return is_pdc;
1958 }
1959
1960 /*
1961   work out if we are a Global Catalog server for the domain of the current open ldb
1962 */
1963 bool samdb_is_gc(struct ldb_context *ldb)
1964 {
1965         uint32_t options = 0;
1966         if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1967                 return false;
1968         }
1969         return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1970 }
1971
1972 /* Find a domain object in the parents of a particular DN.  */
1973 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1974                                    struct ldb_dn **parent_dn, const char **errstring)
1975 {
1976         TALLOC_CTX *local_ctx;
1977         struct ldb_dn *sdn = dn;
1978         struct ldb_result *res = NULL;
1979         int ret = LDB_SUCCESS;
1980         const char *attrs[] = { NULL };
1981
1982         local_ctx = talloc_new(mem_ctx);
1983         if (local_ctx == NULL) return ldb_oom(ldb);
1984
1985         while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1986                 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1987                                  "(|(objectClass=domain)(objectClass=builtinDomain))");
1988                 if (ret == LDB_SUCCESS) {
1989                         if (res->count == 1) {
1990                                 break;
1991                         }
1992                 } else {
1993                         break;
1994                 }
1995         }
1996
1997         if (ret != LDB_SUCCESS) {
1998                 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1999                                              ldb_dn_get_linearized(dn),
2000                                              ldb_dn_get_linearized(sdn),
2001                                              ldb_errstring(ldb));
2002                 talloc_free(local_ctx);
2003                 return ret;
2004         }
2005         /* should never be true with 'ret=LDB_SUCCESS', here to satisfy clang */
2006         if (res == NULL) {
2007                 talloc_free(local_ctx);
2008                 return LDB_ERR_OTHER;
2009         }
2010         if (res->count != 1) {
2011                 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
2012                                              ldb_dn_get_linearized(dn));
2013                 DEBUG(0,(__location__ ": %s\n", *errstring));
2014                 talloc_free(local_ctx);
2015                 return LDB_ERR_CONSTRAINT_VIOLATION;
2016         }
2017
2018         *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2019         talloc_free(local_ctx);
2020         return ret;
2021 }
2022
2023 static void pwd_timeout_debug(struct tevent_context *unused1,
2024                               struct tevent_timer *unused2,
2025                               struct timeval unused3,
2026                               void *unused4)
2027 {
2028         DEBUG(0, ("WARNING: check_password_complexity: password script "
2029                   "took more than 1 second to run\n"));
2030 }
2031
2032
2033 /*
2034  * Performs checks on a user password (plaintext UNIX format - attribute
2035  * "password"). The remaining parameters have to be extracted from the domain
2036  * object in the AD.
2037  *
2038  * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
2039  */
2040 enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
2041                                                 struct loadparm_context *lp_ctx,
2042                                                 const char *account_name,
2043                                                 const char *user_principal_name,
2044                                                 const char *full_name,
2045                                                 const DATA_BLOB *utf8_blob,
2046                                                 const uint32_t pwdProperties,
2047                                                 const uint32_t minPwdLength)
2048 {
2049         const struct loadparm_substitution *lp_sub =
2050                 lpcfg_noop_substitution();
2051         char *password_script = NULL;
2052         const char *utf8_pw = (const char *)utf8_blob->data;
2053
2054         /*
2055          * This looks strange because it is.
2056          *
2057          * The check for the number of characters in the password
2058          * should clearly not be against the byte length, or else a
2059          * single UTF8 character would count for more than one.
2060          *
2061          * We have chosen to use the number of 16-bit units that the
2062          * password encodes to as the measure of length.  This is not
2063          * the same as the number of codepoints, if a password
2064          * contains a character beyond the Basic Multilingual Plane
2065          * (above 65535) it will count for more than one "character".
2066          */
2067
2068         size_t password_characters_roughly = strlen_m(utf8_pw);
2069
2070         /* checks if the "minPwdLength" property is satisfied */
2071         if (minPwdLength > password_characters_roughly) {
2072                 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
2073         }
2074
2075         /* We might not be asked to check the password complexity */
2076         if (!(pwdProperties & DOMAIN_PASSWORD_COMPLEX)) {
2077                 return SAMR_VALIDATION_STATUS_SUCCESS;
2078         }
2079
2080         if (password_characters_roughly == 0) {
2081                 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2082         }
2083
2084         password_script = lpcfg_check_password_script(lp_ctx, lp_sub, mem_ctx);
2085         if (password_script != NULL && *password_script != '\0') {
2086                 int check_ret = 0;
2087                 int error = 0;
2088                 ssize_t nwritten = 0;
2089                 struct tevent_context *event_ctx = NULL;
2090                 struct tevent_req *req = NULL;
2091                 int cps_stdin = -1;
2092                 const char * const cmd[4] = {
2093                         "/bin/sh", "-c",
2094                         password_script,
2095                         NULL
2096                 };
2097
2098                 event_ctx = tevent_context_init(mem_ctx);
2099                 if (event_ctx == NULL) {
2100                         TALLOC_FREE(password_script);
2101                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2102                 }
2103
2104                 /* Gives a warning after 1 second, terminates after 10 */
2105                 tevent_add_timer(event_ctx, event_ctx,
2106                                  tevent_timeval_current_ofs(1, 0),
2107                                  pwd_timeout_debug, NULL);
2108
2109                 check_ret = setenv("SAMBA_CPS_ACCOUNT_NAME", account_name, 1);
2110                 if (check_ret != 0) {
2111                         TALLOC_FREE(password_script);
2112                         TALLOC_FREE(event_ctx);
2113                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2114                 }
2115                 if (user_principal_name != NULL) {
2116                         check_ret = setenv("SAMBA_CPS_USER_PRINCIPAL_NAME",
2117                                            user_principal_name, 1);
2118                 } else {
2119                         unsetenv("SAMBA_CPS_USER_PRINCIPAL_NAME");
2120                 }
2121                 if (check_ret != 0) {
2122                         TALLOC_FREE(password_script);
2123                         TALLOC_FREE(event_ctx);
2124                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2125                 }
2126                 if (full_name != NULL) {
2127                         check_ret = setenv("SAMBA_CPS_FULL_NAME", full_name, 1);
2128                 } else {
2129                         unsetenv("SAMBA_CPS_FULL_NAME");
2130                 }
2131                 if (check_ret != 0) {
2132                         TALLOC_FREE(password_script);
2133                         TALLOC_FREE(event_ctx);
2134                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2135                 }
2136
2137                 req = samba_runcmd_send(event_ctx, event_ctx,
2138                                         tevent_timeval_current_ofs(10, 0),
2139                                         100, 100, cmd, NULL);
2140                 unsetenv("SAMBA_CPS_ACCOUNT_NAME");
2141                 unsetenv("SAMBA_CPS_USER_PRINCIPAL_NAME");
2142                 unsetenv("SAMBA_CPS_FULL_NAME");
2143                 if (req == NULL) {
2144                         TALLOC_FREE(password_script);
2145                         TALLOC_FREE(event_ctx);
2146                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2147                 }
2148
2149                 cps_stdin = samba_runcmd_export_stdin(req);
2150
2151                 nwritten = write_data(
2152                         cps_stdin, utf8_blob->data, utf8_blob->length);
2153                 if (nwritten == -1) {
2154                         close(cps_stdin);
2155                         TALLOC_FREE(password_script);
2156                         TALLOC_FREE(event_ctx);
2157                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2158                 }
2159
2160                 close(cps_stdin);
2161
2162                 if (!tevent_req_poll(req, event_ctx)) {
2163                         TALLOC_FREE(password_script);
2164                         TALLOC_FREE(event_ctx);
2165                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2166                 }
2167
2168                 check_ret = samba_runcmd_recv(req, &error);
2169                 TALLOC_FREE(event_ctx);
2170
2171                 if (error == ETIMEDOUT) {
2172                         DEBUG(0, ("check_password_complexity: check password script took too long!\n"));
2173                         TALLOC_FREE(password_script);
2174                         return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2175                 }
2176                 DEBUG(5,("check_password_complexity: check password script (%s) "
2177                          "returned [%d]\n", password_script, check_ret));
2178
2179                 if (check_ret != 0) {
2180                         DEBUG(1,("check_password_complexity: "
2181                                  "check password script said new password is not good "
2182                                  "enough!\n"));
2183                         TALLOC_FREE(password_script);
2184                         return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2185                 }
2186
2187                 TALLOC_FREE(password_script);
2188                 return SAMR_VALIDATION_STATUS_SUCCESS;
2189         }
2190
2191         TALLOC_FREE(password_script);
2192
2193         /*
2194          * Here are the standard AD password quality rules, which we
2195          * run after the script.
2196          */
2197
2198         if (!check_password_quality(utf8_pw)) {
2199                 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2200         }
2201
2202         return SAMR_VALIDATION_STATUS_SUCCESS;
2203 }
2204
2205 /*
2206  * Callback for "samdb_set_password" password change
2207  */
2208 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
2209 {
2210         int ret;
2211
2212         if (!ares) {
2213                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2214         }
2215
2216         if (ares->error != LDB_SUCCESS) {
2217                 ret = ares->error;
2218                 req->context = talloc_steal(req,
2219                                             ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2220                 talloc_free(ares);
2221                 return ldb_request_done(req, ret);
2222         }
2223
2224         if (ares->type != LDB_REPLY_DONE) {
2225                 talloc_free(ares);
2226                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2227         }
2228
2229         req->context = talloc_steal(req,
2230                                     ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2231         talloc_free(ares);
2232         return ldb_request_done(req, LDB_SUCCESS);
2233 }
2234
2235 /*
2236  * Sets the user password using plaintext UTF16 (attribute "new_password") or
2237  * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2238  * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2239  * user change or not. The "rejectReason" gives some more information if the
2240  * change failed.
2241  *
2242  * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2243  *   NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2244  */
2245 static NTSTATUS samdb_set_password_internal(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2246                             struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2247                             const DATA_BLOB *new_password,
2248                             const struct samr_Password *lmNewHash,
2249                             const struct samr_Password *ntNewHash,
2250                             const struct samr_Password *lmOldHash,
2251                             const struct samr_Password *ntOldHash,
2252                             enum samPwdChangeReason *reject_reason,
2253                             struct samr_DomInfo1 **_dominfo,
2254                             bool permit_interdomain_trust)
2255 {
2256         struct ldb_message *msg;
2257         struct ldb_message_element *el;
2258         struct ldb_request *req;
2259         struct dsdb_control_password_change_status *pwd_stat = NULL;
2260         int ret;
2261         bool hash_values = false;
2262         NTSTATUS status = NT_STATUS_OK;
2263
2264 #define CHECK_RET(x) \
2265         if (x != LDB_SUCCESS) { \
2266                 talloc_free(msg); \
2267                 return NT_STATUS_NO_MEMORY; \
2268         }
2269
2270         msg = ldb_msg_new(mem_ctx);
2271         if (msg == NULL) {
2272                 return NT_STATUS_NO_MEMORY;
2273         }
2274         msg->dn = user_dn;
2275         if ((new_password != NULL)
2276                         && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2277                 /* we have the password as plaintext UTF16 */
2278                 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2279                                             new_password, NULL));
2280                 el = ldb_msg_find_element(msg, "clearTextPassword");
2281                 el->flags = LDB_FLAG_MOD_REPLACE;
2282         } else if ((new_password == NULL)
2283                         && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2284                 /* we have a password as LM and/or NT hash */
2285                 if (lmNewHash != NULL) {
2286                         CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2287                                 "dBCSPwd", lmNewHash));
2288                         el = ldb_msg_find_element(msg, "dBCSPwd");
2289                         el->flags = LDB_FLAG_MOD_REPLACE;
2290                 }
2291                 if (ntNewHash != NULL) {
2292                         CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2293                                 "unicodePwd", ntNewHash));
2294                         el = ldb_msg_find_element(msg, "unicodePwd");
2295                         el->flags = LDB_FLAG_MOD_REPLACE;
2296                 }
2297                 hash_values = true;
2298         } else {
2299                 /* the password wasn't specified correctly */
2300                 talloc_free(msg);
2301                 return NT_STATUS_INVALID_PARAMETER;
2302         }
2303
2304         /* build modify request */
2305         ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2306                                 samdb_set_password_callback, NULL);
2307         if (ret != LDB_SUCCESS) {
2308                 talloc_free(msg);
2309                 return NT_STATUS_NO_MEMORY;
2310         }
2311
2312         /* A password change operation */
2313         if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2314                 struct dsdb_control_password_change *change;
2315
2316                 change = talloc(req, struct dsdb_control_password_change);
2317                 if (change == NULL) {
2318                         talloc_free(req);
2319                         talloc_free(msg);
2320                         return NT_STATUS_NO_MEMORY;
2321                 }
2322
2323                 change->old_nt_pwd_hash = ntOldHash;
2324                 change->old_lm_pwd_hash = lmOldHash;
2325
2326                 ret = ldb_request_add_control(req,
2327                                               DSDB_CONTROL_PASSWORD_CHANGE_OID,
2328                                               true, change);
2329                 if (ret != LDB_SUCCESS) {
2330                         talloc_free(req);
2331                         talloc_free(msg);
2332                         return NT_STATUS_NO_MEMORY;
2333                 }
2334         }
2335         if (hash_values) {
2336                 ret = ldb_request_add_control(req,
2337                                               DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2338                                               true, NULL);
2339                 if (ret != LDB_SUCCESS) {
2340                         talloc_free(req);
2341                         talloc_free(msg);
2342                         return NT_STATUS_NO_MEMORY;
2343                 }
2344         }
2345         if (permit_interdomain_trust) {
2346                 ret = ldb_request_add_control(req,
2347                                               DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
2348                                               false, NULL);
2349                 if (ret != LDB_SUCCESS) {
2350                         talloc_free(req);
2351                         talloc_free(msg);
2352                         return NT_STATUS_NO_MEMORY;
2353                 }
2354         }
2355         ret = ldb_request_add_control(req,
2356                                       DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2357                                       true, NULL);
2358         if (ret != LDB_SUCCESS) {
2359                 talloc_free(req);
2360                 talloc_free(msg);
2361                 return NT_STATUS_NO_MEMORY;
2362         }
2363
2364         ret = dsdb_autotransaction_request(ldb, req);
2365
2366         if (req->context != NULL) {
2367                 struct ldb_control *control = talloc_get_type_abort(req->context,
2368                                                                     struct ldb_control);
2369                 pwd_stat = talloc_get_type_abort(control->data,
2370                                                  struct dsdb_control_password_change_status);
2371                 talloc_steal(mem_ctx, pwd_stat);
2372         }
2373
2374         talloc_free(req);
2375         talloc_free(msg);
2376
2377         /* Sets the domain info (if requested) */
2378         if (_dominfo != NULL) {
2379                 struct samr_DomInfo1 *dominfo;
2380
2381                 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2382                 if (dominfo == NULL) {
2383                         return NT_STATUS_NO_MEMORY;
2384                 }
2385
2386                 if (pwd_stat != NULL) {
2387                         dominfo->min_password_length     = pwd_stat->domain_data.minPwdLength;
2388                         dominfo->password_properties     = pwd_stat->domain_data.pwdProperties;
2389                         dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2390                         dominfo->max_password_age        = pwd_stat->domain_data.maxPwdAge;
2391                         dominfo->min_password_age        = pwd_stat->domain_data.minPwdAge;
2392                 }
2393
2394                 *_dominfo = dominfo;
2395         }
2396
2397         if (reject_reason != NULL) {
2398                 if (pwd_stat != NULL) {
2399                         *reject_reason = pwd_stat->reject_reason;
2400                 } else {
2401                         *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2402                 }
2403         }
2404
2405         if (pwd_stat != NULL) {
2406                 talloc_free(pwd_stat);
2407         }
2408
2409         if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2410                 const char *errmsg = ldb_errstring(ldb);
2411                 char *endptr = NULL;
2412                 WERROR werr = WERR_GEN_FAILURE;
2413                 status = NT_STATUS_UNSUCCESSFUL;
2414                 if (errmsg != NULL) {
2415                         werr = W_ERROR(strtol(errmsg, &endptr, 16));
2416                         DBG_WARNING("%s\n", errmsg);
2417                 }
2418                 if (endptr != errmsg) {
2419                         if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2420                                 status = NT_STATUS_WRONG_PASSWORD;
2421                         }
2422                         if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2423                                 status = NT_STATUS_PASSWORD_RESTRICTION;
2424                         }
2425                 }
2426         } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2427                 /* don't let the caller know if an account doesn't exist */
2428                 status = NT_STATUS_WRONG_PASSWORD;
2429         } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
2430                 status = NT_STATUS_ACCESS_DENIED;
2431         } else if (ret != LDB_SUCCESS) {
2432                 DEBUG(1, ("Failed to set password on %s: %s\n",
2433                           ldb_dn_get_linearized(user_dn),
2434                           ldb_errstring(ldb)));
2435                 status = NT_STATUS_UNSUCCESSFUL;
2436         }
2437
2438         return status;
2439 }
2440
2441 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2442                             struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2443                             const DATA_BLOB *new_password,
2444                             const struct samr_Password *lmNewHash,
2445                             const struct samr_Password *ntNewHash,
2446                             const struct samr_Password *lmOldHash,
2447                             const struct samr_Password *ntOldHash,
2448                             enum samPwdChangeReason *reject_reason,
2449                             struct samr_DomInfo1 **_dominfo)
2450 {
2451         return samdb_set_password_internal(ldb, mem_ctx,
2452                             user_dn, domain_dn,
2453                             new_password,
2454                             lmNewHash, ntNewHash,
2455                             lmOldHash, ntOldHash,
2456                             reject_reason, _dominfo,
2457                             false); /* reject trusts */
2458 }
2459
2460 /*
2461  * Sets the user password using plaintext UTF16 (attribute "new_password") or
2462  * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2463  * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2464  * user change or not. The "rejectReason" gives some more information if the
2465  * change failed.
2466  *
2467  * This wrapper function for "samdb_set_password" takes a SID as input rather
2468  * than a user DN.
2469  *
2470  * This call encapsulates a new LDB transaction for changing the password;
2471  * therefore the user hasn't to start a new one.
2472  *
2473  * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2474  *   NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2475  *   NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2476  *   NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2477  */
2478 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2479                                 const struct dom_sid *user_sid,
2480                                 const uint32_t *new_version, /* optional for trusts */
2481                                 const DATA_BLOB *new_password,
2482                                 const struct samr_Password *lmNewHash,
2483                                 const struct samr_Password *ntNewHash,
2484                                 const struct samr_Password *lmOldHash,
2485                                 const struct samr_Password *ntOldHash,
2486                                 enum samPwdChangeReason *reject_reason,
2487                                 struct samr_DomInfo1 **_dominfo) 
2488 {
2489         TALLOC_CTX *frame = talloc_stackframe();
2490         NTSTATUS nt_status;
2491         const char * const user_attrs[] = {
2492                 "userAccountControl",
2493                 "sAMAccountName",
2494                 NULL
2495         };
2496         struct ldb_message *user_msg = NULL;
2497         int ret;
2498         uint32_t uac = 0;
2499
2500         ret = ldb_transaction_start(ldb);
2501         if (ret != LDB_SUCCESS) {
2502                 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2503                 TALLOC_FREE(frame);
2504                 return NT_STATUS_TRANSACTION_ABORTED;
2505         }
2506
2507         ret = dsdb_search_one(ldb, frame, &user_msg, ldb_get_default_basedn(ldb),
2508                               LDB_SCOPE_SUBTREE, user_attrs, 0,
2509                               "(&(objectSid=%s)(objectClass=user))",
2510                               ldap_encode_ndr_dom_sid(frame, user_sid));
2511         if (ret != LDB_SUCCESS) {
2512                 ldb_transaction_cancel(ldb);
2513                 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2514                           "returning NO_SUCH_USER\n",
2515                           dom_sid_string(frame, user_sid),
2516                           ldb_strerror(ret), ldb_errstring(ldb)));
2517                 TALLOC_FREE(frame);
2518                 return NT_STATUS_NO_SUCH_USER;
2519         }
2520
2521         uac = ldb_msg_find_attr_as_uint(user_msg, "userAccountControl", 0);
2522         if (!(uac & UF_ACCOUNT_TYPE_MASK)) {
2523                 ldb_transaction_cancel(ldb);
2524                 DEBUG(1, ("samdb_set_password_sid: invalid "
2525                           "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2526                           "returning NO_SUCH_USER\n",
2527                           (unsigned)uac, dom_sid_string(frame, user_sid),
2528                           ldb_dn_get_linearized(user_msg->dn)));
2529                 TALLOC_FREE(frame);
2530                 return NT_STATUS_NO_SUCH_USER;
2531         }
2532
2533         if (uac & UF_INTERDOMAIN_TRUST_ACCOUNT) {
2534                 const char * const tdo_attrs[] = {
2535                         "trustAuthIncoming",
2536                         "trustDirection",
2537                         NULL
2538                 };
2539                 struct ldb_message *tdo_msg = NULL;
2540                 const char *account_name = NULL;
2541                 uint32_t trust_direction;
2542                 uint32_t i;
2543                 const struct ldb_val *old_val = NULL;
2544                 struct trustAuthInOutBlob old_blob = {
2545                         .count = 0,
2546                 };
2547                 uint32_t old_version = 0;
2548                 struct AuthenticationInformation *old_version_a = NULL;
2549                 uint32_t _new_version = 0;
2550                 struct trustAuthInOutBlob new_blob = {
2551                         .count = 0,
2552                 };
2553                 struct ldb_val new_val = {
2554                         .length = 0,
2555                 };
2556                 struct timeval tv = timeval_current();
2557                 NTTIME now = timeval_to_nttime(&tv);
2558                 enum ndr_err_code ndr_err;
2559
2560                 if (new_password == NULL && ntNewHash == NULL) {
2561                         ldb_transaction_cancel(ldb);
2562                         DEBUG(1, ("samdb_set_password_sid: "
2563                                   "no new password provided "
2564                                   "sAMAccountName for SID[%s] DN[%s], "
2565                                   "returning INVALID_PARAMETER\n",
2566                                   dom_sid_string(frame, user_sid),
2567                                   ldb_dn_get_linearized(user_msg->dn)));
2568                         TALLOC_FREE(frame);
2569                         return NT_STATUS_INVALID_PARAMETER;
2570                 }
2571
2572                 if (new_password != NULL && ntNewHash != NULL) {
2573                         ldb_transaction_cancel(ldb);
2574                         DEBUG(1, ("samdb_set_password_sid: "
2575                                   "two new passwords provided "
2576                                   "sAMAccountName for SID[%s] DN[%s], "
2577                                   "returning INVALID_PARAMETER\n",
2578                                   dom_sid_string(frame, user_sid),
2579                                   ldb_dn_get_linearized(user_msg->dn)));
2580                         TALLOC_FREE(frame);
2581                         return NT_STATUS_INVALID_PARAMETER;
2582                 }
2583
2584                 if (new_password != NULL && (new_password->length % 2)) {
2585                         ldb_transaction_cancel(ldb);
2586                         DEBUG(2, ("samdb_set_password_sid: "
2587                                   "invalid utf16 length (%zu) "
2588                                   "sAMAccountName for SID[%s] DN[%s], "
2589                                   "returning WRONG_PASSWORD\n",
2590                                   new_password->length,
2591                                   dom_sid_string(frame, user_sid),
2592                                   ldb_dn_get_linearized(user_msg->dn)));
2593                         TALLOC_FREE(frame);
2594                         return NT_STATUS_WRONG_PASSWORD;
2595                 }
2596
2597                 if (new_password != NULL && new_password->length >= 500) {
2598                         ldb_transaction_cancel(ldb);
2599                         DEBUG(2, ("samdb_set_password_sid: "
2600                                   "utf16 password too long (%zu) "
2601                                   "sAMAccountName for SID[%s] DN[%s], "
2602                                   "returning WRONG_PASSWORD\n",
2603                                   new_password->length,
2604                                   dom_sid_string(frame, user_sid),
2605                                   ldb_dn_get_linearized(user_msg->dn)));
2606                         TALLOC_FREE(frame);
2607                         return NT_STATUS_WRONG_PASSWORD;
2608                 }
2609
2610                 account_name = ldb_msg_find_attr_as_string(user_msg,
2611                                                         "sAMAccountName", NULL);
2612                 if (account_name == NULL) {
2613                         ldb_transaction_cancel(ldb);
2614                         DEBUG(1, ("samdb_set_password_sid: missing "
2615                                   "sAMAccountName for SID[%s] DN[%s], "
2616                                   "returning NO_SUCH_USER\n",
2617                                   dom_sid_string(frame, user_sid),
2618                                   ldb_dn_get_linearized(user_msg->dn)));
2619                         TALLOC_FREE(frame);
2620                         return NT_STATUS_NO_SUCH_USER;
2621                 }
2622
2623                 nt_status = dsdb_trust_search_tdo_by_type(ldb,
2624                                                           SEC_CHAN_DOMAIN,
2625                                                           account_name,
2626                                                           tdo_attrs,
2627                                                           frame, &tdo_msg);
2628                 if (!NT_STATUS_IS_OK(nt_status)) {
2629                         ldb_transaction_cancel(ldb);
2630                         DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2631                                   "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2632                                   "returning INTERNAL_DB_CORRUPTION\n",
2633                                   nt_errstr(nt_status), account_name,
2634                                   dom_sid_string(frame, user_sid),
2635                                   ldb_dn_get_linearized(user_msg->dn)));
2636                         TALLOC_FREE(frame);
2637                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
2638                 }
2639
2640                 trust_direction = ldb_msg_find_attr_as_int(tdo_msg,
2641                                                            "trustDirection", 0);
2642                 if (!(trust_direction & LSA_TRUST_DIRECTION_INBOUND)) {
2643                         ldb_transaction_cancel(ldb);
2644                         DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2645                                   "not inbound for sAMAccountName[%s] "
2646                                   "DN[%s] TDO[%s], "
2647                                   "returning INTERNAL_DB_CORRUPTION\n",
2648                                   (unsigned)trust_direction,
2649                                   account_name,
2650                                   ldb_dn_get_linearized(user_msg->dn),
2651                                   ldb_dn_get_linearized(tdo_msg->dn)));
2652                         TALLOC_FREE(frame);
2653                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
2654                 }
2655
2656                 old_val = ldb_msg_find_ldb_val(tdo_msg, "trustAuthIncoming");
2657                 if (old_val != NULL) {
2658                         ndr_err = ndr_pull_struct_blob(old_val, frame, &old_blob,
2659                                         (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2660                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2661                                 ldb_transaction_cancel(ldb);
2662                                 DEBUG(1, ("samdb_set_password_sid: "
2663                                           "failed(%s) to parse "
2664                                           "trustAuthOutgoing sAMAccountName[%s] "
2665                                           "DN[%s] TDO[%s], "
2666                                           "returning INTERNAL_DB_CORRUPTION\n",
2667                                           ndr_map_error2string(ndr_err),
2668                                           account_name,
2669                                           ldb_dn_get_linearized(user_msg->dn),
2670                                           ldb_dn_get_linearized(tdo_msg->dn)));
2671
2672                                 TALLOC_FREE(frame);
2673                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2674                         }
2675                 }
2676
2677                 for (i = old_blob.current.count; i > 0; i--) {
2678                         struct AuthenticationInformation *a =
2679                                 &old_blob.current.array[i - 1];
2680
2681                         switch (a->AuthType) {
2682                         case TRUST_AUTH_TYPE_NONE:
2683                                 if (i == old_blob.current.count) {
2684                                         /*
2685                                          * remove TRUST_AUTH_TYPE_NONE at the
2686                                          * end
2687                                          */
2688                                         old_blob.current.count--;
2689                                 }
2690                                 break;
2691
2692                         case TRUST_AUTH_TYPE_VERSION:
2693                                 old_version_a = a;
2694                                 old_version = a->AuthInfo.version.version;
2695                                 break;
2696
2697                         case TRUST_AUTH_TYPE_CLEAR:
2698                                 break;
2699
2700                         case TRUST_AUTH_TYPE_NT4OWF:
2701                                 break;
2702                         }
2703                 }
2704
2705                 if (new_version == NULL) {
2706                         _new_version = 0;
2707                         new_version = &_new_version;
2708                 }
2709
2710                 if (old_version_a != NULL && *new_version != (old_version + 1)) {
2711                         old_version_a->LastUpdateTime = now;
2712                         old_version_a->AuthType = TRUST_AUTH_TYPE_NONE;
2713                 }
2714
2715                 new_blob.count = MAX(old_blob.current.count, 2);
2716                 new_blob.current.array = talloc_zero_array(frame,
2717                                                 struct AuthenticationInformation,
2718                                                 new_blob.count);
2719                 if (new_blob.current.array == NULL) {
2720                         ldb_transaction_cancel(ldb);
2721                         TALLOC_FREE(frame);
2722                         return NT_STATUS_NO_MEMORY;
2723                 }
2724                 new_blob.previous.array = talloc_zero_array(frame,
2725                                                 struct AuthenticationInformation,
2726                                                 new_blob.count);
2727                 if (new_blob.current.array == NULL) {
2728                         ldb_transaction_cancel(ldb);
2729                         TALLOC_FREE(frame);
2730                         return NT_STATUS_NO_MEMORY;
2731                 }
2732
2733                 for (i = 0; i < old_blob.current.count; i++) {
2734                         struct AuthenticationInformation *o =
2735                                 &old_blob.current.array[i];
2736                         struct AuthenticationInformation *p =
2737                                 &new_blob.previous.array[i];
2738
2739                         *p = *o;
2740                         new_blob.previous.count++;
2741                 }
2742                 for (; i < new_blob.count; i++) {
2743                         struct AuthenticationInformation *pi =
2744                                 &new_blob.previous.array[i];
2745
2746                         if (i == 0) {
2747                                 /*
2748                                  * new_blob.previous is still empty so
2749                                  * we'll do new_blob.previous = new_blob.current
2750                                  * below.
2751                                  */
2752                                 break;
2753                         }
2754
2755                         pi->LastUpdateTime = now;
2756                         pi->AuthType = TRUST_AUTH_TYPE_NONE;
2757                         new_blob.previous.count++;
2758                 }
2759
2760                 for (i = 0; i < new_blob.count; i++) {
2761                         struct AuthenticationInformation *ci =
2762                                 &new_blob.current.array[i];
2763
2764                         ci->LastUpdateTime = now;
2765                         switch (i) {
2766                         case 0:
2767                                 if (ntNewHash != NULL) {
2768                                         ci->AuthType = TRUST_AUTH_TYPE_NT4OWF;
2769                                         ci->AuthInfo.nt4owf.password = *ntNewHash;
2770                                         break;
2771                                 }
2772
2773                                 ci->AuthType = TRUST_AUTH_TYPE_CLEAR;
2774                                 ci->AuthInfo.clear.size = new_password->length;
2775                                 ci->AuthInfo.clear.password = new_password->data;
2776                                 break;
2777                         case 1:
2778                                 ci->AuthType = TRUST_AUTH_TYPE_VERSION;
2779                                 ci->AuthInfo.version.version = *new_version;
2780                                 break;
2781                         default:
2782                                 ci->AuthType = TRUST_AUTH_TYPE_NONE;
2783                                 break;
2784                         }
2785
2786                         new_blob.current.count++;
2787                 }
2788
2789                 if (new_blob.previous.count == 0) {
2790                         TALLOC_FREE(new_blob.previous.array);
2791                         new_blob.previous = new_blob.current;
2792                 }
2793
2794                 ndr_err = ndr_push_struct_blob(&new_val, frame, &new_blob,
2795                                 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2796                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2797                         ldb_transaction_cancel(ldb);
2798                         DEBUG(1, ("samdb_set_password_sid: "
2799                                   "failed(%s) to generate "
2800                                   "trustAuthOutgoing sAMAccountName[%s] "
2801                                   "DN[%s] TDO[%s], "
2802                                   "returning UNSUCCESSFUL\n",
2803                                   ndr_map_error2string(ndr_err),
2804                                   account_name,
2805                                   ldb_dn_get_linearized(user_msg->dn),
2806                                   ldb_dn_get_linearized(tdo_msg->dn)));
2807                         TALLOC_FREE(frame);
2808                         return NT_STATUS_UNSUCCESSFUL;
2809                 }
2810
2811                 tdo_msg->num_elements = 0;
2812                 TALLOC_FREE(tdo_msg->elements);
2813
2814                 ret = ldb_msg_add_empty(tdo_msg, "trustAuthIncoming",
2815                                         LDB_FLAG_MOD_REPLACE, NULL);
2816                 if (ret != LDB_SUCCESS) {
2817                         ldb_transaction_cancel(ldb);
2818                         TALLOC_FREE(frame);
2819                         return NT_STATUS_NO_MEMORY;
2820                 }
2821                 ret = ldb_msg_add_value(tdo_msg, "trustAuthIncoming",
2822                                         &new_val, NULL);
2823                 if (ret != LDB_SUCCESS) {
2824                         ldb_transaction_cancel(ldb);
2825                         TALLOC_FREE(frame);
2826                         return NT_STATUS_NO_MEMORY;
2827                 }
2828
2829                 ret = ldb_modify(ldb, tdo_msg);
2830                 if (ret != LDB_SUCCESS) {
2831                         nt_status = dsdb_ldb_err_to_ntstatus(ret);
2832                         ldb_transaction_cancel(ldb);
2833                         DEBUG(1, ("samdb_set_password_sid: "
2834                                   "failed to replace "
2835                                   "trustAuthOutgoing sAMAccountName[%s] "
2836                                   "DN[%s] TDO[%s], "
2837                                   "%s - %s\n",
2838                                   account_name,
2839                                   ldb_dn_get_linearized(user_msg->dn),
2840                                   ldb_dn_get_linearized(tdo_msg->dn),
2841                                   nt_errstr(nt_status), ldb_errstring(ldb)));
2842                         TALLOC_FREE(frame);
2843                         return nt_status;
2844                 }
2845         }
2846
2847         nt_status = samdb_set_password_internal(ldb, mem_ctx,
2848                                                 user_msg->dn, NULL,
2849                                                 new_password,
2850                                                 lmNewHash, ntNewHash,
2851                                                 lmOldHash, ntOldHash,
2852                                                 reject_reason, _dominfo,
2853                                                 true); /* permit trusts */
2854         if (!NT_STATUS_IS_OK(nt_status)) {
2855                 ldb_transaction_cancel(ldb);
2856                 TALLOC_FREE(frame);
2857                 return nt_status;
2858         }
2859
2860         ret = ldb_transaction_commit(ldb);
2861         if (ret != LDB_SUCCESS) {
2862                 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2863                          ldb_dn_get_linearized(user_msg->dn),
2864                          ldb_errstring(ldb)));
2865                 TALLOC_FREE(frame);
2866                 return NT_STATUS_TRANSACTION_ABORTED;
2867         }
2868
2869         TALLOC_FREE(frame);
2870         return NT_STATUS_OK;
2871 }
2872
2873
2874 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
2875                                                  struct dom_sid *sid, struct ldb_dn **ret_dn) 
2876 {
2877         struct ldb_message *msg;
2878         struct ldb_dn *basedn = NULL;
2879         char *sidstr;
2880         int ret;
2881
2882         sidstr = dom_sid_string(mem_ctx, sid);
2883         NT_STATUS_HAVE_NO_MEMORY(sidstr);
2884
2885         /* We might have to create a ForeignSecurityPrincipal, even if this user
2886          * is in our own domain */
2887
2888         msg = ldb_msg_new(sidstr);
2889         if (msg == NULL) {
2890                 talloc_free(sidstr);
2891                 return NT_STATUS_NO_MEMORY;
2892         }
2893
2894         ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2895                                 ldb_get_default_basedn(sam_ctx),
2896                                 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2897                                 &basedn);
2898         if (ret != LDB_SUCCESS) {
2899                 DEBUG(0, ("Failed to find DN for "
2900                           "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2901                 talloc_free(sidstr);
2902                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2903         }
2904
2905         /* add core elements to the ldb_message for the alias */
2906         msg->dn = basedn;
2907         if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2908                 talloc_free(sidstr);
2909                 return NT_STATUS_NO_MEMORY;
2910         }
2911
2912         ret = ldb_msg_add_string(msg, "objectClass",
2913                                  "foreignSecurityPrincipal");
2914         if (ret != LDB_SUCCESS) {
2915                 talloc_free(sidstr);
2916                 return NT_STATUS_NO_MEMORY;
2917         }
2918
2919         /* create the alias */
2920         ret = ldb_add(sam_ctx, msg);
2921         if (ret != LDB_SUCCESS) {
2922                 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2923                          "record %s: %s\n", 
2924                          ldb_dn_get_linearized(msg->dn),
2925                          ldb_errstring(sam_ctx)));
2926                 talloc_free(sidstr);
2927                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2928         }
2929
2930         *ret_dn = talloc_steal(mem_ctx, msg->dn);
2931         talloc_free(sidstr);
2932
2933         return NT_STATUS_OK;
2934 }
2935
2936
2937 /*
2938   Find the DN of a domain, assuming it to be a dotted.dns name
2939 */
2940
2941 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain) 
2942 {
2943         unsigned int i;
2944         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2945         const char *binary_encoded;
2946         const char * const *split_realm;
2947         struct ldb_dn *dn;
2948
2949         if (!tmp_ctx) {
2950                 return NULL;
2951         }
2952
2953         split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2954         if (!split_realm) {
2955                 talloc_free(tmp_ctx);
2956                 return NULL;
2957         }
2958         dn = ldb_dn_new(mem_ctx, ldb, NULL);
2959         for (i=0; split_realm[i]; i++) {
2960                 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2961                 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2962                         DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2963                                   binary_encoded, ldb_dn_get_linearized(dn)));
2964                         talloc_free(tmp_ctx);
2965                         return NULL;
2966                 }
2967         }
2968         if (!ldb_dn_validate(dn)) {
2969                 DEBUG(2, ("Failed to validated DN %s\n",
2970                           ldb_dn_get_linearized(dn)));
2971                 talloc_free(tmp_ctx);
2972                 return NULL;
2973         }
2974         talloc_free(tmp_ctx);
2975         return dn;
2976 }
2977
2978
2979 /*
2980   Find the DNS equivalent of a DN, in dotted DNS form
2981 */
2982 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2983 {
2984         int i, num_components = ldb_dn_get_comp_num(dn);
2985         char *dns_name = talloc_strdup(mem_ctx, "");
2986         if (dns_name == NULL) {
2987                 return NULL;
2988         }
2989
2990         for (i=0; i<num_components; i++) {
2991                 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2992                 char *s;
2993                 if (v == NULL) {
2994                         talloc_free(dns_name);
2995                         return NULL;
2996                 }
2997                 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2998                                                   (int)v->length, (int)v->length, (char *)v->data);
2999                 if (s == NULL) {
3000                         talloc_free(dns_name);
3001                         return NULL;
3002                 }
3003                 dns_name = s;
3004         }
3005
3006         /* remove the last '.' */
3007         if (dns_name[0] != 0) {
3008                 dns_name[strlen(dns_name)-1] = 0;
3009         }
3010
3011         return dns_name;
3012 }
3013
3014 /*
3015   Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
3016   name is based on the forest DNS name
3017 */
3018 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
3019                                 TALLOC_CTX *mem_ctx,
3020                                 const struct GUID *ntds_guid)
3021 {
3022         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3023         const char *guid_str;
3024         struct ldb_dn *forest_dn;
3025         const char *dnsforest;
3026         char *ret;
3027
3028         guid_str = GUID_string(tmp_ctx, ntds_guid);
3029         if (guid_str == NULL) {
3030                 talloc_free(tmp_ctx);
3031                 return NULL;
3032         }
3033         forest_dn = ldb_get_root_basedn(samdb);
3034         if (forest_dn == NULL) {
3035                 talloc_free(tmp_ctx);
3036                 return NULL;
3037         }
3038         dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
3039         if (dnsforest == NULL) {
3040                 talloc_free(tmp_ctx);
3041                 return NULL;
3042         }
3043         ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
3044         talloc_free(tmp_ctx);
3045         return ret;
3046 }
3047
3048
3049 /*
3050   Find the DN of a domain, be it the netbios or DNS name 
3051 */
3052 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
3053                                   const char *domain_name) 
3054 {
3055         const char * const domain_ref_attrs[] = {
3056                 "ncName", NULL
3057         };
3058         const char * const domain_ref2_attrs[] = {
3059                 NULL
3060         };
3061         struct ldb_result *res_domain_ref;
3062         char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
3063         /* find the domain's DN */
3064         int ret_domain = ldb_search(ldb, mem_ctx,
3065                                             &res_domain_ref, 
3066                                             samdb_partitions_dn(ldb, mem_ctx), 
3067                                             LDB_SCOPE_ONELEVEL, 
3068                                             domain_ref_attrs,
3069                                             "(&(nETBIOSName=%s)(objectclass=crossRef))", 
3070                                             escaped_domain);
3071         if (ret_domain != LDB_SUCCESS) {
3072                 return NULL;
3073         }
3074
3075         if (res_domain_ref->count == 0) {
3076                 ret_domain = ldb_search(ldb, mem_ctx,
3077                                                 &res_domain_ref, 
3078                                                 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
3079                                                 LDB_SCOPE_BASE,
3080                                                 domain_ref2_attrs,
3081                                                 "(objectclass=domain)");
3082                 if (ret_domain != LDB_SUCCESS) {
3083                         return NULL;
3084                 }
3085
3086                 if (res_domain_ref->count == 1) {
3087                         return res_domain_ref->msgs[0]->dn;
3088                 }
3089                 return NULL;
3090         }
3091
3092         if (res_domain_ref->count > 1) {
3093                 DEBUG(0,("Found %d records matching domain [%s]\n", 
3094                          ret_domain, domain_name));
3095                 return NULL;
3096         }
3097
3098         return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
3099
3100 }
3101
3102
3103 /*
3104   use a GUID to find a DN
3105  */
3106 int dsdb_find_dn_by_guid(struct ldb_context *ldb, 
3107                          TALLOC_CTX *mem_ctx,
3108                          const struct GUID *guid,
3109                          uint32_t dsdb_flags,
3110                          struct ldb_dn **dn)
3111 {
3112         int ret;
3113         struct ldb_result *res;
3114         const char *attrs[] = { NULL };
3115         char *guid_str = GUID_string(mem_ctx, guid);
3116
3117         if (!guid_str) {
3118                 return ldb_operr(ldb);
3119         }
3120
3121         ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3122                           DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3123                           DSDB_SEARCH_SHOW_EXTENDED_DN |
3124                           DSDB_SEARCH_ONE_ONLY | dsdb_flags,
3125                           "objectGUID=%s", guid_str);
3126         talloc_free(guid_str);
3127         if (ret != LDB_SUCCESS) {
3128                 return ret;
3129         }
3130
3131         *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3132         talloc_free(res);
3133
3134         return LDB_SUCCESS;
3135 }
3136
3137 /*
3138   use a DN to find a GUID with a given attribute name
3139  */
3140 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
3141                               struct ldb_dn *dn, const char *attribute,
3142                               struct GUID *guid)
3143 {
3144         int ret;
3145         struct ldb_result *res = NULL;
3146         const char *attrs[2];
3147         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3148
3149         attrs[0] = attribute;
3150         attrs[1] = NULL;
3151
3152         ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3153                              DSDB_SEARCH_SHOW_DELETED |
3154                              DSDB_SEARCH_SHOW_RECYCLED);
3155         if (ret != LDB_SUCCESS) {
3156                 talloc_free(tmp_ctx);
3157                 return ret;
3158         }
3159         /* satisfy clang */
3160         if (res == NULL) {
3161                 talloc_free(tmp_ctx);
3162                 return LDB_ERR_OTHER;
3163         }
3164         if (res->count < 1) {
3165                 talloc_free(tmp_ctx);
3166                 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3167         }
3168         *guid = samdb_result_guid(res->msgs[0], attribute);
3169         talloc_free(tmp_ctx);
3170         return LDB_SUCCESS;
3171 }
3172
3173 /*
3174   use a DN to find a GUID
3175  */
3176 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
3177                          struct ldb_dn *dn, struct GUID *guid)
3178 {
3179         return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
3180 }
3181
3182
3183
3184 /*
3185  adds the given GUID to the given ldb_message. This value is added
3186  for the given attr_name (may be either "objectGUID" or "parentGUID").
3187  */
3188 int dsdb_msg_add_guid(struct ldb_message *msg,
3189                 struct GUID *guid,
3190                 const char *attr_name)
3191 {
3192         int ret;
3193         struct ldb_val v;
3194         NTSTATUS status;
3195         TALLOC_CTX *tmp_ctx =  talloc_init("dsdb_msg_add_guid");
3196
3197         status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
3198         if (!NT_STATUS_IS_OK(status)) {
3199                 ret = LDB_ERR_OPERATIONS_ERROR;
3200                 goto done;
3201         }
3202
3203         ret = ldb_msg_add_steal_value(msg, attr_name, &v);
3204         if (ret != LDB_SUCCESS) {
3205                 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
3206                                          attr_name));
3207                 goto done;
3208         }
3209
3210         ret = LDB_SUCCESS;
3211
3212 done:
3213         talloc_free(tmp_ctx);
3214         return ret;
3215
3216 }
3217
3218
3219 /*
3220   use a DN to find a SID
3221  */
3222 int dsdb_find_sid_by_dn(struct ldb_context *ldb, 
3223                         struct ldb_dn *dn, struct dom_sid *sid)
3224 {
3225         int ret;
3226         struct ldb_result *res = NULL;
3227         const char *attrs[] = { "objectSid", NULL };
3228         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3229         struct dom_sid *s;
3230
3231         ZERO_STRUCTP(sid);
3232
3233         ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3234                              DSDB_SEARCH_SHOW_DELETED |
3235                              DSDB_SEARCH_SHOW_RECYCLED);
3236         if (ret != LDB_SUCCESS) {
3237                 talloc_free(tmp_ctx);
3238                 return ret;
3239         }
3240         if (res == NULL) {
3241                 talloc_free(tmp_ctx);
3242                 return LDB_ERR_OTHER;
3243         }
3244         if (res->count < 1) {
3245                 talloc_free(tmp_ctx);
3246                 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3247         }
3248         s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
3249         if (s == NULL) {
3250                 talloc_free(tmp_ctx);
3251                 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3252         }
3253         *sid = *s;
3254         talloc_free(tmp_ctx);
3255         return LDB_SUCCESS;
3256 }
3257
3258 /*
3259   use a SID to find a DN
3260  */
3261 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
3262                         TALLOC_CTX *mem_ctx,
3263                         struct dom_sid *sid, struct ldb_dn **dn)
3264 {
3265         int ret;
3266         struct ldb_result *res;
3267         const char *attrs[] = { NULL };
3268         char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
3269
3270         if (!sid_str) {
3271                 return ldb_operr(ldb);
3272         }
3273
3274         ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3275                           DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3276                           DSDB_SEARCH_SHOW_EXTENDED_DN |
3277                           DSDB_SEARCH_ONE_ONLY,
3278                           "objectSid=%s", sid_str);
3279         talloc_free(sid_str);
3280         if (ret != LDB_SUCCESS) {
3281                 return ret;
3282         }
3283
3284         *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3285         talloc_free(res);
3286
3287         return LDB_SUCCESS;
3288 }
3289
3290 /*
3291   load a repsFromTo blob list for a given partition GUID
3292   attr must be "repsFrom" or "repsTo"
3293  */
3294 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3295                      const char *attr, struct repsFromToBlob **r, uint32_t *count)
3296 {
3297         const char *attrs[] = { attr, NULL };
3298         struct ldb_result *res = NULL;
3299         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3300         unsigned int i;
3301         struct ldb_message_element *el;
3302         int ret;
3303
3304         *r = NULL;
3305         *count = 0;
3306
3307         ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
3308         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3309                 /* partition hasn't been replicated yet */
3310                 return WERR_OK;
3311         }
3312         if (ret != LDB_SUCCESS) {
3313                 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
3314                 talloc_free(tmp_ctx);
3315                 return WERR_DS_DRA_INTERNAL_ERROR;
3316         }
3317
3318         /* satisfy clang */
3319         if (res == NULL) {
3320                 talloc_free(tmp_ctx);
3321                 return WERR_DS_DRA_INTERNAL_ERROR;
3322         }
3323         el = ldb_msg_find_element(res->msgs[0], attr);
3324         if (el == NULL) {
3325                 /* it's OK to be empty */
3326                 talloc_free(tmp_ctx);
3327                 return WERR_OK;
3328         }
3329
3330         *count = el->num_values;
3331         *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
3332         if (*r == NULL) {
3333                 talloc_free(tmp_ctx);
3334                 return WERR_DS_DRA_INTERNAL_ERROR;
3335         }
3336
3337         for (i=0; i<(*count); i++) {
3338                 enum ndr_err_code ndr_err;
3339                 ndr_err = ndr_pull_struct_blob(&el->values[i], 
3340                                                mem_ctx, 
3341                                                &(*r)[i], 
3342                                                (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
3343                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3344                         talloc_free(tmp_ctx);
3345                         return WERR_DS_DRA_INTERNAL_ERROR;
3346                 }
3347         }
3348
3349         talloc_free(tmp_ctx);
3350         
3351         return WERR_OK;
3352 }
3353
3354 /*
3355   save the repsFromTo blob list for a given partition GUID
3356   attr must be "repsFrom" or "repsTo"
3357  */
3358 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3359                      const char *attr, struct repsFromToBlob *r, uint32_t count)
3360 {
3361         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3362         struct ldb_message *msg;
3363         struct ldb_message_element *el;
3364         unsigned int i;
3365
3366         msg = ldb_msg_new(tmp_ctx);
3367         msg->dn = dn;
3368         if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
3369                 goto failed;
3370         }
3371
3372         el->values = talloc_array(msg, struct ldb_val, count);
3373         if (!el->values) {
3374                 goto failed;
3375         }
3376
3377         for (i=0; i<count; i++) {
3378                 struct ldb_val v;
3379                 enum ndr_err_code ndr_err;
3380
3381                 ndr_err = ndr_push_struct_blob(&v, tmp_ctx, 
3382                                                &r[i], 
3383                                                (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
3384                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3385                         goto failed;
3386                 }
3387
3388                 el->num_values++;
3389                 el->values[i] = v;
3390         }
3391
3392         if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
3393                 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
3394                 goto failed;
3395         }
3396
3397         talloc_free(tmp_ctx);
3398         
3399         return WERR_OK;
3400
3401 failed:
3402         talloc_free(tmp_ctx);
3403         return WERR_DS_DRA_INTERNAL_ERROR;
3404 }
3405
3406
3407 /*
3408   load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3409   object for a partition
3410  */
3411 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
3412                                 uint64_t *uSN, uint64_t *urgent_uSN)
3413 {
3414         struct ldb_request *req;
3415         int ret;
3416         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3417         struct dsdb_control_current_partition *p_ctrl;
3418         struct ldb_result *res;
3419
3420         res = talloc_zero(tmp_ctx, struct ldb_result);
3421         if (!res) {
3422                 talloc_free(tmp_ctx);
3423                 return ldb_oom(ldb);
3424         }
3425
3426         ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3427                                    ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
3428                                    LDB_SCOPE_BASE,
3429                                    NULL, NULL,
3430                                    NULL,
3431                                    res, ldb_search_default_callback,
3432                                    NULL);
3433         if (ret != LDB_SUCCESS) {
3434                 talloc_free(tmp_ctx);
3435                 return ret;
3436         }
3437
3438         p_ctrl = talloc(req, struct dsdb_control_current_partition);
3439         if (p_ctrl == NULL) {
3440                 talloc_free(tmp_ctx);
3441                 return ldb_oom(ldb);
3442         }
3443         p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
3444         p_ctrl->dn = dn;
3445         
3446         ret = ldb_request_add_control(req,
3447                                       DSDB_CONTROL_CURRENT_PARTITION_OID,
3448                                       false, p_ctrl);
3449         if (ret != LDB_SUCCESS) {
3450                 talloc_free(tmp_ctx);
3451                 return ret;
3452         }
3453         
3454         /* Run the new request */
3455         ret = ldb_request(ldb, req);
3456         
3457         if (ret == LDB_SUCCESS) {
3458                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3459         }
3460
3461         if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
3462                 /* it hasn't been created yet, which means
3463                    an implicit value of zero */
3464                 *uSN = 0;
3465                 talloc_free(tmp_ctx);
3466                 return LDB_SUCCESS;
3467         }
3468
3469         if (ret != LDB_SUCCESS) {
3470                 talloc_free(tmp_ctx);
3471                 return ret;
3472         }
3473
3474         if (res->count < 1) {
3475                 *uSN = 0;
3476                 if (urgent_uSN) {
3477                         *urgent_uSN = 0;
3478                 }
3479         } else {
3480                 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
3481                 if (urgent_uSN) {
3482                         *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
3483                 }
3484         }
3485
3486         talloc_free(tmp_ctx);
3487
3488         return LDB_SUCCESS;     
3489 }
3490
3491 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
3492                                                    const struct drsuapi_DsReplicaCursor2 *c2)
3493 {
3494         return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3495 }
3496
3497 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
3498                                     const struct drsuapi_DsReplicaCursor *c2)
3499 {
3500         return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3501 }
3502
3503
3504 /*
3505   see if a computer identified by its invocationId is a RODC
3506 */
3507 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
3508 {
3509         /* 1) find the DN for this servers NTDSDSA object
3510            2) search for the msDS-isRODC attribute
3511            3) if not present then not a RODC
3512            4) if present and TRUE then is a RODC
3513         */
3514         struct ldb_dn *config_dn;
3515         const char *attrs[] = { "msDS-isRODC", NULL };
3516         int ret;
3517         struct ldb_result *res;
3518         TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
3519
3520         config_dn = ldb_get_config_basedn(sam_ctx);
3521         if (!config_dn) {
3522                 talloc_free(tmp_ctx);
3523                 return ldb_operr(sam_ctx);
3524         }
3525
3526         ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
3527                           DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
3528
3529         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3530                 *is_rodc = false;
3531                 talloc_free(tmp_ctx);
3532                 return LDB_SUCCESS;
3533         }
3534
3535         if (ret != LDB_SUCCESS) {
3536                 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3537                          GUID_string(tmp_ctx, objectGUID)));
3538                 *is_rodc = false;
3539                 talloc_free(tmp_ctx);
3540                 return ret;
3541         }
3542
3543         ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
3544         *is_rodc = (ret == 1);
3545
3546         talloc_free(tmp_ctx);
3547         return LDB_SUCCESS;
3548 }
3549
3550
3551 /*
3552   see if we are a RODC
3553 */
3554 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
3555 {
3556         const struct GUID *objectGUID;
3557         int ret;
3558         bool *cached;
3559
3560         /* see if we have a cached copy */
3561         cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
3562         if (cached) {
3563                 *am_rodc = *cached;
3564                 return LDB_SUCCESS;
3565         }
3566
3567         objectGUID = samdb_ntds_objectGUID(sam_ctx);
3568         if (!objectGUID) {
3569                 return ldb_operr(sam_ctx);
3570         }
3571
3572         ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
3573         if (ret != LDB_SUCCESS) {
3574                 return ret;
3575         }
3576
3577         cached = talloc(sam_ctx, bool);
3578         if (cached == NULL) {
3579                 return ldb_oom(sam_ctx);
3580         }
3581         *cached = *am_rodc;
3582
3583         ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
3584         if (ret != LDB_SUCCESS) {
3585                 talloc_free(cached);
3586                 return ldb_operr(sam_ctx);
3587         }
3588
3589         return LDB_SUCCESS;
3590 }
3591
3592 int samdb_dns_host_name(struct ldb_context *sam_ctx, const char **host_name)
3593 {
3594         const char *_host_name = NULL;
3595         const char *attrs[] = { "dnsHostName", NULL };
3596         TALLOC_CTX *tmp_ctx = NULL;
3597         int ret;
3598         struct ldb_result *res = NULL;
3599
3600         _host_name = (const char *)ldb_get_opaque(sam_ctx, "cache.dns_host_name");
3601         if (_host_name != NULL) {
3602                 *host_name = _host_name;
3603                 return LDB_SUCCESS;
3604         }
3605
3606         tmp_ctx = talloc_new(sam_ctx);
3607
3608         ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, NULL, attrs, 0);
3609
3610         if (res == NULL || res->count != 1 || ret != LDB_SUCCESS) {
3611                 DEBUG(0, ("Failed to get rootDSE for dnsHostName: %s",
3612                           ldb_errstring(sam_ctx)));
3613                 TALLOC_FREE(tmp_ctx);
3614                 return ret;
3615         }
3616
3617         _host_name = ldb_msg_find_attr_as_string(res->msgs[0],
3618                                                  "dnsHostName",
3619                                                  NULL);
3620         if (_host_name == NULL) {
3621                 DEBUG(0, ("Failed to get dnsHostName from rootDSE"));
3622                 TALLOC_FREE(tmp_ctx);
3623                 return LDB_ERR_OPERATIONS_ERROR;
3624         }
3625         ret = ldb_set_opaque(sam_ctx, "cache.dns_host_name",
3626                              discard_const_p(char *, _host_name));
3627         if (ret != LDB_SUCCESS) {
3628                 TALLOC_FREE(tmp_ctx);
3629                 return ldb_operr(sam_ctx);
3630         }
3631
3632         *host_name = talloc_steal(sam_ctx, _host_name);
3633
3634         TALLOC_FREE(tmp_ctx);
3635         return LDB_SUCCESS;
3636 }
3637
3638 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
3639 {
3640         TALLOC_CTX *tmp_ctx;
3641         bool *cached;
3642
3643         tmp_ctx = talloc_new(ldb);
3644         if (tmp_ctx == NULL) {
3645                 goto failed;
3646         }
3647
3648         cached = talloc(tmp_ctx, bool);
3649         if (!cached) {
3650                 goto failed;
3651         }
3652
3653         *cached = am_rodc;
3654         if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
3655                 goto failed;
3656         }
3657
3658         talloc_steal(ldb, cached);
3659         talloc_free(tmp_ctx);
3660         return true;
3661
3662 failed:
3663         DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3664         talloc_free(tmp_ctx);
3665         return false;
3666 }
3667
3668
3669 /*
3670  * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3671  * flags are DS_NTDSSETTINGS_OPT_*
3672  */
3673 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
3674                                         uint32_t *options)
3675 {
3676         int rc;
3677         TALLOC_CTX *tmp_ctx;
3678         struct ldb_result *res;
3679         struct ldb_dn *site_dn;
3680         const char *attrs[] = { "options", NULL };
3681
3682         tmp_ctx = talloc_new(ldb_ctx);
3683         if (tmp_ctx == NULL)
3684                 goto failed;
3685
3686         /* Retrieve the site dn for the ldb that we
3687          * have open.  This is our local site.
3688          */
3689         site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3690         if (site_dn == NULL)
3691                 goto failed;
3692
3693         /* Perform a one level (child) search from the local
3694          * site distinguided name.   We're looking for the
3695          * "options" attribute within the nTDSSiteSettings
3696          * object
3697          */
3698         rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3699                         LDB_SCOPE_ONELEVEL, attrs,
3700                         "objectClass=nTDSSiteSettings");
3701
3702         if (rc != LDB_SUCCESS || res->count != 1)
3703                 goto failed;
3704
3705         *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3706
3707         talloc_free(tmp_ctx);
3708
3709         return LDB_SUCCESS;
3710
3711 failed:
3712         DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3713         talloc_free(tmp_ctx);
3714         return ldb_error(ldb_ctx, LDB_ERR_NO_SUCH_OBJECT, __func__);
3715 }
3716
3717 /*
3718   return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1 
3719
3720   flags are DS_NTDS_OPTION_*
3721 */
3722 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3723 {
3724         TALLOC_CTX *tmp_ctx;
3725         const char *attrs[] = { "options", NULL };
3726         int ret;
3727         struct ldb_result *res;
3728
3729         tmp_ctx = talloc_new(ldb);
3730         if (tmp_ctx == NULL) {
3731                 goto failed;
3732         }
3733
3734         ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3735         if (ret != LDB_SUCCESS) {
3736                 goto failed;
3737         }
3738
3739         if (res->count != 1) {
3740                 goto failed;
3741         }
3742
3743         *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3744
3745         talloc_free(tmp_ctx);
3746
3747         return LDB_SUCCESS;
3748
3749 failed:
3750         DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3751         talloc_free(tmp_ctx);
3752         return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3753 }
3754
3755 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3756 {
3757         const char *attrs[] = { "objectCategory", NULL };
3758         int ret;
3759         struct ldb_result *res;
3760
3761         ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3762         if (ret != LDB_SUCCESS) {
3763                 goto failed;
3764         }
3765
3766         if (res->count != 1) {
3767                 goto failed;
3768         }
3769
3770         return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3771
3772 failed:
3773         DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3774         return NULL;
3775 }
3776
3777 /*
3778  * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3779  * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3780  */
3781 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3782 {
3783         char **tokens, *ret;
3784         size_t i;
3785
3786         tokens = str_list_make(mem_ctx, cn, " -_");
3787         if (tokens == NULL || tokens[0] == NULL) {
3788                 return NULL;
3789         }
3790
3791         /* "tolower()" and "toupper()" should also work properly on 0x00 */
3792         tokens[0][0] = tolower(tokens[0][0]);
3793         for (i = 1; tokens[i] != NULL; i++)
3794                 tokens[i][0] = toupper(tokens[i][0]);
3795
3796         ret = talloc_strdup(mem_ctx, tokens[0]);
3797         for (i = 1; tokens[i] != NULL; i++)
3798                 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3799
3800         talloc_free(tokens);
3801
3802         return ret;
3803 }
3804
3805 /*
3806  * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3807  */
3808 int dsdb_functional_level(struct ldb_context *ldb)
3809 {
3810         int *domainFunctionality =
3811                 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3812         if (!domainFunctionality) {
3813                 /* this is expected during initial provision */
3814                 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3815                 return DS_DOMAIN_FUNCTION_2000;
3816         }
3817         return *domainFunctionality;
3818 }
3819
3820 /*
3821  * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3822  */
3823 int dsdb_forest_functional_level(struct ldb_context *ldb)
3824 {
3825         int *forestFunctionality =
3826                 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3827         if (!forestFunctionality) {
3828                 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3829                 return DS_DOMAIN_FUNCTION_2000;
3830         }
3831         return *forestFunctionality;
3832 }
3833
3834 /*
3835   set a GUID in an extended DN structure
3836  */
3837 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3838 {
3839         struct ldb_val v;
3840         NTSTATUS status;
3841         int ret;
3842
3843         status = GUID_to_ndr_blob(guid, dn, &v);
3844         if (!NT_STATUS_IS_OK(status)) {
3845                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3846         }
3847
3848         ret = ldb_dn_set_extended_component(dn, component_name, &v);
3849         data_blob_free(&v);
3850         return ret;
3851 }
3852
3853 /*
3854   return a GUID from a extended DN structure
3855  */
3856 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3857 {
3858         const struct ldb_val *v;
3859
3860         v = ldb_dn_get_extended_component(dn, component_name);
3861         if (v == NULL) {
3862                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3863         }
3864
3865         return GUID_from_ndr_blob(v, guid);
3866 }
3867
3868 /*
3869   return a uint64_t from a extended DN structure
3870  */
3871 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3872 {
3873         const struct ldb_val *v;
3874         int error = 0;
3875
3876         v = ldb_dn_get_extended_component(dn, component_name);
3877         if (v == NULL) {
3878                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3879         }
3880
3881         /* Just check we don't allow the caller to fill our stack */
3882         if (v->length >= 64) {
3883                 return NT_STATUS_INVALID_PARAMETER;
3884         } else {
3885                 char s[v->length+1];
3886                 memcpy(s, v->data, v->length);
3887                 s[v->length] = 0;
3888
3889                 *val = smb_strtoull(s, NULL, 0, &error, SMB_STR_STANDARD);
3890                 if (error != 0) {
3891                         return NT_STATUS_INVALID_PARAMETER;
3892                 }
3893         }
3894         return NT_STATUS_OK;
3895 }
3896
3897 /*
3898   return a NTTIME from a extended DN structure
3899  */
3900 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3901 {
3902         return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3903 }
3904
3905 /*
3906   return a uint32_t from a extended DN structure
3907  */
3908 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3909 {
3910         const struct ldb_val *v;
3911         int error = 0;
3912
3913         v = ldb_dn_get_extended_component(dn, component_name);
3914         if (v == NULL) {
3915                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3916         }
3917
3918         /* Just check we don't allow the caller to fill our stack */
3919         if (v->length >= 32) {
3920                 return NT_STATUS_INVALID_PARAMETER;
3921         } else {
3922                 char s[v->length + 1];
3923                 memcpy(s, v->data, v->length);
3924                 s[v->length] = 0;
3925                 *val = smb_strtoul(s, NULL, 0, &error, SMB_STR_STANDARD);
3926                 if (error != 0) {
3927                         return NT_STATUS_INVALID_PARAMETER;
3928                 }
3929         }
3930
3931         return NT_STATUS_OK;
3932 }
3933
3934 /*
3935   return a dom_sid from a extended DN structure
3936  */
3937 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3938 {
3939         const struct ldb_val *sid_blob;
3940         enum ndr_err_code ndr_err;
3941
3942         sid_blob = ldb_dn_get_extended_component(dn, component_name);
3943         if (!sid_blob) {
3944                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3945         }
3946
3947         ndr_err = ndr_pull_struct_blob_all_noalloc(sid_blob, sid,
3948                                                    (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3949         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3950                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3951                 return status;
3952         }
3953
3954         return NT_STATUS_OK;
3955 }
3956
3957
3958 /*
3959   return RMD_FLAGS directly from a ldb_dn
3960   returns 0 if not found
3961  */
3962 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3963 {
3964         uint32_t rmd_flags = 0;
3965         NTSTATUS status = dsdb_get_extended_dn_uint32(dn, &rmd_flags,
3966                                                       "RMD_FLAGS");
3967         if (NT_STATUS_IS_OK(status)) {
3968                 return rmd_flags;
3969         }
3970         return 0;
3971 }
3972
3973 /*
3974   return RMD_FLAGS directly from a ldb_val for a DN
3975   returns 0 if RMD_FLAGS is not found
3976  */
3977 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3978 {
3979         const char *p;
3980         uint32_t flags;
3981         char *end;
3982         int error = 0;
3983
3984         if (val->length < 13) {
3985                 return 0;
3986         }
3987         p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3988         if (!p) {
3989                 return 0;
3990         }
3991         flags = smb_strtoul(p+11, &end, 10, &error, SMB_STR_STANDARD);
3992         if (!end || *end != '>' || error != 0) {
3993                 /* it must end in a > */
3994                 return 0;
3995         }
3996         return flags;
3997 }
3998
3999 /*
4000   return true if a ldb_val containing a DN in storage form is deleted
4001  */
4002 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
4003 {
4004         return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
4005 }
4006
4007 /*
4008   return true if a ldb_val containing a DN in storage form is
4009   in the upgraded w2k3 linked attribute format
4010  */
4011 bool dsdb_dn_is_upgraded_link_val(const struct ldb_val *val)
4012 {
4013         return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
4014 }
4015
4016 /*
4017   return a DN for a wellknown GUID
4018  */
4019 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
4020                       struct ldb_dn *nc_root, const char *wk_guid,
4021                       struct ldb_dn **wkguid_dn)
4022 {
4023         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4024         const char *attrs[] = { NULL };
4025         int ret;
4026         struct ldb_dn *dn;
4027         struct ldb_result *res = NULL;
4028
4029         /* construct the magic WKGUID DN */
4030         dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
4031                             wk_guid, ldb_dn_get_linearized(nc_root));
4032         if (!wkguid_dn) {
4033                 talloc_free(tmp_ctx);
4034                 return ldb_operr(samdb);
4035         }
4036
4037         ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
4038                              DSDB_SEARCH_SHOW_DELETED |
4039                              DSDB_SEARCH_SHOW_RECYCLED);
4040         if (ret != LDB_SUCCESS) {
4041                 talloc_free(tmp_ctx);
4042                 return ret;
4043         }
4044         /* fix clang warning */
4045         if (res == NULL){
4046                 talloc_free(tmp_ctx);
4047                 return LDB_ERR_OTHER;
4048         }
4049
4050         (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
4051         talloc_free(tmp_ctx);
4052         return LDB_SUCCESS;
4053 }
4054
4055
4056 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
4057 {
4058         return ldb_dn_compare(*dn1, *dn2);
4059 }
4060
4061 /*
4062   find a NC root given a DN within the NC
4063  */
4064 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
4065                       struct ldb_dn **nc_root)
4066 {
4067         const char *root_attrs[] = { "namingContexts", NULL };
4068         TALLOC_CTX *tmp_ctx;
4069         int ret;
4070         struct ldb_message_element *el;
4071         struct ldb_result *root_res;
4072         unsigned int i;
4073         struct ldb_dn **nc_dns;
4074
4075         tmp_ctx = talloc_new(samdb);
4076         if (tmp_ctx == NULL) {
4077                 return ldb_oom(samdb);
4078         }
4079
4080         ret = ldb_search(samdb, tmp_ctx, &root_res,
4081                          ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
4082         if (ret != LDB_SUCCESS || root_res->count == 0) {
4083                 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
4084                 talloc_free(tmp_ctx);
4085                 return ret;
4086         }
4087
4088         el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
4089         if ((el == NULL) || (el->num_values < 3)) {
4090                 struct ldb_message *tmp_msg;
4091
4092                 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list.\n"));
4093
4094                 /* This generates a temporary list of NCs in order to let the
4095                  * provisioning work. */
4096                 tmp_msg = ldb_msg_new(tmp_ctx);
4097                 if (tmp_msg == NULL) {
4098                         talloc_free(tmp_ctx);
4099                         return ldb_oom(samdb);
4100                 }
4101                 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4102                                                ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
4103                 if (ret != LDB_SUCCESS) {
4104                         talloc_free(tmp_ctx);
4105                         return ret;
4106                 }
4107                 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4108                                                ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
4109                 if (ret != LDB_SUCCESS) {
4110                         talloc_free(tmp_ctx);
4111                         return ret;
4112                 }
4113                 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4114                                                ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
4115                 if (ret != LDB_SUCCESS) {
4116                         talloc_free(tmp_ctx);
4117                         return ret;
4118                 }
4119                 el = &tmp_msg->elements[0];
4120         }
4121
4122        nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
4123        if (!nc_dns) {
4124                talloc_free(tmp_ctx);
4125                return ldb_oom(samdb);
4126        }
4127
4128        for (i=0; i<el->num_values; i++) {
4129                nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
4130                if (nc_dns[i] == NULL) {
4131                        talloc_free(tmp_ctx);
4132                        return ldb_operr(samdb);
4133                }
4134        }
4135
4136        TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
4137
4138        for (i=0; i<el->num_values; i++) {
4139                if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
4140                        (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
4141                        talloc_free(tmp_ctx);
4142                        return LDB_SUCCESS;
4143                }
4144        }
4145
4146        talloc_free(tmp_ctx);
4147        return ldb_error(samdb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4148 }
4149
4150
4151 /*
4152   find the deleted objects DN for any object, by looking for the NC
4153   root, then looking up the wellknown GUID
4154  */
4155 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
4156                                 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
4157                                 struct ldb_dn **do_dn)
4158 {
4159         struct ldb_dn *nc_root;
4160         int ret;
4161
4162         ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
4163         if (ret != LDB_SUCCESS) {
4164                 return ret;
4165         }
4166
4167         ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
4168         talloc_free(nc_root);
4169         return ret;
4170 }
4171
4172 /*
4173   return the tombstoneLifetime, in days
4174  */
4175 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
4176 {
4177         struct ldb_dn *dn;
4178         dn = ldb_get_config_basedn(ldb);
4179         if (!dn) {
4180                 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4181         }
4182         dn = ldb_dn_copy(ldb, dn);
4183         if (!dn) {
4184                 return ldb_operr(ldb);
4185         }
4186         /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
4187          be a wellknown GUID for this */
4188         if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
4189                 talloc_free(dn);
4190                 return ldb_operr(ldb);
4191         }
4192
4193         *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
4194         talloc_free(dn);
4195         return LDB_SUCCESS;
4196 }
4197
4198 /*
4199   compare a ldb_val to a string case insensitively
4200  */
4201 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
4202 {
4203         size_t len = strlen(s);
4204         int ret;
4205         if (len > v->length) return 1;
4206         ret = strncasecmp(s, (const char *)v->data, v->length);
4207         if (ret != 0) return ret;
4208         if (v->length > len && v->data[len] != 0) {
4209                 return -1;
4210         }
4211         return 0;
4212 }
4213
4214
4215 /*
4216   load the UDV for a partition in v2 format
4217   The list is returned sorted, and with our local cursor added
4218  */
4219 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4220                      struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
4221 {
4222         static const char *attrs[] = { "replUpToDateVector", NULL };
4223         struct ldb_result *r = NULL;
4224         const struct ldb_val *ouv_value;
4225         unsigned int i;
4226         int ret;
4227         uint64_t highest_usn = 0;
4228         const struct GUID *our_invocation_id;
4229         static const struct timeval tv1970;
4230         NTTIME nt1970 = timeval_to_nttime(&tv1970);
4231
4232         ret = dsdb_search_dn(samdb, mem_ctx, &r, dn, attrs, DSDB_SEARCH_SHOW_RECYCLED|DSDB_SEARCH_SHOW_DELETED);
4233         if (ret != LDB_SUCCESS) {
4234                 return ret;
4235         }
4236         /* fix clang warning */
4237         if (r == NULL) {
4238                 return LDB_ERR_OTHER;
4239         }
4240         ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
4241         if (ouv_value) {
4242                 enum ndr_err_code ndr_err;
4243                 struct replUpToDateVectorBlob ouv;
4244
4245                 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
4246                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
4247                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4248                         talloc_free(r);
4249                         return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4250                 }
4251                 if (ouv.version != 2) {
4252                         /* we always store as version 2, and
4253                          * replUpToDateVector is not replicated
4254                          */
4255                         return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4256                 }
4257
4258                 *count = ouv.ctr.ctr2.count;
4259                 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
4260         } else {
4261                 *count = 0;
4262                 *cursors = NULL;
4263         }
4264
4265         talloc_free(r);
4266
4267         our_invocation_id = samdb_ntds_invocation_id(samdb);
4268         if (!our_invocation_id) {
4269                 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
4270                 talloc_free(*cursors);
4271                 return ldb_operr(samdb);
4272         }
4273
4274         ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
4275         if (ret != LDB_SUCCESS) {
4276                 /* nothing to add - this can happen after a vampire */
4277                 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4278                 return LDB_SUCCESS;
4279         }
4280
4281         for (i=0; i<*count; i++) {
4282                 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
4283                         (*cursors)[i].highest_usn = highest_usn;
4284                         (*cursors)[i].last_sync_success = nt1970;
4285                         TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4286                         return LDB_SUCCESS;
4287                 }
4288         }
4289
4290         (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
4291         if (! *cursors) {
4292                 return ldb_oom(samdb);
4293         }
4294
4295         (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
4296         (*cursors)[*count].highest_usn = highest_usn;
4297         (*cursors)[*count].last_sync_success = nt1970;
4298         (*count)++;
4299
4300         TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4301
4302         return LDB_SUCCESS;
4303 }
4304
4305 /*
4306   load the UDV for a partition in version 1 format
4307   The list is returned sorted, and with our local cursor added
4308  */
4309 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4310                      struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
4311 {
4312         struct drsuapi_DsReplicaCursor2 *v2 = NULL;
4313         uint32_t i;
4314         int ret;
4315
4316         ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
4317         if (ret != LDB_SUCCESS) {
4318                 return ret;
4319         }
4320
4321         if (*count == 0) {
4322                 talloc_free(v2);
4323                 *cursors = NULL;
4324                 return LDB_SUCCESS;
4325         }
4326
4327         *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
4328         if (*cursors == NULL) {
4329                 talloc_free(v2);
4330                 return ldb_oom(samdb);
4331         }
4332
4333         for (i=0; i<*count; i++) {
4334                 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
4335                 (*cursors)[i].highest_usn = v2[i].highest_usn;
4336         }
4337         talloc_free(v2);
4338         return LDB_SUCCESS;
4339 }
4340
4341 /*
4342   add a set of controls to a ldb_request structure based on a set of
4343   flags. See util.h for a list of available flags
4344  */
4345 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
4346 {
4347         int ret;
4348         if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
4349                 struct ldb_search_options_control *options;
4350                 /* Using the phantom root control allows us to search all partitions */
4351                 options = talloc(req, struct ldb_search_options_control);
4352                 if (options == NULL) {
4353                         return LDB_ERR_OPERATIONS_ERROR;
4354                 }
4355                 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
4356
4357                 ret = ldb_request_add_control(req,
4358                                               LDB_CONTROL_SEARCH_OPTIONS_OID,
4359                                               true, options);
4360                 if (ret != LDB_SUCCESS) {
4361                         return ret;
4362                 }
4363         }
4364
4365         if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
4366                 ret = ldb_request_add_control(req,
4367                                               DSDB_CONTROL_NO_GLOBAL_CATALOG,
4368                                               false, NULL);
4369                 if (ret != LDB_SUCCESS) {
4370                         return ret;
4371                 }
4372         }
4373
4374         if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
4375                 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
4376                 if (ret != LDB_SUCCESS) {
4377                         return ret;
4378                 }
4379         }
4380
4381         if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
4382                 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
4383                 if (ret != LDB_SUCCESS) {
4384                         return ret;
4385                 }
4386         }
4387
4388         if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
4389                 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
4390                 if (ret != LDB_SUCCESS) {
4391                         return ret;
4392                 }
4393         }
4394
4395         if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
4396                 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
4397                 if (!extended_ctrl) {
4398                         return LDB_ERR_OPERATIONS_ERROR;
4399                 }
4400                 extended_ctrl->type = 1;
4401
4402                 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
4403                 if (ret != LDB_SUCCESS) {
4404                         return ret;
4405                 }
4406         }
4407
4408         if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
4409                 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
4410                 if (ret != LDB_SUCCESS) {
4411                         return ret;
4412                 }
4413         }
4414
4415         if (dsdb_flags & DSDB_MODIFY_RELAX) {
4416                 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
4417                 if (ret != LDB_SUCCESS) {
4418                         return ret;
4419                 }
4420         }
4421
4422         if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
4423                 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
4424                 if (ret != LDB_SUCCESS) {
4425                         return ret;
4426                 }
4427         }
4428
4429         if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
4430                 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
4431                 if (ret != LDB_SUCCESS) {
4432                         return ret;
4433                 }
4434         }
4435
4436         if (dsdb_flags & DSDB_TREE_DELETE) {
4437                 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
4438                 if (ret != LDB_SUCCESS) {
4439                         return ret;
4440                 }
4441         }
4442
4443         if (dsdb_flags & DSDB_PROVISION) {
4444                 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
4445                 if (ret != LDB_SUCCESS) {
4446                         return ret;
4447                 }
4448         }
4449
4450         /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4451         if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
4452                 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
4453                 if (ret != LDB_SUCCESS) {
4454                         return ret;
4455                 }
4456         }
4457
4458         if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
4459                 /* 
4460                  * This must not be critical, as it will only be
4461                  * handled (and need to be handled) if the other
4462                  * attributes in the request bring password_hash into
4463                  * action
4464                  */
4465                 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
4466                 if (ret != LDB_SUCCESS) {
4467                         return ret;
4468                 }
4469         }
4470
4471         if (dsdb_flags & DSDB_REPLMD_VANISH_LINKS) {
4472                 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLMD_VANISH_LINKS, true, NULL);
4473                 if (ret != LDB_SUCCESS) {
4474                         return ret;
4475                 }
4476         }
4477
4478         if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
4479                 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
4480                 if (ret != LDB_SUCCESS) {
4481                         return ret;
4482                 }
4483         }
4484
4485         if (dsdb_flags & DSDB_FLAG_REPLICATED_UPDATE) {
4486                 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID, false, NULL);
4487                 if (ret != LDB_SUCCESS) {
4488                         return ret;
4489                 }
4490         }
4491
4492         return LDB_SUCCESS;
4493 }
4494
4495 /*
4496    returns true if a control with the specified "oid" exists
4497 */
4498 bool dsdb_request_has_control(struct ldb_request *req, const char *oid)
4499 {
4500         return (ldb_request_get_control(req, oid) != NULL);
4501 }
4502
4503 /*
4504   an add with a set of controls
4505 */
4506 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
4507              uint32_t dsdb_flags)
4508 {
4509         struct ldb_request *req;
4510         int ret;
4511
4512         ret = ldb_build_add_req(&req, ldb, ldb,
4513                                 message,
4514                                 NULL,
4515                                 NULL,
4516                                 ldb_op_default_callback,
4517                                 NULL);
4518
4519         if (ret != LDB_SUCCESS) return ret;
4520
4521         ret = dsdb_request_add_controls(req, dsdb_flags);
4522         if (ret != LDB_SUCCESS) {
4523                 talloc_free(req);
4524                 return ret;
4525         }
4526
4527         ret = dsdb_autotransaction_request(ldb, req);
4528
4529         talloc_free(req);
4530         return ret;
4531 }
4532
4533 /*
4534   a modify with a set of controls
4535 */
4536 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
4537                 uint32_t dsdb_flags)
4538 {
4539         struct ldb_request *req;
4540         int ret;
4541
4542         ret = ldb_build_mod_req(&req, ldb, ldb,
4543                                 message,
4544                                 NULL,
4545                                 NULL,
4546                                 ldb_op_default_callback,
4547                                 NULL);
4548
4549         if (ret != LDB_SUCCESS) return ret;
4550
4551         ret = dsdb_request_add_controls(req, dsdb_flags);
4552         if (ret != LDB_SUCCESS) {
4553                 talloc_free(req);
4554                 return ret;
4555         }
4556
4557         ret = dsdb_autotransaction_request(ldb, req);
4558
4559         talloc_free(req);
4560         return ret;
4561 }
4562
4563 /*
4564   a delete with a set of flags
4565 */
4566 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
4567                 uint32_t dsdb_flags)
4568 {
4569         struct ldb_request *req;
4570         int ret;
4571
4572         ret = ldb_build_del_req(&req, ldb, ldb,
4573                                 dn,
4574                                 NULL,
4575                                 NULL,
4576                                 ldb_op_default_callback,
4577                                 NULL);
4578
4579         if (ret != LDB_SUCCESS) return ret;
4580
4581         ret = dsdb_request_add_controls(req, dsdb_flags);
4582         if (ret != LDB_SUCCESS) {
4583                 talloc_free(req);
4584                 return ret;
4585         }
4586
4587         ret = dsdb_autotransaction_request(ldb, req);
4588
4589         talloc_free(req);
4590         return ret;
4591 }
4592
4593 /*
4594   like dsdb_modify() but set all the element flags to
4595   LDB_FLAG_MOD_REPLACE
4596  */
4597 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
4598 {
4599         unsigned int i;
4600
4601         /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4602         for (i=0;i<msg->num_elements;i++) {
4603                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
4604         }
4605
4606         return dsdb_modify(ldb, msg, dsdb_flags);
4607 }
4608
4609
4610 /*
4611   search for attrs on one DN, allowing for dsdb_flags controls
4612  */
4613 int dsdb_search_dn(struct ldb_context *ldb,
4614                    TALLOC_CTX *mem_ctx,
4615                    struct ldb_result **_result,
4616                    struct ldb_dn *basedn,
4617                    const char * const *attrs,
4618                    uint32_t dsdb_flags)
4619 {
4620         int ret;
4621         struct ldb_request *req;
4622         struct ldb_result *res;
4623
4624         res = talloc_zero(mem_ctx, struct ldb_result);
4625         if (!res) {
4626                 return ldb_oom(ldb);
4627         }
4628
4629         ret = ldb_build_search_req(&req, ldb, res,
4630                                    basedn,
4631                                    LDB_SCOPE_BASE,
4632                                    NULL,
4633                                    attrs,
4634                                    NULL,
4635                                    res,
4636                                    ldb_search_default_callback,
4637                                    NULL);
4638         if (ret != LDB_SUCCESS) {
4639                 talloc_free(res);
4640                 return ret;
4641         }
4642
4643         ret = dsdb_request_add_controls(req, dsdb_flags);
4644         if (ret != LDB_SUCCESS) {
4645                 talloc_free(res);
4646                 return ret;
4647         }
4648
4649         ret = ldb_request(ldb, req);
4650         if (ret == LDB_SUCCESS) {
4651                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4652         }
4653
4654         talloc_free(req);
4655         if (ret != LDB_SUCCESS) {
4656                 talloc_free(res);
4657                 return ret;
4658         }
4659
4660         *_result = res;
4661         return LDB_SUCCESS;
4662 }
4663
4664 /*
4665   search for attrs on one DN, by the GUID of the DN, allowing for
4666   dsdb_flags controls
4667  */
4668 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
4669                            TALLOC_CTX *mem_ctx,
4670                            struct ldb_result **_result,
4671                            const struct GUID *guid,
4672                            const char * const *attrs,
4673                            uint32_t dsdb_flags)
4674 {
4675         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4676         struct ldb_dn *dn;
4677         int ret;
4678
4679         dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
4680         if (dn == NULL) {
4681                 talloc_free(tmp_ctx);
4682                 return ldb_oom(ldb);
4683         }
4684
4685         ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
4686         talloc_free(tmp_ctx);
4687         return ret;
4688 }
4689
4690 /*
4691   general search with dsdb_flags for controls
4692  */
4693 int dsdb_search(struct ldb_context *ldb,
4694                 TALLOC_CTX *mem_ctx,
4695                 struct ldb_result **_result,
4696                 struct ldb_dn *basedn,
4697                 enum ldb_scope scope,
4698                 const char * const *attrs,
4699                 uint32_t dsdb_flags,
4700                 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4701 {
4702         int ret;
4703         struct ldb_request *req;
4704         struct ldb_result *res;
4705         va_list ap;
4706         char *expression = NULL;
4707         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4708
4709         /* cross-partitions searches with a basedn break multi-domain support */
4710         SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
4711
4712         res = talloc_zero(tmp_ctx, struct ldb_result);
4713         if (!res) {
4714                 talloc_free(tmp_ctx);
4715                 return ldb_oom(ldb);
4716         }
4717
4718         if (exp_fmt) {
4719                 va_start(ap, exp_fmt);
4720                 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4721                 va_end(ap);
4722
4723                 if (!expression) {
4724                         talloc_free(tmp_ctx);
4725                         return ldb_oom(ldb);
4726                 }
4727         }
4728
4729         ret = ldb_build_search_req(&req, ldb, tmp_ctx,
4730                                    basedn,
4731                                    scope,
4732                                    expression,
4733                                    attrs,
4734                                    NULL,
4735                                    res,
4736                                    ldb_search_default_callback,
4737                                    NULL);
4738         if (ret != LDB_SUCCESS) {
4739                 talloc_free(tmp_ctx);
4740                 return ret;
4741         }
4742
4743         ret = dsdb_request_add_controls(req, dsdb_flags);
4744         if (ret != LDB_SUCCESS) {
4745                 talloc_free(tmp_ctx);
4746                 ldb_reset_err_string(ldb);
4747                 return ret;
4748         }
4749
4750         ret = ldb_request(ldb, req);
4751         if (ret == LDB_SUCCESS) {
4752                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4753         }
4754
4755         if (ret != LDB_SUCCESS) {
4756                 talloc_free(tmp_ctx);
4757                 return ret;
4758         }
4759
4760         if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4761                 if (res->count == 0) {
4762                         talloc_free(tmp_ctx);
4763                         ldb_reset_err_string(ldb);
4764                         return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4765                 }
4766                 if (res->count != 1) {
4767                         talloc_free(tmp_ctx);
4768                         ldb_reset_err_string(ldb);
4769                         return LDB_ERR_CONSTRAINT_VIOLATION;
4770                 }
4771         }
4772
4773         *_result = talloc_steal(mem_ctx, res);
4774         talloc_free(tmp_ctx);
4775
4776         return LDB_SUCCESS;
4777 }
4778
4779
4780 /*
4781   general search with dsdb_flags for controls
4782   returns exactly 1 record or an error
4783  */
4784 int dsdb_search_one(struct ldb_context *ldb,
4785                     TALLOC_CTX *mem_ctx,
4786                     struct ldb_message **msg,
4787                     struct ldb_dn *basedn,
4788                     enum ldb_scope scope,
4789                     const char * const *attrs,
4790                     uint32_t dsdb_flags,
4791                     const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4792 {
4793         int ret;
4794         struct ldb_result *res;
4795         va_list ap;
4796         char *expression = NULL;
4797         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4798
4799         dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4800
4801         res = talloc_zero(tmp_ctx, struct ldb_result);
4802         if (!res) {
4803                 talloc_free(tmp_ctx);
4804                 return ldb_oom(ldb);
4805         }
4806
4807         if (exp_fmt) {
4808                 va_start(ap, exp_fmt);
4809                 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4810                 va_end(ap);
4811
4812                 if (!expression) {
4813                         talloc_free(tmp_ctx);
4814                         return ldb_oom(ldb);
4815                 }
4816                 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4817                                   dsdb_flags, "%s", expression);
4818         } else {
4819                 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4820                                   dsdb_flags, NULL);
4821         }
4822
4823         if (ret != LDB_SUCCESS) {
4824                 talloc_free(tmp_ctx);
4825                 return ret;
4826         }
4827
4828         *msg = talloc_steal(mem_ctx, res->msgs[0]);
4829         talloc_free(tmp_ctx);
4830
4831         return LDB_SUCCESS;
4832 }
4833
4834 /* returns back the forest DNS name */
4835 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4836 {
4837         const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4838                                                           ldb_get_root_basedn(ldb));
4839         char *p;
4840
4841         if (forest_name == NULL) {
4842                 return NULL;
4843         }
4844
4845         p = strchr(forest_name, '/');
4846         if (p) {
4847                 *p = '\0';
4848         }
4849
4850         return forest_name;
4851 }
4852
4853 /* returns back the default domain DNS name */
4854 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4855 {
4856         const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4857                                                           ldb_get_default_basedn(ldb));
4858         char *p;
4859
4860         if (domain_name == NULL) {
4861                 return NULL;
4862         }
4863
4864         p = strchr(domain_name, '/');
4865         if (p) {
4866                 *p = '\0';
4867         }
4868
4869         return domain_name;
4870 }
4871
4872 /*
4873    validate that an DSA GUID belongs to the specified user sid.
4874    The user SID must be a domain controller account (either RODC or
4875    RWDC)
4876  */
4877 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4878                            const struct GUID *dsa_guid,
4879                            const struct dom_sid *sid)
4880 {
4881         /* strategy:
4882             - find DN of record with the DSA GUID in the
4883               configuration partition (objectGUID)
4884             - remove "NTDS Settings" component from DN
4885             - do a base search on that DN for serverReference with
4886               extended-dn enabled
4887             - extract objectSid from resulting serverReference
4888               attribute
4889             - check this sid matches the sid argument
4890         */
4891         struct ldb_dn *config_dn;
4892         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4893         struct ldb_message *msg;
4894         const char *attrs1[] = { NULL };
4895         const char *attrs2[] = { "serverReference", NULL };
4896         int ret;
4897         struct ldb_dn *dn, *account_dn;
4898         struct dom_sid sid2;
4899         NTSTATUS status;
4900
4901         config_dn = ldb_get_config_basedn(ldb);
4902
4903         ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4904                               attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4905         if (ret != LDB_SUCCESS) {
4906                 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4907                          GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4908                 talloc_free(tmp_ctx);
4909                 return ldb_operr(ldb);
4910         }
4911         dn = msg->dn;
4912
4913         if (!ldb_dn_remove_child_components(dn, 1)) {
4914                 talloc_free(tmp_ctx);
4915                 return ldb_operr(ldb);
4916         }
4917
4918         ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4919                               attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4920                               "(objectClass=server)");
4921         if (ret != LDB_SUCCESS) {
4922                 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4923                          GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4924                 talloc_free(tmp_ctx);
4925                 return ldb_operr(ldb);
4926         }
4927
4928         account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4929         if (account_dn == NULL) {
4930                 DEBUG(1,(__location__ ": Failed to find account dn "
4931                          "(serverReference) for %s, parent of DSA with "
4932                          "objectGUID %s, sid %s\n",
4933                          ldb_dn_get_linearized(msg->dn),
4934                          GUID_string(tmp_ctx, dsa_guid),
4935                          dom_sid_string(tmp_ctx, sid)));
4936                 talloc_free(tmp_ctx);
4937                 return ldb_operr(ldb);
4938         }
4939
4940         status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4941         if (!NT_STATUS_IS_OK(status)) {
4942                 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4943                          GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4944                 talloc_free(tmp_ctx);
4945                 return ldb_operr(ldb);
4946         }
4947
4948         if (!dom_sid_equal(sid, &sid2)) {
4949                 /* someone is trying to spoof another account */
4950                 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4951                          GUID_string(tmp_ctx, dsa_guid),
4952                          dom_sid_string(tmp_ctx, sid),
4953                          dom_sid_string(tmp_ctx, &sid2)));
4954                 talloc_free(tmp_ctx);
4955                 return ldb_operr(ldb);
4956         }
4957
4958         talloc_free(tmp_ctx);
4959         return LDB_SUCCESS;
4960 }
4961
4962 static const char * const secret_attributes[] = {
4963         DSDB_SECRET_ATTRIBUTES,
4964         NULL
4965 };
4966
4967 /*
4968   check if the attribute belongs to the RODC filtered attribute set
4969   Note that attributes that are in the filtered attribute set are the
4970   ones that _are_ always sent to a RODC
4971 */
4972 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4973 {
4974         /* they never get secret attributes */
4975         if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4976                 return false;
4977         }
4978
4979         /* they do get non-secret critical attributes */
4980         if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4981                 return true;
4982         }
4983
4984         /* they do get non-secret attributes marked as being in the FAS  */
4985         if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4986                 return true;
4987         }
4988
4989         /* other attributes are denied */
4990         return false;
4991 }
4992
4993 /* return fsmo role dn and role owner dn for a particular role*/
4994 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4995                                struct ldb_context *ldb,
4996                                uint32_t role,
4997                                struct ldb_dn **fsmo_role_dn,
4998                                struct ldb_dn **role_owner_dn)
4999 {
5000         int ret;
5001         switch (role) {
5002         case DREPL_NAMING_MASTER:
5003                 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
5004                 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
5005                 if (ret != LDB_SUCCESS) {
5006                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
5007                                  ldb_errstring(ldb)));
5008                         talloc_free(tmp_ctx);
5009                         return WERR_DS_DRA_INTERNAL_ERROR;
5010                 }
5011                 break;
5012         case DREPL_INFRASTRUCTURE_MASTER:
5013                 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
5014                 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
5015                 if (ret != LDB_SUCCESS) {
5016                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
5017                                  ldb_errstring(ldb)));
5018                         talloc_free(tmp_ctx);
5019                         return WERR_DS_DRA_INTERNAL_ERROR;
5020                 }
5021                 break;
5022         case DREPL_RID_MASTER:
5023                 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
5024                 if (ret != LDB_SUCCESS) {
5025                         DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
5026                         talloc_free(tmp_ctx);
5027                         return WERR_DS_DRA_INTERNAL_ERROR;
5028                 }
5029
5030                 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
5031                 if (ret != LDB_SUCCESS) {
5032                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
5033                                  ldb_errstring(ldb)));
5034                         talloc_free(tmp_ctx);
5035                         return WERR_DS_DRA_INTERNAL_ERROR;
5036                 }
5037                 break;
5038         case DREPL_SCHEMA_MASTER:
5039                 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
5040                 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
5041                 if (ret != LDB_SUCCESS) {
5042                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
5043                                  ldb_errstring(ldb)));
5044                         talloc_free(tmp_ctx);
5045                         return WERR_DS_DRA_INTERNAL_ERROR;
5046                 }
5047                 break;
5048         case DREPL_PDC_MASTER:
5049                 *fsmo_role_dn = ldb_get_default_basedn(ldb);
5050                 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
5051                 if (ret != LDB_SUCCESS) {
5052                         DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
5053                                  ldb_errstring(ldb)));
5054                         talloc_free(tmp_ctx);
5055                         return WERR_DS_DRA_INTERNAL_ERROR;
5056                 }
5057                 break;
5058         default:
5059                 return WERR_DS_DRA_INTERNAL_ERROR;
5060         }
5061         return WERR_OK;
5062 }
5063
5064 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
5065                                     TALLOC_CTX *mem_ctx,
5066                                     struct ldb_dn *server_dn)
5067 {
5068         int ldb_ret;
5069         struct ldb_result *res = NULL;
5070         const char * const attrs[] = { "dNSHostName", NULL};
5071
5072         ldb_ret = ldb_search(ldb, mem_ctx, &res,
5073                              server_dn,
5074                              LDB_SCOPE_BASE,
5075                              attrs, NULL);
5076         if (ldb_ret != LDB_SUCCESS) {
5077                 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
5078                           ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
5079                 return NULL;
5080         }
5081
5082         return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
5083 }
5084
5085 /*
5086   returns true if an attribute is in the filter,
5087   false otherwise, provided that attribute value is provided with the expression
5088 */
5089 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
5090                              const char *attr)
5091 {
5092        unsigned int i;
5093        switch (tree->operation) {
5094        case LDB_OP_AND:
5095        case LDB_OP_OR:
5096                for (i=0;i<tree->u.list.num_elements;i++) {
5097                        if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
5098                                                        attr))
5099                                return true;
5100                }
5101                return false;
5102        case LDB_OP_NOT:
5103                return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
5104        case LDB_OP_EQUALITY:
5105        case LDB_OP_GREATER:
5106        case LDB_OP_LESS:
5107        case LDB_OP_APPROX:
5108                if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
5109                        return true;
5110                }
5111                return false;
5112        case LDB_OP_SUBSTRING:
5113                if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
5114                        return true;
5115                }
5116                return false;
5117        case LDB_OP_PRESENT:
5118                /* (attrname=*) is not filtered out */
5119                return false;
5120        case LDB_OP_EXTENDED:
5121                if (tree->u.extended.attr &&
5122                    ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
5123                        return true;
5124                }
5125                return false;
5126        }
5127        return false;
5128 }
5129
5130 bool is_attr_in_list(const char * const * attrs, const char *attr)
5131 {
5132         unsigned int i;
5133
5134         for (i = 0; attrs[i]; i++) {
5135                 if (ldb_attr_cmp(attrs[i], attr) == 0)
5136                         return true;
5137         }
5138
5139         return false;
5140 }
5141
5142 int dsdb_werror_at(struct ldb_context *ldb, int ldb_ecode, WERROR werr,
5143                    const char *location, const char *func,
5144                    const char *reason)
5145 {
5146         if (reason == NULL) {
5147                 reason = win_errstr(werr);
5148         }
5149         ldb_asprintf_errstring(ldb, "%08X: %s at %s:%s",
5150                                W_ERROR_V(werr), reason, location, func);
5151         return ldb_ecode;
5152 }
5153
5154 /*
5155   map an ldb error code to an approximate NTSTATUS code
5156  */
5157 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
5158 {
5159         switch (err) {
5160         case LDB_SUCCESS:
5161                 return NT_STATUS_OK;
5162
5163         case LDB_ERR_PROTOCOL_ERROR:
5164                 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
5165
5166         case LDB_ERR_TIME_LIMIT_EXCEEDED:
5167                 return NT_STATUS_IO_TIMEOUT;
5168
5169         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
5170                 return NT_STATUS_BUFFER_TOO_SMALL;
5171
5172         case LDB_ERR_COMPARE_FALSE:
5173         case LDB_ERR_COMPARE_TRUE:
5174                 return NT_STATUS_REVISION_MISMATCH;
5175
5176         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
5177                 return NT_STATUS_NOT_SUPPORTED;
5178
5179         case LDB_ERR_STRONG_AUTH_REQUIRED:
5180         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
5181         case LDB_ERR_SASL_BIND_IN_PROGRESS:
5182         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
5183         case LDB_ERR_INVALID_CREDENTIALS:
5184         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
5185         case LDB_ERR_UNWILLING_TO_PERFORM:
5186                 return NT_STATUS_ACCESS_DENIED;
5187
5188         case LDB_ERR_NO_SUCH_OBJECT:
5189                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5190
5191         case LDB_ERR_REFERRAL:
5192         case LDB_ERR_NO_SUCH_ATTRIBUTE:
5193                 return NT_STATUS_NOT_FOUND;
5194
5195         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
5196                 return NT_STATUS_NOT_SUPPORTED;
5197
5198         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
5199                 return NT_STATUS_BUFFER_TOO_SMALL;
5200
5201         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
5202         case LDB_ERR_INAPPROPRIATE_MATCHING:
5203         case LDB_ERR_CONSTRAINT_VIOLATION:
5204         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
5205         case LDB_ERR_INVALID_DN_SYNTAX:
5206         case LDB_ERR_NAMING_VIOLATION:
5207         case LDB_ERR_OBJECT_CLASS_VIOLATION:
5208         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
5209         case LDB_ERR_NOT_ALLOWED_ON_RDN:
5210                 return NT_STATUS_INVALID_PARAMETER;
5211
5212         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
5213         case LDB_ERR_ENTRY_ALREADY_EXISTS:
5214                 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
5215
5216         case LDB_ERR_BUSY:
5217                 return NT_STATUS_NETWORK_BUSY;
5218
5219         case LDB_ERR_ALIAS_PROBLEM:
5220         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
5221         case LDB_ERR_UNAVAILABLE:
5222         case LDB_ERR_LOOP_DETECT:
5223         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
5224         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
5225         case LDB_ERR_OTHER:
5226         case LDB_ERR_OPERATIONS_ERROR:
5227                 break;
5228         }
5229         return NT_STATUS_UNSUCCESSFUL;
5230 }
5231
5232
5233 /*
5234   create a new naming context that will hold a partial replica
5235  */
5236 int dsdb_create_partial_replica_NC(struct ldb_context *ldb,  struct ldb_dn *dn)
5237 {
5238         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
5239         struct ldb_message *msg;
5240         int ret;
5241
5242         msg = ldb_msg_new(tmp_ctx);
5243         if (msg == NULL) {
5244                 talloc_free(tmp_ctx);
5245                 return ldb_oom(ldb);
5246         }
5247
5248         msg->dn = dn;
5249         ret = ldb_msg_add_string(msg, "objectClass", "top");
5250         if (ret != LDB_SUCCESS) {
5251                 talloc_free(tmp_ctx);
5252                 return ldb_oom(ldb);
5253         }
5254
5255         /* [MS-DRSR] implies that we should only add the 'top'
5256          * objectclass, but that would cause lots of problems with our
5257          * objectclass code as top is not structural, so we add
5258          * 'domainDNS' as well to keep things sane. We're expecting
5259          * this new NC to be of objectclass domainDNS after
5260          * replication anyway
5261          */
5262         ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
5263         if (ret != LDB_SUCCESS) {
5264                 talloc_free(tmp_ctx);
5265                 return ldb_oom(ldb);
5266         }
5267
5268         ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
5269                               INSTANCE_TYPE_IS_NC_HEAD|
5270                               INSTANCE_TYPE_NC_ABOVE|
5271                               INSTANCE_TYPE_UNINSTANT);
5272         if (ret != LDB_SUCCESS) {
5273                 talloc_free(tmp_ctx);
5274                 return ldb_oom(ldb);
5275         }
5276
5277         ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
5278         if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
5279                 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
5280                          ldb_dn_get_linearized(dn),
5281                          ldb_errstring(ldb), ldb_strerror(ret)));
5282                 talloc_free(tmp_ctx);
5283                 return ret;
5284         }
5285
5286         DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
5287
5288         talloc_free(tmp_ctx);
5289         return LDB_SUCCESS;
5290 }
5291
5292 /*
5293  * Return the effective badPwdCount
5294  *
5295  * This requires that the user_msg have (if present):
5296  *  - badPasswordTime
5297  *  - badPwdCount
5298  *
5299  * This also requires that the domain_msg have (if present):
5300  *  - lockOutObservationWindow
5301  */
5302 static int dsdb_effective_badPwdCount(const struct ldb_message *user_msg,
5303                                       int64_t lockOutObservationWindow,
5304                                       NTTIME now)
5305 {
5306         int64_t badPasswordTime;
5307         badPasswordTime = ldb_msg_find_attr_as_int64(user_msg, "badPasswordTime", 0);
5308
5309         if (badPasswordTime - lockOutObservationWindow >= now) {
5310                 return ldb_msg_find_attr_as_int(user_msg, "badPwdCount", 0);
5311         } else {
5312                 return 0;
5313         }
5314 }
5315
5316 /*
5317  * Returns a user's PSO, or NULL if none was found
5318  */
5319 static struct ldb_result *lookup_user_pso(struct ldb_context *sam_ldb,
5320                                           TALLOC_CTX *mem_ctx,
5321                                           const struct ldb_message *user_msg,
5322                                           const char * const *attrs)
5323 {
5324         struct ldb_result *res = NULL;
5325         struct ldb_dn *pso_dn = NULL;
5326         int ret;
5327
5328         /* if the user has a PSO that applies, then use the PSO's setting */
5329         pso_dn = ldb_msg_find_attr_as_dn(sam_ldb, mem_ctx, user_msg,
5330                                          "msDS-ResultantPSO");
5331
5332         if (pso_dn != NULL) {
5333
5334                 ret = dsdb_search_dn(sam_ldb, mem_ctx, &res, pso_dn, attrs, 0);
5335                 if (ret != LDB_SUCCESS) {
5336
5337                         /*
5338                          * log the error. The caller should fallback to using
5339                          * the default domain password settings
5340                          */
5341                         DBG_ERR("Error retrieving msDS-ResultantPSO %s for %s",
5342                                 ldb_dn_get_linearized(pso_dn),
5343                                 ldb_dn_get_linearized(user_msg->dn));
5344                 }
5345                 talloc_free(pso_dn);
5346         }
5347         return res;
5348 }
5349
5350 /*
5351  * Return the effective badPwdCount
5352  *
5353  * This requires that the user_msg have (if present):
5354  *  - badPasswordTime
5355  *  - badPwdCount
5356  *  - msDS-ResultantPSO
5357  */
5358 int samdb_result_effective_badPwdCount(struct ldb_context *sam_ldb,
5359                                        TALLOC_CTX *mem_ctx,
5360                                        struct ldb_dn *domain_dn,
5361                                        const struct ldb_message *user_msg)
5362 {
5363         struct timeval tv_now = timeval_current();
5364         NTTIME now = timeval_to_nttime(&tv_now);
5365         int64_t lockOutObservationWindow;
5366         struct ldb_result *res = NULL;
5367         const char *attrs[] = { "msDS-LockoutObservationWindow",
5368                                 NULL };
5369
5370         res = lookup_user_pso(sam_ldb, mem_ctx, user_msg, attrs);
5371
5372         if (res != NULL) {
5373                 lockOutObservationWindow =
5374                         ldb_msg_find_attr_as_int64(res->msgs[0],
5375                                                    "msDS-LockoutObservationWindow",
5376                                                     DEFAULT_OBSERVATION_WINDOW);
5377                 talloc_free(res);
5378         } else {
5379
5380                 /* no PSO was found, lookup the default domain setting */
5381                 lockOutObservationWindow =
5382                          samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
5383                                             "lockOutObservationWindow", NULL);
5384         }
5385
5386         return dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5387 }
5388
5389 /*
5390  * Returns the lockoutThreshold that applies. If a PSO is specified, then that
5391  * setting is used over the domain defaults
5392  */
5393 static int64_t get_lockout_threshold(struct ldb_message *domain_msg,
5394                                      struct ldb_message *pso_msg)
5395 {
5396         if (pso_msg != NULL) {
5397                 return ldb_msg_find_attr_as_int(pso_msg,
5398                                                 "msDS-LockoutThreshold", 0);
5399         } else {
5400                 return ldb_msg_find_attr_as_int(domain_msg,
5401                                                 "lockoutThreshold", 0);
5402         }
5403 }
5404
5405 /*
5406  * Returns the lockOutObservationWindow that applies. If a PSO is specified,
5407  * then that setting is used over the domain defaults
5408  */
5409 static int64_t get_lockout_observation_window(struct ldb_message *domain_msg,
5410                                               struct ldb_message *pso_msg)
5411 {
5412         if (pso_msg != NULL) {
5413                 return ldb_msg_find_attr_as_int64(pso_msg,
5414                                                   "msDS-LockoutObservationWindow",
5415                                                    DEFAULT_OBSERVATION_WINDOW);
5416         } else {
5417                 return ldb_msg_find_attr_as_int64(domain_msg,
5418                                                   "lockOutObservationWindow",
5419                                                    DEFAULT_OBSERVATION_WINDOW);
5420         }
5421 }
5422
5423 /*
5424  * Prepare an update to the badPwdCount and associated attributes.
5425  *
5426  * This requires that the user_msg have (if present):
5427  *  - objectSid
5428  *  - badPasswordTime
5429  *  - badPwdCount
5430  *
5431  * This also requires that the domain_msg have (if present):
5432  *  - pwdProperties
5433  *  - lockoutThreshold
5434  *  - lockOutObservationWindow
5435  *
5436  * This also requires that the pso_msg have (if present):
5437  *  - msDS-LockoutThreshold
5438  *  - msDS-LockoutObservationWindow
5439  */
5440 NTSTATUS dsdb_update_bad_pwd_count(TALLOC_CTX *mem_ctx,
5441                                    struct ldb_context *sam_ctx,
5442                                    struct ldb_message *user_msg,
5443                                    struct ldb_message *domain_msg,
5444                                    struct ldb_message *pso_msg,
5445                                    struct ldb_message **_mod_msg)
5446 {
5447         int ret, badPwdCount;
5448         unsigned int i;
5449         int64_t lockoutThreshold, lockOutObservationWindow;
5450         struct dom_sid *sid;
5451         struct timeval tv_now = timeval_current();
5452         NTTIME now = timeval_to_nttime(&tv_now);
5453         NTSTATUS status;
5454         uint32_t pwdProperties, rid = 0;
5455         struct ldb_message *mod_msg;
5456
5457         sid = samdb_result_dom_sid(mem_ctx, user_msg, "objectSid");
5458
5459         pwdProperties = ldb_msg_find_attr_as_uint(domain_msg,
5460                                                   "pwdProperties", -1);
5461         if (sid && !(pwdProperties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
5462                 status = dom_sid_split_rid(NULL, sid, NULL, &rid);
5463                 if (!NT_STATUS_IS_OK(status)) {
5464                         /*
5465                          * This can't happen anyway, but always try
5466                          * and update the badPwdCount on failure
5467                          */
5468                         rid = 0;
5469                 }
5470         }
5471         TALLOC_FREE(sid);
5472
5473         /*
5474          * Work out if we are doing password lockout on the domain.
5475          * Also, the built in administrator account is exempt:
5476          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5477          */
5478         lockoutThreshold = get_lockout_threshold(domain_msg, pso_msg);
5479         if (lockoutThreshold == 0 || (rid == DOMAIN_RID_ADMINISTRATOR)) {
5480                 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5481                           ldb_dn_get_linearized(user_msg->dn)));
5482                 return NT_STATUS_OK;
5483         }
5484
5485         mod_msg = ldb_msg_new(mem_ctx);
5486         if (mod_msg == NULL) {
5487                 return NT_STATUS_NO_MEMORY;
5488         }
5489         mod_msg->dn = ldb_dn_copy(mod_msg, user_msg->dn);
5490         if (mod_msg->dn == NULL) {
5491                 TALLOC_FREE(mod_msg);
5492                 return NT_STATUS_NO_MEMORY;
5493         }
5494
5495         lockOutObservationWindow = get_lockout_observation_window(domain_msg,
5496                                                                   pso_msg);
5497
5498         badPwdCount = dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5499
5500         badPwdCount++;
5501
5502         ret = samdb_msg_add_int(sam_ctx, mod_msg, mod_msg, "badPwdCount", badPwdCount);
5503         if (ret != LDB_SUCCESS) {
5504                 TALLOC_FREE(mod_msg);
5505                 return NT_STATUS_NO_MEMORY;
5506         }
5507         ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "badPasswordTime", now);
5508         if (ret != LDB_SUCCESS) {
5509                 TALLOC_FREE(mod_msg);
5510                 return NT_STATUS_NO_MEMORY;
5511         }
5512
5513         if (badPwdCount >= lockoutThreshold) {
5514                 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "lockoutTime", now);
5515                 if (ret != LDB_SUCCESS) {
5516                         TALLOC_FREE(mod_msg);
5517                         return NT_STATUS_NO_MEMORY;
5518                 }
5519                 DEBUGC( DBGC_AUTH, 1, ("Locked out user %s after %d wrong passwords\n",
5520                           ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5521         } else {
5522                 DEBUGC( DBGC_AUTH, 5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5523                           ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5524         }
5525
5526         /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5527         for (i=0; i< mod_msg->num_elements; i++) {
5528                 mod_msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
5529         }
5530
5531         *_mod_msg = mod_msg;
5532         return NT_STATUS_OK;
5533 }
5534
5535 /**
5536  * Sets defaults for a User object
5537  * List of default attributes set:
5538  *      accountExpires, badPasswordTime, badPwdCount,
5539  *      codePage, countryCode, lastLogoff, lastLogon
5540  *      logonCount, pwdLastSet
5541  */
5542 int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
5543                                struct ldb_message *usr_obj,
5544                                struct ldb_request *req)
5545 {
5546         size_t i;
5547         int ret;
5548         const struct attribute_values {
5549                 const char *name;
5550                 const char *value;
5551                 const char *add_value;
5552                 const char *mod_value;
5553                 const char *control;
5554                 unsigned add_flags;
5555                 unsigned mod_flags;
5556         } map[] = {
5557                 {
5558                         .name = "accountExpires",
5559                         .add_value = "9223372036854775807",
5560                         .mod_value = "0",
5561                 },
5562                 {
5563                         .name = "badPasswordTime",
5564                         .value = "0"
5565                 },
5566                 {
5567                         .name = "badPwdCount",
5568                         .value = "0"
5569                 },
5570                 {
5571                         .name = "codePage",
5572                         .value = "0"
5573                 },
5574                 {
5575                         .name = "countryCode",
5576                         .value = "0"
5577                 },
5578                 {
5579                         .name = "lastLogoff",
5580                         .value = "0"
5581                 },
5582                 {
5583                         .name = "lastLogon",
5584                         .value = "0"
5585                 },
5586                 {
5587                         .name = "logonCount",
5588                         .value = "0"
5589                 },
5590                 {
5591                         .name = "logonHours",
5592                         .add_flags = DSDB_FLAG_INTERNAL_FORCE_META_DATA,
5593                 },
5594                 {
5595                         .name = "pwdLastSet",
5596                         .value = "0",
5597                         .control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
5598                 },
5599                 {
5600                         .name = "adminCount",
5601                         .mod_value = "0",
5602                 },
5603                 {
5604                         .name = "operatorCount",
5605                         .mod_value = "0",
5606                 },
5607         };
5608
5609         for (i = 0; i < ARRAY_SIZE(map); i++) {
5610                 bool added = false;
5611                 const char *value = NULL;
5612                 unsigned flags = 0;
5613
5614                 if (req != NULL && req->operation == LDB_ADD) {
5615                         value = map[i].add_value;
5616                         flags = map[i].add_flags;
5617                 } else {
5618                         value = map[i].mod_value;
5619                         flags = map[i].mod_flags;
5620                 }
5621
5622                 if (value == NULL) {
5623                         value = map[i].value;
5624                 }
5625
5626                 if (value != NULL) {
5627                         flags |= LDB_FLAG_MOD_ADD;
5628                 }
5629
5630                 if (flags == 0) {
5631                         continue;
5632                 }
5633
5634                 ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
5635                                                      map[i].name,
5636                                                      value, flags,
5637                                                      &added);
5638                 if (ret != LDB_SUCCESS) {
5639                         return ret;
5640                 }
5641
5642                 if (req != NULL && added && map[i].control != NULL) {
5643                         ret = ldb_request_add_control(req,
5644                                                       map[i].control,
5645                                                       false, NULL);
5646                         if (ret != LDB_SUCCESS) {
5647                                 return ret;
5648                         }
5649                 }
5650         }
5651
5652         return LDB_SUCCESS;
5653 }
5654
5655 /**
5656  * Sets 'sAMAccountType on user object based on userAccountControl
5657  * @param ldb Current ldb_context
5658  * @param usr_obj ldb_message representing User object
5659  * @param user_account_control Value for userAccountControl flags
5660  * @param account_type_p Optional pointer to account_type to return
5661  * @return LDB_SUCCESS or LDB_ERR* code on failure
5662  */
5663 int dsdb_user_obj_set_account_type(struct ldb_context *ldb, struct ldb_message *usr_obj,
5664                                    uint32_t user_account_control, uint32_t *account_type_p)
5665 {
5666         int ret;
5667         uint32_t account_type;
5668         struct ldb_message_element *el;
5669
5670         account_type = ds_uf2atype(user_account_control);
5671         if (account_type == 0) {
5672                 ldb_set_errstring(ldb, "dsdb: Unrecognized account type!");
5673                 return LDB_ERR_UNWILLING_TO_PERFORM;
5674         }
5675         ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5676                                  "sAMAccountType",
5677                                  account_type);
5678         if (ret != LDB_SUCCESS) {
5679                 return ret;
5680         }
5681         el = ldb_msg_find_element(usr_obj, "sAMAccountType");
5682         el->flags = LDB_FLAG_MOD_REPLACE;
5683
5684         if (account_type_p) {
5685                 *account_type_p = account_type;
5686         }
5687
5688         return LDB_SUCCESS;
5689 }
5690
5691 /**
5692  * Determine and set primaryGroupID based on userAccountControl value
5693  * @param ldb Current ldb_context
5694  * @param usr_obj ldb_message representing User object
5695  * @param user_account_control Value for userAccountControl flags
5696  * @param group_rid_p Optional pointer to group RID to return
5697  * @return LDB_SUCCESS or LDB_ERR* code on failure
5698  */
5699 int dsdb_user_obj_set_primary_group_id(struct ldb_context *ldb, struct ldb_message *usr_obj,
5700                                        uint32_t user_account_control, uint32_t *group_rid_p)
5701 {
5702         int ret;
5703         uint32_t rid;
5704         struct ldb_message_element *el;
5705
5706         rid = ds_uf2prim_group_rid(user_account_control);
5707
5708         ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5709                                  "primaryGroupID", rid);
5710         if (ret != LDB_SUCCESS) {
5711                 return ret;
5712         }
5713         el = ldb_msg_find_element(usr_obj, "primaryGroupID");
5714         el->flags = LDB_FLAG_MOD_REPLACE;
5715
5716         if (group_rid_p) {
5717                 *group_rid_p = rid;
5718         }
5719
5720         return LDB_SUCCESS;
5721 }
5722
5723 /**
5724  * Returns True if the source and target DNs both have the same naming context,
5725  * i.e. they're both in the same partition.
5726  */
5727 bool dsdb_objects_have_same_nc(struct ldb_context *ldb,
5728                                TALLOC_CTX *mem_ctx,
5729                                struct ldb_dn *source_dn,
5730                                struct ldb_dn *target_dn)
5731 {
5732         TALLOC_CTX *tmp_ctx;
5733         struct ldb_dn *source_nc = NULL;
5734         struct ldb_dn *target_nc = NULL;
5735         int ret;
5736         bool same_nc = true;
5737
5738         tmp_ctx = talloc_new(mem_ctx);
5739
5740         ret = dsdb_find_nc_root(ldb, tmp_ctx, source_dn, &source_nc);
5741         /* fix clang warning */
5742         if (source_nc == NULL) {
5743                 ret = LDB_ERR_OTHER;
5744         }
5745         if (ret != LDB_SUCCESS) {
5746                 DBG_ERR("Failed to find base DN for source %s\n",
5747                         ldb_dn_get_linearized(source_dn));
5748                 talloc_free(tmp_ctx);
5749                 return true;
5750         }
5751
5752         ret = dsdb_find_nc_root(ldb, tmp_ctx, target_dn, &target_nc);
5753         /* fix clang warning */
5754         if (target_nc == NULL) {
5755                 ret = LDB_ERR_OTHER;
5756         }
5757         if (ret != LDB_SUCCESS) {
5758                 DBG_ERR("Failed to find base DN for target %s\n",
5759                         ldb_dn_get_linearized(target_dn));
5760                 talloc_free(tmp_ctx);
5761                 return true;
5762         }
5763
5764         same_nc = (ldb_dn_compare(source_nc, target_nc) == 0);
5765
5766         talloc_free(tmp_ctx);
5767
5768         return same_nc;
5769 }
5770 /*
5771  * Context for dsdb_count_domain_callback
5772  */
5773 struct dsdb_count_domain_context {
5774         /*
5775          * Number of matching records
5776          */
5777         size_t count;
5778         /*
5779          * sid of the domain that the records must belong to.
5780          * if NULL records can belong to any domain.
5781          */
5782         struct dom_sid *dom_sid;
5783 };
5784
5785 /*
5786  * @brief ldb aysnc callback for dsdb_domain_count.
5787  *
5788  * count the number of records in the database matching an LDAP query,
5789  * optionally filtering for domain membership.
5790  *
5791  * @param [in,out] req the ldb request being processed
5792  *                    req->context contains:
5793  *                        count   The number of matching records
5794  *                        dom_sid The domain sid, if present records must belong
5795  *                                to the domain to be counted.
5796  *@param [in,out] ares The query result.
5797  *
5798  * @return an LDB error code
5799  *
5800  */
5801 static int dsdb_count_domain_callback(
5802         struct ldb_request *req,
5803         struct ldb_reply *ares)
5804 {
5805
5806         if (ares == NULL) {
5807                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
5808         }
5809         if (ares->error != LDB_SUCCESS) {
5810                 int error = ares->error;
5811                 TALLOC_FREE(ares);
5812                 return ldb_request_done(req, error);
5813         }
5814
5815         switch (ares->type) {
5816         case LDB_REPLY_ENTRY:
5817         {
5818                 struct dsdb_count_domain_context *context = NULL;
5819                 ssize_t ret;
5820                 bool in_domain;
5821                 struct dom_sid sid;
5822                 const struct ldb_val *v;
5823
5824                 context = req->context;
5825                 if (context->dom_sid == NULL) {
5826                         context->count++;
5827                         break;
5828                 }
5829
5830                 v = ldb_msg_find_ldb_val(ares->message, "objectSid");
5831                 if (v == NULL) {
5832                         break;
5833                 }
5834
5835                 ret = sid_parse(v->data, v->length, &sid);
5836                 if (ret == -1) {
5837                         break;
5838                 }
5839
5840                 in_domain = dom_sid_in_domain(context->dom_sid, &sid);
5841                 if (!in_domain) {
5842                         break;
5843                 }
5844
5845                 context->count++;
5846                 break;
5847         }
5848         case LDB_REPLY_REFERRAL:
5849                 break;
5850
5851         case LDB_REPLY_DONE:
5852                 TALLOC_FREE(ares);
5853                 return ldb_request_done(req, LDB_SUCCESS);
5854         }
5855
5856         TALLOC_FREE(ares);
5857
5858         return LDB_SUCCESS;
5859 }
5860
5861 /*
5862  * @brief Count the number of records matching a query.
5863  *
5864  * Count the number of entries in the database matching the supplied query,
5865  * optionally filtering only those entries belonging to the supplied domain.
5866  *
5867  * @param ldb [in] Current ldb context
5868  * @param count [out] Pointer to the count
5869  * @param base [in] The base dn for the quey
5870  * @param dom_sid [in] The domain sid, if non NULL records that are not a member
5871  *                     of the domain are ignored.
5872  * @param scope [in] Search scope.
5873  * @param exp_fmt [in] format string for the query.
5874  *
5875  * @return LDB_STATUS code.
5876  */
5877 int PRINTF_ATTRIBUTE(6, 7) dsdb_domain_count(
5878         struct ldb_context *ldb,
5879         size_t *count,
5880         struct ldb_dn *base,
5881         struct dom_sid *dom_sid,
5882         enum ldb_scope scope,
5883         const char *exp_fmt, ...)
5884 {
5885         TALLOC_CTX *tmp_ctx = NULL;
5886         struct ldb_request *req = NULL;
5887         struct dsdb_count_domain_context *context = NULL;
5888         char *expression = NULL;
5889         const char *object_sid[] = {"objectSid", NULL};
5890         const char *none[] = {NULL};
5891         va_list ap;
5892         int ret;
5893
5894         *count = 0;
5895         tmp_ctx = talloc_new(ldb);
5896
5897         context = talloc_zero(tmp_ctx, struct dsdb_count_domain_context);
5898         if (context == NULL) {
5899                 return LDB_ERR_OPERATIONS_ERROR;
5900         }
5901         context->dom_sid = dom_sid;
5902
5903         if (exp_fmt) {
5904                 va_start(ap, exp_fmt);
5905                 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
5906                 va_end(ap);
5907
5908                 if (expression == NULL) {
5909                         TALLOC_FREE(context);
5910                         TALLOC_FREE(tmp_ctx);
5911                         return LDB_ERR_OPERATIONS_ERROR;
5912                 }
5913         }
5914
5915         ret = ldb_build_search_req(
5916                 &req,
5917                 ldb,
5918                 tmp_ctx,
5919                 base,
5920                 scope,
5921                 expression,
5922                 (dom_sid == NULL) ? none : object_sid,
5923                 NULL,
5924                 context,
5925                 dsdb_count_domain_callback,
5926                 NULL);
5927         ldb_req_set_location(req, "dsdb_domain_count");
5928
5929         if (ret != LDB_SUCCESS) goto done;
5930
5931         ret = ldb_request(ldb, req);
5932
5933         if (ret == LDB_SUCCESS) {
5934                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
5935                 if (ret == LDB_SUCCESS) {
5936                         *count = context->count;
5937                 }
5938         }
5939
5940
5941 done:
5942         TALLOC_FREE(expression);
5943         TALLOC_FREE(req);
5944         TALLOC_FREE(context);
5945         TALLOC_FREE(tmp_ctx);
5946
5947         return ret;
5948 }