Author: Isomer <isomer@coders.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
254   assert(0 != cptr);
255   assert(0 != sptr);
256   assert(IsServer(cptr));
257
258   if ((IsServer(sptr) && parc < 8) || parc < 3) {
259     sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
260                          parv[1], cptr);
261     return need_more_params(sptr, "NICK");
262   }
263
264   ircd_strncpy(nick, parv[1], NICKLEN);
265   nick[NICKLEN] = '\0';
266
267   if (IsServer(sptr)) {
268     lastnick = atoi(parv[3]);
269     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr)) 
270       cli_serv(sptr)->lag = TStime() - lastnick;
271   }
272   else {
273     lastnick = atoi(parv[2]); 
274     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
275       cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
276   }
277   /*
278    * If do_nick_name() returns a null name OR if the server sent a nick
279    * name and do_nick_name() changed it in some way (due to rules of nick
280    * creation) then reject it. If from a server and we reject it,
281    * and KILL it. -avalon 4/4/92
282    */
283   if (0 == do_nick_name(nick) || 0 != strcmp(nick, parv[1])) {
284     send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
285
286     ++ServerStats->is_kill;
287     sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
288                          parv[0], cptr);
289     sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
290                   IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
291                   nick, cli_name(cptr));
292     if (!IsServer(sptr)) {
293       /*
294        * bad nick _change_
295        */
296       sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
297                             parv[0], cli_name(&me), cli_name(cptr), parv[0],
298                             cli_user(sptr) ? cli_username(sptr) : "",
299                             cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
300                             cli_name(cptr));
301     }
302     return 0;
303   }
304   /*
305    * Check against nick name collisions.
306    *
307    * Put this 'if' here so that the nesting goes nicely on the screen :)
308    * We check against server name list before determining if the nickname
309    * is present in the nicklist (due to the way the below for loop is
310    * constructed). -avalon
311    */
312    
313   acptr = FindClient(nick);
314   if (!acptr) {
315     /*
316      * No collisions, all clear...
317      */
318     return set_nick_name(cptr, sptr, nick, parc, parv);
319   }
320   assert(0 != acptr);
321
322   if (IsServer(acptr)) { /* shouldn't even happen, actually */
323     /*
324      * We have a nickname trying to use the same name as
325      * a server. Send out a nick collision KILL to remove
326      * the nickname. As long as only a KILL is sent out,
327      * there is no danger of the server being disconnected.
328      * Ultimate way to jupiter a nick ? >;-). -avalon
329      */
330     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C(%C <- %C)", sptr,
331                          cli_from(acptr), cptr);
332     ++ServerStats->is_kill;
333
334     sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s <- %s)", sptr, cli_name(&me),
335                   cli_name(cli_from(acptr)), cli_name(cptr));
336
337     cli_flags(sptr) |= FLAGS_KILLED;
338     /*
339      * if sptr is a server it is exited here, nothing else to do
340      */
341     return exit_client_msg(cptr, sptr, &me,
342                            "Killed (" HEAD_IN_SAND_SERVERNAME " (%s <- %s))",
343                            cli_name(cli_from(acptr)), cli_name(cptr));
344   }
345
346   /*
347    * If acptr == sptr, then we have a client doing a nick
348    * change between *equivalent* nicknames as far as server
349    * is concerned (user is changing the case of his/her
350    * nickname or somesuch)
351    */
352   if (acptr == sptr) {
353     if (strcmp(cli_name(acptr), nick) != 0)
354       /*
355        * Allows change of case in his/her nick
356        */
357       return set_nick_name(cptr, sptr, nick, parc, parv);
358     else
359       /*
360        * This is just ':old NICK old' type thing.
361        * Just forget the whole thing here. There is
362        * no point forwarding it to anywhere,
363        * especially since servers prior to this
364        * version would treat it as nick collision.
365        */
366       return 0;                        /* NICK Message ignored */
367   }
368
369   /*
370    * Note: From this point forward it can be assumed that
371    * acptr != sptr (point to different client structures).
372    */
373   assert(acptr != sptr);
374   /*
375    * If the older one is "non-person", the new entry is just
376    * allowed to overwrite it. Just silently drop non-person,
377    * and proceed with the nick. This should take care of the
378    * "dormant nick" way of generating collisions...
379    */
380   if (IsUnknown(acptr) && MyConnect(acptr)) {
381     ++ServerStats->is_ref;
382     IPcheck_connect_fail(cli_ip(acptr));
383     exit_client(cptr, acptr, &me, "Overridden by other sign on");
384     return set_nick_name(cptr, sptr, nick, parc, parv);
385   }
386   /*
387    * Decide, we really have a nick collision and deal with it
388    */
389   /*
390    * NICK was coming from a server connection.
391    * This means we have a race condition (two users signing on
392    * at the same time), or two net fragments reconnecting with the same nick.
393    * The latter can happen because two different users connected
394    * or because one and the same user switched server during a net break.
395    * If the TimeStamps are equal, we kill both (or only 'new'
396    * if it was a ":server NICK new ...").
397    * Otherwise we kill the youngest when user@host differ,
398    * or the oldest when they are the same.
399    * We treat user and ~user as different, because if it wasn't
400    * a faked ~user the AUTH wouldn't have added the '~'.
401    * --Run
402    *
403    */
404   if (IsServer(sptr)) {
405     /*
406      * A new NICK being introduced by a neighbouring
407      * server (e.g. message type ":server NICK new ..." received)
408      *
409      * compare IP address and username
410      */
411     differ =  (cli_ip(acptr).s_addr != htonl(base64toint(parv[parc - 3]))) ||
412               (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
413     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
414                          "%C %Tu (%s user@host))", acptr, cli_from(acptr),
415                          cli_lastnick(acptr), cptr, lastnick,
416                          differ ? "Different" : "Same");
417   }
418   else {
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   /*
431    * Now remove (kill) the nick on our side if it is the youngest.
432    * If no timestamp was received, we ignore the incoming nick
433    * (and expect a KILL for our legit nick soon ):
434    * When the timestamps are equal we kill both nicks. --Run
435    * acptr->from != cptr should *always* be true (?).
436    *
437    * This exits the client sending the NICK message
438    */
439   if (cli_from(acptr) != cptr) {
440     if ((differ && lastnick >= cli_lastnick(acptr)) ||
441         (!differ && lastnick <= cli_lastnick(acptr))) {
442       if (!IsServer(sptr)) {
443         ++ServerStats->is_kill;
444         sendcmdto_serv_butone(&me, CMD_KILL, sptr, "%C :%s (Nick collision)",
445                               sptr, cli_name(&me));
446         assert(!MyConnect(sptr));
447
448         cli_flags(sptr) |= FLAGS_KILLED;
449         exit_client(cptr, sptr, &me,
450                     "Killed (" HEAD_IN_SAND_SERVERNAME " (Nick collision))");
451         /*
452          * we have killed sptr off, zero out it's pointer so if it's used
453          * again we'll know about it --Bleep
454          */
455         sptr = 0;
456       }
457       if (lastnick != cli_lastnick(acptr))
458         return 0;                /* Ignore the NICK */
459     }
460     send_reply(acptr, ERR_NICKCOLLISION, nick);
461   }
462
463   ++ServerStats->is_kill;
464   cli_flags(acptr) |= FLAGS_KILLED;
465   /*
466    * This exits the client we had before getting the NICK message
467    */
468   if (differ) {
469     sendcmdto_serv_butone(&me, CMD_KILL, acptr, "%C :%s (older nick "
470                           "overruled)", acptr, cli_name(&me));
471     if (MyConnect(acptr)) {
472       sendcmdto_one(acptr, CMD_QUIT, cptr, ":Killed (" HEAD_IN_SAND_SERVERNAME " (older "
473                     "nick overruled))");
474       sendcmdto_one(&me, CMD_KILL, acptr, "%C :" HEAD_IN_SAND_SERVERNAME " (older nick "
475                     "overruled)", acptr);
476     }
477     exit_client(cptr, acptr, &me, "Killed (" HEAD_IN_SAND_SERVERNAME " (older nick "
478                 "overruled))");
479   }
480   else {
481     sendcmdto_serv_butone(&me, CMD_KILL, acptr, "%C :%s (nick collision from "
482                           "same user@host)", acptr, cli_name(&me));
483     if (MyConnect(acptr)) {
484       sendcmdto_one(acptr, CMD_QUIT, cptr, ":Killed (" HEAD_IN_SAND_SERVERNAME " (nick "
485                     "collision from same user@host))");
486       sendcmdto_one(&me, CMD_KILL, acptr, "%C :" HEAD_IN_SAND_SERVERNAME " (older nick "
487                     "overruled)", acptr);
488     }
489     exit_client(cptr, acptr, &me, "Killed (" HEAD_IN_SAND_SERVERNAME " (nick collision "
490                 "from same user@host))");
491   }
492   if (lastnick == cli_lastnick(acptr))
493     return 0;
494
495   assert(0 != sptr);
496   return set_nick_name(cptr, sptr, nick, parc, parv);
497 }