r1812: Fix from Richard Renard <rrenard@idealx.com> to be able to reset
[sfrench/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_RESERV_1    0x00000020
30 #define BIT_RESERV_2    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          0x00401F00
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))) {
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  Print info from sam structure
123 **********************************************************/
124
125 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
126 {
127         uid_t uid;
128         time_t tmp;
129
130         /* TODO: chaeck if entry is a user or a workstation */
131         if (!sam_pwent) return -1;
132         
133         if (verbosity) {
134                 pstring temp;
135                 const uint8 *hours;
136                 
137                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
138                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
139                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
140                 printf ("User SID:             %s\n",
141                         sid_string_static(pdb_get_user_sid(sam_pwent)));
142                 printf ("Primary Group SID:    %s\n",
143                         sid_string_static(pdb_get_group_sid(sam_pwent)));
144                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
145                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
146                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
147                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
148                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
149                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
150                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
151                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
152                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
153                 
154                 tmp = pdb_get_logon_time(sam_pwent);
155                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
156                 
157                 tmp = pdb_get_logoff_time(sam_pwent);
158                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
159                 
160                 tmp = pdb_get_kickoff_time(sam_pwent);
161                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
162                 
163                 tmp = pdb_get_pass_last_set_time(sam_pwent);
164                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
165                 
166                 tmp = pdb_get_pass_can_change_time(sam_pwent);
167                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
168                 
169                 tmp = pdb_get_pass_must_change_time(sam_pwent);
170                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
171
172                 tmp = pdb_get_bad_password_time(sam_pwent);
173                 printf ("Last bad password   : %s\n", tmp ? http_timestring(tmp) : "0");
174                 printf ("Bad password count  : %d\n", 
175                         pdb_get_bad_password_count(sam_pwent));
176                 
177                 hours = pdb_get_hours(sam_pwent);
178                 pdb_sethexhours(temp, (const char *)hours);
179                 printf ("Logon hours         : %s\n", temp);
180                 
181         } else if (smbpwdstyle) {
182                 char lm_passwd[33];
183                 char nt_passwd[33];
184
185                 uid = nametouid(pdb_get_username(sam_pwent));
186                 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
187                 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
188                         
189                 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
190                        pdb_get_username(sam_pwent),
191                        (unsigned long)uid,
192                        lm_passwd,
193                        nt_passwd,
194                        pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
195                        (uint32)pdb_get_pass_last_set_time(sam_pwent));
196         } else {
197                 uid = nametouid(pdb_get_username(sam_pwent));
198                 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid, 
199                         pdb_get_fullname(sam_pwent));
200         }
201
202         return 0;       
203 }
204
205 /*********************************************************
206  Get an Print User Info
207 **********************************************************/
208
209 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
210 {
211         SAM_ACCOUNT *sam_pwent=NULL;
212         BOOL ret;
213
214         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
215                 return -1;
216         }
217
218         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
219
220         if (ret==False) {
221                 fprintf (stderr, "Username not found!\n");
222                 pdb_free_sam(&sam_pwent);
223                 return -1;
224         }
225
226         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
227         pdb_free_sam(&sam_pwent);
228         
229         return ret;
230 }
231         
232 /*********************************************************
233  List Users
234 **********************************************************/
235 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
236 {
237         SAM_ACCOUNT *sam_pwent=NULL;
238         BOOL check, ret;
239         
240         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
241         if (!check) {
242                 return 1;
243         }
244
245         check = True;
246         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
247
248         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
249                 if (verbosity)
250                         printf ("---------------\n");
251                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
252                 pdb_free_sam(&sam_pwent);
253                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
254         }
255         if (check) pdb_free_sam(&sam_pwent);
256         
257         in->pdb_endsampwent(in);
258         return 0;
259 }
260
261 /*********************************************************
262  Fix a list of Users for uninitialised passwords
263 **********************************************************/
264 static int fix_users_list (struct pdb_context *in)
265 {
266         SAM_ACCOUNT *sam_pwent=NULL;
267         BOOL check, ret;
268         
269         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
270         if (!check) {
271                 return 1;
272         }
273
274         check = True;
275         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
276
277         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
278                 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
279         
280                 if (!pdb_update_sam_account(sam_pwent)) {
281                         printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
282                 }
283                 pdb_free_sam(&sam_pwent);
284                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
285                 if (!check) {
286                         fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
287                 }
288                         
289         }
290         if (check) pdb_free_sam(&sam_pwent);
291         
292         in->pdb_endsampwent(in);
293         return 0;
294 }
295
296 /*********************************************************
297  Set User Info
298 **********************************************************/
299
300 static int set_user_info (struct pdb_context *in, const char *username, 
301                           const char *fullname, const char *homedir, 
302                           const char *drive, const char *script, 
303                           const char *profile, const char *account_control,
304                           const char *user_sid, const char *group_sid,
305                           const BOOL badpw, const BOOL hours)
306 {
307         BOOL updated_autolock = False, updated_badpw = False;
308         SAM_ACCOUNT *sam_pwent=NULL;
309         BOOL ret;
310         
311         pdb_init_sam(&sam_pwent);
312         
313         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
314         if (ret==False) {
315                 fprintf (stderr, "Username not found!\n");
316                 pdb_free_sam(&sam_pwent);
317                 return -1;
318         }
319
320         if (hours) {
321                 uint8 hours_array[MAX_HOURS_LEN];
322                 uint32 hours_len;
323                 
324                 hours_len = pdb_get_hours_len(sam_pwent);
325                 memset(hours_array, 0xff, hours_len);
326                 
327                 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
328         }
329         
330         if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
331                 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
332         }
333
334         if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
335                 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
336         }
337
338         if (fullname)
339                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
340         if (homedir)
341                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
342         if (drive)
343                 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
344         if (script)
345                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
346         if (profile)
347                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
348
349         if (account_control) {
350                 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
351                                         ACB_PWNOEXP|ACB_AUTOLOCK);
352
353                 uint16 newflag = pdb_decode_acct_ctrl(account_control);
354
355                 if (newflag & not_settable) {
356                         fprintf(stderr, "Can only set [NDHLX] flags\n");
357                         pdb_free_sam(&sam_pwent);
358                         return -1;
359                 }
360
361                 pdb_set_acct_ctrl(sam_pwent,
362                                   (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
363                                   PDB_CHANGED);
364         }
365         if (user_sid) {
366                 DOM_SID u_sid;
367                 if (!string_to_sid(&u_sid, user_sid)) {
368                         /* not a complete sid, may be a RID, try building a SID */
369                         int u_rid;
370                         
371                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
372                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
373                                 return -1;
374                         }
375                         sid_copy(&u_sid, get_global_sam_sid());
376                         sid_append_rid(&u_sid, u_rid);
377                 }
378                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
379         }
380         if (group_sid) {
381                 DOM_SID g_sid;
382                 if (!string_to_sid(&g_sid, group_sid)) {
383                         /* not a complete sid, may be a RID, try building a SID */
384                         int g_rid;
385                         
386                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
387                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
388                                 return -1;
389                         }
390                         sid_copy(&g_sid, get_global_sam_sid());
391                         sid_append_rid(&g_sid, g_rid);
392                 }
393                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
394         }
395
396         if (badpw) {
397                 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
398                 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
399         }
400
401         if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
402                 print_user_info (in, username, True, False);
403         else {
404                 fprintf (stderr, "Unable to modify entry!\n");
405                 pdb_free_sam(&sam_pwent);
406                 return -1;
407         }
408         pdb_free_sam(&sam_pwent);
409         return 0;
410 }
411
412 /*********************************************************
413  Add New User
414 **********************************************************/
415 static int new_user (struct pdb_context *in, const char *username,
416                         const char *fullname, const char *homedir,
417                         const char *drive, const char *script,
418                         const char *profile, char *user_sid, char *group_sid)
419 {
420         SAM_ACCOUNT *sam_pwent=NULL;
421         NTSTATUS nt_status;
422         char *password1, *password2, *staticpass;
423         
424         get_global_sam_sid();
425
426         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_new(&sam_pwent, username, 0))) {
427                 DEBUG(0, ("could not create account to add new user %s\n", username));
428                 return -1;
429         }
430
431         staticpass = getpass("new password:");
432         password1 = strdup(staticpass);
433         memset(staticpass, 0, strlen(staticpass));
434         staticpass = getpass("retype new password:");
435         password2 = strdup(staticpass);
436         memset(staticpass, 0, strlen(staticpass));
437         if (strcmp (password1, password2)) {
438                 fprintf (stderr, "Passwords does not match!\n");
439                 memset(password1, 0, strlen(password1));
440                 SAFE_FREE(password1);
441                 memset(password2, 0, strlen(password2));
442                 SAFE_FREE(password2);
443                 pdb_free_sam (&sam_pwent);
444                 return -1;
445         }
446
447         pdb_set_plaintext_passwd(sam_pwent, password1);
448         memset(password1, 0, strlen(password1));
449         SAFE_FREE(password1);
450         memset(password2, 0, strlen(password2));
451         SAFE_FREE(password2);
452
453         if (fullname)
454                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
455         if (homedir)
456                 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
457         if (drive)
458                 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
459         if (script)
460                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
461         if (profile)
462                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
463         if (user_sid) {
464                 DOM_SID u_sid;
465                 if (!string_to_sid(&u_sid, user_sid)) {
466                         /* not a complete sid, may be a RID, try building a SID */
467                         int u_rid;
468                         
469                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
470                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
471                                 return -1;
472                         }
473                         sid_copy(&u_sid, get_global_sam_sid());
474                         sid_append_rid(&u_sid, u_rid);
475                 }
476                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
477         }
478         if (group_sid) {
479                 DOM_SID g_sid;
480                 if (!string_to_sid(&g_sid, group_sid)) {
481                         /* not a complete sid, may be a RID, try building a SID */
482                         int g_rid;
483                         
484                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
485                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
486                                 return -1;
487                         }
488                         sid_copy(&g_sid, get_global_sam_sid());
489                         sid_append_rid(&g_sid, g_rid);
490                 }
491                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
492         }
493         
494         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
495         
496         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) { 
497                 print_user_info (in, username, True, False);
498         } else {
499                 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
500                 pdb_free_sam (&sam_pwent);
501                 return -1;
502         }
503         pdb_free_sam (&sam_pwent);
504         return 0;
505 }
506
507 /*********************************************************
508  Add New Machine
509 **********************************************************/
510
511 static int new_machine (struct pdb_context *in, const char *machine_in)
512 {
513         SAM_ACCOUNT *sam_pwent=NULL;
514         fstring machinename;
515         fstring machineaccount;
516         struct passwd  *pwd = NULL;
517         
518         get_global_sam_sid();
519
520         fstrcpy(machinename, machine_in); 
521         machinename[15]= '\0';
522
523         if (machinename[strlen (machinename) -1] == '$')
524                 machinename[strlen (machinename) -1] = '\0';
525         
526         strlower_m(machinename);
527         
528         fstrcpy(machineaccount, machinename);
529         fstrcat(machineaccount, "$");
530
531         if ((pwd = getpwnam_alloc(machineaccount))) {
532                 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
533                         fprintf(stderr, "Could not init sam from pw\n");
534                         passwd_free(&pwd);
535                         return -1;
536                 }
537                 passwd_free(&pwd);
538         } else {
539                 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
540                         fprintf(stderr, "Could not init sam from pw\n");
541                         return -1;
542                 }
543         }
544
545         pdb_set_plaintext_passwd (sam_pwent, machinename);
546
547         pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
548         
549         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
550         
551         pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
552         
553         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
554                 print_user_info (in, machineaccount, True, False);
555         } else {
556                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
557                 pdb_free_sam (&sam_pwent);
558                 return -1;
559         }
560         pdb_free_sam (&sam_pwent);
561         return 0;
562 }
563
564 /*********************************************************
565  Delete user entry
566 **********************************************************/
567
568 static int delete_user_entry (struct pdb_context *in, const char *username)
569 {
570         SAM_ACCOUNT *samaccount = NULL;
571
572         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
573                 return -1;
574         }
575
576         if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
577                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
578                 return -1;
579         }
580
581         if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
582                 fprintf (stderr, "Unable to delete user %s\n", username);
583                 return -1;
584         }
585         return 0;
586 }
587
588 /*********************************************************
589  Delete machine entry
590 **********************************************************/
591
592 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
593 {
594         fstring name;
595         SAM_ACCOUNT *samaccount = NULL;
596         
597         fstrcpy(name, machinename);
598         name[15] = '\0';
599         if (name[strlen(name)-1] != '$')
600                 fstrcat (name, "$");
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, name))) {
607                 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
608                 return -1;
609         }
610
611         if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
612                 fprintf (stderr, "Unable to delete machine %s\n", name);
613                 return -1;
614         }
615
616         return 0;
617 }
618
619 /*********************************************************
620  Start here.
621 **********************************************************/
622
623 int main (int argc, char **argv)
624 {
625         static BOOL list_users = False;
626         static BOOL verbose = False;
627         static BOOL spstyle = False;
628         static BOOL machine = False;
629         static BOOL add_user = False;
630         static BOOL delete_user = False;
631         static BOOL modify_user = False;
632         uint32  setparms, checkparms;
633         int opt;
634         static char *full_name = NULL;
635         static const char *user_name = NULL;
636         static char *home_dir = NULL;
637         static char *home_drive = NULL;
638         static char *backend = NULL;
639         static char *backend_in = NULL;
640         static char *backend_out = NULL;
641         static BOOL transfer_groups = False;
642         static BOOL  force_initialised_password = False;
643         static char *logon_script = NULL;
644         static char *profile_path = NULL;
645         static char *account_control = NULL;
646         static char *account_policy = NULL;
647         static char *user_sid = NULL;
648         static char *group_sid = NULL;
649         static long int account_policy_value = 0;
650         BOOL account_policy_value_set = False;
651         static BOOL badpw_reset = False;
652         static BOOL hours_reset = False;
653
654         struct pdb_context *bin;
655         struct pdb_context *bout;
656         struct pdb_context *bdef;
657         poptContext pc;
658         struct poptOption long_options[] = {
659                 POPT_AUTOHELP
660                 {"list",        'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
661                 {"verbose",     'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
662                 {"smbpasswd-style",     'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
663                 {"user",        'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
664                 {"fullname",    'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
665                 {"homedir",     'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
666                 {"drive",       'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
667                 {"script",      'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
668                 {"profile",     'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
669                 {"user SID",    'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
670                 {"group SID",   'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
671                 {"create",      'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
672                 {"modify",      'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
673                 {"machine",     'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
674                 {"delete",      'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
675                 {"backend",     'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
676                 {"import",      'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
677                 {"export",      'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
678                 {"group",       'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
679                 {"account-policy",      'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
680                 {"value",       'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
681                 {"account-control",     'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
682                 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
683                 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
684                 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
685                 POPT_COMMON_SAMBA
686                 POPT_TABLEEND
687         };
688         
689         setup_logging("pdbedit", True);
690         
691         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
692                             POPT_CONTEXT_KEEP_FIRST);
693         
694         while((opt = poptGetNextOpt(pc)) != -1) {
695                 switch (opt) {
696                 case 'C':
697                         account_policy_value_set = True;
698                         break;
699                 }
700         }
701
702         poptGetArg(pc); /* Drop argv[0], the program name */
703
704         if (user_name == NULL)
705                 user_name = poptGetArg(pc);
706
707         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
708                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
709                 exit(1);
710         }
711
712         if(!initialize_password_db(False))
713                 exit(1);
714
715         if (!init_names())
716                 exit(1);
717
718         setparms =      (backend ? BIT_BACKEND : 0) +
719                         (verbose ? BIT_VERBOSE : 0) +
720                         (spstyle ? BIT_SPSTYLE : 0) +
721                         (full_name ? BIT_FULLNAME : 0) +
722                         (home_dir ? BIT_HOMEDIR : 0) +
723                         (home_drive ? BIT_HDIRDRIVE : 0) +
724                         (logon_script ? BIT_LOGSCRIPT : 0) +
725                         (profile_path ? BIT_PROFILE : 0) +
726                         (machine ? BIT_MACHINE : 0) +
727                         (user_name ? BIT_USER : 0) +
728                         (list_users ? BIT_LIST : 0) +
729                         (force_initialised_password ? BIT_FIX_INIT : 0) +
730                         (modify_user ? BIT_MODIFY : 0) +
731                         (add_user ? BIT_CREATE : 0) +
732                         (delete_user ? BIT_DELETE : 0) +
733                         (account_control ? BIT_ACCTCTRL : 0) +
734                         (account_policy ? BIT_ACCPOLICY : 0) +
735                         (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
736                         (backend_in ? BIT_IMPORT : 0) +
737                         (backend_out ? BIT_EXPORT : 0) +
738                         (badpw_reset ? BIT_BADPWRESET : 0) +
739                         (hours_reset ? BIT_LOGONHOURS : 0);
740
741         if (setparms & BIT_BACKEND) {
742                 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
743                         fprintf(stderr, "Can't initialize passdb backend.\n");
744                         return 1;
745                 }
746         } else {
747                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
748                         fprintf(stderr, "Can't initialize passdb backend.\n");
749                         return 1;
750                 }
751         }
752         
753         /* the lowest bit options are always accepted */
754         checkparms = setparms & ~MASK_ALWAYS_GOOD;
755
756         if (checkparms & BIT_FIX_INIT) {
757                 return fix_users_list(bdef);
758         }
759
760         /* account policy operations */
761         if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
762                 uint32 value;
763                 int field = account_policy_name_to_fieldnum(account_policy);
764                 if (field == 0) {
765                         char *apn = account_policy_names_list();
766                         fprintf(stderr, "No account policy by that name\n");
767                         if (apn) {
768                                 fprintf(stderr, "Account policy names are :\n%s\n", apn);
769                         }
770                         SAFE_FREE(apn);
771                         exit(1);
772                 }
773                 if (!account_policy_get(field, &value)) {
774                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
775                         exit(1);
776                 }
777                 if (account_policy_value_set) {
778                         printf("account policy value for %s was %u\n", account_policy, value);
779                         if (!account_policy_set(field, account_policy_value)) {
780                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
781                                 exit(1);
782                         }
783                         printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
784                         exit(0);
785                 } else {
786                         printf("account policy value for %s is %u\n", account_policy, value);
787                         exit(0);
788                 }
789         }
790
791         /* import and export operations */
792         if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
793             && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
794                 if (backend_in) {
795                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
796                                 fprintf(stderr, "Can't initialize passdb backend.\n");
797                                 return 1;
798                         }
799                 } else {
800                         bin = bdef;
801                 }
802                 if (backend_out) {
803                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
804                                 fprintf(stderr, "Can't initialize %s.\n", backend_out);
805                                 return 1;
806                         }
807                 } else {
808                         bout = bdef;
809                 }
810                 if (transfer_groups) {
811                         if (!(checkparms & BIT_USER))
812                                 return export_groups(bin, bout);
813                 } else {
814                         if (checkparms & BIT_USER)
815                                 return export_database(bin, bout,
816                                                        user_name);
817                         else
818                                 return export_database(bin, bout,
819                                                        NULL);
820                 }
821         }
822
823         /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
824         /* fake up BIT_LIST if only BIT_USER is defined */
825         if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
826                 checkparms += BIT_LIST;
827         }
828         
829         /* modify flag is optional to maintain backwards compatibility */
830         /* fake up BIT_MODIFY if BIT_USER  and at least one of MASK_USER_GOOD is defined */
831         if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
832                 checkparms += BIT_MODIFY;
833         }
834
835         /* list users operations */
836         if (checkparms & BIT_LIST) {
837                 if (!(checkparms & ~BIT_LIST)) {
838                         return print_users_list (bdef, verbose, spstyle);
839                 }
840                 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
841                         return print_user_info (bdef, user_name, verbose, spstyle);
842                 }
843         }
844         
845         /* mask out users options */
846         checkparms &= ~MASK_USER_GOOD;
847
848         /* if bad password count is reset, we must be modifying */
849         if (checkparms & BIT_BADPWRESET) {
850                 checkparms |= BIT_MODIFY;
851                 checkparms &= ~BIT_BADPWRESET;
852         }
853
854         /* if logon hours is reset, must modify */
855         if (checkparms & BIT_LOGONHOURS) {
856                 checkparms |= BIT_MODIFY;
857                 checkparms &= ~BIT_LOGONHOURS;
858         }
859         
860         /* account operation */
861         if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
862                 /* check use of -u option */
863                 if (!(checkparms & BIT_USER)) {
864                         fprintf (stderr, "Username not specified! (use -u option)\n");
865                         return -1;
866                 }
867
868                 /* account creation operations */
869                 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
870                         if (checkparms & BIT_MACHINE) {
871                                 return new_machine (bdef, user_name);
872                         } else {
873                                 return new_user (bdef, user_name, full_name, home_dir, 
874                                                  home_drive, logon_script, 
875                                                  profile_path, user_sid, group_sid);
876                         }
877                 }
878
879                 /* account deletion operations */
880                 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
881                         if (checkparms & BIT_MACHINE) {
882                                 return delete_machine_entry (bdef, user_name);
883                         } else {
884                                 return delete_user_entry (bdef, user_name);
885                         }
886                 }
887
888                 /* account modification operations */
889                 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
890                         return set_user_info (bdef, user_name, full_name,
891                                               home_dir,
892                                               home_drive,
893                                               logon_script,
894                                               profile_path, account_control,
895                                               user_sid, group_sid,
896                                               badpw_reset, hours_reset);
897                 }
898         }
899
900         if (setparms >= 0x20) {
901                 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
902         }
903         poptPrintHelp(pc, stderr, 0);
904
905         return 1;
906 }