]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/parse.c
Move "ClientHost" and "ClientUserNick" to end of [Global] section
[ngircd-alex.git] / src / ngircd / parse.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 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 #include "portab.h"
13
14 /**
15  * @file
16  * IRC command parser and validator.
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <strings.h>
25
26 #include "ngircd.h"
27 #include "defines.h"
28 #include "conn-func.h"
29 #include "channel.h"
30 #include "log.h"
31 #include "messages.h"
32 #include "tool.h"
33
34 #include "exp.h"
35 #include "parse.h"
36
37 #include "imp.h"
38 #include "irc.h"
39 #include "irc-channel.h"
40 #include "irc-info.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 #include "numeric.h"
48
49 #include "exp.h"
50
51 struct _NUMERIC {
52         int numeric;
53         bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
54 };
55
56
57 static COMMAND My_Commands[] =
58 {
59         { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
60         { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
61         { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
62         { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
63         { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
64         { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
65         { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
66         { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
67         { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68         { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
69         { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70         { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71         { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72         { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73         { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74         { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75         { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76         { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77         { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78         { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
79         { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
80         { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
81         { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
82         { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
83         { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
84         { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
85         { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86         { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
87         { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
88         { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
89         { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
90         { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
91         { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
92         { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
93         { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
94         { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
95         { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96         { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97         { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98         { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99         { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100         { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
101         { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
102         { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103         { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104         { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105         { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
106         { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
107         { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108         { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
109 #ifdef IRCPLUS
110         { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
111 #endif
112         { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
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  * Return the pointer to the global "IRC command structure".
125  * This structure, an array of type "COMMAND" describes all the IRC commands
126  * implemented by ngIRCd and how to handle them.
127  * @return Pointer to the global command structure.
128  */
129 GLOBAL COMMAND *
130 Parse_GetCommandStruct( void )
131 {
132         return My_Commands;
133 } /* Parse_GetCommandStruct */
134
135
136 /**
137  * Parse a command ("request") received from a client.
138  * 
139  * This function is called after the connection layer received a valid CR+LF
140  * terminated line of text: we asume that this is a valid IRC command and
141  * try to do something useful with it :-)
142  *
143  * All errors are reported to the client from which the command has been
144  * received, and if the error is fatal this connection is closed down.
145  *
146  * This function is able to parse the syntax as described in RFC 2812,
147  * section 2.3.
148  *
149  * @param Idx Index of the connection from which the command has been received.
150  * @param Request NULL terminated line of text (the "command").
151  * @return true on success (valid command or "regular" error), false if a
152  *      fatal error occured and the connection has been shut down.
153  */
154 GLOBAL bool
155 Parse_Request( CONN_ID Idx, char *Request )
156 {
157         REQUEST req;
158         char *start, *ptr;
159         bool closed;
160
161         assert( Idx >= 0 );
162         assert( Request != NULL );
163
164 #ifdef SNIFFER
165         if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
166 #endif
167
168         Init_Request( &req );
169
170         /* remove leading & trailing whitespace */
171         ngt_TrimStr( Request );
172
173         if( Request[0] == ':' )
174         {
175                 /* Prefix */
176                 req.prefix = Request + 1;
177                 ptr = strchr( Request, ' ' );
178                 if( ! ptr )
179                 {
180                         LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
181                         return Conn_WriteStr(Idx, "ERROR :Prefix without command");
182                 }
183                 *ptr = '\0';
184 #ifndef STRICT_RFC
185                 /* ignore multiple spaces between prefix and command */
186                 while( *(ptr + 1) == ' ' ) ptr++;
187 #endif
188                 start = ptr + 1;
189         }
190         else start = Request;
191
192         ptr = strchr( start, ' ' );
193         if( ptr )
194         {
195                 *ptr = '\0';
196 #ifndef STRICT_RFC
197                 /* ignore multiple spaces between parameters */
198                 while( *(ptr + 1) == ' ' ) ptr++;
199 #endif
200         }
201         req.command = start;
202
203         /* Arguments, Parameters */
204         if( ptr )
205         {
206                 start = ptr + 1;
207                 while( start )
208                 {
209                         if( start[0] == ':' )
210                         {
211                                 req.argv[req.argc] = start + 1;
212                                 ptr = NULL;
213                         }
214                         else
215                         {
216                                 req.argv[req.argc] = start;
217                                 ptr = strchr( start, ' ' );
218                                 if( ptr )
219                                 {
220                                         *ptr = '\0';
221 #ifndef STRICT_RFC
222                                         while( *(ptr + 1) == ' ' ) ptr++;
223 #endif
224                                 }
225                         }
226
227                         req.argc++;
228
229                         if( start[0] == ':' ) break;
230                         if( req.argc > 14 ) break;
231
232                         if( ptr ) start = ptr + 1;
233                         else start = NULL;
234                 }
235         }
236
237         if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
238         if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
239         if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
240
241         return Handle_Request( Idx, &req );
242 } /* Parse_Request */
243
244
245 /**
246  * Initialize request structure.
247  * @param Req Request structure to be initialized.
248  */
249 static void
250 Init_Request( REQUEST *Req )
251 {
252         /* Neue Request-Struktur initialisieren */
253
254         int i;
255
256         assert( Req != NULL );
257
258         Req->prefix = NULL;
259         Req->command = NULL;
260         for( i = 0; i < 15; Req->argv[i++] = NULL );
261         Req->argc = 0;
262 } /* Init_Request */
263
264
265 static bool
266 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
267 {
268         CLIENT *client, *c;
269
270         assert( Idx >= 0 );
271         assert( Req != NULL );
272
273         *Closed = false;
274
275         if( ! Req->prefix ) return true;
276
277         client = Conn_GetClient( Idx );
278         assert( client != NULL );
279
280         /* only validate if this connection is already registered */
281         if (Client_Type(client) != CLIENT_USER
282             && Client_Type(client) != CLIENT_SERVER
283             && Client_Type(client) != CLIENT_SERVICE) {
284                 /* not registered, ignore prefix */
285                 Req->prefix = NULL;
286                 return true;
287         }
288
289         /* check if client in prefix is known */
290         c = Client_Search( Req->prefix );
291         if (!c) {
292                 Log(LOG_ERR,
293                     "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
294                     Req->prefix, Idx, Req->command);
295                 if (!Conn_WriteStr(Idx,
296                                    "ERROR :Invalid prefix \"%s\", client not known",
297                                    Req->prefix))
298                         *Closed = true;
299                 return false;
300         }
301
302         /* check if the client named in the prefix is expected
303          * to come from that direction */
304         if (Client_NextHop(c) != client) {
305                 Log(LOG_ERR,
306                     "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
307                     Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
308                     Req->command);
309                 Conn_Close(Idx, NULL, "Spoofed prefix", true);
310                 *Closed = true;
311                 return false;
312         }
313
314         return true;
315 } /* Validate_Prefix */
316
317
318 static bool
319 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
320 {
321         assert( Idx >= 0 );
322         assert( Req != NULL );
323         *Closed = false;
324
325         return true;
326 } /* Validate_Comman */
327
328
329 static bool
330 #ifdef STRICT_RFC
331 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
332 #else
333 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
334 #endif
335 {
336 #ifdef STRICT_RFC
337         int i;
338 #endif
339
340         *Closed = false;
341
342 #ifdef STRICT_RFC
343         assert( Idx >= 0 );
344         assert( Req != NULL );
345
346         /* CR and LF are never allowed in command parameters.
347          * But since we do accept lines terminated only with CR or LF in
348          * "non-RFC-compliant mode" (besides the correct CR+LF combination),
349          * this check can only trigger in "strict RFC" mode; therefore we
350          * optimize it away otherwise ... */
351         for (i = 0; i < Req->argc; i++) {
352                 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
353                         Log(LOG_ERR,
354                             "Invalid character(s) in parameter (connection %d, command %s)!?",
355                             Idx, Req->command);
356                         if (!Conn_WriteStr(Idx,
357                                            "ERROR :Invalid character(s) in parameter!"))
358                                 *Closed = true;
359                         return false;
360                 }
361         }
362 #endif
363
364         return true;
365 } /* Validate_Args */
366
367
368 /* Command is a status code ("numeric") from another server */
369 static bool
370 Handle_Numeric(CLIENT *client, REQUEST *Req)
371 {
372         static const struct _NUMERIC Numerics[] = {
373                 {   5, IRC_Num_ISUPPORT },
374                 {  20, NULL },
375                 { 376, IRC_Num_ENDOFMOTD }
376         };
377         int i, num;
378         char str[LINE_LEN];
379         CLIENT *prefix, *target = NULL;
380
381         /* Determine target */
382         if (Req->argc > 0) {
383                 if (strcmp(Req->argv[0], "*") != 0)
384                         target = Client_Search(Req->argv[0]);
385                 else
386                         target = Client_ThisServer();
387         }
388
389         if (!target) {
390                 /* Status code without target!? */
391                 if (Req->argc > 0)
392                         Log(LOG_WARNING,
393                             "Unknown target for status code %s: \"%s\"",
394                             Req->command, Req->argv[0]);
395                 else
396                         Log(LOG_WARNING,
397                             "Unknown target for status code %s!",
398                             Req->command);
399                 return true;
400         }
401         if (target == Client_ThisServer()) {
402                 /* This server is the target of the numeric */
403                 num = atoi(Req->command);
404
405                 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
406                         if (num == Numerics[i].numeric) {
407                                 if (!Numerics[i].function)
408                                         return CONNECTED;
409                                 return Numerics[i].function(client, Req);
410                         }
411                 }
412
413                 LogDebug("Ignored status code %s from \"%s\".",
414                          Req->command, Client_ID(client));
415                 return true;
416         }
417
418         /* Determine source */
419         if (! Req->prefix[0]) {
420                 /* Oops, no prefix!? */
421                 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
422                                                 Req->command, Client_ID(client));
423                 return true;
424         }
425
426         prefix = Client_Search(Req->prefix);
427         if (! prefix) { /* Oops, unknown prefix!? */
428                 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
429                 return true;
430         }
431
432         /* Forward status code */
433         strlcpy(str, Req->command, sizeof(str));
434         for (i = 0; i < Req->argc; i++) {
435                 if (i < Req->argc - 1)
436                         strlcat(str, " ", sizeof(str));
437                 else
438                         strlcat(str, " :", sizeof(str));
439                 strlcat(str, Req->argv[i], sizeof(str));
440         }
441         return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
442 }
443
444
445 static bool
446 Handle_Request( CONN_ID Idx, REQUEST *Req )
447 {
448         CLIENT *client;
449         bool result = true;
450         int client_type;
451         COMMAND *cmd;
452
453         assert( Idx >= 0 );
454         assert( Req != NULL );
455         assert( Req->command != NULL );
456
457         client = Conn_GetClient( Idx );
458         assert( client != NULL );
459
460         /* Numeric? */
461         client_type = Client_Type(client);
462         if ((client_type == CLIENT_SERVER ||
463              client_type == CLIENT_UNKNOWNSERVER)
464             && strlen(Req->command) == 3 && atoi(Req->command) > 1)
465                 return Handle_Numeric(client, Req);
466
467         cmd = My_Commands;
468         while (cmd->name) {
469                 if (strcasecmp(Req->command, cmd->name) != 0) {
470                         cmd++;
471                         continue;
472                 }
473
474                 if (!(client_type & cmd->type))
475                         return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
476
477                 /* Command is allowed for this client: call it and count produced bytes */
478                 Conn_ResetWCounter();
479                 result = (cmd->function)(client, Req);
480                 cmd->bytes += Conn_WCounter();
481
482                 /* Adjust counters */
483                 if (client_type != CLIENT_SERVER)
484                         cmd->lcount++;
485                 else
486                         cmd->rcount++;
487                 return result;
488         }
489
490         if (client_type != CLIENT_USER &&
491             client_type != CLIENT_SERVER &&
492             client_type != CLIENT_SERVICE )
493                 return true;
494
495         /* Unknown command and registered connection: generate error: */
496         LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
497                         Client_Conn( client ), Req->command, Req->argc,
498                         Req->argc == 1 ? "parameter" : "parameters",
499                         Req->prefix ? "" : " no" );
500
501         if (Client_Type(client) != CLIENT_SERVER) {
502                 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
503                                 Client_ID(client), Req->command);
504                 Conn_SetPenalty(Idx, 1);
505         }
506         return result;
507 } /* Handle_Request */
508
509
510 /* -eof- */