1 /*
2 * IRC - Internet Relay Chat, ircd/channel.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Co Center
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 1, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /** @file
21 * @brief Channel management and maintenance
22 * @version $Id$
23 */
24 #include "config.h"
25
26 #include "channel.h"
27 #include "client.h"
28 #include "destruct_event.h"
29 #include "hash.h"
30 #include "ircd.h"
31 #include "ircd_alloc.h"
32 #include "ircd_chattr.h"
33 #include "ircd_defs.h"
34 #include "ircd_features.h"
35 #include "ircd_log.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
39 #include "list.h"
40 #include "match.h"
41 #include "msg.h"
42 #include "msgq.h"
43 #include "numeric.h"
44 #include "numnicks.h"
45 #include "querycmds.h"
46 #include "s_bsd.h"
47 #include "s_conf.h"
48 #include "s_debug.h"
49 #include "s_misc.h"
50 #include "s_user.h"
51 #include "send.h"
52 #include "struct.h"
53 #include "sys.h"
54 #include "whowas.h"
55
56 /* #include <assert.h> -- Now using assert in ircd_log.h */
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61 /** Linked list containing the full list of all channels */
62 struct Channel* GlobalChannelList = 0;
63
64 /** Number of struct Membership*'s allocated */
65 static unsigned int membershipAllocCount;
66 /** Freelist for struct Membership*'s */
67 static struct Membership* membershipFreeList;
68 /** Freelist for struct Ban*'s */
69 static struct Ban* free_bans;
70 /** Number of ban structures allocated. */
71 static size_t bans_alloc;
72 /** Number of ban structures in use. */
73 static size_t bans_inuse;
74
75 #if !defined(NDEBUG)
76 /** return the length (>=0) of a chain of links.
77 * @param lp pointer to the start of the linked list
78 * @return the number of items in the list
79 */
list_length(struct SLink * lp)80 static int list_length(struct SLink *lp)
81 {
82 int count = 0;
83
84 for (; lp; lp = lp->next)
85 ++count;
86 return count;
87 }
88 #endif
89
90 /** Set the mask for a ban, checking for IP masks.
91 * @param[in,out] ban Ban structure to modify.
92 * @param[in] banstr Mask to ban.
93 */
94 static void
set_ban_mask(struct Ban * ban,const char * banstr)95 set_ban_mask(struct Ban *ban, const char *banstr)
96 {
97 char *sep;
98 assert(banstr != NULL);
99 ircd_strncpy(ban->banstr, banstr, sizeof(ban->banstr) - 1);
100 sep = strrchr(banstr, '@');
101 if (sep) {
102 ban->nu_len = sep - banstr;
103 if (ipmask_parse(sep + 1, &ban->address, &ban->addrbits))
104 ban->flags |= BAN_IPMASK;
105 }
106 }
107
108 /** Allocate a new Ban structure.
109 * @param[in] banstr Ban mask to use.
110 * @return Newly allocated ban.
111 */
112 struct Ban *
make_ban(const char * banstr)113 make_ban(const char *banstr)
114 {
115 struct Ban *ban;
116 if (free_bans) {
117 ban = free_bans;
118 free_bans = free_bans->next;
119 }
120 else if (!(ban = MyMalloc(sizeof(*ban))))
121 return NULL;
122 else
123 bans_alloc++;
124 bans_inuse++;
125 memset(ban, 0, sizeof(*ban));
126 set_ban_mask(ban, banstr);
127 return ban;
128 }
129
130 /** Deallocate a ban structure.
131 * @param[in] ban Ban to deallocate.
132 */
133 void
free_ban(struct Ban * ban)134 free_ban(struct Ban *ban)
135 {
136 ban->next = free_bans;
137 free_bans = ban;
138 bans_inuse--;
139 }
140
141 /** Report ban usage to \a cptr.
142 * @param[in] cptr Client requesting information.
143 */
bans_send_meminfo(struct Client * cptr)144 void bans_send_meminfo(struct Client *cptr)
145 {
146 struct Ban *ban;
147 size_t num_free;
148 for (num_free = 0, ban = free_bans; ban; ban = ban->next)
149 num_free++;
150 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Bans: inuse %zu(%zu) free %zu alloc %zu",
151 bans_inuse, bans_inuse * sizeof(*ban), num_free, bans_alloc);
152 }
153
154 /** return the struct Membership* that represents a client on a channel
155 * This function finds a struct Membership* which holds the state about
156 * a client on a specific channel. The code is smart enough to iterate
157 * over the channels a user is in, or the users in a channel to find the
158 * user depending on which is likely to be more efficient.
159 *
160 * @param chptr pointer to the channel struct
161 * @param cptr pointer to the client struct
162 *
163 * @returns pointer to the struct Membership representing this client on
164 * this channel. Returns NULL if the client is not on the channel.
165 * Returns NULL if the client is actually a server.
166 * @see find_channel_member()
167 */
find_member_link(struct Channel * chptr,const struct Client * cptr)168 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
169 {
170 struct Membership *m;
171 assert(0 != cptr);
172 assert(0 != chptr);
173
174 /* Servers don't have member links */
175 if (IsServer(cptr)||IsMe(cptr))
176 return 0;
177
178 /* +k users are typically on a LOT of channels. So we iterate over who
179 * is in the channel. X/W are +k and are in about 5800 channels each.
180 * however there are typically no more than 1000 people in a channel
181 * at a time.
182 */
183 if (IsChannelService(cptr)) {
184 m = chptr->members;
185 while (m) {
186 assert(m->channel == chptr);
187 if (m->user == cptr)
188 return m;
189 m = m->next_member;
190 }
191 }
192 /* Users on the other hand aren't allowed on more than 15 channels. 50%
193 * of users that are on channels are on 2 or less, 95% are on 7 or less,
194 * and 99% are on 10 or less.
195 */
196 else {
197 m = (cli_user(cptr))->channel;
198 while (m) {
199 assert(m->user == cptr);
200 if (m->channel == chptr)
201 return m;
202 m = m->next_channel;
203 }
204 }
205 return 0;
206 }
207
208 /** Find the client structure for a nick name (user)
209 * Find the client structure for a nick name (user)
210 * using history mechanism if necessary. If the client is not found, an error
211 * message (NO SUCH NICK) is generated. If the client was found
212 * through the history, chasing will be 1 and otherwise 0.
213 *
214 * This function was used extensively in the P09 days, and since we now have
215 * numeric nicks is no longer quite as important.
216 *
217 * @param sptr Pointer to the client that has requested the search
218 * @param user a string representing the client to be found
219 * @param chasing a variable set to 0 if the user was found directly,
220 * 1 otherwise
221 * @returns a pointer the client, or NULL if the client wasn't found.
222 */
find_chasing(struct Client * sptr,const char * user,int * chasing)223 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
224 {
225 struct Client* who = FindClient(user);
226
227 if (chasing)
228 *chasing = 0;
229 if (who)
230 return who;
231
232 if (!(who = get_history(user))) {
233 send_reply(sptr, ERR_NOSUCHNICK, user);
234 return 0;
235 }
236 if (chasing)
237 *chasing = 1;
238 return who;
239 }
240
241 /** Decrement the count of users, and free if empty.
242 * Subtract one user from channel i (and free channel * block, if channel
243 * became empty).
244 *
245 * @param chptr The channel to subtract one from.
246 *
247 * @returns true (1) if channel still has members.
248 * false (0) if the channel is now empty.
249 */
sub1_from_channel(struct Channel * chptr)250 int sub1_from_channel(struct Channel* chptr)
251 {
252 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
253 {
254 assert(0 != chptr->members);
255 --chptr->users;
256 return 1;
257 }
258
259 chptr->users = 0;
260
261 /*
262 * Also channels without Apass set need to be kept alive,
263 * otherwise Bad Guys(tm) would be able to takeover
264 * existing channels too easily, and then set an Apass!
265 * However, if a channel without Apass becomes empty
266 * then we try to be kind to them and remove possible
267 * limiting modes.
268 */
269 chptr->mode.mode &= ~MODE_INVITEONLY;
270 chptr->mode.limit = 0;
271 /*
272 * We do NOT reset a possible key or bans because when
273 * the 'channel owners' can't get in because of a key
274 * or ban then apparently there was a fight/takeover
275 * on the channel and we want them to contact IRC opers
276 * who then will educate them on the use of Apass/Upass.
277 */
278 if (!chptr->mode.apass[0]) /* If no Apass, reset all modes. */
279 {
280 struct Ban *link, *next;
281 chptr->mode.mode = 0;
282 *chptr->mode.key = '\0';
283 while (chptr->invites)
284 del_invite(chptr->invites->value.cptr, chptr);
285 for (link = chptr->banlist; link; link = next) {
286 next = link->next;
287 free_ban(link);
288 }
289 chptr->banlist = NULL;
290
291 /* Immediately destruct empty -A channels if not using apass. */
292 if (!feature_bool(FEAT_OPLEVELS))
293 {
294 destruct_channel(chptr);
295 return 0;
296 }
297 }
298 if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
299 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
300 else
301 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
302
303 return 0;
304 }
305
306 /** Destroy an empty channel
307 * This function destroys an empty channel, removing it from hashtables,
308 * and removing any resources it may have consumed.
309 *
310 * @param chptr The channel to destroy
311 *
312 * @returns 0 (success)
313 *
314 * FIXME: Change to return void, this function never fails.
315 */
destruct_channel(struct Channel * chptr)316 int destruct_channel(struct Channel* chptr)
317 {
318 struct Ban *ban, *next;
319
320 assert(0 == chptr->members);
321
322 /*
323 * Now, find all invite links from channel structure
324 */
325 while (chptr->invites)
326 del_invite(chptr->invites->value.cptr, chptr);
327
328 for (ban = chptr->banlist; ban; ban = next)
329 {
330 next = ban->next;
331 free_ban(ban);
332 }
333 if (chptr->prev)
334 chptr->prev->next = chptr->next;
335 else
336 GlobalChannelList = chptr->next;
337 if (chptr->next)
338 chptr->next->prev = chptr->prev;
339 hRemChannel(chptr);
340 --UserStats.channels;
341 /*
342 * make sure that channel actually got removed from hash table
343 */
344 assert(chptr->hnext == chptr);
345 MyFree(chptr);
346 return 0;
347 }
348
349 /** returns Membership * if a person is joined and not a zombie
350 * @param cptr Client
351 * @param chptr Channel
352 * @returns pointer to the client's struct Membership * on the channel if that
353 * user is a full member of the channel, or NULL otherwise.
354 *
355 * @see find_member_link()
356 */
find_channel_member(struct Client * cptr,struct Channel * chptr)357 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
358 {
359 struct Membership* member;
360 assert(0 != chptr);
361
362 member = find_member_link(chptr, cptr);
363 return (member && !IsZombie(member)) ? member : 0;
364 }
365
366 /** Searches for a ban from a ban list that matches a user.
367 * @param[in] cptr The client to test.
368 * @param[in] banlist The list of bans to test.
369 * @return Pointer to a matching ban, or NULL if none exit.
370 */
find_ban(struct Client * cptr,struct Ban * banlist)371 struct Ban *find_ban(struct Client *cptr, struct Ban *banlist)
372 {
373 char nu[NICKLEN + USERLEN + 2];
374 char tmphost[HOSTLEN + 1];
375 char iphost[SOCKIPLEN + 1];
376 char *hostmask;
377 char *sr;
378 struct Ban *found;
379
380 /* Build nick!user and alternate host names. */
381 ircd_snprintf(0, nu, sizeof(nu), "%s!%s",
382 cli_name(cptr), cli_user(cptr)->username);
383 ircd_ntoa_r(iphost, &cli_ip(cptr));
384 if (!IsAccount(cptr))
385 sr = NULL;
386 else if (HasHiddenHost(cptr))
387 sr = cli_user(cptr)->realhost;
388 else
389 {
390 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
391 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
392 sr = tmphost;
393 }
394
395 /* Walk through ban list. */
396 for (found = NULL; banlist; banlist = banlist->next) {
397 int res;
398 /* If we have found a positive ban already, only consider exceptions. */
399 if (found && !(banlist->flags & BAN_EXCEPTION))
400 continue;
401 /* Compare nick!user portion of ban. */
402 banlist->banstr[banlist->nu_len] = '\0';
403 res = match(banlist->banstr, nu);
404 banlist->banstr[banlist->nu_len] = '@';
405 if (res)
406 continue;
407 /* Compare host portion of ban. */
408 hostmask = banlist->banstr + banlist->nu_len + 1;
409 if (!((banlist->flags & BAN_IPMASK)
410 && ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits))
411 && match(hostmask, cli_user(cptr)->host)
412 && match(hostmask, iphost)
413 && !(sr && !match(hostmask, sr)))
414 continue;
415 /* If an exception matches, no ban can match. */
416 if (banlist->flags & BAN_EXCEPTION)
417 return NULL;
418 /* Otherwise, remember this ban but keep searching for an exception. */
419 found = banlist;
420 }
421 return found;
422 }
423
424 /**
425 * This function returns true if the user is banned on the said channel.
426 * This function will check the ban cache if applicable, otherwise will
427 * do the comparisons and cache the result.
428 *
429 * @param[in] member The Membership to test for banned-ness.
430 * @return Non-zero if the member is banned, zero if not.
431 */
is_banned(struct Membership * member)432 static int is_banned(struct Membership* member)
433 {
434 if (IsBanValid(member))
435 return IsBanned(member);
436
437 SetBanValid(member);
438 if (find_ban(member->user, member->channel->banlist)) {
439 SetBanned(member);
440 return 1;
441 } else {
442 ClearBanned(member);
443 return 0;
444 }
445 }
446
447 /** add a user to a channel.
448 * adds a user to a channel by adding another link to the channels member
449 * chain.
450 *
451 * @param chptr The channel to add to.
452 * @param who The user to add.
453 * @param flags The flags the user gets initially.
454 * @param oplevel The oplevel the user starts with.
455 */
add_user_to_channel(struct Channel * chptr,struct Client * who,unsigned int flags,int oplevel)456 void add_user_to_channel(struct Channel* chptr, struct Client* who,
457 unsigned int flags, int oplevel)
458 {
459 assert(0 != chptr);
460 assert(0 != who);
461
462 if (cli_user(who)) {
463
464 struct Membership* member = membershipFreeList;
465 if (member)
466 membershipFreeList = member->next_member;
467 else {
468 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
469 ++membershipAllocCount;
470 }
471
472 assert(0 != member);
473 member->user = who;
474 member->channel = chptr;
475 member->status = flags;
476 SetOpLevel(member, oplevel);
477
478 member->next_member = chptr->members;
479 if (member->next_member)
480 member->next_member->prev_member = member;
481 member->prev_member = 0;
482 chptr->members = member;
483
484 member->next_channel = (cli_user(who))->channel;
485 if (member->next_channel)
486 member->next_channel->prev_channel = member;
487 member->prev_channel = 0;
488 (cli_user(who))->channel = member;
489
490 if (chptr->destruct_event)
491 remove_destruct_event(chptr);
492 ++chptr->users;
493 ++((cli_user(who))->joined);
494 }
495 }
496
497 /** Remove a person from a channel, given their Membership*
498 *
499 * @param member A member of a channel.
500 *
501 * @returns true if there are more people in the channel.
502 */
remove_member_from_channel(struct Membership * member)503 static int remove_member_from_channel(struct Membership* member)
504 {
505 struct Channel* chptr;
506 assert(0 != member);
507 chptr = member->channel;
508 /*
509 * unlink channel member list
510 */
511 if (member->next_member)
512 member->next_member->prev_member = member->prev_member;
513 if (member->prev_member)
514 member->prev_member->next_member = member->next_member;
515 else
516 member->channel->members = member->next_member;
517
518 /*
519 * If this is the last delayed-join user, may have to clear WASDELJOINS.
520 */
521 if (IsDelayedJoin(member))
522 CheckDelayedJoins(chptr);
523
524 /*
525 * unlink client channel list
526 */
527 if (member->next_channel)
528 member->next_channel->prev_channel = member->prev_channel;
529 if (member->prev_channel)
530 member->prev_channel->next_channel = member->next_channel;
531 else
532 (cli_user(member->user))->channel = member->next_channel;
533
534 --(cli_user(member->user))->joined;
535
536 member->next_member = membershipFreeList;
537 membershipFreeList = member;
538
539 return sub1_from_channel(chptr);
540 }
541
542 /** Check if all the remaining members on the channel are zombies
543 *
544 * @returns False if the channel has any non zombie members, True otherwise.
545 * @see \ref zombie
546 */
channel_all_zombies(struct Channel * chptr)547 static int channel_all_zombies(struct Channel* chptr)
548 {
549 struct Membership* member;
550
551 for (member = chptr->members; member; member = member->next_member) {
552 if (!IsZombie(member))
553 return 0;
554 }
555 return 1;
556 }
557
558
559 /** Remove a user from a channel
560 * This is the generic entry point for removing a user from a channel, this
561 * function will remove the client from the channel, and destroy the channel
562 * if there are no more normal users left.
563 *
564 * @param cptr The client
565 * @param chptr The channel
566 */
remove_user_from_channel(struct Client * cptr,struct Channel * chptr)567 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
568 {
569
570 struct Membership* member;
571 assert(0 != chptr);
572
573 if ((member = find_member_link(chptr, cptr))) {
574 if (remove_member_from_channel(member)) {
575 if (channel_all_zombies(chptr)) {
576 /*
577 * XXX - this looks dangerous but isn't if we got the referential
578 * integrity right for channels
579 */
580 while (remove_member_from_channel(chptr->members))
581 ;
582 }
583 }
584 }
585 }
586
587 /** Remove a user from all channels they are on.
588 *
589 * This function removes a user from all channels they are on.
590 *
591 * @param cptr The client to remove.
592 */
remove_user_from_all_channels(struct Client * cptr)593 void remove_user_from_all_channels(struct Client* cptr)
594 {
595 struct Membership* chan;
596 assert(0 != cptr);
597 assert(0 != cli_user(cptr));
598
599 while ((chan = (cli_user(cptr))->channel))
600 remove_user_from_channel(cptr, chan->channel);
601 }
602
603 /** Check if this user is a legitimate chanop
604 *
605 * @param cptr Client to check
606 * @param chptr Channel to check
607 *
608 * @returns True if the user is a chanop (And not a zombie), False otherwise.
609 * @see \ref zombie
610 */
is_chan_op(struct Client * cptr,struct Channel * chptr)611 int is_chan_op(struct Client *cptr, struct Channel *chptr)
612 {
613 struct Membership* member;
614 assert(chptr);
615 if ((member = find_member_link(chptr, cptr)))
616 return (!IsZombie(member) && IsChanOp(member));
617
618 return 0;
619 }
620
621 /** Check if a user is a Zombie on a specific channel.
622 *
623 * @param cptr The client to check.
624 * @param chptr The channel to check.
625 *
626 * @returns True if the client (cptr) is a zombie on the channel (chptr),
627 * False otherwise.
628 *
629 * @see \ref zombie
630 */
is_zombie(struct Client * cptr,struct Channel * chptr)631 int is_zombie(struct Client *cptr, struct Channel *chptr)
632 {
633 struct Membership* member;
634
635 assert(0 != chptr);
636
637 if ((member = find_member_link(chptr, cptr)))
638 return IsZombie(member);
639 return 0;
640 }
641
642 /** Returns if a user has voice on a channel.
643 *
644 * @param cptr The client
645 * @param chptr The channel
646 *
647 * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie.
648 * @see \ref zombie
649 */
has_voice(struct Client * cptr,struct Channel * chptr)650 int has_voice(struct Client* cptr, struct Channel* chptr)
651 {
652 struct Membership* member;
653
654 assert(0 != chptr);
655 if ((member = find_member_link(chptr, cptr)))
656 return (!IsZombie(member) && HasVoice(member));
657
658 return 0;
659 }
660
661 /** Can this member send to a channel
662 *
663 * A user can speak on a channel iff:
664 * <ol>
665 * <li> They didn't use the Apass to gain ops.
666 * <li> They are op'd or voice'd.
667 * <li> You aren't banned.
668 * <li> The channel isn't +m
669 * <li> The channel isn't +n or you are on the channel.
670 * </ol>
671 *
672 * This function will optionally reveal a user on a delayed join channel if
673 * they are allowed to send to the channel.
674 *
675 * @param member The membership of the user
676 * @param reveal If true, the user will be "revealed" on a delayed
677 * joined channel.
678 *
679 * @returns True if the client can speak on the channel.
680 */
member_can_send_to_channel(struct Membership * member,int reveal)681 int member_can_send_to_channel(struct Membership* member, int reveal)
682 {
683 assert(0 != member);
684
685 /* Do not check for users on other servers: This should be a
686 * temporary desynch, or maybe they are on an older server, but
687 * we do not want to send ERR_CANNOTSENDTOCHAN more than once.
688 */
689 if (!MyUser(member->user))
690 {
691 if (IsDelayedJoin(member) && reveal)
692 RevealDelayedJoin(member);
693 return 1;
694 }
695
696 /* Discourage using the Apass to get op. They should use the Upass. */
697 if (IsChannelManager(member) && member->channel->mode.apass[0])
698 return 0;
699
700 /* If you have voice or ops, you can speak. */
701 if (IsVoicedOrOpped(member))
702 return 1;
703
704 /*
705 * If it's moderated, and you aren't a privileged user, you can't
706 * speak.
707 */
708 if (member->channel->mode.mode & MODE_MODERATED)
709 return 0;
710
711 /* If only logged in users may join and you're not one, you can't speak. */
712 if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
713 return 0;
714
715 /* If you're banned then you can't speak either. */
716 if (is_banned(member))
717 return 0;
718
719 if (IsDelayedJoin(member) && reveal)
720 RevealDelayedJoin(member);
721
722 return 1;
723 }
724
725 /** Check if a client can send to a channel.
726 *
727 * Has the added check over member_can_send_to_channel() of servers can
728 * always speak.
729 *
730 * @param cptr The client to check
731 * @param chptr The channel to check
732 * @param reveal If the user should be revealed (see
733 * member_can_send_to_channel())
734 *
735 * @returns true if the client is allowed to speak on the channel, false
736 * otherwise
737 *
738 * @see member_can_send_to_channel()
739 */
client_can_send_to_channel(struct Client * cptr,struct Channel * chptr,int reveal)740 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
741 {
742 struct Membership *member;
743 assert(0 != cptr);
744 /*
745 * Servers can always speak on channels.
746 */
747 if (IsServer(cptr))
748 return 1;
749
750 member = find_channel_member(cptr, chptr);
751
752 /*
753 * You can't speak if you're off channel, and it is +n (no external messages)
754 * or +m (moderated).
755 */
756 if (!member) {
757 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
758 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
759 return 0;
760 else
761 return !find_ban(cptr, chptr->banlist);
762 }
763 return member_can_send_to_channel(member, reveal);
764 }
765
766 /** Returns the name of a channel that prevents the user from changing nick.
767 * if a member and not (opped or voiced) and (banned or moderated), return
768 * the name of the first channel banned on.
769 *
770 * @param cptr The client
771 *
772 * @returns the name of the first channel banned on, or NULL if the user
773 * can change nicks.
774 */
find_no_nickchange_channel(struct Client * cptr)775 const char* find_no_nickchange_channel(struct Client* cptr)
776 {
777 if (MyUser(cptr)) {
778 struct Membership* member;
779 for (member = (cli_user(cptr))->channel; member;
780 member = member->next_channel) {
781 if (IsVoicedOrOpped(member))
782 continue;
783 if ((member->channel->mode.mode & MODE_MODERATED)
784 || (member->channel->mode.mode & MODE_REGONLY && !IsAccount(cptr))
785 || is_banned(member))
786 return member->channel->chname;
787 }
788 }
789 return 0;
790 }
791
792
793 /** Fill mbuf/pbuf with modes from chptr
794 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
795 * with the parameters in pbuf as visible by cptr.
796 *
797 * This function will hide keys from non-op'd, non-server clients.
798 *
799 * @param cptr The client to generate the mode for.
800 * @param mbuf The buffer to write the modes into.
801 * @param pbuf The buffer to write the mode parameters into.
802 * @param buflen The length of the buffers.
803 * @param chptr The channel to get the modes from.
804 * @param member The membership of this client on this channel (or NULL
805 * if this client isn't on this channel)
806 *
807 */
channel_modes(struct Client * cptr,char * mbuf,char * pbuf,int buflen,struct Channel * chptr,struct Membership * member)808 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
809 struct Channel *chptr, struct Membership *member)
810 {
811 int previous_parameter = 0;
812
813 assert(0 != mbuf);
814 assert(0 != pbuf);
815 assert(0 != chptr);
816
817 *mbuf++ = '+';
818 if (chptr->mode.mode & MODE_SECRET)
819 *mbuf++ = 's';
820 else if (chptr->mode.mode & MODE_PRIVATE)
821 *mbuf++ = 'p';
822 if (chptr->mode.mode & MODE_MODERATED)
823 *mbuf++ = 'm';
824 if (chptr->mode.mode & MODE_TOPICLIMIT)
825 *mbuf++ = 't';
826 if (chptr->mode.mode & MODE_INVITEONLY)
827 *mbuf++ = 'i';
828 if (chptr->mode.mode & MODE_NOPRIVMSGS)
829 *mbuf++ = 'n';
830 if (chptr->mode.mode & MODE_REGONLY)
831 *mbuf++ = 'r';
832 if (chptr->mode.mode & MODE_DELJOINS)
833 *mbuf++ = 'D';
834 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
835 *mbuf++ = 'd';
836 if (chptr->mode.mode & MODE_REGISTERED)
837 *mbuf++ = 'R';
838 if (chptr->mode.mode & MODE_NOCOLOR)
839 *mbuf++ = 'c';
840 if (chptr->mode.mode & MODE_NOCTCP)
841 *mbuf++ = 'C';
842 if (chptr->mode.limit) {
843 *mbuf++ = 'l';
844 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
845 previous_parameter = 1;
846 }
847
848 if (*chptr->mode.key) {
849 *mbuf++ = 'k';
850 if (previous_parameter)
851 strcat(pbuf, " ");
852 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
853 strcat(pbuf, chptr->mode.key);
854 } else
855 strcat(pbuf, "*");
856 previous_parameter = 1;
857 }
858 if (*chptr->mode.apass) {
859 *mbuf++ = 'A';
860 if (previous_parameter)
861 strcat(pbuf, " ");
862 if (IsServer(cptr)) {
863 strcat(pbuf, chptr->mode.apass);
864 } else
865 strcat(pbuf, "*");
866 previous_parameter = 1;
867 }
868 if (*chptr->mode.upass) {
869 *mbuf++ = 'U';
870 if (previous_parameter)
871 strcat(pbuf, " ");
872 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
873 strcat(pbuf, chptr->mode.upass);
874 } else
875 strcat(pbuf, "*");
876 }
877 *mbuf = '\0';
878 }
879
880 /** Compare two members oplevel
881 *
882 * @param mp1 Pointer to a pointer to a membership
883 * @param mp2 Pointer to a pointer to a membership
884 *
885 * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise.
886 *
887 * Used for qsort(3).
888 */
compare_member_oplevel(const void * mp1,const void * mp2)889 int compare_member_oplevel(const void *mp1, const void *mp2)
890 {
891 struct Membership const* member1 = *(struct Membership const**)mp1;
892 struct Membership const* member2 = *(struct Membership const**)mp2;
893 if (member1->oplevel == member2->oplevel)
894 return 0;
895 return (member1->oplevel < member2->oplevel) ? -1 : 1;
896 }
897
898 /* send "cptr" a full list of the modes for channel chptr.
899 *
900 * Sends a BURST line to cptr, bursting all the modes for the channel.
901 *
902 * @param cptr Client pointer
903 * @param chptr Channel pointer
904 */
send_channel_modes(struct Client * cptr,struct Channel * chptr)905 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
906 {
907 /* The order in which modes are generated is now mandatory */
908 static unsigned int current_flags[4] =
909 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
910 int first = 1;
911 int full = 1;
912 int flag_cnt = 0;
913 int new_mode = 0;
914 size_t len;
915 struct Membership* member;
916 struct Ban* lp2;
917 char modebuf[MODEBUFLEN];
918 char parabuf[MODEBUFLEN];
919 struct MsgBuf *mb;
920 int number_of_ops = 0;
921 int opped_members_index = 0;
922 struct Membership** opped_members = NULL;
923 int last_oplevel = 0;
924 int send_oplevels = 0;
925
926 assert(0 != cptr);
927 assert(0 != chptr);
928
929 if (IsLocalChannel(chptr->chname))
930 return;
931
932 member = chptr->members;
933 lp2 = chptr->banlist;
934
935 *modebuf = *parabuf = '\0';
936 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
937
938 for (first = 1; full; first = 0) /* Loop for multiple messages */
939 {
940 full = 0; /* Assume by default we get it
941 all in one message */
942
943 /* (Continued) prefix: "<Y> B <channel> <TS>" */
944 /* is there any better way we can do this? */
945 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
946 chptr->creationtime);
947
948 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
949 if first message */
950 {
951 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
952 msgq_append(&me, mb, " %s", modebuf);
953
954 if (*parabuf)
955 msgq_append(&me, mb, " %s", parabuf);
956 }
957
958 /*
959 * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
960 *
961 * First find all opless members.
962 * Run 2 times over all members, to group the members with
963 * and without voice together.
964 * Then run 2 times over all opped members (which are ordered
965 * by op-level) to also group voice and non-voice together.
966 */
967 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
968 {
969 while (member)
970 {
971 if (flag_cnt < 2 && IsChanOp(member))
972 {
973 /*
974 * The first loop (to find all non-voice/op), we count the ops.
975 * The second loop (to find all voiced non-ops), store the ops
976 * in a dynamic array.
977 */
978 if (flag_cnt == 0)
979 ++number_of_ops;
980 else
981 opped_members[opped_members_index++] = member;
982 /* We also send oplevels if anyone is below the weakest level. */
983 if (OpLevel(member) < MAXOPLEVEL)
984 send_oplevels = 1;
985 }
986 /* Only handle the members with the flags that we are interested in. */
987 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
988 {
989 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
990 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
991 {
992 full = 1; /* Make sure we continue after
993 sending it so far */
994 /* Ensure the new BURST line contains the current
995 * ":mode", except when there is no mode yet. */
996 new_mode = (flag_cnt > 0) ? 1 : 0;
997 break; /* Do not add this member to this message */
998 }
999 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
1000 first = 0; /* From now on, use commas to add new nicks */
1001
1002 /*
1003 * Do we have a nick with a new mode ?
1004 * Or are we starting a new BURST line?
1005 */
1006 if (new_mode)
1007 {
1008 /*
1009 * This means we are at the _first_ member that has only
1010 * voice, or the first member that has only ops, or the
1011 * first member that has voice and ops (so we get here
1012 * at most three times, plus once for every start of
1013 * a continued BURST line where only these modes is current.
1014 * In the two cases where the current mode includes ops,
1015 * we need to add the _absolute_ value of the oplevel to the mode.
1016 */
1017 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
1018 int loc = 1;
1019
1020 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
1021 tbuf[loc++] = 'v';
1022 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
1023 {
1024 /* append the absolute value of the oplevel */
1025 if (send_oplevels)
1026 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel);
1027 else
1028 tbuf[loc++] = 'o';
1029 }
1030 tbuf[loc] = '\0';
1031 msgq_append(&me, mb, tbuf);
1032 new_mode = 0;
1033 }
1034 else if (send_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
1035 {
1036 /*
1037 * This can't be the first member of a (continued) BURST
1038 * message because then either flag_cnt == 0 or new_mode == 1
1039 * Now we need to append the incremental value of the oplevel.
1040 */
1041 char tbuf[2 + MAXOPLEVELDIGITS];
1042 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
1043 last_oplevel = member->oplevel;
1044 msgq_append(&me, mb, tbuf);
1045 }
1046 }
1047 /* Go to the next `member'. */
1048 if (flag_cnt < 2)
1049 member = member->next_member;
1050 else
1051 member = opped_members[++opped_members_index];
1052 }
1053 if (full)
1054 break;
1055
1056 /* Point `member' at the start of the list again. */
1057 if (flag_cnt == 0)
1058 {
1059 member = chptr->members;
1060 /* Now, after one loop, we know the number of ops and can
1061 * allocate the dynamic array with pointer to the ops. */
1062 opped_members = (struct Membership**)
1063 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
1064 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
1065 }
1066 else
1067 {
1068 /* At the end of the second loop, sort the opped members with
1069 * increasing op-level, so that we will output them in the
1070 * correct order (and all op-level increments stay positive) */
1071 if (flag_cnt == 1)
1072 qsort(opped_members, number_of_ops,
1073 sizeof(struct Membership*), compare_member_oplevel);
1074 /* The third and fourth loop run only over the opped members. */
1075 member = opped_members[(opped_members_index = 0)];
1076 }
1077
1078 } /* loop over 0,+v,+o,+ov */
1079
1080 if (!full)
1081 {
1082 /* Attach all bans, space separated " :%ban ban ..." */
1083 for (first = 2; lp2; lp2 = lp2->next)
1084 {
1085 len = strlen(lp2->banstr);
1086 if (msgq_bufleft(mb) < len + 1 + first)
1087 /* The +1 stands for the added ' '.
1088 * The +first stands for the added ":%".
1089 */
1090 {
1091 full = 1;
1092 break;
1093 }
1094 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1095 lp2->banstr);
1096 first = 0;
1097 }
1098 }
1099
1100 send_buffer(cptr, mb, 0); /* Send this message */
1101 msgq_clean(mb);
1102 } /* Continue when there was something
1103 that didn't fit (full==1) */
1104 if (opped_members)
1105 MyFree(opped_members);
1106 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1107 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1108 chptr->creationtime, chptr->topic_time, chptr->topic);
1109 }
1110
1111 /** Canonify a mask.
1112 * pretty_mask
1113 *
1114 * @author Carlo Wood (Run),
1115 * 05 Oct 1998.
1116 *
1117 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1118 * When the user name or host name are too long (USERLEN and HOSTLEN
1119 * respectively) then they are cut off at the start with a '*'.
1120 *
1121 * The following transformations are made:
1122 *
1123 * 1) xxx -> nick!*@*
1124 * 2) xxx.xxx -> *!*\@host
1125 * 3) xxx\!yyy -> nick!user\@*
1126 * 4) xxx\@yyy -> *!user\@host
1127 * 5) xxx!yyy\@zzz -> nick!user\@host
1128 *
1129 * @param mask The uncanonified mask.
1130 * @returns The updated mask in a static buffer.
1131 */
pretty_mask(char * mask)1132 char *pretty_mask(char *mask)
1133 {
1134 static char star[2] = { '*', 0 };
1135 static char retmask[NICKLEN + USERLEN + HOSTLEN + 3];
1136 char *last_dot = NULL;
1137 char *ptr;
1138
1139 /* Case 1: default */
1140 char *nick = mask;
1141 char *user = star;
1142 char *host = star;
1143
1144 /* Do a _single_ pass through the characters of the mask: */
1145 for (ptr = mask; *ptr; ++ptr)
1146 {
1147 if (*ptr == '!')
1148 {
1149 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1150 user = ++ptr;
1151 host = star;
1152 }
1153 else if (*ptr == '@')
1154 {
1155 /* Case 4: Found last '@' (without finding a '!' yet) */
1156 nick = star;
1157 user = mask;
1158 host = ++ptr;
1159 }
1160 else if (*ptr == '.' || *ptr == ':')
1161 {
1162 /* Case 2: Found character specific to IP or hostname (without
1163 * finding a '!' or '@' yet) */
1164 last_dot = ptr;
1165 continue;
1166 }
1167 else
1168 continue;
1169 for (; *ptr; ++ptr)
1170 {
1171 if (*ptr == '@')
1172 {
1173 /* Case 4 or 5: Found last '@' */
1174 host = ptr + 1;
1175 }
1176 }
1177 break;
1178 }
1179 if (user == star && last_dot)
1180 {
1181 /* Case 2: */
1182 nick = star;
1183 user = star;
1184 host = mask;
1185 }
1186 /* Check lengths */
1187 if (nick != star)
1188 {
1189 char *nick_end = (user != star) ? user - 1 : ptr;
1190 if (nick_end - nick > NICKLEN)
1191 nick[NICKLEN] = 0;
1192 *nick_end = 0;
1193 }
1194 if (user != star)
1195 {
1196 char *user_end = (host != star) ? host - 1 : ptr;
1197 if (user_end - user > USERLEN)
1198 {
1199 user = user_end - USERLEN;
1200 *user = '*';
1201 }
1202 *user_end = 0;
1203 }
1204 if (host != star && ptr - host > HOSTLEN)
1205 {
1206 host = ptr - HOSTLEN;
1207 *host = '*';
1208 }
1209 ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host);
1210 return retmask;
1211 }
1212
1213 /** send a banlist to a client for a channel
1214 *
1215 * @param cptr Client to send the banlist to.
1216 * @param chptr Channel whose banlist to send.
1217 */
send_ban_list(struct Client * cptr,struct Channel * chptr)1218 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1219 {
1220 struct Ban* lp;
1221
1222 assert(0 != cptr);
1223 assert(0 != chptr);
1224
1225 for (lp = chptr->banlist; lp; lp = lp->next)
1226 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr,
1227 lp->who, lp->when);
1228
1229 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1230 }
1231
1232 /** Get a channel block, creating if necessary.
1233 * Get Channel block for chname (and allocate a new channel
1234 * block, if it didn't exists before).
1235 *
1236 * @param cptr Client joining the channel.
1237 * @param chname The name of the channel to join.
1238 * @param flag set to CGT_CREATE to create the channel if it doesn't
1239 * exist
1240 *
1241 * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE
1242 * wasn't specified or a pointer to the channel structure
1243 */
get_channel(struct Client * cptr,char * chname,ChannelGetType flag)1244 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1245 {
1246 struct Channel *chptr;
1247 int len;
1248
1249 if (EmptyString(chname))
1250 return NULL;
1251
1252 len = strlen(chname);
1253 if (MyUser(cptr) && len > CHANNELLEN)
1254 {
1255 len = CHANNELLEN;
1256 *(chname + CHANNELLEN) = '\0';
1257 }
1258 if ((chptr = FindChannel(chname)))
1259 return (chptr);
1260 if (flag == CGT_CREATE)
1261 {
1262 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1263 assert(0 != chptr);
1264 ++UserStats.channels;
1265 memset(chptr, 0, sizeof(struct Channel));
1266 strcpy(chptr->chname, chname);
1267 if (GlobalChannelList)
1268 GlobalChannelList->prev = chptr;
1269 chptr->prev = NULL;
1270 chptr->next = GlobalChannelList;
1271 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1272 GlobalChannelList = chptr;
1273 hAddChannel(chptr);
1274 }
1275 return chptr;
1276 }
1277
1278 /** invite a user to a channel.
1279 *
1280 * Adds an invite for a user to a channel. Limits the number of invites
1281 * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user.
1282 *
1283 * @param cptr The client to be invited.
1284 * @param chptr The channel to be invited to.
1285 */
add_invite(struct Client * cptr,struct Channel * chptr)1286 void add_invite(struct Client *cptr, struct Channel *chptr)
1287 {
1288 struct SLink *inv, **tmp;
1289
1290 del_invite(cptr, chptr);
1291 /*
1292 * Delete last link in chain if the list is max length
1293 */
1294 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1295 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1296 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1297 /*
1298 * Add client to channel invite list
1299 */
1300 inv = make_link();
1301 inv->value.cptr = cptr;
1302 inv->next = chptr->invites;
1303 chptr->invites = inv;
1304 /*
1305 * Add channel to the end of the client invite list
1306 */
1307 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1308 inv = make_link();
1309 inv->value.chptr = chptr;
1310 inv->next = NULL;
1311 (*tmp) = inv;
1312 (cli_user(cptr))->invites++;
1313 }
1314
1315 /** Delete an invite
1316 * Delete Invite block from channel invite list and client invite list
1317 *
1318 * @param cptr Client pointer
1319 * @param chptr Channel pointer
1320 */
del_invite(struct Client * cptr,struct Channel * chptr)1321 void del_invite(struct Client *cptr, struct Channel *chptr)
1322 {
1323 struct SLink **inv, *tmp;
1324
1325 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1326 if (tmp->value.cptr == cptr)
1327 {
1328 *inv = tmp->next;
1329 free_link(tmp);
1330 tmp = 0;
1331 (cli_user(cptr))->invites--;
1332 break;
1333 }
1334
1335 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1336 if (tmp->value.chptr == chptr)
1337 {
1338 *inv = tmp->next;
1339 free_link(tmp);
1340 tmp = 0;
1341 break;
1342 }
1343 }
1344
1345 /** @page zombie Explanation of Zombies
1346 *
1347 * Synopsis:
1348 *
1349 * A channel member is turned into a zombie when he is kicked from a
1350 * channel but his server has not acknowledged the kick. Servers that
1351 * see the member as a zombie can accept actions he performed before
1352 * being kicked, without allowing chanop operations from outsiders or
1353 * desyncing the network.
1354 *
1355 * Consider:
1356 * <pre>
1357 * client
1358 * |
1359 * c
1360 * |
1361 * X --a--> A --b--> B --d--> D
1362 * |
1363 * who
1364 * </pre>
1365 *
1366 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1367 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1368 *
1369 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1370 * Remove the user immediately when no users are left on the channel.
1371 * b) On server B : remove the user (who/lp) from the channel, send a
1372 * PART upstream (to A) and pass on the KICK.
1373 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1374 * channel, and pass on the KICK.
1375 * d) On server D : remove the user (who/lp) from the channel, and pass on
1376 * the KICK.
1377 *
1378 * Note:
1379 * - Setting the ZOMBIE flag never hurts, we either remove the
1380 * client after that or we don't.
1381 * - The KICK message was already passed on, as should be in all cases.
1382 * - `who' is removed in all cases except case a) when users are left.
1383 * - A PART is only sent upstream in case b).
1384 *
1385 * 2 aug 97:
1386 * <pre>
1387 * 6
1388 * |
1389 * 1 --- 2 --- 3 --- 4 --- 5
1390 * | |
1391 * kicker who
1392 * </pre>
1393 *
1394 * We also need to turn 'who' into a zombie on servers 1 and 6,
1395 * because a KICK from 'who' (kicking someone else in that direction)
1396 * can arrive there afterward - which should not be bounced itself.
1397 * Therefore case a) also applies for servers 1 and 6.
1398 *
1399 * --Run
1400 */
1401
1402 /** Turn a user on a channel into a zombie
1403 * This function turns a user into a zombie (see \ref zombie)
1404 *
1405 * @param member The structure representing this user on this channel.
1406 * @param who The client that is being kicked.
1407 * @param cptr The connection the kick came from.
1408 * @param sptr The client that is doing the kicking.
1409 * @param chptr The channel the user is being kicked from.
1410 */
make_zombie(struct Membership * member,struct Client * who,struct Client * cptr,struct Client * sptr,struct Channel * chptr)1411 void make_zombie(struct Membership* member, struct Client* who,
1412 struct Client* cptr, struct Client* sptr, struct Channel* chptr)
1413 {
1414 assert(0 != member);
1415 assert(0 != who);
1416 assert(0 != cptr);
1417 assert(0 != chptr);
1418
1419 /* Default for case a): */
1420 SetZombie(member);
1421
1422 /* Case b) or c) ?: */
1423 if (MyUser(who)) /* server 4 */
1424 {
1425 if (IsServer(cptr)) /* Case b) ? */
1426 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1427 remove_user_from_channel(who, chptr);
1428 return;
1429 }
1430 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1431 {
1432 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1433 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1434 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1435 {
1436 remove_user_from_channel(who, chptr);
1437 return;
1438 }
1439 }
1440
1441 /* Case a) (servers 1, 2, 3 and 6) */
1442 if (channel_all_zombies(chptr))
1443 remove_user_from_channel(who, chptr);
1444
1445 /* XXX Can't actually call Debug here; if the channel is all zombies,
1446 * chptr will no longer exist when we get here.
1447 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1448 */
1449 }
1450
1451 /** returns the number of zombies on a channel
1452 * @param chptr Channel to count zombies in.
1453 *
1454 * @returns The number of zombies on the channel.
1455 */
number_of_zombies(struct Channel * chptr)1456 int number_of_zombies(struct Channel *chptr)
1457 {
1458 struct Membership* member;
1459 int count = 0;
1460
1461 assert(0 != chptr);
1462 for (member = chptr->members; member; member = member->next_member) {
1463 if (IsZombie(member))
1464 ++count;
1465 }
1466 return count;
1467 }
1468
1469 /** Concatenate some strings together.
1470 * This helper function builds an argument string in strptr, consisting
1471 * of the original string, a space, and str1 and str2 concatenated (if,
1472 * of course, str2 is not NULL)
1473 *
1474 * @param strptr The buffer to concatenate into
1475 * @param strptr_i modified offset to the position to modify
1476 * @param str1 The string to concatenate from.
1477 * @param str2 The second string to contatenate from.
1478 * @param c Charactor to separate the string from str1 and str2.
1479 */
1480 static void
build_string(char * strptr,int * strptr_i,const char * str1,const char * str2,char c)1481 build_string(char *strptr, int *strptr_i, const char *str1,
1482 const char *str2, char c)
1483 {
1484 if (c)
1485 strptr[(*strptr_i)++] = c;
1486
1487 while (*str1)
1488 strptr[(*strptr_i)++] = *(str1++);
1489
1490 if (str2)
1491 while (*str2)
1492 strptr[(*strptr_i)++] = *(str2++);
1493
1494 strptr[(*strptr_i)] = '\0';
1495 }
1496
1497 /** Check a channel for join-delayed members.
1498 * @param[in] chan Channel to search.
1499 * @return Non-zero if any members are join-delayed; false if none are.
1500 */
1501 static int
find_delayed_joins(const struct Channel * chan)1502 find_delayed_joins(const struct Channel *chan)
1503 {
1504 const struct Membership *memb;
1505 for (memb = chan->members; memb; memb = memb->next_member)
1506 if (IsDelayedJoin(memb))
1507 return 1;
1508 return 0;
1509 }
1510
1511 /** Flush out the modes
1512 * This is the workhorse of our ModeBuf suite; this actually generates the
1513 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1514 *
1515 * @param mbuf The mode buffer to flush
1516 * @param all If true, flush all modes, otherwise leave partial modes in the
1517 * buffer.
1518 *
1519 * @returns 0
1520 */
1521 static int
modebuf_flush_int(struct ModeBuf * mbuf,int all)1522 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1523 {
1524 /* we only need the flags that don't take args right now */
1525 static int flags[] = {
1526 /* MODE_CHANOP, 'o', */
1527 /* MODE_VOICE, 'v', */
1528 MODE_PRIVATE, 'p',
1529 MODE_SECRET, 's',
1530 MODE_MODERATED, 'm',
1531 MODE_TOPICLIMIT, 't',
1532 MODE_INVITEONLY, 'i',
1533 MODE_NOPRIVMSGS, 'n',
1534 MODE_REGONLY, 'r',
1535 MODE_DELJOINS, 'D',
1536 MODE_REGISTERED, 'R',
1537 MODE_NOCOLOR, 'c',
1538 MODE_NOCTCP, 'C',
1539 /* MODE_KEY, 'k', */
1540 /* MODE_BAN, 'b', */
1541 MODE_LIMIT, 'l',
1542 /* MODE_APASS, 'A', */
1543 /* MODE_UPASS, 'U', */
1544 0x0, 0x0
1545 };
1546 static int local_flags[] = {
1547 MODE_WASDELJOINS, 'd',
1548 0x0, 0x0
1549 };
1550 int i;
1551 int *flag_p;
1552
1553 struct Client *app_source; /* where the MODE appears to come from */
1554
1555 char addbuf[20], addbuf_local[20]; /* accumulates +psmtin, etc. */
1556 int addbuf_i = 0, addbuf_local_i = 0;
1557 char rembuf[20], rembuf_local[20]; /* accumulates -psmtin, etc. */
1558 int rembuf_i = 0, rembuf_local_i = 0;
1559 char *bufptr; /* we make use of indirection to simplify the code */
1560 int *bufptr_i;
1561
1562 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1563 int addstr_i;
1564 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1565 int remstr_i;
1566 char *strptr; /* more indirection to simplify the code */
1567 int *strptr_i;
1568
1569 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1570 int tmp;
1571
1572 char limitbuf[20]; /* convert limits to strings */
1573
1574 unsigned int limitdel = MODE_LIMIT;
1575
1576 assert(0 != mbuf);
1577
1578 /* If the ModeBuf is empty, we have nothing to do */
1579 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1580 return 0;
1581
1582 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1583 */
1584 if (feature_bool(FEAT_HIS_MODEWHO) &&
1585 (mbuf->mb_dest & MODEBUF_DEST_OPMODE ||
1586 IsServer(mbuf->mb_source) ||
1587 IsMe(mbuf->mb_source)))
1588 app_source = &his;
1589 else
1590 app_source = mbuf->mb_source;
1591
1592 /* Must be set if going -D and some clients are hidden */
1593 if ((mbuf->mb_rem & MODE_DELJOINS)
1594 && !(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
1595 && find_delayed_joins(mbuf->mb_channel)) {
1596 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
1597 mbuf->mb_add |= MODE_WASDELJOINS;
1598 mbuf->mb_rem &= ~MODE_WASDELJOINS;
1599 }
1600
1601 /* +d must be cleared if +D is set */
1602 if ((mbuf->mb_add & MODE_DELJOINS)
1603 && (mbuf->mb_channel->mode.mode & MODE_WASDELJOINS)) {
1604 mbuf->mb_channel->mode.mode &= ~MODE_WASDELJOINS;
1605 mbuf->mb_add &= ~MODE_WASDELJOINS;
1606 mbuf->mb_rem |= MODE_WASDELJOINS;
1607 }
1608
1609 /*
1610 * Account for user we're bouncing; we have to get it in on the first
1611 * bounced MODE, or we could have problems
1612 */
1613 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1614 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1615
1616 /* Calculate the simple flags */
1617 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1618 if (*flag_p & mbuf->mb_add)
1619 addbuf[addbuf_i++] = flag_p[1];
1620 else if (*flag_p & mbuf->mb_rem)
1621 rembuf[rembuf_i++] = flag_p[1];
1622 }
1623
1624 /* Some flags may be for local display only. */
1625 for (flag_p = local_flags; flag_p[0]; flag_p += 2) {
1626 if (*flag_p & mbuf->mb_add)
1627 addbuf_local[addbuf_local_i++] = flag_p[1];
1628 else if (*flag_p & mbuf->mb_rem)
1629 rembuf_local[rembuf_local_i++] = flag_p[1];
1630 }
1631
1632 /* Now go through the modes with arguments... */
1633 for (i = 0; i < mbuf->mb_count; i++) {
1634 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1635 bufptr = addbuf;
1636 bufptr_i = &addbuf_i;
1637 } else {
1638 bufptr = rembuf;
1639 bufptr_i = &rembuf_i;
1640 }
1641
1642 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1643 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1644
1645 if ((totalbuflen - IRCD_MAX(9, tmp)) <= 0) /* don't overflow buffer */
1646 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1647 else {
1648 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1649 totalbuflen -= IRCD_MAX(9, tmp) + 1;
1650 }
1651 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
1652 tmp = strlen(MB_STRING(mbuf, i));
1653
1654 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1655 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1656 else {
1657 char mode_char;
1658 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
1659 {
1660 case MODE_APASS:
1661 mode_char = 'A';
1662 break;
1663 case MODE_UPASS:
1664 mode_char = 'U';
1665 break;
1666 default:
1667 mode_char = 'b';
1668 break;
1669 }
1670 bufptr[(*bufptr_i)++] = mode_char;
1671 totalbuflen -= tmp + 1;
1672 }
1673 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1674 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1675 strlen(MB_STRING(mbuf, i)));
1676
1677 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1678 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1679 else {
1680 bufptr[(*bufptr_i)++] = 'k';
1681 totalbuflen -= tmp + 1;
1682 }
1683 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1684 /* if it's a limit, we also format the number */
1685 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1686
1687 tmp = strlen(limitbuf);
1688
1689 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1690 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1691 else {
1692 bufptr[(*bufptr_i)++] = 'l';
1693 totalbuflen -= tmp + 1;
1694 }
1695 }
1696 }
1697
1698 /* terminate the mode strings */
1699 addbuf[addbuf_i] = '\0';
1700 rembuf[rembuf_i] = '\0';
1701 addbuf_local[addbuf_local_i] = '\0';
1702 rembuf_local[rembuf_local_i] = '\0';
1703
1704 /* If we're building a user visible MODE or HACK... */
1705 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1706 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1707 MODEBUF_DEST_LOG)) {
1708 /* Set up the parameter strings */
1709 addstr[0] = '\0';
1710 addstr_i = 0;
1711 remstr[0] = '\0';
1712 remstr_i = 0;
1713
1714 for (i = 0; i < mbuf->mb_count; i++) {
1715 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1716 continue;
1717
1718 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1719 strptr = addstr;
1720 strptr_i = &addstr_i;
1721 } else {
1722 strptr = remstr;
1723 strptr_i = &remstr_i;
1724 }
1725
1726 /* deal with clients... */
1727 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1728 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1729
1730 /* deal with bans... */
1731 else if (MB_TYPE(mbuf, i) & MODE_BAN)
1732 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1733
1734 /* deal with keys... */
1735 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1736 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1737 "*" : MB_STRING(mbuf, i), 0, ' ');
1738
1739 /* deal with invisible passwords */
1740 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1741 build_string(strptr, strptr_i, "*", 0, ' ');
1742
1743 /*
1744 * deal with limit; note we cannot include the limit parameter if we're
1745 * removing it
1746 */
1747 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1748 (MODE_ADD | MODE_LIMIT))
1749 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1750 }
1751
1752 /* send the messages off to their destination */
1753 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1754 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1755 "[%Tu]",
1756 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1757 mbuf->mb_source : app_source),
1758 mbuf->mb_channel->chname,
1759 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1760 addbuf, remstr, addstr,
1761 mbuf->mb_channel->creationtime);
1762
1763 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1764 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1765 "%s%s%s%s%s%s [%Tu]",
1766 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1767 mbuf->mb_source : app_source),
1768 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1769 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1770 mbuf->mb_channel->creationtime);
1771
1772 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1773 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1774 "[%Tu]",
1775 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1776 mbuf->mb_source : app_source),
1777 mbuf->mb_channel->chname,
1778 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1779 addbuf, remstr, addstr,
1780 mbuf->mb_channel->creationtime);
1781
1782 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1783 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1784 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1785 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1786 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1787
1788 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1789 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0,
1790 "%H %s%s%s%s%s%s%s%s", mbuf->mb_channel,
1791 rembuf_i || rembuf_local_i ? "-" : "",
1792 rembuf, rembuf_local,
1793 addbuf_i || addbuf_local_i ? "+" : "",
1794 addbuf, addbuf_local,
1795 remstr, addstr);
1796 }
1797
1798 /* Now are we supposed to propagate to other servers? */
1799 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1800 /* set up parameter string */
1801 addstr[0] = '\0';
1802 addstr_i = 0;
1803 remstr[0] = '\0';
1804 remstr_i = 0;
1805
1806 /*
1807 * limit is supressed if we're removing it; we have to figure out which
1808 * direction is the direction for it to be removed, though...
1809 */
1810 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) ? MODE_DEL : MODE_ADD;
1811
1812 for (i = 0; i < mbuf->mb_count; i++) {
1813 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1814 continue;
1815
1816 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1817 strptr = addstr;
1818 strptr_i = &addstr_i;
1819 } else {
1820 strptr = remstr;
1821 strptr_i = &remstr_i;
1822 }
1823
1824 /* if we're changing oplevels and we know the oplevel, pass it on */
1825 if ((MB_TYPE(mbuf, i) & MODE_CHANOP)
1826 && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL)
1827 *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i,
1828 " %s%s:%d",
1829 NumNick(MB_CLIENT(mbuf, i)),
1830 MB_OPLEVEL(mbuf, i));
1831
1832 /* deal with other modes that take clients */
1833 else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1834 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1835
1836 /* deal with modes that take strings */
1837 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1838 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1839
1840 /*
1841 * deal with the limit. Logic here is complicated; if HACK2 is set,
1842 * we're bouncing the mode, so sense is reversed, and we have to
1843 * include the original limit if it looks like it's being removed
1844 */
1845 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1846 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1847 }
1848
1849 /* we were told to deop the source */
1850 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1851 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1852 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1853 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1854
1855 /* mark that we've done this, so we don't do it again */
1856 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1857 }
1858
1859 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1860 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1861 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1862 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1863 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1864 addbuf, remstr, addstr);
1865 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1866 /*
1867 * If HACK2 was set, we're bouncing; we send the MODE back to
1868 * the connection we got it from with the senses reversed and
1869 * the proper TS; origin is us
1870 */
1871 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1872 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1873 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1874 mbuf->mb_channel->creationtime);
1875 } else {
1876 /*
1877 * We're propagating a normal (or HACK3 or HACK4) MODE command
1878 * to the rest of the network. We send the actual channel TS.
1879 */
1880 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1881 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1882 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1883 addbuf, remstr, addstr,
1884 mbuf->mb_channel->creationtime);
1885 }
1886 }
1887
1888 /* We've drained the ModeBuf... */
1889 mbuf->mb_add = 0;
1890 mbuf->mb_rem = 0;
1891 mbuf->mb_count = 0;
1892
1893 /* reinitialize the mode-with-arg slots */
1894 for (i = 0; i < MAXMODEPARAMS; i++) {
1895 /* If we saved any, pack them down */
1896 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1897 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1898 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1899
1900 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1901 continue;
1902 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1903 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1904
1905 MB_TYPE(mbuf, i) = 0;
1906 MB_UINT(mbuf, i) = 0;
1907 }
1908
1909 /* If we're supposed to flush it all, do so--all hail tail recursion */
1910 if (all && mbuf->mb_count)
1911 return modebuf_flush_int(mbuf, 1);
1912
1913 return 0;
1914 }
1915
1916 /** Initialise a modebuf
1917 * This routine just initializes a ModeBuf structure with the information
1918 * needed and the options given.
1919 *
1920 * @param mbuf The mode buffer to initialise.
1921 * @param source The client that is performing the mode.
1922 * @param connect ?
1923 * @param chan The channel that the mode is being performed upon.
1924 * @param dest ?
1925 */
1926 void
modebuf_init(struct ModeBuf * mbuf,struct Client * source,struct Client * connect,struct Channel * chan,unsigned int dest)1927 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1928 struct Client *connect, struct Channel *chan, unsigned int dest)
1929 {
1930 int i;
1931
1932 assert(0 != mbuf);
1933 assert(0 != source);
1934 assert(0 != chan);
1935 assert(0 != dest);
1936
1937 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
1938
1939 mbuf->mb_add = 0;
1940 mbuf->mb_rem = 0;
1941 mbuf->mb_source = source;
1942 mbuf->mb_connect = connect;
1943 mbuf->mb_channel = chan;
1944 mbuf->mb_dest = dest;
1945 mbuf->mb_count = 0;
1946
1947 /* clear each mode-with-parameter slot */
1948 for (i = 0; i < MAXMODEPARAMS; i++) {
1949 MB_TYPE(mbuf, i) = 0;
1950 MB_UINT(mbuf, i) = 0;
1951 }
1952 }
1953
1954 /** Append a new mode to a modebuf
1955 * This routine simply adds modes to be added or deleted; do a binary OR
1956 * with either MODE_ADD or MODE_DEL
1957 *
1958 * @param mbuf Mode buffer
1959 * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
1960 */
1961 void
modebuf_mode(struct ModeBuf * mbuf,unsigned int mode)1962 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1963 {
1964 assert(0 != mbuf);
1965 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1966
1967 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1968 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
1969 MODE_NOCOLOR | MODE_NOCTCP |
1970 MODE_DELJOINS | MODE_WASDELJOINS | MODE_REGISTERED);
1971
1972 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1973 return;
1974
1975 if (mode & MODE_ADD) {
1976 mbuf->mb_rem &= ~mode;
1977 mbuf->mb_add |= mode;
1978 } else {
1979 mbuf->mb_add &= ~mode;
1980 mbuf->mb_rem |= mode;
1981 }
1982 }
1983
1984 /** Append a mode that takes an int argument to the modebuf
1985 *
1986 * This routine adds a mode to be added or deleted that takes a unsigned
1987 * int parameter; mode may *only* be the relevant mode flag ORed with one
1988 * of MODE_ADD or MODE_DEL
1989 *
1990 * @param mbuf The mode buffer to append to.
1991 * @param mode The mode to append.
1992 * @param uint The argument to the mode.
1993 */
1994 void
modebuf_mode_uint(struct ModeBuf * mbuf,unsigned int mode,unsigned int uint)1995 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1996 {
1997 assert(0 != mbuf);
1998 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1999
2000 if (mode == (MODE_LIMIT | MODE_DEL)) {
2001 mbuf->mb_rem |= mode;
2002 return;
2003 }
2004 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2005 MB_UINT(mbuf, mbuf->mb_count) = uint;
2006
2007 /* when we've reached the maximal count, flush the buffer */
2008 if (++mbuf->mb_count >=
2009 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2010 modebuf_flush_int(mbuf, 0);
2011 }
2012
2013 /** append a string mode
2014 * This routine adds a mode to be added or deleted that takes a string
2015 * parameter; mode may *only* be the relevant mode flag ORed with one of
2016 * MODE_ADD or MODE_DEL
2017 *
2018 * @param mbuf The mode buffer to append to.
2019 * @param mode The mode to append.
2020 * @param string The string parameter to append.
2021 * @param free If the string should be free'd later.
2022 */
2023 void
modebuf_mode_string(struct ModeBuf * mbuf,unsigned int mode,char * string,int free)2024 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
2025 int free)
2026 {
2027 assert(0 != mbuf);
2028 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2029
2030 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
2031 MB_STRING(mbuf, mbuf->mb_count) = string;
2032
2033 /* when we've reached the maximal count, flush the buffer */
2034 if (++mbuf->mb_count >=
2035 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2036 modebuf_flush_int(mbuf, 0);
2037 }
2038
2039 /** Append a mode on a client to a modebuf.
2040 * This routine adds a mode to be added or deleted that takes a client
2041 * parameter; mode may *only* be the relevant mode flag ORed with one of
2042 * MODE_ADD or MODE_DEL
2043 *
2044 * @param mbuf The modebuf to append the mode to.
2045 * @param mode The mode to append.
2046 * @param client The client argument to append.
2047 * @param oplevel The oplevel the user had or will have
2048 */
2049 void
modebuf_mode_client(struct ModeBuf * mbuf,unsigned int mode,struct Client * client,int oplevel)2050 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
2051 struct Client *client, int oplevel)
2052 {
2053 assert(0 != mbuf);
2054 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2055
2056 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2057 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2058 MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel;
2059
2060 /* when we've reached the maximal count, flush the buffer */
2061 if (++mbuf->mb_count >=
2062 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2063 modebuf_flush_int(mbuf, 0);
2064 }
2065
2066 /** The exported binding for modebuf_flush()
2067 *
2068 * @param mbuf The mode buffer to flush.
2069 *
2070 * @see modebuf_flush_int()
2071 */
2072 int
modebuf_flush(struct ModeBuf * mbuf)2073 modebuf_flush(struct ModeBuf *mbuf)
2074 {
2075 return modebuf_flush_int(mbuf, 1);
2076 }
2077
2078 /* This extracts the simple modes contained in mbuf
2079 *
2080 * @param mbuf The mode buffer to extract the modes from.
2081 * @param buf The string buffer to write the modes into.
2082 */
2083 void
modebuf_extract(struct ModeBuf * mbuf,char * buf)2084 modebuf_extract(struct ModeBuf *mbuf, char *buf)
2085 {
2086 static int flags[] = {
2087 /* MODE_CHANOP, 'o', */
2088 /* MODE_VOICE, 'v', */
2089 MODE_PRIVATE, 'p',
2090 MODE_SECRET, 's',
2091 MODE_MODERATED, 'm',
2092 MODE_TOPICLIMIT, 't',
2093 MODE_INVITEONLY, 'i',
2094 MODE_NOPRIVMSGS, 'n',
2095 MODE_KEY, 'k',
2096 MODE_APASS, 'A',
2097 MODE_UPASS, 'U',
2098 MODE_REGISTERED, 'R',
2099 /* MODE_BAN, 'b', */
2100 MODE_LIMIT, 'l',
2101 MODE_REGONLY, 'r',
2102 MODE_DELJOINS, 'D',
2103 MODE_NOCOLOR, 'c',
2104 MODE_NOCTCP, 'C',
2105 0x0, 0x0
2106 };
2107 unsigned int add;
2108 int i, bufpos = 0, len;
2109 int *flag_p;
2110 char *key = 0, limitbuf[20];
2111 char *apass = 0, *upass = 0;
2112
2113 assert(0 != mbuf);
2114 assert(0 != buf);
2115
2116 buf[0] = '\0';
2117
2118 add = mbuf->mb_add;
2119
2120 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2121 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2122 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
2123
2124 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2125 key = MB_STRING(mbuf, i);
2126 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2127 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2128 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2129 upass = MB_STRING(mbuf, i);
2130 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2131 apass = MB_STRING(mbuf, i);
2132 }
2133 }
2134
2135 if (!add)
2136 return;
2137
2138 buf[bufpos++] = '+'; /* start building buffer */
2139
2140 for (flag_p = flags; flag_p[0]; flag_p += 2)
2141 if (*flag_p & add)
2142 buf[bufpos++] = flag_p[1];
2143
2144 for (i = 0, len = bufpos; i < len; i++) {
2145 if (buf[i] == 'k')
2146 build_string(buf, &bufpos, key, 0, ' ');
2147 else if (buf[i] == 'l')
2148 build_string(buf, &bufpos, limitbuf, 0, ' ');
2149 else if (buf[i] == 'U')
2150 build_string(buf, &bufpos, upass, 0, ' ');
2151 else if (buf[i] == 'A')
2152 build_string(buf, &bufpos, apass, 0, ' ');
2153 }
2154
2155 buf[bufpos] = '\0';
2156
2157 return;
2158 }
2159
2160 /** Simple function to invalidate a channel's ban cache.
2161 *
2162 * This function marks all members of the channel as being neither
2163 * banned nor banned.
2164 *
2165 * @param chan The channel to operate on.
2166 */
2167 void
mode_ban_invalidate(struct Channel * chan)2168 mode_ban_invalidate(struct Channel *chan)
2169 {
2170 struct Membership *member;
2171
2172 for (member = chan->members; member; member = member->next_member)
2173 ClearBanValid(member);
2174 }
2175
2176 /** Simple function to drop invite structures
2177 *
2178 * Remove all the invites on the channel.
2179 *
2180 * @param chan Channel to remove invites from.
2181 *
2182 */
2183 void
mode_invite_clear(struct Channel * chan)2184 mode_invite_clear(struct Channel *chan)
2185 {
2186 while (chan->invites)
2187 del_invite(chan->invites->value.cptr, chan);
2188 }
2189
2190 /* What we've done for mode_parse so far... */
2191 #define DONE_LIMIT 0x01 /**< We've set the limit */
2192 #define DONE_KEY_ADD 0x02 /**< We've set the key */
2193 #define DONE_BANLIST 0x04 /**< We've sent the ban list */
2194 #define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
2195 #define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
2196 #define DONE_UPASS_ADD 0x20 /**< We've set user pass */
2197 #define DONE_APASS_ADD 0x40 /**< We've set admin pass */
2198 #define DONE_KEY_DEL 0x80 /**< We've removed the key */
2199 #define DONE_UPASS_DEL 0x100 /**< We've removed the user pass */
2200 #define DONE_APASS_DEL 0x200 /**< We've removed the admin pass */
2201
2202 struct ParseState {
2203 struct ModeBuf *mbuf;
2204 struct Client *cptr;
2205 struct Client *sptr;
2206 struct Channel *chptr;
2207 struct Membership *member;
2208 int parc;
2209 char **parv;
2210 unsigned int flags;
2211 unsigned int dir;
2212 unsigned int done;
2213 unsigned int add;
2214 unsigned int del;
2215 int args_used;
2216 int max_args;
2217 int numbans;
2218 struct Ban banlist[MAXPARA];
2219 struct {
2220 unsigned int flag;
2221 unsigned short oplevel;
2222 struct Client *client;
2223 } cli_change[MAXPARA];
2224 };
2225
2226 /** Helper function to send "Not oper" or "Not member" messages
2227 * Here's a helper function to deal with sending along "Not oper" or
2228 * "Not member" messages
2229 *
2230 * @param state Parsing State object
2231 */
2232 static void
send_notoper(struct ParseState * state)2233 send_notoper(struct ParseState *state)
2234 {
2235 if (state->done & DONE_NOTOPER)
2236 return;
2237
2238 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2239 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2240
2241 state->done |= DONE_NOTOPER;
2242 }
2243
2244 /** Parse a limit
2245 * Helper function to convert limits
2246 *
2247 * @param state Parsing state object.
2248 * @param flag_p ?
2249 */
2250 static void
mode_parse_limit(struct ParseState * state,int * flag_p)2251 mode_parse_limit(struct ParseState *state, int *flag_p)
2252 {
2253 unsigned int t_limit;
2254
2255 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2256 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2257 return;
2258
2259 if (state->parc <= 0) { /* warn if not enough args */
2260 if (MyUser(state->sptr))
2261 need_more_params(state->sptr, "MODE +l");
2262 return;
2263 }
2264
2265 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2266 state->parc--;
2267 state->max_args--;
2268
2269 if ((int)t_limit<0) /* don't permit a negative limit */
2270 return;
2271
2272 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2273 (!t_limit || t_limit == state->chptr->mode.limit))
2274 return;
2275 } else
2276 t_limit = state->chptr->mode.limit;
2277
2278 /* If they're not an oper, they can't change modes */
2279 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2280 send_notoper(state);
2281 return;
2282 }
2283
2284 /* Can't remove a limit that's not there */
2285 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2286 return;
2287
2288 /* Skip if this is a burst and a lower limit than this is set already */
2289 if ((state->flags & MODE_PARSE_BURST) &&
2290 (state->chptr->mode.mode & flag_p[0]) &&
2291 (state->chptr->mode.limit < t_limit))
2292 return;
2293
2294 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2295 return;
2296 state->done |= DONE_LIMIT;
2297
2298 if (!state->mbuf)
2299 return;
2300
2301 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2302
2303 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2304 if (state->dir & MODE_ADD) {
2305 state->chptr->mode.mode |= flag_p[0];
2306 state->chptr->mode.limit = t_limit;
2307 } else {
2308 state->chptr->mode.mode &= ~flag_p[0];
2309 state->chptr->mode.limit = 0;
2310 }
2311 }
2312 }
2313
2314 /** Helper function to validate key-like parameters.
2315 *
2316 * @param[in] state Parse state for feedback to user.
2317 * @param[in] s Key to validate.
2318 * @param[in] command String to pass for need_more_params() command.
2319 * @return Zero on an invalid key, non-zero if the key was okay.
2320 */
2321 static int
is_clean_key(struct ParseState * state,char * s,char * command)2322 is_clean_key(struct ParseState *state, char *s, char *command)
2323 {
2324 int ii;
2325
2326 if (s[0] == '\0') {
2327 if (MyUser(state->sptr))
2328 need_more_params(state->sptr, command);
2329 return 0;
2330 }
2331 else if (s[0] == ':') {
2332 if (MyUser(state->sptr))
2333 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2334 return 0;
2335 }
2336 for (ii = 0; (ii <= KEYLEN) && (s[ii] != '\0'); ++ii) {
2337 if ((unsigned char)s[ii] <= ' ' || s[ii] == ',') {
2338 if (MyUser(state->sptr))
2339 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2340 return 0;
2341 }
2342 }
2343 if (ii > KEYLEN) {
2344 if (MyUser(state->sptr))
2345 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2346 return 0;
2347 }
2348 return 1;
2349 }
2350
2351 /*
2352 * Helper function to convert keys
2353 */
2354 static void
mode_parse_key(struct ParseState * state,int * flag_p)2355 mode_parse_key(struct ParseState *state, int *flag_p)
2356 {
2357 char *t_str;
2358
2359 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2360 return;
2361
2362 if (state->parc <= 0) { /* warn if not enough args */
2363 if (MyUser(state->sptr))
2364 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2365 "MODE -k");
2366 return;
2367 }
2368
2369 t_str = state->parv[state->args_used++]; /* grab arg */
2370 state->parc--;
2371 state->max_args--;
2372
2373 /* If they're not an oper, they can't change modes */
2374 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2375 send_notoper(state);
2376 return;
2377 }
2378
2379 /* allow removing and then adding key, but not adding and then removing */
2380 if (state->dir == MODE_ADD)
2381 {
2382 if (state->done & DONE_KEY_ADD)
2383 return;
2384 state->done |= DONE_KEY_ADD;
2385 }
2386 else
2387 {
2388 if (state->done & (DONE_KEY_ADD | DONE_KEY_DEL))
2389 return;
2390 state->done |= DONE_KEY_DEL;
2391 }
2392
2393 /* If the key is invalid, tell the user and bail. */
2394 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +k" :
2395 "MODE -k"))
2396 return;
2397
2398 if (!state->mbuf)
2399 return;
2400
2401 /* Skip if this is a burst, we have a key already and the new key is
2402 * after the old one alphabetically */
2403 if ((state->flags & MODE_PARSE_BURST) &&
2404 *(state->chptr->mode.key) &&
2405 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2406 return;
2407
2408 /* can't add a key if one is set, nor can one remove the wrong key */
2409 if (!(state->flags & MODE_PARSE_FORCE))
2410 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2411 (state->dir == MODE_DEL &&
2412 ircd_strcmp(state->chptr->mode.key, t_str))) {
2413 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2414 return;
2415 }
2416
2417 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2418 !ircd_strcmp(state->chptr->mode.key, t_str))
2419 return; /* no key change */
2420
2421 if (state->flags & MODE_PARSE_BOUNCE) {
2422 if (*state->chptr->mode.key) /* reset old key */
2423 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2424 state->chptr->mode.key, 0);
2425 else /* remove new bogus key */
2426 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2427 } else /* send new key */
2428 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2429
2430 if (state->flags & MODE_PARSE_SET) {
2431 if (state->dir == MODE_DEL) /* remove the old key */
2432 *state->chptr->mode.key = '\0';
2433 else
2434 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2435 }
2436 }
2437
2438 /*
2439 * Helper function to convert user passes
2440 */
2441 static void
mode_parse_upass(struct ParseState * state,int * flag_p)2442 mode_parse_upass(struct ParseState *state, int *flag_p)
2443 {
2444 char *t_str;
2445
2446 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2447 return;
2448
2449 if (state->parc <= 0) { /* warn if not enough args */
2450 if (MyUser(state->sptr))
2451 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2452 "MODE -U");
2453 return;
2454 }
2455
2456 t_str = state->parv[state->args_used++]; /* grab arg */
2457 state->parc--;
2458 state->max_args--;
2459
2460 /* If they're not an oper, they can't change modes */
2461 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2462 send_notoper(state);
2463 return;
2464 }
2465
2466 /* If a non-service user is trying to force it, refuse. */
2467 if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
2468 && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2469 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2470 state->chptr->chname);
2471 return;
2472 }
2473
2474 /* If they are not the channel manager, they are not allowed to change it */
2475 if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
2476 if (*state->chptr->mode.apass) {
2477 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2478 state->chptr->chname);
2479 } else {
2480 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2481 (TStime() - state->chptr->creationtime < 172800) ?
2482 "approximately 4-5 minutes" : "approximately 48 hours");
2483 }
2484 return;
2485 }
2486
2487 /* allow removing and then adding upass, but not adding and then removing */
2488 if (state->dir == MODE_ADD)
2489 {
2490 if (state->done & DONE_UPASS_ADD)
2491 return;
2492 state->done |= DONE_UPASS_ADD;
2493 }
2494 else
2495 {
2496 if (state->done & (DONE_UPASS_ADD | DONE_UPASS_DEL))
2497 return;
2498 state->done |= DONE_UPASS_DEL;
2499 }
2500
2501 /* If the Upass is invalid, tell the user and bail. */
2502 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +U" :
2503 "MODE -U"))
2504 return;
2505
2506 if (!state->mbuf)
2507 return;
2508
2509 if (!(state->flags & MODE_PARSE_FORCE)) {
2510 /* can't add the upass while apass is not set */
2511 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2512 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2513 return;
2514 }
2515 /* cannot set a +U password that is the same as +A */
2516 if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) {
2517 send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname);
2518 return;
2519 }
2520 /* can't add a upass if one is set, nor can one remove the wrong upass */
2521 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2522 (state->dir == MODE_DEL &&
2523 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2524 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2525 return;
2526 }
2527 }
2528
2529 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2530 !ircd_strcmp(state->chptr->mode.upass, t_str))
2531 return; /* no upass change */
2532
2533 /* Skip if this is a burst, we have a Upass already and the new Upass is
2534 * after the old one alphabetically */
2535 if ((state->flags & MODE_PARSE_BURST) &&
2536 *(state->chptr->mode.upass) &&
2537 ircd_strcmp(state->chptr->mode.upass, t_str) <= 0)
2538 return;
2539
2540 if (state->flags & MODE_PARSE_BOUNCE) {
2541 if (*state->chptr->mode.upass) /* reset old upass */
2542 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2543 state->chptr->mode.upass, 0);
2544 else /* remove new bogus upass */
2545 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2546 } else /* send new upass */
2547 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2548
2549 if (state->flags & MODE_PARSE_SET) {
2550 if (state->dir == MODE_DEL) /* remove the old upass */
2551 *state->chptr->mode.upass = '\0';
2552 else
2553 ircd_strncpy(state->chptr->mode.upass, t_str, KEYLEN);
2554 }
2555 }
2556
2557 /*
2558 * Helper function to convert admin passes
2559 */
2560 static void
mode_parse_apass(struct ParseState * state,int * flag_p)2561 mode_parse_apass(struct ParseState *state, int *flag_p)
2562 {
2563 struct Membership *memb;
2564 char *t_str;
2565
2566 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2567 return;
2568
2569 if (state->parc <= 0) { /* warn if not enough args */
2570 if (MyUser(state->sptr))
2571 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2572 "MODE -A");
2573 return;
2574 }
2575
2576 t_str = state->parv[state->args_used++]; /* grab arg */
2577 state->parc--;
2578 state->max_args--;
2579
2580 /* If they're not an oper, they can't change modes */
2581 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2582 send_notoper(state);
2583 return;
2584 }
2585
2586 if (MyUser(state->sptr)) {
2587 if (state->flags & MODE_PARSE_FORCE) {
2588 /* If an unprivileged oper is trying to force it, refuse. */
2589 if (!HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2590 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2591 state->chptr->chname);
2592 return;
2593 }
2594 } else {
2595 /* If they are not the channel manager, they are not allowed to change it. */
2596 if (!IsChannelManager(state->member)) {
2597 if (*state->chptr->mode.apass) {
2598 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2599 state->chptr->chname);
2600 } else {
2601 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2602 (TStime() - state->chptr->creationtime < 172800) ?
2603 "approximately 4-5 minutes" : "approximately 48 hours");
2604 }
2605 return;
2606 }
2607 /* Can't remove the Apass while Upass is still set. */
2608 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2609 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2610 return;
2611 }
2612 /* Can't add an Apass if one is set, nor can one remove the wrong Apass. */
2613 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2614 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2615 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2616 return;
2617 }
2618 }
2619
2620 /* Forbid removing the Apass if the channel is older than 48 hours
2621 * unless an oper is doing it. */
2622 if (TStime() - state->chptr->creationtime >= 172800
2623 && state->dir == MODE_DEL
2624 && !IsAnOper(state->sptr)) {
2625 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2626 return;
2627 }
2628 }
2629
2630 /* allow removing and then adding apass, but not adding and then removing */
2631 if (state->dir == MODE_ADD)
2632 {
2633 if (state->done & DONE_APASS_ADD)
2634 return;
2635 state->done |= DONE_APASS_ADD;
2636 }
2637 else
2638 {
2639 if (state->done & (DONE_APASS_ADD | DONE_APASS_DEL))
2640 return;
2641 state->done |= DONE_APASS_DEL;
2642 }
2643
2644 /* If the Apass is invalid, tell the user and bail. */
2645 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +A" :
2646 "MODE -A"))
2647 return;
2648
2649 if (!state->mbuf)
2650 return;
2651
2652 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2653 !ircd_strcmp(state->chptr->mode.apass, t_str))
2654 return; /* no apass change */
2655
2656 /* Skip if this is a burst, we have an Apass already and the new Apass is
2657 * after the old one alphabetically */
2658 if ((state->flags & MODE_PARSE_BURST) &&
2659 *(state->chptr->mode.apass) &&
2660 ircd_strcmp(state->chptr->mode.apass, t_str) <= 0)
2661 return;
2662
2663 if (state->flags & MODE_PARSE_BOUNCE) {
2664 if (*state->chptr->mode.apass) /* reset old apass */
2665 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2666 state->chptr->mode.apass, 0);
2667 else /* remove new bogus apass */
2668 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2669 } else /* send new apass */
2670 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2671
2672 if (state->flags & MODE_PARSE_SET) {
2673 if (state->dir == MODE_ADD) { /* set the new apass */
2674 /* Only accept the new apass if there is no current apass or
2675 * this is a BURST. */
2676 if (state->chptr->mode.apass[0] == '\0' ||
2677 (state->flags & MODE_PARSE_BURST))
2678 ircd_strncpy(state->chptr->mode.apass, t_str, KEYLEN);
2679 /* Make it VERY clear to the user that this is a one-time password */
2680 if (MyUser(state->sptr)) {
2681 send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
2682 send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
2683 state->chptr->mode.apass);
2684 }
2685 /* Give the channel manager level 0 ops.
2686 There should not be tested for IsChannelManager here because
2687 on the local server it is impossible to set the apass if one
2688 isn't a channel manager and remote servers might need to sync
2689 the oplevel here: when someone creates a channel (and becomes
2690 channel manager) during a net.break, and only sets the Apass
2691 after the net rejoined, they will have oplevel MAXOPLEVEL on
2692 all remote servers. */
2693 if (state->member)
2694 SetOpLevel(state->member, 0);
2695 } else { /* remove the old apass */
2696 *state->chptr->mode.apass = '\0';
2697 /* Clear Upass so that there is never a Upass set when a zannel is burst. */
2698 *state->chptr->mode.upass = '\0';
2699 if (MyUser(state->sptr))
2700 send_reply(state->sptr, RPL_APASSWARN_CLEAR);
2701 /* Revert everyone to MAXOPLEVEL. */
2702 for (memb = state->chptr->members; memb; memb = memb->next_member) {
2703 if (memb->status & MODE_CHANOP)
2704 SetOpLevel(memb, MAXOPLEVEL);
2705 }
2706 }
2707 }
2708 }
2709
2710 /** Compare one ban's extent to another.
2711 * This works very similarly to mmatch() but it knows about CIDR masks
2712 * and ban exceptions. If both bans are CIDR-based, compare their
2713 * address bits; otherwise, use mmatch().
2714 * @param[in] old_ban One ban.
2715 * @param[in] new_ban Another ban.
2716 * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise.
2717 */
2718 static int
bmatch(struct Ban * old_ban,struct Ban * new_ban)2719 bmatch(struct Ban *old_ban, struct Ban *new_ban)
2720 {
2721 int res;
2722 assert(old_ban != NULL);
2723 assert(new_ban != NULL);
2724 /* A ban is never treated as a superset of an exception. */
2725 if (!(old_ban->flags & BAN_EXCEPTION)
2726 && (new_ban->flags & BAN_EXCEPTION))
2727 return 1;
2728 /* If either is not an address mask, match the text masks. */
2729 if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0)
2730 return mmatch(old_ban->banstr, new_ban->banstr);
2731 /* If the old ban has a longer prefix than new, it cannot be a superset. */
2732 if (old_ban->addrbits > new_ban->addrbits)
2733 return 1;
2734 /* Compare the masks before the hostname part. */
2735 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0';
2736 res = mmatch(old_ban->banstr, new_ban->banstr);
2737 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@';
2738 if (res)
2739 return res;
2740 /* If the old ban's mask mismatches, cannot be a superset. */
2741 if (!ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits))
2742 return 1;
2743 /* Otherwise it depends on whether the old ban's text is a superset
2744 * of the new. */
2745 return mmatch(old_ban->banstr, new_ban->banstr);
2746 }
2747
2748 /** Add a ban from a ban list and mark bans that should be removed
2749 * because they overlap.
2750 *
2751 * There are three invariants for a ban list. First, no ban may be
2752 * more specific than another ban. Second, no exception may be more
2753 * specific than another exception. Finally, no ban may be more
2754 * specific than any exception.
2755 *
2756 * @param[in,out] banlist Pointer to head of list.
2757 * @param[in] newban Ban (or exception) to add (or remove).
2758 * @param[in] do_free If non-zero, free \a newban on failure.
2759 * @return Zero if \a newban could be applied, non-zero if not.
2760 */
apply_ban(struct Ban ** banlist,struct Ban * newban,int do_free)2761 int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
2762 {
2763 struct Ban *ban;
2764 size_t count = 0;
2765
2766 assert(newban->flags & (BAN_ADD|BAN_DEL));
2767 if (newban->flags & BAN_ADD) {
2768 size_t totlen = 0;
2769 /* If a less specific *active* entry is found, fail. */
2770 for (ban = *banlist; ban; ban = ban->next) {
2771 if (!bmatch(ban, newban) && !(ban->flags & BAN_DEL)) {
2772 if (do_free)
2773 free_ban(newban);
2774 return 1;
2775 }
2776 if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) {
2777 count++;
2778 totlen += strlen(ban->banstr);
2779 }
2780 }
2781 /* Mark more specific entries and add this one to the end of the list. */
2782 while ((ban = *banlist) != NULL) {
2783 if (!bmatch(newban, ban)) {
2784 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2785 }
2786 banlist = &ban->next;
2787 }
2788 *banlist = newban;
2789 return 0;
2790 } else if (newban->flags & BAN_DEL) {
2791 size_t remove_count = 0;
2792 /* Mark more specific entries. */
2793 for (ban = *banlist; ban; ban = ban->next) {
2794 if (!bmatch(newban, ban)) {
2795 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2796 remove_count++;
2797 }
2798 }
2799 if (remove_count)
2800 return 0;
2801 /* If no matches were found, fail. */
2802 if (do_free)
2803 free_ban(newban);
2804 return 3;
2805 }
2806 if (do_free)
2807 free_ban(newban);
2808 return 4;
2809 }
2810
2811 /*
2812 * Helper function to convert bans
2813 */
2814 static void
mode_parse_ban(struct ParseState * state,int * flag_p)2815 mode_parse_ban(struct ParseState *state, int *flag_p)
2816 {
2817 char *t_str, *s;
2818 struct Ban *ban, *newban;
2819
2820 if (state->parc <= 0) { /* Not enough args, send ban list */
2821 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2822 send_ban_list(state->sptr, state->chptr);
2823 state->done |= DONE_BANLIST;
2824 }
2825
2826 return;
2827 }
2828
2829 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2830 return;
2831
2832 t_str = state->parv[state->args_used++]; /* grab arg */
2833 state->parc--;
2834 state->max_args--;
2835
2836 /* If they're not an oper, they can't change modes */
2837 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2838 send_notoper(state);
2839 return;
2840 }
2841
2842 if ((s = strchr(t_str, ' ')))
2843 *s = '\0';
2844
2845 if (!*t_str || *t_str == ':') { /* warn if empty */
2846 if (MyUser(state->sptr))
2847 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2848 "MODE -b");
2849 return;
2850 }
2851
2852 /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */
2853 if (!(state->done & DONE_BANCLEAN)) {
2854 for (ban = state->chptr->banlist; ban; ban = ban->next)
2855 ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED);
2856 state->done |= DONE_BANCLEAN;
2857 }
2858
2859 /* remember the ban for the moment... */
2860 newban = state->banlist + (state->numbans++);
2861 newban->next = 0;
2862 newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
2863 | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
2864 set_ban_mask(newban, collapse(pretty_mask(t_str)));
2865 ircd_strncpy(newban->who, IsUser(state->sptr) ? cli_name(state->sptr) : "*", NICKLEN);
2866 newban->when = TStime();
2867 apply_ban(&state->chptr->banlist, newban, 0);
2868 }
2869
2870 /*
2871 * This is the bottom half of the ban processor
2872 */
2873 static void
mode_process_bans(struct ParseState * state)2874 mode_process_bans(struct ParseState *state)
2875 {
2876 struct Ban *ban, *newban, *prevban, *nextban;
2877 int count = 0;
2878 int len = 0;
2879 int banlen;
2880 int changed = 0;
2881
2882 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2883 count++;
2884 banlen = strlen(ban->banstr);
2885 len += banlen;
2886 nextban = ban->next;
2887
2888 if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) {
2889 if (prevban)
2890 prevban->next = 0; /* Break the list; ban isn't a real ban */
2891 else
2892 state->chptr->banlist = 0;
2893
2894 count--;
2895 len -= banlen;
2896
2897 continue;
2898 } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
2899 char *bandup;
2900 DupString(bandup, ban->banstr);
2901 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2902 bandup, 1);
2903
2904 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2905 if (prevban) /* clip it out of the list... */
2906 prevban->next = ban->next;
2907 else
2908 state->chptr->banlist = ban->next;
2909
2910 count--;
2911 len -= banlen;
2912 free_ban(ban);
2913
2914 changed++;
2915 continue; /* next ban; keep prevban like it is */
2916 } else
2917 ban->flags &= BAN_IPMASK; /* unset other flags */
2918 } else if (ban->flags & BAN_ADD) { /* adding a ban? */
2919 if (prevban)
2920 prevban->next = 0; /* Break the list; ban isn't a real ban */
2921 else
2922 state->chptr->banlist = 0;
2923
2924 /* If we're supposed to ignore it, do so. */
2925 if (ban->flags & BAN_OVERLAPPED &&
2926 !(state->flags & MODE_PARSE_BOUNCE)) {
2927 count--;
2928 len -= banlen;
2929 } else {
2930 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2931 !(state->mbuf->mb_dest & MODEBUF_DEST_OPMODE) &&
2932 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2933 count > feature_int(FEAT_MAXBANS))) {
2934 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2935 ban->banstr);
2936 count--;
2937 len -= banlen;
2938 } else {
2939 char *bandup;
2940 /* add the ban to the buffer */
2941 DupString(bandup, ban->banstr);
2942 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2943 bandup, 1);
2944
2945 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2946 newban = make_ban(ban->banstr);
2947 strcpy(newban->who, ban->who);
2948 newban->when = ban->when;
2949 newban->flags = ban->flags & BAN_IPMASK;
2950
2951 newban->next = state->chptr->banlist; /* and link it in */
2952 state->chptr->banlist = newban;
2953
2954 changed++;
2955 }
2956 }
2957 }
2958 }
2959
2960 prevban = ban;
2961 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2962
2963 if (changed) /* if we changed the ban list, we must invalidate the bans */
2964 mode_ban_invalidate(state->chptr);
2965 }
2966
2967 /*
2968 * Helper function to process client changes
2969 */
2970 static void
mode_parse_client(struct ParseState * state,int * flag_p)2971 mode_parse_client(struct ParseState *state, int *flag_p)
2972 {
2973 char *t_str;
2974 char *colon;
2975 struct Client *acptr;
2976 struct Membership *member;
2977 int oplevel = MAXOPLEVEL + 1;
2978 int req_oplevel;
2979 int i;
2980
2981 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2982 return;
2983
2984 if (state->parc <= 0) /* return if not enough args */
2985 return;
2986
2987 t_str = state->parv[state->args_used++]; /* grab arg */
2988 state->parc--;
2989 state->max_args--;
2990
2991 /* If they're not an oper, they can't change modes */
2992 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2993 send_notoper(state);
2994 return;
2995 }
2996
2997 if (MyUser(state->sptr)) {
2998 colon = strchr(t_str, ':');
2999 if (colon != NULL) {
3000 *colon++ = '\0';
3001 req_oplevel = atoi(colon);
3002 if (*flag_p == CHFL_VOICE || state->dir == MODE_DEL) {
3003 /* Ignore the colon and its argument. */
3004 } else if (!(state->flags & MODE_PARSE_FORCE)
3005 && state->member
3006 && (req_oplevel < OpLevel(state->member)
3007 || (req_oplevel == OpLevel(state->member)
3008 && OpLevel(state->member) < MAXOPLEVEL)
3009 || req_oplevel > MAXOPLEVEL)) {
3010 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3011 t_str, state->chptr->chname,
3012 OpLevel(state->member), req_oplevel, "op",
3013 OpLevel(state->member) == req_oplevel ? "the same" : "a higher");
3014 return;
3015 } else if (req_oplevel <= MAXOPLEVEL)
3016 oplevel = req_oplevel;
3017 }
3018 /* find client we're manipulating */
3019 acptr = find_chasing(state->sptr, t_str, NULL);
3020 } else {
3021 if (t_str[5] == ':') {
3022 t_str[5] = '\0';
3023 oplevel = atoi(t_str + 6);
3024 }
3025 acptr = findNUser(t_str);
3026 }
3027
3028 if (!acptr)
3029 return; /* find_chasing() already reported an error to the user */
3030
3031 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
3032 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
3033 state->cli_change[i].flag & flag_p[0]))
3034 break; /* found a slot */
3035
3036 /* If we are going to bounce this deop, mark the correct oplevel. */
3037 if (state->flags & MODE_PARSE_BOUNCE
3038 && state->dir == MODE_DEL
3039 && flag_p[0] == MODE_CHANOP
3040 && (member = find_member_link(state->chptr, acptr)))
3041 oplevel = OpLevel(member);
3042
3043 /* Store what we're doing to them */
3044 state->cli_change[i].flag = state->dir | flag_p[0];
3045 state->cli_change[i].oplevel = oplevel;
3046 state->cli_change[i].client = acptr;
3047 }
3048
3049 /*
3050 * Helper function to process the changed client list
3051 */
3052 static void
mode_process_clients(struct ParseState * state)3053 mode_process_clients(struct ParseState *state)
3054 {
3055 int i;
3056 struct Membership *member;
3057
3058 for (i = 0; state->cli_change[i].flag; i++) {
3059 assert(0 != state->cli_change[i].client);
3060
3061 /* look up member link */
3062 if (!(member = find_member_link(state->chptr,
3063 state->cli_change[i].client)) ||
3064 (MyUser(state->sptr) && IsZombie(member))) {
3065 if (MyUser(state->sptr))
3066 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
3067 cli_name(state->cli_change[i].client),
3068 state->chptr->chname);
3069 continue;
3070 }
3071
3072 if ((state->cli_change[i].flag & MODE_ADD &&
3073 (state->cli_change[i].flag & member->status)) ||
3074 (state->cli_change[i].flag & MODE_DEL &&
3075 !(state->cli_change[i].flag & member->status)))
3076 continue; /* no change made, don't do anything */
3077
3078 /* see if the deop is allowed */
3079 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3080 (MODE_DEL | MODE_CHANOP)) {
3081 /* prevent +k users from being deopped */
3082 if (IsChannelService(state->cli_change[i].client)) {
3083 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3084 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
3085 state->chptr,
3086 (IsServer(state->sptr) ? cli_name(state->sptr) :
3087 cli_name((cli_user(state->sptr))->server)));
3088
3089 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3090 send_reply(state->sptr, ERR_ISCHANSERVICE,
3091 cli_name(state->cli_change[i].client),
3092 state->chptr->chname);
3093 continue;
3094 }
3095 }
3096
3097 /* check deop for local user */
3098 if (MyUser(state->sptr)) {
3099
3100 /* don't allow local opers to be deopped on local channels */
3101 if (state->cli_change[i].client != state->sptr &&
3102 IsLocalChannel(state->chptr->chname) &&
3103 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
3104 send_reply(state->sptr, ERR_ISOPERLCHAN,
3105 cli_name(state->cli_change[i].client),
3106 state->chptr->chname);
3107 continue;
3108 }
3109
3110 /* Forbid deopping other members with an oplevel less than
3111 * one's own level, and other members with an oplevel the same
3112 * as one's own unless both are at MAXOPLEVEL. */
3113 if (state->sptr != state->cli_change[i].client
3114 && state->member
3115 && ((OpLevel(member) < OpLevel(state->member))
3116 || (OpLevel(member) == OpLevel(state->member)
3117 && OpLevel(member) < MAXOPLEVEL))) {
3118 int equal = (OpLevel(member) == OpLevel(state->member));
3119 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3120 cli_name(state->cli_change[i].client),
3121 state->chptr->chname,
3122 OpLevel(state->member), OpLevel(member),
3123 "deop", equal ? "the same" : "a higher");
3124 continue;
3125 }
3126 }
3127 }
3128
3129 /* set op-level of member being opped */
3130 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
3131 (MODE_ADD | MODE_CHANOP)) {
3132 /* If a valid oplevel was specified, use it.
3133 * Otherwise, if being opped by an outsider, get MAXOPLEVEL.
3134 * Otherwise, if not an apass channel, or state->member has
3135 * MAXOPLEVEL, get oplevel MAXOPLEVEL.
3136 * Otherwise, get state->member's oplevel+1.
3137 */
3138 if (state->cli_change[i].oplevel <= MAXOPLEVEL)
3139 SetOpLevel(member, state->cli_change[i].oplevel);
3140 else if (!state->member)
3141 SetOpLevel(member, MAXOPLEVEL);
3142 else if (OpLevel(state->member) >= MAXOPLEVEL)
3143 SetOpLevel(member, OpLevel(state->member));
3144 else
3145 SetOpLevel(member, OpLevel(state->member) + 1);
3146 }
3147
3148 /* actually effect the change */
3149 if (state->flags & MODE_PARSE_SET) {
3150 if (state->cli_change[i].flag & MODE_ADD) {
3151 if (IsDelayedJoin(member) && !IsZombie(member))
3152 RevealDelayedJoin(member);
3153 member->status |= (state->cli_change[i].flag &
3154 (MODE_CHANOP | MODE_VOICE));
3155 if (state->cli_change[i].flag & MODE_CHANOP)
3156 ClearDeopped(member);
3157 } else
3158 member->status &= ~(state->cli_change[i].flag &
3159 (MODE_CHANOP | MODE_VOICE));
3160 }
3161
3162 /* accumulate the change */
3163 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3164 state->cli_change[i].client,
3165 state->cli_change[i].oplevel);
3166 } /* for (i = 0; state->cli_change[i].flags; i++) */
3167 }
3168
3169 /*
3170 * Helper function to process the simple modes
3171 */
3172 static void
mode_parse_mode(struct ParseState * state,int * flag_p)3173 mode_parse_mode(struct ParseState *state, int *flag_p)
3174 {
3175 /* If they're not an oper, they can't change modes */
3176 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3177 send_notoper(state);
3178 return;
3179 }
3180
3181 if (!state->mbuf)
3182 return;
3183
3184 /* Local users are not permitted to change registration status */
3185 if (flag_p[0] == MODE_REGISTERED && !(state->flags & MODE_PARSE_FORCE) &&
3186 MyUser(state->sptr))
3187 return;
3188
3189 if (state->dir == MODE_ADD) {
3190 state->add |= flag_p[0];
3191 state->del &= ~flag_p[0];
3192
3193 if (flag_p[0] & MODE_SECRET) {
3194 state->add &= ~MODE_PRIVATE;
3195 state->del |= MODE_PRIVATE;
3196 } else if (flag_p[0] & MODE_PRIVATE) {
3197 state->add &= ~MODE_SECRET;
3198 state->del |= MODE_SECRET;
3199 }
3200 } else {
3201 state->add &= ~flag_p[0];
3202 state->del |= flag_p[0];
3203 }
3204
3205 assert(0 == (state->add & state->del));
3206 assert((MODE_SECRET | MODE_PRIVATE) !=
3207 (state->add & (MODE_SECRET | MODE_PRIVATE)));
3208 }
3209
3210 /**
3211 * This routine is intended to parse MODE or OPMODE commands and effect the
3212 * changes (or just build the bounce buffer).
3213 *
3214 * \param[out] mbuf Receives parsed representation of mode change.
3215 * \param[in] cptr Connection that sent the message to this server.
3216 * \param[in] sptr Original source of the message.
3217 * \param[in] chptr Channel whose modes are being changed.
3218 * \param[in] parc Number of valid strings in \a parv.
3219 * \param[in] parv Text arguments representing mode change, with the
3220 * zero'th element containing a string like "+m" or "-o".
3221 * \param[in] flags Set of bitwise MODE_PARSE_* flags.
3222 * \param[in] member If non-null, the channel member attempting to change the modes.
3223 */
3224 int
mode_parse(struct ModeBuf * mbuf,struct Client * cptr,struct Client * sptr,struct Channel * chptr,int parc,char * parv[],unsigned int flags,struct Membership * member)3225 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3226 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
3227 struct Membership* member)
3228 {
3229 static int chan_flags[] = {
3230 MODE_CHANOP, 'o',
3231 MODE_VOICE, 'v',
3232 MODE_PRIVATE, 'p',
3233 MODE_SECRET, 's',
3234 MODE_MODERATED, 'm',
3235 MODE_TOPICLIMIT, 't',
3236 MODE_INVITEONLY, 'i',
3237 MODE_NOPRIVMSGS, 'n',
3238 MODE_KEY, 'k',
3239 MODE_APASS, 'A',
3240 MODE_UPASS, 'U',
3241 MODE_REGISTERED, 'R',
3242 MODE_BAN, 'b',
3243 MODE_LIMIT, 'l',
3244 MODE_REGONLY, 'r',
3245 MODE_DELJOINS, 'D',
3246 MODE_NOCOLOR, 'c',
3247 MODE_NOCTCP, 'C',
3248 MODE_ADD, '+',
3249 MODE_DEL, '-',
3250 0x0, 0x0
3251 };
3252 int i;
3253 int *flag_p;
3254 unsigned int t_mode;
3255 char *modestr;
3256 struct ParseState state;
3257
3258 assert(0 != cptr);
3259 assert(0 != sptr);
3260 assert(0 != chptr);
3261 assert(0 != parc);
3262 assert(0 != parv);
3263
3264 state.mbuf = mbuf;
3265 state.cptr = cptr;
3266 state.sptr = sptr;
3267 state.chptr = chptr;
3268 state.member = member;
3269 state.parc = parc;
3270 state.parv = parv;
3271 state.flags = flags;
3272 state.dir = MODE_ADD;
3273 state.done = 0;
3274 state.add = 0;
3275 state.del = 0;
3276 state.args_used = 0;
3277 state.max_args = MAXMODEPARAMS;
3278 state.numbans = 0;
3279
3280 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3281 state.banlist[i].next = 0;
3282 state.banlist[i].who[0] = '\0';
3283 state.banlist[i].when = 0;
3284 state.banlist[i].flags = 0;
3285 state.cli_change[i].flag = 0;
3286 state.cli_change[i].client = 0;
3287 }
3288
3289 modestr = state.parv[state.args_used++];
3290 state.parc--;
3291
3292 while (*modestr) {
3293 for (; *modestr; modestr++) {
3294 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3295 if (flag_p[1] == *modestr)
3296 break;
3297
3298 if (!flag_p[0]) { /* didn't find it? complain and continue */
3299 if (MyUser(state.sptr))
3300 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3301 continue;
3302 }
3303
3304 switch (*modestr) {
3305 case '+': /* switch direction to MODE_ADD */
3306 case '-': /* switch direction to MODE_DEL */
3307 state.dir = flag_p[0];
3308 break;
3309
3310 case 'l': /* deal with limits */
3311 mode_parse_limit(&state, flag_p);
3312 break;
3313
3314 case 'k': /* deal with keys */
3315 mode_parse_key(&state, flag_p);
3316 break;
3317
3318 case 'A': /* deal with Admin passes */
3319 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3320 mode_parse_apass(&state, flag_p);
3321 break;
3322
3323 case 'U': /* deal with user passes */
3324 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3325 mode_parse_upass(&state, flag_p);
3326 break;
3327
3328 case 'b': /* deal with bans */
3329 mode_parse_ban(&state, flag_p);
3330 break;
3331
3332 case 'o': /* deal with ops/voice */
3333 case 'v':
3334 mode_parse_client(&state, flag_p);
3335 break;
3336
3337 default: /* deal with other modes */
3338 mode_parse_mode(&state, flag_p);
3339 break;
3340 } /* switch (*modestr) */
3341 } /* for (; *modestr; modestr++) */
3342
3343 if (state.flags & MODE_PARSE_BURST)
3344 break; /* don't interpret any more arguments */
3345
3346 if (state.parc > 0) { /* process next argument in string */
3347 modestr = state.parv[state.args_used++];
3348 state.parc--;
3349
3350 /* is it a TS? */
3351 if (IsServer(state.cptr) && !state.parc && IsDigit(*modestr)) {
3352 time_t recv_ts;
3353
3354 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3355 break; /* we're then going to bounce the mode! */
3356
3357 recv_ts = atoi(modestr);
3358
3359 if (recv_ts && recv_ts < state.chptr->creationtime)
3360 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3361 else if (recv_ts > state.chptr->creationtime) {
3362 struct Client *sserv;
3363
3364 /* Check whether the originating server has fully processed
3365 * the burst to it. */
3366 sserv = state.cptr;
3367 if (!IsServer(sserv))
3368 sserv = cli_user(sserv)->server;
3369 if (IsBurstOrBurstAck(sserv)) {
3370 /* This is a legal but unusual case; the source server
3371 * probably just has not processed the BURST for this
3372 * channel. It SHOULD wipe out all its modes soon, so
3373 * silently ignore the mode change rather than send a
3374 * bounce that could desync modes from our side (that
3375 * have already been sent).
3376 */
3377 state.mbuf->mb_add = 0;
3378 state.mbuf->mb_rem = 0;
3379 state.mbuf->mb_count = 0;
3380 return state.args_used;
3381 } else {
3382 /* Server is desynced; bounce the mode and deop the source
3383 * to fix it. */
3384 state.flags &= ~MODE_PARSE_SET;
3385 state.flags |= MODE_PARSE_BOUNCE;
3386 state.mbuf->mb_dest &= ~(MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK4);
3387 state.mbuf->mb_dest |= MODEBUF_DEST_BOUNCE | MODEBUF_DEST_HACK2;
3388 if (!IsServer(state.cptr))
3389 state.mbuf->mb_dest |= MODEBUF_DEST_DEOP;
3390 }
3391 }
3392
3393 break; /* break out of while loop */
3394 } else if (state.flags & MODE_PARSE_STRICT ||
3395 (MyUser(state.sptr) && state.max_args <= 0)) {
3396 state.parc++; /* we didn't actually gobble the argument */
3397 state.args_used--;
3398 break; /* break out of while loop */
3399 }
3400 }
3401 } /* while (*modestr) */
3402
3403 /*
3404 * the rest of the function finishes building resultant MODEs; if the
3405 * origin isn't a member or an oper, skip it.
3406 */
3407 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3408 return state.args_used; /* tell our parent how many args we gobbled */
3409
3410 t_mode = state.chptr->mode.mode;
3411
3412 if (state.del & t_mode) { /* delete any modes to be deleted... */
3413 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3414
3415 t_mode &= ~state.del;
3416 }
3417 if (state.add & ~t_mode) { /* add any modes to be added... */
3418 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3419
3420 t_mode |= state.add;
3421 }
3422
3423 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3424 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3425 !(t_mode & MODE_INVITEONLY))
3426 mode_invite_clear(state.chptr);
3427
3428 state.chptr->mode.mode = t_mode;
3429 }
3430
3431 if (state.flags & MODE_PARSE_WIPEOUT) {
3432 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3433 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3434 state.chptr->mode.limit);
3435 if (*state.chptr->mode.key && !(state.done & DONE_KEY_DEL))
3436 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3437 state.chptr->mode.key, 0);
3438 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS_DEL))
3439 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3440 state.chptr->mode.upass, 0);
3441 if (*state.chptr->mode.apass && !(state.done & DONE_APASS_DEL))
3442 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3443 state.chptr->mode.apass, 0);
3444 }
3445
3446 if (state.done & DONE_BANCLEAN) /* process bans */
3447 mode_process_bans(&state);
3448
3449 /* process client changes */
3450 if (state.cli_change[0].flag)
3451 mode_process_clients(&state);
3452
3453 return state.args_used; /* tell our parent how many args we gobbled */
3454 }
3455
3456 /*
3457 * Initialize a join buffer
3458 */
3459 void
joinbuf_init(struct JoinBuf * jbuf,struct Client * source,struct Client * connect,unsigned int type,char * comment,time_t create)3460 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3461 struct Client *connect, unsigned int type, char *comment,
3462 time_t create)
3463 {
3464 int i;
3465
3466 assert(0 != jbuf);
3467 assert(0 != source);
3468 assert(0 != connect);
3469
3470 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3471 jbuf->jb_connect = connect;
3472 jbuf->jb_type = type;
3473 jbuf->jb_comment = comment;
3474 jbuf->jb_create = create;
3475 jbuf->jb_count = 0;
3476 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3477 type == JOINBUF_TYPE_PART ||
3478 type == JOINBUF_TYPE_PARTALL) ?
3479 STARTJOINLEN : STARTCREATELEN) +
3480 (comment ? strlen(comment) + 2 : 0));
3481
3482 for (i = 0; i < MAXJOINARGS; i++)
3483 jbuf->jb_channels[i] = 0;
3484 }
3485
3486 /*
3487 * Add a channel to the join buffer
3488 */
3489 void
joinbuf_join(struct JoinBuf * jbuf,struct Channel * chan,unsigned int flags)3490 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3491 {
3492 unsigned int len;
3493 int is_local;
3494
3495 assert(0 != jbuf);
3496
3497 if (!chan) {
3498 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3499 return;
3500 }
3501
3502 is_local = IsLocalChannel(chan->chname);
3503
3504 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3505 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3506 struct Membership *member = find_member_link(chan, jbuf->jb_source);
3507 if (IsUserParting(member))
3508 return;
3509 SetUserParting(member);
3510
3511 /* Send notification to channel */
3512 if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
3513 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
3514 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3515 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3516 else if (MyUser(jbuf->jb_source))
3517 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3518 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3519 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3520 /* XXX: Shouldn't we send a PART here anyway? */
3521 /* to users on the channel? Why? From their POV, the user isn't on
3522 * the channel anymore anyway. We don't send to servers until below,
3523 * when we gang all the channel parts together. Note that this is
3524 * exactly the same logic, albeit somewhat more concise, as was in
3525 * the original m_part.c */
3526
3527 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3528 is_local) /* got to remove user here */
3529 remove_user_from_channel(jbuf->jb_source, chan);
3530 } else {
3531 int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
3532 : (flags & CHFL_CHANNEL_MANAGER) ? 0
3533 : 1;
3534 /* Add user to channel */
3535 if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
3536 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
3537 else
3538 add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
3539
3540 /* send JOIN notification to all servers (CREATE is sent later). */
3541 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
3542 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3543 "%H %Tu", chan, chan->creationtime);
3544
3545 if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
3546 /* Send the notification to the channel */
3547 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
3548
3549 /* send an op, too, if needed */
3550 if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
3551 sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source),
3552 CMD_MODE, chan, NULL, 0, "%H +o %C",
3553 chan, jbuf->jb_source);
3554 } else if (MyUser(jbuf->jb_source))
3555 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
3556 }
3557
3558 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3559 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
3560 return; /* don't send to remote */
3561
3562 /* figure out if channel name will cause buffer to be overflowed */
3563 len = chan ? strlen(chan->chname) + 1 : 2;
3564 if (jbuf->jb_strlen + len > BUFSIZE)
3565 joinbuf_flush(jbuf);
3566
3567 /* add channel to list of channels to send and update counts */
3568 jbuf->jb_channels[jbuf->jb_count++] = chan;
3569 jbuf->jb_strlen += len;
3570
3571 /* if we've used up all slots, flush */
3572 if (jbuf->jb_count >= MAXJOINARGS)
3573 joinbuf_flush(jbuf);
3574 }
3575
3576 /*
3577 * Flush the channel list to remote servers
3578 */
3579 int
joinbuf_flush(struct JoinBuf * jbuf)3580 joinbuf_flush(struct JoinBuf *jbuf)
3581 {
3582 char chanlist[BUFSIZE];
3583 int chanlist_i = 0;
3584 int i;
3585
3586 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3587 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3588 return 0; /* no joins to process */
3589
3590 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3591 build_string(chanlist, &chanlist_i,
3592 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3593 i == 0 ? '\0' : ',');
3594 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3595 /* Remove user from channel */
3596 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3597
3598 jbuf->jb_channels[i] = 0; /* mark slot empty */
3599 }
3600
3601 jbuf->jb_count = 0; /* reset base counters */
3602 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3603 STARTJOINLEN : STARTCREATELEN) +
3604 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3605
3606 /* and send the appropriate command */
3607 switch (jbuf->jb_type) {
3608 case JOINBUF_TYPE_CREATE:
3609 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3610 "%s %Tu", chanlist, jbuf->jb_create);
3611 break;
3612
3613 case JOINBUF_TYPE_PART:
3614 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3615 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
3616 jbuf->jb_comment);
3617 break;
3618 }
3619
3620 return 0;
3621 }
3622
3623 /* Returns TRUE (1) if client is invited, FALSE (0) if not */
IsInvited(struct Client * cptr,const void * chptr)3624 int IsInvited(struct Client* cptr, const void* chptr)
3625 {
3626 struct SLink *lp;
3627
3628 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
3629 if (lp->value.chptr == chptr)
3630 return 1;
3631 return 0;
3632 }
3633
3634 /* RevealDelayedJoin: sends a join for a hidden user */
3635
RevealDelayedJoin(struct Membership * member)3636 void RevealDelayedJoin(struct Membership *member)
3637 {
3638 ClearDelayedJoin(member);
3639 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
3640 member->channel);
3641 CheckDelayedJoins(member->channel);
3642 }
3643
3644 /* CheckDelayedJoins: checks and clear +d if necessary */
3645
CheckDelayedJoins(struct Channel * chan)3646 void CheckDelayedJoins(struct Channel *chan)
3647 {
3648 if ((chan->mode.mode & MODE_WASDELJOINS) && !find_delayed_joins(chan)) {
3649 chan->mode.mode &= ~MODE_WASDELJOINS;
3650 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0,
3651 "%H -d", chan);
3652 }
3653 }
3654
3655 /** Send a join for the user if (s)he is a hidden member of the channel.
3656 */
RevealDelayedJoinIfNeeded(struct Client * sptr,struct Channel * chptr)3657 void RevealDelayedJoinIfNeeded(struct Client *sptr, struct Channel *chptr)
3658 {
3659 struct Membership *member = find_member_link(chptr, sptr);
3660 if (member && IsDelayedJoin(member))
3661 RevealDelayedJoin(member);
3662 }
3663