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