s3-dbwrap_ctdb: fix the build.
[obnox/samba/samba-obnox.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;
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         if (!pdb_getgrsid(&map, group_sid)) {
359                 DEBUG(0, ("Primary group of %s has no mapping!\n",
360                           pdb_get_username(sam_account)));
361         } else {
362                 if (map.gid != passwd->pw_gid) {
363                         if (!(grp = getgrgid(map.gid))) {
364                                 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
365                                           (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_tos(&group_sid)));
366                         } else {
367                                 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
368                         }
369                 }
370         }
371
372         if ( !passwd ) {
373                 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
374                         pdb_get_username(sam_account)));
375         }
376
377  done:
378         TALLOC_FREE(sam_account);
379         return nt_ret;
380 }
381
382 /****************************************************************
383 ****************************************************************/
384
385 static NTSTATUS fetch_group_info(TALLOC_CTX *mem_ctx,
386                                  uint32_t rid,
387                                  struct netr_DELTA_GROUP *r)
388 {
389         fstring name;
390         fstring comment;
391         struct group *grp = NULL;
392         struct dom_sid group_sid;
393         fstring sid_string;
394         GROUP_MAP map;
395         bool insert = true;
396
397         fstrcpy(name, r->group_name.string);
398         fstrcpy(comment, r->description.string);
399
400         /* add the group to the mapping table */
401         sid_compose(&group_sid, get_global_sam_sid(), rid);
402         sid_to_fstring(sid_string, &group_sid);
403
404         if (pdb_getgrsid(&map, group_sid)) {
405                 if ( map.gid != -1 )
406                         grp = getgrgid(map.gid);
407                 insert = false;
408         }
409
410         if (grp == NULL) {
411                 gid_t gid;
412
413                 /* No group found from mapping, find it from its name. */
414                 if ((grp = getgrnam(name)) == NULL) {
415
416                         /* No appropriate group found, create one */
417
418                         d_printf("Creating unix group: '%s'\n", name);
419
420                         if (smb_create_group(name, &gid) != 0)
421                                 return NT_STATUS_ACCESS_DENIED;
422
423                         if ((grp = getgrnam(name)) == NULL)
424                                 return NT_STATUS_ACCESS_DENIED;
425                 }
426         }
427
428         map.gid = grp->gr_gid;
429         map.sid = group_sid;
430         map.sid_name_use = SID_NAME_DOM_GRP;
431         strlcpy(map.nt_name, name, sizeof(map.nt_name));
432         if (r->description.string) {
433                 strlcpy(map.comment, comment, sizeof(map.comment));
434         } else {
435                 strlcpy(map.comment, "", sizeof(map.comment));
436         }
437
438         if (insert)
439                 pdb_add_group_mapping_entry(&map);
440         else
441                 pdb_update_group_mapping_entry(&map);
442
443         return NT_STATUS_OK;
444 }
445
446 /****************************************************************
447 ****************************************************************/
448
449 static NTSTATUS fetch_group_mem_info(TALLOC_CTX *mem_ctx,
450                                      uint32_t rid,
451                                      struct netr_DELTA_GROUP_MEMBER *r)
452 {
453         int i;
454         char **nt_members = NULL;
455         char **unix_members;
456         struct dom_sid group_sid;
457         GROUP_MAP map;
458         struct group *grp;
459
460         if (r->num_rids == 0) {
461                 return NT_STATUS_OK;
462         }
463
464         sid_compose(&group_sid, get_global_sam_sid(), rid);
465
466         if (!get_domain_group_from_sid(group_sid, &map)) {
467                 DEBUG(0, ("Could not find global group %d\n", rid));
468                 return NT_STATUS_NO_SUCH_GROUP;
469         }
470
471         if (!(grp = getgrgid(map.gid))) {
472                 DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid));
473                 return NT_STATUS_NO_SUCH_GROUP;
474         }
475
476         d_printf("Group members of %s: ", grp->gr_name);
477
478         if (r->num_rids) {
479                 if ((nt_members = talloc_zero_array(mem_ctx, char *, r->num_rids)) == NULL) {
480                         DEBUG(0, ("talloc failed\n"));
481                         return NT_STATUS_NO_MEMORY;
482                 }
483         } else {
484                 nt_members = NULL;
485         }
486
487         for (i=0; i < r->num_rids; i++) {
488                 struct samu *member = NULL;
489                 struct dom_sid member_sid;
490
491                 if ( !(member = samu_new(mem_ctx)) ) {
492                         return NT_STATUS_NO_MEMORY;
493                 }
494
495                 sid_compose(&member_sid, get_global_sam_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 struct dom_sid *dom_sid)
577 {
578         fstring name;
579         fstring comment;
580         struct group *grp = NULL;
581         struct 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_compose(&alias_sid, dom_sid, rid);
591         sid_to_fstring(sid_string, &alias_sid);
592
593         if (pdb_getgrsid(&map, alias_sid)) {
594                 grp = getgrgid(map.gid);
595                 insert = false;
596         }
597
598         if (grp == NULL) {
599                 gid_t gid;
600
601                 /* No group found from mapping, find it from its name. */
602                 if ((grp = getgrnam(name)) == NULL) {
603                         /* No appropriate group found, create one */
604                         d_printf("Creating unix group: '%s'\n", name);
605                         if (smb_create_group(name, &gid) != 0)
606                                 return NT_STATUS_ACCESS_DENIED;
607                         if ((grp = getgrgid(gid)) == NULL)
608                                 return NT_STATUS_ACCESS_DENIED;
609                 }
610         }
611
612         map.gid = grp->gr_gid;
613         map.sid = alias_sid;
614
615         if (dom_sid_equal(dom_sid, &global_sid_Builtin))
616                 map.sid_name_use = SID_NAME_WKN_GRP;
617         else
618                 map.sid_name_use = SID_NAME_ALIAS;
619
620         strlcpy(map.nt_name, name, sizeof(map.nt_name));
621         strlcpy(map.comment, comment, sizeof(map.comment));
622
623         if (insert)
624                 pdb_add_group_mapping_entry(&map);
625         else
626                 pdb_update_group_mapping_entry(&map);
627
628         return NT_STATUS_OK;
629 }
630
631 /****************************************************************
632 ****************************************************************/
633
634 static NTSTATUS fetch_alias_mem(TALLOC_CTX *mem_ctx,
635                                 uint32_t rid,
636                                 struct netr_DELTA_ALIAS_MEMBER *r,
637                                 const struct dom_sid *dom_sid)
638 {
639         return NT_STATUS_OK;
640 }
641
642 /****************************************************************
643 ****************************************************************/
644
645 static NTSTATUS fetch_domain_info(TALLOC_CTX *mem_ctx,
646                                   uint32_t rid,
647                                   struct netr_DELTA_DOMAIN *r)
648 {
649         time_t u_max_age, u_min_age, u_logout;
650         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
651         const char *domname;
652         struct netr_AcctLockStr *lockstr = NULL;
653         NTSTATUS status;
654
655         status = pull_netr_AcctLockStr(mem_ctx, &r->account_lockout,
656                                        &lockstr);
657         if (!NT_STATUS_IS_OK(status)) {
658                 d_printf("failed to pull account lockout string: %s\n",
659                         nt_errstr(status));
660         }
661
662         u_max_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->max_password_age);
663         u_min_age = uint64s_nt_time_to_unix_abs((uint64 *)&r->min_password_age);
664         u_logout = uint64s_nt_time_to_unix_abs((uint64 *)&r->force_logoff_time);
665
666         domname = r->domain_name.string;
667         if (!domname) {
668                 return NT_STATUS_NO_MEMORY;
669         }
670
671         /* we don't handle BUILTIN account policies */
672         if (!strequal(domname, get_global_sam_name())) {
673                 printf("skipping SAM_DOMAIN_INFO delta for '%s' (is not my domain)\n", domname);
674                 return NT_STATUS_OK;
675         }
676
677
678         if (!pdb_set_account_policy(PDB_POLICY_PASSWORD_HISTORY,
679                                     r->password_history_length))
680                 return nt_status;
681
682         if (!pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
683                                     r->min_password_length))
684                 return nt_status;
685
686         if (!pdb_set_account_policy(PDB_POLICY_MAX_PASSWORD_AGE,
687                                     (uint32)u_max_age))
688                 return nt_status;
689
690         if (!pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_AGE,
691                                     (uint32)u_min_age))
692                 return nt_status;
693
694         if (!pdb_set_account_policy(PDB_POLICY_TIME_TO_LOGOUT,
695                                     (uint32)u_logout))
696                 return nt_status;
697
698         if (lockstr) {
699                 time_t u_lockoutreset, u_lockouttime;
700
701                 u_lockoutreset = uint64s_nt_time_to_unix_abs(&lockstr->reset_count);
702                 u_lockouttime = uint64s_nt_time_to_unix_abs((uint64_t *)&lockstr->lockout_duration);
703
704                 if (!pdb_set_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT,
705                                             lockstr->bad_attempt_lockout))
706                         return nt_status;
707
708                 if (!pdb_set_account_policy(PDB_POLICY_RESET_COUNT_TIME,
709                                             (uint32_t)u_lockoutreset/60))
710                         return nt_status;
711
712                 if (u_lockouttime != -1)
713                         u_lockouttime /= 60;
714
715                 if (!pdb_set_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
716                                             (uint32_t)u_lockouttime))
717                         return nt_status;
718         }
719
720         if (!pdb_set_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
721                                     r->logon_to_chgpass))
722                 return nt_status;
723
724         return NT_STATUS_OK;
725 }
726
727 /****************************************************************
728 ****************************************************************/
729
730 static NTSTATUS fetch_sam_entry(TALLOC_CTX *mem_ctx,
731                                 enum netr_SamDatabaseID database_id,
732                                 struct netr_DELTA_ENUM *r,
733                                 struct samsync_context *ctx)
734 {
735         NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
736
737         switch (r->delta_type) {
738         case NETR_DELTA_USER:
739                 status = fetch_account_info(mem_ctx,
740                                             r->delta_id_union.rid,
741                                             r->delta_union.user);
742                 break;
743         case NETR_DELTA_GROUP:
744                 status = fetch_group_info(mem_ctx,
745                                           r->delta_id_union.rid,
746                                           r->delta_union.group);
747                 break;
748         case NETR_DELTA_GROUP_MEMBER:
749                 status = fetch_group_mem_info(mem_ctx,
750                                               r->delta_id_union.rid,
751                                               r->delta_union.group_member);
752                 break;
753         case NETR_DELTA_ALIAS:
754                 status = fetch_alias_info(mem_ctx,
755                                           r->delta_id_union.rid,
756                                           r->delta_union.alias,
757                                           ctx->domain_sid);
758                 break;
759         case NETR_DELTA_ALIAS_MEMBER:
760                 status = fetch_alias_mem(mem_ctx,
761                                          r->delta_id_union.rid,
762                                          r->delta_union.alias_member,
763                                          ctx->domain_sid);
764                 break;
765         case NETR_DELTA_DOMAIN:
766                 status = fetch_domain_info(mem_ctx,
767                                            r->delta_id_union.rid,
768                                            r->delta_union.domain);
769                 break;
770         /* The following types are recognised but not handled */
771         case NETR_DELTA_RENAME_GROUP:
772                 d_printf("NETR_DELTA_RENAME_GROUP not handled\n");
773                 break;
774         case NETR_DELTA_RENAME_USER:
775                 d_printf("NETR_DELTA_RENAME_USER not handled\n");
776                 break;
777         case NETR_DELTA_RENAME_ALIAS:
778                 d_printf("NETR_DELTA_RENAME_ALIAS not handled\n");
779                 break;
780         case NETR_DELTA_POLICY:
781                 d_printf("NETR_DELTA_POLICY not handled\n");
782                 break;
783         case NETR_DELTA_TRUSTED_DOMAIN:
784                 d_printf("NETR_DELTA_TRUSTED_DOMAIN not handled\n");
785                 break;
786         case NETR_DELTA_ACCOUNT:
787                 d_printf("NETR_DELTA_ACCOUNT not handled\n");
788                 break;
789         case NETR_DELTA_SECRET:
790                 d_printf("NETR_DELTA_SECRET not handled\n");
791                 break;
792         case NETR_DELTA_DELETE_GROUP:
793                 d_printf("NETR_DELTA_DELETE_GROUP not handled\n");
794                 break;
795         case NETR_DELTA_DELETE_USER:
796                 d_printf("NETR_DELTA_DELETE_USER not handled\n");
797                 break;
798         case NETR_DELTA_MODIFY_COUNT:
799                 d_printf("NETR_DELTA_MODIFY_COUNT not handled\n");
800                 break;
801         case NETR_DELTA_DELETE_ALIAS:
802                 d_printf("NETR_DELTA_DELETE_ALIAS not handled\n");
803                 break;
804         case NETR_DELTA_DELETE_TRUST:
805                 d_printf("NETR_DELTA_DELETE_TRUST not handled\n");
806                 break;
807         case NETR_DELTA_DELETE_ACCOUNT:
808                 d_printf("NETR_DELTA_DELETE_ACCOUNT not handled\n");
809                 break;
810         case NETR_DELTA_DELETE_SECRET:
811                 d_printf("NETR_DELTA_DELETE_SECRET not handled\n");
812                 break;
813         case NETR_DELTA_DELETE_GROUP2:
814                 d_printf("NETR_DELTA_DELETE_GROUP2 not handled\n");
815                 break;
816         case NETR_DELTA_DELETE_USER2:
817                 d_printf("NETR_DELTA_DELETE_USER2 not handled\n");
818                 break;
819         default:
820                 d_printf("Unknown delta record type %d\n", r->delta_type);
821                 status = NT_STATUS_INVALID_PARAMETER;
822                 break;
823         }
824
825         return status;
826 }
827
828 /****************************************************************
829 ****************************************************************/
830
831 static NTSTATUS fetch_sam_entries(TALLOC_CTX *mem_ctx,
832                                   enum netr_SamDatabaseID database_id,
833                                   struct netr_DELTA_ENUM_ARRAY *r,
834                                   uint64_t *sequence_num,
835                                   struct samsync_context *ctx)
836 {
837         int i;
838
839         for (i = 0; i < r->num_deltas; i++) {
840                 fetch_sam_entry(mem_ctx, database_id, &r->delta_enum[i], ctx);
841         }
842
843         return NT_STATUS_OK;
844 }
845
846 /****************************************************************
847 ****************************************************************/
848
849 const struct samsync_ops libnet_samsync_passdb_ops = {
850         .process_objects        = fetch_sam_entries,
851 };