do not kick net riders if channel keys match; do kick them if they try
[ircu2.10.12-pk.git] / ircd / m_burst.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_burst.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_alloc.h"
89 #include "ircd_features.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "list.h"
93 #include "match.h"
94 #include "msg.h"
95 #include "numeric.h"
96 #include "numnicks.h"
97 #include "s_conf.h"
98 #include "s_misc.h"
99 #include "send.h"
100 #include "struct.h"
101 #include "support.h"
102 #include "ircd_snprintf.h"
103
104 #include <assert.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <ctype.h>
108
109 static int
110 netride_modes(int parc, char **parv, const char *curr_key)
111 {
112   char *modes = parv[0];
113   int result = 0;
114
115   assert(modes && modes[0] == '+');
116   while (*modes) {
117     switch (*modes++) {
118     case 'i':
119       result |= MODE_INVITEONLY;
120       break;
121     case 'k':
122       if (strcmp(curr_key, *++parv))
123         result |= MODE_KEY;
124       break;
125     case 'l':
126       ++parv;
127       break;
128     case 'r':
129       result |= MODE_REGONLY;
130       break;
131     }
132   }
133   return result;
134 }
135
136 /*
137  * ms_burst - server message handler
138  *
139  * --  by Run carlo@runaway.xs4all.nl  december 1995 till march 1997
140  *
141  * parv[0] = sender prefix
142  * parv[1] = channel name
143  * parv[2] = channel timestamp
144  * The meaning of the following parv[]'s depend on their first character:
145  * If parv[n] starts with a '+':
146  * Net burst, additive modes
147  *   parv[n] = <mode>
148  *   parv[n+1] = <param> (optional)
149  *   parv[n+2] = <param> (optional)
150  * If parv[n] starts with a '%', then n will be parc-1:
151  *   parv[n] = %<ban> <ban> <ban> ...
152  * If parv[n] starts with another character:
153  *   parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
154  *   where <mode> defines the mode and op-level
155  *   for nick and all following nicks until the
156  *   next <mode> field.
157  *   Digits in the <mode> field have of two meanings:
158  *   1) if it is the first field in this BURST message
159  *      that contains digits, and/or when a 'v' is
160  *      present in the <mode>:
161  *      The absolute value of the op-level.
162  *   2) if there are only digits in this field and
163  *      it is not the first field with digits:
164  *      An op-level increment relative to the previous
165  *      op-level.
166  *   First all modeless nicks must be emmitted,
167  *   then all combinations of modes without ops
168  *   (currently that is only 'v') followed by the same
169  *   series but then with ops (currently 'o','ov').
170  *
171  * Example:
172  * "A8 B #test 87654321 +ntkAl key secret 123 A8AAG,A8AAC:v,A8AAA:0,A8AAF:2,A8AAD,A8AAB:v1,A8AAE:1 :%ban1 ban2"
173  *
174  * <mode> list example:
175  *
176  * "xxx,sss:v,ttt,aaa:123,bbb,ccc:2,ddd,kkk:v2,lll:2,mmm"
177  *
178  * means
179  *
180  *  xxx         // first modeless nicks
181  *  sss +v      // then opless nicks
182  *  ttt +v      // no ":<mode>": everything stays the same
183  *  aaa -123    // first field with digit: absolute value
184  *  bbb -123
185  *  ccc -125    // only digits, not first field: increment
186  *  ddd -125
187  *  kkk -2 +v   // field with a 'v': absolute value
188  *  lll -4 +v   // only digits: increment
189  *  mmm -4 +v
190  *
191  * Anti net.ride code.
192  *
193  * When the channel already exist, and its TS is larger than
194  * the TS in the BURST message, then we cancel all existing modes.
195  * If its is smaller then the received BURST message is ignored.
196  * If it's equal, then the received modes are just added.
197  *
198  * BURST is also accepted outside a netburst now because it
199  * is sent upstream as reaction to a DESTRUCT message.  For
200  * these BURST messages it is possible that the listed channel
201  * members are already joined.
202  */
203 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
204 {
205   struct ModeBuf modebuf, *mbuf = 0;
206   struct Channel *chptr;
207   time_t timestamp;
208   struct Membership *member, *nmember;
209   struct SLink *lp, **lp_p;
210   unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
211   int param, nickpos = 0, banpos = 0;
212   char modestr[BUFSIZE], nickstr[BUFSIZE], banstr[BUFSIZE];
213
214   if (parc < 3)
215     return protocol_violation(sptr,"Too few parameters for BURST");
216
217   if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
218     return 0; /* can't create the channel? */
219
220   timestamp = atoi(parv[2]);
221
222   if (!chptr->creationtime || chptr->creationtime > timestamp) {
223     /*
224      * Kick local members if channel is +i or +k and our TS was larger
225      * than the burst TS (anti net.ride). The modes hack is here because
226      * we have to do this before mode_parse, as chptr may go away.
227      */
228     for (param = 3; param < parc; param++)
229     {
230       int check_modes;
231       if (parv[param][0] != '+')
232         continue;
233       check_modes = netride_modes(parc - param, parv + param, chptr->mode.key);
234       if (check_modes)
235       {
236         /* Clear any outstanding rogue invites */
237         mode_invite_clear(chptr);
238         for (member = chptr->members; member; member = nmember)
239         {
240           nmember = member->next_member;
241           if (!MyUser(member->user) || IsZombie(member))
242             continue;
243           /* Kick as netrider if key mismatch *or* remote channel is +i
244            * *or* remote channel is +r and user has no account.
245            */
246           if ((check_modes == MODE_REGONLY) && IsAccount(member->user))
247             continue;
248           sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Net Rider", chptr, member->user);
249           sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, 0, "%H %C :Net Rider", chptr, member->user);
250           make_zombie(member, member->user, &me, &me, chptr);
251         }
252       }
253       break;
254     }
255
256     /* If the channel had only locals, it went away by now. */
257     if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
258       return 0; /* can't create the channel? */
259   }
260
261   /* turn off burst joined flag */
262   for (member = chptr->members; member; member = member->next_member)
263     member->status &= ~(CHFL_BURST_JOINED|CHFL_BURST_ALREADY_OPPED|CHFL_BURST_ALREADY_VOICED);
264
265   if (!chptr->creationtime) /* mark channel as created during BURST */
266     chptr->mode.mode |= MODE_BURSTADDED;
267
268   /* new channel or an older one */
269   if (!chptr->creationtime || chptr->creationtime > timestamp) {
270     chptr->creationtime = timestamp;
271
272     modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
273                  MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
274     modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
275     chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
276                           MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
277                           MODE_NOPRIVMSGS | MODE_DELJOINS);
278
279     parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
280
281     /* mark bans for wipeout */
282     for (lp = chptr->banlist; lp; lp = lp->next)
283       lp->flags |= CHFL_BURST_BAN_WIPEOUT;
284
285     /* clear topic set by netrider (if set) */
286     if (*chptr->topic) {
287       *chptr->topic = '\0';
288       *chptr->topic_nick = '\0';
289       chptr->topic_time = 0;
290       sendcmdto_channel_butserv_butone(&me, CMD_TOPIC, chptr, NULL, 0,
291                                        "%H :%s", chptr, chptr->topic);
292     }
293   } else if (chptr->creationtime == timestamp) {
294     modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
295                  MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
296
297     parse_flags |= MODE_PARSE_SET; /* set new modes */
298   }
299
300   param = 3; /* parse parameters */
301   while (param < parc) {
302     switch (*parv[param]) {
303     case '+': /* parameter introduces a mode string */
304       param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
305                           parv + param, parse_flags, NULL);
306       break;
307
308     case '%': /* parameter contains bans */
309       if (parse_flags & MODE_PARSE_SET) {
310         char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
311         struct SLink *newban;
312
313         for (ban = ircd_strtok(&p, banlist, " "); ban;
314              ban = ircd_strtok(&p, 0, " ")) {
315           ban = collapse(pretty_mask(ban));
316
317             /*
318              * Yeah, we should probably do this elsewhere, and make it better
319              * and more general; this will hold until we get there, though.
320              * I dislike the current add_banid API... -Kev
321              *
322              * I wish there were a better algo. for this than the n^2 one
323              * shown below *sigh*
324              */
325           for (lp = chptr->banlist; lp; lp = lp->next) {
326             if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
327               ban = 0; /* don't add ban */
328               lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
329               break; /* new ban already existed; don't even repropagate */
330             } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
331                        !mmatch(lp->value.ban.banstr, ban)) {
332               ban = 0; /* don't add ban unless wiping out bans */
333               break; /* new ban is encompassed by an existing one; drop */
334             } else if (!mmatch(ban, lp->value.ban.banstr))
335               lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
336
337             if (!lp->next)
338               break;
339           }
340
341           if (ban) { /* add the new ban to the end of the list */
342             /* Build ban buffer */
343             if (!banpos) {
344               banstr[banpos++] = ' ';
345               banstr[banpos++] = ':';
346               banstr[banpos++] = '%';
347             } else
348               banstr[banpos++] = ' ';
349             for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
350               banstr[banpos++] = *ptr;
351
352             newban = make_link(); /* create new ban */
353
354             DupString(newban->value.ban.banstr, ban);
355
356             DupString(newban->value.ban.who, 
357                       cli_name(feature_bool(FEAT_HIS_BANWHO) ? &me : sptr));
358             newban->value.ban.when = TStime();
359
360             newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
361             if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
362               newban->flags |= CHFL_BAN_IPMASK;
363
364             newban->next = 0;
365             if (lp)
366               lp->next = newban; /* link it in */
367             else
368               chptr->banlist = newban;
369           }
370         }
371       } 
372       param++; /* look at next param */
373       break;
374
375     default: /* parameter contains clients */
376       {
377         struct Client *acptr;
378         char *nicklist = parv[param], *p = 0, *nick, *ptr;
379         int current_mode, last_mode, base_mode;
380         int oplevel = -1;       /* Mark first field with digits: means the same as 'o' (but with level). */
381         int last_oplevel = 0;
382         struct Membership* member;
383
384         base_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
385         if (chptr->mode.mode & MODE_DELJOINS)
386             base_mode |= CHFL_DELAYED;
387         current_mode = last_mode = base_mode;
388
389         for (nick = ircd_strtok(&p, nicklist, ","); nick;
390              nick = ircd_strtok(&p, 0, ",")) {
391
392           if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
393             *ptr++ = '\0';
394
395             if (parse_flags & MODE_PARSE_SET) {
396               int current_mode_needs_reset;
397               for (current_mode_needs_reset = 1; *ptr; ptr++) {
398                 if (*ptr == 'o') { /* has oper status */
399                   /*
400                    * An 'o' is pre-oplevel protocol, so this is only for
401                    * backwards compatibility.  Give them an op-level of
402                    * MAXOPLEVEL so everyone can deop them.
403                    */
404                   oplevel = MAXOPLEVEL;
405                   if (current_mode_needs_reset) {
406                     current_mode = base_mode;
407                     current_mode_needs_reset = 0;
408                   }
409                   current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
410                 }
411                 else if (*ptr == 'v') { /* has voice status */
412                   if (current_mode_needs_reset) {
413                     current_mode = base_mode;
414                     current_mode_needs_reset = 0;
415                   }
416                   current_mode = (current_mode & ~CHFL_DELAYED) | CHFL_VOICE;
417                   oplevel = -1; /* subsequential digits are an absolute op-level value. */
418                 }
419                 else if (isdigit(*ptr)) {
420                   int level_increment = 0;
421                   if (oplevel == -1) { /* op-level is absolute value? */
422                     if (current_mode_needs_reset) {
423                       current_mode = base_mode;
424                       current_mode_needs_reset = 0;
425                     }
426                     oplevel = 0;
427                   }
428                   current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
429                   do {
430                     level_increment = 10 * level_increment + *ptr++ - '0';
431                   } while(isdigit(*ptr));
432                   oplevel += level_increment;
433                 }
434                 else /* I don't recognize that flag */
435                   break; /* so stop processing */
436               }
437             }
438           }
439
440           if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
441             continue; /* ignore this client */
442
443           /* Build nick buffer */
444           nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
445           nickpos++;
446
447           for (ptr = nick; *ptr; ptr++) /* store nick */
448             nickstr[nickpos++] = *ptr;
449
450           if (current_mode != last_mode) { /* if mode changed... */
451             last_mode = current_mode;
452             last_oplevel = oplevel;
453
454             nickstr[nickpos++] = ':'; /* add a specifier */
455             if (current_mode & CHFL_VOICE)
456               nickstr[nickpos++] = 'v';
457             if (current_mode & CHFL_CHANOP)
458               nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel);
459           } else if (current_mode & CHFL_CHANOP && oplevel != last_oplevel) { /* if just op level changed... */
460             nickstr[nickpos++] = ':'; /* add a specifier */
461             nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel - last_oplevel);
462             last_oplevel = oplevel;
463           }
464
465           if (IsBurst(sptr) || !(member = find_member_link(chptr, acptr)))
466           {
467             add_user_to_channel(chptr, acptr, current_mode, oplevel);
468             if (!(current_mode & CHFL_DELAYED))
469               sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, 0, "%H", chptr);
470           }
471           else
472           {
473             /* The member was already joined (either by CREATE or JOIN).
474                Remember the current mode. */
475             if (member->status & CHFL_CHANOP)
476               member->status |= CHFL_BURST_ALREADY_OPPED;
477             if (member->status & CHFL_VOICE)
478               member->status |= CHFL_BURST_ALREADY_VOICED;
479             /* Synchronize with the burst. */
480             member->status |= CHFL_BURST_JOINED | (current_mode & (CHFL_CHANOP|CHFL_VOICE));
481             member->oplevel = oplevel;
482           }
483         }
484       }
485       param++;
486       break;
487     } /* switch (*parv[param]) */
488   } /* while (param < parc) */
489
490   nickstr[nickpos] = '\0';
491   banstr[banpos] = '\0';
492
493   if (parse_flags & MODE_PARSE_SET) {
494     modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
495     modestr[0] = modestr[1] ? ' ' : '\0';
496   } else
497     modestr[0] = '\0';
498
499   sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
500                         chptr->creationtime, modestr, nickstr, banstr);
501
502   if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
503     mode_ban_invalidate(chptr);
504
505   if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
506     /* first deal with channel members */
507     for (member = chptr->members; member; member = member->next_member) {
508       if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
509         if ((member->status & CHFL_CHANOP) && !(member->status & CHFL_BURST_ALREADY_OPPED))
510           modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
511         if ((member->status & CHFL_VOICE) && !(member->status & CHFL_BURST_ALREADY_VOICED))
512           modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
513       } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
514         if (member->status & CHFL_CHANOP)
515           modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
516         if (member->status & CHFL_VOICE)
517           modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
518         member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
519                           CHFL_DEOPPED);
520       }
521     }
522
523     /* Now deal with channel bans */
524     lp_p = &chptr->banlist;
525     while (*lp_p) {
526       lp = *lp_p;
527
528       /* remove ban from channel */
529       if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
530         modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
531                             lp->value.ban.banstr, 1); /* let it free banstr */
532
533         *lp_p = lp->next; /* clip out of list */
534         MyFree(lp->value.ban.who); /* free who */
535         free_link(lp); /* free ban */
536         continue;
537       } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
538         modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
539                             lp->value.ban.banstr, 0); /* don't free banstr */
540
541       lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
542       lp_p = &(*lp_p)->next;
543     }
544   }
545
546   return mbuf ? modebuf_flush(mbuf) : 0;
547 }