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

Comparing:
ircd-hybrid/trunk/src/s_serv.c (file contents), Revision 1834 by michael, Fri Apr 19 19:50:27 2013 UTC vs.
ircd-hybrid/trunk/src/server.c (file contents), Revision 7105 by michael, Sat Jan 23 20:11:27 2016 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-2016 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 16 | Line 15
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18 < *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
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"
26 #ifdef HAVE_LIBCRYPTO
27 #include <openssl/rsa.h>
28 #include "rsa.h"
29 #endif
28   #include "list.h"
31 #include "channel.h"
32 #include "channel_mode.h"
29   #include "client.h"
34 #include "dbuf.h"
30   #include "event.h"
36 #include "fdlist.h"
31   #include "hash.h"
32   #include "irc_string.h"
33   #include "ircd.h"
# Line 41 | Line 35
35   #include "s_bsd.h"
36   #include "numeric.h"
37   #include "packet.h"
44 #include "irc_res.h"
38   #include "conf.h"
39 < #include "s_serv.h"
39 > #include "server.h"
40   #include "log.h"
41 < #include "s_misc.h"
49 < #include "s_user.h"
41 > #include "user.h"
42   #include "send.h"
43   #include "memory.h"
44 < #include "channel.h" /* chcap_usage_counts stuff...*/
44 > #include "channel.h"
45   #include "parse.h"
46  
55 #define MIN_CONN_FREQ 300
56
57 static dlink_list cap_list = { NULL, NULL, 0 };
58 static void server_burst(struct Client *);
59 static void burst_all(struct Client *);
60 static void send_tb(struct Client *client_p, struct Channel *chptr);
47  
48 < static CNCB serv_connect_callback;
48 > dlink_list flatten_links;
49 > static dlink_list server_capabilities_list;
50 > static void serv_connect_callback(fde_t *, int, void *);
51  
64 static void burst_members(struct Client *, struct Channel *);
52  
53   /*
54   * write_links_file
# Line 72 | Line 59 | static void burst_members(struct Client
59   *                but in no particular order.
60   */
61   void
62 < write_links_file(void* notused)
62 > write_links_file(void *unused)
63   {
64 <  MessageFileLine *next_mptr = NULL;
65 <  MessageFileLine *mptr = NULL;
66 <  MessageFileLine *currentMessageLine = NULL;
80 <  MessageFileLine *newMessageLine = NULL;
81 <  MessageFile *MessageFileptr = &ConfigFileEntry.linksfile;
82 <  FILE *file;
83 <  char buff[512];
84 <  dlink_node *ptr;
64 >  FILE *file = NULL;
65 >  dlink_node *node = NULL, *node_next = NULL;
66 >  char buff[IRCD_BUFSIZE] = "";
67  
68 <  if ((file = fopen(MessageFileptr->fileName, "w")) == NULL)
68 >  if (EmptyString(ConfigServerHide.flatten_links_file))
69      return;
70  
71 <  for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr)
71 >  if ((file = fopen(ConfigServerHide.flatten_links_file, "w")) == NULL)
72    {
73 <    next_mptr = mptr->next;
74 <    MyFree(mptr);
73 >    ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
74 >         strerror(errno));
75 >    return;
76    }
77  
78 <  MessageFileptr->contentsOfFile = NULL;
78 >  DLINK_FOREACH_SAFE(node, node_next, flatten_links.head)
79 >  {
80 >    dlinkDelete(node, &flatten_links);
81 >    xfree(node->data);
82 >    free_dlink_node(node);
83 >  }
84  
85 <  DLINK_FOREACH(ptr, global_serv_list.head)
85 >  DLINK_FOREACH(node, global_server_list.head)
86    {
87 <    const struct Client *target_p = ptr->data;
87 >    const struct Client *target_p = node->data;
88  
89 <    /* skip ourselves, we send ourselves in /links */
90 <    if (IsMe(target_p))
89 >    /*
90 >     * Skip hidden servers, aswell as ourselves, since we already send
91 >     * ourselves in /links
92 >     */
93 >    if (IsHidden(target_p) || IsMe(target_p))
94        continue;
95  
96 <    /* skip hidden servers */
106 <    if (IsHidden(target_p))
96 >    if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
97        continue;
98  
109    newMessageLine = MyMalloc(sizeof(MessageFileLine));
110
99      /*
100       * Attempt to format the file in such a way it follows the usual links output
101       * ie  "servername uplink :hops info"
102       * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
103       * - madmax
104       */
105 <    snprintf(newMessageLine->line, sizeof(newMessageLine->line), "%s %s :1 %s",
106 <             target_p->name, me.name, target_p->info);
107 <
108 <    if (MessageFileptr->contentsOfFile)
109 <    {
122 <      if (currentMessageLine)
123 <        currentMessageLine->next = newMessageLine;
124 <      currentMessageLine = newMessageLine;
125 <    }
126 <    else
127 <    {
128 <      MessageFileptr->contentsOfFile = newMessageLine;
129 <      currentMessageLine = newMessageLine;
130 <    }
105 >    snprintf(buff, sizeof(buff), "%s %s :1 %s",   target_p->name,
106 >             me.name, target_p->info);
107 >    dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
108 >    snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
109 >             me.name, target_p->info);
110  
132    snprintf(buff, sizeof(buff), "%s %s :1 %s\n",
133             target_p->name, me.name, target_p->info);
111      fputs(buff, file);
112    }
113  
114    fclose(file);
115   }
116  
117 + void
118 + read_links_file(void)
119 + {
120 +  FILE *file = NULL;
121 +  char *p = NULL;
122 +  char buff[IRCD_BUFSIZE] = "";
123 +
124 +  if (EmptyString(ConfigServerHide.flatten_links_file))
125 +    return;
126 +
127 +  if ((file = fopen(ConfigServerHide.flatten_links_file, "r")) == NULL)
128 +  {
129 +    ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
130 +         strerror(errno));
131 +    return;
132 +  }
133 +
134 +  while (fgets(buff, sizeof(buff), file))
135 +  {
136 +    if ((p = strchr(buff, '\n')))
137 +      *p = '\0';
138 +
139 +    dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
140 +  }
141 +
142 +  fclose(file);
143 + }
144 +
145   /* hunt_server()
146   *      Do the basic thing in delivering the message (command)
147   *      across the relays to the specific server (server) for
# Line 157 | Line 162 | write_links_file(void* notused)
162   *      returns: (see #defines)
163   */
164   int
165 < hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
166 <            int server, int parc, char *parv[])
165 > hunt_server(struct Client *source_p, const char *command,
166 >            const int server, const int parc, char *parv[])
167   {
168    struct Client *target_p = NULL;
169 <  struct Client *target_tmp = NULL;
165 <  dlink_node *ptr;
166 <  int wilds;
169 >  dlink_node *node = NULL;
170  
171    /* Assume it's me, if no server */
172    if (parc <= server || EmptyString(parv[server]))
173      return HUNTED_ISME;
174  
175 <  if (!strcmp(parv[server], me.id) || !match(parv[server], me.name))
176 <    return HUNTED_ISME;
175 >  if ((target_p = find_person(source_p, parv[server])) == NULL)
176 >    target_p = hash_find_server(parv[server]);
177  
178 <  /* These are to pickup matches that would cause the following
178 >  /*
179 >   * These are to pickup matches that would cause the following
180     * message to go in the wrong direction while doing quick fast
181     * non-matching lookups.
182     */
179  if (MyClient(source_p))
180    target_p = hash_find_client(parv[server]);
181  else
182    target_p = find_person(client_p, parv[server]);
183
183    if (target_p)
184      if (target_p->from == source_p->from && !MyConnect(target_p))
185        target_p = NULL;
186  
187 <  if (target_p == NULL && (target_p = hash_find_server(parv[server])))
189 <    if (target_p->from == source_p->from && !MyConnect(target_p))
190 <      target_p = NULL;
191 <
192 <  collapse(parv[server]);
193 <  wilds = has_wildcards(parv[server]);
194 <
195 <  /* Again, if there are no wild cards involved in the server
196 <   * name, use the hash lookup
197 <   */
198 <  if (target_p == NULL)
187 >  if (!target_p && has_wildcards(parv[server]))
188    {
189 <    if (!wilds)
189 >    DLINK_FOREACH(node, global_server_list.head)
190      {
191 <      if (!(target_p = hash_find_server(parv[server])))
191 >      struct Client *tmp = node->data;
192 >
193 >      assert(IsMe(tmp) || IsServer(tmp));
194 >      if (!match(parv[server], tmp->name))
195        {
196 <        sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
197 <                   me.name, source_p->name, parv[server]);
198 <        return(HUNTED_NOSUCH);
196 >        if (tmp->from == source_p->from && !MyConnect(tmp))
197 >          continue;
198 >
199 >        target_p = node->data;
200 >        break;
201        }
202      }
203 <    else
203 >
204 >    if (!target_p)
205      {
206 <      DLINK_FOREACH(ptr, global_client_list.head)
206 >      DLINK_FOREACH(node, global_client_list.head)
207        {
208 <        target_tmp = ptr->data;
208 >        struct Client *tmp = node->data;
209  
210 <        if (!match(parv[server], target_tmp->name))
210 >        assert(IsMe(tmp) || IsServer(tmp) || IsClient(tmp));
211 >        if (!match(parv[server], tmp->name))
212          {
213 <          if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
213 >          if (tmp->from == source_p->from && !MyConnect(tmp))
214              continue;
219          target_p = ptr->data;
215  
216 <          if (IsRegistered(target_p) && (target_p != client_p))
217 <            break;
216 >          target_p = node->data;
217 >          break;
218          }
219        }
220      }
221    }
222  
223 <  if (target_p != NULL)
223 >  if (target_p)
224    {
225 <    if(!IsRegistered(target_p))
231 <    {
232 <      sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
233 <                 me.name, source_p->name, parv[server]);
234 <      return HUNTED_NOSUCH;
235 <    }
236 <
225 >    assert(IsMe(target_p) || IsServer(target_p) || IsClient(target_p));
226      if (IsMe(target_p) || MyClient(target_p))
227        return HUNTED_ISME;
228  
229 <    if (match(target_p->name, parv[server]))
230 <      parv[server] = target_p->name;
242 <
243 <    /* This is a little kludgy but should work... */
244 <    if (IsClient(source_p) &&
245 <        ((MyConnect(target_p) && IsCapable(target_p, CAP_TS6)) ||
246 <         (!MyConnect(target_p) && IsCapable(target_p->from, CAP_TS6))))
247 <      parv[0] = ID(source_p);
248 <
249 <    sendto_one(target_p, command, parv[0],
229 >    parv[server] = target_p->id;
230 >    sendto_one(target_p, command, source_p->id,
231                 parv[1], parv[2], parv[3], parv[4],
232                 parv[5], parv[6], parv[7], parv[8]);
233 <    return(HUNTED_PASS);
234 <  }
233 >    return HUNTED_PASS;
234 >  }
235  
236 <  sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
237 <             me.name, source_p->name, parv[server]);
257 <  return(HUNTED_NOSUCH);
236 >  sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
237 >  return HUNTED_NOSUCH;
238   }
239  
240   /* try_connections()
# Line 270 | Line 250 | hunt_server(struct Client *client_p, str
250   void
251   try_connections(void *unused)
252   {
253 <  dlink_node *ptr = NULL;
274 <  struct MaskItem *conf;
275 <  int confrq;
253 >  dlink_node *node = NULL;
254  
277  /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
255    if (GlobalSetOptions.autoconn == 0)
256      return;
257  
258 <  DLINK_FOREACH(ptr, server_items.head)
258 >  DLINK_FOREACH(node, server_items.head)
259    {
260 <    conf = ptr->data;
260 >    struct MaskItem *conf = node->data;
261  
262      assert(conf->type == CONF_SERVER);
263  
264 <    /* Also when already connecting! (update holdtimes) --SRB
265 <     */
289 <    if (!conf->port ||!IsConfAllowAutoConn(conf))
264 >    /* Also when already connecting! (update holdtimes) --SRB */
265 >    if (!conf->port || !IsConfAllowAutoConn(conf))
266        continue;
267  
268 <
269 <    /* Skip this entry if the use of it is still on hold until
268 >    /*
269 >     * Skip this entry if the use of it is still on hold until
270       * future. Otherwise handle this entry (and set it on hold
271       * until next time). Will reset only hold times, if already
272       * made one successfull connection... [this algorithm is
# Line 299 | Line 275 | try_connections(void *unused)
275      if (conf->until > CurrentTime)
276        continue;
277  
278 <    if (conf->class == NULL)
303 <      confrq = DEFAULT_CONNECTFREQUENCY;
304 <    else
305 <    {
306 <      confrq = conf->class->con_freq;
307 <      if (confrq < MIN_CONN_FREQ)
308 <        confrq = MIN_CONN_FREQ;
309 <    }
278 >    assert(conf->class);
279  
280 <    conf->until = CurrentTime + confrq;
280 >    conf->until = CurrentTime + conf->class->con_freq;
281  
282 <    /* Found a CONNECT config with port specified, scan clients
282 >    /*
283 >     * Found a CONNECT config with port specified, scan clients
284       * and see if this server is already connected?
285       */
286 <    if (hash_find_server(conf->name) != NULL)
286 >    if (hash_find_server(conf->name))
287        continue;
288  
289      if (conf->class->ref_count < conf->class->max_total)
290      {
291 <      /* Go to the end of the list, if not already last */
292 <      if (ptr->next != NULL)
291 >      /* Move this entry to the end of the list, if not already last */
292 >      if (node->next)
293        {
294 <        dlinkDelete(ptr, &server_items);
294 >        dlinkDelete(node, &server_items);
295          dlinkAddTail(conf, &conf->node, &server_items);
296        }
297  
298        if (find_servconn_in_progress(conf->name))
299          return;
300  
301 <      /* We used to only print this if serv_connect() actually
301 >      /*
302 >       * We used to only print this if serv_connect() actually
303         * succeeded, but since comm_tcp_connect() can call the callback
304         * immediately if there is an error, we were getting error messages
305         * in the wrong order. SO, we just print out the activated line,
# Line 337 | Line 308 | try_connections(void *unused)
308         *   -- adrian
309         */
310        if (ConfigServerHide.hide_server_ips)
311 <        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
311 >        sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
312                               "Connection to %s activated.",
313                               conf->name);
314        else
315 <        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
315 >        sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
316                               "Connection to %s[%s] activated.",
317                               conf->name, conf->host);
318  
# Line 355 | Line 326 | try_connections(void *unused)
326   int
327   valid_servname(const char *name)
328   {
329 <  unsigned int length = 0;
359 <  unsigned int dots   = 0;
329 >  unsigned int dots = 0;
330    const char *p = name;
331  
332    for (; *p; ++p)
# Line 364 | Line 334 | valid_servname(const char *name)
334      if (!IsServChar(*p))
335        return 0;
336  
367    ++length;
368
337      if (*p == '.')
338        ++dots;
339    }
340  
341 <  return dots != 0 && length <= HOSTLEN;
341 >  return dots && (p - name) <= HOSTLEN;
342   }
343  
344   int
345   check_server(const char *name, struct Client *client_p)
346   {
347 <  dlink_node *ptr;
347 >  dlink_node *node = NULL;
348    struct MaskItem *conf        = NULL;
349    struct MaskItem *server_conf = NULL;
350    int error = -1;
351  
352 <  assert(client_p != NULL);
352 >  assert(client_p);
353  
354 <  /* loop through looking for all possible connect items that might work */
355 <  DLINK_FOREACH(ptr, server_items.head)
354 >  /* Loop through looking for all possible connect items that might work */
355 >  DLINK_FOREACH(node, server_items.head)
356    {
357 <    conf = ptr->data;
357 >    conf = node->data;
358  
359      if (match(name, conf->name))
360        continue;
361  
362      error = -3;
363  
364 <    /* XXX: Fix me for IPv6                    */
397 <    /* XXX sockhost is the IPv4 ip as a string */
398 <    if (!match(conf->host, client_p->host) ||
364 >    if (!match(conf->host, client_p->host) ||
365          !match(conf->host, client_p->sockhost))
366      {
367        error = -2;
368  
369 <      if (!match_conf_password(client_p->localClient->passwd, conf))
369 >      if (!match_conf_password(client_p->connection->password, conf))
370          return -2;
371  
372 +      if (!EmptyString(conf->certfp))
373 +        if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
374 +          return -4;
375 +
376        server_conf = conf;
377      }
378    }
379  
380    if (server_conf == NULL)
381 <    return(error);
381 >    return error;
382  
383    attach_conf(client_p, server_conf);
384  
385 <
416 <  if (server_conf != NULL)
385 >  switch (server_conf->aftype)
386    {
387 <    struct sockaddr_in *v4;
419 < #ifdef IPV6
420 <    struct sockaddr_in6 *v6;
421 < #endif
422 <    switch (server_conf->aftype)
387 >    case AF_INET6:
388      {
389 < #ifdef IPV6
425 <      case AF_INET6:
426 <        v6 = (struct sockaddr_in6 *)&server_conf->addr;
389 >      const struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)&server_conf->addr;
390  
391 <        if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
392 <          memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
393 <        break;
394 < #endif
395 <      case AF_INET:
396 <        v4 = (struct sockaddr_in *)&server_conf->addr;
391 >      if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
392 >        memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr));
393 >      break;
394 >    }
395 >    case AF_INET:
396 >    {
397 >      const struct sockaddr_in *v4 = (struct sockaddr_in *)&server_conf->addr;
398  
399 <        if (v4->sin_addr.s_addr == INADDR_NONE)
400 <          memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
401 <        break;
399 >      if (v4->sin_addr.s_addr == INADDR_NONE)
400 >        memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr));
401 >      break;
402      }
403    }
404  
405 <  return(0);
405 >  return 0;
406   }
407  
408   /* add_capability()
# Line 451 | Line 415 | check_server(const char *name, struct Cl
415   *                modules to dynamically add or subtract their capability.
416   */
417   void
418 < add_capability(const char *capab_name, int cap_flag, int add_to_default)
418 > add_capability(const char *name, unsigned int flag)
419   {
420 <  struct Capability *cap = MyMalloc(sizeof(*cap));
420 >  struct Capability *cap = xcalloc(sizeof(*cap));
421  
422 <  cap->name = xstrdup(capab_name);
423 <  cap->cap = cap_flag;
424 <  dlinkAdd(cap, &cap->node, &cap_list);
461 <
462 <  if (add_to_default)
463 <    default_server_capabs |= cap_flag;
422 >  cap->name = xstrdup(name);
423 >  cap->cap = flag;
424 >  dlinkAdd(cap, &cap->node, &server_capabilities_list);
425   }
426  
427   /* delete_capability()
# Line 469 | Line 430 | add_capability(const char *capab_name, i
430   * output       - NONE
431   * side effects - delete given capability from ones known.
432   */
433 < int
434 < delete_capability(const char *capab_name)
433 > void
434 > delete_capability(const char *name)
435   {
436 <  dlink_node *ptr;
476 <  dlink_node *next_ptr;
477 <  struct Capability *cap;
436 >  dlink_node *node = NULL, *node_next = NULL;
437  
438 <  DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
438 >  DLINK_FOREACH_SAFE(node, node_next, server_capabilities_list.head)
439    {
440 <    cap = ptr->data;
440 >    struct Capability *cap = node->data;
441  
442 <    if (cap->cap != 0)
442 >    if (!irccmp(cap->name, name))
443      {
444 <      if (irccmp(cap->name, capab_name) == 0)
445 <      {
446 <        default_server_capabs &= ~(cap->cap);
488 <        dlinkDelete(ptr, &cap_list);
489 <        MyFree(cap->name);
490 <        cap->name = NULL;
491 <        MyFree(cap);
492 <      }
444 >      dlinkDelete(node, &server_capabilities_list);
445 >      xfree(cap->name);
446 >      xfree(cap);
447      }
448    }
495
496  return 0;
449   }
450  
451   /*
# Line 503 | Line 455 | delete_capability(const char *capab_name
455   * output       - 0 if not found CAPAB otherwise
456   * side effects - none
457   */
458 < int
459 < find_capability(const char *capab)
458 > unsigned int
459 > find_capability(const char *name)
460   {
461 <  const dlink_node *ptr = NULL;
461 >  const dlink_node *node = NULL;
462  
463 <  DLINK_FOREACH(ptr, cap_list.head)
463 >  DLINK_FOREACH(node, server_capabilities_list.head)
464    {
465 <    const struct Capability *cap = ptr->data;
465 >    const struct Capability *cap = node->data;
466  
467 <    if (cap->cap && !irccmp(cap->name, capab))
467 >    if (!irccmp(cap->name, name))
468        return cap->cap;
469    }
470  
# Line 528 | Line 480 | find_capability(const char *capab)
480   *
481   */
482   void
483 < send_capabilities(struct Client *client_p, int cap_can_send)
483 > send_capabilities(struct Client *client_p)
484   {
485 <  struct Capability *cap=NULL;
486 <  char msgbuf[IRCD_BUFSIZE];
535 <  char *t;
536 <  int tl;
537 <  dlink_node *ptr;
538 <
539 <  t = msgbuf;
485 >  char buf[IRCD_BUFSIZE] = "";
486 >  const dlink_node *node = NULL;
487  
488 <  DLINK_FOREACH(ptr, cap_list.head)
488 >  DLINK_FOREACH(node, server_capabilities_list.head)
489    {
490 <    cap = ptr->data;
490 >    const struct Capability *cap = node->data;
491  
492 <    if (cap->cap & (cap_can_send|default_server_capabs))
546 <    {
547 <      tl = sprintf(t, "%s ", cap->name);
548 <      t += tl;
549 <    }
550 <  }
551 <
552 <  *(t - 1) = '\0';
553 <  sendto_one(client_p, "CAPAB :%s", msgbuf);
554 < }
492 >    strlcat(buf, cap->name, sizeof(buf));
493  
494 < /* sendnick_TS()
495 < *
558 < * inputs       - client (server) to send nick towards
559 < *          - client to send nick for
560 < * output       - NONE
561 < * side effects - NICK message is sent towards given client_p
562 < */
563 < void
564 < sendnick_TS(struct Client *client_p, struct Client *target_p)
565 < {
566 <  static char ubuf[12];
567 <
568 <  if (!IsClient(target_p))
569 <    return;
570 <
571 <  send_umode(NULL, target_p, 0, SEND_UMODES, ubuf);
572 <
573 <  if (ubuf[0] == '\0')
574 <  {
575 <    ubuf[0] = '+';
576 <    ubuf[1] = '\0';
494 >    if (node->next)
495 >      strlcat(buf, " ", sizeof(buf));
496    }
497  
498 <  if (IsCapable(client_p, CAP_SVS))
580 <  {
581 <    if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
582 <      sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s %s :%s",
583 <                 target_p->servptr->id,
584 <                 target_p->name, target_p->hopcount + 1,
585 <                 (unsigned long) target_p->tsinfo,
586 <                 ubuf, target_p->username, target_p->host,
587 <                 (MyClient(target_p) && IsIPSpoof(target_p)) ?
588 <                 "0" : target_p->sockhost, target_p->id,
589 <                 target_p->svid, target_p->info);
590 <    else
591 <      sendto_one(client_p, "NICK %s %d %lu %s %s %s %s %s :%s",
592 <                 target_p->name, target_p->hopcount + 1,
593 <                 (unsigned long) target_p->tsinfo,
594 <                 ubuf, target_p->username, target_p->host,
595 <                 target_p->servptr->name, target_p->svid,
596 <                 target_p->info);
597 <  }
598 <  else
599 <  {
600 <    if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
601 <      sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
602 <                 target_p->servptr->id,
603 <                 target_p->name, target_p->hopcount + 1,
604 <                 (unsigned long) target_p->tsinfo,
605 <                 ubuf, target_p->username, target_p->host,
606 <                 (MyClient(target_p) && IsIPSpoof(target_p)) ?
607 <                 "0" : target_p->sockhost, target_p->id, target_p->info);
608 <    else
609 <      sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
610 <                 target_p->name, target_p->hopcount + 1,
611 <                 (unsigned long) target_p->tsinfo,
612 <                 ubuf, target_p->username, target_p->host,
613 <                 target_p->servptr->name, target_p->info);
614 <  }
615 <
616 <  if (target_p->away[0])
617 <    sendto_one(client_p, ":%s AWAY :%s", ID_or_name(target_p, client_p),
618 <               target_p->away);
619 <
498 >  sendto_one(client_p, "CAPAB :%s", buf);
499   }
500  
501   /*
# Line 627 | Line 506 | sendnick_TS(struct Client *client_p, str
506   * side effects - build up string representing capabilities of server listed
507   */
508   const char *
509 < show_capabilities(struct Client *target_p)
509 > show_capabilities(const struct Client *target_p)
510   {
511 <  static char msgbuf[IRCD_BUFSIZE];
512 <  char *t = msgbuf;
634 <  dlink_node *ptr;
511 >  static char msgbuf[IRCD_BUFSIZE] = "";
512 >  const dlink_node *node = NULL;
513  
514 <  t += sprintf(msgbuf, "TS ");
514 >  strlcpy(msgbuf, "TS", sizeof(msgbuf));
515  
516 <  DLINK_FOREACH(ptr, cap_list.head)
516 >  DLINK_FOREACH(node, server_capabilities_list.head)
517    {
518 <    const struct Capability *cap = ptr->data;
518 >    const struct Capability *cap = node->data;
519  
520 <    if (IsCapable(target_p, cap->cap))
521 <      t += sprintf(t, "%s ", cap->name);
520 >    if (!IsCapable(target_p, cap->cap))
521 >      continue;
522 >
523 >    strlcat(msgbuf,       " ", sizeof(msgbuf));
524 >    strlcat(msgbuf, cap->name, sizeof(msgbuf));
525    }
526  
646  *(t - 1) = '\0';
527    return msgbuf;
528   }
529  
# Line 658 | Line 538 | struct Server *
538   make_server(struct Client *client_p)
539   {
540    if (client_p->serv == NULL)
541 <    client_p->serv = MyMalloc(sizeof(struct Server));
541 >    client_p->serv = xcalloc(sizeof(struct Server));
542  
543    return client_p->serv;
544   }
545  
666 /* server_estab()
667 *
668 * inputs       - pointer to a struct Client
669 * output       -
670 * side effects -
671 */
672 void
673 server_estab(struct Client *client_p)
674 {
675  struct Client *target_p;
676  struct MaskItem *conf = NULL;
677  char *host;
678  const char *inpath;
679  static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
680  dlink_node *ptr;
681 #ifdef HAVE_LIBCRYPTO
682  const COMP_METHOD *compression = NULL, *expansion = NULL;
683 #endif
684
685  assert(client_p != NULL);
686
687  strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
688
689  inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
690  host   = client_p->name;
691
692  if ((conf = find_conf_name(&client_p->localClient->confs, host, CONF_SERVER))
693      == NULL)
694  {
695    /* This shouldn't happen, better tell the ops... -A1kmm */
696    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
697                         "Warning: Lost connect{} block "
698                         "for server %s(this shouldn't happen)!", host);
699    exit_client(client_p, &me, "Lost connect{} block!");
700    return;
701  }
702
703  MyFree(client_p->localClient->passwd);
704  client_p->localClient->passwd = NULL;
705
706  /* Its got identd, since its a server */
707  SetGotId(client_p);
708
709  /* If there is something in the serv_list, it might be this
710   * connecting server..
711   */
712  if (!ServerInfo.hub && serv_list.head)  
713  {
714    if (client_p != serv_list.head->data || serv_list.head->next)
715    {
716      ++ServerStats.is_ref;
717      sendto_one(client_p, "ERROR :I'm a leaf not a hub");
718      exit_client(client_p, &me, "I'm a leaf");
719      return;
720    }
721  }
722
723  if (IsUnknown(client_p))
724  {
725    /* jdc -- 1.  Use EmptyString(), not [0] index reference.
726     *        2.  Check conf->spasswd, not conf->passwd.
727     */
728    if (!EmptyString(conf->spasswd))
729      sendto_one(client_p, "PASS %s TS %d %s",
730                 conf->spasswd, TS_CURRENT, me.id);
731
732    send_capabilities(client_p, 0);
733
734    sendto_one(client_p, "SERVER %s 1 :%s%s",
735               me.name, ConfigServerHide.hidden ? "(H) " : "", me.info);
736  }
737
738  sendto_one(client_p, "SVINFO %d %d 0 :%lu", TS_CURRENT, TS_MIN,
739             (unsigned long)CurrentTime);
740
741  /* assumption here is if they passed the correct TS version, they also passed an SID */
742  if (IsCapable(client_p, CAP_TS6))
743    hash_add_id(client_p);
744
745  /* XXX Does this ever happen? I don't think so -db */
746  detach_conf(client_p, CONF_OPER);
747
748  /* *WARNING*
749  **    In the following code in place of plain server's
750  **    name we send what is returned by get_client_name
751  **    which may add the "sockhost" after the name. It's
752  **    *very* *important* that there is a SPACE between
753  **    the name and sockhost (if present). The receiving
754  **    server will start the information field from this
755  **    first blank and thus puts the sockhost into info.
756  **    ...a bit tricky, but you have been warned, besides
757  **    code is more neat this way...  --msa
758  */
759  client_p->servptr = &me;
760
761  if (IsClosing(client_p))
762    return;
763
764  SetServer(client_p);
765
766  /* Update the capability combination usage counts. -A1kmm */
767  set_chcap_usage_counts(client_p);
768
769  /* Some day, all these lists will be consolidated *sigh* */
770  dlinkAdd(client_p, &client_p->lnode, &me.serv->server_list);
771
772  assert(dlinkFind(&unknown_list, client_p));
773
774  dlink_move_node(&client_p->localClient->lclient_node,
775                  &unknown_list, &serv_list);
776
777  Count.myserver++;
778
779  dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
780  hash_add_client(client_p);
781
782  /* doesnt duplicate client_p->serv if allocated this struct already */
783  make_server(client_p);
784
785  /* fixing eob timings.. -gnp */
786  client_p->localClient->firsttime = CurrentTime;
787
788  if (find_matching_name_conf(CONF_SERVICE, client_p->name, NULL, NULL, 0))
789    AddFlag(client_p, FLAGS_SERVICE);
790
791  /* Show the real host/IP to admins */
792 #ifdef HAVE_LIBCRYPTO
793  if (client_p->localClient->fd.ssl)
794  {
795    compression = SSL_get_current_compression(client_p->localClient->fd.ssl);
796    expansion   = SSL_get_current_expansion(client_p->localClient->fd.ssl);
797
798    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
799                         "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
800                         inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
801                         compression ? SSL_COMP_get_name(compression) : "NONE",
802                         expansion ? SSL_COMP_get_name(expansion) : "NONE",
803                         show_capabilities(client_p));
804    /* Now show the masked hostname/IP to opers */
805    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
806                         "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
807                         inpath, ssl_get_cipher(client_p->localClient->fd.ssl),
808                         compression ? SSL_COMP_get_name(compression) : "NONE",
809                         expansion ? SSL_COMP_get_name(expansion) : "NONE",
810                         show_capabilities(client_p));
811    ilog(LOG_TYPE_IRCD, "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
812         inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
813         compression ? SSL_COMP_get_name(compression) : "NONE",
814         expansion ? SSL_COMP_get_name(expansion) : "NONE",
815         show_capabilities(client_p));
816  }
817  else
818 #endif
819  {
820    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
821                         "Link with %s established: (Capabilities: %s)",
822                         inpath_ip, show_capabilities(client_p));
823    /* Now show the masked hostname/IP to opers */
824    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
825                         "Link with %s established: (Capabilities: %s)",
826                         inpath, show_capabilities(client_p));
827    ilog(LOG_TYPE_IRCD, "Link with %s established: (Capabilities: %s)",
828         inpath_ip, show_capabilities(client_p));
829  }
830
831  fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
832
833  /* Old sendto_serv_but_one() call removed because we now
834  ** need to send different names to different servers
835  ** (domain name matching) Send new server to other servers.
836  */
837  DLINK_FOREACH(ptr, serv_list.head)
838  {
839    target_p = ptr->data;
840
841    if (target_p == client_p)
842      continue;
843
844    if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
845      sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
846                 me.id, client_p->name, client_p->id,
847                 IsHidden(client_p) ? "(H) " : "",
848                 client_p->info);
849    else
850      sendto_one(target_p,":%s SERVER %s 2 :%s%s",
851                 me.name, client_p->name,
852                 IsHidden(client_p) ? "(H) " : "",
853                 client_p->info);
854  }
855
856  /* Pass on my client information to the new server
857  **
858  ** First, pass only servers (idea is that if the link gets
859  ** cancelled beacause the server was already there,
860  ** there are no NICK's to be cancelled...). Of course,
861  ** if cancellation occurs, all this info is sent anyway,
862  ** and I guess the link dies when a read is attempted...? --msa
863  **
864  ** Note: Link cancellation to occur at this point means
865  ** that at least two servers from my fragment are building
866  ** up connection this other fragment at the same time, it's
867  ** a race condition, not the normal way of operation...
868  **
869  ** ALSO NOTE: using the get_client_name for server names--
870  **    see previous *WARNING*!!! (Also, original inpath
871  **    is destroyed...)
872  */
873
874  DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
875  {
876    target_p = ptr->data;
877
878    /* target_p->from == target_p for target_p == client_p */
879    if (IsMe(target_p) || target_p->from == client_p)
880      continue;
881
882    if (IsCapable(client_p, CAP_TS6))
883    {
884      if (HasID(target_p))
885        sendto_one(client_p, ":%s SID %s %d %s :%s%s",
886                   ID(target_p->servptr), target_p->name, target_p->hopcount+1,
887                   target_p->id, IsHidden(target_p) ? "(H) " : "",
888                   target_p->info);
889      else  /* introducing non-ts6 server */
890        sendto_one(client_p, ":%s SERVER %s %d :%s%s",
891                   ID(target_p->servptr), target_p->name, target_p->hopcount+1,
892                   IsHidden(target_p) ? "(H) " : "", target_p->info);
893    }
894    else
895      sendto_one(client_p, ":%s SERVER %s %d :%s%s",
896                 target_p->servptr->name, target_p->name, target_p->hopcount+1,
897                 IsHidden(target_p) ? "(H) " : "", target_p->info);
898  }
899
900  server_burst(client_p);
901 }
902
903 /* server_burst()
904 *
905 * inputs       - struct Client pointer server
906 *              -
907 * output       - none
908 * side effects - send a server burst
909 * bugs         - still too long
910 */
911 static void
912 server_burst(struct Client *client_p)
913 {
914  /* Send it in the shortened format with the TS, if
915  ** it's a TS server; walk the list of channels, sending
916  ** all the nicks that haven't been sent yet for each
917  ** channel, then send the channel itself -- it's less
918  ** obvious than sending all nicks first, but on the
919  ** receiving side memory will be allocated more nicely
920  ** saving a few seconds in the handling of a split
921  ** -orabidoo
922  */
923
924  burst_all(client_p);
925
926  /* EOB stuff is now in burst_all */
927  /* Always send a PING after connect burst is done */
928  sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
929 }
930
931 /* burst_all()
932 *
933 * inputs       - pointer to server to send burst to
934 * output       - NONE
935 * side effects - complete burst of channels/nicks is sent to client_p
936 */
937 static void
938 burst_all(struct Client *client_p)
939 {
940  dlink_node *ptr = NULL;
941
942  DLINK_FOREACH(ptr, global_channel_list.head)
943  {
944    struct Channel *chptr = ptr->data;
945
946    if (dlink_list_length(&chptr->members) != 0)
947    {
948      burst_members(client_p, chptr);
949      send_channel_modes(client_p, chptr);
950
951      if (IsCapable(client_p, CAP_TBURST))
952        send_tb(client_p, chptr);
953    }
954  }
955
956  /* also send out those that are not on any channel
957   */
958  DLINK_FOREACH(ptr, global_client_list.head)
959  {
960    struct Client *target_p = ptr->data;
961
962    if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
963      sendnick_TS(client_p, target_p);
964    
965    DelFlag(target_p, FLAGS_BURSTED);
966  }
967
968  /* We send the time we started the burst, and let the remote host determine an EOB time,
969  ** as otherwise we end up sending a EOB of 0   Sending here means it gets sent last -- fl
970  */
971  /* Its simpler to just send EOB and use the time its been connected.. --fl_ */
972  if (IsCapable(client_p, CAP_EOB))
973    sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
974 }
975
976 /*
977 * send_tb
978 *
979 * inputs       - pointer to Client
980 *              - pointer to channel
981 * output       - NONE
982 * side effects - Called on a server burst when
983 *                server is CAP_TBURST capable
984 */
985 static void
986 send_tb(struct Client *client_p, struct Channel *chptr)
987 {
988  /*
989   * We may also send an empty topic here, but only if topic_time isn't 0,
990   * i.e. if we had a topic that got unset.  This is required for syncing
991   * topics properly.
992   *
993   * Imagine the following scenario: Our downlink introduces a channel
994   * to us with a TS that is equal to ours, but the channel topic on
995   * their side got unset while the servers were in splitmode, which means
996   * their 'topic' is newer.  They simply wanted to unset it, so we have to
997   * deal with it in a more sophisticated fashion instead of just resetting
998   * it to their old topic they had before.  Read m_tburst.c:ms_tburst
999   * for further information   -Michael
1000   */
1001  if (chptr->topic_time != 0)
1002    sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
1003               ID_or_name(&me, client_p),
1004               (unsigned long)chptr->channelts, chptr->chname,
1005               (unsigned long)chptr->topic_time,
1006               chptr->topic_info,
1007               chptr->topic);
1008 }
1009
1010 /* burst_members()
1011 *
1012 * inputs       - pointer to server to send members to
1013 *              - dlink_list pointer to membership list to send
1014 * output       - NONE
1015 * side effects -
1016 */
1017 static void
1018 burst_members(struct Client *client_p, struct Channel *chptr)
1019 {
1020  struct Client *target_p;
1021  struct Membership *ms;
1022  dlink_node *ptr;
1023
1024  DLINK_FOREACH(ptr, chptr->members.head)
1025  {
1026    ms       = ptr->data;
1027    target_p = ms->client_p;
1028
1029    if (!HasFlag(target_p, FLAGS_BURSTED))
1030    {
1031      AddFlag(target_p, FLAGS_BURSTED);
1032
1033      if (target_p->from != client_p)
1034        sendnick_TS(client_p, target_p);
1035    }
1036  }
1037 }
1038
546   /* New server connection code
547   * Based upon the stuff floating about in s_bsd.c
548   *   -- adrian
# Line 1043 | Line 550 | burst_members(struct Client *client_p, s
550  
551   /* serv_connect() - initiate a server connection
552   *
553 < * inputs       - pointer to conf
553 > * inputs       - pointer to conf
554   *              - pointer to client doing the connect
555   * output       -
556   * side effects -
# Line 1060 | Line 567 | burst_members(struct Client *client_p, s
567   int
568   serv_connect(struct MaskItem *conf, struct Client *by)
569   {
570 <  struct Client *client_p;
571 <  char buf[HOSTIPLEN + 1];
570 >  struct Client *client_p = NULL;
571 >  char buf[HOSTIPLEN + 1] = "";
572  
573    /* conversion structs */
574    struct sockaddr_in *v4;
1068  /* Make sure conf is useful */
1069  assert(conf != NULL);
575  
576 +  /* Make sure conf is useful */
577 +  assert(conf);
578  
579 <  getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
579 >  getnameinfo((const struct sockaddr *)&conf->addr, conf->addr.ss_len,
580                buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
581    ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
582         buf);
# Line 1077 | Line 584 | serv_connect(struct MaskItem *conf, stru
584    /* Still processing a DNS lookup? -> exit */
585    if (conf->dns_pending)
586    {
587 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
587 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
588                           "Error connecting to %s: DNS lookup for connect{} in progress.",
589                           conf->name);
590 <    return (0);
590 >    return 0;
591    }
592  
593    if (conf->dns_failed)
594    {
595 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
595 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
596                           "Error connecting to %s: DNS lookup for connect{} failed.",
597                           conf->name);
598 <    return (0);
598 >    return 0;
599    }
600  
601 <  /* Make sure this server isn't already connected
602 <   * Note: conf should ALWAYS be a valid C: line
601 >  /*
602 >   * Make sure this server isn't already connected.
603 >   * Note: conf should ALWAYS be a valid connect {} block
604     */
605 <  if ((client_p = hash_find_server(conf->name)) != NULL)
606 <  {
607 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
608 <                         "Server %s already present from %s",
609 <                         conf->name, get_client_name(client_p, SHOW_IP));
610 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
611 <                         "Server %s already present from %s",
612 <                         conf->name, get_client_name(client_p, MASK_IP));
605 >  if ((client_p = hash_find_server(conf->name)))
606 >  {
607 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
608 >                         "Server %s already present from %s",
609 >                         conf->name, get_client_name(client_p, SHOW_IP));
610 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
611 >                         "Server %s already present from %s",
612 >                         conf->name, get_client_name(client_p, MASK_IP));
613      if (by && IsClient(by) && !MyClient(by))
614 <      sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
615 <                 me.name, by->name, conf->name,
616 <                 get_client_name(client_p, MASK_IP));
1109 <    return (0);
614 >      sendto_one_notice(by, &me, ":Server %s already present from %s",
615 >                        conf->name, get_client_name(client_p, MASK_IP));
616 >    return 0;
617    }
618 <    
618 >
619    /* Create a local client */
620    client_p = make_client(NULL);
621  
# Line 1119 | Line 626 | serv_connect(struct MaskItem *conf, stru
626    /* We already converted the ip once, so lets use it - stu */
627    strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
628  
629 <  /* create a socket for the server connection */
630 <  if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family,
1124 <                SOCK_STREAM, 0, NULL) < 0)
629 >  /* Create a socket for the server connection */
630 >  if (comm_open(&client_p->connection->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0)
631    {
632      /* Eek, failure to create the socket */
633 <    report_error(L_ALL,
634 <                 "opening stream socket to %s: %s", conf->name, errno);
633 >    report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
634 >
635      SetDead(client_p);
636 <    exit_client(client_p, &me, "Connection failed");
637 <    return (0);
636 >    exit_client(client_p, "Connection failed");
637 >    return 0;
638    }
639  
640 <  /* servernames are always guaranteed under HOSTLEN chars */
641 <  fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
640 >  /* Server names are always guaranteed under HOSTLEN chars */
641 >  fd_note(&client_p->connection->fd, "Server: %s", client_p->name);
642  
643 <  /* Attach config entries to client here rather than in
644 <   * serv_connect_callback(). This to avoid null pointer references.
643 >  /*
644 >   * Attach config entries to client here rather than in serv_connect_callback().
645 >   * This to avoid null pointer references.
646     */
647    if (!attach_connect_block(client_p, conf->name, conf->host))
648    {
649 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
650 <                         "Host %s is not enabled for connecting: no connect{} block",
651 <                         conf->name);
652 <    if (by && IsClient(by) && !MyClient(by))  
653 <      sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
654 <                 me.name, by->name, client_p->name);
649 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
650 >                         "Host %s is not enabled for connecting: no connect {} block",
651 >                         conf->name);
652 >    if (by && IsClient(by) && !MyClient(by))
653 >      sendto_one_notice(by, &me, ":Connect to host %s failed: no connect {} block", client_p->name);
654 >
655      SetDead(client_p);
656 <    exit_client(client_p, client_p, "Connection failed");
657 <    return (0);
656 >    exit_client(client_p, "Connection failed");
657 >    return 0;
658    }
659  
660 <  /* at this point we have a connection in progress and C/N lines
661 <   * attached to the client, the socket info should be saved in the
662 <   * client and it should either be resolved or have a valid address.
660 >  /*
661 >   * At this point we have a connection in progress and a connect {} block
662 >   * attached to the client, the socket info should be saved in the client
663 >   * and it should either be resolved or have a valid address.
664     *
665     * The socket has been connected or connect is in progress.
666     */
# Line 1164 | Line 672 | serv_connect(struct MaskItem *conf, stru
672      strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
673  
674    SetConnecting(client_p);
675 <  dlinkAdd(client_p, &client_p->node, &global_client_list);
1168 <  /* from def_fam */
1169 <  client_p->localClient->aftype = conf->aftype;
675 >  client_p->connection->aftype = conf->aftype;
676  
677    /* Now, initiate the connection */
678 <  /* XXX assume that a non 0 type means a specific bind address
678 >  /* XXX assume that a non 0 type means a specific bind address
679     * for this connect.
680     */
681    switch (conf->aftype)
682    {
683      case AF_INET:
684        v4 = (struct sockaddr_in*)&conf->bind;
685 <      if (v4->sin_addr.s_addr != 0)
685 >      if (v4->sin_addr.s_addr)
686        {
687          struct irc_ssaddr ipn;
688          memset(&ipn, 0, sizeof(struct irc_ssaddr));
689          ipn.ss.ss_family = AF_INET;
690          ipn.ss_port = 0;
691          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
692 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
693 <                         (struct sockaddr *)&ipn, ipn.ss_len,
694 <                         serv_connect_callback, client_p, conf->aftype,
695 <                         CONNECTTIMEOUT);
692 >        comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port,
693 >                         (struct sockaddr *)&ipn, ipn.ss_len,
694 >                         serv_connect_callback, client_p, conf->aftype,
695 >                         CONNECTTIMEOUT);
696        }
697 <      else if (ServerInfo.specific_ipv4_vhost)
697 >      else if (ConfigServerInfo.specific_ipv4_vhost)
698        {
699          struct irc_ssaddr ipn;
700          memset(&ipn, 0, sizeof(struct irc_ssaddr));
701          ipn.ss.ss_family = AF_INET;
702          ipn.ss_port = 0;
703 <        memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
704 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
703 >        memcpy(&ipn, &ConfigServerInfo.ip, sizeof(struct irc_ssaddr));
704 >        comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port,
705                           (struct sockaddr *)&ipn, ipn.ss_len,
706                           serv_connect_callback, client_p, conf->aftype,
707 <                         CONNECTTIMEOUT);
707 >                         CONNECTTIMEOUT);
708        }
709        else
710 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
711 <                         NULL, 0, serv_connect_callback, client_p, conf->aftype,
710 >        comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port,
711 >                         NULL, 0, serv_connect_callback, client_p, conf->aftype,
712                           CONNECTTIMEOUT);
713        break;
1208 #ifdef IPV6
714      case AF_INET6:
715        {
716 <        struct irc_ssaddr ipn;
717 <        struct sockaddr_in6 *v6;
718 <        struct sockaddr_in6 *v6conf;
719 <
720 <        memset(&ipn, 0, sizeof(struct irc_ssaddr));
721 <        v6conf = (struct sockaddr_in6 *)&conf->bind;
722 <        v6 = (struct sockaddr_in6 *)&ipn;
723 <
724 <        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr,
725 <                   sizeof(struct in6_addr)) != 0)
726 <        {
727 <          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
728 <          ipn.ss.ss_family = AF_INET6;
729 <          ipn.ss_port = 0;
730 <          comm_connect_tcp(&client_p->localClient->fd,
731 <                           conf->host, conf->port,
732 <                           (struct sockaddr *)&ipn, ipn.ss_len,
733 <                           serv_connect_callback, client_p,
734 <                           conf->aftype, CONNECTTIMEOUT);
735 <        }
1231 <        else if (ServerInfo.specific_ipv6_vhost)
716 >        struct irc_ssaddr ipn;
717 >        struct sockaddr_in6 *v6;
718 >        struct sockaddr_in6 *v6conf;
719 >
720 >        memset(&ipn, 0, sizeof(struct irc_ssaddr));
721 >        v6conf = (struct sockaddr_in6 *)&conf->bind;
722 >        v6 = (struct sockaddr_in6 *)&ipn;
723 >
724 >        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
725 >        {
726 >          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
727 >          ipn.ss.ss_family = AF_INET6;
728 >          ipn.ss_port = 0;
729 >          comm_connect_tcp(&client_p->connection->fd,
730 >                           conf->host, conf->port,
731 >                           (struct sockaddr *)&ipn, ipn.ss_len,
732 >                           serv_connect_callback, client_p,
733 >                           conf->aftype, CONNECTTIMEOUT);
734 >        }
735 >        else if (ConfigServerInfo.specific_ipv6_vhost)
736          {
737 <          memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
738 <          ipn.ss.ss_family = AF_INET6;
739 <          ipn.ss_port = 0;
740 <          comm_connect_tcp(&client_p->localClient->fd,
741 <                           conf->host, conf->port,
742 <                           (struct sockaddr *)&ipn, ipn.ss_len,
743 <                           serv_connect_callback, client_p,
744 <                           conf->aftype, CONNECTTIMEOUT);
745 <        }
746 <        else
747 <          comm_connect_tcp(&client_p->localClient->fd,
748 <                           conf->host, conf->port,
749 <                           NULL, 0, serv_connect_callback, client_p,
750 <                           conf->aftype, CONNECTTIMEOUT);
737 >          memcpy(&ipn, &ConfigServerInfo.ip6, sizeof(struct irc_ssaddr));
738 >          ipn.ss.ss_family = AF_INET6;
739 >          ipn.ss_port = 0;
740 >          comm_connect_tcp(&client_p->connection->fd,
741 >                           conf->host, conf->port,
742 >                           (struct sockaddr *)&ipn, ipn.ss_len,
743 >                           serv_connect_callback, client_p,
744 >                           conf->aftype, CONNECTTIMEOUT);
745 >        }
746 >        else
747 >          comm_connect_tcp(&client_p->connection->fd,
748 >                           conf->host, conf->port,
749 >                           NULL, 0, serv_connect_callback, client_p,
750 >                           conf->aftype, CONNECTTIMEOUT);
751        }
1248 #endif
752    }
753 <  return (1);
753 >
754 >  return 1;
755   }
756  
757 < #ifdef HAVE_LIBCRYPTO
757 > #ifdef HAVE_TLS
758   static void
759   finish_ssl_server_handshake(struct Client *client_p)
760   {
761    struct MaskItem *conf = NULL;
762  
763 <  conf = find_conf_name(&client_p->localClient->confs,
763 >  conf = find_conf_name(&client_p->connection->confs,
764                          client_p->name, CONF_SERVER);
765    if (conf == NULL)
766    {
767 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
768 <                         "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
769 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
767 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
768 >                         "Lost connect{} block for %s", get_client_name(client_p, SHOW_IP));
769 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
770                           "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
771  
772 <    exit_client(client_p, &me, "Lost connect{} block");
772 >    exit_client(client_p, "Lost connect{} block");
773      return;
774    }
775  
776 <  /* jdc -- Check and send spasswd, not passwd. */
1273 <  if (!EmptyString(conf->spasswd))
1274 <    sendto_one(client_p, "PASS %s TS %d %s",
1275 <               conf->spasswd, TS_CURRENT, me.id);
776 >  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
777  
778 <  send_capabilities(client_p, 0);
778 >  send_capabilities(client_p);
779  
780    sendto_one(client_p, "SERVER %s 1 :%s%s",
781               me.name, ConfigServerHide.hidden ? "(H) " : "",
782               me.info);
783  
784 <  /* If we've been marked dead because a send failed, just exit
784 >  /*
785 >   * If we've been marked dead because a send failed, just exit
786     * here now and save everyone the trouble of us ever existing.
787     */
788    if (IsDead(client_p))
789    {
790 <      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
791 <                           "%s[%s] went dead during handshake",
792 <                           client_p->name,
793 <                           client_p->host);
794 <      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1293 <                           "%s went dead during handshake", client_p->name);
1294 <      return;
790 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
791 >                         "%s went dead during handshake", get_client_name(client_p, SHOW_IP));
792 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
793 >                         "%s went dead during handshake", get_client_name(client_p, MASK_IP));
794 >    return;
795    }
796  
797    /* don't move to serv_list yet -- we haven't sent a burst! */
798    /* If we get here, we're ok, so lets start reading some data */
799 <  comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, read_packet, client_p, 0);
799 >  comm_setselect(&client_p->connection->fd, COMM_SELECT_READ, read_packet, client_p, 0);
800   }
801  
802   static void
803 < ssl_server_handshake(fde_t *fd, struct Client *client_p)
803 > ssl_server_handshake(fde_t *fd, void *data)
804   {
805 <  int ret;
806 <  int err;
807 <
1308 <  ret = SSL_connect(client_p->localClient->fd.ssl);
805 >  struct Client *client_p = data;
806 >  int res = 0;
807 >  const char *sslerr = NULL;
808  
809 <  if (ret <= 0)
809 >  tls_handshake_status_t ret = tls_handshake(&client_p->connection->fd.ssl, TLS_ROLE_CLIENT, &sslerr);
810 >  if (ret != TLS_HANDSHAKE_DONE)
811    {
812 <    switch ((err = SSL_get_error(client_p->localClient->fd.ssl, ret)))
812 >    if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT)
813 >    {
814 >      exit_client(client_p, "Timeout during TLS handshake");
815 >      return;
816 >    }
817 >
818 >    switch (ret)
819      {
820 <      case SSL_ERROR_WANT_WRITE:
821 <        comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
822 <                       (PF *)ssl_server_handshake, client_p, 0);
820 >      case TLS_HANDSHAKE_WANT_WRITE:
821 >        comm_setselect(&client_p->connection->fd, COMM_SELECT_WRITE,
822 >                       ssl_server_handshake, client_p, CONNECTTIMEOUT);
823          return;
824 <      case SSL_ERROR_WANT_READ:
825 <        comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
826 <                       (PF *)ssl_server_handshake, client_p, 0);
824 >      case TLS_HANDSHAKE_WANT_READ:
825 >        comm_setselect(&client_p->connection->fd, COMM_SELECT_READ,
826 >                       ssl_server_handshake, client_p, CONNECTTIMEOUT);
827          return;
828        default:
829        {
830 <        const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
1325 <        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
830 >        sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
831                               "Error connecting to %s: %s", client_p->name,
832 <                             sslerr ? sslerr : "unknown SSL error");
833 <        exit_client(client_p, client_p, "Error during SSL handshake");
832 >                             sslerr ? sslerr : "unknown TLS error");
833 >        exit_client(client_p, "Error during TLS handshake");
834          return;
835        }
836      }
837    }
838  
839 +  comm_settimeout(&client_p->connection->fd, 0, NULL, NULL);
840 +
841 +  if (!tls_verify_cert(&client_p->connection->fd.ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp, &res))
842 +    ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad TLS client certificate: %d",
843 +         client_p->name, client_p->username, client_p->host, res);
844 +
845    finish_ssl_server_handshake(client_p);
846   }
847  
848   static void
849 < ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd)
849 > ssl_connect_init(struct Client *client_p, const struct MaskItem *conf, fde_t *fd)
850   {
851 <  if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.client_ctx)) == NULL)
851 >  if (!tls_new(&client_p->connection->fd.ssl, fd->fd, TLS_ROLE_CLIENT))
852    {
1342    ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
1343         ERR_error_string(ERR_get_error(), NULL));
853      SetDead(client_p);
854 <    exit_client(client_p, client_p, "SSL_new failed");
854 >    exit_client(client_p, "TLS context initialization failed");
855      return;
856    }
857  
1349  SSL_set_fd(fd->ssl, fd->fd);
1350
858    if (!EmptyString(conf->cipher_list))
859 <    SSL_set_cipher_list(client_p->localClient->fd.ssl, conf->cipher_list);
859 >    tls_set_ciphers(&client_p->connection->fd.ssl, conf->cipher_list);
860  
861    ssl_server_handshake(NULL, client_p);
862   }
863   #endif
864  
865   /* serv_connect_callback() - complete a server connection.
866 < *
866 > *
867   * This routine is called after the server connection attempt has
868   * completed. If unsucessful, an error is sent to ops and the client
869   * is closed. If sucessful, it goes through the initialisation/check
# Line 1366 | Line 873 | ssl_connect_init(struct Client *client_p
873   static void
874   serv_connect_callback(fde_t *fd, int status, void *data)
875   {
876 <  struct Client *client_p = data;
877 <  struct MaskItem *conf = NULL;
876 >  struct Client *const client_p = data;
877 >  const struct MaskItem *conf = NULL;
878  
879 <  /* First, make sure its a real client! */
880 <  assert(client_p != NULL);
881 <  assert(&client_p->localClient->fd == fd);
879 >  /* First, make sure it's a real client! */
880 >  assert(client_p);
881 >  assert(&client_p->connection->fd == fd);
882  
883    /* Next, for backward purposes, record the ip of the server */
884 <  memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
884 >  memcpy(&client_p->connection->ip, &fd->connect.hostaddr,
885           sizeof(struct irc_ssaddr));
886 +
887    /* Check the status */
888    if (status != COMM_OK)
889    {
890 <    /* We have an error, so report it and quit
891 <     * Admins get to see any IP, mere opers don't *sigh*
890 >    /* We have an error, so report it and quit */
891 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
892 >                         "Error connecting to %s: %s",
893 >                         get_client_name(client_p, SHOW_IP), comm_errstr(status));
894 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
895 >                         "Error connecting to %s: %s",
896 >                         get_client_name(client_p, MASK_IP), comm_errstr(status));
897 >
898 >    /*
899 >     * If a fd goes bad, call dead_link() the socket is no
900 >     * longer valid for reading or writing.
901       */
902 <     if (ConfigServerHide.hide_server_ips)
903 <       sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1387 <                            "Error connecting to %s: %s",
1388 <                            client_p->name, comm_errstr(status));
1389 <     else
1390 <       sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1391 <                            "Error connecting to %s[%s]: %s", client_p->name,
1392 <                            client_p->host, comm_errstr(status));
1393 <
1394 <     sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1395 <                          "Error connecting to %s: %s",
1396 <                          client_p->name, comm_errstr(status));
1397 <
1398 <     /* If a fd goes bad, call dead_link() the socket is no
1399 <      * longer valid for reading or writing.
1400 <      */
1401 <     dead_link_on_write(client_p, 0);
1402 <     return;
902 >    dead_link_on_write(client_p, 0);
903 >    return;
904    }
905  
906    /* COMM_OK, so continue the connection procedure */
907    /* Get the C/N lines */
908 <  conf = find_conf_name(&client_p->localClient->confs,
909 <                        client_p->name, CONF_SERVER);
908 >  conf = find_conf_name(&client_p->connection->confs,
909 >                        client_p->name, CONF_SERVER);
910    if (conf == NULL)
911    {
912 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
913 <                         "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
914 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
915 <                         "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
912 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
913 >                         "Lost connect{} block for %s", get_client_name(client_p, SHOW_IP));
914 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
915 >                         "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
916  
917 <    exit_client(client_p, &me, "Lost connect{} block");
917 >    exit_client(client_p, "Lost connect{} block");
918      return;
919    }
920  
921    /* Next, send the initial handshake */
922    SetHandshake(client_p);
923  
924 < #ifdef HAVE_LIBCRYPTO
924 > #ifdef HAVE_TLS
925    if (IsConfSSL(conf))
926    {
927      ssl_connect_init(client_p, conf, fd);
# Line 1428 | Line 929 | serv_connect_callback(fde_t *fd, int sta
929    }
930   #endif
931  
932 <  /* jdc -- Check and send spasswd, not passwd. */
1432 <  if (!EmptyString(conf->spasswd))
1433 <    sendto_one(client_p, "PASS %s TS %d %s",
1434 <               conf->spasswd, TS_CURRENT, me.id);
932 >  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
933  
934 <  send_capabilities(client_p, 0);
934 >  send_capabilities(client_p);
935  
936 <  sendto_one(client_p, "SERVER %s 1 :%s%s",
937 <             me.name, ConfigServerHide.hidden ? "(H) " : "",
1440 <             me.info);
936 >  sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
937 >             ConfigServerHide.hidden ? "(H) " : "", me.info);
938  
939 <  /* If we've been marked dead because a send failed, just exit
939 >  /*
940 >   * If we've been marked dead because a send failed, just exit
941     * here now and save everyone the trouble of us ever existing.
942     */
943 <  if (IsDead(client_p))
943 >  if (IsDead(client_p))
944    {
945 <      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
946 <                           "%s[%s] went dead during handshake",
947 <                           client_p->name,
948 <                           client_p->host);
949 <      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1452 <                           "%s went dead during handshake", client_p->name);
1453 <      return;
945 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
946 >                         "%s went dead during handshake", get_client_name(client_p, SHOW_IP));
947 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
948 >                         "%s went dead during handshake", get_client_name(client_p, MASK_IP));
949 >    return;
950    }
951  
952    /* don't move to serv_list yet -- we haven't sent a burst! */
# Line 1472 | Line 968 | find_servconn_in_progress(const char *na
968        if (!match(name, cptr->name))
969          return cptr;
970    }
971 <  
971 >
972    return NULL;
973   }

Diff Legend

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