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;
24 /*********************************************************
25 Print command usage on stderr and die.
26 **********************************************************/
28 static void usage(char *name, BOOL is_root)
31 fprintf(stderr, "Usage is : %s [-D DEBUGLEVEL] [-a] [-d] [-m] [-n] [username] [password]\n\
32 %s: [-R <name resolve order>] [-D DEBUGLEVEL] [-r machine] [username] [password]\n%s: [-h]\n", name, name, name);
34 fprintf(stderr, "Usage is : %s [-h] [-D DEBUGLEVEL] [-r machine] [password]\n", name);
38 /*********************************************************
40 **********************************************************/
42 int main(int argc, char **argv)
46 extern int DEBUGLEVEL;
55 struct smb_passwd *smb_pwent;
61 char *remote_machine = NULL;
62 BOOL add_user = False;
63 BOOL got_new_pass = False;
64 BOOL machine_account = False;
65 BOOL disable_user = False;
66 BOOL set_no_password = False;
67 pstring servicesf = CONFIGFILE;
73 memset(old_passwd, '\0', sizeof(old_passwd));
74 memset(new_passwd, '\0', sizeof(new_passwd));
80 setup_logging(prog_name,True);
84 if (!lp_load(servicesf,True,False,False)) {
85 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", prog_name, servicesf);
88 codepage_initialise(lp_client_code_page());
90 /* Get the real uid */
93 /* Check the effective uid */
94 if ((geteuid() == 0) && (real_uid != 0)) {
95 fprintf(stderr, "%s: Must *NOT* be setuid root.\n", prog_name);
99 is_root = (real_uid == 0);
101 while ((ch = getopt(argc, argv, "adhmnr:R:D:")) != EOF) {
107 usage(prog_name, is_root);
113 strcpy(new_passwd, "XXXXXX");
115 usage(prog_name, is_root);
118 DEBUGLEVEL = atoi(optarg);
122 set_no_password = True;
124 strcpy(new_passwd, "NO PASSWORD");
126 usage(prog_name, is_root);
128 remote_machine = optarg;
132 lp_set_name_resolve_order(optarg);
135 usage(prog_name, is_root);
138 machine_account = True;
140 usage(prog_name, is_root);
144 usage(prog_name, is_root);
152 * Ensure add_user and remote machine are
155 if(add_user && (remote_machine != NULL))
156 usage(prog_name, True);
161 * Deal with root - can add a user, but only locally.
168 /* If we are root we can change another's password. */
169 pstrcpy(user_name, argv[0]);
172 pstrcpy(user_name, argv[0]);
173 fstrcpy(new_passwd, argv[1]);
177 usage(prog_name, True);
182 if(machine_account) {
183 int username_len = strlen(user_name);
184 if(username_len >= sizeof(pstring) - 1) {
185 fprintf(stderr, "%s: machine account name too long.\n", user_name);
189 if(user_name[username_len-1] != '$') {
190 user_name[username_len] = '$';
191 user_name[username_len+1] = '\0';
196 * Setup the pwd struct to point to known
197 * values for a machine account (it doesn't
198 * exist in /etc/passwd).
200 if((pwd = getpwnam(user_name)) == NULL) {
201 fprintf(stderr, "%s: User \"%s\" was not found in system password file.\n",
202 prog_name, user_name);
206 if((pwd = getpwuid(real_uid)) != NULL)
207 pstrcpy( user_name, pwd->pw_name);
213 fprintf(stderr, "%s: Only root can set anothers password.\n", prog_name);
214 usage(prog_name, False);
218 usage(prog_name, False);
221 fstrcpy(new_passwd, argv[0]);
225 if((pwd = getpwuid(real_uid)) != NULL)
226 pstrcpy( user_name, pwd->pw_name);
229 * A non-root user is always setting a password
230 * via a remote machine (even if that machine is
234 if(remote_machine == NULL)
235 remote_machine = "127.0.0.1";
238 if (*user_name == '\0') {
239 fprintf(stderr, "%s: Unable to get a user name for password change.\n", prog_name);
244 * If we are adding a machine account then pretend
245 * we already have the new password, we will be using
246 * the machinename as the password.
249 if(add_user && machine_account) {
251 strncpy(new_passwd, user_name, sizeof(fstring));
252 new_passwd[sizeof(fstring)-1] = '\0';
253 strlower(new_passwd);
254 if(new_passwd[strlen(new_passwd)-1] == '$')
255 new_passwd[strlen(new_passwd)-1] = '\0';
259 * If we are root we don't ask for the old password (unless it's on a
263 if (remote_machine != NULL) {
264 p = getpass("Old SMB password:");
265 fstrcpy(old_passwd, p);
269 new_passwd[0] = '\0';
271 p = getpass("New SMB password:");
273 strncpy(new_passwd, p, sizeof(fstring));
274 new_passwd[sizeof(fstring)-1] = '\0';
276 p = getpass("Retype new SMB password:");
278 if (strncmp(p, new_passwd, sizeof(fstring)-1))
280 fprintf(stderr, "%s: Mismatch - password unchanged.\n", prog_name);
285 if (new_passwd[0] == '\0') {
286 printf("Password not set\n");
291 * Now do things differently depending on if we're changing the
292 * password on a remote machine. Remember - a normal user is
293 * always using this code, looping back to the local smbd.
296 if(remote_machine != NULL) {
297 struct cli_state cli;
300 if(get_myname(global_myname,NULL) == False) {
301 fprintf(stderr, "%s: unable to get my hostname.\n", prog_name );
305 if(!resolve_name( remote_machine, &ip)) {
306 fprintf(stderr, "%s: unable to find an IP address for machine %s.\n",
307 prog_name, remote_machine );
311 memset(&cli, '\0', sizeof(struct cli_state));
313 if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
314 fprintf(stderr, "%s: unable to connect to SMB server on machine %s. Error was : %s.\n",
315 prog_name, remote_machine, cli_errstr(&cli) );
319 if (!cli_session_request(&cli, remote_machine, 0x20, global_myname)) {
320 fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
321 prog_name, remote_machine, cli_errstr(&cli) );
326 cli.protocol = PROTOCOL_NT1;
328 if (!cli_negprot(&cli)) {
329 fprintf(stderr, "%s: machine %s rejected the negotiate protocol. Error was : %s.\n",
330 prog_name, remote_machine, cli_errstr(&cli) );
335 if (!cli_session_setup(&cli, user_name, old_passwd, strlen(old_passwd),
337 fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
338 prog_name, remote_machine, cli_errstr(&cli) );
343 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
344 fprintf(stderr, "%s: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
345 prog_name, remote_machine, cli_errstr(&cli) );
350 if(!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
351 fprintf(stderr, "%s: machine %s rejected the password change: Error was : %s.\n",
352 prog_name, remote_machine, cli_errstr(&cli) );
362 * Check for a machine account.
365 if(machine_account && !pwd) {
366 fprintf(stderr, "%s: User %s does not exist in system password file \
367 (usually /etc/passwd). Cannot add machine account without a valid system user.\n",
368 prog_name, user_name);
372 /* Calculate the MD4 hash (NT compatible) of the new password. */
374 memset(new_nt_p16, '\0', 16);
375 E_md4hash((uchar *) new_passwd, new_nt_p16);
377 /* Mangle the password into Lanman format */
378 new_passwd[14] = '\0';
379 strupper(new_passwd);
382 * Calculate the SMB (lanman) hash functions of the new password.
385 memset(new_p16, '\0', 16);
386 E_P16((uchar *) new_passwd, new_p16);
389 * Open the smbpaswd file.
391 vp = startsmbpwent(True);
392 if (!vp && errno == ENOENT) {
393 fp = fopen(lp_smb_passwd_file(), "w");
395 fprintf(fp, "# Samba SMB password file\n");
397 vp = startsmbpwent(True);
402 fprintf(stderr, "%s: Failed to open password file %s.\n",
403 prog_name, lp_smb_passwd_file());
409 /* Get the smb passwd entry for this user */
410 smb_pwent = getsmbpwnam(user_name);
411 if (smb_pwent == NULL) {
412 if(add_user == False) {
413 fprintf(stderr, "%s: Failed to find entry for user %s.\n",
414 prog_name, pwd->pw_name);
419 /* Create a new smb passwd entry and set it to the given password. */
421 struct smb_passwd new_smb_pwent;
423 new_smb_pwent.smb_userid = pwd->pw_uid;
424 new_smb_pwent.smb_name = pwd->pw_name;
425 new_smb_pwent.smb_passwd = NULL;
426 new_smb_pwent.smb_nt_passwd = NULL;
427 new_smb_pwent.acct_ctrl = (machine_account ? ACB_WSTRUST : ACB_NORMAL);
430 new_smb_pwent.acct_ctrl |= ACB_DISABLED;
431 } else if (set_no_password) {
432 new_smb_pwent.acct_ctrl |= ACB_PWNOTREQ;
434 new_smb_pwent.smb_passwd = new_p16;
435 new_smb_pwent.smb_nt_passwd = new_nt_p16;
438 if(add_smbpwd_entry(&new_smb_pwent) == False) {
439 fprintf(stderr, "%s: Failed to add entry for user %s.\n",
440 prog_name, pwd->pw_name);
446 printf("%s: Added user %s.\n", prog_name, user_name);
450 /* the entry already existed */
455 * We are root - just write the new password
456 * and the valid last change time.
461 * This currently won't work as it means changing
462 * the length of the record. JRA.
464 smb_pwent->acct_ctrl |= ACB_DISABLED;
465 smb_pwent->smb_passwd = NULL;
466 smb_pwent->smb_nt_passwd = NULL;
467 } else if (set_no_password) {
469 * This currently won't work as it means changing
470 * the length of the record. JRA.
472 smb_pwent->acct_ctrl |= ACB_PWNOTREQ;
473 smb_pwent->smb_passwd = NULL;
474 smb_pwent->smb_nt_passwd = NULL;
476 smb_pwent->smb_passwd = new_p16;
477 smb_pwent->smb_nt_passwd = new_nt_p16;
480 if(mod_smbpwd_entry(smb_pwent) == False) {
481 fprintf(stderr, "%s: Failed to modify entry for user %s.\n",
482 prog_name, pwd->pw_name);
489 printf("User %s disabled.\n", user_name);
490 else if (set_no_password)
491 printf("User %s - set to no password.\n", user_name);
493 printf("Password changed for user %s.\n", user_name);