]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/parse.c
Implement IRC_xLINE(): handler for "GLINE" and "KLINE" commands
[ngircd-alex.git] / src / ngircd / parse.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC command parser and validator.
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <strings.h>
25
26 #include "ngircd.h"
27 #include "defines.h"
28 #include "conn-func.h"
29 #include "channel.h"
30 #include "log.h"
31 #include "messages.h"
32 #include "tool.h"
33
34 #include "exp.h"
35 #include "parse.h"
36
37 #include "imp.h"
38 #include "irc.h"
39 #include "irc-channel.h"
40 #include "irc-info.h"
41 #include "irc-login.h"
42 #include "irc-mode.h"
43 #include "irc-op.h"
44 #include "irc-oper.h"
45 #include "irc-server.h"
46 #include "irc-write.h"
47 #include "numeric.h"
48
49 #include "exp.h"
50 #include "conf.h"
51
52 struct _NUMERIC {
53         int numeric;
54         bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
55 };
56
57
58 static COMMAND My_Commands[] =
59 {
60         { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
61         { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
62         { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
63         { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
64         { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
65         { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
66         { "GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
67         { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
68         { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
69         { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70         { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
71         { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72         { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73         { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74         { "KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75         { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76         { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77         { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78         { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79         { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80         { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81         { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
82         { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
83         { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
84         { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
85         { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86         { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
87         { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
88         { "PONG", IRC_PONG, 0xFFFF, 0, 0, 0 },
89         { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
90         { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
91         { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
92         { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
93         { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
94         { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
95         { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
96         { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97         { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98         { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99         { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100         { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
101         { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102         { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103         { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
104         { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
105         { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
106         { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
107         { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108         { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
109         { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
110         { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
111         { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
112 #ifdef IRCPLUS
113         { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
114 #endif
115 #ifndef STRICT_RFC
116         { "GET",  IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
117         { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
118 #endif
119         { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
120 };
121
122 static void Init_Request PARAMS(( REQUEST *Req ));
123
124 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
125 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
126 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
127
128 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
129
130 static bool ScrubCTCP PARAMS((char *Request));
131
132 /**
133  * Return the pointer to the global "IRC command structure".
134  * This structure, an array of type "COMMAND" describes all the IRC commands
135  * implemented by ngIRCd and how to handle them.
136  * @return Pointer to the global command structure.
137  */
138 GLOBAL COMMAND *
139 Parse_GetCommandStruct( void )
140 {
141         return My_Commands;
142 } /* Parse_GetCommandStruct */
143
144
145 /**
146  * Parse a command ("request") received from a client.
147  * 
148  * This function is called after the connection layer received a valid CR+LF
149  * terminated line of text: we asume that this is a valid IRC command and
150  * try to do something useful with it :-)
151  *
152  * All errors are reported to the client from which the command has been
153  * received, and if the error is fatal this connection is closed down.
154  *
155  * This function is able to parse the syntax as described in RFC 2812,
156  * section 2.3.
157  *
158  * @param Idx Index of the connection from which the command has been received.
159  * @param Request NULL terminated line of text (the "command").
160  * @return true on success (valid command or "regular" error), false if a
161  *      fatal error occured and the connection has been shut down.
162  */
163 GLOBAL bool
164 Parse_Request( CONN_ID Idx, char *Request )
165 {
166         REQUEST req;
167         char *start, *ptr;
168         bool closed;
169
170         assert( Idx >= 0 );
171         assert( Request != NULL );
172
173 #ifdef SNIFFER
174         if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
175 #endif
176
177         Init_Request( &req );
178
179         /* remove leading & trailing whitespace */
180         ngt_TrimStr( Request );
181
182         if (Conf_ScrubCTCP && ScrubCTCP(Request))
183                 return true;
184
185         if (Request[0] == ':') {
186                 /* Prefix */
187                 req.prefix = Request + 1;
188                 ptr = strchr( Request, ' ' );
189                 if( ! ptr )
190                 {
191                         LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
192                         return Conn_WriteStr(Idx, "ERROR :Prefix without command");
193                 }
194                 *ptr = '\0';
195 #ifndef STRICT_RFC
196                 /* ignore multiple spaces between prefix and command */
197                 while( *(ptr + 1) == ' ' ) ptr++;
198 #endif
199                 start = ptr + 1;
200         }
201         else start = Request;
202
203         ptr = strchr( start, ' ' );
204         if( ptr )
205         {
206                 *ptr = '\0';
207 #ifndef STRICT_RFC
208                 /* ignore multiple spaces between parameters */
209                 while( *(ptr + 1) == ' ' ) ptr++;
210 #endif
211         }
212         req.command = start;
213
214         /* Arguments, Parameters */
215         if( ptr )
216         {
217                 start = ptr + 1;
218                 while( start )
219                 {
220                         if( start[0] == ':' )
221                         {
222                                 req.argv[req.argc] = start + 1;
223                                 ptr = NULL;
224                         }
225                         else
226                         {
227                                 req.argv[req.argc] = start;
228                                 ptr = strchr( start, ' ' );
229                                 if( ptr )
230                                 {
231                                         *ptr = '\0';
232 #ifndef STRICT_RFC
233                                         while( *(ptr + 1) == ' ' ) ptr++;
234 #endif
235                                 }
236                         }
237
238                         req.argc++;
239
240                         if( start[0] == ':' ) break;
241                         if( req.argc > 14 ) break;
242
243                         if( ptr ) start = ptr + 1;
244                         else start = NULL;
245                 }
246         }
247
248         if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
249         if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
250         if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
251
252         return Handle_Request( Idx, &req );
253 } /* Parse_Request */
254
255
256 /**
257  * Initialize request structure.
258  * @param Req Request structure to be initialized.
259  */
260 static void
261 Init_Request( REQUEST *Req )
262 {
263         /* Neue Request-Struktur initialisieren */
264
265         int i;
266
267         assert( Req != NULL );
268
269         Req->prefix = NULL;
270         Req->command = NULL;
271         for( i = 0; i < 15; Req->argv[i++] = NULL );
272         Req->argc = 0;
273 } /* Init_Request */
274
275
276 static bool
277 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
278 {
279         CLIENT *client, *c;
280
281         assert( Idx >= 0 );
282         assert( Req != NULL );
283
284         *Closed = false;
285
286         client = Conn_GetClient( Idx );
287         assert( client != NULL );
288
289         if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
290             && !(Conn_Options(Idx) & CONN_RFC1459)
291             && strcasecmp(Req->command, "ERROR") != 0
292             && strcasecmp(Req->command, "PING") != 0)
293         {
294                 Log(LOG_ERR,
295                     "Received command without prefix (connection %d, command \"%s\")!?",
296                     Idx, Req->command);
297                 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
298                         *Closed = true;
299                 return false;
300         }
301
302         if (!Req->prefix)
303                 return true;
304
305         /* only validate if this connection is already registered */
306         if (Client_Type(client) != CLIENT_USER
307             && Client_Type(client) != CLIENT_SERVER
308             && Client_Type(client) != CLIENT_SERVICE) {
309                 /* not registered, ignore prefix */
310                 Req->prefix = NULL;
311                 return true;
312         }
313
314         /* check if client in prefix is known */
315         c = Client_Search( Req->prefix );
316         if (!c) {
317                 Log(LOG_ERR,
318                     "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
319                     Req->prefix, Idx, Req->command);
320                 if (!Conn_WriteStr(Idx,
321                                    "ERROR :Invalid prefix \"%s\", client not known",
322                                    Req->prefix))
323                         *Closed = true;
324                 return false;
325         }
326
327         /* check if the client named in the prefix is expected
328          * to come from that direction */
329         if (Client_NextHop(c) != client) {
330                 if (Client_Type(c) != CLIENT_SERVER) {
331                         Log(LOG_ERR,
332                             "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
333                             Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
334                             Req->command);
335                         Conn_Close(Idx, NULL, "Spoofed prefix", true);
336                         *Closed = true;
337                 } else {
338                         Log(LOG_INFO,
339                             "Ignoring spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\").",
340                             Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
341                             Req->command);
342                 }
343                 return false;
344
345         }
346
347         return true;
348 } /* Validate_Prefix */
349
350
351 static bool
352 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
353 {
354         assert( Idx >= 0 );
355         assert( Req != NULL );
356         *Closed = false;
357
358         return true;
359 } /* Validate_Comman */
360
361
362 static bool
363 #ifdef STRICT_RFC
364 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
365 #else
366 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
367 #endif
368 {
369 #ifdef STRICT_RFC
370         int i;
371 #endif
372
373         *Closed = false;
374
375 #ifdef STRICT_RFC
376         assert( Idx >= 0 );
377         assert( Req != NULL );
378
379         /* CR and LF are never allowed in command parameters.
380          * But since we do accept lines terminated only with CR or LF in
381          * "non-RFC-compliant mode" (besides the correct CR+LF combination),
382          * this check can only trigger in "strict RFC" mode; therefore we
383          * optimize it away otherwise ... */
384         for (i = 0; i < Req->argc; i++) {
385                 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
386                         Log(LOG_ERR,
387                             "Invalid character(s) in parameter (connection %d, command %s)!?",
388                             Idx, Req->command);
389                         if (!Conn_WriteStr(Idx,
390                                            "ERROR :Invalid character(s) in parameter!"))
391                                 *Closed = true;
392                         return false;
393                 }
394         }
395 #endif
396
397         return true;
398 } /* Validate_Args */
399
400
401 /* Command is a status code ("numeric") from another server */
402 static bool
403 Handle_Numeric(CLIENT *client, REQUEST *Req)
404 {
405         static const struct _NUMERIC Numerics[] = {
406                 {   5, IRC_Num_ISUPPORT },
407                 {  20, NULL },
408                 { 376, IRC_Num_ENDOFMOTD }
409         };
410         int i, num;
411         char str[LINE_LEN];
412         CLIENT *prefix, *target = NULL;
413
414         /* Determine target */
415         if (Req->argc > 0) {
416                 if (strcmp(Req->argv[0], "*") != 0)
417                         target = Client_Search(Req->argv[0]);
418                 else
419                         target = Client_ThisServer();
420         }
421
422         if (!target) {
423                 /* Status code without target!? */
424                 if (Req->argc > 0)
425                         Log(LOG_WARNING,
426                             "Unknown target for status code %s: \"%s\"",
427                             Req->command, Req->argv[0]);
428                 else
429                         Log(LOG_WARNING,
430                             "Unknown target for status code %s!",
431                             Req->command);
432                 return true;
433         }
434         if (target == Client_ThisServer()) {
435                 /* This server is the target of the numeric */
436                 num = atoi(Req->command);
437
438                 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
439                         if (num == Numerics[i].numeric) {
440                                 if (!Numerics[i].function)
441                                         return CONNECTED;
442                                 return Numerics[i].function(client, Req);
443                         }
444                 }
445
446                 LogDebug("Ignored status code %s from \"%s\".",
447                          Req->command, Client_ID(client));
448                 return true;
449         }
450
451         /* Determine source */
452         if (! Req->prefix[0]) {
453                 /* Oops, no prefix!? */
454                 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
455                                                 Req->command, Client_ID(client));
456                 return true;
457         }
458
459         prefix = Client_Search(Req->prefix);
460         if (! prefix) { /* Oops, unknown prefix!? */
461                 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
462                 return true;
463         }
464
465         /* Forward status code */
466         strlcpy(str, Req->command, sizeof(str));
467         for (i = 0; i < Req->argc; i++) {
468                 if (i < Req->argc - 1)
469                         strlcat(str, " ", sizeof(str));
470                 else
471                         strlcat(str, " :", sizeof(str));
472                 strlcat(str, Req->argv[i], sizeof(str));
473         }
474         return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
475 }
476
477 static bool
478 Handle_Request( CONN_ID Idx, REQUEST *Req )
479 {
480         CLIENT *client;
481         bool result = true;
482         int client_type;
483         COMMAND *cmd;
484
485         assert( Idx >= 0 );
486         assert( Req != NULL );
487         assert( Req->command != NULL );
488
489         client = Conn_GetClient( Idx );
490         assert( client != NULL );
491
492         /* Numeric? */
493         client_type = Client_Type(client);
494         if ((client_type == CLIENT_SERVER ||
495              client_type == CLIENT_UNKNOWNSERVER)
496             && strlen(Req->command) == 3 && atoi(Req->command) > 1)
497                 return Handle_Numeric(client, Req);
498
499         cmd = My_Commands;
500         while (cmd->name) {
501                 if (strcasecmp(Req->command, cmd->name) != 0) {
502                         cmd++;
503                         continue;
504                 }
505
506                 if (!(client_type & cmd->type))
507                         return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
508
509                 /* Command is allowed for this client: call it and count produced bytes */
510                 Conn_ResetWCounter();
511                 result = (cmd->function)(client, Req);
512                 cmd->bytes += Conn_WCounter();
513
514                 /* Adjust counters */
515                 if (client_type != CLIENT_SERVER)
516                         cmd->lcount++;
517                 else
518                         cmd->rcount++;
519                 return result;
520         }
521
522         if (client_type != CLIENT_USER &&
523             client_type != CLIENT_SERVER &&
524             client_type != CLIENT_SERVICE )
525                 return true;
526
527         /* Unknown command and registered connection: generate error: */
528         LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
529                         Client_Conn( client ), Req->command, Req->argc,
530                         Req->argc == 1 ? "parameter" : "parameters",
531                         Req->prefix ? "" : " no" );
532
533         if (Client_Type(client) != CLIENT_SERVER) {
534                 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
535                                 Client_ID(client), Req->command);
536                 Conn_SetPenalty(Idx, 1);
537         }
538         return result;
539 } /* Handle_Request */
540
541
542 /**
543  * Check if incoming messages contains CTCP commands and should be dropped.
544  *
545  * @param Request NULL terminated incoming command.
546  * @returns true, when the message should be dropped.
547  */
548 static bool
549 ScrubCTCP(char *Request)
550 {
551         static const char me_cmd[] = "ACTION ";
552         static const char ctcp_char = 0x1;
553         bool dropCommand = false;
554         char *ptr = Request;
555         char *ptrEnd = strchr(Request, '\0');
556
557         if (Request[0] == ':' && ptrEnd > ptr)
558                 ptr++;
559
560         while (ptr != ptrEnd && *ptr != ':')
561                 ptr++;
562
563         if ((ptrEnd - ptr) > 1) {
564                 ptr++;
565                 if (*ptr == ctcp_char) {
566                         dropCommand = true;
567                         ptr++;
568                         /* allow /me commands */
569                         if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
570                             && !strncmp(ptr, me_cmd, strlen(me_cmd)))
571                                 dropCommand = false;
572                 }
573         }
574         return dropCommand;
575 }
576
577 /* -eof- */