forked from cooldavid/tsps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsps.c
285 lines (256 loc) · 7.12 KB
/
tsps.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* TSP Server
*
* A TSP Server implementation that follows RFC5572 as much as possible.
* It is designed to be compatible with FreeNET6 service.
*
* Copyright (C) 2011 Guo-Fu Tseng <cooldavid@cooldavid.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tsps.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
struct tspserver server;
static int parse_args(int argc, char *argv[])
{
int opt;
uint16_t port;
int len;
char *prelen, *eptr;
char v6addr[64];
server.mode = UNDEFINED_MODE;
server.v4sockaddr.sin_family = AF_INET;
server.v6sockaddr.sin6_family = AF_INET6;
server.v4sockaddr.sin_port = htons(3653);
while ((opt = getopt(argc, argv, "t:b:p:n:NAHh:u:P:d:Dl:B:")) != -1) {
switch (opt) {
case 't':
strncpy(server.tundev, optarg, 31);
server.tundev[31] = '\0';
break;
case 'b':
if (inet_pton(AF_INET, optarg, &server.v4sockaddr.sin_addr) <= 0) {
fprintf(stderr, "Address error: %s\n", optarg);
return -1;
}
break;
case 'p':
port = strtol(optarg, &eptr, 10);
if (*eptr != '\0' || eptr == optarg) {
fprintf(stderr, "Port number error: %s\n", optarg);
return -1;
}
if (port <= 0 || port > 65535) {
fprintf(stderr, "Port error: %d(%s)\n", port, optarg);
return -1;
}
server.v4sockaddr.sin_port = htons(port);
break;
case 'n':
prelen = strchr(optarg, '/');
if (!prelen || prelen != strrchr(optarg, '/')) {
fprintf(stderr, "Address prefix error: %s\n", optarg);
return -1;
}
server.v6prefixlen = strtol(prelen + 1, &eptr, 10);
if (*eptr != '\0' || eptr == (prelen + 1)) {
fprintf(stderr, "Address prefix error: %s\n", prelen + 1);
return -1;
}
if (server.v6prefixlen > 80 || server.v6prefixlen < 32) {
fprintf(stderr, "Prefix length must between 32 and 80\n");
return -1;
}
strncpy(v6addr, optarg, 64);
v6addr[63] = '\0';
prelen = strchr(v6addr, '/');
if (prelen)
*prelen = '\0';
if (inet_pton(AF_INET6, v6addr, &server.v6sockaddr.sin6_addr) <= 0) {
fprintf(stderr, "Address error: %s\n", v6addr);
return -1;
}
break;
case 'N':
if (server.mode != UNDEFINED_MODE) {
fprintf(stderr, "Can not set mode twice.\n");
return -1;
}
server.mode = ANONYMOUS_MODE;
break;
case 'A':
if (server.mode != UNDEFINED_MODE) {
fprintf(stderr, "Can not set mode twice.\n");
return -1;
}
server.mode = AUTHENTICATED_MODE;
break;
case 'H':
if (server.mode != UNDEFINED_MODE) {
fprintf(stderr, "Can not set mode twice.\n");
return -1;
}
server.mode = HYBRID_MODE;
break;
case 'h':
len = strlen(optarg);
server.dbhost = malloc(len + 1);
strcpy(server.dbhost, optarg);
break;
case 'u':
len = strlen(optarg);
server.dbuser = malloc(len + 1);
strcpy(server.dbuser, optarg);
break;
case 'P':
len = strlen(optarg);
server.dbpass = malloc(len + 1);
strcpy(server.dbpass, optarg);
memset(optarg, '*', len);
break;
case 'd':
len = strlen(optarg);
server.dbname = malloc(len + 1);
strcpy(server.dbname, optarg);
break;
case 'D':
server.debug = 1;
break;
case 'l':
server.ldap_uri = strdup(optarg);
break;
case 'B':
server.ldap_user_base = strdup(optarg);
break;
default:
return -1;
}
}
if (server.mode == UNDEFINED_MODE)
server.mode = ANONYMOUS_MODE;
return 0;
}
static void usage(const char *progname)
{
fprintf(stderr, "\n"
"Usage: %s %s -b IPv4_bind_address [-p IPv4_bind_port] -n IPv6_prefix\n"
" [-A|-N|-H] [-D]\n"
" [-h MySQL_Host -u MySQL_User -P MySQL_Pass -d MySQL_DBName]\n"
" [-l LDAP_URI -B LDAP_user_base]\n"
" IPv4_bind_address: Used for client to connect\n"
" ex: 123.123.123.123\n"
" IPv4_bind_port: Used for client to connect\n"
" default: 3653\n"
" IPv6_prefix: The IPv6 prefix that is allowed for client address\n"
" The value of the prefix length must between 32 and 80\n"
" ex: 2001:DB8:ABC:DEF:123::/80\n"
" -N: Anonymous mode only(Default)\n"
" -A: Authenticated mode only\n"
" -H: Hybrid(Anonymous/Authenticated) mode\n"
" -D: Debug mode: Keep standard I/O, don't fork\n",
progname,
#ifdef linux
"[-t tunX]"
#else
"-t /dev/tunX"
#endif
);
}
static int check_server_configure(void)
{
static char zeros[32];
int i, rplen = 128 - server.v6prefixlen;
uint8_t m, *mask6;
if (!memcmp(&server.v4sockaddr.sin_addr, zeros, sizeof(struct in_addr))) {
fprintf(stderr, "Must specify IPv4_bind_address\n");
return -1;
}
if (!memcmp(&server.v6sockaddr.sin6_addr, zeros, sizeof(struct in6_addr))) {
fprintf(stderr, "Must specify IPv6_prefix\n");
return -1;
}
if (server.mode != ANONYMOUS_MODE &&
(!server.dbhost || !server.dbuser || !server.dbpass || !server.dbname) &&
!server.ldap_uri) {
fprintf(stderr, "Must specify MySQL or LDAP parameters in authenticated mode\n");
return -1;
}
bzero(&server.v6postfixmask, sizeof(struct in6_addr));
mask6 = server.v6postfixmask.s6_addr;
for (i = 0; (i * 8) <= (rplen - 8); ++i)
mask6[15 - i] = 0xFFu;
if ((i * 8) < rplen) {
rplen -= i * 8;
m = (1 << rplen);
m -= 1;
mask6[15 -i] = m;
}
for (i = 0; i < 4; ++i) {
server.v6prefix.s6_addr32[i] =
server.v6sockaddr.sin6_addr.s6_addr32[i] &
~(server.v6postfixmask.s6_addr32[i]);
}
return 0;
}
int main(int argc, char *argv[], char *envv[])
{
openlog("tsps", LOG_CONS | LOG_PID, LOG_USER);
if (parse_args(argc, argv)) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (check_server_configure()) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (bind_tunif()) {
fprintf(stderr, "Binding TUN device error\n");
return EXIT_FAILURE;
}
if (tun_setaddr()) {
fprintf(stderr, "Setting address for TUN device error\n");
return EXIT_FAILURE;
}
if (bind_socket()) {
fprintf(stderr, "Binding socket error\n");
return EXIT_FAILURE;
}
if (server.mode != ANONYMOUS_MODE) {
if (server.dbhost && mysql_initialize()) {
fprintf(stderr, "Initialize MySQL error\n");
return EXIT_FAILURE;
}
if (server.ldap_uri && tsps_ldap_initialize()) {
fprintf(stderr, "Initialize LDAP error\n");
return EXIT_FAILURE;
}
}
if (!server.debug) {
close(0);
close(1);
close(2);
if (fork())
return 0;
}
if (create_threads()) {
tspslog(LOG_ERR, "Failed to create threads\n");
return EXIT_FAILURE;
}
main_loop();
return 0;
}