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 myhostname;
23 extern pstring global_myname;
24 extern int DEBUGLEVEL;
27 /*********************************************************
29 **********************************************************/
30 static char *xstrdup(char *s)
34 fprintf(stderr,"out of memory\n");
41 /*********************************************************
42 Print command usage on stderr and die.
43 **********************************************************/
44 static void usage(void)
47 printf("smbpasswd [options] [username] [password]\n");
49 printf("smbpasswd [options] [password]\n");
52 printf(" -s use stdin for password prompt\n");
53 printf(" -D LEVEL debug level\n");
54 printf(" -U USER remote username\n");
55 printf(" -r MACHINE remote machine\n");
58 printf(" -R ORDER name resolve order\n");
59 printf(" -j DOMAIN join domain name\n");
60 printf(" -a add user\n");
61 printf(" -d disable user\n");
62 printf(" -e enable user\n");
63 printf(" -n set no password\n");
64 printf(" -m machine trust account\n");
69 /*********************************************************
71 **********************************************************/
72 static int join_domain(char *domain, char *remote)
74 pstring remote_machine;
76 unsigned char orig_trust_passwd_hash[16];
79 pstrcpy(remote_machine, remote ? remote : "");
80 fstrcpy(trust_passwd, global_myname);
81 strlower(trust_passwd);
82 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
84 /* Ensure that we are not trying to join a
85 domain if we are locally set up as a domain
88 if(strequal(remote, global_myname)) {
89 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);
94 * Create the machine account password file.
96 if(!trust_password_lock( domain, global_myname, True)) {
97 fprintf(stderr, "unable to open the machine account password file for \
98 machine %s in domain %s.\n", global_myname, domain);
103 * Write the old machine account password.
106 if(!set_trust_account_password( orig_trust_passwd_hash)) {
107 fprintf(stderr, "unable to write the machine account password for \
108 machine %s in domain %s.\n", global_myname, domain);
109 trust_password_unlock();
114 * If we are given a remote machine assume this is the PDC.
118 pstrcpy(remote_machine, lp_passwordserver());
121 if(!*remote_machine) {
122 fprintf(stderr, "No password server list given in smb.conf - \
123 unable to join domain.\n");
124 trust_password_unlock();
128 ret = change_trust_account_password( domain, remote_machine);
129 trust_password_unlock();
132 trust_password_delete( domain, global_myname);
133 fprintf(stderr,"Unable to join domain %s.\n",domain);
135 printf("Joined domain %s.\n",domain);
142 static void set_line_buffering(FILE *f)
144 setvbuf(f, NULL, _IOLBF, 0);
147 /*************************************************************
148 Utility function to prompt for passwords from stdin. Each
149 password entered must end with a newline.
150 *************************************************************/
151 static char *stdin_new_passwd(void)
153 static fstring new_passwd;
156 ZERO_ARRAY(new_passwd);
159 * if no error is reported from fgets() and string at least contains
160 * the newline that ends the password, then replace the newline with
163 if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
164 if ((len = strlen(new_passwd)) > 0) {
165 if(new_passwd[len-1] == '\n')
166 new_passwd[len - 1] = 0;
173 /*************************************************************
174 Utility function to get passwords via tty or stdin
175 Used if the '-s' option is set to silently get passwords
177 *************************************************************/
178 static char *get_pass( char *prompt, BOOL stdin_get)
182 p = stdin_new_passwd();
189 /*************************************************************
190 Utility function to prompt for new password.
191 *************************************************************/
192 static char *prompt_for_new_password(BOOL stdin_get)
197 ZERO_ARRAY(new_passwd);
199 p = get_pass("New SMB password:", stdin_get);
201 fstrcpy(new_passwd, p);
203 p = get_pass("Retype new SMB password:", stdin_get);
205 if (strcmp(p, new_passwd)) {
206 fprintf(stderr, "Mismatch - password unchanged.\n");
214 /*************************************************************
215 change a password either locally or remotely
216 *************************************************************/
217 static BOOL password_change(const char *remote_machine, char *user_name,
218 char *old_passwd, char *new_passwd,
219 BOOL add_user, BOOL enable_user,
220 BOOL disable_user, BOOL set_no_password,
227 if (remote_machine != NULL) {
228 if (add_user || enable_user || disable_user || set_no_password || trust_account) {
229 /* these things can't be done remotely yet */
232 ret = remote_password_change(remote_machine, user_name,
233 old_passwd, new_passwd, err_str, sizeof(err_str));
235 fprintf(stderr, err_str);
239 ret = local_password_change(user_name, trust_account, add_user, enable_user,
240 disable_user, set_no_password, new_passwd,
241 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
246 fprintf(stderr, err_str);
252 /*************************************************************
253 handle password changing for root
254 *************************************************************/
255 static int process_root(int argc, char *argv[])
259 BOOL joining_domain = False;
260 BOOL trust_account = False;
261 BOOL add_user = False;
262 BOOL disable_user = False;
263 BOOL enable_user = False;
264 BOOL set_no_password = False;
265 BOOL stdin_passwd_get = False;
266 char *user_name = NULL;
267 char *new_domain = NULL;
268 char *new_passwd = NULL;
269 char *old_passwd = NULL;
270 char *remote_machine = NULL;
272 while ((ch = getopt(argc, argv, "adehmnj:r:sR:D:U:")) != EOF) {
279 new_passwd = "XXXXXX";
285 DEBUGLEVEL = atoi(optarg);
288 set_no_password = True;
289 new_passwd = "NO PASSWORD";
291 remote_machine = optarg;
294 set_line_buffering(stdin);
295 set_line_buffering(stdout);
296 set_line_buffering(stderr);
297 stdin_passwd_get = True;
300 lp_set_name_resolve_order(optarg);
303 trust_account = True;
307 strupper(new_domain);
308 joining_domain = True;
323 * Ensure add_user and either remote machine or join domain are
326 if(add_user && ((remote_machine != NULL) || joining_domain)) {
331 if (argc != 0) usage();
332 return join_domain(new_domain, remote_machine);
336 * Deal with root - can add a user, but only locally.
347 new_passwd = argv[1];
353 if (!user_name && (pwd = getpwuid(0))) {
354 user_name = xstrdup(pwd->pw_name);
358 fprintf(stderr,"You must specify a username\n");
362 if (!remote_machine && !Get_Pwnam(user_name, True)) {
363 fprintf(stderr, "User \"%s\" was not found in system password file.\n",
368 if (user_name[strlen(user_name)-1] == '$') {
369 user_name[strlen(user_name)-1] = 0;
373 /* add the $ automatically */
377 new_passwd = xstrdup(user_name);
378 strlower(new_passwd);
381 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
385 if (remote_machine != NULL) {
386 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
392 * If we are trying to enable a user, first we need to find out
393 * if they are using a modern version of the smbpasswd file that
394 * disables a user by just writing a flag into the file. If so
395 * then we can re-enable a user without prompting for a new
396 * password. If not (ie. they have a no stored password in the
397 * smbpasswd file) then we need to prompt for a new password.
401 struct smb_passwd *smb_pass = getsmbpwnam(user_name);
402 if((smb_pass != NULL) && (smb_pass->smb_passwd != NULL)) {
403 new_passwd = "XXXX"; /* Don't care. */
408 new_passwd = prompt_for_new_password(stdin_passwd_get);
411 if (!password_change(remote_machine, user_name, old_passwd, new_passwd,
412 add_user, enable_user, disable_user, set_no_password,
414 fprintf(stderr,"Failed to change password entry for %s\n", user_name);
419 printf("User %s disabled.\n", user_name);
420 } else if(enable_user) {
421 printf("User %s enabled.\n", user_name);
422 } else if (set_no_password) {
423 printf("User %s - set to no password.\n", user_name);
425 printf("Password changed for user %s\n", user_name);
431 /*************************************************************
432 handle password changing for non-root
433 *************************************************************/
434 static int process_nonroot(int argc, char *argv[])
436 struct passwd *pwd = NULL;
438 BOOL stdin_passwd_get = False;
439 char *old_passwd = NULL;
440 char *remote_machine = NULL;
441 char *user_name = NULL;
442 char *new_passwd = NULL;
444 while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
447 DEBUGLEVEL = atoi(optarg);
450 remote_machine = optarg;
453 set_line_buffering(stdin);
454 set_line_buffering(stdout);
455 set_line_buffering(stderr);
456 stdin_passwd_get = True;
474 new_passwd = argv[0];
478 pwd = getpwuid(getuid());
480 user_name = xstrdup(pwd->pw_name);
482 fprintf(stderr,"you don't exist - go away\n");
488 * A non-root user is always setting a password
489 * via a remote machine (even if that machine is
492 if (remote_machine == NULL) {
493 remote_machine = "127.0.0.1";
497 if (remote_machine != NULL) {
498 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
502 new_passwd = prompt_for_new_password(stdin_passwd_get);
506 printf("unable to get new password\n");
510 if (!password_change(remote_machine, user_name, old_passwd, new_passwd,
511 False, False, False, False, False)) {
512 fprintf(stderr,"Failed to change password for %s\n", user_name);
516 printf("Password changed for user %s\n", user_name);
522 /*********************************************************
524 **********************************************************/
525 int main(int argc, char **argv)
527 static pstring servicesf = CONFIGFILE;
531 setup_logging("smbpasswd", True);
533 charset_initialise();
535 if(!initialize_password_db()) {
536 fprintf(stderr, "Can't setup password database vectors.\n");
540 if (!lp_load(servicesf,True,False,False)) {
541 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
546 if(!get_myname(myhostname,NULL)) {
547 fprintf(stderr, "unable to get my hostname.\n");
552 * Set the machine NETBIOS name if not already
553 * set from the config file.
556 if (!*global_myname) {
558 fstrcpy(global_myname, myhostname);
559 p = strchr(global_myname, '.' );
562 strupper(global_myname);
564 codepage_initialise(lp_client_code_page());
566 /* Check the effective uid - make sure we are not setuid */
567 if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) {
568 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
573 return process_root(argc, argv);
576 return process_nonroot(argc, argv);