sync getopt() args with 2.2
[Samba.git] / source / utils / smbpasswd.c
blobca1fe377d0d5427f02aceb3305842aa7868389dd
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(" -a add user\n");
54 printf(" -x delete user\n");
55 printf(" -d disable user\n");
56 printf(" -e enable user\n");
57 printf(" -n set no password\n");
58 printf(" -m machine trust account\n");
59 printf(" -i interdomain trust account\n");
60 #ifdef WITH_LDAP_SAM
61 printf(" -w ldap admin password\n");
62 #endif
64 exit(1);
67 static void set_line_buffering(FILE *f)
69 setvbuf(f, NULL, _IOLBF, 0);
72 /*************************************************************
73 Utility function to prompt for passwords from stdin. Each
74 password entered must end with a newline.
75 *************************************************************/
76 static char *stdin_new_passwd(void)
78 static fstring new_passwd;
79 size_t len;
81 ZERO_ARRAY(new_passwd);
84 * if no error is reported from fgets() and string at least contains
85 * the newline that ends the password, then replace the newline with
86 * a null terminator.
88 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
89 if ((len = strlen(new_passwd)) > 0) {
90 if(new_passwd[len-1] == '\n')
91 new_passwd[len - 1] = 0;
94 return(new_passwd);
98 /*************************************************************
99 Utility function to get passwords via tty or stdin
100 Used if the '-s' option is set to silently get passwords
101 to enable scripting.
102 *************************************************************/
103 static char *get_pass( char *prompt, BOOL stdin_get)
105 char *p;
106 if (stdin_get) {
107 p = stdin_new_passwd();
108 } else {
109 p = getpass(prompt);
111 return smb_xstrdup(p);
114 /*************************************************************
115 Utility function to prompt for new password.
116 *************************************************************/
117 static char *prompt_for_new_password(BOOL stdin_get)
119 char *p;
120 fstring new_passwd;
122 ZERO_ARRAY(new_passwd);
124 p = get_pass("New SMB password:", stdin_get);
126 fstrcpy(new_passwd, p);
127 SAFE_FREE(p);
129 p = get_pass("Retype new SMB password:", stdin_get);
131 if (strcmp(p, new_passwd)) {
132 fprintf(stderr, "Mismatch - password unchanged.\n");
133 ZERO_ARRAY(new_passwd);
134 SAFE_FREE(p);
135 return NULL;
138 return p;
142 /*************************************************************
143 Change a password either locally or remotely.
144 *************************************************************/
146 static BOOL password_change(const char *remote_machine, char *user_name,
147 char *old_passwd, char *new_passwd, int local_flags)
149 BOOL ret;
150 pstring err_str;
151 pstring msg_str;
153 if (remote_machine != NULL) {
154 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
155 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
156 /* these things can't be done remotely yet */
157 return False;
159 ret = remote_password_change(remote_machine, user_name,
160 old_passwd, new_passwd, err_str, sizeof(err_str));
161 if(*err_str)
162 fprintf(stderr, err_str);
163 return ret;
166 ret = local_password_change(user_name, local_flags, new_passwd,
167 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
169 if(*msg_str)
170 printf(msg_str);
171 if(*err_str)
172 fprintf(stderr, err_str);
174 return ret;
177 #ifdef WITH_LDAP_SAM
178 /*******************************************************************
179 Store the LDAP admin password in secrets.tdb
180 ******************************************************************/
181 static BOOL store_ldap_admin_pw (char* pw)
183 if (!pw)
184 return False;
186 if (!secrets_init())
187 return False;
189 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw);
191 #endif
193 /*************************************************************
194 Handle password changing for root.
195 *************************************************************/
197 static int process_root(int argc, char *argv[])
199 struct passwd *pwd;
200 int result = 0, ch;
201 BOOL got_pass = False, got_username = False;
202 int local_flags = LOCAL_SET_PASSWORD;
203 BOOL stdin_passwd_get = False;
204 fstring user_name, user_password;
205 char *new_passwd = NULL;
206 char *old_passwd = NULL;
207 char *remote_machine = NULL;
208 #ifdef WITH_LDAP_SAM
209 fstring ldap_secret;
210 #endif
212 ZERO_STRUCT(user_name);
213 ZERO_STRUCT(user_password);
215 user_name[0] = '\0';
217 while ((ch = getopt(argc, argv, "axdehmnijr:sw:R:D:U:L")) != EOF) {
218 switch(ch) {
219 case 'L':
220 local_mode = True;
221 break;
222 case 'a':
223 local_flags |= LOCAL_ADD_USER;
224 break;
225 case 'x':
226 local_flags |= LOCAL_DELETE_USER;
227 local_flags &= ~LOCAL_SET_PASSWORD;
228 break;
229 case 'd':
230 local_flags |= LOCAL_DISABLE_USER;
231 local_flags &= ~LOCAL_SET_PASSWORD;
232 break;
233 case 'e':
234 local_flags |= LOCAL_ENABLE_USER;
235 local_flags &= ~LOCAL_SET_PASSWORD;
236 break;
237 case 'm':
238 local_flags |= LOCAL_TRUST_ACCOUNT;
239 break;
240 case 'i':
241 local_flags |= LOCAL_INTERDOM_ACCOUNT;
242 break;
243 case 'j':
244 d_printf("See 'net rpc join' for this functionality\n");
245 exit(1);
246 break;
247 case 'r':
248 remote_machine = optarg;
249 break;
250 case 's':
251 set_line_buffering(stdin);
252 set_line_buffering(stdout);
253 set_line_buffering(stderr);
254 stdin_passwd_get = True;
255 break;
256 case 'w':
257 #ifdef WITH_LDAP_SAM
258 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
259 fstrcpy(ldap_secret, optarg);
260 break;
261 #else
262 printf("-w not available unless configured --with-ldap\n");
263 goto done;
264 #endif
265 case 'R':
266 lp_set_name_resolve_order(optarg);
267 break;
268 case 'D':
269 DEBUGLEVEL = atoi(optarg);
270 break;
271 case 'U': {
272 char *lp;
274 got_username = True;
275 fstrcpy(user_name, optarg);
277 if ((lp = strchr_m(user_name, '%'))) {
278 *lp = 0;
279 fstrcpy(user_password, lp + 1);
280 got_pass = True;
281 memset(strchr_m(optarg, '%') + 1, 'X',
282 strlen(user_password));
285 break;
287 case 'h':
288 default:
289 usage();
293 argc -= optind;
294 argv += optind;
296 #ifdef WITH_LDAP_SAM
297 if (local_flags & LOCAL_SET_LDAP_ADMIN_PW)
299 printf("Setting stored password for \"%s\" in secrets.tdb\n",
300 lp_ldap_admin_dn());
301 if (!store_ldap_admin_pw(ldap_secret))
302 DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
303 goto done;
305 #endif
307 * Ensure both add/delete user are not set
308 * Ensure add/delete user and either remote machine or join domain are
309 * not both set.
311 if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) ||
312 ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) &&
313 (remote_machine != NULL))) {
314 usage();
317 /* Only load interfaces if we are doing network operations. */
319 if (remote_machine) {
320 load_interfaces();
324 * Deal with root - can add a user, but only locally.
327 switch(argc) {
328 case 0:
329 if (!got_username)
330 fstrcpy(user_name, "");
331 break;
332 case 1:
333 if (got_username)
334 usage();
335 fstrcpy(user_name, argv[0]);
336 break;
337 case 2:
338 if (got_username || got_pass)
339 usage();
340 fstrcpy(user_name, argv[0]);
341 new_passwd = smb_xstrdup(argv[1]);
342 break;
343 default:
344 usage();
347 if (!user_name[0] && (pwd = sys_getpwuid(geteuid()))) {
348 fstrcpy(user_name, pwd->pw_name);
351 if (!user_name[0]) {
352 fprintf(stderr,"You must specify a username\n");
353 exit(1);
356 if (local_flags & LOCAL_TRUST_ACCOUNT) {
357 /* add the $ automatically */
358 static fstring buf;
361 * Remove any trailing '$' before we
362 * generate the initial machine password.
365 if (user_name[strlen(user_name)-1] == '$') {
366 user_name[strlen(user_name)-1] = 0;
369 if (local_flags & LOCAL_ADD_USER) {
370 SAFE_FREE(new_passwd);
371 new_passwd = smb_xstrdup(user_name);
372 strlower(new_passwd);
376 * Now ensure the username ends in '$' for
377 * the machine add.
380 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
381 fstrcpy(user_name, buf);
382 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
383 static fstring buf;
385 if (local_flags & LOCAL_ADD_USER) {
387 * Prompt for trusting domain's account password
389 new_passwd = prompt_for_new_password(stdin_passwd_get);
390 if(!new_passwd) {
391 fprintf(stderr, "Unable to get newpassword.\n");
392 exit(1);
395 slprintf(buf, sizeof(buf) - 1, "%s$", user_name);
396 fstrcpy(user_name, buf);
398 } else {
400 if (remote_machine != NULL) {
401 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
404 if (!(local_flags & LOCAL_SET_PASSWORD)) {
407 * If we are trying to enable a user, first we need to find out
408 * if they are using a modern version of the smbpasswd file that
409 * disables a user by just writing a flag into the file. If so
410 * then we can re-enable a user without prompting for a new
411 * password. If not (ie. they have a no stored password in the
412 * smbpasswd file) then we need to prompt for a new password.
415 if(local_flags & LOCAL_ENABLE_USER) {
416 SAM_ACCOUNT *sampass = NULL;
417 BOOL ret;
419 pdb_init_sam(&sampass);
420 ret = pdb_getsampwnam(sampass, user_name);
421 if((sampass != False) && (pdb_get_lanman_passwd(sampass) == NULL)) {
422 local_flags |= LOCAL_SET_PASSWORD;
424 pdb_free_sam(&sampass);
428 if(local_flags & LOCAL_SET_PASSWORD) {
429 new_passwd = prompt_for_new_password(stdin_passwd_get);
431 if(!new_passwd) {
432 fprintf(stderr, "Unable to get new password.\n");
433 exit(1);
438 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
439 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
440 result = 1;
441 goto done;
444 if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
445 SAM_ACCOUNT *sampass = NULL;
446 BOOL ret;
448 pdb_init_sam(&sampass);
449 ret = pdb_getsampwnam(sampass, user_name);
451 printf("Password changed for user %s.", user_name );
452 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
453 printf(" User has disabled flag set.");
454 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
455 printf(" User has no password flag set.");
456 printf("\n");
457 pdb_free_sam(&sampass);
460 done:
461 SAFE_FREE(new_passwd);
462 return result;
467 handle password changing for non-root
469 static int process_nonroot(int argc, char *argv[])
471 struct passwd *pwd = NULL;
472 int result = 0, ch;
473 BOOL stdin_passwd_get = False;
474 char *old_passwd = NULL;
475 char *remote_machine = NULL;
476 char *user_name = NULL;
477 char *new_passwd = NULL;
479 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
480 switch(ch) {
481 case 'D':
482 DEBUGLEVEL = atoi(optarg);
483 break;
484 case 'r':
485 remote_machine = optarg;
486 break;
487 case 's':
488 set_line_buffering(stdin);
489 set_line_buffering(stdout);
490 set_line_buffering(stderr);
491 stdin_passwd_get = True;
492 break;
493 case 'U':
494 user_name = optarg;
495 break;
496 default:
497 usage();
501 argc -= optind;
502 argv += optind;
504 if(argc > 1) {
505 usage();
508 if (argc == 1) {
509 new_passwd = argv[0];
512 if (!user_name) {
513 pwd = sys_getpwuid(getuid());
514 if (pwd) {
515 user_name = smb_xstrdup(pwd->pw_name);
516 } else {
517 fprintf(stderr, "smbpasswd: you don't exist - go away\n");
518 exit(1);
523 * A non-root user is always setting a password
524 * via a remote machine (even if that machine is
525 * localhost).
528 load_interfaces(); /* Delayed from main() */
530 if (remote_machine == NULL) {
531 remote_machine = "127.0.0.1";
534 if (remote_machine != NULL) {
535 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
538 if (!new_passwd) {
539 new_passwd = prompt_for_new_password(stdin_passwd_get);
542 if (!new_passwd) {
543 fprintf(stderr, "Unable to get new password.\n");
544 exit(1);
547 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
548 fprintf(stderr,"Failed to change password for %s\n", user_name);
549 result = 1;
550 goto done;
553 printf("Password changed for user %s\n", user_name);
555 done:
556 SAFE_FREE(old_passwd);
557 SAFE_FREE(new_passwd);
559 return result;
564 /*********************************************************
565 Start here.
566 **********************************************************/
567 int main(int argc, char **argv)
569 #if defined(HAVE_SET_AUTH_PARAMETERS)
570 set_auth_parameters(argc, argv);
571 #endif /* HAVE_SET_AUTH_PARAMETERS */
573 setup_logging("smbpasswd", True);
575 if(!initialize_password_db(True)) {
576 fprintf(stderr, "Can't setup password database vectors.\n");
577 exit(1);
580 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
581 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
582 dyn_CONFIGFILE);
583 exit(1);
587 * Set the machine NETBIOS name if not already
588 * set from the config file.
591 if (!*global_myname) {
592 char *p;
593 fstrcpy(global_myname, myhostname());
594 p = strchr_m(global_myname, '.' );
595 if (p) *p = 0;
597 strupper(global_myname);
599 /* Check the effective uid - make sure we are not setuid */
600 if (is_setuid_root()) {
601 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
602 exit(1);
605 /* pre-check for local mode option as first option. We can't
606 do this via normal getopt as getopt can't be called
607 twice. */
608 if (argc > 1 && strcmp(argv[1], "-L") == 0) {
609 local_mode = True;
612 if (local_mode || getuid() == 0) {
613 secrets_init();
614 return process_root(argc, argv);
617 return process_nonroot(argc, argv);