ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/server.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/s_serv.c (file contents):
Revision 1636 by michael, Sun Nov 4 17:09:47 2012 UTC vs.
Revision 3096 by michael, Sat Mar 1 23:31:45 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  s_serv.c: Server related functions.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2005 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2014 ircd-hybrid development team
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 18 | Line 17
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   *  USA
20 < *
21 < *  $Id$
20 > */
21 >
22 > /*! \file s_serv.c
23 > * \brief Server related functions.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
# Line 36 | Line 38
38   #include "fdlist.h"
39   #include "hash.h"
40   #include "irc_string.h"
39 #include "sprintf_irc.h"
41   #include "ircd.h"
42   #include "ircd_defs.h"
43   #include "s_bsd.h"
# Line 50 | Line 51
51   #include "s_user.h"
52   #include "send.h"
53   #include "memory.h"
54 < #include "channel.h" /* chcap_usage_counts stuff...*/
54 > #include "channel.h"
55   #include "parse.h"
56  
57   #define MIN_CONN_FREQ 300
58  
59 + dlink_list flatten_links;
60   static dlink_list cap_list = { NULL, NULL, 0 };
61   static void server_burst(struct Client *);
62   static void burst_all(struct Client *);
# Line 73 | Line 75 | static void burst_members(struct Client
75   *                but in no particular order.
76   */
77   void
78 < write_links_file(void* notused)
78 > write_links_file(void *notused)
79   {
80 <  MessageFileLine *next_mptr = NULL;
81 <  MessageFileLine *mptr = NULL;
82 <  MessageFileLine *currentMessageLine = NULL;
81 <  MessageFileLine *newMessageLine = NULL;
82 <  MessageFile *MessageFileptr = &ConfigFileEntry.linksfile;
83 <  FILE *file;
84 <  char buff[512];
85 <  dlink_node *ptr;
80 >  FILE *file = NULL;
81 >  dlink_node *ptr = NULL, *ptr_next = NULL;
82 >  char buff[IRCD_BUFSIZE] = { '\0' };
83  
84 <  if ((file = fopen(MessageFileptr->fileName, "w")) == NULL)
84 >  if ((file = fopen(LIPATH, "w")) == NULL)
85      return;
86  
87 <  for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr)
87 >  DLINK_FOREACH_SAFE(ptr, ptr_next, flatten_links.head)
88    {
89 <    next_mptr = mptr->next;
90 <    MyFree(mptr);
89 >    dlinkDelete(ptr, &flatten_links);
90 >    MyFree(ptr->data);
91 >    free_dlink_node(ptr);
92    }
93  
96  MessageFileptr->contentsOfFile = NULL;
97
94    DLINK_FOREACH(ptr, global_serv_list.head)
95    {
96      const struct Client *target_p = ptr->data;
97  
98 <    /* skip ourselves, we send ourselves in /links */
99 <    if (IsMe(target_p))
98 >    /*
99 >     * Skip hidden servers, aswell as ourselves, since we already send
100 >     * ourselves in /links
101 >     */
102 >    if (IsHidden(target_p) || IsMe(target_p))
103        continue;
104  
105 <    /* skip hidden servers */
107 <    if (IsHidden(target_p))
105 >    if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
106        continue;
107  
110    newMessageLine = MyMalloc(sizeof(MessageFileLine));
111
108      /*
109       * Attempt to format the file in such a way it follows the usual links output
110       * ie  "servername uplink :hops info"
111       * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
112       * - madmax
113       */
114 <    snprintf(newMessageLine->line, sizeof(newMessageLine->line), "%s %s :1 %s",
115 <             target_p->name, me.name, target_p->info);
114 >    snprintf(buff, sizeof(buff), "%s %s :1 %s",   target_p->name,
115 >             me.name, target_p->info);
116 >    dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
117 >    snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
118 >             me.name, target_p->info);
119  
121    if (MessageFileptr->contentsOfFile)
122    {
123      if (currentMessageLine)
124        currentMessageLine->next = newMessageLine;
125      currentMessageLine = newMessageLine;
126    }
127    else
128    {
129      MessageFileptr->contentsOfFile = newMessageLine;
130      currentMessageLine = newMessageLine;
131    }
132
133    snprintf(buff, sizeof(buff), "%s %s :1 %s\n",
134             target_p->name, me.name, target_p->info);
120      fputs(buff, file);
121    }
122  
123    fclose(file);
124   }
125  
126 + void
127 + read_links_file(void)
128 + {
129 +  FILE *file = NULL;
130 +  char *p = NULL;
131 +  char buff[IRCD_BUFSIZE] = { '\0' };
132 +
133 +  if ((file = fopen(LIPATH, "r")) == NULL)
134 +    return;
135 +
136 +  while (fgets(buff, sizeof(buff), file))
137 +  {
138 +    if ((p = strchr(buff, '\n')) != NULL)
139 +      *p = '\0';
140 +
141 +    dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
142 +  }
143 +
144 +  fclose(file);
145 + }
146 +
147   /* hunt_server()
148   *      Do the basic thing in delivering the message (command)
149   *      across the relays to the specific server (server) for
# Line 159 | Line 165 | write_links_file(void* notused)
165   */
166   int
167   hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
168 <            int server, int parc, char *parv[])
168 >            const int server, const int parc, char *parv[])
169   {
170    struct Client *target_p = NULL;
171    struct Client *target_tmp = NULL;
# Line 170 | Line 176 | hunt_server(struct Client *client_p, str
176    if (parc <= server || EmptyString(parv[server]))
177      return HUNTED_ISME;
178  
179 <  if (!strcmp(parv[server], me.id) || match(parv[server], me.name))
179 >  if (!strcmp(parv[server], me.id) || !match(parv[server], me.name))
180      return HUNTED_ISME;
181  
182    /* These are to pickup matches that would cause the following
# Line 190 | Line 196 | hunt_server(struct Client *client_p, str
196      if (target_p->from == source_p->from && !MyConnect(target_p))
197        target_p = NULL;
198  
193  collapse(parv[server]);
199    wilds = has_wildcards(parv[server]);
200  
201    /* Again, if there are no wild cards involved in the server
# Line 204 | Line 209 | hunt_server(struct Client *client_p, str
209        {
210          sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
211                     me.name, source_p->name, parv[server]);
212 <        return(HUNTED_NOSUCH);
212 >        return HUNTED_NOSUCH;
213        }
214      }
215      else
# Line 213 | Line 218 | hunt_server(struct Client *client_p, str
218        {
219          target_tmp = ptr->data;
220  
221 <        if (match(parv[server], target_tmp->name))
221 >        if (!match(parv[server], target_tmp->name))
222          {
223            if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
224              continue;
# Line 238 | Line 243 | hunt_server(struct Client *client_p, str
243      if (IsMe(target_p) || MyClient(target_p))
244        return HUNTED_ISME;
245  
246 <    if (!match(target_p->name, parv[server]))
246 >    if (match(target_p->name, parv[server]))
247        parv[server] = target_p->name;
248  
249 <    /* This is a little kludgy but should work... */
245 <    if (IsClient(source_p) &&
246 <        ((MyConnect(target_p) && IsCapable(target_p, CAP_TS6)) ||
247 <         (!MyConnect(target_p) && IsCapable(target_p->from, CAP_TS6))))
248 <      parv[0] = ID(source_p);
249 <
250 <    sendto_one(target_p, command, parv[0],
249 >    sendto_one(target_p, command, ID_or_name(source_p, target_p),
250                 parv[1], parv[2], parv[3], parv[4],
251                 parv[5], parv[6], parv[7], parv[8]);
252 <    return(HUNTED_PASS);
253 <  }
252 >    return HUNTED_PASS;
253 >  }
254  
255    sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
256               me.name, source_p->name, parv[server]);
257 <  return(HUNTED_NOSUCH);
257 >  return HUNTED_NOSUCH;
258   }
259  
260   /* try_connections()
# Line 285 | Line 284 | try_connections(void *unused)
284  
285      assert(conf->type == CONF_SERVER);
286  
287 <    /* Also when already connecting! (update holdtimes) --SRB
287 >    /* Also when already connecting! (update holdtimes) --SRB
288       */
289      if (!conf->port ||!IsConfAllowAutoConn(conf))
290        continue;
# Line 297 | Line 296 | try_connections(void *unused)
296       * made one successfull connection... [this algorithm is
297       * a bit fuzzy... -- msa >;) ]
298       */
299 <    if (conf->hold > CurrentTime)
299 >    if (conf->until > CurrentTime)
300        continue;
301  
302      if (conf->class == NULL)
# Line 306 | Line 305 | try_connections(void *unused)
305      {
306        confrq = conf->class->con_freq;
307        if (confrq < MIN_CONN_FREQ)
308 <        confrq = MIN_CONN_FREQ;
308 >        confrq = MIN_CONN_FREQ;
309      }
310  
311 <    conf->hold = CurrentTime + confrq;
311 >    conf->until = CurrentTime + confrq;
312  
313      /* Found a CONNECT config with port specified, scan clients
314       * and see if this server is already connected?
# Line 389 | Line 388 | check_server(const char *name, struct Cl
388    {
389      conf = ptr->data;
390  
391 <    if (!match(name, conf->name))
391 >    if (match(name, conf->name))
392        continue;
393  
394      error = -3;
395  
396      /* XXX: Fix me for IPv6                    */
397      /* XXX sockhost is the IPv4 ip as a string */
398 <    if (match(conf->host, client_p->host) ||
399 <        match(conf->host, client_p->sockhost))
398 >    if (!match(conf->host, client_p->host) ||
399 >        !match(conf->host, client_p->sockhost))
400      {
401        error = -2;
402  
403        if (!match_conf_password(client_p->localClient->passwd, conf))
404          return -2;
405  
406 +      if (!EmptyString(conf->certfp))
407 +        if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
408 +          return -4;
409 +
410        server_conf = conf;
411      }
412    }
413  
414    if (server_conf == NULL)
415 <    return(error);
415 >    return error;
416  
417    attach_conf(client_p, server_conf);
418  
# Line 423 | Line 426 | check_server(const char *name, struct Cl
426      switch (server_conf->aftype)
427      {
428   #ifdef IPV6
429 <      case AF_INET6:
429 >      case AF_INET6:
430          v6 = (struct sockaddr_in6 *)&server_conf->addr;
431  
432          if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
# Line 434 | Line 437 | check_server(const char *name, struct Cl
437          v4 = (struct sockaddr_in *)&server_conf->addr;
438  
439          if (v4->sin_addr.s_addr == INADDR_NONE)
440 <          memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
440 >          memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
441          break;
442      }
443    }
444  
445 <  return(0);
445 >  return 0;
446   }
447  
448   /* add_capability()
# Line 456 | Line 459 | add_capability(const char *capab_name, i
459   {
460    struct Capability *cap = MyMalloc(sizeof(*cap));
461  
462 <  DupString(cap->name, capab_name);
462 >  cap->name = xstrdup(capab_name);
463    cap->cap = cap_flag;
464    dlinkAdd(cap, &cap->node, &cap_list);
465  
# Line 485 | Line 488 | delete_capability(const char *capab_name
488      {
489        if (irccmp(cap->name, capab_name) == 0)
490        {
491 <        default_server_capabs &= ~(cap->cap);
492 <        dlinkDelete(ptr, &cap_list);
493 <        MyFree(cap->name);
494 <        cap->name = NULL;
492 <        MyFree(cap);
491 >        default_server_capabs &= ~(cap->cap);
492 >        dlinkDelete(ptr, &cap_list);
493 >        MyFree(cap->name);
494 >        MyFree(cap);
495        }
496      }
497    }
# Line 504 | Line 506 | delete_capability(const char *capab_name
506   * output       - 0 if not found CAPAB otherwise
507   * side effects - none
508   */
509 < int
509 > unsigned int
510   find_capability(const char *capab)
511   {
512    const dlink_node *ptr = NULL;
# Line 545 | Line 547 | send_capabilities(struct Client *client_
547  
548      if (cap->cap & (cap_can_send|default_server_capabs))
549      {
550 <      tl = ircsprintf(t, "%s ", cap->name);
550 >      tl = sprintf(t, "%s ", cap->name);
551        t += tl;
552      }
553    }
# Line 555 | Line 557 | send_capabilities(struct Client *client_
557   }
558  
559   /* sendnick_TS()
560 < *
560 > *
561   * inputs       - client (server) to send nick towards
562   *          - client to send nick for
563   * output       - NONE
# Line 564 | Line 566 | send_capabilities(struct Client *client_
566   void
567   sendnick_TS(struct Client *client_p, struct Client *target_p)
568   {
569 <  static char ubuf[12];
569 >  char ubuf[IRCD_BUFSIZE];
570  
571    if (!IsClient(target_p))
572      return;
# Line 614 | Line 616 | sendnick_TS(struct Client *client_p, str
616                   target_p->servptr->name, target_p->info);
617    }
618  
619 +  if (!EmptyString(target_p->certfp))
620 +    sendto_one(client_p, ":%s CERTFP %s",
621 +               ID_or_name(target_p, client_p), target_p->certfp);
622 +
623    if (target_p->away[0])
624      sendto_one(client_p, ":%s AWAY :%s", ID_or_name(target_p, client_p),
625                 target_p->away);
# Line 628 | Line 634 | sendnick_TS(struct Client *client_p, str
634   * side effects - build up string representing capabilities of server listed
635   */
636   const char *
637 < show_capabilities(struct Client *target_p)
637 > show_capabilities(const struct Client *target_p)
638   {
639 <  static char msgbuf[IRCD_BUFSIZE];
640 <  char *t = msgbuf;
635 <  dlink_node *ptr;
639 >  static char msgbuf[IRCD_BUFSIZE] = "";
640 >  const dlink_node *ptr = NULL;
641  
642 <  t += ircsprintf(msgbuf, "TS ");
642 >  strlcpy(msgbuf, "TS", sizeof(msgbuf));
643  
644    DLINK_FOREACH(ptr, cap_list.head)
645    {
646      const struct Capability *cap = ptr->data;
647  
648 <    if (IsCapable(target_p, cap->cap))
649 <      t += ircsprintf(t, "%s ", cap->name);
648 >    if (!IsCapable(target_p, cap->cap))
649 >      continue;
650 >
651 >    strlcat(msgbuf,       " ", sizeof(msgbuf));
652 >    strlcat(msgbuf, cap->name, sizeof(msgbuf));
653    }
654  
647  *(t - 1) = '\0';
655    return msgbuf;
656   }
657  
# Line 710 | Line 717 | server_estab(struct Client *client_p)
717    /* If there is something in the serv_list, it might be this
718     * connecting server..
719     */
720 <  if (!ServerInfo.hub && serv_list.head)  
720 >  if (!ServerInfo.hub && serv_list.head)
721    {
722      if (client_p != serv_list.head->data || serv_list.head->next)
723      {
# Line 723 | Line 730 | server_estab(struct Client *client_p)
730  
731    if (IsUnknown(client_p))
732    {
733 <    /* jdc -- 1.  Use EmptyString(), not [0] index reference.
727 <     *        2.  Check conf->spasswd, not conf->passwd.
728 <     */
729 <    if (!EmptyString(conf->spasswd))
730 <      sendto_one(client_p, "PASS %s TS %d %s",
731 <                 conf->spasswd, TS_CURRENT, me.id);
733 >    sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
734  
735      send_capabilities(client_p, 0);
736  
# Line 848 | Line 850 | server_estab(struct Client *client_p)
850                   IsHidden(client_p) ? "(H) " : "",
851                   client_p->info);
852      else
853 <      sendto_one(target_p,":%s SERVER %s 2 :%s%s",
853 >      sendto_one(target_p,":%s SERVER %s 2 :%s%s",
854                   me.name, client_p->name,
855                   IsHidden(client_p) ? "(H) " : "",
856                   client_p->info);
857    }
858  
859 <  /* Pass on my client information to the new server
860 <  **
861 <  ** First, pass only servers (idea is that if the link gets
862 <  ** cancelled beacause the server was already there,
863 <  ** there are no NICK's to be cancelled...). Of course,
864 <  ** if cancellation occurs, all this info is sent anyway,
865 <  ** and I guess the link dies when a read is attempted...? --msa
866 <  **
867 <  ** Note: Link cancellation to occur at this point means
868 <  ** that at least two servers from my fragment are building
869 <  ** up connection this other fragment at the same time, it's
870 <  ** a race condition, not the normal way of operation...
871 <  **
872 <  ** ALSO NOTE: using the get_client_name for server names--
873 <  **    see previous *WARNING*!!! (Also, original inpath
874 <  **    is destroyed...)
875 <  */
859 >  /*
860 >   * Pass on my client information to the new server
861 >   *
862 >   * First, pass only servers (idea is that if the link gets
863 >   * cancelled beacause the server was already there,
864 >   * there are no NICK's to be cancelled...). Of course,
865 >   * if cancellation occurs, all this info is sent anyway,
866 >   * and I guess the link dies when a read is attempted...? --msa
867 >   *
868 >   * Note: Link cancellation to occur at this point means
869 >   * that at least two servers from my fragment are building
870 >   * up connection this other fragment at the same time, it's
871 >   * a race condition, not the normal way of operation...
872 >   *
873 >   * ALSO NOTE: using the get_client_name for server names--
874 >   *    see previous *WARNING*!!! (Also, original inpath
875 >   *    is destroyed...)
876 >   */
877  
878    DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
879    {
# Line 893 | Line 896 | server_estab(struct Client *client_p)
896                     IsHidden(target_p) ? "(H) " : "", target_p->info);
897      }
898      else
899 <      sendto_one(client_p, ":%s SERVER %s %d :%s%s",
899 >      sendto_one(client_p, ":%s SERVER %s %d :%s%s",
900                   target_p->servptr->name, target_p->name, target_p->hopcount+1,
901                   IsHidden(target_p) ? "(H) " : "", target_p->info);
902 +
903 +    if (HasFlag(target_p, FLAGS_EOB))
904 +      sendto_one(client_p, ":%s EOB", ID_or_name(target_p, client_p));
905    }
906  
907    server_burst(client_p);
# Line 931 | Line 937 | server_burst(struct Client *client_p)
937  
938   /* burst_all()
939   *
940 < * inputs       - pointer to server to send burst to
940 > * inputs       - pointer to server to send burst to
941   * output       - NONE
942   * side effects - complete burst of channels/nicks is sent to client_p
943   */
# Line 950 | Line 956 | burst_all(struct Client *client_p)
956        send_channel_modes(client_p, chptr);
957  
958        if (IsCapable(client_p, CAP_TBURST))
959 <        send_tb(client_p, chptr);
959 >        send_tb(client_p, chptr);
960      }
961    }
962  
# Line 962 | Line 968 | burst_all(struct Client *client_p)
968  
969      if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
970        sendnick_TS(client_p, target_p);
971 <    
971 >
972      DelFlag(target_p, FLAGS_BURSTED);
973    }
974  
969  /* We send the time we started the burst, and let the remote host determine an EOB time,
970  ** as otherwise we end up sending a EOB of 0   Sending here means it gets sent last -- fl
971  */
972  /* Its simpler to just send EOB and use the time its been connected.. --fl_ */
975    if (IsCapable(client_p, CAP_EOB))
976      sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
977   }
# Line 1044 | Line 1046 | burst_members(struct Client *client_p, s
1046  
1047   /* serv_connect() - initiate a server connection
1048   *
1049 < * inputs       - pointer to conf
1049 > * inputs       - pointer to conf
1050   *              - pointer to client doing the connect
1051   * output       -
1052   * side effects -
# Line 1096 | Line 1098 | serv_connect(struct MaskItem *conf, stru
1098     * Note: conf should ALWAYS be a valid C: line
1099     */
1100    if ((client_p = hash_find_server(conf->name)) != NULL)
1101 <  {
1101 >  {
1102      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1103 <                         "Server %s already present from %s",
1104 <                         conf->name, get_client_name(client_p, SHOW_IP));
1103 >                         "Server %s already present from %s",
1104 >                         conf->name, get_client_name(client_p, SHOW_IP));
1105      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1106 <                         "Server %s already present from %s",
1107 <                         conf->name, get_client_name(client_p, MASK_IP));
1106 >                         "Server %s already present from %s",
1107 >                         conf->name, get_client_name(client_p, MASK_IP));
1108      if (by && IsClient(by) && !MyClient(by))
1109        sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
1110 <                 me.name, by->name, conf->name,
1111 <                 get_client_name(client_p, MASK_IP));
1112 <    return (0);
1110 >                 me.name, by->name, conf->name,
1111 >                 get_client_name(client_p, MASK_IP));
1112 >    return 0;
1113    }
1114 <    
1114 >
1115    /* Create a local client */
1116    client_p = make_client(NULL);
1117  
# Line 1120 | Line 1122 | serv_connect(struct MaskItem *conf, stru
1122    /* We already converted the ip once, so lets use it - stu */
1123    strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
1124  
1125 <  /* create a socket for the server connection */
1125 >  /* create a socket for the server connection */
1126    if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family,
1127                  SOCK_STREAM, 0, NULL) < 0)
1128    {
1129      /* Eek, failure to create the socket */
1130 <    report_error(L_ALL,
1131 <                 "opening stream socket to %s: %s", conf->name, errno);
1130 >    report_error(L_ALL, "opening stream socket to %s: %s",
1131 >                 conf->name, errno);
1132      SetDead(client_p);
1133      exit_client(client_p, &me, "Connection failed");
1134 <    return (0);
1134 >    return 0;
1135    }
1136  
1137    /* servernames are always guaranteed under HOSTLEN chars */
# Line 1141 | Line 1143 | serv_connect(struct MaskItem *conf, stru
1143    if (!attach_connect_block(client_p, conf->name, conf->host))
1144    {
1145      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1146 <                         "Host %s is not enabled for connecting: no connect{} block",
1147 <                         conf->name);
1148 <    if (by && IsClient(by) && !MyClient(by))  
1146 >                         "Host %s is not enabled for connecting: no connect{} block",
1147 >                         conf->name);
1148 >    if (by && IsClient(by) && !MyClient(by))
1149        sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
1150 <                 me.name, by->name, client_p->name);
1150 >                 me.name, by->name, client_p->name);
1151      SetDead(client_p);
1152      exit_client(client_p, client_p, "Connection failed");
1153 <    return (0);
1153 >    return 0;
1154    }
1155  
1156    /* at this point we have a connection in progress and C/N lines
# Line 1170 | Line 1172 | serv_connect(struct MaskItem *conf, stru
1172    client_p->localClient->aftype = conf->aftype;
1173  
1174    /* Now, initiate the connection */
1175 <  /* XXX assume that a non 0 type means a specific bind address
1175 >  /* XXX assume that a non 0 type means a specific bind address
1176     * for this connect.
1177     */
1178    switch (conf->aftype)
# Line 1184 | Line 1186 | serv_connect(struct MaskItem *conf, stru
1186          ipn.ss.ss_family = AF_INET;
1187          ipn.ss_port = 0;
1188          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1189 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1190 <                         (struct sockaddr *)&ipn, ipn.ss_len,
1191 <                         serv_connect_callback, client_p, conf->aftype,
1192 <                         CONNECTTIMEOUT);
1189 >        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1190 >                         (struct sockaddr *)&ipn, ipn.ss_len,
1191 >                         serv_connect_callback, client_p, conf->aftype,
1192 >                         CONNECTTIMEOUT);
1193        }
1194        else if (ServerInfo.specific_ipv4_vhost)
1195        {
# Line 1199 | Line 1201 | serv_connect(struct MaskItem *conf, stru
1201          comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1202                           (struct sockaddr *)&ipn, ipn.ss_len,
1203                           serv_connect_callback, client_p, conf->aftype,
1204 <                         CONNECTTIMEOUT);
1204 >                         CONNECTTIMEOUT);
1205        }
1206        else
1207 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1208 <                         NULL, 0, serv_connect_callback, client_p, conf->aftype,
1207 >        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1208 >                         NULL, 0, serv_connect_callback, client_p, conf->aftype,
1209                           CONNECTTIMEOUT);
1210        break;
1211   #ifdef IPV6
1212      case AF_INET6:
1213        {
1214 <        struct irc_ssaddr ipn;
1215 <        struct sockaddr_in6 *v6;
1216 <        struct sockaddr_in6 *v6conf;
1217 <
1218 <        memset(&ipn, 0, sizeof(struct irc_ssaddr));
1219 <        v6conf = (struct sockaddr_in6 *)&conf->bind;
1220 <        v6 = (struct sockaddr_in6 *)&ipn;
1221 <
1222 <        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr,
1221 <                   sizeof(struct in6_addr)) != 0)
1222 <        {
1223 <          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1224 <          ipn.ss.ss_family = AF_INET6;
1225 <          ipn.ss_port = 0;
1226 <          comm_connect_tcp(&client_p->localClient->fd,
1227 <                           conf->host, conf->port,
1228 <                           (struct sockaddr *)&ipn, ipn.ss_len,
1229 <                           serv_connect_callback, client_p,
1230 <                           conf->aftype, CONNECTTIMEOUT);
1231 <        }
1232 <        else if (ServerInfo.specific_ipv6_vhost)
1214 >        struct irc_ssaddr ipn;
1215 >        struct sockaddr_in6 *v6;
1216 >        struct sockaddr_in6 *v6conf;
1217 >
1218 >        memset(&ipn, 0, sizeof(struct irc_ssaddr));
1219 >        v6conf = (struct sockaddr_in6 *)&conf->bind;
1220 >        v6 = (struct sockaddr_in6 *)&ipn;
1221 >
1222 >        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)) != 0)
1223          {
1224 <          memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
1225 <          ipn.ss.ss_family = AF_INET6;
1226 <          ipn.ss_port = 0;
1227 <          comm_connect_tcp(&client_p->localClient->fd,
1228 <                           conf->host, conf->port,
1229 <                           (struct sockaddr *)&ipn, ipn.ss_len,
1230 <                           serv_connect_callback, client_p,
1231 <                           conf->aftype, CONNECTTIMEOUT);
1232 <        }
1233 <        else
1234 <          comm_connect_tcp(&client_p->localClient->fd,
1235 <                           conf->host, conf->port,
1236 <                           NULL, 0, serv_connect_callback, client_p,
1237 <                           conf->aftype, CONNECTTIMEOUT);
1224 >          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1225 >          ipn.ss.ss_family = AF_INET6;
1226 >          ipn.ss_port = 0;
1227 >          comm_connect_tcp(&client_p->localClient->fd,
1228 >                           conf->host, conf->port,
1229 >                           (struct sockaddr *)&ipn, ipn.ss_len,
1230 >                           serv_connect_callback, client_p,
1231 >                           conf->aftype, CONNECTTIMEOUT);
1232 >        }
1233 >        else if (ServerInfo.specific_ipv6_vhost)
1234 >        {
1235 >          memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
1236 >          ipn.ss.ss_family = AF_INET6;
1237 >          ipn.ss_port = 0;
1238 >          comm_connect_tcp(&client_p->localClient->fd,
1239 >                           conf->host, conf->port,
1240 >                           (struct sockaddr *)&ipn, ipn.ss_len,
1241 >                           serv_connect_callback, client_p,
1242 >                           conf->aftype, CONNECTTIMEOUT);
1243 >        }
1244 >        else
1245 >          comm_connect_tcp(&client_p->localClient->fd,
1246 >                           conf->host, conf->port,
1247 >                           NULL, 0, serv_connect_callback, client_p,
1248 >                           conf->aftype, CONNECTTIMEOUT);
1249        }
1250   #endif
1251    }
1252 <  return (1);
1252 >  return 1;
1253   }
1254  
1255   #ifdef HAVE_LIBCRYPTO
# Line 1270 | Line 1271 | finish_ssl_server_handshake(struct Clien
1271      return;
1272    }
1273  
1274 <  /* jdc -- Check and send spasswd, not passwd. */
1274 <  if (!EmptyString(conf->spasswd))
1275 <    sendto_one(client_p, "PASS %s TS %d %s",
1276 <               conf->spasswd, TS_CURRENT, me.id);
1274 >  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
1275  
1276    send_capabilities(client_p, 0);
1277  
# Line 1303 | Line 1301 | finish_ssl_server_handshake(struct Clien
1301   static void
1302   ssl_server_handshake(fde_t *fd, struct Client *client_p)
1303   {
1304 <  int ret;
1305 <  int err;
1308 <
1309 <  ret = SSL_connect(client_p->localClient->fd.ssl);
1304 >  X509 *cert = NULL;
1305 >  int ret = 0;
1306  
1307 <  if (ret <= 0)
1307 >  if ((ret = SSL_connect(client_p->localClient->fd.ssl)) <= 0)
1308    {
1309 <    switch ((err = SSL_get_error(client_p->localClient->fd.ssl, ret)))
1309 >    switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
1310      {
1311        case SSL_ERROR_WANT_WRITE:
1312          comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
# Line 1332 | Line 1328 | ssl_server_handshake(fde_t *fd, struct C
1328      }
1329    }
1330  
1331 +  if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
1332 +  {
1333 +    int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
1334 +    char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
1335 +    unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
1336 +
1337 +    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
1338 +        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
1339 +        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1340 +    {
1341 +      unsigned int i = 0, n = 0;
1342 +
1343 +      if (X509_digest(cert, EVP_sha256(), md, &n))
1344 +      {
1345 +        for (; i < n; ++i)
1346 +          snprintf(buf + 2 * i, 3, "%02X", md[i]);
1347 +        client_p->certfp = xstrdup(buf);
1348 +      }
1349 +    }
1350 +    else
1351 +      ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad SSL client certificate: %d",
1352 +           client_p->name, client_p->username, client_p->host, res);
1353 +    X509_free(cert);
1354 +  }
1355 +
1356    finish_ssl_server_handshake(client_p);
1357   }
1358  
# Line 1357 | Line 1378 | ssl_connect_init(struct Client *client_p
1378   #endif
1379  
1380   /* serv_connect_callback() - complete a server connection.
1381 < *
1381 > *
1382   * This routine is called after the server connection attempt has
1383   * completed. If unsucessful, an error is sent to ops and the client
1384   * is closed. If sucessful, it goes through the initialisation/check
# Line 1389 | Line 1410 | serv_connect_callback(fde_t *fd, int sta
1410                              client_p->name, comm_errstr(status));
1411       else
1412         sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1413 <                            "Error connecting to %s[%s]: %s", client_p->name,
1414 <                            client_p->host, comm_errstr(status));
1413 >                            "Error connecting to %s[%s]: %s", client_p->name,
1414 >                            client_p->host, comm_errstr(status));
1415  
1416       sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1417 <                          "Error connecting to %s: %s",
1418 <                          client_p->name, comm_errstr(status));
1417 >                          "Error connecting to %s: %s",
1418 >                          client_p->name, comm_errstr(status));
1419  
1420       /* If a fd goes bad, call dead_link() the socket is no
1421        * longer valid for reading or writing.
# Line 1406 | Line 1427 | serv_connect_callback(fde_t *fd, int sta
1427    /* COMM_OK, so continue the connection procedure */
1428    /* Get the C/N lines */
1429    conf = find_conf_name(&client_p->localClient->confs,
1430 <                        client_p->name, CONF_SERVER);
1430 >                        client_p->name, CONF_SERVER);
1431    if (conf == NULL)
1432    {
1433      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1434 <                         "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
1434 >                         "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
1435      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1436 <                         "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1436 >                         "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1437  
1438      exit_client(client_p, &me, "Lost connect{} block");
1439      return;
# Line 1429 | Line 1450 | serv_connect_callback(fde_t *fd, int sta
1450    }
1451   #endif
1452  
1453 <  /* jdc -- Check and send spasswd, not passwd. */
1433 <  if (!EmptyString(conf->spasswd))
1434 <    sendto_one(client_p, "PASS %s TS %d %s",
1435 <               conf->spasswd, TS_CURRENT, me.id);
1453 >  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
1454  
1455    send_capabilities(client_p, 0);
1456  
1457 <  sendto_one(client_p, "SERVER %s 1 :%s%s",
1458 <             me.name, ConfigServerHide.hidden ? "(H) " : "",
1441 <             me.info);
1457 >  sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
1458 >             ConfigServerHide.hidden ? "(H) " : "", me.info);
1459  
1460    /* If we've been marked dead because a send failed, just exit
1461     * here now and save everyone the trouble of us ever existing.
1462     */
1463 <  if (IsDead(client_p))
1463 >  if (IsDead(client_p))
1464    {
1465        sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1466 <                           "%s[%s] went dead during handshake",
1466 >                           "%s[%s] went dead during handshake",
1467                             client_p->name,
1468 <                           client_p->host);
1468 >                           client_p->host);
1469        sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1470 <                           "%s went dead during handshake", client_p->name);
1470 >                           "%s went dead during handshake", client_p->name);
1471        return;
1472    }
1473  
# Line 1470 | Line 1487 | find_servconn_in_progress(const char *na
1487      cptr = ptr->data;
1488  
1489      if (cptr && cptr->name[0])
1490 <      if (match(name, cptr->name))
1490 >      if (!match(name, cptr->name))
1491          return cptr;
1492    }
1493 <  
1493 >
1494    return NULL;
1495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)