-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloki.c
441 lines (394 loc) · 16.3 KB
/
loki.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* LOKI2
*
* [ loki.c ]
*
* 1996/7 Guild Corporation Worldwide [daemon9]
*/
#include "loki.h"
jmp_buf env;
struct loki sdg, rdg;
int verbose = OK, cflags = 0, ripsock = 0, tsock = 0;
u_long p_read = 0; /* packets read */
#ifdef STRONG_CRYPTO
DH *dh_keypair = NULL; /* DH public and private keypair */
extern u_short ivec_salt;
#endif
int main(int argc, char *argv[])
{
static int prot = IPPROTO_ICMP, one = 1, c = 0;
#ifdef STRONG_CRYPTO
static int established = 0, retran = 0;
#endif
static u_short loki_id = 0;
int timer = MIN_TIMEOUT;
u_char buf[BUFSIZE] = {0};
struct protoent *pprot = 0;
struct sockaddr_in sin;
/* Ensure we have proper permissions */
if (getuid() || geteuid()) err_exit(1, 1, verbose, L_MSG_NOPRIV);
loki_id = getpid(); /* Allows us to individualize each
* same protocol loki client session
* on a given host.
*/
bzero((struct sockaddr_in *)&sin, sizeof(sin));
while ((c = getopt(argc, argv, "v:d:t:p:")) != EOF)
{
switch (c)
{
case 'v': /* change verbosity */
verbose = atoi(optarg);
break;
case 'd': /* destination address of daemon */
strncpy(buf, optarg, BUFSIZE - 1);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = name_resolve(buf);
break;
case 't': /* change alarm timer */
if ((timer = atoi(optarg)) < MIN_TIMEOUT)
err_exit(1, 0, 1, "Invalid timeout.\n");
break;
case 'p': /* select transport protocol */
switch (optarg[0])
{
case 'i': /* ICMP_ECHO / ICMP_ECHOREPLY */
prot = IPPROTO_ICMP;
break;
case 'u': /* DNS query / reply */
prot = IPPROTO_UDP;
break;
default:
err_exit(1, 0, verbose, "Unknown transport.\n");
}
break;
default:
err_exit(0, 0, 1, C_MSG_USAGE);
}
}
/* we need a destination address */
if (!sin.sin_addr.s_addr) err_exit(0, 0, verbose, C_MSG_USAGE);
if ((tsock = socket(AF_INET, SOCK_RAW, prot)) < 0)
err_exit(1, 1, 1, L_MSG_SOCKET);
#ifdef STRONG_CRYPTO /* ICMP only with strong crypto */
if (prot != IPPROTO_ICMP) err_exit(0, 0, verbose, L_MSG_ICMPONLY);
#endif
/* Raw socket to build packets */
if ((ripsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
err_exit(1, 1, verbose, L_MSG_SOCKET);
#ifdef DEBUG
fprintf(stderr, "\nRaw IP socket: ");
fd_status(ripsock, OK);
#endif
#ifdef IP_HDRINCL
if (setsockopt(ripsock, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0)
if (verbose) perror("Cannot set IP_HDRINCL socket option");
#endif
/* register packet dumping function
* to be called upon exit
*/
if (atexit(packets_read) == -1) err_exit(1, 1, verbose, L_MSG_ATEXIT);
fprintf(stderr, L_MSG_BANNER);
for (; ;)
{
#ifdef STRONG_CRYPTO
/* Key negotiation phase. Before we
* can do anything, we need to share
* a secret with the server. This
* is our key management phase.
* After this is done, we are
* established. We try MAX_RETRAN
* times to contact a server.
*/
if (!established)
{
/* Generate the DH parameters and public
* and private keypair
*/
if (!dh_keypair)
{
if (verbose) fprintf(stderr, "\nloki: %s", L_MSG_DHKEYGEN);
if (!(dh_keypair = generate_dh_keypair()))
err_exit(1, 0, verbose, L_MSG_DHKGFAIL);
}
if (verbose) fprintf(stderr, "\nloki: submiting our public key to server");
/* convert the BIGNUM public key
* into a big endian byte string
*/
bzero((u_char *)buf, BUFSIZE);
BN_bn2bin((BIGNUM *)dh_keypair -> pub_key, buf);
/* Submit our key and request to
* the server (in one packet)
*/
if (verbose) fprintf(stderr, C_MSG_PKREQ);
loki_xmit(buf, loki_id, prot, sin, L_PK_REQ);
}
else
{
#endif
bzero((u_char *)buf, BUFSIZE);
fprintf(stderr, PROMPT); /* prompt user for input */
read(STDIN_FILENO, buf, BUFSIZE - 1);
buf[strlen(buf)] = 0;
/* Nothing to parse */
if (buf[0] == '\n') continue; /* Escaped command */
if (buf[0] == '/') if ((!c_parse(buf, &timer))) continue;
/* Send request to server */
loki_xmit(buf, loki_id, prot, sin, L_REQ);
#ifdef STRONG_CRYPTO
}
#endif
/* change transports */
if (cflags & NEWTRANS)
{
close(tsock);
prot = (prot == IPPROTO_UDP) ? IPPROTO_ICMP : IPPROTO_UDP;
if ((tsock = socket(AF_INET, SOCK_RAW, prot)) < 0)
err_exit(1, 1, verbose, L_MSG_SOCKET);
pprot = getprotobynumber(prot);
if (verbose) fprintf(stderr, "\nloki: Transport protocol changed to %s.\n", pprot -> p_name);
cflags &= ~NEWTRANS;
continue;
}
if (cflags & TERMINATE) /* client should exit */
{
fprintf(stderr, "\nloki: clean exit\nroute [guild worldwide]\n");
clean_exit(0);
}
/* Clear TRAP and VALID PACKET flags */
cflags &= (~TRAP & ~VALIDP);
/* set alarm singal handler */
if (signal(SIGALRM, catch_timeout) == SIG_ERR)
err_exit(1, 1, verbose, L_MSG_SIGALRM);
/* returns true if we land here as the
* result of a longjmp() -- IOW the
* alarm timer went off
*/
if (setjmp(env))
{
fprintf(stderr, "\nAlarm.\n%s", C_MSG_TIMEOUT);
cflags |= TRAP;
#ifdef STRONG_CRYPTO
if (!established) /* No connection established yet */
if (++retran == MAX_RETRAN) err_exit(1, 0, verbose, "[fatal] cannot contact server. Giving up.\n");
else if (verbose) fprintf(stderr, "Resending...\n");
#endif
}
while (!(cflags & TRAP))
{ /* TRAP will not be set unless the
* alarm timer expires or we get
* an EOT packet
*/
alarm(timer); /* block until alarm or read */
if ((c = read(tsock, (struct loki *)&rdg, LOKIP_SIZE)) < 0)
perror("[non fatal] network read error");
switch (prot)
{ /* Is this a valid Loki packet? */
case IPPROTO_ICMP:
if ((IS_GOOD_ITYPE_C(rdg))) cflags |= VALIDP;
break;
case IPPROTO_UDP:
if ((IS_GOOD_UTYPE_C(rdg))) cflags |= VALIDP;
break;
default:
err_exit(1, 0, verbose, L_MSG_WIERDERR);
}
if (cflags & VALIDP)
{
#ifdef DEBUG
fprintf(stderr, "\n[DEBUG]\tloki: read %d bytes, packet type: ", c);
PACKET_TYPE(rdg);
DUMP_PACKET(rdg, c);
#endif
/* we have a valid packet and can
* turn off the alarm timer
*/
alarm(0);
switch (rdg.payload[0]) /* determine packet type */
{
case L_REPLY : /* standard reply packet */
bcopy(&rdg.payload[1], buf, BUFSIZE - 1);
blur(DECR, BUFSIZE - 1, buf);
#ifndef DEBUG
fprintf(stderr, "%s", buf);
#endif
p_read++;
break;
case L_EOT : /* end of transmission packet */
cflags |= TRAP;
p_read++;
break;
case L_ERR : /* error msg packet (not encrypted) */
bcopy(&rdg.payload[1], buf, BUFSIZE - 1);
fprintf(stderr, "%s", buf);
#ifdef STRONG_CRYPTO
/* If the connection is not established
* we exit upon receipt of an error
*/
if (!established) clean_exit(1);
#endif
break;
#ifdef STRONG_CRYPTO
case L_PK_REPLY : /* public-key receipt */
if (verbose) fprintf(stderr, C_MSG_PKREC);
/* compute DH key parameters */
DH_compute_key(buf, (void *)BN_bin2bn(&rdg.payload[1], BN2BIN_SIZE, NULL), dh_keypair);
/* extract blowfish key from the
* DH shared secret.
*/
if (verbose) fprintf(stderr, C_MSG_SKSET);
extract_bf_key(buf, OK);
established = OK;
break;
#endif
case L_QUIT: /* termination directive packet */
fprintf(stderr, C_MSG_MUSTQUIT);
clean_exit(0);
default :
fprintf(stderr, "\nUnknown LOKI packet type");
break;
}
cflags &= ~VALIDP; /* reset VALID PACKET flag */
}
}
}
return (0);
}
/*
* Build and transmit Loki packets (client version)
*/
void loki_xmit(u_char *payload, u_short loki_id, int prot, struct sockaddr_in sin, int ptype)
{
bzero((struct loki *)&sdg, LOKIP_SIZE);
/* Encrypt and load payload, unless
* we are doing key management
*/
if (ptype != L_PK_REQ)
{
#ifdef STRONG_CRYPTO
ivec_salt++;
#endif
blur(ENCR, BUFSIZE - 1, payload);
}
bcopy(payload, &sdg.payload[1], BUFSIZE - 1);
if (prot == IPPROTO_ICMP)
{
#ifdef NET3 /* Our workaround. */
sdg.ttype.icmph.icmp_type = ICMP_ECHOREPLY;
#else
sdg.ttype.icmph.icmp_type = ICMP_ECHO;
#endif
sdg.ttype.icmph.icmp_code = (int)NULL;
sdg.ttype.icmph.icmp_id = loki_id; /* Session ID */
sdg.ttype.icmph.icmp_seq = L_TAG; /* Loki ID */
sdg.payload[0] = ptype;
sdg.ttype.icmph.icmp_cksum =
i_check((u_short *)&sdg.ttype.icmph, BUFSIZE + ICMPH_SIZE);
}
if (prot == IPPROTO_UDP)
{
sdg.ttype.udph.uh_sport = loki_id;
sdg.ttype.udph.uh_dport = NL_PORT;
sdg.ttype.udph.uh_ulen = htons(UDPH_SIZE + BUFSIZE);
sdg.payload[0] = ptype;
sdg.ttype.udph.uh_sum =
i_check((u_short *)&sdg.ttype.udph, BUFSIZE + UDPH_SIZE);
}
sdg.iph.ip_v = 0x4;
sdg.iph.ip_hl = 0x5;
sdg.iph.ip_len = FIX_LEN(LOKIP_SIZE);
sdg.iph.ip_ttl = 0x40;
sdg.iph.ip_p = prot;
sdg.iph.ip_dst = sin.sin_addr.s_addr;
if ((sendto(ripsock, (struct loki *)&sdg, LOKIP_SIZE, (int)NULL, (struct sockaddr *) &sin, sizeof(sin)) < LOKIP_SIZE))
{
if (verbose) perror("[non fatal] truncated write");
}
}
/*
* help is here
*/
void help()
{
fprintf(stderr,"
%s\t\t- you are here
%s xx\t\t- change alarm timeout to xx seconds (minimum of %d)
%s\t\t- query loki server for client statistics
%s\t\t- query loki server for all client statistics
%s\t\t- swap the transport protocol ( UDP <-> ICMP ) [in beta]
%s\t\t- quit the client
%s\t\t- quit this client and kill all other clients (and the server)
%s dest\t\t- proxy to another server [ UNIMPLIMENTED ]
%s dest\t- redirect to another client [ UNIMPLIMENTED ]\n",
HELP, TIMER, MIN_TIMEOUT, STAT_C, STAT_ALL, SWAP_T, QUIT_C, QUIT_ALL, PROXY_D, REDIR_C);
}
/*
* parse escaped commands
*/
int c_parse(u_char *buf, int *timer)
{
cflags &= ~VALIDC;
/* help */
if (!strncmp(buf, HELP, sizeof(HELP) - 1) || buf[1] == '?')
{
help();
return (NOK);
}
/* change alarm timer */
else if (!strncmp(buf, TIMER, sizeof(TIMER) - 1))
{
cflags |= VALIDC;
(*timer) = atoi(&buf[sizeof(TIMER) - 1]) > MIN_TIMEOUT ? atoi(&buf[sizeof(TIMER) - 1]) : MIN_TIMEOUT;
fprintf(stderr, "\nloki: Alarm timer changed to %d seconds.", *timer);
return (NOK);
}
/* Quit client, send notice to server */
else if (!strncmp(buf, QUIT_C, sizeof(QUIT_C) - 1))
cflags |= (TERMINATE | VALIDC);
/* Quit client, send kill to server */
else if (!strncmp(buf, QUIT_ALL, sizeof(QUIT_ALL) - 1))
cflags |= (TERMINATE | VALIDC);
/* Request server-side statistics */
else if (!strncmp(buf, STAT_C, sizeof(STAT_C) - 1))
cflags |= VALIDC;
/* Swap transport protocols */
else if (!strncmp(buf, SWAP_T, sizeof(SWAP_T) - 1))
{
/* When using strong crypto we do not
* want to swap protocols.
*/
#ifdef STRONG_CRYPTO
fprintf(stderr, C_MSG_NOSWAP);
return (NOK);
#elif !(__linux__)
fprintf(stderr, "\nloki: protocol swapping only supported in Linux\n");
return (NOK);
#else
cflags |= (NEWTRANS | VALIDC);
#endif
}
/* Request server to redirect output
* to another LOKI client
*/
else if (!strncmp(buf, REDIR_C, sizeof(REDIR_C) - 1))
cflags |= (REDIRECT | VALIDC);
/* Request server to simply proxy
* requests to another LOKI server
*/
else if (!strncmp(buf, PROXY_D, sizeof(PROXY_D) - 1))
cflags |= (PROXY | VALIDC);
/* Bad command trap */
if (!(cflags & VALIDC))
{
fprintf(stderr, "Unrecognized command %s\n",buf);
return (NOK);
}
return (OK);
}
/*
* Dumps packets read by client...
*/
void packets_read()
{
fprintf(stderr, "Packets read: %ld\n", p_read);
}
/* EOF */