]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/m_nick.c
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,v 1.25.2.2 2006/11/04 21:42:00 entrope Exp $
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
);
189 if (IsRegistered(sptr
) && !IsAnOper(sptr
) && IsNickGlined(sptr
, nick
)) {
190 send_reply(sptr
, ERR_ERRONEUSNICKNAME
, nick
);
195 * Check if this is a LOCAL user trying to use a reserved (Juped)
196 * nick, if so tell him that it's a nick in use...
198 if (isNickJuped(nick
)) {
199 send_reply(sptr
, ERR_NICKNAMEINUSE
, nick
);
200 return 0; /* NICK message ignored */
203 if (!(acptr
= FindClient(nick
))) {
205 * No collisions, all clear...
207 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
209 if (IsServer(acptr
)) {
210 send_reply(sptr
, ERR_NICKNAMEINUSE
, nick
);
211 return 0; /* NICK message ignored */
214 * If acptr == sptr, then we have a client doing a nick
215 * change between *equivalent* nicknames as far as server
216 * is concerned (user is changing the case of his/her
217 * nickname or somesuch)
221 * If acptr == sptr, then we have a client doing a nick
222 * change between *equivalent* nicknames as far as server
223 * is concerned (user is changing the case of his/her
224 * nickname or somesuch)
226 if (0 != strcmp(cli_name(acptr
), nick
)) {
228 * Allows change of case in his/her nick
230 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
233 * This is just ':old NICK old' type thing.
234 * Just forget the whole thing here. There is
235 * no point forwarding it to anywhere,
236 * especially since servers prior to this
237 * version would treat it as nick collision.
242 * Note: From this point forward it can be assumed that
243 * acptr != sptr (point to different client structures).
245 assert(acptr
!= sptr
);
247 * If the older one is "non-person", the new entry is just
248 * allowed to overwrite it. Just silently drop non-person,
249 * and proceed with the nick. This should take care of the
250 * "dormant nick" way of generating collisions...
252 * XXX - hmmm can this happen after one is registered?
254 * Yes, client 1 connects to IRC and registers, client 2 connects and
255 * sends "NICK foo" but doesn't send anything more. client 1 now does
256 * /nick foo, they should succeed and client 2 gets disconnected with
259 if (IsUnknown(acptr
) && MyConnect(acptr
)) {
260 ServerStats
->is_ref
++;
261 IPcheck_connect_fail(acptr
);
262 exit_client(cptr
, acptr
, &me
, "Overridden by other sign on");
263 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
266 * NICK is coming from local client connection. Just
267 * send error reply and ignore the command.
269 send_reply(sptr
, ERR_NICKNAMEINUSE
, nick
);
270 return 0; /* NICK message ignored */
275 * ms_nick - server message handler for nicks
276 * parv[0] = sender prefix
279 * If from server, source is client:
280 * parv[2] = timestamp
284 * parv[3] = timestamp
287 * parv[6] = umode (optional)
288 * parv[parc-3] = IP# <- Only Protocol >= 10
289 * parv[parc-2] = YXX, numeric nick <- Only Protocol >= 10
290 * parv[parc-1] = info
293 int ms_nick(struct Client
* cptr
, struct Client
* sptr
, int parc
, char* parv
[])
295 struct Client
*acptr
;
296 char nick
[NICKLEN
+ 2];
303 assert(IsServer(cptr
));
305 if ((IsServer(sptr
) && parc
< 8) || parc
< 3)
307 sendto_opmask_butone(0, SNO_OLDSNO
, "bad NICK param count for %s from %C",
309 return need_more_params(sptr
, "NICK");
312 ircd_strncpy(nick
, parv
[1], NICKLEN
);
313 nick
[NICKLEN
] = '\0';
317 lastnick
= atoi(parv
[3]);
318 if (lastnick
> OLDEST_TS
&& !IsBurstOrBurstAck(sptr
))
319 cli_serv(sptr
)->lag
= TStime() - lastnick
;
323 lastnick
= atoi(parv
[2]);
324 if (lastnick
> OLDEST_TS
&& !IsBurstOrBurstAck(sptr
))
325 cli_serv(cli_user(sptr
)->server
)->lag
= TStime() - lastnick
;
328 * If do_nick_name() returns a null name OR if the server sent a nick
329 * name and do_nick_name() changed it in some way (due to rules of nick
330 * creation) then reject it. If from a server and we reject it,
331 * and KILL it. -avalon 4/4/92
333 if (!do_nick_name(nick
) || strcmp(nick
, parv
[1]))
335 send_reply(sptr
, ERR_ERRONEUSNICKNAME
, parv
[1]);
337 ++ServerStats
->is_kill
;
338 sendto_opmask_butone(0, SNO_OLDSNO
, "Bad Nick: %s From: %s %C", parv
[1],
340 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%s :%s (%s <- %s[%s])",
341 IsServer(sptr
) ? parv
[parc
- 2] : parv
[0], cli_name(&me
), parv
[1],
342 nick
, cli_name(cptr
));
348 sendcmdto_serv_butone(&me
, CMD_KILL
, 0, "%s :%s (%s <- %s!%s@%s)",
349 parv
[0], cli_name(&me
), cli_name(cptr
), parv
[0],
350 cli_user(sptr
) ? cli_username(sptr
) : "",
351 cli_user(sptr
) ? cli_name(cli_user(sptr
)->server
) :
356 /* Check against nick name collisions. */
357 if ((acptr
= FindClient(nick
)) == NULL
)
358 /* No collisions, all clear... */
359 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
362 * If acptr == sptr, then we have a client doing a nick
363 * change between *equivalent* nicknames as far as server
364 * is concerned (user is changing the case of his/her
365 * nickname or somesuch)
369 if (strcmp(cli_name(acptr
), nick
) != 0)
370 /* Allows change of case in his/her nick */
371 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
373 /* Setting their nick to what it already is? Ignore it. */
376 /* now we know we have a real collision. */
378 * Note: From this point forward it can be assumed that
379 * acptr != sptr (point to different client structures).
381 assert(acptr
!= sptr
);
383 * If the older one is "non-person", the new entry is just
384 * allowed to overwrite it. Just silently drop non-person,
385 * and proceed with the nick. This should take care of the
386 * "dormant nick" way of generating collisions...
388 if (IsUnknown(acptr
) && MyConnect(acptr
))
390 ServerStats
->is_ref
++;
391 IPcheck_connect_fail(acptr
);
392 exit_client(cptr
, acptr
, &me
, "Overridden by other sign on");
393 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
396 * Decide, we really have a nick collision and deal with it
399 * NICK was coming from a server connection.
400 * This means we have a race condition (two users signing on
401 * at the same time), or two net fragments reconnecting with the same nick.
402 * The latter can happen because two different users connected
403 * or because one and the same user switched server during a net break.
404 * If the TimeStamps are equal, we kill both (or only 'new'
405 * if it was a ":server NICK new ...").
406 * Otherwise we kill the youngest when user@host differ,
407 * or the oldest when they are the same.
408 * We treat user and ~user as different, because if it wasn't
409 * a faked ~user the AUTH wouldn't have added the '~'.
415 struct irc_in_addr ip
;
417 * A new NICK being introduced by a neighbouring
418 * server (e.g. message type ":server NICK new ..." received)
420 * compare IP address and username
422 base64toip(parv
[parc
- 3], &ip
);
423 differ
= (0 != memcmp(&cli_ip(acptr
), &ip
, sizeof(cli_ip(acptr
)))) ||
424 (0 != ircd_strcmp(cli_user(acptr
)->username
, parv
[4]));
425 sendto_opmask_butone(0, SNO_OLDSNO
, "Nick collision on %C (%C %Tu <- "
426 "%C %Tu (%s user@host))", acptr
, cli_from(acptr
),
427 cli_lastnick(acptr
), cptr
, lastnick
,
428 differ
? "Different" : "Same");
433 * A NICK change has collided (e.g. message type ":old NICK new").
435 * compare IP address and username
437 differ
= (0 != memcmp(&cli_ip(acptr
), &cli_ip(sptr
), sizeof(cli_ip(acptr
)))) ||
438 (0 != ircd_strcmp(cli_user(acptr
)->username
, cli_user(sptr
)->username
));
439 sendto_opmask_butone(0, SNO_OLDSNO
, "Nick change collision from %C to "
440 "%C (%C %Tu <- %C %Tu)", sptr
, acptr
, cli_from(acptr
),
441 cli_lastnick(acptr
), cptr
, lastnick
);
443 type
= differ
? "overruled by older nick" : "nick collision from same user@host";
445 * Now remove (kill) the nick on our side if it is the youngest.
446 * If no timestamp was received, we ignore the incoming nick
447 * (and expect a KILL for our legit nick soon ):
448 * When the timestamps are equal we kill both nicks. --Run
449 * acptr->from != cptr should *always* be true (?).
451 * This exits the client sending the NICK message
453 if ((differ
&& lastnick
>= cli_lastnick(acptr
)) ||
454 (!differ
&& lastnick
<= cli_lastnick(acptr
)))
456 ServerStats
->is_kill
++;
459 /* If this was a nick change and not a nick introduction, we
460 * need to ensure that we remove our record of the client, and
461 * send a KILL to the whole network.
463 assert(!MyConnect(sptr
));
464 /* Inform the rest of the net... */
465 sendcmdto_serv_butone(&me
, CMD_KILL
, 0, "%C :%s (%s)",
466 sptr
, cli_name(&me
), type
);
467 /* Don't go sending off a QUIT message... */
468 SetFlag(sptr
, FLAG_KILLED
);
469 /* Remove them locally. */
470 exit_client_msg(cptr
, sptr
, &me
,
472 feature_str(FEAT_HIS_SERVERNAME
), type
);
476 /* If the origin is a server, this was a new client, so we only
477 * send the KILL in the direction it came from. We have no
478 * client record that we would have to clean up.
480 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%s :%s (%s)",
481 parv
[parc
- 2], cli_name(&me
), type
);
483 /* If the timestamps differ and we just killed sptr, we don't need to kill
486 if (lastnick
!= cli_lastnick(acptr
))
489 /* Tell acptr why we are killing it. */
490 send_reply(acptr
, ERR_NICKCOLLISION
, nick
);
492 ServerStats
->is_kill
++;
493 SetFlag(acptr
, FLAG_KILLED
);
495 * This exits the client we had before getting the NICK message
497 sendcmdto_serv_butone(&me
, CMD_KILL
, NULL
, "%C :%s (%s)",
498 acptr
, feature_str(FEAT_HIS_SERVERNAME
),
500 exit_client_msg(cptr
, acptr
, &me
, "Killed (%s (%s))",
501 feature_str(FEAT_HIS_SERVERNAME
), type
);
502 if (lastnick
== cli_lastnick(acptr
))
506 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);