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