pidl:Wireshark Fix the type of array of pointerse to hf_ values
[Samba.git] / source3 / utils / smbfilter.c
blobebf2809750091c01db7875a1078a2c84c505b8be
1 /*
2 Unix SMB/CIFS implementation.
3 SMB filter/socket plugin
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "system/select.h"
23 #include "libsmb/namequery.h"
24 #include "../lib/util/select.h"
25 #include "libsmb/nmblib.h"
26 #include "lib/util/sys_rw_data.h"
28 #define SECURITY_MASK 0
29 #define SECURITY_SET 0
31 /* this forces non-unicode */
32 #define CAPABILITY_MASK 0
33 #define CAPABILITY_SET 0
35 /* and non-unicode for the client too */
36 #define CLI_CAPABILITY_MASK 0
37 #define CLI_CAPABILITY_SET 0
39 static char *netbiosname;
41 static void save_file(const char *fname, void *ppacket, size_t length)
43 int fd;
44 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
45 if (fd == -1) {
46 perror(fname);
47 return;
49 if (write(fd, ppacket, length) != length) {
50 fprintf(stderr,"Failed to write %s\n", fname);
51 close(fd);
52 return;
54 close(fd);
55 printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
58 static void filter_reply(char *buf)
60 int msg_type = CVAL(buf,0);
61 int type = CVAL(buf,smb_com);
62 unsigned x;
64 if (msg_type) return;
66 switch (type) {
68 case SMBnegprot:
69 /* force the security bits */
70 x = CVAL(buf, smb_vwv1);
71 x = (x | SECURITY_SET) & ~SECURITY_MASK;
72 SCVAL(buf, smb_vwv1, x);
74 /* force the capabilities */
75 x = IVAL(buf,smb_vwv9+1);
76 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
77 SIVAL(buf, smb_vwv9+1, x);
78 break;
83 static void filter_request(char *buf, size_t buf_len)
85 int msg_type = CVAL(buf,0);
86 int type = CVAL(buf,smb_com);
87 unsigned x;
88 fstring name1,name2;
89 int name_len1 = 0;
90 int name_len2;
91 int name_type1, name_type2;
92 int ret;
94 if (msg_type) {
95 /* it's a netbios special */
96 switch (msg_type)
97 case 0x81:
98 /* session request */
99 /* inbuf_size is guaranteed to be at least 4. */
100 name_len1 = name_len((unsigned char *)(buf+4),
101 buf_len - 4);
102 if (name_len1 <= 0 || name_len1 > buf_len - 4) {
103 DEBUG(0,("Invalid name length in session request\n"));
104 return;
106 name_len2 = name_len((unsigned char *)(buf+4+name_len1),
107 buf_len - 4 - name_len1);
108 if (name_len2 <= 0 || name_len2 > buf_len - 4 - name_len1) {
109 DEBUG(0,("Invalid name length in session request\n"));
110 return;
113 name_type1 = name_extract((unsigned char *)buf,
114 buf_len,(unsigned int)4,name1);
115 name_type2 = name_extract((unsigned char *)buf,
116 buf_len,(unsigned int)(4 + name_len1),name2);
118 if (name_type1 == -1 || name_type2 == -1) {
119 DEBUG(0,("Invalid name type in session request\n"));
120 return;
123 d_printf("session_request: %s -> %s\n",
124 name1, name2);
125 if (netbiosname) {
126 char *mangled = name_mangle(
127 talloc_tos(), netbiosname, 0x20);
128 if (mangled != NULL) {
129 /* replace the destination netbios
130 * name */
131 memcpy(buf+4, mangled,
132 name_len((unsigned char *)mangled,
133 talloc_get_size(mangled)));
134 TALLOC_FREE(mangled);
137 return;
140 /* it's an ordinary SMB request */
141 switch (type) {
142 case SMBsesssetupX:
143 /* force the client capabilities */
144 x = IVAL(buf,smb_vwv11);
145 d_printf("SMBsesssetupX cap=0x%08x\n", x);
146 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
147 ret = system("mv sessionsetup.dat sessionsetup1.dat");
148 if (ret == -1) {
149 DBG_ERR("failed to call mv command\n");
151 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
152 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
153 SIVAL(buf, smb_vwv11, x);
154 break;
158 /****************************************************************************
159 Send an smb to a fd.
160 ****************************************************************************/
162 static bool send_smb(int fd, char *buffer)
164 size_t len;
165 size_t nwritten=0;
166 ssize_t ret;
168 len = smb_len(buffer) + 4;
170 while (nwritten < len) {
171 ret = write_data(fd,buffer+nwritten,len - nwritten);
172 if (ret <= 0) {
173 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
174 (int)len,(int)ret, strerror(errno) ));
175 return false;
177 nwritten += ret;
180 return true;
183 static void filter_child(int c, struct sockaddr_storage *dest_ss)
185 NTSTATUS status;
186 int s = -1;
187 char packet[128*1024];
189 /* we have a connection from a new client, now connect to the server */
190 status = open_socket_out(dest_ss, TCP_SMB_PORT, LONG_CONNECT_TIMEOUT, &s);
191 if (!NT_STATUS_IS_OK(status)) {
192 char addr[INET6_ADDRSTRLEN];
193 if (dest_ss) {
194 print_sockaddr(addr, sizeof(addr), dest_ss);
197 d_printf("Unable to connect to %s (%s)\n",
198 dest_ss?addr:"NULL", nt_errstr(status));
199 exit(1);
202 while (c != -1 || s != -1) {
203 struct pollfd fds[2];
204 int num_fds, ret;
206 memset(fds, 0, sizeof(struct pollfd) * 2);
207 fds[0].fd = -1;
208 fds[1].fd = -1;
209 num_fds = 0;
211 if (s != -1) {
212 fds[num_fds].fd = s;
213 fds[num_fds].events = POLLIN|POLLHUP;
214 num_fds += 1;
216 if (c != -1) {
217 fds[num_fds].fd = c;
218 fds[num_fds].events = POLLIN|POLLHUP;
219 num_fds += 1;
222 ret = sys_poll_intr(fds, num_fds, -1);
223 if (ret <= 0) {
224 continue;
228 * find c in fds and see if it's readable
230 if ((c != -1) &&
231 (((fds[0].fd == c)
232 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
233 ((fds[1].fd == c)
234 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
235 size_t len;
236 if (!NT_STATUS_IS_OK(receive_smb_raw(
237 c, packet, sizeof(packet),
238 0, 0, &len))) {
239 d_printf("client closed connection\n");
240 exit(0);
242 filter_request(packet, len);
243 if (!send_smb(s, packet)) {
244 d_printf("server is dead\n");
245 exit(1);
250 * find s in fds and see if it's readable
252 if ((s != -1) &&
253 (((fds[0].fd == s)
254 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
255 ((fds[1].fd == s)
256 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
257 size_t len;
258 if (!NT_STATUS_IS_OK(receive_smb_raw(
259 s, packet, sizeof(packet),
260 0, 0, &len))) {
261 d_printf("server closed connection\n");
262 exit(0);
264 filter_reply(packet);
265 if (!send_smb(c, packet)) {
266 d_printf("client is dead\n");
267 exit(1);
271 d_printf("Connection closed\n");
272 exit(0);
276 static void start_filter(char *desthost)
278 int s, c;
279 struct sockaddr_storage dest_ss;
280 struct sockaddr_storage my_ss;
282 CatchChild();
284 /* start listening on port 445 locally */
286 zero_sockaddr(&my_ss);
287 s = open_socket_in(SOCK_STREAM, &my_ss, TCP_SMB_PORT, true);
289 if (s < 0) {
290 d_printf("bind failed: %s\n", strerror(-s));
291 exit(1);
294 if (listen(s, 5) == -1) {
295 d_printf("listen failed\n");
298 if (!resolve_name(desthost, &dest_ss, 0x20, false)) {
299 d_printf("Unable to resolve host %s\n", desthost);
300 exit(1);
303 while (1) {
304 int num, revents;
305 struct sockaddr_storage ss;
306 socklen_t in_addrlen = sizeof(ss);
308 num = poll_intr_one_fd(s, POLLIN|POLLHUP, -1, &revents);
309 if ((num > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
310 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
311 if (c != -1) {
312 smb_set_close_on_exec(c);
313 if (fork() == 0) {
314 close(s);
315 filter_child(c, &dest_ss);
316 exit(0);
317 } else {
318 close(c);
326 int main(int argc, char *argv[])
328 char *desthost;
329 const char *configfile;
330 TALLOC_CTX *frame = talloc_stackframe();
332 smb_init_locale();
334 setup_logging(argv[0], DEBUG_STDOUT);
336 configfile = get_dyn_CONFIGFILE();
338 if (argc < 2) {
339 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
340 exit(1);
343 desthost = argv[1];
344 if (argc > 2) {
345 netbiosname = argv[2];
348 if (!lp_load_global(configfile)) {
349 d_printf("Unable to load config file\n");
352 start_filter(desthost);
353 gfree_all();
354 TALLOC_FREE(frame);
355 return 0;