s3-includes: only include system/filesys.h when needed.
[vlendec/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "system/select.h"
23 #include "../lib/util/select.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 *ppacket, 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, ppacket, length) != length) {
48                 fprintf(stderr,"Failed to write %s\n", fname);
49                 close(fd);
50                 return;
51         }
52         close(fd);
53         printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
54 }
55
56 static void filter_reply(char *buf)
57 {
58         int msg_type = CVAL(buf,0);
59         int type = CVAL(buf,smb_com);
60         unsigned x;
61
62         if (msg_type) return;
63
64         switch (type) {
65
66         case SMBnegprot:
67                 /* force the security bits */
68                 x = CVAL(buf, smb_vwv1);
69                 x = (x | SECURITY_SET) & ~SECURITY_MASK;
70                 SCVAL(buf, smb_vwv1, x);
71
72                 /* force the capabilities */
73                 x = IVAL(buf,smb_vwv9+1);
74                 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
75                 SIVAL(buf, smb_vwv9+1, x);
76                 break;
77
78         }
79 }
80
81 static void filter_request(char *buf, size_t buf_len)
82 {
83         int msg_type = CVAL(buf,0);
84         int type = CVAL(buf,smb_com);
85         unsigned x;
86         fstring name1,name2;
87         int name_len1, name_len2;
88         int name_type1, name_type2;
89
90         if (msg_type) {
91                 /* it's a netbios special */
92                 switch (msg_type)
93                 case 0x81:
94                         /* session request */
95                         /* inbuf_size is guaranteed to be at least 4. */
96                         name_len1 = name_len((unsigned char *)(buf+4),
97                                         buf_len - 4);
98                         if (name_len1 <= 0 || name_len1 > buf_len - 4) {
99                                 DEBUG(0,("Invalid name length in session request\n"));
100                                 return;
101                         }
102                         name_len2 = name_len((unsigned char *)(buf+4+name_len1),
103                                         buf_len - 4 - name_len1);
104                         if (name_len2 <= 0 || name_len2 > buf_len - 4 - name_len1) {
105                                 DEBUG(0,("Invalid name length in session request\n"));
106                                 return;
107                         }
108
109                         name_type1 = name_extract((unsigned char *)buf,
110                                         buf_len,(unsigned int)4,name1);
111                         name_type2 = name_extract((unsigned char *)buf,
112                                         buf_len,(unsigned int)(4 + name_len1),name2);
113
114                         if (name_type1 == -1 || name_type2 == -1) {
115                                 DEBUG(0,("Invalid name type in session request\n"));
116                                 return;
117                         }
118
119                         d_printf("sesion_request: %s -> %s\n",
120                                  name1, name2);
121                         if (netbiosname) {
122                                 char *mangled = name_mangle(
123                                         talloc_tos(), netbiosname, 0x20);
124                                 if (mangled != NULL) {
125                                         /* replace the destination netbios
126                                          * name */
127                                         memcpy(buf+4, mangled,
128                                                name_len((unsigned char *)mangled,
129                                                         talloc_get_size(mangled)));
130                                         TALLOC_FREE(mangled);
131                                 }
132                         }
133                 return;
134         }
135
136         /* it's an ordinary SMB request */
137         switch (type) {
138         case SMBsesssetupX:
139                 /* force the client capabilities */
140                 x = IVAL(buf,smb_vwv11);
141                 d_printf("SMBsesssetupX cap=0x%08x\n", x);
142                 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
143                 system("mv sessionsetup.dat sessionsetup1.dat");
144                 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
145                 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
146                 SIVAL(buf, smb_vwv11, x);
147                 break;
148         }
149 }
150
151 /****************************************************************************
152  Send an smb to a fd.
153 ****************************************************************************/
154
155 static bool send_smb(int fd, char *buffer)
156 {
157         size_t len;
158         size_t nwritten=0;
159         ssize_t ret;
160
161         len = smb_len(buffer) + 4;
162
163         while (nwritten < len) {
164                 ret = write_data(fd,buffer+nwritten,len - nwritten);
165                 if (ret <= 0) {
166                         DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
167                                 (int)len,(int)ret, strerror(errno) ));
168                         return false;
169                 }
170                 nwritten += ret;
171         }
172
173         return true;
174 }
175
176 static void filter_child(int c, struct sockaddr_storage *dest_ss)
177 {
178         NTSTATUS status;
179         int s = -1;
180
181         /* we have a connection from a new client, now connect to the server */
182         status = open_socket_out(dest_ss, 445, LONG_CONNECT_TIMEOUT, &s);
183
184         if (s == -1) {
185                 char addr[INET6_ADDRSTRLEN];
186                 if (dest_ss) {
187                         print_sockaddr(addr, sizeof(addr), dest_ss);
188                 }
189
190                 d_printf("Unable to connect to %s (%s)\n",
191                          dest_ss?addr:"NULL",strerror(errno));
192                 exit(1);
193         }
194
195         while (c != -1 || s != -1) {
196                 struct pollfd fds[2];
197                 int num_fds, ret;
198
199                 memset(fds, 0, sizeof(struct pollfd) * 2);
200                 fds[0].fd = -1;
201                 fds[1].fd = -1;
202                 num_fds = 0;
203
204                 if (s != -1) {
205                         fds[num_fds].fd = s;
206                         fds[num_fds].events = POLLIN|POLLHUP;
207                         num_fds += 1;
208                 }
209                 if (c != -1) {
210                         fds[num_fds].fd = c;
211                         fds[num_fds].events = POLLIN|POLLHUP;
212                         num_fds += 1;
213                 }
214
215                 ret = sys_poll_intr(fds, num_fds, -1);
216                 if (ret <= 0) {
217                         continue;
218                 }
219
220                 /*
221                  * find c in fds and see if it's readable
222                  */
223                 if ((c != -1) &&
224                     (((fds[0].fd == c)
225                       && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
226                      ((fds[1].fd == c)
227                       && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
228                         size_t len;
229                         if (!NT_STATUS_IS_OK(receive_smb_raw(
230                                                         c, packet, sizeof(packet),
231                                                         0, 0, &len))) {
232                                 d_printf("client closed connection\n");
233                                 exit(0);
234                         }
235                         filter_request(packet, len);
236                         if (!send_smb(s, packet)) {
237                                 d_printf("server is dead\n");
238                                 exit(1);
239                         }                       
240                 }
241
242                 /*
243                  * find s in fds and see if it's readable
244                  */
245                 if ((s != -1) &&
246                     (((fds[0].fd == s)
247                       && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
248                      ((fds[1].fd == s)
249                       && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
250                         size_t len;
251                         if (!NT_STATUS_IS_OK(receive_smb_raw(
252                                                         s, packet, sizeof(packet),
253                                                         0, 0, &len))) {
254                                 d_printf("server closed connection\n");
255                                 exit(0);
256                         }
257                         filter_reply(packet);
258                         if (!send_smb(c, packet)) {
259                                 d_printf("client is dead\n");
260                                 exit(1);
261                         }                       
262                 }
263         }
264         d_printf("Connection closed\n");
265         exit(0);
266 }
267
268
269 static void start_filter(char *desthost)
270 {
271         int s, c;
272         struct sockaddr_storage dest_ss;
273         struct sockaddr_storage my_ss;
274
275         CatchChild();
276
277         /* start listening on port 445 locally */
278
279         zero_sockaddr(&my_ss);
280         s = open_socket_in(SOCK_STREAM, 445, 0, &my_ss, True);
281
282         if (s == -1) {
283                 d_printf("bind failed\n");
284                 exit(1);
285         }
286
287         if (listen(s, 5) == -1) {
288                 d_printf("listen failed\n");
289         }
290
291         if (!resolve_name(desthost, &dest_ss, 0x20, false)) {
292                 d_printf("Unable to resolve host %s\n", desthost);
293                 exit(1);
294         }
295
296         while (1) {
297                 int num, revents;
298                 struct sockaddr_storage ss;
299                 socklen_t in_addrlen = sizeof(ss);
300
301                 num = poll_intr_one_fd(s, POLLIN|POLLHUP, -1, &revents);
302                 if ((num > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
303                         c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
304                         if (c != -1) {
305                                 if (fork() == 0) {
306                                         close(s);
307                                         filter_child(c, &dest_ss);
308                                         exit(0);
309                                 } else {
310                                         close(c);
311                                 }
312                         }
313                 }
314         }
315 }
316
317
318 int main(int argc, char *argv[])
319 {
320         char *desthost;
321         const char *configfile;
322         TALLOC_CTX *frame = talloc_stackframe();
323
324         load_case_tables();
325
326         setup_logging(argv[0], DEBUG_STDOUT);
327
328         configfile = get_dyn_CONFIGFILE();
329
330         if (argc < 2) {
331                 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
332                 exit(1);
333         }
334
335         desthost = argv[1];
336         if (argc > 2) {
337                 netbiosname = argv[2];
338         }
339
340         if (!lp_load(configfile,True,False,False,True)) {
341                 d_printf("Unable to load config file\n");
342         }
343
344         start_filter(desthost);
345         TALLOC_FREE(frame);
346         return 0;
347 }