]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/parse.c
- Semantik der Validate_XXX()-Funktionen verbessert,
[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  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: parse.c,v 1.34 2002/07/26 21:12:24 alex Exp $
13  *
14  * parse.c: Parsen der Client-Anfragen
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "ngircd.h"
27 #include "defines.h"
28 #include "conn.h"
29 #include "client.h"
30 #include "channel.h"
31 #include "log.h"
32 #include "messages.h"
33 #include "tool.h"
34
35 #include "exp.h"
36 #include "parse.h"
37
38 #include "imp.h"
39 #include "irc.h"
40 #include "irc-channel.h"
41 #include "irc-login.h"
42 #include "irc-mode.h"
43 #include "irc-op.h"
44 #include "irc-oper.h"
45 #include "irc-server.h"
46 #include "irc-write.h"
47
48 #include "exp.h"
49
50
51 LOCAL VOID Init_Request PARAMS(( REQUEST *Req ));
52
53 LOCAL BOOLEAN Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ));
54 LOCAL BOOLEAN Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ));
55 LOCAL BOOLEAN Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ));
56
57 LOCAL BOOLEAN Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
58
59
60 GLOBAL BOOLEAN
61 Parse_Request( CONN_ID Idx, CHAR *Request )
62 {
63         /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
64          * die Verbindung geschlossen und FALSE geliefert.
65          * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
66
67         REQUEST req;
68         CHAR *start, *ptr;
69         BOOLEAN closed;
70
71         assert( Idx >= 0 );
72         assert( Request != NULL );
73
74 #ifdef SNIFFER
75         if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
76 #endif
77
78         Init_Request( &req );
79
80         /* Fuehrendes und folgendes "Geraffel" verwerfen */
81         ngt_TrimStr( Request );
82
83         /* gibt es ein Prefix? */
84         if( Request[0] == ':' )
85         {
86                 /* Prefix vorhanden */
87                 req.prefix = Request + 1;
88                 ptr = strchr( Request, ' ' );
89                 if( ! ptr )
90                 {
91                         Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
92                         return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
93                 }
94                 *ptr = '\0';
95 #ifndef STRICT_RFC
96                 /* multiple Leerzeichen als Trenner zwischen
97                  * Prefix und Befehl ignorieren */
98                 while( *(ptr + 1) == ' ' ) ptr++;
99 #endif
100                 start = ptr + 1;
101         }
102         else start = Request;
103
104         if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
105
106         /* Befehl */
107         ptr = strchr( start, ' ' );
108         if( ptr )
109         {
110                 *ptr = '\0';
111 #ifndef STRICT_RFC
112                 /* multiple Leerzeichen als Trenner vor
113                 *Parametertrennern ignorieren */
114                 while( *(ptr + 1) == ' ' ) ptr++;
115 #endif
116         }
117         req.command = start;
118
119         if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
120
121         /* Argumente, Parameter */
122         if( ptr )
123         {
124                 /* Prinzipiell gibt es welche :-) */
125                 start = ptr + 1;
126                 while( start )
127                 {
128                         /* Parameter-String "zerlegen" */
129                         if( start[0] == ':' )
130                         {
131                                 req.argv[req.argc] = start + 1;
132                                 ptr = NULL;
133                         }
134                         else
135                         {
136                                 req.argv[req.argc] = start;
137                                 ptr = strchr( start, ' ' );
138                                 if( ptr )
139                                 {
140                                         *ptr = '\0';
141 #ifndef STRICT_RFC
142                                         /* multiple Leerzeichen als
143                                          * Parametertrenner ignorieren */
144                                         while( *(ptr + 1) == ' ' ) ptr++;
145 #endif
146                                 }
147                         }
148
149                         req.argc++;
150
151                         if( start[0] == ':' ) break;
152                         if( req.argc > 14 ) break;
153
154                         if( ptr ) start = ptr + 1;
155                         else start = NULL;
156                 }
157         }
158
159         if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
160
161         return Handle_Request( Idx, &req );
162 } /* Parse_Request */
163
164
165 LOCAL VOID
166 Init_Request( REQUEST *Req )
167 {
168         /* Neue Request-Struktur initialisieren */
169
170         INT i;
171
172         assert( Req != NULL );
173
174         Req->prefix = NULL;
175         Req->command = NULL;
176         for( i = 0; i < 15; Req->argv[i++] = NULL );
177         Req->argc = 0;
178 } /* Init_Request */
179
180
181 LOCAL BOOLEAN
182 Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )
183 {
184         CLIENT *c;
185
186         assert( Idx >= 0 );
187         assert( Req != NULL );
188
189         *Closed = FALSE;
190         
191         /* ist ueberhaupt ein Prefix vorhanden? */
192         if( ! Req->prefix ) return TRUE;
193
194         /* pruefen, ob der im Prefix angegebene Client bekannt ist */
195         c = Client_Search( Req->prefix );
196         if( ! c )
197         {
198                 /* im Prefix angegebener Client ist nicht bekannt */
199                 Log( LOG_ERR, "Invalid prefix, client not known (connection %d)!?", Idx );
200                 if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix, client not known!?" )) *Closed = TRUE;
201                 return FALSE;
202         }
203         
204         /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
205          * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
206          * gefaelscht ist */
207         if( Client_NextHop( c ) != Client_GetFromConn( Idx ))
208         {
209                 /* das angegebene Prefix ist aus dieser Richtung, also
210                  * aus der gegebenen Connection, ungueltig! */
211                 Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d)!", Req->prefix, Client_Mask( Client_GetFromConn( Idx )), Idx );
212                 Conn_Close( Idx, NULL, "Spoofed prefix", TRUE );
213                 *Closed = TRUE;
214                 return FALSE;
215         }
216
217         return TRUE;
218 } /* Validate_Prefix */
219
220
221 LOCAL BOOLEAN
222 Validate_Command( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )
223 {
224         assert( Idx >= 0 );
225         assert( Req != NULL );
226         *Closed = FALSE;
227
228         return TRUE;
229 } /* Validate_Comman */
230
231
232 LOCAL BOOLEAN
233 Validate_Args( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )
234 {
235         assert( Idx >= 0 );
236         assert( Req != NULL );
237         *Closed = FALSE;
238
239         return TRUE;
240 } /* Validate_Args */
241
242
243 LOCAL BOOLEAN
244 Handle_Request( CONN_ID Idx, REQUEST *Req )
245 {
246         /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
247          * wird die Verbindung geschlossen und FALSE geliefert. */
248
249         CLIENT *client, *target, *prefix;
250         CHAR str[LINE_LEN];
251         INT i;
252
253         assert( Idx >= 0 );
254         assert( Req != NULL );
255         assert( Req->command != NULL );
256
257         client = Client_GetFromConn( Idx );
258         assert( client != NULL );
259
260         /* Statuscode, der geforwarded werden muss? */
261         if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
262         {
263                 /* Befehl ist ein Statuscode */
264
265                 /* Zielserver ermitteln */
266                 if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] );
267                 else target = NULL;
268                 if( ! target )
269                 {
270                         if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code: \"%s\"", Req->argv[0] );
271                         else Log( LOG_WARNING, "Unknown target for status code!" );
272                         return TRUE;
273                 }
274                 if( target == Client_ThisServer( ))
275                 {
276                         Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
277                         return TRUE;
278                 }
279
280                 /* Quell-Client ermitteln */
281                 if( ! Req->prefix[0] )
282                 {
283                         Log( LOG_WARNING, "Got status code without prefix!?" );
284                         return TRUE;
285                 }
286                 else prefix = Client_Search( Req->prefix );
287                 if( ! prefix )
288                 {
289                         Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix );
290                         return TRUE;
291                 }
292
293                 /* Statuscode weiterleiten */
294                 strcpy( str, Req->command );
295                 for( i = 0; i < Req->argc; i++ )
296                 {
297                         if( i < Req->argc - 1 ) strcat( str, " " );
298                         else strcat( str, " :" );
299                         strcat( str, Req->argv[i] );
300                 }
301                 return IRC_WriteStrClientPrefix( target, prefix, str );
302         }
303
304         if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req );
305         else if( strcasecmp( Req->command, "NICK" ) == 0 ) return IRC_NICK( client, Req );
306         else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req );
307         else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req );
308         else if( strcasecmp( Req->command, "NJOIN" ) == 0 ) return IRC_NJOIN( client, Req );
309         else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req );
310         else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req );
311         else if( strcasecmp( Req->command, "PING" ) == 0 ) return IRC_PING( client, Req );
312         else if( strcasecmp( Req->command, "PONG" ) == 0 ) return IRC_PONG( client, Req );
313         else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req );
314         else if( strcasecmp( Req->command, "PRIVMSG" ) == 0 ) return IRC_PRIVMSG( client, Req );
315         else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req );
316         else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req );
317         else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req );
318         else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req );
319         else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req );
320         else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req );
321         else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req );
322         else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req );
323         else if( strcasecmp( Req->command, "RESTART" ) == 0 ) return IRC_RESTART( client, Req );
324         else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req );
325         else if( strcasecmp( Req->command, "LUSERS" ) == 0 ) return IRC_LUSERS( client, Req );
326         else if( strcasecmp( Req->command, "LINKS" ) == 0 ) return IRC_LINKS( client, Req );
327         else if( strcasecmp( Req->command, "JOIN" ) == 0 ) return IRC_JOIN( client, Req );
328         else if( strcasecmp( Req->command, "PART" ) == 0 ) return IRC_PART( client, Req );
329         else if( strcasecmp( Req->command, "VERSION" ) == 0 ) return IRC_VERSION( client, Req );
330         else if( strcasecmp( Req->command, "KILL" ) == 0 ) return IRC_KILL( client, Req );
331         else if( strcasecmp( Req->command, "AWAY" ) == 0 ) return IRC_AWAY( client, Req );
332         else if( strcasecmp( Req->command, "TOPIC" ) == 0 ) return IRC_TOPIC( client, Req );
333         else if( strcasecmp( Req->command, "WHO" ) == 0 ) return IRC_WHO( client, Req );
334         else if( strcasecmp( Req->command, "LIST" ) == 0 ) return IRC_LIST( client, Req );
335         else if( strcasecmp( Req->command, "INVITE" ) == 0 ) return IRC_INVITE( client, Req );
336         else if( strcasecmp( Req->command, "KICK" ) == 0 ) return IRC_KICK( client, Req );
337         else if( strcasecmp( Req->command, "BAN" ) == 0 ) return IRC_BAN( client, Req );
338
339         /* Unbekannter Befehl */
340         if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
341         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" );
342
343         return TRUE;
344 } /* Handle_Request */
345
346
347 /* -eof- */