summaryrefslogtreecommitdiff
path: root/other/ssharp/ssharp.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/ssharp/ssharp.c')
-rw-r--r--other/ssharp/ssharp.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/other/ssharp/ssharp.c b/other/ssharp/ssharp.c
new file mode 100644
index 0000000..6733aa6
--- /dev/null
+++ b/other/ssharp/ssharp.c
@@ -0,0 +1,162 @@
1/* SSHARP SSH 1&2 MiM implemenation (C) 2001 Stealth <stealth@segfault.net>
2 *
3 * TESO confidential.
4 */
5#include <stdio.h>
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <string.h>
9#include <netinet/in.h>
10#include <errno.h>
11#include <assert.h>
12
13#include "ssharp.h"
14#include "auth.h"
15
16
17int socket_connect_b(struct sockaddr *s, socklen_t len, u_short minport)
18{
19 int one = 1, i = 0, fd, fa;
20 struct sockaddr_in local4;
21 struct sockaddr_in6 local6;
22 struct sockaddr *sa;
23
24 memset(&local4, 0, sizeof(local4));
25 local4.sin_family = AF_INET;
26
27 memset(&local6, 0, sizeof(local6));
28 local6.sin6_family = AF_INET6;
29
30 if (len == sizeof(struct sockaddr_in6)) {
31 sa = (struct sockaddr*)&local6;
32 fa = PF_INET6;
33 } else {
34 sa = (struct sockaddr*)&local4;
35 fa = PF_INET;
36 }
37
38 for (i = minport; i < 65500; ++i) {
39 local4.sin_port = htons(i);
40 local6.sin6_port = htons(i);
41
42 fd = socket(fa, SOCK_STREAM, 0);
43 if (fd < 0)
44 return -1;
45 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
46
47 if (bind(fd, sa, len) < 0) {
48 if (errno == EADDRINUSE) {
49 close(fd);
50 continue;
51 } else
52 return -1;
53 }
54 if (connect(fd, s, len) < 0) {
55 if (errno == EADDRNOTAVAIL) {
56 close(fd);
57 continue;
58 } else
59 return -1;
60 }
61
62 break;
63 }
64 return fd;
65}
66
67
68int writen(int fd, const void *buf, size_t len)
69{
70 int o = 0, n;
71
72 while (len > 0) {
73 if ((n = write(fd, buf+o, len)) < 0)
74 return n;
75 len -= n;
76 o += n;
77 }
78 return o;
79}
80
81
82int readn(int fd, char *buf, size_t len)
83{
84 int i = 0;
85 while (len > 0) {
86 if (read(fd, &buf[i], 1) < 0) {
87 return -1;
88 }
89 ++i;
90 --len;
91 }
92 return i;
93}
94
95sharp_t sharp_dup(sharp_t *s)
96{
97 sharp_t ret;
98
99 memset(&ret, 0, sizeof(ret));
100
101 if (s->login)
102 ret.login = strdup(s->login);
103 if (s->pass)
104 ret.pass = strdup(s->pass);
105 if (s->remote)
106 ret.remote = strdup(s->remote);
107
108 ret.remote_port = s->remote_port;
109 return ret;
110}
111
112#ifdef FREEBSD
113#define LINUX22
114#endif
115
116// obtain real destination of connection
117int dstaddr(int sock, struct sockaddr_in *dst)
118{
119 socklen_t size;
120
121 assert(dst);
122
123#ifdef LINUX22
124 size = sizeof(struct sockaddr_in);
125 if (getsockname(sock, (struct sockaddr*)dst, &size) < 0) {
126 perror("getsockname");
127 exit(errno);
128 }
129#elif defined(LINUX24)
130#include "netfilter.h"
131 size = sizeof(struct sockaddr_in);
132 if (getsockopt(sock, SOL_IP, SO_ORIGINAL_DST, dst, &size) < 0) {
133 perror("getsockopt");
134 exit(errno);
135 }
136#else
137#error "Not supported on this OS yet."
138#endif
139 return 0;
140}
141
142Authctxt *auth_dup(Authctxt *a)
143{
144 Authctxt *ret = (Authctxt*)malloc(sizeof(*a));
145
146 if (!a || !ret) {
147 free(ret);
148 return NULL;
149 }
150
151 *ret = *a;
152 if (a->user)
153 ret->user = strdup(a->user);
154 if (a->service)
155 ret->service = strdup(a->service);
156 if (a->style)
157 ret->style = strdup(a->style);
158 ret->sharp = sharp_dup(&a->sharp);
159
160 return ret;
161}
162