forked from VrayoSystems/vtrunkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.c
427 lines (349 loc) · 8.63 KB
/
auth.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
vtrunkd - Virtual Tunnel Trunking over TCP/IP network.
Copyright (C) 2011-2016 Vrayo Systems Ltd. team
Vtrunkd has been derived from VTUN package by Maxim Krasnyansky.
vtun Copyright (C) 1998-2000 Maxim Krasnyansky <[email protected]>
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 2 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.
*/
/*
* auth.c,v 1.2.2.7.2.3 2006/11/16 04:02:33 mtbishop Exp
*/
/*
* Challenge based authentication.
* Thanx to Chris Todd<[email protected]> for the good idea.
*
* Jim Yonan, 05/24/2001
* gen_chal rewrite to use better random number generator
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#include <time.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include "vtun.h"
#include "lib.h"
#include "lock.h"
#include "auth.h"
/* Encryption and Decryption of the challenge key */
#ifdef HAVE_SSL
#include <openssl/md5.h>
#include <openssl/blowfish.h>
#include <openssl/rand.h>
void gen_chal(char *buf)
{
RAND_bytes(buf, VTUN_CHAL_SIZE);
}
void encrypt_chal(char *chal, char *pwd)
{
register int i;
BF_KEY key;
BF_set_key(&key, 16, MD5(pwd,strlen(pwd),NULL));
for(i=0; i < VTUN_CHAL_SIZE; i += 8 )
BF_ecb_encrypt(chal + i, chal + i, &key, BF_ENCRYPT);
}
void decrypt_chal(char *chal, char *pwd)
{
register int i;
BF_KEY key;
BF_set_key(&key, 16, MD5(pwd,strlen(pwd),NULL));
for(i=0; i < VTUN_CHAL_SIZE; i += 8 )
BF_ecb_encrypt(chal + i, chal + i, &key, BF_DECRYPT);
}
#else /* HAVE_SSL */
void encrypt_chal(char *chal, char *pwd)
{
char * xor_msk = pwd;
register int i, xor_len = strlen(xor_msk);
for(i=0; i < VTUN_CHAL_SIZE; i++)
chal[i] ^= xor_msk[i%xor_len];
}
void decrypt_chal(char *chal, char *pwd)
{
encrypt_chal(chal, pwd);
}
/* Generate PSEUDO random challenge key. */
void gen_chal(char *buf)
{
register int i;
srand(time(NULL));
for(i=0; i < VTUN_CHAL_SIZE; i++)
buf[i] = (unsigned int)(255.0 * rand()/RAND_MAX);
}
#endif /* HAVE_SSL */
/*
* Functions to convert binary flags to character string.
* string format: <CS64>
* C - compression, S - speed for shaper and so on.
*/
char *bf2cf(struct vtun_host *host)
{
static char str[20], *ptr = str;
*(ptr++) = '<';
switch( host->flags & VTUN_PROT_MASK ){
case VTUN_TCP:
*(ptr++) = 'T';
break;
case VTUN_UDP:
*(ptr++) = 'U';
break;
}
switch( host->flags & VTUN_TYPE_MASK ){
case VTUN_TTY:
*(ptr++) = 't';
break;
case VTUN_PIPE:
*(ptr++) = 'p';
break;
case VTUN_ETHER:
*(ptr++) = 'e';
break;
case VTUN_TUN:
*(ptr++) = 'u';
break;
}
if( (host->flags & VTUN_SHAPE) /* && host->spd_in */)
ptr += sprintf(ptr,"S%d",host->spd_in);
if( host->flags & VTUN_ZLIB )
ptr += sprintf(ptr,"C%d", host->zlevel);
if( host->flags & VTUN_LZO )
ptr += sprintf(ptr,"L%d", host->zlevel);
if( host->flags & VTUN_KEEP_ALIVE )
*(ptr++) = 'K';
if( host->flags & VTUN_ENCRYPT )
ptr += sprintf(ptr,"E%d", host->cipher);
strcat(ptr,">");
return str;
}
/* return 1 on success, otherwise 0 */
int cf2bf(char *str, struct vtun_host *host)
{
char *ptr, *p;
int s;
if( (ptr = strchr(str,'<')) ){
ptr++;
while(*ptr){
switch(*ptr++){
case 't':
host->flags |= VTUN_TTY;
break;
case 'p':
host->flags |= VTUN_PIPE;
break;
case 'e':
host->flags |= VTUN_ETHER;
break;
case 'u':
host->flags |= VTUN_TUN;
break;
case 'U':
host->flags &= ~VTUN_PROT_MASK;
host->flags |= VTUN_UDP;
break;
case 'T':
host->flags &= ~VTUN_PROT_MASK;
host->flags |= VTUN_TCP;
break;
case 'K':
host->flags |= VTUN_KEEP_ALIVE;
break;
case 'C':
if((s = strtol(ptr,&p,10)) == ERANGE || ptr == p)
return 0;
host->flags |= VTUN_ZLIB;
host->zlevel = s;
ptr = p;
break;
case 'L':
if((s = strtol(ptr,&p,10)) == ERANGE || ptr == p)
return 0;
host->flags |= VTUN_LZO;
host->zlevel = s;
ptr = p;
break;
case 'E':
if((s = strtol(ptr,&p,10)) == ERANGE || ptr == p)
return 0;
host->flags |= VTUN_ENCRYPT;
host->cipher = s;
ptr = p;
break;
case 'S':
if((s = strtol(ptr,&p,10)) == ERANGE || ptr == p)
return 0;
if( s ){
host->flags |= VTUN_SHAPE;
host->spd_out = s;
}
ptr = p;
break;
case '>':
return 1;
default:
return 0;
}
}
}
return 0;
}
/*
* Functions to convert binary key data to character string.
* string format: <char_data>
*/
char *cl2cs(char *chal)
{
static char str[VTUN_CHAL_SIZE*2+3], *chr="abcdefghijklmnop";
register char *ptr = str;
register int i;
*(ptr++) = '<';
for(i=0; i<VTUN_CHAL_SIZE; i++){
*(ptr++) = chr[ ((chal[i] & 0xf0) >> 4) ];
*(ptr++) = chr[ (chal[i] & 0x0f) ];
}
*(ptr++) = '>';
*ptr = '\0';
return str;
}
int cs2cl(char *str, char *chal)
{
register char *ptr = str;
register int i;
if( !(ptr = strchr(str,'<')) )
return 0;
ptr++;
if( !strtok(ptr,">") || strlen(ptr) != VTUN_CHAL_SIZE*2 )
return 0;
for(i=0; i<VTUN_CHAL_SIZE && *ptr; i++, ptr+=2) {
chal[i] = (*ptr - 'a') << 4;
chal[i] |= *(ptr+1) - 'a';
}
return 1;
}
/* Authentication (Server side) */
struct vtun_host * auth_server(int fd, int *reason)
{
char chal_req[VTUN_CHAL_SIZE], chal_res[VTUN_CHAL_SIZE];
char buf[VTUN_MESG_SIZE], *str1, *str2;
struct vtun_host *h = NULL;
char *host = NULL;
int stage;
set_title("authentication");
print_p(fd, "VTRUNKD server ver %s\n", VERSION);
stage = ST_HOST;
*reason = D_NOREAD;
while( readn_t(fd, buf, VTUN_MESG_SIZE, vtun.timeout) > 0 ){
*reason = D_OTHER;
buf[sizeof(buf)-1]='\0';
strtok(buf,"\r\n");
if( !(str1=strtok(buf," :")) ) {
*reason = D_NOSHAKE1;
break;
}
if( !(str2=strtok(NULL," :")) ) {
*reason = D_NOSHAKE2;
break;
}
switch( stage ){
case ST_HOST:
if( !strcmp(str1,"HOST") ){
host = strdup(str2);
gen_chal(chal_req);
print_p(fd,"OK CHAL: %s\n", cl2cs(chal_req));
stage = ST_CHAL;
continue;
}
*reason = D_ST_CHAL;
break;
case ST_CHAL:
if( !strcmp(str1,"CHAL") ){
if( !cs2cl(str2,chal_res) ) {
*reason = D_CHAL;
break;
}
if( !(h = find_host(host)) ) {
*reason = D_NOHOST;
break;
}
decrypt_chal(chal_res, h->passwd);
if( !memcmp(chal_req, chal_res, VTUN_CHAL_SIZE) ){
/* Auth successeful. */
/* Lock host */
if( lock_host(h) < 0 ){
/* Multiple connections are denied */
h = NULL;
*reason = D_NOMULT;
break;
}
print_p(fd,"OK FLAGS: %s\n", bf2cf(h));
} else
h = NULL;
}
break;
}
break;
}
if( host )
free(host);
if( !h )
print_p(fd,"ERR\n");
return h;
}
/* Authentication (Client side) */
int auth_client(int fd, struct vtun_host *host, int * reason)
{
char buf[VTUN_MESG_SIZE], chal[VTUN_CHAL_SIZE];
int stage, success=0 ;
stage = ST_INIT;
*reason = D_NOREAD;
while( readn_t(fd, buf, VTUN_MESG_SIZE, vtun.timeout) > 0 ){
*reason = D_OTHER;
buf[sizeof(buf)-1]='\0';
switch( stage ){
case ST_INIT:
if (!strncmp(buf, "VTRUNKD", 7)) {
stage = ST_HOST;
print_p(fd,"HOST: %s\n",host->host);
continue;
}
*reason = D_GREET;
break;
case ST_HOST:
if( !strncmp(buf,"OK",2) && cs2cl(buf,chal)){
stage = ST_CHAL;
encrypt_chal(chal,host->passwd);
print_p(fd,"CHAL: %s\n", cl2cs(chal));
continue;
}
*reason = D_CHAL;
break;
case ST_CHAL:
if( !strncmp(buf,"OK",2) && cf2bf(buf,host) )
success = 1;
else *reason = D_PWD;
break;
}
break;
}
return success;
}