8 #include <sys/socket.h>
13 #include <sys/types.h>
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
18 #define MAX(a,b) ((a)>(b)?(a):(b))
20 #define SEARCH "GSS-SPNEGO"
21 #define REPLACE "GSS-XXXXXX"
23 #define SEARCH2 "NTLMSSP"
24 #define REPLACE2 "NTLMXXX"
26 static void replace_str(char *buf, int n)
29 p = memmem(buf, n, SEARCH, strlen(SEARCH));
31 printf("Replaced %s with %s\n", SEARCH, REPLACE);
32 memcpy(p, REPLACE, strlen(REPLACE));
34 p = memmem(buf, n, SEARCH2, strlen(SEARCH2));
36 printf("Replaced %s with %s\n", SEARCH2, REPLACE2);
37 memcpy(p, REPLACE2, strlen(REPLACE2));
41 /* open a socket to a tcp remote host with the specified port */
42 static int open_socket_out(const char *host, int port)
44 struct sockaddr_in sock_out;
49 res = socket(PF_INET, SOCK_STREAM, 0);
54 if (inet_pton(AF_INET, host, &addr) > 0) {
55 memcpy(&sock_out.sin_addr, &addr, sizeof(addr));
57 hp = gethostbyname(host);
59 fprintf(stderr,"unknown host %s\n", host);
62 memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
65 sock_out.sin_port = htons(port);
66 sock_out.sin_family = PF_INET;
68 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out)) != 0) {
70 fprintf(stderr,"failed to connect to %s (%s)\n",
71 host, strerror(errno));
80 open a socket of the specified type, port and address for incoming data
82 int open_socket_in(int port)
84 struct sockaddr_in sock;
88 memset(&sock,0,sizeof(sock));
90 #ifdef HAVE_SOCK_SIN_LEN
91 sock.sin_len = sizeof(sock);
93 sock.sin_port = htons(port);
94 sock.sin_family = AF_INET;
96 res = socket(AF_INET, SOCK_STREAM, 0);
98 fprintf(stderr, "socket failed\n"); return -1;
102 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
104 if (bind(res, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
111 /* write to a file descriptor, making sure we get all the data out or
113 static void write_all(int fd, unsigned char *s, size_t n)
126 static void main_loop(int sock1, int sock2)
128 unsigned char buf[1024];
132 char *fname1, *fname2;
133 asprintf(&fname1, "sockspy-in.%d", i);
134 asprintf(&fname2, "sockspy-out.%d", i);
135 log1 = open(fname1, O_WRONLY|O_CREAT|O_EXCL, 0644);
136 log2 = open(fname2, O_WRONLY|O_CREAT|O_EXCL, 0644);
140 } while (i<1000 && (log1 == -1 || log2 == -1));
142 if (log1 == -1 || log2 == -1) {
143 fprintf(stderr,"Failed to open log files\n");
155 ret = select(MAX(sock1, sock2)+1, &fds, NULL, NULL, NULL);
156 if (ret == -1 && errno == EINTR) continue;
159 if (FD_ISSET(sock1, &fds)) {
160 int n = read(sock1, buf, sizeof(buf));
165 write_all(sock2, buf, n);
166 write_all(log1, buf, n);
169 if (FD_ISSET(sock2, &fds)) {
170 int n = read(sock2, buf, sizeof(buf));
175 write_all(sock1, buf, n);
176 write_all(log2, buf, n);
181 int main(int argc, char *argv[])
183 int listen_port, dest_port;
188 struct sockaddr addr;
189 int in_addrlen = sizeof(addr);
192 printf("Usage: sockspy <inport> <host> <port>\n");
196 listen_port = atoi(argv[1]);
198 dest_port = atoi(argv[3]);
200 listen_fd = open_socket_in(listen_port);
202 if (listen_fd == -1) {
203 fprintf(stderr,"listen on port %d failed - %s\n",
204 listen_port, strerror(errno));
208 if (listen(listen_fd, 5) == -1) {
209 fprintf(stderr,"listen failed\n");
213 sock_in = accept(listen_fd,&addr,&in_addrlen);
216 fprintf(stderr,"accept on port %d failed - %s\n",
217 listen_port, strerror(errno));
223 sock_out = open_socket_out(host, dest_port);
224 if (sock_out == -1) {
228 main_loop(sock_in, sock_out);