Give pdbedit a -D paramater for setting the DEBUGLEVEL (makes debugging passdb
[kai/samba.git] / source / utils / pdbedit.c
1 /* 
2    Unix SMB/Netbios implementation.
3    passdb editing frontend
4    Version 3.0
5    
6    Copyright (C) Simo Sorce      2000
7    Copyright (C) Andrew Bartlett 2001   
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  * Next two lines needed for SunOS and don't
31  * hurt anything else...
32  */
33 extern char *optarg;
34 extern int optind;
35
36 /*********************************************************
37  Print command usage on stderr and die.
38 **********************************************************/
39 static void usage(void)
40 {
41         if (getuid() == 0) {
42                 printf("pdbedit options\n");
43         } else {
44                 printf("You need to be root to use this tool!\n");
45         }
46         printf("(actually to add a user you need to use smbpasswd)\n");
47         printf("options:\n");
48         printf("  -l                   list usernames\n");
49         printf("     -v                verbose output\n");
50         printf("     -w                smbpasswd file style\n");
51         printf("  -u username          print user's info\n");
52         printf("     -f fullname       set Full Name\n");
53         printf("     -h homedir        set home directory\n");
54         printf("     -d drive          set home dir drive\n");
55         printf("     -s script         set logon script\n");
56         printf("     -p profile        set profile path\n");
57         printf("  -a                   create new account\n");
58         printf("     -m                it is a machine trust\n");
59         printf("  -x                   delete this user\n");
60         printf("  -i file              import account from file (smbpasswd style)\n");
61         printf("  -D debuglevel        set DEBUGELEVEL (default = 1)\n");
62         exit(1);
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
74         /* TODO: chaeck if entry is a user or a workstation */
75         if (!sam_pwent) return -1;
76         
77         if (verbosity) {
78                 printf ("username:       %s\n",  pdb_get_username(sam_pwent));
79                 if (IS_SAM_UNIX_USER(sam_pwent)) {
80                         uid = pdb_get_uid(sam_pwent);
81                         gid = pdb_get_gid(sam_pwent);
82                         printf ("user ID/Group:  %d/%d\n", uid, gid);
83                 }
84                 printf ("user RID/GRID:  %u/%u\n", (unsigned int)pdb_get_user_rid(sam_pwent),
85                         (unsigned int)pdb_get_group_rid(sam_pwent));
86                 printf ("Full Name:      %s\n", pdb_get_fullname(sam_pwent));
87                 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
88                 printf ("HomeDir Drive:  %s\n", pdb_get_dirdrive(sam_pwent));
89                 printf ("Logon Script:   %s\n", pdb_get_logon_script(sam_pwent));
90                 printf ("Profile Path:   %s\n", pdb_get_profile_path(sam_pwent));
91         } else if (smbpwdstyle) {
92                 if (IS_SAM_UNIX_USER(sam_pwent)) {
93                         char lm_passwd[33];
94                         char nt_passwd[33];
95
96                         uid = pdb_get_uid(sam_pwent);
97                         pdb_sethexpwd(lm_passwd, 
98                                       pdb_get_lanman_passwd(sam_pwent), 
99                                       pdb_get_acct_ctrl(sam_pwent));
100                         pdb_sethexpwd(nt_passwd, 
101                                       pdb_get_nt_passwd(sam_pwent), 
102                                       pdb_get_acct_ctrl(sam_pwent));
103                         
104                         printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
105                                pdb_get_username(sam_pwent),
106                                uid,
107                                lm_passwd,
108                                nt_passwd,
109                                pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
110                                (uint32)pdb_get_pass_last_set_time(sam_pwent));
111                 } else {
112                         fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
113                 }
114         } else {
115                 if (IS_SAM_UNIX_USER(sam_pwent)) {
116                         printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent), 
117                                 pdb_get_fullname(sam_pwent));
118                 } else {        
119                         printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
120                 }
121         }
122
123         return 0;       
124 }
125
126 /*********************************************************
127  Get an Print User Info
128 **********************************************************/
129
130 static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
131 {
132         SAM_ACCOUNT *sam_pwent=NULL;
133         BOOL ret;
134         
135         pdb_init_sam(&sam_pwent);
136         
137         ret = pdb_getsampwnam (sam_pwent, username);
138
139         if (ret==False) {
140                 fprintf (stderr, "Username not found!\n");
141                 pdb_free_sam(&sam_pwent);
142                 return -1;
143         }
144         
145         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
146         pdb_free_sam(&sam_pwent);
147         
148         return ret;
149 }
150         
151 /*********************************************************
152  List Users
153 **********************************************************/
154 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
155 {
156         SAM_ACCOUNT *sam_pwent=NULL;
157         BOOL ret;
158         
159         pdb_init_sam(&sam_pwent);
160         errno = 0; /* testing --simo */
161         ret = pdb_setsampwent(False);
162         if (ret && errno == ENOENT) {
163                 fprintf (stderr,"Password database not found!\n");
164                 exit(1);
165         }
166         pdb_free_sam(&sam_pwent);
167
168         while ((NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)) 
169                 && (ret = pdb_getsampwent (sam_pwent)))) {
170                 if (verbosity)
171                         printf ("---------------\n");
172                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
173                 pdb_free_sam(&sam_pwent);
174         }
175         pdb_free_sam(&sam_pwent);
176         
177         pdb_endsampwent ();
178         return 0;
179 }
180
181 /*********************************************************
182  Set User Info
183 **********************************************************/
184
185 static int set_user_info (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
186 {
187         SAM_ACCOUNT *sam_pwent=NULL;
188         BOOL ret;
189         
190         pdb_init_sam(&sam_pwent);
191         
192         ret = pdb_getsampwnam (sam_pwent, username);
193         if (ret==False) {
194                 fprintf (stderr, "Username not found!\n");
195                 pdb_free_sam(&sam_pwent);
196                 return -1;
197         }
198         
199         if (fullname)
200                 pdb_set_fullname(sam_pwent, fullname);
201         if (homedir)
202                 pdb_set_homedir(sam_pwent, homedir, True);
203         if (drive)
204                 pdb_set_dir_drive(sam_pwent,drive, True);
205         if (script)
206                 pdb_set_logon_script(sam_pwent, script, True);
207         if (profile)
208                 pdb_set_profile_path (sam_pwent, profile, True);
209         
210         if (pdb_update_sam_account (sam_pwent))
211                 print_user_info (username, True, False);
212         else {
213                 fprintf (stderr, "Unable to modify entry!\n");
214                 pdb_free_sam(&sam_pwent);
215                 return -1;
216         }
217         pdb_free_sam(&sam_pwent);
218         return 0;
219 }
220
221 /*********************************************************
222  Add New User
223 **********************************************************/
224 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
225 {
226         SAM_ACCOUNT *sam_pwent=NULL;
227         struct passwd  *pwd = NULL;
228         char *password1, *password2;
229         
230         ZERO_STRUCT(sam_pwent);
231
232         if ((pwd = getpwnam_alloc(username))) {
233                 pdb_init_sam_pw (&sam_pwent, pwd);
234                 passwd_free(&pwd);
235         } else {
236                 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
237                 pdb_init_sam(&sam_pwent);
238                 if (!pdb_set_username(sam_pwent, username)) {
239                         return False;
240                 }
241         }
242
243         password1 = getpass("new password:");
244         password2 = getpass("retype new password:");
245         if (strcmp (password1, password2)) {
246                  fprintf (stderr, "Passwords does not match!\n");
247                  pdb_free_sam (&sam_pwent);
248                  return -1;
249         }
250
251         pdb_set_plaintext_passwd(sam_pwent, password1);
252
253         if (fullname)
254                 pdb_set_fullname(sam_pwent, fullname);
255         if (homedir)
256                 pdb_set_homedir (sam_pwent, homedir, True);
257         if (drive)
258                 pdb_set_dir_drive (sam_pwent, drive, True);
259         if (script)
260                 pdb_set_logon_script(sam_pwent, script, True);
261         if (profile)
262                 pdb_set_profile_path (sam_pwent, profile, True);
263         
264         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
265         
266         if (pdb_add_sam_account (sam_pwent)) { 
267                 print_user_info (username, True, False);
268         } else {
269                 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
270                 pdb_free_sam (&sam_pwent);
271                 return -1;
272         }
273         pdb_free_sam (&sam_pwent);
274         return 0;
275 }
276
277 /*********************************************************
278  Add New Machine
279 **********************************************************/
280
281 static int new_machine (char *machinename)
282 {
283         SAM_ACCOUNT *sam_pwent=NULL;
284         char name[16];
285         char *password = NULL;
286         
287         pdb_init_sam (&sam_pwent);
288
289         if (machinename[strlen (machinename) -1] == '$')
290                 machinename[strlen (machinename) -1] = '\0';
291         
292         safe_strcpy (name, machinename, 16);
293         safe_strcat (name, "$", 16);
294         
295         string_set (&password, machinename);
296         strlower_m(password);
297         
298         pdb_set_plaintext_passwd (sam_pwent, password);
299
300         pdb_set_username (sam_pwent, name);
301         
302         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
303         
304         if (pdb_add_sam_account (sam_pwent)) {
305                 print_user_info (name, True, False);
306         } else {
307                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
308                 pdb_free_sam (&sam_pwent);
309                 return -1;
310         }
311         pdb_free_sam (&sam_pwent);
312         return 0;
313 }
314
315 /*********************************************************
316  Delete user entry
317 **********************************************************/
318
319 static int delete_user_entry (char *username)
320 {
321         SAM_ACCOUNT *samaccount;
322
323         pdb_init_sam(&samaccount);
324
325         if (!pdb_getsampwnam(samaccount, username)) {
326                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
327                 return -1;
328         }
329
330         return pdb_delete_sam_account (samaccount);
331 }
332
333 /*********************************************************
334  Delete machine entry
335 **********************************************************/
336
337 static int delete_machine_entry (char *machinename)
338 {
339         char name[16];
340         SAM_ACCOUNT *samaccount;
341         
342         safe_strcpy (name, machinename, 16);
343         if (name[strlen(name)] != '$')
344                 safe_strcat (name, "$", 16);
345
346         pdb_init_sam(&samaccount);
347
348         if (!pdb_getsampwnam(samaccount, name)) {
349                 fprintf (stderr, "user %s does not exist in the passdb\n", name);
350                 return -1;
351         }
352
353         return pdb_delete_sam_account (samaccount);
354 }
355
356 /*********************************************************
357  Import smbpasswd style file
358 **********************************************************/
359
360 static int import_users (char *filename)
361 {
362         FILE *fp = NULL;
363         SAM_ACCOUNT *sam_pwent = NULL;
364         static pstring  user_name;
365         static unsigned char smbpwd[16];
366         static unsigned char smbntpwd[16];
367         char linebuf[256];
368         size_t linebuf_len;
369         unsigned char c;
370         unsigned char *p;
371         long uidval;
372         int line = 0;
373         int good = 0;
374         struct passwd *pwd;
375
376         if((fp = sys_fopen(filename, "rb")) == NULL) {
377                 fprintf (stderr, "%s\n", strerror (ferror (fp)));
378                 return -1;
379         }
380         
381         while (!feof(fp)) {
382                 /*Get a new line*/
383                 linebuf[0] = '\0';
384                 fgets(linebuf, 256, fp);
385                 if (ferror(fp)) {
386                         fprintf (stderr, "%s\n", strerror (ferror (fp)));
387                         return -1;
388                 }
389                 if ((linebuf_len = strlen(linebuf)) == 0) {
390                         line++;
391                         continue;
392                 }
393                 if (linebuf[linebuf_len - 1] != '\n') {
394                         c = '\0';
395                         while (!ferror(fp) && !feof(fp)) {
396                                 c = fgetc(fp);
397                                 if (c == '\n') break;
398                         }
399                 } else
400                         linebuf[linebuf_len - 1] = '\0';
401                 linebuf[linebuf_len] = '\0';
402                 if ((linebuf[0] == 0) && feof(fp)) {
403                         /*end of file!!*/
404                         return 0;
405                 }
406                 line++;
407                 if (linebuf[0] == '#' || linebuf[0] == '\0')
408                         continue;
409                 
410                 /* Get user name */
411                 p = (unsigned char *) strchr_m(linebuf, ':');
412                 if (p == NULL) {
413                         fprintf (stderr, "Error: malformed password entry at line %d !!\n", line);
414                         continue;
415                 }
416                 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
417                 user_name[PTR_DIFF(p, linebuf)] = '\0';
418
419                 /* Get smb uid. */
420                 p++;
421                 if(*p == '-') {
422                         fprintf (stderr, "Error: negative uid at line %d\n", line);
423                         continue;
424                 }
425                 if (!isdigit(*p)) {
426                         fprintf (stderr, "Error: malformed password entry at line %d (uid not number)\n", line);
427                         continue;
428                 }
429                 uidval = atoi((char *) p);
430                 while (*p && isdigit(*p)) p++;
431                 if (*p != ':') {
432                         fprintf (stderr, "Error: malformed password entry at line %d (no : after uid)\n", line);
433                         continue;
434                 }
435                 if(!(pwd = sys_getpwnam(user_name))) {
436                         fprintf(stderr, "User %s does not \
437 exist in system password file (usually /etc/passwd). Cannot add \
438 account without a valid local system user.\n", user_name);
439                         return False;
440                 }
441
442                 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pwent, pwd))) {
443                         fprintf(stderr, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
444                         return False;
445                 }
446
447                 /* Get passwords */
448                 p++;
449                 if (*p == '*' || *p == 'X') {
450                         /* Password deliberately invalid */
451                         fprintf (stderr, "Warning: entry invalidated for user %s\n", user_name);
452                         pdb_set_lanman_passwd(sam_pwent, NULL);
453                         pdb_set_nt_passwd(sam_pwent,NULL);
454                         pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_DISABLED);
455                 } else {
456                         if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
457                                 fprintf (stderr, "Error: malformed password entry at line %d (password too short)\n",line);
458                                 pdb_free_sam (&sam_pwent);
459                                 continue;
460                         }
461                         if (p[32] != ':') {
462                                 fprintf (stderr, "Error: malformed password entry at line %d (no terminating :)\n",line);
463                                 pdb_free_sam (&sam_pwent);
464                                 continue;
465                         }
466                         if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
467                                 pdb_set_lanman_passwd(sam_pwent, NULL);
468                                 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_PWNOTREQ);
469                         } else {
470                                 if (!pdb_gethexpwd((char *)p, smbpwd)) {
471                                         fprintf (stderr, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line);
472                                         pdb_free_sam (&sam_pwent);
473                                         continue;
474                                 }
475                                 pdb_set_lanman_passwd(sam_pwent, smbpwd);
476                         }
477                         /* NT password */
478                         p += 33;
479                         if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
480                                 if (*p != '*' && *p != 'X') {
481                                         if (pdb_gethexpwd((char *)p,smbntpwd)) {
482                                                 pdb_set_nt_passwd(sam_pwent, smbntpwd);
483                                         }
484                                 }
485                                 p += 33;
486                         }
487                 }
488
489                 /* Get ACCT_CTRL field if any */
490                 if (*p == '[') {
491                         uint16 acct_ctrl;
492                         unsigned char *end_p = (unsigned char *)strchr_m((char *)p, ']');
493                         
494                         acct_ctrl = pdb_decode_acct_ctrl((char*)p);
495                         if (acct_ctrl)
496                                 acct_ctrl = ACB_NORMAL;
497
498                         pdb_set_acct_ctrl(sam_pwent, acct_ctrl);
499                         
500                         /* Get last change time */
501                         if(end_p)
502                                 p = end_p + 1;
503                         if(*p == ':') {
504                                 p++;
505                                 if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0)) {
506                                         int i;
507                                         
508                                         p += 4;
509                                         for(i = 0; i < 8; i++) {
510                                                 if(p[i] == '\0' || !isxdigit(p[i])) break;
511                                         }
512                                         if(i == 8) {
513                                                  pdb_set_pass_last_set_time (sam_pwent, (time_t)strtol((char *)p, NULL, 16));
514                                         }
515                                 }
516                         }
517                 }
518
519                  /* Now ADD the entry */
520                 if (!(pdb_add_sam_account (sam_pwent))) {
521                         fprintf (stderr, "Unable to add user entry!\n");
522                         pdb_free_sam (&sam_pwent);
523                         continue;
524                 }
525                 printf ("%s imported!\n", user_name);
526                 good++;
527                 pdb_free_sam (&sam_pwent);
528         }
529         printf ("%d lines read.\n%d entryes imported\n", line, good);
530         return 0;
531 }
532
533 /*********************************************************
534  Start here.
535 **********************************************************/
536
537 int main (int argc, char **argv)
538 {
539         int ch;
540         BOOL list_users = False;
541         BOOL verbose = False;
542         BOOL spstyle = False;
543         BOOL setparms = False;
544         BOOL machine = False;
545         BOOL add_user = False;
546         BOOL delete_user = False;
547         BOOL import = False;
548         char *user_name = NULL;
549         char *full_name = NULL;
550         char *home_dir = NULL;
551         char *home_drive = NULL;
552         char *logon_script = NULL;
553         char *profile_path = NULL;
554         char *smbpasswd = NULL;
555
556         setup_logging("pdbedit", True);
557
558         if (argc < 2) {
559                 usage();
560                 return 0;
561         }
562         
563         DEBUGLEVEL = 1;
564         AllowDebugChange = False;
565
566         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
567                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
568                         dyn_CONFIGFILE);
569                 exit(1);
570         }
571         
572         if(!initialize_password_db(True)) {
573                 fprintf(stderr, "Can't setup password database vectors.\n");
574                 exit(1);
575         }
576         
577         while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwxD:")) != EOF) {
578                 switch(ch) {
579                 case 'a':
580                         add_user = True;
581                         break;
582                 case 'm':
583                         machine = True;
584                         break;
585                 case 'l':
586                         list_users = True;
587                         break;
588                 case 'v':
589                         verbose = True;
590                         break;
591                 case 'w':
592                         spstyle = True;
593                         break;
594                 case 'u':
595                         user_name = optarg;
596                         break;
597                 case 'f':
598                         setparms = True;
599                         full_name = optarg;
600                         break;
601                 case 'h':
602                         setparms = True;
603                         home_dir = optarg;
604                         break;
605                 case 'd':
606                         setparms = True;
607                         home_drive = optarg;
608                         break;
609                 case 's':
610                         setparms = True;
611                         logon_script = optarg;
612                         break;
613                 case 'p':
614                         setparms = True;
615                         profile_path = optarg;
616                         break;
617                 case 'x':
618                         delete_user = True;
619                         break;
620                 case 'i':
621                         import = True;
622                         smbpasswd = optarg;
623                         break;
624                 case 'D':
625                         DEBUGLEVEL = atoi(optarg);
626                         break;
627                 default:
628                         usage();
629                 }
630         }
631         if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1) {
632                 fprintf (stderr, "Incompatible options on command line!\n");
633                 usage();
634                 exit(1);
635         }
636
637         if (add_user) {
638                 if (!user_name) {
639                         fprintf (stderr, "Username not specified! (use -u option)\n");
640                         return -1;
641                 }
642                 if (machine)
643                         return new_machine (user_name);
644                 else
645                         return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
646         }
647
648         if (delete_user) {
649                 if (!user_name) {
650                         fprintf (stderr, "Username not specified! (use -u option)\n");
651                         return -1;
652                 }
653                 if (machine)
654                         return delete_machine_entry (user_name);
655                 else
656                         return delete_user_entry (user_name);
657         }
658         
659         if (user_name) {
660                 if (setparms)
661                         set_user_info ( user_name, full_name,
662                                                 home_dir,
663                                                 home_drive,
664                                                 logon_script,
665                                                 profile_path);
666                 else
667                         return print_user_info (user_name, verbose, spstyle);
668                 
669                 return 0;
670         }
671
672         
673         if (list_users) 
674                 return print_users_list (verbose, spstyle);
675         
676         if (import) 
677                 return import_users (smbpasswd); 
678         
679         usage();
680
681         return 0;
682 }