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_prio_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(),
770 * ms_server - server message handler
772 * parv[0] = sender prefix
773 * parv[1] = servername
775 * parv[3] = start timestamp
776 * parv[4] = link timestamp
777 * parv[5] = major protocol version: P09/P10
778 * parv[parc-1] = serverinfo
780 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
781 * numeric nick mask of this server.
782 * parv[7] = +hs (h == hub, s == service)
784 int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
788 char info[REALLEN + 1];
790 struct Client* acptr;
791 struct Client* bcptr;
792 struct Client* LHcptr = 0;
793 struct ConfItem* aconf = 0;
794 struct ConfItem* lhconf = 0;
797 int active_lh_line = 0;
799 time_t start_timestamp;
800 time_t timestamp = 0;
804 if (IsUserPort(cptr))
805 return exit_client_msg(cptr, cptr, &me,
806 "Cannot connect a server to a user port");
808 recv_time = TStime();
812 return need_more_params(sptr, "SERVER");
813 return exit_client(cptr, cptr, &me, "Need more parameters");
820 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
821 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
823 if (!IsServer(cptr)) /* Don't allow silently connecting a server */
826 if (*parv[7] == '+') {
827 for (ch = parv[7] + 1; *ch; ch++)
838 prot = atoi(parv[5] + 1);
839 if (prot > atoi(MAJOR_PROTOCOL))
840 prot = atoi(MAJOR_PROTOCOL);
842 * Because the previous test is only in 2.10, the following is needed
843 * till all servers are 2.10:
845 if (IsServer(cptr) && prot > Protocol(cptr))
846 prot = Protocol(cptr);
848 start_timestamp = atoi(parv[3]);
849 timestamp = atoi(parv[4]);
850 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
851 host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
852 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
854 return exit_client_msg(cptr, sptr, &me,
855 "Bogus timestamps (%s %s)", parv[3], parv[4]);
857 ircd_strncpy(info, parv[parc - 1], REALLEN);
858 info[REALLEN] = '\0';
859 if (prot < atoi(MINOR_PROTOCOL)) {
860 sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
861 "(%s) from %s", parv[5], cli_name(cptr));
862 return exit_new_server(cptr, sptr, host, timestamp,
863 "Incompatible protocol: %s", parv[5]);
866 * Check for "FRENCH " infection ;-) (actually this should
867 * be replaced with routine to check the hostname syntax in
868 * general). [ This check is still needed, even after the parse
869 * is fixed, because someone can send "SERVER :foo bar " ].
870 * Also, changed to check other "difficult" characters, now
871 * that parse lets all through... --msa
873 if (strlen(host) > HOSTLEN)
874 host[HOSTLEN] = '\0';
875 for (ch = host; *ch; ch++)
876 if (*ch <= ' ' || *ch > '~')
878 if (*ch || !strchr(host, '.')) {
879 sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
880 host, cli_name(cptr));
881 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
887 * A local server introduces a new server behind this link.
888 * Check if this is allowed according L:, H: and Q: lines.
891 return exit_client_msg(cptr, cptr, &me,
892 "No server info specified for %s", host);
894 * See if the newly found server is behind a guaranteed
895 * leaf (L-line). If so, close the link.
897 if ((lhconf = find_conf_byhost(cli_confs(cptr), cli_name(cptr), CONF_LEAF)) &&
898 (!lhconf->port || (hop > lhconf->port)))
901 * L: lines normally come in pairs, here we try to
902 * make sure that the oldest link is squitted, not
906 if (timestamp <= cli_serv(cptr)->timestamp)
907 LHcptr = 0; /* Kill incoming server */
909 LHcptr = cptr; /* Squit ourselfs */
911 else if (!(lhconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_HUB)) ||
912 (lhconf->port && (hop > lhconf->port)))
914 struct Client *ac3ptr;
916 /* Look for net junction causing this: */
917 LHcptr = 0; /* incoming server */
918 if (*parv[5] != 'J') {
919 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up) {
920 if (IsJunction(ac3ptr)) {
929 if (IsUnknown(cptr) || IsHandshake(cptr))
934 * A local link that is still in undefined state wants
935 * to be a SERVER. Check if this is allowed and change
936 * status accordingly...
939 * If there is more then one server on the same machine
940 * that we try to connect to, it could be that the /CONNECT
941 * <mask> caused this connect to be put at the wrong place
942 * in the hashtable. --Run
943 * Same thing for Unknown connections that first send NICK.
945 * Better check if the two strings are (caseless) identical
946 * and not mess with hash internals.
949 if ((!(EmptyString(cli_name(cptr))))
950 && (IsUnknown(cptr) || IsHandshake(cptr))
951 && 0 != ircd_strcmp(cli_name(cptr), host))
952 hChangeClient(cptr, host);
953 ircd_strncpy(cli_name(cptr), host, HOSTLEN);
954 ircd_strncpy(cli_info(cptr), info[0] ? info : cli_name(&me), REALLEN);
955 cli_hopcount(cptr) = hop;
957 /* check connection rules */
958 if (0 != conf_eval_crule(host, CRULE_ALL)) {
959 ServerStats->is_ref++;
960 sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.", cli_name(cptr));
961 return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
963 if (conf_check_server(cptr)) {
964 ++ServerStats->is_ref;
965 sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
966 "from %s.", cli_name(cptr));
967 return exit_client(cptr, cptr, &me, "No C conf lines");
970 host = cli_name(cptr);
974 if (!(aconf = find_conf_byname(cli_confs(cptr), host, CONF_SERVER))) {
975 ++ServerStats->is_ref;
976 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
977 "server %s", cli_name(cptr));
978 return exit_client_msg(cptr, cptr, &me,
979 "Access denied. No conf line for server %s", cli_name(cptr));
981 encr = cli_passwd(cptr);
983 if (*(aconf->passwd) && !!strcmp(aconf->passwd, encr)) {
984 ++ServerStats->is_ref;
985 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
987 return exit_client_msg(cptr, cptr, &me,
988 "No Access (passwd mismatch) %s", cli_name(cptr));
990 memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr)));
992 if (!feature_bool(FEAT_HUB)) {
993 for (i = 0; i <= HighestFd; i++)
994 if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
1003 * We want to find IsConnecting() and IsHandshake() too,
1005 * The second finds collisions with numeric representation of existing
1006 * servers - these shouldn't happen anymore when all upgraded to 2.10.
1009 while ((acptr = FindClient(host)) ||
1010 (parc > 7 && (acptr = FindNServer(parv[6]))))
1013 * This link is trying feed me a server that I already have
1014 * access through another path
1016 * Do not allow Uworld to do this.
1017 * Do not allow servers that are juped.
1018 * Do not allow servers that have older link timestamps
1020 * Do not allow servers that use the same numeric as an existing
1021 * server, but have a different name.
1023 * If my ircd.conf sucks, I can try to connect to myself:
1026 return exit_client_msg(cptr, cptr, &me,
1027 "nick collision with me, check server number in M:? (%s)", host);
1029 * Detect wrong numeric.
1031 if (0 != ircd_strcmp(cli_name(acptr), host))
1033 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
1034 ":SERVER Numeric Collision: %s != %s", cli_name(acptr),
1036 return exit_client_msg(cptr, cptr, &me,
1037 "NUMERIC collision between %s and %s."
1038 " Is your server numeric correct ?", host, cli_name(acptr));
1041 * Kill our try, if we had one.
1043 if (IsConnecting(acptr))
1045 if (!active_lh_line && exit_client(cptr, acptr, &me,
1046 "Just connected via another link") == CPTR_KILLED)
1049 * We can have only ONE 'IsConnecting', 'IsHandshake' or
1050 * 'IsServer', because new 'IsConnecting's are refused to
1051 * the same server if we already had it.
1056 * Avoid other nick collisions...
1057 * This is a doubtfull test though, what else would it be
1058 * when it has a server.name ?
1060 else if (!IsServer(acptr) && !IsHandshake(acptr))
1061 return exit_client_msg(cptr, cptr, &me,
1062 "Nickname %s already exists!", host);
1064 * Our new server might be a juped server,
1065 * or someone trying abuse a second Uworld:
1067 else if (IsServer(acptr) && (0 == ircd_strncmp(cli_info(acptr), "JUPE", 4) ||
1068 find_conf_byhost(cli_confs(cptr), cli_name(acptr), CONF_UWORLD)))
1070 if (!IsServer(sptr))
1071 return exit_client(cptr, sptr, &me, cli_info(acptr));
1072 sendcmdto_one(&me, CMD_WALLOPS, cptr, ":Received :%s SERVER %s "
1073 "from %s !?!", parv[0], parv[1], cli_name(cptr));
1074 return exit_new_server(cptr, sptr, host, timestamp, "%s", cli_info(acptr));
1077 * Of course we find the handshake this link was before :)
1079 else if (IsHandshake(acptr) && acptr == cptr)
1082 * Here we have a server nick collision...
1083 * We don't want to kill the link that was last /connected,
1084 * but we neither want to kill a good (old) link.
1085 * Therefor we kill the second youngest link.
1089 struct Client* c2ptr = 0;
1090 struct Client* c3ptr = acptr;
1091 struct Client* ac2ptr;
1092 struct Client* ac3ptr;
1094 /* Search youngest link: */
1095 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
1096 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
1100 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
1101 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
1104 if (timestamp > cli_serv(c3ptr)->timestamp)
1107 c2ptr = acptr; /* Make sure they differ */
1109 /* Search second youngest link: */
1110 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
1111 if (ac2ptr != c3ptr &&
1112 cli_serv(ac2ptr)->timestamp >
1113 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
1117 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
1118 if (ac2ptr != c3ptr &&
1119 cli_serv(ac2ptr)->timestamp >
1120 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
1123 if (c3ptr && timestamp > (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
1125 /* If timestamps are equal, decide which link to break
1128 if ((c2ptr ? cli_serv(c2ptr)->timestamp : timestamp) ==
1129 (c3ptr ? cli_serv(c3ptr)->timestamp : timestamp))
1137 n2 = cli_name(c2ptr);
1138 n2up = MyConnect(c2ptr) ? cli_name(&me) : cli_name(cli_serv(c2ptr)->up);
1143 n2up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
1147 n3 = cli_name(c3ptr);
1148 n3up = MyConnect(c3ptr) ? cli_name(&me) : cli_name(cli_serv(c3ptr)->up);
1153 n3up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
1155 if (strcmp(n2, n2up) > 0)
1157 if (strcmp(n3, n3up) > 0)
1159 if (strcmp(n3, n2) > 0)
1166 /* Now squit the second youngest link: */
1168 return exit_new_server(cptr, sptr, host, timestamp,
1169 "server %s already exists and is %ld seconds younger.",
1170 host, (long)cli_serv(acptr)->timestamp - (long)timestamp);
1171 else if (cli_from(c2ptr) == cptr || IsServer(sptr))
1173 struct Client *killedptrfrom = cli_from(c2ptr);
1177 * If the L: or H: line also gets rid of this link,
1178 * we sent just one squit.
1180 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
1183 * If breaking the loop here solves the L: or H:
1184 * line problem, we don't squit that.
1186 if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
1191 * If we still have a L: or H: line problem,
1192 * we prefer to squit the new server, solving
1193 * loop and L:/H: line problem with only one squit.
1200 * If the new server was introduced by a server that caused a
1201 * Ghost less then 20 seconds ago, this is probably also
1202 * a Ghost... (20 seconds is more then enough because all
1203 * SERVER messages are at the beginning of a net.burst). --Run
1205 if (CurrentTime - cli_serv(cptr)->ghost < 20)
1207 killedptrfrom = cli_from(acptr);
1208 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
1211 else if (exit_client_msg(cptr, c2ptr, &me,
1212 "Loop <-- %s (new link is %ld seconds younger)", host,
1213 (c3ptr ? (long)cli_serv(c3ptr)->timestamp : timestamp) -
1214 (long)cli_serv(c2ptr)->timestamp) == CPTR_KILLED)
1217 * Did we kill the incoming server off already ?
1219 if (killedptrfrom == cptr)
1226 if (LHcptr && a_kills_b_too(LHcptr, acptr))
1228 if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1237 * We can't believe it is a lagged server message
1238 * when it directly connects to us...
1239 * kill the older link at the ghost, rather then
1240 * at the second youngest link, assuming it isn't
1243 ghost = CurrentTime; /* Mark that it caused a ghost */
1244 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1254 return exit_new_server(cptr, sptr, host, timestamp,
1255 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1256 cli_name(cptr), host,
1257 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1261 int killed = a_kills_b_too(LHcptr, sptr);
1262 if (active_lh_line < 3)
1264 if (exit_client_msg(cptr, LHcptr, &me,
1265 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1266 cli_name(cptr), host,
1267 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1272 ServerStats->is_ref++;
1273 if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1277 * Did we kill the incoming server off already ?
1287 * Server is informing about a new server behind
1288 * this link. Create REMOTE server structure,
1289 * add it to list and propagate word to my other
1293 acptr = make_client(cptr, STAT_SERVER);
1295 cli_serv(acptr)->prot = prot;
1296 cli_serv(acptr)->timestamp = timestamp;
1297 cli_hopcount(acptr) = hop;
1298 ircd_strncpy(cli_name(acptr), host, HOSTLEN);
1299 ircd_strncpy(cli_info(acptr), info, REALLEN);
1300 cli_serv(acptr)->up = sptr;
1301 cli_serv(acptr)->updown = add_dlink(&(cli_serv(sptr))->down, acptr);
1302 /* Use cptr, because we do protocol 9 -> 10 translation
1303 for numeric nicks ! */
1304 SetServerYXX(cptr, acptr, parv[6]);
1306 Count_newremoteserver(UserStats);
1307 if (Protocol(acptr) < 10)
1308 cli_flags(acptr) |= FLAGS_TS8;
1309 add_client_to_list(acptr);
1311 if (*parv[5] == 'J')
1314 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
1315 cli_name(sptr), cli_name(acptr));
1319 * Old sendto_serv_but_one() call removed because we now need to send
1320 * different names to different servers (domain name matching).
1322 for (i = 0; i <= HighestFd; i++)
1324 if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
1325 bcptr == cptr || IsMe(bcptr))
1327 if (0 == match(cli_name(&me), cli_name(acptr)))
1329 sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s :%s",
1330 cli_name(acptr), hop + 1, parv[4], parv[5],
1331 NumServCap(acptr), IsHub(acptr) ? "h" : "",
1332 IsService(acptr) ? "s" : "", cli_info(acptr));
1337 if (IsUnknown(cptr) || IsHandshake(cptr))
1340 cli_serv(cptr)->timestamp = timestamp;
1341 cli_serv(cptr)->prot = prot;
1342 cli_serv(cptr)->ghost = ghost;
1343 SetServerYXX(cptr, cptr, parv[6]);
1344 if (start_timestamp > OLDEST_TS)
1346 Debug((DEBUG_DEBUG, "My start time: %Tu; other's start time: %Tu",
1347 cli_serv(&me)->timestamp, start_timestamp));
1348 Debug((DEBUG_DEBUG, "Receive time: %Tu; received timestamp: %Tu; "
1349 "difference %ld", recv_time, timestamp, timestamp - recv_time));
1350 if (feature_bool(FEAT_RELIABLE_CLOCK)) {
1351 if (start_timestamp < cli_serv(&me)->timestamp)
1352 cli_serv(&me)->timestamp = start_timestamp;
1353 if (IsUnknown(cptr))
1354 cli_serv(cptr)->timestamp = TStime();
1356 if (start_timestamp < cli_serv(&me)->timestamp) {
1357 sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
1358 "%Tu < %Tu", start_timestamp,
1359 cli_serv(&me)->timestamp);
1360 cli_serv(&me)->timestamp = start_timestamp;
1361 TSoffset += timestamp - recv_time;
1362 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
1363 (int)(timestamp - recv_time));
1364 } else if ((start_timestamp > cli_serv(&me)->timestamp) &&
1366 cli_serv(cptr)->timestamp = TStime();
1368 else if (timestamp != recv_time) {
1370 * Equal start times, we have a collision. Let the connected-to
1371 * server decide. This assumes leafs issue more than half of the
1372 * connection attempts.
1374 if (IsUnknown(cptr))
1375 cli_serv(cptr)->timestamp = TStime();
1376 else if (IsHandshake(cptr)) {
1377 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
1378 (int)(timestamp - recv_time));
1379 TSoffset += timestamp - recv_time;
1385 ret = server_estab(cptr, aconf);
1390 if (feature_bool(FEAT_RELIABLE_CLOCK) &&
1391 abs(cli_serv(cptr)->timestamp - recv_time) > 30) {
1392 sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
1393 "timestamp-clock difference of %Td seconds! Used "
1394 "SETTIME to correct this.", timestamp - recv_time);
1395 sendcmdto_prio_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(),
1407 * parv[0] = sender prefix
1408 * parv[1] = servername
1409 * parv[2] = hopcount
1410 * parv[3] = start timestamp
1411 * parv[4] = link timestamp
1412 * parv[5] = major protocol version: P09/P10
1413 * parv[parc-1] = serverinfo
1415 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
1416 * numeric nick mask of this server.
1417 * parv[7] = 0 (not used yet, mandatory unsigned int after u2.10.06)
1419 int m_server(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1423 char info[REALLEN + 1];
1425 struct Client* acptr;
1426 struct Client* bcptr;
1427 struct Client* LHcptr = 0;
1428 struct ConfItem* aconf = 0;
1429 struct ConfItem* lhconf = 0;
1430 struct Jupe* ajupe = 0;
1433 int active_lh_line = 0;
1434 unsigned short prot;
1435 time_t start_timestamp;
1436 time_t timestamp = 0;
1442 sendto_one(cptr, err_str(ERR_ALREADYREGISTRED), me.name, parv[0]); /* XXX DEAD */
1446 if (IsUserPort(cptr))
1447 return exit_client_msg(cptr, cptr, &me,
1448 "You cannot connect a server to a user port; connect to %s port %u",
1449 me.name, server_port);
1451 recv_time = TStime();
1455 return need_more_params(sptr, "SERVER");
1456 return exit_client(cptr, cptr, &me, "Need more parameters");
1462 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
1463 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
1465 if (!IsServer(cptr)) /* Don't allow silently connecting a server */
1468 prot = atoi(parv[5] + 1);
1469 if (prot > atoi(MAJOR_PROTOCOL))
1470 prot = atoi(MAJOR_PROTOCOL);
1472 * Because the previous test is only in 2.10, the following is needed
1473 * till all servers are 2.10:
1475 if (IsServer(cptr) && prot > Protocol(cptr))
1476 prot = Protocol(cptr);
1477 hop = atoi(parv[2]);
1478 start_timestamp = atoi(parv[3]);
1479 timestamp = atoi(parv[4]);
1480 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age " TIME_T_FMT " ("
1481 TIME_T_FMT ")", host, parv[4], start_timestamp, me.serv->timestamp));
1482 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
1484 return exit_client_msg(cptr, sptr, &me,
1485 "Bogus timestamps (%s %s)", parv[3], parv[4]);
1487 ircd_strncpy(info, parv[parc - 1], REALLEN);
1488 info[REALLEN] = '\0';
1489 if (prot < atoi(MINOR_PROTOCOL)) {
1490 sendto_ops("Got incompatible protocol version (%s) from %s", /* XXX DEAD */
1491 parv[5], cptr->name);
1492 return exit_new_server(cptr, sptr, host, timestamp,
1493 "Incompatible protocol: %s", parv[5]);
1496 * Check for "FRENCH " infection ;-) (actually this should
1497 * be replaced with routine to check the hostname syntax in
1498 * general). [ This check is still needed, even after the parse
1499 * is fixed, because someone can send "SERVER :foo bar " ].
1500 * Also, changed to check other "difficult" characters, now
1501 * that parse lets all through... --msa
1503 if (strlen(host) > HOSTLEN)
1504 host[HOSTLEN] = '\0';
1505 for (ch = host; *ch; ch++)
1506 if (*ch <= ' ' || *ch > '~')
1508 if (*ch || !strchr(host, '.'))
1510 sendto_ops("Bogus server name (%s) from %s", host, cptr->name); /* XXX DEAD */
1511 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
1517 * A local server introduces a new server behind this link.
1518 * Check if this is allowed according L:, H: and Q: lines.
1520 if (info[0] == '\0')
1521 return exit_client_msg(cptr, cptr, &me,
1522 "No server info specified for %s", host);
1524 * See if the newly found server is behind a guaranteed
1525 * leaf (L-line). If so, close the link.
1527 if ((lhconf = find_conf_byhost(cptr->confs, cptr->name, CONF_LEAF)) &&
1528 (!lhconf->port || (hop > lhconf->port)))
1531 * L: lines normally come in pairs, here we try to
1532 * make sure that the oldest link is squitted, not
1536 if (timestamp <= cptr->serv->timestamp)
1537 LHcptr = 0; /* Kill incoming server */
1539 LHcptr = cptr; /* Squit ourselfs */
1541 else if (!(lhconf = find_conf_byname(cptr->confs, cptr->name, CONF_HUB)) ||
1542 (lhconf->port && (hop > lhconf->port)))
1544 struct Client *ac3ptr;
1546 /* Look for net junction causing this: */
1547 LHcptr = 0; /* incoming server */
1548 if (*parv[5] != 'J') {
1549 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up) {
1550 if (IsJunction(ac3ptr)) {
1559 if (IsUnknown(cptr) || IsHandshake(cptr))
1564 * A local link that is still in undefined state wants
1565 * to be a SERVER. Check if this is allowed and change
1566 * status accordingly...
1569 * If there is more then one server on the same machine
1570 * that we try to connect to, it could be that the /CONNECT
1571 * <mask> caused this connect to be put at the wrong place
1572 * in the hashtable. --Run
1573 * Same thing for Unknown connections that first send NICK.
1575 * Better check if the two strings are (caseless) identical
1576 * and not mess with hash internals.
1579 if ((!(EmptyString(cptr->name)))
1580 && (IsUnknown(cptr) || IsHandshake(cptr))
1581 && 0 != ircd_strcmp(cptr->name, host))
1582 hChangeClient(cptr, host);
1583 ircd_strncpy(cptr->name, host, HOSTLEN);
1584 ircd_strncpy(cptr->info, info[0] ? info : me.name, REALLEN);
1585 cptr->hopcount = hop;
1587 /* check connection rules */
1588 if (0 != conf_eval_crule(host, CRULE_ALL)) {
1589 ServerStats->is_ref++;
1590 sendto_ops("Refused connection from %s.", cptr->name); /* XXX DEAD */
1591 return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
1593 if (conf_check_server(cptr)) {
1594 ++ServerStats->is_ref;
1595 sendto_ops("Received unauthorized connection from %s.", cptr->name); /* XXX DEAD */
1596 return exit_client(cptr, cptr, &me, "No C/N conf lines");
1603 if (!(aconf = find_conf_byname(cptr->confs, host, CONF_SERVER))) {
1604 ++ServerStats->is_ref;
1606 sendto_ops("Access denied. No conf line for server %s", cptr->name); /* XXX DEAD */
1607 return exit_client_msg(cptr, cptr, &me,
1608 "Access denied. No conf line for server %s", cptr->name);
1610 sendto_ops("General C/N: line active: No line for server %s", cptr->name); /* XXX DEAD */
1612 find_conf_byname(cptr->confs, "general.undernet.org", CONF_SERVER);
1614 sendto_ops("Neither C/N lines for server %s nor " /* XXX DEAD */
1615 "\"general.undernet.org\"", cptr->name);
1616 return exit_client_msg(cptr, cptr, &me,
1617 "No C/N lines for server %s", cptr->name);
1619 #endif /* GODMODE */
1621 #ifdef CRYPT_LINK_PASSWORD
1622 /* passwd may be NULL. Head it off at the pass... */
1625 encr = ircd_crypt(cptr->passwd, aconf->passwd);
1630 encr = cptr->passwd;
1631 #endif /* CRYPT_LINK_PASSWORD */
1633 if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
1634 ++ServerStats->is_ref;
1635 sendto_ops("Access denied (passwd mismatch) %s", cptr->name); /* XXX DEAD */
1636 return exit_client_msg(cptr, cptr, &me,
1637 "No Access (passwd mismatch) %s", cptr->name);
1639 #endif /* not GODMODE */
1640 memset(cptr->passwd, 0, sizeof(cptr->passwd));
1643 for (i = 0; i <= HighestFd; i++)
1644 if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
1653 * We want to find IsConnecting() and IsHandshake() too,
1655 * The second finds collisions with numeric representation of existing
1656 * servers - these shouldn't happen anymore when all upgraded to 2.10.
1659 while ((acptr = FindClient(host)) ||
1660 (parc > 7 && (acptr = FindNServer(parv[6]))))
1663 * This link is trying feed me a server that I already have
1664 * access through another path
1666 * Do not allow Uworld to do this.
1667 * Do not allow servers that are juped.
1668 * Do not allow servers that have older link timestamps
1670 * Do not allow servers that use the same numeric as an existing
1671 * server, but have a different name.
1673 * If my ircd.conf sucks, I can try to connect to myself:
1676 return exit_client_msg(cptr, cptr, &me,
1677 "nick collision with me, check server number in M:? (%s)", host);
1679 * Detect wrong numeric.
1681 if (0 != ircd_strcmp(acptr->name, host))
1683 sendto_serv_butone(cptr, /* XXX DEAD */
1684 ":%s WALLOPS :SERVER Numeric Collision: %s != %s",
1685 me.name, acptr->name, host);
1686 return exit_client_msg(cptr, cptr, &me,
1687 "NUMERIC collision between %s and %s."
1688 " Is your server numeric correct ?", host, acptr->name);
1691 * Kill our try, if we had one.
1693 if (IsConnecting(acptr))
1695 if (!active_lh_line && exit_client(cptr, acptr, &me,
1696 "Just connected via another link") == CPTR_KILLED)
1699 * We can have only ONE 'IsConnecting', 'IsHandshake' or
1700 * 'IsServer', because new 'IsConnecting's are refused to
1701 * the same server if we already had it.
1706 * Avoid other nick collisions...
1707 * This is a doubtfull test though, what else would it be
1708 * when it has a server.name ?
1710 else if (!IsServer(acptr) && !IsHandshake(acptr))
1711 return exit_client_msg(cptr, cptr, &me,
1712 "Nickname %s already exists!", host);
1714 * Our new server might be a juped server,
1715 * or someone trying abuse a second Uworld:
1717 else if (IsServer(acptr) && (0 == ircd_strncmp(acptr->info, "JUPE", 4) ||
1718 find_conf_byhost(cptr->confs, acptr->name, CONF_UWORLD)))
1720 if (!IsServer(sptr))
1721 return exit_client(cptr, sptr, &me, acptr->info);
1722 sendto_one(cptr, ":%s WALLOPS :Received :%s SERVER %s from %s !?!", /* XXX DEAD */
1723 me.name, parv[0], parv[1], cptr->name);
1724 return exit_new_server(cptr, sptr, host, timestamp, "%s", acptr->info);
1727 * Of course we find the handshake this link was before :)
1729 else if (IsHandshake(acptr) && acptr == cptr)
1732 * Here we have a server nick collision...
1733 * We don't want to kill the link that was last /connected,
1734 * but we neither want to kill a good (old) link.
1735 * Therefor we kill the second youngest link.
1739 struct Client* c2ptr = 0;
1740 struct Client* c3ptr = acptr;
1741 struct Client* ac2ptr;
1742 struct Client* ac3ptr;
1744 /* Search youngest link: */
1745 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1746 if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1750 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1751 if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1754 if (timestamp > c3ptr->serv->timestamp)
1757 c2ptr = acptr; /* Make sure they differ */
1759 /* Search second youngest link: */
1760 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1761 if (ac2ptr != c3ptr &&
1762 ac2ptr->serv->timestamp >
1763 (c2ptr ? c2ptr->serv->timestamp : timestamp))
1767 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1768 if (ac2ptr != c3ptr &&
1769 ac2ptr->serv->timestamp >
1770 (c2ptr ? c2ptr->serv->timestamp : timestamp))
1773 if (c3ptr && timestamp > (c2ptr ? c2ptr->serv->timestamp : timestamp))
1775 /* If timestamps are equal, decide which link to break
1778 if ((c2ptr ? c2ptr->serv->timestamp : timestamp) ==
1779 (c3ptr ? c3ptr->serv->timestamp : timestamp))
1788 n2up = MyConnect(c2ptr) ? me.name : c2ptr->serv->up->name;
1793 n2up = IsServer(sptr) ? sptr->name : me.name;
1798 n3up = MyConnect(c3ptr) ? me.name : c3ptr->serv->up->name;
1803 n3up = IsServer(sptr) ? sptr->name : me.name;
1805 if (strcmp(n2, n2up) > 0)
1807 if (strcmp(n3, n3up) > 0)
1809 if (strcmp(n3, n2) > 0)
1816 /* Now squit the second youngest link: */
1818 return exit_new_server(cptr, sptr, host, timestamp,
1819 "server %s already exists and is %ld seconds younger.",
1820 host, (long)acptr->serv->timestamp - (long)timestamp);
1821 else if (c2ptr->from == cptr || IsServer(sptr))
1823 struct Client *killedptrfrom = c2ptr->from;
1827 * If the L: or H: line also gets rid of this link,
1828 * we sent just one squit.
1830 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
1833 * If breaking the loop here solves the L: or H:
1834 * line problem, we don't squit that.
1836 if (c2ptr->from == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
1841 * If we still have a L: or H: line problem,
1842 * we prefer to squit the new server, solving
1843 * loop and L:/H: line problem with only one squit.
1850 * If the new server was introduced by a server that caused a
1851 * Ghost less then 20 seconds ago, this is probably also
1852 * a Ghost... (20 seconds is more then enough because all
1853 * SERVER messages are at the beginning of a net.burst). --Run
1855 if (CurrentTime - cptr->serv->ghost < 20)
1857 killedptrfrom = acptr->from;
1858 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
1861 else if (exit_client_msg(cptr, c2ptr, &me,
1862 "Loop <-- %s (new link is %ld seconds younger)", host,
1863 (c3ptr ? (long)c3ptr->serv->timestamp : timestamp) -
1864 (long)c2ptr->serv->timestamp) == CPTR_KILLED)
1867 * Did we kill the incoming server off already ?
1869 if (killedptrfrom == cptr)
1876 if (LHcptr && a_kills_b_too(LHcptr, acptr))
1878 if (acptr->from == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1887 * We can't believe it is a lagged server message
1888 * when it directly connects to us...
1889 * kill the older link at the ghost, rather then
1890 * at the second youngest link, assuming it isn't
1893 ghost = CurrentTime; /* Mark that it caused a ghost */
1894 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1904 return exit_new_server(cptr, sptr, host, timestamp,
1905 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1907 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1911 int killed = a_kills_b_too(LHcptr, sptr);
1912 if (active_lh_line < 3)
1914 if (exit_client_msg(cptr, LHcptr, &me,
1915 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1917 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1922 ServerStats->is_ref++;
1923 if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1927 * Did we kill the incoming server off already ?
1937 * Server is informing about a new server behind
1938 * this link. Create REMOTE server structure,
1939 * add it to list and propagate word to my other
1943 acptr = make_client(cptr, STAT_SERVER);
1945 acptr->serv->prot = prot;
1946 acptr->serv->timestamp = timestamp;
1947 acptr->hopcount = hop;
1948 ircd_strncpy(acptr->name, host, HOSTLEN);
1949 ircd_strncpy(acptr->info, info, REALLEN);
1950 acptr->serv->up = sptr;
1951 acptr->serv->updown = add_dlink(&sptr->serv->down, acptr);
1952 /* Use cptr, because we do protocol 9 -> 10 translation
1953 for numeric nicks ! */
1954 SetServerYXX(cptr, acptr, parv[6]);
1956 Count_newremoteserver(UserStats);
1957 if (Protocol(acptr) < 10)
1958 acptr->flags |= FLAGS_TS8;
1959 add_client_to_list(acptr);
1961 if (*parv[5] == 'J')
1964 sendto_op_mask(SNO_NETWORK, "Net junction: %s %s", /* XXX DEAD */
1965 sptr->name, acptr->name);
1969 * Old sendto_serv_but_one() call removed because we now need to send
1970 * different names to different servers (domain name matching).
1972 for (i = 0; i <= HighestFd; i++)
1974 if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
1975 bcptr == cptr || IsMe(bcptr))
1977 if (0 == match(me.name, acptr->name))
1979 sendto_one(bcptr, "%s " TOK_SERVER " %s %d 0 %s %s %s%s 0 :%s", /* XXX DEAD */
1980 NumServ(sptr), acptr->name, hop + 1, parv[4], parv[5],
1981 NumServCap(acptr), acptr->info);
1986 if (IsUnknown(cptr) || IsHandshake(cptr))
1989 cptr->serv->timestamp = timestamp;
1990 cptr->serv->prot = prot;
1991 cptr->serv->ghost = ghost;
1992 SetServerYXX(cptr, cptr, parv[6]);
1993 if (start_timestamp > OLDEST_TS)
1995 #ifndef RELIABLE_CLOCK
1997 sendto_ops("Debug: my start time: " TIME_T_FMT " ; others start time: " /* XXX DEAD */
1998 TIME_T_FMT, me.serv->timestamp, start_timestamp);
1999 sendto_ops("Debug: receive time: " TIME_T_FMT " ; received timestamp: " /* XXX DEAD */
2000 TIME_T_FMT " ; difference %ld",
2001 recv_time, timestamp, timestamp - recv_time);
2003 if (start_timestamp < me.serv->timestamp)
2005 sendto_ops("got earlier start time: " TIME_T_FMT " < " TIME_T_FMT, /* XXX DEAD */
2006 start_timestamp, me.serv->timestamp);
2007 me.serv->timestamp = start_timestamp;
2008 TSoffset += timestamp - recv_time;
2009 sendto_ops("clock adjusted by adding %d", (int)(timestamp - recv_time)); /* XXX DEAD */
2011 else if ((start_timestamp > me.serv->timestamp) && IsUnknown(cptr))
2012 cptr->serv->timestamp = TStime();
2014 else if (timestamp != recv_time)
2017 * Equal start times, we have a collision. Let the connected-to server
2018 * decide. This assumes leafs issue more than half of the connection
2021 if (IsUnknown(cptr))
2022 cptr->serv->timestamp = TStime();
2023 else if (IsHandshake(cptr))
2025 sendto_ops("clock adjusted by adding %d", /* XXX DEAD */
2026 (int)(timestamp - recv_time));
2027 TSoffset += timestamp - recv_time;
2030 #else /* RELIABLE CLOCK IS TRUE, we _always_ use our own clock */
2031 if (start_timestamp < me.serv->timestamp)
2032 me.serv->timestamp = start_timestamp;
2033 if (IsUnknown(cptr))
2034 cptr->serv->timestamp = TStime();
2038 ret = server_estab(cptr, aconf); /* XXX DEAD */
2042 #ifdef RELIABLE_CLOCK
2043 if (abs(cptr->serv->timestamp - recv_time) > 30)
2045 sendto_ops("Connected to a net with a timestamp-clock" /* XXX DEAD */
2046 " difference of " STIME_T_FMT " seconds! Used SETTIME to correct"
2047 " this.", timestamp - recv_time);
2048 sendto_one(cptr, ":%s SETTIME " TIME_T_FMT " :%s", /* XXX DEAD */
2049 me.name, TStime(), me.name);