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
26 #include "client.h" /* client struct */
27 #include "common.h" /* TRUE bleah */
28 #include "hash.h" /* add_to_client_hash */
30 #include "ircd.h" /* me */
31 #include "numeric.h" /* ERR_xxx */
32 #include "s_conf.h" /* struct ConfItem */
33 #include "s_newconf.h"
34 #include "logger.h" /* log level defines */
35 #include "s_serv.h" /* server_estab, check_server */
36 #include "s_stats.h" /* ServerStats */
38 #include "send.h" /* sendto_one */
43 static int mr_server(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
44 static int ms_server(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
45 static int ms_sid(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
47 struct Message server_msgtab
= {
49 {{mr_server
, 4}, mg_reg
, mg_ignore
, {ms_server
, 4}, mg_ignore
, mg_reg
}
51 struct Message sid_msgtab
= {
53 {mg_ignore
, mg_reg
, mg_ignore
, {ms_sid
, 5}, mg_ignore
, mg_reg
}
56 mapi_clist_av1 server_clist
[] = { &server_msgtab
, &sid_msgtab
, NULL
};
58 DECLARE_MODULE_AV1(server
, NULL
, NULL
, server_clist
, NULL
, NULL
, "$Revision: 3291 $");
60 int bogus_host(const char *host
);
61 static int set_server_gecos(struct Client
*, const char *);
64 * mr_server - SERVER message handler
65 * parv[1] = servername
66 * parv[2] = serverinfo/hopcount
67 * parv[3] = serverinfo
70 mr_server(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
72 char info
[REALLEN
+ 1];
74 struct Client
*target_p
;
76 unsigned int required_mask
;
81 rb_strlcpy(info
, parv
[3], sizeof(info
));
83 if (IsHandshake(client_p
) && irccmp(client_p
->name
, name
))
85 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
86 "Server %s has unexpected name %s",
87 client_p
->name
, name
);
88 ilog(L_SERVER
, "Server %s has unexpected name %s",
89 log_client_name(client_p
, SHOW_IP
), name
);
90 exit_client(client_p
, client_p
, client_p
, "Server name mismatch");
95 * Reject a direct nonTS server connection if we're TS_ONLY -orabidoo
99 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Link %s dropped, non-TS server",
101 exit_client(client_p
, client_p
, client_p
, "Non-TS server");
107 exit_client(client_p
, client_p
, client_p
, "Bogus server name");
111 /* Now we just have to call check_server and everything should be
112 * check for us... -A1kmm. */
113 switch (check_server(name
, client_p
))
116 if(ConfigFileEntry
.warn_no_nline
)
118 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
119 "Unauthorised server connection attempt from %s: "
120 "No entry for servername %s",
121 "[@255.255.255.255]", name
);
123 ilog(L_SERVER
, "Access denied, no connect block for server %s%s",
124 EmptyString(client_p
->name
) ? name
: "",
125 log_client_name(client_p
, SHOW_IP
));
128 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
134 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
135 "Unauthorised server connection attempt from %s: "
136 "Bad credentials for server %s",
137 "[@255.255.255.255]", name
);
139 ilog(L_SERVER
, "Access denied, invalid credentials for server %s%s",
140 EmptyString(client_p
->name
) ? name
: "",
141 log_client_name(client_p
, SHOW_IP
));
143 exit_client(client_p
, client_p
, client_p
, "Invalid credentials.");
149 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
150 "Unauthorised server connection attempt from %s: "
151 "Invalid host for server %s",
152 "[@255.255.255.255]", name
);
154 ilog(L_SERVER
, "Access denied, invalid host for server %s%s",
155 EmptyString(client_p
->name
) ? name
: "",
156 log_client_name(client_p
, SHOW_IP
));
158 exit_client(client_p
, client_p
, client_p
, "Invalid host.");
163 /* servername is > HOSTLEN */
165 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
166 "Invalid servername %s from %s",
167 name
, "[@255.255.255.255]");
168 ilog(L_SERVER
, "Access denied, invalid servername from %s",
169 log_client_name(client_p
, SHOW_IP
));
171 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
176 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
177 "Connection from servername %s requires SSL/TLS but is plaintext",
179 ilog(L_SERVER
, "Access denied, requires SSL/TLS but is plaintext from %s",
180 log_client_name(client_p
, SHOW_IP
));
182 exit_client(client_p
, client_p
, client_p
, "Access denied, requires SSL/TLS but is plaintext");
186 /* require TS6 for direct links */
187 if(!IsCapable(client_p
, CAP_TS6
))
189 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
190 "Link %s dropped, TS6 protocol is required", name
);
191 exit_client(client_p
, client_p
, client_p
, "Incompatible TS version");
195 /* check to ensure any "required" caps are set. --nenolod */
196 required_mask
= capability_index_get_required(serv_capindex
);
197 if (!IsCapable(client_p
, required_mask
))
199 missing
= capability_index_list(serv_capindex
, required_mask
&
200 ~client_p
->localClient
->caps
);
201 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
202 "Link %s dropped, required CAPABs [%s] are missing",
204 ilog(L_SERVER
, "Link %s%s dropped, required CAPABs [%s] are missing",
205 EmptyString(client_p
->name
) ? name
: "",
206 log_client_name(client_p
, SHOW_IP
), missing
);
207 /* Do not use '[' in the below message because it would cause
208 * it to be considered potentially unsafe (might disclose IP
211 sendto_one(client_p
, "ERROR :Missing required CAPABs (%s)", missing
);
212 exit_client(client_p
, client_p
, client_p
, "Missing required CAPABs");
217 if((target_p
= find_server(NULL
, name
)))
220 * This link is trying feed me a server that I already have
221 * access through another path -- multiple paths not accepted
222 * currently, kill this link immediately!!
224 * Rather than KILL the link which introduced it, KILL the
225 * youngest of the two links. -avalon
227 * Definitely don't do that here. This is from an unregistered
230 if (target_p
->servptr
->flags
& FLAGS_SERVICE
)
232 /* Assume any servers introduced by services
236 sendto_one(client_p
, "ERROR :Server juped.");
240 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
241 "Attempt to re-introduce server %s from %s",
242 name
, "[@255.255.255.255]");
243 ilog(L_SERVER
, "Attempt to re-introduce server %s from %s",
244 name
, log_client_name(client_p
, SHOW_IP
));
246 sendto_one(client_p
, "ERROR :Server already exists.");
248 exit_client(client_p
, client_p
, client_p
, "Server Exists");
252 if(has_id(client_p
) && (target_p
= find_id(client_p
->id
)) != NULL
)
254 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
255 "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
257 EmptyString(client_p
->name
) ? name
: "",
258 client_p
->name
, target_p
->name
);
259 ilog(L_SERVER
, "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
261 EmptyString(client_p
->name
) ? name
: "",
262 log_client_name(client_p
, SHOW_IP
),
265 sendto_one(client_p
, "ERROR :SID already exists.");
266 exit_client(client_p
, client_p
, client_p
, "SID Exists");
271 * if we are connecting (Handshake), we already have the name from the
272 * C:line in client_p->name
275 rb_strlcpy(client_p
->name
, name
, sizeof(client_p
->name
));
276 set_server_gecos(client_p
, info
);
277 client_p
->hopcount
= hop
;
278 server_estab(client_p
);
284 * ms_server - SERVER message handler
285 * parv[1] = servername
286 * parv[2] = serverinfo/hopcount
287 * parv[3] = serverinfo
290 ms_server(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
292 char info
[REALLEN
+ 1];
293 /* same size as in s_misc.c */
295 struct Client
*target_p
;
296 struct remote_conf
*hub_p
;
297 hook_data_client hdata
;
302 char squitreason
[160];
306 rb_strlcpy(info
, parv
[3], sizeof(info
));
308 if(find_server(NULL
, name
))
311 * This link is trying feed me a server that I already have
312 * access through another path -- multiple paths not accepted
313 * currently, kill this link immediately!!
315 * Rather than KILL the link which introduced it, KILL the
316 * youngest of the two links. -avalon
318 * I think that we should exit the link itself, not the introducer,
319 * and we should always exit the most recently received(i.e. the
320 * one we are receiving this SERVER for. -A1kmm
322 * You *cant* do this, if you link somewhere, it bursts you a server
323 * that already exists, then sends you a client burst, you squit the
324 * server, but you keep getting the burst of clients on a server that
325 * doesnt exist, although ircd can handle it, its not a realistic
328 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
329 client_p
->name
, name
);
331 snprintf(squitreason
, sizeof squitreason
,
332 "Server %s already exists",
334 exit_client(client_p
, client_p
, &me
, squitreason
);
339 * User nicks never have '.' in them and server names
340 * must always have '.' in them.
342 if(strchr(name
, '.') == NULL
)
345 * Server trying to use the same name as a person. Would
346 * cause a fair bit of confusion. Enough to make it hellish
347 * for a while and servers to send stuff to the wrong place.
349 sendto_one(client_p
, "ERROR :Nickname %s already exists!", name
);
350 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
351 "Link %s cancelled: Server/nick collision on %s",
352 client_p
->name
, name
);
353 ilog(L_SERVER
, "Link %s cancelled: Server/nick collision on %s",
354 client_p
->name
, name
);
356 exit_client(client_p
, client_p
, client_p
, "Nick as Server");
361 * Server is informing about a new server behind
362 * this link. Create REMOTE server structure,
363 * add it to list and propagate word to my other
368 * See if the newly found server is behind a guaranteed
369 * leaf. If so, close the link.
372 RB_DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
376 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, name
))
378 if(hub_p
->flags
& CONF_HUB
)
385 /* Ok, this way this works is
387 * A server can have a CONF_HUB allowing it to introduce servers
391 * name = "irc.bighub.net";
395 * That would allow "irc.bighub.net" to introduce anything it wanted..
400 * name = "irc.somehub.fi";
404 * Would allow this server in finland to hub anything but
408 /* Ok, check client_p can hub the new server */
411 /* OOOPs nope can't HUB */
412 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Non-Hub link %s introduced %s.",
413 client_p
->name
, name
);
414 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
415 client_p
->name
, name
);
417 snprintf(squitreason
, sizeof squitreason
,
418 "No matching hub_mask for %s",
420 exit_client(NULL
, client_p
, &me
, squitreason
);
424 /* Check for the new server being leafed behind this HUB */
427 /* OOOPs nope can't HUB this leaf */
428 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
429 "Link %s introduced leafed server %s.",
430 client_p
->name
, name
);
431 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
432 client_p
->name
, name
);
434 snprintf(squitreason
, sizeof squitreason
,
435 "Matching leaf_mask for %s",
437 exit_client(NULL
, client_p
, &me
, squitreason
);
443 if(strlen(name
) > HOSTLEN
)
445 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
446 "Link %s introduced server with invalid servername %s",
447 client_p
->name
, name
);
448 ilog(L_SERVER
, "Link %s introduced server with invalid servername %s",
449 client_p
->name
, name
);
451 exit_client(NULL
, client_p
, &me
, "Invalid servername introduced.");
455 target_p
= make_client(client_p
);
456 make_server(target_p
);
457 target_p
->hopcount
= hop
;
459 rb_strlcpy(target_p
->name
, name
, sizeof(target_p
->name
));
461 set_server_gecos(target_p
, info
);
463 target_p
->servptr
= source_p
;
467 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
468 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
469 add_to_client_hash(target_p
->name
, target_p
);
470 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
472 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
474 sendto_server(client_p
, NULL
, NOCAPS
, NOCAPS
,
475 ":%s SERVER %s %d :%s%s",
476 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
477 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
479 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
480 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
482 /* quick, dirty EOB. you know you love it. */
483 sendto_one(target_p
, ":%s PING %s %s", get_id(&me
, target_p
), me
.name
, target_p
->name
);
485 hdata
.client
= source_p
;
486 hdata
.target
= target_p
;
487 call_hook(h_server_introduced
, &hdata
);
493 ms_sid(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
495 struct Client
*target_p
;
496 struct remote_conf
*hub_p
;
497 hook_data_client hdata
;
501 char squitreason
[160];
503 /* collision on the name? */
504 if(find_server(NULL
, parv
[1]) != NULL
)
506 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
507 client_p
->name
, parv
[1]);
509 snprintf(squitreason
, sizeof squitreason
,
510 "Server %s already exists",
512 exit_client(NULL
, client_p
, &me
, squitreason
);
516 /* collision on the SID? */
517 if((target_p
= find_id(parv
[3])) != NULL
)
519 sendto_wallops_flags(UMODE_WALLOP
, &me
,
520 "Link %s cancelled, SID %s for server %s already in use by %s",
521 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
522 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
523 ":%s WALLOPS :Link %s cancelled, SID %s for server %s already in use by %s",
524 me
.id
, client_p
->name
, parv
[3], parv
[1], target_p
->name
);
525 ilog(L_SERVER
, "Link %s cancelled, SID %s for server %s already in use by %s",
526 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
528 snprintf(squitreason
, sizeof squitreason
,
529 "SID %s for %s already in use by %s",
530 parv
[3], parv
[1], target_p
->name
);
531 exit_client(NULL
, client_p
, &me
, squitreason
);
535 if(bogus_host(parv
[1]) || strlen(parv
[1]) > HOSTLEN
)
537 sendto_one(client_p
, "ERROR :Invalid servername");
538 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
539 "Link %s cancelled, servername %s invalid",
540 client_p
->name
, parv
[1]);
541 ilog(L_SERVER
, "Link %s cancelled, servername %s invalid",
542 client_p
->name
, parv
[1]);
544 exit_client(NULL
, client_p
, &me
, "Bogus server name");
548 if(!IsDigit(parv
[3][0]) || !IsIdChar(parv
[3][1]) ||
549 !IsIdChar(parv
[3][2]) || parv
[3][3] != '\0')
551 sendto_one(client_p
, "ERROR :Invalid SID");
552 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
553 "Link %s cancelled, SID %s invalid",
554 client_p
->name
, parv
[3]);
555 ilog(L_SERVER
, "Link %s cancelled, SID %s invalid",
556 client_p
->name
, parv
[3]);
558 exit_client(NULL
, client_p
, &me
, "Bogus SID");
562 /* for the directly connected server:
563 * H: allows it to introduce a server matching that mask
564 * L: disallows it introducing a server matching that mask
566 RB_DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
570 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, parv
[1]))
572 if(hub_p
->flags
& CONF_HUB
)
579 /* no matching hub_mask */
582 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
583 "Non-Hub link %s introduced %s.",
584 client_p
->name
, parv
[1]);
585 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
586 client_p
->name
, parv
[1]);
588 snprintf(squitreason
, sizeof squitreason
,
589 "No matching hub_mask for %s",
591 exit_client(NULL
, client_p
, &me
, squitreason
);
595 /* matching leaf_mask */
598 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
599 "Link %s introduced leafed server %s.",
600 client_p
->name
, parv
[1]);
601 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
602 client_p
->name
, parv
[1]);
604 snprintf(squitreason
, sizeof squitreason
,
605 "Matching leaf_mask for %s",
607 exit_client(NULL
, client_p
, &me
, squitreason
);
612 target_p
= make_client(client_p
);
613 make_server(target_p
);
615 rb_strlcpy(target_p
->name
, parv
[1], sizeof(target_p
->name
));
616 target_p
->hopcount
= atoi(parv
[2]);
617 strcpy(target_p
->id
, parv
[3]);
618 set_server_gecos(target_p
, parv
[4]);
620 target_p
->servptr
= source_p
;
623 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
624 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
625 add_to_client_hash(target_p
->name
, target_p
);
626 add_to_id_hash(target_p
->id
, target_p
);
627 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
629 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
631 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
632 ":%s SID %s %d %s :%s%s",
633 source_p
->id
, target_p
->name
, target_p
->hopcount
+ 1,
634 target_p
->id
, IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
636 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
637 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
639 /* quick, dirty EOB. you know you love it. */
640 sendto_one(target_p
, ":%s PING %s %s",
641 get_id(&me
, target_p
), me
.name
, get_id(target_p
, target_p
));
643 hdata
.client
= source_p
;
644 hdata
.target
= target_p
;
645 call_hook(h_server_introduced
, &hdata
);
650 /* set_server_gecos()
652 * input - pointer to client
654 * side effects - servers gecos field is set
657 set_server_gecos(struct Client
*client_p
, const char *info
)
659 /* check the info for [IP] */
665 s
= LOCAL_COPY(info
);
667 /* we should only check the first word for an ip */
668 if((p
= strchr(s
, ' ')))
671 /* check for a ] which would symbolise an [IP] */
674 /* set s to after the first space */
680 /* no ], put the space back */
684 /* p may have been set to a trailing space, so check s exists and that
686 if(s
&& (*s
!= '\0'))
688 /* a space? if not (H) could be the last part of info.. */
689 if((p
= strchr(s
, ' ')))
692 /* check for (H) which is a hidden server */
693 if(!strcmp(s
, "(H)"))
697 /* if there was no space.. theres nothing to set info to */
706 /* if there was a trailing space, s could point to \0, so check */
707 if(s
&& (*s
!= '\0'))
709 rb_strlcpy(client_p
->info
, s
, sizeof(client_p
->info
));
715 rb_strlcpy(client_p
->info
, "(Unknown Location)", sizeof(client_p
->info
));
724 * output - 1 if a bogus hostname input, 0 if its valid
725 * side effects - none
728 bogus_host(const char *host
)
730 int bogus_server
= 0;
734 for(s
= host
; *s
; s
++)
745 if(!dots
|| bogus_server
)