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