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 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 6e07fb41 2001-12-21 alex *
11 490f28ff 2002-12-12 alex * IRC command parser and validator
12 6e07fb41 2001-12-21 alex */
13 6e07fb41 2001-12-21 alex
14 6e07fb41 2001-12-21 alex
15 ca33cbda 2002-03-12 alex #include "portab.h"
16 490f28ff 2002-12-12 alex
17 57c7e236 2004-01-17 alex static char UNUSED id[] = "$Id: parse.c,v 1.60 2004/01/17 03:17:49 alex Exp $";
18 6e07fb41 2001-12-21 alex
19 ca33cbda 2002-03-12 alex #include "imp.h"
20 6e07fb41 2001-12-21 alex #include <assert.h>
21 1c8c92af 2002-01-05 alex #include <stdlib.h>
22 6e07fb41 2001-12-21 alex #include <stdio.h>
23 6e07fb41 2001-12-21 alex #include <string.h>
24 57c7e236 2004-01-17 alex #include <strings.h>
25 6e07fb41 2001-12-21 alex
26 d79a7d28 2002-01-18 alex #include "ngircd.h"
27 ca33cbda 2002-03-12 alex #include "defines.h"
28 b8d7dcec 2002-12-30 alex #include "conn-func.h"
29 c2f60abe 2002-05-27 alex #include "client.h"
30 c2f60abe 2002-05-27 alex #include "channel.h"
31 c2f60abe 2002-05-27 alex #include "log.h"
32 c2f60abe 2002-05-27 alex #include "messages.h"
33 c2f60abe 2002-05-27 alex #include "tool.h"
34 c2f60abe 2002-05-27 alex
35 c2f60abe 2002-05-27 alex #include "exp.h"
36 c2f60abe 2002-05-27 alex #include "parse.h"
37 c2f60abe 2002-05-27 alex
38 c2f60abe 2002-05-27 alex #include "imp.h"
39 76c4f066 2001-12-23 alex #include "irc.h"
40 14aba7c1 2002-03-03 alex #include "irc-channel.h"
41 0c471b84 2002-11-30 alex #include "irc-info.h"
42 b56eb4d8 2002-02-27 alex #include "irc-login.h"
43 b56eb4d8 2002-02-27 alex #include "irc-mode.h"
44 c2f60abe 2002-05-27 alex #include "irc-op.h"
45 14aba7c1 2002-03-03 alex #include "irc-oper.h"
46 14aba7c1 2002-03-03 alex #include "irc-server.h"
47 b56eb4d8 2002-02-27 alex #include "irc-write.h"
48 6e07fb41 2001-12-21 alex
49 ca33cbda 2002-03-12 alex #include "exp.h"
50 1e59617d 2002-11-30 alex
51 1e59617d 2002-11-30 alex
52 1e59617d 2002-11-30 alex COMMAND My_Commands[] =
53 1e59617d 2002-11-30 alex {
54 17f7c6d3 2002-12-18 alex { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
55 17f7c6d3 2002-12-18 alex { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
56 17f7c6d3 2002-12-18 alex { "CONNECT", IRC_CONNECT, CLIENT_USER, 0, 0, 0 },
57 17f7c6d3 2002-12-18 alex { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
58 a7956f33 2002-12-31 alex { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
59 17f7c6d3 2002-12-18 alex { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
60 2152e377 2003-01-15 alex { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
61 17f7c6d3 2002-12-18 alex { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
62 17f7c6d3 2002-12-18 alex { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
63 17f7c6d3 2002-12-18 alex { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
64 17f7c6d3 2002-12-18 alex { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
65 17f7c6d3 2002-12-18 alex { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
66 17f7c6d3 2002-12-18 alex { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
67 17f7c6d3 2002-12-18 alex { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68 17f7c6d3 2002-12-18 alex { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
69 17f7c6d3 2002-12-18 alex { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70 17f7c6d3 2002-12-18 alex { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 17f7c6d3 2002-12-18 alex { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 17f7c6d3 2002-12-18 alex { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
73 17f7c6d3 2002-12-18 alex { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
74 17f7c6d3 2002-12-18 alex { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
75 17f7c6d3 2002-12-18 alex { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
76 17f7c6d3 2002-12-18 alex { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 17f7c6d3 2002-12-18 alex { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
78 17f7c6d3 2002-12-18 alex { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 17f7c6d3 2002-12-18 alex { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80 17f7c6d3 2002-12-18 alex { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 17f7c6d3 2002-12-18 alex { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
82 17f7c6d3 2002-12-18 alex { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
83 17f7c6d3 2002-12-18 alex { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
84 17f7c6d3 2002-12-18 alex { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
85 17f7c6d3 2002-12-18 alex { "SQUIT", IRC_SQUIT, CLIENT_SERVER, 0, 0, 0 },
86 17f7c6d3 2002-12-18 alex { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
87 17f7c6d3 2002-12-18 alex { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
88 17f7c6d3 2002-12-18 alex { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
89 42c4e6c7 2003-01-13 alex { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
90 17f7c6d3 2002-12-18 alex { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
91 17f7c6d3 2002-12-18 alex { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
92 17f7c6d3 2002-12-18 alex { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
93 17f7c6d3 2002-12-18 alex { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
94 17f7c6d3 2002-12-18 alex { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
95 17f7c6d3 2002-12-18 alex { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96 17f7c6d3 2002-12-18 alex #ifdef IRCPLUS
97 17f7c6d3 2002-12-18 alex { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
98 1e59617d 2002-11-30 alex #endif
99 17f7c6d3 2002-12-18 alex { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
100 1e59617d 2002-11-30 alex };
101 6e07fb41 2001-12-21 alex
102 6e07fb41 2001-12-21 alex
103 c2f60abe 2002-05-27 alex LOCAL VOID Init_Request PARAMS(( REQUEST *Req ));
104 6e07fb41 2001-12-21 alex
105 e0ed3aa1 2002-07-26 alex LOCAL BOOLEAN Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ));
106 e0ed3aa1 2002-07-26 alex LOCAL BOOLEAN Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ));
107 e0ed3aa1 2002-07-26 alex LOCAL BOOLEAN Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ));
108 6e07fb41 2001-12-21 alex
109 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
110 6e07fb41 2001-12-21 alex
111 6e07fb41 2001-12-21 alex
112 17f7c6d3 2002-12-18 alex GLOBAL COMMAND *
113 17f7c6d3 2002-12-18 alex Parse_GetCommandStruct( VOID )
114 17f7c6d3 2002-12-18 alex {
115 17f7c6d3 2002-12-18 alex return My_Commands;
116 17f7c6d3 2002-12-18 alex } /* Parse_GetCommandStruct */
117 17f7c6d3 2002-12-18 alex
118 17f7c6d3 2002-12-18 alex
119 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
120 c2f60abe 2002-05-27 alex Parse_Request( CONN_ID Idx, CHAR *Request )
121 6e07fb41 2001-12-21 alex {
122 76c4f066 2001-12-23 alex /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
123 76c4f066 2001-12-23 alex * die Verbindung geschlossen und FALSE geliefert.
124 6e07fb41 2001-12-21 alex * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
125 6e07fb41 2001-12-21 alex
126 6e07fb41 2001-12-21 alex REQUEST req;
127 6e07fb41 2001-12-21 alex CHAR *start, *ptr;
128 e0ed3aa1 2002-07-26 alex BOOLEAN closed;
129 6e07fb41 2001-12-21 alex
130 6e07fb41 2001-12-21 alex assert( Idx >= 0 );
131 6e07fb41 2001-12-21 alex assert( Request != NULL );
132 76c4f066 2001-12-23 alex
133 d4a60bd4 2001-12-25 alex #ifdef SNIFFER
134 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
135 76c4f066 2001-12-23 alex #endif
136 e0ed3aa1 2002-07-26 alex
137 6e07fb41 2001-12-21 alex Init_Request( &req );
138 6e07fb41 2001-12-21 alex
139 363a03b8 2001-12-29 alex /* Fuehrendes und folgendes "Geraffel" verwerfen */
140 363a03b8 2001-12-29 alex ngt_TrimStr( Request );
141 363a03b8 2001-12-29 alex
142 6e07fb41 2001-12-21 alex /* gibt es ein Prefix? */
143 6e07fb41 2001-12-21 alex if( Request[0] == ':' )
144 6e07fb41 2001-12-21 alex {
145 6e07fb41 2001-12-21 alex /* Prefix vorhanden */
146 6e07fb41 2001-12-21 alex req.prefix = Request + 1;
147 6e07fb41 2001-12-21 alex ptr = strchr( Request, ' ' );
148 e0ed3aa1 2002-07-26 alex if( ! ptr )
149 e0ed3aa1 2002-07-26 alex {
150 e0ed3aa1 2002-07-26 alex Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
151 e0ed3aa1 2002-07-26 alex return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
152 e0ed3aa1 2002-07-26 alex }
153 6e07fb41 2001-12-21 alex *ptr = '\0';
154 3543c222 2002-01-09 alex #ifndef STRICT_RFC
155 3543c222 2002-01-09 alex /* multiple Leerzeichen als Trenner zwischen
156 3543c222 2002-01-09 alex * Prefix und Befehl ignorieren */
157 3543c222 2002-01-09 alex while( *(ptr + 1) == ' ' ) ptr++;
158 3543c222 2002-01-09 alex #endif
159 6e07fb41 2001-12-21 alex start = ptr + 1;
160 6e07fb41 2001-12-21 alex }
161 6e07fb41 2001-12-21 alex else start = Request;
162 6e07fb41 2001-12-21 alex
163 6e07fb41 2001-12-21 alex /* Befehl */
164 6e07fb41 2001-12-21 alex ptr = strchr( start, ' ' );
165 3543c222 2002-01-09 alex if( ptr )
166 3543c222 2002-01-09 alex {
167 3543c222 2002-01-09 alex *ptr = '\0';
168 3543c222 2002-01-09 alex #ifndef STRICT_RFC
169 3543c222 2002-01-09 alex /* multiple Leerzeichen als Trenner vor
170 fc1118cb 2003-01-12 alex * Parametern ignorieren */
171 3543c222 2002-01-09 alex while( *(ptr + 1) == ' ' ) ptr++;
172 3543c222 2002-01-09 alex #endif
173 3543c222 2002-01-09 alex }
174 6e07fb41 2001-12-21 alex req.command = start;
175 6e07fb41 2001-12-21 alex
176 6e07fb41 2001-12-21 alex /* Argumente, Parameter */
177 6e07fb41 2001-12-21 alex if( ptr )
178 6e07fb41 2001-12-21 alex {
179 6e07fb41 2001-12-21 alex /* Prinzipiell gibt es welche :-) */
180 6e07fb41 2001-12-21 alex start = ptr + 1;
181 6e07fb41 2001-12-21 alex while( start )
182 6e07fb41 2001-12-21 alex {
183 6e07fb41 2001-12-21 alex /* Parameter-String "zerlegen" */
184 76c4f066 2001-12-23 alex if( start[0] == ':' )
185 76c4f066 2001-12-23 alex {
186 76c4f066 2001-12-23 alex req.argv[req.argc] = start + 1;
187 76c4f066 2001-12-23 alex ptr = NULL;
188 76c4f066 2001-12-23 alex }
189 76c4f066 2001-12-23 alex else
190 76c4f066 2001-12-23 alex {
191 76c4f066 2001-12-23 alex req.argv[req.argc] = start;
192 76c4f066 2001-12-23 alex ptr = strchr( start, ' ' );
193 3543c222 2002-01-09 alex if( ptr )
194 3543c222 2002-01-09 alex {
195 3543c222 2002-01-09 alex *ptr = '\0';
196 3543c222 2002-01-09 alex #ifndef STRICT_RFC
197 3543c222 2002-01-09 alex /* multiple Leerzeichen als
198 3543c222 2002-01-09 alex * Parametertrenner ignorieren */
199 3543c222 2002-01-09 alex while( *(ptr + 1) == ' ' ) ptr++;
200 3543c222 2002-01-09 alex #endif
201 3543c222 2002-01-09 alex }
202 76c4f066 2001-12-23 alex }
203 e0ed3aa1 2002-07-26 alex
204 6e07fb41 2001-12-21 alex req.argc++;
205 6e07fb41 2001-12-21 alex
206 6e07fb41 2001-12-21 alex if( start[0] == ':' ) break;
207 6e07fb41 2001-12-21 alex if( req.argc > 14 ) break;
208 e0ed3aa1 2002-07-26 alex
209 6e07fb41 2001-12-21 alex if( ptr ) start = ptr + 1;
210 6e07fb41 2001-12-21 alex else start = NULL;
211 6e07fb41 2001-12-21 alex }
212 6e07fb41 2001-12-21 alex }
213 93aa0dbf 2002-01-03 alex
214 b0b797b3 2002-11-28 alex /* Daten validieren */
215 b0b797b3 2002-11-28 alex if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
216 b0b797b3 2002-11-28 alex if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
217 e0ed3aa1 2002-07-26 alex if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
218 e0ed3aa1 2002-07-26 alex
219 6e07fb41 2001-12-21 alex return Handle_Request( Idx, &req );
220 6e07fb41 2001-12-21 alex } /* Parse_Request */
221 6e07fb41 2001-12-21 alex
222 6e07fb41 2001-12-21 alex
223 c2f60abe 2002-05-27 alex LOCAL VOID
224 c2f60abe 2002-05-27 alex Init_Request( REQUEST *Req )
225 6e07fb41 2001-12-21 alex {
226 6e07fb41 2001-12-21 alex /* Neue Request-Struktur initialisieren */
227 6e07fb41 2001-12-21 alex
228 6e07fb41 2001-12-21 alex INT i;
229 e0ed3aa1 2002-07-26 alex
230 6e07fb41 2001-12-21 alex assert( Req != NULL );
231 6e07fb41 2001-12-21 alex
232 6e07fb41 2001-12-21 alex Req->prefix = NULL;
233 6e07fb41 2001-12-21 alex Req->command = NULL;
234 6e07fb41 2001-12-21 alex for( i = 0; i < 15; Req->argv[i++] = NULL );
235 6e07fb41 2001-12-21 alex Req->argc = 0;
236 6e07fb41 2001-12-21 alex } /* Init_Request */
237 6e07fb41 2001-12-21 alex
238 6e07fb41 2001-12-21 alex
239 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
240 e0ed3aa1 2002-07-26 alex Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )
241 6e07fb41 2001-12-21 alex {
242 5063de59 2002-07-29 alex CLIENT *client, *c;
243 6e07fb41 2001-12-21 alex
244 e0ed3aa1 2002-07-26 alex assert( Idx >= 0 );
245 e0ed3aa1 2002-07-26 alex assert( Req != NULL );
246 6e07fb41 2001-12-21 alex
247 e0ed3aa1 2002-07-26 alex *Closed = FALSE;
248 5063de59 2002-07-29 alex
249 e0ed3aa1 2002-07-26 alex /* ist ueberhaupt ein Prefix vorhanden? */
250 e0ed3aa1 2002-07-26 alex if( ! Req->prefix ) return TRUE;
251 6e07fb41 2001-12-21 alex
252 5063de59 2002-07-29 alex /* Client-Struktur der Connection ermitteln */
253 5063de59 2002-07-29 alex client = Client_GetFromConn( Idx );
254 5063de59 2002-07-29 alex assert( client != NULL );
255 5063de59 2002-07-29 alex
256 5063de59 2002-07-29 alex /* nur validieren, wenn bereits registrierte Verbindung */
257 5063de59 2002-07-29 alex if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
258 5063de59 2002-07-29 alex {
259 5063de59 2002-07-29 alex /* noch nicht registrierte Verbindung.
260 5063de59 2002-07-29 alex * Das Prefix wird ignoriert. */
261 5063de59 2002-07-29 alex Req->prefix = NULL;
262 5063de59 2002-07-29 alex return TRUE;
263 5063de59 2002-07-29 alex }
264 5063de59 2002-07-29 alex
265 e0ed3aa1 2002-07-26 alex /* pruefen, ob der im Prefix angegebene Client bekannt ist */
266 e0ed3aa1 2002-07-26 alex c = Client_Search( Req->prefix );
267 e0ed3aa1 2002-07-26 alex if( ! c )
268 e0ed3aa1 2002-07-26 alex {
269 e0ed3aa1 2002-07-26 alex /* im Prefix angegebener Client ist nicht bekannt */
270 b0b797b3 2002-11-28 alex Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
271 dd09b17e 2002-11-20 alex if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = TRUE;
272 e0ed3aa1 2002-07-26 alex return FALSE;
273 e0ed3aa1 2002-07-26 alex }
274 5063de59 2002-07-29 alex
275 e0ed3aa1 2002-07-26 alex /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
276 e0ed3aa1 2002-07-26 alex * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
277 e0ed3aa1 2002-07-26 alex * gefaelscht ist */
278 5063de59 2002-07-29 alex if( Client_NextHop( c ) != client )
279 e0ed3aa1 2002-07-26 alex {
280 e0ed3aa1 2002-07-26 alex /* das angegebene Prefix ist aus dieser Richtung, also
281 e0ed3aa1 2002-07-26 alex * aus der gegebenen Connection, ungueltig! */
282 b0b797b3 2002-11-28 alex Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Client_GetFromConn( Idx )), Idx, Req->command );
283 e0ed3aa1 2002-07-26 alex Conn_Close( Idx, NULL, "Spoofed prefix", TRUE );
284 e0ed3aa1 2002-07-26 alex *Closed = TRUE;
285 e0ed3aa1 2002-07-26 alex return FALSE;
286 e0ed3aa1 2002-07-26 alex }
287 e0ed3aa1 2002-07-26 alex
288 6e07fb41 2001-12-21 alex return TRUE;
289 6e07fb41 2001-12-21 alex } /* Validate_Prefix */
290 6e07fb41 2001-12-21 alex
291 6e07fb41 2001-12-21 alex
292 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
293 e0ed3aa1 2002-07-26 alex Validate_Command( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )
294 6e07fb41 2001-12-21 alex {
295 e0ed3aa1 2002-07-26 alex assert( Idx >= 0 );
296 6e07fb41 2001-12-21 alex assert( Req != NULL );
297 e0ed3aa1 2002-07-26 alex *Closed = FALSE;
298 e0ed3aa1 2002-07-26 alex
299 6e07fb41 2001-12-21 alex return TRUE;
300 6e07fb41 2001-12-21 alex } /* Validate_Comman */
301 6e07fb41 2001-12-21 alex
302 6e07fb41 2001-12-21 alex
303 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
304 e0ed3aa1 2002-07-26 alex Validate_Args( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )
305 6e07fb41 2001-12-21 alex {
306 e0ed3aa1 2002-07-26 alex assert( Idx >= 0 );
307 6e07fb41 2001-12-21 alex assert( Req != NULL );
308 e0ed3aa1 2002-07-26 alex *Closed = FALSE;
309 e0ed3aa1 2002-07-26 alex
310 6e07fb41 2001-12-21 alex return TRUE;
311 6e07fb41 2001-12-21 alex } /* Validate_Args */
312 6e07fb41 2001-12-21 alex
313 6e07fb41 2001-12-21 alex
314 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
315 c2f60abe 2002-05-27 alex Handle_Request( CONN_ID Idx, REQUEST *Req )
316 6e07fb41 2001-12-21 alex {
317 76c4f066 2001-12-23 alex /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
318 76c4f066 2001-12-23 alex * wird die Verbindung geschlossen und FALSE geliefert. */
319 76c4f066 2001-12-23 alex
320 1c8c92af 2002-01-05 alex CLIENT *client, *target, *prefix;
321 1c8c92af 2002-01-05 alex CHAR str[LINE_LEN];
322 17f7c6d3 2002-12-18 alex BOOLEAN result;
323 1e59617d 2002-11-30 alex COMMAND *cmd;
324 1c8c92af 2002-01-05 alex INT i;
325 76c4f066 2001-12-23 alex
326 6e07fb41 2001-12-21 alex assert( Idx >= 0 );
327 6e07fb41 2001-12-21 alex assert( Req != NULL );
328 6e07fb41 2001-12-21 alex assert( Req->command != NULL );
329 6e07fb41 2001-12-21 alex
330 76c4f066 2001-12-23 alex client = Client_GetFromConn( Idx );
331 76c4f066 2001-12-23 alex assert( client != NULL );
332 1c8c92af 2002-01-05 alex
333 fc1118cb 2003-01-12 alex /* Statuscode? */
334 fc1118cb 2003-01-12 alex if(( Client_Type( client ) == CLIENT_SERVER ) && ( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
335 fc1118cb 2003-01-12 alex {
336 fc1118cb 2003-01-12 alex /* Command is a status code from an other server */
337 1c8c92af 2002-01-05 alex
338 fc1118cb 2003-01-12 alex /* Determine target */
339 fc1118cb 2003-01-12 alex if( Req->argc > 0 ) target = Client_Search( Req->argv[0] );
340 1c8c92af 2002-01-05 alex else target = NULL;
341 9e18ec30 2002-01-07 alex if( ! target )
342 9e18ec30 2002-01-07 alex {
343 fc1118cb 2003-01-12 alex /* Status code without target!? */
344 dd09b17e 2002-11-20 alex if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code %s: \"%s\"", Req->command, Req->argv[0] );
345 dd09b17e 2002-11-20 alex else Log( LOG_WARNING, "Unknown target for status code %s!", Req->command );
346 9e18ec30 2002-01-07 alex return TRUE;
347 9e18ec30 2002-01-07 alex }
348 9e18ec30 2002-01-07 alex if( target == Client_ThisServer( ))
349 9e18ec30 2002-01-07 alex {
350 fc1118cb 2003-01-12 alex /* This server is the target, ignore it */
351 9e18ec30 2002-01-07 alex Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
352 9e18ec30 2002-01-07 alex return TRUE;
353 9e18ec30 2002-01-07 alex }
354 76c4f066 2001-12-23 alex
355 fc1118cb 2003-01-12 alex /* Determine source */
356 9e18ec30 2002-01-07 alex if( ! Req->prefix[0] )
357 9e18ec30 2002-01-07 alex {
358 fc1118cb 2003-01-12 alex /* Oops, no prefix!? */
359 fc1118cb 2003-01-12 alex Log( LOG_WARNING, "Got status code %s from \"%s\" without prefix!?", Req->command, Client_ID( client ));
360 9e18ec30 2002-01-07 alex return TRUE;
361 9e18ec30 2002-01-07 alex }
362 bc4ed226 2002-03-25 alex else prefix = Client_Search( Req->prefix );
363 9e18ec30 2002-01-07 alex if( ! prefix )
364 9e18ec30 2002-01-07 alex {
365 fc1118cb 2003-01-12 alex /* Oops, unknown prefix!? */
366 fc1118cb 2003-01-12 alex Log( LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix );
367 9e18ec30 2002-01-07 alex return TRUE;
368 9e18ec30 2002-01-07 alex }
369 1c8c92af 2002-01-05 alex
370 fc1118cb 2003-01-12 alex /* Forward status code */
371 695631b2 2002-12-26 alex strlcpy( str, Req->command, sizeof( str ));
372 1c8c92af 2002-01-05 alex for( i = 0; i < Req->argc; i++ )
373 1c8c92af 2002-01-05 alex {
374 6626395c 2002-12-26 alex if( i < Req->argc - 1 ) strlcat( str, " ", sizeof( str ));
375 6626395c 2002-12-26 alex else strlcat( str, " :", sizeof( str ));
376 6626395c 2002-12-26 alex strlcat( str, Req->argv[i], sizeof( str ));
377 1c8c92af 2002-01-05 alex }
378 ec474a4b 2002-10-04 alex return IRC_WriteStrClientPrefix( target, prefix, "%s", str );
379 1c8c92af 2002-01-05 alex }
380 1c8c92af 2002-01-05 alex
381 1e59617d 2002-11-30 alex cmd = My_Commands;
382 1e59617d 2002-11-30 alex while( cmd->name )
383 1e59617d 2002-11-30 alex {
384 1e59617d 2002-11-30 alex /* Befehl suchen */
385 1e59617d 2002-11-30 alex if( strcasecmp( Req->command, cmd->name ) != 0 )
386 1e59617d 2002-11-30 alex {
387 1e59617d 2002-11-30 alex cmd++; continue;
388 1e59617d 2002-11-30 alex }
389 1e59617d 2002-11-30 alex
390 1e59617d 2002-11-30 alex if( Client_Type( client ) & cmd->type )
391 1e59617d 2002-11-30 alex {
392 17f7c6d3 2002-12-18 alex /* Command is allowed for this client: call it and count produced bytes */
393 17f7c6d3 2002-12-18 alex Conn_ResetWCounter( );
394 17f7c6d3 2002-12-18 alex result = (cmd->function)( client, Req );
395 17f7c6d3 2002-12-18 alex cmd->bytes += Conn_WCounter( );
396 17f7c6d3 2002-12-18 alex
397 17f7c6d3 2002-12-18 alex /* Adjust counters */
398 17f7c6d3 2002-12-18 alex if( Client_Type( client ) != CLIENT_SERVER ) cmd->lcount++;
399 17f7c6d3 2002-12-18 alex else cmd->rcount++;
400 17f7c6d3 2002-12-18 alex
401 17f7c6d3 2002-12-18 alex return result;
402 1e59617d 2002-11-30 alex }
403 1e59617d 2002-11-30 alex else
404 1e59617d 2002-11-30 alex {
405 1e59617d 2002-11-30 alex /* Befehl ist fuer diesen Client-Typ nicht erlaubt! */
406 1e59617d 2002-11-30 alex return IRC_WriteStrClient( client, ERR_NOTREGISTERED_MSG, Client_ID( client ));
407 1e59617d 2002-11-30 alex }
408 1e59617d 2002-11-30 alex }
409 d1ac4039 2002-09-03 alex
410 76c4f066 2001-12-23 alex /* Unbekannter Befehl */
411 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" );
412 1e59617d 2002-11-30 alex if( Client_Type( client ) != CLIENT_SERVER ) return IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
413 1e59617d 2002-11-30 alex else return TRUE;
414 6e07fb41 2001-12-21 alex } /* Handle_Request */
415 6e07fb41 2001-12-21 alex
416 6e07fb41 2001-12-21 alex
417 6e07fb41 2001-12-21 alex /* -eof- */