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