Blame


1 6e07fb41 2001-12-21 alex /*
2 6e07fb41 2001-12-21 alex * ngIRCd -- The Next Generation IRC Daemon
3 b6254bbb 2002-01-02 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 6e07fb41 2001-12-21 alex *
5 6e07fb41 2001-12-21 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 6e07fb41 2001-12-21 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 6e07fb41 2001-12-21 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 6e07fb41 2001-12-21 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 6e07fb41 2001-12-21 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 804b1ec4 2001-12-31 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11 6e07fb41 2001-12-21 alex *
12 bc4ed226 2002-03-25 alex * $Id: parse.c,v 1.31 2002/03/25 17:13:46 alex Exp $
13 6e07fb41 2001-12-21 alex *
14 6e07fb41 2001-12-21 alex * parse.c: Parsen der Client-Anfragen
15 6e07fb41 2001-12-21 alex */
16 6e07fb41 2001-12-21 alex
17 6e07fb41 2001-12-21 alex
18 ca33cbda 2002-03-12 alex #include "portab.h"
19 6e07fb41 2001-12-21 alex
20 ca33cbda 2002-03-12 alex #include "imp.h"
21 6e07fb41 2001-12-21 alex #include <assert.h>
22 1c8c92af 2002-01-05 alex #include <stdlib.h>
23 6e07fb41 2001-12-21 alex #include <stdio.h>
24 6e07fb41 2001-12-21 alex #include <string.h>
25 6e07fb41 2001-12-21 alex
26 d79a7d28 2002-01-18 alex #include "ngircd.h"
27 76c4f066 2001-12-23 alex #include "client.h"
28 6e07fb41 2001-12-21 alex #include "conn.h"
29 ca33cbda 2002-03-12 alex #include "defines.h"
30 76c4f066 2001-12-23 alex #include "irc.h"
31 14aba7c1 2002-03-03 alex #include "irc-channel.h"
32 b56eb4d8 2002-02-27 alex #include "irc-login.h"
33 b56eb4d8 2002-02-27 alex #include "irc-mode.h"
34 14aba7c1 2002-03-03 alex #include "irc-oper.h"
35 14aba7c1 2002-03-03 alex #include "irc-server.h"
36 b56eb4d8 2002-02-27 alex #include "irc-write.h"
37 6e07fb41 2001-12-21 alex #include "log.h"
38 76c4f066 2001-12-23 alex #include "messages.h"
39 363a03b8 2001-12-29 alex #include "tool.h"
40 6e07fb41 2001-12-21 alex
41 ca33cbda 2002-03-12 alex #include "exp.h"
42 6e07fb41 2001-12-21 alex #include "parse.h"
43 6e07fb41 2001-12-21 alex
44 6e07fb41 2001-12-21 alex
45 6e07fb41 2001-12-21 alex LOCAL VOID Init_Request( REQUEST *Req );
46 6e07fb41 2001-12-21 alex
47 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error );
48 6e07fb41 2001-12-21 alex
49 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Validate_Prefix( REQUEST *Req );
50 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Validate_Command( REQUEST *Req );
51 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Validate_Args( REQUEST *Req );
52 6e07fb41 2001-12-21 alex
53 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req );
54 6e07fb41 2001-12-21 alex
55 6e07fb41 2001-12-21 alex
56 6e07fb41 2001-12-21 alex GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request )
57 6e07fb41 2001-12-21 alex {
58 76c4f066 2001-12-23 alex /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
59 76c4f066 2001-12-23 alex * die Verbindung geschlossen und FALSE geliefert.
60 6e07fb41 2001-12-21 alex * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
61 6e07fb41 2001-12-21 alex
62 6e07fb41 2001-12-21 alex REQUEST req;
63 6e07fb41 2001-12-21 alex CHAR *start, *ptr;
64 6e07fb41 2001-12-21 alex
65 6e07fb41 2001-12-21 alex assert( Idx >= 0 );
66 6e07fb41 2001-12-21 alex assert( Request != NULL );
67 76c4f066 2001-12-23 alex
68 d4a60bd4 2001-12-25 alex #ifdef SNIFFER
69 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
70 76c4f066 2001-12-23 alex #endif
71 6e07fb41 2001-12-21 alex
72 6e07fb41 2001-12-21 alex Init_Request( &req );
73 6e07fb41 2001-12-21 alex
74 363a03b8 2001-12-29 alex /* Fuehrendes und folgendes "Geraffel" verwerfen */
75 363a03b8 2001-12-29 alex ngt_TrimStr( Request );
76 363a03b8 2001-12-29 alex
77 6e07fb41 2001-12-21 alex /* gibt es ein Prefix? */
78 6e07fb41 2001-12-21 alex if( Request[0] == ':' )
79 6e07fb41 2001-12-21 alex {
80 6e07fb41 2001-12-21 alex /* Prefix vorhanden */
81 6e07fb41 2001-12-21 alex req.prefix = Request + 1;
82 6e07fb41 2001-12-21 alex ptr = strchr( Request, ' ' );
83 356683ff 2002-01-04 alex if( ! ptr ) return Parse_Error( Idx, "Prefix without command!?" );
84 6e07fb41 2001-12-21 alex *ptr = '\0';
85 3543c222 2002-01-09 alex #ifndef STRICT_RFC
86 3543c222 2002-01-09 alex /* multiple Leerzeichen als Trenner zwischen
87 3543c222 2002-01-09 alex * Prefix und Befehl ignorieren */
88 3543c222 2002-01-09 alex while( *(ptr + 1) == ' ' ) ptr++;
89 3543c222 2002-01-09 alex #endif
90 6e07fb41 2001-12-21 alex start = ptr + 1;
91 6e07fb41 2001-12-21 alex }
92 6e07fb41 2001-12-21 alex else start = Request;
93 6e07fb41 2001-12-21 alex
94 6e07fb41 2001-12-21 alex if( ! Validate_Prefix( &req )) return Parse_Error( Idx, "Invalid prefix");
95 6e07fb41 2001-12-21 alex
96 6e07fb41 2001-12-21 alex /* Befehl */
97 6e07fb41 2001-12-21 alex ptr = strchr( start, ' ' );
98 3543c222 2002-01-09 alex if( ptr )
99 3543c222 2002-01-09 alex {
100 3543c222 2002-01-09 alex *ptr = '\0';
101 3543c222 2002-01-09 alex #ifndef STRICT_RFC
102 3543c222 2002-01-09 alex /* multiple Leerzeichen als Trenner vor
103 3543c222 2002-01-09 alex *Parametertrennern ignorieren */
104 3543c222 2002-01-09 alex while( *(ptr + 1) == ' ' ) ptr++;
105 3543c222 2002-01-09 alex #endif
106 3543c222 2002-01-09 alex }
107 6e07fb41 2001-12-21 alex req.command = start;
108 6e07fb41 2001-12-21 alex
109 6e07fb41 2001-12-21 alex if( ! Validate_Command( &req )) return Parse_Error( Idx, "Invalid command" );
110 6e07fb41 2001-12-21 alex
111 6e07fb41 2001-12-21 alex /* Argumente, Parameter */
112 6e07fb41 2001-12-21 alex if( ptr )
113 6e07fb41 2001-12-21 alex {
114 6e07fb41 2001-12-21 alex /* Prinzipiell gibt es welche :-) */
115 6e07fb41 2001-12-21 alex start = ptr + 1;
116 6e07fb41 2001-12-21 alex while( start )
117 6e07fb41 2001-12-21 alex {
118 6e07fb41 2001-12-21 alex /* Parameter-String "zerlegen" */
119 76c4f066 2001-12-23 alex if( start[0] == ':' )
120 76c4f066 2001-12-23 alex {
121 76c4f066 2001-12-23 alex req.argv[req.argc] = start + 1;
122 76c4f066 2001-12-23 alex ptr = NULL;
123 76c4f066 2001-12-23 alex }
124 76c4f066 2001-12-23 alex else
125 76c4f066 2001-12-23 alex {
126 76c4f066 2001-12-23 alex req.argv[req.argc] = start;
127 76c4f066 2001-12-23 alex ptr = strchr( start, ' ' );
128 3543c222 2002-01-09 alex if( ptr )
129 3543c222 2002-01-09 alex {
130 3543c222 2002-01-09 alex *ptr = '\0';
131 3543c222 2002-01-09 alex #ifndef STRICT_RFC
132 3543c222 2002-01-09 alex /* multiple Leerzeichen als
133 3543c222 2002-01-09 alex * Parametertrenner ignorieren */
134 3543c222 2002-01-09 alex while( *(ptr + 1) == ' ' ) ptr++;
135 3543c222 2002-01-09 alex #endif
136 3543c222 2002-01-09 alex }
137 76c4f066 2001-12-23 alex }
138 6e07fb41 2001-12-21 alex
139 6e07fb41 2001-12-21 alex req.argc++;
140 6e07fb41 2001-12-21 alex
141 6e07fb41 2001-12-21 alex if( start[0] == ':' ) break;
142 6e07fb41 2001-12-21 alex if( req.argc > 14 ) break;
143 6e07fb41 2001-12-21 alex
144 6e07fb41 2001-12-21 alex if( ptr ) start = ptr + 1;
145 6e07fb41 2001-12-21 alex else start = NULL;
146 6e07fb41 2001-12-21 alex }
147 6e07fb41 2001-12-21 alex }
148 6e07fb41 2001-12-21 alex
149 6e07fb41 2001-12-21 alex if( ! Validate_Args( &req )) return Parse_Error( Idx, "Invalid argument(s)" );
150 93aa0dbf 2002-01-03 alex
151 6e07fb41 2001-12-21 alex return Handle_Request( Idx, &req );
152 6e07fb41 2001-12-21 alex } /* Parse_Request */
153 6e07fb41 2001-12-21 alex
154 6e07fb41 2001-12-21 alex
155 6e07fb41 2001-12-21 alex LOCAL VOID Init_Request( REQUEST *Req )
156 6e07fb41 2001-12-21 alex {
157 6e07fb41 2001-12-21 alex /* Neue Request-Struktur initialisieren */
158 6e07fb41 2001-12-21 alex
159 6e07fb41 2001-12-21 alex INT i;
160 6e07fb41 2001-12-21 alex
161 6e07fb41 2001-12-21 alex assert( Req != NULL );
162 6e07fb41 2001-12-21 alex
163 6e07fb41 2001-12-21 alex Req->prefix = NULL;
164 6e07fb41 2001-12-21 alex Req->command = NULL;
165 6e07fb41 2001-12-21 alex for( i = 0; i < 15; Req->argv[i++] = NULL );
166 6e07fb41 2001-12-21 alex Req->argc = 0;
167 6e07fb41 2001-12-21 alex } /* Init_Request */
168 6e07fb41 2001-12-21 alex
169 6e07fb41 2001-12-21 alex
170 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error )
171 6e07fb41 2001-12-21 alex {
172 6e07fb41 2001-12-21 alex /* Fehler beim Parsen. Fehlermeldung an den Client schicken.
173 6e07fb41 2001-12-21 alex * TRUE: Connection wurde durch diese Funktion nicht geschlossen,
174 6e07fb41 2001-12-21 alex * FALSE: Connection wurde terminiert. */
175 6e07fb41 2001-12-21 alex
176 6e07fb41 2001-12-21 alex assert( Idx >= 0 );
177 6e07fb41 2001-12-21 alex assert( Error != NULL );
178 6e07fb41 2001-12-21 alex
179 356683ff 2002-01-04 alex Log( LOG_DEBUG, "Connection %d: Parse error: %s", Idx, Error );
180 356683ff 2002-01-04 alex return Conn_WriteStr( Idx, "ERROR :Parse error: %s", Error );
181 6e07fb41 2001-12-21 alex } /* Parse_Error */
182 6e07fb41 2001-12-21 alex
183 6e07fb41 2001-12-21 alex
184 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Validate_Prefix( REQUEST *Req )
185 6e07fb41 2001-12-21 alex {
186 6e07fb41 2001-12-21 alex assert( Req != NULL );
187 6e07fb41 2001-12-21 alex return TRUE;
188 6e07fb41 2001-12-21 alex } /* Validate_Prefix */
189 6e07fb41 2001-12-21 alex
190 6e07fb41 2001-12-21 alex
191 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Validate_Command( REQUEST *Req )
192 6e07fb41 2001-12-21 alex {
193 6e07fb41 2001-12-21 alex assert( Req != NULL );
194 6e07fb41 2001-12-21 alex return TRUE;
195 6e07fb41 2001-12-21 alex } /* Validate_Comman */
196 6e07fb41 2001-12-21 alex
197 6e07fb41 2001-12-21 alex
198 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Validate_Args( REQUEST *Req )
199 6e07fb41 2001-12-21 alex {
200 6e07fb41 2001-12-21 alex assert( Req != NULL );
201 6e07fb41 2001-12-21 alex return TRUE;
202 6e07fb41 2001-12-21 alex } /* Validate_Args */
203 6e07fb41 2001-12-21 alex
204 6e07fb41 2001-12-21 alex
205 6e07fb41 2001-12-21 alex LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req )
206 6e07fb41 2001-12-21 alex {
207 76c4f066 2001-12-23 alex /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
208 76c4f066 2001-12-23 alex * wird die Verbindung geschlossen und FALSE geliefert. */
209 76c4f066 2001-12-23 alex
210 1c8c92af 2002-01-05 alex CLIENT *client, *target, *prefix;
211 1c8c92af 2002-01-05 alex CHAR str[LINE_LEN];
212 1c8c92af 2002-01-05 alex INT i;
213 76c4f066 2001-12-23 alex
214 6e07fb41 2001-12-21 alex assert( Idx >= 0 );
215 6e07fb41 2001-12-21 alex assert( Req != NULL );
216 6e07fb41 2001-12-21 alex assert( Req->command != NULL );
217 6e07fb41 2001-12-21 alex
218 76c4f066 2001-12-23 alex client = Client_GetFromConn( Idx );
219 76c4f066 2001-12-23 alex assert( client != NULL );
220 1c8c92af 2002-01-05 alex
221 1c8c92af 2002-01-05 alex /* Statuscode, der geforwarded werden muss? */
222 1c8c92af 2002-01-05 alex if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
223 1c8c92af 2002-01-05 alex {
224 1c8c92af 2002-01-05 alex /* Befehl ist ein Statuscode */
225 1c8c92af 2002-01-05 alex
226 1c8c92af 2002-01-05 alex /* Zielserver ermitteln */
227 bc4ed226 2002-03-25 alex if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] );
228 1c8c92af 2002-01-05 alex else target = NULL;
229 9e18ec30 2002-01-07 alex if( ! target )
230 9e18ec30 2002-01-07 alex {
231 1ff2fceb 2002-02-26 alex if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code: \"%s\"", Req->argv[0] );
232 9e18ec30 2002-01-07 alex else Log( LOG_WARNING, "Unknown target for status code!" );
233 9e18ec30 2002-01-07 alex return TRUE;
234 9e18ec30 2002-01-07 alex }
235 9e18ec30 2002-01-07 alex if( target == Client_ThisServer( ))
236 9e18ec30 2002-01-07 alex {
237 9e18ec30 2002-01-07 alex Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
238 9e18ec30 2002-01-07 alex return TRUE;
239 9e18ec30 2002-01-07 alex }
240 76c4f066 2001-12-23 alex
241 1c8c92af 2002-01-05 alex /* Quell-Client ermitteln */
242 9e18ec30 2002-01-07 alex if( ! Req->prefix[0] )
243 9e18ec30 2002-01-07 alex {
244 9e18ec30 2002-01-07 alex Log( LOG_WARNING, "Got status code without prefix!?" );
245 9e18ec30 2002-01-07 alex return TRUE;
246 9e18ec30 2002-01-07 alex }
247 bc4ed226 2002-03-25 alex else prefix = Client_Search( Req->prefix );
248 9e18ec30 2002-01-07 alex if( ! prefix )
249 9e18ec30 2002-01-07 alex {
250 9e18ec30 2002-01-07 alex Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix );
251 9e18ec30 2002-01-07 alex return TRUE;
252 9e18ec30 2002-01-07 alex }
253 1c8c92af 2002-01-05 alex
254 1c8c92af 2002-01-05 alex /* Statuscode weiterleiten */
255 1c8c92af 2002-01-05 alex strcpy( str, Req->command );
256 1c8c92af 2002-01-05 alex for( i = 0; i < Req->argc; i++ )
257 1c8c92af 2002-01-05 alex {
258 1c8c92af 2002-01-05 alex if( i < Req->argc - 1 ) strcat( str, " " );
259 1c8c92af 2002-01-05 alex else strcat( str, " :" );
260 1c8c92af 2002-01-05 alex strcat( str, Req->argv[i] );
261 1c8c92af 2002-01-05 alex }
262 1c8c92af 2002-01-05 alex return IRC_WriteStrClientPrefix( target, prefix, str );
263 1c8c92af 2002-01-05 alex }
264 1c8c92af 2002-01-05 alex
265 cfd78b90 2001-12-25 alex if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req );
266 cfd78b90 2001-12-25 alex else if( strcasecmp( Req->command, "NICK" ) == 0 ) return IRC_NICK( client, Req );
267 cfd78b90 2001-12-25 alex else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req );
268 93aa0dbf 2002-01-03 alex else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req );
269 93aa0dbf 2002-01-03 alex else if( strcasecmp( Req->command, "NJOIN" ) == 0 ) return IRC_NJOIN( client, Req );
270 d4a60bd4 2001-12-25 alex else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req );
271 5ff42762 2002-01-04 alex else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req );
272 c9067fb7 2001-12-26 alex else if( strcasecmp( Req->command, "PING" ) == 0 ) return IRC_PING( client, Req );
273 c9067fb7 2001-12-26 alex else if( strcasecmp( Req->command, "PONG" ) == 0 ) return IRC_PONG( client, Req );
274 cfd78b90 2001-12-25 alex else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req );
275 4c6a99cf 2001-12-27 alex else if( strcasecmp( Req->command, "PRIVMSG" ) == 0 ) return IRC_PRIVMSG( client, Req );
276 4c6a99cf 2001-12-27 alex else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req );
277 363a03b8 2001-12-29 alex else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req );
278 fb9d6ce1 2001-12-31 alex else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req );
279 804b1ec4 2001-12-31 alex else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req );
280 804b1ec4 2001-12-31 alex else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req );
281 804b1ec4 2001-12-31 alex else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req );
282 804b1ec4 2001-12-31 alex else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req );
283 804b1ec4 2001-12-31 alex else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req );
284 804b1ec4 2001-12-31 alex else if( strcasecmp( Req->command, "RESTART" ) == 0 ) return IRC_RESTART( client, Req );
285 b6254bbb 2002-01-02 alex else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req );
286 c2ee5437 2002-01-11 alex else if( strcasecmp( Req->command, "LUSERS" ) == 0 ) return IRC_LUSERS( client, Req );
287 c2ee5437 2002-01-11 alex else if( strcasecmp( Req->command, "LINKS" ) == 0 ) return IRC_LINKS( client, Req );
288 d3e0d78d 2002-01-21 alex else if( strcasecmp( Req->command, "JOIN" ) == 0 ) return IRC_JOIN( client, Req );
289 d3e0d78d 2002-01-21 alex else if( strcasecmp( Req->command, "PART" ) == 0 ) return IRC_PART( client, Req );
290 458174ff 2002-02-17 alex else if( strcasecmp( Req->command, "VERSION" ) == 0 ) return IRC_VERSION( client, Req );
291 aaa682fb 2002-02-23 alex else if( strcasecmp( Req->command, "KILL" ) == 0 ) return IRC_KILL( client, Req );
292 949977e8 2002-02-27 alex else if( strcasecmp( Req->command, "AWAY" ) == 0 ) return IRC_AWAY( client, Req );
293 d59f0290 2002-02-27 alex else if( strcasecmp( Req->command, "TOPIC" ) == 0 ) return IRC_TOPIC( client, Req );
294 14aba7c1 2002-03-03 alex else if( strcasecmp( Req->command, "WHO" ) == 0 ) return IRC_WHO( client, Req );
295 4c6a99cf 2001-12-27 alex
296 76c4f066 2001-12-23 alex /* Unbekannter Befehl */
297 a5677689 2002-01-06 alex if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
298 3d2e9448 2002-01-05 alex 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" );
299 76c4f066 2001-12-23 alex
300 6e07fb41 2001-12-21 alex return TRUE;
301 6e07fb41 2001-12-21 alex } /* Handle_Request */
302 6e07fb41 2001-12-21 alex
303 6e07fb41 2001-12-21 alex
304 6e07fb41 2001-12-21 alex /* -eof- */