Blob


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 */
13 #include "portab.h"
15 static char UNUSED id[] = "$Id: parse.c,v 1.62 2005/06/01 21:52:18 alex Exp $";
17 /**
18 * @file
19 * IRC command parser and validator.
20 */
22 #include "imp.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <strings.h>
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"
38 #include "exp.h"
39 #include "parse.h"
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"
52 #include "exp.h"
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 };
106 LOCAL void Init_Request PARAMS(( REQUEST *Req ));
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 ));
112 LOCAL bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
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 )
124 return My_Commands;
125 } /* Parse_GetCommandStruct */
128 /**
129 * Parse a command ("request") received from a client.
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 :-)
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.
138 * This function is able to parse the syntax as described in RFC 2812,
139 * section 2.3.
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 )
149 REQUEST req;
150 char *start, *ptr;
151 bool closed;
153 assert( Idx >= 0 );
154 assert( Request != NULL );
156 #ifdef SNIFFER
157 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
158 #endif
160 Init_Request( &req );
162 /* Fuehrendes und folgendes "Geraffel" verwerfen */
163 ngt_TrimStr( Request );
165 /* gibt es ein Prefix? */
166 if( Request[0] == ':' )
168 /* Prefix vorhanden */
169 req.prefix = Request + 1;
170 ptr = strchr( Request, ' ' );
171 if( ! ptr )
173 Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
174 return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
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;
184 else start = Request;
186 /* Befehl */
187 ptr = strchr( start, ' ' );
188 if( ptr )
190 *ptr = '\0';
191 #ifndef STRICT_RFC
192 /* multiple Leerzeichen als Trenner vor
193 * Parametern ignorieren */
194 while( *(ptr + 1) == ' ' ) ptr++;
195 #endif
197 req.command = start;
199 /* Argumente, Parameter */
200 if( ptr )
202 /* Prinzipiell gibt es welche :-) */
203 start = ptr + 1;
204 while( start )
206 /* Parameter-String "zerlegen" */
207 if( start[0] == ':' )
209 req.argv[req.argc] = start + 1;
210 ptr = NULL;
212 else
214 req.argv[req.argc] = start;
215 ptr = strchr( start, ' ' );
216 if( ptr )
218 *ptr = '\0';
219 #ifndef STRICT_RFC
220 /* multiple Leerzeichen als
221 * Parametertrenner ignorieren */
222 while( *(ptr + 1) == ' ' ) ptr++;
223 #endif
227 req.argc++;
229 if( start[0] == ':' ) break;
230 if( req.argc > 14 ) break;
232 if( ptr ) start = ptr + 1;
233 else start = NULL;
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;
242 return Handle_Request( Idx, &req );
243 } /* Parse_Request */
246 /**
247 * Initialize request structure.
248 * @param Req Request structure to be initialized.
249 */
250 LOCAL void
251 Init_Request( REQUEST *Req )
253 /* Neue Request-Struktur initialisieren */
255 int i;
257 assert( Req != NULL );
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 */
266 LOCAL bool
267 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
269 CLIENT *client, *c;
271 assert( Idx >= 0 );
272 assert( Req != NULL );
274 *Closed = false;
276 /* ist ueberhaupt ein Prefix vorhanden? */
277 if( ! Req->prefix ) return true;
279 /* Client-Struktur der Connection ermitteln */
280 client = Client_GetFromConn( Idx );
281 assert( client != NULL );
283 /* nur validieren, wenn bereits registrierte Verbindung */
284 if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
286 /* noch nicht registrierte Verbindung.
287 * Das Prefix wird ignoriert. */
288 Req->prefix = NULL;
289 return true;
292 /* pruefen, ob der im Prefix angegebene Client bekannt ist */
293 c = Client_Search( Req->prefix );
294 if( ! c )
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;
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 )
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;
315 return true;
316 } /* Validate_Prefix */
319 LOCAL bool
320 Validate_Command( CONN_ID Idx, REQUEST *Req, bool *Closed )
322 assert( Idx >= 0 );
323 assert( Req != NULL );
324 *Closed = false;
326 return true;
327 } /* Validate_Comman */
330 LOCAL bool
331 Validate_Args( CONN_ID Idx, REQUEST *Req, bool *Closed )
333 assert( Idx >= 0 );
334 assert( Req != NULL );
335 *Closed = false;
337 return true;
338 } /* Validate_Args */
341 LOCAL bool
342 Handle_Request( CONN_ID Idx, REQUEST *Req )
344 /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
345 * wird die Verbindung geschlossen und false geliefert. */
347 CLIENT *client, *target, *prefix;
348 char str[LINE_LEN];
349 bool result;
350 COMMAND *cmd;
351 int i;
353 assert( Idx >= 0 );
354 assert( Req != NULL );
355 assert( Req->command != NULL );
357 client = Client_GetFromConn( Idx );
358 assert( client != NULL );
360 /* Statuscode? */
361 if(( Client_Type( client ) == CLIENT_SERVER ) && ( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
363 /* Command is a status code from an other server */
365 /* Determine target */
366 if( Req->argc > 0 ) target = Client_Search( Req->argv[0] );
367 else target = NULL;
368 if( ! target )
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;
375 if( target == Client_ThisServer( ))
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;
382 /* Determine source */
383 if( ! Req->prefix[0] )
385 /* Oops, no prefix!? */
386 Log( LOG_WARNING, "Got status code %s from \"%s\" without prefix!?", Req->command, Client_ID( client ));
387 return true;
389 else prefix = Client_Search( Req->prefix );
390 if( ! prefix )
392 /* Oops, unknown prefix!? */
393 Log( LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix );
394 return true;
397 /* Forward status code */
398 strlcpy( str, Req->command, sizeof( str ));
399 for( i = 0; i < Req->argc; i++ )
401 if( i < Req->argc - 1 ) strlcat( str, " ", sizeof( str ));
402 else strlcat( str, " :", sizeof( str ));
403 strlcat( str, Req->argv[i], sizeof( str ));
405 return IRC_WriteStrClientPrefix( target, prefix, "%s", str );
408 cmd = My_Commands;
409 while( cmd->name )
411 /* Befehl suchen */
412 if( strcasecmp( Req->command, cmd->name ) != 0 )
414 cmd++; continue;
417 if( Client_Type( client ) & cmd->type )
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( );
424 /* Adjust counters */
425 if( Client_Type( client ) != CLIENT_SERVER ) cmd->lcount++;
426 else cmd->rcount++;
428 return result;
430 else
432 /* Befehl ist fuer diesen Client-Typ nicht erlaubt! */
433 return IRC_WriteStrClient( client, ERR_NOTREGISTERED_MSG, Client_ID( client ));
437 /* Unbekannter Befehl */
438 Log( LOG_DEBUG, "Connection %d: Unknown command \"%s\", %d %s,%s prefix.", Client_Conn( client ), Req->command, Req->argc, Req->argc == 1 ? "parameter" : "parameters", Req->prefix ? "" : " no" );
439 if( Client_Type( client ) != CLIENT_SERVER ) return IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
440 else return true;
441 } /* Handle_Request */
444 /* -eof- */