]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
SECURITY: Fixed a message handling bug which could crash the daemon.
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2004 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  * IRC commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "defines.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "match.h"
34 #include "messages.h"
35 #include "parse.h"
36
37 #include "exp.h"
38 #include "irc.h"
39
40
41 static char *Option_String PARAMS((CONN_ID Idx));
42 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType, bool SendErrors));
43 static bool Send_Message_Mask PARAMS((CLIENT *from, char *targetMask, char *message, bool SendErrors));
44
45
46 GLOBAL bool
47 IRC_ERROR( CLIENT *Client, REQUEST *Req )
48 {
49         assert( Client != NULL );
50         assert( Req != NULL );
51
52         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
53         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
54
55         return CONNECTED;
56 } /* IRC_ERROR */
57
58
59 /**
60  * Kill client on request.
61  * This function implements the IRC command "KILL" wich is used to selectively
62  * disconnect clients. It can be used by IRC operators and servers, for example
63  * to "solve" nick collisions after netsplits.
64  * Please note that this function is also called internally, without a real
65  * KILL command beeing received over the network! Client is Client_ThisServer()
66  * in this case. */
67 GLOBAL bool
68 IRC_KILL( CLIENT *Client, REQUEST *Req )
69 {
70         CLIENT *prefix, *c;
71         char reason[COMMAND_LEN], *msg;
72         CONN_ID my_conn, conn;
73
74         assert( Client != NULL );
75         assert( Req != NULL );
76
77         if(( Client_Type( Client ) != CLIENT_SERVER ) &&
78            ( ! Client_OperByMe( Client )))
79         {
80                 /* The originator of the KILL is neither an IRC operator of
81                  * this server nor a server. */
82                 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
83                                            Client_ID( Client ));
84         }
85
86         if( Req->argc != 2 )
87         {
88                 /* This command requires exactly 2 parameters! */
89                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
90                                            Client_ID( Client ), Req->command );
91         }
92
93         if( Req->prefix ) prefix = Client_Search( Req->prefix );
94         else prefix = Client;
95         if( ! prefix )
96         {
97                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
98                      Req->prefix );
99                 prefix = Client_ThisServer( );
100         }
101
102         if( Client != Client_ThisServer( ))
103         {
104                 /* This is a "real" KILL received from the network. */
105                 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
106                      Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
107         }
108
109         /* Build reason string */
110         if( Client_Type( Client ) == CLIENT_USER )
111         {
112                 /* Prefix the "reason" if the originator is a regular user,
113                  * so users can't spoof KILLs of servers. */
114                 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
115                           Client_ID( Client ), Req->argv[1] );
116         }
117         else
118                 strlcpy( reason, Req->argv[1], sizeof( reason ));
119
120         /* Inform other servers */
121         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
122                                    Req->argv[0], reason );
123
124         /* Save ID of this connection */
125         my_conn = Client_Conn( Client );
126
127         /* Do we host such a client? */
128         c = Client_Search( Req->argv[0] );
129         if( c )
130         {
131                 if(( Client_Type( c ) != CLIENT_USER ) &&
132                    ( Client_Type( c ) != CLIENT_GOTNICK ))
133                 {
134                         /* Target of this KILL is not a regular user, this is
135                          * invalid! So we ignore this case if we received a
136                          * regular KILL from the network and try to kill the
137                          * client/connection anyway (but log an error!) if the
138                          * origin is the local server. */
139
140                         if( Client != Client_ThisServer( ))
141                         {
142                                 /* Invalid KILL received from remote */
143                                 if( Client_Type( c ) == CLIENT_SERVER )
144                                         msg = ERR_CANTKILLSERVER_MSG;
145                                 else
146                                         msg = ERR_NOPRIVILEGES_MSG;
147                                 return IRC_WriteStrClient( Client, msg,
148                                         Client_ID( Client ));
149                         }
150
151                         Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
152                              Client_Type( c ), Req->argv[0] );
153                 }
154
155                 /* Kill client NOW! */
156                 conn = Client_Conn( c );
157                 Client_Destroy( c, NULL, reason, false );
158                 if( conn > NONE )
159                         Conn_Close( conn, NULL, reason, true );
160         }
161         else
162                 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
163
164         /* Are we still connected or were we killed, too? */
165         if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
166                 return CONNECTED;
167         else
168                 return DISCONNECTED;
169 } /* IRC_KILL */
170
171
172 /**
173  * Handler for the IRC command NOTICE.
174  */
175 GLOBAL bool
176 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
177 {
178         return Send_Message(Client, Req, CLIENT_USER, false);
179 } /* IRC_NOTICE */
180
181
182 /**
183  * Handler for the IRC command PRIVMSG.
184  */
185 GLOBAL bool
186 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
187 {
188         return Send_Message(Client, Req, CLIENT_USER, true);
189 } /* IRC_PRIVMSG */
190
191
192 /**
193  * Handler for the IRC command SQUERY.
194  */
195 GLOBAL bool
196 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
197 {
198         return Send_Message(Client, Req, CLIENT_SERVICE, true);
199 } /* IRC_SQUERY */
200
201
202 GLOBAL bool
203 IRC_TRACE( CLIENT *Client, REQUEST *Req )
204 {
205         CLIENT *from, *target, *c;
206         CONN_ID idx, idx2;
207         char user[CLIENT_USER_LEN];
208
209         assert( Client != NULL );
210         assert( Req != NULL );
211
212         /* Bad number of arguments? */
213         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
214
215         /* Search sender */
216         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
217         else from = Client;
218         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
219
220         /* Search target */
221         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
222         else target = Client_ThisServer( );
223         
224         /* Forward command to other server? */
225         if( target != Client_ThisServer( ))
226         {
227                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
228
229                 /* Send RPL_TRACELINK back to initiator */
230                 idx = Client_Conn( Client ); assert( idx > NONE );
231                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
232                 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
233
234                 /* Forward command */
235                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
236                 return CONNECTED;
237         }
238
239         /* Infos about all connected servers */
240         c = Client_First( );
241         while( c )
242         {
243                 if( Client_Conn( c ) > NONE )
244                 {
245                         /* Local client */
246                         if( Client_Type( c ) == CLIENT_SERVER )
247                         {
248                                 /* Server link */
249                                 strlcpy( user, Client_User( c ), sizeof( user ));
250                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
251                                 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
252                         }
253                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
254                         {
255                                 /* IRC Operator */
256                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
257                         }
258                 }
259                 c = Client_Next( c );
260         }
261
262         IRC_SetPenalty( Client, 3 );
263         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
264 } /* IRC_TRACE */
265
266
267 GLOBAL bool
268 IRC_HELP( CLIENT *Client, REQUEST *Req )
269 {
270         COMMAND *cmd;
271
272         assert( Client != NULL );
273         assert( Req != NULL );
274
275         /* Bad number of arguments? */
276         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
277
278         cmd = Parse_GetCommandStruct( );
279         while( cmd->name )
280         {
281                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
282                 cmd++;
283         }
284         
285         IRC_SetPenalty( Client, 2 );
286         return CONNECTED;
287 } /* IRC_HELP */
288
289
290 static char *
291 Option_String( CONN_ID Idx )
292 {
293         static char option_txt[8];
294         UINT16 options;
295
296         options = Conn_Options(Idx);
297
298         strcpy(option_txt, "F");        /* No idea what this means, but the
299                                          * original ircd sends it ... */
300 #ifdef ZLIB
301         if(options & CONN_ZIP)          /* zlib compression supported. */
302                 strcat(option_txt, "z");
303 #endif
304
305         return option_txt;
306 } /* Option_String */
307
308
309 static bool
310 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
311 {
312         CLIENT *cl, *from;
313         CHANNEL *chan;
314         char *currentTarget = Req->argv[0];
315         char *lastCurrentTarget = NULL;
316
317         assert(Client != NULL);
318         assert(Req != NULL);
319
320         if (Req->argc == 0) {
321                 if (!SendErrors)
322                         return true;
323                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
324                                           Client_ID(Client), Req->command);
325         }
326         if (Req->argc == 1) {
327                 if (!SendErrors)
328                         return true;
329                 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
330                                           Client_ID(Client));
331         }
332         if (Req->argc > 2) {
333                 if (!SendErrors)
334                         return true;
335                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
336                                           Client_ID(Client), Req->command);
337         }
338
339         if (Client_Type(Client) == CLIENT_SERVER)
340                 from = Client_Search(Req->prefix);
341         else
342                 from = Client;
343         if (!from)
344                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
345                                           Client_ID(Client), Req->prefix);
346
347         /* handle msgtarget = msgto *("," msgto) */
348         currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
349
350         while (currentTarget) {
351                 /* Check for and handle valid <msgto> of form:
352                  * RFC 2812 2.3.1:
353                  *   msgto =  channel / ( user [ "%" host ] "@" servername )
354                  *   msgto =/ ( user "%" host ) / targetmask
355                  *   msgto =/ nickname / ( nickname "!" user "@" host )
356                  */
357                 if (strchr(currentTarget, '!') == NULL)
358                         /* nickname */
359                         cl = Client_Search(currentTarget);
360                 else
361                         cl = NULL;
362
363                 if (cl == NULL) {
364                         /* If currentTarget isn't a nickname check for:
365                          * user ["%" host] "@" servername
366                          * user "%" host
367                          * nickname "!" user "@" host
368                          */
369                         char target[COMMAND_LEN];
370                         char * nick = NULL;
371                         char * user = NULL;
372                         char * host = NULL;
373                         char * server = NULL;
374
375                         strlcpy(target, currentTarget, COMMAND_LEN);
376                         server = strchr(target, '@');
377                         if (server) {
378                                 *server = '\0';
379                                 server++;
380                         }
381                         host = strchr(target, '%');
382                         if (host) {
383                                 *host = '\0';
384                                 host++;
385                         }
386                         user = strchr(target, '!');
387                         if (user) {
388                                 /* msgto form: nick!user@host */
389                                 *user = '\0';
390                                 user++;
391                                 nick = target;
392                                 host = server; /* not "@server" but "@host" */
393                         } else {
394                                 user = target;
395                         }
396
397                         for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
398                                 if (Client_Type(cl) != CLIENT_USER)
399                                         continue;
400                                 if (nick != NULL && host != NULL) {
401                                         if (strcmp(nick, Client_ID(cl)) == 0 &&
402                                             strcmp(user, Client_User(cl)) == 0 &&
403                                             strcasecmp(host, Client_Hostname(cl)) == 0)
404                                                 break;
405                                         else
406                                                 continue;
407                                 }
408                                 if (strcasecmp(user, Client_User(cl)) != 0)
409                                         continue;
410                                 if (host != NULL && strcasecmp(host,
411                                                 Client_Hostname(cl)) != 0)
412                                         continue;
413                                 if (server != NULL && strcasecmp(server,
414                                                 Client_ID(Client_Introducer(cl))) != 0)
415                                         continue;
416                                 break;
417                         }
418                 }
419
420                 if (cl) {
421                         /* Target is a user, enforce type */
422                         if (Client_Type(cl) != ForceType) {
423                                 if (!SendErrors)
424                                         return true;
425                                 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
426                                                           Client_ID(from),
427                                                           currentTarget))
428                                         return false;
429                         } else if (SendErrors
430                                    && (Client_Type(Client) != CLIENT_SERVER)
431                                    && strchr(Client_Modes(cl), 'a')) {
432                                 /* Target is away */
433                                 if (!SendErrors)
434                                         return true;
435                                 if (!IRC_WriteStrClient
436                                     (from, RPL_AWAY_MSG, Client_ID(from),
437                                      Client_ID(cl), Client_Away(cl)))
438                                         return DISCONNECTED;
439                         }
440                         if (Client_Conn(from) > NONE) {
441                                 Conn_UpdateIdle(Client_Conn(from));
442                         }
443                         if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
444                                                 Client_ID(cl), Req->argv[1]))
445                                 return false;
446                 } else if (strchr("$#", currentTarget[0])
447                            && strchr(currentTarget, '.')) {
448                         /* targetmask */
449                         if (!Send_Message_Mask(from, currentTarget,
450                                                Req->argv[1], SendErrors))
451                                 return false;
452                 } else if ((chan = Channel_Search(currentTarget))) {
453                         /* channel */
454                         if (!Channel_Write(chan, from, Client, Req->argv[1]))
455                                 return false;
456                 } else {
457                         if (!SendErrors)
458                                 return true;
459                         if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
460                                                 Client_ID(from), currentTarget))
461                                 return false;
462                 }
463
464                 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
465         }
466
467         return CONNECTED;
468 } /* Send_Message */
469
470
471 static bool
472 Send_Message_Mask(CLIENT * from, char * targetMask, char * message, bool SendErrors)
473 {
474         CLIENT *cl;
475         bool client_match;
476         char *mask = targetMask + 1;
477
478         cl = NULL;
479
480         if (strchr(Client_Modes(from), 'o') == NULL) {
481                 if (!SendErrors)
482                         return true;
483                 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
484                                           Client_ID(from));
485         }
486
487         if (targetMask[0] == '#') {
488                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
489                         if (Client_Type(cl) != CLIENT_USER)
490                                 continue;
491                         client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
492                         if (client_match)
493                                 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
494                                                         Client_ID(cl), message))
495                                         return false;
496                 }
497         } else {
498                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
499                         if (Client_Type(cl) != CLIENT_USER)
500                                 continue;
501                         client_match = MatchCaseInsensitive(mask,
502                                         Client_ID(Client_Introducer(cl)));
503                         if (client_match)
504                                 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
505                                                         Client_ID(cl), message))
506                                         return false;
507                 }
508         }
509         return CONNECTED;
510 } /* Send_Message_Mask */
511
512
513 /* -eof- */