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