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