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