lib: Make callers of base64_encode_data_blob check for success
[samba.git] / source3 / libnet / libnet_samsync_passdb.c
1 /*
2    Unix SMB/CIFS implementation.
3    dump the remote SAM using rpc samsync operations
4
5    Copyright (C) Andrew Tridgell 2002
6    Copyright (C) Tim Potter 2001,2002
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
8    Modified by Volker Lendecke 2002
9    Copyright (C) Jeremy Allison 2005.
10    Copyright (C) Guenther Deschner 2008.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "system/passwd.h"
28 #include "libnet/libnet_samsync.h"
29 #include "../libcli/security/security.h"
30 #include "passdb.h"
31
32 /* Convert a struct samu_DELTA to a struct samu. */
33 #define STRING_CHANGED (old_string && !new_string) ||\
34                     (!old_string && new_string) ||\
35                 (old_string && new_string && (strcmp(old_string, new_string) != 0))
36
37 #define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\
38                     (!(s1) && (s2)) ||\
39                 ((s1) && (s2) && (strcmp((s1), (s2)) != 0))
40
41 /****************************************************************
42 ****************************************************************/
43
44 static NTSTATUS sam_account_from_delta(struct samu *account,
45                                        struct netr_DELTA_USER *r)
46 {
47         const char *old_string, *new_string;
48         time_t unix_time, stored_time;
49         uchar zero_buf[16];
50
51         memset(zero_buf, '\0', sizeof(zero_buf));
52
53         /* Username, fullname, home dir, dir drive, logon script, acct
54            desc, workstations, profile. */
55
56         if (r->account_name.string) {
57                 old_string = pdb_get_nt_username(account);
58                 new_string = r->account_name.string;
59
60                 if (STRING_CHANGED) {
61                         pdb_set_nt_username(account, new_string, PDB_CHANGED);
62                 }
63
64                 /* Unix username is the same - for sanity */
65                 old_string = pdb_get_username( account );
66                 if (STRING_CHANGED) {
67                         pdb_set_username(account, new_string, PDB_CHANGED);
68                 }
69         }
70
71         if (r->full_name.string) {
72                 old_string = pdb_get_fullname(account);
73                 new_string = r->full_name.string;
74
75                 if (STRING_CHANGED)
76                         pdb_set_fullname(account, new_string, PDB_CHANGED);
77         }
78
79         if (r->home_directory.string) {
80                 old_string = pdb_get_homedir(account);
81                 new_string = r->home_directory.string;
82
83                 if (STRING_CHANGED)
84                         pdb_set_homedir(account, new_string, PDB_CHANGED);
85         }
86
87         if (r->home_drive.string) {
88                 old_string = pdb_get_dir_drive(account);
89                 new_string = r->home_drive.string;
90
91                 if (STRING_CHANGED)
92                         pdb_set_dir_drive(account, new_string, PDB_CHANGED);
93         }
94
95         if (r->logon_script.string) {
96                 old_string = pdb_get_logon_script(account);
97                 new_string = r->logon_script.string;
98
99                 if (STRING_CHANGED)
100                         pdb_set_logon_script(account, new_string, PDB_CHANGED);
101         }
102
103         if (r->description.string) {
104                 old_string = pdb_get_acct_desc(account);
105                 new_string = r->description.string;
106
107                 if (STRING_CHANGED)
108                         pdb_set_acct_desc(account, new_string, PDB_CHANGED);
109         }
110
111         if (r->workstations.string) {
112                 old_string = pdb_get_workstations(account);
113                 new_string = r->workstations.string;
114
115                 if (STRING_CHANGED)
116                         pdb_set_workstations(account, new_string, PDB_CHANGED);
117         }
118
119         if (r->profile_path.string) {
120                 old_string = pdb_get_profile_path(account);
121                 new_string = r->profile_path.string;
122
123                 if (STRING_CHANGED)
124                         pdb_set_profile_path(account, new_string, PDB_CHANGED);
125         }
126
127         if (r->parameters.array) {
128                 DATA_BLOB mung;
129                 char *newstr = NULL;
130                 old_string = pdb_get_munged_dial(account);
131                 mung.length = r->parameters.length * 2;
132                 mung.data = (uint8_t *) r->parameters.array;
133
134                 if (mung.length != 0) {
135                         newstr = base64_encode_data_blob(talloc_tos(), mung);
136                         SMB_ASSERT(newstr != NULL);
137                 }
138
139                 if (STRING_CHANGED_NC(old_string, newstr))
140                         pdb_set_munged_dial(account, newstr, PDB_CHANGED);
141                 TALLOC_FREE(newstr);
142         }
143
144         /* User and group sid */
145         if (pdb_get_user_rid(account) != r->rid)
146                 pdb_set_user_sid_from_rid(account, r->rid, PDB_CHANGED);
147         if (pdb_get_group_rid(account) != r->primary_gid)
148                 pdb_set_group_sid_from_rid(account, r->primary_gid, PDB_CHANGED);
149
150         /* Logon and password information */
151         if (!nt_time_is_zero(&r->last_logon)) {
152                 unix_time = nt_time_to_unix(r->last_logon);
153                 stored_time = pdb_get_logon_time(account);
154                 if (stored_time != unix_time)
155                         pdb_set_logon_time(account, unix_time, PDB_CHANGED);
156         }
157
158         if (!nt_time_is_zero(&r->last_logoff)) {
159                 unix_time = nt_time_to_unix(r->last_logoff);
160                 stored_time = pdb_get_logoff_time(account);
161                 if (stored_time != unix_time)
162                         pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
163         }
164
165         /* Logon Divs */
166         if (pdb_get_logon_divs(account) != r->logon_hours.units_per_week)
167                 pdb_set_logon_divs(account, r->logon_hours.units_per_week, PDB_CHANGED);
168
169 #if 0
170         /* no idea what to do with this one - gd */
171         /* Max Logon Hours */
172         if (delta->unknown1 != pdb_get_unknown_6(account)) {
173                 pdb_set_unknown_6(account, delta->unknown1, PDB_CHANGED);
174         }
175 #endif
176         /* Logon Hours Len */
177         if (r->logon_hours.units_per_week/8 != pdb_get_hours_len(account)) {
178                 pdb_set_hours_len(account, r->logon_hours.units_per_week/8, PDB_CHANGED);
179         }
180
181         /* Logon Hours */
182         if (r->logon_hours.bits) {
183                 char oldstr[44], newstr[44];
184                 pdb_sethexhours(oldstr, pdb_get_hours(account));
185                 pdb_sethexhours(newstr, r->logon_hours.bits);
186                 if (!strequal(oldstr, newstr))
187                         pdb_set_hours(account, r->logon_hours.bits,
188                                       pdb_get_hours_len(account), PDB_CHANGED);
189         }
190
191         if (pdb_get_bad_password_count(account) != r->bad_password_count)
192                 pdb_set_bad_password_count(account, r->bad_password_count, PDB_CHANGED);
193
194         if (pdb_get_logon_count(account) != r->logon_count)
195                 pdb_set_logon_count(account, r->logon_count, PDB_CHANGED);
196
197         if (!nt_time_is_zero(&r->last_password_change)) {
198                 unix_time = nt_time_to_unix(r->last_password_change);
199                 stored_time = pdb_get_pass_last_set_time(account);
200                 if (stored_time != unix_time)
201                         pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
202         } else {
203                 /* no last set time, make it now */
204                 pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED);
205         }
206
207         if (!nt_time_is_zero(&r->acct_expiry)) {
208                 unix_time = nt_time_to_unix(r->acct_expiry);
209                 stored_time = pdb_get_kickoff_time(account);
210                 if (stored_time != unix_time)
211                         pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
212         }
213
214         /* Decode hashes from password hash
215            Note that win2000 may send us all zeros for the hashes if it doesn't
216            think this channel is secure enough - don't set the passwords at all
217            in that case
218         */
219         if (memcmp(r->lmpassword.hash, zero_buf, 16) != 0) {
220                 pdb_set_lanman_passwd(account, r->lmpassword.hash, PDB_CHANGED);
221         }
222
223         if (memcmp(r->ntpassword.hash, zero_buf, 16) != 0) {
224                 pdb_set_nt_passwd(account, r->ntpassword.hash, PDB_CHANGED);
225         }
226
227         /* TODO: account expiry time */
228
229         pdb_set_acct_ctrl(account, r->acct_flags, PDB_CHANGED);
230
231         pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
232
233         return NT_STATUS_OK;
234 }
235
236
237 /****************************************************************
238 ****************************************************************/
239
240 static NTSTATUS smb_create_user(TALLOC_CTX *mem_ctx,
241                                 uint32_t acct_flags,
242                                 const char *account,
243                                 struct passwd **passwd_p)
244 {
245         struct passwd *passwd;
246         char *add_script = NULL;
247
248         passwd = Get_Pwnam_alloc(mem_ctx, account);
249         if (passwd) {
250                 *passwd_p = passwd;
251                 return NT_STATUS_OK;
252         }
253
254         /* Create appropriate user */
255         if (acct_flags & ACB_NORMAL) {
256                 add_script = lp_add_user_script(mem_ctx);
257         } else if ( (acct_flags & ACB_WSTRUST) ||
258                     (acct_flags & ACB_SVRTRUST) ||
259                     (acct_flags & ACB_DOMTRUST) ) {
260                 add_script = lp_add_machine_script(mem_ctx);
261         } else {
262                 DEBUG(1, ("Unknown user type: %s\n",
263                           pdb_encode_acct_ctrl(acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
264                 return NT_STATUS_UNSUCCESSFUL;
265         }
266
267         if (!add_script) {
268                 return NT_STATUS_NO_MEMORY;
269         }
270
271         if (*add_script) {
272                 int add_ret;
273                 add_script = talloc_all_string_sub(mem_ctx, add_script,
274                                                    "%u", account);
275                 if (!add_script) {
276                         return NT_STATUS_NO_MEMORY;
277                 }
278                 add_ret = smbrun(add_script, NULL);
279                 DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
280                          "gave %d\n", add_script, add_ret));
281                 if (add_ret == 0) {
282                         smb_nscd_flush_user_cache();
283                 }
284         }
285
286         /* try and find the possible unix account again */
287         passwd = Get_Pwnam_alloc(mem_ctx, account);
288         if (!passwd) {
289                 return NT_STATUS_NO_SUCH_USER;
290         }
291
292         *passwd_p = passwd;
293
294         return NT_STATUS_OK;
295 }
296 /****************************************************************
297 ****************************************************************/
298
299 static NTSTATUS fetch_account_info(TALLOC_CTX *mem_ctx,
300                                    uint32_t rid,
301                                    struct netr_DELTA_USER *r)
302 {
303
304         NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL;
305         fstring account;
306         struct samu *sam_account=NULL;
307         GROUP_MAP *map = NULL;
308         struct group *grp;
309         struct dom_sid user_sid;
310         struct dom_sid group_sid;
311         struct passwd *passwd = NULL;
312         fstring sid_string;
313
314         fstrcpy(account, r->account_name.string);
315         d_printf("Creating account: %s\n", account);
316
317         if ( !(sam_account = samu_new(mem_ctx)) ) {
318                 return NT_STATUS_NO_MEMORY;
319         }
320
321         nt_ret = smb_create_user(sam_account, r->acct_flags, account, &passwd);
322         if (!NT_STATUS_IS_OK(nt_ret)) {
323                 d_fprintf(stderr, "Could not create posix account info for '%s'\n",
324                         account);
325                 goto done;
326         }
327
328         sid_compose(&user_sid, get_global_sam_sid(), r->rid);
329
330         DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n",
331                   sid_to_fstring(sid_string, &user_sid), account));
332         if (!pdb_getsampwsid(sam_account, &user_sid)) {
333                 sam_account_from_delta(sam_account, r);
334                 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
335                           sid_to_fstring(sid_string, &user_sid),
336                           pdb_get_username(sam_account)));
337                 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
338                         DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
339                                   account));
340                         return NT_STATUS_ACCESS_DENIED;
341                 }
342         } else {
343                 sam_account_from_delta(sam_account, r);
344                 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
345                           sid_to_fstring(sid_string, &user_sid),
346                           pdb_get_username(sam_account)));
347                 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
348                         DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
349                                   account));
350                         TALLOC_FREE(sam_account);
351                         return NT_STATUS_ACCESS_DENIED;
352                 }
353         }
354
355         if (pdb_get_group_sid(sam_account) == NULL) {
356                 return NT_STATUS_UNSUCCESSFUL;
357         }
358
359         group_sid = *pdb_get_group_sid(sam_account);
360
361         map = talloc_zero(mem_ctx, GROUP_MAP);
362         if (!map) {
363                 return NT_STATUS_NO_MEMORY;
364         }
365
366         if (!pdb_getgrsid(map, group_sid)) {
367                 DEBUG(0, ("Primary group of %s has no mapping!\n",
368                           pdb_get_username(sam_account)));
369         } else {
370                 if (map->gid != passwd->pw_gid) {
371                         if (!(grp = getgrgid(map->gid))) {
372                                 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
373                                           (unsigned long)map->gid, pdb_get_username(sam_account), sid_string_tos(&group_sid)));
374                         } else {
375                                 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
376                         }
377                 }
378         }
379
380         if ( !passwd ) {
381                 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
382                         pdb_get_username(sam_account)));
383         }
384
385  done:
386         TALLOC_FREE(sam_account);
387         TALLOC_FREE(map);
388         return nt_ret;
389 }
390
391 /****************************************************************
392 ****************************************************************/
393
394 static NTSTATUS fetch_group_info(TALLOC_CTX *mem_ctx,
395                                  uint32_t rid,
396                                  struct netr_DELTA_GROUP *r)
397 {
398         struct group *grp = NULL;
399         struct dom_sid group_sid;
400         fstring sid_string;
401         GROUP_MAP *map;
402         bool insert = true;
403
404         map = talloc_zero(mem_ctx, GROUP_MAP);
405         if (!map) {
406                 return NT_STATUS_NO_MEMORY;
407         }
408         /* add the group to the mapping table */
409         sid_compose(&group_sid, get_global_sam_sid(), rid);
410         sid_to_fstring(sid_string, &group_sid);
411
412         if (pdb_getgrsid(map, group_sid)) {
413                 if (map->gid != -1) {
414                         grp = getgrgid(map->gid);
415                 }
416                 insert = false;
417         }
418
419         map->nt_name = talloc_strdup(map, r->group_name.string);
420         if (!map->nt_name) {
421                 return NT_STATUS_NO_MEMORY;
422         }
423         map->comment = talloc_strdup(map, r->description.string);
424         if (!map->comment) {
425                 return NT_STATUS_NO_MEMORY;
426         }
427
428         if (grp == NULL) {
429                 gid_t gid;
430
431                 /* No group found from mapping, find it from its name. */
432                 if ((grp = getgrnam(map->nt_name)) == NULL) {
433
434                         /* No appropriate group found, create one */
435
436                         d_printf("Creating unix group: '%s'\n", map->nt_name);
437
438                         if (smb_create_group(map->nt_name, &gid) != 0)
439                                 return NT_STATUS_ACCESS_DENIED;
440
441                         if ((grp = getgrnam(map->nt_name)) == NULL)
442                                 return NT_STATUS_ACCESS_DENIED;
443                 }
444         }
445
446         map->gid = grp->gr_gid;
447         map->sid = group_sid;
448         map->sid_name_use = SID_NAME_DOM_GRP;
449
450         if (insert) {
451                 pdb_add_group_mapping_entry(map);
452         } else {
453                 pdb_update_group_mapping_entry(map);
454         }
455
456         TALLOC_FREE(map);
457         return NT_STATUS_OK;
458 }
459
460 /****************************************************************
461 ****************************************************************/
462
463 static NTSTATUS fetch_group_mem_info(TALLOC_CTX *mem_ctx,
464                                      uint32_t rid,
465                                      struct netr_DELTA_GROUP_MEMBER *r)
466 {
467         int i;
468         char **nt_members = NULL;
469         char **unix_members;
470         struct dom_sid group_sid;
471         GROUP_MAP *map;
472         struct group *grp;
473
474         if (r->num_rids == 0) {
475                 return NT_STATUS_OK;
476         }
477
478         sid_compose(&group_sid, get_global_sam_sid(), rid);
479
480         map = talloc_zero(mem_ctx, GROUP_MAP);
481         if (!map) {
482                 return NT_STATUS_NO_MEMORY;
483         }
484
485         if (!get_domain_group_from_sid(group_sid, map)) {
486                 DEBUG(0, ("Could not find global group %d\n", rid));
487                 TALLOC_FREE(map);
488                 return NT_STATUS_NO_SUCH_GROUP;
489         }
490
491         if (!(grp = getgrgid(map->gid))) {
492                 DEBUG(0, ("Could not find unix group %lu\n",
493                                                 (unsigned long)map->gid));
494                 TALLOC_FREE(map);
495                 return NT_STATUS_NO_SUCH_GROUP;
496         }
497
498         TALLOC_FREE(map);
499
500         d_printf("Group members of %s: ", grp->gr_name);
501
502         if (r->num_rids) {
503                 if ((nt_members = talloc_zero_array(mem_ctx, char *, r->num_rids)) == NULL) {
504                         DEBUG(0, ("talloc failed\n"));
505                         return NT_STATUS_NO_MEMORY;
506                 }
507         } else {
508                 nt_members = NULL;
509         }
510
511         for (i=0; i < r->num_rids; i++) {
512                 struct samu *member = NULL;
513                 struct dom_sid member_sid;
514
515                 if ( !(member = samu_new(mem_ctx)) ) {
516                         return NT_STATUS_NO_MEMORY;
517                 }
518
519                 sid_compose(&member_sid, get_global_sam_sid(), r->rids[i]);
520
521                 if (!pdb_getsampwsid(member, &member_sid)) {
522                         DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
523                                   r->rids[i], sid_string_tos(&member_sid), grp->gr_name));
524                         TALLOC_FREE(member);
525                         continue;
526                 }
527
528                 if (pdb_get_group_rid(member) == rid) {
529                         d_printf("%s(primary),", pdb_get_username(member));
530                         TALLOC_FREE(member);
531                         continue;
532                 }
533
534                 d_printf("%s,", pdb_get_username(member));
535                 nt_members[i] = talloc_strdup(mem_ctx, pdb_get_username(member));
536                 TALLOC_FREE(member);
537         }
538
539         d_printf("\n");
540
541         unix_members = grp->gr_mem;
542
543         while (*unix_members) {
544                 bool is_nt_member = false;
545                 for (i=0; i < r->num_rids; i++) {
546                         if (nt_members[i] == NULL) {
547                                 /* This was a primary group */
548                                 continue;
549                         }
550
551                         if (strcmp(*unix_members, nt_members[i]) == 0) {
552                                 is_nt_member = true;
553                                 break;
554                         }
555                 }
556                 if (!is_nt_member) {
557                         /* We look at a unix group member that is not
558                            an nt group member. So, remove it. NT is
559                            boss here. */
560                         smb_delete_user_group(grp->gr_name, *unix_members);
561                 }
562                 unix_members += 1;
563         }
564
565         for (i=0; i < r->num_rids; i++) {
566                 bool is_unix_member = false;
567
568                 if (nt_members[i] == NULL) {
569                         /* This was the primary group */
570                         continue;
571                 }
572
573                 unix_members = grp->gr_mem;
574
575                 while (*unix_members) {
576                         if (strcmp(*unix_members, nt_members[i]) == 0) {
577                                 is_unix_member = true;
578                                 break;
579                         }
580                         unix_members += 1;
581                 }
582
583                 if (!is_unix_member) {
584                         /* We look at a nt group member that is not a
585                            unix group member currently. So, add the nt
586                            group member. */
587                         smb_add_user_group(grp->gr_name, nt_members[i]);
588                 }
589         }
590
591         return NT_STATUS_OK;
592 }
593
594 /****************************************************************
595 ****************************************************************/
596
597 static NTSTATUS fetch_alias_info(TALLOC_CTX *mem_ctx,
598                                  uint32_t rid,
599                                  struct netr_DELTA_ALIAS *r,
600                                  const struct dom_sid *dom_sid)
601 {
602         struct group *grp = NULL;
603         struct dom_sid alias_sid;
604         fstring sid_string;
605         GROUP_MAP *map;
606         bool insert = true;
607
608         map = talloc_zero(mem_ctx, GROUP_MAP);
609         if (!map) {
610                 return NT_STATUS_NO_MEMORY;
611         }
612
613         /* Find out whether the group is already mapped */
614         sid_compose(&alias_sid, dom_sid, rid);
615         sid_to_fstring(sid_string, &alias_sid);
616
617         if (pdb_getgrsid(map, alias_sid)) {
618                 grp = getgrgid(map->gid);
619                 insert = false;
620         }
621
622         map->nt_name = talloc_strdup(map, r->alias_name.string);
623         if (!map->nt_name) {
624                 return NT_STATUS_NO_MEMORY;
625         }
626         map->comment = talloc_strdup(map, r->description.string);
627         if (!map->comment) {
628                 return NT_STATUS_NO_MEMORY;
629         }
630
631         if (grp == NULL) {
632                 gid_t gid;
633
634                 /* No group found from mapping, find it from its name. */
635                 if ((grp = getgrnam(map->nt_name)) == NULL) {
636                         /* No appropriate group found, create one */
637                         d_printf("Creating unix group: '%s'\n", map->nt_name);
638                         if (smb_create_group(map->nt_name, &gid) != 0)
639                                 return NT_STATUS_ACCESS_DENIED;
640                         if ((grp = getgrgid(gid)) == NULL)
641                                 return NT_STATUS_ACCESS_DENIED;
642                 }
643         }
644
645         map->gid = grp->gr_gid;
646         map->sid = alias_sid;
647
648         if (dom_sid_equal(dom_sid, &global_sid_Builtin)) {
649                 map->sid_name_use = SID_NAME_WKN_GRP;
650         } else {
651                 map->sid_name_use = SID_NAME_ALIAS;
652         }
653
654         if (insert) {
655                 pdb_add_group_mapping_entry(map);
656         } else {
657                 pdb_update_group_mapping_entry(map);
658         }
659
660         TALLOC_FREE(map);
661         return NT_STATUS_OK;
662 }
663
664 /****************************************************************
665 ****************************************************************/
666
667 static NTSTATUS fetch_alias_mem(TALLOC_CTX *mem_ctx,
668                                 uint32_t rid,
669                                 struct netr_DELTA_ALIAS_MEMBER *r,
670                                 const struct dom_sid *dom_sid)
671 {
672         return NT_STATUS_OK;
673 }
674
675 /****************************************************************
676 ****************************************************************/
677
678 static NTSTATUS fetch_domain_info(TALLOC_CTX *mem_ctx,
679                                   uint32_t rid,
680                                   struct netr_DELTA_DOMAIN *r)
681 {
682         time_t u_max_age, u_min_age, u_logout;
683         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
684         const char *domname;
685         struct netr_AcctLockStr *lockstr = NULL;
686         NTSTATUS status;
687
688         status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
689                                        &lockstr);
690         if (!NT_STATUS_IS_OK(status)) {
691                 d_printf("failed to pull account lockout string: %s\n",
692                         nt_errstr(status));
693         }
694
695         u_max_age = uint64s_nt_time_to_unix_abs((uint64_t *)&r->max_password_age);
696         u_min_age = uint64s_nt_time_to_unix_abs((uint64_t *)&r->min_password_age);
697         u_logout = uint64s_nt_time_to_unix_abs((uint64_t *)&r->force_logoff_time);
698
699         domname = r->domain_name.string;
700         if (!domname) {
701                 return NT_STATUS_NO_MEMORY;
702         }
703
704         /* we don't handle BUILTIN account policies */
705         if (!strequal(domname, get_global_sam_name())) {
706                 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
707                 return NT_STATUS_OK;
708         }
709
710
711         if (!pdb_set_account_policy(PDB_POLICY_PASSWORD_HISTORY,
712                                     r->password_history_length))
713                 return nt_status;
714
715         if (!pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
716                                     r->min_password_length))
717                 return nt_status;
718
719         if (!pdb_set_account_policy(PDB_POLICY_MAX_PASSWORD_AGE,
720                                     (uint32_t)u_max_age))
721                 return nt_status;
722
723         if (!pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_AGE,
724                                     (uint32_t)u_min_age))
725                 return nt_status;
726
727         if (!pdb_set_account_policy(PDB_POLICY_TIME_TO_LOGOUT,
728                                     (uint32_t)u_logout))
729                 return nt_status;
730
731         if (lockstr) {
732                 time_t u_lockoutreset, u_lockouttime;
733
734                 u_lockoutreset = uint64s_nt_time_to_unix_abs(&lockstr->reset_count);
735                 u_lockouttime = uint64s_nt_time_to_unix_abs((uint64_t *)&lockstr->lockout_duration);
736
737                 if (!pdb_set_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT,
738                                             lockstr->bad_attempt_lockout))
739                         return nt_status;
740
741                 if (!pdb_set_account_policy(PDB_POLICY_RESET_COUNT_TIME,
742                                             (uint32_t)u_lockoutreset/60))
743                         return nt_status;
744
745                 if (u_lockouttime != -1)
746                         u_lockouttime /= 60;
747
748                 if (!pdb_set_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
749                                             (uint32_t)u_lockouttime))
750                         return nt_status;
751         }
752
753         if (!pdb_set_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
754                                     r->logon_to_chgpass))
755                 return nt_status;
756
757         return NT_STATUS_OK;
758 }
759
760 /****************************************************************
761 ****************************************************************/
762
763 static NTSTATUS fetch_sam_entry(TALLOC_CTX *mem_ctx,
764                                 enum netr_SamDatabaseID database_id,
765                                 struct netr_DELTA_ENUM *r,
766                                 struct samsync_context *ctx)
767 {
768         NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
769
770         switch (r->delta_type) {
771         case NETR_DELTA_USER:
772                 status = fetch_account_info(mem_ctx,
773                                             r->delta_id_union.rid,
774                                             r->delta_union.user);
775                 break;
776         case NETR_DELTA_GROUP:
777                 status = fetch_group_info(mem_ctx,
778                                           r->delta_id_union.rid,
779                                           r->delta_union.group);
780                 break;
781         case NETR_DELTA_GROUP_MEMBER:
782                 status = fetch_group_mem_info(mem_ctx,
783                                               r->delta_id_union.rid,
784                                               r->delta_union.group_member);
785                 break;
786         case NETR_DELTA_ALIAS:
787                 status = fetch_alias_info(mem_ctx,
788                                           r->delta_id_union.rid,
789                                           r->delta_union.alias,
790                                           ctx->domain_sid);
791                 break;
792         case NETR_DELTA_ALIAS_MEMBER:
793                 status = fetch_alias_mem(mem_ctx,
794                                          r->delta_id_union.rid,
795                                          r->delta_union.alias_member,
796                                          ctx->domain_sid);
797                 break;
798         case NETR_DELTA_DOMAIN:
799                 status = fetch_domain_info(mem_ctx,
800                                            r->delta_id_union.rid,
801                                            r->delta_union.domain);
802                 break;
803         /* The following types are recognised but not handled */
804         case NETR_DELTA_RENAME_GROUP:
805                 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
806                 break;
807         case NETR_DELTA_RENAME_USER:
808                 d_printf("NETR_DELTA_RENAME_USER not handled\n");
809                 break;
810         case NETR_DELTA_RENAME_ALIAS:
811                 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
812                 break;
813         case NETR_DELTA_POLICY:
814                 d_printf("NETR_DELTA_POLICY not handled\n");
815                 break;
816         case NETR_DELTA_TRUSTED_DOMAIN:
817                 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
818                 break;
819         case NETR_DELTA_ACCOUNT:
820                 d_printf("NETR_DELTA_ACCOUNT not handled\n");
821                 break;
822         case NETR_DELTA_SECRET:
823                 d_printf("NETR_DELTA_SECRET not handled\n");
824                 break;
825         case NETR_DELTA_DELETE_GROUP:
826                 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
827                 break;
828         case NETR_DELTA_DELETE_USER:
829                 d_printf("NETR_DELTA_DELETE_USER not handled\n");
830                 break;
831         case NETR_DELTA_MODIFY_COUNT:
832                 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
833                 break;
834         case NETR_DELTA_DELETE_ALIAS:
835                 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
836                 break;
837         case NETR_DELTA_DELETE_TRUST:
838                 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
839                 break;
840         case NETR_DELTA_DELETE_ACCOUNT:
841                 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
842                 break;
843         case NETR_DELTA_DELETE_SECRET:
844                 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
845                 break;
846         case NETR_DELTA_DELETE_GROUP2:
847                 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
848                 break;
849         case NETR_DELTA_DELETE_USER2:
850                 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
851                 break;
852         default:
853                 d_printf("Unknown delta record type %d\n", r->delta_type);
854                 status = NT_STATUS_INVALID_PARAMETER;
855                 break;
856         }
857
858         return status;
859 }
860
861 /****************************************************************
862 ****************************************************************/
863
864 static NTSTATUS fetch_sam_entries(TALLOC_CTX *mem_ctx,
865                                   enum netr_SamDatabaseID database_id,
866                                   struct netr_DELTA_ENUM_ARRAY *r,
867                                   uint64_t *sequence_num,
868                                   struct samsync_context *ctx)
869 {
870         int i;
871
872         for (i = 0; i < r->num_deltas; i++) {
873                 fetch_sam_entry(mem_ctx, database_id, &r->delta_enum[i], ctx);
874         }
875
876         return NT_STATUS_OK;
877 }
878
879 /****************************************************************
880 ****************************************************************/
881
882 const struct samsync_ops libnet_samsync_passdb_ops = {
883         .process_objects        = fetch_sam_entries,
884 };