2 * IRC - Internet Relay Chat, ircd/m_server.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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 1, or (at your option)
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
86 * No need to include handlers.h here the signatures must match
87 * and we don't need to force a rebuild of all the handlers everytime
88 * we add a new one to the list. --Bleep
96 #include "ircd_features.h"
97 #include "ircd_reply.h"
98 #include "ircd_string.h"
104 #include "numnicks.h"
105 #include "querycmds.h"
112 #include "userload.h"
119 * mr_server - registration message handler
121 * parv[0] = sender prefix
122 * parv[1] = servername
124 * parv[3] = start timestamp
125 * parv[4] = link timestamp
126 * parv[5] = major protocol version: P09/P10
127 * parv[parc-1] = serverinfo
129 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
130 * numeric nick mask of this server.
131 * parv[7] = +hs (h == hub, s == service)
133 int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
137 char info[REALLEN + 1];
139 struct Client* acptr;
140 struct Client* bcptr;
141 struct Client* LHcptr = 0;
142 struct ConfItem* aconf = 0;
143 struct ConfItem* lhconf = 0;
144 struct Jupe* ajupe = 0;
147 int active_lh_line = 0;
149 time_t start_timestamp;
150 time_t timestamp = 0;
154 if (IsUserPort(cptr))
155 return exit_client_msg(cptr, cptr, &me,
156 "Cannot connect a server to a user port");
158 recv_time = TStime();
163 need_more_params(sptr, "SERVER");
164 return exit_client(cptr, cptr, &me, "Need more parameters");
168 if ((ajupe = jupe_find(host)) && JupeIsActive(ajupe))
169 return exit_client_msg(cptr, sptr, &me, "Juped: %s", JupeReason(ajupe));
171 log_write(LS_NETWORK, L_NOTICE, LOG_NOSNOTICE, "SERVER: %s %s[%s]", parv[1],
172 cli_sockhost(cptr), cli_sock_ip(cptr));
177 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
178 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
180 if (!IsServer(cptr)) /* Don't allow silently connecting a server */
183 if (*parv[7] == '+') {
184 for (ch = parv[7] + 1; *ch; ch++)
195 prot = atoi(parv[5] + 1);
196 if (prot > atoi(MAJOR_PROTOCOL))
197 prot = atoi(MAJOR_PROTOCOL);
199 * Because the previous test is only in 2.10, the following is needed
200 * till all servers are 2.10:
202 if (IsServer(cptr) && prot > Protocol(cptr))
203 prot = Protocol(cptr);
205 start_timestamp = atoi(parv[3]);
206 timestamp = atoi(parv[4]);
207 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
208 host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
210 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
212 return exit_client_msg(cptr, sptr, &me,
213 "Bogus timestamps (%s %s)", parv[3], parv[4]);
215 ircd_strncpy(info, parv[parc - 1], REALLEN);
216 info[REALLEN] = '\0';
217 if (prot < atoi(MINOR_PROTOCOL)) {
218 sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
219 "(%s) from %s", parv[5], cli_name(cptr));
220 return exit_new_server(cptr, sptr, host, timestamp,
221 "Incompatible protocol: %s", parv[5]);
224 * Check for "FRENCH " infection ;-) (actually this should
225 * be replaced with routine to check the hostname syntax in
226 * general). [ This check is still needed, even after the parse
227 * is fixed, because someone can send "SERVER :foo bar " ].
228 * Also, changed to check other "difficult" characters, now
229 * that parse lets all through... --msa
231 if (strlen(host) > HOSTLEN)
232 host[HOSTLEN] = '\0';
234 for (ch = host; *ch; ch++) {
235 if (*ch <= ' ' || *ch > '~')
238 if (*ch || !strchr(host, '.')) {
239 sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
240 host, cli_name(cptr));
241 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
247 * A local server introduces a new server behind this link.
248 * Check if this is allowed according L:, H: and Q: lines.
251 return exit_client_msg(cptr, cptr, &me,
252 "No server info specified for %s", host);
254 * See if the newly found server is behind a guaranteed
255 * leaf (L-line). If so, close the link.
257 if ((lhconf = find_conf_byhost(cli_confs(cptr), cli_name(cptr), CONF_LEAF)) &&
258 (!lhconf->port || (hop > lhconf->port)))
261 * L: lines normally come in pairs, here we try to
262 * make sure that the oldest link is squitted, not
266 if (timestamp <= cli_serv(cptr)->timestamp)
267 LHcptr = 0; /* Kill incoming server */
269 LHcptr = cptr; /* Squit ourselfs */
271 else if (!(lhconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_HUB)) ||
272 (lhconf->port && (hop > lhconf->port)))
274 struct Client *ac3ptr;
276 /* Look for net junction causing this: */
277 LHcptr = 0; /* incoming server */
278 if (*parv[5] != 'J') {
279 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up) {
280 if (IsJunction(ac3ptr)) {
289 if (IsUnknown(cptr) || IsHandshake(cptr))
294 * A local link that is still in undefined state wants
295 * to be a SERVER. Check if this is allowed and change
296 * status accordingly...
299 * If there is more then one server on the same machine
300 * that we try to connect to, it could be that the /CONNECT
301 * <mask> caused this connect to be put at the wrong place
302 * in the hashtable. --Run
303 * Same thing for Unknown connections that first send NICK.
305 * Better check if the two strings are (caseless) identical
306 * and not mess with hash internals.
309 if (!EmptyString(cli_name(cptr)) &&
310 (IsUnknown(cptr) || IsHandshake(cptr)) &&
311 0 != ircd_strcmp(cli_name(cptr), host))
312 hChangeClient(cptr, host);
313 ircd_strncpy(cli_name(cptr), host, HOSTLEN);
314 ircd_strncpy(cli_info(cptr), info[0] ? info : cli_name(&me), REALLEN);
315 cli_hopcount(cptr) = hop;
317 /* check connection rules */
318 if (0 != conf_eval_crule(host, CRULE_ALL)) {
319 ServerStats->is_ref++;
320 sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.", cli_name(cptr));
321 return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
324 if (conf_check_server(cptr)) {
325 ++ServerStats->is_ref;
326 sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
327 "from %s.", cli_name(cptr));
328 return exit_client(cptr, cptr, &me, "No C:line");
331 host = cli_name(cptr);
335 if (!(aconf = find_conf_byname(cli_confs(cptr), host, CONF_SERVER))) {
336 ++ServerStats->is_ref;
337 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
338 "server %s", cli_name(cptr));
339 return exit_client_msg(cptr, cptr, &me,
340 "Access denied. No conf line for server %s", cli_name(cptr));
342 encr = cli_passwd(cptr);
344 if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
345 ++ServerStats->is_ref;
346 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
348 return exit_client_msg(cptr, cptr, &me,
349 "No Access (passwd mismatch) %s", cli_name(cptr));
352 memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr)));
354 if (!feature_bool(FEAT_HUB)) {
355 for (i = 0; i <= HighestFd; i++)
356 if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
365 * We want to find IsConnecting() and IsHandshake() too,
367 * The second finds collisions with numeric representation of existing
368 * servers - these shouldn't happen anymore when all upgraded to 2.10.
371 while ((acptr = FindClient(host)) ||
372 (parc > 7 && (acptr = FindNServer(parv[6]))))
375 * This link is trying feed me a server that I already have
376 * access through another path
378 * Do not allow Uworld to do this.
379 * Do not allow servers that are juped.
380 * Do not allow servers that have older link timestamps
382 * Do not allow servers that use the same numeric as an existing
383 * server, but have a different name.
385 * If my ircd.conf sucks, I can try to connect to myself:
388 return exit_client_msg(cptr, cptr, &me, "nick collision with me (%s), check server number in M:?", host);
390 * Detect wrong numeric.
392 if (0 != ircd_strcmp(cli_name(acptr), host)) {
393 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
394 ":SERVER Numeric Collision: %s != %s",
395 cli_name(acptr), host);
396 return exit_client_msg(cptr, cptr, &me,
397 "NUMERIC collision between %s and %s."
398 " Is your server numeric correct ?", host, cli_name(acptr));
401 * Kill our try, if we had one.
403 if (IsConnecting(acptr))
405 if (!active_lh_line && exit_client(cptr, acptr, &me,
406 "Just connected via another link") == CPTR_KILLED)
409 * We can have only ONE 'IsConnecting', 'IsHandshake' or
410 * 'IsServer', because new 'IsConnecting's are refused to
411 * the same server if we already had it.
416 * Avoid other nick collisions...
417 * This is a doubtfull test though, what else would it be
418 * when it has a server.name ?
420 else if (!IsServer(acptr) && !IsHandshake(acptr))
421 return exit_client_msg(cptr, cptr, &me,
422 "Nickname %s already exists!", host);
424 * Our new server might be a juped server,
425 * or someone trying abuse a second Uworld:
427 else if (IsServer(acptr) && (0 == ircd_strncmp(cli_info(acptr), "JUPE", 4) ||
428 find_conf_byhost(cli_confs(cptr), cli_name(acptr), CONF_UWORLD)))
431 return exit_client(cptr, sptr, &me, cli_info(acptr));
432 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
433 ":Received :%s SERVER %s from %s !?!", parv[0],
434 parv[1], cli_name(cptr));
435 return exit_new_server(cptr, sptr, host, timestamp, "%s", cli_info(acptr));
438 * Of course we find the handshake this link was before :)
440 else if (IsHandshake(acptr) && acptr == cptr)
443 * Here we have a server nick collision...
444 * We don't want to kill the link that was last /connected,
445 * but we neither want to kill a good (old) link.
446 * Therefor we kill the second youngest link.
450 struct Client* c2ptr = 0;
451 struct Client* c3ptr = acptr;
452 struct Client* ac2ptr;
453 struct Client* ac3ptr;
455 /* Search youngest link: */
456 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
457 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
461 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
462 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
465 if (timestamp > cli_serv(c3ptr)->timestamp)
468 c2ptr = acptr; /* Make sure they differ */
470 /* Search second youngest link: */
471 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
472 if (ac2ptr != c3ptr &&
473 cli_serv(ac2ptr)->timestamp >
474 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
478 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
479 if (ac2ptr != c3ptr &&
480 cli_serv(ac2ptr)->timestamp >
481 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
484 if (c3ptr && timestamp > (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
486 /* If timestamps are equal, decide which link to break
489 if ((c2ptr ? cli_serv(c2ptr)->timestamp : timestamp) ==
490 (c3ptr ? cli_serv(c3ptr)->timestamp : timestamp))
498 n2 = cli_name(c2ptr);
499 n2up = MyConnect(c2ptr) ? cli_name(&me) : cli_name(cli_serv(c2ptr)->up);
504 n2up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
508 n3 = cli_name(c3ptr);
509 n3up = MyConnect(c3ptr) ? cli_name(&me) : cli_name(cli_serv(c3ptr)->up);
514 n3up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
516 if (strcmp(n2, n2up) > 0)
518 if (strcmp(n3, n3up) > 0)
520 if (strcmp(n3, n2) > 0)
527 /* Now squit the second youngest link: */
529 return exit_new_server(cptr, sptr, host, timestamp,
530 "server %s already exists and is %ld seconds younger.",
531 host, (long)cli_serv(acptr)->timestamp - (long)timestamp);
532 else if (cli_from(c2ptr) == cptr || IsServer(sptr))
534 struct Client *killedptrfrom = cli_from(c2ptr);
538 * If the L: or H: line also gets rid of this link,
539 * we sent just one squit.
541 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
544 * If breaking the loop here solves the L: or H:
545 * line problem, we don't squit that.
547 if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
552 * If we still have a L: or H: line problem,
553 * we prefer to squit the new server, solving
554 * loop and L:/H: line problem with only one squit.
561 * If the new server was introduced by a server that caused a
562 * Ghost less then 20 seconds ago, this is probably also
563 * a Ghost... (20 seconds is more then enough because all
564 * SERVER messages are at the beginning of a net.burst). --Run
566 if (CurrentTime - cli_serv(cptr)->ghost < 20)
568 killedptrfrom = cli_from(acptr);
569 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
572 else if (exit_client_msg(cptr, c2ptr, &me,
573 "Loop <-- %s (new link is %ld seconds younger)", host,
574 (c3ptr ? (long)cli_serv(c3ptr)->timestamp : timestamp) -
575 (long)cli_serv(c2ptr)->timestamp) == CPTR_KILLED)
578 * Did we kill the incoming server off already ?
580 if (killedptrfrom == cptr)
587 if (LHcptr && a_kills_b_too(LHcptr, acptr))
589 if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
598 * We can't believe it is a lagged server message
599 * when it directly connects to us...
600 * kill the older link at the ghost, rather then
601 * at the second youngest link, assuming it isn't
604 ghost = CurrentTime; /* Mark that it caused a ghost */
605 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
615 return exit_new_server(cptr, sptr, host, timestamp,
616 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s), check H:" :
617 "Leaf-only link %s <- %s(%s), check L:",
618 cli_name(cptr), host,
619 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
623 int killed = a_kills_b_too(LHcptr, sptr);
624 if (active_lh_line < 3)
626 if (exit_client_msg(cptr, LHcptr, &me,
627 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s), check H:" :
628 "Leaf-only link %s <- %s(%s), check L:",
629 cli_name(cptr), host,
630 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
635 ServerStats->is_ref++;
636 if (exit_client(cptr, LHcptr, &me, "I'm a leaf, define HUB") == CPTR_KILLED)
640 * Did we kill the incoming server off already ?
650 * Server is informing about a new server behind
651 * this link. Create REMOTE server structure,
652 * add it to list and propagate word to my other
656 acptr = make_client(cptr, STAT_SERVER);
658 cli_serv(acptr)->prot = prot;
659 cli_serv(acptr)->timestamp = timestamp;
660 cli_hopcount(acptr) = hop;
661 ircd_strncpy(cli_name(acptr), host, HOSTLEN);
662 ircd_strncpy(cli_info(acptr), info, REALLEN);
663 cli_serv(acptr)->up = sptr;
664 cli_serv(acptr)->updown = add_dlink(&(cli_serv(sptr))->down, acptr);
665 /* Use cptr, because we do protocol 9 -> 10 translation
666 for numeric nicks ! */
667 SetServerYXX(cptr, acptr, parv[6]);
669 Count_newremoteserver(UserStats);
670 if (Protocol(acptr) < 10)
671 cli_flags(acptr) |= FLAGS_TS8;
672 add_client_to_list(acptr);
677 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
678 cli_name(sptr), cli_name(acptr));
682 * Old sendto_serv_but_one() call removed because we now need to send
683 * different names to different servers (domain name matching).
685 * Personally, I think this is bogus; it's a feature we don't use here.
688 for (i = 0; i <= HighestFd; i++)
690 if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
691 bcptr == cptr || IsMe(bcptr))
693 if (0 == match(cli_name(&me), cli_name(acptr)))
695 sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s :%s",
696 cli_name(acptr), hop + 1, parv[4], parv[5],
697 NumServCap(acptr), IsHub(acptr) ? "h" : "",
698 IsService(acptr) ? "s" : "", cli_info(acptr));
703 if (IsUnknown(cptr) || IsHandshake(cptr))
706 cli_serv(cptr)->timestamp = timestamp;
707 cli_serv(cptr)->prot = prot;
708 cli_serv(cptr)->ghost = ghost;
709 SetServerYXX(cptr, cptr, parv[6]);
710 if (start_timestamp > OLDEST_TS)
712 Debug((DEBUG_DEBUG, "My start time: %Tu; other's start time: %Tu",
713 cli_serv(&me)->timestamp, start_timestamp));
714 Debug((DEBUG_DEBUG, "Receive time: %Tu; received timestamp: %Tu; "
715 "difference %ld", recv_time, timestamp, timestamp - recv_time));
716 if (feature_bool(FEAT_RELIABLE_CLOCK)) {
717 if (start_timestamp < cli_serv(&me)->timestamp)
718 cli_serv(&me)->timestamp = start_timestamp;
720 cli_serv(cptr)->timestamp = TStime();
722 if (start_timestamp < cli_serv(&me)->timestamp) {
723 sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
724 "%Tu < %Tu", start_timestamp,
725 cli_serv(&me)->timestamp);
726 cli_serv(&me)->timestamp = start_timestamp;
727 TSoffset += timestamp - recv_time;
728 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
729 (int)(timestamp - recv_time));
730 } else if ((start_timestamp > cli_serv(&me)->timestamp) &&
732 cli_serv(cptr)->timestamp = TStime();
734 else if (timestamp != recv_time) {
736 * Equal start times, we have a collision. Let the connected-to
737 * server decide. This assumes leafs issue more than half of the
738 * connection attempts.
741 cli_serv(cptr)->timestamp = TStime();
742 else if (IsHandshake(cptr)) {
743 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
744 (int)(timestamp - recv_time));
745 TSoffset += timestamp - recv_time;
751 ret = server_estab(cptr, aconf);
756 if (feature_bool(FEAT_RELIABLE_CLOCK) &&
757 abs(cli_serv(cptr)->timestamp - recv_time) > 30) {
758 sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
759 "timestamp-clock difference of %Td seconds! "
760 "Used SETTIME to correct this.",
761 timestamp - recv_time);
762 sendcmdto_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(), cli_name(&me));
769 * ms_server - server message handler
771 * parv[0] = sender prefix
772 * parv[1] = servername
774 * parv[3] = start timestamp
775 * parv[4] = link timestamp
776 * parv[5] = major protocol version: P09/P10
777 * parv[parc-1] = serverinfo
779 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
780 * numeric nick mask of this server.
781 * parv[7] = +hs (h == hub, s == service)
783 int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
787 char info[REALLEN + 1];
789 struct Client* acptr;
790 struct Client* bcptr;
791 struct Client* LHcptr = 0;
792 struct ConfItem* aconf = 0;
793 struct ConfItem* lhconf = 0;
796 int active_lh_line = 0;
798 time_t start_timestamp;
799 time_t timestamp = 0;
803 if (IsUserPort(cptr))
804 return exit_client_msg(cptr, cptr, &me,
805 "Cannot connect a server to a user port");
807 recv_time = TStime();
811 return need_more_params(sptr, "SERVER");
812 return exit_client(cptr, cptr, &me, "Need more parameters");
819 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
820 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
822 if (!IsServer(cptr)) /* Don't allow silently connecting a server */
825 if (*parv[7] == '+') {
826 for (ch = parv[7] + 1; *ch; ch++)
837 prot = atoi(parv[5] + 1);
838 if (prot > atoi(MAJOR_PROTOCOL))
839 prot = atoi(MAJOR_PROTOCOL);
841 * Because the previous test is only in 2.10, the following is needed
842 * till all servers are 2.10:
844 if (IsServer(cptr) && prot > Protocol(cptr))
845 prot = Protocol(cptr);
847 start_timestamp = atoi(parv[3]);
848 timestamp = atoi(parv[4]);
849 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
850 host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
851 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
853 return exit_client_msg(cptr, sptr, &me,
854 "Bogus timestamps (%s %s)", parv[3], parv[4]);
856 ircd_strncpy(info, parv[parc - 1], REALLEN);
857 info[REALLEN] = '\0';
858 if (prot < atoi(MINOR_PROTOCOL)) {
859 sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
860 "(%s) from %s", parv[5], cli_name(cptr));
861 return exit_new_server(cptr, sptr, host, timestamp,
862 "Incompatible protocol: %s", parv[5]);
865 * Check for "FRENCH " infection ;-) (actually this should
866 * be replaced with routine to check the hostname syntax in
867 * general). [ This check is still needed, even after the parse
868 * is fixed, because someone can send "SERVER :foo bar " ].
869 * Also, changed to check other "difficult" characters, now
870 * that parse lets all through... --msa
872 if (strlen(host) > HOSTLEN)
873 host[HOSTLEN] = '\0';
874 for (ch = host; *ch; ch++)
875 if (*ch <= ' ' || *ch > '~')
877 if (*ch || !strchr(host, '.')) {
878 sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
879 host, cli_name(cptr));
880 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
886 * A local server introduces a new server behind this link.
887 * Check if this is allowed according L:, H: and Q: lines.
890 return exit_client_msg(cptr, cptr, &me,
891 "No server info specified for %s", host);
893 * See if the newly found server is behind a guaranteed
894 * leaf (L-line). If so, close the link.
896 if ((lhconf = find_conf_byhost(cli_confs(cptr), cli_name(cptr), CONF_LEAF)) &&
897 (!lhconf->port || (hop > lhconf->port)))
900 * L: lines normally come in pairs, here we try to
901 * make sure that the oldest link is squitted, not
905 if (timestamp <= cli_serv(cptr)->timestamp)
906 LHcptr = 0; /* Kill incoming server */
908 LHcptr = cptr; /* Squit ourselfs */
910 else if (!(lhconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_HUB)) ||
911 (lhconf->port && (hop > lhconf->port)))
913 struct Client *ac3ptr;
915 /* Look for net junction causing this: */
916 LHcptr = 0; /* incoming server */
917 if (*parv[5] != 'J') {
918 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up) {
919 if (IsJunction(ac3ptr)) {
928 if (IsUnknown(cptr) || IsHandshake(cptr))
933 * A local link that is still in undefined state wants
934 * to be a SERVER. Check if this is allowed and change
935 * status accordingly...
938 * If there is more then one server on the same machine
939 * that we try to connect to, it could be that the /CONNECT
940 * <mask> caused this connect to be put at the wrong place
941 * in the hashtable. --Run
942 * Same thing for Unknown connections that first send NICK.
944 * Better check if the two strings are (caseless) identical
945 * and not mess with hash internals.
948 if ((!(EmptyString(cli_name(cptr))))
949 && (IsUnknown(cptr) || IsHandshake(cptr))
950 && 0 != ircd_strcmp(cli_name(cptr), host))
951 hChangeClient(cptr, host);
952 ircd_strncpy(cli_name(cptr), host, HOSTLEN);
953 ircd_strncpy(cli_info(cptr), info[0] ? info : cli_name(&me), REALLEN);
954 cli_hopcount(cptr) = hop;
956 /* check connection rules */
957 if (0 != conf_eval_crule(host, CRULE_ALL)) {
958 ServerStats->is_ref++;
959 sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.", cli_name(cptr));
960 return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
962 if (conf_check_server(cptr)) {
963 ++ServerStats->is_ref;
964 sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
965 "from %s.", cli_name(cptr));
966 return exit_client(cptr, cptr, &me, "No C conf lines");
969 host = cli_name(cptr);
973 if (!(aconf = find_conf_byname(cli_confs(cptr), host, CONF_SERVER))) {
974 ++ServerStats->is_ref;
975 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
976 "server %s", cli_name(cptr));
977 return exit_client_msg(cptr, cptr, &me,
978 "Access denied. No conf line for server %s", cli_name(cptr));
980 encr = cli_passwd(cptr);
982 if (*(aconf->passwd) && !!strcmp(aconf->passwd, encr)) {
983 ++ServerStats->is_ref;
984 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
986 return exit_client_msg(cptr, cptr, &me,
987 "No Access (passwd mismatch) %s", cli_name(cptr));
989 memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr)));
991 if (!feature_bool(FEAT_HUB)) {
992 for (i = 0; i <= HighestFd; i++)
993 if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
1002 * We want to find IsConnecting() and IsHandshake() too,
1004 * The second finds collisions with numeric representation of existing
1005 * servers - these shouldn't happen anymore when all upgraded to 2.10.
1008 while ((acptr = FindClient(host)) ||
1009 (parc > 7 && (acptr = FindNServer(parv[6]))))
1012 * This link is trying feed me a server that I already have
1013 * access through another path
1015 * Do not allow Uworld to do this.
1016 * Do not allow servers that are juped.
1017 * Do not allow servers that have older link timestamps
1019 * Do not allow servers that use the same numeric as an existing
1020 * server, but have a different name.
1022 * If my ircd.conf sucks, I can try to connect to myself:
1025 return exit_client_msg(cptr, cptr, &me,
1026 "nick collision with me, check server number in M:? (%s)", host);
1028 * Detect wrong numeric.
1030 if (0 != ircd_strcmp(cli_name(acptr), host))
1032 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
1033 ":SERVER Numeric Collision: %s != %s", cli_name(acptr),
1035 return exit_client_msg(cptr, cptr, &me,
1036 "NUMERIC collision between %s and %s."
1037 " Is your server numeric correct ?", host, cli_name(acptr));
1040 * Kill our try, if we had one.
1042 if (IsConnecting(acptr))
1044 if (!active_lh_line && exit_client(cptr, acptr, &me,
1045 "Just connected via another link") == CPTR_KILLED)
1048 * We can have only ONE 'IsConnecting', 'IsHandshake' or
1049 * 'IsServer', because new 'IsConnecting's are refused to
1050 * the same server if we already had it.
1055 * Avoid other nick collisions...
1056 * This is a doubtfull test though, what else would it be
1057 * when it has a server.name ?
1059 else if (!IsServer(acptr) && !IsHandshake(acptr))
1060 return exit_client_msg(cptr, cptr, &me,
1061 "Nickname %s already exists!", host);
1063 * Our new server might be a juped server,
1064 * or someone trying abuse a second Uworld:
1066 else if (IsServer(acptr) && (0 == ircd_strncmp(cli_info(acptr), "JUPE", 4) ||
1067 find_conf_byhost(cli_confs(cptr), cli_name(acptr), CONF_UWORLD)))
1069 if (!IsServer(sptr))
1070 return exit_client(cptr, sptr, &me, cli_info(acptr));
1071 sendcmdto_one(&me, CMD_WALLOPS, cptr, ":Received :%s SERVER %s "
1072 "from %s !?!", parv[0], parv[1], cli_name(cptr));
1073 return exit_new_server(cptr, sptr, host, timestamp, "%s", cli_info(acptr));
1076 * Of course we find the handshake this link was before :)
1078 else if (IsHandshake(acptr) && acptr == cptr)
1081 * Here we have a server nick collision...
1082 * We don't want to kill the link that was last /connected,
1083 * but we neither want to kill a good (old) link.
1084 * Therefor we kill the second youngest link.
1088 struct Client* c2ptr = 0;
1089 struct Client* c3ptr = acptr;
1090 struct Client* ac2ptr;
1091 struct Client* ac3ptr;
1093 /* Search youngest link: */
1094 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
1095 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
1099 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
1100 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
1103 if (timestamp > cli_serv(c3ptr)->timestamp)
1106 c2ptr = acptr; /* Make sure they differ */
1108 /* Search second youngest link: */
1109 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
1110 if (ac2ptr != c3ptr &&
1111 cli_serv(ac2ptr)->timestamp >
1112 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
1116 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
1117 if (ac2ptr != c3ptr &&
1118 cli_serv(ac2ptr)->timestamp >
1119 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
1122 if (c3ptr && timestamp > (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
1124 /* If timestamps are equal, decide which link to break
1127 if ((c2ptr ? cli_serv(c2ptr)->timestamp : timestamp) ==
1128 (c3ptr ? cli_serv(c3ptr)->timestamp : timestamp))
1136 n2 = cli_name(c2ptr);
1137 n2up = MyConnect(c2ptr) ? cli_name(&me) : cli_name(cli_serv(c2ptr)->up);
1142 n2up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
1146 n3 = cli_name(c3ptr);
1147 n3up = MyConnect(c3ptr) ? cli_name(&me) : cli_name(cli_serv(c3ptr)->up);
1152 n3up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
1154 if (strcmp(n2, n2up) > 0)
1156 if (strcmp(n3, n3up) > 0)
1158 if (strcmp(n3, n2) > 0)
1165 /* Now squit the second youngest link: */
1167 return exit_new_server(cptr, sptr, host, timestamp,
1168 "server %s already exists and is %ld seconds younger.",
1169 host, (long)cli_serv(acptr)->timestamp - (long)timestamp);
1170 else if (cli_from(c2ptr) == cptr || IsServer(sptr))
1172 struct Client *killedptrfrom = cli_from(c2ptr);
1176 * If the L: or H: line also gets rid of this link,
1177 * we sent just one squit.
1179 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
1182 * If breaking the loop here solves the L: or H:
1183 * line problem, we don't squit that.
1185 if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
1190 * If we still have a L: or H: line problem,
1191 * we prefer to squit the new server, solving
1192 * loop and L:/H: line problem with only one squit.
1199 * If the new server was introduced by a server that caused a
1200 * Ghost less then 20 seconds ago, this is probably also
1201 * a Ghost... (20 seconds is more then enough because all
1202 * SERVER messages are at the beginning of a net.burst). --Run
1204 if (CurrentTime - cli_serv(cptr)->ghost < 20)
1206 killedptrfrom = cli_from(acptr);
1207 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
1210 else if (exit_client_msg(cptr, c2ptr, &me,
1211 "Loop <-- %s (new link is %ld seconds younger)", host,
1212 (c3ptr ? (long)cli_serv(c3ptr)->timestamp : timestamp) -
1213 (long)cli_serv(c2ptr)->timestamp) == CPTR_KILLED)
1216 * Did we kill the incoming server off already ?
1218 if (killedptrfrom == cptr)
1225 if (LHcptr && a_kills_b_too(LHcptr, acptr))
1227 if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1236 * We can't believe it is a lagged server message
1237 * when it directly connects to us...
1238 * kill the older link at the ghost, rather then
1239 * at the second youngest link, assuming it isn't
1242 ghost = CurrentTime; /* Mark that it caused a ghost */
1243 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1253 return exit_new_server(cptr, sptr, host, timestamp,
1254 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1255 cli_name(cptr), host,
1256 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1260 int killed = a_kills_b_too(LHcptr, sptr);
1261 if (active_lh_line < 3)
1263 if (exit_client_msg(cptr, LHcptr, &me,
1264 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1265 cli_name(cptr), host,
1266 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1271 ServerStats->is_ref++;
1272 if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1276 * Did we kill the incoming server off already ?
1286 * Server is informing about a new server behind
1287 * this link. Create REMOTE server structure,
1288 * add it to list and propagate word to my other
1292 acptr = make_client(cptr, STAT_SERVER);
1294 cli_serv(acptr)->prot = prot;
1295 cli_serv(acptr)->timestamp = timestamp;
1296 cli_hopcount(acptr) = hop;
1297 ircd_strncpy(cli_name(acptr), host, HOSTLEN);
1298 ircd_strncpy(cli_info(acptr), info, REALLEN);
1299 cli_serv(acptr)->up = sptr;
1300 cli_serv(acptr)->updown = add_dlink(&(cli_serv(sptr))->down, acptr);
1301 /* Use cptr, because we do protocol 9 -> 10 translation
1302 for numeric nicks ! */
1303 SetServerYXX(cptr, acptr, parv[6]);
1305 Count_newremoteserver(UserStats);
1306 if (Protocol(acptr) < 10)
1307 cli_flags(acptr) |= FLAGS_TS8;
1308 add_client_to_list(acptr);
1310 if (*parv[5] == 'J')
1313 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
1314 cli_name(sptr), cli_name(acptr));
1318 * Old sendto_serv_but_one() call removed because we now need to send
1319 * different names to different servers (domain name matching).
1321 for (i = 0; i <= HighestFd; i++)
1323 if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
1324 bcptr == cptr || IsMe(bcptr))
1326 if (0 == match(cli_name(&me), cli_name(acptr)))
1328 sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s :%s",
1329 cli_name(acptr), hop + 1, parv[4], parv[5],
1330 NumServCap(acptr), IsHub(acptr) ? "h" : "",
1331 IsService(acptr) ? "s" : "", cli_info(acptr));
1336 if (IsUnknown(cptr) || IsHandshake(cptr))
1339 cli_serv(cptr)->timestamp = timestamp;
1340 cli_serv(cptr)->prot = prot;
1341 cli_serv(cptr)->ghost = ghost;
1342 SetServerYXX(cptr, cptr, parv[6]);
1343 if (start_timestamp > OLDEST_TS)
1345 Debug((DEBUG_DEBUG, "My start time: %Tu; other's start time: %Tu",
1346 cli_serv(&me)->timestamp, start_timestamp));
1347 Debug((DEBUG_DEBUG, "Receive time: %Tu; received timestamp: %Tu; "
1348 "difference %ld", recv_time, timestamp, timestamp - recv_time));
1349 if (feature_bool(FEAT_RELIABLE_CLOCK)) {
1350 if (start_timestamp < cli_serv(&me)->timestamp)
1351 cli_serv(&me)->timestamp = start_timestamp;
1352 if (IsUnknown(cptr))
1353 cli_serv(cptr)->timestamp = TStime();
1355 if (start_timestamp < cli_serv(&me)->timestamp) {
1356 sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
1357 "%Tu < %Tu", start_timestamp,
1358 cli_serv(&me)->timestamp);
1359 cli_serv(&me)->timestamp = start_timestamp;
1360 TSoffset += timestamp - recv_time;
1361 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
1362 (int)(timestamp - recv_time));
1363 } else if ((start_timestamp > cli_serv(&me)->timestamp) &&
1365 cli_serv(cptr)->timestamp = TStime();
1367 else if (timestamp != recv_time) {
1369 * Equal start times, we have a collision. Let the connected-to
1370 * server decide. This assumes leafs issue more than half of the
1371 * connection attempts.
1373 if (IsUnknown(cptr))
1374 cli_serv(cptr)->timestamp = TStime();
1375 else if (IsHandshake(cptr)) {
1376 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
1377 (int)(timestamp - recv_time));
1378 TSoffset += timestamp - recv_time;
1384 ret = server_estab(cptr, aconf);
1389 if (feature_bool(FEAT_RELIABLE_CLOCK) &&
1390 abs(cli_serv(cptr)->timestamp - recv_time) > 30) {
1391 sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
1392 "timestamp-clock difference of %Td seconds! Used "
1393 "SETTIME to correct this.", timestamp - recv_time);
1394 sendcmdto_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(), cli_name(&me));
1405 * parv[0] = sender prefix
1406 * parv[1] = servername
1407 * parv[2] = hopcount
1408 * parv[3] = start timestamp
1409 * parv[4] = link timestamp
1410 * parv[5] = major protocol version: P09/P10
1411 * parv[parc-1] = serverinfo
1413 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
1414 * numeric nick mask of this server.
1415 * parv[7] = 0 (not used yet, mandatory unsigned int after u2.10.06)
1417 int m_server(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1421 char info[REALLEN + 1];
1423 struct Client* acptr;
1424 struct Client* bcptr;
1425 struct Client* LHcptr = 0;
1426 struct ConfItem* aconf = 0;
1427 struct ConfItem* lhconf = 0;
1428 struct Jupe* ajupe = 0;
1431 int active_lh_line = 0;
1432 unsigned short prot;
1433 time_t start_timestamp;
1434 time_t timestamp = 0;
1440 sendto_one(cptr, err_str(ERR_ALREADYREGISTRED), me.name, parv[0]); /* XXX DEAD */
1444 if (IsUserPort(cptr))
1445 return exit_client_msg(cptr, cptr, &me,
1446 "You cannot connect a server to a user port; connect to %s port %u",
1447 me.name, server_port);
1449 recv_time = TStime();
1453 return need_more_params(sptr, "SERVER");
1454 return exit_client(cptr, cptr, &me, "Need more parameters");
1460 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
1461 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
1463 if (!IsServer(cptr)) /* Don't allow silently connecting a server */
1466 prot = atoi(parv[5] + 1);
1467 if (prot > atoi(MAJOR_PROTOCOL))
1468 prot = atoi(MAJOR_PROTOCOL);
1470 * Because the previous test is only in 2.10, the following is needed
1471 * till all servers are 2.10:
1473 if (IsServer(cptr) && prot > Protocol(cptr))
1474 prot = Protocol(cptr);
1475 hop = atoi(parv[2]);
1476 start_timestamp = atoi(parv[3]);
1477 timestamp = atoi(parv[4]);
1478 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age " TIME_T_FMT " ("
1479 TIME_T_FMT ")", host, parv[4], start_timestamp, me.serv->timestamp));
1480 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
1482 return exit_client_msg(cptr, sptr, &me,
1483 "Bogus timestamps (%s %s)", parv[3], parv[4]);
1485 ircd_strncpy(info, parv[parc - 1], REALLEN);
1486 info[REALLEN] = '\0';
1487 if (prot < atoi(MINOR_PROTOCOL)) {
1488 sendto_ops("Got incompatible protocol version (%s) from %s", /* XXX DEAD */
1489 parv[5], cptr->name);
1490 return exit_new_server(cptr, sptr, host, timestamp,
1491 "Incompatible protocol: %s", parv[5]);
1494 * Check for "FRENCH " infection ;-) (actually this should
1495 * be replaced with routine to check the hostname syntax in
1496 * general). [ This check is still needed, even after the parse
1497 * is fixed, because someone can send "SERVER :foo bar " ].
1498 * Also, changed to check other "difficult" characters, now
1499 * that parse lets all through... --msa
1501 if (strlen(host) > HOSTLEN)
1502 host[HOSTLEN] = '\0';
1503 for (ch = host; *ch; ch++)
1504 if (*ch <= ' ' || *ch > '~')
1506 if (*ch || !strchr(host, '.'))
1508 sendto_ops("Bogus server name (%s) from %s", host, cptr->name); /* XXX DEAD */
1509 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
1515 * A local server introduces a new server behind this link.
1516 * Check if this is allowed according L:, H: and Q: lines.
1518 if (info[0] == '\0')
1519 return exit_client_msg(cptr, cptr, &me,
1520 "No server info specified for %s", host);
1522 * See if the newly found server is behind a guaranteed
1523 * leaf (L-line). If so, close the link.
1525 if ((lhconf = find_conf_byhost(cptr->confs, cptr->name, CONF_LEAF)) &&
1526 (!lhconf->port || (hop > lhconf->port)))
1529 * L: lines normally come in pairs, here we try to
1530 * make sure that the oldest link is squitted, not
1534 if (timestamp <= cptr->serv->timestamp)
1535 LHcptr = 0; /* Kill incoming server */
1537 LHcptr = cptr; /* Squit ourselfs */
1539 else if (!(lhconf = find_conf_byname(cptr->confs, cptr->name, CONF_HUB)) ||
1540 (lhconf->port && (hop > lhconf->port)))
1542 struct Client *ac3ptr;
1544 /* Look for net junction causing this: */
1545 LHcptr = 0; /* incoming server */
1546 if (*parv[5] != 'J') {
1547 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up) {
1548 if (IsJunction(ac3ptr)) {
1557 if (IsUnknown(cptr) || IsHandshake(cptr))
1562 * A local link that is still in undefined state wants
1563 * to be a SERVER. Check if this is allowed and change
1564 * status accordingly...
1567 * If there is more then one server on the same machine
1568 * that we try to connect to, it could be that the /CONNECT
1569 * <mask> caused this connect to be put at the wrong place
1570 * in the hashtable. --Run
1571 * Same thing for Unknown connections that first send NICK.
1573 * Better check if the two strings are (caseless) identical
1574 * and not mess with hash internals.
1577 if ((!(EmptyString(cptr->name)))
1578 && (IsUnknown(cptr) || IsHandshake(cptr))
1579 && 0 != ircd_strcmp(cptr->name, host))
1580 hChangeClient(cptr, host);
1581 ircd_strncpy(cptr->name, host, HOSTLEN);
1582 ircd_strncpy(cptr->info, info[0] ? info : me.name, REALLEN);
1583 cptr->hopcount = hop;
1585 /* check connection rules */
1586 if (0 != conf_eval_crule(host, CRULE_ALL)) {
1587 ServerStats->is_ref++;
1588 sendto_ops("Refused connection from %s.", cptr->name); /* XXX DEAD */
1589 return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
1591 if (conf_check_server(cptr)) {
1592 ++ServerStats->is_ref;
1593 sendto_ops("Received unauthorized connection from %s.", cptr->name); /* XXX DEAD */
1594 return exit_client(cptr, cptr, &me, "No C/N conf lines");
1601 if (!(aconf = find_conf_byname(cptr->confs, host, CONF_SERVER))) {
1602 ++ServerStats->is_ref;
1604 sendto_ops("Access denied. No conf line for server %s", cptr->name); /* XXX DEAD */
1605 return exit_client_msg(cptr, cptr, &me,
1606 "Access denied. No conf line for server %s", cptr->name);
1608 sendto_ops("General C/N: line active: No line for server %s", cptr->name); /* XXX DEAD */
1610 find_conf_byname(cptr->confs, "general.undernet.org", CONF_SERVER);
1612 sendto_ops("Neither C/N lines for server %s nor " /* XXX DEAD */
1613 "\"general.undernet.org\"", cptr->name);
1614 return exit_client_msg(cptr, cptr, &me,
1615 "No C/N lines for server %s", cptr->name);
1617 #endif /* GODMODE */
1619 #ifdef CRYPT_LINK_PASSWORD
1620 /* passwd may be NULL. Head it off at the pass... */
1623 encr = ircd_crypt(cptr->passwd, aconf->passwd);
1628 encr = cptr->passwd;
1629 #endif /* CRYPT_LINK_PASSWORD */
1631 if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
1632 ++ServerStats->is_ref;
1633 sendto_ops("Access denied (passwd mismatch) %s", cptr->name); /* XXX DEAD */
1634 return exit_client_msg(cptr, cptr, &me,
1635 "No Access (passwd mismatch) %s", cptr->name);
1637 #endif /* not GODMODE */
1638 memset(cptr->passwd, 0, sizeof(cptr->passwd));
1641 for (i = 0; i <= HighestFd; i++)
1642 if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
1651 * We want to find IsConnecting() and IsHandshake() too,
1653 * The second finds collisions with numeric representation of existing
1654 * servers - these shouldn't happen anymore when all upgraded to 2.10.
1657 while ((acptr = FindClient(host)) ||
1658 (parc > 7 && (acptr = FindNServer(parv[6]))))
1661 * This link is trying feed me a server that I already have
1662 * access through another path
1664 * Do not allow Uworld to do this.
1665 * Do not allow servers that are juped.
1666 * Do not allow servers that have older link timestamps
1668 * Do not allow servers that use the same numeric as an existing
1669 * server, but have a different name.
1671 * If my ircd.conf sucks, I can try to connect to myself:
1674 return exit_client_msg(cptr, cptr, &me,
1675 "nick collision with me, check server number in M:? (%s)", host);
1677 * Detect wrong numeric.
1679 if (0 != ircd_strcmp(acptr->name, host))
1681 sendto_serv_butone(cptr, /* XXX DEAD */
1682 ":%s WALLOPS :SERVER Numeric Collision: %s != %s",
1683 me.name, acptr->name, host);
1684 return exit_client_msg(cptr, cptr, &me,
1685 "NUMERIC collision between %s and %s."
1686 " Is your server numeric correct ?", host, acptr->name);
1689 * Kill our try, if we had one.
1691 if (IsConnecting(acptr))
1693 if (!active_lh_line && exit_client(cptr, acptr, &me,
1694 "Just connected via another link") == CPTR_KILLED)
1697 * We can have only ONE 'IsConnecting', 'IsHandshake' or
1698 * 'IsServer', because new 'IsConnecting's are refused to
1699 * the same server if we already had it.
1704 * Avoid other nick collisions...
1705 * This is a doubtfull test though, what else would it be
1706 * when it has a server.name ?
1708 else if (!IsServer(acptr) && !IsHandshake(acptr))
1709 return exit_client_msg(cptr, cptr, &me,
1710 "Nickname %s already exists!", host);
1712 * Our new server might be a juped server,
1713 * or someone trying abuse a second Uworld:
1715 else if (IsServer(acptr) && (0 == ircd_strncmp(acptr->info, "JUPE", 4) ||
1716 find_conf_byhost(cptr->confs, acptr->name, CONF_UWORLD)))
1718 if (!IsServer(sptr))
1719 return exit_client(cptr, sptr, &me, acptr->info);
1720 sendto_one(cptr, ":%s WALLOPS :Received :%s SERVER %s from %s !?!", /* XXX DEAD */
1721 me.name, parv[0], parv[1], cptr->name);
1722 return exit_new_server(cptr, sptr, host, timestamp, "%s", acptr->info);
1725 * Of course we find the handshake this link was before :)
1727 else if (IsHandshake(acptr) && acptr == cptr)
1730 * Here we have a server nick collision...
1731 * We don't want to kill the link that was last /connected,
1732 * but we neither want to kill a good (old) link.
1733 * Therefor we kill the second youngest link.
1737 struct Client* c2ptr = 0;
1738 struct Client* c3ptr = acptr;
1739 struct Client* ac2ptr;
1740 struct Client* ac3ptr;
1742 /* Search youngest link: */
1743 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1744 if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1748 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1749 if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1752 if (timestamp > c3ptr->serv->timestamp)
1755 c2ptr = acptr; /* Make sure they differ */
1757 /* Search second youngest link: */
1758 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1759 if (ac2ptr != c3ptr &&
1760 ac2ptr->serv->timestamp >
1761 (c2ptr ? c2ptr->serv->timestamp : timestamp))
1765 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1766 if (ac2ptr != c3ptr &&
1767 ac2ptr->serv->timestamp >
1768 (c2ptr ? c2ptr->serv->timestamp : timestamp))
1771 if (c3ptr && timestamp > (c2ptr ? c2ptr->serv->timestamp : timestamp))
1773 /* If timestamps are equal, decide which link to break
1776 if ((c2ptr ? c2ptr->serv->timestamp : timestamp) ==
1777 (c3ptr ? c3ptr->serv->timestamp : timestamp))
1786 n2up = MyConnect(c2ptr) ? me.name : c2ptr->serv->up->name;
1791 n2up = IsServer(sptr) ? sptr->name : me.name;
1796 n3up = MyConnect(c3ptr) ? me.name : c3ptr->serv->up->name;
1801 n3up = IsServer(sptr) ? sptr->name : me.name;
1803 if (strcmp(n2, n2up) > 0)
1805 if (strcmp(n3, n3up) > 0)
1807 if (strcmp(n3, n2) > 0)
1814 /* Now squit the second youngest link: */
1816 return exit_new_server(cptr, sptr, host, timestamp,
1817 "server %s already exists and is %ld seconds younger.",
1818 host, (long)acptr->serv->timestamp - (long)timestamp);
1819 else if (c2ptr->from == cptr || IsServer(sptr))
1821 struct Client *killedptrfrom = c2ptr->from;
1825 * If the L: or H: line also gets rid of this link,
1826 * we sent just one squit.
1828 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
1831 * If breaking the loop here solves the L: or H:
1832 * line problem, we don't squit that.
1834 if (c2ptr->from == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
1839 * If we still have a L: or H: line problem,
1840 * we prefer to squit the new server, solving
1841 * loop and L:/H: line problem with only one squit.
1848 * If the new server was introduced by a server that caused a
1849 * Ghost less then 20 seconds ago, this is probably also
1850 * a Ghost... (20 seconds is more then enough because all
1851 * SERVER messages are at the beginning of a net.burst). --Run
1853 if (CurrentTime - cptr->serv->ghost < 20)
1855 killedptrfrom = acptr->from;
1856 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
1859 else if (exit_client_msg(cptr, c2ptr, &me,
1860 "Loop <-- %s (new link is %ld seconds younger)", host,
1861 (c3ptr ? (long)c3ptr->serv->timestamp : timestamp) -
1862 (long)c2ptr->serv->timestamp) == CPTR_KILLED)
1865 * Did we kill the incoming server off already ?
1867 if (killedptrfrom == cptr)
1874 if (LHcptr && a_kills_b_too(LHcptr, acptr))
1876 if (acptr->from == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1885 * We can't believe it is a lagged server message
1886 * when it directly connects to us...
1887 * kill the older link at the ghost, rather then
1888 * at the second youngest link, assuming it isn't
1891 ghost = CurrentTime; /* Mark that it caused a ghost */
1892 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1902 return exit_new_server(cptr, sptr, host, timestamp,
1903 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1905 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1909 int killed = a_kills_b_too(LHcptr, sptr);
1910 if (active_lh_line < 3)
1912 if (exit_client_msg(cptr, LHcptr, &me,
1913 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1915 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1920 ServerStats->is_ref++;
1921 if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1925 * Did we kill the incoming server off already ?
1935 * Server is informing about a new server behind
1936 * this link. Create REMOTE server structure,
1937 * add it to list and propagate word to my other
1941 acptr = make_client(cptr, STAT_SERVER);
1943 acptr->serv->prot = prot;
1944 acptr->serv->timestamp = timestamp;
1945 acptr->hopcount = hop;
1946 ircd_strncpy(acptr->name, host, HOSTLEN);
1947 ircd_strncpy(acptr->info, info, REALLEN);
1948 acptr->serv->up = sptr;
1949 acptr->serv->updown = add_dlink(&sptr->serv->down, acptr);
1950 /* Use cptr, because we do protocol 9 -> 10 translation
1951 for numeric nicks ! */
1952 SetServerYXX(cptr, acptr, parv[6]);
1954 Count_newremoteserver(UserStats);
1955 if (Protocol(acptr) < 10)
1956 acptr->flags |= FLAGS_TS8;
1957 add_client_to_list(acptr);
1959 if (*parv[5] == 'J')
1962 sendto_op_mask(SNO_NETWORK, "Net junction: %s %s", /* XXX DEAD */
1963 sptr->name, acptr->name);
1967 * Old sendto_serv_but_one() call removed because we now need to send
1968 * different names to different servers (domain name matching).
1970 for (i = 0; i <= HighestFd; i++)
1972 if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
1973 bcptr == cptr || IsMe(bcptr))
1975 if (0 == match(me.name, acptr->name))
1977 sendto_one(bcptr, "%s " TOK_SERVER " %s %d 0 %s %s %s%s 0 :%s", /* XXX DEAD */
1978 NumServ(sptr), acptr->name, hop + 1, parv[4], parv[5],
1979 NumServCap(acptr), acptr->info);
1984 if (IsUnknown(cptr) || IsHandshake(cptr))
1987 cptr->serv->timestamp = timestamp;
1988 cptr->serv->prot = prot;
1989 cptr->serv->ghost = ghost;
1990 SetServerYXX(cptr, cptr, parv[6]);
1991 if (start_timestamp > OLDEST_TS)
1993 #ifndef RELIABLE_CLOCK
1995 sendto_ops("Debug: my start time: " TIME_T_FMT " ; others start time: " /* XXX DEAD */
1996 TIME_T_FMT, me.serv->timestamp, start_timestamp);
1997 sendto_ops("Debug: receive time: " TIME_T_FMT " ; received timestamp: " /* XXX DEAD */
1998 TIME_T_FMT " ; difference %ld",
1999 recv_time, timestamp, timestamp - recv_time);
2001 if (start_timestamp < me.serv->timestamp)
2003 sendto_ops("got earlier start time: " TIME_T_FMT " < " TIME_T_FMT, /* XXX DEAD */
2004 start_timestamp, me.serv->timestamp);
2005 me.serv->timestamp = start_timestamp;
2006 TSoffset += timestamp - recv_time;
2007 sendto_ops("clock adjusted by adding %d", (int)(timestamp - recv_time)); /* XXX DEAD */
2009 else if ((start_timestamp > me.serv->timestamp) && IsUnknown(cptr))
2010 cptr->serv->timestamp = TStime();
2012 else if (timestamp != recv_time)
2015 * Equal start times, we have a collision. Let the connected-to server
2016 * decide. This assumes leafs issue more than half of the connection
2019 if (IsUnknown(cptr))
2020 cptr->serv->timestamp = TStime();
2021 else if (IsHandshake(cptr))
2023 sendto_ops("clock adjusted by adding %d", /* XXX DEAD */
2024 (int)(timestamp - recv_time));
2025 TSoffset += timestamp - recv_time;
2028 #else /* RELIABLE CLOCK IS TRUE, we _always_ use our own clock */
2029 if (start_timestamp < me.serv->timestamp)
2030 me.serv->timestamp = start_timestamp;
2031 if (IsUnknown(cptr))
2032 cptr->serv->timestamp = TStime();
2036 ret = server_estab(cptr, aconf); /* XXX DEAD */
2040 #ifdef RELIABLE_CLOCK
2041 if (abs(cptr->serv->timestamp - recv_time) > 30)
2043 sendto_ops("Connected to a net with a timestamp-clock" /* XXX DEAD */
2044 " difference of " STIME_T_FMT " seconds! Used SETTIME to correct"
2045 " this.", timestamp - recv_time);
2046 sendto_one(cptr, ":%s SETTIME " TIME_T_FMT " :%s", /* XXX DEAD */
2047 me.name, TStime(), me.name);