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(" -L local mode (must be first option)\n");
60 printf(" -h print this usage message\n");
61 printf(" -s use stdin for password prompt\n");
62 printf(" -c smb.conf file Use the given path to the smb.conf file\n");
63 printf(" -D LEVEL debug level\n");
64 printf(" -r MACHINE remote machine\n");
65 printf(" -U USER remote username\n");
67 printf("extra options when run by root or in local mode:\n");
68 printf(" -a add user\n");
69 printf(" -d disable user\n");
70 printf(" -e enable user\n");
71 printf(" -i interdomain trust account\n");
72 printf(" -m machine trust account\n");
73 printf(" -n set no password\n");
75 printf(" -w ldap admin password\n");
77 printf(" -x delete user\n");
78 printf(" -R ORDER name resolve order\n");
83 static void set_line_buffering(FILE *f)
85 setvbuf(f, NULL, _IOLBF, 0);
88 /*******************************************************************
89 Process command line options
90 ******************************************************************/
91 static void process_options(int argc, char **argv, BOOL amroot)
96 local_flags = LOCAL_SET_PASSWORD;
98 ZERO_STRUCT(user_name);
99 ZERO_STRUCT(user_password);
103 while ((ch = getopt(argc, argv, "c:axdehmnj:r:sw:R:D:U:L")) != EOF) {
106 local_mode = amroot = True;
107 local_flags = LOCAL_SET_PASSWORD;
110 pstrcpy(configfile,optarg);
113 if (!amroot) goto bad_args;
114 local_flags |= LOCAL_ADD_USER;
117 if (!amroot) goto bad_args;
118 local_flags |= LOCAL_DELETE_USER;
119 local_flags &= ~LOCAL_SET_PASSWORD;
122 if (!amroot) goto bad_args;
123 local_flags |= LOCAL_DISABLE_USER;
124 local_flags &= ~LOCAL_SET_PASSWORD;
127 if (!amroot) goto bad_args;
128 local_flags |= LOCAL_ENABLE_USER;
129 local_flags &= ~LOCAL_SET_PASSWORD;
132 if (!amroot) goto bad_args;
133 local_flags |= LOCAL_TRUST_ACCOUNT;
136 if (!amroot) goto bad_args;
137 local_flags |= LOCAL_INTERDOM_ACCOUNT;
140 if (!amroot) goto bad_args;
141 d_printf("See 'net rpc join' for this functionality\n");
145 if (!amroot) goto bad_args;
146 local_flags |= LOCAL_SET_NO_PASSWORD;
147 new_passwd = smb_xstrdup("NO PASSWORD");
150 remote_machine = optarg;
153 set_line_buffering(stdin);
154 set_line_buffering(stdout);
155 set_line_buffering(stderr);
156 stdin_passwd_get = True;
159 if (!amroot) goto bad_args;
161 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
162 fstrcpy(ldap_secret, optarg);
165 printf("-w not available unless configured --with-ldap\n");
169 if (!amroot) goto bad_args;
170 lp_set_name_resolve_order(optarg);
173 DEBUGLEVEL = atoi(optarg);
179 fstrcpy(user_name, optarg);
181 if ((lp = strchr(user_name, '%'))) {
183 fstrcpy(user_password, lp + 1);
185 memset(strchr_m(optarg, '%') + 1, 'X',
186 strlen(user_password));
204 fstrcpy(user_name, "");
208 new_passwd = argv[0];
213 fstrcpy(user_name, argv[0]);
216 if (!amroot || got_username || got_pass)
218 fstrcpy(user_name, argv[0]);
219 new_passwd = smb_xstrdup(argv[1]);
227 /*************************************************************
228 Utility function to prompt for passwords from stdin. Each
229 password entered must end with a newline.
230 *************************************************************/
231 static char *stdin_new_passwd(void)
233 static fstring new_passwd;
236 ZERO_ARRAY(new_passwd);
239 * if no error is reported from fgets() and string at least contains
240 * the newline that ends the password, then replace the newline with
243 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
244 if ((len = strlen(new_passwd)) > 0) {
245 if(new_passwd[len-1] == '\n')
246 new_passwd[len - 1] = 0;
253 /*************************************************************
254 Utility function to get passwords via tty or stdin
255 Used if the '-s' option is set to silently get passwords
257 *************************************************************/
258 static char *get_pass( char *prompt, BOOL stdin_get)
262 p = stdin_new_passwd();
266 return smb_xstrdup(p);
269 /*************************************************************
270 Utility function to prompt for new password.
271 *************************************************************/
272 static char *prompt_for_new_password(BOOL stdin_get)
277 ZERO_ARRAY(new_passwd);
279 p = get_pass("New SMB password:", stdin_get);
281 fstrcpy(new_passwd, p);
284 p = get_pass("Retype new SMB password:", stdin_get);
286 if (strcmp(p, new_passwd)) {
287 fprintf(stderr, "Mismatch - password unchanged.\n");
288 ZERO_ARRAY(new_passwd);
297 /*************************************************************
298 Change a password either locally or remotely.
299 *************************************************************/
301 static BOOL password_change(const char *remote_machine, char *user_name,
302 char *old_passwd, char *new_passwd, int local_flags)
308 if (remote_machine != NULL) {
309 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
310 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
311 /* these things can't be done remotely yet */
314 ret = remote_password_change(remote_machine, user_name,
315 old_passwd, new_passwd, err_str, sizeof(err_str));
317 fprintf(stderr, err_str);
321 ret = local_password_change(user_name, local_flags, new_passwd,
322 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
327 fprintf(stderr, err_str);
333 /*******************************************************************
334 Store the LDAP admin password in secrets.tdb
335 ******************************************************************/
336 static BOOL store_ldap_admin_pw (char* pw)
344 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw);
349 /*************************************************************
350 Handle password changing for root.
351 *************************************************************/
353 static int process_root(void)
359 if (local_flags & LOCAL_SET_LDAP_ADMIN_PW)
361 printf("Setting stored password for \"%s\" in secrets.tdb\n",
363 if (!store_ldap_admin_pw(ldap_secret))
364 DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
370 * Ensure both add/delete user are not set
371 * Ensure add/delete user and either remote machine or join domain are
374 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
375 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
376 (remote_machine != NULL))) {
380 /* Only load interfaces if we are doing network operations. */
382 if (remote_machine) {
386 if (!user_name[0] && (pwd = sys_getpwuid(geteuid()))) {
387 fstrcpy(user_name, pwd->pw_name);
391 fprintf(stderr,"You must specify a username\n");
395 if (local_flags & LOCAL_TRUST_ACCOUNT) {
396 /* add the $ automatically */
400 * Remove any trailing '$' before we
401 * generate the initial machine password.
404 if (user_name[strlen(user_name)-1] == '$') {
405 user_name[strlen(user_name)-1] = 0;
408 if (local_flags & LOCAL_ADD_USER) {
409 SAFE_FREE(new_passwd);
410 new_passwd = smb_xstrdup(user_name);
411 strlower(new_passwd);
415 * Now ensure the username ends in '$' for
419 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
420 fstrcpy(user_name, buf);
421 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
424 if (local_flags & LOCAL_ADD_USER) {
426 * Prompt for trusting domain's account password
428 new_passwd = prompt_for_new_password(stdin_passwd_get);
430 fprintf(stderr, "Unable to get newpassword.\n");
434 slprintf(buf, sizeof(buf) - 1, "%s$", user_name);
435 fstrcpy(user_name, buf);
439 if (remote_machine != NULL) {
440 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
443 if (!(local_flags & LOCAL_SET_PASSWORD)) {
446 * If we are trying to enable a user, first we need to find out
447 * if they are using a modern version of the smbpasswd file that
448 * disables a user by just writing a flag into the file. If so
449 * then we can re-enable a user without prompting for a new
450 * password. If not (ie. they have a no stored password in the
451 * smbpasswd file) then we need to prompt for a new password.
454 if(local_flags & LOCAL_ENABLE_USER) {
455 SAM_ACCOUNT *sampass = NULL;
458 pdb_init_sam(&sampass);
459 ret = pdb_getsampwnam(sampass, user_name);
460 if((sampass != False) && (pdb_get_lanman_passwd(sampass) == NULL)) {
461 local_flags |= LOCAL_SET_PASSWORD;
463 pdb_free_sam(&sampass);
467 if(local_flags & LOCAL_SET_PASSWORD) {
468 new_passwd = prompt_for_new_password(stdin_passwd_get);
471 fprintf(stderr, "Unable to get new password.\n");
477 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
478 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
484 printf("Password changed for user %s on %s.\n", user_name, remote_machine );
485 } else if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
486 SAM_ACCOUNT *sampass = NULL;
489 pdb_init_sam(&sampass);
490 ret = pdb_getsampwnam(sampass, user_name);
492 printf("Password changed for user %s.", user_name );
493 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
494 printf(" User has disabled flag set.");
495 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
496 printf(" User has no password flag set.");
498 pdb_free_sam(&sampass);
502 SAFE_FREE(new_passwd);
507 /*************************************************************
508 Handle password changing for non-root.
509 *************************************************************/
511 static int process_nonroot(void)
513 struct passwd *pwd = NULL;
517 pwd = sys_getpwuid(getuid());
519 fstrcpy(user_name,pwd->pw_name);
521 fprintf(stderr, "smbpasswd: you don't exist - go away\n");
527 * A non-root user is always setting a password
528 * via a remote machine (even if that machine is
532 load_interfaces(); /* Delayed from main() */
534 if (remote_machine == NULL) {
535 remote_machine = "127.0.0.1";
538 if (remote_machine != NULL) {
539 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
543 new_passwd = prompt_for_new_password(stdin_passwd_get);
547 fprintf(stderr, "Unable to get new password.\n");
551 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
552 fprintf(stderr,"Failed to change password for %s\n", user_name);
557 printf("Password changed for user %s\n", user_name);
560 SAFE_FREE(old_passwd);
561 SAFE_FREE(new_passwd);
568 /*********************************************************
570 **********************************************************/
571 int main(int argc, char **argv)
573 BOOL amroot = getuid() == 0;
575 pstrcpy(configfile, dyn_CONFIGFILE);
576 AllowDebugChange = False;
578 #if defined(HAVE_SET_AUTH_PARAMETERS)
579 set_auth_parameters(argc, argv);
580 #endif /* HAVE_SET_AUTH_PARAMETERS */
582 process_options(argc, argv, amroot);
584 setup_logging("smbpasswd", True);
586 if (!lp_load(configfile,True,False,False)) {
587 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
593 * Set the machine NETBIOS name if not already
594 * set from the config file.
597 if (!*global_myname) {
599 fstrcpy(global_myname, myhostname());
600 p = strchr_m(global_myname, '.' );
603 strupper(global_myname);
605 /* Check the effective uid - make sure we are not setuid */
606 if (is_setuid_root()) {
607 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
611 if (local_mode || amroot) {
613 return process_root();
616 return process_nonroot();