2 Unix SMB/CIFS implementation.
3 passdb editing frontend
5 Copyright (C) Simo Sorce 2000
6 Copyright (C) Andrew Bartlett 2001
7 Copyright (C) Jelmer Vernooij 2002
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 #define BIT_CONFIGFILE 0x00000001
27 #define BIT_DEBUGLEVEL 0x00000002
28 #define BIT_BACKEND 0x00000004
29 #define BIT_VERBOSE 0x00000008
30 #define BIT_SPSTYLE 0x00000010
31 #define BIT_RESERV_1 0x00000020
32 #define BIT_RESERV_2 0x00000040
33 #define BIT_RESERV_3 0x00000080
34 #define BIT_FULLNAME 0x00000100
35 #define BIT_HOMEDIR 0x00000200
36 #define BIT_HDIRDRIVE 0x00000400
37 #define BIT_LOGSCRIPT 0x00000800
38 #define BIT_PROFILE 0x00001000
39 #define BIT_MACHINE 0x00002000
40 #define BIT_RESERV_4 0x00004000
41 #define BIT_USER 0x00008000
42 #define BIT_LIST 0x00010000
43 #define BIT_MODIFY 0x00020000
44 #define BIT_CREATE 0x00040000
45 #define BIT_DELETE 0x00080000
46 #define BIT_ACCPOLICY 0x00100000
47 #define BIT_ACCPOLVAL 0x00200000
48 #define BIT_RESERV_6 0x00400000
49 #define BIT_RESERV_7 0x00800000
50 #define BIT_IMPORT 0x01000000
51 #define BIT_EXPORT 0x02000000
53 #define MASK_ALWAYS_GOOD 0x0000001F
54 #define MASK_USER_GOOD 0x00001F00
55 extern pstring global_myname;
56 extern BOOL AllowDebugChange;
58 /*********************************************************
59 Add all currently available users to another db
60 ********************************************************/
62 static int export_database (struct pdb_context *in, struct pdb_context *out) {
63 SAM_ACCOUNT *user = NULL;
65 if (!in->pdb_setsampwent(in, 0)) {
66 fprintf(stderr, "Can't sampwent!\n");
70 if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
71 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
75 while (in->pdb_getsampwent(in, user)) {
76 out->pdb_add_sam_account(out, user);
77 if (!NT_STATUS_IS_OK(pdb_reset_sam(user))){
78 fprintf(stderr, "Can't reset SAM_ACCOUNT!\n");
83 in->pdb_endsampwent(in);
88 /*********************************************************
89 Print info from sam structure
90 **********************************************************/
92 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
98 /* TODO: chaeck if entry is a user or a workstation */
99 if (!sam_pwent) return -1;
102 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
103 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
104 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
106 if (IS_SAM_UNIX_USER(sam_pwent)) {
107 uid = pdb_get_uid(sam_pwent);
108 gid = pdb_get_gid(sam_pwent);
109 printf ("User ID/Group ID: %d/%d\n", uid, gid);
111 printf ("User SID: %s\n",
112 sid_string_static(pdb_get_user_sid(sam_pwent)));
113 printf ("Primary Group SID: %s\n",
114 sid_string_static(pdb_get_group_sid(sam_pwent)));
115 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
116 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
117 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
118 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
119 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
120 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
121 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
122 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
123 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
125 tmp = pdb_get_logon_time(sam_pwent);
126 printf ("Logon time: %s\n", tmp ? http_timestring(tmp) : "0");
128 tmp = pdb_get_logoff_time(sam_pwent);
129 printf ("Logoff time: %s\n", tmp ? http_timestring(tmp) : "0");
131 tmp = pdb_get_kickoff_time(sam_pwent);
132 printf ("Kickoff time: %s\n", tmp ? http_timestring(tmp) : "0");
134 tmp = pdb_get_pass_last_set_time(sam_pwent);
135 printf ("Password last set: %s\n", tmp ? http_timestring(tmp) : "0");
137 tmp = pdb_get_pass_can_change_time(sam_pwent);
138 printf ("Password can change: %s\n", tmp ? http_timestring(tmp) : "0");
140 tmp = pdb_get_pass_must_change_time(sam_pwent);
141 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
143 } else if (smbpwdstyle) {
144 if (IS_SAM_UNIX_USER(sam_pwent)) {
148 uid = pdb_get_uid(sam_pwent);
149 pdb_sethexpwd(lm_passwd,
150 pdb_get_lanman_passwd(sam_pwent),
151 pdb_get_acct_ctrl(sam_pwent));
152 pdb_sethexpwd(nt_passwd,
153 pdb_get_nt_passwd(sam_pwent),
154 pdb_get_acct_ctrl(sam_pwent));
156 printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
157 pdb_get_username(sam_pwent),
161 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
162 (uint32)pdb_get_pass_last_set_time(sam_pwent));
164 fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
167 if (IS_SAM_UNIX_USER(sam_pwent)) {
168 printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent),
169 pdb_get_fullname(sam_pwent));
171 printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
178 /*********************************************************
179 Get an Print User Info
180 **********************************************************/
182 static int print_user_info (struct pdb_context *in, char *username, BOOL verbosity, BOOL smbpwdstyle)
184 SAM_ACCOUNT *sam_pwent=NULL;
187 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
191 ret = in->pdb_getsampwnam (in, sam_pwent, username);
194 fprintf (stderr, "Username not found!\n");
195 pdb_free_sam(&sam_pwent);
199 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
200 pdb_free_sam(&sam_pwent);
205 /*********************************************************
207 **********************************************************/
208 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
210 SAM_ACCOUNT *sam_pwent=NULL;
213 check = in->pdb_setsampwent(in, False);
219 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
221 while (check && (ret = in->pdb_getsampwent (in, sam_pwent))) {
223 printf ("---------------\n");
224 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
225 pdb_free_sam(&sam_pwent);
226 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
228 if (check) pdb_free_sam(&sam_pwent);
230 in->pdb_endsampwent(in);
234 /*********************************************************
236 **********************************************************/
238 static int set_user_info (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
240 SAM_ACCOUNT *sam_pwent=NULL;
243 pdb_init_sam(&sam_pwent);
245 ret = in->pdb_getsampwnam (in, sam_pwent, username);
247 fprintf (stderr, "Username not found!\n");
248 pdb_free_sam(&sam_pwent);
253 pdb_set_fullname(sam_pwent, fullname);
255 pdb_set_homedir(sam_pwent, homedir, True);
257 pdb_set_dir_drive(sam_pwent,drive, True);
259 pdb_set_logon_script(sam_pwent, script, True);
261 pdb_set_profile_path (sam_pwent, profile, True);
263 if (in->pdb_update_sam_account (in, sam_pwent))
264 print_user_info (in, username, True, False);
266 fprintf (stderr, "Unable to modify entry!\n");
267 pdb_free_sam(&sam_pwent);
270 pdb_free_sam(&sam_pwent);
274 /*********************************************************
276 **********************************************************/
277 static int new_user (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
279 SAM_ACCOUNT *sam_pwent=NULL;
280 struct passwd *pwd = NULL;
281 char *password1, *password2, *staticpass;
283 ZERO_STRUCT(sam_pwent);
285 if ((pwd = getpwnam_alloc(username))) {
286 pdb_init_sam_pw (&sam_pwent, pwd);
289 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
290 pdb_init_sam(&sam_pwent);
291 if (!pdb_set_username(sam_pwent, username)) {
296 staticpass = getpass("new password:");
297 password1 = strdup(staticpass);
298 memset(staticpass, 0, strlen(staticpass));
299 staticpass = getpass("retype new password:");
300 password2 = strdup(staticpass);
301 memset(staticpass, 0, strlen(staticpass));
302 if (strcmp (password1, password2)) {
303 fprintf (stderr, "Passwords does not match!\n");
304 memset(password1, 0, strlen(password1));
305 SAFE_FREE(password1);
306 memset(password2, 0, strlen(password2));
307 SAFE_FREE(password2);
308 pdb_free_sam (&sam_pwent);
312 pdb_set_plaintext_passwd(sam_pwent, password1);
313 memset(password1, 0, strlen(password1));
314 SAFE_FREE(password1);
315 memset(password2, 0, strlen(password2));
316 SAFE_FREE(password2);
319 pdb_set_fullname(sam_pwent, fullname);
321 pdb_set_homedir (sam_pwent, homedir, True);
323 pdb_set_dir_drive (sam_pwent, drive, True);
325 pdb_set_logon_script(sam_pwent, script, True);
327 pdb_set_profile_path (sam_pwent, profile, True);
329 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
331 if (in->pdb_add_sam_account (in, sam_pwent)) {
332 print_user_info (in, username, True, False);
334 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
335 pdb_free_sam (&sam_pwent);
338 pdb_free_sam (&sam_pwent);
342 /*********************************************************
344 **********************************************************/
346 static int new_machine (struct pdb_context *in, char *machinename)
348 SAM_ACCOUNT *sam_pwent=NULL;
350 char *password = NULL;
352 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
356 if (machinename[strlen (machinename) -1] == '$')
357 machinename[strlen (machinename) -1] = '\0';
359 safe_strcpy (name, machinename, 16);
360 safe_strcat (name, "$", 16);
362 string_set (&password, machinename);
363 strlower_m(password);
365 pdb_set_plaintext_passwd (sam_pwent, password);
367 pdb_set_username (sam_pwent, name);
369 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
371 pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS);
373 if (in->pdb_add_sam_account (in, sam_pwent)) {
374 print_user_info (in, name, True, False);
376 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
377 pdb_free_sam (&sam_pwent);
380 pdb_free_sam (&sam_pwent);
384 /*********************************************************
386 **********************************************************/
388 static int delete_user_entry (struct pdb_context *in, char *username)
390 SAM_ACCOUNT *samaccount = NULL;
392 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
396 if (!in->pdb_getsampwnam(in, samaccount, username)) {
397 fprintf (stderr, "user %s does not exist in the passdb\n", username);
401 return in->pdb_delete_sam_account (in, samaccount);
404 /*********************************************************
406 **********************************************************/
408 static int delete_machine_entry (struct pdb_context *in, char *machinename)
411 SAM_ACCOUNT *samaccount = NULL;
413 safe_strcpy (name, machinename, 16);
414 if (name[strlen(name)] != '$')
415 safe_strcat (name, "$", 16);
417 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
421 if (!in->pdb_getsampwnam(in, samaccount, name)) {
422 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
426 return in->pdb_delete_sam_account (in, samaccount);
429 /*********************************************************
431 **********************************************************/
433 int main (int argc, char **argv)
435 static BOOL list_users = False;
436 static BOOL verbose = False;
437 static BOOL spstyle = False;
438 static BOOL machine = False;
439 static BOOL add_user = False;
440 static BOOL delete_user = False;
441 static BOOL modify_user = False;
442 uint32 setparms, checkparms;
444 static char *full_name = NULL;
445 static char *user_name = NULL;
446 static char *home_dir = NULL;
447 static char *home_drive = NULL;
448 static char *backend = NULL;
449 static char *backend_in = NULL;
450 static char *backend_out = NULL;
451 static char *logon_script = NULL;
452 static char *profile_path = NULL;
453 static char *config_file = dyn_CONFIGFILE;
454 static char *new_debuglevel = NULL;
455 static char *account_policy = NULL;
456 static long int account_policy_value = 0;
457 BOOL account_policy_value_set = False;
459 struct pdb_context *bin;
460 struct pdb_context *bout;
461 struct pdb_context *bdef;
463 struct poptOption long_options[] = {
465 {"list", 'l', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
466 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
467 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
468 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
469 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
470 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
471 {"drive", 'd', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
472 {"script", 's', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
473 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
474 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
475 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
476 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
477 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
478 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
479 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
480 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
481 {"debuglevel", 'D', POPT_ARG_STRING, &new_debuglevel, 0,"set debuglevel",NULL},
482 {"configfile", 'c', POPT_ARG_STRING, &config_file, 0,"use different configuration file",NULL},
483 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
484 {"value", 'V', POPT_ARG_LONG, &account_policy_value, 'V',"set the account policy to this value", NULL},
488 setup_logging("pdbedit", True);
490 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
491 POPT_CONTEXT_KEEP_FIRST);
493 while((opt = poptGetNextOpt(pc)) != -1) {
496 account_policy_value_set = True;
501 if (new_debuglevel) {
502 debug_parse_levels(new_debuglevel);
503 AllowDebugChange = False;
506 if (!lp_load(config_file,True,False,False)) {
507 fprintf(stderr, "Can't load %s - run testparm to debug it\n", config_file);
511 setparms = (config_file ? BIT_CONFIGFILE : 0) +
512 (new_debuglevel ? BIT_DEBUGLEVEL : 0) +
513 (backend ? BIT_BACKEND : 0) +
514 (verbose ? BIT_VERBOSE : 0) +
515 (spstyle ? BIT_SPSTYLE : 0) +
516 (full_name ? BIT_FULLNAME : 0) +
517 (home_dir ? BIT_HOMEDIR : 0) +
518 (home_drive ? BIT_HDIRDRIVE : 0) +
519 (logon_script ? BIT_LOGSCRIPT : 0) +
520 (profile_path ? BIT_PROFILE : 0) +
521 (machine ? BIT_MACHINE : 0) +
522 (user_name ? BIT_USER : 0) +
523 (list_users ? BIT_LIST : 0) +
524 (modify_user ? BIT_MODIFY : 0) +
525 (add_user ? BIT_CREATE : 0) +
526 (delete_user ? BIT_DELETE : 0) +
527 (account_policy ? BIT_ACCPOLICY : 0) +
528 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
529 (backend_in ? BIT_IMPORT : 0) +
530 (backend_out ? BIT_EXPORT : 0);
532 if (setparms & BIT_BACKEND) {
533 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
534 fprintf(stderr, "Can't initialize passdb backend.\n");
538 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
539 fprintf(stderr, "Can't initialize passdb backend.\n");
544 /* the lowest bit options are always accepted */
545 checkparms = setparms & ~MASK_ALWAYS_GOOD;
547 /* accoun tpolicy operations */
548 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
550 int field = account_policy_name_to_fieldnum(account_policy);
552 fprintf(stderr, "No account policy by that name\n");
555 if (!account_policy_get(field, &value)) {
556 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
559 if (account_policy_value_set) {
560 printf("account policy value for %s was %u\n", account_policy, value);
561 if (!account_policy_set(field, account_policy_value)) {
562 fprintf(stderr, "valid account policy, but unable to set value!\n");
565 printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
568 printf("account policy value for %s is %u\n", account_policy, value);
573 /* import and export operations */
574 if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
575 && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT))) {
577 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
578 fprintf(stderr, "Can't initialize passdb backend.\n");
585 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
586 fprintf(stderr, "Can't initialize %s.\n", backend_out);
592 return export_database(bin, bout);
595 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
596 /* fake up BIT_LIST if only BIT_USER is defined */
597 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
598 checkparms += BIT_LIST;
601 /* modify flag is optional to maintain backwards compatibility */
602 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
603 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
604 checkparms += BIT_MODIFY;
607 /* list users operations */
608 if (checkparms & BIT_LIST) {
609 if (!(checkparms & ~BIT_LIST)) {
610 return print_users_list (bdef, verbose, spstyle);
612 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
613 return print_user_info (bdef, user_name, verbose, spstyle);
617 /* mask out users options */
618 checkparms &= ~MASK_USER_GOOD;
620 /* account operation */
621 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
622 /* check use of -u option */
623 if (!(checkparms & BIT_USER)) {
624 fprintf (stderr, "Username not specified! (use -u option)\n");
628 /* account creation operations */
629 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
630 if (checkparms & BIT_MACHINE) {
631 return new_machine (bdef, user_name);
633 return new_user (bdef, user_name, full_name, home_dir,
634 home_drive, logon_script,
639 /* account deletion operations */
640 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
641 if (checkparms & BIT_MACHINE) {
642 return delete_machine_entry (bdef, user_name);
644 return delete_user_entry (bdef, user_name);
648 /* account modification operations */
649 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
650 return set_user_info (bdef, user_name, full_name,
658 if (setparms >= 0x20) {
659 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
661 poptPrintHelp(pc, stderr, 0);