2 * Unix SMB/Netbios implementation. Version 1.9. smbpasswd module.
3 * Copyright (C) Jeremy Allison 1995-1999
4 * Copyright (C) Luke Kenneth Casson Leighton 1996-1999
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
23 extern pstring myhostname;
24 extern pstring global_myname;
25 extern int DEBUGLEVEL;
28 * Next two lines needed for SunOS and don't
29 * hurt anything else...
34 /*********************************************************
36 **********************************************************/
37 static char *xstrdup(char *s)
41 fprintf(stderr,"out of memory\n");
48 /*********************************************************
49 Print command usage on stderr and die.
50 **********************************************************/
51 static void usage(void)
54 printf("smbpasswd [options] [username] [password]\n");
56 printf("smbpasswd [options] [password]\n");
59 printf(" -s use stdin for password prompt\n");
60 printf(" -D LEVEL debug level\n");
61 printf(" -U USER remote username\n");
62 printf(" -r MACHINE remote machine\n");
65 printf(" -R ORDER name resolve order\n");
66 printf(" -j DOMAIN join domain name\n");
67 printf(" -a add user\n");
68 printf(" -d disable user\n");
69 printf(" -e enable user\n");
70 printf(" -n set no password\n");
71 printf(" -m workstation trust account\n");
72 printf(" -b backup domain controller account\n");
73 printf(" -i inter-domain trust account\n");
74 printf(" -p user cannot change password\n");
75 printf(" -x user can change password\n");
81 /*********************************************************
83 **********************************************************/
84 static int join_domain(char *domain, char *remote)
86 pstring remote_machine;
88 unsigned char orig_trust_passwd_hash[16];
92 DEBUG(5,("join_domain: domain %s remote %s\n", domain, remote));
94 pstrcpy(remote_machine, remote ? remote : "");
95 fstrcpy(trust_passwd, global_myname);
96 strlower(trust_passwd);
97 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
99 switch (lp_server_role())
101 case ROLE_DOMAIN_PDC:
103 DEBUG(0, ("Cannot join domain - we are PDC!\n"));
106 case ROLE_DOMAIN_BDC:
108 DEBUG(0, ("Joining Domain as BDC\n"));
109 sec_chan = SEC_CHAN_BDC;
114 DEBUG(0, ("Joining Domain as Workstation\n"));
115 sec_chan = SEC_CHAN_WKSTA;
120 * Create the machine account password file.
122 if(!trust_password_lock( domain, global_myname, True))
124 fprintf(stderr, "unable to open the machine account password file for \
125 machine %s in domain %s.\n", global_myname, domain);
130 * Write the old machine account password.
133 if(!set_trust_account_password( orig_trust_passwd_hash))
135 fprintf(stderr, "unable to write the machine account password for \
136 machine %s in domain %s.\n", global_myname, domain);
137 trust_password_unlock();
142 * If we are given a remote machine assume this is the PDC.
147 pstrcpy(remote_machine, lp_passwordserver());
150 if(!*remote_machine) {
151 fprintf(stderr, "No password server list given in smb.conf - \
152 unable to join domain.\n");
153 trust_password_unlock();
157 ret = change_trust_account_password(domain, remote_machine, sec_chan);
158 trust_password_unlock();
161 fprintf(stderr,"Unable to join domain %s.\n",domain);
163 printf("Joined domain %s.\n",domain);
167 trust_password_delete( domain, global_myname);
174 static void set_line_buffering(FILE *f)
176 setvbuf(f, NULL, _IOLBF, 0);
179 /*************************************************************
180 Utility function to prompt for passwords from stdin. Each
181 password entered must end with a newline.
182 *************************************************************/
183 static char *stdin_new_passwd(void)
185 static fstring new_passwd;
188 ZERO_ARRAY(new_passwd);
191 * if no error is reported from fgets() and string at least contains
192 * the newline that ends the password, then replace the newline with
195 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
196 if ((len = strlen(new_passwd)) > 0) {
197 if(new_passwd[len-1] == '\n')
198 new_passwd[len - 1] = 0;
205 /*************************************************************
206 Utility function to get passwords via tty or stdin
207 Used if the '-s' option is set to silently get passwords
209 *************************************************************/
210 static char *get_pass( char *prompt, BOOL stdin_get)
214 p = stdin_new_passwd();
221 /*************************************************************
222 Utility function to prompt for new password.
223 *************************************************************/
224 static char *prompt_for_new_password(BOOL stdin_get)
229 ZERO_ARRAY(new_passwd);
231 p = get_pass("New SMB password:", stdin_get);
233 fstrcpy(new_passwd, p);
235 p = get_pass("Retype new SMB password:", stdin_get);
237 if (strcmp(p, new_passwd)) {
238 fprintf(stderr, "Mismatch - password unchanged.\n");
246 /*************************************************************
247 change a password either locally or remotely
248 *************************************************************/
249 static BOOL password_change(const char *remote_machine, char *user_name,
250 char *old_passwd, char *new_passwd,
252 uint16 acb_info, uint16 acb_mask)
258 if (remote_machine != NULL)
261 IS_BITS_SET_SOME(acb_info, ACB_PWNOTREQ | ACB_WSTRUST | ACB_DOMTRUST | ACB_SVRTRUST) ||
262 (IS_BITS_SET_SOME(acb_mask, ACB_DISABLED) &&
263 IS_BITS_CLR_ALL(acb_info, ACB_DISABLED)))
265 /* these things can't be done remotely yet */
268 ret = remote_password_change(remote_machine, user_name,
269 old_passwd, new_passwd,
270 err_str, sizeof(err_str));
273 fprintf(stderr, err_str);
278 ret = local_password_change(user_name, add_user, acb_info, acb_mask,
280 err_str, sizeof(err_str),
281 msg_str, sizeof(msg_str));
289 fprintf(stderr, err_str);
296 /*************************************************************
297 handle password changing for root
298 *************************************************************/
299 static int process_root(int argc, char *argv[])
305 BOOL joining_domain = False;
306 BOOL wks_trust_account = False;
307 BOOL srv_trust_account = False;
308 BOOL dom_trust_account = False;
309 BOOL add_user = False;
310 BOOL disable_user = False;
311 BOOL enable_user = False;
312 BOOL set_no_password = False;
313 BOOL stdin_passwd_get = False;
314 BOOL lock_password = False;
315 BOOL unlock_password = False;
316 char *user_name = NULL;
317 char *new_domain = NULL;
318 char *new_passwd = NULL;
319 char *old_passwd = NULL;
320 char *remote_machine = NULL;
322 while ((ch = getopt(argc, argv, "abdehimnpxj:r:sR:D:U:")) != EOF)
334 new_passwd = "XXXXXX";
344 DEBUGLEVEL = atoi(optarg);
349 set_no_password = True;
350 new_passwd = "NO PASSWORD";
354 remote_machine = optarg;
359 set_line_buffering(stdin);
360 set_line_buffering(stdout);
361 set_line_buffering(stderr);
362 stdin_passwd_get = True;
367 lp_set_name_resolve_order(optarg);
372 dom_trust_account = True;
377 srv_trust_account = True;
382 wks_trust_account = True;
388 strupper(new_domain);
389 joining_domain = True;
399 lock_password = True;
404 unlock_password = True;
418 * Ensure add_user and either remote machine or join domain are
421 if (add_user && ((remote_machine != NULL) || joining_domain))
428 if (argc != 0) usage();
429 return join_domain(new_domain, remote_machine);
433 * Deal with root - can add a user, but only locally.
444 new_passwd = argv[1];
450 if (!user_name && (pwd = getpwuid(0))) {
451 user_name = xstrdup(pwd->pw_name);
455 fprintf(stderr,"You must specify a username\n");
459 if (wks_trust_account || srv_trust_account || dom_trust_account)
461 /* add the $ automatically */
465 * Remove any trailing '$' before we
466 * generate the initial machine password.
469 if (user_name[strlen(user_name)-1] == '$') {
470 user_name[strlen(user_name)-1] = 0;
474 new_passwd = xstrdup(user_name);
475 strlower(new_passwd);
479 * Now ensure the username ends in '$' for
483 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
487 if (!remote_machine && !Get_Pwnam(user_name, True)) {
488 fprintf(stderr, "User \"%s\" was not found in system password file.\n",
493 if (remote_machine != NULL) {
494 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
501 * If we are trying to enable a user, first we need to find out
502 * if they are using a modern version of the smbpasswd file that
503 * disables a user by just writing a flag into the file. If so
504 * then we can re-enable a user without prompting for a new
505 * password. If not (ie. they have a no stored password in the
506 * smbpasswd file) then we need to prompt for a new password.
511 struct smb_passwd *smb_pass = getsmbpwnam(user_name);
512 if((smb_pass != NULL) && (smb_pass->smb_passwd != NULL))
514 new_passwd = "XXXX"; /* Don't care. */
520 new_passwd = prompt_for_new_password(stdin_passwd_get);
526 acb_mask |= ACB_DISABLED;
527 acb_info &= ~ACB_DISABLED;
532 acb_mask |= ACB_DISABLED;
533 acb_info |= ACB_DISABLED;
538 acb_mask |= ACB_PWNOTREQ;
539 acb_info |= ACB_PWNOTREQ;
544 acb_mask |= ACB_PWLOCK;
545 acb_info |= ACB_PWLOCK;
550 acb_mask |= ACB_PWLOCK;
551 acb_info &= ~ACB_PWLOCK;
554 if (wks_trust_account)
556 acb_mask |= ACB_WSTRUST;
557 acb_info |= ACB_WSTRUST;
559 else if (srv_trust_account)
561 acb_mask |= ACB_SVRTRUST;
562 acb_info |= ACB_SVRTRUST;
564 else if (dom_trust_account)
566 acb_mask |= ACB_DOMTRUST;
567 acb_info |= ACB_DOMTRUST;
571 acb_mask |= ACB_NORMAL;
572 acb_info |= ACB_NORMAL;
575 if (!password_change(remote_machine, user_name, old_passwd, new_passwd,
576 add_user, acb_info, acb_mask))
578 fprintf(stderr,"Failed to change password entry for %s\n", user_name);
584 printf("User %s disabled.\n", user_name);
588 printf("User %s enabled.\n", user_name);
592 printf("User %s - set to no password.\n", user_name);
594 if (!disable_user && !enable_user && !set_no_password)
596 printf("Password changed for user %s\n", user_name);
602 /*************************************************************
603 handle password changing for non-root
604 *************************************************************/
605 static int process_nonroot(int argc, char *argv[])
607 struct passwd *pwd = NULL;
609 BOOL stdin_passwd_get = False;
610 char *old_passwd = NULL;
611 char *remote_machine = NULL;
612 char *user_name = NULL;
613 char *new_passwd = NULL;
615 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF)
620 DEBUGLEVEL = atoi(optarg);
623 remote_machine = optarg;
626 set_line_buffering(stdin);
627 set_line_buffering(stdout);
628 set_line_buffering(stderr);
629 stdin_passwd_get = True;
647 new_passwd = argv[0];
651 pwd = getpwuid(getuid());
653 user_name = xstrdup(pwd->pw_name);
655 fprintf(stderr,"you don't exist - go away\n");
661 * A non-root user is always setting a password
662 * via a remote machine (even if that machine is
665 if (remote_machine == NULL) {
666 remote_machine = "127.0.0.1";
669 if (remote_machine != NULL) {
670 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
674 new_passwd = prompt_for_new_password(stdin_passwd_get);
678 printf("unable to get new password\n");
682 if (!password_change(remote_machine, user_name,
683 old_passwd, new_passwd,
686 fprintf(stderr,"Failed to change password for %s\n", user_name);
690 printf("Password changed for user %s\n", user_name);
696 /*********************************************************
698 **********************************************************/
699 int main(int argc, char **argv)
701 static pstring servicesf = CONFIGFILE;
705 setup_logging("smbpasswd", True);
707 charset_initialise();
709 if (!lp_load(servicesf,True,False,False)) {
710 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
715 if(!get_myname(myhostname,NULL)) {
716 fprintf(stderr, "unable to get my hostname.\n");
721 * Set the machine NETBIOS name if not already
722 * set from the config file.
725 if (!*global_myname) {
727 fstrcpy(global_myname, myhostname);
728 p = strchr(global_myname, '.' );
731 strupper(global_myname);
733 codepage_initialise(lp_client_code_page());
737 if(!pwdb_initialise(False))
739 fprintf(stderr, "Can't setup password database vectors.\n");
743 /* Check the effective uid - make sure we are not setuid */
744 if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) {
745 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
750 return process_root(argc, argv);
753 return process_nonroot(argc, argv);