2 * Unix SMB/Netbios implementation. Version 1.9. tdbedit module. Copyright
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
20 /* base uid for trust accounts is set to 60000 !
21 * May be we should add the defines in smb.h to make it possible having
22 * different values on different platforms?
25 #define BASE_MACHINE_UID 60000
26 #define MAX_MACHINE_UID 65500 /* 5500 trust acconts aren't enough? */
30 extern pstring global_myname;
31 extern int DEBUGLEVEL;
34 * Next two lines needed for SunOS and don't
35 * hurt anything else...
40 /*********************************************************
41 Print command usage on stderr and die.
42 **********************************************************/
43 static void usage(void)
46 printf("tdbedit options\n");
48 printf("You need to be root to use this tool!\n");
50 printf("(actually to add a user you need to use smbpasswd)\n");
52 printf(" -l list usernames\n");
53 printf(" -v verbose output\n");
54 printf(" -w smbpasswd file style\n");
55 printf(" -u username print user's info\n");
56 printf(" -f fullname set Full Name\n");
57 printf(" -h homedir set home directory\n");
58 printf(" -d drive set home dir drive\n");
59 printf(" -s script set logon script\n");
60 printf(" -p profile set profile path\n");
61 printf(" -a create new account\n");
62 printf(" -m it is a machine trust\n");
63 printf(" -x delete this user\n");
64 printf(" -i file import account from file (smbpasswd style)\n");
67 /*********************************************************
68 Print info from sam structure
69 **********************************************************/
70 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
72 /* TODO: chaeck if entry is a user or a workstation */
73 if (!sam_pwent) return -1;
77 printf ("username: %s\n", sam_pwent->username);
78 printf ("user ID/Group: %d/%d\n", sam_pwent->uid,
80 printf ("user RID/GRID: %d/%d\n", sam_pwent->user_rid,
81 sam_pwent->group_rid);
82 printf ("Full Name: %s\n", sam_pwent->full_name);
83 printf ("Home Directory: %s\n", sam_pwent->home_dir);
84 printf ("HomeDir Drive: %s\n", sam_pwent->dir_drive);
85 printf ("Logon Script: %s\n", sam_pwent->logon_script);
86 printf ("Profile Path: %s\n", sam_pwent->profile_path);
92 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
93 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
95 printf ("%s:%d:%s:%s:%s:LCT-%08x:\n",
96 pdb_get_username(sam_pwent),
97 pdb_get_uid(sam_pwent),
100 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
101 (uint32)pdb_get_pass_last_set_time(sam_pwent));
105 printf ("%s:%d:%s\n", sam_pwent->username, sam_pwent->uid, sam_pwent->full_name);
111 /*********************************************************
112 Get an Print User Info
113 **********************************************************/
114 static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
116 SAM_ACCOUNT *sam_pwent=NULL;
119 pdb_init_sam(&sam_pwent);
121 ret = pdb_getsampwnam (sam_pwent, username);
124 fprintf (stderr, "Username not found!\n");
125 pdb_clear_sam(sam_pwent);
129 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
130 pdb_clear_sam(sam_pwent);
135 /*********************************************************
137 **********************************************************/
138 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
140 SAM_ACCOUNT *sam_pwent=NULL;
143 pdb_init_sam(&sam_pwent);
145 ret = pdb_setsampwent(False);
146 if (ret && errno == ENOENT) {
147 fprintf (stderr,"Password database not found!\n");
148 pdb_clear_sam(sam_pwent);
152 while ((ret = pdb_getsampwent (sam_pwent)))
154 if (verbosity) printf ("---------------\n");
155 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
159 pdb_clear_sam(sam_pwent);
163 /*********************************************************
165 **********************************************************/
166 static int set_user_info (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
168 SAM_ACCOUNT *sam_pwent=NULL;
171 pdb_init_sam(&sam_pwent);
173 ret = pdb_getsampwnam (sam_pwent, username);
176 fprintf (stderr, "Username not found!\n");
177 pdb_clear_sam(sam_pwent);
181 if (fullname) pdb_set_fullname(sam_pwent, fullname);
182 if (homedir) pdb_set_homedir(sam_pwent, homedir);
183 if (drive) pdb_set_dir_drive(sam_pwent,drive);
184 if (script) pdb_set_logon_script(sam_pwent, script);
185 if (profile) pdb_set_profile_path (sam_pwent, profile);
187 if (pdb_update_sam_account (sam_pwent, TRUE)) print_user_info (username, TRUE, FALSE);
190 fprintf (stderr, "Unable to modify entry!\n");
191 pdb_clear_sam(sam_pwent);
194 pdb_clear_sam(sam_pwent);
198 /*********************************************************
200 **********************************************************/
201 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
203 SAM_ACCOUNT sam_pwent;
204 struct passwd *pwd = NULL;
206 uchar new_nt_p16[16];
207 char *password1, *password2;
209 ZERO_STRUCT(sam_pwent);
211 if (pdb_getsampwnam (&sam_pwent, username))
213 fprintf (stderr, "Username already exist in database!\n");
217 if (!(pwd = sys_getpwnam(username)))
219 fprintf (stderr, "User %s does not exist in system passwd!\n", username);
223 password1 = getpass("new password:");
224 password2 = getpass("retype new password:");
225 if (strcmp (password1, password2))
227 fprintf (stderr, "Passwords does not match!\n");
230 nt_lm_owf_gen (password1, new_nt_p16, new_p16);
232 pdb_set_username(&sam_pwent, username);
233 if (fullname) pdb_set_fullname(&sam_pwent, fullname);
234 if (homedir) pdb_set_homedir (&sam_pwent, homedir);
235 if (drive) pdb_set_dir_drive (&sam_pwent, drive);
236 if (script) pdb_set_logon_script(&sam_pwent, script);
237 if (profile) pdb_set_profile_path (&sam_pwent, profile);
239 /* TODO: Check uid not being in MACHINE UID range!! */
240 sam_pwent.uid = pwd->pw_uid;
241 sam_pwent.gid = pwd->pw_gid;
242 sam_pwent.user_rid = pdb_uid_to_user_rid (pwd->pw_uid);
243 sam_pwent.group_rid = pdb_gid_to_group_rid (pwd->pw_gid);
244 sam_pwent.lm_pw = new_p16;
245 sam_pwent.nt_pw = new_nt_p16;
246 sam_pwent.acct_ctrl = ACB_NORMAL;
248 if (pdb_add_sam_account (&sam_pwent)) print_user_info (username, TRUE, FALSE);
251 fprintf (stderr, "Unable to add user!\n");
257 /*********************************************************
259 **********************************************************/
260 static int new_machine (char *machinename)
262 SAM_ACCOUNT sam_pwent;
263 SAM_ACCOUNT sam_trust;
265 uchar new_nt_p16[16];
267 char *password = NULL;
270 if (machinename[strlen (machinename) -1] == '$') machinename[strlen (machinename) -1] = '\0';
272 safe_strcpy (name, machinename, 16);
273 safe_strcat (name, "$", 16);
275 string_set (&password, machinename);
277 nt_lm_owf_gen (password, new_nt_p16, new_p16);
279 pdb_set_username(&sam_pwent, name);
281 for (uid=BASE_MACHINE_UID; uid<=MAX_MACHINE_UID; uid++)
282 if (!(pdb_getsampwuid (&sam_trust, uid)))
285 if (uid>MAX_MACHINE_UID) {
286 fprintf (stderr, "No more free UIDs available to Machine accounts!\n");
291 sam_pwent.gid = BASE_MACHINE_UID; /* TODO: set there more appropriate value!! */
292 sam_pwent.user_rid = pdb_uid_to_user_rid (uid);
293 sam_pwent.group_rid = pdb_gid_to_group_rid (BASE_MACHINE_UID);
294 sam_pwent.lm_pw = new_p16;
295 sam_pwent.nt_pw = new_nt_p16;
296 sam_pwent.acct_ctrl = ACB_WSTRUST;
298 if (pdb_add_sam_account (&sam_pwent))
299 print_user_info (name, TRUE, FALSE);
301 fprintf (stderr, "Unable to add machine!\n");
307 /*********************************************************
309 **********************************************************/
310 static int delete_user_entry (char *username)
312 return pdb_delete_sam_account (username);
315 /*********************************************************
317 **********************************************************/
318 static int delete_machine_entry (char *machinename)
322 safe_strcpy (name, machinename, 16);
323 if (name[strlen(name)] != '$')
325 safe_strcat (name, "$", 16);
327 return pdb_delete_sam_account (name);
330 /*********************************************************
331 Import smbpasswd style file
332 **********************************************************/
333 static int import_users (char *filename)
336 SAM_ACCOUNT sam_pwent;
337 SAM_ACCOUNT sam_test;
338 static pstring user_name;
339 static unsigned char smbpwd[16];
340 static unsigned char smbntpwd[16];
349 if((fp = sys_fopen(filename, "rb")) == NULL)
351 fprintf (stderr, "%s\n", strerror (ferror (fp)));
359 fgets(linebuf, 256, fp);
362 fprintf (stderr, "%s\n", strerror (ferror (fp)));
365 if ((linebuf_len = strlen(linebuf)) == 0)
370 if (linebuf[linebuf_len - 1] != '\n')
373 while (!ferror(fp) && !feof(fp))
376 if (c == '\n') break;
379 else linebuf[linebuf_len - 1] = '\0';
380 linebuf[linebuf_len] = '\0';
381 if ((linebuf[0] == 0) && feof(fp))
387 if (linebuf[0] == '#' || linebuf[0] == '\0') continue;
389 /*pdb_init_sam (&sam_pwent);*/
390 sam_pwent.acct_ctrl = ACB_NORMAL;
393 p = (unsigned char *) strchr(linebuf, ':');
396 fprintf (stderr, "Error: malformed password entry at line %d !!\n", line);
399 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
400 user_name[PTR_DIFF(p, linebuf)] = '\0';
406 fprintf (stderr, "Error: negative uid at line %d\n", line);
411 fprintf (stderr, "Error: malformed password entry at line %d (uid not number)\n", line);
414 uidval = atoi((char *) p);
415 while (*p && isdigit(*p)) p++;
418 fprintf (stderr, "Error: malformed password entry at line %d (no : after uid)\n", line);
422 pdb_set_username(&sam_pwent, user_name);
423 pdb_set_uid (&sam_pwent, uidval);
427 if (*p == '*' || *p == 'X')
429 /* Password deliberately invalid */
430 fprintf (stderr, "Warning: entry invalidated for user %s\n", user_name);
431 sam_pwent.lm_pw = NULL;
432 sam_pwent.nt_pw = NULL;
433 sam_pwent.acct_ctrl |= ACB_DISABLED;
437 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33))
439 fprintf (stderr, "Error: malformed password entry at line %d (password too short)\n",line);
444 fprintf (stderr, "Error: malformed password entry at line %d (no terminating :)\n",line);
447 if (!strncasecmp((char *) p, "NO PASSWORD", 11))
449 sam_pwent.lm_pw = NULL;
450 sam_pwent.acct_ctrl |= ACB_PWNOTREQ;
454 if (!pdb_gethexpwd((char *)p, smbpwd))
456 fprintf (stderr, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line);
459 sam_pwent.lm_pw = smbpwd;
462 sam_pwent.nt_pw = NULL;
464 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':'))
466 if (*p != '*' && *p != 'X')
468 if (pdb_gethexpwd((char *)p,smbntpwd))
470 sam_pwent.nt_pw = smbntpwd;
477 /* Get ACCT_CTRL field if any */
480 unsigned char *end_p = (unsigned char *)strchr((char *)p, ']');
482 sam_pwent.acct_ctrl = pdb_decode_acct_ctrl((char*)p);
483 if(sam_pwent.acct_ctrl == 0) sam_pwent.acct_ctrl = ACB_NORMAL;
485 /* Get last change time */
486 if(end_p) p = end_p + 1;
490 if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0))
495 for(i = 0; i < 8; i++)
497 if(p[i] == '\0' || !isxdigit(p[i])) break;
501 sam_pwent.pass_last_set_time = (time_t)strtol((char *)p, NULL, 16);
507 /* Test if workstation */
510 if(sam_pwent.username[strlen(sam_pwent.username) - 1] == '$')
512 sam_pwent.acct_ctrl &= ~ACB_NORMAL;
513 sam_pwent.acct_ctrl |= ACB_WSTRUST;
516 if (sam_pwent.acct_ctrl & ACB_WSTRUST)
518 if (!(BASE_MACHINE_UID <= uidval <= MAX_MACHINE_UID))
520 fprintf (stderr, "Warning: Machine UID out of normal range %d-%d\n",
524 sam_pwent.gid = BASE_MACHINE_UID;
527 /* Test if user is valid */
528 if (sam_pwent.acct_ctrl & ACB_NORMAL)
530 struct passwd *pwd = NULL;
532 if (pdb_getsampwnam (&sam_test,user_name))
534 fprintf (stderr, "Error: Username already exist in database!\n");
537 if (!(pwd = sys_getpwnam(user_name)))
539 fprintf (stderr, "Error: User %s does not exist in system passwd!\n", user_name);
542 sam_pwent.gid = pwd->pw_gid;
545 /* Fill in sam_pwent structure */
546 sam_pwent.user_rid = pdb_uid_to_user_rid (sam_pwent.uid);
547 sam_pwent.group_rid = pdb_gid_to_group_rid (sam_pwent.gid);
548 /* TODO: set also full_name, home_dir, dir_drive, logon_script, profile_path, ecc...
549 * when defaults will be available (after passdb redesign)
550 * let them blank just now they are not used anyway
553 /* Now ADD the entry */
554 if (!(pdb_add_sam_account (&sam_pwent)))
556 fprintf (stderr, "Unable to add user entry!\n");
559 printf ("%s imported!\n", user_name);
562 printf ("%d lines read.\n%d entryes imported\n", line, good);
567 /*********************************************************
569 **********************************************************/
570 int main (int argc, char **argv)
573 static pstring servicesf = CONFIGFILE;
574 BOOL list_users = FALSE;
575 BOOL verbose = FALSE;
576 BOOL spstyle = FALSE;
577 BOOL setparms = FALSE;
578 BOOL machine = FALSE;
579 BOOL add_user = FALSE;
580 BOOL delete_user = FALSE;
582 char *user_name = NULL;
583 char *full_name = NULL;
584 char *home_dir = NULL;
585 char *home_drive = NULL;
586 char *logon_script = NULL;
587 char *profile_path = NULL;
588 char *smbpasswd = NULL;
592 setup_logging("tdbedit", True);
594 charset_initialise();
603 if(!initialize_password_db(True)) {
604 fprintf(stderr, "Can't setup password database vectors.\n");
608 if (!lp_load(servicesf,True,False,False)) {
609 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
614 while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != EOF) {
648 logon_script = optarg;
652 profile_path = optarg;
665 if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1)
667 fprintf (stderr, "Incompatible options on command line!\n");
676 fprintf (stderr, "Username not specified! (use -u option)\n");
679 if (machine) return new_machine (user_name);
680 else return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
687 fprintf (stderr, "Username not specified! (use -u option)\n");
690 if (machine) return delete_machine_entry (user_name);
691 else return delete_user_entry (user_name);
696 if (setparms) set_user_info ( user_name,
703 else return print_user_info (user_name, verbose, spstyle);
710 return print_users_list (verbose, spstyle);
713 return import_users (smbpasswd);