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