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