]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/core/m_server.c
[svn] - the new plan:
[irc/rqf/shadowircd.git] / modules / core / m_server.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_server.c: Introduces a server.
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_server.c 2733 2006-11-10 00:04:08Z jilles $
25 */
26
27#include "stdinc.h"
28#include "tools.h"
29#include "client.h" /* client struct */
30#include "common.h" /* TRUE bleah */
31#include "event.h"
32#include "hash.h" /* add_to_client_hash */
33#include "irc_string.h"
34#include "ircd.h" /* me */
35#include "numeric.h" /* ERR_xxx */
36#include "s_conf.h" /* struct ConfItem */
37#include "s_newconf.h"
38#include "s_log.h" /* log level defines */
39#include "s_serv.h" /* server_estab, check_server */
40#include "s_stats.h" /* ServerStats */
41#include "scache.h" /* find_or_add */
42#include "send.h" /* sendto_one */
43#include "msg.h"
44#include "parse.h"
45#include "modules.h"
46
47static int mr_server(struct Client *, struct Client *, int, const char **);
48static int ms_server(struct Client *, struct Client *, int, const char **);
49static int ms_sid(struct Client *, struct Client *, int, const char **);
50
51struct Message server_msgtab = {
52 "SERVER", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
53 {{mr_server, 4}, mg_reg, mg_ignore, {ms_server, 4}, mg_ignore, mg_reg}
54};
55struct Message sid_msgtab = {
56 "SID", 0, 0, 0, MFLG_SLOW,
57 {mg_ignore, mg_reg, mg_ignore, {ms_sid, 5}, mg_ignore, mg_reg}
58};
59
60mapi_clist_av1 server_clist[] = { &server_msgtab, &sid_msgtab, NULL };
61
62DECLARE_MODULE_AV1(server, NULL, NULL, server_clist, NULL, NULL, "$Revision: 2733 $");
63
64int bogus_host(const char *host);
65struct Client *server_exists(const char *);
66static int set_server_gecos(struct Client *, const char *);
67
68/*
69 * mr_server - SERVER message handler
70 * parv[0] = sender prefix
71 * parv[1] = servername
72 * parv[2] = serverinfo/hopcount
73 * parv[3] = serverinfo
74 */
75static int
76mr_server(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
77{
78 char info[REALLEN + 1];
79 const char *name;
80 struct Client *target_p;
81 int hop;
82
83 name = parv[1];
84 hop = atoi(parv[2]);
85 strlcpy(info, parv[3], sizeof(info));
86
87 /*
88 * Reject a direct nonTS server connection if we're TS_ONLY -orabidoo
89 */
90 if(!DoesTS(client_p))
91 {
92 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Link %s dropped, non-TS server",
93 get_server_name(client_p, HIDE_IP));
94 exit_client(client_p, client_p, client_p, "Non-TS server");
95 return 0;
96 }
97
98 if(bogus_host(name))
99 {
100 exit_client(client_p, client_p, client_p, "Bogus server name");
101 return 0;
102 }
103
104 /* Now we just have to call check_server and everything should be
105 * check for us... -A1kmm. */
106 switch (check_server(name, client_p))
107 {
108 case -1:
109 if(ConfigFileEntry.warn_no_nline)
110 {
111 sendto_realops_snomask(SNO_GENERAL, L_ALL,
112 "Unauthorised server connection attempt from %s: "
113 "No entry for servername %s",
114 get_server_name(client_p, HIDE_IP), name);
115
116 ilog(L_SERVER, "Access denied, no connect block for server %s%s",
117 EmptyString(client_p->name) ? name : "",
118 log_client_name(client_p, SHOW_IP));
119 }
120
121 exit_client(client_p, client_p, client_p, "Invalid servername.");
122 return 0;
123 /* NOT REACHED */
124 break;
125
126 case -2:
127 sendto_realops_snomask(SNO_GENERAL, L_ALL,
128 "Unauthorised server connection attempt from %s: "
129 "Bad password for server %s",
130 get_server_name(client_p, HIDE_IP), name);
131
132 ilog(L_SERVER, "Access denied, invalid password for server %s%s",
133 EmptyString(client_p->name) ? name : "",
134 log_client_name(client_p, SHOW_IP));
135
136 exit_client(client_p, client_p, client_p, "Invalid password.");
137 return 0;
138 /* NOT REACHED */
139 break;
140
141 case -3:
142 sendto_realops_snomask(SNO_GENERAL, L_ALL,
143 "Unauthorised server connection attempt from %s: "
144 "Invalid host for server %s",
145 get_server_name(client_p, HIDE_IP), name);
146
147 ilog(L_SERVER, "Access denied, invalid host for server %s%s",
148 EmptyString(client_p->name) ? name : "",
149 log_client_name(client_p, SHOW_IP));
150
151 exit_client(client_p, client_p, client_p, "Invalid host.");
152 return 0;
153 /* NOT REACHED */
154 break;
155
156 /* servername is > HOSTLEN */
157 case -4:
158 sendto_realops_snomask(SNO_GENERAL, L_ALL,
159 "Invalid servername %s from %s",
160 name, get_server_name(client_p, HIDE_IP));
161 ilog(L_SERVER, "Access denied, invalid servername from %s",
162 log_client_name(client_p, SHOW_IP));
163
164 exit_client(client_p, client_p, client_p, "Invalid servername.");
165 return 0;
166 /* NOT REACHED */
167 break;
168 }
169
170 if((target_p = server_exists(name)))
171 {
172 /*
173 * This link is trying feed me a server that I already have
174 * access through another path -- multiple paths not accepted
175 * currently, kill this link immediately!!
176 *
177 * Rather than KILL the link which introduced it, KILL the
178 * youngest of the two links. -avalon
179 *
180 * Definitely don't do that here. This is from an unregistered
181 * connect - A1kmm.
182 */
183 sendto_realops_snomask(SNO_GENERAL, L_ALL,
184 "Attempt to re-introduce server %s from %s",
185 name, get_server_name(client_p, HIDE_IP));
186 ilog(L_SERVER, "Attempt to re-introduce server %s from %s",
187 name, log_client_name(client_p, SHOW_IP));
188
189 sendto_one(client_p, "ERROR :Server already exists.");
190 exit_client(client_p, client_p, client_p, "Server Exists");
191 return 0;
192 }
193
194 if(has_id(client_p) && (target_p = find_id(client_p->id)) != NULL)
195 {
196 sendto_realops_snomask(SNO_GENERAL, L_ALL,
197 "Attempt to re-introduce SID %s from %s%s",
198 client_p->id,
199 EmptyString(client_p->name) ? name : "",
200 get_server_name(client_p, HIDE_IP));
201 ilog(L_SERVER, "Attempt to re-introduce SID %s from %s%s",
202 client_p->id,
203 EmptyString(client_p->name) ? name : "",
204 log_client_name(client_p, SHOW_IP));
205
206 sendto_one(client_p, "ERROR :SID already exists.");
207 exit_client(client_p, client_p, client_p, "SID Exists");
208 return 0;
209 }
210
211 /*
212 * if we are connecting (Handshake), we already have the name from the
213 * C:line in client_p->name
214 */
215
216 strlcpy(client_p->name, name, sizeof(client_p->name));
217 set_server_gecos(client_p, info);
218 client_p->hopcount = hop;
219 server_estab(client_p);
220
221 return 0;
222}
223
224/*
225 * ms_server - SERVER message handler
226 * parv[0] = sender prefix
227 * parv[1] = servername
228 * parv[2] = serverinfo/hopcount
229 * parv[3] = serverinfo
230 */
231static int
232ms_server(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
233{
234 char info[REALLEN + 1];
235 /* same size as in s_misc.c */
236 const char *name;
237 struct Client *target_p;
238 struct remote_conf *hub_p;
239 hook_data_client hdata;
240 int hop;
241 int hlined = 0;
242 int llined = 0;
243 dlink_node *ptr;
244
245 name = parv[1];
246 hop = atoi(parv[2]);
247 strlcpy(info, parv[3], sizeof(info));
248
249 if((target_p = server_exists(name)))
250 {
251 /*
252 * This link is trying feed me a server that I already have
253 * access through another path -- multiple paths not accepted
254 * currently, kill this link immediately!!
255 *
256 * Rather than KILL the link which introduced it, KILL the
257 * youngest of the two links. -avalon
258 *
259 * I think that we should exit the link itself, not the introducer,
260 * and we should always exit the most recently received(i.e. the
261 * one we are receiving this SERVER for. -A1kmm
262 *
263 * You *cant* do this, if you link somewhere, it bursts you a server
264 * that already exists, then sends you a client burst, you squit the
265 * server, but you keep getting the burst of clients on a server that
266 * doesnt exist, although ircd can handle it, its not a realistic
267 * solution.. --fl_
268 */
269 /* It is behind a host-masked server. Completely ignore the
270 * server message(don't propagate or we will delink from whoever
271 * we propagate to). -A1kmm */
272 if(irccmp(target_p->name, name) && target_p->from == client_p)
273 return 0;
274
275 sendto_one(client_p, "ERROR :Server %s already exists", name);
276
277 sendto_realops_snomask(SNO_GENERAL, L_ALL,
278 "Link %s cancelled, server %s already exists",
279 get_server_name(client_p, SHOW_IP), name);
280 ilog(L_SERVER, "Link %s cancelled, server %s already exists",
281 client_p->name, name);
282
283 exit_client(client_p, client_p, &me, "Server Exists");
284 return 0;
285 }
286
287 /*
288 * User nicks never have '.' in them and server names
289 * must always have '.' in them.
290 */
291 if(strchr(name, '.') == NULL)
292 {
293 /*
294 * Server trying to use the same name as a person. Would
295 * cause a fair bit of confusion. Enough to make it hellish
296 * for a while and servers to send stuff to the wrong place.
297 */
298 sendto_one(client_p, "ERROR :Nickname %s already exists!", name);
299 sendto_realops_snomask(SNO_GENERAL, L_ALL,
300 "Link %s cancelled: Server/nick collision on %s",
301 get_server_name(client_p, HIDE_IP), name);
302 ilog(L_SERVER, "Link %s cancelled: Server/nick collision on %s",
303 client_p->name, name);
304
305 exit_client(client_p, client_p, client_p, "Nick as Server");
306 return 0;
307 }
308
309 /*
310 * Server is informing about a new server behind
311 * this link. Create REMOTE server structure,
312 * add it to list and propagate word to my other
313 * server links...
314 */
315 if(parc == 1 || EmptyString(info))
316 {
317 sendto_one(client_p, "ERROR :No server info specified for %s", name);
318 return 0;
319 }
320
321 /*
322 * See if the newly found server is behind a guaranteed
323 * leaf. If so, close the link.
324 *
325 */
326 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
327 {
328 hub_p = ptr->data;
329
330 if(match(hub_p->server, client_p->name) && match(hub_p->host, name))
331 {
332 if(hub_p->flags & CONF_HUB)
333 hlined++;
334 else
335 llined++;
336 }
337 }
338
339 /* Ok, this way this works is
340 *
341 * A server can have a CONF_HUB allowing it to introduce servers
342 * behind it.
343 *
344 * connect {
345 * name = "irc.bighub.net";
346 * hub_mask="*";
347 * ...
348 *
349 * That would allow "irc.bighub.net" to introduce anything it wanted..
350 *
351 * However
352 *
353 * connect {
354 * name = "irc.somehub.fi";
355 * hub_mask="*";
356 * leaf_mask="*.edu";
357 *...
358 * Would allow this server in finland to hub anything but
359 * .edu's
360 */
361
362 /* Ok, check client_p can hub the new server, and make sure it's not a LL */
363 if(!hlined)
364 {
365 /* OOOPs nope can't HUB */
366 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Non-Hub link %s introduced %s.",
367 get_server_name(client_p, HIDE_IP), name);
368 ilog(L_SERVER, "Non-Hub link %s introduced %s.",
369 client_p->name, name);
370
371 exit_client(NULL, client_p, &me, "No matching hub_mask.");
372 return 0;
373 }
374
375 /* Check for the new server being leafed behind this HUB */
376 if(llined)
377 {
378 /* OOOPs nope can't HUB this leaf */
379 sendto_realops_snomask(SNO_GENERAL, L_ALL,
380 "Link %s introduced leafed server %s.",
381 get_server_name(client_p, HIDE_IP), name);
382 ilog(L_SERVER, "Link %s introduced leafed server %s.",
383 client_p->name, name);
384
385 exit_client(NULL, client_p, &me, "Leafed Server.");
386 return 0;
387 }
388
389
390
391 if(strlen(name) > HOSTLEN)
392 {
393 sendto_realops_snomask(SNO_GENERAL, L_ALL,
394 "Link %s introduced server with invalid servername %s",
395 get_server_name(client_p, HIDE_IP), name);
396 ilog(L_SERVER, "Link %s introduced server with invalid servername %s",
397 client_p->name, name);
398
399 exit_client(NULL, client_p, &me, "Invalid servername introduced.");
400 return 0;
401 }
402
403 target_p = make_client(client_p);
404 make_server(target_p);
405 target_p->hopcount = hop;
406
407 strlcpy(target_p->name, name, sizeof(target_p->name));
408
409 set_server_gecos(target_p, info);
410
411 target_p->serv->up = find_or_add(source_p->name);
412
413 if(has_id(source_p))
414 target_p->serv->upid = source_p->id;
415
416 target_p->servptr = source_p;
417
418 SetServer(target_p);
419
420 dlinkAddTail(target_p, &target_p->node, &global_client_list);
421 dlinkAddTailAlloc(target_p, &global_serv_list);
422 add_to_client_hash(target_p->name, target_p);
423 dlinkAdd(target_p, &target_p->lnode, &target_p->servptr->serv->servers);
424
425 sendto_server(client_p, NULL, NOCAPS, NOCAPS,
426 ":%s SERVER %s %d :%s%s",
427 source_p->name, target_p->name, target_p->hopcount + 1,
428 IsHidden(target_p) ? "(H) " : "", target_p->info);
429
430 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
431 "Server %s being introduced by %s", target_p->name, source_p->name);
432
433 /* quick, dirty EOB. you know you love it. */
434 sendto_one(target_p, ":%s PING %s %s", get_id(&me, target_p), me.name, target_p->name);
435
436 hdata.client = source_p;
437 hdata.target = target_p;
438 call_hook(h_server_introduced, &hdata);
439
440 return 0;
441}
442
443static int
444ms_sid(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
445{
446 struct Client *target_p;
447 struct remote_conf *hub_p;
448 hook_data_client hdata;
449 dlink_node *ptr;
450 int hop;
451 int hlined = 0;
452 int llined = 0;
453
454 hop = atoi(parv[2]);
455
456 /* collision on the name? */
457 if((target_p = server_exists(parv[1])) != NULL)
458 {
459 sendto_one(client_p, "ERROR :Server %s already exists", parv[1]);
460 sendto_realops_snomask(SNO_GENERAL, L_ALL,
461 "Link %s cancelled, server %s already exists",
462 get_server_name(client_p, SHOW_IP), parv[1]);
463 ilog(L_SERVER, "Link %s cancelled, server %s already exists",
464 client_p->name, parv[1]);
465
466 exit_client(NULL, client_p, &me, "Server Exists");
467 return 0;
468 }
469
470 /* collision on the SID? */
471 if((target_p = find_id(parv[3])) != NULL)
472 {
473 sendto_one(client_p, "ERROR :SID %s already exists", parv[3]);
474 sendto_realops_snomask(SNO_GENERAL, L_ALL,
475 "Link %s cancelled, SID %s already exists",
476 get_server_name(client_p, SHOW_IP), parv[3]);
477 ilog(L_SERVER, "Link %s cancelled, SID %s already exists",
478 client_p->name, parv[3]);
479
480 exit_client(NULL, client_p, &me, "Server Exists");
481 return 0;
482 }
483
484 if(bogus_host(parv[1]) || strlen(parv[1]) > HOSTLEN)
485 {
486 sendto_one(client_p, "ERROR :Invalid servername");
487 sendto_realops_snomask(SNO_GENERAL, L_ALL,
488 "Link %s cancelled, servername %s invalid",
489 get_server_name(client_p, SHOW_IP), parv[1]);
490 ilog(L_SERVER, "Link %s cancelled, servername %s invalid",
491 client_p->name, parv[1]);
492
493 exit_client(NULL, client_p, &me, "Bogus server name");
494 return 0;
495 }
496
497 if(!IsDigit(parv[3][0]) || !IsIdChar(parv[3][1]) ||
498 !IsIdChar(parv[3][2]) || parv[3][3] != '\0')
499 {
500 sendto_one(client_p, "ERROR :Invalid SID");
501 sendto_realops_snomask(SNO_GENERAL, L_ALL,
502 "Link %s cancelled, SID %s invalid",
503 get_server_name(client_p, SHOW_IP), parv[3]);
504 ilog(L_SERVER, "Link %s cancelled, SID %s invalid",
505 client_p->name, parv[3]);
506
507 exit_client(NULL, client_p, &me, "Bogus SID");
508 return 0;
509 }
510
511 /* for the directly connected server:
512 * H: allows it to introduce a server matching that mask
513 * L: disallows it introducing a server matching that mask
514 */
515 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
516 {
517 hub_p = ptr->data;
518
519 if(match(hub_p->server, client_p->name) && match(hub_p->host, parv[1]))
520 {
521 if(hub_p->flags & CONF_HUB)
522 hlined++;
523 else
524 llined++;
525 }
526 }
527
528 /* no matching hub_mask */
529 if(!hlined)
530 {
531 sendto_one(client_p, "ERROR :No matching hub_mask");
532 sendto_realops_snomask(SNO_GENERAL, L_ALL,
533 "Non-Hub link %s introduced %s.",
534 get_server_name(client_p, SHOW_IP), parv[1]);
535 ilog(L_SERVER, "Non-Hub link %s introduced %s.",
536 client_p->name, parv[1]);
537 exit_client(NULL, client_p, &me, "No matching hub_mask.");
538 return 0;
539 }
540
541 /* matching leaf_mask */
542 if(llined)
543 {
544 sendto_one(client_p, "ERROR :Matching leaf_mask");
545 sendto_realops_snomask(SNO_GENERAL, L_ALL,
546 "Link %s introduced leafed server %s.",
547 get_server_name(client_p, SHOW_IP), parv[1]);
548 ilog(L_SERVER, "Link %s introduced leafed server %s.",
549 client_p->name, parv[1]);
550 exit_client(NULL, client_p, &me, "Leafed Server.");
551 return 0;
552 }
553
554 /* ok, alls good */
555 target_p = make_client(client_p);
556 make_server(target_p);
557
558 strlcpy(target_p->name, parv[1], sizeof(target_p->name));
559 target_p->hopcount = atoi(parv[2]);
560 strcpy(target_p->id, parv[3]);
561 set_server_gecos(target_p, parv[4]);
562
563 target_p->serv->up = find_or_add(source_p->name);
564
565 if(has_id(source_p))
566 target_p->serv->upid = source_p->id;
567
568 target_p->servptr = source_p;
569 SetServer(target_p);
570
571 dlinkAddTail(target_p, &target_p->node, &global_client_list);
572 dlinkAddTailAlloc(target_p, &global_serv_list);
573 add_to_client_hash(target_p->name, target_p);
574 add_to_id_hash(target_p->id, target_p);
575 dlinkAdd(target_p, &target_p->lnode, &target_p->servptr->serv->servers);
576
577 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
578 ":%s SID %s %d %s :%s%s",
579 source_p->id, target_p->name, target_p->hopcount + 1,
580 target_p->id, IsHidden(target_p) ? "(H) " : "", target_p->info);
581 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
582 ":%s SERVER %s %d :%s%s",
583 source_p->name, target_p->name, target_p->hopcount + 1,
584 IsHidden(target_p) ? "(H) " : "", target_p->info);
585
586 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
587 "Server %s being introduced by %s", target_p->name, source_p->name);
588
589 /* quick, dirty EOB. you know you love it. */
590 sendto_one(target_p, ":%s PING %s %s",
591 get_id(&me, target_p), me.name, get_id(target_p, target_p));
592
593 hdata.client = source_p;
594 hdata.target = target_p;
595 call_hook(h_server_introduced, &hdata);
596
597 return 0;
598}
599
600/* set_server_gecos()
601 *
602 * input - pointer to client
603 * output - none
604 * side effects - servers gecos field is set
605 */
606static int
607set_server_gecos(struct Client *client_p, const char *info)
608{
609 /* check the info for [IP] */
610 if(info[0])
611 {
612 char *p;
613 char *s;
614 char *t;
615
616 s = LOCAL_COPY(info);
617
618 /* we should only check the first word for an ip */
619 if((p = strchr(s, ' ')))
620 *p = '\0';
621
622 /* check for a ] which would symbolise an [IP] */
623 if((t = strchr(s, ']')))
624 {
625 /* set s to after the first space */
626 if(p)
627 s = ++p;
628 else
629 s = NULL;
630 }
631 /* no ], put the space back */
632 else if(p)
633 *p = ' ';
634
635 /* p may have been set to a trailing space, so check s exists and that
636 * it isnt \0 */
637 if(s && (*s != '\0'))
638 {
639 /* a space? if not (H) could be the last part of info.. */
640 if((p = strchr(s, ' ')))
641 *p = '\0';
642
643 /* check for (H) which is a hidden server */
644 if(!strcmp(s, "(H)"))
645 {
646 SetHidden(client_p);
647
648 /* if there was no space.. theres nothing to set info to */
649 if(p)
650 s = ++p;
651 else
652 s = NULL;
653 }
654 else if(p)
655 *p = ' ';
656
657 /* if there was a trailing space, s could point to \0, so check */
658 if(s && (*s != '\0'))
659 {
660 strlcpy(client_p->info, s, sizeof(client_p->info));
661 return 1;
662 }
663 }
664 }
665
666 strlcpy(client_p->info, "(Unknown Location)", sizeof(client_p->info));
667
668 return 1;
669}
670
671/*
672 * bogus_host
673 *
674 * inputs - hostname
675 * output - 1 if a bogus hostname input, 0 if its valid
676 * side effects - none
677 */
678int
679bogus_host(const char *host)
680{
681 int bogus_server = 0;
682 const char *s;
683 int dots = 0;
684
685 for(s = host; *s; s++)
686 {
687 if(!IsServChar(*s))
688 {
689 bogus_server = 1;
690 break;
691 }
692 if('.' == *s)
693 ++dots;
694 }
695
696 if(!dots || bogus_server)
697 return 1;
698
699 return 0;
700}
701
702/*
703 * server_exists()
704 *
705 * inputs - servername
706 * output - 1 if server exists, 0 if doesnt exist
707 */
708struct Client *
709server_exists(const char *servername)
710{
711 struct Client *target_p;
712 dlink_node *ptr;
713
714 DLINK_FOREACH(ptr, global_serv_list.head)
715 {
716 target_p = ptr->data;
717
718 if(match(target_p->name, servername) || match(servername, target_p->name))
719 return target_p;
720 }
721
722 return NULL;
723}