CVE-2020-25719 kdc: Avoid races and multiple DB lookups in s4u2self check
[Samba.git] / source3 / utils / smbpasswd.c
blob4196e3f98f9a3345ac3e62b1fc4c726987517b97
1 /*
2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jeremy Allison 1995-1998
4 * Copyright (C) Tim Potter 2001
5 *
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
14 * more details.
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/>. */
19 #include "includes.h"
20 #include "system/passwd.h"
21 #include "secrets.h"
22 #include "../librpc/gen_ndr/samr.h"
23 #include "../lib/util/util_pw.h"
24 #include "libsmb/proto.h"
25 #include "passdb.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...
34 extern char *optarg;
35 extern int optind;
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");
57 printf("options:\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");
78 exit(1);
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)
92 int ch;
93 const char *configfile = get_dyn_CONFIGFILE();
95 local_flags |= LOCAL_SET_PASSWORD;
97 ZERO_STRUCT(user_name);
99 user_name[0] = '\0';
101 while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF) {
102 switch(ch) {
103 case 'L':
104 if (getuid() != 0) {
105 fprintf(stderr, "smbpasswd -L can only be used by root.\n");
106 exit(1);
108 local_flags |= LOCAL_AM_ROOT;
109 break;
110 case 'c':
111 configfile = optarg;
112 set_dyn_CONFIGFILE(optarg);
113 break;
114 case 'a':
115 local_flags |= LOCAL_ADD_USER;
116 break;
117 case 'x':
118 local_flags |= LOCAL_DELETE_USER;
119 local_flags &= ~LOCAL_SET_PASSWORD;
120 break;
121 case 'd':
122 local_flags |= LOCAL_DISABLE_USER;
123 local_flags &= ~LOCAL_SET_PASSWORD;
124 break;
125 case 'e':
126 local_flags |= LOCAL_ENABLE_USER;
127 local_flags &= ~LOCAL_SET_PASSWORD;
128 break;
129 case 'm':
130 local_flags |= LOCAL_TRUST_ACCOUNT;
131 break;
132 case 'i':
133 local_flags |= LOCAL_INTERDOM_ACCOUNT;
134 break;
135 case 'j':
136 d_printf("See 'net join' for this functionality\n");
137 exit(1);
138 break;
139 case '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");
144 break;
145 case 'r':
146 remote_machine = optarg;
147 break;
148 case 's':
149 set_line_buffering(stdin);
150 set_line_buffering(stdout);
151 set_line_buffering(stderr);
152 stdin_passwd_get = True;
153 break;
154 case 'w':
155 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
156 fstrcpy(ldap_secret, optarg);
157 break;
158 case 'R':
159 lp_set_cmdline("name resolve order", optarg);
160 break;
161 case 'D':
162 lp_set_cmdline("log level", optarg);
163 break;
164 case 'U': {
165 got_username = True;
166 fstrcpy(user_name, optarg);
167 break;
168 case 'W':
169 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
170 *ldap_secret = '\0';
171 break;
173 case 'h':
174 default:
175 usage();
179 argc -= optind;
180 argv += optind;
182 switch(argc) {
183 case 0:
184 if (!got_username)
185 fstrcpy(user_name, "");
186 break;
187 case 1:
188 if (!(local_flags & LOCAL_AM_ROOT)) {
189 usage();
190 } else {
191 if (got_username) {
192 usage();
193 } else {
194 fstrcpy(user_name, argv[0]);
197 break;
198 default:
199 usage();
202 if (!lp_load_global(configfile)) {
203 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
204 configfile);
205 exit(1);
208 return local_flags;
211 /*************************************************************
212 Utility function to prompt for new password.
213 *************************************************************/
214 static char *prompt_for_new_password(bool stdin_get)
216 char *p;
217 fstring new_pw;
219 ZERO_ARRAY(new_pw);
221 p = get_pass("New SMB password:", stdin_get);
222 if (p == NULL) {
223 return NULL;
226 fstrcpy(new_pw, p);
227 SAFE_FREE(p);
229 p = get_pass("Retype new SMB password:", stdin_get);
230 if (p == NULL) {
231 return NULL;
234 if (strcmp(p, new_pw)) {
235 fprintf(stderr, "Mismatch - password unchanged.\n");
236 ZERO_ARRAY(new_pw);
237 SAFE_FREE(p);
238 return NULL;
241 return p;
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,
252 int local_flags)
254 NTSTATUS ret;
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,
267 domain, username,
268 old_passwd, new_pw, &err_str);
269 } else {
270 ret = local_password_change(username, local_flags, new_pw,
271 &err_str, &msg_str);
274 if (msg_str) {
275 printf("%s", msg_str);
277 if (err_str) {
278 fprintf(stderr, "%s", err_str);
280 if (!NT_STATUS_IS_OK(ret) && !err_str) {
281 fprintf(stderr, "Failed to change password!\n");
284 SAFE_FREE(msg_str);
285 SAFE_FREE(err_str);
286 return ret;
289 /*******************************************************************
290 Store the LDAP admin password in secrets.tdb
291 ******************************************************************/
292 static bool store_ldap_admin_pw (char* pw)
294 if (!pw)
295 return False;
297 if (!secrets_init())
298 return False;
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)
310 struct passwd *pwd;
311 int result = 0;
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"));
318 goto done;
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");
326 exit(1);
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"));
333 goto done;
336 /* Ensure passdb startup(). */
337 if(!initialize_password_db(False, NULL)) {
338 DEBUG(0, ("Failed to open passdb!\n"));
339 exit(1);
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
348 * not both set.
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))) {
353 usage();
356 /* Only load interfaces if we are doing network operations. */
358 if (remote_machine) {
359 load_interfaces();
362 if (!user_name[0] && (pwd = getpwuid_alloc(talloc_tos(), geteuid()))) {
363 fstrcpy(user_name, pwd->pw_name);
364 TALLOC_FREE(pwd);
367 if (!user_name[0]) {
368 fprintf(stderr,"You must specify a username\n");
369 exit(1);
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] == '$') {
377 user_name_len--;
378 } else {
379 if (user_name_len + 2 > sizeof(user_name)) {
380 fprintf(stderr, "machine name too long\n");
381 exit(1);
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",
397 new_passwd);
398 exit(1);
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");
407 exit(1);
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);
418 if(!new_passwd) {
419 fprintf(stderr, "Unable to get newpassword.\n");
420 exit(1);
423 } else {
425 if (remote_machine != NULL) {
426 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
427 if(!old_passwd) {
428 fprintf(stderr, "Unable to get old password.\n");
429 exit(1);
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 );
448 if (!sampass) {
449 fprintf(stderr, "talloc fail for struct samu.\n");
450 exit(1);
452 if (!pdb_getsampwnam(sampass, user_name)) {
453 fprintf(stderr, "Failed to find user %s in passdb backend.\n",
454 user_name );
455 exit(1);
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);
468 if(!new_passwd) {
469 fprintf(stderr, "Unable to get new password.\n");
470 exit(1);
475 if (!NT_STATUS_IS_OK(password_change(remote_machine,
476 NULL, user_name,
477 old_passwd, new_passwd,
478 local_flags))) {
479 result = 1;
480 goto done;
483 if(remote_machine) {
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 );
489 if (!sampass) {
490 fprintf(stderr, "talloc fail for struct samu.\n");
491 exit(1);
494 if (!pdb_getsampwnam(sampass, user_name)) {
495 fprintf(stderr, "Failed to find user %s in passdb backend.\n",
496 user_name );
497 exit(1);
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.");
507 printf("\n");
508 TALLOC_FREE(sampass);
511 done:
512 SAFE_FREE(old_passwd);
513 SAFE_FREE(new_passwd);
514 return result;
518 /*************************************************************
519 Handle password changing for non-root.
520 *************************************************************/
522 static int process_nonroot(int local_flags)
524 struct passwd *pwd = NULL;
525 int result = 0;
526 char *old_pw = NULL;
527 char *new_pw = NULL;
528 const char *username = user_name;
529 const char *domain = NULL;
530 char *p = NULL;
532 if (local_flags & ~(LOCAL_AM_ROOT | LOCAL_SET_PASSWORD)) {
533 /* Extra flags that we can't honor non-root */
534 usage();
537 if (!user_name[0]) {
538 pwd = getpwuid_alloc(talloc_tos(), getuid());
539 if (pwd) {
540 fstrcpy(user_name,pwd->pw_name);
541 TALLOC_FREE(pwd);
542 } else {
543 fprintf(stderr, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
544 exit(1);
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()))) {
552 *p = '\0';
553 username = p + 1;
554 domain = user_name;
558 * A non-root user is always setting a password
559 * via a remote machine (even if that machine is
560 * localhost).
563 load_interfaces(); /* Delayed from main() */
565 if (remote_machine != NULL) {
566 if (!is_ipaddress(remote_machine)) {
567 domain = remote_machine;
569 } else {
570 remote_machine = "127.0.0.1";
573 * If we deal with a local user, change the password for the
574 * user in our SAM.
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");
582 exit(1);
585 if (!new_passwd) {
586 new_pw = prompt_for_new_password(stdin_passwd_get);
588 else
589 new_pw = smb_xstrdup(new_passwd);
591 if (!new_pw) {
592 fprintf(stderr, "Unable to get new password.\n");
593 exit(1);
596 if (!NT_STATUS_IS_OK(password_change(remote_machine,
597 domain, username,
598 old_pw, new_pw, 0))) {
599 result = 1;
600 goto done;
603 printf("Password changed for user %s\n", username);
605 done:
606 SAFE_FREE(old_pw);
607 SAFE_FREE(new_pw);
609 return result;
614 /*********************************************************
615 Start here.
616 **********************************************************/
617 int main(int argc, char **argv)
619 TALLOC_CTX *frame = talloc_stackframe();
620 int local_flags = 0;
621 int ret;
623 #if defined(HAVE_SET_AUTH_PARAMETERS)
624 set_auth_parameters(argc, argv);
625 #endif /* HAVE_SET_AUTH_PARAMETERS */
627 if (getuid() == 0) {
628 local_flags = LOCAL_AM_ROOT;
631 smb_init_locale();
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");
640 exit(1);
643 if (local_flags & LOCAL_AM_ROOT) {
644 bool ok;
646 ok = secrets_init();
647 if (!ok) {
648 return 1;
650 ret = process_root(local_flags);
651 } else {
652 ret = process_nonroot(local_flags);
654 TALLOC_FREE(frame);
655 return ret;