2 * Unix SMB/Netbios implementation.
5 * Copyright (C) Jeremy Allison 1995-1998
6 * Copyright (C) Tim Potter 2001
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 675
20 * Mass Ave, Cambridge, MA 02139, USA. */
24 extern pstring global_myname;
27 * Next two lines needed for SunOS and don't
28 * hurt anything else...
33 /* forced running in root-mode */
34 static BOOL local_mode;
36 /*********************************************************
37 Print command usage on stderr and die.
38 **********************************************************/
39 static void usage(void)
42 printf("smbpasswd [options] [username] [password]\n");
44 printf("smbpasswd [options] [password]\n");
47 printf(" -s use stdin for password prompt\n");
48 printf(" -D LEVEL debug level\n");
49 printf(" -U USER remote username\n");
50 printf(" -r MACHINE remote machine\n");
52 if (getuid() == 0 || local_mode) {
53 printf(" -L local mode (must be first option)\n");
54 printf(" -R ORDER name resolve order\n");
55 printf(" -j DOMAIN join domain name\n");
56 printf(" -a add user\n");
57 printf(" -x delete user\n");
58 printf(" -d disable user\n");
59 printf(" -e enable user\n");
60 printf(" -n set no password\n");
61 printf(" -m machine trust account\n");
66 /*********************************************************
68 **********************************************************/
69 static int join_domain(char *domain, char *remote)
71 pstring remote_machine;
73 unsigned char orig_trust_passwd_hash[16];
76 pstrcpy(remote_machine, remote ? remote : "");
77 fstrcpy(trust_passwd, global_myname);
78 strlower(trust_passwd);
79 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
81 /* Ensure that we are not trying to join a
82 domain if we are locally set up as a domain
85 if(strequal(remote, global_myname)) {
86 fprintf(stderr, "Cannot join domain %s as the domain controller name is our own. We cannot be a domain controller for a domain and also be a domain member.\n", domain);
91 * Write the old machine account password.
94 if(!secrets_store_trust_account_password(domain, orig_trust_passwd_hash)) {
95 fprintf(stderr, "Unable to write the machine account password for \
96 machine %s in domain %s.\n", global_myname, domain);
101 * If we are given a remote machine assume this is the PDC.
105 pstrcpy(remote_machine, lp_passwordserver());
108 if(!*remote_machine) {
109 fprintf(stderr, "No password server list given in smb.conf - \
110 unable to join domain.\n");
114 ret = change_trust_account_password( domain, remote_machine);
117 trust_password_delete(domain);
118 fprintf(stderr,"Unable to join domain %s.\n",domain);
120 printf("Joined domain %s.\n",domain);
126 /* Initialise client credentials for authenticated pipe access */
128 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
129 char* domain, char* password)
133 if (lp_encrypted_passwords()) {
134 pwd_make_lm_nt_16(&creds->pwd, password);
136 pwd_set_cleartext(&creds->pwd, password);
139 fstrcpy(creds->user_name, username);
140 fstrcpy(creds->domain, domain);
143 /*********************************************************
144 Join a domain using the administrator username and password
145 **********************************************************/
147 /* Macro for checking RPC error codes to make things more readable */
149 #define CHECK_RPC_ERR(rpc, msg) \
150 if (!NT_STATUS_IS_OK(result = rpc)) { \
151 DEBUG(0, (msg ": %s\n", get_nt_error_msg(result))); \
155 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
156 if (!NT_STATUS_IS_OK(result = rpc)) { \
157 DEBUG(0, debug_args); \
161 static int join_domain_byuser(char *domain, char *remote_machine,
162 char *username, char *password)
164 /* libsmb variables */
166 struct nmb_name calling, called;
167 struct ntuser_creds creds;
168 struct cli_state cli;
169 fstring dest_host, acct_name;
170 struct in_addr dest_ip;
176 POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
184 uchar pwbuf[516], ntpw[16], sess_key[16];
185 SAM_USERINFO_CTR ctr;
186 SAM_USER_INFO_24 p24;
187 SAM_USER_INFO_10 p10;
194 /* Connect to remote machine */
199 if (!(mem_ctx = talloc_init())) {
200 DEBUG(0, ("Could not initialise talloc context\n"));
204 if (!cli_initialise(&cli)) {
205 DEBUG(0, ("Could not initialise client structure\n"));
209 init_rpcclient_creds(&creds, username, domain, password);
210 cli_init_creds(&cli, &creds);
212 if (!resolve_srv_name(remote_machine, dest_host, &dest_ip)) {
213 DEBUG(0, ("Could not resolve name %s\n", remote_machine));
217 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
218 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
220 if (!cli_establish_connection(&cli, dest_host, &dest_ip, &calling,
221 &called, "IPC$", "IPC", False, True)) {
222 DEBUG(0, ("Error connecting to %s\n", dest_host));
226 /* Fetch domain sid */
228 if (!cli_nt_session_open(&cli, PIPE_LSARPC)) {
229 DEBUG(0, ("Error connecting to SAM pipe\n"));
234 CHECK_RPC_ERR(cli_lsa_open_policy(&cli, mem_ctx, True,
235 SEC_RIGHTS_MAXIMUM_ALLOWED,
237 "error opening lsa policy handle");
239 CHECK_RPC_ERR(cli_lsa_query_info_policy(&cli, mem_ctx, &lsa_pol,
240 5, domain, &domain_sid),
241 "error querying info policy");
243 cli_lsa_close(&cli, mem_ctx, &lsa_pol);
245 cli_nt_session_close(&cli); /* Done with this pipe */
247 /* Create domain user */
249 if (!cli_nt_session_open(&cli, PIPE_SAMR)) {
250 DEBUG(0, ("Error connecting to SAM pipe\n"));
254 CHECK_RPC_ERR(cli_samr_connect(&cli, mem_ctx,
255 SEC_RIGHTS_MAXIMUM_ALLOWED,
257 "could not connect to SAM database");
260 CHECK_RPC_ERR(cli_samr_open_domain(&cli, mem_ctx, &sam_pol,
261 SEC_RIGHTS_MAXIMUM_ALLOWED,
262 &domain_sid, &domain_pol),
263 "could not open domain");
265 /* Create domain user */
267 fstrcpy(acct_name, global_myname);
268 fstrcat(acct_name, "$");
272 acb_info = (lp_server_role() == ROLE_DOMAIN_BDC) ? ACB_SVRTRUST :
276 uint32 unknown = 0xe005000b;
278 result = cli_samr_create_dom_user(&cli, mem_ctx, &domain_pol,
283 /* We *must* do this.... don't ask... */
285 CHECK_RPC_ERR_DEBUG(cli_samr_close(&cli, mem_ctx, &user_pol), ("error closing user policy"));
286 result = NT_STATUS_USER_EXISTS;
289 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_USER_EXISTS)) {
290 uint32 num_rids, *name_types, *user_rids;
291 uint32 flags = 0x3e8;
294 /* Look up existing rid */
296 names = (char *)&acct_name[0];
299 cli_samr_lookup_names(&cli, mem_ctx,
301 1, &names, &num_rids,
302 &user_rids, &name_types),
303 ("error looking up rid for user %s: %s\n",
304 acct_name, get_nt_error_msg(result)));
306 if (name_types[0] != SID_NAME_USER) {
307 DEBUG(0, ("%s is not a user account\n", acct_name));
311 user_rid = user_rids[0];
313 /* Open handle on user */
316 cli_samr_open_user(&cli, mem_ctx, &domain_pol,
317 SEC_RIGHTS_MAXIMUM_ALLOWED,
318 user_rid, &user_pol),
319 ("could not re-open existing user %s: %s\n",
320 acct_name, get_nt_error_msg(result)));
322 } else if (!NT_STATUS_IS_OK(result)) {
323 DEBUG(0, ("error creating domain user: %s\n",
324 get_nt_error_msg(result)));
328 /* Create a random machine account password */
331 UNISTR2 upw; /* Unicode password */
333 upw.buffer = (uint16 *)talloc_zero(mem_ctx, 0xc *
336 upw.uni_str_len = 0xc;
337 upw.uni_max_len = 0xc;
339 machine_pwd = (char *)upw.buffer;
340 plen = upw.uni_str_len * 2;
341 generate_random_buffer((unsigned char *)machine_pwd, plen, True);
343 encode_pw_buffer((char *)pwbuf, machine_pwd, plen, False);
345 nt_owf_genW(&upw, ntpw);
348 /* Set password on machine account */
353 init_sam_user_info24(&p24, (char *)pwbuf,24);
355 ctr.switch_value = 24;
356 ctr.info.id24 = &p24;
358 /* I don't think this is quite the right place for this
359 calculation. It should be moved somewhere where the credentials
360 are calculated. )-: */
362 mdfour(sess_key, cli.pwd.smb_nt_pwd, 16);
364 CHECK_RPC_ERR(cli_samr_set_userinfo(&cli, mem_ctx, &user_pol, 24,
366 "error setting trust account password");
368 /* Why do we have to try to (re-)set the ACB to be the same as what
369 we passed in the samr_create_dom_user() call? When a NT
370 workstation is joined to a domain by an administrator the
371 acb_info is set to 0x80. For a normal user with "Add
372 workstations to the domain" rights the acb_info is 0x84. I'm
373 not sure whether it is supposed to make a difference or not. NT
374 seems to cope with either value so don't bomb out if the set
375 userinfo2 level 0x10 fails. -tpot */
378 ctr.switch_value = 0x10;
379 ctr.info.id10 = &p10;
381 init_sam_user_info10(&p10, acb_info);
383 /* Ignoring the return value is necessary for joining a domain
384 as a normal user with "Add workstation to domain" privilege. */
386 result = cli_samr_set_userinfo2(&cli, mem_ctx, &user_pol, 0x10,
389 /* Now store the secret in the secrets database */
393 if (!secrets_store_domain_sid(domain, &domain_sid) ||
394 !secrets_store_trust_account_password(domain, ntpw)) {
395 DEBUG(0, ("error storing domain secrets\n"));
399 retval = 0; /* Success! */
402 /* Close down pipe - this will clean up open policy handles */
404 if (cli.nt_pipe_fnum)
405 cli_nt_session_close(&cli);
407 /* Display success or failure */
410 trust_password_delete(domain);
411 fprintf(stderr,"Unable to join domain %s.\n",domain);
413 printf("Joined domain %s.\n",domain);
419 static void set_line_buffering(FILE *f)
421 setvbuf(f, NULL, _IOLBF, 0);
424 /*************************************************************
425 Utility function to prompt for passwords from stdin. Each
426 password entered must end with a newline.
427 *************************************************************/
428 static char *stdin_new_passwd(void)
430 static fstring new_passwd;
433 ZERO_ARRAY(new_passwd);
436 * if no error is reported from fgets() and string at least contains
437 * the newline that ends the password, then replace the newline with
440 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
441 if ((len = strlen(new_passwd)) > 0) {
442 if(new_passwd[len-1] == '\n')
443 new_passwd[len - 1] = 0;
450 /*************************************************************
451 Utility function to get passwords via tty or stdin
452 Used if the '-s' option is set to silently get passwords
454 *************************************************************/
455 static char *get_pass( char *prompt, BOOL stdin_get)
459 p = stdin_new_passwd();
466 /*************************************************************
467 Utility function to prompt for new password.
468 *************************************************************/
469 static char *prompt_for_new_password(BOOL stdin_get)
474 ZERO_ARRAY(new_passwd);
476 p = get_pass("New SMB password:", stdin_get);
478 fstrcpy(new_passwd, p);
481 p = get_pass("Retype new SMB password:", stdin_get);
483 if (strcmp(p, new_passwd)) {
484 fprintf(stderr, "Mismatch - password unchanged.\n");
485 ZERO_ARRAY(new_passwd);
494 /*************************************************************
495 Change a password either locally or remotely.
496 *************************************************************/
498 static BOOL password_change(const char *remote_machine, char *user_name,
499 char *old_passwd, char *new_passwd, int local_flags)
505 if (remote_machine != NULL) {
506 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
507 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
508 /* these things can't be done remotely yet */
511 ret = remote_password_change(remote_machine, user_name,
512 old_passwd, new_passwd, err_str, sizeof(err_str));
514 fprintf(stderr, err_str);
518 ret = local_password_change(user_name, local_flags, new_passwd,
519 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
524 fprintf(stderr, err_str);
530 /*************************************************************
531 Handle password changing for root.
532 *************************************************************/
534 static int process_root(int argc, char *argv[])
538 BOOL joining_domain = False, got_pass = False, got_username = False;
539 int local_flags = LOCAL_SET_PASSWORD;
540 BOOL stdin_passwd_get = False;
541 fstring user_name, user_password;
542 char *new_domain = NULL;
543 char *new_passwd = NULL;
544 char *old_passwd = NULL;
545 char *remote_machine = NULL;
547 ZERO_STRUCT(user_name);
548 ZERO_STRUCT(user_password);
552 while ((ch = getopt(argc, argv, "axdehmnj:r:sR:D:U:L")) != EOF) {
558 local_flags |= LOCAL_ADD_USER;
561 local_flags |= LOCAL_DELETE_USER;
562 local_flags &= ~LOCAL_SET_PASSWORD;
565 local_flags |= LOCAL_DISABLE_USER;
566 local_flags &= ~LOCAL_SET_PASSWORD;
569 local_flags |= LOCAL_ENABLE_USER;
570 local_flags &= ~LOCAL_SET_PASSWORD;
573 local_flags |= LOCAL_TRUST_ACCOUNT;
576 local_flags |= LOCAL_SET_NO_PASSWORD;
577 local_flags &= ~LOCAL_SET_PASSWORD;
581 strupper(new_domain);
582 joining_domain = True;
585 remote_machine = optarg;
588 set_line_buffering(stdin);
589 set_line_buffering(stdout);
590 set_line_buffering(stderr);
591 stdin_passwd_get = True;
594 lp_set_name_resolve_order(optarg);
597 DEBUGLEVEL = atoi(optarg);
603 fstrcpy(user_name, optarg);
605 if ((lp = strchr_m(user_name, '%'))) {
607 fstrcpy(user_password, lp + 1);
609 memset(strchr_m(optarg, '%') + 1, 'X',
610 strlen(user_password));
625 * Ensure both add/delete user are not set
626 * Ensure add/delete user and either remote machine or join domain are
629 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
630 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
631 ((remote_machine != NULL) || joining_domain))) {
635 /* Only load interfaces if we are doing network operations. */
637 if (joining_domain || remote_machine) {
643 if (joining_domain) {
648 /* Are we joining by specifing an admin username and
653 /* Get administrator password if not specified */
656 char *pass = getpass("Password: ");
659 pstrcpy(user_password, pass);
662 return join_domain_byuser(new_domain, remote_machine,
663 user_name, user_password);
666 /* Or just with the server manager? */
668 return join_domain(new_domain, remote_machine);
673 * Deal with root - can add a user, but only locally.
679 fstrcpy(user_name, "");
684 fstrcpy(user_name, argv[0]);
687 if (got_username || got_pass)
689 fstrcpy(user_name, argv[0]);
690 new_passwd = xstrdup(argv[1]);
696 if (!user_name[0] && (pwd = sys_getpwuid(geteuid()))) {
697 fstrcpy(user_name, pwd->pw_name);
701 fprintf(stderr,"You must specify a username\n");
705 if (local_flags & LOCAL_TRUST_ACCOUNT) {
706 /* add the $ automatically */
710 * Remove any trailing '$' before we
711 * generate the initial machine password.
714 if (user_name[strlen(user_name)-1] == '$') {
715 user_name[strlen(user_name)-1] = 0;
718 if (local_flags & LOCAL_ADD_USER) {
719 SAFE_FREE(new_passwd);
720 new_passwd = xstrdup(user_name);
721 strlower(new_passwd);
725 * Now ensure the username ends in '$' for
729 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
730 fstrcpy(user_name, buf);
733 if (remote_machine != NULL) {
734 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
737 if (!(local_flags & LOCAL_SET_PASSWORD)) {
740 * If we are trying to enable a user, first we need to find out
741 * if they are using a modern version of the smbpasswd file that
742 * disables a user by just writing a flag into the file. If so
743 * then we can re-enable a user without prompting for a new
744 * password. If not (ie. they have a no stored password in the
745 * smbpasswd file) then we need to prompt for a new password.
748 if(local_flags & LOCAL_ENABLE_USER) {
749 SAM_ACCOUNT *sampass = NULL;
752 pdb_init_sam(&sampass);
753 ret = pdb_getsampwnam(sampass, user_name);
754 if((sampass != False) && (pdb_get_lanman_passwd(sampass) == NULL)) {
755 local_flags |= LOCAL_SET_PASSWORD;
757 pdb_free_sam(&sampass);
761 if(local_flags & LOCAL_SET_PASSWORD) {
762 new_passwd = prompt_for_new_password(stdin_passwd_get);
765 fprintf(stderr, "Unable to get new password.\n");
770 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
771 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
776 if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
777 SAM_ACCOUNT *sampass = NULL;
780 pdb_init_sam(&sampass);
781 ret = pdb_getsampwnam(sampass, user_name);
783 printf("Password changed for user %s.", user_name );
784 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
785 printf(" User has disabled flag set.");
786 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
787 printf(" User has no password flag set.");
789 pdb_free_sam(&sampass);
793 SAFE_FREE(new_passwd);
798 /*************************************************************
799 handle password changing for non-root
800 *************************************************************/
801 static int process_nonroot(int argc, char *argv[])
803 struct passwd *pwd = NULL;
805 BOOL stdin_passwd_get = False;
806 char *old_passwd = NULL;
807 char *remote_machine = NULL;
808 char *user_name = NULL;
809 char *new_passwd = NULL;
811 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
814 DEBUGLEVEL = atoi(optarg);
817 remote_machine = optarg;
820 set_line_buffering(stdin);
821 set_line_buffering(stdout);
822 set_line_buffering(stderr);
823 stdin_passwd_get = True;
841 new_passwd = argv[0];
845 pwd = sys_getpwuid(getuid());
847 user_name = xstrdup(pwd->pw_name);
849 fprintf(stderr,"you don't exist - go away\n");
855 * A non-root user is always setting a password
856 * via a remote machine (even if that machine is
860 load_interfaces(); /* Delayed from main() */
862 if (remote_machine == NULL) {
863 remote_machine = "127.0.0.1";
866 if (remote_machine != NULL) {
867 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
871 new_passwd = prompt_for_new_password(stdin_passwd_get);
875 fprintf(stderr, "Unable to get new password.\n");
879 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
880 fprintf(stderr,"Failed to change password for %s\n", user_name);
885 printf("Password changed for user %s\n", user_name);
888 SAFE_FREE(old_passwd);
889 SAFE_FREE(new_passwd);
896 /*********************************************************
898 **********************************************************/
899 int main(int argc, char **argv)
901 static pstring servicesf = CONFIGFILE;
903 #if defined(HAVE_SET_AUTH_PARAMETERS)
904 set_auth_parameters(argc, argv);
905 #endif /* HAVE_SET_AUTH_PARAMETERS */
909 setup_logging("smbpasswd", True);
911 if(!initialize_password_db(True)) {
912 fprintf(stderr, "Can't setup password database vectors.\n");
916 if (!lp_load(servicesf,True,False,False)) {
917 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
923 * Set the machine NETBIOS name if not already
924 * set from the config file.
927 if (!*global_myname) {
929 fstrcpy(global_myname, myhostname());
930 p = strchr_m(global_myname, '.' );
933 strupper(global_myname);
935 /* Check the effective uid - make sure we are not setuid */
936 if (is_setuid_root()) {
937 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
941 /* pre-check for local mode option as first option. We can't
942 do this via normal getopt as getopt can't be called
944 if (argc > 1 && strcmp(argv[1], "-L") == 0) {
948 if (local_mode || getuid() == 0) {
950 return process_root(argc, argv);
953 return process_nonroot(argc, argv);