tevent: use an immediate event fot tevent_req_post()
[Samba/gbeck.git] / source3 / utils / smbfilter.c
blob39a264011eed4cf90acd6d9ac47893f096d30711
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"
22 #define SECURITY_MASK 0
23 #define SECURITY_SET 0
25 /* this forces non-unicode */
26 #define CAPABILITY_MASK 0
27 #define CAPABILITY_SET 0
29 /* and non-unicode for the client too */
30 #define CLI_CAPABILITY_MASK 0
31 #define CLI_CAPABILITY_SET 0
33 static char *netbiosname;
34 static char packet[BUFFER_SIZE];
36 static void save_file(const char *fname, void *ppacket, size_t length)
38 int fd;
39 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
40 if (fd == -1) {
41 perror(fname);
42 return;
44 if (write(fd, ppacket, length) != length) {
45 fprintf(stderr,"Failed to write %s\n", fname);
46 return;
48 close(fd);
49 printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
52 static void filter_reply(char *buf)
54 int msg_type = CVAL(buf,0);
55 int type = CVAL(buf,smb_com);
56 unsigned x;
58 if (msg_type) return;
60 switch (type) {
62 case SMBnegprot:
63 /* force the security bits */
64 x = CVAL(buf, smb_vwv1);
65 x = (x | SECURITY_SET) & ~SECURITY_MASK;
66 SCVAL(buf, smb_vwv1, x);
68 /* force the capabilities */
69 x = IVAL(buf,smb_vwv9+1);
70 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
71 SIVAL(buf, smb_vwv9+1, x);
72 break;
77 static void filter_request(char *buf)
79 int msg_type = CVAL(buf,0);
80 int type = CVAL(buf,smb_com);
81 fstring name1,name2;
82 unsigned x;
84 if (msg_type) {
85 /* it's a netbios special */
86 switch (msg_type) {
87 case 0x81:
88 /* session request */
89 name_extract(buf,4,name1);
90 name_extract(buf,4 + name_len(buf + 4),name2);
91 d_printf("sesion_request: %s -> %s\n",
92 name1, name2);
93 if (netbiosname) {
94 char *mangled = name_mangle(
95 talloc_tos(), netbiosname, 0x20);
96 if (mangled != NULL) {
97 /* replace the destination netbios
98 * name */
99 memcpy(buf+4, mangled,
100 name_len(mangled));
101 TALLOC_FREE(mangled);
105 return;
108 /* it's an ordinary SMB request */
109 switch (type) {
110 case SMBsesssetupX:
111 /* force the client capabilities */
112 x = IVAL(buf,smb_vwv11);
113 d_printf("SMBsesssetupX cap=0x%08x\n", x);
114 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
115 system("mv sessionsetup.dat sessionsetup1.dat");
116 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
117 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
118 SIVAL(buf, smb_vwv11, x);
119 break;
124 /****************************************************************************
125 Send an smb to a fd.
126 ****************************************************************************/
128 static bool send_smb(int fd, char *buffer)
130 size_t len;
131 size_t nwritten=0;
132 ssize_t ret;
134 len = smb_len(buffer) + 4;
136 while (nwritten < len) {
137 ret = write_data(fd,buffer+nwritten,len - nwritten);
138 if (ret <= 0) {
139 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
140 (int)len,(int)ret, strerror(errno) ));
141 return false;
143 nwritten += ret;
146 return true;
149 static void filter_child(int c, struct sockaddr_storage *dest_ss)
151 NTSTATUS status;
152 int s = -1;
154 /* we have a connection from a new client, now connect to the server */
155 status = open_socket_out(dest_ss, 445, LONG_CONNECT_TIMEOUT, &s);
157 if (s == -1) {
158 char addr[INET6_ADDRSTRLEN];
159 if (dest_ss) {
160 print_sockaddr(addr, sizeof(addr), dest_ss);
163 d_printf("Unable to connect to %s (%s)\n",
164 dest_ss?addr:"NULL",strerror(errno));
165 exit(1);
168 while (c != -1 || s != -1) {
169 fd_set fds;
170 int num;
172 FD_ZERO(&fds);
173 if (s != -1) FD_SET(s, &fds);
174 if (c != -1) FD_SET(c, &fds);
176 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
177 if (num <= 0) continue;
179 if (c != -1 && FD_ISSET(c, &fds)) {
180 size_t len;
181 if (!NT_STATUS_IS_OK(receive_smb_raw(
182 c, packet, sizeof(packet),
183 0, 0, &len))) {
184 d_printf("client closed connection\n");
185 exit(0);
187 filter_request(packet);
188 if (!send_smb(s, packet)) {
189 d_printf("server is dead\n");
190 exit(1);
193 if (s != -1 && FD_ISSET(s, &fds)) {
194 size_t len;
195 if (!NT_STATUS_IS_OK(receive_smb_raw(
196 s, packet, sizeof(packet),
197 0, 0, &len))) {
198 d_printf("server closed connection\n");
199 exit(0);
201 filter_reply(packet);
202 if (!send_smb(c, packet)) {
203 d_printf("client is dead\n");
204 exit(1);
208 d_printf("Connection closed\n");
209 exit(0);
213 static void start_filter(char *desthost)
215 int s, c;
216 struct sockaddr_storage dest_ss;
217 struct sockaddr_storage my_ss;
219 CatchChild();
221 /* start listening on port 445 locally */
223 zero_sockaddr(&my_ss);
224 s = open_socket_in(SOCK_STREAM, 445, 0, &my_ss, True);
226 if (s == -1) {
227 d_printf("bind failed\n");
228 exit(1);
231 if (listen(s, 5) == -1) {
232 d_printf("listen failed\n");
235 if (!resolve_name(desthost, &dest_ss, 0x20)) {
236 d_printf("Unable to resolve host %s\n", desthost);
237 exit(1);
240 while (1) {
241 fd_set fds;
242 int num;
243 struct sockaddr_storage ss;
244 socklen_t in_addrlen = sizeof(ss);
246 FD_ZERO(&fds);
247 FD_SET(s, &fds);
249 num = sys_select_intr(s+1,&fds,NULL,NULL,NULL);
250 if (num > 0) {
251 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
252 if (c != -1) {
253 if (fork() == 0) {
254 close(s);
255 filter_child(c, &dest_ss);
256 exit(0);
257 } else {
258 close(c);
266 int main(int argc, char *argv[])
268 char *desthost;
269 const char *configfile;
270 TALLOC_CTX *frame = talloc_stackframe();
272 load_case_tables();
274 setup_logging(argv[0],True);
276 configfile = get_dyn_CONFIGFILE();
278 if (argc < 2) {
279 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
280 exit(1);
283 desthost = argv[1];
284 if (argc > 2) {
285 netbiosname = argv[2];
288 if (!lp_load(configfile,True,False,False,True)) {
289 d_printf("Unable to load config file\n");
292 start_filter(desthost);
293 TALLOC_FREE(frame);
294 return 0;