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