passwords where not checked (you cannot check if the same buffer differs from itself).
[sfrench/samba-autobuild/.git] / source3 / utils / pdbedit.c
1 /* 
2    Unix SMB/CIFS implementation.
3    passdb editing frontend
4    
5    Copyright (C) Simo Sorce      2000
6    Copyright (C) Andrew Bartlett 2001   
7    Copyright (C) Jelmer Vernooij 2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 extern pstring global_myname;
27 extern BOOL AllowDebugChange;
28
29 /*********************************************************
30  Add all currently available users to another db
31  ********************************************************/
32
33 static int export_database (struct pdb_context *in, char *db){
34         struct pdb_context *context;
35         SAM_ACCOUNT *user = NULL;
36
37         if (!NT_STATUS_IS_OK(make_pdb_context_string(&context, db))){
38                 fprintf(stderr, "Can't initialize %s.\n", db);
39                 return 1;
40         }
41
42         if (!in->pdb_setsampwent(in, 0)){
43                 fprintf(stderr, "Can't sampwent!\n");
44                 return 1;
45         }
46
47         if (!NT_STATUS_IS_OK(pdb_init_sam(&user))){
48                 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
49                 return 1;
50         }
51
52         while (in->pdb_getsampwent(in,user)){
53                 context->pdb_add_sam_account(context,user);
54                 if (!NT_STATUS_IS_OK(pdb_reset_sam(user))){
55                         fprintf(stderr, "Can't reset SAM_ACCOUNT!\n");
56                         return 1;
57                 }
58         }
59
60         in->pdb_endsampwent(in);
61
62         return 0;
63 }
64
65 /*********************************************************
66  Print info from sam structure
67 **********************************************************/
68
69 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
70 {
71         uid_t uid;
72         gid_t gid;
73         time_t tmp;
74
75         /* TODO: chaeck if entry is a user or a workstation */
76         if (!sam_pwent) return -1;
77         
78         if (verbosity) {
79                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
80                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
81                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
82                 
83                 if (IS_SAM_UNIX_USER(sam_pwent)) {
84                         uid = pdb_get_uid(sam_pwent);
85                         gid = pdb_get_gid(sam_pwent);
86                         printf ("User ID/Group ID:     %d/%d\n", uid, gid);
87                 }
88                 printf ("User SID:             %s\n",
89                         sid_string_static(pdb_get_user_sid(sam_pwent)));
90                 printf ("Primary Group SID:    %s\n",
91                         sid_string_static(pdb_get_group_sid(sam_pwent)));
92                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
93                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
94                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
95                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
96                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
97                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
98                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
99                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
100                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
101                 
102                 tmp = pdb_get_logon_time(sam_pwent);
103                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
104                 
105                 tmp = pdb_get_logoff_time(sam_pwent);
106                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
107                 
108                 tmp = pdb_get_kickoff_time(sam_pwent);
109                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
110                 
111                 tmp = pdb_get_pass_last_set_time(sam_pwent);
112                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
113                 
114                 tmp = pdb_get_pass_can_change_time(sam_pwent);
115                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
116                 
117                 tmp = pdb_get_pass_must_change_time(sam_pwent);
118                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
119                 
120         } else if (smbpwdstyle) {
121                 if (IS_SAM_UNIX_USER(sam_pwent)) {
122                         char lm_passwd[33];
123                         char nt_passwd[33];
124
125                         uid = pdb_get_uid(sam_pwent);
126                         pdb_sethexpwd(lm_passwd, 
127                                       pdb_get_lanman_passwd(sam_pwent), 
128                                       pdb_get_acct_ctrl(sam_pwent));
129                         pdb_sethexpwd(nt_passwd, 
130                                       pdb_get_nt_passwd(sam_pwent), 
131                                       pdb_get_acct_ctrl(sam_pwent));
132                         
133                         printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
134                                pdb_get_username(sam_pwent),
135                                uid,
136                                lm_passwd,
137                                nt_passwd,
138                                pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
139                                (uint32)pdb_get_pass_last_set_time(sam_pwent));
140                 } else {
141                         fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
142                 }
143         } else {
144                 if (IS_SAM_UNIX_USER(sam_pwent)) {
145                         printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent), 
146                                 pdb_get_fullname(sam_pwent));
147                 } else {        
148                         printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
149                 }
150         }
151
152         return 0;       
153 }
154
155 /*********************************************************
156  Get an Print User Info
157 **********************************************************/
158
159 static int print_user_info (struct pdb_context *in, char *username, BOOL verbosity, BOOL smbpwdstyle)
160 {
161         SAM_ACCOUNT *sam_pwent=NULL;
162         BOOL ret;
163         
164         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
165                 return -1;
166         }
167         
168         ret = in->pdb_getsampwnam (in, sam_pwent, username);
169
170         if (ret==False) {
171                 fprintf (stderr, "Username not found!\n");
172                 pdb_free_sam(&sam_pwent);
173                 return -1;
174         }
175         
176         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
177         pdb_free_sam(&sam_pwent);
178         
179         return ret;
180 }
181         
182 /*********************************************************
183  List Users
184 **********************************************************/
185 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
186 {
187         SAM_ACCOUNT *sam_pwent=NULL;
188         BOOL check, ret;
189         
190         check = in->pdb_setsampwent(in, False);
191         if (!check) {
192                 return 1;
193         }
194
195         check = True;
196         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
197
198         while (check && (ret = in->pdb_getsampwent (in, sam_pwent))) {
199                 if (verbosity)
200                         printf ("---------------\n");
201                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
202                 pdb_free_sam(&sam_pwent);
203                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
204         }
205         if (check) pdb_free_sam(&sam_pwent);
206         
207         in->pdb_endsampwent(in);
208         return 0;
209 }
210
211 /*********************************************************
212  Set User Info
213 **********************************************************/
214
215 static int set_user_info (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
216 {
217         SAM_ACCOUNT *sam_pwent=NULL;
218         BOOL ret;
219         
220         pdb_init_sam(&sam_pwent);
221         
222         ret = in->pdb_getsampwnam (in, sam_pwent, username);
223         if (ret==False) {
224                 fprintf (stderr, "Username not found!\n");
225                 pdb_free_sam(&sam_pwent);
226                 return -1;
227         }
228         
229         if (fullname)
230                 pdb_set_fullname(sam_pwent, fullname);
231         if (homedir)
232                 pdb_set_homedir(sam_pwent, homedir, True);
233         if (drive)
234                 pdb_set_dir_drive(sam_pwent,drive, True);
235         if (script)
236                 pdb_set_logon_script(sam_pwent, script, True);
237         if (profile)
238                 pdb_set_profile_path (sam_pwent, profile, True);
239         
240         if (in->pdb_update_sam_account (in, sam_pwent))
241                 print_user_info (in, username, True, False);
242         else {
243                 fprintf (stderr, "Unable to modify entry!\n");
244                 pdb_free_sam(&sam_pwent);
245                 return -1;
246         }
247         pdb_free_sam(&sam_pwent);
248         return 0;
249 }
250
251 /*********************************************************
252  Add New User
253 **********************************************************/
254 static int new_user (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
255 {
256         SAM_ACCOUNT *sam_pwent=NULL;
257         struct passwd  *pwd = NULL;
258         char *password1, *password2, *staticpass;
259         
260         ZERO_STRUCT(sam_pwent);
261
262         if ((pwd = getpwnam_alloc(username))) {
263                 pdb_init_sam_pw (&sam_pwent, pwd);
264                 passwd_free(&pwd);
265         } else {
266                 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
267                 pdb_init_sam(&sam_pwent);
268                 if (!pdb_set_username(sam_pwent, username)) {
269                         return False;
270                 }
271         }
272
273         staticpass = getpass("new password:");
274         password1 = strdup(staticpass);
275         memset(staticpass, 0, strlen(staticpass));
276         staticpass = getpass("retype new password:");
277         password2 = strdup(staticpass);
278         memset(staticpass, 0, strlen(staticpass));
279         if (strcmp (password1, password2)) {
280                 fprintf (stderr, "Passwords does not match!\n");
281                 memset(password1, 0, strlen(password1));
282                 SAFE_FREE(password1);
283                 memset(password2, 0, strlen(password2));
284                 SAFE_FREE(password2);
285                 pdb_free_sam (&sam_pwent);
286                 return -1;
287         }
288
289         pdb_set_plaintext_passwd(sam_pwent, password1);
290         memset(password1, 0, strlen(password1));
291         SAFE_FREE(password1);
292         memset(password2, 0, strlen(password2));
293         SAFE_FREE(password2);
294
295         if (fullname)
296                 pdb_set_fullname(sam_pwent, fullname);
297         if (homedir)
298                 pdb_set_homedir (sam_pwent, homedir, True);
299         if (drive)
300                 pdb_set_dir_drive (sam_pwent, drive, True);
301         if (script)
302                 pdb_set_logon_script(sam_pwent, script, True);
303         if (profile)
304                 pdb_set_profile_path (sam_pwent, profile, True);
305         
306         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
307         
308         if (in->pdb_add_sam_account (in, sam_pwent)) { 
309                 print_user_info (in, username, True, False);
310         } else {
311                 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
312                 pdb_free_sam (&sam_pwent);
313                 return -1;
314         }
315         pdb_free_sam (&sam_pwent);
316         return 0;
317 }
318
319 /*********************************************************
320  Add New Machine
321 **********************************************************/
322
323 static int new_machine (struct pdb_context *in, char *machinename)
324 {
325         SAM_ACCOUNT *sam_pwent=NULL;
326         char name[16];
327         char *password = NULL;
328         
329         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
330                 return -1;
331         }
332
333         if (machinename[strlen (machinename) -1] == '$')
334                 machinename[strlen (machinename) -1] = '\0';
335         
336         safe_strcpy (name, machinename, 16);
337         safe_strcat (name, "$", 16);
338         
339         string_set (&password, machinename);
340         strlower_m(password);
341         
342         pdb_set_plaintext_passwd (sam_pwent, password);
343
344         pdb_set_username (sam_pwent, name);
345         
346         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
347         
348         pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS);
349         
350         if (in->pdb_add_sam_account (in, sam_pwent)) {
351                 print_user_info (in, name, True, False);
352         } else {
353                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
354                 pdb_free_sam (&sam_pwent);
355                 return -1;
356         }
357         pdb_free_sam (&sam_pwent);
358         return 0;
359 }
360
361 /*********************************************************
362  Delete user entry
363 **********************************************************/
364
365 static int delete_user_entry (struct pdb_context *in, char *username)
366 {
367         SAM_ACCOUNT *samaccount = NULL;
368
369         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
370                 return -1;
371         }
372
373         if (!in->pdb_getsampwnam(in, samaccount, username)) {
374                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
375                 return -1;
376         }
377
378         return in->pdb_delete_sam_account (in, samaccount);
379 }
380
381 /*********************************************************
382  Delete machine entry
383 **********************************************************/
384
385 static int delete_machine_entry (struct pdb_context *in, char *machinename)
386 {
387         char name[16];
388         SAM_ACCOUNT *samaccount = NULL;
389         
390         safe_strcpy (name, machinename, 16);
391         if (name[strlen(name)] != '$')
392                 safe_strcat (name, "$", 16);
393
394         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
395                 return -1;
396         }
397
398         if (!in->pdb_getsampwnam(in, samaccount, name)) {
399                 fprintf (stderr, "user %s does not exist in the passdb\n", name);
400                 return -1;
401         }
402
403         return in->pdb_delete_sam_account (in, samaccount);
404 }
405
406 /*********************************************************
407  Start here.
408 **********************************************************/
409
410 int main (int argc, char **argv)
411 {
412         static BOOL list_users = False;
413         static BOOL verbose = False;
414         static BOOL spstyle = False;
415         static BOOL setparms = False;
416         static BOOL machine = False;
417         static BOOL add_user = False;
418         static BOOL delete_user = False;
419         static BOOL import = False;
420         int opt;
421         static char *full_name = NULL;
422         static char *user_name = NULL;
423         static char *home_dir = NULL;
424         static char *home_drive = NULL;
425         static char *backend_in = NULL;
426         static char *backend_out = NULL;
427         static char *logon_script = NULL;
428         static char *profile_path = NULL;
429         static char *config_file = dyn_CONFIGFILE;
430         static char *new_debuglevel = NULL;
431         static char *account_policy = NULL;
432         static long int account_policy_value = 0;
433         BOOL account_policy_value_set = False;
434
435         struct pdb_context *in;
436         poptContext pc;
437         struct poptOption long_options[] = {
438                 POPT_AUTOHELP
439                 {"list",        'l',POPT_ARG_VAL, &list_users, 1, "list all users", NULL},
440                 {"verbose",     'v',POPT_ARG_VAL, &verbose, 1, "be verbose", NULL },
441                 {"smbpasswd-style",     'w',POPT_ARG_VAL, &spstyle, 1, "give output in smbpasswd style", NULL},
442                 {"user",        'u',POPT_ARG_STRING,&user_name, 0, "use username", "USER" },
443                 {"fullname",    'f',POPT_ARG_STRING,&full_name, 0, "set full name", NULL},
444                 {"homedir",     'h',POPT_ARG_STRING,&home_dir, 0, "set home directory", NULL},
445                 {"drive",       'd',POPT_ARG_STRING,&home_drive, 0, "set home drive", NULL},
446                 {"script",      's',POPT_ARG_STRING,&logon_script, 0, "set logon script", NULL},
447                 {"profile",     'p',POPT_ARG_STRING,&profile_path, 0, "set profile path", NULL},
448                 {"create",      'a',POPT_ARG_VAL,&add_user, 1, "create user", NULL},
449                 {"machine",     'm',POPT_ARG_VAL,&machine, 1,"account is a machine account",NULL},
450                 {"delete",      'x',POPT_ARG_VAL,&delete_user,1,"delete user",NULL},
451                 {"import",      'i',POPT_ARG_STRING,&backend_in,0,"use different passdb backend",NULL},
452                 {"export",      'e',POPT_ARG_STRING,&backend_out,0,"export user accounts to backend", NULL},
453                 {"debuglevel",  'D',POPT_ARG_STRING, &new_debuglevel,0,"set debuglevel",NULL},
454                 {"configfile",  'c',POPT_ARG_STRING, &config_file,0,"use different configuration file",NULL},
455                 {"account-policy",'P',POPT_ARG_STRING, &account_policy,0,"value of an account policy (like maximum password age)",NULL},
456                 {"value",       'V',POPT_ARG_LONG, &account_policy_value,'V',"set the account policy to this value", NULL},
457                 {0,0,0,0}
458         };
459         
460         setup_logging("pdbedit", True);
461         
462         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
463                             POPT_CONTEXT_KEEP_FIRST);
464         
465         while((opt = poptGetNextOpt(pc)) != -1) {
466                 switch (opt) {
467                 case 'V':
468                         account_policy_value_set = True;
469                         break;
470                 }
471         }
472         
473         if (new_debuglevel){
474                 debug_parse_levels(new_debuglevel);
475                 AllowDebugChange = False;
476         }
477         
478         if (!lp_load(config_file,True,False,False)) {
479                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
480                         config_file);
481                 exit(1);
482         }
483         
484         
485         setparms = (full_name || home_dir || home_drive || logon_script || profile_path);
486         
487         if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) + (backend_out?1:0) + (account_policy?1:0) > 1) {
488                 fprintf (stderr, "Incompatible options on command line!\n");
489                 exit(1);
490         }
491         
492         if (account_policy) {
493                 uint32 value;
494                 int field = account_policy_name_to_feildnum(account_policy);
495                 if (field == 0) {
496                         fprintf(stderr, "No account policy by that name\n");
497                         exit(1);
498                 }
499                 if (!account_policy_get(field, &value)){
500                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
501                         exit(1);
502                 }
503                 if (account_policy_value_set) {
504                         printf("account policy value for %s was %u\n", account_policy, value);
505                         if (!account_policy_set(field, account_policy_value)){
506                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
507                                 exit(1);
508                         }
509                         printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
510                         exit(0);
511                 } else {
512                         printf("account policy value for %s is %u\n", account_policy, value);
513                         exit(0);
514                 }
515         }
516
517         if (!backend_in) {
518                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&in, lp_passdb_backend()))){
519                         fprintf(stderr, "Can't initialize passdb backend.\n");
520                         return 1;
521                 }
522         } else {
523                 if (!NT_STATUS_IS_OK(make_pdb_context_string(&in, backend_in))){
524                         fprintf(stderr, "Can't initialize passdb backend.\n");
525                         return 1;
526                 }
527         }
528
529         if (add_user) {
530                 if (!user_name) {
531                         fprintf (stderr, "Username not specified! (use -u option)\n");
532                         return -1;
533                 }
534                 if (machine)
535                         return new_machine (in, user_name);
536                 else
537                         return new_user (in, user_name, full_name, home_dir, 
538                                          home_drive, logon_script, 
539                                          profile_path);
540         }
541
542         if (delete_user) {
543                 if (!user_name) {
544                         fprintf (stderr, "Username not specified! (use -u option)\n");
545                         return -1;
546                 }
547                 if (machine)
548                         return delete_machine_entry (in, user_name);
549                 else
550                         return delete_user_entry (in, user_name);
551         }
552
553         if (user_name) {
554                 if (setparms)
555                         return set_user_info (in, user_name, full_name,
556                                               home_dir,
557                                               home_drive,
558                                               logon_script,
559                                               profile_path);
560                 else
561                         return print_user_info (in, user_name, verbose, 
562                                                 spstyle);
563         }
564
565         if (list_users) 
566                 return print_users_list (in, verbose, spstyle);
567
568         if (backend_out)
569                 return export_database(in, backend_out);
570
571         poptPrintHelp(pc, stderr, 0);
572
573         return 1;
574 }
575
576