s3-rpc_client: Move client pipe functions to own header.
[Samba.git] / source3 / utils / smbfilter.c
blobf455f2df91c8c8775a24231ca025cc62a0863ef1
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/select.h"
22 #include "../lib/util/select.h"
24 #define SECURITY_MASK 0
25 #define SECURITY_SET 0
27 /* this forces non-unicode */
28 #define CAPABILITY_MASK 0
29 #define CAPABILITY_SET 0
31 /* and non-unicode for the client too */
32 #define CLI_CAPABILITY_MASK 0
33 #define CLI_CAPABILITY_SET 0
35 static char *netbiosname;
36 static char packet[BUFFER_SIZE];
38 static void save_file(const char *fname, void *ppacket, size_t length)
40 int fd;
41 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
42 if (fd == -1) {
43 perror(fname);
44 return;
46 if (write(fd, ppacket, length) != length) {
47 fprintf(stderr,"Failed to write %s\n", fname);
48 close(fd);
49 return;
51 close(fd);
52 printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
55 static void filter_reply(char *buf)
57 int msg_type = CVAL(buf,0);
58 int type = CVAL(buf,smb_com);
59 unsigned x;
61 if (msg_type) return;
63 switch (type) {
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);
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;
80 static void filter_request(char *buf, size_t buf_len)
82 int msg_type = CVAL(buf,0);
83 int type = CVAL(buf,smb_com);
84 unsigned x;
85 fstring name1,name2;
86 int name_len1, name_len2;
87 int name_type1, name_type2;
89 if (msg_type) {
90 /* it's a netbios special */
91 switch (msg_type)
92 case 0x81:
93 /* session request */
94 /* inbuf_size is guaranteed to be at least 4. */
95 name_len1 = name_len((unsigned char *)(buf+4),
96 buf_len - 4);
97 if (name_len1 <= 0 || name_len1 > buf_len - 4) {
98 DEBUG(0,("Invalid name length in session request\n"));
99 return;
101 name_len2 = name_len((unsigned char *)(buf+4+name_len1),
102 buf_len - 4 - name_len1);
103 if (name_len2 <= 0 || name_len2 > buf_len - 4 - name_len1) {
104 DEBUG(0,("Invalid name length in session request\n"));
105 return;
108 name_type1 = name_extract((unsigned char *)buf,
109 buf_len,(unsigned int)4,name1);
110 name_type2 = name_extract((unsigned char *)buf,
111 buf_len,(unsigned int)(4 + name_len1),name2);
113 if (name_type1 == -1 || name_type2 == -1) {
114 DEBUG(0,("Invalid name type in session request\n"));
115 return;
118 d_printf("sesion_request: %s -> %s\n",
119 name1, name2);
120 if (netbiosname) {
121 char *mangled = name_mangle(
122 talloc_tos(), netbiosname, 0x20);
123 if (mangled != NULL) {
124 /* replace the destination netbios
125 * name */
126 memcpy(buf+4, mangled,
127 name_len((unsigned char *)mangled,
128 talloc_get_size(mangled)));
129 TALLOC_FREE(mangled);
132 return;
135 /* it's an ordinary SMB request */
136 switch (type) {
137 case SMBsesssetupX:
138 /* force the client capabilities */
139 x = IVAL(buf,smb_vwv11);
140 d_printf("SMBsesssetupX cap=0x%08x\n", x);
141 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
142 system("mv sessionsetup.dat sessionsetup1.dat");
143 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
144 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
145 SIVAL(buf, smb_vwv11, x);
146 break;
150 /****************************************************************************
151 Send an smb to a fd.
152 ****************************************************************************/
154 static bool send_smb(int fd, char *buffer)
156 size_t len;
157 size_t nwritten=0;
158 ssize_t ret;
160 len = smb_len(buffer) + 4;
162 while (nwritten < len) {
163 ret = write_data(fd,buffer+nwritten,len - nwritten);
164 if (ret <= 0) {
165 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
166 (int)len,(int)ret, strerror(errno) ));
167 return false;
169 nwritten += ret;
172 return true;
175 static void filter_child(int c, struct sockaddr_storage *dest_ss)
177 NTSTATUS status;
178 int s = -1;
180 /* we have a connection from a new client, now connect to the server */
181 status = open_socket_out(dest_ss, 445, LONG_CONNECT_TIMEOUT, &s);
183 if (s == -1) {
184 char addr[INET6_ADDRSTRLEN];
185 if (dest_ss) {
186 print_sockaddr(addr, sizeof(addr), dest_ss);
189 d_printf("Unable to connect to %s (%s)\n",
190 dest_ss?addr:"NULL",strerror(errno));
191 exit(1);
194 while (c != -1 || s != -1) {
195 struct pollfd fds[2];
196 int num_fds, ret;
198 memset(fds, 0, sizeof(struct pollfd) * 2);
199 fds[0].fd = -1;
200 fds[1].fd = -1;
201 num_fds = 0;
203 if (s != -1) {
204 fds[num_fds].fd = s;
205 fds[num_fds].events = POLLIN|POLLHUP;
206 num_fds += 1;
208 if (c != -1) {
209 fds[num_fds].fd = c;
210 fds[num_fds].events = POLLIN|POLLHUP;
211 num_fds += 1;
214 ret = sys_poll_intr(fds, num_fds, -1);
215 if (ret <= 0) {
216 continue;
220 * find c in fds and see if it's readable
222 if ((c != -1) &&
223 (((fds[0].fd == c)
224 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
225 ((fds[1].fd == c)
226 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
227 size_t len;
228 if (!NT_STATUS_IS_OK(receive_smb_raw(
229 c, packet, sizeof(packet),
230 0, 0, &len))) {
231 d_printf("client closed connection\n");
232 exit(0);
234 filter_request(packet, len);
235 if (!send_smb(s, packet)) {
236 d_printf("server is dead\n");
237 exit(1);
242 * find s in fds and see if it's readable
244 if ((s != -1) &&
245 (((fds[0].fd == s)
246 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
247 ((fds[1].fd == s)
248 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
249 size_t len;
250 if (!NT_STATUS_IS_OK(receive_smb_raw(
251 s, packet, sizeof(packet),
252 0, 0, &len))) {
253 d_printf("server closed connection\n");
254 exit(0);
256 filter_reply(packet);
257 if (!send_smb(c, packet)) {
258 d_printf("client is dead\n");
259 exit(1);
263 d_printf("Connection closed\n");
264 exit(0);
268 static void start_filter(char *desthost)
270 int s, c;
271 struct sockaddr_storage dest_ss;
272 struct sockaddr_storage my_ss;
274 CatchChild();
276 /* start listening on port 445 locally */
278 zero_sockaddr(&my_ss);
279 s = open_socket_in(SOCK_STREAM, 445, 0, &my_ss, True);
281 if (s == -1) {
282 d_printf("bind failed\n");
283 exit(1);
286 if (listen(s, 5) == -1) {
287 d_printf("listen failed\n");
290 if (!resolve_name(desthost, &dest_ss, 0x20, false)) {
291 d_printf("Unable to resolve host %s\n", desthost);
292 exit(1);
295 while (1) {
296 int num, revents;
297 struct sockaddr_storage ss;
298 socklen_t in_addrlen = sizeof(ss);
300 num = poll_intr_one_fd(s, POLLIN|POLLHUP, -1, &revents);
301 if ((num > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
302 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
303 if (c != -1) {
304 if (fork() == 0) {
305 close(s);
306 filter_child(c, &dest_ss);
307 exit(0);
308 } else {
309 close(c);
317 int main(int argc, char *argv[])
319 char *desthost;
320 const char *configfile;
321 TALLOC_CTX *frame = talloc_stackframe();
323 load_case_tables();
325 setup_logging(argv[0], DEBUG_STDOUT);
327 configfile = get_dyn_CONFIGFILE();
329 if (argc < 2) {
330 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
331 exit(1);
334 desthost = argv[1];
335 if (argc > 2) {
336 netbiosname = argv[2];
339 if (!lp_load(configfile,True,False,False,True)) {
340 d_printf("Unable to load config file\n");
343 start_filter(desthost);
344 TALLOC_FREE(frame);
345 return 0;