r24005: Attempt to fix the build on host deckchair
[amitay/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
22 #define SECURITY_MASK 0
23 #define SECURITY_SET  0
24
25 /* this forces non-unicode */
26 #define CAPABILITY_MASK 0
27 #define CAPABILITY_SET  0
28
29 /* and non-unicode for the client too */
30 #define CLI_CAPABILITY_MASK 0
31 #define CLI_CAPABILITY_SET  0
32
33 static char *netbiosname;
34 static char packet[BUFFER_SIZE];
35
36 static void save_file(const char *fname, void *ppacket, size_t length)
37 {
38         int fd;
39         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
40         if (fd == -1) {
41                 perror(fname);
42                 return;
43         }
44         if (write(fd, ppacket, length) != length) {
45                 fprintf(stderr,"Failed to write %s\n", fname);
46                 return;
47         }
48         close(fd);
49         printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
50 }
51
52 static void filter_reply(char *buf)
53 {
54         int msg_type = CVAL(buf,0);
55         int type = CVAL(buf,smb_com);
56         unsigned x;
57
58         if (msg_type) return;
59
60         switch (type) {
61
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);
67
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;
73
74         }
75 }
76
77 static void filter_request(char *buf)
78 {
79         int msg_type = CVAL(buf,0);
80         int type = CVAL(buf,smb_com);
81         pstring name1,name2;
82         unsigned x;
83
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                                 /* replace the destination netbios name */
95                                 name_mangle(netbiosname, buf+4, 0x20);
96                         }
97                 }
98                 return;
99         }
100
101         /* it's an ordinary SMB request */
102         switch (type) {
103         case SMBsesssetupX:
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);
112                 break;
113         }
114
115 }
116
117
118 static void filter_child(int c, struct in_addr dest_ip)
119 {
120         int s;
121
122         /* we have a connection from a new client, now connect to the server */
123         s = open_socket_out(SOCK_STREAM, &dest_ip, 445, LONG_CONNECT_TIMEOUT);
124
125         if (s == -1) {
126                 d_printf("Unable to connect to %s\n", inet_ntoa(dest_ip));
127                 exit(1);
128         }
129
130         while (c != -1 || s != -1) {
131                 fd_set fds;
132                 int num;
133                 
134                 FD_ZERO(&fds);
135                 if (s != -1) FD_SET(s, &fds);
136                 if (c != -1) FD_SET(c, &fds);
137
138                 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
139                 if (num <= 0) continue;
140                 
141                 if (c != -1 && FD_ISSET(c, &fds)) {
142                         if (!receive_smb(c, packet, 0)) {
143                                 d_printf("client closed connection\n");
144                                 exit(0);
145                         }
146                         filter_request(packet);
147                         if (!send_smb(s, packet)) {
148                                 d_printf("server is dead\n");
149                                 exit(1);
150                         }                       
151                 }
152                 if (s != -1 && FD_ISSET(s, &fds)) {
153                         if (!receive_smb(s, packet, 0)) {
154                                 d_printf("server closed connection\n");
155                                 exit(0);
156                         }
157                         filter_reply(packet);
158                         if (!send_smb(c, packet)) {
159                                 d_printf("client is dead\n");
160                                 exit(1);
161                         }                       
162                 }
163         }
164         d_printf("Connection closed\n");
165         exit(0);
166 }
167
168
169 static void start_filter(char *desthost)
170 {
171         int s, c;
172         struct in_addr dest_ip;
173
174         CatchChild();
175
176         /* start listening on port 445 locally */
177         s = open_socket_in(SOCK_STREAM, 445, 0, 0, True);
178         
179         if (s == -1) {
180                 d_printf("bind failed\n");
181                 exit(1);
182         }
183
184         if (listen(s, 5) == -1) {
185                 d_printf("listen failed\n");
186         }
187
188         if (!resolve_name(desthost, &dest_ip, 0x20)) {
189                 d_printf("Unable to resolve host %s\n", desthost);
190                 exit(1);
191         }
192
193         while (1) {
194                 fd_set fds;
195                 int num;
196                 struct sockaddr addr;
197                 socklen_t in_addrlen = sizeof(addr);
198                 
199                 FD_ZERO(&fds);
200                 FD_SET(s, &fds);
201
202                 num = sys_select_intr(s+1,&fds,NULL,NULL,NULL);
203                 if (num > 0) {
204                         c = accept(s, &addr, &in_addrlen);
205                         if (c != -1) {
206                                 if (fork() == 0) {
207                                         close(s);
208                                         filter_child(c, dest_ip);
209                                         exit(0);
210                                 } else {
211                                         close(c);
212                                 }
213                         }
214                 }
215         }
216 }
217
218
219 int main(int argc, char *argv[])
220 {
221         char *desthost;
222         pstring configfile;
223
224         setup_logging(argv[0],True);
225   
226         pstrcpy(configfile,dyn_CONFIGFILE);
227  
228         if (argc < 2) {
229                 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
230                 exit(1);
231         }
232
233         desthost = argv[1];
234         if (argc > 2) {
235                 netbiosname = argv[2];
236         }
237
238         if (!lp_load(configfile,True,False,False,True)) {
239                 d_printf("Unable to load config file\n");
240         }
241
242         start_filter(desthost);
243         return 0;
244 }