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/>.
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)
39 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
44 if (write(fd, ppacket, length) != length) {
45 fprintf(stderr,"Failed to write %s\n", fname);
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);
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);
77 static void filter_request(char *buf)
79 int msg_type = CVAL(buf,0);
80 int type = CVAL(buf,smb_com);
85 /* it's a netbios special */
89 name_extract(buf,4,name1);
90 name_extract(buf,4 + name_len(buf + 4),name2);
91 d_printf("sesion_request: %s -> %s\n",
94 /* replace the destination netbios name */
95 name_mangle(netbiosname, buf+4, 0x20);
101 /* it's an ordinary SMB request */
104 /* force the client capabilities */
105 x = IVAL(buf,smb_vwv11);
106 d_printf("SMBsesssetupX cap=0x%08x\n", x);
107 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
108 system("mv sessionsetup.dat sessionsetup1.dat");
109 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
110 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
111 SIVAL(buf, smb_vwv11, x);
118 static void filter_child(int c, struct sockaddr_storage *dest_ss)
122 /* we have a connection from a new client, now connect to the server */
123 s = open_socket_out(SOCK_STREAM, dest_ss, 445, LONG_CONNECT_TIMEOUT);
126 char addr[INET6_ADDRSTRLEN];
128 print_sockaddr(addr, sizeof(addr), dest_ss);
131 d_printf("Unable to connect to %s (%s)\n",
132 dest_ss?addr:"NULL",strerror(errno));
136 while (c != -1 || s != -1) {
141 if (s != -1) FD_SET(s, &fds);
142 if (c != -1) FD_SET(c, &fds);
144 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
145 if (num <= 0) continue;
147 if (c != -1 && FD_ISSET(c, &fds)) {
148 if (!receive_smb(c, packet, 0, NULL)) {
149 d_printf("client closed connection\n");
152 filter_request(packet);
153 if (!send_smb(s, packet)) {
154 d_printf("server is dead\n");
158 if (s != -1 && FD_ISSET(s, &fds)) {
159 if (!receive_smb(s, packet, 0, NULL)) {
160 d_printf("server closed connection\n");
163 filter_reply(packet);
164 if (!send_smb(c, packet)) {
165 d_printf("client is dead\n");
170 d_printf("Connection closed\n");
175 static void start_filter(char *desthost)
178 struct sockaddr_storage dest_ss;
179 struct sockaddr_storage my_ss;
183 /* start listening on port 445 locally */
184 my_ss.ss_family = AF_INET;
185 s = open_socket_in(SOCK_STREAM, 445, 0, &my_ss, True);
188 d_printf("bind failed\n");
192 if (listen(s, 5) == -1) {
193 d_printf("listen failed\n");
196 if (!resolve_name(desthost, &dest_ss, 0x20)) {
197 d_printf("Unable to resolve host %s\n", desthost);
204 struct sockaddr_storage ss;
205 socklen_t in_addrlen = sizeof(ss);
210 num = sys_select_intr(s+1,&fds,NULL,NULL,NULL);
212 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
216 filter_child(c, &dest_ss);
227 int main(int argc, char *argv[])
231 TALLOC_CTX *frame = talloc_stackframe();
235 setup_logging(argv[0],True);
237 pstrcpy(configfile,dyn_CONFIGFILE);
240 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
246 netbiosname = argv[2];
249 if (!lp_load(configfile,True,False,False,True)) {
250 d_printf("Unable to load config file\n");
253 start_filter(desthost);