Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[metze/samba/wip.git] / source3 / utils / smbfilter.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2
4    SMB filter/socket plugin
5    Copyright (C) Andrew Tridgell 1999
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "smb.h"
24
25 #define SECURITY_MASK 0
26 #define SECURITY_SET  0
27
28 /* this forces non-unicode */
29 #define CAPABILITY_MASK (CAP_NT_SMBS | CAP_RPC_REMOTE_APIS)
30 #define CAPABILITY_SET  0
31
32 /* and non-unicode for the client too */
33 #define CLI_CAPABILITY_MASK 0
34 #define CLI_CAPABILITY_SET  0
35
36 static char *netbiosname;
37 static char packet[BUFFER_SIZE];
38
39 static void filter_reply(char *buf)
40 {
41         int msg_type = CVAL(buf,0);
42         int type = CVAL(buf,smb_com);
43         unsigned x;
44
45         if (msg_type) return;
46
47         switch (type) {
48
49         case SMBnegprot:
50                 /* force the security bits */
51                 x = CVAL(buf, smb_vwv1);
52                 x = (x | SECURITY_SET) & ~SECURITY_MASK;
53                 SCVAL(buf, smb_vwv1, x);
54
55                 /* force the capabilities */
56                 x = IVAL(buf,smb_vwv9+1);
57                 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
58                 SIVAL(buf, smb_vwv9+1, x);
59                 break;
60
61         }
62 }
63
64 static void filter_request(char *buf)
65 {
66         int msg_type = CVAL(buf,0);
67         int type = CVAL(buf,smb_com);
68         pstring name1,name2;
69         unsigned x;
70
71         if (msg_type) {
72                 /* it's a netbios special */
73                 switch (msg_type) {
74                 case 0x81:
75                         /* session request */
76                         name_extract(buf,4,name1);
77                         name_extract(buf,4 + name_len(buf + 4),name2);
78                         DEBUG(0,("sesion_request: %s -> %s\n",
79                                  name1, name2));
80                         if (netbiosname) {
81                                 /* replace the destination netbios name */
82                                 name_mangle(netbiosname, buf+4, 0x20);
83                         }
84                 }
85                 return;
86         }
87
88         /* it's an ordinary SMB request */
89         switch (type) {
90         case SMBsesssetupX:
91                 /* force the client capabilities */
92                 x = IVAL(buf,smb_vwv11);
93                 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
94                 SIVAL(buf, smb_vwv11, x);
95                 break;
96         }
97
98 }
99
100
101 static void filter_child(int c, struct in_addr dest_ip)
102 {
103         int s;
104
105         /* we have a connection from a new client, now connect to the server */
106         s = open_socket_out(SOCK_STREAM, &dest_ip, 139, LONG_CONNECT_TIMEOUT);
107
108         if (s == -1) {
109                 DEBUG(0,("Unable to connect to %s\n", inet_ntoa(dest_ip)));
110                 exit(1);
111         }
112
113         while (c != -1 || s != -1) {
114                 fd_set fds;
115                 int num;
116                 
117                 FD_ZERO(&fds);
118                 if (s != -1) FD_SET(s, &fds);
119                 if (c != -1) FD_SET(c, &fds);
120
121                 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL);
122                 if (num <= 0) continue;
123                 
124                 if (c != -1 && FD_ISSET(c, &fds)) {
125                         if (!receive_smb(c, packet, 0)) {
126                                 DEBUG(0,("client closed connection\n"));
127                                 exit(0);
128                         }
129                         filter_request(packet);
130                         if (!send_smb(s, packet)) {
131                                 DEBUG(0,("server is dead\n"));
132                                 exit(1);
133                         }                       
134                 }
135                 if (s != -1 && FD_ISSET(s, &fds)) {
136                         if (!receive_smb(s, packet, 0)) {
137                                 DEBUG(0,("server closed connection\n"));
138                                 exit(0);
139                         }
140                         filter_reply(packet);
141                         if (!send_smb(c, packet)) {
142                                 DEBUG(0,("client is dead\n"));
143                                 exit(1);
144                         }                       
145                 }
146         }
147         DEBUG(0,("Connection closed\n"));
148         exit(0);
149 }
150
151
152 static void start_filter(char *desthost)
153 {
154         int s, c;
155         struct in_addr dest_ip;
156
157         CatchChild();
158
159         /* start listening on port 139 locally */
160         s = open_socket_in(SOCK_STREAM, 139, 0, 0, True);
161         
162         if (s == -1) {
163                 DEBUG(0,("bind failed\n"));
164                 exit(1);
165         }
166
167         if (listen(s, 5) == -1) {
168                 DEBUG(0,("listen failed\n"));
169         }
170
171         if (!resolve_name(desthost, &dest_ip, 0x20)) {
172                 DEBUG(0,("Unable to resolve host %s\n", desthost));
173                 exit(1);
174         }
175
176         while (1) {
177                 fd_set fds;
178                 int num;
179                 struct sockaddr addr;
180                 socklen_t in_addrlen = sizeof(addr);
181                 
182                 FD_ZERO(&fds);
183                 FD_SET(s, &fds);
184
185                 num = sys_select_intr(s+1,&fds,NULL);
186                 if (num > 0) {
187                         c = accept(s, &addr, &in_addrlen);
188                         if (c != -1) {
189                                 if (fork() == 0) {
190                                         close(s);
191                                         filter_child(c, dest_ip);
192                                         exit(0);
193                                 } else {
194                                         close(c);
195                                 }
196                         }
197                 }
198         }
199 }
200
201
202 int main(int argc, char *argv[])
203 {
204         char *desthost;
205         pstring configfile;
206
207         TimeInit();
208
209         setup_logging(argv[0],True);
210   
211         pstrcpy(configfile,CONFIGFILE);
212  
213         if (argc < 2) {
214                 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
215                 exit(1);
216         }
217
218         desthost = argv[1];
219         if (argc > 2) {
220                 netbiosname = argv[2];
221         }
222
223         if (!lp_load(configfile,True,False,False)) {
224                 DEBUG(0,("Unable to load config file\n"));
225         }
226
227         start_filter(desthost);
228         return 0;
229 }