sys_select added one more argument (read, write selectors).
[vlendec/samba-autobuild/.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_UNICODE
30 #define CAPABILITY_SET  0
31
32 /* and non-unicode for the client too */
33 #define CLI_CAPABILITY_MASK CAP_UNICODE
34 #define CLI_CAPABILITY_SET  0
35
36 static char *netbiosname;
37 static char packet[BUFFER_SIZE];
38
39 extern int DEBUGLEVEL;
40
41 static void filter_reply(char *buf)
42 {
43         int msg_type = CVAL(buf,0);
44         int type = CVAL(buf,smb_com);
45         unsigned x;
46
47         if (msg_type) return;
48
49         switch (type) {
50
51         case SMBnegprot:
52                 /* force the security bits */
53                 x = CVAL(buf, smb_vwv1);
54                 x = (x | SECURITY_SET) & ~SECURITY_MASK;
55                 SCVAL(buf, smb_vwv1, x);
56
57                 /* force the capabilities */
58                 x = IVAL(buf,smb_vwv9+1);
59                 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
60                 SIVAL(buf, smb_vwv9+1, x);
61                 break;
62
63         }
64 }
65
66 static void filter_request(char *buf)
67 {
68         int msg_type = CVAL(buf,0);
69         int type = CVAL(buf,smb_com);
70         pstring name1,name2;
71         unsigned x;
72
73         if (msg_type) {
74                 /* it's a netbios special */
75                 switch (msg_type) {
76                 case 0x81:
77                         /* session request */
78                         name_extract(buf,4,name1);
79                         name_extract(buf,4 + name_len(buf + 4),name2);
80                         DEBUG(0,("sesion_request: %s -> %s\n",
81                                  name1, name2));
82                         if (netbiosname) {
83                                 /* replace the destination netbios name */
84                                 name_mangle(netbiosname, buf+4, 0x20);
85                         }
86                 }
87                 return;
88         }
89
90         /* it's an ordinary SMB request */
91         switch (type) {
92         case SMBsesssetupX:
93                 /* force the client capabilities */
94                 x = IVAL(buf,smb_vwv11);
95                 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
96                 SIVAL(buf, smb_vwv11, x);
97                 break;
98         }
99
100 }
101
102
103 static void filter_child(int c, struct in_addr dest_ip)
104 {
105         int s;
106
107         /* we have a connection from a new client, now connect to the server */
108         s = open_socket_out(SOCK_STREAM, &dest_ip, 139, LONG_CONNECT_TIMEOUT);
109
110         if (s == -1) {
111                 DEBUG(0,("Unable to connect to %s\n", inet_ntoa(dest_ip)));
112                 exit(1);
113         }
114
115         while (c != -1 || s != -1) {
116                 fd_set fds;
117                 int num;
118                 
119                 FD_ZERO(&fds);
120                 if (s != -1) FD_SET(s, &fds);
121                 if (c != -1) FD_SET(c, &fds);
122
123                 num = sys_select(MAX(s+1, c+1),&fds,NULL, NULL);
124                 if (num <= 0) continue;
125                 
126                 if (c != -1 && FD_ISSET(c, &fds)) {
127                         if (!receive_smb(c, packet, 0)) {
128                                 DEBUG(0,("client closed connection\n"));
129                                 exit(0);
130                         }
131                         filter_request(packet);
132                         if (!send_smb(s, packet)) {
133                                 DEBUG(0,("server is dead\n"));
134                                 exit(1);
135                         }                       
136                 }
137                 if (s != -1 && FD_ISSET(s, &fds)) {
138                         if (!receive_smb(s, packet, 0)) {
139                                 DEBUG(0,("server closed connection\n"));
140                                 exit(0);
141                         }
142                         filter_reply(packet);
143                         if (!send_smb(c, packet)) {
144                                 DEBUG(0,("client is dead\n"));
145                                 exit(1);
146                         }                       
147                 }
148         }
149         DEBUG(0,("Connection closed\n"));
150         exit(0);
151 }
152
153
154 static void start_filter(char *desthost)
155 {
156         int s, c;
157         struct in_addr dest_ip;
158
159         CatchChild();
160
161         /* start listening on port 139 locally */
162         s = open_socket_in(SOCK_STREAM, 139, 0, 0);
163         
164         if (s == -1) {
165                 DEBUG(0,("bind failed\n"));
166                 exit(1);
167         }
168
169         if (listen(s, 5) == -1) {
170                 DEBUG(0,("listen failed\n"));
171         }
172
173         if (!resolve_name(desthost, &dest_ip, 0x20)) {
174                 DEBUG(0,("Unable to resolve host %s\n", desthost));
175                 exit(1);
176         }
177
178         while (1) {
179                 fd_set fds;
180                 int num;
181                 struct sockaddr addr;
182                 int in_addrlen = sizeof(addr);
183                 
184                 FD_ZERO(&fds);
185                 FD_SET(s, &fds);
186
187                 num = sys_select(s+1,&fds,NULL, NULL);
188                 if (num > 0) {
189                         c = accept(s, &addr, &in_addrlen);
190                         if (c != -1) {
191                                 if (fork() == 0) {
192                                         close(s);
193                                         filter_child(c, dest_ip);
194                                         exit(0);
195                                 } else {
196                                         close(c);
197                                 }
198                         }
199                 }
200         }
201 }
202
203
204 int main(int argc, char *argv[])
205 {
206         char *desthost;
207         pstring configfile;
208
209         TimeInit();
210
211         setup_logging(argv[0],True);
212   
213         charset_initialise();
214
215         pstrcpy(configfile,CONFIGFILE);
216  
217         if (argc < 2) {
218                 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
219                 exit(1);
220         }
221
222         desthost = argv[1];
223         if (argc > 2) {
224                 netbiosname = argv[2];
225         }
226
227         if (!lp_load(configfile,True,False,False)) {
228                 DEBUG(0,("Unable to load config file\n"));
229         }
230
231         start_filter(desthost);
232         return 0;
233 }