pidl:Wireshark Fix the type of array of pointerse to hf_ values
[Samba.git] / source3 / utils / smbpasswd.c
blobf2c5dfd2b0ff84c2377330e1a582340f45d9f19f
1 /*
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
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"
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...
36 extern char *optarg;
37 extern int optind;
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");
59 printf("options:\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");
80 exit(1);
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)
95 int ch;
96 const char *configfile = get_dyn_CONFIGFILE();
98 local_flags |= LOCAL_SET_PASSWORD;
100 ZERO_STRUCT(user_name);
102 user_name[0] = '\0';
104 while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF) {
105 switch(ch) {
106 case 'L':
107 if (getuid() != 0) {
108 fprintf(stderr, "smbpasswd -L can only be used by root.\n");
109 exit(1);
111 local_flags |= LOCAL_AM_ROOT;
112 break;
113 case 'c':
114 configfile = optarg;
115 set_dyn_CONFIGFILE(optarg);
116 break;
117 case 'a':
118 local_flags |= LOCAL_ADD_USER;
119 break;
120 case 'x':
121 local_flags |= LOCAL_DELETE_USER;
122 local_flags &= ~LOCAL_SET_PASSWORD;
123 break;
124 case 'd':
125 local_flags |= LOCAL_DISABLE_USER;
126 local_flags &= ~LOCAL_SET_PASSWORD;
127 break;
128 case 'e':
129 local_flags |= LOCAL_ENABLE_USER;
130 local_flags &= ~LOCAL_SET_PASSWORD;
131 break;
132 case 'm':
133 local_flags |= LOCAL_TRUST_ACCOUNT;
134 break;
135 case 'i':
136 local_flags |= LOCAL_INTERDOM_ACCOUNT;
137 break;
138 case 'j':
139 d_printf("See 'net join' for this functionality\n");
140 exit(1);
141 break;
142 case '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");
147 break;
148 case 'r':
149 remote_machine = optarg;
150 break;
151 case 's':
152 set_line_buffering(stdin);
153 set_line_buffering(stdout);
154 set_line_buffering(stderr);
155 stdin_passwd_get = True;
156 break;
157 case 'w':
158 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
159 fstrcpy(ldap_secret, optarg);
160 break;
161 case 'R':
162 lpcfg_set_cmdline(lp_ctx, "name resolve order", optarg);
163 break;
164 case 'D':
165 lpcfg_set_cmdline(lp_ctx, "log level", optarg);
166 break;
167 case 'U': {
168 got_username = True;
169 fstrcpy(user_name, optarg);
170 break;
171 case 'W':
172 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
173 *ldap_secret = '\0';
174 break;
176 case 'h':
177 default:
178 usage();
182 argc -= optind;
183 argv += optind;
185 switch(argc) {
186 case 0:
187 if (!got_username)
188 fstrcpy(user_name, "");
189 break;
190 case 1:
191 if (!(local_flags & LOCAL_AM_ROOT)) {
192 usage();
193 } else {
194 if (got_username) {
195 usage();
196 } else {
197 fstrcpy(user_name, argv[0]);
200 break;
201 default:
202 usage();
205 if (!lp_load_global(configfile)) {
206 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
207 configfile);
208 exit(1);
211 return local_flags;
214 /*************************************************************
215 Utility function to prompt for new password.
216 *************************************************************/
217 static char *prompt_for_new_password(bool stdin_get)
219 char *p;
220 fstring new_pw;
222 ZERO_ARRAY(new_pw);
224 p = get_pass("New SMB password:", stdin_get);
225 if (p == NULL) {
226 return NULL;
229 fstrcpy(new_pw, p);
230 SAFE_FREE(p);
232 p = get_pass("Retype new SMB password:", stdin_get);
233 if (p == NULL) {
234 return NULL;
237 if (strcmp(p, new_pw)) {
238 fprintf(stderr, "Mismatch - password unchanged.\n");
239 ZERO_ARRAY(new_pw);
240 SAFE_FREE(p);
241 return NULL;
244 return p;
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,
255 int local_flags)
257 NTSTATUS ret;
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,
270 domain, username,
271 old_passwd, new_pw, &err_str);
272 } else {
273 ret = local_password_change(username, local_flags, new_pw,
274 &err_str, &msg_str);
277 if (msg_str) {
278 printf("%s", msg_str);
280 if (err_str) {
281 fprintf(stderr, "%s", err_str);
283 if (!NT_STATUS_IS_OK(ret) && !err_str) {
284 fprintf(stderr, "Failed to change password!\n");
287 SAFE_FREE(msg_str);
288 SAFE_FREE(err_str);
289 return ret;
292 /*******************************************************************
293 Store the LDAP admin password in secrets.tdb
294 ******************************************************************/
295 static bool store_ldap_admin_pw (char* pw)
297 if (!pw)
298 return False;
300 if (!secrets_init())
301 return False;
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)
313 struct passwd *pwd;
314 int result = 0;
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"));
321 goto done;
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");
329 exit(1);
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"));
336 goto done;
339 /* Ensure passdb startup(). */
340 if(!initialize_password_db(False, NULL)) {
341 DEBUG(0, ("Failed to open passdb!\n"));
342 exit(1);
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
351 * not both set.
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))) {
356 usage();
359 /* Only load interfaces if we are doing network operations. */
361 if (remote_machine) {
362 load_interfaces();
365 if (!user_name[0] && (pwd = getpwuid_alloc(talloc_tos(), geteuid()))) {
366 fstrcpy(user_name, pwd->pw_name);
367 TALLOC_FREE(pwd);
370 if (!user_name[0]) {
371 fprintf(stderr,"You must specify a username\n");
372 exit(1);
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] == '$') {
380 user_name_len--;
381 } else {
382 if (user_name_len + 2 > sizeof(user_name)) {
383 fprintf(stderr, "machine name too long\n");
384 exit(1);
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",
400 new_passwd);
401 exit(1);
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");
410 exit(1);
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);
421 if(!new_passwd) {
422 fprintf(stderr, "Unable to get newpassword.\n");
423 exit(1);
426 } else {
428 if (remote_machine != NULL) {
429 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
430 if(!old_passwd) {
431 fprintf(stderr, "Unable to get old password.\n");
432 exit(1);
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 );
451 if (!sampass) {
452 fprintf(stderr, "talloc fail for struct samu.\n");
453 exit(1);
455 if (!pdb_getsampwnam(sampass, user_name)) {
456 fprintf(stderr, "Failed to find user %s in passdb backend.\n",
457 user_name );
458 exit(1);
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);
471 if(!new_passwd) {
472 fprintf(stderr, "Unable to get new password.\n");
473 exit(1);
478 if (!NT_STATUS_IS_OK(password_change(remote_machine,
479 NULL, user_name,
480 old_passwd, new_passwd,
481 local_flags))) {
482 result = 1;
483 goto done;
486 if(remote_machine) {
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 );
492 if (!sampass) {
493 fprintf(stderr, "talloc fail for struct samu.\n");
494 exit(1);
497 if (!pdb_getsampwnam(sampass, user_name)) {
498 fprintf(stderr, "Failed to find user %s in passdb backend.\n",
499 user_name );
500 exit(1);
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.");
510 printf("\n");
511 TALLOC_FREE(sampass);
514 done:
515 SAFE_FREE(old_passwd);
516 SAFE_FREE(new_passwd);
517 return result;
521 /*************************************************************
522 Handle password changing for non-root.
523 *************************************************************/
525 static int process_nonroot(int local_flags)
527 struct passwd *pwd = NULL;
528 int result = 0;
529 char *old_pw = NULL;
530 char *new_pw = NULL;
531 const char *username = user_name;
532 const char *domain = NULL;
533 char *p = NULL;
535 if (local_flags & ~(LOCAL_AM_ROOT | LOCAL_SET_PASSWORD)) {
536 /* Extra flags that we can't honor non-root */
537 usage();
540 if (!user_name[0]) {
541 pwd = getpwuid_alloc(talloc_tos(), getuid());
542 if (pwd) {
543 fstrcpy(user_name,pwd->pw_name);
544 TALLOC_FREE(pwd);
545 } else {
546 fprintf(stderr, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
547 exit(1);
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()))) {
555 *p = '\0';
556 username = p + 1;
557 domain = user_name;
561 * A non-root user is always setting a password
562 * via a remote machine (even if that machine is
563 * localhost).
566 load_interfaces(); /* Delayed from main() */
568 if (remote_machine != NULL) {
569 if (!is_ipaddress(remote_machine)) {
570 domain = remote_machine;
572 } else {
573 remote_machine = "127.0.0.1";
576 * If we deal with a local user, change the password for the
577 * user in our SAM.
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");
585 exit(1);
588 if (!new_passwd) {
589 new_pw = prompt_for_new_password(stdin_passwd_get);
591 else
592 new_pw = smb_xstrdup(new_passwd);
594 if (!new_pw) {
595 fprintf(stderr, "Unable to get new password.\n");
596 exit(1);
599 if (!NT_STATUS_IS_OK(password_change(remote_machine,
600 domain, username,
601 old_pw, new_pw, 0))) {
602 result = 1;
603 goto done;
606 printf("Password changed for user %s\n", username);
608 done:
609 SAFE_FREE(old_pw);
610 SAFE_FREE(new_pw);
612 return result;
617 /*********************************************************
618 Start here.
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;
625 int local_flags = 0;
626 int ret;
628 mcache = memcache_init(NULL, 0);
629 if (mcache == NULL) {
630 fprintf(stderr, "%s: memcache_init failed\n", __location__);
631 return 1;
633 memcache_set_global(mcache);
635 #if defined(HAVE_SET_AUTH_PARAMETERS)
636 set_auth_parameters(argc, argv);
637 #endif /* HAVE_SET_AUTH_PARAMETERS */
639 if (getuid() == 0) {
640 local_flags = LOCAL_AM_ROOT;
643 smb_init_locale();
645 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
646 if (lp_ctx == NULL) {
647 fprintf(stderr,
648 "Failed to initialise the global parameter structure.\n");
649 return 1;
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");
659 exit(1);
662 if (local_flags & LOCAL_AM_ROOT) {
663 bool ok;
665 ok = secrets_init();
666 if (!ok) {
667 return 1;
669 ret = process_root(local_flags);
670 } else {
671 ret = process_nonroot(local_flags);
674 gfree_all();
676 TALLOC_FREE(frame);
677 return ret;