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