Clarify the "I'm a leaf, define HUB" message.
[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 /** @file
24  * @brief Handlers for the SERVER command.
25  * @version $Id$
26  */
27
28 #include "config.h"
29
30 #include "client.h"
31 #include "hash.h"
32 #include "ircd.h"
33 #include "ircd_log.h"
34 #include "ircd_features.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
37 #include "jupe.h"
38 #include "list.h"
39 #include "match.h"
40 #include "msg.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "querycmds.h"
44 #include "s_bsd.h"
45 #include "s_conf.h"
46 #include "s_debug.h"
47 #include "s_misc.h"
48 #include "s_serv.h"
49 #include "send.h"
50 #include "userload.h"
51
52 /* #include <assert.h> -- Now using assert in ircd_log.h */
53 #include <stdlib.h>
54 #include <string.h>
55
56 /** Clean up a server name.
57  * @param[in] host Input server name.
58  * @return NULL if the name is invalid, else pointer to cleaned-up name.
59  */
60 static char *
61 clean_servername(char *host)
62 {
63   char*            ch;
64   /*
65    * Check for "FRENCH " infection ;-) (actually this should
66    * be replaced with routine to check the hostname syntax in
67    * general). [ This check is still needed, even after the parse
68    * is fixed, because someone can send "SERVER :foo bar " ].
69    * Also, changed to check other "difficult" characters, now
70    * that parse lets all through... --msa
71    */
72   if (strlen(host) > HOSTLEN)
73     host[HOSTLEN] = '\0';
74
75   for (ch = host; *ch; ch++)
76     if (*ch <= ' ' || *ch > '~')
77       break;
78   if (*ch || !strchr(host, '.') || strlen(host) > HOSTLEN)
79     return NULL;
80   return host;
81 }
82
83 /** Parse protocol version from a string.
84  * @param[in] proto String version of protocol number.
85  * @return Zero if \a proto is unrecognized, else protocol version.
86  */
87 static unsigned short
88 parse_protocol(const char *proto)
89 {
90   unsigned short prot;
91   if (strlen(proto) != 3 || (proto[0] != 'P' && proto[0] != 'J'))
92     return 0;
93   prot = atoi(proto+1);
94   if (prot > atoi(MAJOR_PROTOCOL))
95     prot = atoi(MAJOR_PROTOCOL);
96   return prot;
97 }
98
99 /** Reason not to accept a server's new announcement. */
100 enum lh_type {
101   ALLOWED, /**< The new server link is accepted. */
102   MAX_HOPS_EXCEEDED, /**< The path to the server is too long. */
103   NOT_ALLOWED_TO_HUB, /**< My peer is not allowed to hub for the server. */
104   I_AM_NOT_HUB /**< I have another active server link but not FEAT_HUB. */
105 };
106
107 /** Check whether the introduction of a new server would cause a loop
108  * or be disallowed by leaf and hub configuration directives.
109  * @param[in] cptr Neighbor who sent the message.
110  * @param[in] sptr Client that originated the message.
111  * @param[out] ghost If non-NULL, receives ghost timestamp for new server.
112  * @param[in] host Name of new server.
113  * @param[in] numnick Numnick mask of new server.
114  * @param[in] timestamp Claimed link timestamp of new server.
115  * @param[in] hop Number of hops to the new server.
116  * @param[in] junction Non-zero if the new server is still bursting.
117  * @return CPTR_KILLED if \a cptr was SQUIT.  0 if some other server
118  * was SQUIT.  1 if the new server is allowed.
119  */
120 static int
121 check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const char *host, const char *numnick, time_t timestamp, int hop, int junction)
122 {
123   struct Client* acptr;
124   struct Client* LHcptr = NULL;
125   struct ConfItem* lhconf;
126   enum lh_type active_lh_line = ALLOWED;
127   int ii;
128
129   if (ghost)
130     *ghost = 0;
131
132   /*
133    * Calculate type of connect limit and applicable config item.
134    */
135   lhconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_SERVER);
136   assert(lhconf != NULL);
137   if (ghost)
138   {
139     if (!feature_bool(FEAT_HUB))
140       for (ii = 0; ii <= HighestFd; ii++)
141         if (LocalClientArray[ii] && IsServer(LocalClientArray[ii])) {
142           active_lh_line = I_AM_NOT_HUB;
143           break;
144         }
145   }
146   else if (hop > lhconf->maximum)
147   {
148     /* Because "maximum" should be 0 for non-hub links, check whether
149      * there is a hub mask -- if not, complain that the server isn't
150      * allowed to hub.
151      */
152     active_lh_line = lhconf->hub_limit ? MAX_HOPS_EXCEEDED : NOT_ALLOWED_TO_HUB;
153   }
154   else if (lhconf->hub_limit && match(lhconf->hub_limit, host))
155   {
156     struct Client *ac3ptr;
157     active_lh_line = NOT_ALLOWED_TO_HUB;
158     if (junction)
159       for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
160         if (IsJunction(ac3ptr)) {
161           LHcptr = ac3ptr;
162           break;
163         }
164   }
165
166   /*
167    *  We want to find IsConnecting() and IsHandshake() too,
168    *  use FindClient().
169    *  The second finds collisions with numeric representation of existing
170    *  servers - these shouldn't happen anymore when all upgraded to 2.10.
171    *  -- Run
172    */
173   while ((acptr = FindClient(host))
174          || (numnick && (acptr = FindNServer(numnick))))
175   {
176     /*
177      *  This link is trying feed me a server that I already have
178      *  access through another path
179      *
180      *  Do not allow Uworld to do this.
181      *  Do not allow servers that are juped.
182      *  Do not allow servers that have older link timestamps
183      *    then this try.
184      *  Do not allow servers that use the same numeric as an existing
185      *    server, but have a different name.
186      *
187      *  If my ircd.conf sucks, I can try to connect to myself:
188      */
189     if (acptr == &me)
190       return exit_client_msg(cptr, cptr, &me, "nick collision with me (%s), check server number in M:?", host);
191     /*
192      * Detect wrong numeric.
193      */
194     if (0 != ircd_strcmp(cli_name(acptr), host))
195     {
196       sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
197                             ":SERVER Numeric Collision: %s != %s",
198                             cli_name(acptr), host);
199       return exit_client_msg(cptr, cptr, &me,
200           "NUMERIC collision between %s and %s."
201           " Is your server numeric correct ?", host, cli_name(acptr));
202     }
203     /*
204      *  Kill our try, if we had one.
205      */
206     if (IsConnecting(acptr))
207     {
208       if (active_lh_line == ALLOWED && exit_client(cptr, acptr, &me,
209           "Just connected via another link") == CPTR_KILLED)
210         return CPTR_KILLED;
211       /*
212        * We can have only ONE 'IsConnecting', 'IsHandshake' or
213        * 'IsServer', because new 'IsConnecting's are refused to
214        * the same server if we already had it.
215        */
216       break;
217     }
218     /*
219      * Avoid other nick collisions...
220      * This is a doubtful test though, what else would it be
221      * when it has a server.name ?
222      */
223     else if (!IsServer(acptr) && !IsHandshake(acptr))
224       return exit_client_msg(cptr, cptr, &me,
225                              "Nickname %s already exists!", host);
226     /*
227      * Our new server might be a juped server,
228      * or someone trying abuse a second Uworld:
229      */
230     else if (IsServer(acptr) && (0 == ircd_strncmp(cli_info(acptr), "JUPE", 4) ||
231         find_conf_byhost(cli_confs(cptr), cli_name(acptr), CONF_UWORLD)))
232     {
233       if (!IsServer(sptr))
234         return exit_client(cptr, sptr, &me, cli_info(acptr));
235       sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
236                             ":Received :%s SERVER %s from %s !?!",
237                             NumServ(cptr), host, cli_name(cptr));
238       return exit_new_server(cptr, sptr, host, timestamp, "%s", cli_info(acptr));
239     }
240     /*
241      * Of course we find the handshake this link was before :)
242      */
243     else if (IsHandshake(acptr) && acptr == cptr)
244       break;
245     /*
246      * Here we have a server nick collision...
247      * We don't want to kill the link that was last /connected,
248      * but we neither want to kill a good (old) link.
249      * Therefor we kill the second youngest link.
250      */
251     if (1)
252     {
253       struct Client* c2ptr = 0;
254       struct Client* c3ptr = acptr;
255       struct Client* ac2ptr;
256       struct Client* ac3ptr;
257
258       /* Search youngest link: */
259       for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
260         if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
261           c3ptr = ac3ptr;
262       if (IsServer(sptr))
263       {
264         for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
265           if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
266             c3ptr = ac3ptr;
267       }
268       if (timestamp > cli_serv(c3ptr)->timestamp)
269       {
270         c3ptr = 0;
271         c2ptr = acptr;          /* Make sure they differ */
272       }
273       /* Search second youngest link: */
274       for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
275         if (ac2ptr != c3ptr &&
276             cli_serv(ac2ptr)->timestamp >
277             (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
278           c2ptr = ac2ptr;
279       if (IsServer(sptr))
280       {
281         for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
282           if (ac2ptr != c3ptr &&
283               cli_serv(ac2ptr)->timestamp >
284               (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
285             c2ptr = ac2ptr;
286       }
287       if (c3ptr && timestamp > (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
288         c2ptr = 0;
289       /* If timestamps are equal, decide which link to break
290        *  by name.
291        */
292       if ((c2ptr ? cli_serv(c2ptr)->timestamp : timestamp) ==
293           (c3ptr ? cli_serv(c3ptr)->timestamp : timestamp))
294       {
295         const char *n2, *n2up, *n3, *n3up;
296         if (c2ptr)
297         {
298           n2 = cli_name(c2ptr);
299           n2up = MyConnect(c2ptr) ? cli_name(&me) : cli_name(cli_serv(c2ptr)->up);
300         }
301         else
302         {
303           n2 = host;
304           n2up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
305         }
306         if (c3ptr)
307         {
308           n3 = cli_name(c3ptr);
309           n3up = MyConnect(c3ptr) ? cli_name(&me) : cli_name(cli_serv(c3ptr)->up);
310         }
311         else
312         {
313           n3 = host;
314           n3up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
315         }
316         if (strcmp(n2, n2up) > 0)
317           n2 = n2up;
318         if (strcmp(n3, n3up) > 0)
319           n3 = n3up;
320         if (strcmp(n3, n2) > 0)
321         {
322           ac2ptr = c2ptr;
323           c2ptr = c3ptr;
324           c3ptr = ac2ptr;
325         }
326       }
327       /* Now squit the second youngest link: */
328       if (!c2ptr)
329         return exit_new_server(cptr, sptr, host, timestamp,
330                                "server %s already exists and is %ld seconds younger.",
331                                host, (long)cli_serv(acptr)->timestamp - (long)timestamp);
332       else if (cli_from(c2ptr) == cptr || IsServer(sptr))
333       {
334         struct Client *killedptrfrom = cli_from(c2ptr);
335         if (active_lh_line != ALLOWED)
336         {
337           /*
338            * If the L: or H: line also gets rid of this link,
339            * we sent just one squit.
340            */
341           if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
342             break;
343           /*
344            * If breaking the loop here solves the L: or H:
345            * line problem, we don't squit that.
346            */
347           if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
348             active_lh_line = ALLOWED;
349           else
350           {
351             /*
352              * If we still have a L: or H: line problem,
353              * we prefer to squit the new server, solving
354              * loop and L:/H: line problem with only one squit.
355              */
356             LHcptr = 0;
357             break;
358           }
359         }
360         /*
361          * If the new server was introduced by a server that caused a
362          * Ghost less then 20 seconds ago, this is probably also
363          * a Ghost... (20 seconds is more then enough because all
364          * SERVER messages are at the beginning of a net.burst). --Run
365          */
366         if (CurrentTime - cli_serv(cptr)->ghost < 20)
367         {
368           killedptrfrom = cli_from(acptr);
369           if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
370             return CPTR_KILLED;
371         }
372         else if (exit_client_msg(cptr, c2ptr, &me,
373             "Loop <-- %s (new link is %ld seconds younger)", host,
374             (c3ptr ? (long)cli_serv(c3ptr)->timestamp : timestamp) -
375             (long)cli_serv(c2ptr)->timestamp) == CPTR_KILLED)
376           return CPTR_KILLED;
377         /*
378          * Did we kill the incoming server off already ?
379          */
380         if (killedptrfrom == cptr)
381           return 0;
382       }
383       else
384       {
385         if (active_lh_line != ALLOWED)
386         {
387           if (LHcptr && a_kills_b_too(LHcptr, acptr))
388             break;
389           if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
390             active_lh_line = ALLOWED;
391           else
392           {
393             LHcptr = 0;
394             break;
395           }
396         }
397         /*
398          * We can't believe it is a lagged server message
399          * when it directly connects to us...
400          * kill the older link at the ghost, rather then
401          * at the second youngest link, assuming it isn't
402          * a REAL loop.
403          */
404         if (ghost)
405           *ghost = CurrentTime;            /* Mark that it caused a ghost */
406         if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
407           return CPTR_KILLED;
408         break;
409       }
410     }
411   }
412
413   if (active_lh_line != ALLOWED)
414   {
415     if (!LHcptr)
416       LHcptr = sptr;
417     if (active_lh_line == MAX_HOPS_EXCEEDED)
418     {
419       return exit_client_msg(cptr, LHcptr, &me,
420                              "Maximum hops exceeded for %s at %s",
421                              cli_name(cptr), host);
422     }
423     else if (active_lh_line == NOT_ALLOWED_TO_HUB)
424     {
425       return exit_client_msg(cptr, LHcptr, &me,
426                              "%s is not allowed to hub for %s",
427                              cli_name(cptr), host);
428     }
429     else /* I_AM_NOT_HUB */
430     {
431       ServerStats->is_ref++;
432       return exit_client(cptr, LHcptr, &me, "I'm a leaf, define the HUB feature");
433     }
434   }
435
436   return 1;
437 }
438
439 /** Update server start timestamps and TS offsets.
440  * @param[in] cptr Server that just connected.
441  * @param[in] timestamp Current time according to \a cptr.
442  * @param[in] start_timestamp Time that \a cptr started.
443  * @param[in] recv_time Current time as we know it.
444  */
445 static void
446 check_start_timestamp(struct Client *cptr, time_t timestamp, time_t start_timestamp, time_t recv_time)
447 {
448   Debug((DEBUG_DEBUG, "My start time: %Tu; other's start time: %Tu",
449          cli_serv(&me)->timestamp, start_timestamp));
450   Debug((DEBUG_DEBUG, "Receive time: %Tu; received timestamp: %Tu; "
451          "difference %ld", recv_time, timestamp, timestamp - recv_time));
452   if (feature_bool(FEAT_RELIABLE_CLOCK)) {
453     if (start_timestamp < cli_serv(&me)->timestamp)
454       cli_serv(&me)->timestamp = start_timestamp;
455     if (IsUnknown(cptr))
456       cli_serv(cptr)->timestamp = TStime();
457   } else if (start_timestamp < cli_serv(&me)->timestamp) {
458     sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
459                          "%Tu < %Tu", start_timestamp,
460                          cli_serv(&me)->timestamp);
461     cli_serv(&me)->timestamp = start_timestamp;
462     TSoffset += timestamp - recv_time;
463     sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
464                          (int)(timestamp - recv_time));
465   } else if ((start_timestamp > cli_serv(&me)->timestamp) &&
466              IsUnknown(cptr)) {
467     cli_serv(cptr)->timestamp = TStime();
468   } else if (timestamp != recv_time) {
469     /*
470      * Equal start times, we have a collision.  Let the connected-to
471      * server decide. This assumes leafs issue more than half of the
472      * connection attempts.
473      */
474     if (IsUnknown(cptr))
475       cli_serv(cptr)->timestamp = TStime();
476     else if (IsHandshake(cptr)) {
477       sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
478                            (int)(timestamp - recv_time));
479       TSoffset += timestamp - recv_time;
480     }
481   }
482 }
483
484 /** Interpret a server's flags.
485  *
486  * @param[in] cptr New server structure.
487  * @param[in] flags String listing server's P10 flags.
488  */
489 void set_server_flags(struct Client *cptr, const char *flags)
490 {
491     while (*flags) switch (*flags++) {
492     case 'h': SetHub(cptr); break;
493     case 's': SetService(cptr); break;
494     case '6': SetIPv6(cptr); break;
495     }
496 }
497
498 /** Handle a SERVER message from an unregistered connection.
499  *
500  * \a parv has the following elements:
501  * \li \a parv[1] is the server name
502  * \li \a parv[2] is the hop count to the server
503  * \li \a parv[3] is the start timestamp for the server
504  * \li \a parv[4] is the link timestamp
505  * \li \a parv[5] is the protocol version (P10 or J10)
506  * \li \a parv[6] is the numnick mask for the server
507  * \li \a parv[7] is a string of flags like +hs to mark hubs and services
508  * \li \a parv[\a parc - 1] is the server description
509  *
510  * See @ref m_functions for discussion of the arguments.
511  * @param[in] cptr Client that sent us the message.
512  * @param[in] sptr Original source of message.
513  * @param[in] parc Number of arguments.
514  * @param[in] parv Argument vector.
515  */
516 int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
517 {
518   char*            host;
519   struct ConfItem* aconf;
520   struct Jupe*     ajupe;
521   int              hop;
522   int              ret;
523   unsigned short   prot;
524   time_t           start_timestamp;
525   time_t           timestamp;
526   time_t           recv_time;
527   time_t           ghost;
528
529   if (IsUserPort(cptr))
530     return exit_client_msg(cptr, cptr, &me,
531                            "Cannot connect a server to a user port");
532
533   if (parc < 8)
534   {
535     need_more_params(sptr, "SERVER");
536     return exit_client(cptr, cptr, &me, "Need more parameters");
537   }
538   host = clean_servername(parv[1]);
539   if (!host)
540   {
541     sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
542                          host, cli_name(cptr));
543     return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
544   }
545
546   if ((ajupe = jupe_find(host)) && JupeIsActive(ajupe))
547     return exit_client_msg(cptr, sptr, &me, "Juped: %s", JupeReason(ajupe));
548
549   /* check connection rules */
550   if (0 != conf_eval_crule(host, CRULE_ALL)) {
551     ServerStats->is_ref++;
552     sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.", cli_name(cptr));
553     return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
554   }
555
556   log_write(LS_NETWORK, L_NOTICE, LOG_NOSNOTICE, "SERVER: %s %s[%s]", host,
557             cli_sockhost(cptr), cli_sock_ip(cptr));
558
559   /*
560    * Detect protocol
561    */
562   hop = atoi(parv[2]);
563   start_timestamp = atoi(parv[3]);
564   timestamp = atoi(parv[4]);
565   prot = parse_protocol(parv[5]);
566   if (!prot)
567     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
568   else if (prot < atoi(MINOR_PROTOCOL))
569     return exit_new_server(cptr, sptr, host, timestamp,
570                            "Incompatible protocol: %s", parv[5]);
571
572   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
573          host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
574
575   if (timestamp < OLDEST_TS || start_timestamp < OLDEST_TS)
576     return exit_client_msg(cptr, sptr, &me,
577         "Bogus timestamps (%s %s)", parv[3], parv[4]);
578
579   /* If the server had a different name before, change it. */
580   if (!EmptyString(cli_name(cptr)) &&
581       (IsUnknown(cptr) || IsHandshake(cptr)) &&
582       0 != ircd_strcmp(cli_name(cptr), host))
583     hChangeClient(cptr, host);
584   ircd_strncpy(cli_name(cptr), host, HOSTLEN);
585   ircd_strncpy(cli_info(cptr), parv[parc-1][0] ? parv[parc-1] : cli_name(&me), REALLEN);
586   cli_hopcount(cptr) = hop;
587
588   if (conf_check_server(cptr)) {
589     ++ServerStats->is_ref;
590     sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
591                          "from %s.", cli_name(cptr));
592     log_write(LS_NETWORK, L_NOTICE, LOG_NOSNOTICE, "Received unauthorized "
593               "connection from %C [%s]", cptr,
594               ircd_ntoa(&cli_ip(cptr)));
595     return exit_client(cptr, cptr, &me, "No Connect block");
596   }
597
598   host = cli_name(cptr);
599
600   update_load();
601
602   if (!(aconf = find_conf_byname(cli_confs(cptr), host, CONF_SERVER))) {
603     ++ServerStats->is_ref;
604     sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
605                          "server %s", cli_name(cptr));
606     return exit_client_msg(cptr, cptr, &me,
607                            "Access denied. No conf line for server %s", cli_name(cptr));
608   }
609
610   if (*aconf->passwd && !!strcmp(aconf->passwd, cli_passwd(cptr))) {
611     ++ServerStats->is_ref;
612     sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
613                          cli_name(cptr));
614     return exit_client_msg(cptr, cptr, &me,
615                            "No Access (passwd mismatch) %s", cli_name(cptr));
616   }
617
618   memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr)));
619
620   ret = check_loop_and_lh(cptr, sptr, &ghost, host, (parc > 7 ? parv[6] : NULL), timestamp, hop, 1);
621   if (ret != 1)
622     return ret;
623
624   make_server(cptr);
625   cli_serv(cptr)->timestamp = timestamp;
626   cli_serv(cptr)->prot = prot;
627   cli_serv(cptr)->ghost = ghost;
628   memset(cli_privs(cptr), 255, sizeof(struct Privs));
629   ClrPriv(cptr, PRIV_SET);
630   SetServerYXX(cptr, cptr, parv[6]);
631
632   /* Attach any necessary UWorld config items. */
633   attach_confs_byhost(cptr, host, CONF_UWORLD);
634
635   if (*parv[7] == '+')
636     set_server_flags(cptr, parv[7] + 1);
637
638   recv_time = TStime();
639   check_start_timestamp(cptr, timestamp, start_timestamp, recv_time);
640   ret = server_estab(cptr, aconf);
641
642   if (feature_bool(FEAT_RELIABLE_CLOCK) &&
643       abs(cli_serv(cptr)->timestamp - recv_time) > 30) {
644     sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
645                          "timestamp-clock difference of %Td seconds! "
646                          "Used SETTIME to correct this.",
647                          timestamp - recv_time);
648     sendcmdto_prio_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(),
649                        cli_name(&me));
650   }
651
652   return ret;
653 }
654
655 /** Handle a SERVER message from another server.
656  *
657  * \a parv has the following elements:
658  * \li \a parv[1] is the server name
659  * \li \a parv[2] is the hop count to the server
660  * \li \a parv[3] is the start timestamp for the server
661  * \li \a parv[4] is the link timestamp
662  * \li \a parv[5] is the protocol version (P10 or J10)
663  * \li \a parv[6] is the numnick mask for the server
664  * \li \a parv[7] is a string of flags like +hs to mark hubs and services
665  * \li \a parv[\a parc - 1] is the server description
666  *
667  * See @ref m_functions for discussion of the arguments.
668  * @param[in] cptr Client that sent us the message.
669  * @param[in] sptr Original source of message.
670  * @param[in] parc Number of arguments.
671  * @param[in] parv Argument vector.
672  */
673 int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
674 {
675   int              i;
676   char*            host;
677   struct Client*   acptr;
678   struct Client*   bcptr;
679   int              hop;
680   int              ret;
681   unsigned short   prot;
682   time_t           start_timestamp;
683   time_t           timestamp;
684
685   if (parc < 8)
686   {
687     return need_more_params(sptr, "SERVER");
688     return exit_client(cptr, cptr, &me, "Need more parameters");
689   }
690   host = clean_servername(parv[1]);
691   if (!host)
692   {
693     sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
694                          host, cli_name(cptr));
695     return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
696   }
697
698   /*
699    * Detect protocol
700    */
701   hop = atoi(parv[2]);
702   start_timestamp = atoi(parv[3]);
703   timestamp = atoi(parv[4]);
704   prot = parse_protocol(parv[5]);
705   if (!prot)
706     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
707   else if (prot < atoi(MINOR_PROTOCOL))
708     return exit_new_server(cptr, sptr, host, timestamp,
709                            "Incompatible protocol: %s", parv[5]);
710
711   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
712          host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
713
714   if (timestamp < OLDEST_TS)
715     return exit_client_msg(cptr, sptr, &me,
716         "Bogus timestamps (%s %s)", parv[3], parv[4]);
717
718   if (parv[parc - 1][0] == '\0')
719     return exit_client_msg(cptr, cptr, &me,
720                            "No server info specified for %s", host);
721
722   ret = check_loop_and_lh(cptr, sptr, NULL, host, (parc > 7 ? parv[6] : NULL), timestamp, hop, parv[5][0] == 'J');
723   if (ret != 1)
724     return ret;
725
726   /*
727    * Server is informing about a new server behind
728    * this link. Create REMOTE server structure,
729    * add it to list and propagate word to my other
730    * server links...
731    */
732
733   acptr = make_client(cptr, STAT_SERVER);
734   make_server(acptr);
735   cli_serv(acptr)->prot = prot;
736   cli_serv(acptr)->timestamp = timestamp;
737   cli_hopcount(acptr) = hop;
738   ircd_strncpy(cli_name(acptr), host, HOSTLEN);
739   ircd_strncpy(cli_info(acptr), parv[parc-1], REALLEN);
740   cli_serv(acptr)->up = sptr;
741   cli_serv(acptr)->updown = add_dlink(&(cli_serv(sptr))->down, acptr);
742   /* Use cptr, because we do protocol 9 -> 10 translation
743      for numeric nicks ! */
744   SetServerYXX(cptr, acptr, parv[6]);
745
746   /* Attach any necessary UWorld config items. */
747   attach_confs_byhost(cptr, host, CONF_UWORLD);
748
749   if (*parv[7] == '+')
750     set_server_flags(acptr, parv[7] + 1);
751
752   Count_newremoteserver(UserStats);
753   if (Protocol(acptr) < 10)
754     SetFlag(acptr, FLAG_TS8);
755   add_client_to_list(acptr);
756   hAddClient(acptr);
757   if (*parv[5] == 'J')
758   {
759     SetBurst(acptr);
760     SetJunction(acptr);
761     for (bcptr = cli_serv(acptr)->up; !IsMe(bcptr); bcptr = cli_serv(bcptr)->up)
762       if (IsBurstOrBurstAck(bcptr))
763           break;
764     if (IsMe(bcptr))
765       sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
766                            cli_name(sptr), cli_name(acptr));
767   }
768   /*
769    * Old sendto_serv_but_one() call removed because we now need to send
770    * different names to different servers (domain name matching).
771    */
772   for (i = 0; i <= HighestFd; i++)
773   {
774     if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
775         bcptr == cptr || IsMe(bcptr))
776       continue;
777     if (0 == match(cli_name(&me), cli_name(acptr)))
778       continue;
779     sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s%s :%s",
780                   cli_name(acptr), hop + 1, parv[4], parv[5],
781                   NumServCap(acptr), IsHub(acptr) ? "h" : "",
782                   IsService(acptr) ? "s" : "", IsIPv6(acptr) ? "6" : "",
783                   cli_info(acptr));
784   }
785   return 0;
786 }