Rip out clean_channelname() and reject invalid names where appropriate.
[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 #include "config.h"
27
28 #include "channel.h"
29 #include "client.h"
30 #include "gline.h"
31 #include "hash.h"
32 #include "ircd.h"
33 #include "ircd_chattr.h"
34 #include "ircd_features.h"
35 #include "ircd_log.h"
36 #include "ircd_reply.h"
37 #include "ircd_string.h"
38 #include "msg.h"
39 #include "numeric.h"
40 #include "numnicks.h"
41 #include "s_debug.h"
42 #include "s_user.h"
43 #include "send.h"
44
45 /* #include <assert.h> -- Now using assert in ircd_log.h */
46 #include <stdlib.h>
47 #include <string.h>
48
49 /** Searches for and handles a 0 in a join list.
50  * @param[in] cptr Client that sent us the message.
51  * @param[in] sptr Original source of message.
52  * @param[in] chanlist List of channels to join.
53  * @return First token in \a chanlist after the final 0 entry, which
54  * may be its nul terminator (if the final entry is a 0 entry).
55  */
56 static char *
57 last0(struct Client *cptr, struct Client *sptr, char *chanlist)
58 {
59   char *p;
60   int join0 = 0;
61
62   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
63     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
64       if (*++p == ',')
65         p++;
66       chanlist = p;
67       join0 = 1;
68     } else {
69       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
70         p++;
71
72       if (!p[0]) /* hit the end */
73         break;
74     }
75
76   if (join0) {
77     struct JoinBuf part;
78     struct Membership *member;
79
80     joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
81                  "Left all channels", 0);
82
83     joinbuf_join(&part, 0, 0);
84
85     while ((member = cli_user(sptr)->channel))
86       joinbuf_join(&part, member->channel,
87                    IsZombie(member) ? CHFL_ZOMBIE :
88                    IsDelayedJoin(member) ? CHFL_DELAYED :
89                    0);
90
91     joinbuf_flush(&part);
92   }
93
94   return chanlist;
95 }
96
97 /** Handle a JOIN message from a client connection.
98  * See @ref m_functions for discussion of the arguments.
99  * @param[in] cptr Client that sent us the message.
100  * @param[in] sptr Original source of message.
101  * @param[in] parc Number of arguments.
102  * @param[in] parv Argument vector.
103  */
104 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
105 {
106   struct Channel *chptr;
107   struct JoinBuf join;
108   struct JoinBuf create;
109   struct Gline *gline;
110   char *p = 0;
111   char *chanlist;
112   char *name;
113   char *keys;
114
115   if (parc < 2 || *parv[1] == '\0')
116     return need_more_params(sptr, "JOIN");
117
118   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
119   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
120
121   chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
122
123   keys = parv[2]; /* remember where keys are */
124
125   for (name = ircd_strtok(&p, chanlist, ","); name;
126        name = ircd_strtok(&p, 0, ",")) {
127     char *key = 0;
128
129     /* If we have any more keys, take the first for this channel. */
130     if (!BadPtr(keys)
131         && (keys = strchr(key = keys, ',')))
132       *keys++ = '\0';
133
134     /* Empty keys are the same as no keys. */
135     if (key && !key[0])
136       key = 0;
137
138     if (!IsChannelName(name) || !strIsIrcCh(name))
139     {
140       /* bad channel name */
141       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
142       continue;
143     }
144
145     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER)
146         && !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
147       send_reply(sptr, ERR_TOOMANYCHANNELS, name);
148       break; /* no point processing the other channels */
149     }
150
151     /* BADCHANed channel */
152     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
153         GlineIsActive(gline) && !IsAnOper(sptr)) {
154       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
155       continue;
156     }
157
158     if (!(chptr = FindChannel(name))) {
159       if ((name[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) {
160         send_reply(sptr, ERR_NOSUCHCHANNEL, name);
161         continue;
162       }
163
164       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
165         continue;
166
167       /* Try to add the new channel as a recent target for the user. */
168       if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
169         chptr->members = 0;
170         destruct_channel(chptr);
171         continue;
172       }
173
174       joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
175     } else if (find_member_link(chptr, sptr)) {
176       continue; /* already on channel */
177     } else if (check_target_limit(sptr, chptr, chptr->chname, 0)) {
178       continue;
179     } else {
180       int flags = CHFL_DEOPPED;
181       int err = 0;
182
183       /* Check target change limits. */
184
185       /* Check Apass/Upass -- since we only ever look at a single
186        * "key" per channel now, this hampers brute force attacks. */
187       if (key && !strcmp(key, chptr->mode.apass))
188         flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
189       else if (key && !strcmp(key, chptr->mode.upass))
190         flags = CHFL_CHANOP;
191       else if (IsInvited(sptr, chptr)) {
192         /* Invites bypass these other checks. */
193       } else if (chptr->mode.mode & MODE_INVITEONLY)
194         err = ERR_INVITEONLYCHAN;
195       else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
196         err = ERR_CHANNELISFULL;
197       else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
198         err = ERR_NEEDREGGEDNICK;
199       else if (find_ban(sptr, chptr->banlist))
200         err = ERR_BANNEDFROMCHAN;
201       else if (*chptr->mode.key && (!key || strcmp(key, chptr->mode.key)))
202         err = ERR_BADCHANNELKEY;
203
204       /* An oper with WALK_LCHAN privilege can join a local channel
205        * he otherwise could not join by using "OVERRIDE" as the key.
206        * This will generate a HACK(4) notice, but fails if the oper
207        * could normally join the channel. */
208       if (IsLocalChannel(chptr->chname)
209           && HasPriv(sptr, PRIV_WALK_LCHAN)
210           && !(flags & CHFL_CHANOP)
211           && key && !strcmp(key, "OVERRIDE")
212           && strcmp(chptr->mode.key, "OVERRIDE"))
213       {
214         switch (err) {
215         case 0:
216           send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
217           continue;
218         case ERR_INVITEONLYCHAN: err = 'i'; break;
219         case ERR_CHANNELISFULL:  err = 'l'; break;
220         case ERR_BANNEDFROMCHAN: err = 'b'; break;
221         case ERR_BADCHANNELKEY:  err = 'k'; break;
222         case ERR_NEEDREGGEDNICK: err = 'r'; break;
223         default: err = '?'; break;
224         }
225         /* send accountability notice */
226         sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
227                              "(overriding +%c)", sptr, chptr, err);
228         err = 0;
229       }
230
231       /* Is there some reason the user may not join? */
232       if (err) {
233         send_reply(sptr, err, chptr->chname);
234         continue;
235       }
236
237       joinbuf_join(&join, chptr, flags);
238     }
239
240     del_invite(sptr, chptr);
241
242     if (chptr->topic[0]) {
243       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
244       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
245                  chptr->topic_time);
246     }
247
248     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
249   }
250
251   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
252   joinbuf_flush(&create);
253
254   return 0;
255 }
256
257 /** Handle a JOIN message from a server connection.
258  * See @ref m_functions for discussion of the arguments.
259  * @param[in] cptr Client that sent us the message.
260  * @param[in] sptr Original source of message.
261  * @param[in] parc Number of arguments.
262  * @param[in] parv Argument vector.
263  */
264 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
265 {
266   struct Membership *member;
267   struct Channel *chptr;
268   struct JoinBuf join;
269   unsigned int flags;
270   time_t creation = 0;
271   char *p = 0;
272   char *chanlist;
273   char *name;
274
275   if (IsServer(sptr))
276   {
277     return protocol_violation(cptr,
278                               "%s tried to JOIN %s, duh!",
279                               cli_name(sptr),
280                               (parc < 2 || *parv[1] == '\0') ? "a channel" :
281                                                                parv[1]
282                               );
283   }
284
285   if (parc < 2 || *parv[1] == '\0')
286     return need_more_params(sptr, "JOIN");
287
288   if (parc > 2 && parv[2])
289     creation = atoi(parv[2]);
290
291   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
292
293   chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
294
295   for (name = ircd_strtok(&p, chanlist, ","); name;
296        name = ircd_strtok(&p, 0, ",")) {
297
298     if (name[0] == '0' && name[1] == ':')
299     {
300       flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
301       name += 2;
302     }
303     else if (name[0] == '1' && name[1] == ':')
304     {
305       flags = CHFL_CHANOP;
306       name += 2;
307     }
308     else
309       flags = CHFL_DEOPPED;
310
311     if (IsLocalChannel(name) || !IsChannelName(name))
312     {
313       protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
314       continue;
315     }
316
317     if (!(chptr = FindChannel(name)))
318     {
319       /* No channel exists, so create one */
320       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
321       {
322         protocol_violation(sptr,"couldn't get channel %s for %s",
323                            name,cli_name(sptr));
324         continue;
325       }
326       flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
327
328       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
329       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
330     }
331     else { /* We have a valid channel? */
332       if ((member = find_member_link(chptr, sptr)))
333       {
334         /* It is impossible to get here --Run */
335         if (!IsZombie(member)) /* already on channel */
336           continue;
337
338         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
339         remove_user_from_channel(sptr, chptr);
340         chptr = FindChannel(name);
341       }
342       else
343         flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
344       /* Always copy the timestamp when it is older, that is the only way to
345          ensure network-wide synchronization of creation times. */
346       if (creation && creation < chptr->creationtime)
347         chptr->creationtime = creation;
348     }
349
350     joinbuf_join(&join, chptr, flags);
351   }
352
353   joinbuf_flush(&join); /* flush joins... */
354
355   return 0;
356 }