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