Line data Source code
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.
10 : *
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.
15 : *
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/>. */
18 :
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"
29 : #include "lib/param/param.h"
30 : #include "lib/util/memcache.h"
31 :
32 : /*
33 : * Next two lines needed for SunOS and don't
34 : * hurt anything else...
35 : */
36 : extern char *optarg;
37 : extern int optind;
38 :
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;
45 :
46 : static fstring ldap_secret;
47 :
48 :
49 : /*********************************************************
50 : Print command usage on stderr and die.
51 : **********************************************************/
52 0 : static void usage(void)
53 : {
54 0 : printf("When run by root:\n");
55 0 : printf(" smbpasswd [options] [username]\n");
56 0 : printf("otherwise:\n");
57 0 : printf(" smbpasswd [options]\n\n");
58 :
59 0 : printf("options:\n");
60 0 : printf(" -L local mode (must be first option)\n");
61 0 : printf(" -h print this usage message\n");
62 0 : printf(" -s use stdin for password prompt\n");
63 0 : printf(" -c smb.conf file Use the given path to the smb.conf file\n");
64 0 : printf(" -D LEVEL debug level\n");
65 0 : printf(" -r MACHINE remote machine\n");
66 0 : printf(" -U USER remote username (e.g. SAM/user)\n");
67 :
68 0 : printf("extra options when run by root or in local mode:\n");
69 0 : printf(" -a add user\n");
70 0 : printf(" -d disable user\n");
71 0 : printf(" -e enable user\n");
72 0 : printf(" -i interdomain trust account\n");
73 0 : printf(" -m machine trust account\n");
74 0 : printf(" -n set no password\n");
75 0 : printf(" -W use stdin ldap admin password\n");
76 0 : printf(" -w PASSWORD ldap admin password\n");
77 0 : printf(" -x delete user\n");
78 0 : printf(" -R ORDER name resolve order\n");
79 :
80 0 : exit(1);
81 : }
82 :
83 1470 : static void set_line_buffering(FILE *f)
84 : {
85 1470 : setvbuf(f, NULL, _IOLBF, 0);
86 1470 : }
87 :
88 : /*******************************************************************
89 : Process command line options
90 : ******************************************************************/
91 :
92 511 : static int process_options(int argc, char **argv, int local_flags,
93 : struct loadparm_context *lp_ctx)
94 : {
95 : int ch;
96 511 : const char *configfile = get_dyn_CONFIGFILE();
97 :
98 511 : local_flags |= LOCAL_SET_PASSWORD;
99 :
100 511 : ZERO_STRUCT(user_name);
101 :
102 511 : user_name[0] = '\0';
103 :
104 2519 : while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF) {
105 2008 : switch(ch) {
106 497 : case 'L':
107 497 : if (getuid() != 0) {
108 0 : fprintf(stderr, "smbpasswd -L can only be used by root.\n");
109 0 : exit(1);
110 : }
111 497 : local_flags |= LOCAL_AM_ROOT;
112 497 : break;
113 511 : case 'c':
114 511 : configfile = optarg;
115 511 : set_dyn_CONFIGFILE(optarg);
116 511 : break;
117 496 : case 'a':
118 496 : local_flags |= LOCAL_ADD_USER;
119 496 : break;
120 4 : case 'x':
121 4 : local_flags |= LOCAL_DELETE_USER;
122 4 : local_flags &= ~LOCAL_SET_PASSWORD;
123 4 : break;
124 0 : case 'd':
125 0 : local_flags |= LOCAL_DISABLE_USER;
126 0 : local_flags &= ~LOCAL_SET_PASSWORD;
127 0 : break;
128 0 : case 'e':
129 0 : local_flags |= LOCAL_ENABLE_USER;
130 0 : local_flags &= ~LOCAL_SET_PASSWORD;
131 0 : break;
132 2 : case 'm':
133 2 : local_flags |= LOCAL_TRUST_ACCOUNT;
134 2 : break;
135 0 : case 'i':
136 0 : local_flags |= LOCAL_INTERDOM_ACCOUNT;
137 0 : break;
138 0 : case 'j':
139 0 : d_printf("See 'net join' for this functionality\n");
140 0 : exit(1);
141 : break;
142 0 : case 'n':
143 0 : local_flags |= LOCAL_SET_NO_PASSWORD;
144 0 : local_flags &= ~LOCAL_SET_PASSWORD;
145 0 : SAFE_FREE(new_passwd);
146 0 : new_passwd = smb_xstrdup("NO PASSWORD");
147 0 : break;
148 6 : case 'r':
149 6 : remote_machine = optarg;
150 6 : break;
151 490 : case 's':
152 490 : set_line_buffering(stdin);
153 490 : set_line_buffering(stdout);
154 490 : set_line_buffering(stderr);
155 490 : stdin_passwd_get = True;
156 490 : break;
157 0 : case 'w':
158 0 : local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
159 0 : fstrcpy(ldap_secret, optarg);
160 0 : break;
161 0 : case 'R':
162 0 : lpcfg_set_cmdline(lp_ctx, "name resolve order", optarg);
163 0 : break;
164 0 : case 'D':
165 0 : lpcfg_set_cmdline(lp_ctx, "log level", optarg);
166 0 : break;
167 2 : case 'U': {
168 2 : got_username = True;
169 2 : fstrcpy(user_name, optarg);
170 2 : break;
171 0 : case 'W':
172 0 : local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
173 0 : *ldap_secret = '\0';
174 0 : break;
175 : }
176 0 : case 'h':
177 : default:
178 0 : usage();
179 : }
180 : }
181 :
182 511 : argc -= optind;
183 511 : argv += optind;
184 :
185 511 : switch(argc) {
186 6 : case 0:
187 6 : if (!got_username)
188 4 : fstrcpy(user_name, "");
189 6 : break;
190 505 : case 1:
191 505 : if (!(local_flags & LOCAL_AM_ROOT)) {
192 0 : usage();
193 : } else {
194 505 : if (got_username) {
195 0 : usage();
196 : } else {
197 505 : fstrcpy(user_name, argv[0]);
198 : }
199 : }
200 505 : break;
201 0 : default:
202 0 : usage();
203 : }
204 :
205 511 : if (!lp_load_global(configfile)) {
206 0 : fprintf(stderr, "Can't load %s - run testparm to debug it\n",
207 : configfile);
208 0 : exit(1);
209 : }
210 :
211 511 : return local_flags;
212 : }
213 :
214 : /*************************************************************
215 : Utility function to prompt for new password.
216 : *************************************************************/
217 505 : static char *prompt_for_new_password(bool stdin_get)
218 : {
219 : char *p;
220 : fstring new_pw;
221 :
222 505 : ZERO_ARRAY(new_pw);
223 :
224 505 : p = get_pass("New SMB password:", stdin_get);
225 505 : if (p == NULL) {
226 0 : return NULL;
227 : }
228 :
229 505 : fstrcpy(new_pw, p);
230 505 : SAFE_FREE(p);
231 :
232 505 : p = get_pass("Retype new SMB password:", stdin_get);
233 505 : if (p == NULL) {
234 0 : return NULL;
235 : }
236 :
237 505 : if (strcmp(p, new_pw)) {
238 0 : fprintf(stderr, "Mismatch - password unchanged.\n");
239 0 : ZERO_ARRAY(new_pw);
240 0 : SAFE_FREE(p);
241 0 : return NULL;
242 : }
243 :
244 505 : return p;
245 : }
246 :
247 :
248 : /*************************************************************
249 : Change a password either locally or remotely.
250 : *************************************************************/
251 :
252 511 : static NTSTATUS password_change(const char *remote_mach,
253 : const char *domain, const char *username,
254 : const char *old_passwd, const char *new_pw,
255 : int local_flags)
256 : {
257 : NTSTATUS ret;
258 511 : char *err_str = NULL;
259 511 : char *msg_str = NULL;
260 :
261 511 : if (remote_mach != NULL) {
262 6 : 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 0 : fprintf(stderr, "Invalid remote operation!\n");
267 0 : return NT_STATUS_UNSUCCESSFUL;
268 : }
269 6 : ret = remote_password_change(remote_mach,
270 : domain, username,
271 : old_passwd, new_pw, &err_str);
272 : } else {
273 505 : ret = local_password_change(username, local_flags, new_pw,
274 : &err_str, &msg_str);
275 : }
276 :
277 511 : if (msg_str) {
278 500 : printf("%s", msg_str);
279 : }
280 511 : if (err_str) {
281 0 : fprintf(stderr, "%s", err_str);
282 : }
283 511 : if (!NT_STATUS_IS_OK(ret) && !err_str) {
284 0 : fprintf(stderr, "Failed to change password!\n");
285 : }
286 :
287 511 : SAFE_FREE(msg_str);
288 511 : SAFE_FREE(err_str);
289 511 : return ret;
290 : }
291 :
292 : /*******************************************************************
293 : Store the LDAP admin password in secrets.tdb
294 : ******************************************************************/
295 0 : static bool store_ldap_admin_pw (char* pw)
296 : {
297 0 : if (!pw)
298 0 : return False;
299 :
300 0 : if (!secrets_init())
301 0 : return False;
302 :
303 0 : return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw);
304 : }
305 :
306 :
307 : /*************************************************************
308 : Handle password changing for root.
309 : *************************************************************/
310 :
311 505 : static int process_root(int local_flags)
312 : {
313 : struct passwd *pwd;
314 505 : int result = 0;
315 505 : char *old_passwd = NULL;
316 :
317 505 : if (local_flags & LOCAL_SET_LDAP_ADMIN_PW) {
318 0 : const char *ldap_admin_dn = lp_ldap_admin_dn();
319 0 : if ( ! *ldap_admin_dn ) {
320 0 : DEBUG(0,("ERROR: 'ldap admin dn' not defined! Please check your smb.conf\n"));
321 0 : goto done;
322 : }
323 :
324 0 : printf("Setting stored password for \"%s\" in secrets.tdb\n", ldap_admin_dn);
325 0 : if ( ! *ldap_secret ) {
326 0 : new_passwd = prompt_for_new_password(stdin_passwd_get);
327 0 : if (new_passwd == NULL) {
328 0 : fprintf(stderr, "Failed to read new password!\n");
329 0 : exit(1);
330 : }
331 0 : fstrcpy(ldap_secret, new_passwd);
332 : }
333 0 : if (!store_ldap_admin_pw(ldap_secret)) {
334 0 : DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
335 : }
336 0 : goto done;
337 : }
338 :
339 : /* Ensure passdb startup(). */
340 505 : if(!initialize_password_db(False, NULL)) {
341 0 : DEBUG(0, ("Failed to open passdb!\n"));
342 0 : exit(1);
343 : }
344 :
345 : /* Ensure we have a SAM sid. */
346 505 : get_global_sam_sid();
347 :
348 : /*
349 : * Ensure both add/delete user are not set
350 : * Ensure add/delete user and either remote machine or join domain are
351 : * not both set.
352 : */
353 505 : if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
354 505 : ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
355 500 : (remote_machine != NULL))) {
356 0 : usage();
357 : }
358 :
359 : /* Only load interfaces if we are doing network operations. */
360 :
361 505 : if (remote_machine) {
362 0 : load_interfaces();
363 : }
364 :
365 505 : if (!user_name[0] && (pwd = getpwuid_alloc(talloc_tos(), geteuid()))) {
366 0 : fstrcpy(user_name, pwd->pw_name);
367 0 : TALLOC_FREE(pwd);
368 : }
369 :
370 505 : if (!user_name[0]) {
371 0 : fprintf(stderr,"You must specify a username\n");
372 0 : exit(1);
373 : }
374 :
375 505 : if (local_flags & LOCAL_TRUST_ACCOUNT) {
376 : /* add the $ automatically */
377 2 : size_t user_name_len = strlen(user_name);
378 :
379 2 : if (user_name[user_name_len - 1] == '$') {
380 0 : user_name_len--;
381 : } else {
382 2 : if (user_name_len + 2 > sizeof(user_name)) {
383 0 : fprintf(stderr, "machine name too long\n");
384 0 : exit(1);
385 : }
386 2 : user_name[user_name_len] = '$';
387 2 : user_name[user_name_len + 1] = '\0';
388 : }
389 :
390 2 : if (local_flags & LOCAL_ADD_USER) {
391 2 : SAFE_FREE(new_passwd);
392 :
393 : /*
394 : * Remove any trailing '$' before we
395 : * generate the initial machine password.
396 : */
397 2 : new_passwd = smb_xstrndup(user_name, user_name_len);
398 2 : if (!strlower_m(new_passwd)) {
399 0 : fprintf(stderr, "strlower_m %s failed\n",
400 : new_passwd);
401 0 : exit(1);
402 : }
403 : }
404 503 : } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
405 0 : size_t user_name_len = strlen(user_name);
406 :
407 0 : if (user_name[user_name_len - 1] != '$') {
408 0 : if (user_name_len + 2 > sizeof(user_name)) {
409 0 : fprintf(stderr, "machine name too long\n");
410 0 : exit(1);
411 : }
412 0 : user_name[user_name_len] = '$';
413 0 : user_name[user_name_len + 1] = '\0';
414 : }
415 :
416 0 : if ((local_flags & LOCAL_ADD_USER) && (new_passwd == NULL)) {
417 : /*
418 : * Prompt for trusting domain's account password
419 : */
420 0 : new_passwd = prompt_for_new_password(stdin_passwd_get);
421 0 : if(!new_passwd) {
422 0 : fprintf(stderr, "Unable to get newpassword.\n");
423 0 : exit(1);
424 : }
425 : }
426 : } else {
427 :
428 503 : if (remote_machine != NULL) {
429 0 : old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
430 0 : if(!old_passwd) {
431 0 : fprintf(stderr, "Unable to get old password.\n");
432 0 : exit(1);
433 : }
434 : }
435 :
436 503 : if (!(local_flags & LOCAL_SET_PASSWORD)) {
437 :
438 : /*
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.
445 : */
446 :
447 4 : if(local_flags & LOCAL_ENABLE_USER) {
448 0 : struct samu *sampass = NULL;
449 :
450 0 : sampass = samu_new( NULL );
451 0 : if (!sampass) {
452 0 : fprintf(stderr, "talloc fail for struct samu.\n");
453 0 : exit(1);
454 : }
455 0 : if (!pdb_getsampwnam(sampass, user_name)) {
456 0 : fprintf(stderr, "Failed to find user %s in passdb backend.\n",
457 : user_name );
458 0 : exit(1);
459 : }
460 :
461 0 : if(pdb_get_nt_passwd(sampass) == NULL) {
462 0 : local_flags |= LOCAL_SET_PASSWORD;
463 : }
464 0 : TALLOC_FREE(sampass);
465 : }
466 : }
467 :
468 503 : if((local_flags & LOCAL_SET_PASSWORD) && (new_passwd == NULL)) {
469 :
470 499 : new_passwd = prompt_for_new_password(stdin_passwd_get);
471 499 : if(!new_passwd) {
472 0 : fprintf(stderr, "Unable to get new password.\n");
473 0 : exit(1);
474 : }
475 : }
476 : }
477 :
478 505 : if (!NT_STATUS_IS_OK(password_change(remote_machine,
479 : NULL, user_name,
480 : old_passwd, new_passwd,
481 : local_flags))) {
482 0 : result = 1;
483 0 : goto done;
484 : }
485 :
486 505 : if(remote_machine) {
487 0 : printf("Password changed for user %s on %s.\n", user_name, remote_machine );
488 505 : } else if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
489 0 : struct samu *sampass = NULL;
490 :
491 0 : sampass = samu_new( NULL );
492 0 : if (!sampass) {
493 0 : fprintf(stderr, "talloc fail for struct samu.\n");
494 0 : exit(1);
495 : }
496 :
497 0 : if (!pdb_getsampwnam(sampass, user_name)) {
498 0 : fprintf(stderr, "Failed to find user %s in passdb backend.\n",
499 : user_name );
500 0 : exit(1);
501 : }
502 :
503 0 : printf("Password changed for user %s.", user_name );
504 0 : if(pdb_get_acct_ctrl(sampass)&ACB_DISABLED) {
505 0 : printf(" User has disabled flag set.");
506 : }
507 0 : if(pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) {
508 0 : printf(" User has no password flag set.");
509 : }
510 0 : printf("\n");
511 0 : TALLOC_FREE(sampass);
512 : }
513 :
514 505 : done:
515 505 : SAFE_FREE(old_passwd);
516 505 : SAFE_FREE(new_passwd);
517 505 : return result;
518 : }
519 :
520 :
521 : /*************************************************************
522 : Handle password changing for non-root.
523 : *************************************************************/
524 :
525 6 : static int process_nonroot(int local_flags)
526 : {
527 6 : struct passwd *pwd = NULL;
528 6 : int result = 0;
529 6 : char *old_pw = NULL;
530 6 : char *new_pw = NULL;
531 6 : const char *username = user_name;
532 6 : const char *domain = NULL;
533 6 : char *p = NULL;
534 :
535 6 : if (local_flags & ~(LOCAL_AM_ROOT | LOCAL_SET_PASSWORD)) {
536 : /* Extra flags that we can't honor non-root */
537 0 : usage();
538 : }
539 :
540 6 : if (!user_name[0]) {
541 4 : pwd = getpwuid_alloc(talloc_tos(), getuid());
542 4 : if (pwd) {
543 4 : fstrcpy(user_name,pwd->pw_name);
544 4 : TALLOC_FREE(pwd);
545 : } else {
546 0 : fprintf(stderr, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
547 0 : exit(1);
548 : }
549 : }
550 :
551 : /* Allow domain as part of the username */
552 12 : if ((p = strchr_m(user_name, '\\')) ||
553 12 : (p = strchr_m(user_name, '/')) ||
554 6 : (p = strchr_m(user_name, *lp_winbind_separator()))) {
555 0 : *p = '\0';
556 0 : username = p + 1;
557 0 : domain = user_name;
558 : }
559 :
560 : /*
561 : * A non-root user is always setting a password
562 : * via a remote machine (even if that machine is
563 : * localhost).
564 : */
565 :
566 6 : load_interfaces(); /* Delayed from main() */
567 :
568 6 : if (remote_machine != NULL) {
569 6 : if (!is_ipaddress(remote_machine)) {
570 6 : domain = remote_machine;
571 : }
572 : } else {
573 0 : remote_machine = "127.0.0.1";
574 :
575 : /*
576 : * If we deal with a local user, change the password for the
577 : * user in our SAM.
578 : */
579 0 : domain = get_global_sam_name();
580 : }
581 :
582 6 : old_pw = get_pass("Old SMB password:",stdin_passwd_get);
583 6 : if (old_pw == NULL) {
584 0 : fprintf(stderr, "Unable to get old password.\n");
585 0 : exit(1);
586 : }
587 :
588 6 : if (!new_passwd) {
589 6 : new_pw = prompt_for_new_password(stdin_passwd_get);
590 : }
591 : else
592 0 : new_pw = smb_xstrdup(new_passwd);
593 :
594 6 : if (!new_pw) {
595 0 : fprintf(stderr, "Unable to get new password.\n");
596 0 : exit(1);
597 : }
598 :
599 6 : if (!NT_STATUS_IS_OK(password_change(remote_machine,
600 : domain, username,
601 : old_pw, new_pw, 0))) {
602 0 : result = 1;
603 0 : goto done;
604 : }
605 :
606 6 : printf("Password changed for user %s\n", username);
607 :
608 6 : done:
609 6 : SAFE_FREE(old_pw);
610 6 : SAFE_FREE(new_pw);
611 :
612 6 : return result;
613 : }
614 :
615 :
616 :
617 : /*********************************************************
618 : Start here.
619 : **********************************************************/
620 511 : int main(int argc, char **argv)
621 : {
622 511 : TALLOC_CTX *frame = talloc_stackframe();
623 511 : struct loadparm_context *lp_ctx = NULL;
624 511 : struct memcache *mcache = NULL;
625 511 : int local_flags = 0;
626 : int ret;
627 :
628 511 : mcache = memcache_init(NULL, 0);
629 511 : if (mcache == NULL) {
630 0 : fprintf(stderr, "%s: memcache_init failed\n", __location__);
631 0 : return 1;
632 : }
633 511 : memcache_set_global(mcache);
634 :
635 : #if defined(HAVE_SET_AUTH_PARAMETERS)
636 : set_auth_parameters(argc, argv);
637 : #endif /* HAVE_SET_AUTH_PARAMETERS */
638 :
639 511 : if (getuid() == 0) {
640 505 : local_flags = LOCAL_AM_ROOT;
641 : }
642 :
643 511 : smb_init_locale();
644 :
645 511 : lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
646 511 : if (lp_ctx == NULL) {
647 0 : fprintf(stderr,
648 : "Failed to initialise the global parameter structure.\n");
649 0 : return 1;
650 : }
651 :
652 511 : local_flags = process_options(argc, argv, local_flags, lp_ctx);
653 :
654 511 : setup_logging("smbpasswd", DEBUG_STDERR);
655 :
656 : /* Check the effective uid - make sure we are not setuid */
657 511 : if (is_setuid_root()) {
658 0 : fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
659 0 : exit(1);
660 : }
661 :
662 511 : if (local_flags & LOCAL_AM_ROOT) {
663 : bool ok;
664 :
665 505 : ok = secrets_init();
666 505 : if (!ok) {
667 0 : return 1;
668 : }
669 505 : ret = process_root(local_flags);
670 : } else {
671 6 : ret = process_nonroot(local_flags);
672 : }
673 :
674 511 : gfree_all();
675 :
676 511 : TALLOC_FREE(frame);
677 511 : return ret;
678 : }
|