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