-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
477 lines (369 loc) · 9.75 KB
/
server.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "bouncethis.h"
BT_SERVER server = { 0 } ;
void proxythread (void * client_ptr)
{
BT_CLIENT *client;
BT_PROXY proxy = { 0 };
BT_CLIENTINFO ci = { 0 };
char buf[4096] = { 0 };
int c = 0;
// set up client information
client = (BT_CLIENT*) client_ptr;
// welcome user
if (!client->use_pass) // anon server
{
logevent("Accepted %s:%i", client->ip, client->port);
ci.pass_reg = 1;
bt_send (client->sock,
":BounceThis NOTICE :You have connected to a BounceThis server. (version " BT_VERSION_NUMBER ")\r\n"
":BounceThis NOTICE :Please type \002/BOUNCE <server> [port] [password]\002 to connect to a server.\r\n"
":BounceThis NOTICE :For a list of commands, type \002/COMMANDS\002.\r\n");
}
/* parsing engine */
while (1)
{
char *ptr, *bufptr, *msgptr ;
char msgbuf[512] = { 0 };
c = bt_recv (client->sock, buf, 4096);
if (c == SOCKET_ERROR)
goto abort ;
buf[c] = 0;
bufptr = buf;
do
{
msgptr = msgbuf;
// get first message (terminated with \r\n)
while (*bufptr != '\n' && *bufptr != '\0')
{
if (*bufptr == 0) goto abort;
*msgptr++ = *bufptr++;
}
*bufptr++;
*msgptr = 0;
ptr = strtok (msgbuf," ");
if (!ptr)
continue;
if (!strcmpi (ptr,"PASS"))
{
if (ci.pass_reg)
{
bt_send(client->sock,
":BounceThis NOTICE :Password not needed.\r\n");
continue;
}
else
{
ptr = strtok(NULL," \r");
if (!ptr)
continue;
if (!strcmp (ptr, client->password))
{
logevent("Accepted %s:%i", client->ip, client->port);
ci.pass_reg = 1;
bt_send(client->sock,
":BounceThis NOTICE :Password accepted.\r\n"
":BounceThis NOTICE :You have connected to a BounceThis server. (version " BT_VERSION_NUMBER ")\r\n"
":BounceThis NOTICE :Please type \002/BOUNCE <server> [port] [password]\002 to connect to a server.\r\n"
":BounceThis NOTICE :For a list of commands, type \002/COMMANDS\002.\r\n");
continue;
}
else
{
logevent("Refused %s (incorrect password)", client->ip);
bt_send (client->sock,
":BounceThis NOTICE :Password incorrect.\r\n");
goto abort2;
}
}
}
if (ci.pass_reg)
{
if (!strcmpi (ptr,"COMMANDS"))
{
bt_send (client->sock,
":BounceThis NOTICE :Commands listing:\r\n"
":BounceThis NOTICE :\002/ABOUT\002 About BounceThis.\r\n"
":BounceThis NOTICE :\002/ADMIN\002 Login as the admin.\r\n"
":BounceThis NOTICE :\002/BOUNCE\002 Connects you to a server.\r\n"
":BounceThis NOTICE :\002/COMMANDS\002 Shows this screen.\r\n"
":BounceThis NOTICE :\002/INFO\002 Info about yourself and the server.\r\n"
":BounceThis NOTICE :\002/NICK\002 Changes your nickname.\r\n"
":BounceThis NOTICE :\002/QUIT\002 Disconnects you.\r\n");
if (ci.is_admin)
bt_send (client->sock,
":BounceThis NOTICE :Admin commands listing:\r\n"
":BounceThis NOTICE :\002/BROADCAST\002 Send a message to all users.\r\n"
":BounceThis NOTICE :\002/KILL\002 Kill everyone matching an ip (BT style filters allowed)\r\n"
":BounceThis NOTICE :\002/TEMPFILTER\002 Ban everyone matching an ip (BT style filters allowed) until server is stopped.\r\n"
":BounceThis NOTICE :\002/LISTCONN\002 Lists all connections to the server.\r\n");
}
else if (!strcmpi (ptr,"ABOUT"))
bt_send(client->sock,
":BounceThis NOTICE :About BounceThis:\r\n"
":BounceThis NOTICE :BounceThis was created by Chris M. in 2002.\r\n"
":BounceThis NOTICE :It is a full featured Win32 BNC server designed for IRC.\r\n"
":BounceThis NOTICE :Check us out on the web at 12http://bouncethis.linear.cc\r\n");
else if (!strcmpi (ptr,"INFO"))
{
bt_sendf (client->sock,
":BounceThis NOTICE :" BT_VERSION ", bound on IP %s on port %i\r\n"
":BounceThis NOTICE :You are connecting as %s!%s@%s (%s)\r\n",
server.ip, client->port, real(ci.nick), ci.user, client->ip, real(ci.name));
if (server.use_identd)
bt_sendf (client->sock,
":BounceThis NOTICE :Identd server enabled, using ident '%s'\r\n",
server.ident);
}
else if (!strcmpi (ptr,"NICK"))
{
ptr = strtok(NULL," \r");
if (!ptr)
continue;
bt_cpy (ci.nick, ptr);
if (ci.nick_reg)
bt_sendf (client->sock,
":BounceThis NOTICE :Your nickname is now \002%s\002.\r\n",
real(ci.nick));
else
ci.nick_reg = 1;
}
else if (!strcmpi (ptr,"QUIT"))
goto abort;
else if (!strcmpi (ptr,"USER"))
{
if (ci.user_reg)
{
bt_send (client->sock,
":BounceThis NOTICE :You cannot reregister.\r\n");
continue;
}
ptr = strtok(NULL," ");
if (!ptr)
continue;
bt_cpy (ci.user, ptr);
// skip next two fields. useless -_-;
strtok(NULL," ");
strtok(NULL," ");
ptr = strtok(NULL,"\r");
if (!ptr)
continue;
bt_cpy (ci.name, ptr);
ci.user_reg = 1;
}
else if (!strcmpi (ptr,"ADMIN"))
{
char user[16] ;
char pass[16] ;
ptr = strtok(NULL," \r");
if (!ptr)
continue;
bt_cpy(user,ptr) ;
ptr = strtok(NULL," \r");
if (!ptr)
continue;
bt_cpy(pass,ptr) ;
if (!strcmpi(user,server.admin_user) && !strcmp(pass,server.admin_pass))
{
bt_send(client->sock,":BounceThis NOTICE :Admin password accepted.\r\n");
ci.is_admin = 1 ;
}
else
{
logevent("Connection from %s:%i closed (invalid admin attempt)", client->ip,client->port);
bt_send (client->sock,
":BounceThis NOTICE :Admin password incorrect. Disconnecting.\r\n");
goto abort2;
}
}
else if (!strcmpi (ptr,"BROADCAST"))
{
if (!ci.is_admin) // :O!
{
bt_send(client->sock,":BounceThis NOTICE :You cannot perform this command.\r\n");
continue ;
}
ptr = strtok(NULL,"\r");
if (!ptr)
continue;
broadcast(ptr) ;
}
else if (!strcmpi (ptr,"KILL"))
{
if (!ci.is_admin) // :O!
{
bt_send(client->sock,":BounceThis NOTICE :You cannot perform this command.\r\n");
continue ;
}
ptr = strtok(NULL,"\r");
if (!ptr)
continue;
bt_sendf(client->sock,":BounceThis NOTICE :Kill effected \002%i\002 users.\r\n",kill(ptr)) ;
}
else if (!strcmpi (ptr,"TEMPFILTER"))
{
if (!ci.is_admin) // :O!
{
bt_send(client->sock,":BounceThis NOTICE :You cannot perform this command.\r\n");
continue ;
}
ptr = strtok(NULL,"\r");
if (!ptr)
continue;
bt_sendf(client->sock,":BounceThis NOTICE :Filter effected \002%i\002 users.\r\n",tempfilter(ptr)) ;
}
else if (!strcmpi (ptr,"LISTCONN"))
{
if (!ci.is_admin) // :O!
{
bt_send(client->sock,":BounceThis NOTICE :You cannot perform this command.\r\n");
continue ;
}
list(client->sock);
}
else if (!strcmpi (ptr,"BOUNCE"))
{
ptr = strtok(NULL," \r");
if (!ptr)
continue;
bt_cpy (proxy.host, ptr);
ptr = strtok(NULL," \r");
if (!ptr)
bt_cpy(proxy.port,"6667");
else
{
bt_cpy (proxy.port, ptr);
ptr = strtok(NULL," \r");
if (ptr)
{
bt_cpy (ci.pass,ptr);
ci.use_pass = 1;
}
}
proxy.sock = bt_connect (proxy.host, proxy.port);
if (!proxy.sock)
bt_sendf (client->sock,
":BounceThis NOTICE :Error connecting to %s:%s.\r\n",
proxy.host, proxy.port);
else
{
bt_sendf (client->sock,
":BounceThis NOTICE :Connection established to %s:%s.\r\n",proxy.host,
proxy.port);
if (ci.use_pass)
bt_sendf (client->sock,":BounceThis NOTICE :Attempting to login with password '%s'\r\n",
ci.pass);
break;
}
}
else
bt_sendf (client->sock,
":BounceThis NOTICE :\002/%s\002: Invalid command.\r\n",ptr);
}
else
{
logevent("Refused %s:%i (incorrect password)", client->ip,client->port);
bt_send (client->sock,
":BounceThis NOTICE :You must supply the password before performing any other commands.\r\n");
goto abort2;
}
} while ((char)*bufptr != (char)0) ;
if (proxy.sock) /* proxy connected, break from BT IRC interface */
break;
}
logevent("Proxy connection to %s:%s from %s:%i", proxy.host,proxy.port,
client->ip, client->port);
// Register user.
if (ci.use_pass)
bt_sendf(proxy.sock,"PASS %s\r\n", ci.pass);
bt_sendf(proxy.sock,"NICK %s\r\nUSER %s # # %s\r\n",ci.nick,ci.user,ci.name);
while (1)
{
fd_set rset = { 0 };
FD_SET (client->sock, &rset);
FD_SET (proxy.sock, &rset);
if (select (0, &rset, NULL, NULL, NULL) == -1)
goto abort;
if (FD_ISSET (client->sock, &rset))
{
c = bt_recv(client->sock, buf, 4096);
if (c <= 0)
goto abort;
buf[c] = 0;
client->sent += bt_send (proxy.sock, buf);
}
if (FD_ISSET (proxy.sock, &rset))
{
c = bt_recv (proxy.sock, buf, 4096);
if (c <= 0)
goto abort;
buf[c] = 0;
client->recv += bt_send (client->sock, buf);
}
updatesrtowin(client);
}
abort:
logevent("Connection from %s:%i closed", client->ip,client->port);
abort2: // for skipping auto log.
bt_close(client->sock);
if (proxy.sock)
{
bt_close(proxy.sock);
}
delconn(client);
_endthread();
}
void portthread (void * port_ptr)
{
SOCKET s;
BT_PORT port_info ;
port_info = *(BT_PORT*) port_ptr ;
s = bt_listen (port_info.port);
if (!s)
_endthread();
while (1)
{
socklen_t addrlen = sizeof(SOCKADDR_IN);
SOCKADDR_IN addr ;
SOCKET client_sock;
BT_CLIENT * client ;
char reason, *ip;
client_sock = accept (s, (SOCKADDR*) &addr, &addrlen);
if (client_sock == -1)
continue;
ip = inet_ntoa (addr.sin_addr);
if ((reason = allow(ip)) <= 0)
{
switch (reason)
{
case 0:
logevent("Refused %s:%i (filter)",
ip,port_info.port);
break;
case -1:
logevent("Refused %s:%i (connection limit)",
ip,port_info.port);
break;
case -2:
logevent("Refused %s:%i (clone limit)",
ip,port_info.port);
break;
}
bt_close(client_sock);
continue;
}
client = addconn(ip,port_info.port);
client->sock = client_sock;
client->use_pass = port_info.use_pass ;
if (client->use_pass)
bt_cpy (client->password, port_info.password);
if (_beginthread (proxythread,0,client) == -1)
{
logevent ("Could not create proxy thread for %s:%i",
client->ip,client->port);
bt_close(client->sock);
delconn(client);
_endthread();
}
}
_endthread();
}