Allow -c to specify the location of the config file, and fix up some handling
[kai/samba.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 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_name(&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/NT username:     %s/%s\n",  pdb_get_username(sam_pwent),pdb_get_nt_username(sam_pwent));
80                 if (IS_SAM_UNIX_USER(sam_pwent)) {
81                         uid = pdb_get_uid(sam_pwent);
82                         gid = pdb_get_gid(sam_pwent);
83                         printf ("user ID/Group:        %d/%d\n", uid, gid);
84                 }
85                 printf ("user RID/GRID:        %u/%u\n", (unsigned int)pdb_get_user_rid(sam_pwent),
86                         (unsigned int)pdb_get_group_rid(sam_pwent));
87                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
88                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
89                 printf ("HomeDir Drive:        %s\n", pdb_get_dirdrive(sam_pwent));
90                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
91                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
92                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
93                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
94                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
95                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
96                 
97                 tmp = pdb_get_logon_time(sam_pwent);
98                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
99                 
100                 tmp = pdb_get_logoff_time(sam_pwent);
101                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
102                 
103                 tmp = pdb_get_kickoff_time(sam_pwent);
104                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
105                 
106                 tmp = pdb_get_pass_last_set_time(sam_pwent);
107                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
108                 
109                 tmp = pdb_get_pass_can_change_time(sam_pwent);
110                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
111                 
112                 tmp = pdb_get_pass_must_change_time(sam_pwent);
113                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
114                 
115         } else if (smbpwdstyle) {
116                 if (IS_SAM_UNIX_USER(sam_pwent)) {
117                         char lm_passwd[33];
118                         char nt_passwd[33];
119
120                         uid = pdb_get_uid(sam_pwent);
121                         pdb_sethexpwd(lm_passwd, 
122                                       pdb_get_lanman_passwd(sam_pwent), 
123                                       pdb_get_acct_ctrl(sam_pwent));
124                         pdb_sethexpwd(nt_passwd, 
125                                       pdb_get_nt_passwd(sam_pwent), 
126                                       pdb_get_acct_ctrl(sam_pwent));
127                         
128                         printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
129                                pdb_get_username(sam_pwent),
130                                uid,
131                                lm_passwd,
132                                nt_passwd,
133                                pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
134                                (uint32)pdb_get_pass_last_set_time(sam_pwent));
135                 } else {
136                         fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
137                 }
138         } else {
139                 if (IS_SAM_UNIX_USER(sam_pwent)) {
140                         printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent), 
141                                 pdb_get_fullname(sam_pwent));
142                 } else {        
143                         printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
144                 }
145         }
146
147         return 0;       
148 }
149
150 /*********************************************************
151  Get an Print User Info
152 **********************************************************/
153
154 static int print_user_info (struct pdb_context *in, char *username, BOOL verbosity, BOOL smbpwdstyle)
155 {
156         SAM_ACCOUNT *sam_pwent=NULL;
157         BOOL ret;
158         
159         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
160                 return -1;
161         }
162         
163         ret = in->pdb_getsampwnam (in, sam_pwent, username);
164
165         if (ret==False) {
166                 fprintf (stderr, "Username not found!\n");
167                 pdb_free_sam(&sam_pwent);
168                 return -1;
169         }
170         
171         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
172         pdb_free_sam(&sam_pwent);
173         
174         return ret;
175 }
176         
177 /*********************************************************
178  List Users
179 **********************************************************/
180 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
181 {
182         SAM_ACCOUNT *sam_pwent=NULL;
183         BOOL check, ret;
184         
185         check = in->pdb_setsampwent(in, False);
186         if (!check) {
187                 return 1;
188         }
189
190         check = True;
191         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
192
193         while (check && (ret = in->pdb_getsampwent (in, sam_pwent))) {
194                 if (verbosity)
195                         printf ("---------------\n");
196                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
197                 pdb_free_sam(&sam_pwent);
198                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
199         }
200         if (check) pdb_free_sam(&sam_pwent);
201         
202         in->pdb_endsampwent(in);
203         return 0;
204 }
205
206 /*********************************************************
207  Set User Info
208 **********************************************************/
209
210 static int set_user_info (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
211 {
212         SAM_ACCOUNT *sam_pwent=NULL;
213         BOOL ret;
214         
215         pdb_init_sam(&sam_pwent);
216         
217         ret = in->pdb_getsampwnam (in, sam_pwent, username);
218         if (ret==False) {
219                 fprintf (stderr, "Username not found!\n");
220                 pdb_free_sam(&sam_pwent);
221                 return -1;
222         }
223         
224         if (fullname)
225                 pdb_set_fullname(sam_pwent, fullname);
226         if (homedir)
227                 pdb_set_homedir(sam_pwent, homedir, True);
228         if (drive)
229                 pdb_set_dir_drive(sam_pwent,drive, True);
230         if (script)
231                 pdb_set_logon_script(sam_pwent, script, True);
232         if (profile)
233                 pdb_set_profile_path (sam_pwent, profile, True);
234         
235         if (in->pdb_update_sam_account (in, sam_pwent))
236                 print_user_info (in, username, True, False);
237         else {
238                 fprintf (stderr, "Unable to modify entry!\n");
239                 pdb_free_sam(&sam_pwent);
240                 return -1;
241         }
242         pdb_free_sam(&sam_pwent);
243         return 0;
244 }
245
246 /*********************************************************
247  Add New User
248 **********************************************************/
249 static int new_user (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
250 {
251         SAM_ACCOUNT *sam_pwent=NULL;
252         struct passwd  *pwd = NULL;
253         char *password1, *password2;
254         
255         ZERO_STRUCT(sam_pwent);
256
257         if ((pwd = getpwnam_alloc(username))) {
258                 pdb_init_sam_pw (&sam_pwent, pwd);
259                 passwd_free(&pwd);
260         } else {
261                 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
262                 pdb_init_sam(&sam_pwent);
263                 if (!pdb_set_username(sam_pwent, username)) {
264                         return False;
265                 }
266         }
267
268         password1 = getpass("new password:");
269         password2 = getpass("retype new password:");
270         if (strcmp (password1, password2)) {
271                  fprintf (stderr, "Passwords does not match!\n");
272                  pdb_free_sam (&sam_pwent);
273                  return -1;
274         }
275
276         pdb_set_plaintext_passwd(sam_pwent, password1);
277
278         if (fullname)
279                 pdb_set_fullname(sam_pwent, fullname);
280         if (homedir)
281                 pdb_set_homedir (sam_pwent, homedir, True);
282         if (drive)
283                 pdb_set_dir_drive (sam_pwent, drive, True);
284         if (script)
285                 pdb_set_logon_script(sam_pwent, script, True);
286         if (profile)
287                 pdb_set_profile_path (sam_pwent, profile, True);
288         
289         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
290         
291         if (in->pdb_add_sam_account (in, sam_pwent)) { 
292                 print_user_info (in, username, True, False);
293         } else {
294                 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
295                 pdb_free_sam (&sam_pwent);
296                 return -1;
297         }
298         pdb_free_sam (&sam_pwent);
299         return 0;
300 }
301
302 /*********************************************************
303  Add New Machine
304 **********************************************************/
305
306 static int new_machine (struct pdb_context *in, char *machinename)
307 {
308         SAM_ACCOUNT *sam_pwent=NULL;
309         char name[16];
310         char *password = NULL;
311         
312         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
313                 return -1;
314         }
315
316         if (machinename[strlen (machinename) -1] == '$')
317                 machinename[strlen (machinename) -1] = '\0';
318         
319         safe_strcpy (name, machinename, 16);
320         safe_strcat (name, "$", 16);
321         
322         string_set (&password, machinename);
323         strlower_m(password);
324         
325         pdb_set_plaintext_passwd (sam_pwent, password);
326
327         pdb_set_username (sam_pwent, name);
328         
329         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
330         
331         pdb_set_group_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS);
332         
333         if (in->pdb_add_sam_account (in, sam_pwent)) {
334                 print_user_info (in, name, True, False);
335         } else {
336                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
337                 pdb_free_sam (&sam_pwent);
338                 return -1;
339         }
340         pdb_free_sam (&sam_pwent);
341         return 0;
342 }
343
344 /*********************************************************
345  Delete user entry
346 **********************************************************/
347
348 static int delete_user_entry (struct pdb_context *in, char *username)
349 {
350         SAM_ACCOUNT *samaccount = NULL;
351
352         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
353                 return -1;
354         }
355
356         if (!in->pdb_getsampwnam(in, samaccount, username)) {
357                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
358                 return -1;
359         }
360
361         return in->pdb_delete_sam_account (in, samaccount);
362 }
363
364 /*********************************************************
365  Delete machine entry
366 **********************************************************/
367
368 static int delete_machine_entry (struct pdb_context *in, char *machinename)
369 {
370         char name[16];
371         SAM_ACCOUNT *samaccount = NULL;
372         
373         safe_strcpy (name, machinename, 16);
374         if (name[strlen(name)] != '$')
375                 safe_strcat (name, "$", 16);
376
377         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
378                 return -1;
379         }
380
381         if (!in->pdb_getsampwnam(in, samaccount, name)) {
382                 fprintf (stderr, "user %s does not exist in the passdb\n", name);
383                 return -1;
384         }
385
386         return in->pdb_delete_sam_account (in, samaccount);
387 }
388
389 /*********************************************************
390  Start here.
391 **********************************************************/
392
393 int main (int argc, char **argv)
394 {
395         static BOOL list_users = False;
396         static BOOL verbose = False;
397         static BOOL spstyle = False;
398         static BOOL setparms = False;
399         static BOOL machine = False;
400         static BOOL add_user = False;
401         static BOOL delete_user = False;
402         static BOOL import = False;
403         int opt;
404         static char *full_name = NULL;
405         static char *user_name = NULL;
406         static char *home_dir = NULL;
407         static char *home_drive = NULL;
408         static char *backend_in = NULL;
409         static char *backend_out = NULL;
410         static char *logon_script = NULL;
411         static char *profile_path = NULL;
412         static char *config_file = dyn_CONFIGFILE;
413         static int new_debuglevel = -1;
414
415         struct pdb_context *in;
416         poptContext pc;
417         struct poptOption long_options[] = {
418                 POPT_AUTOHELP
419                 {"list",        'l',POPT_ARG_VAL, &list_users, 1, "list all users", NULL},
420                 {"verbose",     'v',POPT_ARG_VAL, &verbose, 1, "be verbose", NULL },
421                 {"smbpasswd-style",     'w',POPT_ARG_VAL, &spstyle, 1, "give output in smbpasswd style", NULL},
422                 {"user",        'u',POPT_ARG_STRING,&user_name, 0, "use username", "USER" },
423                 {"fullname",    'f',POPT_ARG_STRING,&full_name, 0, "set full name", NULL},
424                 {"homedir",     'h',POPT_ARG_STRING,&home_dir, 0, "set home directory", NULL},
425                 {"drive",       'd',POPT_ARG_STRING,&home_drive, 0, "set home drive", NULL},
426                 {"script",      's',POPT_ARG_STRING,&logon_script, 0, "set logon script", NULL},
427                 {"profile",     'p',POPT_ARG_STRING,&profile_path, 0, "set profile path", NULL},
428                 {"create",      'a',POPT_ARG_VAL,&add_user, 1, "create user", NULL},
429                 {"machine",     'm',POPT_ARG_VAL,&machine, 1,"account is a machine account",NULL},
430                 {"delete",      'x',POPT_ARG_VAL,&delete_user,1,"delete user",NULL},
431                 {"import",      'i',POPT_ARG_STRING,&backend_in,0,"use different passdb backend",NULL},
432                 {"export",      'e',POPT_ARG_STRING,&backend_out,0,"export user accounts to backend", NULL},
433                 {"debuglevel",'D', POPT_ARG_INT, &new_debuglevel,0,"set debuglevel",NULL},
434                 {"configfile",'c',POPT_ARG_STRING, &config_file,0,"use different configuration file",NULL},
435                 {0,0,0,0}
436         };
437
438         DEBUGLEVEL = 1;
439         setup_logging("pdbedit", True);
440         AllowDebugChange = False;
441
442         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
443                                                 POPT_CONTEXT_KEEP_FIRST);
444
445         while((opt = poptGetNextOpt(pc)) != -1);
446
447         if (new_debuglevel != -1) {
448                 DEBUGLEVEL = new_debuglevel;
449         }
450
451         if (!lp_load(config_file,True,False,False)) {
452                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
453                                 config_file);
454                 exit(1);
455         }
456
457         if (!backend_in) {
458                 backend_in = lp_passdb_backend();
459         }
460
461         setparms = (full_name || home_dir || home_drive || logon_script || profile_path);
462
463         if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) + (backend_out?1:0) > 1) {
464                 fprintf (stderr, "Incompatible options on command line!\n");
465                 exit(1);
466         }
467
468
469         if(!NT_STATUS_IS_OK(make_pdb_context_name(&in, backend_in))){
470                 fprintf(stderr, "Can't initialize %s.\n", backend_in);
471                 return 1;
472         }
473
474         if (add_user) {
475                 if (!user_name) {
476                         fprintf (stderr, "Username not specified! (use -u option)\n");
477                         return -1;
478                 }
479                 if (machine)
480                         return new_machine (in, user_name);
481                 else
482                         return new_user (in, user_name, full_name, home_dir, 
483                                          home_drive, logon_script, 
484                                          profile_path);
485         }
486
487         if (delete_user) {
488                 if (!user_name) {
489                         fprintf (stderr, "Username not specified! (use -u option)\n");
490                         return -1;
491                 }
492                 if (machine)
493                         return delete_machine_entry (in, user_name);
494                 else
495                         return delete_user_entry (in, user_name);
496         }
497
498         if (user_name) {
499                 if (setparms)
500                         return set_user_info (in, user_name, full_name,
501                                               home_dir,
502                                               home_drive,
503                                               logon_script,
504                                               profile_path);
505                 else
506                         return print_user_info (in, user_name, verbose, 
507                                                 spstyle);
508         }
509
510         if (list_users) 
511                 return print_users_list (in, verbose, spstyle);
512
513         if (backend_out)
514                 return export_database(in, backend_out);
515
516         poptPrintHelp(pc, stderr, 0);
517
518         return 1;
519 }
520
521