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];
256 assert(IsServer(cptr));
258 if ((IsServer(sptr) && parc < 8) || parc < 3) {
259 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
261 return need_more_params(sptr, "NICK");
264 ircd_strncpy(nick, parv[1], NICKLEN);
265 nick[NICKLEN] = '\0';
267 if (IsServer(sptr)) {
268 lastnick = atoi(parv[3]);
269 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
270 cli_serv(sptr)->lag = TStime() - lastnick;
273 lastnick = atoi(parv[2]);
274 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
275 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
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
283 if (0 == do_nick_name(nick) || 0 != strcmp(nick, parv[1])) {
284 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
286 ++ServerStats->is_kill;
287 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
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)) {
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) :
305 * Check against nick name collisions.
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
313 acptr = FindClient(nick);
316 * No collisions, all clear...
318 return set_nick_name(cptr, sptr, nick, parc, parv);
322 if (IsServer(acptr)) { /* shouldn't even happen, actually */
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
330 sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C(%C <- %C)", sptr,
331 cli_from(acptr), cptr);
332 ++ServerStats->is_kill;
334 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s <- %s)", sptr, cli_name(&me),
335 cli_name(cli_from(acptr)), cli_name(cptr));
337 cli_flags(sptr) |= FLAGS_KILLED;
339 * if sptr is a server it is exited here, nothing else to do
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));
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)
353 if (strcmp(cli_name(acptr), nick) != 0)
355 * Allows change of case in his/her nick
357 return set_nick_name(cptr, sptr, nick, parc, parv);
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.
366 return 0; /* NICK Message ignored */
370 * Note: From this point forward it can be assumed that
371 * acptr != sptr (point to different client structures).
373 assert(acptr != sptr);
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...
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);
387 * Decide, we really have a nick collision and deal with it
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 '~'.
404 if (IsServer(sptr)) {
406 * A new NICK being introduced by a neighbouring
407 * server (e.g. message type ":server NICK new ..." received)
409 * compare IP address and username
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");
420 * A NICK change has collided (e.g. message type ":old NICK new").
422 * compare IP address and username
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);
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 (?).
437 * This exits the client sending the NICK message
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));
448 cli_flags(sptr) |= FLAGS_KILLED;
449 exit_client(cptr, sptr, &me,
450 "Killed (" HEAD_IN_SAND_SERVERNAME " (Nick collision))");
452 * we have killed sptr off, zero out it's pointer so if it's used
453 * again we'll know about it --Bleep
457 if (lastnick != cli_lastnick(acptr))
458 return 0; /* Ignore the NICK */
460 send_reply(acptr, ERR_NICKCOLLISION, nick);
463 ++ServerStats->is_kill;
464 cli_flags(acptr) |= FLAGS_KILLED;
466 * This exits the client we had before getting the NICK message
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 "
474 sendcmdto_one(&me, CMD_KILL, acptr, "%C :" HEAD_IN_SAND_SERVERNAME " (older nick "
475 "overruled)", acptr);
477 exit_client(cptr, acptr, &me, "Killed (" HEAD_IN_SAND_SERVERNAME " (older nick "
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);
489 exit_client(cptr, acptr, &me, "Killed (" HEAD_IN_SAND_SERVERNAME " (nick collision "
490 "from same user@host))");
492 if (lastnick == cli_lastnick(acptr))
496 return set_nick_name(cptr, sptr, nick, parc, parv);