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 "hash.h" /* add_to_client_hash */
29 #include "ircd.h" /* me */
30 #include "numeric.h" /* ERR_xxx */
31 #include "s_conf.h" /* struct ConfItem */
32 #include "s_newconf.h"
33 #include "logger.h" /* log level defines */
34 #include "s_serv.h" /* server_estab, check_server */
35 #include "s_stats.h" /* ServerStats */
37 #include "send.h" /* sendto_one */
42 static const char server_desc
[] =
43 "Provides the TS6 commands to introduce a new server to the network";
45 static void mr_server(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
46 static void ms_server(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
47 static void ms_sid(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
49 static bool bogus_host(const char *host
);
50 static void set_server_gecos(struct Client
*, const char *);
52 struct Message server_msgtab
= {
54 {{mr_server
, 4}, mg_reg
, mg_ignore
, {ms_server
, 4}, mg_ignore
, mg_reg
}
56 struct Message sid_msgtab
= {
58 {mg_ignore
, mg_reg
, mg_ignore
, {ms_sid
, 5}, mg_ignore
, mg_reg
}
61 mapi_clist_av1 server_clist
[] = { &server_msgtab
, &sid_msgtab
, NULL
};
63 DECLARE_MODULE_AV2(server
, NULL
, NULL
, server_clist
, NULL
, NULL
, NULL
, NULL
, server_desc
);
66 * mr_server - SERVER message handler
67 * parv[1] = servername
68 * parv[2] = serverinfo/hopcount
69 * parv[3] = serverinfo
72 mr_server(struct MsgBuf
*msgbuf_p
, 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
;
84 rb_strlcpy(info
, parv
[3], sizeof(info
));
86 if (IsHandshake(client_p
) && irccmp(client_p
->name
, name
))
88 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
89 "Server %s has unexpected name %s",
90 client_p
->name
, name
);
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");
98 * Reject a direct nonTS server connection if we're TS_ONLY -orabidoo
100 if(!DoesTS(client_p
))
102 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Link %s dropped, non-TS server",
104 exit_client(client_p
, client_p
, client_p
, "Non-TS server");
110 exit_client(client_p
, client_p
, client_p
, "Bogus server name");
114 /* Now we just have to call check_server and everything should be
115 * check for us... -A1kmm. */
116 ret
= check_server(name
, client_p
);
123 if(ConfigFileEntry
.warn_no_nline
)
125 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
126 "Unauthorised server connection attempt from %s: "
127 "No entry for servername %s",
128 "[@255.255.255.255]", name
);
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
));
135 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
141 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
142 "Unauthorised server connection attempt from %s: "
143 "Bad credentials for server %s",
144 "[@255.255.255.255]", name
);
146 ilog(L_SERVER
, "Access denied, invalid credentials for server %s%s",
147 EmptyString(client_p
->name
) ? name
: "",
148 log_client_name(client_p
, SHOW_IP
));
150 exit_client(client_p
, client_p
, client_p
, "Invalid credentials.");
156 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
157 "Unauthorised server connection attempt from %s: "
158 "Invalid host for server %s",
159 "[@255.255.255.255]", name
);
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
));
165 exit_client(client_p
, client_p
, client_p
, "Invalid host.");
170 /* servername is > HOSTLEN */
172 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
173 "Invalid servername %s from %s",
174 name
, "[@255.255.255.255]");
175 ilog(L_SERVER
, "Access denied, invalid servername from %s",
176 log_client_name(client_p
, SHOW_IP
));
178 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
183 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
184 "Connection from servername %s requires SSL/TLS but is plaintext",
186 ilog(L_SERVER
, "Access denied, requires SSL/TLS but is plaintext from %s",
187 log_client_name(client_p
, SHOW_IP
));
189 exit_client(client_p
, client_p
, client_p
, "Access denied, requires SSL/TLS but is plaintext");
192 if (client_p
->certfp
)
194 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
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.");
203 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
204 "Connection from servername %s failed certificate validation",
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.");
213 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
214 "Connection from servername %s rejected, no more connections allowed in class",
216 ilog(L_SERVER
, "Access denied, no more connections allowed in class for %s",
217 log_client_name(client_p
, SHOW_IP
));
219 exit_client(client_p
, client_p
, client_p
, "Access denied, no more connections allowed in class");
222 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
223 "Connection from servername %s rejected, unknown error %d",
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
));
229 exit_client(client_p
, client_p
, client_p
, "Unknown error.");
233 /* require TS6 for direct links */
234 if(!IsCapable(client_p
, CAP_TS6
))
236 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
237 "Link %s dropped, TS6 protocol is required", name
);
238 exit_client(client_p
, client_p
, client_p
, "Incompatible TS version");
242 /* check to ensure any "required" caps are set. --nenolod */
243 required_mask
= capability_index_get_required(serv_capindex
);
244 if (!IsCapable(client_p
, required_mask
))
246 missing
= capability_index_list(serv_capindex
, required_mask
&
247 ~client_p
->localClient
->caps
);
248 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
249 "Link %s dropped, required CAPABs [%s] are missing",
251 ilog(L_SERVER
, "Link %s%s dropped, required CAPABs [%s] are missing",
252 EmptyString(client_p
->name
) ? name
: "",
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
258 sendto_one(client_p
, "ERROR :Missing required CAPABs (%s)", missing
);
259 exit_client(client_p
, client_p
, client_p
, "Missing required CAPABs");
264 if((target_p
= find_server(NULL
, name
)))
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!!
271 * Rather than KILL the link which introduced it, KILL the
272 * youngest of the two links. -avalon
274 * Definitely don't do that here. This is from an unregistered
277 if (target_p
->servptr
->flags
& FLAGS_SERVICE
)
279 /* Assume any servers introduced by services
283 sendto_one(client_p
, "ERROR :Server juped.");
287 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
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
));
293 sendto_one(client_p
, "ERROR :Server already exists.");
295 exit_client(client_p
, client_p
, client_p
, "Server Exists");
299 if (client_p
->preClient
&& !EmptyString(client_p
->preClient
->id
)) {
300 if ((target_p
= find_id(client_p
->preClient
->id
)) != NULL
) {
301 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
302 "Attempt to re-introduce SID %s from %s%s (already in use by %s)",
303 client_p
->preClient
->id
,
304 EmptyString(client_p
->name
) ? name
: "",
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)",
307 client_p
->preClient
->id
,
308 EmptyString(client_p
->name
) ? name
: "",
309 log_client_name(client_p
, SHOW_IP
),
312 sendto_one(client_p
, "ERROR :SID already exists.");
313 exit_client(client_p
, client_p
, client_p
, "SID Exists");
316 rb_strlcpy(client_p
->id
, client_p
->preClient
->id
, sizeof(client_p
->id
));
321 * if we are connecting (Handshake), we already have the name from the
322 * C:line in client_p->name
325 rb_strlcpy(client_p
->name
, name
, sizeof(client_p
->name
));
326 set_server_gecos(client_p
, info
);
327 client_p
->hopcount
= hop
;
328 server_estab(client_p
);
332 * ms_server - SERVER message handler
333 * parv[1] = servername
334 * parv[2] = serverinfo/hopcount
335 * parv[3] = serverinfo
338 ms_server(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
340 char info
[REALLEN
+ 1];
341 /* same size as in s_misc.c */
343 struct Client
*target_p
;
344 hook_data_client hdata
;
346 char squitreason
[160];
350 rb_strlcpy(info
, parv
[3], sizeof(info
));
352 if(find_server(NULL
, name
))
355 * This link is trying feed me a server that I already have
356 * access through another path -- multiple paths not accepted
357 * currently, kill this link immediately!!
359 * Rather than KILL the link which introduced it, KILL the
360 * youngest of the two links. -avalon
362 * I think that we should exit the link itself, not the introducer,
363 * and we should always exit the most recently received(i.e. the
364 * one we are receiving this SERVER for. -A1kmm
366 * You *cant* do this, if you link somewhere, it bursts you a server
367 * that already exists, then sends you a client burst, you squit the
368 * server, but you keep getting the burst of clients on a server that
369 * doesnt exist, although ircd can handle it, its not a realistic
372 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
373 client_p
->name
, name
);
375 snprintf(squitreason
, sizeof squitreason
,
376 "Server %s already exists",
378 exit_client(client_p
, client_p
, &me
, squitreason
);
383 * User nicks never have '.' in them and server names
384 * must always have '.' in them.
386 if(strchr(name
, '.') == NULL
)
389 * Server trying to use the same name as a person. Would
390 * cause a fair bit of confusion. Enough to make it hellish
391 * for a while and servers to send stuff to the wrong place.
393 sendto_one(client_p
, "ERROR :Nickname %s already exists!", name
);
394 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
395 "Link %s cancelled: Server/nick collision on %s",
396 client_p
->name
, name
);
397 ilog(L_SERVER
, "Link %s cancelled: Server/nick collision on %s",
398 client_p
->name
, name
);
400 exit_client(client_p
, client_p
, client_p
, "Nick as Server");
405 * Server is informing about a new server behind
406 * this link. Create REMOTE server structure,
407 * add it to list and propagate word to my other
411 if(strlen(name
) > HOSTLEN
)
413 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
414 "Link %s introduced server with invalid servername %s",
415 client_p
->name
, name
);
416 ilog(L_SERVER
, "Link %s introduced server with invalid servername %s",
417 client_p
->name
, name
);
419 exit_client(NULL
, client_p
, &me
, "Invalid servername introduced.");
423 target_p
= make_client(client_p
);
424 make_server(target_p
);
425 target_p
->hopcount
= hop
;
427 rb_strlcpy(target_p
->name
, name
, sizeof(target_p
->name
));
429 set_server_gecos(target_p
, info
);
431 target_p
->servptr
= source_p
;
435 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
436 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
437 add_to_client_hash(target_p
->name
, target_p
);
438 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
440 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
442 sendto_server(client_p
, NULL
, NOCAPS
, NOCAPS
,
443 ":%s SERVER %s %d :%s%s",
444 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
445 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
447 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
448 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
450 /* quick, dirty EOB. you know you love it. */
451 sendto_one(target_p
, ":%s PING %s %s", get_id(&me
, target_p
), me
.name
, target_p
->name
);
453 hdata
.client
= source_p
;
454 hdata
.target
= target_p
;
455 call_hook(h_server_introduced
, &hdata
);
459 ms_sid(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
461 struct Client
*target_p
;
462 hook_data_client hdata
;
463 char squitreason
[160];
465 /* collision on the name? */
466 if(find_server(NULL
, parv
[1]) != NULL
)
468 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
469 client_p
->name
, parv
[1]);
471 snprintf(squitreason
, sizeof squitreason
,
472 "Server %s already exists",
474 exit_client(NULL
, client_p
, &me
, squitreason
);
478 /* collision on the SID? */
479 if((target_p
= find_id(parv
[3])) != NULL
)
481 sendto_wallops_flags(UMODE_WALLOP
, &me
,
482 "Link %s cancelled, SID %s for server %s already in use by %s",
483 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
484 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
485 ":%s WALLOPS :Link %s cancelled, SID %s for server %s already in use by %s",
486 me
.id
, client_p
->name
, parv
[3], parv
[1], target_p
->name
);
487 ilog(L_SERVER
, "Link %s cancelled, SID %s for server %s already in use by %s",
488 client_p
->name
, parv
[3], parv
[1], target_p
->name
);
490 snprintf(squitreason
, sizeof squitreason
,
491 "SID %s for %s already in use by %s",
492 parv
[3], parv
[1], target_p
->name
);
493 exit_client(NULL
, client_p
, &me
, squitreason
);
497 if(bogus_host(parv
[1]) || strlen(parv
[1]) > HOSTLEN
)
499 sendto_one(client_p
, "ERROR :Invalid servername");
500 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
501 "Link %s cancelled, servername %s invalid",
502 client_p
->name
, parv
[1]);
503 ilog(L_SERVER
, "Link %s cancelled, servername %s invalid",
504 client_p
->name
, parv
[1]);
506 exit_client(NULL
, client_p
, &me
, "Bogus server name");
510 if(!IsDigit(parv
[3][0]) || !IsIdChar(parv
[3][1]) ||
511 !IsIdChar(parv
[3][2]) || parv
[3][3] != '\0')
513 sendto_one(client_p
, "ERROR :Invalid SID");
514 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
515 "Link %s cancelled, SID %s invalid",
516 client_p
->name
, parv
[3]);
517 ilog(L_SERVER
, "Link %s cancelled, SID %s invalid",
518 client_p
->name
, parv
[3]);
520 exit_client(NULL
, client_p
, &me
, "Bogus SID");
525 target_p
= make_client(client_p
);
526 make_server(target_p
);
528 rb_strlcpy(target_p
->name
, parv
[1], sizeof(target_p
->name
));
529 target_p
->hopcount
= atoi(parv
[2]);
530 rb_strlcpy(target_p
->id
, parv
[3], sizeof(target_p
->id
));
531 set_server_gecos(target_p
, parv
[4]);
533 target_p
->servptr
= source_p
;
536 rb_dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
537 rb_dlinkAddTailAlloc(target_p
, &global_serv_list
);
538 add_to_client_hash(target_p
->name
, target_p
);
539 add_to_id_hash(target_p
->id
, target_p
);
540 rb_dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
542 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
544 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
545 ":%s SID %s %d %s :%s%s",
546 source_p
->id
, target_p
->name
, target_p
->hopcount
+ 1,
547 target_p
->id
, IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
549 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
550 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
552 /* quick, dirty EOB. you know you love it. */
553 sendto_one(target_p
, ":%s PING %s %s",
554 get_id(&me
, target_p
), me
.name
, get_id(target_p
, target_p
));
556 hdata
.client
= source_p
;
557 hdata
.target
= target_p
;
558 call_hook(h_server_introduced
, &hdata
);
561 /* set_server_gecos()
563 * input - pointer to client
565 * side effects - servers gecos field is set
568 set_server_gecos(struct Client
*client_p
, const char *info
)
570 /* check the info for [IP] */
576 s
= LOCAL_COPY(info
);
578 /* we should only check the first word for an ip */
579 if((p
= strchr(s
, ' ')))
582 /* check for a ] which would symbolise an [IP] */
585 /* set s to after the first space */
591 /* no ], put the space back */
595 /* p may have been set to a trailing space, so check s exists and that
597 if(s
&& (*s
!= '\0'))
599 /* a space? if not (H) could be the last part of info.. */
600 if((p
= strchr(s
, ' ')))
603 /* check for (H) which is a hidden server */
604 if(!strcmp(s
, "(H)"))
608 /* if there was no space.. theres nothing to set info to */
617 /* if there was a trailing space, s could point to \0, so check */
618 if(s
&& (*s
!= '\0'))
620 rb_strlcpy(client_p
->info
, s
, sizeof(client_p
->info
));
626 rb_strlcpy(client_p
->info
, "(Unknown Location)", sizeof(client_p
->info
));
633 * output - true if a bogus hostname input, false if its valid
634 * side effects - none
637 bogus_host(const char *host
)
639 bool bogus_server
= false;
643 for(s
= host
; *s
; s
++)
654 if(!dots
|| bogus_server
)