Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_server.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_server.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
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...).
34  *
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.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
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 ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
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*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "client.h"
91 #include "crule.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_log.h"
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
97 #include "jupe.h"
98 #include "list.h"
99 #include "match.h"
100 #include "msg.h"
101 #include "numeric.h"
102 #include "numnicks.h"
103 #include "querycmds.h"
104 #include "s_bsd.h"
105 #include "s_conf.h"
106 #include "s_debug.h"
107 #include "s_misc.h"
108 #include "s_serv.h"
109 #include "send.h"
110 #include "userload.h"
111
112 #include <assert.h>
113 #include <stdlib.h>
114 #include <string.h>
115
116 /*
117  * mr_server - registration message handler
118  *
119  *    parv[0] = sender prefix
120  *    parv[1] = servername
121  *    parv[2] = hopcount
122  *    parv[3] = start timestamp
123  *    parv[4] = link timestamp
124  *    parv[5] = major protocol version: P09/P10
125  *    parv[parc-1] = serverinfo
126  *  If cptr is P10:
127  *    parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
128  *              numeric nick mask of this server.
129  *    parv[7] = 0 (not used yet, mandatory unsigned int after u2.10.06)
130  */
131 int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
132 {
133   char*            ch;
134   int              i;
135   char             info[REALLEN + 1];
136   char*            host;
137   struct Client*   acptr;
138   struct Client*   bcptr;
139   struct Client*   LHcptr = 0;
140   struct ConfItem* aconf = 0;
141   struct ConfItem* cconf;
142   struct ConfItem* lhconf = 0;
143   struct Jupe*     ajupe = 0;
144   int              hop;
145   int              ret;
146   int              active_lh_line = 0;
147   unsigned short   prot;
148   time_t           start_timestamp;
149   time_t           timestamp = 0;
150   time_t           recv_time;
151   time_t           ghost = 0;
152
153   if (IsUserPort(cptr))
154     return exit_client_msg(cptr, cptr, &me, 
155                            "Cannot connect a server to a user port");
156
157   recv_time = TStime();
158   info[0] = '\0';
159
160   if (parc < 7)
161   {
162     return need_more_params(sptr, "SERVER");
163     return exit_client(cptr, cptr, &me, "Need more parameters");
164   }
165   host = parv[1];
166
167   if ((ajupe = jupe_find(host)) && JupeIsActive(ajupe))
168     return exit_client_msg(cptr, sptr, &me, "Juped: %s", JupeReason(ajupe));
169
170   ircd_log(L_NOTICE, "SERVER: %s %s[%s]", parv[1], cptr->sockhost, cptr->sock_ip);
171
172   /*
173    * Detect protocol
174    */
175   if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
176     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
177
178   if (!IsServer(cptr))          /* Don't allow silently connecting a server */
179     *parv[5] = 'J';
180
181   prot = atoi(parv[5] + 1);
182   if (prot > atoi(MAJOR_PROTOCOL))
183     prot = atoi(MAJOR_PROTOCOL);
184   /*
185    * Because the previous test is only in 2.10, the following is needed
186    * till all servers are 2.10:
187    */
188   if (IsServer(cptr) && prot > Protocol(cptr))
189     prot = Protocol(cptr);
190   hop = atoi(parv[2]);
191   start_timestamp = atoi(parv[3]);
192   timestamp = atoi(parv[4]);
193   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age " TIME_T_FMT " ("
194         TIME_T_FMT ")", host, parv[4], start_timestamp, me.serv->timestamp));
195
196   if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
197   {
198     return exit_client_msg(cptr, sptr, &me,
199         "Bogus timestamps (%s %s)", parv[3], parv[4]);
200   }
201   ircd_strncpy(info, parv[parc - 1], REALLEN);
202   info[REALLEN] = '\0';
203   if (prot < atoi(MINOR_PROTOCOL)) {
204     sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
205                          "(%s) from %s", parv[5], cptr->name);
206     return exit_new_server(cptr, sptr, host, timestamp,
207                            "Incompatible protocol: %s", parv[5]);
208   }
209   /*
210    * Check for "FRENCH " infection ;-) (actually this should
211    * be replaced with routine to check the hostname syntax in
212    * general). [ This check is still needed, even after the parse
213    * is fixed, because someone can send "SERVER :foo bar " ].
214    * Also, changed to check other "difficult" characters, now
215    * that parse lets all through... --msa
216    */
217   if (strlen(host) > HOSTLEN)
218     host[HOSTLEN] = '\0';
219
220   for (ch = host; *ch; ch++) {
221     if (*ch <= ' ' || *ch > '~')
222       break;
223   }
224   if (*ch || !strchr(host, '.')) {
225     sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
226                          host, cptr->name);
227     return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
228   }
229
230   if (IsServer(cptr))
231   {
232     /*
233      * A local server introduces a new server behind this link.
234      * Check if this is allowed according L:, H: and Q: lines.
235      */
236     if (info[0] == '\0')
237       return exit_client_msg(cptr, cptr, &me,
238                              "No server info specified for %s", host);
239     /*
240      * See if the newly found server is behind a guaranteed
241      * leaf (L-line). If so, close the link.
242      */
243     if ((lhconf = find_conf_byhost(cptr->confs, cptr->name, CONF_LEAF)) &&
244         (!lhconf->port || (hop > lhconf->port)))
245     {
246       /*
247        * L: lines normally come in pairs, here we try to
248        * make sure that the oldest link is squitted, not
249        * both.
250        */
251       active_lh_line = 1;
252       if (timestamp <= cptr->serv->timestamp)
253         LHcptr = 0;          /* Kill incoming server */
254       else
255         LHcptr = cptr;          /* Squit ourselfs */
256     }
257     else if (!(lhconf = find_conf_byname(cptr->confs, cptr->name, CONF_HUB)) ||
258              (lhconf->port && (hop > lhconf->port)))
259     {
260       struct Client *ac3ptr;
261       active_lh_line = 2;
262       /* Look for net junction causing this: */
263       LHcptr = 0;            /* incoming server */
264       if (*parv[5] != 'J') {
265         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up) {
266           if (IsJunction(ac3ptr)) {
267             LHcptr = ac3ptr;
268             break;
269           }
270         }
271       }
272     }
273   }
274
275   if (IsUnknown(cptr) || IsHandshake(cptr))
276   {
277     const char* encr;
278
279     /*
280      * A local link that is still in undefined state wants
281      * to be a SERVER. Check if this is allowed and change
282      * status accordingly...
283      */
284     /*
285      * If there is more then one server on the same machine
286      * that we try to connect to, it could be that the /CONNECT
287      * <mask> caused this connect to be put at the wrong place
288      * in the hashtable.        --Run
289      * Same thing for Unknown connections that first send NICK.
290      *                          --Xorath
291      * Better check if the two strings are (caseless) identical 
292      * and not mess with hash internals. 
293      *                          --Nemesi
294      */
295     if (!EmptyString(cptr->name) && 
296         (IsUnknown(cptr) || IsHandshake(cptr)) &&
297         0 != ircd_strcmp(cptr->name, host))
298       hChangeClient(cptr, host);
299     ircd_strncpy(cptr->name, host, HOSTLEN);
300     ircd_strncpy(cptr->info, info[0] ? info : me.name, REALLEN);
301     cptr->hopcount = hop;
302
303     /* check connection rules */
304     for (cconf = GlobalConfList; cconf; cconf = cconf->next) {
305       if ((cconf->status == CONF_CRULEALL) && (match(cconf->host, host) == 0)) {
306         if (crule_eval(cconf->passwd)) {
307           ServerStats->is_ref++;
308           sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.",
309                                cptr->name);
310           return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
311         }
312       }
313     }
314     if (conf_check_server(cptr)) {
315       ++ServerStats->is_ref;
316       sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
317                            "from %s.", cptr->name);
318       return exit_client(cptr, cptr, &me, "No C:line");
319     }
320
321     host = cptr->name;
322
323     update_load();
324
325     if (!(aconf = find_conf_byname(cptr->confs, host, CONF_SERVER))) {
326       ++ServerStats->is_ref;
327 #ifndef GODMODE
328       sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
329                            "server %s", cptr->name);
330       return exit_client_msg(cptr, cptr, &me,
331           "Access denied. No conf line for server %s", cptr->name);
332 #else /* GODMODE */
333       sendto_opmask_butone(0, SNO_OLDSNO, "General C: line active: No line "
334                            "for server %s", cptr->name);
335       aconf = find_conf_byname(cptr->confs, "general.undernet.org", CONF_SERVER);
336       if (!aconf) {
337         sendto_opmask_butone(0, SNO_OLDSNO, "Neither C lines for server %s "
338                              "nor \"general.undernet.org\"", cptr->name);
339         return exit_client_msg(cptr, cptr, &me, "No C lines for server %s", cptr->name);
340       }
341 #endif /* GODMODE */
342     }
343 #ifdef CRYPT_LINK_PASSWORD
344     /* passwd may be NULL. Head it off at the pass... */
345     if (*cptr->passwd) {
346       char salt[3];
347
348       salt[0] = aconf->passwd[0];
349       salt[1] = aconf->passwd[1];
350       salt[2] = '\0';
351       encr = ircd_crypt(cptr->passwd, salt);
352     }
353     else
354       encr = "";
355 #else
356     encr = cptr->passwd;
357 #endif /* CRYPT_LINK_PASSWORD */
358 #ifndef GODMODE
359     if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
360       ++ServerStats->is_ref;
361       sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
362                            cptr->name);
363       return exit_client_msg(cptr, cptr, &me,
364           "No Access (passwd mismatch) %s", cptr->name);
365     }
366 #endif /* not GODMODE */
367     memset(cptr->passwd, 0, sizeof(cptr->passwd));
368
369 #ifndef HUB
370     for (i = 0; i <= HighestFd; i++)
371       if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
372         active_lh_line = 3;
373         LHcptr = 0;
374         break;
375       }
376 #endif
377   }
378
379   /*
380    *  We want to find IsConnecting() and IsHandshake() too,
381    *  use FindClient().
382    *  The second finds collisions with numeric representation of existing
383    *  servers - these shouldn't happen anymore when all upgraded to 2.10.
384    *  -- Run
385    */
386   while ((acptr = FindClient(host)) || 
387          (parc > 7 && (acptr = FindNServer(parv[6]))))
388   {
389     /*
390      *  This link is trying feed me a server that I already have
391      *  access through another path
392      *
393      *  Do not allow Uworld to do this.
394      *  Do not allow servers that are juped.
395      *  Do not allow servers that have older link timestamps
396      *    then this try.
397      *  Do not allow servers that use the same numeric as an existing
398      *    server, but have a different name.
399      *
400      *  If my ircd.conf sucks, I can try to connect to myself:
401      */
402     if (acptr == &me)
403       return exit_client_msg(cptr, cptr, &me, "nick collision with me (%s), check server number in M:?", host);
404     /*
405      * Detect wrong numeric.
406      */
407     if (0 != ircd_strcmp(acptr->name, host)) {
408       sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
409                             ":SERVER Numeric Collision: %s != %s",
410                             acptr->name, host);
411       return exit_client_msg(cptr, cptr, &me,
412           "NUMERIC collision between %s and %s."
413           " Is your server numeric correct ?", host, acptr->name);
414     }
415     /*
416      *  Kill our try, if we had one.
417      */
418     if (IsConnecting(acptr))
419     {
420       if (!active_lh_line && exit_client(cptr, acptr, &me,
421           "Just connected via another link") == CPTR_KILLED)
422         return CPTR_KILLED;
423       /*
424        * We can have only ONE 'IsConnecting', 'IsHandshake' or
425        * 'IsServer', because new 'IsConnecting's are refused to
426        * the same server if we already had it.
427        */
428       break;
429     }
430     /*
431      * Avoid other nick collisions...
432      * This is a doubtfull test though, what else would it be
433      * when it has a server.name ?
434      */
435     else if (!IsServer(acptr) && !IsHandshake(acptr))
436       return exit_client_msg(cptr, cptr, &me,
437                              "Nickname %s already exists!", host);
438     /*
439      * Our new server might be a juped server,
440      * or someone trying abuse a second Uworld:
441      */
442     else if (IsServer(acptr) && (0 == ircd_strncmp(acptr->info, "JUPE", 4) ||
443         find_conf_byhost(cptr->confs, acptr->name, CONF_UWORLD)))
444     {
445       if (!IsServer(sptr))
446         return exit_client(cptr, sptr, &me, acptr->info);
447       sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
448                             ":Received :%s SERVER %s from %s !?!", parv[0],
449                             parv[1], cptr->name);
450       return exit_new_server(cptr, sptr, host, timestamp, "%s", acptr->info);
451     }
452     /*
453      * Of course we find the handshake this link was before :)
454      */
455     else if (IsHandshake(acptr) && acptr == cptr)
456       break;
457     /*
458      * Here we have a server nick collision...
459      * We don't want to kill the link that was last /connected,
460      * but we neither want to kill a good (old) link.
461      * Therefor we kill the second youngest link.
462      */
463     if (1)
464     {
465       struct Client* c2ptr = 0;
466       struct Client* c3ptr = acptr;
467       struct Client* ac2ptr;
468       struct Client* ac3ptr;
469
470       /* Search youngest link: */
471       for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
472         if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
473           c3ptr = ac3ptr;
474       if (IsServer(sptr))
475       {
476         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
477           if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
478             c3ptr = ac3ptr;
479       }
480       if (timestamp > c3ptr->serv->timestamp)
481       {
482         c3ptr = 0;
483         c2ptr = acptr;          /* Make sure they differ */
484       }
485       /* Search second youngest link: */
486       for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
487         if (ac2ptr != c3ptr &&
488             ac2ptr->serv->timestamp >
489             (c2ptr ? c2ptr->serv->timestamp : timestamp))
490           c2ptr = ac2ptr;
491       if (IsServer(sptr))
492       {
493         for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
494           if (ac2ptr != c3ptr &&
495               ac2ptr->serv->timestamp >
496               (c2ptr ? c2ptr->serv->timestamp : timestamp))
497             c2ptr = ac2ptr;
498       }
499       if (c3ptr && timestamp > (c2ptr ? c2ptr->serv->timestamp : timestamp))
500         c2ptr = 0;
501       /* If timestamps are equal, decide which link to break
502        *  by name.
503        */
504       if ((c2ptr ? c2ptr->serv->timestamp : timestamp) ==
505           (c3ptr ? c3ptr->serv->timestamp : timestamp))
506       {
507         char* n2;
508         char* n2up;
509         char* n3;
510         char* n3up;
511         if (c2ptr)
512         {
513           n2 = c2ptr->name;
514           n2up = MyConnect(c2ptr) ? me.name : c2ptr->serv->up->name;
515         }
516         else
517         {
518           n2 = host;
519           n2up = IsServer(sptr) ? sptr->name : me.name;
520         }
521         if (c3ptr)
522         {
523           n3 = c3ptr->name;
524           n3up = MyConnect(c3ptr) ? me.name : c3ptr->serv->up->name;
525         }
526         else
527         {
528           n3 = host;
529           n3up = IsServer(sptr) ? sptr->name : me.name;
530         }
531         if (strcmp(n2, n2up) > 0)
532           n2 = n2up;
533         if (strcmp(n3, n3up) > 0)
534           n3 = n3up;
535         if (strcmp(n3, n2) > 0)
536         {
537           ac2ptr = c2ptr;
538           c2ptr = c3ptr;
539           c3ptr = ac2ptr;
540         }
541       }
542       /* Now squit the second youngest link: */
543       if (!c2ptr)
544         return exit_new_server(cptr, sptr, host, timestamp,
545                                "server %s already exists and is %ld seconds younger.",
546                                host, (long)acptr->serv->timestamp - (long)timestamp);
547       else if (c2ptr->from == cptr || IsServer(sptr))
548       {
549         struct Client *killedptrfrom = c2ptr->from;
550         if (active_lh_line)
551         {
552           /*
553            * If the L: or H: line also gets rid of this link,
554            * we sent just one squit.
555            */
556           if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
557             break;
558           /*
559            * If breaking the loop here solves the L: or H:
560            * line problem, we don't squit that.
561            */
562           if (c2ptr->from == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
563             active_lh_line = 0;
564           else
565           {
566             /*
567              * If we still have a L: or H: line problem,
568              * we prefer to squit the new server, solving
569              * loop and L:/H: line problem with only one squit.
570              */
571             LHcptr = 0;
572             break;
573           }
574         }
575         /*
576          * If the new server was introduced by a server that caused a
577          * Ghost less then 20 seconds ago, this is probably also
578          * a Ghost... (20 seconds is more then enough because all
579          * SERVER messages are at the beginning of a net.burst). --Run
580          */
581         if (CurrentTime - cptr->serv->ghost < 20)
582         {
583           killedptrfrom = acptr->from;
584           if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
585             return CPTR_KILLED;
586         }
587         else if (exit_client_msg(cptr, c2ptr, &me,
588             "Loop <-- %s (new link is %ld seconds younger)", host,
589             (c3ptr ? (long)c3ptr->serv->timestamp : timestamp) -
590             (long)c2ptr->serv->timestamp) == CPTR_KILLED)
591           return CPTR_KILLED;
592         /*
593          * Did we kill the incoming server off already ?
594          */
595         if (killedptrfrom == cptr)
596           return 0;
597       }
598       else
599       {
600         if (active_lh_line)
601         {
602           if (LHcptr && a_kills_b_too(LHcptr, acptr))
603             break;
604           if (acptr->from == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
605             active_lh_line = 0;
606           else
607           {
608             LHcptr = 0;
609             break;
610           }
611         }
612         /*
613          * We can't believe it is a lagged server message
614          * when it directly connects to us...
615          * kill the older link at the ghost, rather then
616          * at the second youngest link, assuming it isn't
617          * a REAL loop.
618          */
619         ghost = CurrentTime;            /* Mark that it caused a ghost */
620         if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
621           return CPTR_KILLED;
622         break;
623       }
624     }
625   }
626
627   if (active_lh_line)
628   {
629     if (LHcptr == 0) {
630       return exit_new_server(cptr, sptr, host, timestamp,
631           (active_lh_line == 2) ?  "Non-Hub link %s <- %s(%s), check H:" : 
632                                    "Leaf-only link %s <- %s(%s), check L:",
633           cptr->name, host, 
634           lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
635     }
636     else
637     {
638       int killed = a_kills_b_too(LHcptr, sptr);
639       if (active_lh_line < 3)
640       {
641         if (exit_client_msg(cptr, LHcptr, &me,
642             (active_lh_line == 2) ?  "Non-Hub link %s <- %s(%s), check H:" : 
643                                      "Leaf-only link %s <- %s(%s), check L:",
644             cptr->name, host,
645             lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
646           return CPTR_KILLED;
647       }
648       else
649       {
650         ServerStats->is_ref++;
651         if (exit_client(cptr, LHcptr, &me, "I'm a leaf, define HUB") == CPTR_KILLED)
652           return CPTR_KILLED;
653       }
654       /*
655        * Did we kill the incoming server off already ?
656        */
657       if (killed)
658         return 0;
659     }
660   }
661
662   if (IsServer(cptr))
663   {
664     /*
665      * Server is informing about a new server behind
666      * this link. Create REMOTE server structure,
667      * add it to list and propagate word to my other
668      * server links...
669      */
670
671     acptr = make_client(cptr, STAT_SERVER);
672     make_server(acptr);
673     acptr->serv->prot = prot;
674     acptr->serv->timestamp = timestamp;
675     acptr->hopcount = hop;
676     ircd_strncpy(acptr->name, host, HOSTLEN);
677     ircd_strncpy(acptr->info, info, REALLEN);
678     acptr->serv->up = sptr;
679     acptr->serv->updown = add_dlink(&sptr->serv->down, acptr);
680     /* Use cptr, because we do protocol 9 -> 10 translation
681        for numeric nicks ! */
682     SetServerYXX(cptr, acptr, parv[6]);
683
684     Count_newremoteserver(UserStats);
685     if (Protocol(acptr) < 10)
686       acptr->flags |= FLAGS_TS8;
687     add_client_to_list(acptr);
688     hAddClient(acptr);
689     if (*parv[5] == 'J')
690     {
691       SetBurst(acptr);
692       sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
693           sptr->name, acptr->name);
694       SetJunction(acptr);
695     }
696     /*
697      * Old sendto_serv_but_one() call removed because we now need to send
698      * different names to different servers (domain name matching).
699      *
700      * Personally, I think this is bogus; it's a feature we don't use here.
701      * -Kev
702      */
703     for (i = 0; i <= HighestFd; i++)
704     {
705       if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
706           bcptr == cptr || IsMe(bcptr))
707         continue;
708       if (0 == match(me.name, acptr->name))
709         continue;
710       sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s 0 :%s",
711                     acptr->name, hop + 1, parv[4], parv[5], NumServCap(acptr),
712                     acptr->info);
713     }
714     return 0;
715   }
716
717   if (IsUnknown(cptr) || IsHandshake(cptr))
718   {
719     make_server(cptr);
720     cptr->serv->timestamp = timestamp;
721     cptr->serv->prot = prot;
722     cptr->serv->ghost = ghost;
723     SetServerYXX(cptr, cptr, parv[6]);
724     if (start_timestamp > OLDEST_TS)
725     {
726 #ifndef RELIABLE_CLOCK
727 #ifdef TESTNET
728       sendto_opmask_butone(0, SNO_OLDSNO, "Debug: my start time: %Tu ; "
729                            "others start time: %Tu", me.serv->timestamp,
730                            start_timestamp);
731       sendto_opmask_butone(0, SNO_OLDSNO, "Debug: receive time: %Tu ; "
732                            "received timestamp: %Tu ; difference %ld",
733                            recv_time, timestamp, timestamp - recv_time);
734 #endif
735       if (start_timestamp < me.serv->timestamp)
736       {
737         sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
738                              "%Tu < %Tu", start_timestamp, me.serv->timestamp);
739         me.serv->timestamp = start_timestamp;
740         TSoffset += timestamp - recv_time;
741         sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
742                              (int)(timestamp - recv_time));
743       }
744       else if ((start_timestamp > me.serv->timestamp) && IsUnknown(cptr))
745         cptr->serv->timestamp = TStime();
746
747       else if (timestamp != recv_time)
748       {
749         /*
750          * Equal start times, we have a collision.  Let the connected-to server
751          * decide. This assumes leafs issue more than half of the connection
752          * attempts.
753          */
754         if (IsUnknown(cptr))
755           cptr->serv->timestamp = TStime();
756         else if (IsHandshake(cptr))
757         {
758           sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
759                                (int)(timestamp - recv_time));
760           TSoffset += timestamp - recv_time;
761         }
762       }
763 #else /* RELIABLE CLOCK IS TRUE, we _always_ use our own clock */
764       if (start_timestamp < me.serv->timestamp)
765         me.serv->timestamp = start_timestamp;
766       if (IsUnknown(cptr))
767         cptr->serv->timestamp = TStime();
768 #endif
769     }
770
771     ret = server_estab(cptr, aconf);
772   }
773   else
774     ret = 0;
775 #ifdef RELIABLE_CLOCK
776   if (abs(cptr->serv->timestamp - recv_time) > 30)
777   {
778     sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
779                          "timestamp-clock difference of %Td seconds! "
780                          "Used SETTIME to correct this.",
781                          timestamp - recv_time);
782     sendcmdto_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(), me.name);
783   }
784 #endif
785
786   return ret;
787 }
788
789 /*
790  * ms_server - server message handler
791  *
792  *    parv[0] = sender prefix
793  *    parv[1] = servername
794  *    parv[2] = hopcount
795  *    parv[3] = start timestamp
796  *    parv[4] = link timestamp
797  *    parv[5] = major protocol version: P09/P10
798  *    parv[parc-1] = serverinfo
799  *  If cptr is P10:
800  *    parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
801  *              numeric nick mask of this server.
802  *    parv[7] = 0 (not used yet, mandatory unsigned int after u2.10.06)
803  */
804 int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
805 {
806   char*            ch;
807   int              i;
808   char             info[REALLEN + 1];
809   char*            host;
810   struct Client*   acptr;
811   struct Client*   bcptr;
812   struct Client*   LHcptr = 0;
813   struct ConfItem* aconf = 0;
814   struct ConfItem* cconf;
815   struct ConfItem* lhconf = 0;
816   struct Jupe*     ajupe = 0;
817   int              hop;
818   int              ret;
819   int              active_lh_line = 0;
820   unsigned short   prot;
821   time_t           start_timestamp;
822   time_t           timestamp = 0;
823   time_t           recv_time;
824   time_t           ghost = 0;
825
826   if (IsUserPort(cptr))
827     return exit_client_msg(cptr, cptr, &me,
828                            "Cannot connect a server to a user port");
829
830   recv_time = TStime();
831   info[0] = '\0';
832   if (parc < 7)
833   {
834     return need_more_params(sptr, "SERVER");
835     return exit_client(cptr, cptr, &me, "Need more parameters");
836   }
837   host = parv[1];
838
839   if ((ajupe = jupe_find(host)) && JupeIsActive(ajupe))
840     jupe_resend(cptr, ajupe);
841
842   /*
843    * Detect protocol
844    */
845   if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
846     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
847
848   if (!IsServer(cptr))          /* Don't allow silently connecting a server */
849     *parv[5] = 'J';
850
851   prot = atoi(parv[5] + 1);
852   if (prot > atoi(MAJOR_PROTOCOL))
853     prot = atoi(MAJOR_PROTOCOL);
854   /*
855    * Because the previous test is only in 2.10, the following is needed
856    * till all servers are 2.10:
857    */
858   if (IsServer(cptr) && prot > Protocol(cptr))
859     prot = Protocol(cptr);
860   hop = atoi(parv[2]);
861   start_timestamp = atoi(parv[3]);
862   timestamp = atoi(parv[4]);
863   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age " TIME_T_FMT " ("
864       TIME_T_FMT ")", host, parv[4], start_timestamp, me.serv->timestamp));
865   if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
866   {
867     return exit_client_msg(cptr, sptr, &me,
868         "Bogus timestamps (%s %s)", parv[3], parv[4]);
869   }
870   ircd_strncpy(info, parv[parc - 1], REALLEN);
871   info[REALLEN] = '\0';
872   if (prot < atoi(MINOR_PROTOCOL)) {
873     sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
874                          "(%s) from %s", parv[5], cptr->name);
875     return exit_new_server(cptr, sptr, host, timestamp,
876                            "Incompatible protocol: %s", parv[5]);
877   }
878   /*
879    * Check for "FRENCH " infection ;-) (actually this should
880    * be replaced with routine to check the hostname syntax in
881    * general). [ This check is still needed, even after the parse
882    * is fixed, because someone can send "SERVER :foo bar " ].
883    * Also, changed to check other "difficult" characters, now
884    * that parse lets all through... --msa
885    */
886   if (strlen(host) > HOSTLEN)
887     host[HOSTLEN] = '\0';
888   for (ch = host; *ch; ch++)
889     if (*ch <= ' ' || *ch > '~')
890       break;
891   if (*ch || !strchr(host, '.')) {
892     sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
893                          host, cptr->name);
894     return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
895   }
896
897   if (IsServer(cptr))
898   {
899     /*
900      * A local server introduces a new server behind this link.
901      * Check if this is allowed according L:, H: and Q: lines.
902      */
903     if (info[0] == '\0')
904       return exit_client_msg(cptr, cptr, &me,
905           "No server info specified for %s", host);
906     /*
907      * See if the newly found server is behind a guaranteed
908      * leaf (L-line). If so, close the link.
909      */
910     if ((lhconf = find_conf_byhost(cptr->confs, cptr->name, CONF_LEAF)) &&
911         (!lhconf->port || (hop > lhconf->port)))
912     {
913       /*
914        * L: lines normally come in pairs, here we try to
915        * make sure that the oldest link is squitted, not
916        * both.
917        */
918       active_lh_line = 1;
919       if (timestamp <= cptr->serv->timestamp)
920         LHcptr = 0;          /* Kill incoming server */
921       else
922         LHcptr = cptr;          /* Squit ourselfs */
923     }
924     else if (!(lhconf = find_conf_byname(cptr->confs, cptr->name, CONF_HUB)) ||
925              (lhconf->port && (hop > lhconf->port)))
926     {
927       struct Client *ac3ptr;
928       active_lh_line = 2;
929       /* Look for net junction causing this: */
930       LHcptr = 0;            /* incoming server */
931       if (*parv[5] != 'J') {
932         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up) {
933           if (IsJunction(ac3ptr)) {
934             LHcptr = ac3ptr;
935             break;
936           }
937         }
938       }
939     }
940   }
941
942   if (IsUnknown(cptr) || IsHandshake(cptr))
943   {
944     const char* encr;
945
946     /*
947      * A local link that is still in undefined state wants
948      * to be a SERVER. Check if this is allowed and change
949      * status accordingly...
950      */
951     /*
952      * If there is more then one server on the same machine
953      * that we try to connect to, it could be that the /CONNECT
954      * <mask> caused this connect to be put at the wrong place
955      * in the hashtable.        --Run
956      * Same thing for Unknown connections that first send NICK.
957      *                          --Xorath
958      * Better check if the two strings are (caseless) identical 
959      * and not mess with hash internals. 
960      *                          --Nemesi
961      */
962     if ((!(EmptyString(cptr->name)))
963         && (IsUnknown(cptr) || IsHandshake(cptr))
964         && 0 != ircd_strcmp(cptr->name, host))
965       hChangeClient(cptr, host);
966     ircd_strncpy(cptr->name, host, HOSTLEN);
967     ircd_strncpy(cptr->info, info[0] ? info : me.name, REALLEN);
968     cptr->hopcount = hop;
969
970     /* check connection rules */
971     for (cconf = GlobalConfList; cconf; cconf = cconf->next) {
972       if ((cconf->status == CONF_CRULEALL) && (match(cconf->host, host) == 0)) {
973         if (crule_eval(cconf->passwd))
974         {
975           ServerStats->is_ref++;
976           sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.",
977                                cptr->name);
978           return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
979         }
980       }
981     }
982     if (conf_check_server(cptr)) {
983       ++ServerStats->is_ref;
984       sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
985                            "from %s.", cptr->name);
986       return exit_client(cptr, cptr, &me, "No C conf lines");
987     }
988
989     host = cptr->name;
990
991     update_load();
992
993     if (!(aconf = find_conf_byname(cptr->confs, host, CONF_SERVER))) {
994       ++ServerStats->is_ref;
995 #ifndef GODMODE
996       sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
997                            "server %s", cptr->name);
998       return exit_client_msg(cptr, cptr, &me,
999                              "Access denied. No conf line for server %s", cptr->name);
1000 #else /* GODMODE */
1001       sendto_opmask_butone(0, SNO_OLDSNO, "General C line active: No line "
1002                            "for server %s", cptr->name);
1003       aconf =
1004           find_conf_byname(cptr->confs, "general.undernet.org", CONF_SERVER);
1005       if (!aconf) {
1006         sendto_opmask_butone(0, SNO_OLDSNO, "Neither C lines for server %s "
1007                              "nor \"general.undernet.org\"", cptr->name);
1008         return exit_client_msg(cptr, cptr, &me,
1009             "No C lines for server %s", cptr->name);
1010       }
1011 #endif /* GODMODE */
1012     }
1013 #ifdef CRYPT_LINK_PASSWORD
1014     /* passwd may be NULL. Head it off at the pass... */
1015     if (*cptr->passwd)
1016     {
1017       char salt[3];
1018
1019       salt[0] = aconf->passwd[0];
1020       salt[1] = aconf->passwd[1];
1021       salt[2] = '\0';
1022       encr = ircd_crypt(cptr->passwd, salt);
1023     }
1024     else
1025       encr = "";
1026 #else
1027     encr = cptr->passwd;
1028 #endif /* CRYPT_LINK_PASSWORD */
1029 #ifndef GODMODE
1030     if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
1031       ++ServerStats->is_ref;
1032       sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
1033                            cptr->name);
1034       return exit_client_msg(cptr, cptr, &me,
1035                              "No Access (passwd mismatch) %s", cptr->name);
1036     }
1037 #endif /* not GODMODE */
1038     memset(cptr->passwd, 0, sizeof(cptr->passwd));
1039
1040 #ifndef HUB
1041     for (i = 0; i <= HighestFd; i++)
1042       if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
1043         active_lh_line = 3;
1044         LHcptr = 0;
1045         break;
1046       }
1047 #endif
1048   }
1049
1050   /*
1051    *  We want to find IsConnecting() and IsHandshake() too,
1052    *  use FindClient().
1053    *  The second finds collisions with numeric representation of existing
1054    *  servers - these shouldn't happen anymore when all upgraded to 2.10.
1055    *  -- Run
1056    */
1057   while ((acptr = FindClient(host)) || 
1058          (parc > 7 && (acptr = FindNServer(parv[6]))))
1059   {
1060     /*
1061      *  This link is trying feed me a server that I already have
1062      *  access through another path
1063      *
1064      *  Do not allow Uworld to do this.
1065      *  Do not allow servers that are juped.
1066      *  Do not allow servers that have older link timestamps
1067      *    then this try.
1068      *  Do not allow servers that use the same numeric as an existing
1069      *    server, but have a different name.
1070      *
1071      *  If my ircd.conf sucks, I can try to connect to myself:
1072      */
1073     if (acptr == &me)
1074       return exit_client_msg(cptr, cptr, &me,
1075           "nick collision with me, check server number in M:? (%s)", host);
1076     /*
1077      * Detect wrong numeric.
1078      */
1079     if (0 != ircd_strcmp(acptr->name, host))
1080     {
1081       sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
1082                             ":SERVER Numeric Collision: %s != %s", acptr->name,
1083                             host);
1084       return exit_client_msg(cptr, cptr, &me,
1085           "NUMERIC collision between %s and %s."
1086           " Is your server numeric correct ?", host, acptr->name);
1087     }
1088     /*
1089      *  Kill our try, if we had one.
1090      */
1091     if (IsConnecting(acptr))
1092     {
1093       if (!active_lh_line && exit_client(cptr, acptr, &me,
1094           "Just connected via another link") == CPTR_KILLED)
1095         return CPTR_KILLED;
1096       /*
1097        * We can have only ONE 'IsConnecting', 'IsHandshake' or
1098        * 'IsServer', because new 'IsConnecting's are refused to
1099        * the same server if we already had it.
1100        */
1101       break;
1102     }
1103     /*
1104      * Avoid other nick collisions...
1105      * This is a doubtfull test though, what else would it be
1106      * when it has a server.name ?
1107      */
1108     else if (!IsServer(acptr) && !IsHandshake(acptr))
1109       return exit_client_msg(cptr, cptr, &me,
1110           "Nickname %s already exists!", host);
1111     /*
1112      * Our new server might be a juped server,
1113      * or someone trying abuse a second Uworld:
1114      */
1115     else if (IsServer(acptr) && (0 == ircd_strncmp(acptr->info, "JUPE", 4) ||
1116         find_conf_byhost(cptr->confs, acptr->name, CONF_UWORLD)))
1117     {
1118       if (!IsServer(sptr))
1119         return exit_client(cptr, sptr, &me, acptr->info);
1120       sendcmdto_one(&me, CMD_WALLOPS, cptr, ":Received :%s SERVER %s "
1121                     "from %s !?!", parv[0], parv[1], cptr->name);
1122       return exit_new_server(cptr, sptr, host, timestamp, "%s", acptr->info);
1123     }
1124     /*
1125      * Of course we find the handshake this link was before :)
1126      */
1127     else if (IsHandshake(acptr) && acptr == cptr)
1128       break;
1129     /*
1130      * Here we have a server nick collision...
1131      * We don't want to kill the link that was last /connected,
1132      * but we neither want to kill a good (old) link.
1133      * Therefor we kill the second youngest link.
1134      */
1135     if (1)
1136     {
1137       struct Client* c2ptr = 0;
1138       struct Client* c3ptr = acptr;
1139       struct Client* ac2ptr;
1140       struct Client* ac3ptr;
1141
1142       /* Search youngest link: */
1143       for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1144         if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1145           c3ptr = ac3ptr;
1146       if (IsServer(sptr))
1147       {
1148         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1149           if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1150             c3ptr = ac3ptr;
1151       }
1152       if (timestamp > c3ptr->serv->timestamp)
1153       {
1154         c3ptr = 0;
1155         c2ptr = acptr;          /* Make sure they differ */
1156       }
1157       /* Search second youngest link: */
1158       for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1159         if (ac2ptr != c3ptr &&
1160             ac2ptr->serv->timestamp >
1161             (c2ptr ? c2ptr->serv->timestamp : timestamp))
1162           c2ptr = ac2ptr;
1163       if (IsServer(sptr))
1164       {
1165         for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1166           if (ac2ptr != c3ptr &&
1167               ac2ptr->serv->timestamp >
1168               (c2ptr ? c2ptr->serv->timestamp : timestamp))
1169             c2ptr = ac2ptr;
1170       }
1171       if (c3ptr && timestamp > (c2ptr ? c2ptr->serv->timestamp : timestamp))
1172         c2ptr = 0;
1173       /* If timestamps are equal, decide which link to break
1174        *  by name.
1175        */
1176       if ((c2ptr ? c2ptr->serv->timestamp : timestamp) ==
1177           (c3ptr ? c3ptr->serv->timestamp : timestamp))
1178       {
1179         char* n2;
1180         char* n2up;
1181         char* n3;
1182         char* n3up;
1183         if (c2ptr)
1184         {
1185           n2 = c2ptr->name;
1186           n2up = MyConnect(c2ptr) ? me.name : c2ptr->serv->up->name;
1187         }
1188         else
1189         {
1190           n2 = host;
1191           n2up = IsServer(sptr) ? sptr->name : me.name;
1192         }
1193         if (c3ptr)
1194         {
1195           n3 = c3ptr->name;
1196           n3up = MyConnect(c3ptr) ? me.name : c3ptr->serv->up->name;
1197         }
1198         else
1199         {
1200           n3 = host;
1201           n3up = IsServer(sptr) ? sptr->name : me.name;
1202         }
1203         if (strcmp(n2, n2up) > 0)
1204           n2 = n2up;
1205         if (strcmp(n3, n3up) > 0)
1206           n3 = n3up;
1207         if (strcmp(n3, n2) > 0)
1208         {
1209           ac2ptr = c2ptr;
1210           c2ptr = c3ptr;
1211           c3ptr = ac2ptr;
1212         }
1213       }
1214       /* Now squit the second youngest link: */
1215       if (!c2ptr)
1216         return exit_new_server(cptr, sptr, host, timestamp,
1217             "server %s already exists and is %ld seconds younger.",
1218             host, (long)acptr->serv->timestamp - (long)timestamp);
1219       else if (c2ptr->from == cptr || IsServer(sptr))
1220       {
1221         struct Client *killedptrfrom = c2ptr->from;
1222         if (active_lh_line)
1223         {
1224           /*
1225            * If the L: or H: line also gets rid of this link,
1226            * we sent just one squit.
1227            */
1228           if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
1229             break;
1230           /*
1231            * If breaking the loop here solves the L: or H:
1232            * line problem, we don't squit that.
1233            */
1234           if (c2ptr->from == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
1235             active_lh_line = 0;
1236           else
1237           {
1238             /*
1239              * If we still have a L: or H: line problem,
1240              * we prefer to squit the new server, solving
1241              * loop and L:/H: line problem with only one squit.
1242              */
1243             LHcptr = 0;
1244             break;
1245           }
1246         }
1247         /*
1248          * If the new server was introduced by a server that caused a
1249          * Ghost less then 20 seconds ago, this is probably also
1250          * a Ghost... (20 seconds is more then enough because all
1251          * SERVER messages are at the beginning of a net.burst). --Run
1252          */
1253         if (CurrentTime - cptr->serv->ghost < 20)
1254         {
1255           killedptrfrom = acptr->from;
1256           if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
1257             return CPTR_KILLED;
1258         }
1259         else if (exit_client_msg(cptr, c2ptr, &me,
1260             "Loop <-- %s (new link is %ld seconds younger)", host,
1261             (c3ptr ? (long)c3ptr->serv->timestamp : timestamp) -
1262             (long)c2ptr->serv->timestamp) == CPTR_KILLED)
1263           return CPTR_KILLED;
1264         /*
1265          * Did we kill the incoming server off already ?
1266          */
1267         if (killedptrfrom == cptr)
1268           return 0;
1269       }
1270       else
1271       {
1272         if (active_lh_line)
1273         {
1274           if (LHcptr && a_kills_b_too(LHcptr, acptr))
1275             break;
1276           if (acptr->from == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1277             active_lh_line = 0;
1278           else
1279           {
1280             LHcptr = 0;
1281             break;
1282           }
1283         }
1284         /*
1285          * We can't believe it is a lagged server message
1286          * when it directly connects to us...
1287          * kill the older link at the ghost, rather then
1288          * at the second youngest link, assuming it isn't
1289          * a REAL loop.
1290          */
1291         ghost = CurrentTime;            /* Mark that it caused a ghost */
1292         if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1293           return CPTR_KILLED;
1294         break;
1295       }
1296     }
1297   }
1298
1299   if (active_lh_line)
1300   {
1301     if (LHcptr == 0) {
1302       return exit_new_server(cptr, sptr, host, timestamp,
1303           (active_lh_line == 2) ?  "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1304           cptr->name, host,
1305           lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1306     }
1307     else
1308     {
1309       int killed = a_kills_b_too(LHcptr, sptr);
1310       if (active_lh_line < 3)
1311       {
1312         if (exit_client_msg(cptr, LHcptr, &me,
1313             (active_lh_line == 2) ?  "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1314             cptr->name, host,
1315             lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1316           return CPTR_KILLED;
1317       }
1318       else
1319       {
1320         ServerStats->is_ref++;
1321         if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1322           return CPTR_KILLED;
1323       }
1324       /*
1325        * Did we kill the incoming server off already ?
1326        */
1327       if (killed)
1328         return 0;
1329     }
1330   }
1331
1332   if (IsServer(cptr))
1333   {
1334     /*
1335      * Server is informing about a new server behind
1336      * this link. Create REMOTE server structure,
1337      * add it to list and propagate word to my other
1338      * server links...
1339      */
1340
1341     acptr = make_client(cptr, STAT_SERVER);
1342     make_server(acptr);
1343     acptr->serv->prot = prot;
1344     acptr->serv->timestamp = timestamp;
1345     acptr->hopcount = hop;
1346     ircd_strncpy(acptr->name, host, HOSTLEN);
1347     ircd_strncpy(acptr->info, info, REALLEN);
1348     acptr->serv->up = sptr;
1349     acptr->serv->updown = add_dlink(&sptr->serv->down, acptr);
1350     /* Use cptr, because we do protocol 9 -> 10 translation
1351        for numeric nicks ! */
1352     SetServerYXX(cptr, acptr, parv[6]);
1353
1354     Count_newremoteserver(UserStats);
1355     if (Protocol(acptr) < 10)
1356       acptr->flags |= FLAGS_TS8;
1357     add_client_to_list(acptr);
1358     hAddClient(acptr);
1359     if (*parv[5] == 'J')
1360     {
1361       SetBurst(acptr);
1362       sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
1363                            sptr->name, acptr->name);
1364       SetJunction(acptr);
1365     }
1366     /*
1367      * Old sendto_serv_but_one() call removed because we now need to send
1368      * different names to different servers (domain name matching).
1369      */
1370     for (i = 0; i <= HighestFd; i++)
1371     {
1372       if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
1373           bcptr == cptr || IsMe(bcptr))
1374         continue;
1375       if (0 == match(me.name, acptr->name))
1376         continue;
1377       sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s 0 :%s",
1378                     acptr->name, hop + 1, parv[4], parv[5], NumServCap(acptr),
1379                     acptr->info);
1380     }
1381     return 0;
1382   }
1383
1384   if (IsUnknown(cptr) || IsHandshake(cptr))
1385   {
1386     make_server(cptr);
1387     cptr->serv->timestamp = timestamp;
1388     cptr->serv->prot = prot;
1389     cptr->serv->ghost = ghost;
1390     SetServerYXX(cptr, cptr, parv[6]);
1391     if (start_timestamp > OLDEST_TS)
1392     {
1393 #ifndef RELIABLE_CLOCK
1394 #ifdef TESTNET
1395       sendto_opmask_butone(0, SNO_OLDSNO, "Debug: my start time: %Tu ; "
1396                            "others start time: %Tu", me.serv->timestamp,
1397                            start_timestamp);
1398       sendto_opmask_butone(0, SNO_OLDSNO, "Debug: receive time: %Tu ; "
1399                            "received timestamp: %Tu ; difference %ld",
1400                            recv_time, timestamp, timestamp - recv_time);
1401 #endif
1402       if (start_timestamp < me.serv->timestamp)
1403       {
1404         sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
1405                              "%Tu < %Tu", start_timestamp, me.serv->timestamp);
1406         me.serv->timestamp = start_timestamp;
1407         TSoffset += timestamp - recv_time;
1408         sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
1409                              (int)(timestamp - recv_time));
1410       }
1411       else if ((start_timestamp > me.serv->timestamp) && IsUnknown(cptr))
1412         cptr->serv->timestamp = TStime();
1413
1414       else if (timestamp != recv_time)
1415       {
1416         /*
1417          * Equal start times, we have a collision.  Let the connected-to server
1418          * decide. This assumes leafs issue more than half of the connection
1419          * attempts.
1420          */
1421         if (IsUnknown(cptr))
1422           cptr->serv->timestamp = TStime();
1423         else if (IsHandshake(cptr))
1424         {
1425           sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
1426                                (int)(timestamp - recv_time));
1427           TSoffset += timestamp - recv_time;
1428         }
1429       }
1430 #else /* RELIABLE CLOCK IS TRUE, we _always_ use our own clock */
1431       if (start_timestamp < me.serv->timestamp)
1432         me.serv->timestamp = start_timestamp;
1433       if (IsUnknown(cptr))
1434         cptr->serv->timestamp = TStime();
1435 #endif
1436     }
1437
1438     ret = server_estab(cptr, aconf);
1439   }
1440   else
1441     ret = 0;
1442 #ifdef RELIABLE_CLOCK
1443   if (abs(cptr->serv->timestamp - recv_time) > 30)
1444   {
1445     sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
1446                          "timestamp-clock difference of %Td seconds! Used "
1447                          "SETTIME to correct this.", timestamp - recv_time);
1448     sendcmdto_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(), me.name);
1449   }
1450 #endif
1451
1452   return ret;
1453 }
1454
1455
1456 #if 0
1457 /*
1458  *  m_server
1459  *
1460  *    parv[0] = sender prefix
1461  *    parv[1] = servername
1462  *    parv[2] = hopcount
1463  *    parv[3] = start timestamp
1464  *    parv[4] = link timestamp
1465  *    parv[5] = major protocol version: P09/P10
1466  *    parv[parc-1] = serverinfo
1467  *  If cptr is P10:
1468  *    parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
1469  *              numeric nick mask of this server.
1470  *    parv[7] = 0 (not used yet, mandatory unsigned int after u2.10.06)
1471  */
1472 int m_server(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1473 {
1474   char*            ch;
1475   int              i;
1476   char             info[REALLEN + 1];
1477   char*            host;
1478   struct Client*   acptr;
1479   struct Client*   bcptr;
1480   struct Client*   LHcptr = 0;
1481   struct ConfItem* aconf = 0;
1482   struct ConfItem* cconf;
1483   struct ConfItem* lhconf = 0;
1484   struct Jupe*     ajupe = 0;
1485   int              hop;
1486   int              ret;
1487   int              active_lh_line = 0;
1488   unsigned short   prot;
1489   time_t           start_timestamp;
1490   time_t           timestamp = 0;
1491   time_t           recv_time;
1492   time_t           ghost = 0;
1493
1494   if (IsUser(cptr))
1495   {
1496     sendto_one(cptr, err_str(ERR_ALREADYREGISTRED), me.name, parv[0]); /* XXX DEAD */
1497     return 0;
1498   }
1499
1500   if (IsUserPort(cptr))
1501     return exit_client_msg(cptr, cptr, &me,
1502         "You cannot connect a server to a user port; connect to %s port %u",
1503         me.name, server_port);
1504
1505   recv_time = TStime();
1506   info[0] = '\0';
1507   if (parc < 7)
1508   {
1509     return need_more_params(sptr, "SERVER");
1510     return exit_client(cptr, cptr, &me, "Need more parameters");
1511   }
1512   host = parv[1];
1513   /*
1514    * Detect protocol
1515    */
1516   if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
1517     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
1518
1519   if (!IsServer(cptr))          /* Don't allow silently connecting a server */
1520     *parv[5] = 'J';
1521
1522   prot = atoi(parv[5] + 1);
1523   if (prot > atoi(MAJOR_PROTOCOL))
1524     prot = atoi(MAJOR_PROTOCOL);
1525   /*
1526    * Because the previous test is only in 2.10, the following is needed
1527    * till all servers are 2.10:
1528    */
1529   if (IsServer(cptr) && prot > Protocol(cptr))
1530     prot = Protocol(cptr);
1531   hop = atoi(parv[2]);
1532   start_timestamp = atoi(parv[3]);
1533   timestamp = atoi(parv[4]);
1534   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age " TIME_T_FMT " ("
1535       TIME_T_FMT ")", host, parv[4], start_timestamp, me.serv->timestamp));
1536   if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
1537   {
1538     return exit_client_msg(cptr, sptr, &me,
1539         "Bogus timestamps (%s %s)", parv[3], parv[4]);
1540   }
1541   ircd_strncpy(info, parv[parc - 1], REALLEN);
1542   info[REALLEN] = '\0';
1543   if (prot < atoi(MINOR_PROTOCOL)) {
1544     sendto_ops("Got incompatible protocol version (%s) from %s", /* XXX DEAD */
1545                parv[5], cptr->name);
1546     return exit_new_server(cptr, sptr, host, timestamp,
1547         "Incompatible protocol: %s", parv[5]);
1548   }
1549   /*
1550    * Check for "FRENCH " infection ;-) (actually this should
1551    * be replaced with routine to check the hostname syntax in
1552    * general). [ This check is still needed, even after the parse
1553    * is fixed, because someone can send "SERVER :foo bar " ].
1554    * Also, changed to check other "difficult" characters, now
1555    * that parse lets all through... --msa
1556    */
1557   if (strlen(host) > HOSTLEN)
1558     host[HOSTLEN] = '\0';
1559   for (ch = host; *ch; ch++)
1560     if (*ch <= ' ' || *ch > '~')
1561       break;
1562   if (*ch || !strchr(host, '.'))
1563   {
1564     sendto_ops("Bogus server name (%s) from %s", host, cptr->name); /* XXX DEAD */
1565     return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
1566   }
1567
1568   if (IsServer(cptr))
1569   {
1570     /*
1571      * A local server introduces a new server behind this link.
1572      * Check if this is allowed according L:, H: and Q: lines.
1573      */
1574     if (info[0] == '\0')
1575       return exit_client_msg(cptr, cptr, &me,
1576           "No server info specified for %s", host);
1577     /*
1578      * See if the newly found server is behind a guaranteed
1579      * leaf (L-line). If so, close the link.
1580      */
1581     if ((lhconf = find_conf_byhost(cptr->confs, cptr->name, CONF_LEAF)) &&
1582         (!lhconf->port || (hop > lhconf->port)))
1583     {
1584       /*
1585        * L: lines normally come in pairs, here we try to
1586        * make sure that the oldest link is squitted, not
1587        * both.
1588        */
1589       active_lh_line = 1;
1590       if (timestamp <= cptr->serv->timestamp)
1591         LHcptr = 0;          /* Kill incoming server */
1592       else
1593         LHcptr = cptr;          /* Squit ourselfs */
1594     }
1595     else if (!(lhconf = find_conf_byname(cptr->confs, cptr->name, CONF_HUB)) ||
1596              (lhconf->port && (hop > lhconf->port)))
1597     {
1598       struct Client *ac3ptr;
1599       active_lh_line = 2;
1600       /* Look for net junction causing this: */
1601       LHcptr = 0;            /* incoming server */
1602       if (*parv[5] != 'J') {
1603         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up) {
1604           if (IsJunction(ac3ptr)) {
1605             LHcptr = ac3ptr;
1606             break;
1607           }
1608         }
1609       }
1610     }
1611   }
1612
1613   if (IsUnknown(cptr) || IsHandshake(cptr))
1614   {
1615     const char* encr;
1616
1617     /*
1618      * A local link that is still in undefined state wants
1619      * to be a SERVER. Check if this is allowed and change
1620      * status accordingly...
1621      */
1622     /*
1623      * If there is more then one server on the same machine
1624      * that we try to connect to, it could be that the /CONNECT
1625      * <mask> caused this connect to be put at the wrong place
1626      * in the hashtable.        --Run
1627      * Same thing for Unknown connections that first send NICK.
1628      *                          --Xorath
1629      * Better check if the two strings are (caseless) identical 
1630      * and not mess with hash internals. 
1631      *                          --Nemesi
1632      */
1633     if ((!(EmptyString(cptr->name)))
1634         && (IsUnknown(cptr) || IsHandshake(cptr))
1635         && 0 != ircd_strcmp(cptr->name, host))
1636       hChangeClient(cptr, host);
1637     ircd_strncpy(cptr->name, host, HOSTLEN);
1638     ircd_strncpy(cptr->info, info[0] ? info : me.name, REALLEN);
1639     cptr->hopcount = hop;
1640
1641     /* check connection rules */
1642     for (cconf = GlobalConfList; cconf; cconf = cconf->next) {
1643       if ((cconf->status == CONF_CRULEALL) && (match(cconf->host, host) == 0)) {
1644         if (crule_eval(cconf->passwd))
1645         {
1646           ServerStats->is_ref++;
1647           sendto_ops("Refused connection from %s.", cptr->name); /* XXX DEAD */
1648           return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
1649         }
1650       }
1651     }
1652     if (conf_check_server(cptr)) {
1653       ++ServerStats->is_ref;
1654       sendto_ops("Received unauthorized connection from %s.", cptr->name); /* XXX DEAD */
1655       return exit_client(cptr, cptr, &me, "No C/N conf lines");
1656     }
1657
1658     host = cptr->name;
1659
1660     update_load();
1661
1662     if (!(aconf = find_conf_byname(cptr->confs, host, CONF_SERVER))) {
1663       ++ServerStats->is_ref;
1664 #ifndef GODMODE
1665       sendto_ops("Access denied. No conf line for server %s", cptr->name); /* XXX DEAD */
1666       return exit_client_msg(cptr, cptr, &me,
1667           "Access denied. No conf line for server %s", cptr->name);
1668 #else /* GODMODE */
1669       sendto_ops("General C/N: line active: No line for server %s", cptr->name); /* XXX DEAD */
1670       aconf =
1671           find_conf_byname(cptr->confs, "general.undernet.org", CONF_SERVER);
1672       if (!aconf) {
1673         sendto_ops("Neither C/N lines for server %s nor " /* XXX DEAD */
1674             "\"general.undernet.org\"", cptr->name);
1675         return exit_client_msg(cptr, cptr, &me,
1676             "No C/N lines for server %s", cptr->name);
1677       }
1678 #endif /* GODMODE */
1679     }
1680 #ifdef CRYPT_LINK_PASSWORD
1681     /* passwd may be NULL. Head it off at the pass... */
1682     if (*cptr->passwd)
1683     {
1684       char salt[3];
1685
1686       salt[0] = aconf->passwd[0];
1687       salt[1] = aconf->passwd[1];
1688       salt[2] = '\0';
1689       encr = ircd_crypt(cptr->passwd, salt);
1690     }
1691     else
1692       encr = "";
1693 #else
1694     encr = cptr->passwd;
1695 #endif /* CRYPT_LINK_PASSWORD */
1696 #ifndef GODMODE
1697     if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
1698       ++ServerStats->is_ref;
1699       sendto_ops("Access denied (passwd mismatch) %s", cptr->name); /* XXX DEAD */
1700       return exit_client_msg(cptr, cptr, &me,
1701           "No Access (passwd mismatch) %s", cptr->name);
1702     }
1703 #endif /* not GODMODE */
1704     memset(cptr->passwd, 0, sizeof(cptr->passwd));
1705
1706 #ifndef HUB
1707     for (i = 0; i <= HighestFd; i++)
1708       if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
1709         active_lh_line = 3;
1710         LHcptr = 0;
1711         break;
1712       }
1713 #endif
1714   }
1715
1716   /*
1717    *  We want to find IsConnecting() and IsHandshake() too,
1718    *  use FindClient().
1719    *  The second finds collisions with numeric representation of existing
1720    *  servers - these shouldn't happen anymore when all upgraded to 2.10.
1721    *  -- Run
1722    */
1723   while ((acptr = FindClient(host)) || 
1724          (parc > 7 && (acptr = FindNServer(parv[6]))))
1725   {
1726     /*
1727      *  This link is trying feed me a server that I already have
1728      *  access through another path
1729      *
1730      *  Do not allow Uworld to do this.
1731      *  Do not allow servers that are juped.
1732      *  Do not allow servers that have older link timestamps
1733      *    then this try.
1734      *  Do not allow servers that use the same numeric as an existing
1735      *    server, but have a different name.
1736      *
1737      *  If my ircd.conf sucks, I can try to connect to myself:
1738      */
1739     if (acptr == &me)
1740       return exit_client_msg(cptr, cptr, &me,
1741           "nick collision with me, check server number in M:? (%s)", host);
1742     /*
1743      * Detect wrong numeric.
1744      */
1745     if (0 != ircd_strcmp(acptr->name, host))
1746     {
1747       sendto_serv_butone(cptr, /* XXX DEAD */
1748           ":%s WALLOPS :SERVER Numeric Collision: %s != %s",
1749           me.name, acptr->name, host);
1750       return exit_client_msg(cptr, cptr, &me,
1751           "NUMERIC collision between %s and %s."
1752           " Is your server numeric correct ?", host, acptr->name);
1753     }
1754     /*
1755      *  Kill our try, if we had one.
1756      */
1757     if (IsConnecting(acptr))
1758     {
1759       if (!active_lh_line && exit_client(cptr, acptr, &me,
1760           "Just connected via another link") == CPTR_KILLED)
1761         return CPTR_KILLED;
1762       /*
1763        * We can have only ONE 'IsConnecting', 'IsHandshake' or
1764        * 'IsServer', because new 'IsConnecting's are refused to
1765        * the same server if we already had it.
1766        */
1767       break;
1768     }
1769     /*
1770      * Avoid other nick collisions...
1771      * This is a doubtfull test though, what else would it be
1772      * when it has a server.name ?
1773      */
1774     else if (!IsServer(acptr) && !IsHandshake(acptr))
1775       return exit_client_msg(cptr, cptr, &me,
1776           "Nickname %s already exists!", host);
1777     /*
1778      * Our new server might be a juped server,
1779      * or someone trying abuse a second Uworld:
1780      */
1781     else if (IsServer(acptr) && (0 == ircd_strncmp(acptr->info, "JUPE", 4) ||
1782         find_conf_byhost(cptr->confs, acptr->name, CONF_UWORLD)))
1783     {
1784       if (!IsServer(sptr))
1785         return exit_client(cptr, sptr, &me, acptr->info);
1786       sendto_one(cptr, ":%s WALLOPS :Received :%s SERVER %s from %s !?!", /* XXX DEAD */
1787           me.name, parv[0], parv[1], cptr->name);
1788       return exit_new_server(cptr, sptr, host, timestamp, "%s", acptr->info);
1789     }
1790     /*
1791      * Of course we find the handshake this link was before :)
1792      */
1793     else if (IsHandshake(acptr) && acptr == cptr)
1794       break;
1795     /*
1796      * Here we have a server nick collision...
1797      * We don't want to kill the link that was last /connected,
1798      * but we neither want to kill a good (old) link.
1799      * Therefor we kill the second youngest link.
1800      */
1801     if (1)
1802     {
1803       struct Client* c2ptr = 0;
1804       struct Client* c3ptr = acptr;
1805       struct Client* ac2ptr;
1806       struct Client* ac3ptr;
1807
1808       /* Search youngest link: */
1809       for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1810         if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1811           c3ptr = ac3ptr;
1812       if (IsServer(sptr))
1813       {
1814         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = ac3ptr->serv->up)
1815           if (ac3ptr->serv->timestamp > c3ptr->serv->timestamp)
1816             c3ptr = ac3ptr;
1817       }
1818       if (timestamp > c3ptr->serv->timestamp)
1819       {
1820         c3ptr = 0;
1821         c2ptr = acptr;          /* Make sure they differ */
1822       }
1823       /* Search second youngest link: */
1824       for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1825         if (ac2ptr != c3ptr &&
1826             ac2ptr->serv->timestamp >
1827             (c2ptr ? c2ptr->serv->timestamp : timestamp))
1828           c2ptr = ac2ptr;
1829       if (IsServer(sptr))
1830       {
1831         for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = ac2ptr->serv->up)
1832           if (ac2ptr != c3ptr &&
1833               ac2ptr->serv->timestamp >
1834               (c2ptr ? c2ptr->serv->timestamp : timestamp))
1835             c2ptr = ac2ptr;
1836       }
1837       if (c3ptr && timestamp > (c2ptr ? c2ptr->serv->timestamp : timestamp))
1838         c2ptr = 0;
1839       /* If timestamps are equal, decide which link to break
1840        *  by name.
1841        */
1842       if ((c2ptr ? c2ptr->serv->timestamp : timestamp) ==
1843           (c3ptr ? c3ptr->serv->timestamp : timestamp))
1844       {
1845         char* n2;
1846         char* n2up;
1847         char* n3;
1848         char* n3up;
1849         if (c2ptr)
1850         {
1851           n2 = c2ptr->name;
1852           n2up = MyConnect(c2ptr) ? me.name : c2ptr->serv->up->name;
1853         }
1854         else
1855         {
1856           n2 = host;
1857           n2up = IsServer(sptr) ? sptr->name : me.name;
1858         }
1859         if (c3ptr)
1860         {
1861           n3 = c3ptr->name;
1862           n3up = MyConnect(c3ptr) ? me.name : c3ptr->serv->up->name;
1863         }
1864         else
1865         {
1866           n3 = host;
1867           n3up = IsServer(sptr) ? sptr->name : me.name;
1868         }
1869         if (strcmp(n2, n2up) > 0)
1870           n2 = n2up;
1871         if (strcmp(n3, n3up) > 0)
1872           n3 = n3up;
1873         if (strcmp(n3, n2) > 0)
1874         {
1875           ac2ptr = c2ptr;
1876           c2ptr = c3ptr;
1877           c3ptr = ac2ptr;
1878         }
1879       }
1880       /* Now squit the second youngest link: */
1881       if (!c2ptr)
1882         return exit_new_server(cptr, sptr, host, timestamp,
1883             "server %s already exists and is %ld seconds younger.",
1884             host, (long)acptr->serv->timestamp - (long)timestamp);
1885       else if (c2ptr->from == cptr || IsServer(sptr))
1886       {
1887         struct Client *killedptrfrom = c2ptr->from;
1888         if (active_lh_line)
1889         {
1890           /*
1891            * If the L: or H: line also gets rid of this link,
1892            * we sent just one squit.
1893            */
1894           if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
1895             break;
1896           /*
1897            * If breaking the loop here solves the L: or H:
1898            * line problem, we don't squit that.
1899            */
1900           if (c2ptr->from == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
1901             active_lh_line = 0;
1902           else
1903           {
1904             /*
1905              * If we still have a L: or H: line problem,
1906              * we prefer to squit the new server, solving
1907              * loop and L:/H: line problem with only one squit.
1908              */
1909             LHcptr = 0;
1910             break;
1911           }
1912         }
1913         /*
1914          * If the new server was introduced by a server that caused a
1915          * Ghost less then 20 seconds ago, this is probably also
1916          * a Ghost... (20 seconds is more then enough because all
1917          * SERVER messages are at the beginning of a net.burst). --Run
1918          */
1919         if (CurrentTime - cptr->serv->ghost < 20)
1920         {
1921           killedptrfrom = acptr->from;
1922           if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
1923             return CPTR_KILLED;
1924         }
1925         else if (exit_client_msg(cptr, c2ptr, &me,
1926             "Loop <-- %s (new link is %ld seconds younger)", host,
1927             (c3ptr ? (long)c3ptr->serv->timestamp : timestamp) -
1928             (long)c2ptr->serv->timestamp) == CPTR_KILLED)
1929           return CPTR_KILLED;
1930         /*
1931          * Did we kill the incoming server off already ?
1932          */
1933         if (killedptrfrom == cptr)
1934           return 0;
1935       }
1936       else
1937       {
1938         if (active_lh_line)
1939         {
1940           if (LHcptr && a_kills_b_too(LHcptr, acptr))
1941             break;
1942           if (acptr->from == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1943             active_lh_line = 0;
1944           else
1945           {
1946             LHcptr = 0;
1947             break;
1948           }
1949         }
1950         /*
1951          * We can't believe it is a lagged server message
1952          * when it directly connects to us...
1953          * kill the older link at the ghost, rather then
1954          * at the second youngest link, assuming it isn't
1955          * a REAL loop.
1956          */
1957         ghost = CurrentTime;            /* Mark that it caused a ghost */
1958         if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1959           return CPTR_KILLED;
1960         break;
1961       }
1962     }
1963   }
1964
1965   if (active_lh_line)
1966   {
1967     if (LHcptr == 0) {
1968       return exit_new_server(cptr, sptr, host, timestamp,
1969           (active_lh_line == 2) ?  "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1970           cptr->name, host,
1971           lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1972     }
1973     else
1974     {
1975       int killed = a_kills_b_too(LHcptr, sptr);
1976       if (active_lh_line < 3)
1977       {
1978         if (exit_client_msg(cptr, LHcptr, &me,
1979             (active_lh_line == 2) ?  "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1980             cptr->name, host,
1981             lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1982           return CPTR_KILLED;
1983       }
1984       else
1985       {
1986         ServerStats->is_ref++;
1987         if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1988           return CPTR_KILLED;
1989       }
1990       /*
1991        * Did we kill the incoming server off already ?
1992        */
1993       if (killed)
1994         return 0;
1995     }
1996   }
1997
1998   if (IsServer(cptr))
1999   {
2000     /*
2001      * Server is informing about a new server behind
2002      * this link. Create REMOTE server structure,
2003      * add it to list and propagate word to my other
2004      * server links...
2005      */
2006
2007     acptr = make_client(cptr, STAT_SERVER);
2008     make_server(acptr);
2009     acptr->serv->prot = prot;
2010     acptr->serv->timestamp = timestamp;
2011     acptr->hopcount = hop;
2012     ircd_strncpy(acptr->name, host, HOSTLEN);
2013     ircd_strncpy(acptr->info, info, REALLEN);
2014     acptr->serv->up = sptr;
2015     acptr->serv->updown = add_dlink(&sptr->serv->down, acptr);
2016     /* Use cptr, because we do protocol 9 -> 10 translation
2017        for numeric nicks ! */
2018     SetServerYXX(cptr, acptr, parv[6]);
2019
2020     Count_newremoteserver(UserStats);
2021     if (Protocol(acptr) < 10)
2022       acptr->flags |= FLAGS_TS8;
2023     add_client_to_list(acptr);
2024     hAddClient(acptr);
2025     if (*parv[5] == 'J')
2026     {
2027       SetBurst(acptr);
2028       sendto_op_mask(SNO_NETWORK, "Net junction: %s %s", /* XXX DEAD */
2029           sptr->name, acptr->name);
2030       SetJunction(acptr);
2031     }
2032     /*
2033      * Old sendto_serv_but_one() call removed because we now need to send
2034      * different names to different servers (domain name matching).
2035      */
2036     for (i = 0; i <= HighestFd; i++)
2037     {
2038       if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
2039           bcptr == cptr || IsMe(bcptr))
2040         continue;
2041       if (0 == match(me.name, acptr->name))
2042         continue;
2043         sendto_one(bcptr, "%s " TOK_SERVER " %s %d 0 %s %s %s%s 0 :%s", /* XXX DEAD */
2044             NumServ(sptr), acptr->name, hop + 1, parv[4], parv[5],
2045             NumServCap(acptr), acptr->info);
2046     }
2047     return 0;
2048   }
2049
2050   if (IsUnknown(cptr) || IsHandshake(cptr))
2051   {
2052     make_server(cptr);
2053     cptr->serv->timestamp = timestamp;
2054     cptr->serv->prot = prot;
2055     cptr->serv->ghost = ghost;
2056     SetServerYXX(cptr, cptr, parv[6]);
2057     if (start_timestamp > OLDEST_TS)
2058     {
2059 #ifndef RELIABLE_CLOCK
2060 #ifdef TESTNET
2061       sendto_ops("Debug: my start time: " TIME_T_FMT " ; others start time: " /* XXX DEAD */
2062           TIME_T_FMT, me.serv->timestamp, start_timestamp);
2063       sendto_ops("Debug: receive time: " TIME_T_FMT " ; received timestamp: " /* XXX DEAD */
2064           TIME_T_FMT " ; difference %ld",
2065           recv_time, timestamp, timestamp - recv_time);
2066 #endif
2067       if (start_timestamp < me.serv->timestamp)
2068       {
2069         sendto_ops("got earlier start time: " TIME_T_FMT " < " TIME_T_FMT, /* XXX DEAD */
2070             start_timestamp, me.serv->timestamp);
2071         me.serv->timestamp = start_timestamp;
2072         TSoffset += timestamp - recv_time;
2073         sendto_ops("clock adjusted by adding %d", (int)(timestamp - recv_time)); /* XXX DEAD */
2074       }
2075       else if ((start_timestamp > me.serv->timestamp) && IsUnknown(cptr))
2076         cptr->serv->timestamp = TStime();
2077
2078       else if (timestamp != recv_time)
2079       {
2080         /*
2081          * Equal start times, we have a collision.  Let the connected-to server
2082          * decide. This assumes leafs issue more than half of the connection
2083          * attempts.
2084          */
2085         if (IsUnknown(cptr))
2086           cptr->serv->timestamp = TStime();
2087         else if (IsHandshake(cptr))
2088         {
2089           sendto_ops("clock adjusted by adding %d", /* XXX DEAD */
2090               (int)(timestamp - recv_time));
2091           TSoffset += timestamp - recv_time;
2092         }
2093       }
2094 #else /* RELIABLE CLOCK IS TRUE, we _always_ use our own clock */
2095       if (start_timestamp < me.serv->timestamp)
2096         me.serv->timestamp = start_timestamp;
2097       if (IsUnknown(cptr))
2098         cptr->serv->timestamp = TStime();
2099 #endif
2100     }
2101
2102     ret = server_estab(cptr, aconf);
2103   }
2104   else
2105     ret = 0;
2106 #ifdef RELIABLE_CLOCK
2107   if (abs(cptr->serv->timestamp - recv_time) > 30)
2108   {
2109     sendto_ops("Connected to a net with a timestamp-clock" /* XXX DEAD */
2110         " difference of " STIME_T_FMT " seconds! Used SETTIME to correct"
2111         " this.", timestamp - recv_time);
2112     sendto_one(cptr, ":%s SETTIME " TIME_T_FMT " :%s", /* XXX DEAD */
2113         me.name, TStime(), me.name);
2114   }
2115 #endif
2116
2117   return ret;
2118 }
2119 #endif /* 0 */
2120