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 $
29 #include "client.h" /* client struct */
30 #include "common.h" /* TRUE bleah */
32 #include "hash.h" /* add_to_client_hash */
33 #include "irc_string.h"
34 #include "ircd.h" /* me */
35 #include "numeric.h" /* ERR_xxx */
36 #include "s_conf.h" /* struct ConfItem */
37 #include "s_newconf.h"
38 #include "s_log.h" /* log level defines */
39 #include "s_serv.h" /* server_estab, check_server */
40 #include "s_stats.h" /* ServerStats */
42 #include "send.h" /* sendto_one */
47 static int mr_server(struct Client
*, struct Client
*, int, const char **);
48 static int ms_server(struct Client
*, struct Client
*, int, const char **);
49 static int ms_sid(struct Client
*, struct Client
*, int, const char **);
51 struct Message server_msgtab
= {
52 "SERVER", 0, 0, 0, MFLG_SLOW
| MFLG_UNREG
,
53 {{mr_server
, 4}, mg_reg
, mg_ignore
, {ms_server
, 4}, mg_ignore
, mg_reg
}
55 struct Message sid_msgtab
= {
56 "SID", 0, 0, 0, MFLG_SLOW
,
57 {mg_ignore
, mg_reg
, mg_ignore
, {ms_sid
, 5}, mg_ignore
, mg_reg
}
60 mapi_clist_av1 server_clist
[] = { &server_msgtab
, &sid_msgtab
, NULL
};
62 DECLARE_MODULE_AV1(server
, NULL
, NULL
, server_clist
, NULL
, NULL
, "$Revision: 3291 $");
64 int bogus_host(const char *host
);
65 static int set_server_gecos(struct Client
*, const char *);
68 * mr_server - SERVER message handler
69 * parv[0] = sender prefix
70 * parv[1] = servername
71 * parv[2] = serverinfo/hopcount
72 * parv[3] = serverinfo
75 mr_server(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
77 char info
[REALLEN
+ 1];
79 struct Client
*target_p
;
84 strlcpy(info
, parv
[3], sizeof(info
));
86 if (IsHandshake(client_p
) && irccmp(client_p
->name
, name
))
88 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
89 "Server %s has unexpected name %s",
90 get_server_name(client_p
, HIDE_IP
), 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_ALL
, "Link %s dropped, non-TS server",
103 get_server_name(client_p
, HIDE_IP
));
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 switch (check_server(name
, client_p
))
119 if(ConfigFileEntry
.warn_no_nline
)
121 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
122 "Unauthorised server connection attempt from %s: "
123 "No entry for servername %s",
124 get_server_name(client_p
, HIDE_IP
), name
);
126 ilog(L_SERVER
, "Access denied, no connect block for server %s%s",
127 EmptyString(client_p
->name
) ? name
: "",
128 log_client_name(client_p
, SHOW_IP
));
131 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
137 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
138 "Unauthorised server connection attempt from %s: "
139 "Bad password for server %s",
140 get_server_name(client_p
, HIDE_IP
), name
);
142 ilog(L_SERVER
, "Access denied, invalid password for server %s%s",
143 EmptyString(client_p
->name
) ? name
: "",
144 log_client_name(client_p
, SHOW_IP
));
146 exit_client(client_p
, client_p
, client_p
, "Invalid password.");
152 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
153 "Unauthorised server connection attempt from %s: "
154 "Invalid host for server %s",
155 get_server_name(client_p
, HIDE_IP
), name
);
157 ilog(L_SERVER
, "Access denied, invalid host for server %s%s",
158 EmptyString(client_p
->name
) ? name
: "",
159 log_client_name(client_p
, SHOW_IP
));
161 exit_client(client_p
, client_p
, client_p
, "Invalid host.");
166 /* servername is > HOSTLEN */
168 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
169 "Invalid servername %s from %s",
170 name
, get_server_name(client_p
, HIDE_IP
));
171 ilog(L_SERVER
, "Access denied, invalid servername from %s",
172 log_client_name(client_p
, SHOW_IP
));
174 exit_client(client_p
, client_p
, client_p
, "Invalid servername.");
180 if((target_p
= find_server(NULL
, name
)))
183 * This link is trying feed me a server that I already have
184 * access through another path -- multiple paths not accepted
185 * currently, kill this link immediately!!
187 * Rather than KILL the link which introduced it, KILL the
188 * youngest of the two links. -avalon
190 * Definitely don't do that here. This is from an unregistered
193 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
194 "Attempt to re-introduce server %s from %s",
195 name
, get_server_name(client_p
, HIDE_IP
));
196 ilog(L_SERVER
, "Attempt to re-introduce server %s from %s",
197 name
, log_client_name(client_p
, SHOW_IP
));
199 sendto_one(client_p
, "ERROR :Server already exists.");
200 exit_client(client_p
, client_p
, client_p
, "Server Exists");
204 if(has_id(client_p
) && (target_p
= find_id(client_p
->id
)) != NULL
)
206 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
207 "Attempt to re-introduce SID %s from %s%s",
209 EmptyString(client_p
->name
) ? name
: "",
210 get_server_name(client_p
, HIDE_IP
));
211 ilog(L_SERVER
, "Attempt to re-introduce SID %s from %s%s",
213 EmptyString(client_p
->name
) ? name
: "",
214 log_client_name(client_p
, SHOW_IP
));
216 sendto_one(client_p
, "ERROR :SID already exists.");
217 exit_client(client_p
, client_p
, client_p
, "SID Exists");
222 * if we are connecting (Handshake), we already have the name from the
223 * C:line in client_p->name
226 strlcpy(client_p
->name
, name
, sizeof(client_p
->name
));
227 set_server_gecos(client_p
, info
);
228 client_p
->hopcount
= hop
;
229 server_estab(client_p
);
235 * ms_server - SERVER message handler
236 * parv[0] = sender prefix
237 * parv[1] = servername
238 * parv[2] = serverinfo/hopcount
239 * parv[3] = serverinfo
242 ms_server(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
244 char info
[REALLEN
+ 1];
245 /* same size as in s_misc.c */
247 struct Client
*target_p
;
248 struct remote_conf
*hub_p
;
249 hook_data_client hdata
;
257 strlcpy(info
, parv
[3], sizeof(info
));
259 if((target_p
= find_server(NULL
, name
)))
262 * This link is trying feed me a server that I already have
263 * access through another path -- multiple paths not accepted
264 * currently, kill this link immediately!!
266 * Rather than KILL the link which introduced it, KILL the
267 * youngest of the two links. -avalon
269 * I think that we should exit the link itself, not the introducer,
270 * and we should always exit the most recently received(i.e. the
271 * one we are receiving this SERVER for. -A1kmm
273 * You *cant* do this, if you link somewhere, it bursts you a server
274 * that already exists, then sends you a client burst, you squit the
275 * server, but you keep getting the burst of clients on a server that
276 * doesnt exist, although ircd can handle it, its not a realistic
279 /* It is behind a host-masked server. Completely ignore the
280 * server message(don't propagate or we will delink from whoever
281 * we propagate to). -A1kmm */
282 if(irccmp(target_p
->name
, name
) && target_p
->from
== client_p
)
285 sendto_one(client_p
, "ERROR :Server %s already exists", name
);
287 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
288 "Link %s cancelled, server %s already exists",
289 get_server_name(client_p
, SHOW_IP
), name
);
290 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
291 client_p
->name
, name
);
293 exit_client(client_p
, client_p
, &me
, "Server Exists");
298 * User nicks never have '.' in them and server names
299 * must always have '.' in them.
301 if(strchr(name
, '.') == NULL
)
304 * Server trying to use the same name as a person. Would
305 * cause a fair bit of confusion. Enough to make it hellish
306 * for a while and servers to send stuff to the wrong place.
308 sendto_one(client_p
, "ERROR :Nickname %s already exists!", name
);
309 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
310 "Link %s cancelled: Server/nick collision on %s",
311 get_server_name(client_p
, HIDE_IP
), name
);
312 ilog(L_SERVER
, "Link %s cancelled: Server/nick collision on %s",
313 client_p
->name
, name
);
315 exit_client(client_p
, client_p
, client_p
, "Nick as Server");
320 * Server is informing about a new server behind
321 * this link. Create REMOTE server structure,
322 * add it to list and propagate word to my other
325 if(parc
== 1 || EmptyString(info
))
327 sendto_one(client_p
, "ERROR :No server info specified for %s", name
);
332 * See if the newly found server is behind a guaranteed
333 * leaf. If so, close the link.
336 DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
340 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, name
))
342 if(hub_p
->flags
& CONF_HUB
)
349 /* Ok, this way this works is
351 * A server can have a CONF_HUB allowing it to introduce servers
355 * name = "irc.bighub.net";
359 * That would allow "irc.bighub.net" to introduce anything it wanted..
364 * name = "irc.somehub.fi";
368 * Would allow this server in finland to hub anything but
372 /* Ok, check client_p can hub the new server, and make sure it's not a LL */
375 /* OOOPs nope can't HUB */
376 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Non-Hub link %s introduced %s.",
377 get_server_name(client_p
, HIDE_IP
), name
);
378 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
379 client_p
->name
, name
);
381 exit_client(NULL
, client_p
, &me
, "No matching hub_mask.");
385 /* Check for the new server being leafed behind this HUB */
388 /* OOOPs nope can't HUB this leaf */
389 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
390 "Link %s introduced leafed server %s.",
391 get_server_name(client_p
, HIDE_IP
), name
);
392 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
393 client_p
->name
, name
);
395 exit_client(NULL
, client_p
, &me
, "Leafed Server.");
401 if(strlen(name
) > HOSTLEN
)
403 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
404 "Link %s introduced server with invalid servername %s",
405 get_server_name(client_p
, HIDE_IP
), name
);
406 ilog(L_SERVER
, "Link %s introduced server with invalid servername %s",
407 client_p
->name
, name
);
409 exit_client(NULL
, client_p
, &me
, "Invalid servername introduced.");
413 target_p
= make_client(client_p
);
414 make_server(target_p
);
415 target_p
->hopcount
= hop
;
417 strlcpy(target_p
->name
, name
, sizeof(target_p
->name
));
419 set_server_gecos(target_p
, info
);
421 target_p
->servptr
= source_p
;
425 dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
426 dlinkAddTailAlloc(target_p
, &global_serv_list
);
427 add_to_client_hash(target_p
->name
, target_p
);
428 dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
430 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
432 sendto_server(client_p
, NULL
, NOCAPS
, NOCAPS
,
433 ":%s SERVER %s %d :%s%s",
434 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
435 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
437 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
438 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
440 /* quick, dirty EOB. you know you love it. */
441 sendto_one(target_p
, ":%s PING %s %s", get_id(&me
, target_p
), me
.name
, target_p
->name
);
443 hdata
.client
= source_p
;
444 hdata
.target
= target_p
;
445 call_hook(h_server_introduced
, &hdata
);
451 ms_sid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
453 struct Client
*target_p
;
454 struct remote_conf
*hub_p
;
455 hook_data_client hdata
;
463 /* collision on the name? */
464 if((target_p
= find_server(NULL
, parv
[1])) != NULL
)
466 sendto_one(client_p
, "ERROR :Server %s already exists", parv
[1]);
467 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
468 "Link %s cancelled, server %s already exists",
469 get_server_name(client_p
, SHOW_IP
), parv
[1]);
470 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
471 client_p
->name
, parv
[1]);
473 exit_client(NULL
, client_p
, &me
, "Server Exists");
477 /* collision on the SID? */
478 if((target_p
= find_id(parv
[3])) != NULL
)
480 sendto_one(client_p
, "ERROR :SID %s already exists", parv
[3]);
481 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
482 "Link %s cancelled, SID %s already exists",
483 get_server_name(client_p
, SHOW_IP
), parv
[3]);
484 ilog(L_SERVER
, "Link %s cancelled, SID %s already exists",
485 client_p
->name
, parv
[3]);
487 exit_client(NULL
, client_p
, &me
, "Server Exists");
491 if(bogus_host(parv
[1]) || strlen(parv
[1]) > HOSTLEN
)
493 sendto_one(client_p
, "ERROR :Invalid servername");
494 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
495 "Link %s cancelled, servername %s invalid",
496 get_server_name(client_p
, SHOW_IP
), parv
[1]);
497 ilog(L_SERVER
, "Link %s cancelled, servername %s invalid",
498 client_p
->name
, parv
[1]);
500 exit_client(NULL
, client_p
, &me
, "Bogus server name");
504 if(!IsDigit(parv
[3][0]) || !IsIdChar(parv
[3][1]) ||
505 !IsIdChar(parv
[3][2]) || parv
[3][3] != '\0')
507 sendto_one(client_p
, "ERROR :Invalid SID");
508 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
509 "Link %s cancelled, SID %s invalid",
510 get_server_name(client_p
, SHOW_IP
), parv
[3]);
511 ilog(L_SERVER
, "Link %s cancelled, SID %s invalid",
512 client_p
->name
, parv
[3]);
514 exit_client(NULL
, client_p
, &me
, "Bogus SID");
518 /* for the directly connected server:
519 * H: allows it to introduce a server matching that mask
520 * L: disallows it introducing a server matching that mask
522 DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
526 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, parv
[1]))
528 if(hub_p
->flags
& CONF_HUB
)
535 /* no matching hub_mask */
538 sendto_one(client_p
, "ERROR :No matching hub_mask");
539 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
540 "Non-Hub link %s introduced %s.",
541 get_server_name(client_p
, SHOW_IP
), parv
[1]);
542 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
543 client_p
->name
, parv
[1]);
544 exit_client(NULL
, client_p
, &me
, "No matching hub_mask.");
548 /* matching leaf_mask */
551 sendto_one(client_p
, "ERROR :Matching leaf_mask");
552 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
553 "Link %s introduced leafed server %s.",
554 get_server_name(client_p
, SHOW_IP
), parv
[1]);
555 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
556 client_p
->name
, parv
[1]);
557 exit_client(NULL
, client_p
, &me
, "Leafed Server.");
562 target_p
= make_client(client_p
);
563 make_server(target_p
);
565 strlcpy(target_p
->name
, parv
[1], sizeof(target_p
->name
));
566 target_p
->hopcount
= atoi(parv
[2]);
567 strcpy(target_p
->id
, parv
[3]);
568 set_server_gecos(target_p
, parv
[4]);
570 target_p
->servptr
= source_p
;
573 dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
574 dlinkAddTailAlloc(target_p
, &global_serv_list
);
575 add_to_client_hash(target_p
->name
, target_p
);
576 add_to_id_hash(target_p
->id
, target_p
);
577 dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
579 target_p
->serv
->nameinfo
= scache_connect(target_p
->name
, target_p
->info
, IsHidden(target_p
));
581 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
582 ":%s SID %s %d %s :%s%s",
583 source_p
->id
, target_p
->name
, target_p
->hopcount
+ 1,
584 target_p
->id
, IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
585 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
586 ":%s SERVER %s %d :%s%s",
587 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
588 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
590 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
591 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
593 /* quick, dirty EOB. you know you love it. */
594 sendto_one(target_p
, ":%s PING %s %s",
595 get_id(&me
, target_p
), me
.name
, get_id(target_p
, target_p
));
597 hdata
.client
= source_p
;
598 hdata
.target
= target_p
;
599 call_hook(h_server_introduced
, &hdata
);
604 /* set_server_gecos()
606 * input - pointer to client
608 * side effects - servers gecos field is set
611 set_server_gecos(struct Client
*client_p
, const char *info
)
613 /* check the info for [IP] */
620 s
= LOCAL_COPY(info
);
622 /* we should only check the first word for an ip */
623 if((p
= strchr(s
, ' ')))
626 /* check for a ] which would symbolise an [IP] */
627 if((t
= strchr(s
, ']')))
629 /* set s to after the first space */
635 /* no ], put the space back */
639 /* p may have been set to a trailing space, so check s exists and that
641 if(s
&& (*s
!= '\0'))
643 /* a space? if not (H) could be the last part of info.. */
644 if((p
= strchr(s
, ' ')))
647 /* check for (H) which is a hidden server */
648 if(!strcmp(s
, "(H)"))
652 /* if there was no space.. theres nothing to set info to */
661 /* if there was a trailing space, s could point to \0, so check */
662 if(s
&& (*s
!= '\0'))
664 strlcpy(client_p
->info
, s
, sizeof(client_p
->info
));
670 strlcpy(client_p
->info
, "(Unknown Location)", sizeof(client_p
->info
));
679 * output - 1 if a bogus hostname input, 0 if its valid
680 * side effects - none
683 bogus_host(const char *host
)
685 int bogus_server
= 0;
689 for(s
= host
; *s
; s
++)
700 if(!dots
|| bogus_server
)