]> jfr.im git - solanum.git/blob - modules/core/m_nick.c
UID/EUID: Add server's SID to invalid UID error message.
[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 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], ConfigFileEntry.nicklen);
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], ConfigFileEntry.nicklen);
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], source_p->id))
366 {
367 rb_snprintf(squitreason, sizeof squitreason,
368 "Invalid UID %s for nick %s on %s/%s",
369 parv[8], parv[1], source_p->name, source_p->id);
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], source_p->id))
457 {
458 rb_snprintf(squitreason, sizeof squitreason,
459 "Invalid UID %s for nick %s on %s/%s",
460 parv[8], parv[1], source_p->name, source_p->id);
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 && len >= ConfigFileEntry.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, const char *sid)
627 {
628 int len = 1;
629
630 if(strncmp(uid, sid, strlen(sid)))
631 return 0;
632
633 if(!IsDigit(*uid++))
634 return 0;
635
636 for(; *uid; uid++)
637 {
638 len++;
639
640 if(!IsIdChar(*uid))
641 return 0;
642 }
643
644 if(len != IDLEN - 1)
645 return 0;
646
647 return 1;
648 }
649
650 static void
651 set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick)
652 {
653 char buf[USERLEN + 1];
654 char note[NICKLEN + 10];
655
656 /* This had to be copied here to avoid problems.. */
657 source_p->tsinfo = rb_current_time();
658 if(source_p->name[0])
659 del_from_client_hash(source_p->name, source_p);
660
661 strcpy(source_p->name, nick);
662 add_to_client_hash(nick, source_p);
663
664 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
665 rb_note(client_p->localClient->F, note);
666
667 if(source_p->flags & FLAGS_SENTUSER)
668 {
669 rb_strlcpy(buf, source_p->username, sizeof(buf));
670
671 /* got user, heres nick. */
672 register_local_user(client_p, source_p, buf);
673
674 }
675 }
676
677 static void
678 change_local_nick(struct Client *client_p, struct Client *source_p,
679 char *nick, int dosend)
680 {
681 struct Client *target_p;
682 rb_dlink_node *ptr, *next_ptr;
683 struct Channel *chptr;
684 char note[NICKLEN + 10];
685 int samenick;
686
687 if (dosend)
688 {
689 chptr = find_bannickchange_channel(source_p);
690 if (chptr != NULL)
691 {
692 sendto_one_numeric(source_p, ERR_BANNICKCHANGE,
693 form_str(ERR_BANNICKCHANGE),
694 nick, chptr->chname);
695 return;
696 }
697 if((source_p->localClient->last_nick_change + ConfigFileEntry.max_nick_time) < rb_current_time())
698 source_p->localClient->number_of_nick_changes = 0;
699
700 source_p->localClient->last_nick_change = rb_current_time();
701 source_p->localClient->number_of_nick_changes++;
702
703 if(ConfigFileEntry.anti_nick_flood && !IsOper(source_p) &&
704 source_p->localClient->number_of_nick_changes > ConfigFileEntry.max_nick_changes)
705 {
706 sendto_one(source_p, form_str(ERR_NICKTOOFAST),
707 me.name, source_p->name, source_p->name,
708 nick, ConfigFileEntry.max_nick_time);
709 return;
710 }
711 }
712
713 samenick = irccmp(source_p->name, nick) ? 0 : 1;
714
715 /* dont reset TS if theyre just changing case of nick */
716 if(!samenick)
717 {
718 /* force the TS to increase -- jilles */
719 if (source_p->tsinfo >= rb_current_time())
720 source_p->tsinfo++;
721 else
722 source_p->tsinfo = rb_current_time();
723 monitor_signoff(source_p);
724 /* we only do bancache for local users -- jilles */
725 if(source_p->user)
726 invalidate_bancache_user(source_p);
727 }
728
729 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
730 "Nick change: From %s to %s [%s@%s]",
731 source_p->name, nick, source_p->username, source_p->host);
732
733 /* send the nick change to the users channels */
734 sendto_common_channels_local(source_p, NOCAPS, ":%s!%s@%s NICK :%s",
735 source_p->name, source_p->username, source_p->host, nick);
736
737 /* send the nick change to servers.. */
738 if(source_p->user)
739 {
740 add_history(source_p, 1);
741
742 if (dosend)
743 {
744 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
745 use_id(source_p), nick, (long) source_p->tsinfo);
746 }
747 }
748
749 /* Finally, add to hash */
750 del_from_client_hash(source_p->name, source_p);
751 strcpy(source_p->name, nick);
752 add_to_client_hash(nick, source_p);
753
754 if(!samenick)
755 monitor_signon(source_p);
756
757 /* Make sure everyone that has this client on its accept list
758 * loses that reference.
759 */
760 /* we used to call del_all_accepts() here, but theres no real reason
761 * to clear a clients own list of accepted clients. So just remove
762 * them from everyone elses list --anfl
763 */
764 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->on_allow_list.head)
765 {
766 target_p = ptr->data;
767
768 rb_dlinkFindDestroy(source_p, &target_p->localClient->allow_list);
769 rb_dlinkDestroy(ptr, &source_p->on_allow_list);
770 }
771
772 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
773 rb_note(client_p->localClient->F, note);
774
775 return;
776 }
777
778 /*
779 * change_remote_nick()
780 */
781 static int
782 change_remote_nick(struct Client *client_p, struct Client *source_p,
783 time_t newts, const char *nick, int dosend)
784 {
785 struct nd_entry *nd;
786 int samenick = irccmp(source_p->name, nick) ? 0 : 1;
787
788 /* client changing their nick - dont reset ts if its same */
789 if(!samenick)
790 {
791 source_p->tsinfo = newts ? newts : rb_current_time();
792 monitor_signoff(source_p);
793 }
794
795 sendto_common_channels_local(source_p, NOCAPS, ":%s!%s@%s NICK :%s",
796 source_p->name, source_p->username, source_p->host, nick);
797
798 if(source_p->user)
799 {
800 add_history(source_p, 1);
801 if (dosend)
802 {
803 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
804 use_id(source_p), nick, (long) source_p->tsinfo);
805 }
806 }
807
808 del_from_client_hash(source_p->name, source_p);
809
810 /* invalidate nick delay when a remote client uses the nick.. */
811 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
812 free_nd_entry(nd);
813
814 strcpy(source_p->name, nick);
815 add_to_client_hash(nick, source_p);
816
817 if(!samenick)
818 monitor_signon(source_p);
819
820 /* remove all accepts pointing to the client */
821 del_all_accepts(source_p);
822
823 return 0;
824 }
825
826 static int
827 perform_nick_collides(struct Client *source_p, struct Client *client_p,
828 struct Client *target_p, int parc, const char *parv[],
829 time_t newts, const char *nick, const char *uid)
830 {
831 int sameuser;
832 int use_save;
833 const char *action;
834
835 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
836 uid != NULL && can_save(source_p);
837 action = use_save ? "saved" : "killed";
838
839 /* if we dont have a ts, or their TS's are the same, kill both */
840 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo))
841 {
842 sendto_realops_snomask(SNO_SKILL, L_ALL,
843 "Nick collision on %s(%s <- %s)(both %s)",
844 target_p->name, target_p->from->name, client_p->name, action);
845
846 if (use_save)
847 {
848 save_user(&me, &me, target_p);
849 ServerStats.is_save++;
850 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
851 uid, (long)newts);
852 register_client(client_p, source_p,
853 uid, SAVE_NICKTS, parc, parv);
854 }
855 else
856 {
857 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
858 form_str(ERR_NICKCOLLISION), target_p->name);
859
860 /* if the new client being introduced has a UID, we need to
861 * issue a KILL for it..
862 */
863 if(uid)
864 sendto_one(client_p, ":%s KILL %s :%s (Nick collision (new))",
865 me.id, uid, me.name);
866
867 /* we then need to KILL the old client everywhere */
868 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (new))", me.name);
869 ServerStats.is_kill++;
870
871 target_p->flags |= FLAGS_KILLED;
872 exit_client(client_p, target_p, &me, "Nick collision (new)");
873 }
874 return 0;
875 }
876 /* the timestamps are different */
877 else
878 {
879 sameuser = (target_p->user) && !irccmp(target_p->username, parv[5])
880 && !irccmp(target_p->host, parv[6]);
881
882 if((sameuser && newts < target_p->tsinfo) ||
883 (!sameuser && newts > target_p->tsinfo))
884 {
885 /* if we have a UID, then we need to issue a KILL,
886 * otherwise we do nothing and hope that the other
887 * client will collide it..
888 */
889 if (use_save)
890 {
891 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
892 uid, (long)newts);
893 register_client(client_p, source_p,
894 uid, SAVE_NICKTS, parc, parv);
895 }
896 else if(uid)
897 sendto_one(client_p,
898 ":%s KILL %s :%s (Nick collision (new))",
899 me.id, uid, me.name);
900 return 0;
901 }
902 else
903 {
904 if(sameuser)
905 sendto_realops_snomask(SNO_SKILL, L_ALL,
906 "Nick collision on %s(%s <- %s)(older %s)",
907 target_p->name, target_p->from->name,
908 client_p->name, action);
909 else
910 sendto_realops_snomask(SNO_SKILL, L_ALL,
911 "Nick collision on %s(%s <- %s)(newer %s)",
912 target_p->name, target_p->from->name,
913 client_p->name, action);
914
915 if (use_save)
916 {
917 ServerStats.is_save++;
918 save_user(&me, &me, target_p);
919 }
920 else
921 {
922 ServerStats.is_kill++;
923 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
924 form_str(ERR_NICKCOLLISION), target_p->name);
925
926 /* now we just need to kill the existing client */
927 kill_client_serv_butone(client_p, target_p,
928 "%s (Nick collision (new))", me.name);
929
930 target_p->flags |= FLAGS_KILLED;
931 (void) exit_client(client_p, target_p, &me, "Nick collision");
932 }
933
934 register_client(client_p, source_p,
935 nick, newts, parc, parv);
936
937 return 0;
938 }
939 }
940 }
941
942
943 static int
944 perform_nickchange_collides(struct Client *source_p, struct Client *client_p,
945 struct Client *target_p, int parc,
946 const char *parv[], time_t newts, const char *nick)
947 {
948 int sameuser;
949 int use_save;
950 const char *action;
951
952 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
953 can_save(source_p);
954 action = use_save ? "saved" : "killed";
955
956 /* its a client changing nick and causing a collide */
957 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo) || !source_p->user)
958 {
959 sendto_realops_snomask(SNO_SKILL, L_ALL,
960 "Nick change collision from %s to %s(%s <- %s)(both %s)",
961 source_p->name, target_p->name, target_p->from->name,
962 client_p->name, action);
963
964 if (use_save)
965 {
966 ServerStats.is_save += 2;
967 save_user(&me, &me, target_p);
968 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
969 source_p->id, (long)newts);
970 /* don't send a redundant nick change */
971 if (!IsDigit(source_p->name[0]))
972 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
973 }
974 else
975 {
976 ServerStats.is_kill++;
977 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
978 form_str(ERR_NICKCOLLISION), target_p->name);
979
980 kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name);
981
982 ServerStats.is_kill++;
983
984 kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name);
985
986 target_p->flags |= FLAGS_KILLED;
987 exit_client(NULL, target_p, &me, "Nick collision(new)");
988 source_p->flags |= FLAGS_KILLED;
989 exit_client(client_p, source_p, &me, "Nick collision(old)");
990 }
991 return 0;
992 }
993 else
994 {
995 sameuser = !irccmp(target_p->username, source_p->username) &&
996 !irccmp(target_p->host, source_p->host);
997
998 if((sameuser && newts < target_p->tsinfo) ||
999 (!sameuser && newts > target_p->tsinfo))
1000 {
1001 if(sameuser)
1002 sendto_realops_snomask(SNO_SKILL, L_ALL,
1003 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1004 source_p->name, target_p->name,
1005 target_p->from->name, client_p->name, action);
1006 else
1007 sendto_realops_snomask(SNO_SKILL, L_ALL,
1008 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1009 source_p->name, target_p->name,
1010 target_p->from->name, client_p->name, action);
1011
1012 if (use_save)
1013 {
1014 ServerStats.is_save++;
1015 /* can't broadcast a SAVE because the
1016 * nickchange has happened at client_p
1017 * but not in other directions -- jilles */
1018 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
1019 source_p->id, (long)newts);
1020 /* send a :<id> NICK <id> <ts> (!) */
1021 if (!IsDigit(source_p->name[0]))
1022 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
1023 }
1024 else
1025 {
1026 ServerStats.is_kill++;
1027
1028 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1029 form_str(ERR_NICKCOLLISION), target_p->name);
1030
1031 /* kill the client issuing the nickchange */
1032 kill_client_serv_butone(client_p, source_p,
1033 "%s (Nick change collision)", me.name);
1034
1035 source_p->flags |= FLAGS_KILLED;
1036
1037 if(sameuser)
1038 exit_client(client_p, source_p, &me, "Nick collision(old)");
1039 else
1040 exit_client(client_p, source_p, &me, "Nick collision(new)");
1041 }
1042 return 0;
1043 }
1044 else
1045 {
1046 if(sameuser)
1047 sendto_realops_snomask(SNO_SKILL, L_ALL,
1048 "Nick collision on %s(%s <- %s)(older %s)",
1049 target_p->name, target_p->from->name,
1050 client_p->name, action);
1051 else
1052 sendto_realops_snomask(SNO_SKILL, L_ALL,
1053 "Nick collision on %s(%s <- %s)(newer %s)",
1054 target_p->name, target_p->from->name,
1055 client_p->name, action);
1056
1057 if (use_save)
1058 {
1059 ServerStats.is_save++;
1060 save_user(&me, &me, target_p);
1061 }
1062 else
1063 {
1064 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1065 form_str(ERR_NICKCOLLISION), target_p->name);
1066
1067 /* kill the client who existed before hand */
1068 kill_client_serv_butone(client_p, target_p, "%s (Nick collision)", me.name);
1069
1070 ServerStats.is_kill++;
1071
1072 target_p->flags |= FLAGS_KILLED;
1073 (void) exit_client(client_p, target_p, &me, "Nick collision");
1074 }
1075 }
1076 }
1077
1078 change_remote_nick(client_p, source_p, newts, nick, 1);
1079
1080 return 0;
1081 }
1082
1083 static int
1084 register_client(struct Client *client_p, struct Client *server,
1085 const char *nick, time_t newts, int parc, const char *parv[])
1086 {
1087 struct Client *source_p;
1088 struct User *user;
1089 struct nd_entry *nd;
1090 const char *m;
1091 int flag;
1092
1093 source_p = make_client(client_p);
1094 user = make_user(source_p);
1095 rb_dlinkAddTail(source_p, &source_p->node, &global_client_list);
1096
1097 source_p->hopcount = atoi(parv[2]);
1098 source_p->tsinfo = newts;
1099
1100 strcpy(source_p->name, nick);
1101 rb_strlcpy(source_p->username, parv[5], sizeof(source_p->username));
1102 rb_strlcpy(source_p->host, parv[6], sizeof(source_p->host));
1103 rb_strlcpy(source_p->orighost, source_p->host, sizeof(source_p->orighost));
1104
1105 if(parc == 12)
1106 {
1107 rb_strlcpy(source_p->info, parv[11], sizeof(source_p->info));
1108 rb_strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1109 rb_strlcpy(source_p->id, parv[8], sizeof(source_p->id));
1110 add_to_id_hash(source_p->id, source_p);
1111 if (strcmp(parv[9], "*"))
1112 {
1113 rb_strlcpy(source_p->orighost, parv[9], sizeof(source_p->orighost));
1114 if (irccmp(source_p->host, source_p->orighost))
1115 SetDynSpoof(source_p);
1116 }
1117 if (strcmp(parv[10], "*"))
1118 rb_strlcpy(source_p->user->suser, parv[10], sizeof(source_p->user->suser));
1119 }
1120 else if(parc == 10)
1121 {
1122 rb_strlcpy(source_p->info, parv[9], sizeof(source_p->info));
1123 rb_strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1124 rb_strlcpy(source_p->id, parv[8], sizeof(source_p->id));
1125 add_to_id_hash(source_p->id, source_p);
1126 }
1127 else
1128 {
1129 s_assert(0);
1130 }
1131
1132 /* remove any nd entries for this nick */
1133 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
1134 free_nd_entry(nd);
1135
1136 add_to_client_hash(nick, source_p);
1137 add_to_hostname_hash(source_p->orighost, source_p);
1138 monitor_signon(source_p);
1139
1140 m = &parv[4][1];
1141 while(*m)
1142 {
1143 flag = user_modes[(unsigned char) *m];
1144
1145 if(flag & UMODE_SERVICE)
1146 {
1147 int hit = 0;
1148 rb_dlink_node *ptr;
1149
1150 RB_DLINK_FOREACH(ptr, service_list.head)
1151 {
1152 if(!irccmp((const char *) ptr->data, server->name))
1153 {
1154 hit++;
1155 break;
1156 }
1157 }
1158
1159 if(!hit)
1160 {
1161 m++;
1162 continue;
1163 }
1164 }
1165
1166 /* increment +i count if theyre invis */
1167 if(!(source_p->umodes & UMODE_INVISIBLE) && (flag & UMODE_INVISIBLE))
1168 Count.invisi++;
1169
1170 /* increment opered count if theyre opered */
1171 if(!(source_p->umodes & UMODE_OPER) && (flag & UMODE_OPER))
1172 Count.oper++;
1173
1174 source_p->umodes |= flag;
1175 m++;
1176 }
1177
1178 if(IsOper(source_p) && !IsService(source_p))
1179 rb_dlinkAddAlloc(source_p, &oper_list);
1180
1181 SetRemoteClient(source_p);
1182
1183 if(++Count.total > Count.max_tot)
1184 Count.max_tot = Count.total;
1185
1186 source_p->servptr = server;
1187
1188 rb_dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
1189
1190 call_hook(h_new_remote_user, source_p);
1191
1192 return (introduce_client(client_p, source_p, user, nick, parc == 12));
1193 }
1194
1195 /* Check if we can do SAVE. target_p can be a client to save or a
1196 * server introducing a client -- jilles */
1197 static int
1198 can_save(struct Client *target_p)
1199 {
1200 struct Client *serv_p;
1201
1202 if (MyClient(target_p))
1203 return 1;
1204 if (!has_id(target_p))
1205 return 0;
1206 serv_p = IsServer(target_p) ? target_p : target_p->servptr;
1207 while (serv_p != NULL && serv_p != &me)
1208 {
1209 if (!(serv_p->serv->caps & CAP_SAVE))
1210 return 0;
1211 serv_p = serv_p->servptr;
1212 }
1213 return serv_p == &me;
1214 }
1215
1216 static void
1217 save_user(struct Client *client_p, struct Client *source_p,
1218 struct Client *target_p)
1219 {
1220 if (!MyConnect(target_p) && (!has_id(target_p) || !IsCapable(target_p->from, CAP_SAVE)))
1221 {
1222 /* This shouldn't happen */
1223 /* Note we only need SAVE support in this direction */
1224 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1225 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1226 target_p->name, target_p->username, target_p->host, source_p->name, target_p->from->name);
1227 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (no SAVE support))", me.name);
1228 ServerStats.is_kill++;
1229
1230 target_p->flags |= FLAGS_KILLED;
1231 (void) exit_client(NULL, target_p, &me, "Nick collision (no SAVE support)");
1232 return;
1233 }
1234 sendto_server(client_p, NULL, CAP_SAVE|CAP_TS6, NOCAPS, ":%s SAVE %s %ld",
1235 source_p->id, target_p->id, (long)target_p->tsinfo);
1236 sendto_server(client_p, NULL, CAP_TS6, CAP_SAVE, ":%s NICK %s :%ld",
1237 target_p->id, target_p->id, (long)SAVE_NICKTS);
1238 if (!IsMe(client_p))
1239 sendto_realops_snomask(SNO_SKILL, L_ALL,
1240 "Received SAVE message for %s from %s",
1241 target_p->name, source_p->name);
1242 if (MyClient(target_p))
1243 {
1244 sendto_one_numeric(target_p, RPL_SAVENICK,
1245 form_str(RPL_SAVENICK), target_p->id);
1246 change_local_nick(target_p, target_p, target_p->id, 0);
1247 target_p->tsinfo = SAVE_NICKTS;
1248 }
1249 else
1250 change_remote_nick(target_p, target_p, SAVE_NICKTS, target_p->id, 0);
1251 }
1252
1253 static void bad_nickname(struct Client *client_p, const char *nick)
1254 {
1255 char squitreason[100];
1256
1257 sendto_wallops_flags(UMODE_WALLOP, &me,
1258 "Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1259 client_p->name, nick);
1260 sendto_server(NULL, NULL, CAP_TS6, NOCAPS,
1261 ":%s WALLOPS :Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1262 me.id, client_p->name, nick);
1263 ilog(L_SERVER, "Link %s cancelled, bad nickname %s sent (NICKLEN mismatch?)",
1264 client_p->name, nick);
1265
1266 rb_snprintf(squitreason, sizeof squitreason,
1267 "Bad nickname introduced [%s]", nick);
1268 exit_client(client_p, client_p, &me, squitreason);
1269 }