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