Rework JOIN handler to make keys, apass, and upass consistent.
[ircu2.10.12-pk.git] / ircd / m_join.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_join.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 "gline.h"
87 #include "hash.h"
88 #include "ircd.h"
89 #include "ircd_chattr.h"
90 #include "ircd_features.h"
91 #include "ircd_log.h"
92 #include "ircd_reply.h"
93 #include "ircd_string.h"
94 #include "msg.h"
95 #include "numeric.h"
96 #include "numnicks.h"
97 #include "s_debug.h"
98 #include "s_user.h"
99 #include "send.h"
100
101 /* #include <assert.h> -- Now using assert in ircd_log.h */
102 #include <stdlib.h>
103 #include <string.h>
104
105 /*
106  * Helper function to find last 0 in a comma-separated list of
107  * channel names.
108  */
109 static char *
110 last0(char *chanlist)
111 {
112   char *p;
113
114   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
115     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
116       chanlist = p; /* we'll start parsing here */
117
118       if (!p[1]) /* hit the end */
119         break;
120
121       p++;
122     } else {
123       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
124         p++;
125
126       if (!p[0]) /* hit the end */
127         break;
128     }
129
130   return chanlist;
131 }
132
133 /*
134  * Helper function to perform a JOIN 0 if needed; returns 0 if channel
135  * name is not 0, else removes user from all channels and returns 1.
136  */
137 static int
138 join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
139       char *chan)
140 {
141   struct Membership *member;
142   struct JoinBuf part;
143
144   /* is it a JOIN 0? */
145   if (chan[0] != '0' || chan[1] != '\0')
146     return 0;
147   
148   joinbuf_join(join, 0, 0); /* join special channel 0 */
149
150   /* leave all channels */
151   joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
152                "Left all channels", 0);
153
154   while ((member = cli_user(sptr)->channel))
155     joinbuf_join(&part, member->channel,
156                  IsZombie(member) ? CHFL_ZOMBIE :
157                  IsDelayedJoin(member) ? CHFL_DELAYED :
158                  0);
159
160   joinbuf_flush(&part);
161
162   return 1;
163 }
164
165 /*
166  * m_join - generic message handler
167  */
168 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
169 {
170   struct Channel *chptr;
171   struct JoinBuf join;
172   struct JoinBuf create;
173   struct Gline *gline;
174   unsigned int flags;
175   char *p = 0;
176   char *chanlist;
177   char *name;
178   char *keys;
179
180   if (parc < 2 || *parv[1] == '\0')
181     return need_more_params(sptr, "JOIN");
182
183   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
184   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
185
186   chanlist = last0(parv[1]); /* find last "JOIN 0" */
187
188   keys = parv[2]; /* remember where keys are */
189
190   for (name = ircd_strtok(&p, chanlist, ","); name;
191        name = ircd_strtok(&p, 0, ",")) {
192     clean_channelname(name);
193
194     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
195       continue;
196
197     if (!IsChannelName(name))
198     {
199       /* bad channel name */
200       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
201       continue;
202     }
203
204     /* BADCHANed channel */
205     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
206         GlineIsActive(gline) && !IsAnOper(sptr)) {
207       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
208       continue;
209     }
210
211     if (!(chptr = FindChannel(name)))
212       flags = CHFL_CHANOP;
213     else if (find_member_link(chptr, sptr))
214       continue; /* already on channel */
215     else
216       flags = CHFL_DEOPPED;
217
218     /* disallow creating local channels */
219     if ((name[0] == '&') && !chptr && !feature_bool(FEAT_LOCAL_CHANNELS)) {
220         send_reply(sptr, ERR_NOSUCHCHANNEL, name);
221         continue;
222     }
223
224     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER) &&
225         !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
226       send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
227       break; /* no point processing the other channels */
228     }
229
230     if (chptr) {
231       char *key = 0;
232       int is_level0_op = 0;
233       int err = 0;
234
235       /* Check target change limits. */
236       if (check_target_limit(sptr, chptr, chptr->chname, 0))
237         continue;
238
239       /* If we have any more keys, take the first for this channel. */
240       if (!BadPtr(keys)
241           && (keys = strchr(key = keys, ',')))
242         *keys++ = '\0';
243
244       /* Check Apass/Upass -- since we only ever look at a single
245        * "key" per channel now, this hampers brute force attacks. */
246       if (!BadPtr(key) && !strcmp(key, chptr->mode.apass)) {
247         is_level0_op = 1;
248         flags = (flags & ~CHFL_DEOPPED) | CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
249       } else if (!BadPtr(key) && !strcmp(key, chptr->mode.upass)) {
250         is_level0_op = 1;
251         flags = (flags & ~CHFL_DEOPPED) | CHFL_CHANOP;
252       } else if (IsInvited(sptr, chptr)) {
253         /* Invites bypass these other checks. */
254       } else if (chptr->mode.mode & MODE_INVITEONLY)
255         err = ERR_INVITEONLYCHAN;
256       else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
257         err = ERR_CHANNELISFULL;
258       else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
259         err = ERR_NEEDREGGEDNICK;
260       else if (find_ban(sptr, chptr->banlist))
261         err = ERR_BANNEDFROMCHAN;
262       else if (*chptr->mode.key && strcmp(chptr->mode.key, key))
263         err = ERR_BADCHANNELKEY;
264
265       /* An oper with WALK_LCHAN privilege can join a local channel
266        * he otherwise could not join by using "OVERRIDE" as the key.
267        * This will generate a HACK(4) notice, but fails if the oper
268        * could normally join the channel. */
269       if (IsLocalChannel(chptr->chname)
270           && HasPriv(sptr, PRIV_WALK_LCHAN)
271           && !is_level0_op
272           && !BadPtr(key)
273           && !strcmp("OVERRIDE", key))
274       {
275         switch (err) {
276         case 0:
277           send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
278           continue;
279         case ERR_INVITEONLYCHAN: err = 'i'; break;
280         case ERR_CHANNELISFULL:  err = 'l'; break;
281         case ERR_BANNEDFROMCHAN: err = 'b'; break;
282         case ERR_BADCHANNELKEY:  err = 'k'; break;
283         case ERR_NEEDREGGEDNICK: err = 'r'; break;
284         default: err = '?'; break;
285         }
286         /* send accountability notice */
287         sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
288                              "(overriding +%c)", sptr, chptr, err);
289         err = 0;
290       }
291
292       /* Is there some reason the user may not join? */
293       if (err) {
294         send_reply(sptr, err, chptr->chname);
295         continue;
296       }
297
298       joinbuf_join(&join, chptr, flags);
299     } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
300       continue; /* couldn't get channel */
301     else if (check_target_limit(sptr, chptr, chptr->chname, 1))
302     {
303       /* Note: check_target_limit will only ever return 0 here */
304       chptr->members = 0;
305       destruct_channel(chptr); /* created it... */
306       continue;
307     }
308     else
309       joinbuf_join(&create, chptr, flags | CHFL_CHANNEL_MANAGER);
310
311     del_invite(sptr, chptr);
312
313     if (chptr->topic[0]) {
314       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
315       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
316                  chptr->topic_time);
317     }
318
319     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
320   }
321
322   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
323   joinbuf_flush(&create);
324
325   return 0;
326 }
327
328 /*
329  * ms_join - server message handler
330  */
331 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
332 {
333   struct Membership *member;
334   struct Channel *chptr;
335   struct JoinBuf join;
336   unsigned int flags;
337   time_t creation = 0;
338   char *p = 0;
339   char *chanlist;
340   char *name;
341
342   if (IsServer(sptr))
343   {
344     return protocol_violation(cptr,
345                               "%s tried to JOIN %s, duh!",
346                               cli_name(sptr),
347                               (parc < 2 || *parv[1] == '\0') ? "a channel" :
348                                                                parv[1]
349                               );
350   }
351
352   if (parc < 2 || *parv[1] == '\0')
353     return need_more_params(sptr, "JOIN");
354
355   if (parc > 2 && parv[2])
356     creation = atoi(parv[2]);
357
358   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
359
360   chanlist = last0(parv[1]); /* find last "JOIN 0" */
361
362   for (name = ircd_strtok(&p, chanlist, ","); name;
363        name = ircd_strtok(&p, 0, ",")) {
364
365     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
366       continue;
367
368     if (name[0] == '0' && name[1] == ':')
369     {
370       flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
371       name += 2;
372     }
373     else if (name[0] == '1' && name[1] == ':')
374     {
375       flags = CHFL_CHANOP;
376       name += 2;
377     }
378     else
379       flags = CHFL_DEOPPED;
380
381     if (IsLocalChannel(name) || !IsChannelName(name))
382     {
383       protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
384       continue;
385     }
386
387     if (!(chptr = FindChannel(name)))
388     {
389       /* No channel exists, so create one */
390       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
391       {
392         protocol_violation(sptr,"couldn't get channel %s for %s",
393                            name,cli_name(sptr));
394         continue;
395       }
396       flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
397
398       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
399       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
400     }
401     else { /* We have a valid channel? */
402       if ((member = find_member_link(chptr, sptr)))
403       {
404         /* It is impossible to get here --Run */
405         if (!IsZombie(member)) /* already on channel */
406           continue;
407
408         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
409         remove_user_from_channel(sptr, chptr);
410         chptr = FindChannel(name);
411       }
412       else
413         flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
414       /* Always copy the timestamp when it is older, that is the only way to
415          ensure network-wide synchronization of creation times. */
416       if (creation && creation < chptr->creationtime)
417         chptr->creationtime = creation;
418     }
419
420     joinbuf_join(&join, chptr, flags);
421   }
422
423   joinbuf_flush(&join); /* flush joins... */
424
425   return 0;
426 }