2 * ircd-ratbox: A slightly useful ircd.
3 * m_server.c: Introduces a server.
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
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.
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.
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
24 * $Id: m_server.c 3291 2007-03-28 14:30:10Z jilles $
28 #include "client.h" /* client struct */
29 #include "common.h" /* TRUE bleah */
30 #include "hash.h" /* add_to_client_hash */
32 #include "ircd.h" /* me */
33 #include "numeric.h" /* ERR_xxx */
34 #include "s_conf.h" /* struct ConfItem */
35 #include "s_newconf.h"
36 #include "logger.h" /* log level defines */
37 #include "s_serv.h" /* server_estab, check_server */
38 #include "s_stats.h" /* ServerStats */
40 #include "send.h" /* sendto_one */
45 static int mr_server(struct Client
*, struct Client
*, int, const char **);
46 static int ms_server(struct Client
*, struct Client
*, int, const char **);
47 static int ms_sid(struct Client
*, struct Client
*, int, const char **);
49 struct Message server_msgtab
= {
50 "SERVER", 0, 0, 0, MFLG_SLOW
| MFLG_UNREG
,
51 {{mr_server
, 4}, mg_reg
, mg_ignore
, {ms_server
, 4}, mg_ignore
, mg_reg
}
53 struct Message sid_msgtab
= {
54 "SID", 0, 0, 0, MFLG_SLOW
,
55 {mg_ignore
, mg_reg
, mg_ignore
, {ms_sid
, 5}, mg_ignore
, mg_reg
}
58 mapi_clist_av1 server_clist
[] = { &server_msgtab
, &sid_msgtab
, NULL
};
60 DECLARE_MODULE_AV1(server
, NULL
, NULL
, server_clist
, NULL
, NULL
, "$Revision: 3291 $");
62 int bogus_host(const char *host
);
63 static int set_server_gecos(struct Client
*, const char *);
66 * mr_server - SERVER message handler
67 * parv[1] = servername
68 * parv[2] = serverinfo/hopcount
69 * parv[3] = serverinfo
72 mr_server(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
74 char info
[REALLEN
+ 1];
76 struct Client
*target_p
;
78 unsigned int required_mask
;
82 rb_strlcpy(info
, parv
[3], sizeof(info
));
84 if (IsHandshake(client_p
) && irccmp(client_p
->name
, name
))
86 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
87 "Server %s has unexpected name %s",
88 client_p
->name
, name
);
89 ilog(L_SERVER
, "Server %s has unexpected name %s",
90 log_client_name(client_p
, SHOW_IP
), name
);
91 exit_client(client_p
, client_p
, client_p
, "Server name mismatch");
96 * Reject a direct nonTS server connection if we're TS_ONLY -orabidoo
100 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Link %s dropped, non-TS server",
102 exit_client(client_p
, client_p
, client_p
, "Non-TS server");
108 exit_client(client_p
, client_p
, client_p
, "Bogus server name");
112 /* check to ensure any "required" caps are set. --nenolod */
113 /* XXX: show required CAPABs. */
114 required_mask
= capability_index_get_required(serv_capindex
);
115 if (required_mask
&& !(client_p
->localClient
->caps
& required_mask
))
117 exit_client(client_p
, client_p
, client_p
, "Missing required CAPABs");
122 /* Now we just have to call check_server and everything should be
123 * check for us... -A1kmm. */
124 switch (check_server(name
, client_p
))
127 if(ConfigFileEntry
.warn_no_nline
)
129 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
130 "Unauthorised server connection attempt from %s: "
131 "No entry for servername %s",
132 "[@255.255.255.255]", name
);
134 ilog(L_SERVER
, "Access denied, no connect block for server %s%s",
135 EmptyString(client_p
->name
) ? name
: "",
136 log_client_name(client_p
, SHOW_IP
));
139 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
145 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
146 "Unauthorised server connection attempt from %s: "
147 "Bad credentials for server %s",
148 "[@255.255.255.255]", name
);
150 ilog(L_SERVER
, "Access denied, invalid credentials for server %s%s",
151 EmptyString(client_p
->name
) ? name
: "",
152 log_client_name(client_p
, SHOW_IP
));
154 exit_client(client_p
, client_p
, client_p
, "Invalid credentials.");
160 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
161 "Unauthorised server connection attempt from %s: "
162 "Invalid host for server %s",
163 "[@255.255.255.255]", name
);
165 ilog(L_SERVER
, "Access denied, invalid host for server %s%s",
166 EmptyString(client_p
->name
) ? name
: "",
167 log_client_name(client_p
, SHOW_IP
));
169 exit_client(client_p
, client_p
, client_p
, "Invalid host.");
174 /* servername is > HOSTLEN */
176 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
177 "Invalid servername %s from %s",
178 name
, "[@255.255.255.255]");
179 ilog(L_SERVER
, "Access denied, invalid servername from %s",
180 log_client_name(client_p
, SHOW_IP
));
182 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
187 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
188 "Connection from servername %s requires SSL/TLS but is plaintext",
190 ilog(L_SERVER
, "Access denied, requires SSL/TLS but is plaintext from %s",
191 log_client_name(client_p
, SHOW_IP
));
193 exit_client(client_p
, client_p
, client_p
, "Access denied, requires SSL/TLS but is plaintext");
197 /* require TS6 for direct links */
198 if(!IsCapable(client_p
, CAP_TS6
))
200 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
201 "Link %s dropped, TS6 protocol is required", name
);
202 exit_client(client_p
, client_p
, client_p
, "Incompatible TS version");
206 if((target_p
= find_server(NULL
, name
)))
209 * This link is trying feed me a server that I already have
210 * access through another path -- multiple paths not accepted
211 * currently, kill this link immediately!!
213 * Rather than KILL the link which introduced it, KILL the
214 * youngest of the two links. -avalon
216 * Definitely don't do that here. This is from an unregistered
219 if (target_p
->servptr
->flags
& FLAGS_SERVICE
)
221 /* Assume any servers introduced by services
225 sendto_one(client_p
, "ERROR :Server juped.");
229 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
230 "Attempt to re-introduce server %s from %s",
231 name
, "[@255.255.255.255]");
232 ilog(L_SERVER
, "Attempt to re-introduce server %s from %s",
233 name
, log_client_name(client_p
, SHOW_IP
));
235 sendto_one(client_p
, "ERROR :Server already exists.");
237 exit_client(client_p
, client_p
, client_p
, "Server Exists");
241 if(has_id(client_p
) && (target_p
= find_id(client_p
->id
)) != NULL
)
243 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
244 "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
246 EmptyString(client_p
->name
) ? name
: "",
247 client_p
->name
, target_p
->name
);
248 ilog(L_SERVER
, "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
250 EmptyString(client_p
->name
) ? name
: "",
251 log_client_name(client_p
, SHOW_IP
),
254 sendto_one(client_p
, "ERROR :SID already exists.");
255 exit_client(client_p
, client_p
, client_p
, "SID Exists");
260 * if we are connecting (Handshake), we already have the name from the
261 * C:line in client_p->name
264 rb_strlcpy(client_p
->name
, name
, sizeof(client_p
->name
));
265 set_server_gecos(client_p
, info
);
266 client_p
->hopcount
= hop
;
267 server_estab(client_p
);
273 * ms_server - SERVER message handler
274 * parv[1] = servername
275 * parv[2] = serverinfo/hopcount
276 * parv[3] = serverinfo
279 ms_server(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
281 char info
[REALLEN
+ 1];
282 /* same size as in s_misc.c */
284 struct Client
*target_p
;
285 struct remote_conf
*hub_p
;
286 hook_data_client hdata
;
291 char squitreason
[160];
295 rb_strlcpy(info
, parv
[3], sizeof(info
));
297 if((target_p
= find_server(NULL
, name
)))
300 * This link is trying feed me a server that I already have
301 * access through another path -- multiple paths not accepted
302 * currently, kill this link immediately!!
304 * Rather than KILL the link which introduced it, KILL the
305 * youngest of the two links. -avalon
307 * I think that we should exit the link itself, not the introducer,
308 * and we should always exit the most recently received(i.e. the
309 * one we are receiving this SERVER for. -A1kmm
311 * You *cant* do this, if you link somewhere, it bursts you a server
312 * that already exists, then sends you a client burst, you squit the
313 * server, but you keep getting the burst of clients on a server that
314 * doesnt exist, although ircd can handle it, its not a realistic
317 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
318 client_p
->name
, name
);
320 rb_snprintf(squitreason
, sizeof squitreason
,
321 "Server %s already exists",
323 exit_client(client_p
, client_p
, &me
, squitreason
);
328 * User nicks never have '.' in them and server names
329 * must always have '.' in them.
331 if(strchr(name
, '.') == NULL
)
334 * Server trying to use the same name as a person. Would
335 * cause a fair bit of confusion. Enough to make it hellish
336 * for a while and servers to send stuff to the wrong place.
338 sendto_one(client_p
, "ERROR :Nickname %s already exists!", name
);
339 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
340 "Link %s cancelled: Server/nick collision on %s",
341 client_p
->name
, name
);
342 ilog(L_SERVER
, "Link %s cancelled: Server/nick collision on %s",
343 client_p
->name
, name
);
345 exit_client(client_p
, client_p
, client_p
, "Nick as Server");
350 * Server is informing about a new server behind
351 * this link. Create REMOTE server structure,
352 * add it to list and propagate word to my other
357 * See if the newly found server is behind a guaranteed
358 * leaf. If so, close the link.
361 RB_DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
365 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, name
))
367 if(hub_p
->flags
& CONF_HUB
)
374 /* Ok, this way this works is
376 * A server can have a CONF_HUB allowing it to introduce servers
380 * name = "irc.bighub.net";
384 * That would allow "irc.bighub.net" to introduce anything it wanted..
389 * name = "irc.somehub.fi";
393 * Would allow this server in finland to hub anything but
397 /* Ok, check client_p can hub the new server */
400 /* OOOPs nope can't HUB */
401 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Non-Hub link %s introduced %s.",
402 client_p
->name
, name
);
403 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
404 client_p
->name
, name
);
406 rb_snprintf(squitreason
, sizeof squitreason
,
407 "No matching hub_mask for %s",
409 exit_client(NULL
, client_p
, &me
, squitreason
);
413 /* Check for the new server being leafed behind this HUB */
416 /* OOOPs nope can't HUB this leaf */
417 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
418 "Link %s introduced leafed server %s.",
419 client_p
->name
, name
);
420 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
421 client_p
->name
, name
);
423 rb_snprintf(squitreason
, sizeof squitreason
,
424 "Matching leaf_mask for %s",
426 exit_client(NULL
, client_p
, &me
, squitreason
);
432 if(strlen(name
) > HOSTLEN
)
434 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
435 "Link %s introduced server with invalid servername %s",
436 client_p
->name
, name
);
437 ilog(L_SERVER
, "Link %s introduced server with invalid servername %s",
438 client_p
->name
, name
);
440 exit_client(NULL
, client_p
, &me
, "Invalid servername introduced.");
444 target_p
= make_client(client_p
);
445 make_server(target_p
);
446 target_p
->hopcount
= hop
;
448 rb_strlcpy(target_p
->name
, name
, sizeof(target_p
->name
));
450 set_server_gecos(target_p
, info
);
452 target_p
->servptr
= source_p
;
456 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
457 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
458 add_to_client_hash(target_p
->name
, target_p
);
459 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
461 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
463 sendto_server(client_p
, NULL
, NOCAPS
, NOCAPS
,
464 ":%s SERVER %s %d :%s%s",
465 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
466 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
468 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
469 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
471 /* quick, dirty EOB. you know you love it. */
472 sendto_one(target_p
, ":%s PING %s %s", get_id(&me
, target_p
), me
.name
, target_p
->name
);
474 hdata
.client
= source_p
;
475 hdata
.target
= target_p
;
476 call_hook(h_server_introduced
, &hdata
);
482 ms_sid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
484 struct Client
*target_p
;
485 struct remote_conf
*hub_p
;
486 hook_data_client hdata
;
491 char squitreason
[160];
495 /* collision on the name? */
496 if((target_p
= find_server(NULL
, parv
[1])) != NULL
)
498 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
499 client_p
->name
, parv
[1]);
501 rb_snprintf(squitreason
, sizeof squitreason
,
502 "Server %s already exists",
504 exit_client(NULL
, client_p
, &me
, squitreason
);
508 /* collision on the SID? */
509 if((target_p
= find_id(parv
[3])) != NULL
)
511 sendto_wallops_flags(UMODE_WALLOP
, &me
,
512 "Link %s cancelled, SID %s for server %s already in use by %s",
513 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
514 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
515 ":%s WALLOPS :Link %s cancelled, SID %s for server %s already in use by %s",
516 me
.id
, client_p
->name
, parv
[3], parv
[1], target_p
->name
);
517 ilog(L_SERVER
, "Link %s cancelled, SID %s for server %s already in use by %s",
518 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
520 rb_snprintf(squitreason
, sizeof squitreason
,
521 "SID %s for %s already in use by %s",
522 parv
[3], parv
[1], target_p
->name
);
523 exit_client(NULL
, client_p
, &me
, squitreason
);
527 if(bogus_host(parv
[1]) || strlen(parv
[1]) > HOSTLEN
)
529 sendto_one(client_p
, "ERROR :Invalid servername");
530 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
531 "Link %s cancelled, servername %s invalid",
532 client_p
->name
, parv
[1]);
533 ilog(L_SERVER
, "Link %s cancelled, servername %s invalid",
534 client_p
->name
, parv
[1]);
536 exit_client(NULL
, client_p
, &me
, "Bogus server name");
540 if(!IsDigit(parv
[3][0]) || !IsIdChar(parv
[3][1]) ||
541 !IsIdChar(parv
[3][2]) || parv
[3][3] != '\0')
543 sendto_one(client_p
, "ERROR :Invalid SID");
544 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
545 "Link %s cancelled, SID %s invalid",
546 client_p
->name
, parv
[3]);
547 ilog(L_SERVER
, "Link %s cancelled, SID %s invalid",
548 client_p
->name
, parv
[3]);
550 exit_client(NULL
, client_p
, &me
, "Bogus SID");
554 /* for the directly connected server:
555 * H: allows it to introduce a server matching that mask
556 * L: disallows it introducing a server matching that mask
558 RB_DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
562 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, parv
[1]))
564 if(hub_p
->flags
& CONF_HUB
)
571 /* no matching hub_mask */
574 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
575 "Non-Hub link %s introduced %s.",
576 client_p
->name
, parv
[1]);
577 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
578 client_p
->name
, parv
[1]);
580 rb_snprintf(squitreason
, sizeof squitreason
,
581 "No matching hub_mask for %s",
583 exit_client(NULL
, client_p
, &me
, squitreason
);
587 /* matching leaf_mask */
590 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
591 "Link %s introduced leafed server %s.",
592 client_p
->name
, parv
[1]);
593 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
594 client_p
->name
, parv
[1]);
596 rb_snprintf(squitreason
, sizeof squitreason
,
597 "Matching leaf_mask for %s",
599 exit_client(NULL
, client_p
, &me
, squitreason
);
604 target_p
= make_client(client_p
);
605 make_server(target_p
);
607 rb_strlcpy(target_p
->name
, parv
[1], sizeof(target_p
->name
));
608 target_p
->hopcount
= atoi(parv
[2]);
609 strcpy(target_p
->id
, parv
[3]);
610 set_server_gecos(target_p
, parv
[4]);
612 target_p
->servptr
= source_p
;
615 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
616 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
617 add_to_client_hash(target_p
->name
, target_p
);
618 add_to_id_hash(target_p
->id
, target_p
);
619 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
621 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
623 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
624 ":%s SID %s %d %s :%s%s",
625 source_p
->id
, target_p
->name
, target_p
->hopcount
+ 1,
626 target_p
->id
, IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
628 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
629 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
631 /* quick, dirty EOB. you know you love it. */
632 sendto_one(target_p
, ":%s PING %s %s",
633 get_id(&me
, target_p
), me
.name
, get_id(target_p
, target_p
));
635 hdata
.client
= source_p
;
636 hdata
.target
= target_p
;
637 call_hook(h_server_introduced
, &hdata
);
642 /* set_server_gecos()
644 * input - pointer to client
646 * side effects - servers gecos field is set
649 set_server_gecos(struct Client
*client_p
, const char *info
)
651 /* check the info for [IP] */
657 s
= LOCAL_COPY(info
);
659 /* we should only check the first word for an ip */
660 if((p
= strchr(s
, ' ')))
663 /* check for a ] which would symbolise an [IP] */
666 /* set s to after the first space */
672 /* no ], put the space back */
676 /* p may have been set to a trailing space, so check s exists and that
678 if(s
&& (*s
!= '\0'))
680 /* a space? if not (H) could be the last part of info.. */
681 if((p
= strchr(s
, ' ')))
684 /* check for (H) which is a hidden server */
685 if(!strcmp(s
, "(H)"))
689 /* if there was no space.. theres nothing to set info to */
698 /* if there was a trailing space, s could point to \0, so check */
699 if(s
&& (*s
!= '\0'))
701 rb_strlcpy(client_p
->info
, s
, sizeof(client_p
->info
));
707 rb_strlcpy(client_p
->info
, "(Unknown Location)", sizeof(client_p
->info
));
716 * output - 1 if a bogus hostname input, 0 if its valid
717 * side effects - none
720 bogus_host(const char *host
)
722 int bogus_server
= 0;
726 for(s
= host
; *s
; s
++)
737 if(!dots
|| bogus_server
)