Removed version number from file header.
[ira/wip.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "smb.h"
23
24 #define SECURITY_MASK 0
25 #define SECURITY_SET  0
26
27 /* this forces non-unicode */
28 #define CAPABILITY_MASK 0
29 #define CAPABILITY_SET  0
30
31 /* and non-unicode for the client too */
32 #define CLI_CAPABILITY_MASK 0
33 #define CLI_CAPABILITY_SET  0
34
35 static char *netbiosname;
36 static char packet[BUFFER_SIZE];
37
38 static void save_file(const char *fname, void *packet, size_t length)
39 {
40         int fd;
41         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
42         if (fd == -1) {
43                 perror(fname);
44                 return;
45         }
46         if (write(fd, packet, length) != length) {
47                 fprintf(stderr,"Failed to write %s\n", fname);
48                 return;
49         }
50         close(fd);
51         printf("Wrote %d bytes to %s\n", length, fname);
52 }
53
54 static void filter_reply(char *buf)
55 {
56         int msg_type = CVAL(buf,0);
57         int type = CVAL(buf,smb_com);
58         unsigned x;
59
60         if (msg_type) return;
61
62         switch (type) {
63
64         case SMBnegprot:
65                 /* force the security bits */
66                 x = CVAL(buf, smb_vwv1);
67                 x = (x | SECURITY_SET) & ~SECURITY_MASK;
68                 SCVAL(buf, smb_vwv1, x);
69
70                 /* force the capabilities */
71                 x = IVAL(buf,smb_vwv9+1);
72                 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
73                 SIVAL(buf, smb_vwv9+1, x);
74                 break;
75
76         }
77 }
78
79 static void filter_request(char *buf)
80 {
81         int msg_type = CVAL(buf,0);
82         int type = CVAL(buf,smb_com);
83         pstring name1,name2;
84         unsigned x;
85
86         if (msg_type) {
87                 /* it's a netbios special */
88                 switch (msg_type) {
89                 case 0x81:
90                         /* session request */
91                         name_extract(buf,4,name1);
92                         name_extract(buf,4 + name_len(buf + 4),name2);
93                         d_printf("sesion_request: %s -> %s\n",
94                                  name1, name2);
95                         if (netbiosname) {
96                                 /* replace the destination netbios name */
97                                 name_mangle(netbiosname, buf+4, 0x20);
98                         }
99                 }
100                 return;
101         }
102
103         /* it's an ordinary SMB request */
104         switch (type) {
105         case SMBsesssetupX:
106                 /* force the client capabilities */
107                 x = IVAL(buf,smb_vwv11);
108                 d_printf("SMBsesssetupX cap=0x%08x\n", x);
109                 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
110                 system("mv sessionsetup.dat sessionsetup1.dat");
111                 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
112                 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
113                 SIVAL(buf, smb_vwv11, x);
114                 break;
115         }
116
117 }
118
119
120 static void filter_child(int c, struct in_addr dest_ip)
121 {
122         int s;
123
124         /* we have a connection from a new client, now connect to the server */
125         s = open_socket_out(SOCK_STREAM, &dest_ip, 445, LONG_CONNECT_TIMEOUT);
126
127         if (s == -1) {
128                 d_printf("Unable to connect to %s\n", inet_ntoa(dest_ip));
129                 exit(1);
130         }
131
132         while (c != -1 || s != -1) {
133                 fd_set fds;
134                 int num;
135                 
136                 FD_ZERO(&fds);
137                 if (s != -1) FD_SET(s, &fds);
138                 if (c != -1) FD_SET(c, &fds);
139
140                 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL);
141                 if (num <= 0) continue;
142                 
143                 if (c != -1 && FD_ISSET(c, &fds)) {
144                         if (!receive_smb(c, packet, 0)) {
145                                 d_printf("client closed connection\n");
146                                 exit(0);
147                         }
148                         filter_request(packet);
149                         if (!send_smb(s, packet)) {
150                                 d_printf("server is dead\n");
151                                 exit(1);
152                         }                       
153                 }
154                 if (s != -1 && FD_ISSET(s, &fds)) {
155                         if (!receive_smb(s, packet, 0)) {
156                                 d_printf("server closed connection\n");
157                                 exit(0);
158                         }
159                         filter_reply(packet);
160                         if (!send_smb(c, packet)) {
161                                 d_printf("client is dead\n");
162                                 exit(1);
163                         }                       
164                 }
165         }
166         d_printf("Connection closed\n");
167         exit(0);
168 }
169
170
171 static void start_filter(char *desthost)
172 {
173         int s, c;
174         struct in_addr dest_ip;
175
176         CatchChild();
177
178         /* start listening on port 445 locally */
179         s = open_socket_in(SOCK_STREAM, 445, 0, 0, True);
180         
181         if (s == -1) {
182                 d_printf("bind failed\n");
183                 exit(1);
184         }
185
186         if (listen(s, 5) == -1) {
187                 d_printf("listen failed\n");
188         }
189
190         if (!resolve_name(desthost, &dest_ip, 0x20)) {
191                 d_printf("Unable to resolve host %s\n", desthost);
192                 exit(1);
193         }
194
195         while (1) {
196                 fd_set fds;
197                 int num;
198                 struct sockaddr addr;
199                 socklen_t in_addrlen = sizeof(addr);
200                 
201                 FD_ZERO(&fds);
202                 FD_SET(s, &fds);
203
204                 num = sys_select_intr(s+1,&fds,NULL);
205                 if (num > 0) {
206                         c = accept(s, &addr, &in_addrlen);
207                         if (c != -1) {
208                                 if (fork() == 0) {
209                                         close(s);
210                                         filter_child(c, dest_ip);
211                                         exit(0);
212                                 } else {
213                                         close(c);
214                                 }
215                         }
216                 }
217         }
218 }
219
220
221 int main(int argc, char *argv[])
222 {
223         char *desthost;
224         pstring configfile;
225
226         setup_logging(argv[0],True);
227   
228         pstrcpy(configfile,dyn_CONFIGFILE);
229  
230         if (argc < 2) {
231                 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
232                 exit(1);
233         }
234
235         desthost = argv[1];
236         if (argc > 2) {
237                 netbiosname = argv[2];
238         }
239
240         if (!lp_load(configfile,True,False,False)) {
241                 d_printf("Unable to load config file\n");
242         }
243
244         start_filter(desthost);
245         return 0;
246 }