]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/core/m_nick.c
Various changes for libratbox.
[irc/rqf/shadowircd.git] / modules / core / m_nick.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_nick.c: Sets a users nick.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: m_nick.c 3518 2007-06-22 21:59:09Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "client.h"
29 #include "hash.h"
30 #include "irc_string.h"
31 #include "ircd.h"
32 #include "numeric.h"
33 #include "s_conf.h"
34 #include "s_stats.h"
35 #include "s_user.h"
36 #include "hash.h"
37 #include "whowas.h"
38 #include "s_serv.h"
39 #include "send.h"
40 #include "channel.h"
41 #include "s_log.h"
42 #include "msg.h"
43 #include "parse.h"
44 #include "modules.h"
45 #include "common.h"
46 #include "packet.h"
47 #include "scache.h"
48 #include "s_newconf.h"
49 #include "monitor.h"
50
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
58 static int mr_nick(struct Client *, struct Client *, int, const char **);
59 static int m_nick(struct Client *, struct Client *, int, const char **);
60 static int mc_nick(struct Client *, struct Client *, int, const char **);
61 static int ms_nick(struct Client *, struct Client *, int, const char **);
62 static int ms_uid(struct Client *, struct Client *, int, const char **);
63 static int ms_euid(struct Client *, struct Client *, int, const char **);
64 static int ms_save(struct Client *, struct Client *, int, const char **);
65 static int can_save(struct Client *);
66 static void save_user(struct Client *, struct Client *, struct Client *);
67
68 struct 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 };
72 struct 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 };
76 struct 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 };
80 struct 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
85 mapi_clist_av1 nick_clist[] = { &nick_msgtab, &uid_msgtab, &euid_msgtab,
86 &save_msgtab, NULL };
87
88 DECLARE_MODULE_AV1(nick, NULL, NULL, nick_clist, NULL, NULL, "$Revision: 3518 $");
89
90 static int change_remote_nick(struct Client *, struct Client *, time_t,
91 const char *, int);
92
93 static int clean_nick(const char *, int loc_client);
94 static int clean_username(const char *);
95 static int clean_host(const char *);
96 static int clean_uid(const char *uid);
97
98 static void set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick);
99 static void change_local_nick(struct Client *client_p, struct Client *source_p, char *nick, int);
100 static int register_client(struct Client *client_p, struct Client *server,
101 const char *nick, time_t newts, int parc, const char *parv[]);
102
103 static int perform_nick_collides(struct Client *, struct Client *,
104 struct Client *, int, const char **,
105 time_t, const char *, const char *);
106 static 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 */
113 static int
114 mr_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
159 if(irc_dictionary_find(nd_dict, nick))
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 */
180 static int
181 m_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
220 if(irc_dictionary_find(nd_dict, nick))
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 */
275 static int
276 mc_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)",
287 parv[1], source_p->servptr->name, client_p->name);
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
326 static int
327 ms_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 */
416 static int
417 ms_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 */
508 static int
509 ms_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 */
601 static int
602 ms_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 */
632 static int
633 clean_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 */
664 static int
665 clean_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 */
689 static int
690 clean_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
708 static int
709 clean_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
730 static void
731 set_initial_nick(struct Client *client_p, struct Client *source_p, char *nick)
732 {
733 char buf[USERLEN + 1];
734 char note[NICKLEN + 10];
735
736 /* This had to be copied here to avoid problems.. */
737 source_p->tsinfo = CurrentTime;
738 if(source_p->name[0])
739 del_from_client_hash(source_p->name, source_p);
740
741 strcpy(source_p->name, nick);
742 add_to_client_hash(nick, source_p);
743
744 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
745 rb_note(client_p->localClient->F, note);
746
747 if(source_p->flags & FLAGS_SENTUSER)
748 {
749 strlcpy(buf, source_p->username, sizeof(buf));
750
751 /* got user, heres nick. */
752 register_local_user(client_p, source_p, buf);
753
754 }
755 }
756
757 static void
758 change_local_nick(struct Client *client_p, struct Client *source_p,
759 char *nick, int dosend)
760 {
761 struct Client *target_p;
762 rb_dlink_node *ptr, *next_ptr;
763 struct Channel *chptr;
764 char note[NICKLEN + 10];
765 int samenick;
766
767 if (dosend)
768 {
769 chptr = find_bannickchange_channel(source_p);
770 if (chptr != NULL)
771 {
772 sendto_one_numeric(source_p, ERR_BANNICKCHANGE,
773 form_str(ERR_BANNICKCHANGE),
774 nick, chptr->chname);
775 return;
776 }
777 if((source_p->localClient->last_nick_change + ConfigFileEntry.max_nick_time) < CurrentTime)
778 source_p->localClient->number_of_nick_changes = 0;
779
780 source_p->localClient->last_nick_change = CurrentTime;
781 source_p->localClient->number_of_nick_changes++;
782
783 if(ConfigFileEntry.anti_nick_flood && !IsOper(source_p) &&
784 source_p->localClient->number_of_nick_changes > ConfigFileEntry.max_nick_changes)
785 {
786 sendto_one(source_p, form_str(ERR_NICKTOOFAST),
787 me.name, source_p->name, source_p->name,
788 nick, ConfigFileEntry.max_nick_time);
789 return;
790 }
791 }
792
793 samenick = irccmp(source_p->name, nick) ? 0 : 1;
794
795 /* dont reset TS if theyre just changing case of nick */
796 if(!samenick)
797 {
798 /* force the TS to increase -- jilles */
799 if (source_p->tsinfo >= CurrentTime)
800 source_p->tsinfo++;
801 else
802 source_p->tsinfo = CurrentTime;
803 monitor_signoff(source_p);
804 /* we only do bancache for local users -- jilles */
805 if(source_p->user)
806 invalidate_bancache_user(source_p);
807 }
808
809 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
810 "Nick change: From %s to %s [%s@%s]",
811 source_p->name, nick, source_p->username, source_p->host);
812
813 /* send the nick change to the users channels */
814 sendto_common_channels_local(source_p, ":%s!%s@%s NICK :%s",
815 source_p->name, source_p->username, source_p->host, nick);
816
817 /* send the nick change to servers.. */
818 if(source_p->user)
819 {
820 add_history(source_p, 1);
821
822 if (dosend)
823 {
824 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
825 use_id(source_p), nick, (long) source_p->tsinfo);
826 sendto_server(client_p, NULL, NOCAPS, CAP_TS6, ":%s NICK %s :%ld",
827 source_p->name, nick, (long) source_p->tsinfo);
828 }
829 }
830
831 /* Finally, add to hash */
832 del_from_client_hash(source_p->name, source_p);
833 strcpy(source_p->name, nick);
834 add_to_client_hash(nick, source_p);
835
836 if(!samenick)
837 monitor_signon(source_p);
838
839 /* Make sure everyone that has this client on its accept list
840 * loses that reference.
841 */
842 /* we used to call del_all_accepts() here, but theres no real reason
843 * to clear a clients own list of accepted clients. So just remove
844 * them from everyone elses list --anfl
845 */
846 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->on_allow_list.head)
847 {
848 target_p = ptr->data;
849
850 rb_dlinkFindDestroy(source_p, &target_p->localClient->allow_list);
851 rb_dlinkDestroy(ptr, &source_p->on_allow_list);
852 }
853
854 rb_snprintf(note, sizeof(note), "Nick: %s", nick);
855 rb_note(client_p->localClient->F, note);
856
857 return;
858 }
859
860 /*
861 * change_remote_nick()
862 */
863 static int
864 change_remote_nick(struct Client *client_p, struct Client *source_p,
865 time_t newts, const char *nick, int dosend)
866 {
867 struct nd_entry *nd;
868 int samenick = irccmp(source_p->name, nick) ? 0 : 1;
869
870 /* client changing their nick - dont reset ts if its same */
871 if(!samenick)
872 {
873 source_p->tsinfo = newts ? newts : CurrentTime;
874 monitor_signoff(source_p);
875 }
876
877 sendto_common_channels_local(source_p, ":%s!%s@%s NICK :%s",
878 source_p->name, source_p->username, source_p->host, nick);
879
880 if(source_p->user)
881 {
882 add_history(source_p, 1);
883 if (dosend)
884 {
885 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
886 use_id(source_p), nick, (long) source_p->tsinfo);
887 sendto_server(client_p, NULL, NOCAPS, CAP_TS6, ":%s NICK %s :%ld",
888 source_p->name, nick, (long) source_p->tsinfo);
889 }
890 }
891
892 del_from_client_hash(source_p->name, source_p);
893
894 /* invalidate nick delay when a remote client uses the nick.. */
895 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
896 free_nd_entry(nd);
897
898 strcpy(source_p->name, nick);
899 add_to_client_hash(nick, source_p);
900
901 if(!samenick)
902 monitor_signon(source_p);
903
904 /* remove all accepts pointing to the client */
905 del_all_accepts(source_p);
906
907 return 0;
908 }
909
910 static int
911 perform_nick_collides(struct Client *source_p, struct Client *client_p,
912 struct Client *target_p, int parc, const char *parv[],
913 time_t newts, const char *nick, const char *uid)
914 {
915 int sameuser;
916 int use_save;
917 const char *action;
918
919 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
920 uid != NULL && can_save(source_p);
921 action = use_save ? "saved" : "killed";
922
923 /* if we dont have a ts, or their TS's are the same, kill both */
924 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo))
925 {
926 sendto_realops_snomask(SNO_GENERAL, L_ALL,
927 "Nick collision on %s(%s <- %s)(both %s)",
928 target_p->name, target_p->from->name, client_p->name, action);
929
930 if (use_save)
931 {
932 save_user(&me, &me, target_p);
933 ServerStats->is_save++;
934 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
935 uid, (long)newts);
936 register_client(client_p, source_p,
937 uid, SAVE_NICKTS, parc, parv);
938 }
939 else
940 {
941 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
942 form_str(ERR_NICKCOLLISION), target_p->name);
943
944 /* if the new client being introduced has a UID, we need to
945 * issue a KILL for it..
946 */
947 if(uid)
948 sendto_one(client_p, ":%s KILL %s :%s (Nick collision (new))",
949 me.id, uid, me.name);
950
951 /* we then need to KILL the old client everywhere */
952 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (new))", me.name);
953 ServerStats->is_kill++;
954
955 target_p->flags |= FLAGS_KILLED;
956 exit_client(client_p, target_p, &me, "Nick collision (new)");
957 }
958 return 0;
959 }
960 /* the timestamps are different */
961 else
962 {
963 sameuser = (target_p->user) && !irccmp(target_p->username, parv[5])
964 && !irccmp(target_p->host, parv[6]);
965
966 if((sameuser && newts < target_p->tsinfo) ||
967 (!sameuser && newts > target_p->tsinfo))
968 {
969 /* if we have a UID, then we need to issue a KILL,
970 * otherwise we do nothing and hope that the other
971 * client will collide it..
972 */
973 if (use_save)
974 {
975 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
976 uid, (long)newts);
977 register_client(client_p, source_p,
978 uid, SAVE_NICKTS, parc, parv);
979 }
980 else if(uid)
981 sendto_one(client_p,
982 ":%s KILL %s :%s (Nick collision (new))",
983 me.id, uid, me.name);
984 return 0;
985 }
986 else
987 {
988 if(sameuser)
989 sendto_realops_snomask(SNO_GENERAL, L_ALL,
990 "Nick collision on %s(%s <- %s)(older %s)",
991 target_p->name, target_p->from->name,
992 client_p->name, action);
993 else
994 sendto_realops_snomask(SNO_GENERAL, L_ALL,
995 "Nick collision on %s(%s <- %s)(newer %s)",
996 target_p->name, target_p->from->name,
997 client_p->name, action);
998
999 if (use_save)
1000 {
1001 ServerStats->is_save++;
1002 save_user(&me, &me, target_p);
1003 }
1004 else
1005 {
1006 ServerStats->is_kill++;
1007 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1008 form_str(ERR_NICKCOLLISION), target_p->name);
1009
1010 /* now we just need to kill the existing client */
1011 kill_client_serv_butone(client_p, target_p,
1012 "%s (Nick collision (new))", me.name);
1013
1014 target_p->flags |= FLAGS_KILLED;
1015 (void) exit_client(client_p, target_p, &me, "Nick collision");
1016 }
1017
1018 register_client(client_p, parc >= 10 ? source_p : NULL,
1019 nick, newts, parc, parv);
1020
1021 return 0;
1022 }
1023 }
1024 }
1025
1026
1027 static int
1028 perform_nickchange_collides(struct Client *source_p, struct Client *client_p,
1029 struct Client *target_p, int parc,
1030 const char *parv[], time_t newts, const char *nick)
1031 {
1032 int sameuser;
1033 int use_save;
1034 const char *action;
1035
1036 use_save = ConfigFileEntry.collision_fnc && can_save(target_p) &&
1037 can_save(source_p);
1038 action = use_save ? "saved" : "killed";
1039
1040 /* its a client changing nick and causing a collide */
1041 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo) || !source_p->user)
1042 {
1043 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1044 "Nick change collision from %s to %s(%s <- %s)(both %s)",
1045 source_p->name, target_p->name, target_p->from->name,
1046 client_p->name, action);
1047
1048 if (use_save)
1049 {
1050 ServerStats->is_save += 2;
1051 save_user(&me, &me, target_p);
1052 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
1053 source_p->id, (long)newts);
1054 /* don't send a redundant nick change */
1055 if (!IsDigit(source_p->name[0]))
1056 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
1057 }
1058 else
1059 {
1060 ServerStats->is_kill++;
1061 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1062 form_str(ERR_NICKCOLLISION), target_p->name);
1063
1064 kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name);
1065
1066 ServerStats->is_kill++;
1067
1068 kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name);
1069
1070 target_p->flags |= FLAGS_KILLED;
1071 exit_client(NULL, target_p, &me, "Nick collision(new)");
1072 source_p->flags |= FLAGS_KILLED;
1073 exit_client(client_p, source_p, &me, "Nick collision(old)");
1074 }
1075 return 0;
1076 }
1077 else
1078 {
1079 sameuser = !irccmp(target_p->username, source_p->username) &&
1080 !irccmp(target_p->host, source_p->host);
1081
1082 if((sameuser && newts < target_p->tsinfo) ||
1083 (!sameuser && newts > target_p->tsinfo))
1084 {
1085 if(sameuser)
1086 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1087 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1088 source_p->name, target_p->name,
1089 target_p->from->name, client_p->name, action);
1090 else
1091 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1092 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1093 source_p->name, target_p->name,
1094 target_p->from->name, client_p->name, action);
1095
1096 if (use_save)
1097 {
1098 ServerStats->is_save++;
1099 /* can't broadcast a SAVE because the
1100 * nickchange has happened at client_p
1101 * but not in other directions -- jilles */
1102 sendto_one(client_p, ":%s SAVE %s %ld", me.id,
1103 source_p->id, (long)newts);
1104 /* send a :<id> NICK <id> <ts> (!) */
1105 if (!IsDigit(source_p->name[0]))
1106 change_remote_nick(client_p, source_p, SAVE_NICKTS, source_p->id, 1);
1107 }
1108 else
1109 {
1110 ServerStats->is_kill++;
1111
1112 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1113 form_str(ERR_NICKCOLLISION), target_p->name);
1114
1115 /* kill the client issuing the nickchange */
1116 kill_client_serv_butone(client_p, source_p,
1117 "%s (Nick change collision)", me.name);
1118
1119 source_p->flags |= FLAGS_KILLED;
1120
1121 if(sameuser)
1122 exit_client(client_p, source_p, &me, "Nick collision(old)");
1123 else
1124 exit_client(client_p, source_p, &me, "Nick collision(new)");
1125 }
1126 return 0;
1127 }
1128 else
1129 {
1130 if(sameuser)
1131 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1132 "Nick collision on %s(%s <- %s)(older %s)",
1133 target_p->name, target_p->from->name,
1134 client_p->name, action);
1135 else
1136 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1137 "Nick collision on %s(%s <- %s)(newer %s)",
1138 target_p->name, target_p->from->name,
1139 client_p->name, action);
1140
1141 if (use_save)
1142 {
1143 ServerStats->is_save++;
1144 save_user(&me, &me, target_p);
1145 }
1146 else
1147 {
1148 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
1149 form_str(ERR_NICKCOLLISION), target_p->name);
1150
1151 /* kill the client who existed before hand */
1152 kill_client_serv_butone(client_p, target_p, "%s (Nick collision)", me.name);
1153
1154 ServerStats->is_kill++;
1155
1156 target_p->flags |= FLAGS_KILLED;
1157 (void) exit_client(client_p, target_p, &me, "Nick collision");
1158 }
1159 }
1160 }
1161
1162 change_remote_nick(client_p, source_p, newts, nick, 1);
1163
1164 return 0;
1165 }
1166
1167 static int
1168 register_client(struct Client *client_p, struct Client *server,
1169 const char *nick, time_t newts, int parc, const char *parv[])
1170 {
1171 struct Client *source_p;
1172 struct User *user;
1173 struct nd_entry *nd;
1174 const char *m;
1175 int flag;
1176
1177 if(server == NULL)
1178 {
1179 if((server = find_server(NULL, parv[7])) == NULL)
1180 {
1181 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1182 "Ghost killed: %s on invalid server %s",
1183 nick, parv[7]);
1184 sendto_one(client_p, ":%s KILL %s :%s (Server doesn't exist)",
1185 get_id(&me, client_p), nick, me.name);
1186 return 0;
1187 }
1188 }
1189
1190 source_p = make_client(client_p);
1191 user = make_user(source_p);
1192 rb_dlinkAddTail(source_p, &source_p->node, &global_client_list);
1193
1194 source_p->hopcount = atoi(parv[2]);
1195 source_p->tsinfo = newts;
1196
1197 strcpy(source_p->name, nick);
1198 strlcpy(source_p->username, parv[5], sizeof(source_p->username));
1199 strlcpy(source_p->host, parv[6], sizeof(source_p->host));
1200 strlcpy(source_p->orighost, source_p->host, sizeof(source_p->orighost));
1201
1202 if(parc == 12)
1203 {
1204 strlcpy(source_p->info, parv[11], sizeof(source_p->info));
1205 strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1206 strlcpy(source_p->id, parv[8], sizeof(source_p->id));
1207 add_to_id_hash(source_p->id, source_p);
1208 if (strcmp(parv[9], "*"))
1209 {
1210 strlcpy(source_p->orighost, parv[9], sizeof(source_p->orighost));
1211 if (irccmp(source_p->host, source_p->orighost))
1212 SetDynSpoof(source_p);
1213 }
1214 if (strcmp(parv[10], "*"))
1215 strlcpy(source_p->user->suser, parv[10], sizeof(source_p->user->suser));
1216 }
1217 else if(parc == 10)
1218 {
1219 strlcpy(source_p->info, parv[9], sizeof(source_p->info));
1220 strlcpy(source_p->sockhost, parv[7], sizeof(source_p->sockhost));
1221 strlcpy(source_p->id, parv[8], sizeof(source_p->id));
1222 add_to_id_hash(source_p->id, source_p);
1223 }
1224 else
1225 {
1226 strlcpy(source_p->info, parv[8], sizeof(source_p->info));
1227 }
1228
1229 /* remove any nd entries for this nick */
1230 if((nd = irc_dictionary_retrieve(nd_dict, nick)))
1231 free_nd_entry(nd);
1232
1233 add_to_client_hash(nick, source_p);
1234 add_to_hostname_hash(source_p->orighost, source_p);
1235 monitor_signon(source_p);
1236
1237 m = &parv[4][1];
1238 while(*m)
1239 {
1240 flag = user_modes[(unsigned char) *m];
1241
1242 if(flag & UMODE_SERVICE)
1243 {
1244 int hit = 0;
1245 rb_dlink_node *ptr;
1246
1247 RB_DLINK_FOREACH(ptr, service_list.head)
1248 {
1249 if(!irccmp((const char *) ptr->data, server->name))
1250 {
1251 hit++;
1252 break;
1253 }
1254 }
1255
1256 if(!hit)
1257 {
1258 m++;
1259 continue;
1260 }
1261 }
1262
1263 /* increment +i count if theyre invis */
1264 if(!(source_p->umodes & UMODE_INVISIBLE) && (flag & UMODE_INVISIBLE))
1265 Count.invisi++;
1266
1267 /* increment opered count if theyre opered */
1268 if(!(source_p->umodes & UMODE_OPER) && (flag & UMODE_OPER))
1269 Count.oper++;
1270
1271 source_p->umodes |= flag;
1272 m++;
1273 }
1274
1275 if(IsOper(source_p) && !IsService(source_p))
1276 rb_dlinkAddAlloc(source_p, &oper_list);
1277
1278 SetRemoteClient(source_p);
1279
1280 if(++Count.total > Count.max_tot)
1281 Count.max_tot = Count.total;
1282
1283 source_p->servptr = server;
1284
1285 rb_dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
1286
1287 /* fake direction */
1288 if(source_p->servptr->from != source_p->from)
1289 {
1290 struct Client *target_p = source_p->servptr->from;
1291
1292 sendto_realops_snomask(SNO_DEBUG, L_ALL,
1293 "Bad User [%s] :%s USER %s@%s %s, != %s[%s]",
1294 client_p->name, source_p->name,
1295 source_p->username, source_p->host,
1296 server->name, target_p->name, target_p->from->name);
1297 kill_client(client_p, source_p,
1298 "%s (NICK from wrong direction (%s != %s))",
1299 me.name, server->name, target_p->from->name);
1300 source_p->flags |= FLAGS_KILLED;
1301 return exit_client(source_p, source_p, &me, "USER server wrong direction");
1302 }
1303
1304 call_hook(h_new_remote_user, source_p);
1305
1306 return (introduce_client(client_p, source_p, user, nick, parc == 12));
1307 }
1308
1309 /* Check if we can do SAVE. target_p can be a client to save or a
1310 * server introducing a client -- jilles */
1311 static int
1312 can_save(struct Client *target_p)
1313 {
1314 struct Client *serv_p;
1315
1316 if (MyClient(target_p))
1317 return 1;
1318 if (!has_id(target_p))
1319 return 0;
1320 serv_p = IsServer(target_p) ? target_p : target_p->servptr;
1321 while (serv_p != NULL && serv_p != &me)
1322 {
1323 if (!(serv_p->serv->caps & CAP_SAVE))
1324 return 0;
1325 serv_p = serv_p->servptr;
1326 }
1327 return serv_p == &me;
1328 }
1329
1330 static void
1331 save_user(struct Client *client_p, struct Client *source_p,
1332 struct Client *target_p)
1333 {
1334 if (!MyConnect(target_p) && (!has_id(target_p) || !IsCapable(target_p->from, CAP_SAVE)))
1335 {
1336 /* This shouldn't happen */
1337 /* Note we only need SAVE support in this direction */
1338 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1339 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1340 target_p->name, target_p->username, target_p->host, source_p->name, target_p->from->name);
1341 kill_client_serv_butone(NULL, target_p, "%s (Nick collision (no SAVE support))", me.name);
1342 ServerStats->is_kill++;
1343
1344 target_p->flags |= FLAGS_KILLED;
1345 (void) exit_client(NULL, target_p, &me, "Nick collision (no SAVE support)");
1346 return;
1347 }
1348 sendto_server(client_p, NULL, CAP_SAVE|CAP_TS6, NOCAPS, ":%s SAVE %s %ld",
1349 source_p->id, target_p->id, (long)target_p->tsinfo);
1350 sendto_server(client_p, NULL, CAP_TS6, CAP_SAVE, ":%s NICK %s :%ld",
1351 target_p->id, target_p->id, (long)SAVE_NICKTS);
1352 sendto_server(client_p, NULL, NOCAPS, CAP_TS6, ":%s NICK %s :%ld",
1353 target_p->name, target_p->id, (long)SAVE_NICKTS);
1354 if (!IsMe(client_p))
1355 sendto_realops_snomask(SNO_SKILL, L_ALL,
1356 "Received SAVE message for %s from %s",
1357 target_p->name, source_p->name);
1358 if (MyClient(target_p))
1359 {
1360 sendto_one_numeric(target_p, RPL_SAVENICK,
1361 form_str(RPL_SAVENICK), target_p->id);
1362 change_local_nick(target_p, target_p, target_p->id, 0);
1363 target_p->tsinfo = SAVE_NICKTS;
1364 }
1365 else
1366 change_remote_nick(target_p, target_p, SAVE_NICKTS, target_p->id, 0);
1367 }