s3-passdb: add smb_create_user().
[ira/wip.git] / source3 / libnet / libnet_samsync_passdb.c
1 /*
2    Unix SMB/CIFS implementation.
3    dump the remote SAM using rpc samsync operations
4
5    Copyright (C) Andrew Tridgell 2002
6    Copyright (C) Tim Potter 2001,2002
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
8    Modified by Volker Lendecke 2002
9    Copyright (C) Jeremy Allison 2005.
10    Copyright (C) Guenther Deschner 2008.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "libnet/libnet.h"
28
29 /* Convert a struct samu_DELTA to a struct samu. */
30 #define STRING_CHANGED (old_string && !new_string) ||\
31                     (!old_string && new_string) ||\
32                 (old_string && new_string && (strcmp(old_string, new_string) != 0))
33
34 #define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\
35                     (!(s1) && (s2)) ||\
36                 ((s1) && (s2) && (strcmp((s1), (s2)) != 0))
37
38 /****************************************************************
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 static NTSTATUS fetch_account_info(TALLOC_CTX *mem_ctx,
233                                    uint32_t rid,
234                                    struct netr_DELTA_USER *r)
235 {
236
237         NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL;
238         fstring account;
239         char *add_script = NULL;
240         struct samu *sam_account=NULL;
241         GROUP_MAP map;
242         struct group *grp;
243         DOM_SID user_sid;
244         DOM_SID group_sid;
245         struct passwd *passwd;
246         fstring sid_string;
247
248         fstrcpy(account, r->account_name.string);
249         d_printf("Creating account: %s\n", account);
250
251         if ( !(sam_account = samu_new(mem_ctx)) ) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         if (!(passwd = Get_Pwnam_alloc(sam_account, account))) {
256                 /* Create appropriate user */
257                 if (r->acct_flags & ACB_NORMAL) {
258                         add_script = talloc_strdup(sam_account,
259                                         lp_adduser_script());
260                 } else if ( (r->acct_flags & ACB_WSTRUST) ||
261                             (r->acct_flags & ACB_SVRTRUST) ||
262                             (r->acct_flags & ACB_DOMTRUST) ) {
263                         add_script = talloc_strdup(sam_account,
264                                         lp_addmachine_script());
265                 } else {
266                         DEBUG(1, ("Unknown user type: %s\n",
267                                   pdb_encode_acct_ctrl(r->acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
268                         nt_ret = NT_STATUS_UNSUCCESSFUL;
269                         goto done;
270                 }
271                 if (!add_script) {
272                         nt_ret = NT_STATUS_NO_MEMORY;
273                         goto done;
274                 }
275                 if (*add_script) {
276                         int add_ret;
277                         add_script = talloc_all_string_sub(sam_account,
278                                         add_script,
279                                         "%u",
280                                         account);
281                         if (!add_script) {
282                                 nt_ret = NT_STATUS_NO_MEMORY;
283                                 goto done;
284                         }
285                         add_ret = smbrun(add_script,NULL);
286                         DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
287                                  "gave %d\n", add_script, add_ret));
288                         if (add_ret == 0) {
289                                 smb_nscd_flush_user_cache();
290                         }
291                 }
292
293                 /* try and find the possible unix account again */
294                 if ( !(passwd = Get_Pwnam_alloc(sam_account, account)) ) {
295                         d_fprintf(stderr, "Could not create posix account info for '%s'\n", account);
296                         nt_ret = NT_STATUS_NO_SUCH_USER;
297                         goto done;
298                 }
299         }
300
301         sid_copy(&user_sid, get_global_sam_sid());
302         sid_append_rid(&user_sid, r->rid);
303
304         DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n",
305                   sid_to_fstring(sid_string, &user_sid), account));
306         if (!pdb_getsampwsid(sam_account, &user_sid)) {
307                 sam_account_from_delta(sam_account, r);
308                 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
309                           sid_to_fstring(sid_string, &user_sid),
310                           pdb_get_username(sam_account)));
311                 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
312                         DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
313                                   account));
314                         return NT_STATUS_ACCESS_DENIED;
315                 }
316         } else {
317                 sam_account_from_delta(sam_account, r);
318                 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
319                           sid_to_fstring(sid_string, &user_sid),
320                           pdb_get_username(sam_account)));
321                 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
322                         DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
323                                   account));
324                         TALLOC_FREE(sam_account);
325                         return NT_STATUS_ACCESS_DENIED;
326                 }
327         }
328
329         if (pdb_get_group_sid(sam_account) == NULL) {
330                 return NT_STATUS_UNSUCCESSFUL;
331         }
332
333         group_sid = *pdb_get_group_sid(sam_account);
334
335         if (!pdb_getgrsid(&map, group_sid)) {
336                 DEBUG(0, ("Primary group of %s has no mapping!\n",
337                           pdb_get_username(sam_account)));
338         } else {
339                 if (map.gid != passwd->pw_gid) {
340                         if (!(grp = getgrgid(map.gid))) {
341                                 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
342                                           (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_tos(&group_sid)));
343                         } else {
344                                 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
345                         }
346                 }
347         }
348
349         if ( !passwd ) {
350                 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
351                         pdb_get_username(sam_account)));
352         }
353
354  done:
355         TALLOC_FREE(sam_account);
356         return nt_ret;
357 }
358
359 /****************************************************************
360 ****************************************************************/
361
362 static NTSTATUS fetch_group_info(TALLOC_CTX *mem_ctx,
363                                  uint32_t rid,
364                                  struct netr_DELTA_GROUP *r)
365 {
366         fstring name;
367         fstring comment;
368         struct group *grp = NULL;
369         DOM_SID group_sid;
370         fstring sid_string;
371         GROUP_MAP map;
372         bool insert = true;
373
374         fstrcpy(name, r->group_name.string);
375         fstrcpy(comment, r->description.string);
376
377         /* add the group to the mapping table */
378         sid_copy(&group_sid, get_global_sam_sid());
379         sid_append_rid(&group_sid, rid);
380         sid_to_fstring(sid_string, &group_sid);
381
382         if (pdb_getgrsid(&map, group_sid)) {
383                 if ( map.gid != -1 )
384                         grp = getgrgid(map.gid);
385                 insert = false;
386         }
387
388         if (grp == NULL) {
389                 gid_t gid;
390
391                 /* No group found from mapping, find it from its name. */
392                 if ((grp = getgrnam(name)) == NULL) {
393
394                         /* No appropriate group found, create one */
395
396                         d_printf("Creating unix group: '%s'\n", name);
397
398                         if (smb_create_group(name, &gid) != 0)
399                                 return NT_STATUS_ACCESS_DENIED;
400
401                         if ((grp = getgrnam(name)) == NULL)
402                                 return NT_STATUS_ACCESS_DENIED;
403                 }
404         }
405
406         map.gid = grp->gr_gid;
407         map.sid = group_sid;
408         map.sid_name_use = SID_NAME_DOM_GRP;
409         fstrcpy(map.nt_name, name);
410         if (r->description.string) {
411                 fstrcpy(map.comment, comment);
412         } else {
413                 fstrcpy(map.comment, "");
414         }
415
416         if (insert)
417                 pdb_add_group_mapping_entry(&map);
418         else
419                 pdb_update_group_mapping_entry(&map);
420
421         return NT_STATUS_OK;
422 }
423
424 /****************************************************************
425 ****************************************************************/
426
427 static NTSTATUS fetch_group_mem_info(TALLOC_CTX *mem_ctx,
428                                      uint32_t rid,
429                                      struct netr_DELTA_GROUP_MEMBER *r)
430 {
431         int i;
432         char **nt_members = NULL;
433         char **unix_members;
434         DOM_SID group_sid;
435         GROUP_MAP map;
436         struct group *grp;
437
438         if (r->num_rids == 0) {
439                 return NT_STATUS_OK;
440         }
441
442         sid_copy(&group_sid, get_global_sam_sid());
443         sid_append_rid(&group_sid, rid);
444
445         if (!get_domain_group_from_sid(group_sid, &map)) {
446                 DEBUG(0, ("Could not find global group %d\n", rid));
447                 return NT_STATUS_NO_SUCH_GROUP;
448         }
449
450         if (!(grp = getgrgid(map.gid))) {
451                 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid));
452                 return NT_STATUS_NO_SUCH_GROUP;
453         }
454
455         d_printf("Group members of %s: ", grp->gr_name);
456
457         if (r->num_rids) {
458                 if ((nt_members = TALLOC_ZERO_ARRAY(mem_ctx, char *, r->num_rids)) == NULL) {
459                         DEBUG(0, ("talloc failed\n"));
460                         return NT_STATUS_NO_MEMORY;
461                 }
462         } else {
463                 nt_members = NULL;
464         }
465
466         for (i=0; i < r->num_rids; i++) {
467                 struct samu *member = NULL;
468                 DOM_SID member_sid;
469
470                 if ( !(member = samu_new(mem_ctx)) ) {
471                         return NT_STATUS_NO_MEMORY;
472                 }
473
474                 sid_copy(&member_sid, get_global_sam_sid());
475                 sid_append_rid(&member_sid, r->rids[i]);
476
477                 if (!pdb_getsampwsid(member, &member_sid)) {
478                         DEBUG(1, ("Found bogus group member: %d (member_sid=%s group=%s)\n",
479                                   r->rids[i], sid_string_tos(&member_sid), grp->gr_name));
480                         TALLOC_FREE(member);
481                         continue;
482                 }
483
484                 if (pdb_get_group_rid(member) == rid) {
485                         d_printf("%s(primary),", pdb_get_username(member));
486                         TALLOC_FREE(member);
487                         continue;
488                 }
489
490                 d_printf("%s,", pdb_get_username(member));
491                 nt_members[i] = talloc_strdup(mem_ctx, pdb_get_username(member));
492                 TALLOC_FREE(member);
493         }
494
495         d_printf("\n");
496
497         unix_members = grp->gr_mem;
498
499         while (*unix_members) {
500                 bool is_nt_member = false;
501                 for (i=0; i < r->num_rids; i++) {
502                         if (nt_members[i] == NULL) {
503                                 /* This was a primary group */
504                                 continue;
505                         }
506
507                         if (strcmp(*unix_members, nt_members[i]) == 0) {
508                                 is_nt_member = true;
509                                 break;
510                         }
511                 }
512                 if (!is_nt_member) {
513                         /* We look at a unix group member that is not
514                            an nt group member. So, remove it. NT is
515                            boss here. */
516                         smb_delete_user_group(grp->gr_name, *unix_members);
517                 }
518                 unix_members += 1;
519         }
520
521         for (i=0; i < r->num_rids; i++) {
522                 bool is_unix_member = false;
523
524                 if (nt_members[i] == NULL) {
525                         /* This was the primary group */
526                         continue;
527                 }
528
529                 unix_members = grp->gr_mem;
530
531                 while (*unix_members) {
532                         if (strcmp(*unix_members, nt_members[i]) == 0) {
533                                 is_unix_member = true;
534                                 break;
535                         }
536                         unix_members += 1;
537                 }
538
539                 if (!is_unix_member) {
540                         /* We look at a nt group member that is not a
541                            unix group member currently. So, add the nt
542                            group member. */
543                         smb_add_user_group(grp->gr_name, nt_members[i]);
544                 }
545         }
546
547         return NT_STATUS_OK;
548 }
549
550 /****************************************************************
551 ****************************************************************/
552
553 static NTSTATUS fetch_alias_info(TALLOC_CTX *mem_ctx,
554                                  uint32_t rid,
555                                  struct netr_DELTA_ALIAS *r,
556                                  const DOM_SID *dom_sid)
557 {
558         fstring name;
559         fstring comment;
560         struct group *grp = NULL;
561         DOM_SID alias_sid;
562         fstring sid_string;
563         GROUP_MAP map;
564         bool insert = true;
565
566         fstrcpy(name, r->alias_name.string);
567         fstrcpy(comment, r->description.string);
568
569         /* Find out whether the group is already mapped */
570         sid_copy(&alias_sid, dom_sid);
571         sid_append_rid(&alias_sid, rid);
572         sid_to_fstring(sid_string, &alias_sid);
573
574         if (pdb_getgrsid(&map, alias_sid)) {
575                 grp = getgrgid(map.gid);
576                 insert = false;
577         }
578
579         if (grp == NULL) {
580                 gid_t gid;
581
582                 /* No group found from mapping, find it from its name. */
583                 if ((grp = getgrnam(name)) == NULL) {
584                         /* No appropriate group found, create one */
585                         d_printf("Creating unix group: '%s'\n", name);
586                         if (smb_create_group(name, &gid) != 0)
587                                 return NT_STATUS_ACCESS_DENIED;
588                         if ((grp = getgrgid(gid)) == NULL)
589                                 return NT_STATUS_ACCESS_DENIED;
590                 }
591         }
592
593         map.gid = grp->gr_gid;
594         map.sid = alias_sid;
595
596         if (sid_equal(dom_sid, &global_sid_Builtin))
597                 map.sid_name_use = SID_NAME_WKN_GRP;
598         else
599                 map.sid_name_use = SID_NAME_ALIAS;
600
601         fstrcpy(map.nt_name, name);
602         fstrcpy(map.comment, comment);
603
604         if (insert)
605                 pdb_add_group_mapping_entry(&map);
606         else
607                 pdb_update_group_mapping_entry(&map);
608
609         return NT_STATUS_OK;
610 }
611
612 /****************************************************************
613 ****************************************************************/
614
615 static NTSTATUS fetch_alias_mem(TALLOC_CTX *mem_ctx,
616                                 uint32_t rid,
617                                 struct netr_DELTA_ALIAS_MEMBER *r,
618                                 const DOM_SID *dom_sid)
619 {
620         return NT_STATUS_OK;
621 }
622
623 /****************************************************************
624 ****************************************************************/
625
626 static NTSTATUS fetch_domain_info(TALLOC_CTX *mem_ctx,
627                                   uint32_t rid,
628                                   struct netr_DELTA_DOMAIN *r)
629 {
630         time_t u_max_age, u_min_age, u_logout;
631         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
632         const char *domname;
633         struct netr_AcctLockStr *lockstr = NULL;
634         NTSTATUS status;
635
636         status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
637                                        &lockstr);
638         if (!NT_STATUS_IS_OK(status)) {
639                 d_printf("failed to pull account lockout string: %s\n",
640                         nt_errstr(status));
641         }
642
643         u_max_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->max_password_age);
644         u_min_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->min_password_age);
645         u_logout = uint64s_nt_time_to_unix_abs((uint64 *)&r->force_logoff_time);
646
647         domname = r->domain_name.string;
648         if (!domname) {
649                 return NT_STATUS_NO_MEMORY;
650         }
651
652         /* we don't handle BUILTIN account policies */
653         if (!strequal(domname, get_global_sam_name())) {
654                 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
655                 return NT_STATUS_OK;
656         }
657
658
659         if (!pdb_set_account_policy(AP_PASSWORD_HISTORY,
660                                     r->password_history_length))
661                 return nt_status;
662
663         if (!pdb_set_account_policy(AP_MIN_PASSWORD_LEN,
664                                     r->min_password_length))
665                 return nt_status;
666
667         if (!pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (uint32)u_max_age))
668                 return nt_status;
669
670         if (!pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (uint32)u_min_age))
671                 return nt_status;
672
673         if (!pdb_set_account_policy(AP_TIME_TO_LOGOUT, (uint32)u_logout))
674                 return nt_status;
675
676         if (lockstr) {
677                 time_t u_lockoutreset, u_lockouttime;
678
679                 u_lockoutreset = uint64s_nt_time_to_unix_abs(&lockstr->reset_count);
680                 u_lockouttime = uint64s_nt_time_to_unix_abs((uint64_t *)&lockstr->lockout_duration);
681
682                 if (!pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
683                                             lockstr->bad_attempt_lockout))
684                         return nt_status;
685
686                 if (!pdb_set_account_policy(AP_RESET_COUNT_TIME, (uint32_t)u_lockoutreset/60))
687                         return nt_status;
688
689                 if (u_lockouttime != -1)
690                         u_lockouttime /= 60;
691
692                 if (!pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (uint32_t)u_lockouttime))
693                         return nt_status;
694         }
695
696         if (!pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
697                                     r->logon_to_chgpass))
698                 return nt_status;
699
700         return NT_STATUS_OK;
701 }
702
703 /****************************************************************
704 ****************************************************************/
705
706 static NTSTATUS fetch_sam_entry(TALLOC_CTX *mem_ctx,
707                                 enum netr_SamDatabaseID database_id,
708                                 struct netr_DELTA_ENUM *r,
709                                 struct samsync_context *ctx)
710 {
711         switch(r->delta_type) {
712         case NETR_DELTA_USER:
713                 fetch_account_info(mem_ctx,
714                                    r->delta_id_union.rid,
715                                    r->delta_union.user);
716                 break;
717         case NETR_DELTA_GROUP:
718                 fetch_group_info(mem_ctx,
719                                  r->delta_id_union.rid,
720                                  r->delta_union.group);
721                 break;
722         case NETR_DELTA_GROUP_MEMBER:
723                 fetch_group_mem_info(mem_ctx,
724                                      r->delta_id_union.rid,
725                                      r->delta_union.group_member);
726                 break;
727         case NETR_DELTA_ALIAS:
728                 fetch_alias_info(mem_ctx,
729                                  r->delta_id_union.rid,
730                                  r->delta_union.alias,
731                                  ctx->domain_sid);
732                 break;
733         case NETR_DELTA_ALIAS_MEMBER:
734                 fetch_alias_mem(mem_ctx,
735                                 r->delta_id_union.rid,
736                                 r->delta_union.alias_member,
737                                 ctx->domain_sid);
738                 break;
739         case NETR_DELTA_DOMAIN:
740                 fetch_domain_info(mem_ctx,
741                                   r->delta_id_union.rid,
742                                   r->delta_union.domain);
743                 break;
744         /* The following types are recognised but not handled */
745         case NETR_DELTA_RENAME_GROUP:
746                 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
747                 break;
748         case NETR_DELTA_RENAME_USER:
749                 d_printf("NETR_DELTA_RENAME_USER not handled\n");
750                 break;
751         case NETR_DELTA_RENAME_ALIAS:
752                 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
753                 break;
754         case NETR_DELTA_POLICY:
755                 d_printf("NETR_DELTA_POLICY not handled\n");
756                 break;
757         case NETR_DELTA_TRUSTED_DOMAIN:
758                 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
759                 break;
760         case NETR_DELTA_ACCOUNT:
761                 d_printf("NETR_DELTA_ACCOUNT not handled\n");
762                 break;
763         case NETR_DELTA_SECRET:
764                 d_printf("NETR_DELTA_SECRET not handled\n");
765                 break;
766         case NETR_DELTA_DELETE_GROUP:
767                 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
768                 break;
769         case NETR_DELTA_DELETE_USER:
770                 d_printf("NETR_DELTA_DELETE_USER not handled\n");
771                 break;
772         case NETR_DELTA_MODIFY_COUNT:
773                 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
774                 break;
775         case NETR_DELTA_DELETE_ALIAS:
776                 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
777                 break;
778         case NETR_DELTA_DELETE_TRUST:
779                 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
780                 break;
781         case NETR_DELTA_DELETE_ACCOUNT:
782                 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
783                 break;
784         case NETR_DELTA_DELETE_SECRET:
785                 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
786                 break;
787         case NETR_DELTA_DELETE_GROUP2:
788                 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
789                 break;
790         case NETR_DELTA_DELETE_USER2:
791                 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
792                 break;
793         default:
794                 d_printf("Unknown delta record type %d\n", r->delta_type);
795                 break;
796         }
797
798         return NT_STATUS_OK;
799 }
800
801 /****************************************************************
802 ****************************************************************/
803
804 static NTSTATUS fetch_sam_entries(TALLOC_CTX *mem_ctx,
805                                   enum netr_SamDatabaseID database_id,
806                                   struct netr_DELTA_ENUM_ARRAY *r,
807                                   uint64_t *sequence_num,
808                                   struct samsync_context *ctx)
809 {
810         int i;
811
812         for (i = 0; i < r->num_deltas; i++) {
813                 fetch_sam_entry(mem_ctx, database_id, &r->delta_enum[i], ctx);
814         }
815
816         return NT_STATUS_OK;
817 }
818
819 /****************************************************************
820 ****************************************************************/
821
822 const struct samsync_ops libnet_samsync_passdb_ops = {
823         .process_objects        = fetch_sam_entries,
824 };