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