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.
26 extern pstring global_myname;
27 extern BOOL AllowDebugChange;
30 * Next two lines needed for SunOS and don't
31 * hurt anything else...
36 /*********************************************************
37 Print command usage on stderr and die.
38 **********************************************************/
39 static void usage(void)
42 printf("pdbedit options\n");
44 printf("You need to be root to use this tool!\n");
46 printf("(actually to add a user you need to use smbpasswd)\n");
48 printf(" -l list usernames\n");
49 printf(" -v verbose output\n");
50 printf(" -w smbpasswd file style\n");
51 printf(" -u username print user's info\n");
52 printf(" -f fullname set Full Name\n");
53 printf(" -h homedir set home directory\n");
54 printf(" -d drive set home dir drive\n");
55 printf(" -s script set logon script\n");
56 printf(" -p profile set profile path\n");
57 printf(" -a create new account\n");
58 printf(" -m it is a machine trust\n");
59 printf(" -x delete this user\n");
60 printf(" -i file import account from file (smbpasswd style)\n");
61 printf(" -D debuglevel set DEBUGELEVEL (default = 1)\n");
65 /*********************************************************
66 Print info from sam structure
67 **********************************************************/
69 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
74 /* TODO: chaeck if entry is a user or a workstation */
75 if (!sam_pwent) return -1;
78 printf ("username: %s\n", pdb_get_username(sam_pwent));
79 if (IS_SAM_UNIX_USER(sam_pwent)) {
80 uid = pdb_get_uid(sam_pwent);
81 gid = pdb_get_gid(sam_pwent);
82 printf ("user ID/Group: %d/%d\n", uid, gid);
84 printf ("user RID/GRID: %u/%u\n", (unsigned int)pdb_get_user_rid(sam_pwent),
85 (unsigned int)pdb_get_group_rid(sam_pwent));
86 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
87 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
88 printf ("HomeDir Drive: %s\n", pdb_get_dirdrive(sam_pwent));
89 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
90 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
91 } else if (smbpwdstyle) {
92 if (IS_SAM_UNIX_USER(sam_pwent)) {
96 uid = pdb_get_uid(sam_pwent);
97 pdb_sethexpwd(lm_passwd,
98 pdb_get_lanman_passwd(sam_pwent),
99 pdb_get_acct_ctrl(sam_pwent));
100 pdb_sethexpwd(nt_passwd,
101 pdb_get_nt_passwd(sam_pwent),
102 pdb_get_acct_ctrl(sam_pwent));
104 printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
105 pdb_get_username(sam_pwent),
109 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
110 (uint32)pdb_get_pass_last_set_time(sam_pwent));
112 fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
115 if (IS_SAM_UNIX_USER(sam_pwent)) {
116 printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent),
117 pdb_get_fullname(sam_pwent));
119 printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
126 /*********************************************************
127 Get an Print User Info
128 **********************************************************/
130 static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
132 SAM_ACCOUNT *sam_pwent=NULL;
135 pdb_init_sam(&sam_pwent);
137 ret = pdb_getsampwnam (sam_pwent, username);
140 fprintf (stderr, "Username not found!\n");
141 pdb_free_sam(&sam_pwent);
145 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
146 pdb_free_sam(&sam_pwent);
151 /*********************************************************
153 **********************************************************/
154 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
156 SAM_ACCOUNT *sam_pwent=NULL;
159 pdb_init_sam(&sam_pwent);
160 errno = 0; /* testing --simo */
161 ret = pdb_setsampwent(False);
162 if (ret && errno == ENOENT) {
163 fprintf (stderr,"Password database not found!\n");
166 pdb_free_sam(&sam_pwent);
168 while ((NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent))
169 && (ret = pdb_getsampwent (sam_pwent)))) {
171 printf ("---------------\n");
172 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
173 pdb_free_sam(&sam_pwent);
175 pdb_free_sam(&sam_pwent);
181 /*********************************************************
183 **********************************************************/
185 static int set_user_info (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
187 SAM_ACCOUNT *sam_pwent=NULL;
190 pdb_init_sam(&sam_pwent);
192 ret = pdb_getsampwnam (sam_pwent, username);
194 fprintf (stderr, "Username not found!\n");
195 pdb_free_sam(&sam_pwent);
200 pdb_set_fullname(sam_pwent, fullname);
202 pdb_set_homedir(sam_pwent, homedir, True);
204 pdb_set_dir_drive(sam_pwent,drive, True);
206 pdb_set_logon_script(sam_pwent, script, True);
208 pdb_set_profile_path (sam_pwent, profile, True);
210 if (pdb_update_sam_account (sam_pwent))
211 print_user_info (username, True, False);
213 fprintf (stderr, "Unable to modify entry!\n");
214 pdb_free_sam(&sam_pwent);
217 pdb_free_sam(&sam_pwent);
221 /*********************************************************
223 **********************************************************/
224 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
226 SAM_ACCOUNT *sam_pwent=NULL;
227 struct passwd *pwd = NULL;
228 char *password1, *password2;
230 ZERO_STRUCT(sam_pwent);
232 if ((pwd = getpwnam_alloc(username))) {
233 pdb_init_sam_pw (&sam_pwent, pwd);
236 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
237 pdb_init_sam(&sam_pwent);
238 if (!pdb_set_username(sam_pwent, username)) {
243 password1 = getpass("new password:");
244 password2 = getpass("retype new password:");
245 if (strcmp (password1, password2)) {
246 fprintf (stderr, "Passwords does not match!\n");
247 pdb_free_sam (&sam_pwent);
251 pdb_set_plaintext_passwd(sam_pwent, password1);
254 pdb_set_fullname(sam_pwent, fullname);
256 pdb_set_homedir (sam_pwent, homedir, True);
258 pdb_set_dir_drive (sam_pwent, drive, True);
260 pdb_set_logon_script(sam_pwent, script, True);
262 pdb_set_profile_path (sam_pwent, profile, True);
264 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
266 if (pdb_add_sam_account (sam_pwent)) {
267 print_user_info (username, True, False);
269 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
270 pdb_free_sam (&sam_pwent);
273 pdb_free_sam (&sam_pwent);
277 /*********************************************************
279 **********************************************************/
281 static int new_machine (char *machinename)
283 SAM_ACCOUNT *sam_pwent=NULL;
285 char *password = NULL;
287 pdb_init_sam (&sam_pwent);
289 if (machinename[strlen (machinename) -1] == '$')
290 machinename[strlen (machinename) -1] = '\0';
292 safe_strcpy (name, machinename, 16);
293 safe_strcat (name, "$", 16);
295 string_set (&password, machinename);
296 strlower_m(password);
298 pdb_set_plaintext_passwd (sam_pwent, password);
300 pdb_set_username (sam_pwent, name);
302 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
304 if (pdb_add_sam_account (sam_pwent)) {
305 print_user_info (name, True, False);
307 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
308 pdb_free_sam (&sam_pwent);
311 pdb_free_sam (&sam_pwent);
315 /*********************************************************
317 **********************************************************/
319 static int delete_user_entry (char *username)
321 SAM_ACCOUNT *samaccount;
323 pdb_init_sam(&samaccount);
325 if (!pdb_getsampwnam(samaccount, username)) {
326 fprintf (stderr, "user %s does not exist in the passdb\n", username);
330 return pdb_delete_sam_account (samaccount);
333 /*********************************************************
335 **********************************************************/
337 static int delete_machine_entry (char *machinename)
340 SAM_ACCOUNT *samaccount;
342 safe_strcpy (name, machinename, 16);
343 if (name[strlen(name)] != '$')
344 safe_strcat (name, "$", 16);
346 pdb_init_sam(&samaccount);
348 if (!pdb_getsampwnam(samaccount, name)) {
349 fprintf (stderr, "user %s does not exist in the passdb\n", name);
353 return pdb_delete_sam_account (samaccount);
356 /*********************************************************
357 Import smbpasswd style file
358 **********************************************************/
360 static int import_users (char *filename)
363 SAM_ACCOUNT *sam_pwent = NULL;
364 static pstring user_name;
365 static unsigned char smbpwd[16];
366 static unsigned char smbntpwd[16];
376 if((fp = sys_fopen(filename, "rb")) == NULL) {
377 fprintf (stderr, "%s\n", strerror (ferror (fp)));
384 fgets(linebuf, 256, fp);
386 fprintf (stderr, "%s\n", strerror (ferror (fp)));
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)) {
407 if (linebuf[0] == '#' || linebuf[0] == '\0')
411 p = (unsigned char *) strchr_m(linebuf, ':');
413 fprintf (stderr, "Error: malformed password entry at line %d !!\n", line);
416 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
417 user_name[PTR_DIFF(p, linebuf)] = '\0';
422 fprintf (stderr, "Error: negative uid at line %d\n", line);
426 fprintf (stderr, "Error: malformed password entry at line %d (uid not number)\n", line);
429 uidval = atoi((char *) p);
430 while (*p && isdigit(*p)) p++;
432 fprintf (stderr, "Error: malformed password entry at line %d (no : after uid)\n", line);
435 if(!(pwd = sys_getpwnam(user_name))) {
436 fprintf(stderr, "User %s does not \
437 exist in system password file (usually /etc/passwd). Cannot add \
438 account without a valid local system user.\n", user_name);
442 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pwent, pwd))) {
443 fprintf(stderr, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
449 if (*p == '*' || *p == 'X') {
450 /* Password deliberately invalid */
451 fprintf (stderr, "Warning: entry invalidated for user %s\n", user_name);
452 pdb_set_lanman_passwd(sam_pwent, NULL);
453 pdb_set_nt_passwd(sam_pwent,NULL);
454 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_DISABLED);
456 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
457 fprintf (stderr, "Error: malformed password entry at line %d (password too short)\n",line);
458 pdb_free_sam (&sam_pwent);
462 fprintf (stderr, "Error: malformed password entry at line %d (no terminating :)\n",line);
463 pdb_free_sam (&sam_pwent);
466 if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
467 pdb_set_lanman_passwd(sam_pwent, NULL);
468 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_PWNOTREQ);
470 if (!pdb_gethexpwd((char *)p, smbpwd)) {
471 fprintf (stderr, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line);
472 pdb_free_sam (&sam_pwent);
475 pdb_set_lanman_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 /* Now ADD the entry */
520 if (!(pdb_add_sam_account (sam_pwent))) {
521 fprintf (stderr, "Unable to add user entry!\n");
522 pdb_free_sam (&sam_pwent);
525 printf ("%s imported!\n", user_name);
527 pdb_free_sam (&sam_pwent);
529 printf ("%d lines read.\n%d entryes imported\n", line, good);
533 /*********************************************************
535 **********************************************************/
537 int main (int argc, char **argv)
540 BOOL list_users = False;
541 BOOL verbose = False;
542 BOOL spstyle = False;
543 BOOL setparms = False;
544 BOOL machine = False;
545 BOOL add_user = False;
546 BOOL delete_user = False;
548 char *user_name = NULL;
549 char *full_name = NULL;
550 char *home_dir = NULL;
551 char *home_drive = NULL;
552 char *logon_script = NULL;
553 char *profile_path = NULL;
554 char *smbpasswd = NULL;
556 setup_logging("pdbedit", True);
564 AllowDebugChange = False;
566 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
567 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
572 if(!initialize_password_db(True)) {
573 fprintf(stderr, "Can't setup password database vectors.\n");
577 while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwxD:")) != EOF) {
611 logon_script = optarg;
615 profile_path = optarg;
625 DEBUGLEVEL = atoi(optarg);
631 if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1) {
632 fprintf (stderr, "Incompatible options on command line!\n");
639 fprintf (stderr, "Username not specified! (use -u option)\n");
643 return new_machine (user_name);
645 return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
650 fprintf (stderr, "Username not specified! (use -u option)\n");
654 return delete_machine_entry (user_name);
656 return delete_user_entry (user_name);
661 set_user_info ( user_name, full_name,
667 return print_user_info (user_name, verbose, spstyle);
674 return print_users_list (verbose, spstyle);
677 return import_users (smbpasswd);