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