Forward port NICKLEN feature from 2.10.11.
[ircu2.10.12-pk.git] / ircd / m_nick.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_nick.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 "IPcheck.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_chattr.h"
89 #include "ircd_features.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "msg.h"
93 #include "numeric.h"
94 #include "numnicks.h"
95 #include "s_debug.h"
96 #include "s_misc.h"
97 #include "s_user.h"
98 #include "send.h"
99 #include "sys.h"
100
101 #include <assert.h>
102 #include <stdlib.h>
103 #include <string.h>
104
105  /*
106 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
107 * string for a nickname (note, the 'nick' may be modified in the process...)
108 *
109 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
110 *
111 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
112 *  anything outside the above set will terminate nickname.
113 * In addition, the first character cannot be '-' or a Digit.
114 *
115 * Note:
116 *  The '~'-character should be allowed, but a change should be global,
117 *  some confusion would result if only few servers allowed it...
118 */
119 static int do_nick_name(char* nick)
120 {
121   char* ch  = nick;
122   char* end = ch + NICKLEN;
123   assert(0 != ch);
124   
125   /* first character in [0..9-] */
126   if (*ch == '-' || IsDigit(*ch))
127     return 0;
128   for ( ; (ch < end) && *ch; ++ch)
129     if (!IsNickChar(*ch))
130       break;
131
132   *ch = '\0';
133
134   return (ch - nick);
135 }
136
137 /*
138  * m_nick - message handler for local clients
139  * parv[0] = sender prefix
140  * parv[1] = nickname
141  */
142 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
143 {
144   struct Client* acptr;
145   char           nick[NICKLEN + 2];
146   char*          arg;
147   char*          s;
148   const char*    client_name;
149
150   assert(0 != cptr);
151   assert(cptr == sptr);
152
153   /*
154    * parv[0] will be empty for clients connecting for the first time
155    */
156   client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
157
158   if (parc < 2) {
159     send_reply(sptr, ERR_NONICKNAMEGIVEN);
160     return 0;
161   }
162   /*
163    * Don't let them send make us send back a really long string of
164    * garbage
165    */
166   arg = parv[1];
167   if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
168     arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
169
170   if ((s = strchr(arg, '~')))
171     *s = '\0';
172
173   strcpy(nick, arg);
174
175   /*
176    * If do_nick_name() returns a null name then reject it.
177    */
178   if (0 == do_nick_name(nick)) {
179     send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
180     return 0;
181   }
182
183   /* 
184    * Check if this is a LOCAL user trying to use a reserved (Juped)
185    * nick, if so tell him that it's a nick in use...
186    */
187   if (isNickJuped(nick)) {
188     send_reply(sptr, ERR_NICKNAMEINUSE, nick);
189     return 0;                        /* NICK message ignored */
190   }
191
192   if (!(acptr = FindClient(nick))) {
193     /*
194      * No collisions, all clear...
195      */
196     return set_nick_name(cptr, sptr, nick, parc, parv);
197   }
198   if (IsServer(acptr)) {
199     send_reply(sptr, ERR_NICKNAMEINUSE, nick);
200     return 0;                        /* NICK message ignored */
201   }
202   /*
203    * If acptr == sptr, then we have a client doing a nick
204    * change between *equivalent* nicknames as far as server
205    * is concerned (user is changing the case of his/her
206    * nickname or somesuch)
207    */
208   if (acptr == sptr) {
209     /*
210      * If acptr == sptr, then we have a client doing a nick
211      * change between *equivalent* nicknames as far as server
212      * is concerned (user is changing the case of his/her
213      * nickname or somesuch)
214      */
215     if (0 != strcmp(cli_name(acptr), nick)) {
216       /*
217        * Allows change of case in his/her nick
218        */
219       return set_nick_name(cptr, sptr, nick, parc, parv);
220     }
221     /*
222      * This is just ':old NICK old' type thing.
223      * Just forget the whole thing here. There is
224      * no point forwarding it to anywhere,
225      * especially since servers prior to this
226      * version would treat it as nick collision.
227      */
228     return 0;
229   }
230   /*
231    * Note: From this point forward it can be assumed that
232    * acptr != sptr (point to different client structures).
233    */
234   assert(acptr != sptr);
235   /*
236    * If the older one is "non-person", the new entry is just
237    * allowed to overwrite it. Just silently drop non-person,
238    * and proceed with the nick. This should take care of the
239    * "dormant nick" way of generating collisions...
240    *
241    * XXX - hmmm can this happen after one is registered?
242    *
243    * Yes, client 1 connects to IRC and registers, client 2 connects and
244    * sends "NICK foo" but doesn't send anything more.  client 1 now does
245    * /nick foo, they should succeed and client 2 gets disconnected with
246    * the message below.
247    */
248   if (IsUnknown(acptr) && MyConnect(acptr)) {
249     ++ServerStats->is_ref;
250     IPcheck_connect_fail(cli_ip(acptr));
251     exit_client(cptr, acptr, &me, "Overridden by other sign on");
252     return set_nick_name(cptr, sptr, nick, parc, parv);
253   }
254   /*
255    * NICK is coming from local client connection. Just
256    * send error reply and ignore the command.
257    */
258   send_reply(sptr, ERR_NICKNAMEINUSE, nick);
259   return 0;                        /* NICK message ignored */
260 }
261
262
263 /*
264  * ms_nick - server message handler for nicks
265  * parv[0] = sender prefix
266  * parv[1] = nickname
267  *
268  * If from server, source is client:
269  *   parv[2] = timestamp
270  *
271  * Source is server:
272  *   parv[2] = hopcount
273  *   parv[3] = timestamp
274  *   parv[4] = username
275  *   parv[5] = hostname
276  *   parv[6] = umode (optional)
277  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
278  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
279  *   parv[parc-1] = info
280  *   parv[0] = server
281  */
282 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
283 {
284   struct Client *acptr;
285   char nick[NICKLEN + 2];
286   time_t lastnick = 0;
287   int differ = 1;
288   const char *type;
289
290   assert(0 != cptr);
291   assert(0 != sptr);
292   assert(IsServer(cptr));
293
294   if ((IsServer(sptr) && parc < 8) || parc < 3)
295   {
296     sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
297                          parv[1], cptr);
298     return need_more_params(sptr, "NICK");
299   }
300
301   ircd_strncpy(nick, parv[1], NICKLEN);
302   nick[NICKLEN] = '\0';
303
304   if (IsServer(sptr))
305   {
306     lastnick = atoi(parv[3]);
307     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr)) 
308       cli_serv(sptr)->lag = TStime() - lastnick;
309   }
310   else
311   {
312     lastnick = atoi(parv[2]); 
313     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
314       cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
315   }
316   /*
317    * If do_nick_name() returns a null name OR if the server sent a nick
318    * name and do_nick_name() changed it in some way (due to rules of nick
319    * creation) then reject it. If from a server and we reject it,
320    * and KILL it. -avalon 4/4/92
321    */
322   if (!do_nick_name(nick) || strcmp(nick, parv[1]))
323   {
324     send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
325     
326     ++ServerStats->is_kill;
327     sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
328                          parv[0], cptr);
329     sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
330                   IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
331                   nick, cli_name(cptr));
332     if (!IsServer(sptr))
333     {
334       /*
335        * bad nick _change_
336        */
337       sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
338                             parv[0], cli_name(&me), cli_name(cptr), parv[0],
339                             cli_user(sptr) ? cli_username(sptr) : "",
340                             cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
341                             cli_name(cptr));
342     }
343     return 0;
344   }
345   /* Check against nick name collisions. */
346   if ((acptr = FindClient(nick)) == NULL)
347     /* No collisions, all clear... */
348     return set_nick_name(cptr, sptr, nick, parc, parv);
349
350   /*
351    * If acptr == sptr, then we have a client doing a nick
352    * change between *equivalent* nicknames as far as server
353    * is concerned (user is changing the case of his/her
354    * nickname or somesuch)
355    */
356   if (acptr == sptr)
357   {
358     if (strcmp(cli_name(acptr), nick) != 0)
359       /* Allows change of case in his/her nick */
360       return set_nick_name(cptr, sptr, nick, parc, parv);
361     else
362       /* Setting their nick to what it already is? Ignore it. */
363       return 0;
364   }
365   /* now we know we have a real collision. */
366   /*
367    * Note: From this point forward it can be assumed that
368    * acptr != sptr (point to different client structures).
369    */
370   assert(acptr != sptr);
371   /*
372    * If the older one is "non-person", the new entry is just
373    * allowed to overwrite it. Just silently drop non-person,
374    * and proceed with the nick. This should take care of the
375    * "dormant nick" way of generating collisions...
376    */
377   if (IsUnknown(acptr) && MyConnect(acptr))
378   {
379     ServerStats->is_ref++;
380     IPcheck_connect_fail(cli_ip(acptr));
381     exit_client(cptr, acptr, &me, "Overridden by other sign on");
382     return set_nick_name(cptr, sptr, nick, parc, parv);
383   }
384   /*
385    * Decide, we really have a nick collision and deal with it
386    */
387   /*
388    * NICK was coming from a server connection.
389    * This means we have a race condition (two users signing on
390    * at the same time), or two net fragments reconnecting with the same nick.
391    * The latter can happen because two different users connected
392    * or because one and the same user switched server during a net break.
393    * If the TimeStamps are equal, we kill both (or only 'new'
394    * if it was a ":server NICK new ...").
395    * Otherwise we kill the youngest when user@host differ,
396    * or the oldest when they are the same.
397    * We treat user and ~user as different, because if it wasn't
398    * a faked ~user the AUTH wouldn't have added the '~'.
399    * --Run
400    *
401    */
402   if (IsServer(sptr))
403   {
404     /*
405      * A new NICK being introduced by a neighbouring
406      * server (e.g. message type ":server NICK new ..." received)
407      *
408      * compare IP address and username
409      */
410     differ =  (cli_ip(acptr).s_addr != htonl(base64toint(parv[parc - 3]))) ||
411               (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
412     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
413                          "%C %Tu (%s user@host))", acptr, cli_from(acptr),
414                          cli_lastnick(acptr), cptr, lastnick,
415                          differ ? "Different" : "Same");
416   }
417   else
418   {
419     /*
420      * A NICK change has collided (e.g. message type ":old NICK new").
421      *
422      * compare IP address and username
423      */
424     differ =  (cli_ip(acptr).s_addr != cli_ip(sptr).s_addr) ||
425               (0 != ircd_strcmp(cli_user(acptr)->username, cli_user(sptr)->username));              
426     sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
427                          "%C (%C %Tu <- %C %Tu)", sptr, acptr, cli_from(acptr),
428                          cli_lastnick(acptr), cptr, lastnick);
429   }
430   type = differ ? "older nick overruled" : "nick collision from same user@host";
431   /*
432    * Now remove (kill) the nick on our side if it is the youngest.
433    * If no timestamp was received, we ignore the incoming nick
434    * (and expect a KILL for our legit nick soon ):
435    * When the timestamps are equal we kill both nicks. --Run
436    * acptr->from != cptr should *always* be true (?).
437    *
438    * This exits the client sending the NICK message
439    */
440   if ((differ && lastnick >= cli_lastnick(acptr)) ||
441       (!differ && lastnick <= cli_lastnick(acptr)))
442   {
443     /* We need to bounce this kill straight back... Although the nick message
444      * for acptr is probably waiting in their recvq from me, its also possible
445      * that sptr will change their nick on cptr before cptr receives the
446      * nick message for acptr, which would leave acptr and sptr both alive
447      * on cptr, but only acptr alive on me, i.e. desync. This extra kill
448      * message has been absent for a while in ircu although it was a major
449      * problem when it was tried on efnet, so I don't know how big an issue it
450      * is. Probably best that this be left here, anyway...
451      */
452     ServerStats->is_kill++;
453     sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
454                   nick, cli_name(&me), type);
455     /* But if this was a nick change and not a nick introduction,
456      * we also need to ensure that we remove our local state
457      * record of the original client... Also, the rest of the
458      * net should be informed...
459      */
460     if (!IsServer(sptr))
461     {
462       assert(!MyConnect(sptr));
463       /* Inform the rest of the net... */
464       sendcmdto_serv_butone(&me, CMD_KILL, cptr, "%s :%s (%s)",
465                             nick, cli_name(&me), type);
466       /* Don't go sending off a QUIT message... */
467       SetFlag(sptr, FLAG_KILLED);
468       /* Remove them locally. */
469       exit_client_msg(cptr, sptr, &me,
470                       "Killed (%s (%s))",
471                       feature_str(FEAT_HIS_SERVERNAME), type);
472       /*
473        * We have killed sptr off, zero out it's pointer so if it's used
474        * again we'll know about it --Bleep
475        */
476       sptr = NULL;
477     }
478     /* If the timestamps differ and we just killed sptr, we don't need to kill
479      * acptr as well.
480      */
481     if (lastnick != cli_lastnick(acptr))
482       return 0;
483   }
484   /* Tell acptr why we are killing it. */
485   send_reply(acptr, ERR_NICKCOLLISION, nick);
486
487   ServerStats->is_kill++;
488   SetFlag(acptr, FLAG_KILLED);
489   /*
490    * This exits the client we had before getting the NICK message
491    */
492   sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s"
493                         " (%s)", acptr, feature_str(FEAT_HIS_SERVERNAME),
494                         type);
495   exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
496                   feature_str(FEAT_HIS_SERVERNAME), type);
497   if (lastnick == cli_lastnick(acptr))
498     return 0;
499   if (sptr == NULL)
500     return 0;
501   return set_nick_name(cptr, sptr, nick, parc, parv);
502 }