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"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
106 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
107 * string for a nickname (note, the 'nick' may be modified in the process...)
109 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
111 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
112 * anything outside the above set will terminate nickname.
113 * In addition, the first character cannot be '-' or a Digit.
116 * The '~'-character should be allowed, but a change should be global,
117 * some confusion would result if only few servers allowed it...
119 static int do_nick_name(char* nick)
122 char* end = ch + NICKLEN;
125 /* first character in [0..9-] */
126 if (*ch == '-' || IsDigit(*ch))
128 for ( ; (ch < end) && *ch; ++ch)
129 if (!IsNickChar(*ch))
138 * m_nick - message handler for local clients
139 * parv[0] = sender prefix
142 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
144 struct Client* acptr;
145 char nick[NICKLEN + 2];
148 const char* client_name;
151 assert(cptr == sptr);
154 * parv[0] will be empty for clients connecting for the first time
156 client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
159 send_reply(sptr, ERR_NONICKNAMEGIVEN);
163 * Don't let them send make us send back a really long string of
167 if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
168 arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
170 if ((s = strchr(arg, '~')))
176 * If do_nick_name() returns a null name then reject it.
178 if (0 == do_nick_name(nick)) {
179 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
184 * Check if this is a LOCAL user trying to use a reserved (Juped)
185 * nick, if so tell him that it's a nick in use...
187 if (isNickJuped(nick)) {
188 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
189 return 0; /* NICK message ignored */
192 if (!(acptr = FindClient(nick))) {
194 * No collisions, all clear...
196 return set_nick_name(cptr, sptr, nick, parc, parv);
198 if (IsServer(acptr)) {
199 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
200 return 0; /* NICK message ignored */
203 * If acptr == sptr, then we have a client doing a nick
204 * change between *equivalent* nicknames as far as server
205 * is concerned (user is changing the case of his/her
206 * nickname or somesuch)
210 * If acptr == sptr, then we have a client doing a nick
211 * change between *equivalent* nicknames as far as server
212 * is concerned (user is changing the case of his/her
213 * nickname or somesuch)
215 if (0 != strcmp(cli_name(acptr), nick)) {
217 * Allows change of case in his/her nick
219 return set_nick_name(cptr, sptr, nick, parc, parv);
222 * This is just ':old NICK old' type thing.
223 * Just forget the whole thing here. There is
224 * no point forwarding it to anywhere,
225 * especially since servers prior to this
226 * version would treat it as nick collision.
231 * Note: From this point forward it can be assumed that
232 * acptr != sptr (point to different client structures).
234 assert(acptr != sptr);
236 * If the older one is "non-person", the new entry is just
237 * allowed to overwrite it. Just silently drop non-person,
238 * and proceed with the nick. This should take care of the
239 * "dormant nick" way of generating collisions...
241 * XXX - hmmm can this happen after one is registered?
243 * Yes, client 1 connects to IRC and registers, client 2 connects and
244 * sends "NICK foo" but doesn't send anything more. client 1 now does
245 * /nick foo, they should succeed and client 2 gets disconnected with
248 if (IsUnknown(acptr) && MyConnect(acptr)) {
249 ++ServerStats->is_ref;
250 IPcheck_connect_fail(cli_ip(acptr));
251 exit_client(cptr, acptr, &me, "Overridden by other sign on");
252 return set_nick_name(cptr, sptr, nick, parc, parv);
255 * NICK is coming from local client connection. Just
256 * send error reply and ignore the command.
258 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
259 return 0; /* NICK message ignored */
264 * ms_nick - server message handler for nicks
265 * parv[0] = sender prefix
268 * If from server, source is client:
269 * parv[2] = timestamp
273 * parv[3] = timestamp
276 * parv[6] = umode (optional)
277 * parv[parc-3] = IP# <- Only Protocol >= 10
278 * parv[parc-2] = YXX, numeric nick <- Only Protocol >= 10
279 * parv[parc-1] = info
282 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
284 struct Client *acptr;
285 char nick[NICKLEN + 2];
292 assert(IsServer(cptr));
294 if ((IsServer(sptr) && parc < 8) || parc < 3)
296 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
298 return need_more_params(sptr, "NICK");
301 ircd_strncpy(nick, parv[1], NICKLEN);
302 nick[NICKLEN] = '\0';
306 lastnick = atoi(parv[3]);
307 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
308 cli_serv(sptr)->lag = TStime() - lastnick;
312 lastnick = atoi(parv[2]);
313 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
314 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
317 * If do_nick_name() returns a null name OR if the server sent a nick
318 * name and do_nick_name() changed it in some way (due to rules of nick
319 * creation) then reject it. If from a server and we reject it,
320 * and KILL it. -avalon 4/4/92
322 if (!do_nick_name(nick) || strcmp(nick, parv[1]))
324 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
326 ++ServerStats->is_kill;
327 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
329 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
330 IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
331 nick, cli_name(cptr));
337 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
338 parv[0], cli_name(&me), cli_name(cptr), parv[0],
339 cli_user(sptr) ? cli_username(sptr) : "",
340 cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
345 /* Check against nick name collisions. */
346 if ((acptr = FindClient(nick)) == NULL)
347 /* No collisions, all clear... */
348 return set_nick_name(cptr, sptr, nick, parc, parv);
351 * If acptr == sptr, then we have a client doing a nick
352 * change between *equivalent* nicknames as far as server
353 * is concerned (user is changing the case of his/her
354 * nickname or somesuch)
358 if (strcmp(cli_name(acptr), nick) != 0)
359 /* Allows change of case in his/her nick */
360 return set_nick_name(cptr, sptr, nick, parc, parv);
362 /* Setting their nick to what it already is? Ignore it. */
365 /* now we know we have a real collision. */
367 * Note: From this point forward it can be assumed that
368 * acptr != sptr (point to different client structures).
370 assert(acptr != sptr);
372 * If the older one is "non-person", the new entry is just
373 * allowed to overwrite it. Just silently drop non-person,
374 * and proceed with the nick. This should take care of the
375 * "dormant nick" way of generating collisions...
377 if (IsUnknown(acptr) && MyConnect(acptr))
379 ServerStats->is_ref++;
380 IPcheck_connect_fail(cli_ip(acptr));
381 exit_client(cptr, acptr, &me, "Overridden by other sign on");
382 return set_nick_name(cptr, sptr, nick, parc, parv);
385 * Decide, we really have a nick collision and deal with it
388 * NICK was coming from a server connection.
389 * This means we have a race condition (two users signing on
390 * at the same time), or two net fragments reconnecting with the same nick.
391 * The latter can happen because two different users connected
392 * or because one and the same user switched server during a net break.
393 * If the TimeStamps are equal, we kill both (or only 'new'
394 * if it was a ":server NICK new ...").
395 * Otherwise we kill the youngest when user@host differ,
396 * or the oldest when they are the same.
397 * We treat user and ~user as different, because if it wasn't
398 * a faked ~user the AUTH wouldn't have added the '~'.
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 differ = (cli_ip(acptr).s_addr != htonl(base64toint(parv[parc - 3]))) ||
411 (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
412 sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
413 "%C %Tu (%s user@host))", acptr, cli_from(acptr),
414 cli_lastnick(acptr), cptr, lastnick,
415 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);
430 type = differ ? "older nick overruled" : "nick collision from same user@host";
432 * Now remove (kill) the nick on our side if it is the youngest.
433 * If no timestamp was received, we ignore the incoming nick
434 * (and expect a KILL for our legit nick soon ):
435 * When the timestamps are equal we kill both nicks. --Run
436 * acptr->from != cptr should *always* be true (?).
438 * This exits the client sending the NICK message
440 if ((differ && lastnick >= cli_lastnick(acptr)) ||
441 (!differ && lastnick <= cli_lastnick(acptr)))
443 /* We need to bounce this kill straight back... Although the nick message
444 * for acptr is probably waiting in their recvq from me, its also possible
445 * that sptr will change their nick on cptr before cptr receives the
446 * nick message for acptr, which would leave acptr and sptr both alive
447 * on cptr, but only acptr alive on me, i.e. desync. This extra kill
448 * message has been absent for a while in ircu although it was a major
449 * problem when it was tried on efnet, so I don't know how big an issue it
450 * is. Probably best that this be left here, anyway...
452 ServerStats->is_kill++;
453 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
454 nick, cli_name(&me), type);
455 /* But if this was a nick change and not a nick introduction,
456 * we also need to ensure that we remove our local state
457 * record of the original client... Also, the rest of the
458 * net should be informed...
462 assert(!MyConnect(sptr));
463 /* Inform the rest of the net... */
464 sendcmdto_serv_butone(&me, CMD_KILL, cptr, "%s :%s (%s)",
465 nick, cli_name(&me), type);
466 /* Don't go sending off a QUIT message... */
467 SetFlag(sptr, FLAG_KILLED);
468 /* Remove them locally. */
469 exit_client_msg(cptr, sptr, &me,
471 feature_str(FEAT_HIS_SERVERNAME), type);
473 * We have killed sptr off, zero out it's pointer so if it's used
474 * again we'll know about it --Bleep
478 /* If the timestamps differ and we just killed sptr, we don't need to kill
481 if (lastnick != cli_lastnick(acptr))
484 /* Tell acptr why we are killing it. */
485 send_reply(acptr, ERR_NICKCOLLISION, nick);
487 ServerStats->is_kill++;
488 SetFlag(acptr, FLAG_KILLED);
490 * This exits the client we had before getting the NICK message
492 sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s"
493 " (%s)", acptr, feature_str(FEAT_HIS_SERVERNAME),
495 exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
496 feature_str(FEAT_HIS_SERVERNAME), type);
497 if (lastnick == cli_lastnick(acptr))
501 return set_nick_name(cptr, sptr, nick, parc, parv);