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