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