r1643: syncing all changes from 3.0 and hopefully get 3.0.6rc2 out tomorrow
[Samba.git] / source / utils / pdbedit.c
blob1201cf88fcf4f26a8cdb3a5c261cbf3516ffe1b6
1 /*
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.
24 #include "includes.h"
26 #define BIT_BACKEND 0x00000004
27 #define BIT_VERBOSE 0x00000008
28 #define BIT_SPSTYLE 0x00000010
29 #define BIT_RESERV_1 0x00000020
30 #define BIT_RESERV_2 0x00000040
31 #define BIT_RESERV_3 0x00000080
32 #define BIT_FULLNAME 0x00000100
33 #define BIT_HOMEDIR 0x00000200
34 #define BIT_HDIRDRIVE 0x00000400
35 #define BIT_LOGSCRIPT 0x00000800
36 #define BIT_PROFILE 0x00001000
37 #define BIT_MACHINE 0x00002000
38 #define BIT_RESERV_4 0x00004000
39 #define BIT_USER 0x00008000
40 #define BIT_LIST 0x00010000
41 #define BIT_MODIFY 0x00020000
42 #define BIT_CREATE 0x00040000
43 #define BIT_DELETE 0x00080000
44 #define BIT_ACCPOLICY 0x00100000
45 #define BIT_ACCPOLVAL 0x00200000
46 #define BIT_ACCTCTRL 0x00400000
47 #define BIT_RESERV_7 0x00800000
48 #define BIT_IMPORT 0x01000000
49 #define BIT_EXPORT 0x02000000
50 #define BIT_FIX_INIT 0x04000000
51 #define BIT_BADPWRESET 0x08000000
53 #define MASK_ALWAYS_GOOD 0x0000001F
54 #define MASK_USER_GOOD 0x00401F00
56 /*********************************************************
57 Add all currently available users to another db
58 ********************************************************/
60 static int export_database (struct pdb_context *in, struct pdb_context
61 *out, const char *username) {
62 SAM_ACCOUNT *user = NULL;
64 DEBUG(3, ("called with username=\"%s\"\n", username));
66 if (NT_STATUS_IS_ERR(in->pdb_setsampwent(in, 0))) {
67 fprintf(stderr, "Can't sampwent!\n");
68 return 1;
71 if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
72 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
73 return 1;
76 while (NT_STATUS_IS_OK(in->pdb_getsampwent(in, user))) {
77 DEBUG(4, ("Processing account %s\n",
78 user->private.username));
79 if (!username ||
80 (strcmp(username, user->private.username)
81 == 0)) {
82 out->pdb_add_sam_account(out, user);
83 if (!NT_STATUS_IS_OK(pdb_reset_sam(user))) {
84 fprintf(stderr,
85 "Can't reset SAM_ACCOUNT!\n");
86 return 1;
91 in->pdb_endsampwent(in);
93 return 0;
96 /*********************************************************
97 Add all currently available group mappings to another db
98 ********************************************************/
100 static int export_groups (struct pdb_context *in, struct pdb_context *out) {
101 GROUP_MAP *maps = NULL;
102 int i, entries = 0;
104 if (NT_STATUS_IS_ERR(in->pdb_enum_group_mapping(in, SID_NAME_UNKNOWN,
105 &maps, &entries,
106 False))) {
107 fprintf(stderr, "Can't get group mappings!\n");
108 return 1;
111 for (i=0; i<entries; i++) {
112 out->pdb_add_group_mapping_entry(out, &(maps[i]));
115 SAFE_FREE(maps);
117 return 0;
120 /*********************************************************
121 Print info from sam structure
122 **********************************************************/
124 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
126 uid_t uid;
127 time_t tmp;
129 /* TODO: chaeck if entry is a user or a workstation */
130 if (!sam_pwent) return -1;
132 if (verbosity) {
133 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
134 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
135 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
136 printf ("User SID: %s\n",
137 sid_string_static(pdb_get_user_sid(sam_pwent)));
138 printf ("Primary Group SID: %s\n",
139 sid_string_static(pdb_get_group_sid(sam_pwent)));
140 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
141 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
142 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
143 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
144 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
145 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
146 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
147 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
148 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
150 tmp = pdb_get_logon_time(sam_pwent);
151 printf ("Logon time: %s\n", tmp ? http_timestring(tmp) : "0");
153 tmp = pdb_get_logoff_time(sam_pwent);
154 printf ("Logoff time: %s\n", tmp ? http_timestring(tmp) : "0");
156 tmp = pdb_get_kickoff_time(sam_pwent);
157 printf ("Kickoff time: %s\n", tmp ? http_timestring(tmp) : "0");
159 tmp = pdb_get_pass_last_set_time(sam_pwent);
160 printf ("Password last set: %s\n", tmp ? http_timestring(tmp) : "0");
162 tmp = pdb_get_pass_can_change_time(sam_pwent);
163 printf ("Password can change: %s\n", tmp ? http_timestring(tmp) : "0");
165 tmp = pdb_get_pass_must_change_time(sam_pwent);
166 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
168 tmp = pdb_get_bad_password_time(sam_pwent);
169 printf ("Last bad password : %s\n", tmp ? http_timestring(tmp) : "0");
170 printf ("Bad password count : %d\n",
171 pdb_get_bad_password_count(sam_pwent));
173 } else if (smbpwdstyle) {
174 char lm_passwd[33];
175 char nt_passwd[33];
177 uid = nametouid(pdb_get_username(sam_pwent));
178 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
179 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
181 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
182 pdb_get_username(sam_pwent),
183 (unsigned long)uid,
184 lm_passwd,
185 nt_passwd,
186 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
187 (uint32)pdb_get_pass_last_set_time(sam_pwent));
188 } else {
189 uid = nametouid(pdb_get_username(sam_pwent));
190 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
191 pdb_get_fullname(sam_pwent));
194 return 0;
197 /*********************************************************
198 Get an Print User Info
199 **********************************************************/
201 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
203 SAM_ACCOUNT *sam_pwent=NULL;
204 BOOL ret;
206 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
207 return -1;
210 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
212 if (ret==False) {
213 fprintf (stderr, "Username not found!\n");
214 pdb_free_sam(&sam_pwent);
215 return -1;
218 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
219 pdb_free_sam(&sam_pwent);
221 return ret;
224 /*********************************************************
225 List Users
226 **********************************************************/
227 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
229 SAM_ACCOUNT *sam_pwent=NULL;
230 BOOL check, ret;
232 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
233 if (!check) {
234 return 1;
237 check = True;
238 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
240 while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
241 if (verbosity)
242 printf ("---------------\n");
243 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
244 pdb_free_sam(&sam_pwent);
245 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
247 if (check) pdb_free_sam(&sam_pwent);
249 in->pdb_endsampwent(in);
250 return 0;
253 /*********************************************************
254 Fix a list of Users for uninitialised passwords
255 **********************************************************/
256 static int fix_users_list (struct pdb_context *in)
258 SAM_ACCOUNT *sam_pwent=NULL;
259 BOOL check, ret;
261 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
262 if (!check) {
263 return 1;
266 check = True;
267 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
269 while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
270 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
272 if (!pdb_update_sam_account(sam_pwent)) {
273 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
275 pdb_free_sam(&sam_pwent);
276 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
277 if (!check) {
278 fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
282 if (check) pdb_free_sam(&sam_pwent);
284 in->pdb_endsampwent(in);
285 return 0;
288 /*********************************************************
289 Set User Info
290 **********************************************************/
292 static int set_user_info (struct pdb_context *in, const char *username,
293 const char *fullname, const char *homedir,
294 const char *drive, const char *script,
295 const char *profile, const char *account_control,
296 const char *user_sid, const char *group_sid,
297 const BOOL badpw)
299 BOOL updated_autolock = False, updated_badpw = False;
300 SAM_ACCOUNT *sam_pwent=NULL;
301 BOOL ret;
303 pdb_init_sam(&sam_pwent);
305 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
306 if (ret==False) {
307 fprintf (stderr, "Username not found!\n");
308 pdb_free_sam(&sam_pwent);
309 return -1;
312 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
313 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
316 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
317 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
320 if (fullname)
321 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
322 if (homedir)
323 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
324 if (drive)
325 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
326 if (script)
327 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
328 if (profile)
329 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
331 if (account_control) {
332 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
333 ACB_PWNOEXP|ACB_AUTOLOCK);
335 uint16 newflag = pdb_decode_acct_ctrl(account_control);
337 if (newflag & not_settable) {
338 fprintf(stderr, "Can only set [NDHLX] flags\n");
339 pdb_free_sam(&sam_pwent);
340 return -1;
343 pdb_set_acct_ctrl(sam_pwent,
344 (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
345 PDB_CHANGED);
347 if (user_sid) {
348 DOM_SID u_sid;
349 if (!string_to_sid(&u_sid, user_sid)) {
350 /* not a complete sid, may be a RID, try building a SID */
351 int u_rid;
353 if (sscanf(user_sid, "%d", &u_rid) != 1) {
354 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
355 return -1;
357 sid_copy(&u_sid, get_global_sam_sid());
358 sid_append_rid(&u_sid, u_rid);
360 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
362 if (group_sid) {
363 DOM_SID g_sid;
364 if (!string_to_sid(&g_sid, group_sid)) {
365 /* not a complete sid, may be a RID, try building a SID */
366 int g_rid;
368 if (sscanf(group_sid, "%d", &g_rid) != 1) {
369 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
370 return -1;
372 sid_copy(&g_sid, get_global_sam_sid());
373 sid_append_rid(&g_sid, g_rid);
375 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
378 if (badpw) {
379 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
380 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
383 if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
384 print_user_info (in, username, True, False);
385 else {
386 fprintf (stderr, "Unable to modify entry!\n");
387 pdb_free_sam(&sam_pwent);
388 return -1;
390 pdb_free_sam(&sam_pwent);
391 return 0;
394 /*********************************************************
395 Add New User
396 **********************************************************/
397 static int new_user (struct pdb_context *in, const char *username,
398 const char *fullname, const char *homedir,
399 const char *drive, const char *script,
400 const char *profile, char *user_sid, char *group_sid)
402 SAM_ACCOUNT *sam_pwent=NULL;
403 NTSTATUS nt_status;
404 char *password1, *password2, *staticpass;
406 get_global_sam_sid();
408 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_new(&sam_pwent, username, 0))) {
409 DEBUG(0, ("could not create account to add new user %s\n", username));
410 return -1;
413 staticpass = getpass("new password:");
414 password1 = strdup(staticpass);
415 memset(staticpass, 0, strlen(staticpass));
416 staticpass = getpass("retype new password:");
417 password2 = strdup(staticpass);
418 memset(staticpass, 0, strlen(staticpass));
419 if (strcmp (password1, password2)) {
420 fprintf (stderr, "Passwords does not match!\n");
421 memset(password1, 0, strlen(password1));
422 SAFE_FREE(password1);
423 memset(password2, 0, strlen(password2));
424 SAFE_FREE(password2);
425 pdb_free_sam (&sam_pwent);
426 return -1;
429 pdb_set_plaintext_passwd(sam_pwent, password1);
430 memset(password1, 0, strlen(password1));
431 SAFE_FREE(password1);
432 memset(password2, 0, strlen(password2));
433 SAFE_FREE(password2);
435 if (fullname)
436 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
437 if (homedir)
438 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
439 if (drive)
440 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
441 if (script)
442 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
443 if (profile)
444 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
445 if (user_sid) {
446 DOM_SID u_sid;
447 if (!string_to_sid(&u_sid, user_sid)) {
448 /* not a complete sid, may be a RID, try building a SID */
449 int u_rid;
451 if (sscanf(user_sid, "%d", &u_rid) != 1) {
452 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
453 return -1;
455 sid_copy(&u_sid, get_global_sam_sid());
456 sid_append_rid(&u_sid, u_rid);
458 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
460 if (group_sid) {
461 DOM_SID g_sid;
462 if (!string_to_sid(&g_sid, group_sid)) {
463 /* not a complete sid, may be a RID, try building a SID */
464 int g_rid;
466 if (sscanf(group_sid, "%d", &g_rid) != 1) {
467 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
468 return -1;
470 sid_copy(&g_sid, get_global_sam_sid());
471 sid_append_rid(&g_sid, g_rid);
473 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
476 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
478 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
479 print_user_info (in, username, True, False);
480 } else {
481 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
482 pdb_free_sam (&sam_pwent);
483 return -1;
485 pdb_free_sam (&sam_pwent);
486 return 0;
489 /*********************************************************
490 Add New Machine
491 **********************************************************/
493 static int new_machine (struct pdb_context *in, const char *machine_in)
495 SAM_ACCOUNT *sam_pwent=NULL;
496 fstring machinename;
497 fstring machineaccount;
498 struct passwd *pwd = NULL;
500 get_global_sam_sid();
502 fstrcpy(machinename, machine_in);
503 machinename[15]= '\0';
505 if (machinename[strlen (machinename) -1] == '$')
506 machinename[strlen (machinename) -1] = '\0';
508 strlower_m(machinename);
510 fstrcpy(machineaccount, machinename);
511 fstrcat(machineaccount, "$");
513 if ((pwd = getpwnam_alloc(machineaccount))) {
514 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
515 fprintf(stderr, "Could not init sam from pw\n");
516 passwd_free(&pwd);
517 return -1;
519 passwd_free(&pwd);
520 } else {
521 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
522 fprintf(stderr, "Could not init sam from pw\n");
523 return -1;
527 pdb_set_plaintext_passwd (sam_pwent, machinename);
529 pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
531 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
533 pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
535 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
536 print_user_info (in, machineaccount, True, False);
537 } else {
538 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
539 pdb_free_sam (&sam_pwent);
540 return -1;
542 pdb_free_sam (&sam_pwent);
543 return 0;
546 /*********************************************************
547 Delete user entry
548 **********************************************************/
550 static int delete_user_entry (struct pdb_context *in, const char *username)
552 SAM_ACCOUNT *samaccount = NULL;
554 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
555 return -1;
558 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
559 fprintf (stderr, "user %s does not exist in the passdb\n", username);
560 return -1;
563 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
564 fprintf (stderr, "Unable to delete user %s\n", username);
565 return -1;
567 return 0;
570 /*********************************************************
571 Delete machine entry
572 **********************************************************/
574 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
576 fstring name;
577 SAM_ACCOUNT *samaccount = NULL;
579 fstrcpy(name, machinename);
580 name[15] = '\0';
581 if (name[strlen(name)-1] != '$')
582 fstrcat (name, "$");
584 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
585 return -1;
588 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) {
589 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
590 return -1;
593 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
594 fprintf (stderr, "Unable to delete machine %s\n", name);
595 return -1;
598 return 0;
601 /*********************************************************
602 Start here.
603 **********************************************************/
605 int main (int argc, char **argv)
607 static BOOL list_users = False;
608 static BOOL verbose = False;
609 static BOOL spstyle = False;
610 static BOOL machine = False;
611 static BOOL add_user = False;
612 static BOOL delete_user = False;
613 static BOOL modify_user = False;
614 uint32 setparms, checkparms;
615 int opt;
616 static char *full_name = NULL;
617 static const char *user_name = NULL;
618 static char *home_dir = NULL;
619 static char *home_drive = NULL;
620 static char *backend = NULL;
621 static char *backend_in = NULL;
622 static char *backend_out = NULL;
623 static BOOL transfer_groups = False;
624 static BOOL force_initialised_password = False;
625 static char *logon_script = NULL;
626 static char *profile_path = NULL;
627 static char *account_control = NULL;
628 static char *account_policy = NULL;
629 static char *user_sid = NULL;
630 static char *group_sid = NULL;
631 static long int account_policy_value = 0;
632 BOOL account_policy_value_set = False;
633 static BOOL badpw_reset = False;
635 struct pdb_context *bin;
636 struct pdb_context *bout;
637 struct pdb_context *bdef;
638 poptContext pc;
639 struct poptOption long_options[] = {
640 POPT_AUTOHELP
641 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
642 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
643 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
644 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
645 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
646 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
647 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
648 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
649 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
650 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
651 {"group SID", 'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
652 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
653 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
654 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
655 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
656 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
657 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
658 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
659 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
660 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
661 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
662 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
663 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
664 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
665 POPT_COMMON_SAMBA
666 POPT_TABLEEND
669 setup_logging("pdbedit", True);
671 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
672 POPT_CONTEXT_KEEP_FIRST);
674 while((opt = poptGetNextOpt(pc)) != -1) {
675 switch (opt) {
676 case 'C':
677 account_policy_value_set = True;
678 break;
682 poptGetArg(pc); /* Drop argv[0], the program name */
684 if (user_name == NULL)
685 user_name = poptGetArg(pc);
687 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
688 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
689 exit(1);
692 if(!initialize_password_db(False))
693 exit(1);
695 if (!init_names())
696 exit(1);
698 setparms = (backend ? BIT_BACKEND : 0) +
699 (verbose ? BIT_VERBOSE : 0) +
700 (spstyle ? BIT_SPSTYLE : 0) +
701 (full_name ? BIT_FULLNAME : 0) +
702 (home_dir ? BIT_HOMEDIR : 0) +
703 (home_drive ? BIT_HDIRDRIVE : 0) +
704 (logon_script ? BIT_LOGSCRIPT : 0) +
705 (profile_path ? BIT_PROFILE : 0) +
706 (machine ? BIT_MACHINE : 0) +
707 (user_name ? BIT_USER : 0) +
708 (list_users ? BIT_LIST : 0) +
709 (force_initialised_password ? BIT_FIX_INIT : 0) +
710 (modify_user ? BIT_MODIFY : 0) +
711 (add_user ? BIT_CREATE : 0) +
712 (delete_user ? BIT_DELETE : 0) +
713 (account_control ? BIT_ACCTCTRL : 0) +
714 (account_policy ? BIT_ACCPOLICY : 0) +
715 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
716 (backend_in ? BIT_IMPORT : 0) +
717 (backend_out ? BIT_EXPORT : 0) +
718 (badpw_reset ? BIT_BADPWRESET : 0);
720 if (setparms & BIT_BACKEND) {
721 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
722 fprintf(stderr, "Can't initialize passdb backend.\n");
723 return 1;
725 } else {
726 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
727 fprintf(stderr, "Can't initialize passdb backend.\n");
728 return 1;
732 /* the lowest bit options are always accepted */
733 checkparms = setparms & ~MASK_ALWAYS_GOOD;
735 if (checkparms & BIT_FIX_INIT) {
736 return fix_users_list(bdef);
739 /* account policy operations */
740 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
741 uint32 value;
742 int field = account_policy_name_to_fieldnum(account_policy);
743 if (field == 0) {
744 char *apn = account_policy_names_list();
745 fprintf(stderr, "No account policy by that name\n");
746 if (apn) {
747 fprintf(stderr, "Account policy names are :\n%s\n", apn);
749 SAFE_FREE(apn);
750 exit(1);
752 if (!account_policy_get(field, &value)) {
753 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
754 exit(1);
756 if (account_policy_value_set) {
757 printf("account policy value for %s was %u\n", account_policy, value);
758 if (!account_policy_set(field, account_policy_value)) {
759 fprintf(stderr, "valid account policy, but unable to set value!\n");
760 exit(1);
762 printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
763 exit(0);
764 } else {
765 printf("account policy value for %s is %u\n", account_policy, value);
766 exit(0);
770 /* import and export operations */
771 if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
772 && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
773 if (backend_in) {
774 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
775 fprintf(stderr, "Can't initialize passdb backend.\n");
776 return 1;
778 } else {
779 bin = bdef;
781 if (backend_out) {
782 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
783 fprintf(stderr, "Can't initialize %s.\n", backend_out);
784 return 1;
786 } else {
787 bout = bdef;
789 if (transfer_groups) {
790 if (!(checkparms & BIT_USER))
791 return export_groups(bin, bout);
792 } else {
793 if (checkparms & BIT_USER)
794 return export_database(bin, bout,
795 user_name);
796 else
797 return export_database(bin, bout,
798 NULL);
802 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
803 /* fake up BIT_LIST if only BIT_USER is defined */
804 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
805 checkparms += BIT_LIST;
808 /* modify flag is optional to maintain backwards compatibility */
809 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
810 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
811 checkparms += BIT_MODIFY;
814 /* list users operations */
815 if (checkparms & BIT_LIST) {
816 if (!(checkparms & ~BIT_LIST)) {
817 return print_users_list (bdef, verbose, spstyle);
819 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
820 return print_user_info (bdef, user_name, verbose, spstyle);
824 /* mask out users options */
825 checkparms &= ~MASK_USER_GOOD;
827 /* if bad password count is reset, we must be modifying */
828 if (checkparms & BIT_BADPWRESET) {
829 checkparms |= BIT_MODIFY;
830 checkparms &= ~BIT_BADPWRESET;
833 /* account operation */
834 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
835 /* check use of -u option */
836 if (!(checkparms & BIT_USER)) {
837 fprintf (stderr, "Username not specified! (use -u option)\n");
838 return -1;
841 /* account creation operations */
842 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
843 if (checkparms & BIT_MACHINE) {
844 return new_machine (bdef, user_name);
845 } else {
846 return new_user (bdef, user_name, full_name, home_dir,
847 home_drive, logon_script,
848 profile_path, user_sid, group_sid);
852 /* account deletion operations */
853 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
854 if (checkparms & BIT_MACHINE) {
855 return delete_machine_entry (bdef, user_name);
856 } else {
857 return delete_user_entry (bdef, user_name);
861 /* account modification operations */
862 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
863 return set_user_info (bdef, user_name, full_name,
864 home_dir,
865 home_drive,
866 logon_script,
867 profile_path, account_control,
868 user_sid, group_sid,
869 badpw_reset);
873 if (setparms >= 0x20) {
874 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
876 poptPrintHelp(pc, stderr, 0);
878 return 1;