2 Unix SMB/Netbios implementation.
4 Authentication utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Andrew Bartlett 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* Data to do lanman1/2 password challenge. */
26 static unsigned char saved_challenge[8];
27 static BOOL challenge_sent=False;
29 /*******************************************************************
30 Get the next challenge value - no repeats.
31 ********************************************************************/
32 void generate_next_challenge(char *challenge)
36 generate_random_buffer(buf,8,False);
37 memcpy(saved_challenge, buf, 8);
38 memcpy(challenge,buf,8);
39 challenge_sent = True;
42 /*******************************************************************
43 set the last challenge sent, usually from a password server
44 ********************************************************************/
45 BOOL set_challenge(unsigned char *challenge)
47 memcpy(saved_challenge,challenge,8);
48 challenge_sent = True;
52 /*******************************************************************
53 get the last challenge sent
54 ********************************************************************/
55 BOOL last_challenge(unsigned char *challenge)
57 if (!challenge_sent) return(False);
58 memcpy(challenge,saved_challenge,8);
63 /****************************************************************************
64 Create a UNIX user on demand.
65 ****************************************************************************/
67 static int smb_create_user(char *unix_user, char *homedir)
72 pstrcpy(add_script, lp_adduser_script());
73 if (! *add_script) return -1;
74 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
76 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
77 ret = smbrun(add_script,NULL);
78 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
82 /****************************************************************************
83 Delete a UNIX user on demand.
84 ****************************************************************************/
86 static int smb_delete_user(char *unix_user)
91 pstrcpy(del_script, lp_deluser_script());
92 if (! *del_script) return -1;
93 all_string_sub(del_script, "%u", unix_user, sizeof(pstring));
94 ret = smbrun(del_script,NULL);
95 DEBUG(3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
99 /****************************************************************************
100 Add and Delete UNIX users on demand, based on NTSTATUS codes.
101 ****************************************************************************/
103 void smb_user_control(char *unix_user, NTSTATUS nt_status)
105 struct passwd *pwd=NULL;
107 if (NT_STATUS_IS_OK(nt_status)) {
109 * User validated ok against Domain controller.
110 * If the admin wants us to try and create a UNIX
111 * user on the fly, do so.
113 if(lp_adduser_script() && !(pwd = smb_getpwnam(unix_user,True)))
114 smb_create_user(unix_user, NULL);
116 if(lp_adduser_script() && pwd) {
120 * Also call smb_create_user if the users home directory
121 * doesn't exist. Used with winbindd to allow the script to
122 * create the home directory for a user mapped with winbindd.
125 if (pwd->pw_dir && (sys_stat(pwd->pw_dir, &st) == -1) && (errno == ENOENT))
126 smb_create_user(unix_user, pwd->pw_dir);
129 } else if (NT_STATUS_V(nt_status) == NT_STATUS_V(NT_STATUS_NO_SUCH_USER)) {
131 * User failed to validate ok against Domain controller.
132 * If the failure was "user doesn't exist" and admin
133 * wants us to try and delete that UNIX user on the fly,
136 if(lp_deluser_script() && smb_getpwnam(unix_user,True))
137 smb_delete_user(unix_user);