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