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 * Password changing error codes.
30 } pw_change_errmap[] =
32 {5, "User has insufficient privilege" },
33 {86, "The specified password is invalid" },
34 {2226, "Operation only permitted on a Primary Domain Controller" },
35 {2242, "The password of this user has expired." },
36 {2243, "The password of this user cannot change." },
37 {2244, "This password cannot be used now (password history conflict)." },
38 {2245, "The password is shorter than required." },
39 {2246, "The password of this user is too recent to change."},
43 /******************************************************
44 Return an error message for a remote password change.
45 *******************************************************/
47 char *get_error_message(struct cli_state *cli)
49 static fstring error_message;
55 * Errors are of two kinds - smb errors,
56 * dealt with by cli_errstr, and rap
57 * errors, whose error code is in cli.error.
60 cli_error(cli, &errclass, &errnum);
62 return cli_errstr(cli);
64 sprintf(error_message, "code %d", cli->error);
66 for(i = 0; pw_change_errmap[i].message != NULL; i++) {
67 if (pw_change_errmap[i].err == cli->error) {
68 fstrcpy( error_message, pw_change_errmap[i].message);
76 /*********************************************************
77 Print command usage on stderr and die.
78 **********************************************************/
80 static void usage(char *name, BOOL is_root)
83 fprintf(stderr, "Usage is : %s [-D DEBUGLEVEL] [-a] [-d] [-m] [-n] [username] [password]\n\
84 %s: [-R <name resolve order>] [-D DEBUGLEVEL] [-r machine] [username] [password]\n%s: [-h]\n", name, name, name);
86 fprintf(stderr, "Usage is : %s [-h] [-D DEBUGLEVEL] [-r machine] [password]\n", name);
90 /*********************************************************
92 **********************************************************/
94 int main(int argc, char **argv)
98 extern int DEBUGLEVEL;
105 uchar new_nt_p16[16];
107 struct smb_passwd *smb_pwent;
111 BOOL is_root = False;
113 char *remote_machine = NULL;
114 BOOL add_user = False;
115 BOOL got_new_pass = False;
116 BOOL machine_account = False;
117 BOOL disable_user = False;
118 BOOL set_no_password = False;
119 pstring servicesf = CONFIGFILE;
122 new_passwd[0] = '\0';
125 memset(old_passwd, '\0', sizeof(old_passwd));
126 memset(new_passwd, '\0', sizeof(new_passwd));
132 setup_logging(prog_name,True);
134 charset_initialise();
136 if (!lp_load(servicesf,True,False,False)) {
137 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", prog_name, servicesf);
140 codepage_initialise(lp_client_code_page());
142 /* Get the real uid */
145 /* Check the effective uid */
146 if ((geteuid() == 0) && (real_uid != 0)) {
147 fprintf(stderr, "%s: Must *NOT* be setuid root.\n", prog_name);
151 is_root = (real_uid == 0);
153 while ((ch = getopt(argc, argv, "adhmnr:R:D:")) != EOF) {
159 usage(prog_name, is_root);
165 strcpy(new_passwd, "XXXXXX");
167 usage(prog_name, is_root);
170 DEBUGLEVEL = atoi(optarg);
174 set_no_password = True;
176 strcpy(new_passwd, "NO PASSWORD");
178 usage(prog_name, is_root);
180 remote_machine = optarg;
184 lp_set_name_resolve_order(optarg);
187 usage(prog_name, is_root);
190 machine_account = True;
192 usage(prog_name, is_root);
196 usage(prog_name, is_root);
204 * Ensure add_user and remote machine are
207 if(add_user && (remote_machine != NULL))
208 usage(prog_name, True);
213 * Deal with root - can add a user, but only locally.
220 /* If we are root we can change another's password. */
221 pstrcpy(user_name, argv[0]);
224 pstrcpy(user_name, argv[0]);
225 fstrcpy(new_passwd, argv[1]);
229 usage(prog_name, True);
234 if(machine_account) {
235 int username_len = strlen(user_name);
236 if(username_len >= sizeof(pstring) - 1) {
237 fprintf(stderr, "%s: machine account name too long.\n", user_name);
241 if(user_name[username_len-1] != '$') {
242 user_name[username_len] = '$';
243 user_name[username_len+1] = '\0';
248 * Setup the pwd struct to point to known
249 * values for a machine account (it doesn't
250 * exist in /etc/passwd).
252 if((pwd = getpwnam(user_name)) == NULL) {
253 fprintf(stderr, "%s: User \"%s\" was not found in system password file.\n",
254 prog_name, user_name);
258 if((pwd = getpwuid(real_uid)) != NULL)
259 pstrcpy( user_name, pwd->pw_name);
265 fprintf(stderr, "%s: Only root can set anothers password.\n", prog_name);
266 usage(prog_name, False);
270 usage(prog_name, False);
273 fstrcpy(new_passwd, argv[0]);
277 if((pwd = getpwuid(real_uid)) != NULL)
278 pstrcpy( user_name, pwd->pw_name);
281 * A non-root user is always setting a password
282 * via a remote machine (even if that machine is
286 if(remote_machine == NULL)
287 remote_machine = "127.0.0.1";
290 if (*user_name == '\0') {
291 fprintf(stderr, "%s: Unable to get a user name for password change.\n", prog_name);
296 * If we are adding a machine account then pretend
297 * we already have the new password, we will be using
298 * the machinename as the password.
301 if(add_user && machine_account) {
303 strncpy(new_passwd, user_name, sizeof(fstring));
304 new_passwd[sizeof(fstring)-1] = '\0';
305 strlower(new_passwd);
306 if(new_passwd[strlen(new_passwd)-1] == '$')
307 new_passwd[strlen(new_passwd)-1] = '\0';
311 * If we are root we don't ask for the old password (unless it's on a
315 if (remote_machine != NULL) {
316 p = getpass("Old SMB password:");
317 fstrcpy(old_passwd, p);
321 new_passwd[0] = '\0';
323 p = getpass("New SMB password:");
325 strncpy(new_passwd, p, sizeof(fstring));
326 new_passwd[sizeof(fstring)-1] = '\0';
328 p = getpass("Retype new SMB password:");
330 if (strncmp(p, new_passwd, sizeof(fstring)-1))
332 fprintf(stderr, "%s: Mismatch - password unchanged.\n", prog_name);
337 if (new_passwd[0] == '\0') {
338 printf("Password not set\n");
343 * Now do things differently depending on if we're changing the
344 * password on a remote machine. Remember - a normal user is
345 * always using this code, looping back to the local smbd.
348 if(remote_machine != NULL) {
349 struct cli_state cli;
353 if(get_myname(myname,NULL) == False) {
354 fprintf(stderr, "%s: unable to get my hostname.\n", prog_name );
358 if(!resolve_name( remote_machine, &ip)) {
359 fprintf(stderr, "%s: unable to find an IP address for machine %s.\n",
360 prog_name, remote_machine );
364 memset(&cli, '\0', sizeof(struct cli_state));
366 if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
367 fprintf(stderr, "%s: unable to connect to SMB server on machine %s. Error was : %s.\n",
368 prog_name, remote_machine, get_error_message(&cli) );
372 if (!cli_session_request(&cli, remote_machine, 0x20, myname)) {
373 fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
374 prog_name, remote_machine, get_error_message(&cli) );
379 cli.protocol = PROTOCOL_NT1;
381 if (!cli_negprot(&cli)) {
382 fprintf(stderr, "%s: machine %s rejected the negotiate protocol. Error was : %s.\n",
383 prog_name, remote_machine, get_error_message(&cli) );
388 if (!cli_session_setup(&cli, user_name, old_passwd, strlen(old_passwd),
390 fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
391 prog_name, remote_machine, get_error_message(&cli) );
396 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
397 fprintf(stderr, "%s: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
398 prog_name, remote_machine, get_error_message(&cli) );
403 if(!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
404 fprintf(stderr, "%s: machine %s rejected the password change: Error was : %s.\n",
405 prog_name, remote_machine, get_error_message(&cli) );
415 * Check for a machine account.
418 if(machine_account && !pwd) {
419 fprintf(stderr, "%s: User %s does not exist in system password file \
420 (usually /etc/passwd). Cannot add machine account without a valid system user.\n",
421 prog_name, user_name);
425 /* Calculate the MD4 hash (NT compatible) of the new password. */
427 memset(new_nt_p16, '\0', 16);
428 E_md4hash((uchar *) new_passwd, new_nt_p16);
430 /* Mangle the password into Lanman format */
431 new_passwd[14] = '\0';
432 strupper(new_passwd);
435 * Calculate the SMB (lanman) hash functions of the new password.
438 memset(new_p16, '\0', 16);
439 E_P16((uchar *) new_passwd, new_p16);
442 * Open the smbpaswd file.
444 vp = startsmbpwent(True);
445 if (!vp && errno == ENOENT) {
446 fp = fopen(lp_smb_passwd_file(), "w");
448 fprintf(fp, "# Samba SMB password file\n");
450 vp = startsmbpwent(True);
455 fprintf(stderr, "%s: Failed to open password file %s.\n",
456 prog_name, lp_smb_passwd_file());
462 /* Get the smb passwd entry for this user */
463 smb_pwent = getsmbpwnam(user_name);
464 if (smb_pwent == NULL) {
465 if(add_user == False) {
466 fprintf(stderr, "%s: Failed to find entry for user %s.\n",
467 prog_name, pwd->pw_name);
472 /* Create a new smb passwd entry and set it to the given password. */
474 struct smb_passwd new_smb_pwent;
476 new_smb_pwent.smb_userid = pwd->pw_uid;
477 new_smb_pwent.smb_name = pwd->pw_name;
478 new_smb_pwent.smb_passwd = NULL;
479 new_smb_pwent.smb_nt_passwd = NULL;
480 new_smb_pwent.acct_ctrl = (machine_account ? ACB_WSTRUST : ACB_NORMAL);
483 new_smb_pwent.acct_ctrl |= ACB_DISABLED;
484 } else if (set_no_password) {
485 new_smb_pwent.acct_ctrl |= ACB_PWNOTREQ;
487 new_smb_pwent.smb_passwd = new_p16;
488 new_smb_pwent.smb_nt_passwd = new_nt_p16;
491 if(add_smbpwd_entry(&new_smb_pwent) == False) {
492 fprintf(stderr, "%s: Failed to add entry for user %s.\n",
493 prog_name, pwd->pw_name);
499 printf("%s: Added user %s.\n", prog_name, user_name);
503 /* the entry already existed */
508 * We are root - just write the new password
509 * and the valid last change time.
514 * This currently won't work as it means changing
515 * the length of the record. JRA.
517 smb_pwent->acct_ctrl |= ACB_DISABLED;
518 smb_pwent->smb_passwd = NULL;
519 smb_pwent->smb_nt_passwd = NULL;
520 } else if (set_no_password) {
522 * This currently won't work as it means changing
523 * the length of the record. JRA.
525 smb_pwent->acct_ctrl |= ACB_PWNOTREQ;
526 smb_pwent->smb_passwd = NULL;
527 smb_pwent->smb_nt_passwd = NULL;
529 smb_pwent->smb_passwd = new_p16;
530 smb_pwent->smb_nt_passwd = new_nt_p16;
533 if(mod_smbpwd_entry(smb_pwent) == False) {
534 fprintf(stderr, "%s: Failed to modify entry for user %s.\n",
535 prog_name, pwd->pw_name);
542 printf("User %s disabled.\n", user_name);
543 else if (set_no_password)
544 printf("User %s - set to no password.\n", user_name);
546 printf("Password changed for user %s.\n", user_name);