]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/m_nick.c
Initial import of 2.10.12.01
[irc/quakenet/snircd.git] / ircd / m_nick.c
1 /*
2 * IRC - Internet Relay Chat, ircd/m_nick.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
5 *
6 * See file AUTHORS in IRC package for additional names of
7 * the programmers.
8 *
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)
12 * any later version.
13 *
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.
18 *
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.
22 *
23 * $Id: m_nick.c,v 1.25 2005/03/19 23:22:09 entrope Exp $
24 */
25
26 /*
27 * m_functions execute protocol messages on this server:
28 *
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...).
34 *
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.
38 *
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
41 *
42 * (IsServer(cptr))
43 * (sptr == cptr) => the message didn't
44 * have the prefix.
45 *
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
48 *
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
51 * user (not local).
52 *
53 * combining
54 *
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
58 *
59 * *Always* true (if 'parse' and others are working correct):
60 *
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
62 *
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 ]
67 *
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
70 *
71 * parv a NULL terminated list of parameter pointers,
72 *
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*
78 *
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
80 * non-NULL pointers.
81 */
82 #include "config.h"
83
84 #include "IPcheck.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_chattr.h"
89 #include "ircd_features.h"
90 #include "ircd_log.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_debug.h"
97 #include "s_misc.h"
98 #include "s_user.h"
99 #include "send.h"
100 #include "sys.h"
101
102 /* #include <assert.h> -- Now using assert in ircd_log.h */
103 #include <stdlib.h>
104 #include <string.h>
105
106 /*
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...)
109 *
110 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
111 *
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.
115 *
116 * Note:
117 * The '~'-character should be allowed, but a change should be global,
118 * some confusion would result if only few servers allowed it...
119 */
120 static int do_nick_name(char* nick)
121 {
122 char* ch = nick;
123 char* end = ch + NICKLEN;
124 assert(0 != ch);
125
126 /* first character in [0..9-] */
127 if (*ch == '-' || IsDigit(*ch))
128 return 0;
129 for ( ; (ch < end) && *ch; ++ch)
130 if (!IsNickChar(*ch))
131 break;
132
133 *ch = '\0';
134
135 return (ch - nick);
136 }
137
138 /*
139 * m_nick - message handler for local clients
140 * parv[0] = sender prefix
141 * parv[1] = nickname
142 */
143 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
144 {
145 struct Client* acptr;
146 char nick[NICKLEN + 2];
147 char* arg;
148 char* s;
149 const char* client_name;
150
151 assert(0 != cptr);
152 assert(cptr == sptr);
153
154 /*
155 * parv[0] will be empty for clients connecting for the first time
156 */
157 client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
158
159 if (parc < 2) {
160 send_reply(sptr, ERR_NONICKNAMEGIVEN);
161 return 0;
162 }
163 /*
164 * Don't let them send make us send back a really long string of
165 * garbage
166 */
167 arg = parv[1];
168 if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
169 arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
170
171 if ((s = strchr(arg, '~')))
172 *s = '\0';
173
174 strcpy(nick, arg);
175
176 /*
177 * If do_nick_name() returns a null name then reject it.
178 */
179 if (0 == do_nick_name(nick)) {
180 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
181 return 0;
182 }
183
184 /*
185 * Check if this is a LOCAL user trying to use a reserved (Juped)
186 * nick, if so tell him that it's a nick in use...
187 */
188 if (isNickJuped(nick)) {
189 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
190 return 0; /* NICK message ignored */
191 }
192
193 if (!(acptr = FindClient(nick))) {
194 /*
195 * No collisions, all clear...
196 */
197 return set_nick_name(cptr, sptr, nick, parc, parv);
198 }
199 if (IsServer(acptr)) {
200 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
201 return 0; /* NICK message ignored */
202 }
203 /*
204 * If acptr == sptr, then we have a client doing a nick
205 * change between *equivalent* nicknames as far as server
206 * is concerned (user is changing the case of his/her
207 * nickname or somesuch)
208 */
209 if (acptr == sptr) {
210 /*
211 * If acptr == sptr, then we have a client doing a nick
212 * change between *equivalent* nicknames as far as server
213 * is concerned (user is changing the case of his/her
214 * nickname or somesuch)
215 */
216 if (0 != strcmp(cli_name(acptr), nick)) {
217 /*
218 * Allows change of case in his/her nick
219 */
220 return set_nick_name(cptr, sptr, nick, parc, parv);
221 }
222 /*
223 * This is just ':old NICK old' type thing.
224 * Just forget the whole thing here. There is
225 * no point forwarding it to anywhere,
226 * especially since servers prior to this
227 * version would treat it as nick collision.
228 */
229 return 0;
230 }
231 /*
232 * Note: From this point forward it can be assumed that
233 * acptr != sptr (point to different client structures).
234 */
235 assert(acptr != sptr);
236 /*
237 * If the older one is "non-person", the new entry is just
238 * allowed to overwrite it. Just silently drop non-person,
239 * and proceed with the nick. This should take care of the
240 * "dormant nick" way of generating collisions...
241 *
242 * XXX - hmmm can this happen after one is registered?
243 *
244 * Yes, client 1 connects to IRC and registers, client 2 connects and
245 * sends "NICK foo" but doesn't send anything more. client 1 now does
246 * /nick foo, they should succeed and client 2 gets disconnected with
247 * the message below.
248 */
249 if (IsUnknown(acptr) && MyConnect(acptr)) {
250 ServerStats->is_ref++;
251 IPcheck_connect_fail(acptr);
252 exit_client(cptr, acptr, &me, "Overridden by other sign on");
253 return set_nick_name(cptr, sptr, nick, parc, parv);
254 }
255 /*
256 * NICK is coming from local client connection. Just
257 * send error reply and ignore the command.
258 */
259 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
260 return 0; /* NICK message ignored */
261 }
262
263
264 /*
265 * ms_nick - server message handler for nicks
266 * parv[0] = sender prefix
267 * parv[1] = nickname
268 *
269 * If from server, source is client:
270 * parv[2] = timestamp
271 *
272 * Source is server:
273 * parv[2] = hopcount
274 * parv[3] = timestamp
275 * parv[4] = username
276 * parv[5] = hostname
277 * parv[6] = umode (optional)
278 * parv[parc-3] = IP# <- Only Protocol >= 10
279 * parv[parc-2] = YXX, numeric nick <- Only Protocol >= 10
280 * parv[parc-1] = info
281 * parv[0] = server
282 */
283 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
284 {
285 struct Client *acptr;
286 char nick[NICKLEN + 2];
287 time_t lastnick = 0;
288 int differ = 1;
289 const char *type;
290
291 assert(0 != cptr);
292 assert(0 != sptr);
293 assert(IsServer(cptr));
294
295 if ((IsServer(sptr) && parc < 8) || parc < 3)
296 {
297 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
298 parv[1], cptr);
299 return need_more_params(sptr, "NICK");
300 }
301
302 ircd_strncpy(nick, parv[1], NICKLEN);
303 nick[NICKLEN] = '\0';
304
305 if (IsServer(sptr))
306 {
307 lastnick = atoi(parv[3]);
308 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
309 cli_serv(sptr)->lag = TStime() - lastnick;
310 }
311 else
312 {
313 lastnick = atoi(parv[2]);
314 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
315 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
316 }
317 /*
318 * If do_nick_name() returns a null name OR if the server sent a nick
319 * name and do_nick_name() changed it in some way (due to rules of nick
320 * creation) then reject it. If from a server and we reject it,
321 * and KILL it. -avalon 4/4/92
322 */
323 if (!do_nick_name(nick) || strcmp(nick, parv[1]))
324 {
325 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
326
327 ++ServerStats->is_kill;
328 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
329 parv[0], cptr);
330 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
331 IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
332 nick, cli_name(cptr));
333 if (!IsServer(sptr))
334 {
335 /*
336 * bad nick _change_
337 */
338 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
339 parv[0], cli_name(&me), cli_name(cptr), parv[0],
340 cli_user(sptr) ? cli_username(sptr) : "",
341 cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
342 cli_name(cptr));
343 }
344 return 0;
345 }
346 /* Check against nick name collisions. */
347 if ((acptr = FindClient(nick)) == NULL)
348 /* No collisions, all clear... */
349 return set_nick_name(cptr, sptr, nick, parc, parv);
350
351 /*
352 * If acptr == sptr, then we have a client doing a nick
353 * change between *equivalent* nicknames as far as server
354 * is concerned (user is changing the case of his/her
355 * nickname or somesuch)
356 */
357 if (acptr == sptr)
358 {
359 if (strcmp(cli_name(acptr), nick) != 0)
360 /* Allows change of case in his/her nick */
361 return set_nick_name(cptr, sptr, nick, parc, parv);
362 else
363 /* Setting their nick to what it already is? Ignore it. */
364 return 0;
365 }
366 /* now we know we have a real collision. */
367 /*
368 * Note: From this point forward it can be assumed that
369 * acptr != sptr (point to different client structures).
370 */
371 assert(acptr != sptr);
372 /*
373 * If the older one is "non-person", the new entry is just
374 * allowed to overwrite it. Just silently drop non-person,
375 * and proceed with the nick. This should take care of the
376 * "dormant nick" way of generating collisions...
377 */
378 if (IsUnknown(acptr) && MyConnect(acptr))
379 {
380 ServerStats->is_ref++;
381 IPcheck_connect_fail(acptr);
382 exit_client(cptr, acptr, &me, "Overridden by other sign on");
383 return set_nick_name(cptr, sptr, nick, parc, parv);
384 }
385 /*
386 * Decide, we really have a nick collision and deal with it
387 */
388 /*
389 * NICK was coming from a server connection.
390 * This means we have a race condition (two users signing on
391 * at the same time), or two net fragments reconnecting with the same nick.
392 * The latter can happen because two different users connected
393 * or because one and the same user switched server during a net break.
394 * If the TimeStamps are equal, we kill both (or only 'new'
395 * if it was a ":server NICK new ...").
396 * Otherwise we kill the youngest when user@host differ,
397 * or the oldest when they are the same.
398 * We treat user and ~user as different, because if it wasn't
399 * a faked ~user the AUTH wouldn't have added the '~'.
400 * --Run
401 *
402 */
403 if (IsServer(sptr))
404 {
405 struct irc_in_addr ip;
406 /*
407 * A new NICK being introduced by a neighbouring
408 * server (e.g. message type ":server NICK new ..." received)
409 *
410 * compare IP address and username
411 */
412 base64toip(parv[parc - 3], &ip);
413 differ = (0 != memcmp(&cli_ip(acptr), &ip, sizeof(cli_ip(acptr)))) ||
414 (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
415 sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
416 "%C %Tu (%s user@host))", acptr, cli_from(acptr),
417 cli_lastnick(acptr), cptr, lastnick,
418 differ ? "Different" : "Same");
419 }
420 else
421 {
422 /*
423 * A NICK change has collided (e.g. message type ":old NICK new").
424 *
425 * compare IP address and username
426 */
427 differ = (0 != memcmp(&cli_ip(acptr), &cli_ip(sptr), sizeof(cli_ip(acptr)))) ||
428 (0 != ircd_strcmp(cli_user(acptr)->username, cli_user(sptr)->username));
429 sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
430 "%C (%C %Tu <- %C %Tu)", sptr, acptr, cli_from(acptr),
431 cli_lastnick(acptr), cptr, lastnick);
432 }
433 type = differ ? "older nick overruled" : "nick collision from same user@host";
434 /*
435 * Now remove (kill) the nick on our side if it is the youngest.
436 * If no timestamp was received, we ignore the incoming nick
437 * (and expect a KILL for our legit nick soon ):
438 * When the timestamps are equal we kill both nicks. --Run
439 * acptr->from != cptr should *always* be true (?).
440 *
441 * This exits the client sending the NICK message
442 */
443 if ((differ && lastnick >= cli_lastnick(acptr)) ||
444 (!differ && lastnick <= cli_lastnick(acptr)))
445 {
446 /* We need to bounce this kill straight back... Although the nick message
447 * for acptr is probably waiting in their recvq from me, its also possible
448 * that sptr will change their nick on cptr before cptr receives the
449 * nick message for acptr, which would leave acptr and sptr both alive
450 * on cptr, but only acptr alive on me, i.e. desync. This extra kill
451 * message has been absent for a while in ircu although it was a major
452 * problem when it was tried on efnet, so I don't know how big an issue it
453 * is. Probably best that this be left here, anyway...
454 */
455 ServerStats->is_kill++;
456 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
457 nick, cli_name(&me), type);
458 /* But if this was a nick change and not a nick introduction,
459 * we also need to ensure that we remove our local state
460 * record of the original client... Also, the rest of the
461 * net should be informed...
462 */
463 if (!IsServer(sptr))
464 {
465 assert(!MyConnect(sptr));
466 /* Inform the rest of the net... */
467 sendcmdto_serv_butone(&me, CMD_KILL, cptr, "%s :%s (%s)",
468 nick, cli_name(&me), type);
469 /* Don't go sending off a QUIT message... */
470 SetFlag(sptr, FLAG_KILLED);
471 /* Remove them locally. */
472 exit_client_msg(cptr, sptr, &me,
473 "Killed (%s (%s))",
474 feature_str(FEAT_HIS_SERVERNAME), type);
475 /*
476 * We have killed sptr off, zero out it's pointer so if it's used
477 * again we'll know about it --Bleep
478 */
479 sptr = NULL;
480 }
481 /* If the timestamps differ and we just killed sptr, we don't need to kill
482 * acptr as well.
483 */
484 if (lastnick != cli_lastnick(acptr))
485 return 0;
486 }
487 /* Tell acptr why we are killing it. */
488 send_reply(acptr, ERR_NICKCOLLISION, nick);
489
490 ServerStats->is_kill++;
491 SetFlag(acptr, FLAG_KILLED);
492 /*
493 * This exits the client we had before getting the NICK message
494 */
495 sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s"
496 " (%s)", acptr, feature_str(FEAT_HIS_SERVERNAME),
497 type);
498 exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
499 feature_str(FEAT_HIS_SERVERNAME), type);
500 if (lastnick == cli_lastnick(acptr))
501 return 0;
502 if (sptr == NULL)
503 return 0;
504 return set_nick_name(cptr, sptr, nick, parc, parv);
505 }