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"
29 #include "lib/param/param.h"
30 #include "lib/util/memcache.h"
33 * Next two lines needed for SunOS and don't
34 * hurt anything else...
39 /* forced running in root-mode */
40 static bool got_username
= False
;
41 static bool stdin_passwd_get
= False
;
42 static fstring user_name
;
43 static char *new_passwd
= NULL
;
44 static const char *remote_machine
= NULL
;
46 static fstring ldap_secret
;
49 /*********************************************************
50 Print command usage on stderr and die.
51 **********************************************************/
52 static void usage(void)
54 printf("When run by root:\n");
55 printf(" smbpasswd [options] [username]\n");
56 printf("otherwise:\n");
57 printf(" smbpasswd [options]\n\n");
60 printf(" -L local mode (must be first option)\n");
61 printf(" -h print this usage message\n");
62 printf(" -s use stdin for password prompt\n");
63 printf(" -c smb.conf file Use the given path to the smb.conf file\n");
64 printf(" -D LEVEL debug level\n");
65 printf(" -r MACHINE remote machine\n");
66 printf(" -U USER remote username (e.g. SAM/user)\n");
68 printf("extra options when run by root or in local mode:\n");
69 printf(" -a add user\n");
70 printf(" -d disable user\n");
71 printf(" -e enable user\n");
72 printf(" -i interdomain trust account\n");
73 printf(" -m machine trust account\n");
74 printf(" -n set no password\n");
75 printf(" -W use stdin ldap admin password\n");
76 printf(" -w PASSWORD 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 ******************************************************************/
92 static int process_options(int argc
, char **argv
, int local_flags
,
93 struct loadparm_context
*lp_ctx
)
96 const char *configfile
= get_dyn_CONFIGFILE();
98 local_flags
|= LOCAL_SET_PASSWORD
;
100 ZERO_STRUCT(user_name
);
104 while ((ch
= getopt(argc
, argv
, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF
) {
108 fprintf(stderr
, "smbpasswd -L can only be used by root.\n");
111 local_flags
|= LOCAL_AM_ROOT
;
115 set_dyn_CONFIGFILE(optarg
);
118 local_flags
|= LOCAL_ADD_USER
;
121 local_flags
|= LOCAL_DELETE_USER
;
122 local_flags
&= ~LOCAL_SET_PASSWORD
;
125 local_flags
|= LOCAL_DISABLE_USER
;
126 local_flags
&= ~LOCAL_SET_PASSWORD
;
129 local_flags
|= LOCAL_ENABLE_USER
;
130 local_flags
&= ~LOCAL_SET_PASSWORD
;
133 local_flags
|= LOCAL_TRUST_ACCOUNT
;
136 local_flags
|= LOCAL_INTERDOM_ACCOUNT
;
139 d_printf("See 'net join' for this functionality\n");
143 local_flags
|= LOCAL_SET_NO_PASSWORD
;
144 local_flags
&= ~LOCAL_SET_PASSWORD
;
145 SAFE_FREE(new_passwd
);
146 new_passwd
= smb_xstrdup("NO PASSWORD");
149 remote_machine
= optarg
;
152 set_line_buffering(stdin
);
153 set_line_buffering(stdout
);
154 set_line_buffering(stderr
);
155 stdin_passwd_get
= True
;
158 local_flags
|= LOCAL_SET_LDAP_ADMIN_PW
;
159 fstrcpy(ldap_secret
, optarg
);
162 lpcfg_set_cmdline(lp_ctx
, "name resolve order", optarg
);
165 lpcfg_set_cmdline(lp_ctx
, "log level", optarg
);
169 fstrcpy(user_name
, optarg
);
172 local_flags
|= LOCAL_SET_LDAP_ADMIN_PW
;
188 fstrcpy(user_name
, "");
191 if (!(local_flags
& LOCAL_AM_ROOT
)) {
197 fstrcpy(user_name
, argv
[0]);
205 if (!lp_load_global(configfile
)) {
206 fprintf(stderr
, "Can't load %s - run testparm to debug it\n",
214 /*************************************************************
215 Utility function to prompt for new password.
216 *************************************************************/
217 static char *prompt_for_new_password(bool stdin_get
)
224 p
= get_pass("New SMB password:", stdin_get
);
232 p
= get_pass("Retype new SMB password:", stdin_get
);
237 if (strcmp(p
, new_pw
)) {
238 fprintf(stderr
, "Mismatch - password unchanged.\n");
248 /*************************************************************
249 Change a password either locally or remotely.
250 *************************************************************/
252 static NTSTATUS
password_change(const char *remote_mach
,
253 const char *domain
, const char *username
,
254 const char *old_passwd
, const char *new_pw
,
258 char *err_str
= NULL
;
259 char *msg_str
= NULL
;
261 if (remote_mach
!= NULL
) {
262 if (local_flags
& (LOCAL_ADD_USER
|LOCAL_DELETE_USER
|
263 LOCAL_DISABLE_USER
|LOCAL_ENABLE_USER
|
264 LOCAL_TRUST_ACCOUNT
|LOCAL_SET_NO_PASSWORD
)) {
265 /* these things can't be done remotely yet */
266 fprintf(stderr
, "Invalid remote operation!\n");
267 return NT_STATUS_UNSUCCESSFUL
;
269 ret
= remote_password_change(remote_mach
,
271 old_passwd
, new_pw
, &err_str
);
273 ret
= local_password_change(username
, local_flags
, new_pw
,
278 printf("%s", msg_str
);
281 fprintf(stderr
, "%s", err_str
);
283 if (!NT_STATUS_IS_OK(ret
) && !err_str
) {
284 fprintf(stderr
, "Failed to change password!\n");
292 /*******************************************************************
293 Store the LDAP admin password in secrets.tdb
294 ******************************************************************/
295 static bool store_ldap_admin_pw (char* pw
)
303 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw
);
307 /*************************************************************
308 Handle password changing for root.
309 *************************************************************/
311 static int process_root(int local_flags
)
315 char *old_passwd
= NULL
;
317 if (local_flags
& LOCAL_SET_LDAP_ADMIN_PW
) {
318 const char *ldap_admin_dn
= lp_ldap_admin_dn();
319 if ( ! *ldap_admin_dn
) {
320 DEBUG(0,("ERROR: 'ldap admin dn' not defined! Please check your smb.conf\n"));
324 printf("Setting stored password for \"%s\" in secrets.tdb\n", ldap_admin_dn
);
325 if ( ! *ldap_secret
) {
326 new_passwd
= prompt_for_new_password(stdin_passwd_get
);
327 if (new_passwd
== NULL
) {
328 fprintf(stderr
, "Failed to read new password!\n");
331 fstrcpy(ldap_secret
, new_passwd
);
333 if (!store_ldap_admin_pw(ldap_secret
)) {
334 DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
339 /* Ensure passdb startup(). */
340 if(!initialize_password_db(False
, NULL
)) {
341 DEBUG(0, ("Failed to open passdb!\n"));
345 /* Ensure we have a SAM sid. */
346 get_global_sam_sid();
349 * Ensure both add/delete user are not set
350 * Ensure add/delete user and either remote machine or join domain are
353 if(((local_flags
& (LOCAL_ADD_USER
|LOCAL_DELETE_USER
)) == (LOCAL_ADD_USER
|LOCAL_DELETE_USER
)) ||
354 ((local_flags
& (LOCAL_ADD_USER
|LOCAL_DELETE_USER
)) &&
355 (remote_machine
!= NULL
))) {
359 /* Only load interfaces if we are doing network operations. */
361 if (remote_machine
) {
365 if (!user_name
[0] && (pwd
= getpwuid_alloc(talloc_tos(), geteuid()))) {
366 fstrcpy(user_name
, pwd
->pw_name
);
371 fprintf(stderr
,"You must specify a username\n");
375 if (local_flags
& LOCAL_TRUST_ACCOUNT
) {
376 /* add the $ automatically */
377 size_t user_name_len
= strlen(user_name
);
379 if (user_name
[user_name_len
- 1] == '$') {
382 if (user_name_len
+ 2 > sizeof(user_name
)) {
383 fprintf(stderr
, "machine name too long\n");
386 user_name
[user_name_len
] = '$';
387 user_name
[user_name_len
+ 1] = '\0';
390 if (local_flags
& LOCAL_ADD_USER
) {
391 SAFE_FREE(new_passwd
);
394 * Remove any trailing '$' before we
395 * generate the initial machine password.
397 new_passwd
= smb_xstrndup(user_name
, user_name_len
);
398 if (!strlower_m(new_passwd
)) {
399 fprintf(stderr
, "strlower_m %s failed\n",
404 } else if (local_flags
& LOCAL_INTERDOM_ACCOUNT
) {
405 size_t user_name_len
= strlen(user_name
);
407 if (user_name
[user_name_len
- 1] != '$') {
408 if (user_name_len
+ 2 > sizeof(user_name
)) {
409 fprintf(stderr
, "machine name too long\n");
412 user_name
[user_name_len
] = '$';
413 user_name
[user_name_len
+ 1] = '\0';
416 if ((local_flags
& LOCAL_ADD_USER
) && (new_passwd
== NULL
)) {
418 * Prompt for trusting domain's account password
420 new_passwd
= prompt_for_new_password(stdin_passwd_get
);
422 fprintf(stderr
, "Unable to get newpassword.\n");
428 if (remote_machine
!= NULL
) {
429 old_passwd
= get_pass("Old SMB password:",stdin_passwd_get
);
431 fprintf(stderr
, "Unable to get old password.\n");
436 if (!(local_flags
& LOCAL_SET_PASSWORD
)) {
439 * If we are trying to enable a user, first we need to find out
440 * if they are using a modern version of the smbpasswd file that
441 * disables a user by just writing a flag into the file. If so
442 * then we can re-enable a user without prompting for a new
443 * password. If not (ie. they have a no stored password in the
444 * smbpasswd file) then we need to prompt for a new password.
447 if(local_flags
& LOCAL_ENABLE_USER
) {
448 struct samu
*sampass
= NULL
;
450 sampass
= samu_new( NULL
);
452 fprintf(stderr
, "talloc fail for struct samu.\n");
455 if (!pdb_getsampwnam(sampass
, user_name
)) {
456 fprintf(stderr
, "Failed to find user %s in passdb backend.\n",
461 if(pdb_get_nt_passwd(sampass
) == NULL
) {
462 local_flags
|= LOCAL_SET_PASSWORD
;
464 TALLOC_FREE(sampass
);
468 if((local_flags
& LOCAL_SET_PASSWORD
) && (new_passwd
== NULL
)) {
470 new_passwd
= prompt_for_new_password(stdin_passwd_get
);
472 fprintf(stderr
, "Unable to get new password.\n");
478 if (!NT_STATUS_IS_OK(password_change(remote_machine
,
480 old_passwd
, new_passwd
,
487 printf("Password changed for user %s on %s.\n", user_name
, remote_machine
);
488 } else if(!(local_flags
& (LOCAL_ADD_USER
|LOCAL_DISABLE_USER
|LOCAL_ENABLE_USER
|LOCAL_DELETE_USER
|LOCAL_SET_NO_PASSWORD
|LOCAL_SET_PASSWORD
))) {
489 struct samu
*sampass
= NULL
;
491 sampass
= samu_new( NULL
);
493 fprintf(stderr
, "talloc fail for struct samu.\n");
497 if (!pdb_getsampwnam(sampass
, user_name
)) {
498 fprintf(stderr
, "Failed to find user %s in passdb backend.\n",
503 printf("Password changed for user %s.", user_name
);
504 if(pdb_get_acct_ctrl(sampass
)&ACB_DISABLED
) {
505 printf(" User has disabled flag set.");
507 if(pdb_get_acct_ctrl(sampass
) & ACB_PWNOTREQ
) {
508 printf(" User has no password flag set.");
511 TALLOC_FREE(sampass
);
515 SAFE_FREE(old_passwd
);
516 SAFE_FREE(new_passwd
);
521 /*************************************************************
522 Handle password changing for non-root.
523 *************************************************************/
525 static int process_nonroot(int local_flags
)
527 struct passwd
*pwd
= NULL
;
531 const char *username
= user_name
;
532 const char *domain
= NULL
;
535 if (local_flags
& ~(LOCAL_AM_ROOT
| LOCAL_SET_PASSWORD
)) {
536 /* Extra flags that we can't honor non-root */
541 pwd
= getpwuid_alloc(talloc_tos(), getuid());
543 fstrcpy(user_name
,pwd
->pw_name
);
546 fprintf(stderr
, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
551 /* Allow domain as part of the username */
552 if ((p
= strchr_m(user_name
, '\\')) ||
553 (p
= strchr_m(user_name
, '/')) ||
554 (p
= strchr_m(user_name
, *lp_winbind_separator()))) {
561 * A non-root user is always setting a password
562 * via a remote machine (even if that machine is
566 load_interfaces(); /* Delayed from main() */
568 if (remote_machine
!= NULL
) {
569 if (!is_ipaddress(remote_machine
)) {
570 domain
= remote_machine
;
573 remote_machine
= "127.0.0.1";
576 * If we deal with a local user, change the password for the
579 domain
= get_global_sam_name();
582 old_pw
= get_pass("Old SMB password:",stdin_passwd_get
);
583 if (old_pw
== NULL
) {
584 fprintf(stderr
, "Unable to get old password.\n");
589 new_pw
= prompt_for_new_password(stdin_passwd_get
);
592 new_pw
= smb_xstrdup(new_passwd
);
595 fprintf(stderr
, "Unable to get new password.\n");
599 if (!NT_STATUS_IS_OK(password_change(remote_machine
,
601 old_pw
, new_pw
, 0))) {
606 printf("Password changed for user %s\n", username
);
617 /*********************************************************
619 **********************************************************/
620 int main(int argc
, char **argv
)
622 TALLOC_CTX
*frame
= talloc_stackframe();
623 struct loadparm_context
*lp_ctx
= NULL
;
624 struct memcache
*mcache
= NULL
;
628 mcache
= memcache_init(NULL
, 0);
629 if (mcache
== NULL
) {
630 fprintf(stderr
, "%s: memcache_init failed\n", __location__
);
633 memcache_set_global(mcache
);
635 #if defined(HAVE_SET_AUTH_PARAMETERS)
636 set_auth_parameters(argc
, argv
);
637 #endif /* HAVE_SET_AUTH_PARAMETERS */
640 local_flags
= LOCAL_AM_ROOT
;
645 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
646 if (lp_ctx
== NULL
) {
648 "Failed to initialise the global parameter structure.\n");
652 local_flags
= process_options(argc
, argv
, local_flags
, lp_ctx
);
654 setup_logging("smbpasswd", DEBUG_STDERR
);
656 /* Check the effective uid - make sure we are not setuid */
657 if (is_setuid_root()) {
658 fprintf(stderr
, "smbpasswd must *NOT* be setuid root.\n");
662 if (local_flags
& LOCAL_AM_ROOT
) {
669 ret
= process_root(local_flags
);
671 ret
= process_nonroot(local_flags
);