90067a733ba2893ac470509188c325723e56a69e
[tprouty/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 /* base uid for trust accounts is set to 60000 ! 
25  * May be we should add the defines in smb.h to make it possible having 
26  * different values on different platforms?
27  */
28
29 #define BASE_MACHINE_UID 60000
30 #define MAX_MACHINE_UID 65500 /* 5500 trust accounts aren't enough? */
31
32 #include "includes.h"
33
34 extern pstring global_myname;
35
36 /*
37  * Next two lines needed for SunOS and don't
38  * hurt anything else...
39  */
40 extern char *optarg;
41 extern int optind;
42
43 /*********************************************************
44  Print command usage on stderr and die.
45 **********************************************************/
46 static void usage(void)
47 {
48         if (getuid() == 0) {
49                 printf("tdbedit options\n");
50         } else {
51                 printf("You need to be root to use this tool!\n");
52         }
53         printf("(actually to add a user you need to use smbpasswd)\n");
54         printf("options:\n");
55         printf("  -l                   list usernames\n");
56         printf("     -v                verbose output\n");
57         printf("     -w                smbpasswd file style\n");
58         printf("  -u username          print user's info\n");
59         printf("     -f fullname       set Full Name\n");
60         printf("     -h homedir        set home directory\n");
61         printf("     -d drive          set home dir drive\n");
62         printf("     -s script         set logon script\n");
63         printf("     -p profile        set profile path\n");
64         printf("  -a                   create new account\n");
65         printf("     -m                it is a machine trust\n");
66         printf("  -x                   delete this user\n");
67         printf("  -i file              import account from file (smbpasswd style)\n");
68         exit(1);
69 }
70
71 /*********************************************************
72  Print info from sam structure
73 **********************************************************/
74
75 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
76 {
77         uid_t *puid;
78         gid_t *pgid;
79
80         /* TODO: chaeck if entry is a user or a workstation */
81         if (!sam_pwent) return -1;
82         
83         if (verbosity) {
84                 printf ("username:       %s\n",  pdb_get_username(sam_pwent));
85                 if ((puid = pdb_get_uid(sam_pwent)) && (pgid = pdb_get_gid(sam_pwent))) {
86                         printf ("user ID/Group:  %d/%d\n", (unsigned int)*puid,
87                                 (unsigned int)*pgid);
88                 }
89                 printf ("user RID/GRID:  %u/%u\n", (unsigned int)sam_pwent->user_rid,
90                         (unsigned int)sam_pwent->group_rid);
91                 printf ("Full Name:      %s\n", pdb_get_fullname(sam_pwent));
92                 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
93                 printf ("HomeDir Drive:  %s\n", pdb_get_dirdrive(sam_pwent));
94                 printf ("Logon Script:   %s\n", pdb_get_logon_script(sam_pwent));
95                 printf ("Profile Path:   %s\n", pdb_get_profile_path(sam_pwent));
96         } else if (smbpwdstyle) {
97                 if ((puid = pdb_get_uid(sam_pwent))) {
98                         char lm_passwd[33];
99                         char nt_passwd[33];
100                         pdb_sethexpwd(lm_passwd, 
101                                       pdb_get_lanman_passwd(sam_pwent), 
102                                       pdb_get_acct_ctrl(sam_pwent));
103                         pdb_sethexpwd(nt_passwd, 
104                                       pdb_get_nt_passwd(sam_pwent), 
105                                       pdb_get_acct_ctrl(sam_pwent));
106                         
107                         printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
108                                pdb_get_username(sam_pwent),
109                                (unsigned int)*puid,
110                                lm_passwd,
111                                nt_passwd,
112                                pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
113                                (uint32)pdb_get_pass_last_set_time(sam_pwent));
114                 } else {
115                         fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
116                 }
117         } else {
118                 if ((puid = pdb_get_uid(sam_pwent))) {          
119                         printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), *puid, pdb_get_fullname(sam_pwent));
120                 } else {        
121                         printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
122                 }
123         }
124
125         return 0;       
126 }
127
128 /*********************************************************
129  Get an Print User Info
130 **********************************************************/
131
132 static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
133 {
134         SAM_ACCOUNT *sam_pwent=NULL;
135         BOOL ret;
136         
137         pdb_init_sam(&sam_pwent);
138         
139         ret = pdb_getsampwnam (sam_pwent, username);
140
141         if (ret==False) {
142                 fprintf (stderr, "Username not found!\n");
143                 pdb_free_sam(&sam_pwent);
144                 return -1;
145         }
146         
147         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
148         pdb_free_sam(&sam_pwent);
149         
150         return ret;
151 }
152         
153 /*********************************************************
154  List Users
155 **********************************************************/
156 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
157 {
158         SAM_ACCOUNT *sam_pwent=NULL;
159         BOOL ret;
160         
161         pdb_init_sam(&sam_pwent);
162         errno = 0; /* testing --simo */
163         ret = pdb_setsampwent(False);
164         if (ret && errno == ENOENT) {
165                 fprintf (stderr,"Password database not found!\n");
166                 pdb_free_sam(&sam_pwent);
167                 exit(1);
168         }
169
170         while ((ret = pdb_getsampwent (sam_pwent))) {
171                 if (verbosity)
172                         printf ("---------------\n");
173                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
174                 pdb_reset_sam(sam_pwent);
175         }
176         
177         pdb_endsampwent ();
178         pdb_free_sam(&sam_pwent);
179         return 0;
180 }
181
182 /*********************************************************
183  Set User Info
184 **********************************************************/
185
186 static int set_user_info (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
187 {
188         SAM_ACCOUNT *sam_pwent=NULL;
189         BOOL ret;
190         
191         pdb_init_sam(&sam_pwent);
192         
193         ret = pdb_getsampwnam (sam_pwent, username);
194         if (ret==False) {
195                 fprintf (stderr, "Username not found!\n");
196                 pdb_free_sam(&sam_pwent);
197                 return -1;
198         }
199         
200         if (fullname)
201                 pdb_set_fullname(sam_pwent, fullname);
202         if (homedir)
203                 pdb_set_homedir(sam_pwent, homedir);
204         if (drive)
205                 pdb_set_dir_drive(sam_pwent,drive);
206         if (script)
207                 pdb_set_logon_script(sam_pwent, script);
208         if (profile)
209                 pdb_set_profile_path (sam_pwent, profile);
210         
211         if (pdb_update_sam_account (sam_pwent, True))
212                 print_user_info (username, True, False);
213         else {
214                 fprintf (stderr, "Unable to modify entry!\n");
215                 pdb_free_sam(&sam_pwent);
216                 return -1;
217         }
218         pdb_free_sam(&sam_pwent);
219         return 0;
220 }
221
222 /*********************************************************
223  Add New User
224 **********************************************************/
225 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
226 {
227         SAM_ACCOUNT *sam_pwent=NULL;
228         struct passwd  *pwd = NULL;
229         char *password1, *password2;
230         
231         ZERO_STRUCT(sam_pwent);
232
233         if (!(pwd = sys_getpwnam(username))) {
234                 fprintf (stderr, "User %s does not exist in system passwd!\n", username);
235                 return -1;
236         }
237         
238         pdb_init_sam_pw (&sam_pwent, pwd);
239
240         password1 = getpass("new password:");
241         password2 = getpass("retype new password:");
242         if (strcmp (password1, password2)) {
243                  fprintf (stderr, "Passwords does not match!\n");
244                  pdb_free_sam (&sam_pwent);
245                  return -1;
246         }
247
248         pdb_set_plaintext_passwd(sam_pwent, password1);
249
250         pdb_set_username(sam_pwent, username);
251         if (fullname)
252                 pdb_set_fullname(sam_pwent, fullname);
253         if (homedir)
254                 pdb_set_homedir (sam_pwent, homedir);
255         if (drive)
256                 pdb_set_dir_drive (sam_pwent, drive);
257         if (script)
258                 pdb_set_logon_script(sam_pwent, script);
259         if (profile)
260                 pdb_set_profile_path (sam_pwent, profile);
261         
262         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
263         
264         if (pdb_add_sam_account (sam_pwent)) { 
265                 print_user_info (username, True, False);
266         } else {
267                 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
268                 pdb_free_sam (&sam_pwent);
269                 return -1;
270         }
271         pdb_free_sam (&sam_pwent);
272         return 0;
273 }
274
275 /*********************************************************
276  Add New Machine
277 **********************************************************/
278
279 static int new_machine (char *machinename)
280 {
281         SAM_ACCOUNT *sam_pwent=NULL;
282         SAM_ACCOUNT *sam_trust=NULL;
283         char name[16];
284         char *password = NULL;
285         uid_t uid;
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         for (uid=BASE_MACHINE_UID; uid<=MAX_MACHINE_UID; uid++) {
303                 pdb_init_sam (&sam_trust);
304                 if (pdb_getsampwrid (sam_trust, pdb_uid_to_user_rid (uid))) {
305                         pdb_free_sam (&sam_trust);
306                 } else {
307                         break;
308                 }
309         }
310
311         if (uid>MAX_MACHINE_UID) {
312                 fprintf (stderr, "No more free UIDs available to Machine accounts!\n");
313                 pdb_free_sam(&sam_pwent);               
314                 return -1;
315         }
316
317         pdb_set_user_rid (sam_pwent,pdb_uid_to_user_rid (uid));
318         pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (BASE_MACHINE_UID));
319         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
320         
321         if (pdb_add_sam_account (sam_pwent)) {
322                 print_user_info (name, True, False);
323         } else {
324                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
325                 pdb_free_sam (&sam_pwent);
326                 return -1;
327         }
328         pdb_free_sam (&sam_pwent);
329         return 0;
330 }
331
332 /*********************************************************
333  Delete user entry
334 **********************************************************/
335
336 static int delete_user_entry (char *username)
337 {
338         return pdb_delete_sam_account (username);
339 }
340
341 /*********************************************************
342  Delete machine entry
343 **********************************************************/
344
345 static int delete_machine_entry (char *machinename)
346 {
347         char name[16];
348         
349         safe_strcpy (name, machinename, 16);
350         if (name[strlen(name)] != '$')
351                 safe_strcat (name, "$", 16);
352         return pdb_delete_sam_account (name);
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 (!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         static pstring servicesf = CONFIGFILE;
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         TimeInit();
557         
558         setup_logging("tdbedit", True);
559
560         if (argc < 2) {
561                 usage();
562                 return 0;
563         }
564         
565         if(!initialize_password_db(True)) {
566                 fprintf(stderr, "Can't setup password database vectors.\n");
567                 exit(1);
568         }
569         
570         if (!lp_load(servicesf,True,False,False)) {
571                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
572                         servicesf);
573                 exit(1);
574         }
575         
576         while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != 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                 default:
624                         usage();
625                 }
626         }
627         if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1) {
628                 fprintf (stderr, "Incompatible options on command line!\n");
629                 usage();
630                 exit(1);
631         }
632
633         if (add_user) {
634                 if (!user_name) {
635                         fprintf (stderr, "Username not specified! (use -u option)\n");
636                         return -1;
637                 }
638                 if (machine)
639                         return new_machine (user_name);
640                 else
641                         return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
642         }
643
644         if (delete_user) {
645                 if (!user_name) {
646                         fprintf (stderr, "Username not specified! (use -u option)\n");
647                         return -1;
648                 }
649                 if (machine)
650                         return delete_machine_entry (user_name);
651                 else
652                         return delete_user_entry (user_name);
653         }
654         
655         if (user_name) {
656                 if (setparms)
657                         set_user_info ( user_name, full_name,
658                                                 home_dir,
659                                                 home_drive,
660                                                 logon_script,
661                                                 profile_path);
662                 else
663                         return print_user_info (user_name, verbose, spstyle);
664                 
665                 return 0;
666         }
667
668         
669         if (list_users) 
670                 return print_users_list (verbose, spstyle);
671         
672         if (import) 
673                 return import_users (smbpasswd); 
674         
675         usage();
676
677         return 0;
678 }