Adding ability to operate on trust passwords to pdbedit. This enables
[Samba/nascimento.git] / source3 / utils / pdbedit.c
blob4178afdaf8d47a1152ab9ebc72c13620b0f1ebb9
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
52 #define BIT_TRUSTDOM 0x10000000
53 #define BIT_TRUSTPW 0x20000000
54 #define BIT_TRUSTSID 0x40000000
55 #define BIT_TRUSTFLAGS 0x80000000
57 #define MASK_ALWAYS_GOOD 0x0000001F
58 #define MASK_USER_GOOD 0x00401F00
60 /*********************************************************
61 Add all currently available users to another db
62 ********************************************************/
64 static int export_database (struct pdb_context *in, struct pdb_context *out) {
65 SAM_ACCOUNT *user = NULL;
67 if (NT_STATUS_IS_ERR(in->pdb_setsampwent(in, 0))) {
68 fprintf(stderr, "Can't sampwent!\n");
69 return 1;
72 if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
73 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
74 return 1;
77 while (NT_STATUS_IS_OK(in->pdb_getsampwent(in, user))) {
78 out->pdb_add_sam_account(out, user);
79 if (!NT_STATUS_IS_OK(pdb_reset_sam(user))){
80 fprintf(stderr, "Can't reset SAM_ACCOUNT!\n");
81 return 1;
85 in->pdb_endsampwent(in);
87 return 0;
90 /*********************************************************
91 Add all currently available group mappings to another db
92 ********************************************************/
94 static int export_groups (struct pdb_context *in, struct pdb_context *out) {
95 GROUP_MAP *maps = NULL;
96 int i, entries = 0;
98 if (NT_STATUS_IS_ERR(in->pdb_enum_group_mapping(in, SID_NAME_UNKNOWN,
99 &maps, &entries,
100 False))) {
101 fprintf(stderr, "Can't get group mappings!\n");
102 return 1;
105 for (i=0; i<entries; i++) {
106 out->pdb_add_group_mapping_entry(out, &(maps[i]));
109 SAFE_FREE(maps);
111 return 0;
114 /*********************************************************
115 Print info from sam structure
116 **********************************************************/
118 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
120 uid_t uid;
121 time_t tmp;
123 /* TODO: chaeck if entry is a user or a workstation */
124 if (!sam_pwent) return -1;
126 if (verbosity) {
127 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
128 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
129 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
130 printf ("User SID: %s\n",
131 sid_string_static(pdb_get_user_sid(sam_pwent)));
132 printf ("Primary Group SID: %s\n",
133 sid_string_static(pdb_get_group_sid(sam_pwent)));
134 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
135 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
136 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
137 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
138 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
139 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
140 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
141 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
142 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
144 tmp = pdb_get_logon_time(sam_pwent);
145 printf ("Logon time: %s\n", tmp ? http_timestring(tmp) : "0");
147 tmp = pdb_get_logoff_time(sam_pwent);
148 printf ("Logoff time: %s\n", tmp ? http_timestring(tmp) : "0");
150 tmp = pdb_get_kickoff_time(sam_pwent);
151 printf ("Kickoff time: %s\n", tmp ? http_timestring(tmp) : "0");
153 tmp = pdb_get_pass_last_set_time(sam_pwent);
154 printf ("Password last set: %s\n", tmp ? http_timestring(tmp) : "0");
156 tmp = pdb_get_pass_can_change_time(sam_pwent);
157 printf ("Password can change: %s\n", tmp ? http_timestring(tmp) : "0");
159 tmp = pdb_get_pass_must_change_time(sam_pwent);
160 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
162 tmp = pdb_get_bad_password_time(sam_pwent);
163 printf ("Last bad password : %s\n", tmp ? http_timestring(tmp) : "0");
164 printf ("Bad password count : %d\n",
165 pdb_get_bad_password_count(sam_pwent));
167 } else if (smbpwdstyle) {
168 char lm_passwd[33];
169 char nt_passwd[33];
171 uid = nametouid(pdb_get_username(sam_pwent));
172 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
173 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
175 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
176 pdb_get_username(sam_pwent),
177 (unsigned long)uid,
178 lm_passwd,
179 nt_passwd,
180 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
181 (uint32)pdb_get_pass_last_set_time(sam_pwent));
182 } else {
183 uid = nametouid(pdb_get_username(sam_pwent));
184 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
185 pdb_get_fullname(sam_pwent));
188 return 0;
191 /*********************************************************
192 Get an Print User Info
193 **********************************************************/
195 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
197 SAM_ACCOUNT *sam_pwent=NULL;
198 BOOL ret;
199 BOOL updated_autolock = False, updated_badpw = False;
201 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
202 return -1;
205 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
207 if (ret==False) {
208 fprintf (stderr, "Username not found!\n");
209 pdb_free_sam(&sam_pwent);
210 return -1;
213 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock))
214 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
216 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw))
217 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
219 if (updated_autolock || updated_badpw) {
220 become_root();
221 if(!pdb_update_sam_account(sam_pwent))
222 DEBUG(1, ("Failed to modify entry.\n"));
223 unbecome_root();
226 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
227 pdb_free_sam(&sam_pwent);
229 return ret;
232 /*********************************************************
233 List Users
234 **********************************************************/
235 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
237 SAM_ACCOUNT *sam_pwent=NULL;
238 BOOL check, ret;
240 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
241 if (!check) {
242 return 1;
245 check = True;
246 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
248 while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
249 if (verbosity)
250 printf ("---------------\n");
251 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
252 pdb_free_sam(&sam_pwent);
253 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
255 if (check) pdb_free_sam(&sam_pwent);
257 in->pdb_endsampwent(in);
258 return 0;
261 /*********************************************************
262 Fix a list of Users for uninitialised passwords
263 **********************************************************/
264 static int fix_users_list (struct pdb_context *in)
266 SAM_ACCOUNT *sam_pwent=NULL;
267 BOOL check, ret;
269 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
270 if (!check) {
271 return 1;
274 check = True;
275 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
277 while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
278 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
280 if (!pdb_update_sam_account(sam_pwent)) {
281 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
283 pdb_free_sam(&sam_pwent);
284 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
285 if (!check) {
286 fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
290 if (check) pdb_free_sam(&sam_pwent);
292 in->pdb_endsampwent(in);
293 return 0;
296 /*********************************************************
297 Set User Info
298 **********************************************************/
300 static int set_user_info (struct pdb_context *in, const char *username,
301 const char *fullname, const char *homedir,
302 const char *drive, const char *script,
303 const char *profile, const char *account_control,
304 const char *user_sid, const char *group_sid,
305 const BOOL badpw)
307 SAM_ACCOUNT *sam_pwent=NULL;
308 BOOL ret;
310 pdb_init_sam(&sam_pwent);
312 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
313 if (ret==False) {
314 fprintf (stderr, "Username not found!\n");
315 pdb_free_sam(&sam_pwent);
316 return -1;
319 if (fullname)
320 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
321 if (homedir)
322 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
323 if (drive)
324 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
325 if (script)
326 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
327 if (profile)
328 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
330 if (account_control) {
331 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
332 ACB_PWNOEXP|ACB_AUTOLOCK);
334 uint16 newflag = pdb_decode_acct_ctrl(account_control);
336 if (newflag & not_settable) {
337 fprintf(stderr, "Can only set [NDHLX] flags\n");
338 pdb_free_sam(&sam_pwent);
339 return -1;
342 pdb_set_acct_ctrl(sam_pwent,
343 (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
344 PDB_CHANGED);
346 if (user_sid) {
347 DOM_SID u_sid;
348 if (!string_to_sid(&u_sid, user_sid)) {
349 /* not a complete sid, may be a RID, try building a SID */
350 int u_rid;
352 if (sscanf(user_sid, "%d", &u_rid) != 1) {
353 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
354 return -1;
356 sid_copy(&u_sid, get_global_sam_sid());
357 sid_append_rid(&u_sid, u_rid);
359 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
361 if (group_sid) {
362 DOM_SID g_sid;
363 if (!string_to_sid(&g_sid, group_sid)) {
364 /* not a complete sid, may be a RID, try building a SID */
365 int g_rid;
367 if (sscanf(group_sid, "%d", &g_rid) != 1) {
368 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
369 return -1;
371 sid_copy(&g_sid, get_global_sam_sid());
372 sid_append_rid(&g_sid, g_rid);
374 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
377 if (badpw) {
378 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
379 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
382 if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
383 print_user_info (in, username, True, False);
384 else {
385 fprintf (stderr, "Unable to modify entry!\n");
386 pdb_free_sam(&sam_pwent);
387 return -1;
389 pdb_free_sam(&sam_pwent);
390 return 0;
393 /*********************************************************
394 Add New User
395 **********************************************************/
396 static int new_user (struct pdb_context *in, const char *username,
397 const char *fullname, const char *homedir,
398 const char *drive, const char *script,
399 const char *profile, char *user_sid, char *group_sid)
401 SAM_ACCOUNT *sam_pwent=NULL;
402 NTSTATUS nt_status;
403 char *password1, *password2, *staticpass;
405 get_global_sam_sid();
407 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_new(&sam_pwent, username, 0))) {
408 DEBUG(0, ("could not create account to add new user %s\n", username));
409 return -1;
412 staticpass = getpass("new password:");
413 password1 = strdup(staticpass);
414 memset(staticpass, 0, strlen(staticpass));
415 staticpass = getpass("retype new password:");
416 password2 = strdup(staticpass);
417 memset(staticpass, 0, strlen(staticpass));
418 if (strcmp (password1, password2)) {
419 fprintf (stderr, "Passwords does not match!\n");
420 memset(password1, 0, strlen(password1));
421 SAFE_FREE(password1);
422 memset(password2, 0, strlen(password2));
423 SAFE_FREE(password2);
424 pdb_free_sam (&sam_pwent);
425 return -1;
428 pdb_set_plaintext_passwd(sam_pwent, password1);
429 memset(password1, 0, strlen(password1));
430 SAFE_FREE(password1);
431 memset(password2, 0, strlen(password2));
432 SAFE_FREE(password2);
434 if (fullname)
435 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
436 if (homedir)
437 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
438 if (drive)
439 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
440 if (script)
441 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
442 if (profile)
443 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
444 if (user_sid) {
445 DOM_SID u_sid;
446 if (!string_to_sid(&u_sid, user_sid)) {
447 /* not a complete sid, may be a RID, try building a SID */
448 int u_rid;
450 if (sscanf(user_sid, "%d", &u_rid) != 1) {
451 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
452 return -1;
454 sid_copy(&u_sid, get_global_sam_sid());
455 sid_append_rid(&u_sid, u_rid);
457 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
459 if (group_sid) {
460 DOM_SID g_sid;
461 if (!string_to_sid(&g_sid, group_sid)) {
462 /* not a complete sid, may be a RID, try building a SID */
463 int g_rid;
465 if (sscanf(group_sid, "%d", &g_rid) != 1) {
466 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
467 return -1;
469 sid_copy(&g_sid, get_global_sam_sid());
470 sid_append_rid(&g_sid, g_rid);
472 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
475 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
477 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
478 print_user_info (in, username, True, False);
479 } else {
480 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
481 pdb_free_sam (&sam_pwent);
482 return -1;
484 pdb_free_sam (&sam_pwent);
485 return 0;
488 /*********************************************************
489 Add New Machine
490 **********************************************************/
492 static int new_machine (struct pdb_context *in, const char *machine_in)
494 SAM_ACCOUNT *sam_pwent=NULL;
495 fstring machinename;
496 fstring machineaccount;
497 struct passwd *pwd = NULL;
499 get_global_sam_sid();
501 fstrcpy(machinename, machine_in);
502 machinename[15]= '\0';
504 if (machinename[strlen (machinename) -1] == '$')
505 machinename[strlen (machinename) -1] = '\0';
507 strlower_m(machinename);
509 fstrcpy(machineaccount, machinename);
510 fstrcat(machineaccount, "$");
512 if ((pwd = getpwnam_alloc(machineaccount))) {
513 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
514 fprintf(stderr, "Could not init sam from pw\n");
515 passwd_free(&pwd);
516 return -1;
518 passwd_free(&pwd);
519 } else {
520 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
521 fprintf(stderr, "Could not init sam from pw\n");
522 return -1;
526 pdb_set_plaintext_passwd (sam_pwent, machinename);
528 pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
530 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
532 pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
534 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
535 print_user_info (in, machineaccount, True, False);
536 } else {
537 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
538 pdb_free_sam (&sam_pwent);
539 return -1;
541 pdb_free_sam (&sam_pwent);
542 return 0;
547 * Add new trusting domain account
549 * @param in initialised pdb_context
550 * @param dom_name trusted domain name given in command line
552 * @return 0 on success, -1 otherwise
555 static int new_trustdom(struct pdb_context *in, const char *dom_name)
557 /* TODO */
558 return -1;
563 * Add new trust relationship password
565 * @param in initialised pdb_context
566 * @param dom_name trusting domain name given in command line
567 * @param dom_sid domain sid given in command line
568 * @param flag trust password type flag given in command line
570 * @return 0 on success, -1 otherwise
573 static int new_trustpw(struct pdb_context *in, const char *dom_name,
574 const char *dom_sid, const char* flag)
576 const int flag_num = 5;
577 typedef struct { const char *name; int val; } flag_conv;
578 flag_conv flags[] = {{ "PASS_MACHINE_TRUST_NT", PASS_MACHINE_TRUST_NT },
579 { "PASS_SERVER_TRUST_NT", PASS_SERVER_TRUST_NT },
580 { "PASS_DOMAIN_TRUST_NT", PASS_DOMAIN_TRUST_NT },
581 { "PASS_MACHINE_TRUST_ADS",PASS_MACHINE_TRUST_ADS },
582 { "PASS_DOMAIN_TRUST_ADS", PASS_DOMAIN_TRUST_ADS }};
584 TALLOC_CTX *mem_ctx = NULL;
585 SAM_TRUST_PASSWD trust;
586 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
587 POLICY_HND connect_hnd;
588 DOM_SID *domain_sid = NULL;
589 smb_ucs2_t *uni_name = NULL;
590 char *givenpass, *domain_name = NULL;
591 struct in_addr srv_ip;
592 fstring srv_name, myname;
593 struct cli_state *cli;
594 int i;
595 time_t lct;
597 if (!dom_name) return -1;
599 mem_ctx = talloc_init("pdbedit: adding new trust password");
601 /* unicode name */
602 trust.private.uni_name_len = strnlen(dom_name, 32);
603 push_ucs2_talloc(mem_ctx, &uni_name, dom_name);
604 strncpy_w(trust.private.uni_name, uni_name, 32);
606 /* flags */
607 for (i = 0; i < flag_num; i++) {
608 if (!StrCaseCmp(flags[i].name, flag)) {
609 trust.private.flags = flags[i].val;
610 i = flag_num; /* stop comparing */
614 /* trusting SID */
615 if (!dom_sid) {
616 /* if sid is not specified in command line, do our best
617 to establish it */
619 /* find domain PDC */
620 if (!get_pdc_ip(dom_name, &srv_ip))
621 return -1;
622 if (is_zero_ip(srv_ip))
623 return -1;
624 if (!name_status_find(dom_name, 0x1b, 0x20, srv_ip, srv_name))
625 return -1;
627 get_myname(myname);
629 /* Connect the domain pdc... */
630 nt_status = cli_full_connection(&cli, myname, srv_name, &srv_ip, 139,
631 "IPC$", "IPC", "", "", "", 0, Undefined, NULL);
632 if (NT_STATUS_IS_ERR(nt_status))
633 return -1;
634 if (!cli_nt_session_open(cli, PI_LSARPC))
635 return -1;
637 /* ...and query the domain sid */
638 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
639 &connect_hnd);
640 if (NT_STATUS_IS_ERR(nt_status)) return -1;
642 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
643 5, &domain_name, &domain_sid);
644 if (NT_STATUS_IS_ERR(nt_status)) return -1;
646 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
647 if (NT_STATUS_IS_ERR(nt_status)) return -1;
649 cli_nt_session_close(cli);
650 cli_shutdown(cli);
652 /* copying sid to trust password structure */
653 sid_copy(&trust.private.domain_sid, domain_sid);
655 } else {
656 if (!string_to_sid(&trust.private.domain_sid, dom_sid)) {
657 printf("Error: wrong SID specified !\n");
658 return -1;
662 /* password */
663 givenpass = getpass("password:");
664 memset(trust.private.pass, '\0', FSTRING_LEN);
665 strncpy(trust.private.pass, givenpass, FSTRING_LEN);
667 /* last change time */
668 lct = time(NULL);
669 trust.private.mod_time = lct;
671 /* store trust password in passdb */
672 nt_status = in->pdb_add_trust_passwd(in, &trust);
674 talloc_destroy(mem_ctx);
675 if (NT_STATUS_IS_OK(nt_status))
676 return 0;
678 return -1;
682 /*********************************************************
683 Delete user entry
684 **********************************************************/
686 static int delete_user_entry (struct pdb_context *in, const char *username)
688 SAM_ACCOUNT *samaccount = NULL;
690 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
691 return -1;
694 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
695 fprintf (stderr, "user %s does not exist in the passdb\n", username);
696 return -1;
699 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
700 fprintf (stderr, "Unable to delete user %s\n", username);
701 return -1;
703 return 0;
706 /*********************************************************
707 Delete machine entry
708 **********************************************************/
710 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
712 fstring name;
713 SAM_ACCOUNT *samaccount = NULL;
715 fstrcpy(name, machinename);
716 name[15] = '\0';
717 if (name[strlen(name)-1] != '$')
718 fstrcat (name, "$");
720 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
721 return -1;
724 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) {
725 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
726 return -1;
729 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
730 fprintf (stderr, "Unable to delete machine %s\n", name);
731 return -1;
734 return 0;
737 /*********************************************************
738 Start here.
739 **********************************************************/
741 int main (int argc, char **argv)
743 static BOOL list_users = False;
744 static BOOL verbose = False;
745 static BOOL spstyle = False;
746 static BOOL machine = False;
747 static BOOL trustdom = False;
748 static BOOL add_user = False;
749 static BOOL delete_user = False;
750 static BOOL modify_user = False;
751 uint32 setparms, checkparms;
752 int opt;
753 static char *full_name = NULL;
754 static const char *user_name = NULL;
755 static char *home_dir = NULL;
756 static char *home_drive = NULL;
757 static char *backend = NULL;
758 static char *backend_in = NULL;
759 static char *backend_out = NULL;
760 static BOOL transfer_groups = False;
761 static BOOL force_initialised_password = False;
762 static char *logon_script = NULL;
763 static char *profile_path = NULL;
764 static char *account_control = NULL;
765 static char *account_policy = NULL;
766 static char *user_sid = NULL;
767 static char *group_sid = NULL;
768 static long int account_policy_value = 0;
769 BOOL account_policy_value_set = False;
770 static BOOL badpw_reset = False;
771 /* trust password parameters */
772 static char *trustpw = NULL;
773 static char *trustsid = NULL;
774 static char *trustflags = NULL;
776 struct pdb_context *bin;
777 struct pdb_context *bout;
778 struct pdb_context *bdef;
779 poptContext pc;
780 struct poptOption long_options[] = {
781 POPT_AUTOHELP
782 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
783 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
784 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
785 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
786 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
787 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
788 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
789 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
790 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
791 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
792 {"group SID", 'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
793 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
794 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
795 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
796 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
797 {"trustdom", 'I', POPT_ARG_NONE, &trustdom, 0, "account is a domain trust account", NULL},
798 {"trustpw", 'N', POPT_ARG_STRING, &trustpw, 0, "trust password's domain name", NULL},
799 {"trustsid", 'T', POPT_ARG_STRING, &trustsid, 0, "trust password's domain sid", NULL},
800 {"trustflags", 'F', POPT_ARG_STRING, &trustflags, 0, "trust password flags", NULL},
801 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
802 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
803 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
804 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
805 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
806 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
807 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
808 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
809 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
810 POPT_COMMON_SAMBA
811 POPT_TABLEEND
814 setup_logging("pdbedit", True);
816 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
817 POPT_CONTEXT_KEEP_FIRST);
819 while((opt = poptGetNextOpt(pc)) != -1) {
820 switch (opt) {
821 case 'C':
822 account_policy_value_set = True;
823 break;
827 poptGetArg(pc); /* Drop argv[0], the program name */
829 if (user_name == NULL)
830 user_name = poptGetArg(pc);
832 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
833 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
834 exit(1);
837 if(!initialize_password_db(False))
838 exit(1);
840 if (!init_names())
841 exit(1);
843 setparms = (backend ? BIT_BACKEND : 0) +
844 (verbose ? BIT_VERBOSE : 0) +
845 (spstyle ? BIT_SPSTYLE : 0) +
846 (full_name ? BIT_FULLNAME : 0) +
847 (home_dir ? BIT_HOMEDIR : 0) +
848 (home_drive ? BIT_HDIRDRIVE : 0) +
849 (logon_script ? BIT_LOGSCRIPT : 0) +
850 (profile_path ? BIT_PROFILE : 0) +
851 (machine ? BIT_MACHINE : 0) +
852 (trustdom ? BIT_TRUSTDOM : 0) +
853 (trustpw ? BIT_TRUSTPW : 0) +
854 (trustsid ? BIT_TRUSTSID : 0) +
855 (trustflags ? BIT_TRUSTFLAGS : 0) +
856 (user_name ? BIT_USER : 0) +
857 (list_users ? BIT_LIST : 0) +
858 (force_initialised_password ? BIT_FIX_INIT : 0) +
859 (modify_user ? BIT_MODIFY : 0) +
860 (add_user ? BIT_CREATE : 0) +
861 (delete_user ? BIT_DELETE : 0) +
862 (account_control ? BIT_ACCTCTRL : 0) +
863 (account_policy ? BIT_ACCPOLICY : 0) +
864 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
865 (backend_in ? BIT_IMPORT : 0) +
866 (backend_out ? BIT_EXPORT : 0) +
867 (badpw_reset ? BIT_BADPWRESET : 0);
869 if (setparms & BIT_BACKEND) {
870 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
871 fprintf(stderr, "Can't initialize passdb backend.\n");
872 return 1;
874 } else {
875 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
876 fprintf(stderr, "Can't initialize passdb backend.\n");
877 return 1;
881 /* the lowest bit options are always accepted */
882 checkparms = setparms & ~MASK_ALWAYS_GOOD;
884 if (checkparms & BIT_FIX_INIT) {
885 return fix_users_list(bdef);
888 /* account policy operations */
889 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
890 uint32 value;
891 int field = account_policy_name_to_fieldnum(account_policy);
892 if (field == 0) {
893 fprintf(stderr, "No account policy by that name\n");
894 exit(1);
896 if (!account_policy_get(field, &value)) {
897 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
898 exit(1);
900 if (account_policy_value_set) {
901 printf("account policy value for %s was %u\n", account_policy, value);
902 if (!account_policy_set(field, account_policy_value)) {
903 fprintf(stderr, "valid account policy, but unable to set value!\n");
904 exit(1);
906 printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
907 exit(0);
908 } else {
909 printf("account policy value for %s is %u\n", account_policy, value);
910 exit(0);
914 /* import and export operations */
915 if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
916 && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT))) {
917 if (backend_in) {
918 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
919 fprintf(stderr, "Can't initialize passdb backend.\n");
920 return 1;
922 } else {
923 bin = bdef;
925 if (backend_out) {
926 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
927 fprintf(stderr, "Can't initialize %s.\n", backend_out);
928 return 1;
930 } else {
931 bout = bdef;
933 if (transfer_groups) {
934 return export_groups(bin, bout);
935 } else {
936 return export_database(bin, bout);
940 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
941 /* fake up BIT_LIST if only BIT_USER is defined */
942 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
943 checkparms += BIT_LIST;
946 /* modify flag is optional to maintain backwards compatibility */
947 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
948 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
949 checkparms += BIT_MODIFY;
952 /* list users operations */
953 if (checkparms & BIT_LIST) {
954 if (!(checkparms & ~BIT_LIST)) {
955 return print_users_list (bdef, verbose, spstyle);
957 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
958 return print_user_info (bdef, user_name, verbose, spstyle);
962 /* mask out users options */
963 checkparms &= ~MASK_USER_GOOD;
965 /* if bad password count is reset, we must be modifying */
966 if (checkparms & BIT_BADPWRESET) {
967 checkparms |= BIT_MODIFY;
968 checkparms &= ~BIT_BADPWRESET;
971 /* account operation */
972 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
973 /* check use of -u option */
974 if (!(checkparms & (BIT_USER + BIT_TRUSTPW))) {
975 fprintf (stderr, "Username not specified! (use -u option)\n");
976 return -1;
979 /* account creation operations */
980 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE + BIT_TRUSTDOM))) {
981 /* machine trust account */
982 if (checkparms & BIT_MACHINE) {
983 return new_machine (bdef, user_name);
984 /* interdomain trust account */
985 } else if (checkparms & BIT_TRUSTDOM) {
986 return new_trustdom(bdef, user_name);
988 /* ordinary user account */
989 } else {
990 return new_user (bdef, user_name, full_name, home_dir,
991 home_drive, logon_script,
992 profile_path, user_sid, group_sid);
996 /* account deletion operations */
997 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
998 if (checkparms & BIT_MACHINE) {
999 return delete_machine_entry (bdef, user_name);
1000 } else {
1001 return delete_user_entry (bdef, user_name);
1005 /* account modification operations */
1006 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
1007 return set_user_info (bdef, user_name, full_name,
1008 home_dir,
1009 home_drive,
1010 logon_script,
1011 profile_path, account_control,
1012 user_sid, group_sid,
1013 badpw_reset);
1017 /* trust password operation */
1018 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
1019 /* trust password creation */
1020 if (!(checkparms & ~(BIT_CREATE + BIT_TRUSTPW + BIT_TRUSTSID + BIT_TRUSTFLAGS))) {
1021 return new_trustpw(bdef, trustpw, trustsid, trustflags);
1026 if (setparms >= 0x20) {
1027 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1029 poptPrintHelp(pc, stderr, 0);
1031 return 1;