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