]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/parse.c
[Parser]: Fix minor sparse warnings
[ngircd-alex.git] / src / ngircd / parse.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by 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
13 #include "portab.h"
14
15 static char UNUSED id[] = "$Id: parse.c,v 1.70 2008/01/13 16:12:49 fw Exp $";
16
17 /**
18  * @file
19  * IRC command parser and validator.
20  */
21
22 #include "imp.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <strings.h>
28
29 #include "ngircd.h"
30 #include "defines.h"
31 #include "conn-func.h"
32 #include "client.h"
33 #include "channel.h"
34 #include "log.h"
35 #include "messages.h"
36 #include "tool.h"
37
38 #include "exp.h"
39 #include "parse.h"
40
41 #include "imp.h"
42 #include "irc.h"
43 #include "irc-channel.h"
44 #include "irc-info.h"
45 #include "irc-login.h"
46 #include "irc-mode.h"
47 #include "irc-op.h"
48 #include "irc-oper.h"
49 #include "irc-server.h"
50 #include "irc-write.h"
51 #include "numeric.h"
52
53 #include "exp.h"
54
55 struct _NUMERIC {
56         int numeric;
57         bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
58 };
59
60
61 static COMMAND My_Commands[] =
62 {
63         { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
64         { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
65         { "CONNECT", IRC_CONNECT, CLIENT_USER, 0, 0, 0 },
66         { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
67         { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
68         { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
69         { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
70         { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71         { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
72         { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73         { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74         { "KILL", IRC_KILL, 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, CLIENT_USER|CLIENT_SERVER, 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         { "SQUIT", IRC_SQUIT, CLIENT_SERVER, 0, 0, 0 },
95         { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96         { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97         { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98         { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99         { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
100         { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
101         { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102         { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103         { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
104         { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105         { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
106 #ifdef IRCPLUS
107         { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
108 #endif
109         { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
110 };
111
112 static void Init_Request PARAMS(( REQUEST *Req ));
113
114 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
115 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
116 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
117
118 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
119
120 #define ARRAY_SIZE(x)   (sizeof(x)/sizeof((x)[0]))
121
122 /**
123  * Return the pointer to the global "IRC command structure".
124  * This structure, an array of type "COMMAND" describes all the IRC commands
125  * implemented by ngIRCd and how to handle them.
126  * @return Pointer to the global command structure.
127  */
128 GLOBAL COMMAND *
129 Parse_GetCommandStruct( void )
130 {
131         return My_Commands;
132 } /* Parse_GetCommandStruct */
133
134
135 /**
136  * Parse a command ("request") received from a client.
137  * 
138  * This function is called after the connection layer received a valid CR+LF
139  * terminated line of text: we asume that this is a valid IRC command and
140  * try to do something useful with it :-)
141  *
142  * All errors are reported to the client from which the command has been
143  * received, and if the error is fatal this connection is closed down.
144  *
145  * This function is able to parse the syntax as described in RFC 2812,
146  * section 2.3.
147  *
148  * @param Idx Index of the connection from which the command has been received.
149  * @param Request NULL terminated line of text (the "command").
150  * @return true on success (valid command or "regular" error), false if a
151  *      fatal error occured and the connection has been shut down.
152  */
153 GLOBAL bool
154 Parse_Request( CONN_ID Idx, char *Request )
155 {
156         REQUEST req;
157         char *start, *ptr;
158         bool closed;
159
160         assert( Idx >= 0 );
161         assert( Request != NULL );
162
163 #ifdef SNIFFER
164         if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
165 #endif
166
167         Init_Request( &req );
168
169         /* Fuehrendes und folgendes "Geraffel" verwerfen */
170         ngt_TrimStr( Request );
171
172         /* gibt es ein Prefix? */
173         if( Request[0] == ':' )
174         {
175                 /* Prefix vorhanden */
176                 req.prefix = Request + 1;
177                 ptr = strchr( Request, ' ' );
178                 if( ! ptr )
179                 {
180                         Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
181                         return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
182                 }
183                 *ptr = '\0';
184 #ifndef STRICT_RFC
185                 /* multiple Leerzeichen als Trenner zwischen
186                  * Prefix und Befehl ignorieren */
187                 while( *(ptr + 1) == ' ' ) ptr++;
188 #endif
189                 start = ptr + 1;
190         }
191         else start = Request;
192
193         /* Befehl */
194         ptr = strchr( start, ' ' );
195         if( ptr )
196         {
197                 *ptr = '\0';
198 #ifndef STRICT_RFC
199                 /* multiple Leerzeichen als Trenner vor
200                  * Parametern ignorieren */
201                 while( *(ptr + 1) == ' ' ) ptr++;
202 #endif
203         }
204         req.command = start;
205
206         /* Argumente, Parameter */
207         if( ptr )
208         {
209                 /* Prinzipiell gibt es welche :-) */
210                 start = ptr + 1;
211                 while( start )
212                 {
213                         /* Parameter-String "zerlegen" */
214                         if( start[0] == ':' )
215                         {
216                                 req.argv[req.argc] = start + 1;
217                                 ptr = NULL;
218                         }
219                         else
220                         {
221                                 req.argv[req.argc] = start;
222                                 ptr = strchr( start, ' ' );
223                                 if( ptr )
224                                 {
225                                         *ptr = '\0';
226 #ifndef STRICT_RFC
227                                         /* multiple Leerzeichen als
228                                          * Parametertrenner ignorieren */
229                                         while( *(ptr + 1) == ' ' ) ptr++;
230 #endif
231                                 }
232                         }
233
234                         req.argc++;
235
236                         if( start[0] == ':' ) break;
237                         if( req.argc > 14 ) break;
238
239                         if( ptr ) start = ptr + 1;
240                         else start = NULL;
241                 }
242         }
243
244         /* Daten validieren */
245         if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
246         if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
247         if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
248
249         return Handle_Request( Idx, &req );
250 } /* Parse_Request */
251
252
253 /**
254  * Initialize request structure.
255  * @param Req Request structure to be initialized.
256  */
257 static void
258 Init_Request( REQUEST *Req )
259 {
260         /* Neue Request-Struktur initialisieren */
261
262         int i;
263
264         assert( Req != NULL );
265
266         Req->prefix = NULL;
267         Req->command = NULL;
268         for( i = 0; i < 15; Req->argv[i++] = NULL );
269         Req->argc = 0;
270 } /* Init_Request */
271
272
273 static bool
274 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
275 {
276         CLIENT *client, *c;
277
278         assert( Idx >= 0 );
279         assert( Req != NULL );
280
281         *Closed = false;
282
283         /* ist ueberhaupt ein Prefix vorhanden? */
284         if( ! Req->prefix ) return true;
285
286         /* Client-Struktur der Connection ermitteln */
287         client = Conn_GetClient( Idx );
288         assert( client != NULL );
289
290         /* nur validieren, wenn bereits registrierte Verbindung */
291         if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
292         {
293                 /* noch nicht registrierte Verbindung.
294                  * Das Prefix wird ignoriert. */
295                 Req->prefix = NULL;
296                 return true;
297         }
298
299         /* pruefen, ob der im Prefix angegebene Client bekannt ist */
300         c = Client_Search( Req->prefix );
301         if( ! c )
302         {
303                 /* im Prefix angegebener Client ist nicht bekannt */
304                 Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
305                 if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true;
306                 return false;
307         }
308
309         /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
310          * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
311          * gefaelscht ist */
312         if( Client_NextHop( c ) != client )
313         {
314                 /* das angegebene Prefix ist aus dieser Richtung, also
315                  * aus der gegebenen Connection, ungueltig! */
316                 Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
317                 Conn_Close( Idx, NULL, "Spoofed prefix", true);
318                 *Closed = true;
319                 return false;
320         }
321
322         return true;
323 } /* Validate_Prefix */
324
325
326 static bool
327 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
328 {
329         assert( Idx >= 0 );
330         assert( Req != NULL );
331         *Closed = false;
332
333         return true;
334 } /* Validate_Comman */
335
336
337 static bool
338 Validate_Args( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
339 {
340         assert( Idx >= 0 );
341         assert( Req != NULL );
342         *Closed = false;
343
344         return true;
345 } /* Validate_Args */
346
347
348 /* Command is a status code ("numeric") from another server */
349 static bool
350 Handle_Numeric(CLIENT *client, REQUEST *Req)
351 {
352         static const struct _NUMERIC Numerics[] = {
353                 { 005, IRC_Num_ISUPPORT },
354                 { 376, IRC_Num_ENDOFMOTD }
355         };
356         int i, num;
357         char str[LINE_LEN];
358         CLIENT *prefix, *target = NULL;
359
360         /* Determine target */
361         if (Req->argc > 0)
362                 target = Client_Search(Req->argv[0]);
363
364         if (!target) {
365                 /* Status code without target!? */
366                 if (Req->argc > 0)
367                         Log(LOG_WARNING,
368                             "Unknown target for status code %s: \"%s\"",
369                             Req->command, Req->argv[0]);
370                 else
371                         Log(LOG_WARNING,
372                             "Unknown target for status code %s!",
373                             Req->command);
374                 return true;
375         }
376         if (target == Client_ThisServer()) {
377                 /* This server is the target of the numeric */
378                 num = atoi(Req->command);
379
380                 for (i = 0; i < (int) ARRAY_SIZE(Numerics); i++) {
381                         if (num == Numerics[i].numeric)
382                                 return Numerics[i].function(client, Req);
383                 }
384
385                 LogDebug("Ignored status code %s from \"%s\".",
386                          Req->command, Client_ID(client));
387                 return true;
388         }
389
390         /* Determine source */
391         if (! Req->prefix[0]) {
392                 /* Oops, no prefix!? */
393                 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
394                                                 Req->command, Client_ID(client));
395                 return true;
396         }
397
398         prefix = Client_Search(Req->prefix);
399         if (! prefix) { /* Oops, unknown prefix!? */
400                 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
401                 return true;
402         }
403
404         /* Forward status code */
405         strlcpy(str, Req->command, sizeof(str));
406         for (i = 0; i < Req->argc; i++) {
407                 if (i < Req->argc - 1)
408                         strlcat(str, " ", sizeof(str));
409                 else
410                         strlcat(str, " :", sizeof(str));
411                 strlcat(str, Req->argv[i], sizeof(str));
412         }
413         return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
414 }
415
416
417 static bool
418 Handle_Request( CONN_ID Idx, REQUEST *Req )
419 {
420         /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
421          * wird die Verbindung geschlossen und false geliefert. */
422         CLIENT *client;
423         bool result = true;
424         COMMAND *cmd;
425
426         assert( Idx >= 0 );
427         assert( Req != NULL );
428         assert( Req->command != NULL );
429
430         client = Conn_GetClient( Idx );
431         assert( client != NULL );
432
433         /* Numeric? */
434         if ((Client_Type(client) == CLIENT_SERVER ||
435              Client_Type(client) == CLIENT_UNKNOWNSERVER)
436             && strlen(Req->command) == 3 && atoi(Req->command) > 1)
437                 return Handle_Numeric(client, Req);
438
439         cmd = My_Commands;
440         while (cmd->name) {
441                 /* Befehl suchen */
442                 if (strcasecmp(Req->command, cmd->name) != 0) {
443                         cmd++;
444                         continue;
445                 }
446
447                 if (!(Client_Type(client) & cmd->type))
448                         return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
449
450                 /* Command is allowed for this client: call it and count produced bytes */
451                 Conn_ResetWCounter();
452                 result = (cmd->function)(client, Req);
453                 cmd->bytes += Conn_WCounter();
454
455                 /* Adjust counters */
456                 if (Client_Type(client) != CLIENT_SERVER)
457                         cmd->lcount++;
458                 else
459                         cmd->rcount++;
460                 return result;
461         }
462
463         if (Client_Type( client ) != CLIENT_USER &&
464             Client_Type( client ) != CLIENT_SERVER &&
465             Client_Type( client ) != CLIENT_SERVICE )
466                 return true;
467
468         /* Unknown command and registered connection: generate error: */
469         LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
470                         Client_Conn( client ), Req->command, Req->argc,
471                         Req->argc == 1 ? "parameter" : "parameters",
472                         Req->prefix ? "" : " no" );
473
474         if (Client_Type(client) != CLIENT_SERVER) {
475                 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
476                                 Client_ID(client), Req->command);
477                 Conn_SetPenalty(Idx, 1);
478         }
479         return result;
480 } /* Handle_Request */
481
482
483 /* -eof- */