9b63468f99cd079a450a49799edc40479bbf40bb
[kai/samba.git] / source / dsdb / samdb / samdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    interface functions for the sam database
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_netlogon.h"
25 #include "librpc/gen_ndr/ndr_misc.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "system/time.h"
28 #include "system/filesys.h"
29 #include "db_wrap.h"
30
31 /*
32   connect to the SAM database
33   return an opaque context pointer on success, or NULL on failure
34  */
35 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx, struct auth_session_info *session_info)
36 {
37         struct ldb_context *ldb;
38         ldb = ldb_wrap_connect(mem_ctx, lp_sam_url(), 0, NULL);
39         if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
40                 return NULL;
41         }
42         return ldb;
43 }
44
45 /*
46   search the sam for the specified attributes in a specific domain, filter on
47   objectSid being in domain_sid.
48 */
49 int samdb_search_domain(struct ldb_context *sam_ldb,
50                         TALLOC_CTX *mem_ctx, 
51                         const struct ldb_dn *basedn,
52                         struct ldb_message ***res,
53                         const char * const *attrs,
54                         const struct dom_sid *domain_sid,
55                         const char *format, ...)  _PRINTF_ATTRIBUTE(7,8)
56 {
57         va_list ap;
58         int i, count;
59
60         va_start(ap, format);
61         count = gendb_search_v(sam_ldb, mem_ctx, basedn,
62                                res, attrs, format, ap);
63         va_end(ap);
64
65         i=0;
66
67         while (i<count) {
68                 struct dom_sid *entry_sid;
69
70                 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
71
72                 if ((entry_sid == NULL) ||
73                     (!dom_sid_in_domain(domain_sid, entry_sid))) {
74                         /* Delete that entry from the result set */
75                         (*res)[i] = (*res)[count-1];
76                         count -= 1;
77                         talloc_free(entry_sid);
78                         continue;
79                 }
80                 talloc_free(entry_sid);
81                 i += 1;
82         }
83
84         return count;
85 }
86
87 /*
88   search the sam for a single string attribute in exactly 1 record
89 */
90 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
91                                   TALLOC_CTX *mem_ctx,
92                                   const struct ldb_dn *basedn,
93                                   const char *attr_name,
94                                   const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
95 {
96         int count;
97         const char *attrs[2] = { NULL, NULL };
98         struct ldb_message **res = NULL;
99
100         attrs[0] = attr_name;
101
102         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
103         if (count > 1) {                
104                 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n", 
105                          attr_name, format, count));
106         }
107         if (count != 1) {
108                 talloc_free(res);
109                 return NULL;
110         }
111
112         return samdb_result_string(res[0], attr_name, NULL);
113 }
114                                  
115
116 /*
117   search the sam for a single string attribute in exactly 1 record
118 */
119 const char *samdb_search_string(struct ldb_context *sam_ldb,
120                                 TALLOC_CTX *mem_ctx,
121                                 const struct ldb_dn *basedn,
122                                 const char *attr_name,
123                                 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
124 {
125         va_list ap;
126         const char *str;
127
128         va_start(ap, format);
129         str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
130         va_end(ap);
131
132         return str;
133 }
134
135 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
136                                TALLOC_CTX *mem_ctx,
137                                const struct ldb_dn *basedn,
138                                const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
139 {
140         va_list ap;
141         struct ldb_dn *ret;
142         struct ldb_message **res = NULL;
143         int count;
144
145         va_start(ap, format);
146         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
147         va_end(ap);
148
149         if (count != 1) return NULL;
150
151         ret = talloc_steal(mem_ctx, res[0]->dn);
152         talloc_free(res);
153
154         return ret;
155 }
156
157 /*
158   search the sam for a dom_sid attribute in exactly 1 record
159 */
160 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
161                                      TALLOC_CTX *mem_ctx,
162                                      const struct ldb_dn *basedn,
163                                      const char *attr_name,
164                                      const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
165 {
166         va_list ap;
167         int count;
168         struct ldb_message **res;
169         const char *attrs[2] = { NULL, NULL };
170         struct dom_sid *sid;
171
172         attrs[0] = attr_name;
173
174         va_start(ap, format);
175         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
176         va_end(ap);
177         if (count > 1) {                
178                 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n", 
179                          attr_name, format, count));
180         }
181         if (count != 1) {
182                 talloc_free(res);
183                 return NULL;
184         }
185         sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
186         talloc_free(res);
187         return sid;     
188 }
189
190 /*
191   return the count of the number of records in the sam matching the query
192 */
193 int samdb_search_count(struct ldb_context *sam_ldb,
194                        TALLOC_CTX *mem_ctx,
195                        const struct ldb_dn *basedn,
196                        const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
197 {
198         va_list ap;
199         struct ldb_message **res;
200         const char * const attrs[] = { NULL };
201         int ret;
202
203         va_start(ap, format);
204         ret = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
205         va_end(ap);
206
207         return ret;
208 }
209
210
211 /*
212   search the sam for a single integer attribute in exactly 1 record
213 */
214 uint_t samdb_search_uint(struct ldb_context *sam_ldb,
215                          TALLOC_CTX *mem_ctx,
216                          uint_t default_value,
217                          const struct ldb_dn *basedn,
218                          const char *attr_name,
219                          const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
220 {
221         va_list ap;
222         int count;
223         struct ldb_message **res;
224         const char *attrs[2] = { NULL, NULL };
225
226         attrs[0] = attr_name;
227
228         va_start(ap, format);
229         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
230         va_end(ap);
231
232         if (count != 1) {
233                 return default_value;
234         }
235
236         return samdb_result_uint(res[0], attr_name, default_value);
237 }
238
239 /*
240   search the sam for a single signed 64 bit integer attribute in exactly 1 record
241 */
242 int64_t samdb_search_int64(struct ldb_context *sam_ldb,
243                            TALLOC_CTX *mem_ctx,
244                            int64_t default_value,
245                            const struct ldb_dn *basedn,
246                            const char *attr_name,
247                            const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
248 {
249         va_list ap;
250         int count;
251         struct ldb_message **res;
252         const char *attrs[2] = { NULL, NULL };
253
254         attrs[0] = attr_name;
255
256         va_start(ap, format);
257         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
258         va_end(ap);
259
260         if (count != 1) {
261                 return default_value;
262         }
263
264         return samdb_result_int64(res[0], attr_name, default_value);
265 }
266
267 /*
268   search the sam for multipe records each giving a single string attribute
269   return the number of matches, or -1 on error
270 */
271 int samdb_search_string_multiple(struct ldb_context *sam_ldb,
272                                  TALLOC_CTX *mem_ctx,
273                                  const struct ldb_dn *basedn,
274                                  const char ***strs,
275                                  const char *attr_name,
276                                  const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
277 {
278         va_list ap;
279         int count, i;
280         const char *attrs[2] = { NULL, NULL };
281         struct ldb_message **res = NULL;
282
283         attrs[0] = attr_name;
284
285         va_start(ap, format);
286         count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
287         va_end(ap);
288
289         if (count <= 0) {
290                 return count;
291         }
292
293         /* make sure its single valued */
294         for (i=0;i<count;i++) {
295                 if (res[i]->num_elements != 1) {
296                         DEBUG(1,("samdb: search for %s %s not single valued\n", 
297                                  attr_name, format));
298                         talloc_free(res);
299                         return -1;
300                 }
301         }
302
303         *strs = talloc_array(mem_ctx, const char *, count+1);
304         if (! *strs) {
305                 talloc_free(res);
306                 return -1;
307         }
308
309         for (i=0;i<count;i++) {
310                 (*strs)[i] = samdb_result_string(res[i], attr_name, NULL);
311         }
312         (*strs)[count] = NULL;
313
314         return count;
315 }
316
317 /*
318   pull a uint from a result set. 
319 */
320 uint_t samdb_result_uint(struct ldb_message *msg, const char *attr, uint_t default_value)
321 {
322         return ldb_msg_find_uint(msg, attr, default_value);
323 }
324
325 /*
326   pull a (signed) int64 from a result set. 
327 */
328 int64_t samdb_result_int64(struct ldb_message *msg, const char *attr, int64_t default_value)
329 {
330         return ldb_msg_find_int64(msg, attr, default_value);
331 }
332
333 /*
334   pull a string from a result set. 
335 */
336 const char *samdb_result_string(struct ldb_message *msg, const char *attr, 
337                                 const char *default_value)
338 {
339         return ldb_msg_find_string(msg, attr, default_value);
340 }
341
342 struct ldb_dn *samdb_result_dn(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
343                                const char *attr, struct ldb_dn *default_value)
344 {
345         const char *string = samdb_result_string(msg, attr, NULL);
346         if (string == NULL) return default_value;
347         return ldb_dn_explode(mem_ctx, string);
348 }
349
350 /*
351   pull a rid from a objectSid in a result set. 
352 */
353 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, struct ldb_message *msg, 
354                                    const char *attr, uint32_t default_value)
355 {
356         struct dom_sid *sid;
357         uint32_t rid;
358
359         sid = samdb_result_dom_sid(mem_ctx, msg, attr);
360         if (sid == NULL) {
361                 return default_value;
362         }
363         rid = sid->sub_auths[sid->num_auths-1];
364         talloc_free(sid);
365         return rid;
366 }
367
368 /*
369   pull a dom_sid structure from a objectSid in a result set. 
370 */
371 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, struct ldb_message *msg, 
372                                      const char *attr)
373 {
374         const struct ldb_val *v;
375         struct dom_sid *sid;
376         NTSTATUS status;
377         v = ldb_msg_find_ldb_val(msg, attr);
378         if (v == NULL) {
379                 return NULL;
380         }
381         sid = talloc(mem_ctx, struct dom_sid);
382         if (sid == NULL) {
383                 return NULL;
384         }
385         status = ndr_pull_struct_blob(v, sid, sid, 
386                                       (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
387         if (!NT_STATUS_IS_OK(status)) {
388                 talloc_free(sid);
389                 return NULL;
390         }
391         return sid;
392 }
393
394 /*
395   pull a guid structure from a objectGUID in a result set. 
396 */
397 struct GUID samdb_result_guid(struct ldb_message *msg, const char *attr)
398 {
399         const struct ldb_val *v;
400         NTSTATUS status;
401         struct GUID guid;
402         TALLOC_CTX *mem_ctx;
403
404         ZERO_STRUCT(guid);
405
406         v = ldb_msg_find_ldb_val(msg, attr);
407         if (!v) return guid;
408
409         mem_ctx = talloc_named_const(NULL, 0, "samdb_result_guid");
410         if (!mem_ctx) return guid;
411         status = ndr_pull_struct_blob(v, mem_ctx, &guid, 
412                                       (ndr_pull_flags_fn_t)ndr_pull_GUID);
413         talloc_free(mem_ctx);
414         if (!NT_STATUS_IS_OK(status)) {
415                 return guid;
416         }
417
418         return guid;
419 }
420
421 /*
422   pull a sid prefix from a objectSid in a result set. 
423   this is used to find the domain sid for a user
424 */
425 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, struct ldb_message *msg, 
426                                         const char *attr)
427 {
428         struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
429         if (!sid || sid->num_auths < 1) return NULL;
430         sid->num_auths--;
431         return sid;
432 }
433
434 /*
435   pull a NTTIME in a result set. 
436 */
437 NTTIME samdb_result_nttime(struct ldb_message *msg, const char *attr, NTTIME default_value)
438 {
439         const char *str = ldb_msg_find_string(msg, attr, NULL);
440         if (!str) return default_value;
441         return nttime_from_string(str);
442 }
443
444 /*
445   pull a uint64_t from a result set. 
446 */
447 uint64_t samdb_result_uint64(struct ldb_message *msg, const char *attr, uint64_t default_value)
448 {
449         return ldb_msg_find_uint64(msg, attr, default_value);
450 }
451
452
453 /*
454   construct the allow_password_change field from the PwdLastSet attribute and the 
455   domain password settings
456 */
457 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb, 
458                                           TALLOC_CTX *mem_ctx, 
459                                           const struct ldb_dn *domain_dn, 
460                                           struct ldb_message *msg, 
461                                           const char *attr)
462 {
463         uint64_t attr_time = samdb_result_uint64(msg, attr, 0);
464         int64_t minPwdAge;
465
466         if (attr_time == 0) {
467                 return 0;
468         }
469
470         minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
471
472         /* yes, this is a -= not a += as minPwdAge is stored as the negative
473            of the number of 100-nano-seconds */
474         attr_time -= minPwdAge;
475
476         return attr_time;
477 }
478
479 /*
480   construct the force_password_change field from the PwdLastSet attribute and the 
481   domain password settings
482 */
483 NTTIME samdb_result_force_password_change(struct ldb_context *sam_ldb, 
484                                           TALLOC_CTX *mem_ctx, 
485                                           const struct ldb_dn *domain_dn, 
486                                           struct ldb_message *msg, 
487                                           const char *attr)
488 {
489         uint64_t attr_time = samdb_result_uint64(msg, attr, 0);
490         int64_t maxPwdAge;
491
492         if (attr_time == 0) {
493                 return 0;
494         }
495
496         maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "maxPwdAge", NULL);
497         if (maxPwdAge == 0) {
498                 return 0;
499         } else {
500                 attr_time -= maxPwdAge;
501         }
502
503         return attr_time;
504 }
505
506 /*
507   pull a samr_Password structutre from a result set. 
508 */
509 struct samr_Password samdb_result_hash(struct ldb_message *msg, const char *attr)
510 {
511         struct samr_Password hash;
512         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
513         ZERO_STRUCT(hash);
514         if (val) {
515                 memcpy(hash.hash, val->data, MIN(val->length, sizeof(hash.hash)));
516         }
517         return hash;
518 }
519
520 /*
521   pull an array of samr_Password structutres from a result set. 
522 */
523 uint_t samdb_result_hashes(TALLOC_CTX *mem_ctx, struct ldb_message *msg, 
524                            const char *attr, struct samr_Password **hashes)
525 {
526         uint_t count = 0;
527         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
528         int i;
529
530         *hashes = NULL;
531         if (!val) {
532                 return 0;
533         }
534         count = val->length / 16;
535         if (count == 0) {
536                 return 0;
537         }
538
539         *hashes = talloc_array(mem_ctx, struct samr_Password, count);
540         if (! *hashes) {
541                 return 0;
542         }
543
544         for (i=0;i<count;i++) {
545                 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
546         }
547
548         return count;
549 }
550
551 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct ldb_message *msg, 
552                                 struct samr_Password **lm_pwd, struct samr_Password **nt_pwd) 
553 {
554
555         const char *unicodePwd = samdb_result_string(msg, "unicodePwd", NULL);
556         
557         struct samr_Password *lmPwdHash, *ntPwdHash;
558         if (unicodePwd) {
559                 if (nt_pwd) {
560                         ntPwdHash = talloc(mem_ctx, struct samr_Password);
561                         if (!ntPwdHash) {
562                                 return NT_STATUS_NO_MEMORY;
563                         }
564                         
565                         E_md4hash(unicodePwd, ntPwdHash->hash);
566                         *nt_pwd = ntPwdHash;
567                 }
568
569                 if (lm_pwd) {
570                         BOOL lm_hash_ok;
571                 
572                         lmPwdHash = talloc(mem_ctx, struct samr_Password);
573                         if (!lmPwdHash) {
574                                 return NT_STATUS_NO_MEMORY;
575                         }
576                         
577                         /* compute the new nt and lm hashes */
578                         lm_hash_ok = E_deshash(unicodePwd, lmPwdHash->hash);
579                         
580                         if (lm_hash_ok) {
581                                 *lm_pwd = lmPwdHash;
582                         } else {
583                                 *lm_pwd = NULL;
584                         }
585                 }
586         } else {
587                 if (nt_pwd) {
588                         int num_nt;
589                         num_nt = samdb_result_hashes(mem_ctx, msg, "ntPwdHash", &ntPwdHash);
590                         if (num_nt == 0) {
591                                 *nt_pwd = NULL;
592                         } else if (num_nt > 1) {
593                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
594                         } else {
595                                 *nt_pwd = &ntPwdHash[0];
596                         }
597                 }
598                 if (lm_pwd) {
599                         int num_lm;
600                         num_lm = samdb_result_hashes(mem_ctx, msg, "lmPwdHash", &lmPwdHash);
601                         if (num_lm == 0) {
602                                 *lm_pwd = NULL;
603                         } else if (num_lm > 1) {
604                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
605                         } else {
606                                 *lm_pwd = &lmPwdHash[0];
607                         }
608                 }
609                 
610         }
611         return NT_STATUS_OK;
612 }
613
614 /*
615   pull a samr_LogonHours structutre from a result set. 
616 */
617 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
618 {
619         struct samr_LogonHours hours;
620         const int units_per_week = 168;
621         const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
622         ZERO_STRUCT(hours);
623         hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
624         if (!hours.bits) {
625                 return hours;
626         }
627         hours.units_per_week = units_per_week;
628         memset(hours.bits, 0xFF, units_per_week);
629         if (val) {
630                 memcpy(hours.bits, val->data, MIN(val->length, units_per_week));
631         }
632         return hours;
633 }
634
635 /*
636   pull a set of account_flags from a result set. 
637 */
638 uint16_t samdb_result_acct_flags(struct ldb_message *msg, const char *attr)
639 {
640         uint_t userAccountControl = ldb_msg_find_uint(msg, attr, 0);
641         return samdb_uf2acb(userAccountControl);
642 }
643
644 /*
645   copy from a template record to a message
646 */
647 int samdb_copy_template(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, 
648                         struct ldb_message *msg, const char *expression)
649 {
650         struct ldb_message **res, *t;
651         int ret, i, j;
652         
653
654         /* pull the template record */
655         ret = gendb_search(sam_ldb, mem_ctx, NULL, &res, NULL, "%s", expression);
656         if (ret != 1) {
657                 DEBUG(1,("samdb: ERROR: template '%s' matched %d records\n", 
658                          expression, ret));
659                 return -1;
660         }
661         t = res[0];
662
663         for (i=0;i<t->num_elements;i++) {
664                 struct ldb_message_element *el = &t->elements[i];
665                 /* some elements should not be copied from the template */
666                 if (strcasecmp(el->name, "cn") == 0 ||
667                     strcasecmp(el->name, "name") == 0 ||
668                     strcasecmp(el->name, "sAMAccountName") == 0) {
669                         continue;
670                 }
671                 for (j=0;j<el->num_values;j++) {
672                         if (strcasecmp(el->name, "objectClass") == 0 &&
673                             (strcasecmp((char *)el->values[j].data, "Template") == 0 ||
674                              strcasecmp((char *)el->values[j].data, "userTemplate") == 0 ||
675                              strcasecmp((char *)el->values[j].data, "groupTemplate") == 0 ||
676                              strcasecmp((char *)el->values[j].data, "foreignSecurityTemplate") == 0 ||
677                              strcasecmp((char *)el->values[j].data, "aliasTemplate") == 0 || 
678                              strcasecmp((char *)el->values[j].data, "trustedDomainTemplate") == 0 || 
679                              strcasecmp((char *)el->values[j].data, "secretTemplate") == 0)) {
680                                 continue;
681                         }
682                         samdb_msg_add_string(sam_ldb, mem_ctx, msg, el->name, 
683                                              (char *)el->values[j].data);
684                 }
685         }
686
687         return 0;
688 }
689
690
691 /*
692   add a string element to a message
693 */
694 int samdb_msg_add_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
695                          const char *attr_name, const char *str)
696 {
697         char *s = talloc_strdup(mem_ctx, str);
698         char *a = talloc_strdup(mem_ctx, attr_name);
699         if (s == NULL || a == NULL) {
700                 return -1;
701         }
702         return ldb_msg_add_string(msg, a, s);
703 }
704
705 /*
706   add a dom_sid element to a message
707 */
708 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
709                          const char *attr_name, struct dom_sid *sid)
710 {
711         struct ldb_val v;
712         NTSTATUS status;
713         status = ndr_push_struct_blob(&v, mem_ctx, sid, 
714                                       (ndr_push_flags_fn_t)ndr_push_dom_sid);
715         if (!NT_STATUS_IS_OK(status)) {
716                 return -1;
717         }
718         return ldb_msg_add_value(msg, attr_name, &v);
719 }
720
721
722 /*
723   add a delete element operation to a message
724 */
725 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
726                          const char *attr_name)
727 {
728         char *a = talloc_strdup(mem_ctx, attr_name);
729         if (a == NULL) {
730                 return -1;
731         }
732         /* we use an empty replace rather than a delete, as it allows for 
733            samdb_replace() to be used everywhere */
734         return ldb_msg_add_empty(msg, a, LDB_FLAG_MOD_REPLACE);
735 }
736
737 /*
738   add a add attribute value to a message
739 */
740 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
741                          const char *attr_name, const char *value)
742 {
743         struct ldb_message_element *el;
744         char *a, *v;
745         int ret;
746         a = talloc_strdup(mem_ctx, attr_name);
747         if (a == NULL)
748                 return -1;
749         v = talloc_strdup(mem_ctx, value);
750         if (v == NULL)
751                 return -1;
752         ret = ldb_msg_add_string(msg, a, v);
753         if (ret != 0)
754                 return ret;
755         el = ldb_msg_find_element(msg, a);
756         if (el == NULL)
757                 return -1;
758         el->flags = LDB_FLAG_MOD_ADD;
759         return 0;
760 }
761
762 /*
763   add a delete attribute value to a message
764 */
765 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
766                          const char *attr_name, const char *value)
767 {
768         struct ldb_message_element *el;
769         char *a, *v;
770         int ret;
771         a = talloc_strdup(mem_ctx, attr_name);
772         if (a == NULL)
773                 return -1;
774         v = talloc_strdup(mem_ctx, value);
775         if (v == NULL)
776                 return -1;
777         ret = ldb_msg_add_string(msg, a, v);
778         if (ret != 0)
779                 return ret;
780         el = ldb_msg_find_element(msg, a);
781         if (el == NULL)
782                 return -1;
783         el->flags = LDB_FLAG_MOD_DELETE;
784         return 0;
785 }
786
787 /*
788   add a uint_t element to a message
789 */
790 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
791                        const char *attr_name, uint_t v)
792 {
793         const char *s = talloc_asprintf(mem_ctx, "%u", v);
794         return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
795 }
796
797 /*
798   add a (signed) int64_t element to a message
799 */
800 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
801                         const char *attr_name, int64_t v)
802 {
803         const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
804         return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
805 }
806
807 /*
808   add a uint64_t element to a message
809 */
810 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
811                         const char *attr_name, uint64_t v)
812 {
813         const char *s = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)v);
814         return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
815 }
816
817 /*
818   add a samr_Password element to a message
819 */
820 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
821                        const char *attr_name, struct samr_Password *hash)
822 {
823         struct ldb_val val;
824         val.data = talloc_memdup(mem_ctx, hash->hash, 16);
825         if (!val.data) {
826                 return -1;
827         }
828         val.length = 16;
829         return ldb_msg_add_value(msg, attr_name, &val);
830 }
831
832 /*
833   add a samr_Password array to a message
834 */
835 int samdb_msg_add_hashes(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
836                          const char *attr_name, struct samr_Password *hashes, uint_t count)
837 {
838         struct ldb_val val;
839         int i;
840         val.data = talloc_array_size(mem_ctx, 16, count);
841         val.length = count*16;
842         if (!val.data) {
843                 return -1;
844         }
845         for (i=0;i<count;i++) {
846                 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
847         }
848         return ldb_msg_add_value(msg, attr_name, &val);
849 }
850
851 /*
852   add a acct_flags element to a message
853 */
854 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
855                              const char *attr_name, uint32_t v)
856 {
857         return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, samdb_acb2uf(v));
858 }
859
860 /*
861   add a logon_hours element to a message
862 */
863 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
864                               const char *attr_name, struct samr_LogonHours *hours)
865 {
866         struct ldb_val val;
867         val.length = hours->units_per_week / 8;
868         val.data = hours->bits;
869         return ldb_msg_add_value(msg, attr_name, &val);
870 }
871
872 /*
873   add a general value element to a message
874 */
875 int samdb_msg_add_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
876                               const char *attr_name, const struct ldb_val *val)
877 {
878         return ldb_msg_add_value(msg, attr_name, val);
879 }
880
881 /*
882   sets a general value element to a message
883 */
884 int samdb_msg_set_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
885                         const char *attr_name, const struct ldb_val *val)
886 {
887         struct ldb_message_element *el;
888
889         el = ldb_msg_find_element(msg, attr_name);
890         if (el) {
891                 el->num_values = 0;
892         }
893         return ldb_msg_add_value(msg, attr_name, val);
894 }
895
896 /*
897   set a string element in a message
898 */
899 int samdb_msg_set_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
900                          const char *attr_name, const char *str)
901 {
902         struct ldb_message_element *el;
903
904         el = ldb_msg_find_element(msg, attr_name);
905         if (el) {
906                 el->num_values = 0;
907         }
908         return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, str);
909 }
910
911 /*
912   add a record
913 */
914 int samdb_add(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
915 {
916         return ldb_add(sam_ldb, msg);
917 }
918
919 /*
920   delete a record
921 */
922 int samdb_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, const struct ldb_dn *dn)
923 {
924         return ldb_delete(sam_ldb, dn);
925 }
926
927 /*
928   modify a record
929 */
930 int samdb_modify(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
931 {
932         return ldb_modify(sam_ldb, msg);
933 }
934
935 /*
936   replace elements in a record
937 */
938 int samdb_replace(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
939 {
940         int i;
941
942         /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
943         for (i=0;i<msg->num_elements;i++) {
944                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
945         }
946
947         /* modify the samdb record */
948         return samdb_modify(sam_ldb, mem_ctx, msg);
949 }
950
951 /*
952   return a default security descriptor
953 */
954 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
955 {
956         struct security_descriptor *sd;
957
958         sd = security_descriptor_initialise(mem_ctx);
959
960         return sd;
961 }
962
963 struct ldb_dn *samdb_base_dn(TALLOC_CTX *mem_ctx) 
964 {
965         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
966         int server_role = lp_server_role();
967         const char **split_realm;
968         struct ldb_dn *dn;
969         
970         if (!tmp_ctx) {
971                 return NULL;
972         }
973
974         if ((server_role == ROLE_DOMAIN_PDC)
975             || (server_role == ROLE_DOMAIN_BDC)) {
976                 int i;
977                 split_realm = str_list_make(tmp_ctx, lp_realm(), ".");
978                 if (!split_realm) {
979                         talloc_free(tmp_ctx);
980                         return NULL;
981                 }
982                 dn = NULL;
983                 i = str_list_length(split_realm);
984                 i--;
985                 for (; i >= 0; i--) {
986                         dn = ldb_dn_build_child(tmp_ctx, "dc", split_realm[i], dn);
987                         if (!dn) {
988                                 talloc_free(tmp_ctx);
989                                 return NULL;
990                         }
991                 }
992                 return dn;
993         }
994         return ldb_dn_string_compose(mem_ctx, NULL, "cn=%s", lp_netbios_name());
995 }