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("tdbedit 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);
192 pdb_set_dir_drive(sam_pwent,drive);
194 pdb_set_logon_script(sam_pwent, script);
196 pdb_set_profile_path (sam_pwent, profile);
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 **********************************************************/
212 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
214 SAM_ACCOUNT *sam_pwent=NULL;
215 struct passwd *pwd = NULL;
216 char *password1, *password2;
218 ZERO_STRUCT(sam_pwent);
220 pdb_init_sam (&sam_pwent);
222 if (!(pwd = sys_getpwnam(username))) {
223 fprintf (stderr, "User %s does not exist in system passwd!\n", username);
224 pdb_free_sam (&sam_pwent);
228 password1 = getpass("new password:");
229 password2 = getpass("retype new password:");
230 if (strcmp (password1, password2)) {
231 fprintf (stderr, "Passwords does not match!\n");
232 pdb_free_sam (&sam_pwent);
236 pdb_set_plaintext_passwd(sam_pwent, password1);
238 pdb_set_username(sam_pwent, username);
240 pdb_set_fullname(sam_pwent, fullname);
242 pdb_set_homedir (sam_pwent, homedir);
244 pdb_set_dir_drive (sam_pwent, drive);
246 pdb_set_logon_script(sam_pwent, script);
248 pdb_set_profile_path (sam_pwent, profile);
250 /* TODO: Check uid not being in MACHINE UID range!! */
251 pdb_set_uid (sam_pwent, pwd->pw_uid);
252 pdb_set_gid (sam_pwent, pwd->pw_gid);
253 pdb_set_user_rid (sam_pwent, pdb_uid_to_user_rid (pwd->pw_uid));
254 pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (pwd->pw_gid));
256 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
258 if (pdb_add_sam_account (sam_pwent)) {
259 print_user_info (username, True, False);
261 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
262 pdb_free_sam (&sam_pwent);
265 pdb_free_sam (&sam_pwent);
269 /*********************************************************
271 **********************************************************/
273 static int new_machine (char *machinename)
275 SAM_ACCOUNT *sam_pwent=NULL;
276 SAM_ACCOUNT *sam_trust=NULL;
278 char *password = NULL;
281 pdb_init_sam (&sam_pwent);
283 if (machinename[strlen (machinename) -1] == '$')
284 machinename[strlen (machinename) -1] = '\0';
286 safe_strcpy (name, machinename, 16);
287 safe_strcat (name, "$", 16);
289 string_set (&password, machinename);
290 strlower_m(password);
292 pdb_set_plaintext_passwd (sam_pwent, password);
294 pdb_set_username (sam_pwent, name);
296 for (uid=BASE_MACHINE_UID; uid<=MAX_MACHINE_UID; uid++) {
297 pdb_init_sam (&sam_trust);
298 if (pdb_getsampwuid (sam_trust, uid)) {
299 pdb_free_sam (&sam_trust);
305 if (uid>MAX_MACHINE_UID) {
306 fprintf (stderr, "No more free UIDs available to Machine accounts!\n");
307 pdb_free_sam(&sam_pwent);
311 pdb_set_uid (sam_pwent, uid);
312 pdb_set_gid (sam_pwent, BASE_MACHINE_UID); /* TODO: set there more appropriate value!! */
313 pdb_set_user_rid (sam_pwent,pdb_uid_to_user_rid (uid));
314 pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (BASE_MACHINE_UID));
315 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
317 if (pdb_add_sam_account (sam_pwent)) {
318 print_user_info (name, True, False);
320 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
321 pdb_free_sam (&sam_pwent);
324 pdb_free_sam (&sam_pwent);
328 /*********************************************************
330 **********************************************************/
332 static int delete_user_entry (char *username)
334 return pdb_delete_sam_account (username);
337 /*********************************************************
339 **********************************************************/
341 static int delete_machine_entry (char *machinename)
345 safe_strcpy (name, machinename, 16);
346 if (name[strlen(name)] != '$')
347 safe_strcat (name, "$", 16);
348 return pdb_delete_sam_account (name);
351 /*********************************************************
352 Import smbpasswd style file
353 **********************************************************/
355 static int import_users (char *filename)
358 SAM_ACCOUNT *sam_pwent = NULL;
359 static pstring user_name;
360 static unsigned char smbpwd[16];
361 static unsigned char smbntpwd[16];
370 if (!pdb_init_sam (&sam_pwent)) {
371 fprintf (stderr, "pdb_init_sam FAILED!\n");
374 if((fp = sys_fopen(filename, "rb")) == NULL) {
375 fprintf (stderr, "%s\n", strerror (ferror (fp)));
382 fgets(linebuf, 256, fp);
384 fprintf (stderr, "%s\n", strerror (ferror (fp)));
385 pdb_free_sam(&sam_pwent);
388 if ((linebuf_len = strlen(linebuf)) == 0) {
392 if (linebuf[linebuf_len - 1] != '\n') {
394 while (!ferror(fp) && !feof(fp)) {
396 if (c == '\n') break;
399 linebuf[linebuf_len - 1] = '\0';
400 linebuf[linebuf_len] = '\0';
401 if ((linebuf[0] == 0) && feof(fp)) {
403 pdb_free_sam(&sam_pwent);
407 if (linebuf[0] == '#' || linebuf[0] == '\0')
410 pdb_set_acct_ctrl (sam_pwent,ACB_NORMAL);
413 p = (unsigned char *) strchr_m(linebuf, ':');
415 fprintf (stderr, "Error: malformed password entry at line %d !!\n", line);
416 pdb_reset_sam (sam_pwent);
419 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
420 user_name[PTR_DIFF(p, linebuf)] = '\0';
425 fprintf (stderr, "Error: negative uid at line %d\n", line);
426 pdb_reset_sam (sam_pwent);
430 fprintf (stderr, "Error: malformed password entry at line %d (uid not number)\n", line);
431 pdb_reset_sam (sam_pwent);
434 uidval = atoi((char *) p);
435 while (*p && isdigit(*p)) p++;
437 fprintf (stderr, "Error: malformed password entry at line %d (no : after uid)\n", line);
438 pdb_reset_sam (sam_pwent);
442 pdb_set_username(sam_pwent, user_name);
443 pdb_set_uid (sam_pwent, uidval);
447 if (*p == '*' || *p == 'X') {
448 /* Password deliberately invalid */
449 fprintf (stderr, "Warning: entry invalidated for user %s\n", user_name);
450 pdb_set_lanman_passwd(sam_pwent, NULL);
451 pdb_set_nt_passwd(sam_pwent,NULL);
452 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_DISABLED);
454 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
455 fprintf (stderr, "Error: malformed password entry at line %d (password too short)\n",line);
456 pdb_reset_sam (sam_pwent);
460 fprintf (stderr, "Error: malformed password entry at line %d (no terminating :)\n",line);
461 pdb_reset_sam (sam_pwent);
464 if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
465 pdb_set_lanman_passwd(sam_pwent, NULL);
466 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_PWNOTREQ);
468 if (!pdb_gethexpwd((char *)p, smbpwd)) {
469 fprintf (stderr, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line);
470 pdb_reset_sam (sam_pwent);
473 pdb_set_lanman_passwd(sam_pwent, smbpwd);
477 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
478 if (*p != '*' && *p != 'X') {
479 if (pdb_gethexpwd((char *)p,smbntpwd)) {
480 pdb_set_nt_passwd(sam_pwent, smbntpwd);
487 /* Get ACCT_CTRL field if any */
490 unsigned char *end_p = (unsigned char *)strchr_m((char *)p, ']');
492 acct_ctrl = pdb_decode_acct_ctrl((char*)p);
494 acct_ctrl = ACB_NORMAL;
496 pdb_set_acct_ctrl(sam_pwent, acct_ctrl);
498 /* Get last change time */
503 if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0)) {
507 for(i = 0; i < 8; i++) {
508 if(p[i] == '\0' || !isxdigit(p[i])) break;
511 pdb_set_pass_last_set_time (sam_pwent, (time_t)strtol((char *)p, NULL, 16));
517 /* Old-style workstation account code droped. */
519 if (pdb_get_acct_ctrl(sam_pwent) & ACB_WSTRUST) {
520 if ((uidval < BASE_MACHINE_UID) || (uidval > MAX_MACHINE_UID)) {
521 fprintf (stderr, "Warning: Machine UID out of normal range %d-%d\n",
525 pdb_set_uid(sam_pwent, BASE_MACHINE_UID);
528 /* Test if user is valid */
529 if (pdb_get_acct_ctrl(sam_pwent) & ACB_NORMAL) {
530 struct passwd *pwd = NULL;
532 if (!(pwd = sys_getpwnam(user_name))) {
533 fprintf (stderr, "Error: User %s does not exist in system passwd!\n", user_name);
536 pdb_set_gid(sam_pwent, pwd->pw_gid);
539 /* Fill in sam_pwent structure */
540 pdb_set_user_rid(sam_pwent, pdb_uid_to_user_rid (pdb_get_uid(sam_pwent)));
541 pdb_set_group_rid(sam_pwent, pdb_gid_to_group_rid (pdb_get_gid(sam_pwent)));
543 /* TODO: set also full_name, home_dir, dir_drive, logon_script, profile_path, ecc...
544 * when defaults will be available (after passdb redesign)
545 * let them blank just now they are not used anyway
548 /* Now ADD the entry */
549 if (!(pdb_add_sam_account (sam_pwent))) {
550 fprintf (stderr, "Unable to add user entry!\n");
551 pdb_reset_sam (sam_pwent);
554 printf ("%s imported!\n", user_name);
556 pdb_reset_sam (sam_pwent);
558 printf ("%d lines read.\n%d entryes imported\n", line, good);
559 pdb_free_sam(&sam_pwent);
563 /*********************************************************
565 **********************************************************/
567 int main (int argc, char **argv)
570 static pstring servicesf = CONFIGFILE;
571 BOOL list_users = False;
572 BOOL verbose = False;
573 BOOL spstyle = False;
574 BOOL setparms = False;
575 BOOL machine = False;
576 BOOL add_user = False;
577 BOOL delete_user = False;
579 char *user_name = NULL;
580 char *full_name = NULL;
581 char *home_dir = NULL;
582 char *home_drive = NULL;
583 char *logon_script = NULL;
584 char *profile_path = NULL;
585 char *smbpasswd = NULL;
589 setup_logging("tdbedit", True);
596 if(!initialize_password_db(True)) {
597 fprintf(stderr, "Can't setup password database vectors.\n");
601 if (!lp_load(servicesf,True,False,False)) {
602 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
607 while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != EOF) {
641 logon_script = optarg;
645 profile_path = optarg;
658 if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1) {
659 fprintf (stderr, "Incompatible options on command line!\n");
666 fprintf (stderr, "Username not specified! (use -u option)\n");
670 return new_machine (user_name);
672 return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
677 fprintf (stderr, "Username not specified! (use -u option)\n");
681 return delete_machine_entry (user_name);
683 return delete_user_entry (user_name);
688 set_user_info ( user_name, full_name,
694 return print_user_info (user_name, verbose, spstyle);
701 return print_users_list (verbose, spstyle);
704 return import_users (smbpasswd);