Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[tprouty/samba.git] / source / auth / auth_util.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Authentication utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Andrew Bartlett 2001
7
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 /* Data to do lanman1/2 password challenge. */
26 static unsigned char saved_challenge[8];
27 static BOOL challenge_sent=False;
28
29 /*******************************************************************
30 Get the next challenge value - no repeats.
31 ********************************************************************/
32 void generate_next_challenge(char *challenge)
33 {
34         unsigned char buf[8];
35
36         generate_random_buffer(buf,8,False);
37         memcpy(saved_challenge, buf, 8);
38         memcpy(challenge,buf,8);
39         challenge_sent = True;
40 }
41
42 /*******************************************************************
43 set the last challenge sent, usually from a password server
44 ********************************************************************/
45 BOOL set_challenge(unsigned char *challenge)
46 {
47         memcpy(saved_challenge,challenge,8);
48         challenge_sent = True;
49         return(True);
50 }
51
52 /*******************************************************************
53 get the last challenge sent
54 ********************************************************************/
55 BOOL last_challenge(unsigned char *challenge)
56 {
57         if (!challenge_sent) return(False);
58         memcpy(challenge,saved_challenge,8);
59         return(True);
60 }
61
62
63 /****************************************************************************
64  Create a UNIX user on demand.
65 ****************************************************************************/
66
67 static int smb_create_user(char *unix_user, char *homedir)
68 {
69         pstring add_script;
70         int ret;
71
72         pstrcpy(add_script, lp_adduser_script());
73         if (! *add_script) return -1;
74         all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
75         if (homedir)
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));
79         return ret;
80 }
81
82 /****************************************************************************
83  Delete a UNIX user on demand.
84 ****************************************************************************/
85
86 static int smb_delete_user(char *unix_user)
87 {
88         pstring del_script;
89         int ret;
90
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));
96         return ret;
97 }
98
99 /****************************************************************************
100  Add and Delete UNIX users on demand, based on NTSTATUS codes.
101 ****************************************************************************/
102
103 void smb_user_control(char *unix_user, NTSTATUS nt_status) 
104 {
105         struct passwd *pwd=NULL;
106
107         if (NT_STATUS_IS_OK(nt_status)) {
108                 /*
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.
112                  */
113                 if(lp_adduser_script() && !(pwd = smb_getpwnam(unix_user,True)))
114                         smb_create_user(unix_user, NULL);
115
116                 if(lp_adduser_script() && pwd) {
117                         SMB_STRUCT_STAT st;
118
119                         /*
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.
123                          */
124
125                         if (pwd->pw_dir && (sys_stat(pwd->pw_dir, &st) == -1) && (errno == ENOENT))
126                                 smb_create_user(unix_user, pwd->pw_dir);
127                 }
128
129         } else if (NT_STATUS_V(nt_status) == NT_STATUS_V(NT_STATUS_NO_SUCH_USER)) {
130                 /*
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,
134                  * do so.
135                  */
136                 if(lp_deluser_script() && smb_getpwnam(unix_user,True))
137                         smb_delete_user(unix_user);
138         }
139 }