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