build: set shared libraries flags correctly on mac os X
[Samba/gebeck_regimport.git] / source3 / utils / smbfilter.c
blob9d3e46825ffa22f890df5c18b66fb3c849ffb52d
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"
21 #include "../lib/util/select.h"
23 #define SECURITY_MASK 0
24 #define SECURITY_SET 0
26 /* this forces non-unicode */
27 #define CAPABILITY_MASK 0
28 #define CAPABILITY_SET 0
30 /* and non-unicode for the client too */
31 #define CLI_CAPABILITY_MASK 0
32 #define CLI_CAPABILITY_SET 0
34 static char *netbiosname;
35 static char packet[BUFFER_SIZE];
37 static void save_file(const char *fname, void *ppacket, size_t length)
39 int fd;
40 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
41 if (fd == -1) {
42 perror(fname);
43 return;
45 if (write(fd, ppacket, length) != length) {
46 fprintf(stderr,"Failed to write %s\n", fname);
47 return;
49 close(fd);
50 printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
53 static void filter_reply(char *buf)
55 int msg_type = CVAL(buf,0);
56 int type = CVAL(buf,smb_com);
57 unsigned x;
59 if (msg_type) return;
61 switch (type) {
63 case SMBnegprot:
64 /* force the security bits */
65 x = CVAL(buf, smb_vwv1);
66 x = (x | SECURITY_SET) & ~SECURITY_MASK;
67 SCVAL(buf, smb_vwv1, x);
69 /* force the capabilities */
70 x = IVAL(buf,smb_vwv9+1);
71 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
72 SIVAL(buf, smb_vwv9+1, x);
73 break;
78 static void filter_request(char *buf, size_t buf_len)
80 int msg_type = CVAL(buf,0);
81 int type = CVAL(buf,smb_com);
82 unsigned x;
83 fstring name1,name2;
84 int name_len1, name_len2;
85 int name_type1, name_type2;
87 if (msg_type) {
88 /* it's a netbios special */
89 switch (msg_type)
90 case 0x81:
91 /* session request */
92 /* inbuf_size is guaranteed to be at least 4. */
93 name_len1 = name_len((unsigned char *)(buf+4),
94 buf_len - 4);
95 if (name_len1 <= 0 || name_len1 > buf_len - 4) {
96 DEBUG(0,("Invalid name length in session request\n"));
97 return;
99 name_len2 = name_len((unsigned char *)(buf+4+name_len1),
100 buf_len - 4 - name_len1);
101 if (name_len2 <= 0 || name_len2 > buf_len - 4 - name_len1) {
102 DEBUG(0,("Invalid name length in session request\n"));
103 return;
106 name_type1 = name_extract((unsigned char *)buf,
107 buf_len,(unsigned int)4,name1);
108 name_type2 = name_extract((unsigned char *)buf,
109 buf_len,(unsigned int)(4 + name_len1),name2);
111 if (name_type1 == -1 || name_type2 == -1) {
112 DEBUG(0,("Invalid name type in session request\n"));
113 return;
116 d_printf("sesion_request: %s -> %s\n",
117 name1, name2);
118 if (netbiosname) {
119 char *mangled = name_mangle(
120 talloc_tos(), netbiosname, 0x20);
121 if (mangled != NULL) {
122 /* replace the destination netbios
123 * name */
124 memcpy(buf+4, mangled,
125 name_len((unsigned char *)mangled,
126 talloc_get_size(mangled)));
127 TALLOC_FREE(mangled);
130 return;
133 /* it's an ordinary SMB request */
134 switch (type) {
135 case SMBsesssetupX:
136 /* force the client capabilities */
137 x = IVAL(buf,smb_vwv11);
138 d_printf("SMBsesssetupX cap=0x%08x\n", x);
139 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
140 system("mv sessionsetup.dat sessionsetup1.dat");
141 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
142 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
143 SIVAL(buf, smb_vwv11, x);
144 break;
148 /****************************************************************************
149 Send an smb to a fd.
150 ****************************************************************************/
152 static bool send_smb(int fd, char *buffer)
154 size_t len;
155 size_t nwritten=0;
156 ssize_t ret;
158 len = smb_len(buffer) + 4;
160 while (nwritten < len) {
161 ret = write_data(fd,buffer+nwritten,len - nwritten);
162 if (ret <= 0) {
163 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
164 (int)len,(int)ret, strerror(errno) ));
165 return false;
167 nwritten += ret;
170 return true;
173 static void filter_child(int c, struct sockaddr_storage *dest_ss)
175 NTSTATUS status;
176 int s = -1;
178 /* we have a connection from a new client, now connect to the server */
179 status = open_socket_out(dest_ss, 445, LONG_CONNECT_TIMEOUT, &s);
181 if (s == -1) {
182 char addr[INET6_ADDRSTRLEN];
183 if (dest_ss) {
184 print_sockaddr(addr, sizeof(addr), dest_ss);
187 d_printf("Unable to connect to %s (%s)\n",
188 dest_ss?addr:"NULL",strerror(errno));
189 exit(1);
192 while (c != -1 || s != -1) {
193 fd_set fds;
194 int num;
196 FD_ZERO(&fds);
197 if (s != -1) FD_SET(s, &fds);
198 if (c != -1) FD_SET(c, &fds);
200 num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
201 if (num <= 0) continue;
203 if (c != -1 && FD_ISSET(c, &fds)) {
204 size_t len;
205 if (!NT_STATUS_IS_OK(receive_smb_raw(
206 c, packet, sizeof(packet),
207 0, 0, &len))) {
208 d_printf("client closed connection\n");
209 exit(0);
211 filter_request(packet, len);
212 if (!send_smb(s, packet)) {
213 d_printf("server is dead\n");
214 exit(1);
217 if (s != -1 && FD_ISSET(s, &fds)) {
218 size_t len;
219 if (!NT_STATUS_IS_OK(receive_smb_raw(
220 s, packet, sizeof(packet),
221 0, 0, &len))) {
222 d_printf("server closed connection\n");
223 exit(0);
225 filter_reply(packet);
226 if (!send_smb(c, packet)) {
227 d_printf("client is dead\n");
228 exit(1);
232 d_printf("Connection closed\n");
233 exit(0);
237 static void start_filter(char *desthost)
239 int s, c;
240 struct sockaddr_storage dest_ss;
241 struct sockaddr_storage my_ss;
243 CatchChild();
245 /* start listening on port 445 locally */
247 zero_sockaddr(&my_ss);
248 s = open_socket_in(SOCK_STREAM, 445, 0, &my_ss, True);
250 if (s == -1) {
251 d_printf("bind failed\n");
252 exit(1);
255 if (listen(s, 5) == -1) {
256 d_printf("listen failed\n");
259 if (!resolve_name(desthost, &dest_ss, 0x20, false)) {
260 d_printf("Unable to resolve host %s\n", desthost);
261 exit(1);
264 while (1) {
265 fd_set fds;
266 int num;
267 struct sockaddr_storage ss;
268 socklen_t in_addrlen = sizeof(ss);
270 FD_ZERO(&fds);
271 FD_SET(s, &fds);
273 num = sys_select_intr(s+1,&fds,NULL,NULL,NULL);
274 if (num > 0) {
275 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
276 if (c != -1) {
277 if (fork() == 0) {
278 close(s);
279 filter_child(c, &dest_ss);
280 exit(0);
281 } else {
282 close(c);
290 int main(int argc, char *argv[])
292 char *desthost;
293 const char *configfile;
294 TALLOC_CTX *frame = talloc_stackframe();
296 load_case_tables();
298 setup_logging(argv[0],True);
300 configfile = get_dyn_CONFIGFILE();
302 if (argc < 2) {
303 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
304 exit(1);
307 desthost = argv[1];
308 if (argc > 2) {
309 netbiosname = argv[2];
312 if (!lp_load(configfile,True,False,False,True)) {
313 d_printf("Unable to load config file\n");
316 start_filter(desthost);
317 TALLOC_FREE(frame);
318 return 0;