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.
23 * $Id: m_nick.c 1729 2006-11-04 21:42:00Z entrope $
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_features.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
103 /* #include <assert.h> -- Now using assert in ircd_log.h */
108 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
109 * string for a nickname (note, the 'nick' may be modified in the process...)
111 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
113 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
114 * anything outside the above set will terminate nickname.
115 * In addition, the first character cannot be '-' or a Digit.
118 * The '~'-character should be allowed, but a change should be global,
119 * some confusion would result if only few servers allowed it...
121 static int do_nick_name(char* nick)
124 char* end = ch + NICKLEN;
127 /* first character in [0..9-] */
128 if (*ch == '-' || IsDigit(*ch))
130 for ( ; (ch < end) && *ch; ++ch)
131 if (!IsNickChar(*ch))
140 * m_nick - message handler for local clients
141 * parv[0] = sender prefix
144 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
146 struct Client* acptr;
147 char nick[NICKLEN + 2];
150 const char* client_name;
153 assert(cptr == sptr);
155 if (IsServerPort(cptr))
156 return exit_client(cptr, cptr, &me, "Use a different port");
159 * parv[0] will be empty for clients connecting for the first time
161 client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
164 send_reply(sptr, ERR_NONICKNAMEGIVEN);
169 * Don't let them send make us send back a really long string of
173 if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
174 arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
176 if ((s = strchr(arg, '~')))
182 * If do_nick_name() returns a null name then reject it.
184 if (0 == do_nick_name(nick)) {
185 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
190 * Check if this is a LOCAL user trying to use a reserved (Juped)
191 * nick, if so tell him that it's a nick in use...
193 if (isNickJuped(nick) && !HasPriv(sptr, PRIV_UMODE_CHSERV)) {
194 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
195 return 0; /* NICK message ignored */
198 if (!(acptr = FindClient(nick))) {
200 * No collisions, all clear...
202 return set_nick_name(cptr, sptr, nick, parc, parv, 0);
204 if (IsServer(acptr)) {
205 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
206 return 0; /* NICK message ignored */
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)
216 * If acptr == sptr, then we have a client doing a nick
217 * change between *equivalent* nicknames as far as server
218 * is concerned (user is changing the case of his/her
219 * nickname or somesuch)
221 if (0 != strcmp(cli_name(acptr), nick)) {
223 * Allows change of case in his/her nick
225 return set_nick_name(cptr, sptr, nick, parc, parv, 0);
228 * This is just ':old NICK old' type thing.
229 * Just forget the whole thing here. There is
230 * no point forwarding it to anywhere,
231 * especially since servers prior to this
232 * version would treat it as nick collision.
237 * Note: From this point forward it can be assumed that
238 * acptr != sptr (point to different client structures).
240 assert(acptr != sptr);
242 * If the older one is "non-person", the new entry is just
243 * allowed to overwrite it. Just silently drop non-person,
244 * and proceed with the nick. This should take care of the
245 * "dormant nick" way of generating collisions...
247 * XXX - hmmm can this happen after one is registered?
249 * Yes, client 1 connects to IRC and registers, client 2 connects and
250 * sends "NICK foo" but doesn't send anything more. client 1 now does
251 * /nick foo, they should succeed and client 2 gets disconnected with
254 if (IsUnknown(acptr) && MyConnect(acptr)) {
255 ServerStats->is_ref++;
256 IPcheck_connect_fail(acptr);
257 exit_client(cptr, acptr, &me, "Overridden by other sign on");
258 return set_nick_name(cptr, sptr, nick, parc, parv, 0);
261 * If the Nickname is in use by a Zombie, wait for possible LOC information
262 * to recover the zombie connection...
264 if(IsNotConn(acptr) && !(cli_name(sptr))[0]) {
265 auth_set_recover_client(cli_auth(sptr), acptr);
266 return set_nick_name(cptr, sptr, nick, parc, parv, 1);
269 * NICK is coming from local client connection. Just
270 * send error reply and ignore the command.
272 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
273 return 0; /* NICK message ignored */
278 * ms_nick - server message handler for nicks
279 * parv[0] = sender prefix
282 * If from server, source is client:
283 * parv[2] = timestamp
287 * parv[3] = timestamp
290 * parv[6] = umode (optional)
291 * parv[parc-3] = IP# <- Only Protocol >= 10
292 * parv[parc-2] = YXX, numeric nick <- Only Protocol >= 10
293 * parv[parc-1] = info
296 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
298 struct Client *acptr;
299 char nick[NICKLEN + 2];
306 assert(IsServer(cptr));
308 if ((IsServer(sptr) && parc < 8) || parc < 3)
310 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
312 return need_more_params(sptr, "NICK");
315 ircd_strncpy(nick, parv[1], NICKLEN);
316 nick[NICKLEN] = '\0';
320 lastnick = atoi(parv[3]);
321 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
322 cli_serv(sptr)->lag = TStime() - lastnick;
326 lastnick = atoi(parv[2]);
327 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
328 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
331 * If do_nick_name() returns a null name OR if the server sent a nick
332 * name and do_nick_name() changed it in some way (due to rules of nick
333 * creation) then reject it. If from a server and we reject it,
334 * and KILL it. -avalon 4/4/92
336 if (!do_nick_name(nick) || strcmp(nick, parv[1]))
338 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
340 ++ServerStats->is_kill;
341 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
343 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
344 IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
345 nick, cli_name(cptr));
351 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
352 parv[0], cli_name(&me), cli_name(cptr), parv[0],
353 cli_user(sptr) ? cli_username(sptr) : "",
354 cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
359 /* Check against nick name collisions. */
360 if ((acptr = FindClient(nick)) == NULL)
361 /* No collisions, all clear... */
362 return set_nick_name(cptr, sptr, nick, parc, parv, 0);
365 * If acptr == sptr, then we have a client doing a nick
366 * change between *equivalent* nicknames as far as server
367 * is concerned (user is changing the case of his/her
368 * nickname or somesuch)
372 if (strcmp(cli_name(acptr), nick) != 0)
373 /* Allows change of case in his/her nick */
374 return set_nick_name(cptr, sptr, nick, parc, parv, 0);
376 /* Setting their nick to what it already is? Ignore it. */
379 /* now we know we have a real collision. */
381 * Note: From this point forward it can be assumed that
382 * acptr != sptr (point to different client structures).
384 assert(acptr != sptr);
386 * If the older one is "non-person", the new entry is just
387 * allowed to overwrite it. Just silently drop non-person,
388 * and proceed with the nick. This should take care of the
389 * "dormant nick" way of generating collisions...
391 if (IsUnknown(acptr) && MyConnect(acptr))
393 ServerStats->is_ref++;
394 IPcheck_connect_fail(acptr);
395 exit_client(cptr, acptr, &me, "Overridden by other sign on");
396 return set_nick_name(cptr, sptr, nick, parc, parv, 0);
399 * Decide, we really have a nick collision and deal with it
402 * NICK was coming from a server connection.
403 * This means we have a race condition (two users signing on
404 * at the same time), or two net fragments reconnecting with the same nick.
405 * The latter can happen because two different users connected
406 * or because one and the same user switched server during a net break.
407 * If the TimeStamps are equal, we kill both (or only 'new'
408 * if it was a ":server NICK new ...").
409 * Otherwise we kill the youngest when user@host differ,
410 * or the oldest when they are the same.
411 * We treat user and ~user as different, because if it wasn't
412 * a faked ~user the AUTH wouldn't have added the '~'.
418 struct irc_in_addr ip;
420 * A new NICK being introduced by a neighbouring
421 * server (e.g. message type ":server NICK new ..." received)
423 * compare IP address and username
425 base64toip(parv[parc - 3], &ip);
426 differ = (0 != memcmp(&cli_ip(acptr), &ip, sizeof(cli_ip(acptr)))) ||
427 (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
428 sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
429 "%C %Tu (%s user@host))", acptr, cli_from(acptr),
430 cli_lastnick(acptr), cptr, lastnick,
431 differ ? "Different" : "Same");
436 * A NICK change has collided (e.g. message type ":old NICK new").
438 * compare IP address and username
440 differ = (0 != memcmp(&cli_ip(acptr), &cli_ip(sptr), sizeof(cli_ip(acptr)))) ||
441 (0 != ircd_strcmp(cli_user(acptr)->username, cli_user(sptr)->username));
442 sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
443 "%C (%C %Tu <- %C %Tu)", sptr, acptr, cli_from(acptr),
444 cli_lastnick(acptr), cptr, lastnick);
446 type = differ ? "overruled by older nick" : "nick collision from same user@host";
448 * Now remove (kill) the nick on our side if it is the youngest.
449 * If no timestamp was received, we ignore the incoming nick
450 * (and expect a KILL for our legit nick soon ):
451 * When the timestamps are equal we kill both nicks. --Run
452 * acptr->from != cptr should *always* be true (?).
454 * This exits the client sending the NICK message
456 if ((differ && lastnick >= cli_lastnick(acptr)) ||
457 (!differ && lastnick <= cli_lastnick(acptr)))
459 ServerStats->is_kill++;
462 /* If this was a nick change and not a nick introduction, we
463 * need to ensure that we remove our record of the client, and
464 * send a KILL to the whole network.
466 assert(!MyConnect(sptr));
467 /* Inform the rest of the net... */
468 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%C :%s (%s)",
469 sptr, cli_name(&me), type);
470 /* Don't go sending off a QUIT message... */
471 SetFlag(sptr, FLAG_KILLED);
472 /* Remove them locally. */
473 exit_client_msg(cptr, sptr, &me,
475 feature_str(FEAT_HIS_SERVERNAME), type);
479 /* If the origin is a server, this was a new client, so we only
480 * send the KILL in the direction it came from. We have no
481 * client record that we would have to clean up.
483 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
484 parv[parc - 2], cli_name(&me), type);
486 /* If the timestamps differ and we just killed sptr, we don't need to kill
489 if (lastnick != cli_lastnick(acptr))
492 /* Tell acptr why we are killing it. */
493 send_reply(acptr, ERR_NICKCOLLISION, nick);
495 ServerStats->is_kill++;
496 SetFlag(acptr, FLAG_KILLED);
498 * This exits the client we had before getting the NICK message
500 sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s (%s)",
501 acptr, feature_str(FEAT_HIS_SERVERNAME),
503 exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
504 feature_str(FEAT_HIS_SERVERNAME), type);
505 if (lastnick == cli_lastnick(acptr))
509 return set_nick_name(cptr, sptr, nick, parc, parv, 0);