2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jeremy Allison 1995-1998
4 * Copyright (C) Tim Potter 2001
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
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. */
22 extern pstring global_myname;
23 extern BOOL AllowDebugChange;
26 * Next two lines needed for SunOS and don't
27 * hurt anything else...
32 /* forced running in root-mode */
33 static BOOL local_mode;
34 static BOOL got_pass = False, got_username = False;
35 static int local_flags = 0;
36 static BOOL stdin_passwd_get = False;
37 static fstring user_name, user_password;
38 static char *new_domain = NULL;
39 static char *new_passwd = NULL;
40 static char *old_passwd = NULL;
41 static char *remote_machine = NULL;
42 static pstring configfile;
45 static fstring ldap_secret;
48 /*********************************************************
49 Print command usage on stderr and die.
50 **********************************************************/
51 static void usage(void)
53 printf("When run by root:\n");
54 printf(" smbpasswd [options] [username] [password]\n");
55 printf("otherwise:\n");
56 printf(" smbpasswd [options] [password]\n\n");
59 printf(" -c smb.conf file Use the given path to the smb.conf file\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");
65 printf("extra options when run by root or in local mode:\n");
66 printf(" -L local mode\n");
67 printf(" -R ORDER name resolve order\n");
68 printf(" -a add user\n");
69 printf(" -x delete user\n");
70 printf(" -d disable user\n");
71 printf(" -e enable user\n");
72 printf(" -n set no password\n");
73 printf(" -m machine trust account\n");
74 printf(" -i interdomain trust account\n");
76 printf(" -w ldap admin password\n");
82 static void set_line_buffering(FILE *f)
84 setvbuf(f, NULL, _IOLBF, 0);
87 /*******************************************************************
88 Process command line options
89 ******************************************************************/
90 static void process_options(int argc, char **argv, BOOL amroot)
95 local_flags = LOCAL_SET_PASSWORD;
97 ZERO_STRUCT(user_name);
98 ZERO_STRUCT(user_password);
102 while ((ch = getopt(argc, argv, "c:axdehmnj:r:sw:R:D:U:L")) != EOF) {
105 local_mode = amroot = True;
108 pstrcpy(configfile,optarg);
111 if (!amroot) goto bad_args;
112 local_flags |= LOCAL_ADD_USER;
115 if (!amroot) goto bad_args;
116 local_flags |= LOCAL_DELETE_USER;
117 local_flags &= ~LOCAL_SET_PASSWORD;
120 if (!amroot) goto bad_args;
121 local_flags |= LOCAL_DISABLE_USER;
122 local_flags &= ~LOCAL_SET_PASSWORD;
125 if (!amroot) goto bad_args;
126 local_flags |= LOCAL_ENABLE_USER;
127 local_flags &= ~LOCAL_SET_PASSWORD;
130 if (!amroot) goto bad_args;
131 local_flags |= LOCAL_TRUST_ACCOUNT;
134 if (!amroot) goto bad_args;
135 local_flags |= LOCAL_INTERDOM_ACCOUNT;
138 if (!amroot) goto bad_args;
139 d_printf("See 'net rpc join' for this functionality\n");
143 if (!amroot) goto bad_args;
144 local_flags |= LOCAL_SET_NO_PASSWORD;
145 new_passwd = smb_xstrdup("NO PASSWORD");
148 remote_machine = optarg;
151 set_line_buffering(stdin);
152 set_line_buffering(stdout);
153 set_line_buffering(stderr);
154 stdin_passwd_get = True;
157 if (!amroot) goto bad_args;
159 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
160 fstrcpy(ldap_secret, optarg);
163 printf("-w not available unless configured --with-ldap\n");
167 if (!amroot) goto bad_args;
168 lp_set_name_resolve_order(optarg);
171 DEBUGLEVEL = atoi(optarg);
177 fstrcpy(user_name, optarg);
179 if ((lp = strchr(user_name, '%'))) {
181 fstrcpy(user_password, lp + 1);
183 memset(strchr_m(optarg, '%') + 1, 'X',
184 strlen(user_password));
202 fstrcpy(user_name, "");
206 new_passwd = argv[0];
211 fstrcpy(user_name, argv[0]);
214 if (!amroot || got_username || got_pass)
216 fstrcpy(user_name, argv[0]);
217 new_passwd = smb_xstrdup(argv[1]);
225 /*************************************************************
226 Utility function to prompt for passwords from stdin. Each
227 password entered must end with a newline.
228 *************************************************************/
229 static char *stdin_new_passwd(void)
231 static fstring new_passwd;
234 ZERO_ARRAY(new_passwd);
237 * if no error is reported from fgets() and string at least contains
238 * the newline that ends the password, then replace the newline with
241 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
242 if ((len = strlen(new_passwd)) > 0) {
243 if(new_passwd[len-1] == '\n')
244 new_passwd[len - 1] = 0;
251 /*************************************************************
252 Utility function to get passwords via tty or stdin
253 Used if the '-s' option is set to silently get passwords
255 *************************************************************/
256 static char *get_pass( char *prompt, BOOL stdin_get)
260 p = stdin_new_passwd();
264 return smb_xstrdup(p);
267 /*************************************************************
268 Utility function to prompt for new password.
269 *************************************************************/
270 static char *prompt_for_new_password(BOOL stdin_get)
275 ZERO_ARRAY(new_passwd);
277 p = get_pass("New SMB password:", stdin_get);
279 fstrcpy(new_passwd, p);
282 p = get_pass("Retype new SMB password:", stdin_get);
284 if (strcmp(p, new_passwd)) {
285 fprintf(stderr, "Mismatch - password unchanged.\n");
286 ZERO_ARRAY(new_passwd);
295 /*************************************************************
296 Change a password either locally or remotely.
297 *************************************************************/
299 static BOOL password_change(const char *remote_machine, char *user_name,
300 char *old_passwd, char *new_passwd, int local_flags)
306 if (remote_machine != NULL) {
307 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
308 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
309 /* these things can't be done remotely yet */
312 ret = remote_password_change(remote_machine, user_name,
313 old_passwd, new_passwd, err_str, sizeof(err_str));
315 fprintf(stderr, err_str);
319 ret = local_password_change(user_name, local_flags, new_passwd,
320 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
325 fprintf(stderr, err_str);
331 /*******************************************************************
332 Store the LDAP admin password in secrets.tdb
333 ******************************************************************/
334 static BOOL store_ldap_admin_pw (char* pw)
342 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw);
347 /*************************************************************
348 Handle password changing for root.
349 *************************************************************/
351 static int process_root(void)
357 if (local_flags & LOCAL_SET_LDAP_ADMIN_PW)
359 printf("Setting stored password for \"%s\" in secrets.tdb\n",
361 if (!store_ldap_admin_pw(ldap_secret))
362 DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
368 * Ensure both add/delete user are not set
369 * Ensure add/delete user and either remote machine or join domain are
372 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
373 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
374 (remote_machine != NULL))) {
378 /* Only load interfaces if we are doing network operations. */
380 if (remote_machine) {
384 if (!user_name[0] && (pwd = sys_getpwuid(geteuid()))) {
385 fstrcpy(user_name, pwd->pw_name);
389 fprintf(stderr,"You must specify a username\n");
393 if (local_flags & LOCAL_TRUST_ACCOUNT) {
394 /* add the $ automatically */
398 * Remove any trailing '$' before we
399 * generate the initial machine password.
402 if (user_name[strlen(user_name)-1] == '$') {
403 user_name[strlen(user_name)-1] = 0;
406 if (local_flags & LOCAL_ADD_USER) {
407 SAFE_FREE(new_passwd);
408 new_passwd = smb_xstrdup(user_name);
409 strlower(new_passwd);
413 * Now ensure the username ends in '$' for
417 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
418 fstrcpy(user_name, buf);
419 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
422 if (local_flags & LOCAL_ADD_USER) {
424 * Prompt for trusting domain's account password
426 new_passwd = prompt_for_new_password(stdin_passwd_get);
428 fprintf(stderr, "Unable to get newpassword.\n");
432 slprintf(buf, sizeof(buf) - 1, "%s$", user_name);
433 fstrcpy(user_name, buf);
437 if (remote_machine != NULL) {
438 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
441 if (!(local_flags & LOCAL_SET_PASSWORD)) {
444 * If we are trying to enable a user, first we need to find out
445 * if they are using a modern version of the smbpasswd file that
446 * disables a user by just writing a flag into the file. If so
447 * then we can re-enable a user without prompting for a new
448 * password. If not (ie. they have a no stored password in the
449 * smbpasswd file) then we need to prompt for a new password.
452 if(local_flags & LOCAL_ENABLE_USER) {
453 SAM_ACCOUNT *sampass = NULL;
456 pdb_init_sam(&sampass);
457 ret = pdb_getsampwnam(sampass, user_name);
458 if((sampass != False) && (pdb_get_lanman_passwd(sampass) == NULL)) {
459 local_flags |= LOCAL_SET_PASSWORD;
461 pdb_free_sam(&sampass);
465 if(local_flags & LOCAL_SET_PASSWORD) {
466 new_passwd = prompt_for_new_password(stdin_passwd_get);
469 fprintf(stderr, "Unable to get new password.\n");
475 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
476 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
482 printf("Password changed for user %s on %s.\n", user_name, remote_machine );
483 } else if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
484 SAM_ACCOUNT *sampass = NULL;
487 pdb_init_sam(&sampass);
488 ret = pdb_getsampwnam(sampass, user_name);
490 printf("Password changed for user %s.", user_name );
491 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
492 printf(" User has disabled flag set.");
493 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
494 printf(" User has no password flag set.");
496 pdb_free_sam(&sampass);
500 SAFE_FREE(new_passwd);
505 /*************************************************************
506 Handle password changing for non-root.
507 *************************************************************/
509 static int process_nonroot(void)
511 struct passwd *pwd = NULL;
515 pwd = sys_getpwuid(getuid());
517 fstrcpy(user_name,pwd->pw_name);
519 fprintf(stderr, "smbpasswd: you don't exist - go away\n");
525 * A non-root user is always setting a password
526 * via a remote machine (even if that machine is
530 load_interfaces(); /* Delayed from main() */
532 if (remote_machine == NULL) {
533 remote_machine = "127.0.0.1";
536 if (remote_machine != NULL) {
537 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
541 new_passwd = prompt_for_new_password(stdin_passwd_get);
545 fprintf(stderr, "Unable to get new password.\n");
549 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
550 fprintf(stderr,"Failed to change password for %s\n", user_name);
555 printf("Password changed for user %s\n", user_name);
558 SAFE_FREE(old_passwd);
559 SAFE_FREE(new_passwd);
566 /*********************************************************
568 **********************************************************/
569 int main(int argc, char **argv)
571 BOOL amroot = getuid() == 0;
573 pstrcpy(configfile, dyn_CONFIGFILE);
574 AllowDebugChange = False;
576 #if defined(HAVE_SET_AUTH_PARAMETERS)
577 set_auth_parameters(argc, argv);
578 #endif /* HAVE_SET_AUTH_PARAMETERS */
580 process_options(argc, argv, amroot);
582 setup_logging("smbpasswd", True);
584 if (!lp_load(configfile,True,False,False)) {
585 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
591 * Set the machine NETBIOS name if not already
592 * set from the config file.
595 if (!*global_myname) {
597 fstrcpy(global_myname, myhostname());
598 p = strchr_m(global_myname, '.' );
601 strupper(global_myname);
603 /* Check the effective uid - make sure we are not setuid */
604 if (is_setuid_root()) {
605 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
609 if (local_mode || amroot) {
611 return process_root();
614 return process_nonroot();