Fix the build of smbfilter
[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 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         fstring 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  Send an smb to a fd.
119 ****************************************************************************/
120
121 static bool send_smb(int fd, char *buffer)
122 {
123         size_t len;
124         size_t nwritten=0;
125         ssize_t ret;
126
127         len = smb_len(buffer) + 4;
128
129         while (nwritten < len) {
130                 ret = write_data(fd,buffer+nwritten,len - nwritten);
131                 if (ret <= 0) {
132                         DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
133                                 (int)len,(int)ret, strerror(errno) ));
134                         return false;
135                 }
136                 nwritten += ret;
137         }
138
139         return true;
140 }
141
142 static void filter_child(int c, struct sockaddr_storage *dest_ss)
143 {
144         NTSTATUS status;
145         int s = -1;
146
147         /* we have a connection from a new client, now connect to the server */
148         status = open_socket_out(dest_ss, 445, LONG_CONNECT_TIMEOUT, &s);
149
150         if (s == -1) {
151                 char addr[INET6_ADDRSTRLEN];
152                 if (dest_ss) {
153                         print_sockaddr(addr, sizeof(addr), dest_ss);
154                 }
155
156                 d_printf("Unable to connect to %s (%s)\n",
157                          dest_ss?addr:"NULL",strerror(errno));
158                 exit(1);
159         }
160
161         while (c != -1 || s != -1) {
162                 fd_set fds;
163                 int num;
164                 
165                 FD_ZERO(&fds);
166                 if (s != -1) FD_SET(s, &fds);
167                 if (c != -1) FD_SET(c, &fds);
168
169                 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
170                 if (num <= 0) continue;
171                 
172                 if (c != -1 && FD_ISSET(c, &fds)) {
173                         size_t len;
174                         if (!NT_STATUS_IS_OK(receive_smb_raw(
175                                                         c, packet, sizeof(packet),
176                                                         0, 0, &len))) {
177                                 d_printf("client closed connection\n");
178                                 exit(0);
179                         }
180                         filter_request(packet);
181                         if (!send_smb(s, packet)) {
182                                 d_printf("server is dead\n");
183                                 exit(1);
184                         }                       
185                 }
186                 if (s != -1 && FD_ISSET(s, &fds)) {
187                         size_t len;
188                         if (!NT_STATUS_IS_OK(receive_smb_raw(
189                                                         s, packet, sizeof(packet),
190                                                         0, 0, &len))) {
191                                 d_printf("server closed connection\n");
192                                 exit(0);
193                         }
194                         filter_reply(packet);
195                         if (!send_smb(c, packet)) {
196                                 d_printf("client is dead\n");
197                                 exit(1);
198                         }                       
199                 }
200         }
201         d_printf("Connection closed\n");
202         exit(0);
203 }
204
205
206 static void start_filter(char *desthost)
207 {
208         int s, c;
209         struct sockaddr_storage dest_ss;
210         struct sockaddr_storage my_ss;
211
212         CatchChild();
213
214         /* start listening on port 445 locally */
215
216         zero_sockaddr(&my_ss);
217         s = open_socket_in(SOCK_STREAM, 445, 0, &my_ss, True);
218         
219         if (s == -1) {
220                 d_printf("bind failed\n");
221                 exit(1);
222         }
223
224         if (listen(s, 5) == -1) {
225                 d_printf("listen failed\n");
226         }
227
228         if (!resolve_name(desthost, &dest_ss, 0x20)) {
229                 d_printf("Unable to resolve host %s\n", desthost);
230                 exit(1);
231         }
232
233         while (1) {
234                 fd_set fds;
235                 int num;
236                 struct sockaddr_storage ss;
237                 socklen_t in_addrlen = sizeof(ss);
238                 
239                 FD_ZERO(&fds);
240                 FD_SET(s, &fds);
241
242                 num = sys_select_intr(s+1,&fds,NULL,NULL,NULL);
243                 if (num > 0) {
244                         c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
245                         if (c != -1) {
246                                 if (fork() == 0) {
247                                         close(s);
248                                         filter_child(c, &dest_ss);
249                                         exit(0);
250                                 } else {
251                                         close(c);
252                                 }
253                         }
254                 }
255         }
256 }
257
258
259 int main(int argc, char *argv[])
260 {
261         char *desthost;
262         const char *configfile;
263         TALLOC_CTX *frame = talloc_stackframe();
264
265         load_case_tables();
266
267         setup_logging(argv[0],True);
268
269         configfile = get_dyn_CONFIGFILE();
270
271         if (argc < 2) {
272                 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
273                 exit(1);
274         }
275
276         desthost = argv[1];
277         if (argc > 2) {
278                 netbiosname = argv[2];
279         }
280
281         if (!lp_load(configfile,True,False,False,True)) {
282                 d_printf("Unable to load config file\n");
283         }
284
285         start_filter(desthost);
286         TALLOC_FREE(frame);
287         return 0;
288 }