]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/core/m_nick.c
Update NEWS.
[irc/rqf/shadowircd.git] / modules / core / m_nick.c
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 *
24 * $Id: m_nick.c 3518 2007-06-22 21:59:09Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "client.h"
29 #include "hash.h"
30 #include "match.h"
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"
41 #include "logger.h"
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
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
58 static int mr_nick(struct Client *, struct Client *, int, const char **);
59 static int m_nick(struct Client *, struct Client *, int, const char **);
60 static int mc_nick(struct Client *, struct Client *, int, const char **);
61 static int ms_nick(struct Client *, struct Client *, int, const char **);
62 static int ms_uid(struct Client *, struct Client *, int, const char **);
63 static int ms_euid(struct Client *, struct Client *, int, const char **);
64 static int ms_save(struct Client *, struct Client *, int, const char **);
65 static int can_save(struct Client *);
66 static void save_user(struct Client *, struct Client *, struct Client *);
67 static void bad_nickname(struct Client *, const char *);
68
69 struct Message nick_msgtab = {
70 "NICK", 0, 0, 0, MFLG_SLOW,
71 {{mr_nick, 0}, {m_nick, 0}, {mc_nick, 3}, {ms_nick, 0}, mg_ignore, {m_nick, 0}}
72 };
73 struct 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 };
77 struct 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 };
81 struct 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
86 mapi_clist_av1 nick_clist[] = { &nick_msgtab, &uid_msgtab, &euid_msgtab,
87 &save_msgtab, NULL };
88
89 DECLARE_MODULE_AV1(nick, NULL, NULL, nick_clist, NULL, NULL, "$Revision: 3518 $");
90
91 static int change_remote_nick(struct Client *, struct Client *, time_t,
92 const char *, int);
93
94 static int clean_nick(const char *, int loc_client);
95 static int clean_username(const char *);
96 static int clean_host(const char *);
97 static int clean_uid(const char *uid);
98
99 static void set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick);
100 static void change_local_nick(struct Client *client_p, struct Client *source_p, char *nick, int);
101 static int register_client(struct Client *client_p, struct Client *server,
102 const char *nick, time_t newts, int parc, const char *parv[]);
103
104 static int perform_nick_collides(struct Client *, struct Client *,
105 struct Client *, int, const char **,
106 time_t, const char *, const char *);
107 static int perform_nickchange_collides(struct Client *, struct Client *,
108 struct Client *, int, const char **, time_t, const char *);
109
110 /* mr_nick()
111 * parv[1] = nickname
112 */
113 static int
114 mr_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 */
141 rb_strlcpy(nick, parv[1], sizeof(nick));
142
143 /* check the nickname is ok */
144 if(!clean_nick(nick, 1))
145 {
146 sendto_one(source_p, form_str(ERR_ERRONEUSNICKNAME),
147 me.name, EmptyString(source_p->name) ? "*" : source_p->name, parv[1]);
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
159 if(irc_dictionary_find(nd_dict, nick))
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()
177 * parv[1] = nickname
178 */
179 static int
180 m_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! */
204 rb_strlcpy(nick, parv[1], sizeof(nick));
205
206 /* check the nickname is ok */
207 if(!clean_nick(nick, 1))
208 {
209 sendto_one(source_p, form_str(ERR_ERRONEUSNICKNAME), me.name, source_p->name, nick);
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
219 if(irc_dictionary_find(nd_dict, nick))
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
246 sendto_one(source_p, form_str(ERR_NICKNAMEINUSE), me.name, source_p->name, nick);
247
248 return 0;
249 }
250 else
251 change_local_nick(client_p, source_p, nick, 1);
252
253 return 0;
254 }
255
256 /* mc_nick()
257 *
258 * server -> server nick change
259 * parv[1] = nickname
260 * parv[2] = TS when nick change
261 */
262 static int
263 mc_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 {
271 bad_nickname(client_p, parv[1]);
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
302 static int
303 ms_nick(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
304 {
305 const char *nick, *server;
306
307 nick = parc > 1 ? parv[1] : "?";
308 server = parc > 7 ? parv[7] : "?";
309
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);
318
319 exit_client(client_p, client_p, &me, "TS5 nickname introduced");
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 */
335 static int
336 ms_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;
340 char squitreason[120];
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);
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);
355 return 0;
356 }
357
358 /* if nicks erroneous, or too long, kill */
359 if(!clean_nick(parv[1], 0))
360 {
361 bad_nickname(client_p, parv[1]);
362 return 0;
363 }
364
365 if(!clean_uid(parv[8]))
366 {
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);
371 return 0;
372 }
373
374 if(!clean_username(parv[5]) || !clean_host(parv[6]))
375 {
376 ServerStats.is_kill++;
377 sendto_realops_snomask(SNO_DEBUG, L_ALL,
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);
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",
389 source_p->name, parv[1]);
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 */
426 static int
427 ms_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;
431 char squitreason[120];
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);
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);
446 return 0;
447 }
448
449 /* if nicks erroneous, or too long, kill */
450 if(!clean_nick(parv[1], 0))
451 {
452 bad_nickname(client_p, parv[1]);
453 return 0;
454 }
455
456 if(!clean_uid(parv[8]))
457 {
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);
462 return 0;
463 }
464
465 if(!clean_username(parv[5]) || !clean_host(parv[6]))
466 {
467 ServerStats.is_kill++;
468 sendto_realops_snomask(SNO_DEBUG, L_ALL,
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);
472 return 0;
473 }
474
475 if(strcmp(parv[9], "*") && !clean_host(parv[9]))
476 {
477 ServerStats.is_kill++;
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",
490 source_p->name, parv[1]);
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 */
518 static int
519 ms_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 */
549 static int
550 clean_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 */
581 static int
582 clean_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 */
606 static int
607 clean_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
625 static int
626 clean_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
647 static void
648 set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick)
649 {
650 char buf[USERLEN + 1];
651 char note[NICKLEN + 10];
652
653 /* This had to be copied here to avoid problems.. */
654 source_p->tsinfo = rb_current_time();
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
661 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
662 rb_note(client_p->localClient->F, note);
663
664 if(source_p->flags & FLAGS_SENTUSER)
665 {
666 rb_strlcpy(buf, source_p->username, sizeof(buf));
667
668 /* got user, heres nick. */
669 register_local_user(client_p, source_p, buf);
670
671 }
672 }
673
674 static void
675 change_local_nick(struct Client *client_p, struct Client *source_p,
676 char *nick, int dosend)
677 {
678 struct Client *target_p;
679 rb_dlink_node *ptr, *next_ptr;
680 struct Channel *chptr;
681 char note[NICKLEN + 10];
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 }
694 if((source_p->localClient->last_nick_change + ConfigFileEntry.max_nick_time) < rb_current_time())
695 source_p->localClient->number_of_nick_changes = 0;
696
697 source_p->localClient->last_nick_change = rb_current_time();
698 source_p->localClient->number_of_nick_changes++;
699
700 if(ConfigFileEntry.anti_nick_flood && !IsOper(source_p) &&
701 source_p->localClient->number_of_nick_changes > ConfigFileEntry.max_nick_changes)
702 {
703 sendto_one(source_p, form_str(ERR_NICKTOOFAST),
704 me.name, source_p->name, source_p->name,
705 nick, ConfigFileEntry.max_nick_time);
706 return;
707 }
708 }
709
710 samenick = irccmp(source_p->name, nick) ? 0 : 1;
711
712 /* dont reset TS if theyre just changing case of nick */
713 if(!samenick)
714 {
715 /* force the TS to increase -- jilles */
716 if (source_p->tsinfo >= rb_current_time())
717 source_p->tsinfo++;
718 else
719 source_p->tsinfo = rb_current_time();
720 monitor_signoff(source_p);
721 /* we only do bancache for local users -- jilles */
722 if(source_p->user)
723 invalidate_bancache_user(source_p);
724 }
725
726 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
727 "Nick change: From %s to %s [%s@%s]",
728 source_p->name, nick, source_p->username, source_p->host);
729
730 /* send the nick change to the users channels */
731 sendto_common_channels_local(source_p, ":%s!%s@%s NICK :%s",
732 source_p->name, source_p->username, source_p->host, nick);
733
734 /* send the nick change to servers.. */
735 if(source_p->user)
736 {
737 add_history(source_p, 1);
738
739 if (dosend)
740 {
741 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
742 use_id(source_p), nick, (long) source_p->tsinfo);
743 }
744 }
745
746 /* Finally, add to hash */
747 del_from_client_hash(source_p->name, source_p);
748 strcpy(source_p->name, nick);
749 add_to_client_hash(nick, source_p);
750
751 if(!samenick)
752 monitor_signon(source_p);
753
754 /* Make sure everyone that has this client on its accept list
755 * loses that reference.
756 */
757 /* we used to call del_all_accepts() here, but theres no real reason
758 * to clear a clients own list of accepted clients. So just remove
759 * them from everyone elses list --anfl
760 */
761 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->on_allow_list.head)
762 {
763 target_p = ptr->data;
764
765 rb_dlinkFindDestroy(source_p, &target_p->localClient->allow_list);
766 rb_dlinkDestroy(ptr, &source_p->on_allow_list);
767 }
768
769 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
770 rb_note(client_p->localClient->F, note);
771
772 return;
773 }
774
775 /*
776 * change_remote_nick()
777 */
778 static int
779 change_remote_nick(struct Client *client_p, struct Client *source_p,
780 time_t newts, const char *nick, int dosend)
781 {
782 struct nd_entry *nd;
783 int samenick = irccmp(source_p->name, nick) ? 0 : 1;
784
785 /* client changing their nick - dont reset ts if its same */
786 if(!samenick)
787 {
788 source_p->tsinfo = newts ? newts : rb_current_time();
789 monitor_signoff(source_p);
790 }
791
792 sendto_common_channels_local(source_p, ":%s!%s@%s NICK :%s",
793 source_p->name, source_p->username, source_p->host, nick);
794
795 if(source_p->user)
796 {
797 add_history(source_p, 1);
798 if (dosend)
799 {
800 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
801 use_id(source_p), nick, (long) source_p->tsinfo);
802 }
803 }
804
805 del_from_client_hash(source_p->name, source_p);
806
807 /* invalidate nick delay when a remote client uses the nick.. */
808 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
809 free_nd_entry(nd);
810
811 strcpy(source_p->name, nick);
812 add_to_client_hash(nick, source_p);
813
814 if(!samenick)
815 monitor_signon(source_p);
816
817 /* remove all accepts pointing to the client */
818 del_all_accepts(source_p);
819
820 return 0;
821 }
822
823 static int
824 perform_nick_collides(struct Client *source_p, struct Client *client_p,
825 struct Client *target_p, int parc, const char *parv[],
826 time_t newts, const char *nick, const char *uid)
827 {
828 int sameuser;
829 int use_save;
830 const char *action;
831
832 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
833 uid != NULL && can_save(source_p);
834 action = use_save ? "saved" : "killed";
835
836 /* if we dont have a ts, or their TS's are the same, kill both */
837 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo))
838 {
839 sendto_realops_snomask(SNO_SKILL, L_ALL,
840 "Nick collision on %s(%s <- %s)(both %s)",
841 target_p->name, target_p->from->name, client_p->name, action);
842
843 if (use_save)
844 {
845 save_user(&me, &me, target_p);
846 ServerStats.is_save++;
847 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
848 uid, (long)newts);
849 register_client(client_p, source_p,
850 uid, SAVE_NICKTS, parc, parv);
851 }
852 else
853 {
854 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
855 form_str(ERR_NICKCOLLISION), target_p->name);
856
857 /* if the new client being introduced has a UID, we need to
858 * issue a KILL for it..
859 */
860 if(uid)
861 sendto_one(client_p, ":%s KILL %s :%s (Nick collision (new))",
862 me.id, uid, me.name);
863
864 /* we then need to KILL the old client everywhere */
865 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (new))", me.name);
866 ServerStats.is_kill++;
867
868 target_p->flags |= FLAGS_KILLED;
869 exit_client(client_p, target_p, &me, "Nick collision (new)");
870 }
871 return 0;
872 }
873 /* the timestamps are different */
874 else
875 {
876 sameuser = (target_p->user) && !irccmp(target_p->username, parv[5])
877 && !irccmp(target_p->host, parv[6]);
878
879 if((sameuser && newts < target_p->tsinfo) ||
880 (!sameuser && newts > target_p->tsinfo))
881 {
882 /* if we have a UID, then we need to issue a KILL,
883 * otherwise we do nothing and hope that the other
884 * client will collide it..
885 */
886 if (use_save)
887 {
888 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
889 uid, (long)newts);
890 register_client(client_p, source_p,
891 uid, SAVE_NICKTS, parc, parv);
892 }
893 else if(uid)
894 sendto_one(client_p,
895 ":%s KILL %s :%s (Nick collision (new))",
896 me.id, uid, me.name);
897 return 0;
898 }
899 else
900 {
901 if(sameuser)
902 sendto_realops_snomask(SNO_SKILL, L_ALL,
903 "Nick collision on %s(%s <- %s)(older %s)",
904 target_p->name, target_p->from->name,
905 client_p->name, action);
906 else
907 sendto_realops_snomask(SNO_SKILL, L_ALL,
908 "Nick collision on %s(%s <- %s)(newer %s)",
909 target_p->name, target_p->from->name,
910 client_p->name, action);
911
912 if (use_save)
913 {
914 ServerStats.is_save++;
915 save_user(&me, &me, target_p);
916 }
917 else
918 {
919 ServerStats.is_kill++;
920 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
921 form_str(ERR_NICKCOLLISION), target_p->name);
922
923 /* now we just need to kill the existing client */
924 kill_client_serv_butone(client_p, target_p,
925 "%s (Nick collision (new))", me.name);
926
927 target_p->flags |= FLAGS_KILLED;
928 (void) exit_client(client_p, target_p, &me, "Nick collision");
929 }
930
931 register_client(client_p, source_p,
932 nick, newts, parc, parv);
933
934 return 0;
935 }
936 }
937 }
938
939
940 static int
941 perform_nickchange_collides(struct Client *source_p, struct Client *client_p,
942 struct Client *target_p, int parc,
943 const char *parv[], time_t newts, const char *nick)
944 {
945 int sameuser;
946 int use_save;
947 const char *action;
948
949 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
950 can_save(source_p);
951 action = use_save ? "saved" : "killed";
952
953 /* its a client changing nick and causing a collide */
954 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo) || !source_p->user)
955 {
956 sendto_realops_snomask(SNO_SKILL, L_ALL,
957 "Nick change collision from %s to %s(%s <- %s)(both %s)",
958 source_p->name, target_p->name, target_p->from->name,
959 client_p->name, action);
960
961 if (use_save)
962 {
963 ServerStats.is_save += 2;
964 save_user(&me, &me, target_p);
965 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
966 source_p->id, (long)newts);
967 /* don't send a redundant nick change */
968 if (!IsDigit(source_p->name[0]))
969 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
970 }
971 else
972 {
973 ServerStats.is_kill++;
974 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
975 form_str(ERR_NICKCOLLISION), target_p->name);
976
977 kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name);
978
979 ServerStats.is_kill++;
980
981 kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name);
982
983 target_p->flags |= FLAGS_KILLED;
984 exit_client(NULL, target_p, &me, "Nick collision(new)");
985 source_p->flags |= FLAGS_KILLED;
986 exit_client(client_p, source_p, &me, "Nick collision(old)");
987 }
988 return 0;
989 }
990 else
991 {
992 sameuser = !irccmp(target_p->username, source_p->username) &&
993 !irccmp(target_p->host, source_p->host);
994
995 if((sameuser && newts < target_p->tsinfo) ||
996 (!sameuser && newts > target_p->tsinfo))
997 {
998 if(sameuser)
999 sendto_realops_snomask(SNO_SKILL, L_ALL,
1000 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1001 source_p->name, target_p->name,
1002 target_p->from->name, client_p->name, action);
1003 else
1004 sendto_realops_snomask(SNO_SKILL, L_ALL,
1005 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1006 source_p->name, target_p->name,
1007 target_p->from->name, client_p->name, action);
1008
1009 if (use_save)
1010 {
1011 ServerStats.is_save++;
1012 /* can't broadcast a SAVE because the
1013 * nickchange has happened at client_p
1014 * but not in other directions -- jilles */
1015 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
1016 source_p->id, (long)newts);
1017 /* send a :<id> NICK <id> <ts> (!) */
1018 if (!IsDigit(source_p->name[0]))
1019 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
1020 }
1021 else
1022 {
1023 ServerStats.is_kill++;
1024
1025 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1026 form_str(ERR_NICKCOLLISION), target_p->name);
1027
1028 /* kill the client issuing the nickchange */
1029 kill_client_serv_butone(client_p, source_p,
1030 "%s (Nick change collision)", me.name);
1031
1032 source_p->flags |= FLAGS_KILLED;
1033
1034 if(sameuser)
1035 exit_client(client_p, source_p, &me, "Nick collision(old)");
1036 else
1037 exit_client(client_p, source_p, &me, "Nick collision(new)");
1038 }
1039 return 0;
1040 }
1041 else
1042 {
1043 if(sameuser)
1044 sendto_realops_snomask(SNO_SKILL, L_ALL,
1045 "Nick collision on %s(%s <- %s)(older %s)",
1046 target_p->name, target_p->from->name,
1047 client_p->name, action);
1048 else
1049 sendto_realops_snomask(SNO_SKILL, L_ALL,
1050 "Nick collision on %s(%s <- %s)(newer %s)",
1051 target_p->name, target_p->from->name,
1052 client_p->name, action);
1053
1054 if (use_save)
1055 {
1056 ServerStats.is_save++;
1057 save_user(&me, &me, target_p);
1058 }
1059 else
1060 {
1061 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1062 form_str(ERR_NICKCOLLISION), target_p->name);
1063
1064 /* kill the client who existed before hand */
1065 kill_client_serv_butone(client_p, target_p, "%s (Nick collision)", me.name);
1066
1067 ServerStats.is_kill++;
1068
1069 target_p->flags |= FLAGS_KILLED;
1070 (void) exit_client(client_p, target_p, &me, "Nick collision");
1071 }
1072 }
1073 }
1074
1075 change_remote_nick(client_p, source_p, newts, nick, 1);
1076
1077 return 0;
1078 }
1079
1080 static int
1081 register_client(struct Client *client_p, struct Client *server,
1082 const char *nick, time_t newts, int parc, const char *parv[])
1083 {
1084 struct Client *source_p;
1085 struct User *user;
1086 struct nd_entry *nd;
1087 const char *m;
1088 int flag;
1089
1090 source_p = make_client(client_p);
1091 user = make_user(source_p);
1092 rb_dlinkAddTail(source_p, &source_p->node, &global_client_list);
1093
1094 source_p->hopcount = atoi(parv[2]);
1095 source_p->tsinfo = newts;
1096
1097 strcpy(source_p->name, nick);
1098 rb_strlcpy(source_p->username, parv[5], sizeof(source_p->username));
1099 rb_strlcpy(source_p->host, parv[6], sizeof(source_p->host));
1100 rb_strlcpy(source_p->orighost, source_p->host, sizeof(source_p->orighost));
1101
1102 if(parc == 12)
1103 {
1104 rb_strlcpy(source_p->info, parv[11], sizeof(source_p->info));
1105 rb_strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1106 rb_strlcpy(source_p->id, parv[8], sizeof(source_p->id));
1107 add_to_id_hash(source_p->id, source_p);
1108 if (strcmp(parv[9], "*"))
1109 {
1110 rb_strlcpy(source_p->orighost, parv[9], sizeof(source_p->orighost));
1111 if (irccmp(source_p->host, source_p->orighost))
1112 SetDynSpoof(source_p);
1113 }
1114 if (strcmp(parv[10], "*"))
1115 rb_strlcpy(source_p->user->suser, parv[10], sizeof(source_p->user->suser));
1116 }
1117 else if(parc == 10)
1118 {
1119 rb_strlcpy(source_p->info, parv[9], sizeof(source_p->info));
1120 rb_strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1121 rb_strlcpy(source_p->id, parv[8], sizeof(source_p->id));
1122 add_to_id_hash(source_p->id, source_p);
1123 }
1124 else
1125 {
1126 s_assert(0);
1127 }
1128
1129 /* remove any nd entries for this nick */
1130 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
1131 free_nd_entry(nd);
1132
1133 add_to_client_hash(nick, source_p);
1134 add_to_hostname_hash(source_p->orighost, source_p);
1135 monitor_signon(source_p);
1136
1137 m = &parv[4][1];
1138 while(*m)
1139 {
1140 flag = user_modes[(unsigned char) *m];
1141
1142 if(flag & UMODE_SERVICE)
1143 {
1144 int hit = 0;
1145 rb_dlink_node *ptr;
1146
1147 RB_DLINK_FOREACH(ptr, service_list.head)
1148 {
1149 if(!irccmp((const char *) ptr->data, server->name))
1150 {
1151 hit++;
1152 break;
1153 }
1154 }
1155
1156 if(!hit)
1157 {
1158 m++;
1159 continue;
1160 }
1161 }
1162
1163 /* increment +i count if theyre invis */
1164 if(!(source_p->umodes & UMODE_INVISIBLE) && (flag & UMODE_INVISIBLE))
1165 Count.invisi++;
1166
1167 /* increment opered count if theyre opered */
1168 if(!(source_p->umodes & UMODE_OPER) && (flag & UMODE_OPER))
1169 Count.oper++;
1170
1171 source_p->umodes |= flag;
1172 m++;
1173 }
1174
1175 if(IsOper(source_p) && !IsService(source_p))
1176 rb_dlinkAddAlloc(source_p, &oper_list);
1177
1178 SetRemoteClient(source_p);
1179
1180 if(++Count.total > Count.max_tot)
1181 Count.max_tot = Count.total;
1182
1183 source_p->servptr = server;
1184
1185 rb_dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
1186
1187 call_hook(h_new_remote_user, source_p);
1188
1189 return (introduce_client(client_p, source_p, user, nick, parc == 12));
1190 }
1191
1192 /* Check if we can do SAVE. target_p can be a client to save or a
1193 * server introducing a client -- jilles */
1194 static int
1195 can_save(struct Client *target_p)
1196 {
1197 struct Client *serv_p;
1198
1199 if (MyClient(target_p))
1200 return 1;
1201 if (!has_id(target_p))
1202 return 0;
1203 serv_p = IsServer(target_p) ? target_p : target_p->servptr;
1204 while (serv_p != NULL && serv_p != &me)
1205 {
1206 if (!(serv_p->serv->caps & CAP_SAVE))
1207 return 0;
1208 serv_p = serv_p->servptr;
1209 }
1210 return serv_p == &me;
1211 }
1212
1213 static void
1214 save_user(struct Client *client_p, struct Client *source_p,
1215 struct Client *target_p)
1216 {
1217 if (!MyConnect(target_p) && (!has_id(target_p) || !IsCapable(target_p->from, CAP_SAVE)))
1218 {
1219 /* This shouldn't happen */
1220 /* Note we only need SAVE support in this direction */
1221 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1222 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1223 target_p->name, target_p->username, target_p->host, source_p->name, target_p->from->name);
1224 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (no SAVE support))", me.name);
1225 ServerStats.is_kill++;
1226
1227 target_p->flags |= FLAGS_KILLED;
1228 (void) exit_client(NULL, target_p, &me, "Nick collision (no SAVE support)");
1229 return;
1230 }
1231 sendto_server(client_p, NULL, CAP_SAVE|CAP_TS6, NOCAPS, ":%s SAVE %s %ld",
1232 source_p->id, target_p->id, (long)target_p->tsinfo);
1233 sendto_server(client_p, NULL, CAP_TS6, CAP_SAVE, ":%s NICK %s :%ld",
1234 target_p->id, target_p->id, (long)SAVE_NICKTS);
1235 if (!IsMe(client_p))
1236 sendto_realops_snomask(SNO_SKILL, L_ALL,
1237 "Received SAVE message for %s from %s",
1238 target_p->name, source_p->name);
1239 if (MyClient(target_p))
1240 {
1241 sendto_one_numeric(target_p, RPL_SAVENICK,
1242 form_str(RPL_SAVENICK), target_p->id);
1243 change_local_nick(target_p, target_p, target_p->id, 0);
1244 target_p->tsinfo = SAVE_NICKTS;
1245 }
1246 else
1247 change_remote_nick(target_p, target_p, SAVE_NICKTS, target_p->id, 0);
1248 }
1249
1250 static void bad_nickname(struct Client *client_p, const char *nick)
1251 {
1252 char squitreason[100];
1253
1254 sendto_wallops_flags(UMODE_WALLOP, &me,
1255 "Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1256 client_p->name, nick);
1257 sendto_server(NULL, NULL, CAP_TS6, NOCAPS,
1258 ":%s WALLOPS :Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1259 me.id, client_p->name, nick);
1260 ilog(L_SERVER, "Link %s cancelled, bad nickname %s sent (NICKLEN mismatch?)",
1261 client_p->name, nick);
1262
1263 rb_snprintf(squitreason, sizeof squitreason,
1264 "Bad nickname introduced [%s]", nick);
1265 exit_client(client_p, client_p, &me, squitreason);
1266 }