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