]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/m_nick.c
Update my e-mail address.
[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.2.2 2006/11/04 21:42:00 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 #include "gline.h"
102
103 /* #include <assert.h> -- Now using assert in ircd_log.h */
104 #include <stdlib.h>
105 #include <string.h>
106
107 /*
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...)
110 *
111 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
112 *
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.
116 *
117 * Note:
118 * The '~'-character should be allowed, but a change should be global,
119 * some confusion would result if only few servers allowed it...
120 */
121 static int do_nick_name(char* nick)
122 {
123 char* ch = nick;
124 char* end = ch + NICKLEN;
125 assert(0 != ch);
126
127 /* first character in [0..9-] */
128 if (*ch == '-' || IsDigit(*ch))
129 return 0;
130 for ( ; (ch < end) && *ch; ++ch)
131 if (!IsNickChar(*ch))
132 break;
133
134 *ch = '\0';
135
136 return (ch - nick);
137 }
138
139 /*
140 * m_nick - message handler for local clients
141 * parv[0] = sender prefix
142 * parv[1] = nickname
143 */
144 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
145 {
146 struct Client* acptr;
147 char nick[NICKLEN + 2];
148 char* arg;
149 char* s;
150 const char* client_name;
151
152 assert(0 != cptr);
153 assert(cptr == sptr);
154
155 if (IsServerPort(cptr))
156 return exit_client(cptr, cptr, &me, "Use a different port");
157
158 /*
159 * parv[0] will be empty for clients connecting for the first time
160 */
161 client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
162
163 if (parc < 2) {
164 send_reply(sptr, ERR_NONICKNAMEGIVEN);
165 return 0;
166 }
167
168 /*
169 * Don't let them send make us send back a really long string of
170 * garbage
171 */
172 arg = parv[1];
173 if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
174 arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
175
176 if ((s = strchr(arg, '~')))
177 *s = '\0';
178
179 strcpy(nick, arg);
180
181 /*
182 * If do_nick_name() returns a null name then reject it.
183 */
184 if (0 == do_nick_name(nick)) {
185 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
186 return 0;
187 }
188
189 if (IsRegistered(sptr) && !IsAnOper(sptr) && IsNickGlined(sptr, nick)) {
190 send_reply(sptr, ERR_ERRONEUSNICKNAME, nick);
191 return 0;
192 }
193
194 /*
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...
197 */
198 if (isNickJuped(nick)) {
199 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
200 return 0; /* NICK message ignored */
201 }
202
203 if (!(acptr = FindClient(nick))) {
204 /*
205 * No collisions, all clear...
206 */
207 return set_nick_name(cptr, sptr, nick, parc, parv);
208 }
209 if (IsServer(acptr)) {
210 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
211 return 0; /* NICK message ignored */
212 }
213 /*
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)
218 */
219 if (acptr == sptr) {
220 /*
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)
225 */
226 if (0 != strcmp(cli_name(acptr), nick)) {
227 /*
228 * Allows change of case in his/her nick
229 */
230 return set_nick_name(cptr, sptr, nick, parc, parv);
231 }
232 /*
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.
238 */
239 return 0;
240 }
241 /*
242 * Note: From this point forward it can be assumed that
243 * acptr != sptr (point to different client structures).
244 */
245 assert(acptr != sptr);
246 /*
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...
251 *
252 * XXX - hmmm can this happen after one is registered?
253 *
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
257 * the message below.
258 */
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);
264 }
265 /*
266 * NICK is coming from local client connection. Just
267 * send error reply and ignore the command.
268 */
269 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
270 return 0; /* NICK message ignored */
271 }
272
273
274 /*
275 * ms_nick - server message handler for nicks
276 * parv[0] = sender prefix
277 * parv[1] = nickname
278 *
279 * If from server, source is client:
280 * parv[2] = timestamp
281 *
282 * Source is server:
283 * parv[2] = hopcount
284 * parv[3] = timestamp
285 * parv[4] = username
286 * parv[5] = hostname
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
291 * parv[0] = server
292 */
293 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
294 {
295 struct Client *acptr;
296 char nick[NICKLEN + 2];
297 time_t lastnick = 0;
298 int differ = 1;
299 const char *type;
300
301 assert(0 != cptr);
302 assert(0 != sptr);
303 assert(IsServer(cptr));
304
305 if ((IsServer(sptr) && parc < 8) || parc < 3)
306 {
307 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
308 parv[1], cptr);
309 return need_more_params(sptr, "NICK");
310 }
311
312 ircd_strncpy(nick, parv[1], NICKLEN);
313 nick[NICKLEN] = '\0';
314
315 if (IsServer(sptr))
316 {
317 lastnick = atoi(parv[3]);
318 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
319 cli_serv(sptr)->lag = TStime() - lastnick;
320 }
321 else
322 {
323 lastnick = atoi(parv[2]);
324 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
325 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
326 }
327 /*
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
332 */
333 if (!do_nick_name(nick) || strcmp(nick, parv[1]))
334 {
335 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
336
337 ++ServerStats->is_kill;
338 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
339 parv[0], cptr);
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));
343 if (!IsServer(sptr))
344 {
345 /*
346 * bad nick _change_
347 */
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) :
352 cli_name(cptr));
353 }
354 return 0;
355 }
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);
360
361 /*
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)
366 */
367 if (acptr == sptr)
368 {
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);
372 else
373 /* Setting their nick to what it already is? Ignore it. */
374 return 0;
375 }
376 /* now we know we have a real collision. */
377 /*
378 * Note: From this point forward it can be assumed that
379 * acptr != sptr (point to different client structures).
380 */
381 assert(acptr != sptr);
382 /*
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...
387 */
388 if (IsUnknown(acptr) && MyConnect(acptr))
389 {
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);
394 }
395 /*
396 * Decide, we really have a nick collision and deal with it
397 */
398 /*
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 '~'.
410 * --Run
411 *
412 */
413 if (IsServer(sptr))
414 {
415 struct irc_in_addr ip;
416 /*
417 * A new NICK being introduced by a neighbouring
418 * server (e.g. message type ":server NICK new ..." received)
419 *
420 * compare IP address and username
421 */
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");
429 }
430 else
431 {
432 /*
433 * A NICK change has collided (e.g. message type ":old NICK new").
434 *
435 * compare IP address and username
436 */
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);
442 }
443 type = differ ? "overruled by older nick" : "nick collision from same user@host";
444 /*
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 (?).
450 *
451 * This exits the client sending the NICK message
452 */
453 if ((differ && lastnick >= cli_lastnick(acptr)) ||
454 (!differ && lastnick <= cli_lastnick(acptr)))
455 {
456 ServerStats->is_kill++;
457 if (!IsServer(sptr))
458 {
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.
462 */
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,
471 "Killed (%s (%s))",
472 feature_str(FEAT_HIS_SERVERNAME), type);
473 }
474 else
475 {
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.
479 */
480 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
481 parv[parc - 2], cli_name(&me), type);
482 }
483 /* If the timestamps differ and we just killed sptr, we don't need to kill
484 * acptr as well.
485 */
486 if (lastnick != cli_lastnick(acptr))
487 return 0;
488 }
489 /* Tell acptr why we are killing it. */
490 send_reply(acptr, ERR_NICKCOLLISION, nick);
491
492 ServerStats->is_kill++;
493 SetFlag(acptr, FLAG_KILLED);
494 /*
495 * This exits the client we had before getting the NICK message
496 */
497 sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s (%s)",
498 acptr, feature_str(FEAT_HIS_SERVERNAME),
499 type);
500 exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
501 feature_str(FEAT_HIS_SERVERNAME), type);
502 if (lastnick == cli_lastnick(acptr))
503 return 0;
504 if (sptr == NULL)
505 return 0;
506 return set_nick_name(cptr, sptr, nick, parc, parv);
507 }