2 Unix SMB/Netbios implementation.
3 passdb editing frontend
6 Copyright (C) Simo Sorce 2000
7 Copyright (C) Andrew Bartlett 2001
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.
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.
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.
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?
29 #define BASE_MACHINE_UID 60000
30 #define MAX_MACHINE_UID 65500 /* 5500 trust accounts aren't enough? */
34 extern pstring global_myname
;
37 * Next two lines needed for SunOS and don't
38 * hurt anything else...
43 /*********************************************************
44 Print command usage on stderr and die.
45 **********************************************************/
46 static void usage(void)
49 printf("pdbedit options\n");
51 printf("You need to be root to use this tool!\n");
53 printf("(actually to add a user you need to use smbpasswd)\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");
71 /*********************************************************
72 Print info from sam structure
73 **********************************************************/
75 static int print_sam_info (SAM_ACCOUNT
*sam_pwent
, BOOL verbosity
, BOOL smbpwdstyle
)
77 /* TODO: chaeck if entry is a user or a workstation */
78 if (!sam_pwent
) return -1;
81 printf ("username: %s\n", sam_pwent
->username
);
82 printf ("user ID/Group: %d/%d\n", sam_pwent
->uid
,
84 printf ("user RID/GRID: %d/%d\n", sam_pwent
->user_rid
,
85 sam_pwent
->group_rid
);
86 printf ("Full Name: %s\n", sam_pwent
->full_name
);
87 printf ("Home Directory: %s\n", sam_pwent
->home_dir
);
88 printf ("HomeDir Drive: %s\n", sam_pwent
->dir_drive
);
89 printf ("Logon Script: %s\n", sam_pwent
->logon_script
);
90 printf ("Profile Path: %s\n", sam_pwent
->profile_path
);
91 } else if (smbpwdstyle
) {
94 pdb_sethexpwd(lm_passwd
,
95 pdb_get_lanman_passwd(sam_pwent
),
96 pdb_get_acct_ctrl(sam_pwent
));
97 pdb_sethexpwd(nt_passwd
,
98 pdb_get_nt_passwd(sam_pwent
),
99 pdb_get_acct_ctrl(sam_pwent
));
101 printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
102 pdb_get_username(sam_pwent
),
103 pdb_get_uid(sam_pwent
),
106 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent
),NEW_PW_FORMAT_SPACE_PADDED_LEN
),
107 (uint32
)pdb_get_pass_last_set_time(sam_pwent
));
109 printf ("%s:%d:%s\n", sam_pwent
->username
, sam_pwent
->uid
, sam_pwent
->full_name
);
115 /*********************************************************
116 Get an Print User Info
117 **********************************************************/
119 static int print_user_info (char *username
, BOOL verbosity
, BOOL smbpwdstyle
)
121 SAM_ACCOUNT
*sam_pwent
=NULL
;
124 pdb_init_sam(&sam_pwent
);
126 ret
= pdb_getsampwnam (sam_pwent
, username
);
129 fprintf (stderr
, "Username not found!\n");
130 pdb_free_sam(sam_pwent
);
134 ret
=print_sam_info (sam_pwent
, verbosity
, smbpwdstyle
);
135 pdb_free_sam(sam_pwent
);
140 /*********************************************************
142 **********************************************************/
143 static int print_users_list (BOOL verbosity
, BOOL smbpwdstyle
)
145 SAM_ACCOUNT
*sam_pwent
=NULL
;
148 pdb_init_sam(&sam_pwent
);
150 ret
= pdb_setsampwent(False
);
151 if (ret
&& errno
== ENOENT
) {
152 fprintf (stderr
,"Password database not found!\n");
153 pdb_free_sam(sam_pwent
);
157 while ((ret
= pdb_getsampwent (sam_pwent
))) {
159 printf ("---------------\n");
160 print_sam_info (sam_pwent
, verbosity
, smbpwdstyle
);
161 pdb_reset_sam(sam_pwent
);
165 pdb_free_sam(sam_pwent
);
169 /*********************************************************
171 **********************************************************/
173 static int set_user_info (char *username
, char *fullname
, char *homedir
, char *drive
, char *script
, char *profile
)
175 SAM_ACCOUNT
*sam_pwent
=NULL
;
178 pdb_init_sam(&sam_pwent
);
180 ret
= pdb_getsampwnam (sam_pwent
, username
);
182 fprintf (stderr
, "Username not found!\n");
183 pdb_free_sam(sam_pwent
);
188 pdb_set_fullname(sam_pwent
, fullname
);
190 pdb_set_homedir(sam_pwent
, homedir
, True
);
192 pdb_set_dir_drive(sam_pwent
, drive
, True
);
194 pdb_set_logon_script(sam_pwent
, script
, True
);
196 pdb_set_profile_path (sam_pwent
, profile
, True
);
198 if (pdb_update_sam_account (sam_pwent
, True
))
199 print_user_info (username
, True
, False
);
201 fprintf (stderr
, "Unable to modify entry!\n");
202 pdb_free_sam(sam_pwent
);
205 pdb_free_sam(sam_pwent
);
209 /*********************************************************
211 **********************************************************/
213 static char *strdup_x(const char *s
)
215 char *new_s
= strdup(s
);
217 fprintf(stderr
,"out of memory\n");
223 /*************************************************************
224 Utility function to prompt for passwords from stdin. Each
225 password entered must end with a newline.
226 *************************************************************/
227 static char *stdin_new_passwd(void)
229 static fstring new_passwd
;
232 ZERO_ARRAY(new_passwd
);
235 * if no error is reported from fgets() and string at least contains
236 * the newline that ends the password, then replace the newline with
239 if ( fgets(new_passwd
, sizeof(new_passwd
), stdin
) != NULL
) {
240 if ((len
= strlen(new_passwd
)) > 0) {
241 if(new_passwd
[len
-1] == '\n')
242 new_passwd
[len
- 1] = 0;
248 /*************************************************************
249 Utility function to get passwords via tty or stdin
250 Used if the '-s' option is set to silently get passwords
252 _copied_ from smbpasswd
253 *************************************************************/
254 static char *get_pass( char *prompt
, BOOL stdin_get
)
258 p
= stdin_new_passwd();
265 /*************************************************************
266 Utility function to prompt for new password.
267 _copied_ from smbpasswd
268 *************************************************************/
269 static char *prompt_for_new_password(BOOL stdin_get
)
274 ZERO_ARRAY(new_passwd
);
276 p
= get_pass("New SMB password:", stdin_get
);
278 fstrcpy(new_passwd
, p
);
281 p
= get_pass("Retype new SMB password:", stdin_get
);
283 if (strcmp(p
, new_passwd
)) {
284 fprintf(stderr
, "Mismatch - password unchanged.\n");
285 ZERO_ARRAY(new_passwd
);
294 /*********************************************************
296 **********************************************************/
297 static int new_user (char *username
, char *fullname
, char *homedir
, char *drive
, char *script
, char *profile
)
299 SAM_ACCOUNT
*sam_pwent
=NULL
;
300 struct passwd
*pwd
= NULL
;
303 ZERO_STRUCT(sam_pwent
);
305 pdb_init_sam (&sam_pwent
);
307 if (!(pwd
= sys_getpwnam(username
))) {
308 fprintf (stderr
, "User %s does not exist in system passwd!\n", username
);
309 pdb_free_sam (sam_pwent
);
313 password
= prompt_for_new_password(0);
315 fprintf (stderr
, "Passwords do not match!\n");
316 pdb_free_sam (sam_pwent
);
320 pdb_set_plaintext_passwd(sam_pwent
, password
);
322 pdb_set_username(sam_pwent
, username
);
324 pdb_set_fullname(sam_pwent
, fullname
);
326 pdb_set_homedir (sam_pwent
, homedir
, True
);
328 pdb_set_dir_drive (sam_pwent
, drive
, True
);
330 pdb_set_logon_script(sam_pwent
, script
, True
);
332 pdb_set_profile_path (sam_pwent
, profile
, True
);
334 /* TODO: Check uid not being in MACHINE UID range!! */
335 pdb_set_uid (sam_pwent
, pwd
->pw_uid
);
336 pdb_set_gid (sam_pwent
, pwd
->pw_gid
);
337 pdb_set_user_rid (sam_pwent
, pdb_uid_to_user_rid (pwd
->pw_uid
));
338 pdb_set_group_rid (sam_pwent
, pdb_gid_to_group_rid (pwd
->pw_gid
));
340 pdb_set_acct_ctrl (sam_pwent
, ACB_NORMAL
);
342 if (pdb_add_sam_account (sam_pwent
)) {
343 print_user_info (username
, True
, False
);
345 fprintf (stderr
, "Unable to add user! (does it alredy exist?)\n");
346 pdb_free_sam (sam_pwent
);
349 pdb_free_sam (sam_pwent
);
353 /*********************************************************
355 **********************************************************/
357 static int new_machine (char *machinename
)
359 SAM_ACCOUNT
*sam_pwent
=NULL
;
360 SAM_ACCOUNT
*sam_trust
=NULL
;
362 char *password
= NULL
;
365 pdb_init_sam (&sam_pwent
);
367 if (machinename
[strlen (machinename
) -1] == '$')
368 machinename
[strlen (machinename
) -1] = '\0';
370 safe_strcpy (name
, machinename
, 16);
371 safe_strcat (name
, "$", 16);
373 string_set (&password
, machinename
);
376 pdb_set_plaintext_passwd (sam_pwent
, password
);
378 pdb_set_username (sam_pwent
, name
);
380 for (uid
=BASE_MACHINE_UID
; uid
<=MAX_MACHINE_UID
; uid
++) {
381 pdb_init_sam (&sam_trust
);
382 if (pdb_getsampwuid (sam_trust
, uid
)) {
383 pdb_free_sam (sam_trust
);
389 if (uid
>MAX_MACHINE_UID
) {
390 fprintf (stderr
, "No more free UIDs available to Machine accounts!\n");
391 pdb_free_sam(sam_pwent
);
395 pdb_set_uid (sam_pwent
, uid
);
396 pdb_set_gid (sam_pwent
, BASE_MACHINE_UID
); /* TODO: set there more appropriate value!! */
397 pdb_set_user_rid (sam_pwent
,pdb_uid_to_user_rid (uid
));
398 pdb_set_group_rid (sam_pwent
, pdb_gid_to_group_rid (BASE_MACHINE_UID
));
399 pdb_set_acct_ctrl (sam_pwent
, ACB_WSTRUST
);
401 if (pdb_add_sam_account (sam_pwent
)) {
402 print_user_info (name
, True
, False
);
404 fprintf (stderr
, "Unable to add machine! (does it already exist?)\n");
405 pdb_free_sam (sam_pwent
);
408 pdb_free_sam (sam_pwent
);
412 /*********************************************************
414 **********************************************************/
416 static int delete_user_entry (char *username
)
418 return pdb_delete_sam_account (username
);
421 /*********************************************************
423 **********************************************************/
425 static int delete_machine_entry (char *machinename
)
429 safe_strcpy (name
, machinename
, 16);
430 if (name
[strlen(name
)] != '$')
431 safe_strcat (name
, "$", 16);
432 return pdb_delete_sam_account (name
);
435 /*********************************************************
436 Import smbpasswd style file
437 **********************************************************/
439 static int import_users (char *filename
)
442 SAM_ACCOUNT
*sam_pwent
= NULL
;
443 static pstring user_name
;
444 static unsigned char smbpwd
[16];
445 static unsigned char smbntpwd
[16];
454 if (!pdb_init_sam (&sam_pwent
)) {
455 fprintf (stderr
, "pdb_init_sam FAILED!\n");
458 if((fp
= sys_fopen(filename
, "rb")) == NULL
) {
459 fprintf (stderr
, "%s\n", strerror (ferror (fp
)));
466 fgets(linebuf
, 256, fp
);
468 fprintf (stderr
, "%s\n", strerror (ferror (fp
)));
469 pdb_free_sam(sam_pwent
);
472 if ((linebuf_len
= strlen(linebuf
)) == 0) {
476 if (linebuf
[linebuf_len
- 1] != '\n') {
478 while (!ferror(fp
) && !feof(fp
)) {
480 if (c
== '\n') break;
483 linebuf
[linebuf_len
- 1] = '\0';
484 linebuf
[linebuf_len
] = '\0';
485 if ((linebuf
[0] == 0) && feof(fp
)) {
487 pdb_free_sam(sam_pwent
);
491 if (linebuf
[0] == '#' || linebuf
[0] == '\0')
494 pdb_set_acct_ctrl (sam_pwent
,ACB_NORMAL
);
497 p
= (unsigned char *) strchr(linebuf
, ':');
499 fprintf (stderr
, "Error: malformed password entry at line %d !!\n", line
);
500 pdb_reset_sam (sam_pwent
);
503 strncpy(user_name
, linebuf
, PTR_DIFF(p
, linebuf
));
504 user_name
[PTR_DIFF(p
, linebuf
)] = '\0';
509 fprintf (stderr
, "Error: negative uid at line %d\n", line
);
510 pdb_reset_sam (sam_pwent
);
514 fprintf (stderr
, "Error: malformed password entry at line %d (uid not number)\n", line
);
515 pdb_reset_sam (sam_pwent
);
518 uidval
= atoi((char *) p
);
519 while (*p
&& isdigit(*p
)) p
++;
521 fprintf (stderr
, "Error: malformed password entry at line %d (no : after uid)\n", line
);
522 pdb_reset_sam (sam_pwent
);
526 pdb_set_username(sam_pwent
, user_name
);
527 pdb_set_uid (sam_pwent
, uidval
);
531 if (*p
== '*' || *p
== 'X') {
532 /* Password deliberately invalid */
533 fprintf (stderr
, "Warning: entry invalidated for user %s\n", user_name
);
534 pdb_set_lanman_passwd(sam_pwent
, NULL
);
535 pdb_set_nt_passwd(sam_pwent
,NULL
);
536 pdb_set_acct_ctrl(sam_pwent
, pdb_get_acct_ctrl(sam_pwent
) | ACB_DISABLED
);
538 if (linebuf_len
< (PTR_DIFF(p
, linebuf
) + 33)) {
539 fprintf (stderr
, "Error: malformed password entry at line %d (password too short)\n",line
);
540 pdb_reset_sam (sam_pwent
);
544 fprintf (stderr
, "Error: malformed password entry at line %d (no terminating :)\n",line
);
545 pdb_reset_sam (sam_pwent
);
548 if (!strncasecmp((char *) p
, "NO PASSWORD", 11)) {
549 pdb_set_lanman_passwd(sam_pwent
, NULL
);
550 pdb_set_acct_ctrl(sam_pwent
, pdb_get_acct_ctrl(sam_pwent
) | ACB_PWNOTREQ
);
552 if (!pdb_gethexpwd((char *)p
, smbpwd
)) {
553 fprintf (stderr
, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line
);
554 pdb_reset_sam (sam_pwent
);
557 pdb_set_lanman_passwd(sam_pwent
, smbpwd
);
560 pdb_set_nt_passwd(sam_pwent
, smbpwd
);
562 if ((linebuf_len
>= (PTR_DIFF(p
, linebuf
) + 33)) && (p
[32] == ':')) {
563 if (*p
!= '*' && *p
!= 'X') {
564 if (pdb_gethexpwd((char *)p
,smbntpwd
)) {
565 pdb_set_nt_passwd(sam_pwent
, smbntpwd
);
572 /* Get ACCT_CTRL field if any */
575 unsigned char *end_p
= (unsigned char *)strchr((char *)p
, ']');
577 acct_ctrl
= pdb_decode_acct_ctrl((char*)p
);
579 acct_ctrl
= ACB_NORMAL
;
581 pdb_set_acct_ctrl(sam_pwent
, acct_ctrl
);
583 /* Get last change time */
588 if(*p
&& (StrnCaseCmp((char *)p
, "LCT-", 4)==0)) {
592 for(i
= 0; i
< 8; i
++) {
593 if(p
[i
] == '\0' || !isxdigit(p
[i
])) break;
596 pdb_set_pass_last_set_time (sam_pwent
, (time_t)strtol((char *)p
, NULL
, 16));
602 /* Old-style workstation account code droped. */
604 if (pdb_get_acct_ctrl(sam_pwent
) & ACB_WSTRUST
) {
605 if ((uidval
< BASE_MACHINE_UID
) || (uidval
> MAX_MACHINE_UID
)) {
606 fprintf (stderr
, "Warning: Machine UID out of normal range %d-%d\n",
610 pdb_set_uid(sam_pwent
, BASE_MACHINE_UID
);
613 /* Test if user is valid */
614 if (pdb_get_acct_ctrl(sam_pwent
) & ACB_NORMAL
) {
615 struct passwd
*pwd
= NULL
;
617 if (!(pwd
= sys_getpwnam(user_name
))) {
618 fprintf (stderr
, "Error: User %s does not exist in system passwd!\n", user_name
);
621 pdb_set_gid(sam_pwent
, pwd
->pw_gid
);
624 /* Fill in sam_pwent structure */
625 pdb_set_user_rid(sam_pwent
, pdb_uid_to_user_rid (pdb_get_uid(sam_pwent
)));
626 pdb_set_group_rid(sam_pwent
, pdb_gid_to_group_rid (pdb_get_gid(sam_pwent
)));
628 /* TODO: set also full_name, home_dir, dir_drive, logon_script, profile_path, ecc...
629 * when defaults will be available (after passdb redesign)
630 * let them blank just now they are not used anyway
633 /* Now ADD the entry */
634 if (!(pdb_add_sam_account (sam_pwent
))) {
635 fprintf (stderr
, "Unable to add user entry!\n");
636 pdb_reset_sam (sam_pwent
);
639 printf ("%s imported!\n", user_name
);
641 pdb_reset_sam (sam_pwent
);
643 printf ("%d lines read.\n%d entries imported\n", line
, good
);
644 pdb_free_sam(sam_pwent
);
648 /*********************************************************
650 **********************************************************/
652 int main (int argc
, char **argv
)
655 static pstring servicesf
= CONFIGFILE
;
656 BOOL list_users
= False
;
657 BOOL verbose
= False
;
658 BOOL spstyle
= False
;
659 BOOL setparms
= False
;
660 BOOL machine
= False
;
661 BOOL add_user
= False
;
662 BOOL delete_user
= False
;
664 char *user_name
= NULL
;
665 char *full_name
= NULL
;
666 char *home_dir
= NULL
;
667 char *home_drive
= NULL
;
668 char *logon_script
= NULL
;
669 char *profile_path
= NULL
;
670 char *smbpasswd
= NULL
;
674 setup_logging("pdbedit", True
);
676 charset_initialise();
683 if (!lp_load(servicesf
,True
,False
,False
)) {
684 fprintf(stderr
, "Can't load %s - run testparm to debug it\n",
691 if(!initialize_password_db(True
)) {
692 fprintf(stderr
, "Can't setup password database vectors.\n");
697 codepage_initialise(lp_client_code_page());
699 while ((ch
= getopt(argc
, argv
, "ad:f:h:i:lmp:s:u:vwxD:")) != EOF
) {
733 logon_script
= optarg
;
737 profile_path
= optarg
;
747 DEBUGLEVEL
= atoi(optarg
);
753 if (((add_user
?1:0) + (delete_user
?1:0) + (list_users
?1:0) + (import
?1:0) + (setparms
?1:0)) > 1) {
754 fprintf (stderr
, "Incompatible options on command line!\n");
761 fprintf (stderr
, "Username not specified! (use -u option)\n");
765 return new_machine (user_name
);
767 return new_user (user_name
, full_name
, home_dir
, home_drive
, logon_script
, profile_path
);
772 fprintf (stderr
, "Username not specified! (use -u option)\n");
776 return delete_machine_entry (user_name
);
778 return delete_user_entry (user_name
);
783 set_user_info ( user_name
, full_name
,
789 return print_user_info (user_name
, verbose
, spstyle
);
796 return print_users_list (verbose
, spstyle
);
799 return import_users (smbpasswd
);