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;
25 extern int DEBUGLEVEL;
28 * Next two lines needed for SunOS and don't
29 * hurt anything else...
34 /* forced running in root-mode */
35 static BOOL local_mode;
37 /*********************************************************
39 **********************************************************/
40 static char *xstrdup(char *s)
44 fprintf(stderr,"out of memory\n");
51 /*********************************************************
52 Print command usage on stderr and die.
53 **********************************************************/
54 static void usage(void)
57 printf("smbpasswd [options] [username] [password]\n");
59 printf("smbpasswd [options] [password]\n");
62 printf(" -s use stdin for password prompt\n");
63 printf(" -D LEVEL debug level\n");
64 printf(" -U USER remote username\n");
65 printf(" -r MACHINE remote machine\n");
67 if (getuid() == 0 || local_mode) {
68 printf(" -L local mode (must be first option)\n");
69 printf(" -R ORDER name resolve order\n");
70 printf(" -j DOMAIN join domain name\n");
71 printf(" -a add user\n");
72 printf(" -x delete user\n");
73 printf(" -d disable user\n");
74 printf(" -e enable user\n");
75 printf(" -n set no password\n");
76 printf(" -m machine trust account\n");
81 /*********************************************************
83 **********************************************************/
84 static int join_domain(char *domain, char *remote)
86 pstring remote_machine;
88 unsigned char orig_trust_passwd_hash[16];
91 pstrcpy(remote_machine, remote ? remote : "");
92 fstrcpy(trust_passwd, global_myname);
93 strlower(trust_passwd);
94 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
96 /* Ensure that we are not trying to join a
97 domain if we are locally set up as a domain
100 if(strequal(remote, global_myname)) {
101 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);
106 * Write the old machine account password.
109 if(!secrets_store_trust_account_password(domain, orig_trust_passwd_hash)) {
110 fprintf(stderr, "Unable to write the machine account password for \
111 machine %s in domain %s.\n", global_myname, domain);
116 * If we are given a remote machine assume this is the PDC.
120 pstrcpy(remote_machine, lp_passwordserver());
123 if(!*remote_machine) {
124 fprintf(stderr, "No password server list given in smb.conf - \
125 unable to join domain.\n");
129 ret = change_trust_account_password( domain, remote_machine);
132 trust_password_delete(domain);
133 fprintf(stderr,"Unable to join domain %s.\n",domain);
135 printf("Joined domain %s.\n",domain);
141 /* Initialise client credentials for authenticated pipe access */
143 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
144 char* domain, char* password)
148 if (lp_encrypted_passwords()) {
149 pwd_make_lm_nt_16(&creds->pwd, password);
151 pwd_set_cleartext(&creds->pwd, password);
154 fstrcpy(creds->user_name, username);
155 fstrcpy(creds->domain, domain);
158 /*********************************************************
159 Join a domain using the administrator username and password
160 **********************************************************/
162 /* Macro for checking RPC error codes to make things more readable */
164 #define CHECK_RPC_ERR(rpc, msg) \
165 if ((result = rpc) != NT_STATUS_OK) { \
166 DEBUG(0, (msg ": %s\n", get_nt_error_msg(result))); \
170 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
171 if ((result = rpc) != NT_STATUS_OK) { \
172 DEBUG(0, debug_args); \
176 static int join_domain_byuser(char *domain, char *remote_machine,
177 char *username, char *password)
179 /* libsmb variables */
181 struct nmb_name calling, called;
182 struct ntuser_creds creds;
183 struct cli_state cli;
184 fstring dest_host, acct_name;
185 struct in_addr dest_ip;
190 POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
198 uchar pwbuf[516], ntpw[16], sess_key[16];
199 SAM_USERINFO_CTR ctr;
200 SAM_USER_INFO_24 p24;
201 SAM_USER_INFO_10 p10;
208 /* Connect to remote machine */
213 if (!(mem_ctx = talloc_init())) {
214 DEBUG(0, ("Could not initialise talloc context\n"));
218 if (!cli_initialise(&cli)) {
219 DEBUG(0, ("Could not initialise client structure\n"));
223 init_rpcclient_creds(&creds, username, domain, password);
224 cli_init_creds(&cli, &creds);
226 if (!resolve_srv_name(remote_machine, dest_host, &dest_ip)) {
227 DEBUG(0, ("Could not resolve name %s\n", remote_machine));
231 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
232 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
234 if (!cli_establish_connection(&cli, dest_host, &dest_ip, &calling,
235 &called, "IPC$", "IPC", False, True)) {
236 DEBUG(0, ("Error connecting to %s\n", dest_host));
240 /* Fetch domain sid */
242 if (!cli_nt_session_open(&cli, PIPE_LSARPC)) {
243 DEBUG(0, ("Error connecting to SAM pipe\n"));
248 CHECK_RPC_ERR(cli_lsa_open_policy(&cli, mem_ctx, True,
249 SEC_RIGHTS_MAXIMUM_ALLOWED,
251 "error opening lsa policy handle");
253 CHECK_RPC_ERR(cli_lsa_query_info_policy(&cli, mem_ctx, &lsa_pol,
254 5, domain, &domain_sid),
255 "error querying info policy");
257 cli_lsa_close(&cli, mem_ctx, &lsa_pol);
259 cli_nt_session_close(&cli); /* Done with this pipe */
261 /* Create domain user */
263 if (!cli_nt_session_open(&cli, PIPE_SAMR)) {
264 DEBUG(0, ("Error connecting to SAM pipe\n"));
268 CHECK_RPC_ERR(cli_samr_connect(&cli, mem_ctx,
269 SEC_RIGHTS_MAXIMUM_ALLOWED,
271 "could not connect to SAM database");
274 CHECK_RPC_ERR(cli_samr_open_domain(&cli, mem_ctx, &sam_pol,
275 SEC_RIGHTS_MAXIMUM_ALLOWED,
276 &domain_sid, &domain_pol),
277 "could not open domain");
279 /* Create domain user */
281 fstrcpy(acct_name, global_myname);
282 fstrcat(acct_name, "$");
287 uint32 unknown = 0xe005000b;
289 result = cli_samr_create_dom_user(&cli, mem_ctx, &domain_pol,
290 acct_name, ACB_WSTRUST,
294 /* We *must* do this.... don't ask... */
296 CHECK_RPC_ERR_DEBUG(cli_samr_close(&cli, mem_ctx, &user_pol), ("error closing user policy"));
297 result = NT_STATUS_USER_EXISTS;
300 if (result == NT_STATUS_USER_EXISTS) {
301 uint32 num_rids, *name_types, *user_rids;
302 uint32 flags = 0x3e8;
305 /* Look up existing rid */
307 names = (char *)&acct_name[0];
310 cli_samr_lookup_names(&cli, mem_ctx,
312 1, &names, &num_rids,
313 &user_rids, &name_types),
314 ("error looking up rid for user %s: %s\n",
315 acct_name, get_nt_error_msg(result)));
317 if (name_types[0] != SID_NAME_USER) {
318 DEBUG(0, ("%s is not a user account\n", acct_name));
322 user_rid = user_rids[0];
324 /* Open handle on user */
327 cli_samr_open_user(&cli, mem_ctx, &domain_pol,
328 SEC_RIGHTS_MAXIMUM_ALLOWED,
329 user_rid, &user_pol),
330 ("could not re-open existing user %s: %s\n",
331 acct_name, get_nt_error_msg(result)));
333 } else if (result != NT_STATUS_OK) {
334 DEBUG(0, ("error creating domain user: %s\n",
335 get_nt_error_msg(result)));
339 /* Create a random machine account password */
342 UNISTR2 upw; /* Unicode password */
344 upw.buffer = (uint16 *)talloc_zero(mem_ctx, 0xc *
347 upw.uni_str_len = 0xc;
348 upw.uni_max_len = 0xc;
350 machine_pwd = (char *)upw.buffer;
351 plen = upw.uni_str_len * 2;
352 generate_random_buffer((unsigned char *)machine_pwd, plen, True);
354 encode_pw_buffer((char *)pwbuf, machine_pwd, plen, False);
356 nt_owf_genW(&upw, ntpw);
359 /* Set password on machine account */
364 init_sam_user_info24(&p24, (char *)pwbuf,24);
366 ctr.switch_value = 24;
367 ctr.info.id24 = &p24;
369 /* I don't think this is quite the right place for this
370 calculation. It should be moved somewhere where the credentials
371 are calculated. )-: */
373 mdfour(sess_key, cli.pwd.smb_nt_pwd, 16);
375 CHECK_RPC_ERR(cli_samr_set_userinfo(&cli, mem_ctx, &user_pol, 24,
377 "error setting trust account password");
379 /* Why do we have to try to (re-)set the ACB to be the same as what
380 we passed in the samr_create_dom_user() call? When a NT
381 workstation is joined to a domain by an administrator the
382 acb_info is set to 0x80. For a normal user with "Add
383 workstations to the domain" rights the acb_info is 0x84. I'm
384 not sure whether it is supposed to make a difference or not. NT
385 seems to cope with either value so don't bomb out if the set
386 userinfo2 level 0x10 fails. -tpot */
389 ctr.switch_value = 0x10;
390 ctr.info.id10 = &p10;
392 init_sam_user_info10(&p10, ACB_WSTRUST);
394 /* Ignoring the return value is necessary for joining a domain
395 as a normal user with "Add workstation to domain" privilege. */
397 result = cli_samr_set_userinfo2(&cli, mem_ctx, &user_pol, 0x10,
400 /* Now store the secret in the secrets database */
404 if (!secrets_store_domain_sid(domain, &domain_sid) ||
405 !secrets_store_trust_account_password(domain, ntpw)) {
406 DEBUG(0, ("error storing domain secrets\n"));
410 retval = 0; /* Success! */
413 /* Close down pipe - this will clean up open policy handles */
415 if (cli.nt_pipe_fnum)
416 cli_nt_session_close(&cli);
418 /* Display success or failure */
421 trust_password_delete(domain);
422 fprintf(stderr,"Unable to join domain %s.\n",domain);
424 printf("Joined domain %s.\n",domain);
430 static void set_line_buffering(FILE *f)
432 setvbuf(f, NULL, _IOLBF, 0);
435 /*************************************************************
436 Utility function to prompt for passwords from stdin. Each
437 password entered must end with a newline.
438 *************************************************************/
439 static char *stdin_new_passwd(void)
441 static fstring new_passwd;
444 ZERO_ARRAY(new_passwd);
447 * if no error is reported from fgets() and string at least contains
448 * the newline that ends the password, then replace the newline with
451 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
452 if ((len = strlen(new_passwd)) > 0) {
453 if(new_passwd[len-1] == '\n')
454 new_passwd[len - 1] = 0;
461 /*************************************************************
462 Utility function to get passwords via tty or stdin
463 Used if the '-s' option is set to silently get passwords
465 *************************************************************/
466 static char *get_pass( char *prompt, BOOL stdin_get)
470 p = stdin_new_passwd();
477 /*************************************************************
478 Utility function to prompt for new password.
479 *************************************************************/
480 static char *prompt_for_new_password(BOOL stdin_get)
485 ZERO_ARRAY(new_passwd);
487 p = get_pass("New SMB password:", stdin_get);
489 fstrcpy(new_passwd, p);
492 p = get_pass("Retype new SMB password:", stdin_get);
494 if (strcmp(p, new_passwd)) {
495 fprintf(stderr, "Mismatch - password unchanged.\n");
496 ZERO_ARRAY(new_passwd);
505 /*************************************************************
506 Change a password either locally or remotely.
507 *************************************************************/
509 static BOOL password_change(const char *remote_machine, char *user_name,
510 char *old_passwd, char *new_passwd, int local_flags)
516 if (remote_machine != NULL) {
517 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
518 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
519 /* these things can't be done remotely yet */
522 ret = remote_password_change(remote_machine, user_name,
523 old_passwd, new_passwd, err_str, sizeof(err_str));
525 fprintf(stderr, err_str);
529 ret = local_password_change(user_name, local_flags, new_passwd,
530 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
535 fprintf(stderr, err_str);
541 /*************************************************************
542 Handle password changing for root.
543 *************************************************************/
545 static int process_root(int argc, char *argv[])
549 BOOL joining_domain = False, got_pass = False;
551 BOOL stdin_passwd_get = False;
552 fstring user_name, user_password;
553 char *new_domain = NULL;
554 char *new_passwd = NULL;
555 char *old_passwd = NULL;
556 char *remote_machine = NULL;
558 ZERO_STRUCT(user_name);
559 ZERO_STRUCT(user_password);
561 while ((ch = getopt(argc, argv, "ax:d:e:hmnj:r:sR:D:U:L")) != EOF) {
567 local_flags |= LOCAL_ADD_USER;
570 local_flags |= LOCAL_DELETE_USER;
571 fstrcpy(user_name, optarg);
572 new_passwd = xstrdup("XXXXXX");
575 local_flags |= LOCAL_DISABLE_USER;
576 fstrcpy(user_name, optarg);
577 new_passwd = xstrdup("XXXXXX");
580 local_flags |= LOCAL_ENABLE_USER;
581 fstrcpy(user_name, optarg);
584 local_flags |= LOCAL_TRUST_ACCOUNT;
587 local_flags |= LOCAL_SET_NO_PASSWORD;
588 new_passwd = xstrdup("NO PASSWORD");
592 strupper(new_domain);
593 joining_domain = True;
596 remote_machine = optarg;
599 set_line_buffering(stdin);
600 set_line_buffering(stdout);
601 set_line_buffering(stderr);
602 stdin_passwd_get = True;
605 lp_set_name_resolve_order(optarg);
608 DEBUGLEVEL = atoi(optarg);
613 fstrcpy(user_name, optarg);
615 if ((lp = strchr_m(user_name, '%'))) {
617 fstrcpy(user_password, lp + 1);
619 memset(strchr_m(optarg, '%') + 1, 'X',
620 strlen(user_password));
635 * Ensure both add/delete user are not set
636 * Ensure add/delete user and either remote machine or join domain are
639 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
640 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
641 ((remote_machine != NULL) || joining_domain))) {
645 /* Only load interfaces if we are doing network operations. */
647 if (joining_domain || remote_machine) {
653 if (joining_domain) {
658 /* Are we joining by specifing an admin username and
663 /* Get administrator password if not specified */
666 char *pass = getpass("Password: ");
669 pstrcpy(user_password, pass);
672 return join_domain_byuser(new_domain, remote_machine,
673 user_name, user_password);
676 /* Or just with the server manager? */
678 return join_domain(new_domain, remote_machine);
683 * Deal with root - can add a user, but only locally.
690 fstrcpy(user_name, argv[0]);
693 fstrcpy(user_name, argv[0]);
694 new_passwd = xstrdup(argv[1]);
700 if (!user_name[0] && (pwd = sys_getpwuid(0))) {
701 fstrcpy(user_name, pwd->pw_name);
705 fprintf(stderr,"You must specify a username\n");
709 if (local_flags & LOCAL_TRUST_ACCOUNT) {
710 /* add the $ automatically */
714 * Remove any trailing '$' before we
715 * generate the initial machine password.
718 if (user_name[strlen(user_name)-1] == '$') {
719 user_name[strlen(user_name)-1] = 0;
722 if (local_flags & LOCAL_ADD_USER) {
723 safe_free(new_passwd);
724 new_passwd = xstrdup(user_name);
725 strlower(new_passwd);
729 * Now ensure the username ends in '$' for
733 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
734 fstrcpy(user_name, buf);
737 if (remote_machine != NULL) {
738 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
744 * If we are trying to enable a user, first we need to find out
745 * if they are using a modern version of the smbpasswd file that
746 * disables a user by just writing a flag into the file. If so
747 * then we can re-enable a user without prompting for a new
748 * password. If not (ie. they have a no stored password in the
749 * smbpasswd file) then we need to prompt for a new password.
752 if(local_flags & LOCAL_ENABLE_USER) {
753 SAM_ACCOUNT *sampass = NULL;
756 pdb_init_sam(&sampass);
757 ret = pdb_getsampwnam(sampass, user_name);
758 if((sampass != False) && (pdb_get_lanman_passwd(sampass) != NULL)) {
759 new_passwd = xstrdup("XXXX"); /* Don't care. */
761 pdb_free_sam(sampass);
765 new_passwd = prompt_for_new_password(stdin_passwd_get);
768 fprintf(stderr, "Unable to get new password.\n");
773 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
774 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
779 if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD))) {
780 SAM_ACCOUNT *sampass = NULL;
783 pdb_init_sam(&sampass);
784 ret = pdb_getsampwnam(sampass, user_name);
786 printf("Password changed for user %s.", user_name );
787 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
788 printf(" User has disabled flag set.");
789 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
790 printf(" User has no password flag set.");
792 pdb_free_sam(sampass);
796 safe_free(new_passwd);
801 /*************************************************************
802 handle password changing for non-root
803 *************************************************************/
804 static int process_nonroot(int argc, char *argv[])
806 struct passwd *pwd = NULL;
808 BOOL stdin_passwd_get = False;
809 char *old_passwd = NULL;
810 char *remote_machine = NULL;
811 char *user_name = NULL;
812 char *new_passwd = NULL;
814 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
817 DEBUGLEVEL = atoi(optarg);
820 remote_machine = optarg;
823 set_line_buffering(stdin);
824 set_line_buffering(stdout);
825 set_line_buffering(stderr);
826 stdin_passwd_get = True;
844 new_passwd = argv[0];
848 pwd = sys_getpwuid(getuid());
850 user_name = xstrdup(pwd->pw_name);
852 fprintf(stderr,"you don't exist - go away\n");
858 * A non-root user is always setting a password
859 * via a remote machine (even if that machine is
863 load_interfaces(); /* Delayed from main() */
865 if (remote_machine == NULL) {
866 remote_machine = "127.0.0.1";
869 if (remote_machine != NULL) {
870 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
874 new_passwd = prompt_for_new_password(stdin_passwd_get);
878 fprintf(stderr, "Unable to get new password.\n");
882 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
883 fprintf(stderr,"Failed to change password for %s\n", user_name);
888 printf("Password changed for user %s\n", user_name);
891 safe_free(old_passwd);
892 safe_free(new_passwd);
899 /*********************************************************
901 **********************************************************/
902 int main(int argc, char **argv)
904 static pstring servicesf = CONFIGFILE;
906 #if defined(HAVE_SET_AUTH_PARAMETERS)
907 set_auth_parameters(argc, argv);
908 #endif /* HAVE_SET_AUTH_PARAMETERS */
912 setup_logging("smbpasswd", True);
914 if(!initialize_password_db(True)) {
915 fprintf(stderr, "Can't setup password database vectors.\n");
919 if (!lp_load(servicesf,True,False,False)) {
920 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
926 * Set the machine NETBIOS name if not already
927 * set from the config file.
930 if (!*global_myname) {
932 fstrcpy(global_myname, myhostname());
933 p = strchr_m(global_myname, '.' );
936 strupper(global_myname);
938 /* Check the effective uid - make sure we are not setuid */
939 if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) {
940 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
944 /* pre-check for local mode option as first option. We can't
945 do this via normal getopt as getopt can't be called
947 if (argc > 1 && strcmp(argv[1], "-L") == 0) {
951 if (local_mode || getuid() == 0) {
953 return process_root(argc, argv);
956 return process_nonroot(argc, argv);