]> jfr.im git - solanum.git/blame - modules/core/m_nick.c
Remove s_assert definition from ircd_defs.h and add it to its own header.
[solanum.git] / modules / core / m_nick.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_nick.c: Sets a users nick.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
66b4a7ae 24 * $Id: m_nick.c 3518 2007-06-22 21:59:09Z jilles $
212380e3
AC
25 */
26
27#include "stdinc.h"
28#include "client.h"
29#include "hash.h"
4562c604 30#include "match.h"
212380e3
AC
31#include "ircd.h"
32#include "numeric.h"
33#include "s_conf.h"
34#include "s_stats.h"
35#include "s_user.h"
36#include "hash.h"
37#include "whowas.h"
38#include "s_serv.h"
39#include "send.h"
40#include "channel.h"
4016731b 41#include "logger.h"
212380e3
AC
42#include "msg.h"
43#include "parse.h"
44#include "modules.h"
45#include "common.h"
46#include "packet.h"
47#include "scache.h"
48#include "s_newconf.h"
49#include "monitor.h"
77d3d2db 50#include "s_assert.h"
212380e3 51
3f7e0642
JT
52/* Give all UID nicks the same TS. This ensures nick TS is always the same on
53 * all servers for each nick-user pair, also if a user with a UID nick changes
54 * their nick but is collided again (the server detecting the collision will
55 * not propagate the nick change further). -- jilles
56 */
57#define SAVE_NICKTS 100
58
212380e3
AC
59static int mr_nick(struct Client *, struct Client *, int, const char **);
60static int m_nick(struct Client *, struct Client *, int, const char **);
61static int mc_nick(struct Client *, struct Client *, int, const char **);
62static int ms_nick(struct Client *, struct Client *, int, const char **);
63static int ms_uid(struct Client *, struct Client *, int, const char **);
64static int ms_euid(struct Client *, struct Client *, int, const char **);
65static int ms_save(struct Client *, struct Client *, int, const char **);
66static int can_save(struct Client *);
67static void save_user(struct Client *, struct Client *, struct Client *);
ad35b2cd 68static void bad_nickname(struct Client *, const char *);
212380e3
AC
69
70struct Message nick_msgtab = {
71 "NICK", 0, 0, 0, MFLG_SLOW,
6c77f1f7 72 {{mr_nick, 0}, {m_nick, 0}, {mc_nick, 3}, {ms_nick, 0}, mg_ignore, {m_nick, 0}}
212380e3
AC
73};
74struct Message uid_msgtab = {
75 "UID", 0, 0, 0, MFLG_SLOW,
76 {mg_ignore, mg_ignore, mg_ignore, {ms_uid, 9}, mg_ignore, mg_ignore}
77};
78struct Message euid_msgtab = {
79 "EUID", 0, 0, 0, MFLG_SLOW,
80 {mg_ignore, mg_ignore, mg_ignore, {ms_euid, 12}, mg_ignore, mg_ignore}
81};
82struct Message save_msgtab = {
83 "SAVE", 0, 0, 0, MFLG_SLOW,
84 {mg_ignore, mg_ignore, mg_ignore, {ms_save, 3}, mg_ignore, mg_ignore}
85};
86
87mapi_clist_av1 nick_clist[] = { &nick_msgtab, &uid_msgtab, &euid_msgtab,
88 &save_msgtab, NULL };
89
66b4a7ae 90DECLARE_MODULE_AV1(nick, NULL, NULL, nick_clist, NULL, NULL, "$Revision: 3518 $");
212380e3
AC
91
92static int change_remote_nick(struct Client *, struct Client *, time_t,
93 const char *, int);
94
95static int clean_nick(const char *, int loc_client);
96static int clean_username(const char *);
97static int clean_host(const char *);
561d7efc 98static int clean_uid(const char *uid, const char *sid);
212380e3
AC
99
100static void set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick);
101static void change_local_nick(struct Client *client_p, struct Client *source_p, char *nick, int);
102static int register_client(struct Client *client_p, struct Client *server,
103 const char *nick, time_t newts, int parc, const char *parv[]);
104
105static int perform_nick_collides(struct Client *, struct Client *,
106 struct Client *, int, const char **,
107 time_t, const char *, const char *);
108static int perform_nickchange_collides(struct Client *, struct Client *,
109 struct Client *, int, const char **, time_t, const char *);
110
111/* mr_nick()
212380e3
AC
112 * parv[1] = nickname
113 */
114static int
115mr_nick(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
116{
117 struct Client *target_p;
118 char nick[NICKLEN];
212380e3
AC
119
120 if (strlen(client_p->id) == 3)
121 {
122 exit_client(client_p, client_p, client_p, "Mixing client and server protocol");
123 return 0;
124 }
125
fda96b89 126 if(parc < 2 || EmptyString(parv[1]))
212380e3
AC
127 {
128 sendto_one(source_p, form_str(ERR_NONICKNAMEGIVEN),
129 me.name, EmptyString(source_p->name) ? "*" : source_p->name);
130 return 0;
131 }
132
212380e3 133 /* copy the nick and terminate it */
b583faf9 134 rb_strlcpy(nick, parv[1], ConfigFileEntry.nicklen);
212380e3
AC
135
136 /* check the nickname is ok */
137 if(!clean_nick(nick, 1))
138 {
139 sendto_one(source_p, form_str(ERR_ERRONEUSNICKNAME),
3dfaa671 140 me.name, EmptyString(source_p->name) ? "*" : source_p->name, parv[1]);
212380e3
AC
141 return 0;
142 }
143
144 /* check if the nick is resv'd */
145 if(find_nick_resv(nick))
146 {
147 sendto_one(source_p, form_str(ERR_ERRONEUSNICKNAME),
148 me.name, EmptyString(source_p->name) ? "*" : source_p->name, nick);
149 return 0;
150 }
151
b37021a4 152 if(irc_dictionary_find(nd_dict, nick))
212380e3
AC
153 {
154 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
155 me.name, EmptyString(source_p->name) ? "*" : source_p->name, nick);
156 return 0;
157 }
158
159 if((target_p = find_named_client(nick)) == NULL)
160 set_initial_nick(client_p, source_p, nick);
161 else if(source_p == target_p)
162 strcpy(source_p->name, nick);
163 else
164 sendto_one(source_p, form_str(ERR_NICKNAMEINUSE), me.name, "*", nick);
165
166 return 0;
167}
168
169/* m_nick()
212380e3
AC
170 * parv[1] = nickname
171 */
172static int
173m_nick(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
174{
175 struct Client *target_p;
176 char nick[NICKLEN];
212380e3 177
fda96b89 178 if(parc < 2 || EmptyString(parv[1]))
212380e3
AC
179 {
180 sendto_one(source_p, form_str(ERR_NONICKNAMEGIVEN), me.name, source_p->name);
181 return 0;
182 }
183
212380e3
AC
184 /* mark end of grace period, to prevent nickflooding */
185 if(!IsFloodDone(source_p))
186 flood_endgrace(source_p);
187
188 /* terminate nick to NICKLEN, we dont want clean_nick() to error! */
b583faf9 189 rb_strlcpy(nick, parv[1], ConfigFileEntry.nicklen);
212380e3
AC
190
191 /* check the nickname is ok */
192 if(!clean_nick(nick, 1))
193 {
3dfaa671 194 sendto_one(source_p, form_str(ERR_ERRONEUSNICKNAME), me.name, source_p->name, nick);
212380e3
AC
195 return 0;
196 }
197
198 if(!IsExemptResv(source_p) && find_nick_resv(nick))
199 {
200 sendto_one(source_p, form_str(ERR_ERRONEUSNICKNAME), me.name, source_p->name, nick);
201 return 0;
202 }
203
b37021a4 204 if(irc_dictionary_find(nd_dict, nick))
212380e3
AC
205 {
206 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
207 me.name, EmptyString(source_p->name) ? "*" : source_p->name, nick);
208 return 0;
209 }
210
211 if((target_p = find_named_client(nick)))
212 {
213 /* If(target_p == source_p) the client is changing nicks between
214 * equivalent nicknames ie: [nick] -> {nick}
215 */
216 if(target_p == source_p)
217 {
218 /* check the nick isnt exactly the same */
219 if(strcmp(target_p->name, nick))
220 change_local_nick(client_p, source_p, nick, 1);
221
222 }
223
224 /* drop unregged client */
225 else if(IsUnknown(target_p))
226 {
227 exit_client(NULL, target_p, &me, "Overridden");
228 change_local_nick(client_p, source_p, nick, 1);
229 }
230 else
3dfaa671 231 sendto_one(source_p, form_str(ERR_NICKNAMEINUSE), me.name, source_p->name, nick);
212380e3
AC
232
233 return 0;
234 }
235 else
236 change_local_nick(client_p, source_p, nick, 1);
237
238 return 0;
239}
240
6c77f1f7 241/* mc_nick()
212380e3
AC
242 *
243 * server -> server nick change
212380e3
AC
244 * parv[1] = nickname
245 * parv[2] = TS when nick change
212380e3
AC
246 */
247static int
248mc_nick(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
249{
250 struct Client *target_p;
251 time_t newts = 0;
252
253 /* if nicks erroneous, or too long, kill */
254 if(!clean_nick(parv[1], 0))
255 {
ad35b2cd 256 bad_nickname(client_p, parv[1]);
212380e3
AC
257 return 0;
258 }
259
260 newts = atol(parv[2]);
261 target_p = find_named_client(parv[1]);
262
263 /* if the nick doesnt exist, allow it and process like normal */
264 if(target_p == NULL)
265 {
266 change_remote_nick(client_p, source_p, newts, parv[1], 1);
267 }
268 else if(IsUnknown(target_p))
269 {
270 exit_client(NULL, target_p, &me, "Overridden");
271 change_remote_nick(client_p, source_p, newts, parv[1], 1);
272 }
273 else if(target_p == source_p)
274 {
275 /* client changing case of nick */
276 if(strcmp(target_p->name, parv[1]))
277 change_remote_nick(client_p, source_p, newts, parv[1], 1);
278 }
279 /* we've got a collision! */
280 else
281 perform_nickchange_collides(source_p, client_p, target_p,
282 parc, parv, newts, parv[1]);
283
284 return 0;
285}
286
287static int
288ms_nick(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
289{
6c77f1f7 290 const char *nick, *server;
212380e3 291
6c77f1f7
JT
292 nick = parc > 1 ? parv[1] : "?";
293 server = parc > 7 ? parv[7] : "?";
212380e3 294
6c77f1f7
JT
295 sendto_wallops_flags(UMODE_WALLOP, &me,
296 "Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
297 client_p->name, nick, server);
298 sendto_server(NULL, NULL, CAP_TS6, NOCAPS,
299 ":%s WALLOPS :Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
300 me.id, client_p->name, nick, server);
301 ilog(L_SERVER, "Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
302 client_p->name, nick, server);
212380e3 303
6c77f1f7 304 exit_client(client_p, client_p, &me, "TS5 nickname introduced");
212380e3
AC
305
306 return 0;
307}
308
309/* ms_uid()
310 * parv[1] - nickname
311 * parv[2] - hops
312 * parv[3] - TS
313 * parv[4] - umodes
314 * parv[5] - username
315 * parv[6] - hostname
316 * parv[7] - IP
317 * parv[8] - UID
318 * parv[9] - gecos
319 */
320static int
321ms_uid(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
322{
323 struct Client *target_p;
324 time_t newts = 0;
4a5655b2 325 char squitreason[120];
212380e3
AC
326
327 newts = atol(parv[3]);
328
329 if(parc != 10)
330 {
331 sendto_realops_snomask(SNO_GENERAL, L_ALL,
332 "Dropping server %s due to (invalid) command 'UID' "
333 "with %d arguments (expecting 10)", client_p->name, parc);
334 ilog(L_SERVER, "Excess parameters (%d) for command 'UID' from %s.",
335 parc, client_p->name);
56023eb2
JT
336 rb_snprintf(squitreason, sizeof squitreason,
337 "Excess parameters (%d) to %s command, expecting %d",
338 parc, "UID", 10);
339 exit_client(client_p, client_p, client_p, squitreason);
212380e3
AC
340 return 0;
341 }
342
343 /* if nicks erroneous, or too long, kill */
344 if(!clean_nick(parv[1], 0))
345 {
ad35b2cd 346 bad_nickname(client_p, parv[1]);
212380e3
AC
347 return 0;
348 }
349
561d7efc 350 if(!clean_uid(parv[8], source_p->id))
212380e3 351 {
4a5655b2 352 rb_snprintf(squitreason, sizeof squitreason,
e0c7937a
JT
353 "Invalid UID %s for nick %s on %s/%s",
354 parv[8], parv[1], source_p->name, source_p->id);
4a5655b2 355 exit_client(client_p, client_p, client_p, squitreason);
212380e3
AC
356 return 0;
357 }
358
4a5655b2 359 if(!clean_username(parv[5]) || !clean_host(parv[6]))
212380e3 360 {
47adde3d 361 ServerStats.is_kill++;
212380e3 362 sendto_realops_snomask(SNO_DEBUG, L_ALL,
4a5655b2
JT
363 "Bad user@host: %s@%s From: %s(via %s)",
364 parv[5], parv[6], source_p->name, client_p->name);
365 sendto_one(client_p, ":%s KILL %s :%s (Bad user@host)", me.id, parv[8], me.name);
212380e3
AC
366 return 0;
367 }
368
369 /* check length of clients gecos */
370 if(strlen(parv[9]) > REALLEN)
371 {
372 char *s = LOCAL_COPY(parv[9]);
373 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Long realname from server %s for %s",
3dfaa671 374 source_p->name, parv[1]);
212380e3
AC
375 s[REALLEN] = '\0';
376 parv[9] = s;
377 }
378
379 target_p = find_named_client(parv[1]);
380
381 if(target_p == NULL)
382 {
383 register_client(client_p, source_p, parv[1], newts, parc, parv);
384 }
385 else if(IsUnknown(target_p))
386 {
387 exit_client(NULL, target_p, &me, "Overridden");
388 register_client(client_p, source_p, parv[1], newts, parc, parv);
389 }
390 /* we've got a collision! */
391 else
392 perform_nick_collides(source_p, client_p, target_p, parc, parv,
393 newts, parv[1], parv[8]);
394
395 return 0;
396}
397
398/* ms_euid()
399 * parv[1] - nickname
400 * parv[2] - hops
401 * parv[3] - TS
402 * parv[4] - umodes
403 * parv[5] - username
404 * parv[6] - hostname
405 * parv[7] - IP
406 * parv[8] - UID
407 * parv[9] - realhost
408 * parv[10] - account
409 * parv[11] - gecos
410 */
411static int
412ms_euid(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
413{
414 struct Client *target_p;
415 time_t newts = 0;
4a5655b2 416 char squitreason[120];
212380e3
AC
417
418 newts = atol(parv[3]);
419
420 if(parc != 12)
421 {
422 sendto_realops_snomask(SNO_GENERAL, L_ALL,
423 "Dropping server %s due to (invalid) command 'EUID' "
424 "with %d arguments (expecting 12)", client_p->name, parc);
425 ilog(L_SERVER, "Excess parameters (%d) for command 'EUID' from %s.",
426 parc, client_p->name);
56023eb2
JT
427 rb_snprintf(squitreason, sizeof squitreason,
428 "Excess parameters (%d) to %s command, expecting %d",
429 parc, "EUID", 12);
430 exit_client(client_p, client_p, client_p, squitreason);
212380e3
AC
431 return 0;
432 }
433
434 /* if nicks erroneous, or too long, kill */
435 if(!clean_nick(parv[1], 0))
436 {
ad35b2cd 437 bad_nickname(client_p, parv[1]);
212380e3
AC
438 return 0;
439 }
440
561d7efc 441 if(!clean_uid(parv[8], source_p->id))
212380e3 442 {
4a5655b2 443 rb_snprintf(squitreason, sizeof squitreason,
e0c7937a
JT
444 "Invalid UID %s for nick %s on %s/%s",
445 parv[8], parv[1], source_p->name, source_p->id);
4a5655b2 446 exit_client(client_p, client_p, client_p, squitreason);
212380e3
AC
447 return 0;
448 }
449
4a5655b2 450 if(!clean_username(parv[5]) || !clean_host(parv[6]))
212380e3 451 {
47adde3d 452 ServerStats.is_kill++;
212380e3 453 sendto_realops_snomask(SNO_DEBUG, L_ALL,
4a5655b2
JT
454 "Bad user@host: %s@%s From: %s(via %s)",
455 parv[5], parv[6], source_p->name, client_p->name);
456 sendto_one(client_p, ":%s KILL %s :%s (Bad user@host)", me.id, parv[8], me.name);
212380e3
AC
457 return 0;
458 }
459
460 if(strcmp(parv[9], "*") && !clean_host(parv[9]))
461 {
47adde3d 462 ServerStats.is_kill++;
212380e3
AC
463 sendto_realops_snomask(SNO_DEBUG, L_ALL,
464 "Bad realhost: %s From: %s(via %s)",
465 parv[9], source_p->name, client_p->name);
466 sendto_one(client_p, ":%s KILL %s :%s (Bad user@host)", me.id, parv[8], me.name);
467 return 0;
468 }
469
470 /* check length of clients gecos */
471 if(strlen(parv[11]) > REALLEN)
472 {
473 char *s = LOCAL_COPY(parv[11]);
474 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Long realname from server %s for %s",
3dfaa671 475 source_p->name, parv[1]);
212380e3
AC
476 s[REALLEN] = '\0';
477 parv[11] = s;
478 }
479
480 target_p = find_named_client(parv[1]);
481
482 if(target_p == NULL)
483 {
484 register_client(client_p, source_p, parv[1], newts, parc, parv);
485 }
486 else if(IsUnknown(target_p))
487 {
488 exit_client(NULL, target_p, &me, "Overridden");
489 register_client(client_p, source_p, parv[1], newts, parc, parv);
490 }
491 /* we've got a collision! */
492 else
493 perform_nick_collides(source_p, client_p, target_p, parc, parv,
494 newts, parv[1], parv[8]);
495
496 return 0;
497}
498
499/* ms_save()
500 * parv[1] - UID
501 * parv[2] - TS
502 */
503static int
504ms_save(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
505{
506 struct Client *target_p;
507
508 target_p = find_id(parv[1]);
509 if (target_p == NULL)
510 return 0;
511 if (!IsPerson(target_p))
512 sendto_realops_snomask(SNO_GENERAL, L_ALL,
513 "Ignored SAVE message for non-person %s from %s",
514 target_p->name, source_p->name);
515 else if (IsDigit(target_p->name[0]))
516 sendto_realops_snomask(SNO_DEBUG, L_ALL,
517 "Ignored noop SAVE message for %s from %s",
518 target_p->name, source_p->name);
519 else if (target_p->tsinfo == atol(parv[2]))
520 save_user(client_p, source_p, target_p);
521 else
522 sendto_realops_snomask(SNO_SKILL, L_ALL,
523 "Ignored SAVE message for %s from %s",
524 target_p->name, source_p->name);
525 return 0;
526}
527
528/* clean_nick()
529 *
530 * input - nickname to check
531 * output - 0 if erroneous, else 1
532 * side effects -
533 */
534static int
535clean_nick(const char *nick, int loc_client)
536{
537 int len = 0;
538
539 /* nicks cant start with a digit or -, and must have a length */
540 if(*nick == '-' || *nick == '\0')
541 return 0;
542
543 if(loc_client && IsDigit(*nick))
544 return 0;
545
546 for(; *nick; nick++)
547 {
548 len++;
549 if(!IsNickChar(*nick))
550 return 0;
551 }
552
553 /* nicklen is +1 */
b583faf9 554 if(len >= NICKLEN && len >= ConfigFileEntry.nicklen)
212380e3
AC
555 return 0;
556
557 return 1;
558}
559
560/* clean_username()
561 *
562 * input - username to check
563 * output - 0 if erroneous, else 0
564 * side effects -
565 */
566static int
567clean_username(const char *username)
568{
569 int len = 0;
570
571 for(; *username; username++)
572 {
573 len++;
574
575 if(!IsUserChar(*username))
576 return 0;
577 }
578
579 if(len > USERLEN)
580 return 0;
581
582 return 1;
583}
584
585/* clean_host()
586 *
587 * input - host to check
588 * output - 0 if erroneous, else 0
589 * side effects -
590 */
591static int
592clean_host(const char *host)
593{
594 int len = 0;
595
596 for(; *host; host++)
597 {
598 len++;
599
600 if(!IsHostChar(*host))
601 return 0;
602 }
603
604 if(len > HOSTLEN)
605 return 0;
606
607 return 1;
608}
609
610static int
561d7efc 611clean_uid(const char *uid, const char *sid)
212380e3
AC
612{
613 int len = 1;
614
561d7efc
JT
615 if(strncmp(uid, sid, strlen(sid)))
616 return 0;
617
212380e3
AC
618 if(!IsDigit(*uid++))
619 return 0;
620
621 for(; *uid; uid++)
622 {
623 len++;
624
625 if(!IsIdChar(*uid))
626 return 0;
627 }
628
629 if(len != IDLEN - 1)
630 return 0;
631
632 return 1;
633}
634
635static void
636set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick)
637{
638 char buf[USERLEN + 1];
0e7cb7e6 639 char note[NICKLEN + 10];
212380e3
AC
640
641 /* This had to be copied here to avoid problems.. */
e3354945 642 source_p->tsinfo = rb_current_time();
212380e3
AC
643 if(source_p->name[0])
644 del_from_client_hash(source_p->name, source_p);
645
646 strcpy(source_p->name, nick);
647 add_to_client_hash(nick, source_p);
648
0e7cb7e6
JT
649 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
650 rb_note(client_p->localClient->F, note);
212380e3
AC
651
652 if(source_p->flags & FLAGS_SENTUSER)
653 {
f427c8b0 654 rb_strlcpy(buf, source_p->username, sizeof(buf));
212380e3
AC
655
656 /* got user, heres nick. */
657 register_local_user(client_p, source_p, buf);
658
659 }
660}
661
662static void
663change_local_nick(struct Client *client_p, struct Client *source_p,
664 char *nick, int dosend)
665{
666 struct Client *target_p;
637c4932 667 rb_dlink_node *ptr, *next_ptr;
212380e3 668 struct Channel *chptr;
0e7cb7e6 669 char note[NICKLEN + 10];
212380e3
AC
670 int samenick;
671
672 if (dosend)
673 {
674 chptr = find_bannickchange_channel(source_p);
675 if (chptr != NULL)
676 {
677 sendto_one_numeric(source_p, ERR_BANNICKCHANGE,
678 form_str(ERR_BANNICKCHANGE),
679 nick, chptr->chname);
680 return;
681 }
e3354945 682 if((source_p->localClient->last_nick_change + ConfigFileEntry.max_nick_time) < rb_current_time())
212380e3
AC
683 source_p->localClient->number_of_nick_changes = 0;
684
e3354945 685 source_p->localClient->last_nick_change = rb_current_time();
212380e3
AC
686 source_p->localClient->number_of_nick_changes++;
687
688 if(ConfigFileEntry.anti_nick_flood && !IsOper(source_p) &&
689 source_p->localClient->number_of_nick_changes > ConfigFileEntry.max_nick_changes)
690 {
691 sendto_one(source_p, form_str(ERR_NICKTOOFAST),
692 me.name, source_p->name, source_p->name,
693 nick, ConfigFileEntry.max_nick_time);
694 return;
695 }
696 }
697
698 samenick = irccmp(source_p->name, nick) ? 0 : 1;
699
700 /* dont reset TS if theyre just changing case of nick */
701 if(!samenick)
702 {
95ffa685 703 /* force the TS to increase -- jilles */
e3354945 704 if (source_p->tsinfo >= rb_current_time())
95ffa685
JT
705 source_p->tsinfo++;
706 else
e3354945 707 source_p->tsinfo = rb_current_time();
212380e3
AC
708 monitor_signoff(source_p);
709 /* we only do bancache for local users -- jilles */
710 if(source_p->user)
711 invalidate_bancache_user(source_p);
712 }
713
714 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
715 "Nick change: From %s to %s [%s@%s]",
716 source_p->name, nick, source_p->username, source_p->host);
717
718 /* send the nick change to the users channels */
7a948bda 719 sendto_common_channels_local(source_p, NOCAPS, ":%s!%s@%s NICK :%s",
212380e3
AC
720 source_p->name, source_p->username, source_p->host, nick);
721
722 /* send the nick change to servers.. */
723 if(source_p->user)
724 {
725 add_history(source_p, 1);
726
727 if (dosend)
728 {
729 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
730 use_id(source_p), nick, (long) source_p->tsinfo);
212380e3
AC
731 }
732 }
733
734 /* Finally, add to hash */
735 del_from_client_hash(source_p->name, source_p);
736 strcpy(source_p->name, nick);
737 add_to_client_hash(nick, source_p);
738
739 if(!samenick)
740 monitor_signon(source_p);
741
742 /* Make sure everyone that has this client on its accept list
743 * loses that reference.
744 */
745 /* we used to call del_all_accepts() here, but theres no real reason
746 * to clear a clients own list of accepted clients. So just remove
747 * them from everyone elses list --anfl
748 */
637c4932 749 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->on_allow_list.head)
212380e3
AC
750 {
751 target_p = ptr->data;
752
555ac41f
JT
753 rb_dlinkFindDestroy(source_p, &target_p->localClient->allow_list);
754 rb_dlinkDestroy(ptr, &source_p->on_allow_list);
212380e3
AC
755 }
756
0e7cb7e6
JT
757 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
758 rb_note(client_p->localClient->F, note);
212380e3
AC
759
760 return;
761}
762
763/*
764 * change_remote_nick()
765 */
766static int
767change_remote_nick(struct Client *client_p, struct Client *source_p,
768 time_t newts, const char *nick, int dosend)
769{
770 struct nd_entry *nd;
771 int samenick = irccmp(source_p->name, nick) ? 0 : 1;
772
773 /* client changing their nick - dont reset ts if its same */
774 if(!samenick)
775 {
e3354945 776 source_p->tsinfo = newts ? newts : rb_current_time();
212380e3
AC
777 monitor_signoff(source_p);
778 }
779
7a948bda 780 sendto_common_channels_local(source_p, NOCAPS, ":%s!%s@%s NICK :%s",
212380e3
AC
781 source_p->name, source_p->username, source_p->host, nick);
782
783 if(source_p->user)
784 {
785 add_history(source_p, 1);
786 if (dosend)
787 {
788 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
789 use_id(source_p), nick, (long) source_p->tsinfo);
212380e3
AC
790 }
791 }
792
793 del_from_client_hash(source_p->name, source_p);
794
795 /* invalidate nick delay when a remote client uses the nick.. */
b37021a4 796 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
212380e3
AC
797 free_nd_entry(nd);
798
799 strcpy(source_p->name, nick);
800 add_to_client_hash(nick, source_p);
801
802 if(!samenick)
803 monitor_signon(source_p);
804
805 /* remove all accepts pointing to the client */
806 del_all_accepts(source_p);
807
808 return 0;
809}
810
811static int
812perform_nick_collides(struct Client *source_p, struct Client *client_p,
813 struct Client *target_p, int parc, const char *parv[],
814 time_t newts, const char *nick, const char *uid)
815{
816 int sameuser;
817 int use_save;
818 const char *action;
819
820 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
821 uid != NULL && can_save(source_p);
822 action = use_save ? "saved" : "killed";
823
824 /* if we dont have a ts, or their TS's are the same, kill both */
825 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo))
826 {
9d107a71 827 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
828 "Nick collision on %s(%s <- %s)(both %s)",
829 target_p->name, target_p->from->name, client_p->name, action);
830
831 if (use_save)
832 {
833 save_user(&me, &me, target_p);
47adde3d 834 ServerStats.is_save++;
212380e3
AC
835 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
836 uid, (long)newts);
837 register_client(client_p, source_p,
3f7e0642 838 uid, SAVE_NICKTS, parc, parv);
212380e3
AC
839 }
840 else
841 {
842 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
843 form_str(ERR_NICKCOLLISION), target_p->name);
844
845 /* if the new client being introduced has a UID, we need to
846 * issue a KILL for it..
847 */
848 if(uid)
849 sendto_one(client_p, ":%s KILL %s :%s (Nick collision (new))",
850 me.id, uid, me.name);
851
852 /* we then need to KILL the old client everywhere */
853 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (new))", me.name);
47adde3d 854 ServerStats.is_kill++;
212380e3
AC
855
856 target_p->flags |= FLAGS_KILLED;
857 exit_client(client_p, target_p, &me, "Nick collision (new)");
858 }
859 return 0;
860 }
861 /* the timestamps are different */
862 else
863 {
864 sameuser = (target_p->user) && !irccmp(target_p->username, parv[5])
865 && !irccmp(target_p->host, parv[6]);
866
867 if((sameuser && newts < target_p->tsinfo) ||
868 (!sameuser && newts > target_p->tsinfo))
869 {
870 /* if we have a UID, then we need to issue a KILL,
871 * otherwise we do nothing and hope that the other
872 * client will collide it..
873 */
874 if (use_save)
875 {
876 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
877 uid, (long)newts);
878 register_client(client_p, source_p,
3f7e0642 879 uid, SAVE_NICKTS, parc, parv);
212380e3
AC
880 }
881 else if(uid)
882 sendto_one(client_p,
883 ":%s KILL %s :%s (Nick collision (new))",
884 me.id, uid, me.name);
885 return 0;
886 }
887 else
888 {
889 if(sameuser)
9d107a71 890 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
891 "Nick collision on %s(%s <- %s)(older %s)",
892 target_p->name, target_p->from->name,
893 client_p->name, action);
894 else
9d107a71 895 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
896 "Nick collision on %s(%s <- %s)(newer %s)",
897 target_p->name, target_p->from->name,
898 client_p->name, action);
899
900 if (use_save)
901 {
47adde3d 902 ServerStats.is_save++;
212380e3
AC
903 save_user(&me, &me, target_p);
904 }
905 else
906 {
47adde3d 907 ServerStats.is_kill++;
212380e3
AC
908 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
909 form_str(ERR_NICKCOLLISION), target_p->name);
910
911 /* now we just need to kill the existing client */
912 kill_client_serv_butone(client_p, target_p,
913 "%s (Nick collision (new))", me.name);
914
915 target_p->flags |= FLAGS_KILLED;
916 (void) exit_client(client_p, target_p, &me, "Nick collision");
917 }
918
463947ad 919 register_client(client_p, source_p,
212380e3
AC
920 nick, newts, parc, parv);
921
922 return 0;
923 }
924 }
925}
926
927
928static int
929perform_nickchange_collides(struct Client *source_p, struct Client *client_p,
930 struct Client *target_p, int parc,
931 const char *parv[], time_t newts, const char *nick)
932{
933 int sameuser;
934 int use_save;
935 const char *action;
936
937 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
938 can_save(source_p);
939 action = use_save ? "saved" : "killed";
940
941 /* its a client changing nick and causing a collide */
942 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo) || !source_p->user)
943 {
9d107a71 944 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
945 "Nick change collision from %s to %s(%s <- %s)(both %s)",
946 source_p->name, target_p->name, target_p->from->name,
947 client_p->name, action);
948
949 if (use_save)
950 {
47adde3d 951 ServerStats.is_save += 2;
212380e3
AC
952 save_user(&me, &me, target_p);
953 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
954 source_p->id, (long)newts);
955 /* don't send a redundant nick change */
956 if (!IsDigit(source_p->name[0]))
3f7e0642 957 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
212380e3
AC
958 }
959 else
960 {
47adde3d 961 ServerStats.is_kill++;
212380e3
AC
962 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
963 form_str(ERR_NICKCOLLISION), target_p->name);
964
965 kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name);
966
47adde3d 967 ServerStats.is_kill++;
212380e3
AC
968
969 kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name);
970
971 target_p->flags |= FLAGS_KILLED;
972 exit_client(NULL, target_p, &me, "Nick collision(new)");
973 source_p->flags |= FLAGS_KILLED;
974 exit_client(client_p, source_p, &me, "Nick collision(old)");
975 }
976 return 0;
977 }
978 else
979 {
980 sameuser = !irccmp(target_p->username, source_p->username) &&
981 !irccmp(target_p->host, source_p->host);
982
983 if((sameuser && newts < target_p->tsinfo) ||
984 (!sameuser && newts > target_p->tsinfo))
985 {
986 if(sameuser)
9d107a71 987 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
988 "Nick change collision from %s to %s(%s <- %s)(older %s)",
989 source_p->name, target_p->name,
990 target_p->from->name, client_p->name, action);
991 else
9d107a71 992 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
993 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
994 source_p->name, target_p->name,
995 target_p->from->name, client_p->name, action);
996
997 if (use_save)
998 {
47adde3d 999 ServerStats.is_save++;
212380e3
AC
1000 /* can't broadcast a SAVE because the
1001 * nickchange has happened at client_p
1002 * but not in other directions -- jilles */
1003 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
1004 source_p->id, (long)newts);
212380e3
AC
1005 /* send a :<id> NICK <id> <ts> (!) */
1006 if (!IsDigit(source_p->name[0]))
3f7e0642 1007 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
212380e3
AC
1008 }
1009 else
1010 {
47adde3d 1011 ServerStats.is_kill++;
212380e3 1012
ce782b68
JT
1013 sendto_one_numeric(source_p, ERR_NICKCOLLISION,
1014 form_str(ERR_NICKCOLLISION), source_p->name);
212380e3
AC
1015
1016 /* kill the client issuing the nickchange */
1017 kill_client_serv_butone(client_p, source_p,
1018 "%s (Nick change collision)", me.name);
1019
1020 source_p->flags |= FLAGS_KILLED;
1021
1022 if(sameuser)
1023 exit_client(client_p, source_p, &me, "Nick collision(old)");
1024 else
1025 exit_client(client_p, source_p, &me, "Nick collision(new)");
1026 }
1027 return 0;
1028 }
1029 else
1030 {
1031 if(sameuser)
9d107a71 1032 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
1033 "Nick collision on %s(%s <- %s)(older %s)",
1034 target_p->name, target_p->from->name,
1035 client_p->name, action);
1036 else
9d107a71 1037 sendto_realops_snomask(SNO_SKILL, L_ALL,
212380e3
AC
1038 "Nick collision on %s(%s <- %s)(newer %s)",
1039 target_p->name, target_p->from->name,
1040 client_p->name, action);
1041
1042 if (use_save)
1043 {
47adde3d 1044 ServerStats.is_save++;
212380e3
AC
1045 save_user(&me, &me, target_p);
1046 }
1047 else
1048 {
1049 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1050 form_str(ERR_NICKCOLLISION), target_p->name);
1051
1052 /* kill the client who existed before hand */
1053 kill_client_serv_butone(client_p, target_p, "%s (Nick collision)", me.name);
1054
47adde3d 1055 ServerStats.is_kill++;
212380e3
AC
1056
1057 target_p->flags |= FLAGS_KILLED;
1058 (void) exit_client(client_p, target_p, &me, "Nick collision");
1059 }
1060 }
1061 }
1062
1063 change_remote_nick(client_p, source_p, newts, nick, 1);
1064
1065 return 0;
1066}
1067
1068static int
1069register_client(struct Client *client_p, struct Client *server,
1070 const char *nick, time_t newts, int parc, const char *parv[])
1071{
1072 struct Client *source_p;
1073 struct User *user;
1074 struct nd_entry *nd;
1075 const char *m;
1076 int flag;
1077
1078 source_p = make_client(client_p);
1079 user = make_user(source_p);
0e7cb7e6 1080 rb_dlinkAddTail(source_p, &source_p->node, &global_client_list);
212380e3
AC
1081
1082 source_p->hopcount = atoi(parv[2]);
1083 source_p->tsinfo = newts;
1084
1085 strcpy(source_p->name, nick);
f427c8b0
VY
1086 rb_strlcpy(source_p->username, parv[5], sizeof(source_p->username));
1087 rb_strlcpy(source_p->host, parv[6], sizeof(source_p->host));
1088 rb_strlcpy(source_p->orighost, source_p->host, sizeof(source_p->orighost));
212380e3
AC
1089
1090 if(parc == 12)
1091 {
f427c8b0
VY
1092 rb_strlcpy(source_p->info, parv[11], sizeof(source_p->info));
1093 rb_strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1094 rb_strlcpy(source_p->id, parv[8], sizeof(source_p->id));
212380e3
AC
1095 add_to_id_hash(source_p->id, source_p);
1096 if (strcmp(parv[9], "*"))
1097 {
f427c8b0 1098 rb_strlcpy(source_p->orighost, parv[9], sizeof(source_p->orighost));
212380e3
AC
1099 if (irccmp(source_p->host, source_p->orighost))
1100 SetDynSpoof(source_p);
1101 }
1102 if (strcmp(parv[10], "*"))
f427c8b0 1103 rb_strlcpy(source_p->user->suser, parv[10], sizeof(source_p->user->suser));
212380e3
AC
1104 }
1105 else if(parc == 10)
1106 {
f427c8b0
VY
1107 rb_strlcpy(source_p->info, parv[9], sizeof(source_p->info));
1108 rb_strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1109 rb_strlcpy(source_p->id, parv[8], sizeof(source_p->id));
212380e3
AC
1110 add_to_id_hash(source_p->id, source_p);
1111 }
1112 else
1113 {
463947ad 1114 s_assert(0);
212380e3
AC
1115 }
1116
1117 /* remove any nd entries for this nick */
b37021a4 1118 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
212380e3
AC
1119 free_nd_entry(nd);
1120
1121 add_to_client_hash(nick, source_p);
66b4a7ae 1122 add_to_hostname_hash(source_p->orighost, source_p);
212380e3
AC
1123 monitor_signon(source_p);
1124
1125 m = &parv[4][1];
1126 while(*m)
1127 {
1128 flag = user_modes[(unsigned char) *m];
1129
1130 if(flag & UMODE_SERVICE)
1131 {
1132 int hit = 0;
5b96d9a6 1133 rb_dlink_node *ptr;
212380e3 1134
5b96d9a6 1135 RB_DLINK_FOREACH(ptr, service_list.head)
212380e3 1136 {
c88cdb00 1137 if(!irccmp((const char *) ptr->data, server->name))
212380e3
AC
1138 {
1139 hit++;
1140 break;
1141 }
1142 }
1143
1144 if(!hit)
1145 {
1146 m++;
1147 continue;
1148 }
1149 }
1150
1151 /* increment +i count if theyre invis */
1152 if(!(source_p->umodes & UMODE_INVISIBLE) && (flag & UMODE_INVISIBLE))
1153 Count.invisi++;
1154
1155 /* increment opered count if theyre opered */
1156 if(!(source_p->umodes & UMODE_OPER) && (flag & UMODE_OPER))
1157 Count.oper++;
1158
1159 source_p->umodes |= flag;
1160 m++;
1161 }
1162
1163 if(IsOper(source_p) && !IsService(source_p))
0e7cb7e6 1164 rb_dlinkAddAlloc(source_p, &oper_list);
212380e3
AC
1165
1166 SetRemoteClient(source_p);
1167
1168 if(++Count.total > Count.max_tot)
1169 Count.max_tot = Count.total;
1170
64449595 1171 source_p->servptr = server;
212380e3 1172
0e7cb7e6 1173 rb_dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
212380e3 1174
212380e3
AC
1175 call_hook(h_new_remote_user, source_p);
1176
1177 return (introduce_client(client_p, source_p, user, nick, parc == 12));
1178}
1179
1180/* Check if we can do SAVE. target_p can be a client to save or a
1181 * server introducing a client -- jilles */
1182static int
1183can_save(struct Client *target_p)
1184{
1185 struct Client *serv_p;
1186
1187 if (MyClient(target_p))
1188 return 1;
1189 if (!has_id(target_p))
1190 return 0;
1191 serv_p = IsServer(target_p) ? target_p : target_p->servptr;
1192 while (serv_p != NULL && serv_p != &me)
1193 {
1194 if (!(serv_p->serv->caps & CAP_SAVE))
1195 return 0;
1196 serv_p = serv_p->servptr;
1197 }
1198 return serv_p == &me;
1199}
1200
1201static void
1202save_user(struct Client *client_p, struct Client *source_p,
1203 struct Client *target_p)
1204{
1205 if (!MyConnect(target_p) && (!has_id(target_p) || !IsCapable(target_p->from, CAP_SAVE)))
1206 {
1207 /* This shouldn't happen */
1208 /* Note we only need SAVE support in this direction */
1209 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1210 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1211 target_p->name, target_p->username, target_p->host, source_p->name, target_p->from->name);
1212 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (no SAVE support))", me.name);
47adde3d 1213 ServerStats.is_kill++;
212380e3
AC
1214
1215 target_p->flags |= FLAGS_KILLED;
1216 (void) exit_client(NULL, target_p, &me, "Nick collision (no SAVE support)");
1217 return;
1218 }
1219 sendto_server(client_p, NULL, CAP_SAVE|CAP_TS6, NOCAPS, ":%s SAVE %s %ld",
1220 source_p->id, target_p->id, (long)target_p->tsinfo);
1221 sendto_server(client_p, NULL, CAP_TS6, CAP_SAVE, ":%s NICK %s :%ld",
3f7e0642 1222 target_p->id, target_p->id, (long)SAVE_NICKTS);
212380e3
AC
1223 if (!IsMe(client_p))
1224 sendto_realops_snomask(SNO_SKILL, L_ALL,
1225 "Received SAVE message for %s from %s",
1226 target_p->name, source_p->name);
1227 if (MyClient(target_p))
1228 {
1229 sendto_one_numeric(target_p, RPL_SAVENICK,
1230 form_str(RPL_SAVENICK), target_p->id);
1231 change_local_nick(target_p, target_p, target_p->id, 0);
3f7e0642 1232 target_p->tsinfo = SAVE_NICKTS;
212380e3
AC
1233 }
1234 else
3f7e0642 1235 change_remote_nick(target_p, target_p, SAVE_NICKTS, target_p->id, 0);
212380e3 1236}
ad35b2cd
JT
1237
1238static void bad_nickname(struct Client *client_p, const char *nick)
1239{
1240 char squitreason[100];
1241
1242 sendto_wallops_flags(UMODE_WALLOP, &me,
1243 "Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1244 client_p->name, nick);
1245 sendto_server(NULL, NULL, CAP_TS6, NOCAPS,
1246 ":%s WALLOPS :Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1247 me.id, client_p->name, nick);
4dd5d304
JT
1248 ilog(L_SERVER, "Link %s cancelled, bad nickname %s sent (NICKLEN mismatch?)",
1249 client_p->name, nick);
ad35b2cd
JT
1250
1251 rb_snprintf(squitreason, sizeof squitreason,
1252 "Bad nickname introduced [%s]", nick);
1253 exit_client(client_p, client_p, &me, squitreason);
1254}