ctdb: Accept the key in hex format for the pstore command
[Samba.git] / source3 / utils / smbfilter.c
blob9b871beda8e017a4fdd91f10487e24a04f8a15a9
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/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;
92 if (msg_type) {
93 /* it's a netbios special */
94 switch (msg_type)
95 case 0x81:
96 /* session request */
97 /* inbuf_size is guaranteed to be at least 4. */
98 name_len1 = name_len((unsigned char *)(buf+4),
99 buf_len - 4);
100 if (name_len1 <= 0 || name_len1 > buf_len - 4) {
101 DEBUG(0,("Invalid name length in session request\n"));
102 return;
104 name_len2 = name_len((unsigned char *)(buf+4+name_len1),
105 buf_len - 4 - name_len1);
106 if (name_len2 <= 0 || name_len2 > buf_len - 4 - name_len1) {
107 DEBUG(0,("Invalid name length in session request\n"));
108 return;
111 name_type1 = name_extract((unsigned char *)buf,
112 buf_len,(unsigned int)4,name1);
113 name_type2 = name_extract((unsigned char *)buf,
114 buf_len,(unsigned int)(4 + name_len1),name2);
116 if (name_type1 == -1 || name_type2 == -1) {
117 DEBUG(0,("Invalid name type in session request\n"));
118 return;
121 d_printf("sesion_request: %s -> %s\n",
122 name1, name2);
123 if (netbiosname) {
124 char *mangled = name_mangle(
125 talloc_tos(), netbiosname, 0x20);
126 if (mangled != NULL) {
127 /* replace the destination netbios
128 * name */
129 memcpy(buf+4, mangled,
130 name_len((unsigned char *)mangled,
131 talloc_get_size(mangled)));
132 TALLOC_FREE(mangled);
135 return;
138 /* it's an ordinary SMB request */
139 switch (type) {
140 case SMBsesssetupX:
141 /* force the client capabilities */
142 x = IVAL(buf,smb_vwv11);
143 d_printf("SMBsesssetupX cap=0x%08x\n", x);
144 d_printf("pwlen=%d/%d\n", SVAL(buf, smb_vwv7), SVAL(buf, smb_vwv8));
145 system("mv sessionsetup.dat sessionsetup1.dat");
146 save_file("sessionsetup.dat", smb_buf(buf), SVAL(buf, smb_vwv7));
147 x = (x | CLI_CAPABILITY_SET) & ~CLI_CAPABILITY_MASK;
148 SIVAL(buf, smb_vwv11, x);
149 break;
153 /****************************************************************************
154 Send an smb to a fd.
155 ****************************************************************************/
157 static bool send_smb(int fd, char *buffer)
159 size_t len;
160 size_t nwritten=0;
161 ssize_t ret;
163 len = smb_len(buffer) + 4;
165 while (nwritten < len) {
166 ret = write_data(fd,buffer+nwritten,len - nwritten);
167 if (ret <= 0) {
168 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
169 (int)len,(int)ret, strerror(errno) ));
170 return false;
172 nwritten += ret;
175 return true;
178 static void filter_child(int c, struct sockaddr_storage *dest_ss)
180 NTSTATUS status;
181 int s = -1;
182 char packet[128*1024];
184 /* we have a connection from a new client, now connect to the server */
185 status = open_socket_out(dest_ss, TCP_SMB_PORT, LONG_CONNECT_TIMEOUT, &s);
186 if (!NT_STATUS_IS_OK(status)) {
187 char addr[INET6_ADDRSTRLEN];
188 if (dest_ss) {
189 print_sockaddr(addr, sizeof(addr), dest_ss);
192 d_printf("Unable to connect to %s (%s)\n",
193 dest_ss?addr:"NULL", nt_errstr(status));
194 exit(1);
197 while (c != -1 || s != -1) {
198 struct pollfd fds[2];
199 int num_fds, ret;
201 memset(fds, 0, sizeof(struct pollfd) * 2);
202 fds[0].fd = -1;
203 fds[1].fd = -1;
204 num_fds = 0;
206 if (s != -1) {
207 fds[num_fds].fd = s;
208 fds[num_fds].events = POLLIN|POLLHUP;
209 num_fds += 1;
211 if (c != -1) {
212 fds[num_fds].fd = c;
213 fds[num_fds].events = POLLIN|POLLHUP;
214 num_fds += 1;
217 ret = sys_poll_intr(fds, num_fds, -1);
218 if (ret <= 0) {
219 continue;
223 * find c in fds and see if it's readable
225 if ((c != -1) &&
226 (((fds[0].fd == c)
227 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
228 ((fds[1].fd == c)
229 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
230 size_t len;
231 if (!NT_STATUS_IS_OK(receive_smb_raw(
232 c, packet, sizeof(packet),
233 0, 0, &len))) {
234 d_printf("client closed connection\n");
235 exit(0);
237 filter_request(packet, len);
238 if (!send_smb(s, packet)) {
239 d_printf("server is dead\n");
240 exit(1);
245 * find s in fds and see if it's readable
247 if ((s != -1) &&
248 (((fds[0].fd == s)
249 && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
250 ((fds[1].fd == s)
251 && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
252 size_t len;
253 if (!NT_STATUS_IS_OK(receive_smb_raw(
254 s, packet, sizeof(packet),
255 0, 0, &len))) {
256 d_printf("server closed connection\n");
257 exit(0);
259 filter_reply(packet);
260 if (!send_smb(c, packet)) {
261 d_printf("client is dead\n");
262 exit(1);
266 d_printf("Connection closed\n");
267 exit(0);
271 static void start_filter(char *desthost)
273 int s, c;
274 struct sockaddr_storage dest_ss;
275 struct sockaddr_storage my_ss;
277 CatchChild();
279 /* start listening on port 445 locally */
281 zero_sockaddr(&my_ss);
282 s = open_socket_in(SOCK_STREAM, TCP_SMB_PORT, 0, &my_ss, True);
284 if (s == -1) {
285 d_printf("bind failed\n");
286 exit(1);
289 if (listen(s, 5) == -1) {
290 d_printf("listen failed\n");
293 if (!resolve_name(desthost, &dest_ss, 0x20, false)) {
294 d_printf("Unable to resolve host %s\n", desthost);
295 exit(1);
298 while (1) {
299 int num, revents;
300 struct sockaddr_storage ss;
301 socklen_t in_addrlen = sizeof(ss);
303 num = poll_intr_one_fd(s, POLLIN|POLLHUP, -1, &revents);
304 if ((num > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
305 c = accept(s, (struct sockaddr *)&ss, &in_addrlen);
306 if (c != -1) {
307 if (fork() == 0) {
308 close(s);
309 filter_child(c, &dest_ss);
310 exit(0);
311 } else {
312 close(c);
320 int main(int argc, char *argv[])
322 char *desthost;
323 const char *configfile;
324 TALLOC_CTX *frame = talloc_stackframe();
326 smb_init_locale();
328 setup_logging(argv[0], DEBUG_STDOUT);
330 configfile = get_dyn_CONFIGFILE();
332 if (argc < 2) {
333 fprintf(stderr,"smbfilter <desthost> <netbiosname>\n");
334 exit(1);
337 desthost = argv[1];
338 if (argc > 2) {
339 netbiosname = argv[2];
342 if (!lp_load_global(configfile)) {
343 d_printf("Unable to load config file\n");
346 start_filter(desthost);
347 TALLOC_FREE(frame);
348 return 0;