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