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