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