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