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