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