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"
30 * Next two lines needed for SunOS and don't
31 * hurt anything else...
36 /* forced running in root-mode */
37 static bool got_username = False;
38 static bool stdin_passwd_get = False;
39 static fstring user_name;
40 static char *new_passwd = NULL;
41 static const char *remote_machine = NULL;
43 static fstring ldap_secret;
46 /*********************************************************
47 Print command usage on stderr and die.
48 **********************************************************/
49 static void usage(void)
51 printf("When run by root:\n");
52 printf(" smbpasswd [options] [username]\n");
53 printf("otherwise:\n");
54 printf(" smbpasswd [options]\n\n");
57 printf(" -L local mode (must be first option)\n");
58 printf(" -h print this usage message\n");
59 printf(" -s use stdin for password prompt\n");
60 printf(" -c smb.conf file Use the given path to the smb.conf file\n");
61 printf(" -D LEVEL debug level\n");
62 printf(" -r MACHINE remote machine\n");
63 printf(" -U USER remote username (e.g. SAM/user)\n");
65 printf("extra options when run by root or in local mode:\n");
66 printf(" -a add user\n");
67 printf(" -d disable user\n");
68 printf(" -e enable user\n");
69 printf(" -i interdomain trust account\n");
70 printf(" -m machine trust account\n");
71 printf(" -n set no password\n");
72 printf(" -W use stdin ldap admin password\n");
73 printf(" -w PASSWORD ldap admin password\n");
74 printf(" -x delete user\n");
75 printf(" -R ORDER name resolve order\n");
80 static void set_line_buffering(FILE *f)
82 setvbuf(f, NULL, _IOLBF, 0);
85 /*******************************************************************
86 Process command line options
87 ******************************************************************/
89 static int process_options(int argc, char **argv, int local_flags)
92 const char *configfile = get_dyn_CONFIGFILE();
94 local_flags |= LOCAL_SET_PASSWORD;
96 ZERO_STRUCT(user_name);
100 while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF) {
104 fprintf(stderr, "smbpasswd -L can only be used by root.\n");
107 local_flags |= LOCAL_AM_ROOT;
111 set_dyn_CONFIGFILE(optarg);
114 local_flags |= LOCAL_ADD_USER;
117 local_flags |= LOCAL_DELETE_USER;
118 local_flags &= ~LOCAL_SET_PASSWORD;
121 local_flags |= LOCAL_DISABLE_USER;
122 local_flags &= ~LOCAL_SET_PASSWORD;
125 local_flags |= LOCAL_ENABLE_USER;
126 local_flags &= ~LOCAL_SET_PASSWORD;
129 local_flags |= LOCAL_TRUST_ACCOUNT;
132 local_flags |= LOCAL_INTERDOM_ACCOUNT;
135 d_printf("See 'net join' for this functionality\n");
139 local_flags |= LOCAL_SET_NO_PASSWORD;
140 local_flags &= ~LOCAL_SET_PASSWORD;
141 SAFE_FREE(new_passwd);
142 new_passwd = smb_xstrdup("NO PASSWORD");
145 remote_machine = optarg;
148 set_line_buffering(stdin);
149 set_line_buffering(stdout);
150 set_line_buffering(stderr);
151 stdin_passwd_get = True;
154 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
155 fstrcpy(ldap_secret, optarg);
158 lp_set_cmdline("name resolve order", optarg);
161 lp_set_cmdline("log level", optarg);
165 fstrcpy(user_name, optarg);
168 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
184 fstrcpy(user_name, "");
187 if (!(local_flags & LOCAL_AM_ROOT)) {
193 fstrcpy(user_name, argv[0]);
201 if (!lp_load_global(configfile)) {
202 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
210 /*************************************************************
211 Utility function to prompt for new password.
212 *************************************************************/
213 static char *prompt_for_new_password(bool stdin_get)
220 p = get_pass("New SMB password:", stdin_get);
228 p = get_pass("Retype new SMB password:", stdin_get);
233 if (strcmp(p, new_pw)) {
234 fprintf(stderr, "Mismatch - password unchanged.\n");
244 /*************************************************************
245 Change a password either locally or remotely.
246 *************************************************************/
248 static NTSTATUS password_change(const char *remote_mach,
249 const char *domain, const char *username,
250 const char *old_passwd, const char *new_pw,
254 char *err_str = NULL;
255 char *msg_str = NULL;
257 if (remote_mach != NULL) {
258 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|
259 LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
260 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
261 /* these things can't be done remotely yet */
262 fprintf(stderr, "Invalid remote operation!\n");
263 return NT_STATUS_UNSUCCESSFUL;
265 ret = remote_password_change(remote_mach,
267 old_passwd, new_pw, &err_str);
269 ret = local_password_change(username, local_flags, new_pw,
274 printf("%s", msg_str);
277 fprintf(stderr, "%s", err_str);
279 if (!NT_STATUS_IS_OK(ret) && !err_str) {
280 fprintf(stderr, "Failed to change password!\n");
288 /*******************************************************************
289 Store the LDAP admin password in secrets.tdb
290 ******************************************************************/
291 static bool store_ldap_admin_pw (char* pw)
299 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw);
303 /*************************************************************
304 Handle password changing for root.
305 *************************************************************/
307 static int process_root(int local_flags)
311 char *old_passwd = NULL;
313 if (local_flags & LOCAL_SET_LDAP_ADMIN_PW) {
314 const char *ldap_admin_dn = lp_ldap_admin_dn();
315 if ( ! *ldap_admin_dn ) {
316 DEBUG(0,("ERROR: 'ldap admin dn' not defined! Please check your smb.conf\n"));
320 printf("Setting stored password for \"%s\" in secrets.tdb\n", ldap_admin_dn);
321 if ( ! *ldap_secret ) {
322 new_passwd = prompt_for_new_password(stdin_passwd_get);
323 if (new_passwd == NULL) {
324 fprintf(stderr, "Failed to read new password!\n");
327 fstrcpy(ldap_secret, new_passwd);
329 if (!store_ldap_admin_pw(ldap_secret)) {
330 DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
335 /* Ensure passdb startup(). */
336 if(!initialize_password_db(False, NULL)) {
337 DEBUG(0, ("Failed to open passdb!\n"));
341 /* Ensure we have a SAM sid. */
342 get_global_sam_sid();
345 * Ensure both add/delete user are not set
346 * Ensure add/delete user and either remote machine or join domain are
349 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
350 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
351 (remote_machine != NULL))) {
355 /* Only load interfaces if we are doing network operations. */
357 if (remote_machine) {
361 if (!user_name[0] && (pwd = getpwuid_alloc(talloc_tos(), geteuid()))) {
362 fstrcpy(user_name, pwd->pw_name);
367 fprintf(stderr,"You must specify a username\n");
371 if (local_flags & LOCAL_TRUST_ACCOUNT) {
372 /* add the $ automatically */
373 size_t user_name_len = strlen(user_name);
375 if (user_name[user_name_len - 1] == '$') {
378 if (user_name_len + 2 > sizeof(user_name)) {
379 fprintf(stderr, "machine name too long\n");
382 user_name[user_name_len] = '$';
383 user_name[user_name_len + 1] = '\0';
386 if (local_flags & LOCAL_ADD_USER) {
387 SAFE_FREE(new_passwd);
390 * Remove any trailing '$' before we
391 * generate the initial machine password.
393 new_passwd = smb_xstrndup(user_name, user_name_len);
394 if (!strlower_m(new_passwd)) {
395 fprintf(stderr, "strlower_m %s failed\n",
400 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
401 size_t user_name_len = strlen(user_name);
403 if (user_name[user_name_len - 1] != '$') {
404 if (user_name_len + 2 > sizeof(user_name)) {
405 fprintf(stderr, "machine name too long\n");
408 user_name[user_name_len] = '$';
409 user_name[user_name_len + 1] = '\0';
412 if ((local_flags & LOCAL_ADD_USER) && (new_passwd == NULL)) {
414 * Prompt for trusting domain's account password
416 new_passwd = prompt_for_new_password(stdin_passwd_get);
418 fprintf(stderr, "Unable to get newpassword.\n");
424 if (remote_machine != NULL) {
425 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
427 fprintf(stderr, "Unable to get old password.\n");
432 if (!(local_flags & LOCAL_SET_PASSWORD)) {
435 * If we are trying to enable a user, first we need to find out
436 * if they are using a modern version of the smbpasswd file that
437 * disables a user by just writing a flag into the file. If so
438 * then we can re-enable a user without prompting for a new
439 * password. If not (ie. they have a no stored password in the
440 * smbpasswd file) then we need to prompt for a new password.
443 if(local_flags & LOCAL_ENABLE_USER) {
444 struct samu *sampass = NULL;
446 sampass = samu_new( NULL );
448 fprintf(stderr, "talloc fail for struct samu.\n");
451 if (!pdb_getsampwnam(sampass, user_name)) {
452 fprintf(stderr, "Failed to find user %s in passdb backend.\n",
457 if(pdb_get_nt_passwd(sampass) == NULL) {
458 local_flags |= LOCAL_SET_PASSWORD;
460 TALLOC_FREE(sampass);
464 if((local_flags & LOCAL_SET_PASSWORD) && (new_passwd == NULL)) {
466 new_passwd = prompt_for_new_password(stdin_passwd_get);
468 fprintf(stderr, "Unable to get new password.\n");
474 if (!NT_STATUS_IS_OK(password_change(remote_machine,
476 old_passwd, new_passwd,
483 printf("Password changed for user %s on %s.\n", user_name, remote_machine );
484 } else if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
485 struct samu *sampass = NULL;
487 sampass = samu_new( NULL );
489 fprintf(stderr, "talloc fail for struct samu.\n");
493 if (!pdb_getsampwnam(sampass, user_name)) {
494 fprintf(stderr, "Failed to find user %s in passdb backend.\n",
499 printf("Password changed for user %s.", user_name );
500 if(pdb_get_acct_ctrl(sampass)&ACB_DISABLED) {
501 printf(" User has disabled flag set.");
503 if(pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) {
504 printf(" User has no password flag set.");
507 TALLOC_FREE(sampass);
511 SAFE_FREE(old_passwd);
512 SAFE_FREE(new_passwd);
517 /*************************************************************
518 Handle password changing for non-root.
519 *************************************************************/
521 static int process_nonroot(int local_flags)
523 struct passwd *pwd = NULL;
527 const char *username = user_name;
528 const char *domain = NULL;
531 if (local_flags & ~(LOCAL_AM_ROOT | LOCAL_SET_PASSWORD)) {
532 /* Extra flags that we can't honor non-root */
537 pwd = getpwuid_alloc(talloc_tos(), getuid());
539 fstrcpy(user_name,pwd->pw_name);
542 fprintf(stderr, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
547 /* Allow domain as part of the username */
548 if ((p = strchr_m(user_name, '\\')) ||
549 (p = strchr_m(user_name, '/')) ||
550 (p = strchr_m(user_name, *lp_winbind_separator()))) {
557 * A non-root user is always setting a password
558 * via a remote machine (even if that machine is
562 load_interfaces(); /* Delayed from main() */
564 if (remote_machine != NULL) {
565 if (!is_ipaddress(remote_machine)) {
566 domain = remote_machine;
569 remote_machine = "127.0.0.1";
572 * If we deal with a local user, change the password for the
575 domain = get_global_sam_name();
578 old_pw = get_pass("Old SMB password:",stdin_passwd_get);
579 if (old_pw == NULL) {
580 fprintf(stderr, "Unable to get old password.\n");
585 new_pw = prompt_for_new_password(stdin_passwd_get);
588 new_pw = smb_xstrdup(new_passwd);
591 fprintf(stderr, "Unable to get new password.\n");
595 if (!NT_STATUS_IS_OK(password_change(remote_machine,
597 old_pw, new_pw, 0))) {
602 printf("Password changed for user %s\n", username);
613 /*********************************************************
615 **********************************************************/
616 int main(int argc, char **argv)
618 TALLOC_CTX *frame = talloc_stackframe();
622 #if defined(HAVE_SET_AUTH_PARAMETERS)
623 set_auth_parameters(argc, argv);
624 #endif /* HAVE_SET_AUTH_PARAMETERS */
627 local_flags = LOCAL_AM_ROOT;
632 local_flags = process_options(argc, argv, local_flags);
634 setup_logging("smbpasswd", DEBUG_STDERR);
637 * Set the machine NETBIOS name if not already
638 * set from the config file.
644 /* Check the effective uid - make sure we are not setuid */
645 if (is_setuid_root()) {
646 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
650 if (local_flags & LOCAL_AM_ROOT) {
657 ret = process_root(local_flags);
659 ret = process_nonroot(local_flags);