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