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