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 fstring global_myworkgroup
;
26 static char *prog_name
;
28 /*********************************************************
29 Print command usage on stderr and die.
30 **********************************************************/
32 static void usage(char *name
, BOOL is_root
)
35 fprintf(stderr
, "Usage is : %s [-D DEBUGLEVEL] [-a] [-d] [-m] [-n] [username] [password]\n\
36 %s: [-R <name resolve order>] [-D DEBUGLEVEL] [-j DOMAINNAME] [-r machine] [username] [password]\n%s: [-h]\n", name
, name
, name
);
38 fprintf(stderr
, "Usage is : %s [-h] [-D DEBUGLEVEL] [-r machine] [password]\n", name
);
42 /*********************************************************
44 **********************************************************/
46 static int join_domain( char *domain
, char *remote
)
48 pstring remote_machine
;
50 unsigned char orig_trust_passwd_hash
[16];
53 pstrcpy(remote_machine
, remote
? remote
: "");
54 fstrcpy(trust_passwd
, global_myname
);
55 strlower(trust_passwd
);
56 E_md4hash( (uchar
*)trust_passwd
, orig_trust_passwd_hash
);
58 /* Ensure that we are not trying to join a
59 domain if we are locally set up as a domain
62 if(lp_domain_controller() && strequal(lp_workgroup(), domain
)) {
63 fprintf(stderr
, "%s: Cannot join domain %s as we already configured as \
64 domain controller for that domain.\n", prog_name
, domain
);
69 * Create the machine account password file.
71 if(!trust_password_lock( domain
, global_myname
, True
)) {
72 fprintf(stderr
, "%s: unable to open the machine account password file for \
73 machine %s in domain %s.\n", prog_name
, global_myname
, domain
);
78 * Write the old machine account password.
81 if(!set_trust_account_password( orig_trust_passwd_hash
)) {
82 fprintf(stderr
, "%s: unable to write the machine account password for \
83 machine %s in domain %s.\n", prog_name
, global_myname
, domain
);
84 trust_password_unlock();
89 * If we are given a remote machine assume this is the PDC.
93 pstrcpy(remote_machine
, lp_passwordserver());
95 if(!*remote_machine
) {
96 fprintf(stderr
, "%s: No password server list given in smb.conf - \
97 unable to join domain.\n", prog_name
);
98 trust_password_unlock();
102 ret
= change_trust_account_password( domain
, remote_machine
);
103 trust_password_unlock();
106 trust_password_delete( domain
, global_myname
);
107 fprintf(stderr
,"%s: Unable to join domain %s.\n", prog_name
, domain
);
109 printf("%s: Joined domain %s.\n", prog_name
, domain
);
115 /*********************************************************
117 **********************************************************/
119 int main(int argc
, char **argv
)
123 extern int DEBUGLEVEL
;
129 uchar new_nt_p16
[16];
131 struct smb_passwd
*smb_pwent
;
135 BOOL is_root
= False
;
137 char *remote_machine
= NULL
;
138 BOOL add_user
= False
;
139 BOOL got_new_pass
= False
;
140 BOOL trust_account
= False
;
141 BOOL disable_user
= False
;
142 BOOL set_no_password
= False
;
143 BOOL joining_domain
= False
;
144 char *new_domain
= NULL
;
145 pstring servicesf
= CONFIGFILE
;
148 new_passwd
[0] = '\0';
151 memset(old_passwd
, '\0', sizeof(old_passwd
));
152 memset(new_passwd
, '\0', sizeof(new_passwd
));
158 setup_logging(prog_name
,True
);
160 charset_initialise();
162 if(!initialize_password_db()) {
163 fprintf(stderr
, "%s: Can't setup password database vectors.\n", prog_name
);
167 if (!lp_load(servicesf
,True
,False
,False
)) {
168 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n", prog_name
, servicesf
);
172 if(!get_myname(myhostname
,NULL
)) {
173 fprintf(stderr
, "%s: unable to get my hostname.\n", prog_name
);
178 * Set the machine NETBIOS name if not already
179 * set from the config file.
184 fstrcpy( global_myname
, myhostname
);
185 p
= strchr( global_myname
, '.' );
189 strupper( global_myname
);
191 codepage_initialise(lp_client_code_page());
193 /* Get the real uid */
196 /* Check the effective uid */
197 if ((geteuid() == 0) && (real_uid
!= 0)) {
198 fprintf(stderr
, "%s: Must *NOT* be setuid root.\n", prog_name
);
202 is_root
= (real_uid
== 0);
204 while ((ch
= getopt(argc
, argv
, "adhmnj:r:R:D:")) != EOF
) {
210 usage(prog_name
, is_root
);
216 fstrcpy(new_passwd
, "XXXXXX");
218 usage(prog_name
, is_root
);
221 DEBUGLEVEL
= atoi(optarg
);
225 set_no_password
= True
;
227 fstrcpy(new_passwd
, "NO PASSWORD");
229 usage(prog_name
, is_root
);
231 remote_machine
= optarg
;
235 lp_set_name_resolve_order(optarg
);
238 usage(prog_name
, is_root
);
241 trust_account
= True
;
243 usage(prog_name
, is_root
);
248 strupper(new_domain
);
249 joining_domain
= True
;
251 usage(prog_name
, is_root
);
255 usage(prog_name
, is_root
);
263 * Ensure add_user and either remote machine or join domain are
267 if(add_user
&& ((remote_machine
!= NULL
) || joining_domain
))
268 usage(prog_name
, True
);
271 * Deal with joining a domain.
273 if(joining_domain
&& argc
!= 0)
274 usage(prog_name
, True
);
277 return join_domain( new_domain
, remote_machine
);
283 * Deal with root - can add a user, but only locally.
290 /* If we are root we can change another's password. */
291 pstrcpy(user_name
, argv
[0]);
294 pstrcpy(user_name
, argv
[0]);
295 fstrcpy(new_passwd
, argv
[1]);
299 usage(prog_name
, True
);
305 int username_len
= strlen(user_name
);
306 if(username_len
>= sizeof(pstring
) - 1) {
307 fprintf(stderr
, "%s: machine account name too long.\n", user_name
);
311 if(user_name
[username_len
-1] != '$') {
312 user_name
[username_len
] = '$';
313 user_name
[username_len
+1] = '\0';
318 * Setup the pwd struct to point to known
319 * values for a machine account (it doesn't
320 * exist in /etc/passwd).
322 if((pwd
= Get_Pwnam(user_name
, True
)) == NULL
) {
323 fprintf(stderr
, "%s: User \"%s\" was not found in system password file.\n",
324 prog_name
, user_name
);
328 if((pwd
= getpwuid(real_uid
)) != NULL
)
329 pstrcpy( user_name
, pwd
->pw_name
);
335 fprintf(stderr
, "%s: Only root can set anothers password.\n", prog_name
);
336 usage(prog_name
, False
);
340 usage(prog_name
, False
);
343 fstrcpy(new_passwd
, argv
[0]);
347 if((pwd
= getpwuid(real_uid
)) != NULL
)
348 pstrcpy( user_name
, pwd
->pw_name
);
351 * A non-root user is always setting a password
352 * via a remote machine (even if that machine is
356 if(remote_machine
== NULL
)
357 remote_machine
= "127.0.0.1";
360 if (*user_name
== '\0') {
361 fprintf(stderr
, "%s: Unable to get a user name for password change.\n", prog_name
);
366 * If we are adding a machine account then pretend
367 * we already have the new password, we will be using
368 * the machinename as the password.
371 if(add_user
&& trust_account
) {
373 strncpy(new_passwd
, user_name
, sizeof(fstring
));
374 new_passwd
[sizeof(fstring
)-1] = '\0';
375 strlower(new_passwd
);
376 if(new_passwd
[strlen(new_passwd
)-1] == '$')
377 new_passwd
[strlen(new_passwd
)-1] = '\0';
381 * If we are root we don't ask for the old password (unless it's on a
385 if (remote_machine
!= NULL
) {
386 p
= getpass("Old SMB password:");
387 fstrcpy(old_passwd
, p
);
391 new_passwd
[0] = '\0';
393 p
= getpass("New SMB password:");
395 strncpy(new_passwd
, p
, sizeof(fstring
));
396 new_passwd
[sizeof(fstring
)-1] = '\0';
398 p
= getpass("Retype new SMB password:");
400 if (strncmp(p
, new_passwd
, sizeof(fstring
)-1))
402 fprintf(stderr
, "%s: Mismatch - password unchanged.\n", prog_name
);
407 if (new_passwd
[0] == '\0') {
408 printf("Password not set\n");
413 * Now do things differently depending on if we're changing the
414 * password on a remote machine. Remember - a normal user is
415 * always using this code, looping back to the local smbd.
418 if(remote_machine
!= NULL
) {
419 struct cli_state cli
;
422 if(!resolve_name( remote_machine
, &ip
)) {
423 fprintf(stderr
, "%s: unable to find an IP address for machine %s.\n",
424 prog_name
, remote_machine
);
428 memset(&cli
, '\0', sizeof(struct cli_state
));
430 if (!cli_initialise(&cli
) || !cli_connect(&cli
, remote_machine
, &ip
)) {
431 fprintf(stderr
, "%s: unable to connect to SMB server on machine %s. Error was : %s.\n",
432 prog_name
, remote_machine
, cli_errstr(&cli
) );
436 if (!cli_session_request(&cli
, remote_machine
, 0x20, global_myname
)) {
437 fprintf(stderr
, "%s: machine %s rejected the session setup. Error was : %s.\n",
438 prog_name
, remote_machine
, cli_errstr(&cli
) );
443 cli
.protocol
= PROTOCOL_NT1
;
445 if (!cli_negprot(&cli
)) {
446 fprintf(stderr
, "%s: machine %s rejected the negotiate protocol. Error was : %s.\n",
447 prog_name
, remote_machine
, cli_errstr(&cli
) );
452 if (!cli_session_setup(&cli
, user_name
, old_passwd
, strlen(old_passwd
),
454 fprintf(stderr
, "%s: machine %s rejected the session setup. Error was : %s.\n",
455 prog_name
, remote_machine
, cli_errstr(&cli
) );
460 if (!cli_send_tconX(&cli
, "IPC$", "IPC", "", 1)) {
461 fprintf(stderr
, "%s: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
462 prog_name
, remote_machine
, cli_errstr(&cli
) );
467 if(!cli_oem_change_password(&cli
, user_name
, new_passwd
, old_passwd
)) {
468 fprintf(stderr
, "%s: machine %s rejected the password change: Error was : %s.\n",
469 prog_name
, remote_machine
, cli_errstr(&cli
) );
479 * Check for a machine account.
482 if(trust_account
&& !pwd
) {
483 fprintf(stderr
, "%s: User %s does not exist in system password file \
484 (usually /etc/passwd). Cannot add machine account without a valid system user.\n",
485 prog_name
, user_name
);
489 /* Calculate the MD4 hash (NT compatible) of the new password. */
491 memset(new_nt_p16
, '\0', 16);
492 E_md4hash((uchar
*) new_passwd
, new_nt_p16
);
494 /* Mangle the password into Lanman format */
495 new_passwd
[14] = '\0';
496 strupper(new_passwd
);
499 * Calculate the SMB (lanman) hash functions of the new password.
502 memset(new_p16
, '\0', 16);
503 E_P16((uchar
*) new_passwd
, new_p16
);
506 * Open the smbpaswd file.
508 vp
= startsmbpwent(True
);
509 if (!vp
&& errno
== ENOENT
) {
510 fp
= fopen(lp_smb_passwd_file(), "w");
512 fprintf(fp
, "# Samba SMB password file\n");
514 vp
= startsmbpwent(True
);
519 fprintf(stderr
, "%s: Failed to open password file %s.\n",
520 prog_name
, lp_smb_passwd_file());
526 /* Get the smb passwd entry for this user */
527 smb_pwent
= getsmbpwnam(user_name
);
528 if (smb_pwent
== NULL
) {
529 if(add_user
== False
) {
530 fprintf(stderr
, "%s: Failed to find entry for user %s.\n",
531 prog_name
, pwd
->pw_name
);
536 /* Create a new smb passwd entry and set it to the given password. */
538 struct smb_passwd new_smb_pwent
;
540 new_smb_pwent
.smb_userid
= pwd
->pw_uid
;
541 new_smb_pwent
.smb_name
= pwd
->pw_name
;
542 new_smb_pwent
.smb_passwd
= NULL
;
543 new_smb_pwent
.smb_nt_passwd
= NULL
;
544 new_smb_pwent
.acct_ctrl
= (trust_account
? ACB_WSTRUST
: ACB_NORMAL
);
547 new_smb_pwent
.acct_ctrl
|= ACB_DISABLED
;
548 } else if (set_no_password
) {
549 new_smb_pwent
.acct_ctrl
|= ACB_PWNOTREQ
;
551 new_smb_pwent
.smb_passwd
= new_p16
;
552 new_smb_pwent
.smb_nt_passwd
= new_nt_p16
;
555 if(add_smbpwd_entry(&new_smb_pwent
) == False
) {
556 fprintf(stderr
, "%s: Failed to add entry for user %s.\n",
557 prog_name
, pwd
->pw_name
);
563 printf("%s: Added user %s.\n", prog_name
, user_name
);
567 /* the entry already existed */
572 * We are root - just write the new password
573 * and the valid last change time.
578 * This currently won't work as it means changing
579 * the length of the record. JRA.
581 smb_pwent
->acct_ctrl
|= ACB_DISABLED
;
582 smb_pwent
->smb_passwd
= NULL
;
583 smb_pwent
->smb_nt_passwd
= NULL
;
584 } else if (set_no_password
) {
586 * This currently won't work as it means changing
587 * the length of the record. JRA.
589 smb_pwent
->acct_ctrl
|= ACB_PWNOTREQ
;
590 smb_pwent
->smb_passwd
= NULL
;
591 smb_pwent
->smb_nt_passwd
= NULL
;
593 smb_pwent
->smb_passwd
= new_p16
;
594 smb_pwent
->smb_nt_passwd
= new_nt_p16
;
597 if(mod_smbpwd_entry(smb_pwent
,True
) == False
) {
598 fprintf(stderr
, "%s: Failed to modify entry for user %s.\n",
599 prog_name
, pwd
->pw_name
);
606 printf("User %s disabled.\n", user_name
);
607 else if (set_no_password
)
608 printf("User %s - set to no password.\n", user_name
);
610 printf("Password changed for user %s.\n", user_name
);