Another patch from jelmer:
[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/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         errno = 0; /* testing --simo */
186         check = in->pdb_setsampwent(in, False);
187         if (check && errno == ENOENT) {
188                 fprintf (stderr,"Password database not found!\n");
189                 exit(1);
190         }
191
192         check = True;
193         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
194
195         while (check && (ret = in->pdb_getsampwent (in, sam_pwent))) {
196                 if (verbosity)
197                         printf ("---------------\n");
198                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
199                 pdb_free_sam(&sam_pwent);
200                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
201         }
202         if (check) pdb_free_sam(&sam_pwent);
203         
204         in->pdb_endsampwent(in);
205         return 0;
206 }
207
208 /*********************************************************
209  Set User Info
210 **********************************************************/
211
212 static int set_user_info (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
213 {
214         SAM_ACCOUNT *sam_pwent=NULL;
215         BOOL ret;
216         
217         pdb_init_sam(&sam_pwent);
218         
219         ret = in->pdb_getsampwnam (in, sam_pwent, username);
220         if (ret==False) {
221                 fprintf (stderr, "Username not found!\n");
222                 pdb_free_sam(&sam_pwent);
223                 return -1;
224         }
225         
226         if (fullname)
227                 pdb_set_fullname(sam_pwent, fullname);
228         if (homedir)
229                 pdb_set_homedir(sam_pwent, homedir, True);
230         if (drive)
231                 pdb_set_dir_drive(sam_pwent,drive, True);
232         if (script)
233                 pdb_set_logon_script(sam_pwent, script, True);
234         if (profile)
235                 pdb_set_profile_path (sam_pwent, profile, True);
236         
237         if (in->pdb_update_sam_account (in, sam_pwent))
238                 print_user_info (in, username, True, False);
239         else {
240                 fprintf (stderr, "Unable to modify entry!\n");
241                 pdb_free_sam(&sam_pwent);
242                 return -1;
243         }
244         pdb_free_sam(&sam_pwent);
245         return 0;
246 }
247
248 /*********************************************************
249  Add New User
250 **********************************************************/
251 static int new_user (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
252 {
253         SAM_ACCOUNT *sam_pwent=NULL;
254         struct passwd  *pwd = NULL;
255         char *password1, *password2;
256         
257         ZERO_STRUCT(sam_pwent);
258
259         if ((pwd = getpwnam_alloc(username))) {
260                 pdb_init_sam_pw (&sam_pwent, pwd);
261                 passwd_free(&pwd);
262         } else {
263                 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
264                 pdb_init_sam(&sam_pwent);
265                 if (!pdb_set_username(sam_pwent, username)) {
266                         return False;
267                 }
268         }
269
270         password1 = getpass("new password:");
271         password2 = getpass("retype new password:");
272         if (strcmp (password1, password2)) {
273                  fprintf (stderr, "Passwords does not match!\n");
274                  pdb_free_sam (&sam_pwent);
275                  return -1;
276         }
277
278         pdb_set_plaintext_passwd(sam_pwent, password1);
279
280         if (fullname)
281                 pdb_set_fullname(sam_pwent, fullname);
282         if (homedir)
283                 pdb_set_homedir (sam_pwent, homedir, True);
284         if (drive)
285                 pdb_set_dir_drive (sam_pwent, drive, True);
286         if (script)
287                 pdb_set_logon_script(sam_pwent, script, True);
288         if (profile)
289                 pdb_set_profile_path (sam_pwent, profile, True);
290         
291         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
292         
293         if (in->pdb_add_sam_account (in, sam_pwent)) { 
294                 print_user_info (in, username, True, False);
295         } else {
296                 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
297                 pdb_free_sam (&sam_pwent);
298                 return -1;
299         }
300         pdb_free_sam (&sam_pwent);
301         return 0;
302 }
303
304 /*********************************************************
305  Add New Machine
306 **********************************************************/
307
308 static int new_machine (struct pdb_context *in, char *machinename)
309 {
310         SAM_ACCOUNT *sam_pwent=NULL;
311         char name[16];
312         char *password = NULL;
313         
314         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
315                 return -1;
316         }
317
318         if (machinename[strlen (machinename) -1] == '$')
319                 machinename[strlen (machinename) -1] = '\0';
320         
321         safe_strcpy (name, machinename, 16);
322         safe_strcat (name, "$", 16);
323         
324         string_set (&password, machinename);
325         strlower_m(password);
326         
327         pdb_set_plaintext_passwd (sam_pwent, password);
328
329         pdb_set_username (sam_pwent, name);
330         
331         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
332         
333         pdb_set_group_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS);
334         
335         if (in->pdb_add_sam_account (in, sam_pwent)) {
336                 print_user_info (in, name, True, False);
337         } else {
338                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
339                 pdb_free_sam (&sam_pwent);
340                 return -1;
341         }
342         pdb_free_sam (&sam_pwent);
343         return 0;
344 }
345
346 /*********************************************************
347  Delete user entry
348 **********************************************************/
349
350 static int delete_user_entry (struct pdb_context *in, char *username)
351 {
352         SAM_ACCOUNT *samaccount = NULL;
353
354         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
355                 return -1;
356         }
357
358         if (!in->pdb_getsampwnam(in, samaccount, username)) {
359                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
360                 return -1;
361         }
362
363         return in->pdb_delete_sam_account (in, samaccount);
364 }
365
366 /*********************************************************
367  Delete machine entry
368 **********************************************************/
369
370 static int delete_machine_entry (struct pdb_context *in, char *machinename)
371 {
372         char name[16];
373         SAM_ACCOUNT *samaccount = NULL;
374         
375         safe_strcpy (name, machinename, 16);
376         if (name[strlen(name)] != '$')
377                 safe_strcat (name, "$", 16);
378
379         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
380                 return -1;
381         }
382
383         if (!in->pdb_getsampwnam(in, samaccount, name)) {
384                 fprintf (stderr, "user %s does not exist in the passdb\n", name);
385                 return -1;
386         }
387
388         return in->pdb_delete_sam_account (in, samaccount);
389 }
390
391 /*********************************************************
392  Start here.
393 **********************************************************/
394
395 int main (int argc, char **argv)
396 {
397         static BOOL list_users = False;
398         static BOOL verbose = False;
399         static BOOL spstyle = False;
400         static BOOL setparms = False;
401         static BOOL machine = False;
402         static BOOL add_user = False;
403         static BOOL delete_user = False;
404         static BOOL import = False;
405         int opt;
406         static char *full_name = NULL;
407         static char *user_name = NULL;
408         static char *home_dir = NULL;
409         static char *home_drive = NULL;
410         static char *backend_in = NULL;
411         static char *backend_out = NULL;
412         static char *logon_script = NULL;
413         static char *profile_path = NULL;
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,&DEBUGLEVEL,0,"set debuglevel",NULL},
434                 {0,0,0,0}
435         };
436
437         DEBUGLEVEL = 1;
438         setup_logging("pdbedit", True);
439         AllowDebugChange = False;
440
441         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
442                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
443                                 dyn_CONFIGFILE);
444                 exit(1);
445         }
446
447         backend_in = lp_passdb_backend();
448
449         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
450                                                 POPT_CONTEXT_KEEP_FIRST);
451
452         while((opt = poptGetNextOpt(pc)) != -1);
453
454         setparms = (full_name || home_dir || home_drive || logon_script || profile_path);
455
456         if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) + (backend_out?1:0) > 1) {
457                 fprintf (stderr, "Incompatible options on command line!\n");
458                 exit(1);
459         }
460
461
462         if(!NT_STATUS_IS_OK(make_pdb_context_name(&in, backend_in))){
463                 fprintf(stderr, "Can't initialize %s.\n", backend_in);
464                 return 1;
465         }
466
467         if (add_user) {
468                 if (!user_name) {
469                         fprintf (stderr, "Username not specified! (use -u option)\n");
470                         return -1;
471                 }
472                 if (machine)
473                         return new_machine (in, user_name);
474                 else
475                         return new_user (in, user_name, full_name, home_dir, 
476                                          home_drive, logon_script, 
477                                          profile_path);
478         }
479
480         if (delete_user) {
481                 if (!user_name) {
482                         fprintf (stderr, "Username not specified! (use -u option)\n");
483                         return -1;
484                 }
485                 if (machine)
486                         return delete_machine_entry (in, user_name);
487                 else
488                         return delete_user_entry (in, user_name);
489         }
490
491         if (user_name) {
492                 if (setparms)
493                         return set_user_info (in, user_name, full_name,
494                                               home_dir,
495                                               home_drive,
496                                               logon_script,
497                                               profile_path);
498                 else
499                         return print_user_info (in, user_name, verbose, 
500                                                 spstyle);
501         }
502
503         if (list_users) 
504                 return print_users_list (in, verbose, spstyle);
505
506         if (backend_out)
507                 return export_database(in, backend_out);
508
509         poptPrintHelp(pc, stderr, 0);
510
511         return 1;
512 }
513
514