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