r4925: Migrate Account Policies to passdb (esp. replicating ldapsam).
[vlendec/samba-autobuild/.git] / source3 / utils / pdbedit.c
1 /* 
2    Unix SMB/CIFS implementation.
3    passdb editing frontend
4    
5    Copyright (C) Simo Sorce      2000
6    Copyright (C) Andrew Bartlett 2001   
7    Copyright (C) Jelmer Vernooij 2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 #define BIT_BACKEND     0x00000004
27 #define BIT_VERBOSE     0x00000008
28 #define BIT_SPSTYLE     0x00000010
29 #define BIT_CAN_CHANGE  0x00000020
30 #define BIT_MUST_CHANGE 0x00000040
31 #define BIT_RESERV_3    0x00000080
32 #define BIT_FULLNAME    0x00000100
33 #define BIT_HOMEDIR     0x00000200
34 #define BIT_HDIRDRIVE   0x00000400
35 #define BIT_LOGSCRIPT   0x00000800
36 #define BIT_PROFILE     0x00001000
37 #define BIT_MACHINE     0x00002000
38 #define BIT_RESERV_4    0x00004000
39 #define BIT_USER        0x00008000
40 #define BIT_LIST        0x00010000
41 #define BIT_MODIFY      0x00020000
42 #define BIT_CREATE      0x00040000
43 #define BIT_DELETE      0x00080000
44 #define BIT_ACCPOLICY   0x00100000
45 #define BIT_ACCPOLVAL   0x00200000
46 #define BIT_ACCTCTRL    0x00400000
47 #define BIT_RESERV_7    0x00800000
48 #define BIT_IMPORT      0x01000000
49 #define BIT_EXPORT      0x02000000
50 #define BIT_FIX_INIT    0x04000000
51 #define BIT_BADPWRESET  0x08000000
52 #define BIT_LOGONHOURS  0x10000000
53
54 #define MASK_ALWAYS_GOOD        0x0000001F
55 #define MASK_USER_GOOD          0x00401F60
56
57 /*********************************************************
58  Add all currently available users to another db
59  ********************************************************/
60
61 static int export_database (struct pdb_context *in, struct pdb_context
62                             *out, const char *username) {
63         SAM_ACCOUNT *user = NULL;
64
65         DEBUG(3, ("called with username=\"%s\"\n", username));
66
67         if (NT_STATUS_IS_ERR(in->pdb_setsampwent(in, 0, 0))) {
68                 fprintf(stderr, "Can't sampwent!\n");
69                 return 1;
70         }
71
72         if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
73                 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
74                 return 1;
75         }
76
77         while (NT_STATUS_IS_OK(in->pdb_getsampwent(in, user))) {
78                 DEBUG(4, ("Processing account %s\n",
79                           user->private.username));
80                 if (!username || 
81                     (strcmp(username, user->private.username)
82                      == 0)) {
83                         out->pdb_add_sam_account(out, user);
84                         if (!NT_STATUS_IS_OK(pdb_reset_sam(user))) {
85                                 fprintf(stderr,
86                                         "Can't reset SAM_ACCOUNT!\n");
87                                 return 1;
88                         }
89                 }
90         }
91
92         in->pdb_endsampwent(in);
93
94         return 0;
95 }
96
97 /*********************************************************
98  Add all currently available group mappings to another db
99  ********************************************************/
100
101 static int export_groups (struct pdb_context *in, struct pdb_context *out) {
102         GROUP_MAP *maps = NULL;
103         int i, entries = 0;
104
105         if (NT_STATUS_IS_ERR(in->pdb_enum_group_mapping(in, SID_NAME_UNKNOWN,
106                                                         &maps, &entries,
107                                                         False))) {
108                 fprintf(stderr, "Can't get group mappings!\n");
109                 return 1;
110         }
111
112         for (i=0; i<entries; i++) {
113                 out->pdb_add_group_mapping_entry(out, &(maps[i]));
114         }
115
116         SAFE_FREE(maps);
117
118         return 0;
119 }
120
121 /*********************************************************
122  Add all currently available account policy from tdb to one backend
123  ********************************************************/
124
125 static int export_account_policies (struct pdb_context *in, struct pdb_context *out) 
126 {
127         int i;
128
129         for (i=1; decode_account_policy_name(i) != NULL; i++) {
130                 uint32 policy_value;
131                 if (NT_STATUS_IS_ERR(in->pdb_get_account_policy(in, i, &policy_value))) {
132                         fprintf(stderr, "Can't get account policy from tdb\n");
133                         return -1;
134                 }
135                 out->pdb_set_account_policy(out, i, policy_value);
136         }
137
138         return 0;
139 }
140
141
142 /*********************************************************
143  Print info from sam structure
144 **********************************************************/
145
146 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
147 {
148         uid_t uid;
149         time_t tmp;
150
151         /* TODO: chaeck if entry is a user or a workstation */
152         if (!sam_pwent) return -1;
153         
154         if (verbosity) {
155                 pstring temp;
156                 const uint8 *hours;
157                 
158                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
159                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
160                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
161                 printf ("User SID:             %s\n",
162                         sid_string_static(pdb_get_user_sid(sam_pwent)));
163                 printf ("Primary Group SID:    %s\n",
164                         sid_string_static(pdb_get_group_sid(sam_pwent)));
165                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
166                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
167                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
168                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
169                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
170                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
171                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
172                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
173                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
174                 
175                 tmp = pdb_get_logon_time(sam_pwent);
176                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
177                 
178                 tmp = pdb_get_logoff_time(sam_pwent);
179                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
180                 
181                 tmp = pdb_get_kickoff_time(sam_pwent);
182                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
183                 
184                 tmp = pdb_get_pass_last_set_time(sam_pwent);
185                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
186                 
187                 tmp = pdb_get_pass_can_change_time(sam_pwent);
188                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
189                 
190                 tmp = pdb_get_pass_must_change_time(sam_pwent);
191                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
192
193                 tmp = pdb_get_bad_password_time(sam_pwent);
194                 printf ("Last bad password   : %s\n", tmp ? http_timestring(tmp) : "0");
195                 printf ("Bad password count  : %d\n", 
196                         pdb_get_bad_password_count(sam_pwent));
197                 
198                 hours = pdb_get_hours(sam_pwent);
199                 pdb_sethexhours(temp, (const char *)hours);
200                 printf ("Logon hours         : %s\n", temp);
201                 
202         } else if (smbpwdstyle) {
203                 char lm_passwd[33];
204                 char nt_passwd[33];
205
206                 uid = nametouid(pdb_get_username(sam_pwent));
207                 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
208                 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
209                         
210                 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
211                        pdb_get_username(sam_pwent),
212                        (unsigned long)uid,
213                        lm_passwd,
214                        nt_passwd,
215                        pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
216                        (uint32)pdb_get_pass_last_set_time(sam_pwent));
217         } else {
218                 uid = nametouid(pdb_get_username(sam_pwent));
219                 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid, 
220                         pdb_get_fullname(sam_pwent));
221         }
222
223         return 0;       
224 }
225
226 /*********************************************************
227  Get an Print User Info
228 **********************************************************/
229
230 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
231 {
232         SAM_ACCOUNT *sam_pwent=NULL;
233         BOOL ret;
234
235         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
236                 return -1;
237         }
238
239         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
240
241         if (ret==False) {
242                 fprintf (stderr, "Username not found!\n");
243                 pdb_free_sam(&sam_pwent);
244                 return -1;
245         }
246
247         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
248         pdb_free_sam(&sam_pwent);
249         
250         return ret;
251 }
252         
253 /*********************************************************
254  List Users
255 **********************************************************/
256 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
257 {
258         SAM_ACCOUNT *sam_pwent=NULL;
259         BOOL check, ret;
260         
261         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False, 0));
262         if (!check) {
263                 return 1;
264         }
265
266         check = True;
267         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
268
269         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
270                 if (verbosity)
271                         printf ("---------------\n");
272                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
273                 pdb_free_sam(&sam_pwent);
274                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
275         }
276         if (check) pdb_free_sam(&sam_pwent);
277         
278         in->pdb_endsampwent(in);
279         return 0;
280 }
281
282 /*********************************************************
283  Fix a list of Users for uninitialised passwords
284 **********************************************************/
285 static int fix_users_list (struct pdb_context *in)
286 {
287         SAM_ACCOUNT *sam_pwent=NULL;
288         BOOL check, ret;
289         
290         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False, 0));
291         if (!check) {
292                 return 1;
293         }
294
295         check = True;
296         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
297
298         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
299                 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
300         
301                 if (!pdb_update_sam_account(sam_pwent)) {
302                         printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
303                 }
304                 pdb_free_sam(&sam_pwent);
305                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
306                 if (!check) {
307                         fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
308                 }
309                         
310         }
311         if (check) pdb_free_sam(&sam_pwent);
312         
313         in->pdb_endsampwent(in);
314         return 0;
315 }
316
317 /*********************************************************
318  Set User Info
319 **********************************************************/
320
321 static int set_user_info (struct pdb_context *in, const char *username, 
322                           const char *fullname, const char *homedir, 
323                           const char *drive, const char *script, 
324                           const char *profile, const char *account_control,
325                           const char *user_sid, const char *group_sid,
326                           const BOOL badpw, const BOOL hours,
327                           time_t pwd_can_change, time_t pwd_must_change)
328 {
329         BOOL updated_autolock = False, updated_badpw = False;
330         SAM_ACCOUNT *sam_pwent=NULL;
331         BOOL ret;
332         
333         pdb_init_sam(&sam_pwent);
334         
335         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
336         if (ret==False) {
337                 fprintf (stderr, "Username not found!\n");
338                 pdb_free_sam(&sam_pwent);
339                 return -1;
340         }
341
342         if (hours) {
343                 uint8 hours_array[MAX_HOURS_LEN];
344                 uint32 hours_len;
345                 
346                 hours_len = pdb_get_hours_len(sam_pwent);
347                 memset(hours_array, 0xff, hours_len);
348                 
349                 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
350         }
351
352         if (pwd_can_change != -1) {
353                 pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED);
354         }
355
356         if (pwd_must_change != -1) {
357                 pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED);
358         }
359
360         if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
361                 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
362         }
363
364         if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
365                 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
366         }
367
368         if (fullname)
369                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
370         if (homedir)
371                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
372         if (drive)
373                 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
374         if (script)
375                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
376         if (profile)
377                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
378
379         if (account_control) {
380                 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
381                                         ACB_PWNOEXP|ACB_AUTOLOCK);
382
383                 uint16 newflag = pdb_decode_acct_ctrl(account_control);
384
385                 if (newflag & not_settable) {
386                         fprintf(stderr, "Can only set [NDHLX] flags\n");
387                         pdb_free_sam(&sam_pwent);
388                         return -1;
389                 }
390
391                 pdb_set_acct_ctrl(sam_pwent,
392                                   (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
393                                   PDB_CHANGED);
394         }
395         if (user_sid) {
396                 DOM_SID u_sid;
397                 if (!string_to_sid(&u_sid, user_sid)) {
398                         /* not a complete sid, may be a RID, try building a SID */
399                         int u_rid;
400                         
401                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
402                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
403                                 return -1;
404                         }
405                         sid_copy(&u_sid, get_global_sam_sid());
406                         sid_append_rid(&u_sid, u_rid);
407                 }
408                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
409         }
410         if (group_sid) {
411                 DOM_SID g_sid;
412                 if (!string_to_sid(&g_sid, group_sid)) {
413                         /* not a complete sid, may be a RID, try building a SID */
414                         int g_rid;
415                         
416                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
417                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
418                                 return -1;
419                         }
420                         sid_copy(&g_sid, get_global_sam_sid());
421                         sid_append_rid(&g_sid, g_rid);
422                 }
423                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
424         }
425
426         if (badpw) {
427                 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
428                 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
429         }
430
431         if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
432                 print_user_info (in, username, True, False);
433         else {
434                 fprintf (stderr, "Unable to modify entry!\n");
435                 pdb_free_sam(&sam_pwent);
436                 return -1;
437         }
438         pdb_free_sam(&sam_pwent);
439         return 0;
440 }
441
442 /*********************************************************
443  Add New User
444 **********************************************************/
445 static int new_user (struct pdb_context *in, const char *username,
446                         const char *fullname, const char *homedir,
447                         const char *drive, const char *script,
448                         const char *profile, char *user_sid, char *group_sid)
449 {
450         SAM_ACCOUNT *sam_pwent=NULL;
451         NTSTATUS nt_status;
452         char *password1, *password2, *staticpass;
453         
454         get_global_sam_sid();
455
456         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_new(&sam_pwent, username, 0))) {
457                 DEBUG(0, ("could not create account to add new user %s\n", username));
458                 return -1;
459         }
460
461         staticpass = getpass("new password:");
462         password1 = SMB_STRDUP(staticpass);
463         memset(staticpass, 0, strlen(staticpass));
464         staticpass = getpass("retype new password:");
465         password2 = SMB_STRDUP(staticpass);
466         memset(staticpass, 0, strlen(staticpass));
467         if (strcmp (password1, password2)) {
468                 fprintf (stderr, "Passwords does not match!\n");
469                 memset(password1, 0, strlen(password1));
470                 SAFE_FREE(password1);
471                 memset(password2, 0, strlen(password2));
472                 SAFE_FREE(password2);
473                 pdb_free_sam (&sam_pwent);
474                 return -1;
475         }
476
477         pdb_set_plaintext_passwd(sam_pwent, password1);
478         memset(password1, 0, strlen(password1));
479         SAFE_FREE(password1);
480         memset(password2, 0, strlen(password2));
481         SAFE_FREE(password2);
482
483         if (fullname)
484                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
485         if (homedir)
486                 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
487         if (drive)
488                 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
489         if (script)
490                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
491         if (profile)
492                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
493         if (user_sid) {
494                 DOM_SID u_sid;
495                 if (!string_to_sid(&u_sid, user_sid)) {
496                         /* not a complete sid, may be a RID, try building a SID */
497                         int u_rid;
498                         
499                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
500                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
501                                 return -1;
502                         }
503                         sid_copy(&u_sid, get_global_sam_sid());
504                         sid_append_rid(&u_sid, u_rid);
505                 }
506                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
507         }
508         if (group_sid) {
509                 DOM_SID g_sid;
510                 if (!string_to_sid(&g_sid, group_sid)) {
511                         /* not a complete sid, may be a RID, try building a SID */
512                         int g_rid;
513                         
514                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
515                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
516                                 return -1;
517                         }
518                         sid_copy(&g_sid, get_global_sam_sid());
519                         sid_append_rid(&g_sid, g_rid);
520                 }
521                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
522         }
523         
524         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
525         
526         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) { 
527                 print_user_info (in, username, True, False);
528         } else {
529                 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
530                 pdb_free_sam (&sam_pwent);
531                 return -1;
532         }
533         pdb_free_sam (&sam_pwent);
534         return 0;
535 }
536
537 /*********************************************************
538  Add New Machine
539 **********************************************************/
540
541 static int new_machine (struct pdb_context *in, const char *machine_in)
542 {
543         SAM_ACCOUNT *sam_pwent=NULL;
544         fstring machinename;
545         fstring machineaccount;
546         struct passwd  *pwd = NULL;
547         
548         get_global_sam_sid();
549
550         fstrcpy(machinename, machine_in); 
551         machinename[15]= '\0';
552
553         if (machinename[strlen (machinename) -1] == '$')
554                 machinename[strlen (machinename) -1] = '\0';
555         
556         strlower_m(machinename);
557         
558         fstrcpy(machineaccount, machinename);
559         fstrcat(machineaccount, "$");
560
561         if ((pwd = getpwnam_alloc(machineaccount))) {
562                 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
563                         fprintf(stderr, "Could not init sam from pw\n");
564                         passwd_free(&pwd);
565                         return -1;
566                 }
567                 passwd_free(&pwd);
568         } else {
569                 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
570                         fprintf(stderr, "Could not init sam from pw\n");
571                         return -1;
572                 }
573         }
574
575         pdb_set_plaintext_passwd (sam_pwent, machinename);
576
577         pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
578         
579         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
580         
581         pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
582         
583         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
584                 print_user_info (in, machineaccount, True, False);
585         } else {
586                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
587                 pdb_free_sam (&sam_pwent);
588                 return -1;
589         }
590         pdb_free_sam (&sam_pwent);
591         return 0;
592 }
593
594 /*********************************************************
595  Delete user entry
596 **********************************************************/
597
598 static int delete_user_entry (struct pdb_context *in, const char *username)
599 {
600         SAM_ACCOUNT *samaccount = NULL;
601
602         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
603                 return -1;
604         }
605
606         if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
607                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
608                 return -1;
609         }
610
611         if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
612                 fprintf (stderr, "Unable to delete user %s\n", username);
613                 return -1;
614         }
615         return 0;
616 }
617
618 /*********************************************************
619  Delete machine entry
620 **********************************************************/
621
622 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
623 {
624         fstring name;
625         SAM_ACCOUNT *samaccount = NULL;
626         
627         fstrcpy(name, machinename);
628         name[15] = '\0';
629         if (name[strlen(name)-1] != '$')
630                 fstrcat (name, "$");
631
632         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
633                 return -1;
634         }
635
636         if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) {
637                 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
638                 return -1;
639         }
640
641         if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
642                 fprintf (stderr, "Unable to delete machine %s\n", name);
643                 return -1;
644         }
645
646         return 0;
647 }
648
649 /*********************************************************
650  Start here.
651 **********************************************************/
652
653 int main (int argc, char **argv)
654 {
655         static BOOL list_users = False;
656         static BOOL verbose = False;
657         static BOOL spstyle = False;
658         static BOOL machine = False;
659         static BOOL add_user = False;
660         static BOOL delete_user = False;
661         static BOOL modify_user = False;
662         uint32  setparms, checkparms;
663         int opt;
664         static char *full_name = NULL;
665         static const char *user_name = NULL;
666         static char *home_dir = NULL;
667         static char *home_drive = NULL;
668         static char *backend = NULL;
669         static char *backend_in = NULL;
670         static char *backend_out = NULL;
671         static BOOL transfer_groups = False;
672         static BOOL transfer_account_policies = False;
673         static BOOL  force_initialised_password = False;
674         static char *logon_script = NULL;
675         static char *profile_path = NULL;
676         static char *account_control = NULL;
677         static char *account_policy = NULL;
678         static char *user_sid = NULL;
679         static char *group_sid = NULL;
680         static long int account_policy_value = 0;
681         BOOL account_policy_value_set = False;
682         static BOOL badpw_reset = False;
683         static BOOL hours_reset = False;
684         static char *pwd_can_change_time = NULL;
685         static char *pwd_must_change_time = NULL;
686         static char *pwd_time_format = NULL;
687
688         struct pdb_context *bin;
689         struct pdb_context *bout;
690         struct pdb_context *bdef;
691         poptContext pc;
692         struct poptOption long_options[] = {
693                 POPT_AUTOHELP
694                 {"list",        'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
695                 {"verbose",     'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
696                 {"smbpasswd-style",     'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
697                 {"user",        'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
698                 {"fullname",    'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
699                 {"homedir",     'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
700                 {"drive",       'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
701                 {"script",      'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
702                 {"profile",     'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
703                 {"user SID",    'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
704                 {"group SID",   'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
705                 {"create",      'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
706                 {"modify",      'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
707                 {"machine",     'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
708                 {"delete",      'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
709                 {"backend",     'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
710                 {"import",      'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
711                 {"export",      'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
712                 {"group",       'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
713                 {"policies",    'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
714                 {"account-policy",      'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
715                 {"value",       'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
716                 {"account-control",     'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
717                 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
718                 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
719                 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
720                 {"pwd-can-change-time", 0, POPT_ARG_STRING, &pwd_can_change_time, 0, "Set password can change time (unix time in seconds since 1970 if time format not provided)", NULL },
721                 {"pwd-must-change-time", 0, POPT_ARG_STRING, &pwd_must_change_time, 0, "Set password can change time (unix time in seconds since 1970 if time format not provided)", NULL },
722                 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
723                 POPT_COMMON_SAMBA
724                 POPT_TABLEEND
725         };
726         
727         setup_logging("pdbedit", True);
728         
729         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
730                             POPT_CONTEXT_KEEP_FIRST);
731         
732         while((opt = poptGetNextOpt(pc)) != -1) {
733                 switch (opt) {
734                 case 'C':
735                         account_policy_value_set = True;
736                         break;
737                 }
738         }
739
740         poptGetArg(pc); /* Drop argv[0], the program name */
741
742         if (user_name == NULL)
743                 user_name = poptGetArg(pc);
744
745         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
746                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
747                 exit(1);
748         }
749
750         if(!initialize_password_db(False))
751                 exit(1);
752
753         if (!init_names())
754                 exit(1);
755
756         setparms =      (backend ? BIT_BACKEND : 0) +
757                         (verbose ? BIT_VERBOSE : 0) +
758                         (spstyle ? BIT_SPSTYLE : 0) +
759                         (full_name ? BIT_FULLNAME : 0) +
760                         (home_dir ? BIT_HOMEDIR : 0) +
761                         (home_drive ? BIT_HDIRDRIVE : 0) +
762                         (logon_script ? BIT_LOGSCRIPT : 0) +
763                         (profile_path ? BIT_PROFILE : 0) +
764                         (machine ? BIT_MACHINE : 0) +
765                         (user_name ? BIT_USER : 0) +
766                         (list_users ? BIT_LIST : 0) +
767                         (force_initialised_password ? BIT_FIX_INIT : 0) +
768                         (modify_user ? BIT_MODIFY : 0) +
769                         (add_user ? BIT_CREATE : 0) +
770                         (delete_user ? BIT_DELETE : 0) +
771                         (account_control ? BIT_ACCTCTRL : 0) +
772                         (account_policy ? BIT_ACCPOLICY : 0) +
773                         (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
774                         (backend_in ? BIT_IMPORT : 0) +
775                         (backend_out ? BIT_EXPORT : 0) +
776                         (badpw_reset ? BIT_BADPWRESET : 0) +
777                         (hours_reset ? BIT_LOGONHOURS : 0) +
778                         (pwd_can_change_time ? BIT_CAN_CHANGE: 0) +
779                         (pwd_must_change_time ? BIT_MUST_CHANGE: 0);
780
781         if (setparms & BIT_BACKEND) {
782                 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
783                         fprintf(stderr, "Can't initialize passdb backend.\n");
784                         return 1;
785                 }
786         } else {
787                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
788                         fprintf(stderr, "Can't initialize passdb backend.\n");
789                         return 1;
790                 }
791         }
792         
793         /* the lowest bit options are always accepted */
794         checkparms = setparms & ~MASK_ALWAYS_GOOD;
795
796         if (checkparms & BIT_FIX_INIT) {
797                 return fix_users_list(bdef);
798         }
799
800         /* account policy operations */
801         if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
802                 uint32 value;
803                 int field = account_policy_name_to_fieldnum(account_policy);
804                 if (field == 0) {
805                         char *apn = account_policy_names_list();
806                         fprintf(stderr, "No account policy by that name\n");
807                         if (apn) {
808                                 fprintf(stderr, "Account policy names are :\n%s\n", apn);
809                         }
810                         SAFE_FREE(apn);
811                         exit(1);
812                 }
813                 if (!pdb_get_account_policy(field, &value)) {
814                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
815                         if (!account_policy_value_set)
816                                 exit(1);
817                 }
818                 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_comment(field));
819                 if (account_policy_value_set) {
820                         printf("account policy \"%s\" value was: %u\n", account_policy, value);
821                         if (!pdb_set_account_policy(field, account_policy_value)) {
822                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
823                                 exit(1);
824                         }
825                         printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
826                         exit(0);
827                 } else {
828                         printf("account policy \"%s\" value is: %u\n", account_policy, value);
829                         exit(0);
830                 }
831         }
832
833         /* import and export operations */
834         if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
835             && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
836                 if (backend_in) {
837                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
838                                 fprintf(stderr, "Can't initialize passdb backend.\n");
839                                 return 1;
840                         }
841                 } else {
842                         bin = bdef;
843                 }
844                 if (backend_out) {
845                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
846                                 fprintf(stderr, "Can't initialize %s.\n", backend_out);
847                                 return 1;
848                         }
849                 } else {
850                         bout = bdef;
851                 }
852                 if (transfer_account_policies) {
853                         if (!(checkparms & BIT_USER))
854                                 return export_account_policies(bin, bout);
855                 } else  if (transfer_groups) {
856                         if (!(checkparms & BIT_USER))
857                                 return export_groups(bin, bout);
858                 } else {
859                         if (checkparms & BIT_USER)
860                                 return export_database(bin, bout,
861                                                        user_name);
862                         else
863                                 return export_database(bin, bout,
864                                                        NULL);
865                 }
866         }
867
868         /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
869         /* fake up BIT_LIST if only BIT_USER is defined */
870         if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
871                 checkparms += BIT_LIST;
872         }
873         
874         /* modify flag is optional to maintain backwards compatibility */
875         /* fake up BIT_MODIFY if BIT_USER  and at least one of MASK_USER_GOOD is defined */
876         if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
877                 checkparms += BIT_MODIFY;
878         }
879
880         /* list users operations */
881         if (checkparms & BIT_LIST) {
882                 if (!(checkparms & ~BIT_LIST)) {
883                         return print_users_list (bdef, verbose, spstyle);
884                 }
885                 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
886                         return print_user_info (bdef, user_name, verbose, spstyle);
887                 }
888         }
889         
890         /* mask out users options */
891         checkparms &= ~MASK_USER_GOOD;
892
893         /* if bad password count is reset, we must be modifying */
894         if (checkparms & BIT_BADPWRESET) {
895                 checkparms |= BIT_MODIFY;
896                 checkparms &= ~BIT_BADPWRESET;
897         }
898
899         /* if logon hours is reset, must modify */
900         if (checkparms & BIT_LOGONHOURS) {
901                 checkparms |= BIT_MODIFY;
902                 checkparms &= ~BIT_LOGONHOURS;
903         }
904         
905         /* account operation */
906         if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
907                 /* check use of -u option */
908                 if (!(checkparms & BIT_USER)) {
909                         fprintf (stderr, "Username not specified! (use -u option)\n");
910                         return -1;
911                 }
912
913                 /* account creation operations */
914                 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
915                         if (checkparms & BIT_MACHINE) {
916                                 return new_machine (bdef, user_name);
917                         } else {
918                                 return new_user (bdef, user_name, full_name, home_dir, 
919                                                  home_drive, logon_script, 
920                                                  profile_path, user_sid, group_sid);
921                         }
922                 }
923
924                 /* account deletion operations */
925                 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
926                         if (checkparms & BIT_MACHINE) {
927                                 return delete_machine_entry (bdef, user_name);
928                         } else {
929                                 return delete_user_entry (bdef, user_name);
930                         }
931                 }
932
933                 /* account modification operations */
934                 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
935                         time_t pwd_can_change = -1;
936                         time_t pwd_must_change = -1;
937                         char *errstr;
938
939                         if (pwd_can_change_time) {
940                                 errstr = "can";
941                                 if (pwd_time_format) {
942                                         struct tm tm;
943                                         char *ret;
944
945                                         memset(&tm, 0, sizeof(struct tm));
946                                         ret = strptime(pwd_can_change_time, pwd_time_format, &tm);
947                                         if (ret == NULL || *ret != '\0') {
948                                                 goto error;
949                                         }
950
951                                         pwd_can_change = mktime(&tm);
952
953                                         if (pwd_can_change == -1) {
954                                                 goto error;
955                                         }
956                                 } else { /* assume it is unix time */
957                                         errno = 0;
958                                         pwd_can_change = strtol(pwd_can_change_time, NULL, 10);
959                                         if (errno) {
960                                                 goto error;
961                                         }
962                                 }       
963                         }
964                         if (pwd_must_change_time) {
965                                 errstr = "must";
966                                 if (pwd_time_format) {
967                                         struct tm tm;
968                                         char *ret;
969
970                                         memset(&tm, 0, sizeof(struct tm));
971                                         ret = strptime(pwd_must_change_time, pwd_time_format, &tm);
972                                         if (ret == NULL || *ret != '\0') {
973                                                 goto error;
974                                         }
975
976                                         pwd_must_change = mktime(&tm);
977
978                                         if (pwd_must_change == -1) {
979                                                 goto error;
980                                         }
981                                 } else { /* assume it is unix time */
982                                         errno = 0;
983                                         pwd_must_change = strtol(pwd_must_change_time, NULL, 10);
984                                         if (errno) {
985                                                 goto error;
986                                         }
987                                 }       
988                         }
989                         return set_user_info (bdef, user_name, full_name,
990                                               home_dir,
991                                               home_drive,
992                                               logon_script,
993                                               profile_path, account_control,
994                                               user_sid, group_sid,
995                                               badpw_reset, hours_reset,
996                                               pwd_can_change, pwd_must_change);
997 error:
998                         fprintf (stderr, "Error parsing the time in pwd-%s-change-time!\n", errstr);
999                         return -1;
1000                 }
1001         }
1002
1003         if (setparms >= 0x20) {
1004                 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1005         }
1006         poptPrintHelp(pc, stderr, 0);
1007
1008         return 1;
1009 }