]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/m_nick.c
Should be unsigned long for A
[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.1 2005/11/16 04:18:47 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 /*
156 * parv[0] will be empty for clients connecting for the first time
157 */
158 client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
159
160 if (parc < 2) {
161 send_reply(sptr, ERR_NONICKNAMEGIVEN);
162 return 0;
163 }
164 /*
165 * Don't let them send make us send back a really long string of
166 * garbage
167 */
168 arg = parv[1];
169 if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
170 arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
171
172 if ((s = strchr(arg, '~')))
173 *s = '\0';
174
175 strcpy(nick, arg);
176
177 /*
178 * If do_nick_name() returns a null name then reject it.
179 */
180 if (0 == do_nick_name(nick)) {
181 send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
182 return 0;
183 }
184
185 if (IsRegistered(sptr) && !IsAnOper(sptr) && IsNickGlined(sptr, nick)) {
186 send_reply(sptr, ERR_ERRONEUSNICKNAME, nick);
187 return 0;
188 }
189
190 /*
191 * Check if this is a LOCAL user trying to use a reserved (Juped)
192 * nick, if so tell him that it's a nick in use...
193 */
194 if (isNickJuped(nick)) {
195 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
196 return 0; /* NICK message ignored */
197 }
198
199 if (!(acptr = FindClient(nick))) {
200 /*
201 * No collisions, all clear...
202 */
203 return set_nick_name(cptr, sptr, nick, parc, parv);
204 }
205 if (IsServer(acptr)) {
206 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
207 return 0; /* NICK message ignored */
208 }
209 /*
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)
214 */
215 if (acptr == sptr) {
216 /*
217 * If acptr == sptr, then we have a client doing a nick
218 * change between *equivalent* nicknames as far as server
219 * is concerned (user is changing the case of his/her
220 * nickname or somesuch)
221 */
222 if (0 != strcmp(cli_name(acptr), nick)) {
223 /*
224 * Allows change of case in his/her nick
225 */
226 return set_nick_name(cptr, sptr, nick, parc, parv);
227 }
228 /*
229 * This is just ':old NICK old' type thing.
230 * Just forget the whole thing here. There is
231 * no point forwarding it to anywhere,
232 * especially since servers prior to this
233 * version would treat it as nick collision.
234 */
235 return 0;
236 }
237 /*
238 * Note: From this point forward it can be assumed that
239 * acptr != sptr (point to different client structures).
240 */
241 assert(acptr != sptr);
242 /*
243 * If the older one is "non-person", the new entry is just
244 * allowed to overwrite it. Just silently drop non-person,
245 * and proceed with the nick. This should take care of the
246 * "dormant nick" way of generating collisions...
247 *
248 * XXX - hmmm can this happen after one is registered?
249 *
250 * Yes, client 1 connects to IRC and registers, client 2 connects and
251 * sends "NICK foo" but doesn't send anything more. client 1 now does
252 * /nick foo, they should succeed and client 2 gets disconnected with
253 * the message below.
254 */
255 if (IsUnknown(acptr) && MyConnect(acptr)) {
256 ServerStats->is_ref++;
257 IPcheck_connect_fail(acptr);
258 exit_client(cptr, acptr, &me, "Overridden by other sign on");
259 return set_nick_name(cptr, sptr, nick, parc, parv);
260 }
261 /*
262 * NICK is coming from local client connection. Just
263 * send error reply and ignore the command.
264 */
265 send_reply(sptr, ERR_NICKNAMEINUSE, nick);
266 return 0; /* NICK message ignored */
267 }
268
269
270 /*
271 * ms_nick - server message handler for nicks
272 * parv[0] = sender prefix
273 * parv[1] = nickname
274 *
275 * If from server, source is client:
276 * parv[2] = timestamp
277 *
278 * Source is server:
279 * parv[2] = hopcount
280 * parv[3] = timestamp
281 * parv[4] = username
282 * parv[5] = hostname
283 * parv[6] = umode (optional)
284 * parv[parc-3] = IP# <- Only Protocol >= 10
285 * parv[parc-2] = YXX, numeric nick <- Only Protocol >= 10
286 * parv[parc-1] = info
287 * parv[0] = server
288 */
289 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
290 {
291 struct Client *acptr;
292 char nick[NICKLEN + 2];
293 time_t lastnick = 0;
294 int differ = 1;
295 const char *type;
296
297 assert(0 != cptr);
298 assert(0 != sptr);
299 assert(IsServer(cptr));
300
301 if ((IsServer(sptr) && parc < 8) || parc < 3)
302 {
303 sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
304 parv[1], cptr);
305 return need_more_params(sptr, "NICK");
306 }
307
308 ircd_strncpy(nick, parv[1], NICKLEN);
309 nick[NICKLEN] = '\0';
310
311 if (IsServer(sptr))
312 {
313 lastnick = atoi(parv[3]);
314 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
315 cli_serv(sptr)->lag = TStime() - lastnick;
316 }
317 else
318 {
319 lastnick = atoi(parv[2]);
320 if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
321 cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
322 }
323 /*
324 * If do_nick_name() returns a null name OR if the server sent a nick
325 * name and do_nick_name() changed it in some way (due to rules of nick
326 * creation) then reject it. If from a server and we reject it,
327 * and KILL it. -avalon 4/4/92
328 */
329 if (!do_nick_name(nick) || strcmp(nick, parv[1]))
330 {
331 send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
332
333 ++ServerStats->is_kill;
334 sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
335 parv[0], cptr);
336 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
337 IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
338 nick, cli_name(cptr));
339 if (!IsServer(sptr))
340 {
341 /*
342 * bad nick _change_
343 */
344 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
345 parv[0], cli_name(&me), cli_name(cptr), parv[0],
346 cli_user(sptr) ? cli_username(sptr) : "",
347 cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
348 cli_name(cptr));
349 }
350 return 0;
351 }
352 /* Check against nick name collisions. */
353 if ((acptr = FindClient(nick)) == NULL)
354 /* No collisions, all clear... */
355 return set_nick_name(cptr, sptr, nick, parc, parv);
356
357 /*
358 * If acptr == sptr, then we have a client doing a nick
359 * change between *equivalent* nicknames as far as server
360 * is concerned (user is changing the case of his/her
361 * nickname or somesuch)
362 */
363 if (acptr == sptr)
364 {
365 if (strcmp(cli_name(acptr), nick) != 0)
366 /* Allows change of case in his/her nick */
367 return set_nick_name(cptr, sptr, nick, parc, parv);
368 else
369 /* Setting their nick to what it already is? Ignore it. */
370 return 0;
371 }
372 /* now we know we have a real collision. */
373 /*
374 * Note: From this point forward it can be assumed that
375 * acptr != sptr (point to different client structures).
376 */
377 assert(acptr != sptr);
378 /*
379 * If the older one is "non-person", the new entry is just
380 * allowed to overwrite it. Just silently drop non-person,
381 * and proceed with the nick. This should take care of the
382 * "dormant nick" way of generating collisions...
383 */
384 if (IsUnknown(acptr) && MyConnect(acptr))
385 {
386 ServerStats->is_ref++;
387 IPcheck_connect_fail(acptr);
388 exit_client(cptr, acptr, &me, "Overridden by other sign on");
389 return set_nick_name(cptr, sptr, nick, parc, parv);
390 }
391 /*
392 * Decide, we really have a nick collision and deal with it
393 */
394 /*
395 * NICK was coming from a server connection.
396 * This means we have a race condition (two users signing on
397 * at the same time), or two net fragments reconnecting with the same nick.
398 * The latter can happen because two different users connected
399 * or because one and the same user switched server during a net break.
400 * If the TimeStamps are equal, we kill both (or only 'new'
401 * if it was a ":server NICK new ...").
402 * Otherwise we kill the youngest when user@host differ,
403 * or the oldest when they are the same.
404 * We treat user and ~user as different, because if it wasn't
405 * a faked ~user the AUTH wouldn't have added the '~'.
406 * --Run
407 *
408 */
409 if (IsServer(sptr))
410 {
411 struct irc_in_addr ip;
412 /*
413 * A new NICK being introduced by a neighbouring
414 * server (e.g. message type ":server NICK new ..." received)
415 *
416 * compare IP address and username
417 */
418 base64toip(parv[parc - 3], &ip);
419 differ = (0 != memcmp(&cli_ip(acptr), &ip, sizeof(cli_ip(acptr)))) ||
420 (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
421 sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
422 "%C %Tu (%s user@host))", acptr, cli_from(acptr),
423 cli_lastnick(acptr), cptr, lastnick,
424 differ ? "Different" : "Same");
425 }
426 else
427 {
428 /*
429 * A NICK change has collided (e.g. message type ":old NICK new").
430 *
431 * compare IP address and username
432 */
433 differ = (0 != memcmp(&cli_ip(acptr), &cli_ip(sptr), sizeof(cli_ip(acptr)))) ||
434 (0 != ircd_strcmp(cli_user(acptr)->username, cli_user(sptr)->username));
435 sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
436 "%C (%C %Tu <- %C %Tu)", sptr, acptr, cli_from(acptr),
437 cli_lastnick(acptr), cptr, lastnick);
438 }
439 type = differ ? "overruled by older nick" : "nick collision from same user@host";
440 /*
441 * Now remove (kill) the nick on our side if it is the youngest.
442 * If no timestamp was received, we ignore the incoming nick
443 * (and expect a KILL for our legit nick soon ):
444 * When the timestamps are equal we kill both nicks. --Run
445 * acptr->from != cptr should *always* be true (?).
446 *
447 * This exits the client sending the NICK message
448 */
449 if ((differ && lastnick >= cli_lastnick(acptr)) ||
450 (!differ && lastnick <= cli_lastnick(acptr)))
451 {
452 ServerStats->is_kill++;
453 if (!IsServer(sptr))
454 {
455 /* If this was a nick change and not a nick introduction, we
456 * need to ensure that we remove our record of the client, and
457 * send a KILL to the whole network.
458 */
459 assert(!MyConnect(sptr));
460 /* Inform the rest of the net... */
461 sendcmdto_serv_butone(&me, CMD_KILL, 0, "%C :%s (%s)",
462 sptr, cli_name(&me), type);
463 /* Don't go sending off a QUIT message... */
464 SetFlag(sptr, FLAG_KILLED);
465 /* Remove them locally. */
466 exit_client_msg(cptr, sptr, &me,
467 "Killed (%s (%s))",
468 feature_str(FEAT_HIS_SERVERNAME), type);
469 }
470 else
471 {
472 /* If the origin is a server, this was a new client, so we only
473 * send the KILL in the direction it came from. We have no
474 * client record that we would have to clean up.
475 */
476 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
477 parv[parc - 2], cli_name(&me), type);
478 }
479 /* If the timestamps differ and we just killed sptr, we don't need to kill
480 * acptr as well.
481 */
482 if (lastnick != cli_lastnick(acptr))
483 return 0;
484 }
485 /* Tell acptr why we are killing it. */
486 send_reply(acptr, ERR_NICKCOLLISION, nick);
487
488 ServerStats->is_kill++;
489 SetFlag(acptr, FLAG_KILLED);
490 /*
491 * This exits the client we had before getting the NICK message
492 */
493 sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s (%s)",
494 acptr, feature_str(FEAT_HIS_SERVERNAME),
495 type);
496 exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
497 feature_str(FEAT_HIS_SERVERNAME), type);
498 if (lastnick == cli_lastnick(acptr))
499 return 0;
500 if (sptr == NULL)
501 return 0;
502 return set_nick_name(cptr, sptr, nick, parc, parv);
503 }