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