selftest/Samba4: create add ${TRUST_DOMSID}-513 to a local group
[Samba.git] / source3 / utils / smbfilter.c
blob85aca4f9334c200ddbb261ce2b2b046d6e869e64
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 "system/filesys.h"
22 #include "system/select.h"
23 #include "../lib/util/select.h"
24 #include "libsmb/nmblib.h"
25 #include "lib/util/sys_rw_data.h"
27 #define SECURITY_MASK 0
28 #define SECURITY_SET 0
30 /* this forces non-unicode */
31 #define CAPABILITY_MASK 0
32 #define CAPABILITY_SET 0
34 /* and non-unicode for the client too */
35 #define CLI_CAPABILITY_MASK 0
36 #define CLI_CAPABILITY_SET 0
38 static char *netbiosname;
40 static void save_file(const char *fname, void *ppacket, size_t length)
42 int fd;
43 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
44 if (fd == -1) {
45 perror(fname);
46 return;
48 if (write(fd, ppacket, length) != length) {
49 fprintf(stderr,"Failed to write %s\n", fname);
50 close(fd);
51 return;
53 close(fd);
54 printf("Wrote %ld bytes to %s\n", (unsigned long)length, fname);
57 static void filter_reply(char *buf)
59 int msg_type = CVAL(buf,0);
60 int type = CVAL(buf,smb_com);
61 unsigned x;
63 if (msg_type) return;
65 switch (type) {
67 case SMBnegprot:
68 /* force the security bits */
69 x = CVAL(buf, smb_vwv1);
70 x = (x | SECURITY_SET) & ~SECURITY_MASK;
71 SCVAL(buf, smb_vwv1, x);
73 /* force the capabilities */
74 x = IVAL(buf,smb_vwv9+1);
75 x = (x | CAPABILITY_SET) & ~CAPABILITY_MASK;
76 SIVAL(buf, smb_vwv9+1, x);
77 break;
82 static void filter_request(char *buf, size_t buf_len)
84 int msg_type = CVAL(buf,0);
85 int type = CVAL(buf,smb_com);
86 unsigned x;
87 fstring name1,name2;
88 int name_len1 = 0;
89 int name_len2;
90 int name_type1, name_type2;
91 int ret;
93 if (msg_type) {
94 /* it's a netbios special */
95 switch (msg_type)
96 case 0x81:
97 /* session request */
98 /* inbuf_size is guaranteed to be at least 4. */
99 name_len1 = name_len((unsigned char *)(buf+4),
100 buf_len - 4);
101 if (name_len1 <= 0 || name_len1 > buf_len - 4) {
102 DEBUG(0,("Invalid name length in session request\n"));
103 return;
105 name_len2 = name_len((unsigned char *)(buf+4+name_len1),
106 buf_len - 4 - name_len1);
107 if (name_len2 <= 0 || name_len2 > buf_len - 4 - name_len1) {
108 DEBUG(0,("Invalid name length in session request\n"));
109 return;
112 name_type1 = name_extract((unsigned char *)buf,
113 buf_len,(unsigned int)4,name1);
114 name_type2 = name_extract((unsigned char *)buf,
115 buf_len,(unsigned int)(4 + name_len1),name2);
117 if (name_type1 == -1 || name_type2 == -1) {
118 DEBUG(0,("Invalid name type in session request\n"));
119 return;
122 d_printf("sesion_request: %s -> %s\n",
123 name1, name2);
124 if (netbiosname) {
125 char *mangled = name_mangle(
126 talloc_tos(), netbiosname, 0x20);
127 if (mangled != NULL) {
128 /* replace the destination netbios
129 * name */
130 memcpy(buf+4, mangled,
131 name_len((unsigned char *)mangled,
132 talloc_get_size(mangled)));
133 TALLOC_FREE(mangled);
136 return;
139 /* it's an ordinary SMB request */
140 switch (type) {
141 case SMBsesssetupX:
142 /* force the client capabilities */
143 x = IVAL(buf,smb_vwv11);
144 d_printf("SMBsesssetupX cap=0x%08x\n", x);
145 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
146 ret = system("mv sessionsetup.dat sessionsetup1.dat");
147 if (ret == -1) {
148 DBG_ERR("failed to call mv command\n");
150 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
151 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
152 SIVAL(buf, smb_vwv11, x);
153 break;
157 /****************************************************************************
158 Send an smb to a fd.
159 ****************************************************************************/
161 static bool send_smb(int fd, char *buffer)
163 size_t len;
164 size_t nwritten=0;
165 ssize_t ret;
167 len = smb_len(buffer) + 4;
169 while (nwritten < len) {
170 ret = write_data(fd,buffer+nwritten,len - nwritten);
171 if (ret <= 0) {
172 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
173 (int)len,(int)ret, strerror(errno) ));
174 return false;
176 nwritten += ret;
179 return true;
182 static void filter_child(int c, struct sockaddr_storage *dest_ss)
184 NTSTATUS status;
185 int s = -1;
186 char packet[128*1024];
188 /* we have a connection from a new client, now connect to the server */
189 status = open_socket_out(dest_ss, TCP_SMB_PORT, LONG_CONNECT_TIMEOUT, &s);
190 if (!NT_STATUS_IS_OK(status)) {
191 char addr[INET6_ADDRSTRLEN];
192 if (dest_ss) {
193 print_sockaddr(addr, sizeof(addr), dest_ss);
196 d_printf("Unable to connect to %s (%s)\n",
197 dest_ss?addr:"NULL", nt_errstr(status));
198 exit(1);
201 while (c != -1 || s != -1) {
202 struct pollfd fds[2];
203 int num_fds, ret;
205 memset(fds, 0, sizeof(struct pollfd) * 2);
206 fds[0].fd = -1;
207 fds[1].fd = -1;
208 num_fds = 0;
210 if (s != -1) {
211 fds[num_fds].fd = s;
212 fds[num_fds].events = POLLIN|POLLHUP;
213 num_fds += 1;
215 if (c != -1) {
216 fds[num_fds].fd = c;
217 fds[num_fds].events = POLLIN|POLLHUP;
218 num_fds += 1;
221 ret = sys_poll_intr(fds, num_fds, -1);
222 if (ret <= 0) {
223 continue;
227 * find c in fds and see if it's readable
229 if ((c != -1) &&
230 (((fds[0].fd == c)
231 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
232 ((fds[1].fd == c)
233 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
234 size_t len;
235 if (!NT_STATUS_IS_OK(receive_smb_raw(
236 c, packet, sizeof(packet),
237 0, 0, &len))) {
238 d_printf("client closed connection\n");
239 exit(0);
241 filter_request(packet, len);
242 if (!send_smb(s, packet)) {
243 d_printf("server is dead\n");
244 exit(1);
249 * find s in fds and see if it's readable
251 if ((s != -1) &&
252 (((fds[0].fd == s)
253 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
254 ((fds[1].fd == s)
255 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
256 size_t len;
257 if (!NT_STATUS_IS_OK(receive_smb_raw(
258 s, packet, sizeof(packet),
259 0, 0, &len))) {
260 d_printf("server closed connection\n");
261 exit(0);
263 filter_reply(packet);
264 if (!send_smb(c, packet)) {
265 d_printf("client is dead\n");
266 exit(1);
270 d_printf("Connection closed\n");
271 exit(0);
275 static void start_filter(char *desthost)
277 int s, c;
278 struct sockaddr_storage dest_ss;
279 struct sockaddr_storage my_ss;
281 CatchChild();
283 /* start listening on port 445 locally */
285 zero_sockaddr(&my_ss);
286 s = open_socket_in(SOCK_STREAM, TCP_SMB_PORT, 0, &my_ss, True);
288 if (s == -1) {
289 d_printf("bind failed\n");
290 exit(1);
293 if (listen(s, 5) == -1) {
294 d_printf("listen failed\n");
297 if (!resolve_name(desthost, &dest_ss, 0x20, false)) {
298 d_printf("Unable to resolve host %s\n", desthost);
299 exit(1);
302 while (1) {
303 int num, revents;
304 struct sockaddr_storage ss;
305 socklen_t in_addrlen = sizeof(ss);
307 num = poll_intr_one_fd(s, POLLIN|POLLHUP, -1, &revents);
308 if ((num > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
309 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
310 if (c != -1) {
311 smb_set_close_on_exec(c);
312 if (fork() == 0) {
313 close(s);
314 filter_child(c, &dest_ss);
315 exit(0);
316 } else {
317 close(c);
325 int main(int argc, char *argv[])
327 char *desthost;
328 const char *configfile;
329 TALLOC_CTX *frame = talloc_stackframe();
331 smb_init_locale();
333 setup_logging(argv[0], DEBUG_STDOUT);
335 configfile = get_dyn_CONFIGFILE();
337 if (argc < 2) {
338 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
339 exit(1);
342 desthost = argv[1];
343 if (argc > 2) {
344 netbiosname = argv[2];
347 if (!lp_load_global(configfile)) {
348 d_printf("Unable to load config file\n");
351 start_filter(desthost);
352 TALLOC_FREE(frame);
353 return 0;