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 BOOL AllowDebugChange;
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("pdbedit 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)
81 /* TODO: chaeck if entry is a user or a workstation */
82 if (!sam_pwent) return -1;
85 printf ("username: %s\n", pdb_get_username(sam_pwent));
86 if (IS_SAM_UNIX_USER(sam_pwent)) {
87 uid = pdb_get_uid(sam_pwent);
88 gid = pdb_get_gid(sam_pwent);
89 printf ("user ID/Group: %d/%d\n", uid, gid);
91 printf ("user RID/GRID: %u/%u\n", (unsigned int)pdb_get_user_rid(sam_pwent),
92 (unsigned int)pdb_get_group_rid(sam_pwent));
93 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
94 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
95 printf ("HomeDir Drive: %s\n", pdb_get_dirdrive(sam_pwent));
96 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
97 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
98 } else if (smbpwdstyle) {
99 if (IS_SAM_UNIX_USER(sam_pwent)) {
103 uid = pdb_get_uid(sam_pwent);
104 pdb_sethexpwd(lm_passwd,
105 pdb_get_lanman_passwd(sam_pwent),
106 pdb_get_acct_ctrl(sam_pwent));
107 pdb_sethexpwd(nt_passwd,
108 pdb_get_nt_passwd(sam_pwent),
109 pdb_get_acct_ctrl(sam_pwent));
111 printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
112 pdb_get_username(sam_pwent),
116 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
117 (uint32)pdb_get_pass_last_set_time(sam_pwent));
119 fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
122 if (IS_SAM_UNIX_USER(sam_pwent)) {
123 printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent),
124 pdb_get_fullname(sam_pwent));
126 printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
133 /*********************************************************
134 Get an Print User Info
135 **********************************************************/
137 static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
139 SAM_ACCOUNT *sam_pwent=NULL;
142 pdb_init_sam(&sam_pwent);
144 ret = pdb_getsampwnam (sam_pwent, username);
147 fprintf (stderr, "Username not found!\n");
148 pdb_free_sam(&sam_pwent);
152 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
153 pdb_free_sam(&sam_pwent);
158 /*********************************************************
160 **********************************************************/
161 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
163 SAM_ACCOUNT *sam_pwent=NULL;
166 pdb_init_sam(&sam_pwent);
167 errno = 0; /* testing --simo */
168 ret = pdb_setsampwent(False);
169 if (ret && errno == ENOENT) {
170 fprintf (stderr,"Password database not found!\n");
171 pdb_free_sam(&sam_pwent);
175 while ((ret = pdb_getsampwent (sam_pwent))) {
177 printf ("---------------\n");
178 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
179 pdb_reset_sam(sam_pwent);
183 pdb_free_sam(&sam_pwent);
187 /*********************************************************
189 **********************************************************/
191 static int set_user_info (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
193 SAM_ACCOUNT *sam_pwent=NULL;
196 pdb_init_sam(&sam_pwent);
198 ret = pdb_getsampwnam (sam_pwent, username);
200 fprintf (stderr, "Username not found!\n");
201 pdb_free_sam(&sam_pwent);
206 pdb_set_fullname(sam_pwent, fullname);
208 pdb_set_homedir(sam_pwent, homedir, True);
210 pdb_set_dir_drive(sam_pwent,drive, True);
212 pdb_set_logon_script(sam_pwent, script, True);
214 pdb_set_profile_path (sam_pwent, profile, True);
216 if (pdb_update_sam_account (sam_pwent, True))
217 print_user_info (username, True, False);
219 fprintf (stderr, "Unable to modify entry!\n");
220 pdb_free_sam(&sam_pwent);
223 pdb_free_sam(&sam_pwent);
227 /*********************************************************
229 **********************************************************/
230 static int new_user (char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
232 SAM_ACCOUNT *sam_pwent=NULL;
233 struct passwd *pwd = NULL;
234 char *password1, *password2;
236 ZERO_STRUCT(sam_pwent);
238 if (!(pwd = sys_getpwnam(username))) {
239 fprintf (stderr, "User %s does not exist in system passwd!\n", username);
243 pdb_init_sam_pw (&sam_pwent, pwd);
245 password1 = getpass("new password:");
246 password2 = getpass("retype new password:");
247 if (strcmp (password1, password2)) {
248 fprintf (stderr, "Passwords does not match!\n");
249 pdb_free_sam (&sam_pwent);
253 pdb_set_plaintext_passwd(sam_pwent, password1);
255 pdb_set_username(sam_pwent, username);
257 pdb_set_fullname(sam_pwent, fullname);
259 pdb_set_homedir (sam_pwent, homedir, True);
261 pdb_set_dir_drive (sam_pwent, drive, True);
263 pdb_set_logon_script(sam_pwent, script, True);
265 pdb_set_profile_path (sam_pwent, profile, True);
267 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
269 if (pdb_add_sam_account (sam_pwent)) {
270 print_user_info (username, True, False);
272 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
273 pdb_free_sam (&sam_pwent);
276 pdb_free_sam (&sam_pwent);
280 /*********************************************************
282 **********************************************************/
284 static int new_machine (char *machinename)
286 SAM_ACCOUNT *sam_pwent=NULL;
287 SAM_ACCOUNT *sam_trust=NULL;
289 char *password = NULL;
292 pdb_init_sam (&sam_pwent);
294 if (machinename[strlen (machinename) -1] == '$')
295 machinename[strlen (machinename) -1] = '\0';
297 safe_strcpy (name, machinename, 16);
298 safe_strcat (name, "$", 16);
300 string_set (&password, machinename);
301 strlower_m(password);
303 pdb_set_plaintext_passwd (sam_pwent, password);
305 pdb_set_username (sam_pwent, name);
307 for (uid=BASE_MACHINE_UID; uid<=MAX_MACHINE_UID; uid++) {
308 pdb_init_sam (&sam_trust);
309 if (pdb_getsampwrid (sam_trust, pdb_uid_to_user_rid (uid))) {
310 pdb_free_sam (&sam_trust);
316 if (uid>MAX_MACHINE_UID) {
317 fprintf (stderr, "No more free UIDs available to Machine accounts!\n");
318 pdb_free_sam(&sam_pwent);
322 pdb_set_user_rid (sam_pwent,pdb_uid_to_user_rid (uid));
323 pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (BASE_MACHINE_UID));
324 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
326 if (pdb_add_sam_account (sam_pwent)) {
327 print_user_info (name, True, False);
329 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
330 pdb_free_sam (&sam_pwent);
333 pdb_free_sam (&sam_pwent);
337 /*********************************************************
339 **********************************************************/
341 static int delete_user_entry (char *username)
343 return pdb_delete_sam_account (username);
346 /*********************************************************
348 **********************************************************/
350 static int delete_machine_entry (char *machinename)
354 safe_strcpy (name, machinename, 16);
355 if (name[strlen(name)] != '$')
356 safe_strcat (name, "$", 16);
357 return pdb_delete_sam_account (name);
360 /*********************************************************
361 Import smbpasswd style file
362 **********************************************************/
364 static int import_users (char *filename)
367 SAM_ACCOUNT *sam_pwent = NULL;
368 static pstring user_name;
369 static unsigned char smbpwd[16];
370 static unsigned char smbntpwd[16];
380 if((fp = sys_fopen(filename, "rb")) == NULL) {
381 fprintf (stderr, "%s\n", strerror (ferror (fp)));
388 fgets(linebuf, 256, fp);
390 fprintf (stderr, "%s\n", strerror (ferror (fp)));
393 if ((linebuf_len = strlen(linebuf)) == 0) {
397 if (linebuf[linebuf_len - 1] != '\n') {
399 while (!ferror(fp) && !feof(fp)) {
401 if (c == '\n') break;
404 linebuf[linebuf_len - 1] = '\0';
405 linebuf[linebuf_len] = '\0';
406 if ((linebuf[0] == 0) && feof(fp)) {
411 if (linebuf[0] == '#' || linebuf[0] == '\0')
415 p = (unsigned char *) strchr_m(linebuf, ':');
417 fprintf (stderr, "Error: malformed password entry at line %d !!\n", line);
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);
430 fprintf (stderr, "Error: malformed password entry at line %d (uid not number)\n", line);
433 uidval = atoi((char *) p);
434 while (*p && isdigit(*p)) p++;
436 fprintf (stderr, "Error: malformed password entry at line %d (no : after uid)\n", line);
439 if(!(pwd = sys_getpwnam(user_name))) {
440 fprintf(stderr, "User %s does not \
441 exist in system password file (usually /etc/passwd). Cannot add \
442 account without a valid local system user.\n", user_name);
446 if (!pdb_init_sam_pw(&sam_pwent, pwd)) {
447 fprintf(stderr, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
453 if (*p == '*' || *p == 'X') {
454 /* Password deliberately invalid */
455 fprintf (stderr, "Warning: entry invalidated for user %s\n", user_name);
456 pdb_set_lanman_passwd(sam_pwent, NULL);
457 pdb_set_nt_passwd(sam_pwent,NULL);
458 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_DISABLED);
460 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
461 fprintf (stderr, "Error: malformed password entry at line %d (password too short)\n",line);
462 pdb_free_sam (&sam_pwent);
466 fprintf (stderr, "Error: malformed password entry at line %d (no terminating :)\n",line);
467 pdb_free_sam (&sam_pwent);
470 if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
471 pdb_set_lanman_passwd(sam_pwent, NULL);
472 pdb_set_acct_ctrl(sam_pwent, pdb_get_acct_ctrl(sam_pwent) | ACB_PWNOTREQ);
474 if (!pdb_gethexpwd((char *)p, smbpwd)) {
475 fprintf (stderr, "Error: malformed Lanman password entry at line %d (non hex chars)\n", line);
476 pdb_free_sam (&sam_pwent);
479 pdb_set_lanman_passwd(sam_pwent, smbpwd);
483 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
484 if (*p != '*' && *p != 'X') {
485 if (pdb_gethexpwd((char *)p,smbntpwd)) {
486 pdb_set_nt_passwd(sam_pwent, smbntpwd);
493 /* Get ACCT_CTRL field if any */
496 unsigned char *end_p = (unsigned char *)strchr_m((char *)p, ']');
498 acct_ctrl = pdb_decode_acct_ctrl((char*)p);
500 acct_ctrl = ACB_NORMAL;
502 pdb_set_acct_ctrl(sam_pwent, acct_ctrl);
504 /* Get last change time */
509 if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0)) {
513 for(i = 0; i < 8; i++) {
514 if(p[i] == '\0' || !isxdigit(p[i])) break;
517 pdb_set_pass_last_set_time (sam_pwent, (time_t)strtol((char *)p, NULL, 16));
523 /* Now ADD the entry */
524 if (!(pdb_add_sam_account (sam_pwent))) {
525 fprintf (stderr, "Unable to add user entry!\n");
526 pdb_free_sam (&sam_pwent);
529 printf ("%s imported!\n", user_name);
531 pdb_free_sam (&sam_pwent);
533 printf ("%d lines read.\n%d entryes imported\n", line, good);
537 /*********************************************************
539 **********************************************************/
541 int main (int argc, char **argv)
544 BOOL list_users = False;
545 BOOL verbose = False;
546 BOOL spstyle = False;
547 BOOL setparms = False;
548 BOOL machine = False;
549 BOOL add_user = False;
550 BOOL delete_user = False;
552 char *user_name = NULL;
553 char *full_name = NULL;
554 char *home_dir = NULL;
555 char *home_drive = NULL;
556 char *logon_script = NULL;
557 char *profile_path = NULL;
558 char *smbpasswd = NULL;
560 setup_logging("pdbedit", True);
567 if(!initialize_password_db(True)) {
568 fprintf(stderr, "Can't setup password database vectors.\n");
573 AllowDebugChange = False;
575 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
576 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
581 while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != EOF) {
615 logon_script = optarg;
619 profile_path = optarg;
632 if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) > 1) {
633 fprintf (stderr, "Incompatible options on command line!\n");
640 fprintf (stderr, "Username not specified! (use -u option)\n");
644 return new_machine (user_name);
646 return new_user (user_name, full_name, home_dir, home_drive, logon_script, profile_path);
651 fprintf (stderr, "Username not specified! (use -u option)\n");
655 return delete_machine_entry (user_name);
657 return delete_user_entry (user_name);
662 set_user_info ( user_name, full_name,
668 return print_user_info (user_name, verbose, spstyle);
675 return print_users_list (verbose, spstyle);
678 return import_users (smbpasswd);