2 * IRC - Internet Relay Chat, ircd/m_nick.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
88 #include "ircd_chattr.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
91 #include "ircd_policy.h"
105 * m_nick - message handler for local clients
106 * parv[0] = sender prefix
109 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
111 struct Client* acptr;
112 char nick[NICKLEN + 2];
115 const char* client_name;
118 assert(cptr == sptr);
121 * parv[0] will be empty for clients connecting for the first time
123 client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
126 send_reply(sptr, ERR_NONICKNAMEGIVEN);
130 * Don't let them send make us send back a really long string of
134 if (strlen(arg) > NICKLEN)
137 if ((s = strchr(arg, '~')))
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
148 if (0 == do_nick_name(nick)) {
149 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
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...
157 if (isNickJuped(nick)) {
158 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
159 return 0; /* NICK message ignored */
162 if (!(acptr = FindClient(nick))) {
164 * No collisions, all clear...
166 return set_nick_name(cptr, sptr, nick, parc, parv);
168 if (IsServer(acptr)) {
169 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
170 return 0; /* NICK message ignored */
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)
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)
185 if (0 != strcmp(cli_name(acptr), nick)) {
187 * Allows change of case in his/her nick
189 return set_nick_name(cptr, sptr, nick, parc, parv);
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.
201 * Note: From this point forward it can be assumed that
202 * acptr != sptr (point to different client structures).
204 assert(acptr != sptr);
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...
211 * XXX - hmmm can this happen after one is registered?
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);
220 * NICK is coming from local client connection. Just
221 * send error reply and ignore the command.
223 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
224 return 0; /* NICK message ignored */
229 * ms_nick - server message handler for nicks
230 * parv[0] = sender prefix
233 * If from server, source is client:
234 * parv[2] = timestamp
238 * parv[3] = timestamp
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
247 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
249 struct Client *acptr;
250 char nick[NICKLEN + 2];
257 assert(IsServer(cptr));
259 if ((IsServer(sptr) && parc < 8) || parc < 3)
261 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
263 return need_more_params(sptr, "NICK");
266 ircd_strncpy(nick, parv[1], NICKLEN);
267 nick[NICKLEN] = '\0';
271 lastnick = atoi(parv[3]);
272 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
273 cli_serv(sptr)->lag = TStime() - lastnick;
277 lastnick = atoi(parv[2]);
278 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
279 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
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
287 if (strlen(nick) != do_nick_name(nick))
289 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
291 ++ServerStats->is_kill;
292 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
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));
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) :
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);
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)
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);
327 /* Setting their nick to what it already is? Ignore it. */
330 /* now we know we have a real collision. */
331 if (IsUnknown(acptr) && MyConnect(acptr))
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);
339 * Decide, we really have a nick collision and deal with it
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 '~'.
359 * A new NICK being introduced by a neighbouring
360 * server (e.g. message type ":server NICK new ..." received)
362 * compare IP address and username
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");
374 * A NICK change has collided (e.g. message type ":old NICK new").
376 * compare IP address and username
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);
384 type = differ ? "older nick overruled" : "nick collision from same user@host";
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 (?).
392 * This exits the client sending the NICK message
394 if ((differ && lastnick >= cli_lastnick(acptr)) ||
395 (!differ && lastnick <= cli_lastnick(acptr)))
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...
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...
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))",
427 * We have killed sptr off, zero out it's pointer so if it's used
428 * again we'll know about it --Bleep
432 /* If the timestamps differ and we just killed sptr, we don't need to kill
435 if (lastnick != cli_lastnick(acptr))
438 /* Tell acptr why we are killing it. */
439 send_reply(acptr, ERR_NICKCOLLISION, nick);
441 ServerStats->is_kill++;
442 cli_flags(acptr) |= FLAGS_KILLED;
444 * This exits the client we had before getting the NICK message
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))",
450 if (lastnick == cli_lastnick(acptr))
454 return set_nick_name(cptr, sptr, nick, parc, parv);