2 * Unix SMB/Netbios implementation. Version 1.9. smbpasswd module. Copyright
3 * (C) Jeremy Allison 1995-1998
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
22 extern pstring global_myname;
23 extern int DEBUGLEVEL;
26 * Next two lines needed for SunOS and don't
27 * hurt anything else...
32 /*********************************************************
34 **********************************************************/
35 static char *xstrdup(char *s)
39 fprintf(stderr,"out of memory\n");
46 /*********************************************************
47 Print command usage on stderr and die.
48 **********************************************************/
49 static void usage(void)
52 printf("smbpasswd [options] [username] [password]\n");
54 printf("smbpasswd [options] [password]\n");
57 printf(" -s use stdin for password prompt\n");
58 printf(" -D LEVEL debug level\n");
59 printf(" -U USER remote username\n");
60 printf(" -r MACHINE remote machine\n");
63 printf(" -R ORDER name resolve order\n");
64 printf(" -j DOMAIN join domain name\n");
65 printf(" -a add user\n");
66 printf(" -x delete user\n");
67 printf(" -d disable user\n");
68 printf(" -e enable user\n");
69 printf(" -n set no password\n");
70 printf(" -m machine trust account\n");
75 /*********************************************************
77 **********************************************************/
78 static int join_domain(char *domain, char *remote)
80 pstring remote_machine;
82 unsigned char orig_trust_passwd_hash[16];
85 pstrcpy(remote_machine, remote ? remote : "");
86 fstrcpy(trust_passwd, global_myname);
87 strlower(trust_passwd);
88 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
90 /* Ensure that we are not trying to join a
91 domain if we are locally set up as a domain
94 if(strequal(remote, global_myname)) {
95 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);
100 * Write the old machine account password.
103 if(!secrets_store_trust_account_password(domain, orig_trust_passwd_hash)) {
104 fprintf(stderr, "Unable to write the machine account password for \
105 machine %s in domain %s.\n", global_myname, domain);
110 * If we are given a remote machine assume this is the PDC.
114 pstrcpy(remote_machine, lp_passwordserver());
117 if(!*remote_machine) {
118 fprintf(stderr, "No password server list given in smb.conf - \
119 unable to join domain.\n");
123 ret = change_trust_account_password( domain, remote_machine);
126 trust_password_delete(domain);
127 fprintf(stderr,"Unable to join domain %s.\n",domain);
129 printf("Joined domain %s.\n",domain);
136 static void set_line_buffering(FILE *f)
138 setvbuf(f, NULL, _IOLBF, 0);
141 /*************************************************************
142 Utility function to prompt for passwords from stdin. Each
143 password entered must end with a newline.
144 *************************************************************/
145 static char *stdin_new_passwd(void)
147 static fstring new_passwd;
150 ZERO_ARRAY(new_passwd);
153 * if no error is reported from fgets() and string at least contains
154 * the newline that ends the password, then replace the newline with
157 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
158 if ((len = strlen(new_passwd)) > 0) {
159 if(new_passwd[len-1] == '\n')
160 new_passwd[len - 1] = 0;
167 /*************************************************************
168 Utility function to get passwords via tty or stdin
169 Used if the '-s' option is set to silently get passwords
171 *************************************************************/
172 static char *get_pass( char *prompt, BOOL stdin_get)
176 p = stdin_new_passwd();
183 /*************************************************************
184 Utility function to prompt for new password.
185 *************************************************************/
186 static char *prompt_for_new_password(BOOL stdin_get)
191 ZERO_ARRAY(new_passwd);
193 p = get_pass("New SMB password:", stdin_get);
195 fstrcpy(new_passwd, p);
198 p = get_pass("Retype new SMB password:", stdin_get);
200 if (strcmp(p, new_passwd)) {
201 fprintf(stderr, "Mismatch - password unchanged.\n");
202 ZERO_ARRAY(new_passwd);
211 /*************************************************************
212 Change a password either locally or remotely.
213 *************************************************************/
215 static BOOL password_change(const char *remote_machine, char *user_name,
216 char *old_passwd, char *new_passwd, int local_flags)
222 if (remote_machine != NULL) {
223 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
224 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD))
226 /* these things can't be done remotely yet */
229 ret = remote_password_change(remote_machine, user_name, old_passwd,
230 new_passwd, err_str, sizeof(err_str));
232 fprintf(stderr, err_str);
236 ret = local_password_change(user_name, local_flags, new_passwd,
237 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
242 fprintf(stderr, err_str);
248 /*************************************************************
249 Handle password changing for root.
250 *************************************************************/
252 static int process_root(int argc, char *argv[])
256 BOOL joining_domain = False;
258 BOOL stdin_passwd_get = False;
259 char *user_name = NULL;
260 char *new_domain = NULL;
261 char *new_passwd = NULL;
262 char *old_passwd = NULL;
263 char *remote_machine = NULL;
265 while ((ch = getopt(argc, argv, "ax:d:e:mnj:r:sR:D:U:")) != EOF) {
268 local_flags |= LOCAL_ADD_USER;
271 local_flags |= LOCAL_DELETE_USER;
273 new_passwd = xstrdup("XXXXXX");
276 local_flags |= LOCAL_DISABLE_USER;
278 new_passwd = xstrdup("XXXXXX");
281 local_flags |= LOCAL_ENABLE_USER;
285 local_flags |= LOCAL_TRUST_ACCOUNT;
288 local_flags |= LOCAL_SET_NO_PASSWORD;
289 new_passwd = xstrdup("NO PASSWORD");
293 strupper(new_domain);
294 joining_domain = True;
297 remote_machine = optarg;
300 set_line_buffering(stdin);
301 set_line_buffering(stdout);
302 set_line_buffering(stderr);
303 stdin_passwd_get = True;
306 lp_set_name_resolve_order(optarg);
309 DEBUGLEVEL = atoi(optarg);
323 * Ensure add/delete user and either remote machine or join domain are
326 if((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) && ((remote_machine != NULL) || joining_domain)) {
330 /* Only load interfaces if we are doing network operations. */
332 if (joining_domain || remote_machine) {
339 return join_domain(new_domain, remote_machine);
343 * Deal with root - can add a user, but only locally.
354 new_passwd = xstrdup(argv[1]);
360 if (!user_name && (pwd = sys_getpwuid(0))) {
361 user_name = xstrdup(pwd->pw_name);
365 fprintf(stderr,"You must specify a username\n");
369 if (local_flags & LOCAL_TRUST_ACCOUNT) {
370 /* add the $ automatically */
374 * Remove any trailing '$' before we
375 * generate the initial machine password.
378 if (user_name[strlen(user_name)-1] == '$') {
379 user_name[strlen(user_name)-1] = 0;
382 if (local_flags & LOCAL_ADD_USER) {
383 safe_free(new_passwd);
384 new_passwd = xstrdup(user_name);
385 strlower(new_passwd);
389 * Now ensure the username ends in '$' for
393 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
397 if (remote_machine != NULL) {
398 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
404 * If we are trying to enable a user, first we need to find out
405 * if they are using a modern version of the smbpasswd file that
406 * disables a user by just writing a flag into the file. If so
407 * then we can re-enable a user without prompting for a new
408 * password. If not (ie. they have a no stored password in the
409 * smbpasswd file) then we need to prompt for a new password.
412 if(local_flags & LOCAL_ENABLE_USER)
414 SAM_ACCOUNT *sampass = pdb_getsampwnam(user_name);
415 if((sampass != NULL) && (pdb_get_lanman_passwd(sampass) != NULL))
417 new_passwd = xstrdup("XXXX"); /* Don't care. */
422 new_passwd = prompt_for_new_password(stdin_passwd_get);
425 fprintf(stderr, "Unable to get new password.\n");
430 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags))
432 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
437 if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD)))
439 SAM_ACCOUNT *sampass = pdb_getsampwnam(user_name);
440 printf("Password changed for user %s.", user_name );
441 if( (sampass != NULL) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
442 printf(" User has disabled flag set.");
443 if((sampass != NULL) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
444 printf(" User has no password flag set.");
449 safe_free(new_passwd);
454 /*************************************************************
455 handle password changing for non-root
456 *************************************************************/
457 static int process_nonroot(int argc, char *argv[])
459 struct passwd *pwd = NULL;
461 BOOL stdin_passwd_get = False;
462 char *old_passwd = NULL;
463 char *remote_machine = NULL;
464 char *user_name = NULL;
465 char *new_passwd = NULL;
467 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
470 DEBUGLEVEL = atoi(optarg);
473 remote_machine = optarg;
476 set_line_buffering(stdin);
477 set_line_buffering(stdout);
478 set_line_buffering(stderr);
479 stdin_passwd_get = True;
497 new_passwd = argv[0];
501 pwd = sys_getpwuid(getuid());
503 user_name = xstrdup(pwd->pw_name);
505 fprintf(stderr,"you don't exist - go away\n");
511 * A non-root user is always setting a password
512 * via a remote machine (even if that machine is
516 load_interfaces(); /* Delayed from main() */
518 if (remote_machine == NULL) {
519 remote_machine = "127.0.0.1";
522 if (remote_machine != NULL) {
523 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
527 new_passwd = prompt_for_new_password(stdin_passwd_get);
531 fprintf(stderr, "Unable to get new password.\n");
535 if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
536 fprintf(stderr,"Failed to change password for %s\n", user_name);
541 printf("Password changed for user %s\n", user_name);
544 safe_free(old_passwd);
545 safe_free(new_passwd);
552 /*********************************************************
554 **********************************************************/
555 int main(int argc, char **argv)
557 static pstring servicesf = CONFIGFILE;
559 #if defined(HAVE_SET_AUTH_PARAMETERS)
560 set_auth_parameters(argc, argv);
561 #endif /* HAVE_SET_AUTH_PARAMETERS */
565 setup_logging("smbpasswd", True);
567 charset_initialise();
569 if(!initialize_password_db(True)) {
570 fprintf(stderr, "Can't setup password database vectors.\n");
574 if (!lp_load(servicesf,True,False,False)) {
575 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
581 * Set the machine NETBIOS name if not already
582 * set from the config file.
585 if (!*global_myname) {
587 fstrcpy(global_myname, myhostname());
588 p = strchr(global_myname, '.' );
591 strupper(global_myname);
593 codepage_initialise(lp_client_code_page());
597 /* Check the effective uid - make sure we are not setuid */
598 if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) {
599 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
604 return process_root(argc, argv);
607 return process_nonroot(argc, argv);