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