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