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