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_features.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
102 /* #include <assert.h> -- Now using assert in ircd_log.h */
107 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
108 * string for a nickname (note, the 'nick' may be modified in the process...)
110 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
112 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
113 * anything outside the above set will terminate nickname.
114 * In addition, the first character cannot be '-' or a Digit.
117 * The '~'-character should be allowed, but a change should be global,
118 * some confusion would result if only few servers allowed it...
120 static int do_nick_name(char* nick)
123 char* end = ch + NICKLEN;
126 /* first character in [0..9-] */
127 if (*ch == '-' || IsDigit(*ch))
129 for ( ; (ch < end) && *ch; ++ch)
130 if (!IsNickChar(*ch))
139 * m_nick - message handler for local clients
140 * parv[0] = sender prefix
143 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
145 struct Client* acptr;
146 char nick[NICKLEN + 2];
151 assert(cptr == sptr);
153 if (IsServerPort(cptr))
154 return exit_client(cptr, cptr, &me, "Use a different port");
157 send_reply(sptr, ERR_NONICKNAMEGIVEN);
162 * Don't let them send make us send back a really long string of
166 if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
167 arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
169 if ((s = strchr(arg, '~')))
175 * If do_nick_name() returns a null name then reject it.
177 if (0 == do_nick_name(nick)) {
178 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
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...
186 if (isNickJuped(nick)) {
187 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
188 return 0; /* NICK message ignored */
191 if (!(acptr = FindClient(nick))) {
193 * No collisions, all clear...
195 return set_nick_name(cptr, sptr, nick, parc, parv);
197 if (IsServer(acptr)) {
198 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
199 return 0; /* NICK message ignored */
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)
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)
214 if (0 != strcmp(cli_name(acptr), nick)) {
216 * Allows change of case in his/her nick
218 return set_nick_name(cptr, sptr, nick, parc, parv);
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.
230 * Note: From this point forward it can be assumed that
231 * acptr != sptr (point to different client structures).
233 assert(acptr != sptr);
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...
240 * XXX - hmmm can this happen after one is registered?
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
247 if (IsUnknown(acptr) && MyConnect(acptr)) {
248 ServerStats->is_ref++;
249 IPcheck_connect_fail(acptr, 0);
250 exit_client(cptr, acptr, &me, "Overridden by other sign on");
251 return set_nick_name(cptr, sptr, nick, parc, parv);
254 * NICK is coming from local client connection. Just
255 * send error reply and ignore the command.
257 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
258 return 0; /* NICK message ignored */
263 * ms_nick - server message handler for nicks
264 * parv[0] = sender prefix
267 * If from server, source is client:
268 * parv[2] = timestamp
272 * parv[3] = timestamp
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
281 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
283 struct Client *acptr;
284 char nick[NICKLEN + 2];
291 assert(IsServer(cptr));
293 if ((IsServer(sptr) && parc < 8) || parc < 3)
295 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
297 return need_more_params(sptr, "NICK");
300 ircd_strncpy(nick, parv[1], NICKLEN);
301 nick[NICKLEN] = '\0';
305 lastnick = atoi(parv[3]);
306 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
307 cli_serv(sptr)->lag = TStime() - lastnick;
311 lastnick = atoi(parv[2]);
312 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
313 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
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
321 if (!do_nick_name(nick) || strcmp(nick, parv[1]))
323 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
325 ++ServerStats->is_kill;
326 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
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));
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) :
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);
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)
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);
361 /* Setting their nick to what it already is? Ignore it. */
364 /* now we know we have a real collision. */
366 * Note: From this point forward it can be assumed that
367 * acptr != sptr (point to different client structures).
369 assert(acptr != sptr);
371 * If the older one is "non-person", the new entry is just
372 * allowed to overwrite it. Just silently drop non-person,
373 * and proceed with the nick. This should take care of the
374 * "dormant nick" way of generating collisions...
376 if (IsUnknown(acptr) && MyConnect(acptr))
378 ServerStats->is_ref++;
379 IPcheck_connect_fail(acptr, 0);
380 exit_client(cptr, acptr, &me, "Overridden by other sign on");
381 return set_nick_name(cptr, sptr, nick, parc, parv);
384 * Decide, we really have a nick collision and deal with it
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 '~'.
403 struct irc_in_addr ip;
405 * A new NICK being introduced by a neighbouring
406 * server (e.g. message type ":server NICK new ..." received)
408 * compare IP address and username
410 base64toip(parv[parc - 3], &ip);
411 differ = (0 != memcmp(&cli_ip(acptr), &ip, sizeof(cli_ip(acptr)))) ||
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");
421 * A NICK change has collided (e.g. message type ":old NICK new").
423 * compare IP address and username
425 differ = (0 != memcmp(&cli_ip(acptr), &cli_ip(sptr), sizeof(cli_ip(acptr)))) ||
426 (0 != ircd_strcmp(cli_user(acptr)->username, cli_user(sptr)->username));
427 sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
428 "%C (%C %Tu <- %C %Tu)", sptr, acptr, cli_from(acptr),
429 cli_lastnick(acptr), cptr, lastnick);
431 type = differ ? "overruled by older nick" : "nick collision from same user@host";
433 * Now remove (kill) the nick on our side if it is the youngest.
434 * If no timestamp was received, we ignore the incoming nick
435 * (and expect a KILL for our legit nick soon ):
436 * When the timestamps are equal we kill both nicks. --Run
437 * acptr->from != cptr should *always* be true (?).
439 * This exits the client sending the NICK message
441 if ((differ && lastnick >= cli_lastnick(acptr)) ||
442 (!differ && lastnick <= cli_lastnick(acptr)))
444 ServerStats->is_kill++;
447 /* If this was a nick change and not a nick introduction, we
448 * need to ensure that we remove our record of the client, and
449 * send a KILL to the whole network.
451 assert(!MyConnect(sptr));
452 /* Inform the rest of the net... */
453 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%C :%s (%s)",
454 sptr, cli_name(&me), type);
455 /* Don't go sending off a QUIT message... */
456 SetFlag(sptr, FLAG_KILLED);
457 /* Remove them locally. */
458 exit_client_msg(cptr, sptr, &me,
460 feature_str(FEAT_HIS_SERVERNAME), type);
464 /* If the origin is a server, this was a new client, so we only
465 * send the KILL in the direction it came from. We have no
466 * client record that we would have to clean up.
468 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
469 parv[parc - 2], cli_name(&me), type);
471 /* If the timestamps differ and we just killed sptr, we don't need to kill
474 if (lastnick != cli_lastnick(acptr))
477 /* Tell acptr why we are killing it. */
478 send_reply(acptr, ERR_NICKCOLLISION, nick);
480 ServerStats->is_kill++;
481 SetFlag(acptr, FLAG_KILLED);
483 * This exits the client we had before getting the NICK message
485 sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s (%s)",
486 acptr, feature_str(FEAT_HIS_SERVERNAME),
488 exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
489 feature_str(FEAT_HIS_SERVERNAME), type);
490 if (lastnick == cli_lastnick(acptr))
494 return set_nick_name(cptr, sptr, nick, parc, parv);