]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
SECURITY: Fixed a severe bug in handling JOIN commands, which could
[ngircd-alex.git] / src / ngircd / irc-channel.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by 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 channel commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc-channel.c,v 1.40 2007/07/31 18:56:14 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "defines.h"
26 #include "conn.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "lists.h"
30 #include "log.h"
31 #include "match.h"
32 #include "messages.h"
33 #include "parse.h"
34 #include "irc-info.h"
35 #include "irc-write.h"
36 #include "resolve.h"
37 #include "conf.h"
38
39 #include "exp.h"
40 #include "irc-channel.h"
41
42
43 GLOBAL bool
44 IRC_JOIN( CLIENT *Client, REQUEST *Req )
45 {
46         char *channame, *channame_ptr, *key, *key_ptr, *flags, *topic, modes[8];
47         bool is_new_chan, is_invited, is_banned;
48         CLIENT *target;
49         CHANNEL *chan;
50         
51         assert( Client != NULL );
52         assert( Req != NULL );
53
54         /* Bad number of arguments? */
55         if (Req->argc < 1 || Req->argc > 2)
56                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
57                                           Client_ID(Client), Req->command);
58
59         /* Who is the sender? */
60         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
61         else target = Client;
62         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
63
64         /* Are channel keys given? */
65         if (Req->argc > 1) {
66                 key = Req->argv[1];
67                 key_ptr = strchr(key, ',');
68                 if (key_ptr) *key_ptr = '\0';
69         }
70         else
71                 key = key_ptr = NULL;
72
73         channame = Req->argv[0];
74         channame_ptr = strchr(channame, ',');
75         if (channame_ptr) *channame_ptr = '\0';
76
77         /* Channel-Namen durchgehen */
78         while (channame)
79         {
80                 chan = NULL; flags = NULL;
81
82                 /* wird der Channel neu angelegt? */
83                 if( Channel_Search( channame )) {
84                         is_new_chan = false;
85                 } else {
86                         if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
87                                 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
88                                 /* Try next name, if any */
89                                 channame = strchr(channame, ',');
90                                 continue;
91                         }
92                         is_new_chan = true;
93                 }
94
95                 /* Hat ein Server Channel-User-Modes uebergeben? */
96                 if( Client_Type( Client ) == CLIENT_SERVER )
97                 {
98                         /* Channel-Flags extrahieren */
99                         flags = strchr( channame, 0x7 );
100                         if( flags )
101                         {
102                                 *flags = '\0';
103                                 flags++;
104                         }
105                 }
106
107                 /* Local client? */
108                 if( Client_Type( Client ) == CLIENT_USER )
109                 {
110                         /* Test if the user has reached his maximum channel count */
111                         if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
112                                 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
113                                                         Client_ID( Client ), channame );
114
115                         /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
116                         if( is_new_chan )
117                         {
118                                 /* Erster User im Channel: Operator-Flag setzen */
119                                 flags = "o";
120                         }
121                         else
122                         {
123                                 /* Existierenden Channel suchen */
124                                 chan = Channel_Search( channame );
125                                 assert( chan != NULL );
126
127                                 is_banned = Lists_Check(Channel_GetListBans(chan), target );
128                                 is_invited = Lists_Check(Channel_GetListInvites(chan), target );
129
130                                 /* Testen, ob Client gebanned ist */
131                                 if(( is_banned == true) &&  ( is_invited == false ))
132                                 {
133                                         /* Client ist gebanned (und nicht invited): */
134                                         IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
135
136                                         /* Try next name, if any */
137                                         channame = strchr(channame, ',');
138                                         continue;
139                                 }
140
141                                 /* Ist der Channel "invite-only"? */
142                                 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
143                                 {
144                                         /* Channel ist "invite-only" und Client wurde nicht invited: */
145                                         IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
146
147                                         /* Try next name, if any */
148                                         channame = strchr(channame, ',');
149                                         continue;
150                                 }
151
152                                 /* Is the channel protected by a key? */
153                                 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
154                                 {
155                                         /* Bad channel key! */
156                                         IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
157
158                                         /* Try next name, if any */
159                                         channame = strchr(channame, ',');
160                                         continue;
161                                 }
162
163                                 /* Are there already too many members? */
164                                 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
165                                 {
166                                         /* Bad channel key! */
167                                         IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
168
169                                         /* Try next name, if any */
170                                         channame = strchr(channame, ',');
171                                         continue;
172                                 }
173                         }
174                 }
175                 else
176                 {
177                         /* Remote server: we don't need to know whether the
178                          * client is invited or not, but we have to make sure
179                          * that the "one shot" entries (generated by INVITE
180                          * commands) in this list become deleted when a user
181                          * joins a channel this way. */
182                         chan = Channel_Search( channame );
183                         if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
184                 }
185
186                 /* Channel joinen (und ggf. anlegen) */
187                 if( ! Channel_Join( target, channame ))
188                 {
189                         /* naechsten Namen ermitteln */
190                         channame = strchr(channame, ',');
191                         continue;
192                 }
193                 if( ! chan ) chan = Channel_Search( channame );
194                 assert( chan != NULL );
195
196                 /* Modes setzen (wenn vorhanden) */
197                 while( flags && *flags )
198                 {
199                         Channel_UserModeAdd( chan, target, *flags );
200                         flags++;
201                 }
202
203                 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
204                 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
205
206                 /* Muessen Modes an andere Server gemeldet werden? */
207                 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
208                 if( modes[1] ) modes[0] = 0x7;
209                 else modes[0] = '\0';
210
211                 /* An andere Server weiterleiten */
212                 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
213
214                 /* im Channel bekannt machen */
215                 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
216                 if( modes[1] )
217                 {
218                         /* Modes im Channel bekannt machen */
219                         IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
220                 }
221
222                 if( Client_Type( Client ) == CLIENT_USER )
223                 {
224                         /* an Client bestaetigen */
225                         IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
226
227                         /* Send topic to client, if any */
228                         topic = Channel_Topic(chan);
229                         if (*topic) {
230                                 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
231                                         Client_ID(Client), channame, topic);
232 #ifndef STRICT_RFC
233                                 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
234                                         Client_ID(Client), channame,
235                                         Channel_TopicWho(chan),
236                                         Channel_TopicTime(chan));
237 #endif
238                         }
239
240                         /* Mitglieder an Client Melden */
241                         IRC_Send_NAMES( Client, chan );
242                         IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
243                 }
244
245                 /* next channel? */
246                 channame = channame_ptr;
247                 if (channame) {
248                         channame++;
249                         channame_ptr = strchr(channame, ',');
250                         if (channame_ptr) *channame_ptr = '\0';
251
252                         if (key_ptr) {
253                                 key = ++key_ptr;
254                                 key_ptr = strchr(key, ',');
255                                 if (key_ptr) *key_ptr = '\0';
256                         }
257                 }
258         }
259         return CONNECTED;
260 } /* IRC_JOIN */
261
262
263 GLOBAL bool
264 IRC_PART( CLIENT *Client, REQUEST *Req )
265 {
266         CLIENT *target;
267         char *chan;
268
269         assert( Client != NULL );
270         assert( Req != NULL );
271
272         /* Falsche Anzahl Parameter? */
273         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
274
275         /* Wer ist der Absender? */
276         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
277         else target = Client;
278         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
279
280         /* Channel-Namen durchgehen */
281         chan = strtok( Req->argv[0], "," );
282         while( chan )
283         {
284                 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
285                 {
286                         /* naechsten Namen ermitteln */
287                         chan = strtok( NULL, "," );
288                         continue;
289                 }
290
291                 /* naechsten Namen ermitteln */
292                 chan = strtok( NULL, "," );
293         }
294         return CONNECTED;
295 } /* IRC_PART */
296
297
298 GLOBAL bool
299 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
300 {
301         CHANNEL *chan;
302         CLIENT *from;
303         char *topic;
304         bool r;
305
306         assert( Client != NULL );
307         assert( Req != NULL );
308
309         /* Falsche Anzahl Parameter? */
310         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
311
312         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
313         else from = Client;
314         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
315
316         /* Welcher Channel? */
317         chan = Channel_Search( Req->argv[0] );
318         if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
319
320         /* Ist der User Mitglied in dem Channel? */
321         if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
322
323         if( Req->argc == 1 )
324         {
325                 /* Request actual topic */
326                 topic = Channel_Topic(chan);
327                 if (*topic) {
328                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
329                                 Client_ID(Client), Channel_Name(chan), topic);
330 #ifndef STRICT_RFC
331                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
332                                 Client_ID(Client), Channel_Name(chan),
333                                 Channel_TopicWho(chan),
334                                 Channel_TopicTime(chan));
335 #endif
336                         return r;
337                 }
338                 else
339                          return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
340                                         Client_ID(from), Channel_Name(chan));
341         }
342
343         if( strchr( Channel_Modes( chan ), 't' ))
344         {
345                 /* Topic Lock. Ist der User ein Channel Operator? */
346                 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
347         }
348
349         /* Set new topic */
350         Channel_SetTopic(chan, from, Req->argv[1]);
351         Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
352                 Client_Mask(from), Channel_Name(chan),
353                 Req->argv[1][0] ? Req->argv[1] : "<none>");
354
355         /* im Channel bekannt machen und an Server weiterleiten */
356         IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
357         IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
358
359         if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
360         else return CONNECTED;
361 } /* IRC_TOPIC */
362
363
364 /**
365  * Handler for the IRC "LIST" command.
366  * This implementation handles the local case as well as the forwarding of the
367  * LIST command to other servers in the IRC network.
368  */
369 GLOBAL bool
370 IRC_LIST( CLIENT *Client, REQUEST *Req )
371 {
372         char *pattern;
373         CHANNEL *chan;
374         CLIENT *from, *target;
375
376         assert( Client != NULL );
377         assert( Req != NULL );
378
379         /* Bad number of prameters? */
380         if( Req->argc > 2 )
381                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
382                         Client_ID( Client ), Req->command );
383
384         if( Req->argc > 0 )
385                 pattern = strtok( Req->argv[0], "," );
386         else
387                 pattern = "*";
388
389         /* Get sender from prefix, if any */
390         if( Client_Type( Client ) == CLIENT_SERVER )
391                 from = Client_Search( Req->prefix );
392         else
393                 from = Client;
394
395         if( ! from )
396                 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
397                                 Client_ID( Client ), Req->prefix );
398
399         if( Req->argc == 2 )
400         {
401                 /* Forward to other server? */
402                 target = Client_Search( Req->argv[1] );
403                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
404                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
405                                         Client_ID( Client ), Req->argv[1] );
406
407                 if( target != Client_ThisServer( ))
408                 {
409                         /* Target is indeed an other server, forward it! */
410                         return IRC_WriteStrClientPrefix( target, from,
411                                         "LIST %s :%s", Client_ID( from ),
412                                         Req->argv[1] );
413                 }
414         }
415         
416         while( pattern )
417         {
418                 /* Loop through all the channels */
419                 chan = Channel_First( );
420                 while( chan )
421                 {
422                         /* Check search pattern */
423                         if( Match( pattern, Channel_Name( chan )))
424                         {
425                                 /* Gotcha! */
426                                 if( ! strchr( Channel_Modes( chan ), 's' ) ||
427                                     Channel_IsMemberOf( chan, from ))
428                                 {
429                                         if( ! IRC_WriteStrClient( from,
430                                             RPL_LIST_MSG, Client_ID( from ),
431                                             Channel_Name( chan ),
432                                             Channel_MemberCount( chan ),
433                                             Channel_Topic( chan )))
434                                                 return DISCONNECTED;
435                                 }
436                         }
437                         chan = Channel_Next( chan );
438                 }
439                 
440                 /* Get next name ... */
441                 if( Req->argc > 0 )
442                         pattern = strtok( NULL, "," );
443                 else
444                         pattern = NULL;
445         }
446         
447         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
448 } /* IRC_LIST */
449
450
451 GLOBAL bool
452 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
453 {
454         char modes_add[COMMAND_LEN], l[16], *ptr;
455         CLIENT *from;
456         CHANNEL *chan;
457         int arg_topic;
458
459         assert( Client != NULL );
460         assert( Req != NULL );
461
462         /* Bad number of parameters? */
463         if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
464
465         /* Compatibility kludge */
466         if( Req->argc == 5 ) arg_topic = 4;
467         else if( Req->argc == 3 ) arg_topic = 2;
468         else arg_topic = -1;
469
470         /* Search origin */
471         from = Client_Search( Req->prefix );
472         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
473
474         /* Search or create channel */
475         chan = Channel_Search( Req->argv[0] );
476         if( ! chan ) chan = Channel_Create( Req->argv[0] );
477         if( ! chan ) return CONNECTED;
478
479         if( Req->argv[1][0] == '+' )
480         {
481                 ptr = Channel_Modes( chan );
482                 if( ! *ptr )
483                 {
484                         /* OK, this channel doesn't have modes jet, set the received ones: */
485                         Channel_SetModes( chan, &Req->argv[1][1] );
486
487                         if( Req->argc == 5 )
488                         {
489                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
490                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
491                         }
492                         else
493                         {
494                                 /* Delete modes which we never want to inherit */
495                                 Channel_ModeDel( chan, 'l' );
496                                 Channel_ModeDel( chan, 'k' );
497                         }
498
499                         strcpy( modes_add, "" );
500                         ptr = Channel_Modes( chan );
501                         while( *ptr )
502                         {
503                                 if( *ptr == 'l' )
504                                 {
505                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
506                                         strlcat( modes_add, l, sizeof( modes_add ));
507                                 }
508                                 if( *ptr == 'k' )
509                                 {
510                                         strlcat( modes_add, " ", sizeof( modes_add ));
511                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
512                                 }
513                                 ptr++;
514                         }
515                         
516                         /* Inform members of this channel */
517                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
518                 }
519         }
520         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
521
522         if( arg_topic > 0 )
523         {
524                 /* We got a topic */
525                 ptr = Channel_Topic( chan );
526                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
527                 {
528                         /* OK, there is no topic jet */
529                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
530                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
531                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
532                 }
533         }
534
535         /* Forward CHANINFO to other serevrs */
536         if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
537         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
538         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
539
540         return CONNECTED;
541 } /* IRC_CHANINFO */
542
543
544 /* -eof- */