Add 'net rpc join' to match the ADS equiv.
[Samba/ekacnet.git] / source / utils / smbpasswd.c
blob92136bea7a163500d2a754059618a9b48855cd19
1 /*
2 * Unix SMB/Netbios 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 2 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, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA. */
20 #include "includes.h"
22 extern pstring global_myname;
25 * Next two lines needed for SunOS and don't
26 * hurt anything else...
28 extern char *optarg;
29 extern int optind;
31 /** forced running in root-mode **/
32 static BOOL local_mode;
34 /**
35 * Print command usage on stderr and die.
36 **/
37 static void usage(void)
39 printf("When run by root:\n");
40 printf(" smbpasswd [options] [username] [password]\n");
41 printf("otherwise:\n");
42 printf(" smbpasswd [options] [password]\n\n");
44 printf("options:\n");
45 printf(" -s use stdin for password prompt\n");
46 printf(" -D LEVEL debug level\n");
47 printf(" -U USER remote username\n");
48 printf(" -r MACHINE remote machine\n");
50 printf("extra options when run by root or in local mode:\n");
51 printf(" -L local mode (must be first option)\n");
52 printf(" -R ORDER name resolve order\n");
53 printf(" -j DOMAIN join domain name\n");
54 printf(" -a add user\n");
55 printf(" -x delete user\n");
56 printf(" -d disable user\n");
57 printf(" -e enable user\n");
58 printf(" -n set no password\n");
59 printf(" -m machine trust account\n");
61 exit(1);
64 /*********************************************************
65 Join a domain.
66 **********************************************************/
67 static int join_domain(char *domain, char *remote)
69 pstring remote_machine;
70 fstring trust_passwd;
71 unsigned char orig_trust_passwd_hash[16];
72 BOOL ret;
74 pstrcpy(remote_machine, remote ? remote : "");
75 fstrcpy(trust_passwd, global_myname);
76 strlower(trust_passwd);
77 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
79 /* Ensure that we are not trying to join a
80 domain if we are locally set up as a domain
81 controller. */
83 if(strequal(remote, global_myname)) {
84 fprintf(stderr, "Cannot join domain %s as the domain controller name is our own. We cannot be a domain controller for a domain and also be a domain member.\n", domain);
85 return 1;
89 * Write the old machine account password.
92 if(!secrets_store_trust_account_password(domain, orig_trust_passwd_hash)) {
93 fprintf(stderr, "Unable to write the machine account password for \
94 machine %s in domain %s.\n", global_myname, domain);
95 return 1;
99 * If we are given a remote machine assume this is the PDC.
102 if(remote == NULL) {
103 pstrcpy(remote_machine, lp_passwordserver());
106 if(!*remote_machine) {
107 fprintf(stderr, "No password server list given in smb.conf - \
108 unable to join domain.\n");
109 return 1;
112 ret = change_trust_account_password( domain, remote_machine);
114 if(!ret) {
115 trust_password_delete(domain);
116 fprintf(stderr,"Unable to join domain %s.\n",domain);
117 } else {
118 printf("Joined domain %s.\n",domain);
121 return (int)ret;
125 static void set_line_buffering(FILE *f)
127 setvbuf(f, NULL, _IOLBF, 0);
130 /*************************************************************
131 Utility function to prompt for passwords from stdin. Each
132 password entered must end with a newline.
133 *************************************************************/
134 static char *stdin_new_passwd(void)
136 static fstring new_passwd;
137 size_t len;
139 ZERO_ARRAY(new_passwd);
142 * if no error is reported from fgets() and string at least contains
143 * the newline that ends the password, then replace the newline with
144 * a null terminator.
146 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
147 if ((len = strlen(new_passwd)) > 0) {
148 if(new_passwd[len-1] == '\n')
149 new_passwd[len - 1] = 0;
152 return(new_passwd);
156 /*************************************************************
157 Utility function to get passwords via tty or stdin
158 Used if the '-s' option is set to silently get passwords
159 to enable scripting.
160 *************************************************************/
161 static char *get_pass( char *prompt, BOOL stdin_get)
163 char *p;
164 if (stdin_get) {
165 p = stdin_new_passwd();
166 } else {
167 p = getpass(prompt);
169 return smb_xstrdup(p);
172 /*************************************************************
173 Utility function to prompt for new password.
174 *************************************************************/
175 static char *prompt_for_new_password(BOOL stdin_get)
177 char *p;
178 fstring new_passwd;
180 ZERO_ARRAY(new_passwd);
182 p = get_pass("New SMB password:", stdin_get);
184 fstrcpy(new_passwd, p);
185 SAFE_FREE(p);
187 p = get_pass("Retype new SMB password:", stdin_get);
189 if (strcmp(p, new_passwd)) {
190 fprintf(stderr, "Mismatch - password unchanged.\n");
191 ZERO_ARRAY(new_passwd);
192 SAFE_FREE(p);
193 return NULL;
196 return p;
200 /*************************************************************
201 Change a password either locally or remotely.
202 *************************************************************/
204 static BOOL password_change(const char *remote_machine, char *user_name,
205 char *old_passwd, char *new_passwd, int local_flags)
207 BOOL ret;
208 pstring err_str;
209 pstring msg_str;
211 if (remote_machine != NULL) {
212 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
213 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
214 /* these things can't be done remotely yet */
215 return False;
217 ret = remote_password_change(remote_machine, user_name,
218 old_passwd, new_passwd, err_str, sizeof(err_str));
219 if(*err_str)
220 fprintf(stderr, err_str);
221 return ret;
224 ret = local_password_change(user_name, local_flags, new_passwd,
225 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
227 if(*msg_str)
228 printf(msg_str);
229 if(*err_str)
230 fprintf(stderr, err_str);
232 return ret;
236 /*************************************************************
237 Handle password changing for root.
238 *************************************************************/
240 static int process_root(int argc, char *argv[])
242 struct passwd *pwd;
243 int result = 0, ch;
244 BOOL joining_domain = False, got_pass = False, got_username = False;
245 int local_flags = LOCAL_SET_PASSWORD;
246 BOOL stdin_passwd_get = False;
247 fstring user_name, user_password;
248 char *new_domain = NULL;
249 char *new_passwd = NULL;
250 char *old_passwd = NULL;
251 char *remote_machine = NULL;
253 ZERO_STRUCT(user_name);
254 ZERO_STRUCT(user_password);
256 user_name[0] = '\0';
258 while ((ch = getopt(argc, argv, "axdehmnj:r:sR:D:U:L")) != EOF) {
259 switch(ch) {
260 case 'L':
261 local_mode = True;
262 break;
263 case 'a':
264 local_flags |= LOCAL_ADD_USER;
265 break;
266 case 'x':
267 local_flags |= LOCAL_DELETE_USER;
268 local_flags &= ~LOCAL_SET_PASSWORD;
269 break;
270 case 'd':
271 local_flags |= LOCAL_DISABLE_USER;
272 local_flags &= ~LOCAL_SET_PASSWORD;
273 break;
274 case 'e':
275 local_flags |= LOCAL_ENABLE_USER;
276 local_flags &= ~LOCAL_SET_PASSWORD;
277 break;
278 case 'm':
279 local_flags |= LOCAL_TRUST_ACCOUNT;
280 break;
281 case 'n':
282 local_flags |= LOCAL_SET_NO_PASSWORD;
283 local_flags &= ~LOCAL_SET_PASSWORD;
284 break;
285 case 'j':
286 new_domain = optarg;
287 strupper(new_domain);
288 joining_domain = True;
289 break;
290 case 'r':
291 remote_machine = optarg;
292 break;
293 case 's':
294 set_line_buffering(stdin);
295 set_line_buffering(stdout);
296 set_line_buffering(stderr);
297 stdin_passwd_get = True;
298 break;
299 case 'R':
300 lp_set_name_resolve_order(optarg);
301 break;
302 case 'D':
303 DEBUGLEVEL = atoi(optarg);
304 break;
305 case 'U': {
306 char *lp;
308 got_username = True;
309 fstrcpy(user_name, optarg);
311 if ((lp = strchr_m(user_name, '%'))) {
312 *lp = 0;
313 fstrcpy(user_password, lp + 1);
314 got_pass = True;
315 memset(strchr_m(optarg, '%') + 1, 'X',
316 strlen(user_password));
319 break;
321 case 'h':
322 default:
323 usage();
327 argc -= optind;
328 argv += optind;
331 * Ensure both add/delete user are not set
332 * Ensure add/delete user and either remote machine or join domain are
333 * not both set.
335 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
336 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
337 ((remote_machine != NULL) || joining_domain))) {
338 usage();
341 /* Only load interfaces if we are doing network operations. */
343 if (joining_domain || remote_machine) {
344 load_interfaces();
347 /* Join a domain */
349 if (joining_domain) {
351 if (argc != 0)
352 usage();
354 /* Are we joining by specifing an admin username and
355 password? */
357 if (user_name[0]) {
359 /* Get administrator password if not specified */
361 if (!got_pass) {
362 char *pass = getpass("Password: ");
364 if (pass)
365 pstrcpy(user_password, pass);
368 d_printf("use net rpc join to do this now.\n");
369 return 1;
371 } else {
373 /* Or just with the server manager? */
375 return join_domain(new_domain, remote_machine);
380 * Deal with root - can add a user, but only locally.
383 switch(argc) {
384 case 0:
385 if (!got_username)
386 fstrcpy(user_name, "");
387 break;
388 case 1:
389 if (got_username)
390 usage();
391 fstrcpy(user_name, argv[0]);
392 break;
393 case 2:
394 if (got_username || got_pass)
395 usage();
396 fstrcpy(user_name, argv[0]);
397 new_passwd = smb_xstrdup(argv[1]);
398 break;
399 default:
400 usage();
403 if (!user_name[0] && (pwd = sys_getpwuid(geteuid()))) {
404 fstrcpy(user_name, pwd->pw_name);
407 if (!user_name[0]) {
408 fprintf(stderr,"You must specify a username\n");
409 exit(1);
412 if (local_flags & LOCAL_TRUST_ACCOUNT) {
413 /* add the $ automatically */
414 static fstring buf;
417 * Remove any trailing '$' before we
418 * generate the initial machine password.
421 if (user_name[strlen(user_name)-1] == '$') {
422 user_name[strlen(user_name)-1] = 0;
425 if (local_flags & LOCAL_ADD_USER) {
426 SAFE_FREE(new_passwd);
427 new_passwd = smb_xstrdup(user_name);
428 strlower(new_passwd);
432 * Now ensure the username ends in '$' for
433 * the machine add.
436 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
437 fstrcpy(user_name, buf);
440 if (remote_machine != NULL) {
441 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
444 if (!(local_flags & LOCAL_SET_PASSWORD)) {
447 * If we are trying to enable a user, first we need to find out
448 * if they are using a modern version of the smbpasswd file that
449 * disables a user by just writing a flag into the file. If so
450 * then we can re-enable a user without prompting for a new
451 * password. If not (ie. they have a no stored password in the
452 * smbpasswd file) then we need to prompt for a new password.
455 if(local_flags & LOCAL_ENABLE_USER) {
456 SAM_ACCOUNT *sampass = NULL;
457 BOOL ret;
459 pdb_init_sam(&sampass);
460 ret = pdb_getsampwnam(sampass, user_name);
461 if((sampass != False) && (pdb_get_lanman_passwd(sampass) == NULL)) {
462 local_flags |= LOCAL_SET_PASSWORD;
464 pdb_free_sam(&sampass);
468 if(local_flags & LOCAL_SET_PASSWORD) {
469 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);
477 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
478 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
479 result = 1;
480 goto done;
483 if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
484 SAM_ACCOUNT *sampass = NULL;
485 BOOL ret;
487 pdb_init_sam(&sampass);
488 ret = pdb_getsampwnam(sampass, user_name);
490 printf("Password changed for user %s.", user_name );
491 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
492 printf(" User has disabled flag set.");
493 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
494 printf(" User has no password flag set.");
495 printf("\n");
496 pdb_free_sam(&sampass);
499 done:
500 SAFE_FREE(new_passwd);
501 return result;
506 handle password changing for non-root
508 static int process_nonroot(int argc, char *argv[])
510 struct passwd *pwd = NULL;
511 int result = 0, ch;
512 BOOL stdin_passwd_get = False;
513 char *old_passwd = NULL;
514 char *remote_machine = NULL;
515 char *user_name = NULL;
516 char *new_passwd = NULL;
518 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
519 switch(ch) {
520 case 'D':
521 DEBUGLEVEL = atoi(optarg);
522 break;
523 case 'r':
524 remote_machine = optarg;
525 break;
526 case 's':
527 set_line_buffering(stdin);
528 set_line_buffering(stdout);
529 set_line_buffering(stderr);
530 stdin_passwd_get = True;
531 break;
532 case 'U':
533 user_name = optarg;
534 break;
535 default:
536 usage();
540 argc -= optind;
541 argv += optind;
543 if(argc > 1) {
544 usage();
547 if (argc == 1) {
548 new_passwd = argv[0];
551 if (!user_name) {
552 pwd = sys_getpwuid(getuid());
553 if (pwd) {
554 user_name = smb_xstrdup(pwd->pw_name);
555 } else {
556 fprintf(stderr, "smbpasswd: you don't exist - go away\n");
557 exit(1);
562 * A non-root user is always setting a password
563 * via a remote machine (even if that machine is
564 * localhost).
567 load_interfaces(); /* Delayed from main() */
569 if (remote_machine == NULL) {
570 remote_machine = "127.0.0.1";
573 if (remote_machine != NULL) {
574 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
577 if (!new_passwd) {
578 new_passwd = prompt_for_new_password(stdin_passwd_get);
581 if (!new_passwd) {
582 fprintf(stderr, "Unable to get new password.\n");
583 exit(1);
586 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
587 fprintf(stderr,"Failed to change password for %s\n", user_name);
588 result = 1;
589 goto done;
592 printf("Password changed for user %s\n", user_name);
594 done:
595 SAFE_FREE(old_passwd);
596 SAFE_FREE(new_passwd);
598 return result;
603 /*********************************************************
604 Start here.
605 **********************************************************/
606 int main(int argc, char **argv)
608 #if defined(HAVE_SET_AUTH_PARAMETERS)
609 set_auth_parameters(argc, argv);
610 #endif /* HAVE_SET_AUTH_PARAMETERS */
612 setup_logging("smbpasswd", True);
614 if(!initialize_password_db(True)) {
615 fprintf(stderr, "Can't setup password database vectors.\n");
616 exit(1);
619 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
620 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
621 dyn_CONFIGFILE);
622 exit(1);
626 * Set the machine NETBIOS name if not already
627 * set from the config file.
630 if (!*global_myname) {
631 char *p;
632 fstrcpy(global_myname, myhostname());
633 p = strchr_m(global_myname, '.' );
634 if (p) *p = 0;
636 strupper(global_myname);
638 /* Check the effective uid - make sure we are not setuid */
639 if (is_setuid_root()) {
640 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
641 exit(1);
644 /* pre-check for local mode option as first option. We can't
645 do this via normal getopt as getopt can't be called
646 twice. */
647 if (argc > 1 && strcmp(argv[1], "-L") == 0) {
648 local_mode = True;
651 if (local_mode || getuid() == 0) {
652 secrets_init();
653 return process_root(argc, argv);
656 return process_nonroot(argc, argv);