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