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