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