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