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;
35 extern int DEBUGLEVEL;
38 * Next two lines needed for SunOS and don't
39 * hurt anything else...
44 /*********************************************************
45 Print command usage on stderr and die.
46 **********************************************************/
47 static void usage(void)
50 printf("tdbedit options\n");
52 printf("You need to be root to use this tool!\n");
54 printf("(actually to add a user you need to use smbpasswd)\n");
56 printf(" -l list usernames\n");
57 printf(" -v verbose output\n");
58 printf(" -w smbpasswd file style\n");
59 printf(" -u username print user's info\n");
60 printf(" -f fullname set Full Name\n");
61 printf(" -h homedir set home directory\n");
62 printf(" -d drive set home dir drive\n");
63 printf(" -s script set logon script\n");
64 printf(" -p profile set profile path\n");
65 printf(" -a create new account\n");
66 printf(" -m it is a machine trust\n");
67 printf(" -x delete this user\n");
68 printf(" -i file import account from file (smbpasswd style)\n");
72 /*********************************************************
73 Print info from sam structure
74 **********************************************************/
76 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
78 /* TODO: chaeck if entry is a user or a workstation */
79 if (!sam_pwent) return -1;
82 printf ("username: %s\n", sam_pwent->username);
83 printf ("user ID/Group: %d/%d\n", sam_pwent->uid,
85 printf ("user RID/GRID: %d/%d\n", sam_pwent->user_rid,
86 sam_pwent->group_rid);
87 printf ("Full Name: %s\n", sam_pwent->full_name);
88 printf ("Home Directory: %s\n", sam_pwent->home_dir);
89 printf ("HomeDir Drive: %s\n", sam_pwent->dir_drive);
90 printf ("Logon Script: %s\n", sam_pwent->logon_script);
91 printf ("Profile Path: %s\n", sam_pwent->profile_path);
92 } else if (smbpwdstyle) {
95 pdb_sethexpwd(lm_passwd,
96 pdb_get_lanman_passwd(sam_pwent),
97 pdb_get_acct_ctrl(sam_pwent));
98 pdb_sethexpwd(nt_passwd,
99 pdb_get_nt_passwd(sam_pwent),
100 pdb_get_acct_ctrl(sam_pwent));
102 printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
103 pdb_get_username(sam_pwent),
104 pdb_get_uid(sam_pwent),
107 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
108 (uint32)pdb_get_pass_last_set_time(sam_pwent));
110 printf ("%s:%d:%s\n", sam_pwent->username, sam_pwent->uid, sam_pwent->full_name);
116 /*********************************************************
117 Get an Print User Info
118 **********************************************************/
120 static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
122 SAM_ACCOUNT *sam_pwent=NULL;
125 pdb_init_sam(&sam_pwent);
127 ret = pdb_getsampwnam (sam_pwent, username);
130 fprintf (stderr, "Username not found!\n");
131 pdb_free_sam(sam_pwent);
135 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
136 pdb_free_sam(sam_pwent);
141 /*********************************************************
143 **********************************************************/
144 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
146 SAM_ACCOUNT *sam_pwent=NULL;
149 pdb_init_sam(&sam_pwent);
151 ret = pdb_setsampwent(False);
152 if (ret && errno == ENOENT) {
153 fprintf (stderr,"Password database not found!\n");
154 pdb_free_sam(sam_pwent);
158 while ((ret = pdb_getsampwent (sam_pwent))) {
160 printf ("---------------\n");
161 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
162 pdb_reset_sam(sam_pwent);
166 pdb_free_sam(sam_pwent);
170 /*********************************************************
172 **********************************************************/
174 static int set_user_info (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
176 SAM_ACCOUNT *sam_pwent=NULL;
179 pdb_init_sam(&sam_pwent);
181 ret = pdb_getsampwnam (sam_pwent, username);
183 fprintf (stderr, "Username not found!\n");
184 pdb_free_sam(sam_pwent);
189 pdb_set_fullname(sam_pwent, fullname);
191 pdb_set_homedir(sam_pwent, homedir);
193 pdb_set_dir_drive(sam_pwent,drive);
195 pdb_set_logon_script(sam_pwent, script);
197 pdb_set_profile_path (sam_pwent, profile);
199 if (pdb_update_sam_account (sam_pwent, True))
200 print_user_info (username, True, False);
202 fprintf (stderr, "Unable to modify entry!\n");
203 pdb_free_sam(sam_pwent);
206 pdb_free_sam(sam_pwent);
210 /*********************************************************
212 **********************************************************/
213 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
215 SAM_ACCOUNT *sam_pwent=NULL;
216 struct passwd *pwd = NULL;
217 char *password1, *password2;
219 ZERO_STRUCT(sam_pwent);
221 pdb_init_sam (&sam_pwent);
223 if (!(pwd = sys_getpwnam(username))) {
224 fprintf (stderr, "User %s does not exist in system passwd!\n", username);
225 pdb_free_sam (sam_pwent);
229 password1 = getpass("new password:");
230 password2 = getpass("retype new password:");
231 if (strcmp (password1, password2)) {
232 fprintf (stderr, "Passwords does not match!\n");
233 pdb_free_sam (sam_pwent);
237 pdb_set_plaintext_passwd(sam_pwent, password1);
239 pdb_set_username(sam_pwent, username);
241 pdb_set_fullname(sam_pwent, fullname);
243 pdb_set_homedir (sam_pwent, homedir);
245 pdb_set_dir_drive (sam_pwent, drive);
247 pdb_set_logon_script(sam_pwent, script);
249 pdb_set_profile_path (sam_pwent, profile);
251 /* TODO: Check uid not being in MACHINE UID range!! */
252 pdb_set_uid (sam_pwent, pwd->pw_uid);
253 pdb_set_gid (sam_pwent, pwd->pw_gid);
254 pdb_set_user_rid (sam_pwent, pdb_uid_to_user_rid (pwd->pw_uid));
255 pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (pwd->pw_gid));
257 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
259 if (pdb_add_sam_account (sam_pwent)) {
260 print_user_info (username, True, False);
262 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
263 pdb_free_sam (sam_pwent);
266 pdb_free_sam (sam_pwent);
270 /*********************************************************
272 **********************************************************/
274 static int new_machine (char *machinename)
276 SAM_ACCOUNT *sam_pwent=NULL;
277 SAM_ACCOUNT *sam_trust=NULL;
279 char *password = NULL;
282 pdb_init_sam (&sam_pwent);
284 if (machinename[strlen (machinename) -1] == '$')
285 machinename[strlen (machinename) -1] = '\0';
287 safe_strcpy (name, machinename, 16);
288 safe_strcat (name, "$", 16);
290 string_set (&password, machinename);
291 strlower_m(password);
293 pdb_set_plaintext_passwd (sam_pwent, password);
295 pdb_set_username (sam_pwent, name);
297 for (uid=BASE_MACHINE_UID; uid<=MAX_MACHINE_UID; uid++) {
298 pdb_init_sam (&sam_trust);
299 if (pdb_getsampwuid (sam_trust, uid)) {
300 pdb_free_sam (sam_trust);
306 if (uid>MAX_MACHINE_UID) {
307 fprintf (stderr, "No more free UIDs available to Machine accounts!\n");
308 pdb_free_sam(sam_pwent);
312 pdb_set_uid (sam_pwent, uid);
313 pdb_set_gid (sam_pwent, BASE_MACHINE_UID); /* TODO: set there more appropriate value!! */
314 pdb_set_user_rid (sam_pwent,pdb_uid_to_user_rid (uid));
315 pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (BASE_MACHINE_UID));
316 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
318 if (pdb_add_sam_account (sam_pwent)) {
319 print_user_info (name, True, False);
321 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
322 pdb_free_sam (sam_pwent);
325 pdb_free_sam (sam_pwent);
329 /*********************************************************
331 **********************************************************/
333 static int delete_user_entry (char *username)
335 return pdb_delete_sam_account (username);
338 /*********************************************************
340 **********************************************************/
342 static int delete_machine_entry (char *machinename)
346 safe_strcpy (name, machinename, 16);
347 if (name[strlen(name)] != '$')
348 safe_strcat (name, "$", 16);
349 return pdb_delete_sam_account (name);
352 /*********************************************************
353 Import smbpasswd style file
354 **********************************************************/
356 static int import_users (char *filename)
359 SAM_ACCOUNT *sam_pwent = NULL;
360 static pstring user_name;
361 static unsigned char smbpwd[16];
362 static unsigned char smbntpwd[16];
371 if (!pdb_init_sam (&sam_pwent)) {
372 fprintf (stderr, "pdb_init_sam FAILED!\n");
375 if((fp = sys_fopen(filename, "rb")) == NULL) {
376 fprintf (stderr, "%s\n", strerror (ferror (fp)));
383 fgets(linebuf, 256, fp);
385 fprintf (stderr, "%s\n", strerror (ferror (fp)));
386 pdb_free_sam(sam_pwent);
389 if ((linebuf_len = strlen(linebuf)) == 0) {
393 if (linebuf[linebuf_len - 1] != '\n') {
395 while (!ferror(fp) && !feof(fp)) {
397 if (c == '\n') break;
400 linebuf[linebuf_len - 1] = '\0';
401 linebuf[linebuf_len] = '\0';
402 if ((linebuf[0] == 0) && feof(fp)) {
404 pdb_free_sam(sam_pwent);
408 if (linebuf[0] == '#' || linebuf[0] == '\0')
411 pdb_set_acct_ctrl (sam_pwent,ACB_NORMAL);
414 p = (unsigned char *) strchr_m(linebuf, ':');
416 fprintf (stderr, "Error: malformed password entry at line %d !!\n", line);
417 pdb_reset_sam (sam_pwent);
420 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
421 user_name[PTR_DIFF(p, linebuf)] = '\0';
426 fprintf (stderr, "Error: negative uid at line %d\n", line);
427 pdb_reset_sam (sam_pwent);
431 fprintf (stderr, "Error: malformed password entry at line %d (uid not number)\n", line);
432 pdb_reset_sam (sam_pwent);
435 uidval = atoi((char *) p);
436 while (*p && isdigit(*p)) p++;
438 fprintf (stderr, "Error: malformed password entry at line %d (no : after uid)\n", line);
439 pdb_reset_sam (sam_pwent);
443 pdb_set_username(sam_pwent, user_name);
444 pdb_set_uid (sam_pwent, uidval);
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);
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_reset_sam (sam_pwent);
461 fprintf (stderr, "Error: malformed password entry at line %d (no terminating :)\n",line);
462 pdb_reset_sam (sam_pwent);
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);
469 if (!smbpasswd_gethexpwd((char *)p, smbpwd)) {
470 fprintf (stderr, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line);
471 pdb_reset_sam (sam_pwent);
474 pdb_set_lanman_passwd(sam_pwent, smbpwd);
477 pdb_set_nt_passwd(sam_pwent, smbpwd);
479 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
480 if (*p != '*' && *p != 'X') {
481 if (pdb_gethexpwd((char *)p,smbntpwd)) {
482 pdb_set_nt_passwd(sam_pwent, smbntpwd);
489 /* Get ACCT_CTRL field if any */
492 unsigned char *end_p = (unsigned char *)strchr_m((char *)p, ']');
494 acct_ctrl = pdb_decode_acct_ctrl((char*)p);
496 acct_ctrl = ACB_NORMAL;
498 pdb_set_acct_ctrl(sam_pwent, acct_ctrl);
500 /* Get last change time */
505 if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0)) {
509 for(i = 0; i < 8; i++) {
510 if(p[i] == '\0' || !isxdigit(p[i])) break;
513 pdb_set_pass_last_set_time (sam_pwent, (time_t)strtol((char *)p, NULL, 16));
519 /* Old-style workstation account code droped. */
521 if (pdb_get_acct_ctrl(sam_pwent) & ACB_WSTRUST) {
522 if ((uidval < BASE_MACHINE_UID) || (uidval > MAX_MACHINE_UID)) {
523 fprintf (stderr, "Warning: Machine UID out of normal range %d-%d\n",
527 pdb_set_uid(sam_pwent, BASE_MACHINE_UID);
530 /* Test if user is valid */
531 if (pdb_get_acct_ctrl(sam_pwent) & ACB_NORMAL) {
532 struct passwd *pwd = NULL;
534 if (!(pwd = sys_getpwnam(user_name))) {
535 fprintf (stderr, "Error: User %s does not exist in system passwd!\n", user_name);
538 pdb_set_gid(sam_pwent, pwd->pw_gid);
541 /* Fill in sam_pwent structure */
542 pdb_set_user_rid(sam_pwent, pdb_uid_to_user_rid (pdb_get_uid(sam_pwent)));
543 pdb_set_group_rid(sam_pwent, pdb_gid_to_group_rid (pdb_get_gid(sam_pwent)));
545 /* TODO: set also full_name, home_dir, dir_drive, logon_script, profile_path, ecc...
546 * when defaults will be available (after passdb redesign)
547 * let them blank just now they are not used anyway
550 /* Now ADD the entry */
551 if (!(pdb_add_sam_account (sam_pwent))) {
552 fprintf (stderr, "Unable to add user entry!\n");
553 pdb_reset_sam (sam_pwent);
556 printf ("%s imported!\n", user_name);
558 pdb_reset_sam (sam_pwent);
560 printf ("%d lines read.\n%d entryes imported\n", line, good);
561 pdb_free_sam(sam_pwent);
565 /*********************************************************
567 **********************************************************/
569 int main (int argc, char **argv)
572 static pstring servicesf = CONFIGFILE;
573 BOOL list_users = False;
574 BOOL verbose = False;
575 BOOL spstyle = False;
576 BOOL setparms = False;
577 BOOL machine = False;
578 BOOL add_user = False;
579 BOOL delete_user = False;
581 char *user_name = NULL;
582 char *full_name = NULL;
583 char *home_dir = NULL;
584 char *home_drive = NULL;
585 char *logon_script = NULL;
586 char *profile_path = NULL;
587 char *smbpasswd = NULL;
591 setup_logging("tdbedit", True);
598 if(!initialize_password_db(True)) {
599 fprintf(stderr, "Can't setup password database vectors.\n");
603 if (!lp_load(servicesf,True,False,False)) {
604 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
609 while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != EOF) {
643 logon_script = optarg;
647 profile_path = optarg;
660 if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1) {
661 fprintf (stderr, "Incompatible options on command line!\n");
668 fprintf (stderr, "Username not specified! (use -u option)\n");
672 return new_machine (user_name);
674 return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
679 fprintf (stderr, "Username not specified! (use -u option)\n");
683 return delete_machine_entry (user_name);
685 return delete_user_entry (user_name);
690 set_user_info ( user_name, full_name,
696 return print_user_info (user_name, verbose, spstyle);
703 return print_users_list (verbose, spstyle);
706 return import_users (smbpasswd);