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