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 */
41 #include "scache.h" /* find_or_add */
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
->serv
->up
= find_or_add(source_p
->name
);
424 target_p
->serv
->upid
= source_p
->id
;
426 target_p
->servptr
= source_p
;
430 dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
431 dlinkAddTailAlloc(target_p
, &global_serv_list
);
432 add_to_client_hash(target_p
->name
, target_p
);
433 dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
435 sendto_server(client_p
, NULL
, NOCAPS
, NOCAPS
,
436 ":%s SERVER %s %d :%s%s",
437 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
438 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
440 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
441 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
443 /* quick, dirty EOB. you know you love it. */
444 sendto_one(target_p
, ":%s PING %s %s", get_id(&me
, target_p
), me
.name
, target_p
->name
);
446 hdata
.client
= source_p
;
447 hdata
.target
= target_p
;
448 call_hook(h_server_introduced
, &hdata
);
454 ms_sid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
456 struct Client
*target_p
;
457 struct remote_conf
*hub_p
;
458 hook_data_client hdata
;
466 /* collision on the name? */
467 if((target_p
= find_server(NULL
, parv
[1])) != NULL
)
469 sendto_one(client_p
, "ERROR :Server %s already exists", parv
[1]);
470 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
471 "Link %s cancelled, server %s already exists",
472 get_server_name(client_p
, SHOW_IP
), parv
[1]);
473 ilog(L_SERVER
, "Link %s cancelled, server %s already exists",
474 client_p
->name
, parv
[1]);
476 exit_client(NULL
, client_p
, &me
, "Server Exists");
480 /* collision on the SID? */
481 if((target_p
= find_id(parv
[3])) != NULL
)
483 sendto_one(client_p
, "ERROR :SID %s already exists", parv
[3]);
484 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
485 "Link %s cancelled, SID %s already exists",
486 get_server_name(client_p
, SHOW_IP
), parv
[3]);
487 ilog(L_SERVER
, "Link %s cancelled, SID %s already exists",
488 client_p
->name
, parv
[3]);
490 exit_client(NULL
, client_p
, &me
, "Server Exists");
494 if(bogus_host(parv
[1]) || strlen(parv
[1]) > HOSTLEN
)
496 sendto_one(client_p
, "ERROR :Invalid servername");
497 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
498 "Link %s cancelled, servername %s invalid",
499 get_server_name(client_p
, SHOW_IP
), parv
[1]);
500 ilog(L_SERVER
, "Link %s cancelled, servername %s invalid",
501 client_p
->name
, parv
[1]);
503 exit_client(NULL
, client_p
, &me
, "Bogus server name");
507 if(!IsDigit(parv
[3][0]) || !IsIdChar(parv
[3][1]) ||
508 !IsIdChar(parv
[3][2]) || parv
[3][3] != '\0')
510 sendto_one(client_p
, "ERROR :Invalid SID");
511 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
512 "Link %s cancelled, SID %s invalid",
513 get_server_name(client_p
, SHOW_IP
), parv
[3]);
514 ilog(L_SERVER
, "Link %s cancelled, SID %s invalid",
515 client_p
->name
, parv
[3]);
517 exit_client(NULL
, client_p
, &me
, "Bogus SID");
521 /* for the directly connected server:
522 * H: allows it to introduce a server matching that mask
523 * L: disallows it introducing a server matching that mask
525 DLINK_FOREACH(ptr
, hubleaf_conf_list
.head
)
529 if(match(hub_p
->server
, client_p
->name
) && match(hub_p
->host
, parv
[1]))
531 if(hub_p
->flags
& CONF_HUB
)
538 /* no matching hub_mask */
541 sendto_one(client_p
, "ERROR :No matching hub_mask");
542 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
543 "Non-Hub link %s introduced %s.",
544 get_server_name(client_p
, SHOW_IP
), parv
[1]);
545 ilog(L_SERVER
, "Non-Hub link %s introduced %s.",
546 client_p
->name
, parv
[1]);
547 exit_client(NULL
, client_p
, &me
, "No matching hub_mask.");
551 /* matching leaf_mask */
554 sendto_one(client_p
, "ERROR :Matching leaf_mask");
555 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
556 "Link %s introduced leafed server %s.",
557 get_server_name(client_p
, SHOW_IP
), parv
[1]);
558 ilog(L_SERVER
, "Link %s introduced leafed server %s.",
559 client_p
->name
, parv
[1]);
560 exit_client(NULL
, client_p
, &me
, "Leafed Server.");
565 target_p
= make_client(client_p
);
566 make_server(target_p
);
568 strlcpy(target_p
->name
, parv
[1], sizeof(target_p
->name
));
569 target_p
->hopcount
= atoi(parv
[2]);
570 strcpy(target_p
->id
, parv
[3]);
571 set_server_gecos(target_p
, parv
[4]);
573 target_p
->serv
->up
= find_or_add(source_p
->name
);
576 target_p
->serv
->upid
= source_p
->id
;
578 target_p
->servptr
= source_p
;
581 dlinkAddTail(target_p
, &target_p
->node
, &global_client_list
);
582 dlinkAddTailAlloc(target_p
, &global_serv_list
);
583 add_to_client_hash(target_p
->name
, target_p
);
584 add_to_id_hash(target_p
->id
, target_p
);
585 dlinkAdd(target_p
, &target_p
->lnode
, &target_p
->servptr
->serv
->servers
);
587 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
588 ":%s SID %s %d %s :%s%s",
589 source_p
->id
, target_p
->name
, target_p
->hopcount
+ 1,
590 target_p
->id
, IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
591 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
592 ":%s SERVER %s %d :%s%s",
593 source_p
->name
, target_p
->name
, target_p
->hopcount
+ 1,
594 IsHidden(target_p
) ? "(H) " : "", target_p
->info
);
596 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
597 "Server %s being introduced by %s", target_p
->name
, source_p
->name
);
599 /* quick, dirty EOB. you know you love it. */
600 sendto_one(target_p
, ":%s PING %s %s",
601 get_id(&me
, target_p
), me
.name
, get_id(target_p
, target_p
));
603 hdata
.client
= source_p
;
604 hdata
.target
= target_p
;
605 call_hook(h_server_introduced
, &hdata
);
610 /* set_server_gecos()
612 * input - pointer to client
614 * side effects - servers gecos field is set
617 set_server_gecos(struct Client
*client_p
, const char *info
)
619 /* check the info for [IP] */
626 s
= LOCAL_COPY(info
);
628 /* we should only check the first word for an ip */
629 if((p
= strchr(s
, ' ')))
632 /* check for a ] which would symbolise an [IP] */
633 if((t
= strchr(s
, ']')))
635 /* set s to after the first space */
641 /* no ], put the space back */
645 /* p may have been set to a trailing space, so check s exists and that
647 if(s
&& (*s
!= '\0'))
649 /* a space? if not (H) could be the last part of info.. */
650 if((p
= strchr(s
, ' ')))
653 /* check for (H) which is a hidden server */
654 if(!strcmp(s
, "(H)"))
658 /* if there was no space.. theres nothing to set info to */
667 /* if there was a trailing space, s could point to \0, so check */
668 if(s
&& (*s
!= '\0'))
670 strlcpy(client_p
->info
, s
, sizeof(client_p
->info
));
676 strlcpy(client_p
->info
, "(Unknown Location)", sizeof(client_p
->info
));
685 * output - 1 if a bogus hostname input, 0 if its valid
686 * side effects - none
689 bogus_host(const char *host
)
691 int bogus_server
= 0;
695 for(s
= host
; *s
; s
++)
706 if(!dots
|| bogus_server
)