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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "system/passwd.h"
22 #include "../librpc/gen_ndr/samr.h"
23 #include "../lib/util/util_pw.h"
24 #include "libsmb/proto.h"
26 #include "cmdline_contexts.h"
27 #include "passwd_proto.h"
28 #include "lib/util/string_wrappers.h"
31 * Next two lines needed for SunOS and don't
32 * hurt anything else...
37 /* forced running in root-mode */
38 static bool got_username
= False
;
39 static bool stdin_passwd_get
= False
;
40 static fstring user_name
;
41 static char *new_passwd
= NULL
;
42 static const char *remote_machine
= NULL
;
44 static fstring ldap_secret
;
47 /*********************************************************
48 Print command usage on stderr and die.
49 **********************************************************/
50 static void usage(void)
52 printf("When run by root:\n");
53 printf(" smbpasswd [options] [username]\n");
54 printf("otherwise:\n");
55 printf(" smbpasswd [options]\n\n");
58 printf(" -L local mode (must be first option)\n");
59 printf(" -h print this usage message\n");
60 printf(" -s use stdin for password prompt\n");
61 printf(" -c smb.conf file Use the given path to the smb.conf file\n");
62 printf(" -D LEVEL debug level\n");
63 printf(" -r MACHINE remote machine\n");
64 printf(" -U USER remote username (e.g. SAM/user)\n");
66 printf("extra options when run by root or in local mode:\n");
67 printf(" -a add user\n");
68 printf(" -d disable user\n");
69 printf(" -e enable user\n");
70 printf(" -i interdomain trust account\n");
71 printf(" -m machine trust account\n");
72 printf(" -n set no password\n");
73 printf(" -W use stdin ldap admin password\n");
74 printf(" -w PASSWORD ldap admin password\n");
75 printf(" -x delete user\n");
76 printf(" -R ORDER name resolve order\n");
81 static void set_line_buffering(FILE *f
)
83 setvbuf(f
, NULL
, _IOLBF
, 0);
86 /*******************************************************************
87 Process command line options
88 ******************************************************************/
90 static int process_options(int argc
, char **argv
, int local_flags
)
93 const char *configfile
= get_dyn_CONFIGFILE();
95 local_flags
|= LOCAL_SET_PASSWORD
;
97 ZERO_STRUCT(user_name
);
101 while ((ch
= getopt(argc
, argv
, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF
) {
105 fprintf(stderr
, "smbpasswd -L can only be used by root.\n");
108 local_flags
|= LOCAL_AM_ROOT
;
112 set_dyn_CONFIGFILE(optarg
);
115 local_flags
|= LOCAL_ADD_USER
;
118 local_flags
|= LOCAL_DELETE_USER
;
119 local_flags
&= ~LOCAL_SET_PASSWORD
;
122 local_flags
|= LOCAL_DISABLE_USER
;
123 local_flags
&= ~LOCAL_SET_PASSWORD
;
126 local_flags
|= LOCAL_ENABLE_USER
;
127 local_flags
&= ~LOCAL_SET_PASSWORD
;
130 local_flags
|= LOCAL_TRUST_ACCOUNT
;
133 local_flags
|= LOCAL_INTERDOM_ACCOUNT
;
136 d_printf("See 'net join' for this functionality\n");
140 local_flags
|= LOCAL_SET_NO_PASSWORD
;
141 local_flags
&= ~LOCAL_SET_PASSWORD
;
142 SAFE_FREE(new_passwd
);
143 new_passwd
= smb_xstrdup("NO PASSWORD");
146 remote_machine
= optarg
;
149 set_line_buffering(stdin
);
150 set_line_buffering(stdout
);
151 set_line_buffering(stderr
);
152 stdin_passwd_get
= True
;
155 local_flags
|= LOCAL_SET_LDAP_ADMIN_PW
;
156 fstrcpy(ldap_secret
, optarg
);
159 lp_set_cmdline("name resolve order", optarg
);
162 lp_set_cmdline("log level", optarg
);
166 fstrcpy(user_name
, optarg
);
169 local_flags
|= LOCAL_SET_LDAP_ADMIN_PW
;
185 fstrcpy(user_name
, "");
188 if (!(local_flags
& LOCAL_AM_ROOT
)) {
194 fstrcpy(user_name
, argv
[0]);
202 if (!lp_load_global(configfile
)) {
203 fprintf(stderr
, "Can't load %s - run testparm to debug it\n",
211 /*************************************************************
212 Utility function to prompt for new password.
213 *************************************************************/
214 static char *prompt_for_new_password(bool stdin_get
)
221 p
= get_pass("New SMB password:", stdin_get
);
229 p
= get_pass("Retype new SMB password:", stdin_get
);
234 if (strcmp(p
, new_pw
)) {
235 fprintf(stderr
, "Mismatch - password unchanged.\n");
245 /*************************************************************
246 Change a password either locally or remotely.
247 *************************************************************/
249 static NTSTATUS
password_change(const char *remote_mach
,
250 const char *domain
, const char *username
,
251 const char *old_passwd
, const char *new_pw
,
255 char *err_str
= NULL
;
256 char *msg_str
= NULL
;
258 if (remote_mach
!= NULL
) {
259 if (local_flags
& (LOCAL_ADD_USER
|LOCAL_DELETE_USER
|
260 LOCAL_DISABLE_USER
|LOCAL_ENABLE_USER
|
261 LOCAL_TRUST_ACCOUNT
|LOCAL_SET_NO_PASSWORD
)) {
262 /* these things can't be done remotely yet */
263 fprintf(stderr
, "Invalid remote operation!\n");
264 return NT_STATUS_UNSUCCESSFUL
;
266 ret
= remote_password_change(remote_mach
,
268 old_passwd
, new_pw
, &err_str
);
270 ret
= local_password_change(username
, local_flags
, new_pw
,
275 printf("%s", msg_str
);
278 fprintf(stderr
, "%s", err_str
);
280 if (!NT_STATUS_IS_OK(ret
) && !err_str
) {
281 fprintf(stderr
, "Failed to change password!\n");
289 /*******************************************************************
290 Store the LDAP admin password in secrets.tdb
291 ******************************************************************/
292 static bool store_ldap_admin_pw (char* pw
)
300 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw
);
304 /*************************************************************
305 Handle password changing for root.
306 *************************************************************/
308 static int process_root(int local_flags
)
312 char *old_passwd
= NULL
;
314 if (local_flags
& LOCAL_SET_LDAP_ADMIN_PW
) {
315 const char *ldap_admin_dn
= lp_ldap_admin_dn();
316 if ( ! *ldap_admin_dn
) {
317 DEBUG(0,("ERROR: 'ldap admin dn' not defined! Please check your smb.conf\n"));
321 printf("Setting stored password for \"%s\" in secrets.tdb\n", ldap_admin_dn
);
322 if ( ! *ldap_secret
) {
323 new_passwd
= prompt_for_new_password(stdin_passwd_get
);
324 if (new_passwd
== NULL
) {
325 fprintf(stderr
, "Failed to read new password!\n");
328 fstrcpy(ldap_secret
, new_passwd
);
330 if (!store_ldap_admin_pw(ldap_secret
)) {
331 DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
336 /* Ensure passdb startup(). */
337 if(!initialize_password_db(False
, NULL
)) {
338 DEBUG(0, ("Failed to open passdb!\n"));
342 /* Ensure we have a SAM sid. */
343 get_global_sam_sid();
346 * Ensure both add/delete user are not set
347 * Ensure add/delete user and either remote machine or join domain are
350 if(((local_flags
& (LOCAL_ADD_USER
|LOCAL_DELETE_USER
)) == (LOCAL_ADD_USER
|LOCAL_DELETE_USER
)) ||
351 ((local_flags
& (LOCAL_ADD_USER
|LOCAL_DELETE_USER
)) &&
352 (remote_machine
!= NULL
))) {
356 /* Only load interfaces if we are doing network operations. */
358 if (remote_machine
) {
362 if (!user_name
[0] && (pwd
= getpwuid_alloc(talloc_tos(), geteuid()))) {
363 fstrcpy(user_name
, pwd
->pw_name
);
368 fprintf(stderr
,"You must specify a username\n");
372 if (local_flags
& LOCAL_TRUST_ACCOUNT
) {
373 /* add the $ automatically */
374 size_t user_name_len
= strlen(user_name
);
376 if (user_name
[user_name_len
- 1] == '$') {
379 if (user_name_len
+ 2 > sizeof(user_name
)) {
380 fprintf(stderr
, "machine name too long\n");
383 user_name
[user_name_len
] = '$';
384 user_name
[user_name_len
+ 1] = '\0';
387 if (local_flags
& LOCAL_ADD_USER
) {
388 SAFE_FREE(new_passwd
);
391 * Remove any trailing '$' before we
392 * generate the initial machine password.
394 new_passwd
= smb_xstrndup(user_name
, user_name_len
);
395 if (!strlower_m(new_passwd
)) {
396 fprintf(stderr
, "strlower_m %s failed\n",
401 } else if (local_flags
& LOCAL_INTERDOM_ACCOUNT
) {
402 size_t user_name_len
= strlen(user_name
);
404 if (user_name
[user_name_len
- 1] != '$') {
405 if (user_name_len
+ 2 > sizeof(user_name
)) {
406 fprintf(stderr
, "machine name too long\n");
409 user_name
[user_name_len
] = '$';
410 user_name
[user_name_len
+ 1] = '\0';
413 if ((local_flags
& LOCAL_ADD_USER
) && (new_passwd
== NULL
)) {
415 * Prompt for trusting domain's account password
417 new_passwd
= prompt_for_new_password(stdin_passwd_get
);
419 fprintf(stderr
, "Unable to get newpassword.\n");
425 if (remote_machine
!= NULL
) {
426 old_passwd
= get_pass("Old SMB password:",stdin_passwd_get
);
428 fprintf(stderr
, "Unable to get old password.\n");
433 if (!(local_flags
& LOCAL_SET_PASSWORD
)) {
436 * If we are trying to enable a user, first we need to find out
437 * if they are using a modern version of the smbpasswd file that
438 * disables a user by just writing a flag into the file. If so
439 * then we can re-enable a user without prompting for a new
440 * password. If not (ie. they have a no stored password in the
441 * smbpasswd file) then we need to prompt for a new password.
444 if(local_flags
& LOCAL_ENABLE_USER
) {
445 struct samu
*sampass
= NULL
;
447 sampass
= samu_new( NULL
);
449 fprintf(stderr
, "talloc fail for struct samu.\n");
452 if (!pdb_getsampwnam(sampass
, user_name
)) {
453 fprintf(stderr
, "Failed to find user %s in passdb backend.\n",
458 if(pdb_get_nt_passwd(sampass
) == NULL
) {
459 local_flags
|= LOCAL_SET_PASSWORD
;
461 TALLOC_FREE(sampass
);
465 if((local_flags
& LOCAL_SET_PASSWORD
) && (new_passwd
== NULL
)) {
467 new_passwd
= prompt_for_new_password(stdin_passwd_get
);
469 fprintf(stderr
, "Unable to get new password.\n");
475 if (!NT_STATUS_IS_OK(password_change(remote_machine
,
477 old_passwd
, new_passwd
,
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 struct samu
*sampass
= NULL
;
488 sampass
= samu_new( NULL
);
490 fprintf(stderr
, "talloc fail for struct samu.\n");
494 if (!pdb_getsampwnam(sampass
, user_name
)) {
495 fprintf(stderr
, "Failed to find user %s in passdb backend.\n",
500 printf("Password changed for user %s.", user_name
);
501 if(pdb_get_acct_ctrl(sampass
)&ACB_DISABLED
) {
502 printf(" User has disabled flag set.");
504 if(pdb_get_acct_ctrl(sampass
) & ACB_PWNOTREQ
) {
505 printf(" User has no password flag set.");
508 TALLOC_FREE(sampass
);
512 SAFE_FREE(old_passwd
);
513 SAFE_FREE(new_passwd
);
518 /*************************************************************
519 Handle password changing for non-root.
520 *************************************************************/
522 static int process_nonroot(int local_flags
)
524 struct passwd
*pwd
= NULL
;
528 const char *username
= user_name
;
529 const char *domain
= NULL
;
532 if (local_flags
& ~(LOCAL_AM_ROOT
| LOCAL_SET_PASSWORD
)) {
533 /* Extra flags that we can't honor non-root */
538 pwd
= getpwuid_alloc(talloc_tos(), getuid());
540 fstrcpy(user_name
,pwd
->pw_name
);
543 fprintf(stderr
, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
548 /* Allow domain as part of the username */
549 if ((p
= strchr_m(user_name
, '\\')) ||
550 (p
= strchr_m(user_name
, '/')) ||
551 (p
= strchr_m(user_name
, *lp_winbind_separator()))) {
558 * A non-root user is always setting a password
559 * via a remote machine (even if that machine is
563 load_interfaces(); /* Delayed from main() */
565 if (remote_machine
!= NULL
) {
566 if (!is_ipaddress(remote_machine
)) {
567 domain
= remote_machine
;
570 remote_machine
= "127.0.0.1";
573 * If we deal with a local user, change the password for the
576 domain
= get_global_sam_name();
579 old_pw
= get_pass("Old SMB password:",stdin_passwd_get
);
580 if (old_pw
== NULL
) {
581 fprintf(stderr
, "Unable to get old password.\n");
586 new_pw
= prompt_for_new_password(stdin_passwd_get
);
589 new_pw
= smb_xstrdup(new_passwd
);
592 fprintf(stderr
, "Unable to get new password.\n");
596 if (!NT_STATUS_IS_OK(password_change(remote_machine
,
598 old_pw
, new_pw
, 0))) {
603 printf("Password changed for user %s\n", username
);
614 /*********************************************************
616 **********************************************************/
617 int main(int argc
, char **argv
)
619 TALLOC_CTX
*frame
= talloc_stackframe();
623 #if defined(HAVE_SET_AUTH_PARAMETERS)
624 set_auth_parameters(argc
, argv
);
625 #endif /* HAVE_SET_AUTH_PARAMETERS */
628 local_flags
= LOCAL_AM_ROOT
;
633 local_flags
= process_options(argc
, argv
, local_flags
);
635 setup_logging("smbpasswd", DEBUG_STDERR
);
637 /* Check the effective uid - make sure we are not setuid */
638 if (is_setuid_root()) {
639 fprintf(stderr
, "smbpasswd must *NOT* be setuid root.\n");
643 if (local_flags
& LOCAL_AM_ROOT
) {
650 ret
= process_root(local_flags
);
652 ret
= process_nonroot(local_flags
);