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