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.
23 extern pstring myhostname;
24 extern pstring global_myname;
25 extern fstring global_myworkgroup;
27 static char *prog_name;
29 /*********************************************************
30 Print command usage on stderr and die.
31 **********************************************************/
33 static void usage(char *name, BOOL is_root)
36 fprintf(stderr, "Usage is : %s [-D DEBUGLEVEL] [-a] [-d] [-e] [-m] [-n] [username] [password]\n\
37 %s: [-R <name resolve order>] [-D DEBUGLEVEL] [-j DOMAINNAME] [-r machine] [-U remote_username] [username] [password]\n%s: [-h]\n", name, name, name);
39 fprintf(stderr, "Usage is : %s [-h] [-D DEBUGLEVEL] [-r machine] [-U remote_username] [password]\n", name);
43 /*********************************************************
45 **********************************************************/
47 static int join_domain( char *domain, char *remote)
49 pstring remote_machine;
51 unsigned char orig_trust_passwd_hash[16];
54 pstrcpy(remote_machine, remote ? remote : "");
55 fstrcpy(trust_passwd, global_myname);
56 strlower(trust_passwd);
57 E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
59 /* Ensure that we are not trying to join a
60 domain if we are locally set up as a domain
63 if(lp_domain_controller() && strequal(lp_workgroup(), domain)) {
64 fprintf(stderr, "%s: Cannot join domain %s as we already configured as \
65 domain controller for that domain.\n", prog_name, domain);
70 * Create the machine account password file.
72 if(!trust_password_lock( domain, global_myname, True)) {
73 fprintf(stderr, "%s: unable to open the machine account password file for \
74 machine %s in domain %s.\n", prog_name, global_myname, domain);
79 * Write the old machine account password.
82 if(!set_trust_account_password( orig_trust_passwd_hash)) {
83 fprintf(stderr, "%s: unable to write the machine account password for \
84 machine %s in domain %s.\n", prog_name, global_myname, domain);
85 trust_password_unlock();
90 * If we are given a remote machine assume this is the PDC.
94 pstrcpy(remote_machine, lp_passwordserver());
96 if(!*remote_machine) {
97 fprintf(stderr, "%s: No password server list given in smb.conf - \
98 unable to join domain.\n", prog_name);
99 trust_password_unlock();
103 ret = change_trust_account_password( domain, remote_machine);
104 trust_password_unlock();
107 trust_password_delete( domain, global_myname);
108 fprintf(stderr,"%s: Unable to join domain %s.\n", prog_name, domain);
110 printf("%s: Joined domain %s.\n", prog_name, domain);
116 /*************************************************************
117 Utility function to prompt for new password.
118 *************************************************************/
120 static void prompt_for_new_password(char *new_passwd)
124 new_passwd[0] = '\0';
126 p = getpass("New SMB password:");
128 strncpy(new_passwd, p, sizeof(fstring));
129 new_passwd[sizeof(fstring)-1] = '\0';
131 p = getpass("Retype new SMB password:");
133 if (strncmp(p, new_passwd, sizeof(fstring)-1))
135 fprintf(stderr, "%s: Mismatch - password unchanged.\n", prog_name);
139 if (new_passwd[0] == '\0') {
140 printf("Password not set\n");
145 /*********************************************************
147 **********************************************************/
149 int main(int argc, char **argv)
153 extern int DEBUGLEVEL;
155 struct passwd *pwd = NULL;
159 uchar new_nt_p16[16];
161 struct smb_passwd *smb_pwent;
165 BOOL is_root = False;
167 BOOL remote_user_name = False;
168 char *remote_machine = NULL;
169 BOOL add_user = False;
170 BOOL got_new_pass = False;
171 BOOL trust_account = False;
172 BOOL disable_user = False;
173 BOOL enable_user = False;
174 BOOL set_no_password = False;
175 BOOL joining_domain = False;
176 char *new_domain = NULL;
177 pstring servicesf = CONFIGFILE;
179 struct nmb_name calling, called;
182 new_passwd[0] = '\0';
185 memset(old_passwd, '\0', sizeof(old_passwd));
186 memset(new_passwd, '\0', sizeof(new_passwd));
192 setup_logging(prog_name,True);
194 charset_initialise();
196 if(!initialize_password_db()) {
197 fprintf(stderr, "%s: Can't setup password database vectors.\n", prog_name);
201 if (!lp_load(servicesf,True,False,False)) {
202 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", prog_name, servicesf);
206 if(!get_myname(myhostname,NULL)) {
207 fprintf(stderr, "%s: unable to get my hostname.\n", prog_name );
212 * Set the machine NETBIOS name if not already
213 * set from the config file.
218 fstrcpy( global_myname, myhostname );
219 p = strchr( global_myname, '.' );
223 strupper( global_myname );
225 codepage_initialise(lp_client_code_page());
227 /* Get the real uid */
230 /* Check the effective uid */
231 if ((geteuid() == 0) && (real_uid != 0)) {
232 fprintf(stderr, "%s: Must *NOT* be setuid root.\n", prog_name);
236 is_root = (real_uid == 0);
238 while ((ch = getopt(argc, argv, "adehmnj:r:R:D:U:")) != EOF) {
244 usage(prog_name, is_root);
250 fstrcpy(new_passwd, "XXXXXX");
252 usage(prog_name, is_root);
258 fstrcpy(new_passwd, "XXXXXX");
260 usage(prog_name, is_root);
263 DEBUGLEVEL = atoi(optarg);
267 set_no_password = True;
269 fstrcpy(new_passwd, "NO PASSWORD");
271 usage(prog_name, is_root);
273 remote_machine = optarg;
277 lp_set_name_resolve_order(optarg);
280 usage(prog_name, is_root);
283 trust_account = True;
285 usage(prog_name, is_root);
290 strupper(new_domain);
291 joining_domain = True;
293 usage(prog_name, is_root);
296 remote_user_name = True;
297 pstrcpy(user_name, optarg);
301 usage(prog_name, is_root);
308 if (!is_root && remote_user_name && !remote_machine) {
309 fprintf(stderr, "%s: You can only use -U with -r.\n", prog_name);
310 usage(prog_name, False);
314 * Ensure add_user and either remote machine or join domain are
318 if(add_user && ((remote_machine != NULL) || joining_domain))
319 usage(prog_name, True);
322 * Deal with joining a domain.
324 if(joining_domain && argc != 0)
325 usage(prog_name, True);
328 return join_domain( new_domain, remote_machine);
334 * Deal with root - can add a user, but only locally.
341 /* If we are root we can change another's password. */
342 pstrcpy(user_name, argv[0]);
345 pstrcpy(user_name, argv[0]);
346 fstrcpy(new_passwd, argv[1]);
350 usage(prog_name, True);
356 int username_len = strlen(user_name);
357 if(username_len >= sizeof(pstring) - 1) {
358 fprintf(stderr, "%s: machine account name too long.\n", user_name);
362 if(user_name[username_len-1] != '$') {
363 user_name[username_len] = '$';
364 user_name[username_len+1] = '\0';
368 if(!remote_machine && ((pwd = Get_Pwnam(user_name, True)) == NULL)) {
369 fprintf(stderr, "%s: User \"%s\" was not found in system password file.\n",
370 prog_name, user_name);
374 if((pwd = getpwuid(real_uid)) != NULL)
375 pstrcpy( user_name, pwd->pw_name);
381 fprintf(stderr, "%s: Only root can add a user.\n", prog_name);
382 usage(prog_name, False);
386 fprintf(stderr, "%s: Only root can disable a user.\n", prog_name);
387 usage(prog_name, False);
391 fprintf(stderr, "%s: Only root can enable a user.\n", prog_name);
392 usage(prog_name, False);
396 usage(prog_name, False);
399 fstrcpy(new_passwd, argv[0]);
403 if(!remote_user_name && ((pwd = getpwuid(real_uid)) != NULL))
404 pstrcpy( user_name, pwd->pw_name);
407 * A non-root user is always setting a password
408 * via a remote machine (even if that machine is
412 if(remote_machine == NULL)
413 remote_machine = "127.0.0.1";
416 if (*user_name == '\0') {
417 fprintf(stderr, "%s: Unable to get a user name for password change.\n", prog_name);
422 * If we are adding a machine account then pretend
423 * we already have the new password, we will be using
424 * the machinename as the password.
427 if(add_user && trust_account) {
429 strncpy(new_passwd, user_name, sizeof(fstring));
430 new_passwd[sizeof(fstring)-1] = '\0';
431 strlower(new_passwd);
432 if(new_passwd[strlen(new_passwd)-1] == '$')
433 new_passwd[strlen(new_passwd)-1] = '\0';
437 * If we are root we don't ask for the old password (unless it's on a
441 if (remote_machine != NULL) {
442 p = getpass("Old SMB password:");
443 fstrcpy(old_passwd, p);
447 prompt_for_new_password(new_passwd);
449 if (new_passwd[0] == '\0') {
450 printf("Password not set\n");
455 * Now do things differently depending on if we're changing the
456 * password on a remote machine. Remember - a normal user is
457 * always using this code, looping back to the local smbd.
460 if(remote_machine != NULL) {
461 struct cli_state cli;
464 if(!resolve_name( remote_machine, &ip)) {
465 fprintf(stderr, "%s: unable to find an IP address for machine %s.\n",
466 prog_name, remote_machine );
472 if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
473 fprintf(stderr, "%s: unable to connect to SMB server on machine %s. Error was : %s.\n",
474 prog_name, remote_machine, cli_errstr(&cli) );
478 make_nmb_name(&calling, global_myname , 0x0 , scope);
479 make_nmb_name(&called , remote_machine, 0x20, scope);
481 if (!cli_session_request(&cli, &calling, &called))
483 fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
484 prog_name, remote_machine, cli_errstr(&cli) );
489 cli.protocol = PROTOCOL_NT1;
491 if (!cli_negprot(&cli)) {
492 fprintf(stderr, "%s: machine %s rejected the negotiate protocol. Error was : %s.\n",
493 prog_name, remote_machine, cli_errstr(&cli) );
498 if (!cli_session_setup(&cli, user_name, old_passwd, strlen(old_passwd),
500 fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
501 prog_name, remote_machine, cli_errstr(&cli) );
506 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
507 fprintf(stderr, "%s: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
508 prog_name, remote_machine, cli_errstr(&cli) );
513 if(!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
514 fprintf(stderr, "%s: machine %s rejected the password change: Error was : %s.\n",
515 prog_name, remote_machine, cli_errstr(&cli) );
525 * Check for a machine account.
528 if(trust_account && !pwd) {
529 fprintf(stderr, "%s: User %s does not exist in system password file \
530 (usually /etc/passwd). Cannot add machine account without a valid system user.\n",
531 prog_name, user_name);
535 /* Calculate the MD4 hash (NT compatible) of the new password. */
537 nt_lm_owf_gen( new_passwd, new_nt_p16, new_p16);
540 * Open the smbpaswd file.
542 vp = startsmbpwent(True);
543 if (!vp && errno == ENOENT) {
544 fprintf(stderr,"%s: smbpasswd file did not exist - attempting to create it.\n", prog_name);
545 fp = fopen(lp_smb_passwd_file(), "w");
547 fprintf(fp, "# Samba SMB password file\n");
549 vp = startsmbpwent(True);
554 fprintf(stderr, "%s: Failed to open password file %s.\n",
555 prog_name, lp_smb_passwd_file());
561 /* Get the smb passwd entry for this user */
562 smb_pwent = getsmbpwnam(user_name);
563 if (smb_pwent == NULL) {
564 if(add_user == False) {
565 fprintf(stderr, "%s: Failed to find entry for user %s.\n",
566 prog_name, pwd->pw_name);
571 /* Create a new smb passwd entry and set it to the given password. */
573 struct smb_passwd new_smb_pwent;
575 new_smb_pwent.smb_userid = pwd->pw_uid;
576 new_smb_pwent.smb_name = pwd->pw_name;
577 new_smb_pwent.smb_passwd = NULL;
578 new_smb_pwent.smb_nt_passwd = NULL;
579 new_smb_pwent.acct_ctrl = (trust_account ? ACB_WSTRUST : ACB_NORMAL);
582 new_smb_pwent.acct_ctrl |= ACB_DISABLED;
583 } else if (set_no_password) {
584 new_smb_pwent.acct_ctrl |= ACB_PWNOTREQ;
586 new_smb_pwent.smb_passwd = new_p16;
587 new_smb_pwent.smb_nt_passwd = new_nt_p16;
590 if(add_smbpwd_entry(&new_smb_pwent) == False) {
591 fprintf(stderr, "%s: Failed to add entry for user %s.\n",
592 prog_name, pwd->pw_name);
598 printf("%s: Added user %s.\n", prog_name, user_name);
602 /* the entry already existed */
607 * We are root - just write the new password
608 * and the valid last change time.
612 smb_pwent->acct_ctrl |= ACB_DISABLED;
613 else if (enable_user) {
614 if(smb_pwent->smb_passwd == NULL) {
615 prompt_for_new_password(new_passwd);
616 nt_lm_owf_gen( new_passwd, new_nt_p16, new_p16);
617 smb_pwent->smb_passwd = new_p16;
618 smb_pwent->smb_nt_passwd = new_nt_p16;
620 smb_pwent->acct_ctrl &= ~ACB_DISABLED;
621 } else if (set_no_password) {
622 smb_pwent->acct_ctrl |= ACB_PWNOTREQ;
623 /* This is needed to preserve ACB_PWNOTREQ in mod_smbfilepwd_entry */
624 smb_pwent->smb_passwd = NULL;
625 smb_pwent->smb_nt_passwd = NULL;
627 smb_pwent->acct_ctrl &= ~ACB_PWNOTREQ;
628 smb_pwent->smb_passwd = new_p16;
629 smb_pwent->smb_nt_passwd = new_nt_p16;
632 if(mod_smbpwd_entry(smb_pwent,True) == False) {
633 fprintf(stderr, "%s: Failed to modify entry for user %s.\n",
634 prog_name, pwd->pw_name);
641 printf("User %s disabled.\n", user_name);
643 printf("User %s enabled.\n", user_name);
644 else if (set_no_password)
645 printf("User %s - set to no password.\n", user_name);
647 printf("Password changed for user %s.\n", user_name);