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 pstring global_myworkgroup;
26 extern int DEBUGLEVEL;
29 * Next two lines needed for SunOS and don't
30 * hurt anything else...
35 /*********************************************************
37 **********************************************************/
38 static char *xstrdup(char *s)
42 fprintf(stderr,"out of memory\n");
49 /*********************************************************
50 Print command usage on stderr and die.
51 **********************************************************/
52 static void usage(void)
55 printf("smbpasswd [options] [username] [password]\n");
57 printf("smbpasswd [options] [password]\n");
60 printf(" -s use stdin for password prompt\n");
61 printf(" -D LEVEL debug level\n");
62 printf(" -U USER remote username\n");
63 printf(" -r MACHINE remote machine\n");
66 printf(" -R ORDER name resolve order\n");
67 printf(" -j DOMAIN join domain name\n");
68 printf(" -S synchronise with PDC (if we are BDC)\n");
69 printf(" -a add user\n");
70 printf(" -d disable user\n");
71 printf(" -e enable user\n");
72 printf(" -n set no password\n");
73 printf(" -m workstation trust account\n");
74 printf(" -b backup domain controller account\n");
75 printf(" -i inter-domain trust account\n");
76 printf(" -p user cannot change password\n");
77 printf(" -x user can change password\n");
83 /*********************************************************
85 **********************************************************/
86 static int create_interdomain_trust_acct(char *domain, char *name)
89 unsigned char hash[16];
92 switch (lp_server_role())
96 DEBUG(0, ("Joining domain - we are PDC\n"));
97 sec_chan = SEC_CHAN_DOMAIN;
100 case ROLE_DOMAIN_BDC:
102 DEBUG(0, ("Cannot set up inter-domain trust as BDC!\n"));
107 DEBUG(0, ("Cannot set up inter-domain trust as workstation!\n"));
113 pstrcpy(remote_machine, remote ? remote : lp_passwordserver());
115 if (!remote_machine[0])
117 fprintf(stderr, "You must specify the PDC via 'password server' or -r.");
122 fstrcpy(trust_passwd, name);
123 strlower(trust_passwd);
124 E_md4hash( (uchar *)trust_passwd, hash);
126 if (!create_trust_account_file(domain, name, hash))
132 if(!change_trust_account_password(domain, remote_machine, sec_chan))
134 fprintf(stderr,"Unable to join domain %s.\n",domain);
138 printf("Created Inter-Domain Trust Account for %s.\n",domain);
142 /*********************************************************
144 **********************************************************/
145 static int join_domain(char *domain, char *remote)
147 pstring remote_machine;
148 fstring trust_passwd;
149 unsigned char orig_trust_passwd_hash[16];
152 switch (lp_server_role())
154 case ROLE_DOMAIN_PDC:
156 DEBUG(0, ("Cannot join domain - we are PDC!\n"));
159 case ROLE_DOMAIN_BDC:
161 DEBUG(0, ("Joining Domain as BDC\n"));
162 sec_chan = SEC_CHAN_BDC;
167 DEBUG(0, ("Joining Domain as Workstation\n"));
168 sec_chan = SEC_CHAN_WKSTA;
172 pstrcpy(remote_machine, remote ? remote : lp_passwordserver());
174 if (!remote_machine[0])
176 fprintf(stderr, "You must specify the PDC via 'password server' or -r.");
180 fstrcpy(trust_passwd, global_myname);
181 strlower(trust_passwd);
182 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
184 if (!create_trust_account_file(domain, global_myname, trust_passwd))
189 if(!change_trust_account_password(domain, remote_machine, sec_chan))
191 fprintf(stderr,"Unable to join domain %s.\n",domain);
195 printf("Joined domain %s.\n",domain);
200 static void set_line_buffering(FILE *f)
202 setvbuf(f, NULL, _IOLBF, 0);
205 /*************************************************************
206 Utility function to prompt for passwords from stdin. Each
207 password entered must end with a newline.
208 *************************************************************/
209 static char *stdin_new_passwd(void)
211 static fstring new_passwd;
214 ZERO_ARRAY(new_passwd);
217 * if no error is reported from fgets() and string at least contains
218 * the newline that ends the password, then replace the newline with
221 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
222 if ((len = strlen(new_passwd)) > 0) {
223 if(new_passwd[len-1] == '\n')
224 new_passwd[len - 1] = 0;
231 /*************************************************************
232 Utility function to get passwords via tty or stdin
233 Used if the '-s' option is set to silently get passwords
235 *************************************************************/
236 static char *get_pass( char *prompt, BOOL stdin_get)
240 p = stdin_new_passwd();
247 /*************************************************************
248 Utility function to prompt for new password.
249 *************************************************************/
250 static char *prompt_for_new_password(BOOL stdin_get)
255 ZERO_ARRAY(new_passwd);
257 p = get_pass("New SMB password:", stdin_get);
259 fstrcpy(new_passwd, p);
261 p = get_pass("Retype new SMB password:", stdin_get);
263 if (strcmp(p, new_passwd)) {
264 fprintf(stderr, "Mismatch - password unchanged.\n");
272 /*************************************************************
273 change a password either locally or remotely
274 *************************************************************/
275 static BOOL password_change(const char *remote_machine, char *user_name,
276 char *old_passwd, char *new_passwd,
278 uint16 acb_info, uint16 acb_mask)
284 if (remote_machine != NULL)
287 IS_BITS_SET_SOME(acb_info, ACB_PWNOTREQ | ACB_WSTRUST | ACB_DOMTRUST | ACB_SVRTRUST) ||
288 (IS_BITS_SET_SOME(acb_mask, ACB_DISABLED) &&
289 IS_BITS_CLR_ALL(acb_info, ACB_DISABLED)))
291 /* these things can't be done remotely yet */
294 ret = remote_password_change(remote_machine, user_name,
295 old_passwd, new_passwd,
296 err_str, sizeof(err_str));
299 fprintf(stderr, err_str);
304 ret = local_password_change(user_name, add_user, acb_info, acb_mask,
306 err_str, sizeof(err_str),
307 msg_str, sizeof(msg_str));
315 fprintf(stderr, err_str);
322 /*************************************************************
323 handle password changing for root
324 *************************************************************/
325 static int process_root(int argc, char *argv[])
331 BOOL joining_domain = False;
332 BOOL sam_sync = False;
333 BOOL wks_trust_account = False;
334 BOOL srv_trust_account = False;
335 BOOL dom_trust_account = False;
336 BOOL add_user = False;
337 BOOL disable_user = False;
338 BOOL enable_user = False;
339 BOOL set_no_password = False;
340 BOOL stdin_passwd_get = False;
341 BOOL lock_password = False;
342 BOOL unlock_password = False;
343 char *user_name = NULL;
344 char *new_domain = NULL;
345 char *new_passwd = NULL;
346 char *old_passwd = NULL;
347 char *remote_machine = NULL;
350 while ((ch = getopt(argc, argv, "abdehimnpxj:Sr:sR:D:U:")) != EOF)
362 new_passwd = "XXXXXX";
372 DEBUGLEVEL = atoi(optarg);
377 set_no_password = True;
378 new_passwd = "NO PASSWORD";
382 remote_machine = optarg;
387 set_line_buffering(stdin);
388 set_line_buffering(stdout);
389 set_line_buffering(stderr);
390 stdin_passwd_get = True;
395 lp_set_name_resolve_order(optarg);
400 dom_trust_account = True;
405 srv_trust_account = True;
410 wks_trust_account = True;
416 strupper(new_domain);
417 joining_domain = True;
432 lock_password = True;
437 unlock_password = True;
451 * Ensure add_user and either remote machine or join domain are
454 if (add_user && ((remote_machine != NULL) || joining_domain))
459 if (sam_sync && lp_server_role() != ROLE_DOMAIN_BDC) {
460 fprintf(stderr, "The -S option can only be used on a Backup Domain Controller.\n");
466 if (!dom_trust_account)
468 if (argc != 0) usage();
469 ret = join_domain(new_domain, remote_machine);
471 if ((ret != 0) || (!sam_sync))
478 return synchronise_passdb();
482 * Deal with root - can add a user, but only locally.
493 new_passwd = argv[1];
499 if (!user_name && (pwd = getpwuid(0))) {
500 user_name = xstrdup(pwd->pw_name);
504 fprintf(stderr,"You must specify a username\n");
508 if (wks_trust_account || srv_trust_account || dom_trust_account)
510 /* add the $ automatically */
514 * Remove any trailing '$' before we
515 * generate the initial machine password.
518 if (user_name[strlen(user_name)-1] == '$') {
519 user_name[strlen(user_name)-1] = 0;
523 new_passwd = xstrdup(user_name);
524 strlower(new_passwd);
528 * Now ensure the username ends in '$' for
532 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
536 if (!remote_machine && !Get_Pwnam(user_name, True)) {
537 fprintf(stderr, "User \"%s\" was not found in system password file.\n",
544 if (dom_trust_account)
546 ret = create_interdomain_trust_acct(new_domain,
549 if ((ret != 0) || (!sam_sync))
554 if (remote_machine != NULL) {
555 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
561 * If we are trying to enable a user, first we need to find out
562 * if they are using a modern version of the smbpasswd file that
563 * disables a user by just writing a flag into the file. If so
564 * then we can re-enable a user without prompting for a new
565 * password. If not (ie. they have a no stored password in the
566 * smbpasswd file) then we need to prompt for a new password.
571 struct smb_passwd *smb_pass = getsmbpwnam(user_name);
572 if((smb_pass != NULL) && (smb_pass->smb_passwd != NULL))
574 new_passwd = "XXXX"; /* Don't care. */
580 new_passwd = prompt_for_new_password(stdin_passwd_get);
586 acb_mask |= ACB_DISABLED;
587 acb_info &= ~ACB_DISABLED;
592 acb_mask |= ACB_DISABLED;
593 acb_info |= ACB_DISABLED;
598 acb_mask |= ACB_PWNOTREQ;
599 acb_info |= ACB_PWNOTREQ;
604 acb_mask |= ACB_PWLOCK;
605 acb_info |= ACB_PWLOCK;
610 acb_mask |= ACB_PWLOCK;
611 acb_info &= ~ACB_PWLOCK;
614 if (wks_trust_account)
616 acb_mask |= ACB_WSTRUST;
617 acb_info |= ACB_WSTRUST;
619 else if (srv_trust_account)
621 acb_mask |= ACB_SVRTRUST;
622 acb_info |= ACB_SVRTRUST;
624 else if (dom_trust_account)
626 acb_mask |= ACB_DOMTRUST;
627 acb_info |= ACB_DOMTRUST;
631 acb_mask |= ACB_NORMAL;
632 acb_info |= ACB_NORMAL;
635 if (!password_change(remote_machine, user_name, old_passwd, new_passwd,
636 add_user, acb_info, acb_mask))
638 fprintf(stderr,"Failed to change password entry for %s\n", user_name);
644 printf("User %s disabled.\n", user_name);
648 printf("User %s enabled.\n", user_name);
652 printf("User %s - set to no password.\n", user_name);
654 if (!disable_user && !enable_user && !set_no_password)
656 printf("Password changed for user %s\n", user_name);
662 /*************************************************************
663 handle password changing for non-root
664 *************************************************************/
665 static int process_nonroot(int argc, char *argv[])
667 struct passwd *pwd = NULL;
669 BOOL stdin_passwd_get = False;
670 char *old_passwd = NULL;
671 char *remote_machine = NULL;
672 char *user_name = NULL;
673 char *new_passwd = NULL;
675 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF)
680 DEBUGLEVEL = atoi(optarg);
683 remote_machine = optarg;
686 set_line_buffering(stdin);
687 set_line_buffering(stdout);
688 set_line_buffering(stderr);
689 stdin_passwd_get = True;
707 new_passwd = argv[0];
711 pwd = getpwuid(getuid());
713 user_name = xstrdup(pwd->pw_name);
715 fprintf(stderr,"you don't exist - go away\n");
721 * A non-root user is always setting a password
722 * via a remote machine (even if that machine is
725 if (remote_machine == NULL) {
726 remote_machine = "127.0.0.1";
729 if (remote_machine != NULL) {
730 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
734 new_passwd = prompt_for_new_password(stdin_passwd_get);
738 printf("unable to get new password\n");
742 if (!password_change(remote_machine, user_name,
743 old_passwd, new_passwd,
746 fprintf(stderr,"Failed to change password for %s\n", user_name);
750 printf("Password changed for user %s\n", user_name);
756 /*********************************************************
758 **********************************************************/
759 int main(int argc, char **argv)
761 static pstring servicesf = CONFIGFILE;
765 setup_logging("smbpasswd", True);
767 charset_initialise();
769 if (!lp_load(servicesf,True,False,False)) {
770 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
775 if(!get_myname(myhostname,NULL)) {
776 fprintf(stderr, "unable to get my hostname.\n");
781 * Set the machine NETBIOS name if not already
782 * set from the config file.
785 if (!*global_myname) {
787 fstrcpy(global_myname, myhostname);
788 p = strchr(global_myname, '.' );
791 strupper(global_myname);
793 codepage_initialise(lp_client_code_page());
797 if(!pwdb_initialise(False))
799 fprintf(stderr, "Can't setup password database vectors.\n");
803 /* Check the effective uid - make sure we are not setuid */
804 if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) {
805 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
810 return process_root(argc, argv);
813 return process_nonroot(argc, argv);