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