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[0] = sender prefix
68 * parv[1] = servername
69 * parv[2] = serverinfo/hopcount
70 * parv[3] = serverinfo
73 mr_server(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
75 char info
[REALLEN
+ 1];
77 struct Client
*target_p
;
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 /* Now we just have to call check_server and everything should be
113 * check for us... -A1kmm. */
114 switch (check_server(name
, client_p
))
117 if(ConfigFileEntry
.warn_no_nline
)
119 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
120 "Unauthorised server connection attempt from %s: "
121 "No entry for servername %s",
122 "[@255.255.255.255]", name
);
124 ilog(L_SERVER
, "Access denied, no connect block for server %s%s",
125 EmptyString(client_p
->name
) ? name
: "",
126 log_client_name(client_p
, SHOW_IP
));
129 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
135 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
136 "Unauthorised server connection attempt from %s: "
137 "Bad password for server %s",
138 "[@255.255.255.255]", name
);
140 ilog(L_SERVER
, "Access denied, invalid password for server %s%s",
141 EmptyString(client_p
->name
) ? name
: "",
142 log_client_name(client_p
, SHOW_IP
));
144 exit_client(client_p
, client_p
, client_p
, "Invalid password.");
150 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
151 "Unauthorised server connection attempt from %s: "
152 "Invalid host for server %s",
153 "[@255.255.255.255]", name
);
155 ilog(L_SERVER
, "Access denied, invalid host for server %s%s",
156 EmptyString(client_p
->name
) ? name
: "",
157 log_client_name(client_p
, SHOW_IP
));
159 exit_client(client_p
, client_p
, client_p
, "Invalid host.");
164 /* servername is > HOSTLEN */
166 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
167 "Invalid servername %s from %s",
168 name
, "[@255.255.255.255]");
169 ilog(L_SERVER
, "Access denied, invalid servername from %s",
170 log_client_name(client_p
, SHOW_IP
));
172 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
177 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
178 "Connection from servername %s requires SSL/TLS but is plaintext",
180 ilog(L_SERVER
, "Access denied, requires SSL/TLS but is plaintext from %s",
181 log_client_name(client_p
, SHOW_IP
));
183 exit_client(client_p
, client_p
, client_p
, "Access denied, requires SSL/TLS but is plaintext");
187 /* require TS6 for direct links */
188 if(!IsCapable(client_p
, CAP_TS6
))
190 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
191 "Link %s dropped, TS6 protocol is required", name
);
192 exit_client(client_p
, client_p
, client_p
, "Incompatible TS version");
196 if((target_p
= find_server(NULL
, name
)))
199 * This link is trying feed me a server that I already have
200 * access through another path -- multiple paths not accepted
201 * currently, kill this link immediately!!
203 * Rather than KILL the link which introduced it, KILL the
204 * youngest of the two links. -avalon
206 * Definitely don't do that here. This is from an unregistered
209 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
210 "Attempt to re-introduce server %s from %s",
211 name
, "[@255.255.255.255]");
212 ilog(L_SERVER
, "Attempt to re-introduce server %s from %s",
213 name
, log_client_name(client_p
, SHOW_IP
));
215 sendto_one(client_p
, "ERROR :Server already exists.");
216 exit_client(client_p
, client_p
, client_p
, "Server Exists");
220 if(has_id(client_p
) && (target_p
= find_id(client_p
->id
)) != NULL
)
222 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
223 "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
225 EmptyString(client_p
->name
) ? name
: "",
226 client_p
->name
, target_p
->name
);
227 ilog(L_SERVER
, "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
229 EmptyString(client_p
->name
) ? name
: "",
230 log_client_name(client_p
, SHOW_IP
),
233 sendto_one(client_p
, "ERROR :SID already exists.");
234 exit_client(client_p
, client_p
, client_p
, "SID Exists");
239 * if we are connecting (Handshake), we already have the name from the
240 * C:line in client_p->name
243 rb_strlcpy(client_p
->name
, name
, sizeof(client_p
->name
));
244 set_server_gecos(client_p
, info
);
245 client_p
->hopcount
= hop
;
246 server_estab(client_p
);
252 * ms_server - SERVER message handler
253 * parv[0] = sender prefix
254 * parv[1] = servername
255 * parv[2] = serverinfo/hopcount
256 * parv[3] = serverinfo
259 ms_server(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
261 char info
[REALLEN
+ 1];
262 /* same size as in s_misc.c */
264 struct Client
*target_p
;
265 struct remote_conf
*hub_p
;
266 hook_data_client hdata
;
271 char squitreason
[160];
275 rb_strlcpy(info
, parv
[3], sizeof(info
));
277 if((target_p
= find_server(NULL
, name
)))
280 * This link is trying feed me a server that I already have
281 * access through another path -- multiple paths not accepted
282 * currently, kill this link immediately!!
284 * Rather than KILL the link which introduced it, KILL the
285 * youngest of the two links. -avalon
287 * I think that we should exit the link itself, not the introducer,
288 * and we should always exit the most recently received(i.e. the
289 * one we are receiving this SERVER for. -A1kmm
291 * You *cant* do this, if you link somewhere, it bursts you a server
292 * that already exists, then sends you a client burst, you squit the
293 * server, but you keep getting the burst of clients on a server that
294 * doesnt exist, although ircd can handle it, its not a realistic
297 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
298 client_p
->name
, name
);
300 rb_snprintf(squitreason
, sizeof squitreason
,
301 "Server %s already exists",
303 exit_client(client_p
, client_p
, &me
, squitreason
);
308 * User nicks never have '.' in them and server names
309 * must always have '.' in them.
311 if(strchr(name
, '.') == NULL
)
314 * Server trying to use the same name as a person. Would
315 * cause a fair bit of confusion. Enough to make it hellish
316 * for a while and servers to send stuff to the wrong place.
318 sendto_one(client_p
, "ERROR :Nickname %s already exists!", name
);
319 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
320 "Link %s cancelled: Server/nick collision on %s",
321 client_p
->name
, name
);
322 ilog(L_SERVER
, "Link %s cancelled: Server/nick collision on %s",
323 client_p
->name
, name
);
325 exit_client(client_p
, client_p
, client_p
, "Nick as Server");
330 * Server is informing about a new server behind
331 * this link. Create REMOTE server structure,
332 * add it to list and propagate word to my other
337 * See if the newly found server is behind a guaranteed
338 * leaf. If so, close the link.
341 RB_DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
345 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, name
))
347 if(hub_p
->flags
& CONF_HUB
)
354 /* Ok, this way this works is
356 * A server can have a CONF_HUB allowing it to introduce servers
360 * name = "irc.bighub.net";
364 * That would allow "irc.bighub.net" to introduce anything it wanted..
369 * name = "irc.somehub.fi";
373 * Would allow this server in finland to hub anything but
377 /* Ok, check client_p can hub the new server */
380 /* OOOPs nope can't HUB */
381 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Non-Hub link %s introduced %s.",
382 client_p
->name
, name
);
383 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
384 client_p
->name
, name
);
386 rb_snprintf(squitreason
, sizeof squitreason
,
387 "No matching hub_mask for %s",
389 exit_client(NULL
, client_p
, &me
, squitreason
);
393 /* Check for the new server being leafed behind this HUB */
396 /* OOOPs nope can't HUB this leaf */
397 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
398 "Link %s introduced leafed server %s.",
399 client_p
->name
, name
);
400 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
401 client_p
->name
, name
);
403 rb_snprintf(squitreason
, sizeof squitreason
,
404 "Matching leaf_mask for %s",
406 exit_client(NULL
, client_p
, &me
, squitreason
);
412 if(strlen(name
) > HOSTLEN
)
414 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
415 "Link %s introduced server with invalid servername %s",
416 client_p
->name
, name
);
417 ilog(L_SERVER
, "Link %s introduced server with invalid servername %s",
418 client_p
->name
, name
);
420 exit_client(NULL
, client_p
, &me
, "Invalid servername introduced.");
424 target_p
= make_client(client_p
);
425 make_server(target_p
);
426 target_p
->hopcount
= hop
;
428 rb_strlcpy(target_p
->name
, name
, sizeof(target_p
->name
));
430 set_server_gecos(target_p
, info
);
432 target_p
->servptr
= source_p
;
436 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
437 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
438 add_to_client_hash(target_p
->name
, target_p
);
439 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
441 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
443 sendto_server(client_p
, NULL
, NOCAPS
, NOCAPS
,
444 ":%s SERVER %s %d :%s%s",
445 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
446 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
448 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
449 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
451 /* quick, dirty EOB. you know you love it. */
452 sendto_one(target_p
, ":%s PING %s %s", get_id(&me
, target_p
), me
.name
, target_p
->name
);
454 hdata
.client
= source_p
;
455 hdata
.target
= target_p
;
456 call_hook(h_server_introduced
, &hdata
);
462 ms_sid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
464 struct Client
*target_p
;
465 struct remote_conf
*hub_p
;
466 hook_data_client hdata
;
471 char squitreason
[160];
475 /* collision on the name? */
476 if((target_p
= find_server(NULL
, parv
[1])) != NULL
)
478 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
479 client_p
->name
, parv
[1]);
481 rb_snprintf(squitreason
, sizeof squitreason
,
482 "Server %s already exists",
484 exit_client(NULL
, client_p
, &me
, squitreason
);
488 /* collision on the SID? */
489 if((target_p
= find_id(parv
[3])) != NULL
)
491 sendto_wallops_flags(UMODE_WALLOP
, &me
,
492 "Link %s cancelled, SID %s for server %s already in use by %s",
493 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
494 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
495 ":%s WALLOPS :Link %s cancelled, SID %s for server %s already in use by %s",
496 me
.id
, client_p
->name
, parv
[3], parv
[1], target_p
->name
);
497 ilog(L_SERVER
, "Link %s cancelled, SID %s for server %s already in use by %s",
498 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
500 rb_snprintf(squitreason
, sizeof squitreason
,
501 "SID %s for %s already in use by %s",
502 parv
[3], parv
[1], target_p
->name
);
503 exit_client(NULL
, client_p
, &me
, squitreason
);
507 if(bogus_host(parv
[1]) || strlen(parv
[1]) > HOSTLEN
)
509 sendto_one(client_p
, "ERROR :Invalid servername");
510 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
511 "Link %s cancelled, servername %s invalid",
512 client_p
->name
, parv
[1]);
513 ilog(L_SERVER
, "Link %s cancelled, servername %s invalid",
514 client_p
->name
, parv
[1]);
516 exit_client(NULL
, client_p
, &me
, "Bogus server name");
520 if(!IsDigit(parv
[3][0]) || !IsIdChar(parv
[3][1]) ||
521 !IsIdChar(parv
[3][2]) || parv
[3][3] != '\0')
523 sendto_one(client_p
, "ERROR :Invalid SID");
524 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
525 "Link %s cancelled, SID %s invalid",
526 client_p
->name
, parv
[3]);
527 ilog(L_SERVER
, "Link %s cancelled, SID %s invalid",
528 client_p
->name
, parv
[3]);
530 exit_client(NULL
, client_p
, &me
, "Bogus SID");
534 /* for the directly connected server:
535 * H: allows it to introduce a server matching that mask
536 * L: disallows it introducing a server matching that mask
538 RB_DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
542 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, parv
[1]))
544 if(hub_p
->flags
& CONF_HUB
)
551 /* no matching hub_mask */
554 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
555 "Non-Hub link %s introduced %s.",
556 client_p
->name
, parv
[1]);
557 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
558 client_p
->name
, parv
[1]);
560 rb_snprintf(squitreason
, sizeof squitreason
,
561 "No matching hub_mask for %s",
563 exit_client(NULL
, client_p
, &me
, squitreason
);
567 /* matching leaf_mask */
570 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
571 "Link %s introduced leafed server %s.",
572 client_p
->name
, parv
[1]);
573 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
574 client_p
->name
, parv
[1]);
576 rb_snprintf(squitreason
, sizeof squitreason
,
577 "Matching leaf_mask for %s",
579 exit_client(NULL
, client_p
, &me
, squitreason
);
584 target_p
= make_client(client_p
);
585 make_server(target_p
);
587 rb_strlcpy(target_p
->name
, parv
[1], sizeof(target_p
->name
));
588 target_p
->hopcount
= atoi(parv
[2]);
589 strcpy(target_p
->id
, parv
[3]);
590 set_server_gecos(target_p
, parv
[4]);
592 target_p
->servptr
= source_p
;
595 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
596 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
597 add_to_client_hash(target_p
->name
, target_p
);
598 add_to_id_hash(target_p
->id
, target_p
);
599 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
601 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
603 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
604 ":%s SID %s %d %s :%s%s",
605 source_p
->id
, target_p
->name
, target_p
->hopcount
+ 1,
606 target_p
->id
, IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
608 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
609 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
611 /* quick, dirty EOB. you know you love it. */
612 sendto_one(target_p
, ":%s PING %s %s",
613 get_id(&me
, target_p
), me
.name
, get_id(target_p
, target_p
));
615 hdata
.client
= source_p
;
616 hdata
.target
= target_p
;
617 call_hook(h_server_introduced
, &hdata
);
622 /* set_server_gecos()
624 * input - pointer to client
626 * side effects - servers gecos field is set
629 set_server_gecos(struct Client
*client_p
, const char *info
)
631 /* check the info for [IP] */
638 s
= LOCAL_COPY(info
);
640 /* we should only check the first word for an ip */
641 if((p
= strchr(s
, ' ')))
644 /* check for a ] which would symbolise an [IP] */
645 if((t
= strchr(s
, ']')))
647 /* set s to after the first space */
653 /* no ], put the space back */
657 /* p may have been set to a trailing space, so check s exists and that
659 if(s
&& (*s
!= '\0'))
661 /* a space? if not (H) could be the last part of info.. */
662 if((p
= strchr(s
, ' ')))
665 /* check for (H) which is a hidden server */
666 if(!strcmp(s
, "(H)"))
670 /* if there was no space.. theres nothing to set info to */
679 /* if there was a trailing space, s could point to \0, so check */
680 if(s
&& (*s
!= '\0'))
682 rb_strlcpy(client_p
->info
, s
, sizeof(client_p
->info
));
688 rb_strlcpy(client_p
->info
, "(Unknown Location)", sizeof(client_p
->info
));
697 * output - 1 if a bogus hostname input, 0 if its valid
698 * side effects - none
701 bogus_host(const char *host
)
703 int bogus_server
= 0;
707 for(s
= host
; *s
; s
++)
718 if(!dots
|| bogus_server
)