1 |
adx |
30 |
/* |
2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
|
|
* packet.c: Packet handlers. |
4 |
|
|
* |
5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
|
|
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* This program is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
|
|
* USA |
21 |
|
|
* |
22 |
knight |
31 |
* $Id$ |
23 |
adx |
30 |
*/ |
24 |
|
|
#include "stdinc.h" |
25 |
michael |
1011 |
#include "list.h" |
26 |
adx |
30 |
#include "s_bsd.h" |
27 |
michael |
1309 |
#include "conf.h" |
28 |
adx |
30 |
#include "s_serv.h" |
29 |
|
|
#include "client.h" |
30 |
|
|
#include "ircd.h" |
31 |
|
|
#include "parse.h" |
32 |
|
|
#include "fdlist.h" |
33 |
|
|
#include "packet.h" |
34 |
|
|
#include "irc_string.h" |
35 |
|
|
#include "memory.h" |
36 |
|
|
#include "hook.h" |
37 |
|
|
#include "send.h" |
38 |
michael |
1243 |
#include "s_misc.h" |
39 |
adx |
30 |
|
40 |
|
|
#define READBUF_SIZE 16384 |
41 |
|
|
|
42 |
|
|
static char readBuf[READBUF_SIZE]; |
43 |
|
|
static void client_dopacket(struct Client *, char *, size_t); |
44 |
|
|
|
45 |
|
|
/* extract_one_line() |
46 |
|
|
* |
47 |
|
|
* inputs - pointer to a dbuf queue |
48 |
|
|
* - pointer to buffer to copy data to |
49 |
|
|
* output - length of <buffer> |
50 |
|
|
* side effects - one line is copied and removed from the dbuf |
51 |
|
|
*/ |
52 |
|
|
static int |
53 |
|
|
extract_one_line(struct dbuf_queue *qptr, char *buffer) |
54 |
|
|
{ |
55 |
|
|
struct dbuf_block *block; |
56 |
|
|
int line_bytes = 0, empty_bytes = 0, phase = 0; |
57 |
|
|
unsigned int idx; |
58 |
|
|
|
59 |
|
|
char c; |
60 |
|
|
dlink_node *ptr; |
61 |
|
|
|
62 |
|
|
/* |
63 |
|
|
* Phase 0: "empty" characters before the line |
64 |
|
|
* Phase 1: copying the line |
65 |
|
|
* Phase 2: "empty" characters after the line |
66 |
|
|
* (delete them as well and free some space in the dbuf) |
67 |
|
|
* |
68 |
|
|
* Empty characters are CR, LF and space (but, of course, not |
69 |
|
|
* in the middle of a line). We try to remove as much of them as we can, |
70 |
|
|
* since they simply eat server memory. |
71 |
|
|
* |
72 |
|
|
* --adx |
73 |
|
|
*/ |
74 |
|
|
DLINK_FOREACH(ptr, qptr->blocks.head) |
75 |
|
|
{ |
76 |
|
|
block = ptr->data; |
77 |
|
|
|
78 |
|
|
for (idx = 0; idx < block->size; idx++) |
79 |
|
|
{ |
80 |
|
|
c = block->data[idx]; |
81 |
|
|
if (IsEol(c) || (c == ' ' && phase != 1)) |
82 |
|
|
{ |
83 |
|
|
empty_bytes++; |
84 |
|
|
if (phase == 1) |
85 |
|
|
phase = 2; |
86 |
|
|
} |
87 |
|
|
else switch (phase) |
88 |
|
|
{ |
89 |
|
|
case 0: phase = 1; |
90 |
|
|
case 1: if (line_bytes++ < IRCD_BUFSIZE - 2) |
91 |
|
|
*buffer++ = c; |
92 |
|
|
break; |
93 |
|
|
case 2: *buffer = '\0'; |
94 |
|
|
dbuf_delete(qptr, line_bytes + empty_bytes); |
95 |
|
|
return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/* |
101 |
|
|
* Now, if we haven't reached phase 2, ignore all line bytes |
102 |
|
|
* that we have read, since this is a partial line case. |
103 |
|
|
*/ |
104 |
|
|
if (phase != 2) |
105 |
|
|
line_bytes = 0; |
106 |
|
|
else |
107 |
|
|
*buffer = '\0'; |
108 |
|
|
|
109 |
|
|
/* Remove what is now unnecessary */ |
110 |
|
|
dbuf_delete(qptr, line_bytes + empty_bytes); |
111 |
|
|
return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/* |
115 |
|
|
* parse_client_queued - parse client queued messages |
116 |
|
|
*/ |
117 |
|
|
static void |
118 |
|
|
parse_client_queued(struct Client *client_p) |
119 |
|
|
{ |
120 |
|
|
int dolen = 0; |
121 |
|
|
int checkflood = 1; |
122 |
|
|
struct LocalUser *lclient_p = client_p->localClient; |
123 |
|
|
|
124 |
|
|
if (IsUnknown(client_p)) |
125 |
|
|
{ |
126 |
|
|
int i = 0; |
127 |
|
|
|
128 |
|
|
for(;;) |
129 |
|
|
{ |
130 |
|
|
if (IsDefunct(client_p)) |
131 |
|
|
return; |
132 |
|
|
|
133 |
|
|
/* rate unknown clients at MAX_FLOOD per loop */ |
134 |
|
|
if (i >= MAX_FLOOD) |
135 |
|
|
break; |
136 |
|
|
|
137 |
|
|
dolen = extract_one_line(&lclient_p->buf_recvq, readBuf); |
138 |
|
|
if (dolen == 0) |
139 |
|
|
break; |
140 |
|
|
|
141 |
|
|
client_dopacket(client_p, readBuf, dolen); |
142 |
|
|
i++; |
143 |
|
|
|
144 |
|
|
/* if they've dropped out of the unknown state, break and move |
145 |
|
|
* to the parsing for their appropriate status. --fl |
146 |
|
|
*/ |
147 |
|
|
if(!IsUnknown(client_p)) |
148 |
|
|
break; |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
if (IsServer(client_p) || IsConnecting(client_p) || IsHandshake(client_p)) |
153 |
|
|
{ |
154 |
|
|
while (1) |
155 |
|
|
{ |
156 |
|
|
if (IsDefunct(client_p)) |
157 |
|
|
return; |
158 |
|
|
if ((dolen = extract_one_line(&lclient_p->buf_recvq, |
159 |
|
|
readBuf)) == 0) |
160 |
|
|
break; |
161 |
|
|
client_dopacket(client_p, readBuf, dolen); |
162 |
|
|
} |
163 |
|
|
} |
164 |
adx |
163 |
else if (IsClient(client_p)) |
165 |
adx |
30 |
{ |
166 |
michael |
1219 |
if (ConfigFileEntry.no_oper_flood && (HasUMode(client_p, UMODE_OPER) || IsCanFlood(client_p))) |
167 |
adx |
30 |
{ |
168 |
|
|
if (ConfigFileEntry.true_no_oper_flood) |
169 |
|
|
checkflood = -1; |
170 |
|
|
else |
171 |
|
|
checkflood = 0; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
/* |
175 |
|
|
* Handle flood protection here - if we exceed our flood limit on |
176 |
|
|
* messages in this loop, we simply drop out of the loop prematurely. |
177 |
|
|
* -- adrian |
178 |
|
|
*/ |
179 |
|
|
for (;;) |
180 |
|
|
{ |
181 |
|
|
if (IsDefunct(client_p)) |
182 |
|
|
break; |
183 |
|
|
|
184 |
|
|
/* This flood protection works as follows: |
185 |
|
|
* |
186 |
|
|
* A client is given allow_read lines to send to the server. Every |
187 |
|
|
* time a line is parsed, sent_parsed is increased. sent_parsed |
188 |
|
|
* is decreased by 1 every time flood_recalc is called. |
189 |
|
|
* |
190 |
|
|
* Thus a client can 'burst' allow_read lines to the server, any |
191 |
|
|
* excess lines will be parsed one per flood_recalc() call. |
192 |
|
|
* |
193 |
|
|
* Therefore a client will be penalised more if they keep flooding, |
194 |
|
|
* as sent_parsed will always hover around the allow_read limit |
195 |
|
|
* and no 'bursts' will be permitted. |
196 |
|
|
*/ |
197 |
|
|
if (checkflood > 0) |
198 |
|
|
{ |
199 |
|
|
if(lclient_p->sent_parsed >= lclient_p->allow_read) |
200 |
|
|
break; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/* allow opers 4 times the amount of messages as users. why 4? |
204 |
|
|
* why not. :) --fl_ |
205 |
|
|
*/ |
206 |
|
|
else if (lclient_p->sent_parsed >= (4 * lclient_p->allow_read) && |
207 |
|
|
checkflood != -1) |
208 |
|
|
break; |
209 |
|
|
|
210 |
|
|
dolen = extract_one_line(&lclient_p->buf_recvq, readBuf); |
211 |
|
|
if (dolen == 0) |
212 |
|
|
break; |
213 |
|
|
|
214 |
|
|
client_dopacket(client_p, readBuf, dolen); |
215 |
|
|
lclient_p->sent_parsed++; |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
/* flood_endgrace() |
221 |
|
|
* |
222 |
|
|
* marks the end of the clients grace period |
223 |
|
|
*/ |
224 |
|
|
void |
225 |
|
|
flood_endgrace(struct Client *client_p) |
226 |
|
|
{ |
227 |
|
|
SetFloodDone(client_p); |
228 |
|
|
|
229 |
|
|
/* Drop their flood limit back down */ |
230 |
|
|
client_p->localClient->allow_read = MAX_FLOOD; |
231 |
|
|
|
232 |
|
|
/* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST, |
233 |
|
|
* so reset it. |
234 |
|
|
*/ |
235 |
|
|
client_p->localClient->sent_parsed = 0; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
/* |
239 |
|
|
* flood_recalc |
240 |
|
|
* |
241 |
|
|
* recalculate the number of allowed flood lines. this should be called |
242 |
|
|
* once a second on any given client. We then attempt to flush some data. |
243 |
|
|
*/ |
244 |
|
|
void |
245 |
|
|
flood_recalc(fde_t *fd, void *data) |
246 |
|
|
{ |
247 |
|
|
struct Client *client_p = data; |
248 |
|
|
struct LocalUser *lclient_p = client_p->localClient; |
249 |
|
|
|
250 |
|
|
/* allow a bursting client their allocation per second, allow |
251 |
|
|
* a client whos flooding an extra 2 per second |
252 |
|
|
*/ |
253 |
|
|
if (IsFloodDone(client_p)) |
254 |
|
|
lclient_p->sent_parsed -= 2; |
255 |
|
|
else |
256 |
|
|
lclient_p->sent_parsed = 0; |
257 |
|
|
|
258 |
|
|
if (lclient_p->sent_parsed < 0) |
259 |
|
|
lclient_p->sent_parsed = 0; |
260 |
|
|
|
261 |
|
|
parse_client_queued(client_p); |
262 |
|
|
|
263 |
|
|
/* And now, try flushing .. */ |
264 |
|
|
if (!IsDead(client_p)) |
265 |
|
|
{ |
266 |
|
|
/* and finally, reset the flood check */ |
267 |
|
|
comm_setflush(fd, 1000, flood_recalc, client_p); |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/* |
272 |
|
|
* read_packet - Read a 'packet' of data from a connection and process it. |
273 |
|
|
*/ |
274 |
|
|
void |
275 |
|
|
read_packet(fde_t *fd, void *data) |
276 |
|
|
{ |
277 |
|
|
struct Client *client_p = data; |
278 |
|
|
int length = 0; |
279 |
|
|
|
280 |
|
|
if (IsDefunct(client_p)) |
281 |
|
|
return; |
282 |
|
|
|
283 |
|
|
/* |
284 |
|
|
* Read some data. We *used to* do anti-flood protection here, but |
285 |
|
|
* I personally think it makes the code too hairy to make sane. |
286 |
|
|
* -- adrian |
287 |
|
|
*/ |
288 |
|
|
do { |
289 |
|
|
#ifdef HAVE_LIBCRYPTO |
290 |
|
|
if (fd->ssl) |
291 |
|
|
{ |
292 |
|
|
length = SSL_read(fd->ssl, readBuf, READBUF_SIZE); |
293 |
|
|
|
294 |
|
|
/* translate openssl error codes, sigh */ |
295 |
|
|
if (length < 0) |
296 |
|
|
switch (SSL_get_error(fd->ssl, length)) |
297 |
michael |
2881 |
{ |
298 |
adx |
30 |
case SSL_ERROR_WANT_WRITE: |
299 |
michael |
2881 |
comm_setselect(fd, COMM_SELECT_WRITE, (PF *)sendq_unblocked, client_p, 0); |
300 |
|
|
return; |
301 |
|
|
case SSL_ERROR_WANT_READ: |
302 |
|
|
errno = EWOULDBLOCK; |
303 |
adx |
30 |
case SSL_ERROR_SYSCALL: |
304 |
michael |
2881 |
break; |
305 |
michael |
428 |
case SSL_ERROR_SSL: |
306 |
|
|
if (errno == EAGAIN) |
307 |
|
|
break; |
308 |
adx |
30 |
default: |
309 |
michael |
2881 |
length = errno = 0; |
310 |
|
|
} |
311 |
adx |
30 |
} |
312 |
|
|
else |
313 |
|
|
#endif |
314 |
|
|
{ |
315 |
|
|
length = recv(fd->fd, readBuf, READBUF_SIZE, 0); |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
if (length <= 0) |
319 |
|
|
{ |
320 |
|
|
/* |
321 |
|
|
* If true, then we can recover from this error. Just jump out of |
322 |
|
|
* the loop and re-register a new io-request. |
323 |
|
|
*/ |
324 |
|
|
if (length < 0 && ignoreErrno(errno)) |
325 |
|
|
break; |
326 |
|
|
|
327 |
|
|
dead_link_on_read(client_p, length); |
328 |
|
|
return; |
329 |
|
|
} |
330 |
|
|
|
331 |
michael |
1798 |
dbuf_put(&client_p->localClient->buf_recvq, readBuf, length); |
332 |
adx |
30 |
|
333 |
michael |
1241 |
if (client_p->localClient->lasttime < CurrentTime) |
334 |
|
|
client_p->localClient->lasttime = CurrentTime; |
335 |
|
|
if (client_p->localClient->lasttime > client_p->localClient->since) |
336 |
|
|
client_p->localClient->since = CurrentTime; |
337 |
adx |
30 |
ClearPingSent(client_p); |
338 |
|
|
|
339 |
|
|
/* Attempt to parse what we have */ |
340 |
|
|
parse_client_queued(client_p); |
341 |
|
|
|
342 |
|
|
if (IsDefunct(client_p)) |
343 |
|
|
return; |
344 |
|
|
|
345 |
|
|
/* Check to make sure we're not flooding */ |
346 |
|
|
if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p)) |
347 |
|
|
&& (dbuf_length(&client_p->localClient->buf_recvq) > |
348 |
michael |
1632 |
get_recvq(&client_p->localClient->confs))) |
349 |
adx |
30 |
{ |
350 |
michael |
1219 |
if (!(ConfigFileEntry.no_oper_flood && HasUMode(client_p, UMODE_OPER))) |
351 |
adx |
30 |
{ |
352 |
|
|
exit_client(client_p, client_p, "Excess Flood"); |
353 |
|
|
return; |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
} |
357 |
|
|
#ifdef HAVE_LIBCRYPTO |
358 |
|
|
while (length == sizeof(readBuf) || fd->ssl); |
359 |
|
|
#else |
360 |
|
|
while (length == sizeof(readBuf)); |
361 |
|
|
#endif |
362 |
|
|
|
363 |
|
|
/* If we get here, we need to register for another COMM_SELECT_READ */ |
364 |
|
|
comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0); |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
/* |
368 |
|
|
* client_dopacket - copy packet to client buf and parse it |
369 |
|
|
* client_p - pointer to client structure for which the buffer data |
370 |
|
|
* applies. |
371 |
|
|
* buffer - pointr to the buffer containing the newly read data |
372 |
|
|
* length - number of valid bytes of data in the buffer |
373 |
|
|
* |
374 |
|
|
* Note: |
375 |
|
|
* It is implicitly assumed that dopacket is called only |
376 |
|
|
* with client_p of "local" variation, which contains all the |
377 |
|
|
* necessary fields (buffer etc..) |
378 |
|
|
*/ |
379 |
|
|
static void |
380 |
|
|
client_dopacket(struct Client *client_p, char *buffer, size_t length) |
381 |
|
|
{ |
382 |
|
|
/* |
383 |
|
|
* Update messages received |
384 |
|
|
*/ |
385 |
|
|
++me.localClient->recv.messages; |
386 |
|
|
++client_p->localClient->recv.messages; |
387 |
|
|
|
388 |
|
|
/* |
389 |
|
|
* Update bytes received |
390 |
|
|
*/ |
391 |
|
|
client_p->localClient->recv.bytes += length; |
392 |
|
|
me.localClient->recv.bytes += length; |
393 |
|
|
|
394 |
|
|
parse(client_p, buffer, buffer + length); |
395 |
|
|
} |