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 3137 by michael, Tue Mar 11 19:01:43 2014 UTC vs.
ircd-hybrid/trunk/src/server.c (file contents), Revision 8512 by michael, Fri Apr 6 19:28:05 2018 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 1997-2014 ircd-hybrid development team
4 > *  Copyright (c) 1997-2018 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 15 | 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  
22 < /*! \file s_serv.c
22 > /*! \file server.c
23   * \brief Server related functions.
24   * \version $Id$
25   */
26  
27   #include "stdinc.h"
28 #ifdef HAVE_LIBCRYPTO
29 #include <openssl/rsa.h>
30 #include "rsa.h"
31 #endif
28   #include "list.h"
33 #include "channel.h"
34 #include "channel_mode.h"
29   #include "client.h"
30   #include "event.h"
37 #include "fdlist.h"
31   #include "hash.h"
32   #include "irc_string.h"
33   #include "ircd.h"
34   #include "ircd_defs.h"
35   #include "s_bsd.h"
43 #include "numeric.h"
36   #include "packet.h"
45 #include "irc_res.h"
37   #include "conf.h"
38 < #include "s_serv.h"
38 > #include "server.h"
39 > #include "server_capab.h"
40   #include "log.h"
49 #include "s_misc.h"
50 #include "s_user.h"
41   #include "send.h"
42   #include "memory.h"
53 #include "channel.h"
43   #include "parse.h"
44  
56 #define MIN_CONN_FREQ 300
45  
46   dlink_list flatten_links;
47 < static dlink_list cap_list = { NULL, NULL, 0 };
60 < static void server_burst(struct Client *);
61 < static void burst_all(struct Client *);
62 < static void send_tb(struct Client *client_p, struct Channel *chptr);
47 > static void server_connect_callback(fde_t *, int, void *);
48  
64 static CNCB serv_connect_callback;
65
66 static void burst_members(struct Client *, struct Channel *);
49  
50   /*
51   * write_links_file
# Line 74 | Line 56 | static void burst_members(struct Client
56   *                but in no particular order.
57   */
58   void
59 < write_links_file(void *notused)
59 > write_links_file(void *unused)
60   {
61    FILE *file = NULL;
62 <  dlink_node *ptr = NULL, *ptr_next = NULL;
63 <  char buff[IRCD_BUFSIZE] = { '\0' };
62 >  dlink_node *node, *node_next;
63 >  char buff[IRCD_BUFSIZE] = "";
64 >
65 >  if (EmptyString(ConfigServerHide.flatten_links_file))
66 >    return;
67  
68 <  if ((file = fopen(LIPATH, "w")) == NULL)
68 >  if ((file = fopen(ConfigServerHide.flatten_links_file, "w")) == NULL)
69 >  {
70 >    ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
71 >         strerror(errno));
72      return;
73 +  }
74  
75 <  DLINK_FOREACH_SAFE(ptr, ptr_next, flatten_links.head)
75 >  DLINK_FOREACH_SAFE(node, node_next, flatten_links.head)
76    {
77 <    dlinkDelete(ptr, &flatten_links);
78 <    MyFree(ptr->data);
79 <    free_dlink_node(ptr);
77 >    dlinkDelete(node, &flatten_links);
78 >    xfree(node->data);
79 >    free_dlink_node(node);
80    }
81  
82 <  DLINK_FOREACH(ptr, global_serv_list.head)
82 >  DLINK_FOREACH(node, global_server_list.head)
83    {
84 <    const struct Client *target_p = ptr->data;
84 >    const struct Client *target_p = node->data;
85  
86      /*
87       * Skip hidden servers, aswell as ourselves, since we already send
# Line 127 | Line 116 | read_links_file(void)
116   {
117    FILE *file = NULL;
118    char *p = NULL;
119 <  char buff[IRCD_BUFSIZE] = { '\0' };
119 >  char buff[IRCD_BUFSIZE] = "";
120  
121 <  if ((file = fopen(LIPATH, "r")) == NULL)
121 >  if (EmptyString(ConfigServerHide.flatten_links_file))
122      return;
123  
124 +  if ((file = fopen(ConfigServerHide.flatten_links_file, "r")) == NULL)
125 +  {
126 +    ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
127 +         strerror(errno));
128 +    return;
129 +  }
130 +
131    while (fgets(buff, sizeof(buff), file))
132    {
133 <    if ((p = strchr(buff, '\n')) != NULL)
133 >    if ((p = strchr(buff, '\n')))
134        *p = '\0';
135  
136      dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
# Line 143 | Line 139 | read_links_file(void)
139    fclose(file);
140   }
141  
142 < /* hunt_server()
142 > /* server_hunt()
143   *      Do the basic thing in delivering the message (command)
144   *      across the relays to the specific server (server) for
145   *      actions.
# Line 162 | Line 158 | read_links_file(void)
158   *
159   *      returns: (see #defines)
160   */
161 < int
162 < hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
161 > const struct server_hunt *
162 > server_hunt(struct Client *source_p, const char *command,
163              const int server, const int parc, char *parv[])
164   {
165 <  struct Client *target_p = NULL;
166 <  struct Client *target_tmp = NULL;
167 <  dlink_node *ptr;
172 <  int wilds;
165 >  static struct server_hunt hunt;
166 >  struct server_hunt *const h = &hunt;
167 >  dlink_node *node;
168  
169    /* Assume it's me, if no server */
170    if (parc <= server || EmptyString(parv[server]))
171 <    return HUNTED_ISME;
171 >  {
172 >    h->target_p = &me;
173 >    h->ret = HUNTED_ISME;
174 >    return h;
175 >  }
176  
177 <  if (!strcmp(parv[server], me.id) || !match(parv[server], me.name))
178 <    return HUNTED_ISME;
177 >  if ((h->target_p = find_person(source_p, parv[server])) == NULL)
178 >    h->target_p = hash_find_server(parv[server]);
179  
180 <  /* These are to pickup matches that would cause the following
180 >  /*
181 >   * These are to pickup matches that would cause the following
182     * message to go in the wrong direction while doing quick fast
183     * non-matching lookups.
184     */
185 <  if (MyClient(source_p))
186 <    target_p = hash_find_client(parv[server]);
187 <  else
188 <    target_p = find_person(client_p, parv[server]);
189 <
190 <  if (target_p)
191 <    if (target_p->from == source_p->from && !MyConnect(target_p))
192 <      target_p = NULL;
185 >  if (h->target_p)
186 >    if (h->target_p->from == source_p->from && !MyConnect(h->target_p))
187 >      h->target_p = NULL;
188  
189 <  if (target_p == NULL && (target_p = hash_find_server(parv[server])))
195 <    if (target_p->from == source_p->from && !MyConnect(target_p))
196 <      target_p = NULL;
197 <
198 <  wilds = has_wildcards(parv[server]);
199 <
200 <  /* Again, if there are no wild cards involved in the server
201 <   * name, use the hash lookup
202 <   */
203 <  if (target_p == NULL)
189 >  if (h->target_p == NULL && has_wildcards(parv[server]))
190    {
191 <    if (!wilds)
191 >    DLINK_FOREACH(node, global_server_list.head)
192      {
193 <      if (!(target_p = hash_find_server(parv[server])))
193 >      struct Client *tmp = node->data;
194 >
195 >      assert(IsMe(tmp) || IsServer(tmp));
196 >      if (match(parv[server], tmp->name) == 0)
197        {
198 <        sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
199 <        return HUNTED_NOSUCH;
198 >        if (tmp->from == source_p->from && !MyConnect(tmp))
199 >          continue;
200 >
201 >        h->target_p = tmp;
202 >        break;
203        }
204      }
205 <    else
205 >
206 >    if (h->target_p == NULL)
207      {
208 <      DLINK_FOREACH(ptr, global_client_list.head)
208 >      DLINK_FOREACH(node, global_client_list.head)
209        {
210 <        target_tmp = ptr->data;
210 >        struct Client *tmp = node->data;
211  
212 <        if (!match(parv[server], target_tmp->name))
212 >        assert(IsClient(tmp));
213 >        if (match(parv[server], tmp->name) == 0)
214          {
215 <          if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
215 >          if (tmp->from == source_p->from && !MyConnect(tmp))
216              continue;
223          target_p = ptr->data;
217  
218 <          if (IsRegistered(target_p) && (target_p != client_p))
219 <            break;
218 >          h->target_p = tmp;
219 >          break;
220          }
221        }
222      }
223    }
224  
225 <  if (target_p != NULL)
225 >  if (h->target_p)
226    {
227 <    if(!IsRegistered(target_p))
227 >    assert(IsMe(h->target_p) || IsServer(h->target_p) || IsClient(h->target_p));
228 >    if (IsMe(h->target_p) || MyClient(h->target_p))
229      {
230 <      sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
231 <      return HUNTED_NOSUCH;
230 >      h->ret = HUNTED_ISME;
231 >      return h;
232      }
233  
234 <    if (IsMe(target_p) || MyClient(target_p))
235 <      return HUNTED_ISME;
242 <
243 <    if (match(target_p->name, parv[server]))
244 <      parv[server] = target_p->name;
245 <
246 <    /* This is a little kludgy but should work... */
247 <    sendto_one(target_p, command, ID_or_name(source_p, target_p),
234 >    parv[server] = h->target_p->id;
235 >    sendto_one(h->target_p, command, source_p->id,
236                 parv[1], parv[2], parv[3], parv[4],
237                 parv[5], parv[6], parv[7], parv[8]);
238 <    return HUNTED_PASS;
238 >    h->ret = HUNTED_PASS;
239 >    return h;
240    }
241  
242    sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
243 <  return HUNTED_NOSUCH;
243 >  h->ret = HUNTED_NOSUCH;
244 >  return h;
245   }
246  
247   /* try_connections()
# Line 267 | Line 257 | hunt_server(struct Client *client_p, str
257   void
258   try_connections(void *unused)
259   {
260 <  dlink_node *ptr = NULL;
271 <  struct MaskItem *conf;
272 <  int confrq;
260 >  dlink_node *node;
261  
274  /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
262    if (GlobalSetOptions.autoconn == 0)
263      return;
264  
265 <  DLINK_FOREACH(ptr, server_items.head)
265 >  DLINK_FOREACH(node, connect_items.head)
266    {
267 <    conf = ptr->data;
267 >    struct MaskItem *conf = node->data;
268  
269      assert(conf->type == CONF_SERVER);
270 +    assert(conf->class);
271  
272 <    /* Also when already connecting! (update holdtimes) --SRB
273 <     */
286 <    if (!conf->port ||!IsConfAllowAutoConn(conf))
272 >    /* Also when already connecting! (update holdtimes) --SRB */
273 >    if (conf->port == 0 || !IsConfAllowAutoConn(conf))
274        continue;
275  
276 <
277 <    /* Skip this entry if the use of it is still on hold until
276 >    /*
277 >     * Skip this entry if the use of it is still on hold until
278       * future. Otherwise handle this entry (and set it on hold
279       * until next time). Will reset only hold times, if already
280       * made one successfull connection... [this algorithm is
# Line 296 | Line 283 | try_connections(void *unused)
283      if (conf->until > CurrentTime)
284        continue;
285  
286 <    if (conf->class == NULL)
300 <      confrq = DEFAULT_CONNECTFREQUENCY;
301 <    else
302 <    {
303 <      confrq = conf->class->con_freq;
304 <      if (confrq < MIN_CONN_FREQ)
305 <        confrq = MIN_CONN_FREQ;
306 <    }
307 <
308 <    conf->until = CurrentTime + confrq;
286 >    conf->until = CurrentTime + conf->class->con_freq;
287  
288 <    /* Found a CONNECT config with port specified, scan clients
288 >    /*
289 >     * Found a CONNECT config with port specified, scan clients
290       * and see if this server is already connected?
291       */
292 <    if (hash_find_server(conf->name) != NULL)
292 >    if (hash_find_server(conf->name))
293        continue;
294  
295      if (conf->class->ref_count < conf->class->max_total)
296      {
297 <      /* Go to the end of the list, if not already last */
298 <      if (ptr->next != NULL)
297 >      /* Move this entry to the end of the list, if not already last */
298 >      if (node->next)
299        {
300 <        dlinkDelete(ptr, &server_items);
301 <        dlinkAddTail(conf, &conf->node, &server_items);
300 >        dlinkDelete(node, &connect_items);
301 >        dlinkAddTail(conf, &conf->node, &connect_items);
302        }
303  
304        if (find_servconn_in_progress(conf->name))
305          return;
306  
307 <      /* We used to only print this if serv_connect() actually
307 >      /*
308 >       * We used to only print this if server_connect() actually
309         * succeeded, but since comm_tcp_connect() can call the callback
310         * immediately if there is an error, we were getting error messages
311         * in the wrong order. SO, we just print out the activated line,
312 <       * and let serv_connect() / serv_connect_callback() print an
312 >       * and let server_connect() / server_connect_callback() print an
313         * error afterwards if it fails.
314         *   -- adrian
315         */
316        if (ConfigServerHide.hide_server_ips)
317 <        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
317 >        sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
318                               "Connection to %s activated.",
319                               conf->name);
320        else
321 <        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
321 >        sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
322                               "Connection to %s[%s] activated.",
323                               conf->name, conf->host);
324  
325 <      serv_connect(conf, NULL);
325 >      server_connect(conf, NULL);
326        /* We connect only one at time... */
327        return;
328      }
# Line 350 | Line 330 | try_connections(void *unused)
330   }
331  
332   int
333 < valid_servname(const char *name)
333 > server_valid_name(const char *name)
334   {
335 <  unsigned int length = 0;
356 <  unsigned int dots   = 0;
335 >  unsigned int dots = 0;
336    const char *p = name;
337  
338    for (; *p; ++p)
# Line 361 | Line 340 | valid_servname(const char *name)
340      if (!IsServChar(*p))
341        return 0;
342  
364    ++length;
365
343      if (*p == '.')
344        ++dots;
345    }
346  
347 <  return dots != 0 && length <= HOSTLEN;
347 >  return dots && (p - name) <= HOSTLEN;
348   }
349  
350 < int
374 < check_server(const char *name, struct Client *client_p)
375 < {
376 <  dlink_node *ptr;
377 <  struct MaskItem *conf        = NULL;
378 <  struct MaskItem *server_conf = NULL;
379 <  int error = -1;
380 <
381 <  assert(client_p != NULL);
382 <
383 <  /* loop through looking for all possible connect items that might work */
384 <  DLINK_FOREACH(ptr, server_items.head)
385 <  {
386 <    conf = ptr->data;
387 <
388 <    if (match(name, conf->name))
389 <      continue;
390 <
391 <    error = -3;
392 <
393 <    /* XXX: Fix me for IPv6                    */
394 <    /* XXX sockhost is the IPv4 ip as a string */
395 <    if (!match(conf->host, client_p->host) ||
396 <        !match(conf->host, client_p->sockhost))
397 <    {
398 <      error = -2;
399 <
400 <      if (!match_conf_password(client_p->localClient->passwd, conf))
401 <        return -2;
402 <
403 <      if (!EmptyString(conf->certfp))
404 <        if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
405 <          return -4;
406 <
407 <      server_conf = conf;
408 <    }
409 <  }
410 <
411 <  if (server_conf == NULL)
412 <    return error;
413 <
414 <  attach_conf(client_p, server_conf);
415 <
416 <
417 <  if (server_conf != NULL)
418 <  {
419 <    struct sockaddr_in *v4;
420 < #ifdef IPV6
421 <    struct sockaddr_in6 *v6;
422 < #endif
423 <    switch (server_conf->aftype)
424 <    {
425 < #ifdef IPV6
426 <      case AF_INET6:
427 <        v6 = (struct sockaddr_in6 *)&server_conf->addr;
428 <
429 <        if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
430 <          memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
431 <        break;
432 < #endif
433 <      case AF_INET:
434 <        v4 = (struct sockaddr_in *)&server_conf->addr;
435 <
436 <        if (v4->sin_addr.s_addr == INADDR_NONE)
437 <          memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
438 <        break;
439 <    }
440 <  }
441 <
442 <  return 0;
443 < }
444 <
445 < /* add_capability()
446 < *
447 < * inputs       - string name of CAPAB
448 < *              - int flag of capability
449 < * output       - NONE
450 < * side effects - Adds given capability name and bit mask to
451 < *                current supported capabilities. This allows
452 < *                modules to dynamically add or subtract their capability.
453 < */
454 < void
455 < add_capability(const char *capab_name, int cap_flag, int add_to_default)
456 < {
457 <  struct Capability *cap = MyMalloc(sizeof(*cap));
458 <
459 <  cap->name = xstrdup(capab_name);
460 <  cap->cap = cap_flag;
461 <  dlinkAdd(cap, &cap->node, &cap_list);
462 <
463 <  if (add_to_default)
464 <    default_server_capabs |= cap_flag;
465 < }
466 <
467 < /* delete_capability()
468 < *
469 < * inputs       - string name of CAPAB
470 < * output       - NONE
471 < * side effects - delete given capability from ones known.
472 < */
473 < int
474 < delete_capability(const char *capab_name)
475 < {
476 <  dlink_node *ptr;
477 <  dlink_node *next_ptr;
478 <  struct Capability *cap;
479 <
480 <  DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
481 <  {
482 <    cap = ptr->data;
483 <
484 <    if (cap->cap != 0)
485 <    {
486 <      if (irccmp(cap->name, capab_name) == 0)
487 <      {
488 <        default_server_capabs &= ~(cap->cap);
489 <        dlinkDelete(ptr, &cap_list);
490 <        MyFree(cap->name);
491 <        MyFree(cap);
492 <      }
493 <    }
494 <  }
495 <
496 <  return 0;
497 < }
498 <
499 < /*
500 < * find_capability()
501 < *
502 < * inputs       - string name of capab to find
503 < * output       - 0 if not found CAPAB otherwise
504 < * side effects - none
505 < */
506 < unsigned int
507 < find_capability(const char *capab)
508 < {
509 <  const dlink_node *ptr = NULL;
510 <
511 <  DLINK_FOREACH(ptr, cap_list.head)
512 <  {
513 <    const struct Capability *cap = ptr->data;
514 <
515 <    if (cap->cap && !irccmp(cap->name, capab))
516 <      return cap->cap;
517 <  }
518 <
519 <  return 0;
520 < }
521 <
522 < /* send_capabilities()
523 < *
524 < * inputs       - Client pointer to send to
525 < *              - int flag of capabilities that this server can send
526 < * output       - NONE
527 < * side effects - send the CAPAB line to a server  -orabidoo
528 < *
529 < */
530 < void
531 < send_capabilities(struct Client *client_p, int cap_can_send)
532 < {
533 <  struct Capability *cap=NULL;
534 <  char msgbuf[IRCD_BUFSIZE];
535 <  char *t;
536 <  int tl;
537 <  dlink_node *ptr;
538 <
539 <  t = msgbuf;
540 <
541 <  DLINK_FOREACH(ptr, cap_list.head)
542 <  {
543 <    cap = ptr->data;
544 <
545 <    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 < }
555 <
556 < /* sendnick_TS()
557 < *
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 <  char ubuf[IRCD_BUFSIZE];
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';
577 <  }
578 <
579 <  if (IsCapable(client_p, CAP_SVS))
580 <    sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s %s :%s",
581 <               target_p->servptr->id,
582 <               target_p->name, target_p->hopcount + 1,
583 <               (unsigned long) target_p->tsinfo,
584 <               ubuf, target_p->username, target_p->host,
585 <               (MyClient(target_p) && IsIPSpoof(target_p)) ?
586 <               "0" : target_p->sockhost, target_p->id,
587 <               target_p->svid, target_p->info);
588 <  else
589 <    sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
590 <               target_p->servptr->id,
591 <               target_p->name, target_p->hopcount + 1,
592 <               (unsigned long) target_p->tsinfo,
593 <               ubuf, target_p->username, target_p->host,
594 <               (MyClient(target_p) && IsIPSpoof(target_p)) ?
595 <               "0" : target_p->sockhost, target_p->id, target_p->info);
596 <
597 <  if (!EmptyString(target_p->certfp))
598 <    sendto_one(client_p, ":%s CERTFP %s",
599 <               ID_or_name(target_p, client_p), target_p->certfp);
600 <
601 <  if (target_p->away[0])
602 <    sendto_one(client_p, ":%s AWAY :%s", ID_or_name(target_p, client_p),
603 <               target_p->away);
604 <
605 < }
606 <
607 < /*
608 < * show_capabilities - show current server capabilities
609 < *
610 < * inputs       - pointer to a struct Client
611 < * output       - pointer to static string
612 < * side effects - build up string representing capabilities of server listed
613 < */
614 < const char *
615 < show_capabilities(const struct Client *target_p)
616 < {
617 <  static char msgbuf[IRCD_BUFSIZE] = "";
618 <  const dlink_node *ptr = NULL;
619 <
620 <  strlcpy(msgbuf, "TS", sizeof(msgbuf));
621 <
622 <  DLINK_FOREACH(ptr, cap_list.head)
623 <  {
624 <    const struct Capability *cap = ptr->data;
625 <
626 <    if (!IsCapable(target_p, cap->cap))
627 <      continue;
628 <
629 <    strlcat(msgbuf,       " ", sizeof(msgbuf));
630 <    strlcat(msgbuf, cap->name, sizeof(msgbuf));
631 <  }
632 <
633 <  return msgbuf;
634 < }
635 <
636 < /* make_server()
350 > /* server_make()
351   *
352   * inputs       - pointer to client struct
353   * output       - pointer to struct Server
# Line 641 | Line 355 | show_capabilities(const struct Client *t
355   *                if it was not previously allocated.
356   */
357   struct Server *
358 < make_server(struct Client *client_p)
358 > server_make(struct Client *client_p)
359   {
360    if (client_p->serv == NULL)
361 <    client_p->serv = MyMalloc(sizeof(struct Server));
361 >    client_p->serv = xcalloc(sizeof(*client_p->serv));
362  
363    return client_p->serv;
364   }
365  
366 < /* server_estab()
653 < *
654 < * inputs       - pointer to a struct Client
655 < * output       -
656 < * side effects -
657 < */
658 < void
659 < server_estab(struct Client *client_p)
660 < {
661 <  struct MaskItem *conf = NULL;
662 <  char *host;
663 <  const char *inpath;
664 <  static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
665 <  dlink_node *ptr;
666 < #ifdef HAVE_LIBCRYPTO
667 <  const COMP_METHOD *compression = NULL, *expansion = NULL;
668 < #endif
669 <
670 <  assert(client_p != NULL);
671 <
672 <  strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
673 <
674 <  inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
675 <  host   = client_p->name;
676 <
677 <  if ((conf = find_conf_name(&client_p->localClient->confs, host, CONF_SERVER))
678 <      == NULL)
679 <  {
680 <    /* This shouldn't happen, better tell the ops... -A1kmm */
681 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
682 <                         "Warning: Lost connect{} block "
683 <                         "for server %s(this shouldn't happen)!", host);
684 <    exit_client(client_p, &me, "Lost connect{} block!");
685 <    return;
686 <  }
687 <
688 <  MyFree(client_p->localClient->passwd);
689 <  client_p->localClient->passwd = NULL;
690 <
691 <  /* Its got identd, since its a server */
692 <  SetGotId(client_p);
693 <
694 <  /* If there is something in the serv_list, it might be this
695 <   * connecting server..
696 <   */
697 <  if (!ServerInfo.hub && serv_list.head)
698 <  {
699 <    if (client_p != serv_list.head->data || serv_list.head->next)
700 <    {
701 <      ++ServerStats.is_ref;
702 <      sendto_one(client_p, "ERROR :I'm a leaf not a hub");
703 <      exit_client(client_p, &me, "I'm a leaf");
704 <      return;
705 <    }
706 <  }
707 <
708 <  if (IsUnknown(client_p))
709 <  {
710 <    sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
711 <
712 <    send_capabilities(client_p, 0);
713 <
714 <    sendto_one(client_p, "SERVER %s 1 :%s%s",
715 <               me.name, ConfigServerHide.hidden ? "(H) " : "", me.info);
716 <  }
717 <
718 <  sendto_one(client_p, "SVINFO %d %d 0 :%lu", TS_CURRENT, TS_MIN,
719 <             (unsigned long)CurrentTime);
720 <
721 <  if (HasID(client_p))
722 <    hash_add_id(client_p);
723 <
724 <  /* XXX Does this ever happen? I don't think so -db */
725 <  detach_conf(client_p, CONF_OPER);
726 <
727 <  /* *WARNING*
728 <  **    In the following code in place of plain server's
729 <  **    name we send what is returned by get_client_name
730 <  **    which may add the "sockhost" after the name. It's
731 <  **    *very* *important* that there is a SPACE between
732 <  **    the name and sockhost (if present). The receiving
733 <  **    server will start the information field from this
734 <  **    first blank and thus puts the sockhost into info.
735 <  **    ...a bit tricky, but you have been warned, besides
736 <  **    code is more neat this way...  --msa
737 <  */
738 <  client_p->servptr = &me;
739 <
740 <  if (IsClosing(client_p))
741 <    return;
742 <
743 <  SetServer(client_p);
744 <
745 <  /* Update the capability combination usage counts. -A1kmm */
746 <  set_chcap_usage_counts(client_p);
747 <
748 <  /* Some day, all these lists will be consolidated *sigh* */
749 <  dlinkAdd(client_p, &client_p->lnode, &me.serv->server_list);
750 <
751 <  assert(dlinkFind(&unknown_list, client_p));
752 <
753 <  dlink_move_node(&client_p->localClient->lclient_node,
754 <                  &unknown_list, &serv_list);
755 <
756 <  Count.myserver++;
757 <
758 <  dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
759 <  hash_add_client(client_p);
760 <
761 <  /* doesnt duplicate client_p->serv if allocated this struct already */
762 <  make_server(client_p);
763 <
764 <  /* fixing eob timings.. -gnp */
765 <  client_p->localClient->firsttime = CurrentTime;
766 <
767 <  if (find_matching_name_conf(CONF_SERVICE, client_p->name, NULL, NULL, 0))
768 <    AddFlag(client_p, FLAGS_SERVICE);
769 <
770 <  /* Show the real host/IP to admins */
771 < #ifdef HAVE_LIBCRYPTO
772 <  if (client_p->localClient->fd.ssl)
773 <  {
774 <    compression = SSL_get_current_compression(client_p->localClient->fd.ssl);
775 <    expansion   = SSL_get_current_expansion(client_p->localClient->fd.ssl);
776 <
777 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
778 <                         "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
779 <                         inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
780 <                         compression ? SSL_COMP_get_name(compression) : "NONE",
781 <                         expansion ? SSL_COMP_get_name(expansion) : "NONE",
782 <                         show_capabilities(client_p));
783 <    /* Now show the masked hostname/IP to opers */
784 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
785 <                         "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
786 <                         inpath, ssl_get_cipher(client_p->localClient->fd.ssl),
787 <                         compression ? SSL_COMP_get_name(compression) : "NONE",
788 <                         expansion ? SSL_COMP_get_name(expansion) : "NONE",
789 <                         show_capabilities(client_p));
790 <    ilog(LOG_TYPE_IRCD, "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
791 <         inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
792 <         compression ? SSL_COMP_get_name(compression) : "NONE",
793 <         expansion ? SSL_COMP_get_name(expansion) : "NONE",
794 <         show_capabilities(client_p));
795 <  }
796 <  else
797 < #endif
798 <  {
799 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
800 <                         "Link with %s established: (Capabilities: %s)",
801 <                         inpath_ip, show_capabilities(client_p));
802 <    /* Now show the masked hostname/IP to opers */
803 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
804 <                         "Link with %s established: (Capabilities: %s)",
805 <                         inpath, show_capabilities(client_p));
806 <    ilog(LOG_TYPE_IRCD, "Link with %s established: (Capabilities: %s)",
807 <         inpath_ip, show_capabilities(client_p));
808 <  }
809 <
810 <  fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
811 <
812 <  sendto_server(client_p, NOCAPS, NOCAPS, ":%s SID %s 2 %s :%s%s",
813 <                me.id, client_p->name, client_p->id,
814 <                IsHidden(client_p) ? "(H) " : "", client_p->info);
815 <
816 <  /*
817 <   * Pass on my client information to the new server
818 <   *
819 <   * First, pass only servers (idea is that if the link gets
820 <   * cancelled beacause the server was already there,
821 <   * there are no NICK's to be cancelled...). Of course,
822 <   * if cancellation occurs, all this info is sent anyway,
823 <   * and I guess the link dies when a read is attempted...? --msa
824 <   *
825 <   * Note: Link cancellation to occur at this point means
826 <   * that at least two servers from my fragment are building
827 <   * up connection this other fragment at the same time, it's
828 <   * a race condition, not the normal way of operation...
829 <   *
830 <   * ALSO NOTE: using the get_client_name for server names--
831 <   *    see previous *WARNING*!!! (Also, original inpath
832 <   *    is destroyed...)
833 <   */
834 <
835 <  DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
836 <  {
837 <    struct Client *target_p = ptr->data;
838 <
839 <    /* target_p->from == target_p for target_p == client_p */
840 <    if (IsMe(target_p) || target_p->from == client_p)
841 <      continue;
842 <
843 <    sendto_one(client_p, ":%s SID %s %d %s :%s%s",
844 <               ID(target_p->servptr), target_p->name, target_p->hopcount+1,
845 <               target_p->id, IsHidden(target_p) ? "(H) " : "",
846 <               target_p->info);
847 <
848 <    if (HasFlag(target_p, FLAGS_EOB))
849 <      sendto_one(client_p, ":%s EOB", ID_or_name(target_p, client_p));
850 <  }
851 <
852 <  server_burst(client_p);
853 < }
854 <
855 < /* server_burst()
856 < *
857 < * inputs       - struct Client pointer server
858 < *              -
859 < * output       - none
860 < * side effects - send a server burst
861 < * bugs         - still too long
862 < */
863 < static void
864 < server_burst(struct Client *client_p)
865 < {
866 <  /* Send it in the shortened format with the TS, if
867 <  ** it's a TS server; walk the list of channels, sending
868 <  ** all the nicks that haven't been sent yet for each
869 <  ** channel, then send the channel itself -- it's less
870 <  ** obvious than sending all nicks first, but on the
871 <  ** receiving side memory will be allocated more nicely
872 <  ** saving a few seconds in the handling of a split
873 <  ** -orabidoo
874 <  */
875 <
876 <  burst_all(client_p);
877 <
878 <  /* EOB stuff is now in burst_all */
879 <  /* Always send a PING after connect burst is done */
880 <  sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
881 < }
882 <
883 < /* burst_all()
884 < *
885 < * inputs       - pointer to server to send burst to
886 < * output       - NONE
887 < * side effects - complete burst of channels/nicks is sent to client_p
888 < */
889 < static void
890 < burst_all(struct Client *client_p)
891 < {
892 <  dlink_node *ptr = NULL;
893 <
894 <  DLINK_FOREACH(ptr, global_channel_list.head)
895 <  {
896 <    struct Channel *chptr = ptr->data;
897 <
898 <    if (dlink_list_length(&chptr->members) != 0)
899 <    {
900 <      burst_members(client_p, chptr);
901 <      send_channel_modes(client_p, chptr);
902 <
903 <      if (IsCapable(client_p, CAP_TBURST))
904 <        send_tb(client_p, chptr);
905 <    }
906 <  }
907 <
908 <  /* also send out those that are not on any channel
909 <   */
910 <  DLINK_FOREACH(ptr, global_client_list.head)
911 <  {
912 <    struct Client *target_p = ptr->data;
913 <
914 <    if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
915 <      sendnick_TS(client_p, target_p);
916 <
917 <    DelFlag(target_p, FLAGS_BURSTED);
918 <  }
919 <
920 <  if (IsCapable(client_p, CAP_EOB))
921 <    sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
922 < }
923 <
924 < /*
925 < * send_tb
926 < *
927 < * inputs       - pointer to Client
928 < *              - pointer to channel
929 < * output       - NONE
930 < * side effects - Called on a server burst when
931 < *                server is CAP_TBURST capable
932 < */
933 < static void
934 < send_tb(struct Client *client_p, struct Channel *chptr)
935 < {
936 <  /*
937 <   * We may also send an empty topic here, but only if topic_time isn't 0,
938 <   * i.e. if we had a topic that got unset.  This is required for syncing
939 <   * topics properly.
940 <   *
941 <   * Imagine the following scenario: Our downlink introduces a channel
942 <   * to us with a TS that is equal to ours, but the channel topic on
943 <   * their side got unset while the servers were in splitmode, which means
944 <   * their 'topic' is newer.  They simply wanted to unset it, so we have to
945 <   * deal with it in a more sophisticated fashion instead of just resetting
946 <   * it to their old topic they had before.  Read m_tburst.c:ms_tburst
947 <   * for further information   -Michael
948 <   */
949 <  if (chptr->topic_time != 0)
950 <    sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
951 <               ID_or_name(&me, client_p),
952 <               (unsigned long)chptr->channelts, chptr->chname,
953 <               (unsigned long)chptr->topic_time,
954 <               chptr->topic_info,
955 <               chptr->topic);
956 < }
957 <
958 < /* burst_members()
959 < *
960 < * inputs       - pointer to server to send members to
961 < *              - dlink_list pointer to membership list to send
962 < * output       - NONE
963 < * side effects -
964 < */
965 < static void
966 < burst_members(struct Client *client_p, struct Channel *chptr)
967 < {
968 <  struct Client *target_p;
969 <  struct Membership *ms;
970 <  dlink_node *ptr;
971 <
972 <  DLINK_FOREACH(ptr, chptr->members.head)
973 <  {
974 <    ms       = ptr->data;
975 <    target_p = ms->client_p;
976 <
977 <    if (!HasFlag(target_p, FLAGS_BURSTED))
978 <    {
979 <      AddFlag(target_p, FLAGS_BURSTED);
980 <
981 <      if (target_p->from != client_p)
982 <        sendnick_TS(client_p, target_p);
983 <    }
984 <  }
985 < }
986 <
987 < /* New server connection code
988 < * Based upon the stuff floating about in s_bsd.c
989 < *   -- adrian
990 < */
991 <
992 < /* serv_connect() - initiate a server connection
366 > /* server_connect() - initiate a server connection
367   *
368   * inputs       - pointer to conf
369   *              - pointer to client doing the connect
# Line 1006 | Line 380 | burst_members(struct Client *client_p, s
380   * it suceeded or not, and 0 if it fails in here somewhere.
381   */
382   int
383 < serv_connect(struct MaskItem *conf, struct Client *by)
383 > server_connect(struct MaskItem *conf, struct Client *by)
384   {
385 <  struct Client *client_p;
1012 <  char buf[HOSTIPLEN + 1];
385 >  char buf[HOSTIPLEN + 1] = "";
386  
387    /* conversion structs */
388    struct sockaddr_in *v4;
1016  /* Make sure conf is useful */
1017  assert(conf != NULL);
389  
390 +  /* Make sure conf is useful */
391 +  assert(conf);
392 +  assert(conf->type == CONF_SERVER);
393 +  assert(hash_find_server(conf->name) == NULL);  /* This should have been checked by the caller */
394  
395 <  getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
395 >  getnameinfo((const struct sockaddr *)&conf->addr, conf->addr.ss_len,
396                buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
397 <  ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
1023 <       buf);
397 >  ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host, buf);
398  
399    /* Still processing a DNS lookup? -> exit */
400    if (conf->dns_pending)
401    {
402 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
402 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
403                           "Error connecting to %s: DNS lookup for connect{} in progress.",
404                           conf->name);
405 <    return (0);
405 >    return 0;
406    }
407  
408    if (conf->dns_failed)
409    {
410 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
410 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
411                           "Error connecting to %s: DNS lookup for connect{} failed.",
412                           conf->name);
413 <    return (0);
413 >    return 0;
414    }
415  
416 <  /* Make sure this server isn't already connected
417 <   * Note: conf should ALWAYS be a valid C: line
418 <   */
1045 <  if ((client_p = hash_find_server(conf->name)) != NULL)
416 >  /* Create a socket for the server connection */
417 >  int fd = comm_socket(conf->addr.ss.ss_family, SOCK_STREAM, 0);
418 >  if (fd == -1)
419    {
420 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
421 <                         "Server %s already present from %s",
1049 <                         conf->name, get_client_name(client_p, SHOW_IP));
1050 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1051 <                         "Server %s already present from %s",
1052 <                         conf->name, get_client_name(client_p, MASK_IP));
1053 <    if (by && IsClient(by) && !MyClient(by))
1054 <      sendto_one_notice(by, &me, ":Server %s already present from %s",
1055 <                        conf->name, get_client_name(client_p, MASK_IP));
420 >    /* Eek, failure to create the socket */
421 >    report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
422      return 0;
423    }
424  
425    /* Create a local client */
426 <  client_p = make_client(NULL);
426 >  struct Client *client_p = client_make(NULL);
427  
428    /* Copy in the server, hostname, fd */
429    strlcpy(client_p->name, conf->name, sizeof(client_p->name));
# Line 1066 | Line 432 | serv_connect(struct MaskItem *conf, stru
432    /* We already converted the ip once, so lets use it - stu */
433    strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
434  
435 <  /* create a socket for the server connection */
1070 <  if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family,
1071 <                SOCK_STREAM, 0, NULL) < 0)
1072 <  {
1073 <    /* Eek, failure to create the socket */
1074 <    report_error(L_ALL, "opening stream socket to %s: %s",
1075 <                 conf->name, errno);
1076 <    SetDead(client_p);
1077 <    exit_client(client_p, &me, "Connection failed");
1078 <    return 0;
1079 <  }
435 >  client_p->connection->fd = fd_open(fd, 1, NULL);
436  
437 <  /* servernames are always guaranteed under HOSTLEN chars */
438 <  fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
437 >  /* Server names are always guaranteed under HOSTLEN chars */
438 >  fd_note(client_p->connection->fd, "Server: %s", client_p->name);
439  
440 <  /* Attach config entries to client here rather than in
441 <   * serv_connect_callback(). This to avoid null pointer references.
440 >  /*
441 >   * Attach config entries to client here rather than in server_connect_callback().
442 >   * This to avoid null pointer references.
443     */
444 <  if (!attach_connect_block(client_p, conf->name, conf->host))
1088 <  {
1089 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1090 <                         "Host %s is not enabled for connecting: no connect{} block",
1091 <                         conf->name);
1092 <    if (by && IsClient(by) && !MyClient(by))
1093 <      sendto_one_notice(by, &me, ":Connect to host %s failed.", client_p->name);
1094 <
1095 <    SetDead(client_p);
1096 <    exit_client(client_p, client_p, "Connection failed");
1097 <    return 0;
1098 <  }
444 >  conf_attach(client_p, conf);
445  
446 <  /* at this point we have a connection in progress and C/N lines
1101 <   * attached to the client, the socket info should be saved in the
1102 <   * client and it should either be resolved or have a valid address.
1103 <   *
1104 <   * The socket has been connected or connect is in progress.
1105 <   */
1106 <  make_server(client_p);
446 >  server_make(client_p);
447  
448    if (by && IsClient(by))
449      strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
# Line 1111 | Line 451 | serv_connect(struct MaskItem *conf, stru
451      strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
452  
453    SetConnecting(client_p);
454 <  dlinkAdd(client_p, &client_p->node, &global_client_list);
1115 <  /* from def_fam */
1116 <  client_p->localClient->aftype = conf->aftype;
454 >  client_p->ip.ss.ss_family = conf->aftype;
455  
456    /* Now, initiate the connection */
457    /* XXX assume that a non 0 type means a specific bind address
# Line 1123 | Line 461 | serv_connect(struct MaskItem *conf, stru
461    {
462      case AF_INET:
463        v4 = (struct sockaddr_in*)&conf->bind;
464 <      if (v4->sin_addr.s_addr != 0)
464 >
465 >      if (v4->sin_addr.s_addr)
466        {
467          struct irc_ssaddr ipn;
468 <        memset(&ipn, 0, sizeof(struct irc_ssaddr));
468 >
469 >        memset(&ipn, 0, sizeof(ipn));
470          ipn.ss.ss_family = AF_INET;
471          ipn.ss_port = 0;
472 <        memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
473 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
472 >        memcpy(&ipn, &conf->bind, sizeof(ipn));
473 >
474 >        comm_connect_tcp(client_p->connection->fd, conf->host, conf->port,
475                           (struct sockaddr *)&ipn, ipn.ss_len,
476 <                         serv_connect_callback, client_p, conf->aftype,
476 >                         server_connect_callback, client_p, conf->aftype,
477                           CONNECTTIMEOUT);
478        }
479 <      else if (ServerInfo.specific_ipv4_vhost)
479 >      else if (ConfigServerInfo.specific_ipv4_vhost)
480        {
481          struct irc_ssaddr ipn;
482 <        memset(&ipn, 0, sizeof(struct irc_ssaddr));
482 >
483 >        memset(&ipn, 0, sizeof(ipn));
484          ipn.ss.ss_family = AF_INET;
485          ipn.ss_port = 0;
486 <        memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
487 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
486 >        memcpy(&ipn, &ConfigServerInfo.ip, sizeof(ipn));
487 >
488 >        comm_connect_tcp(client_p->connection->fd, conf->host, conf->port,
489                           (struct sockaddr *)&ipn, ipn.ss_len,
490 <                         serv_connect_callback, client_p, conf->aftype,
490 >                         server_connect_callback, client_p, conf->aftype,
491                           CONNECTTIMEOUT);
492        }
493        else
494 <        comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
495 <                         NULL, 0, serv_connect_callback, client_p, conf->aftype,
494 >        comm_connect_tcp(client_p->connection->fd, conf->host, conf->port,
495 >                         NULL, 0, server_connect_callback, client_p, conf->aftype,
496                           CONNECTTIMEOUT);
497        break;
1155 #ifdef IPV6
498      case AF_INET6:
499        {
500          struct irc_ssaddr ipn;
501          struct sockaddr_in6 *v6;
502          struct sockaddr_in6 *v6conf;
503  
504 <        memset(&ipn, 0, sizeof(struct irc_ssaddr));
504 >        memset(&ipn, 0, sizeof(ipn));
505          v6conf = (struct sockaddr_in6 *)&conf->bind;
506          v6 = (struct sockaddr_in6 *)&ipn;
507  
508 <        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)) != 0)
508 >        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
509          {
510 <          memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
510 >          memcpy(&ipn, &conf->bind, sizeof(ipn));
511            ipn.ss.ss_family = AF_INET6;
512            ipn.ss_port = 0;
513 <          comm_connect_tcp(&client_p->localClient->fd,
513 >
514 >          comm_connect_tcp(client_p->connection->fd,
515                             conf->host, conf->port,
516                             (struct sockaddr *)&ipn, ipn.ss_len,
517 <                           serv_connect_callback, client_p,
517 >                           server_connect_callback, client_p,
518                             conf->aftype, CONNECTTIMEOUT);
519          }
520 <        else if (ServerInfo.specific_ipv6_vhost)
520 >        else if (ConfigServerInfo.specific_ipv6_vhost)
521          {
522 <          memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
522 >          memcpy(&ipn, &ConfigServerInfo.ip6, sizeof(ipn));
523            ipn.ss.ss_family = AF_INET6;
524            ipn.ss_port = 0;
525 <          comm_connect_tcp(&client_p->localClient->fd,
525 >
526 >          comm_connect_tcp(client_p->connection->fd,
527                             conf->host, conf->port,
528                             (struct sockaddr *)&ipn, ipn.ss_len,
529 <                           serv_connect_callback, client_p,
529 >                           server_connect_callback, client_p,
530                             conf->aftype, CONNECTTIMEOUT);
531          }
532          else
533 <          comm_connect_tcp(&client_p->localClient->fd,
533 >          comm_connect_tcp(client_p->connection->fd,
534                             conf->host, conf->port,
535 <                           NULL, 0, serv_connect_callback, client_p,
535 >                           NULL, 0, server_connect_callback, client_p,
536                             conf->aftype, CONNECTTIMEOUT);
537        }
1194 #endif
538    }
539 +
540 +  /*
541 +   * At this point we have a connection in progress and a connect {} block
542 +   * attached to the client, the socket info should be saved in the client
543 +   * and it should either be resolved or have a valid address.
544 +   *
545 +   * The socket has been connected or connect is in progress.
546 +   */
547    return 1;
548   }
549  
1199 #ifdef HAVE_LIBCRYPTO
550   static void
551 < finish_ssl_server_handshake(struct Client *client_p)
551 > server_finish_tls_handshake(struct Client *client_p)
552   {
553 <  struct MaskItem *conf = NULL;
553 >  const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
554 >                                                client_p->name, CONF_SERVER);
555 >  if (!conf)
556 >  {
557 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
558 >                         "Lost connect{} block for %s", client_get_name(client_p, SHOW_IP));
559 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
560 >                         "Lost connect{} block for %s", client_get_name(client_p, MASK_IP));
561  
562 <  conf = find_conf_name(&client_p->localClient->confs,
1206 <                        client_p->name, CONF_SERVER);
1207 <  if (conf == NULL)
1208 <  {
1209 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1210 <                         "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
1211 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1212 <                         "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1213 <
1214 <    exit_client(client_p, &me, "Lost connect{} block");
562 >    exit_client(client_p, "Lost connect{} block");
563      return;
564    }
565  
566 <  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
566 >  sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
567  
568 <  send_capabilities(client_p, 0);
568 >  sendto_one(client_p, "CAPAB :%s", capab_get(NULL));
569  
570    sendto_one(client_p, "SERVER %s 1 :%s%s",
571               me.name, ConfigServerHide.hidden ? "(H) " : "",
572               me.info);
573  
1226  /* If we've been marked dead because a send failed, just exit
1227   * here now and save everyone the trouble of us ever existing.
1228   */
1229  if (IsDead(client_p))
1230  {
1231      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1232                           "%s[%s] went dead during handshake",
1233                           client_p->name,
1234                           client_p->host);
1235      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1236                           "%s went dead during handshake", client_p->name);
1237      return;
1238  }
1239
1240  /* don't move to serv_list yet -- we haven't sent a burst! */
574    /* If we get here, we're ok, so lets start reading some data */
575 <  comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, read_packet, client_p, 0);
575 >  read_packet(client_p->connection->fd, client_p);
576   }
577  
578   static void
579 < ssl_server_handshake(fde_t *fd, struct Client *client_p)
579 > server_tls_handshake(fde_t *F, void *data)
580   {
581 <  X509 *cert = NULL;
582 <  int ret = 0;
581 >  struct Client *client_p = data;
582 >  const char *sslerr = NULL;
583 >
584 >  assert(client_p);
585 >  assert(client_p->connection);
586 >  assert(client_p->connection->fd);
587 >  assert(client_p->connection->fd == F);
588  
589 <  if ((ret = SSL_connect(client_p->localClient->fd.ssl)) <= 0)
589 >  tls_handshake_status_t ret = tls_handshake(&F->ssl, TLS_ROLE_CLIENT, &sslerr);
590 >  if (ret != TLS_HANDSHAKE_DONE)
591    {
592 <    switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
592 >    if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT)
593 >    {
594 >      exit_client(client_p, "Timeout during TLS handshake");
595 >      return;
596 >    }
597 >
598 >    switch (ret)
599      {
600 <      case SSL_ERROR_WANT_WRITE:
601 <        comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
602 <                       (PF *)ssl_server_handshake, client_p, 0);
600 >      case TLS_HANDSHAKE_WANT_WRITE:
601 >        comm_setselect(F, COMM_SELECT_WRITE,
602 >                       server_tls_handshake, client_p, CONNECTTIMEOUT);
603          return;
604 <      case SSL_ERROR_WANT_READ:
605 <        comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
606 <                       (PF *)ssl_server_handshake, client_p, 0);
604 >      case TLS_HANDSHAKE_WANT_READ:
605 >        comm_setselect(F, COMM_SELECT_READ,
606 >                       server_tls_handshake, client_p, CONNECTTIMEOUT);
607          return;
608        default:
609        {
610 <        const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
1266 <        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
610 >        sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
611                               "Error connecting to %s: %s", client_p->name,
612 <                             sslerr ? sslerr : "unknown SSL error");
613 <        exit_client(client_p, client_p, "Error during SSL handshake");
612 >                             sslerr ? sslerr : "unknown TLS error");
613 >        exit_client(client_p, "Error during TLS handshake");
614          return;
615        }
616      }
617    }
618  
619 <  if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
1276 <  {
1277 <    int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
1278 <    char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
1279 <    unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
1280 <
1281 <    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
1282 <        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
1283 <        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1284 <    {
1285 <      unsigned int i = 0, n = 0;
619 >  comm_settimeout(F, 0, NULL, NULL);
620  
621 <      if (X509_digest(cert, EVP_sha256(), md, &n))
622 <      {
623 <        for (; i < n; ++i)
1290 <          snprintf(buf + 2 * i, 3, "%02X", md[i]);
1291 <        client_p->certfp = xstrdup(buf);
1292 <      }
1293 <    }
1294 <    else
1295 <      ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad SSL client certificate: %d",
1296 <           client_p->name, client_p->username, client_p->host, res);
1297 <    X509_free(cert);
1298 <  }
621 >  if (!tls_verify_cert(&F->ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp))
622 >    ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad TLS client certificate",
623 >         client_p->name, client_p->username, client_p->host);
624  
625 <  finish_ssl_server_handshake(client_p);
625 >  server_finish_tls_handshake(client_p);
626   }
627  
628   static void
629 < ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd)
629 > server_tls_connect_init(struct Client *client_p, const struct MaskItem *conf, fde_t *F)
630   {
631 <  if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.client_ctx)) == NULL)
631 >  assert(client_p);
632 >  assert(client_p->connection);
633 >  assert(client_p->connection->fd);
634 >  assert(client_p->connection->fd == F);
635 >
636 >  if (!tls_new(&F->ssl, F->fd, TLS_ROLE_CLIENT))
637    {
1308    ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
1309         ERR_error_string(ERR_get_error(), NULL));
638      SetDead(client_p);
639 <    exit_client(client_p, client_p, "SSL_new failed");
639 >    exit_client(client_p, "TLS context initialization failed");
640      return;
641    }
642  
1315  SSL_set_fd(fd->ssl, fd->fd);
1316
643    if (!EmptyString(conf->cipher_list))
644 <    SSL_set_cipher_list(client_p->localClient->fd.ssl, conf->cipher_list);
644 >    tls_set_ciphers(&F->ssl, conf->cipher_list);
645  
646 <  ssl_server_handshake(NULL, client_p);
646 >  server_tls_handshake(F, client_p);
647   }
1322 #endif
648  
649 < /* serv_connect_callback() - complete a server connection.
649 > /* server_connect_callback() - complete a server connection.
650   *
651   * This routine is called after the server connection attempt has
652   * completed. If unsucessful, an error is sent to ops and the client
# Line 1330 | Line 655 | ssl_connect_init(struct Client *client_p
655   * marked for reading.
656   */
657   static void
658 < serv_connect_callback(fde_t *fd, int status, void *data)
658 > server_connect_callback(fde_t *F, int status, void *data)
659   {
660 <  struct Client *client_p = data;
1336 <  struct MaskItem *conf = NULL;
660 >  struct Client *const client_p = data;
661  
662 <  /* First, make sure its a real client! */
663 <  assert(client_p != NULL);
664 <  assert(&client_p->localClient->fd == fd);
662 >  /* First, make sure it's a real client! */
663 >  assert(client_p);
664 >  assert(client_p->connection);
665 >  assert(client_p->connection->fd);
666 >  assert(client_p->connection->fd == F);
667  
668    /* Next, for backward purposes, record the ip of the server */
669 <  memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
670 <         sizeof(struct irc_ssaddr));
669 >  memcpy(&client_p->ip, &F->connect.hostaddr, sizeof(client_p->ip));
670 >
671    /* Check the status */
672    if (status != COMM_OK)
673    {
674 <    /* We have an error, so report it and quit
675 <     * Admins get to see any IP, mere opers don't *sigh*
674 >    /* We have an error, so report it and quit */
675 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
676 >                         "Error connecting to %s: %s",
677 >                         client_get_name(client_p, SHOW_IP), comm_errstr(status));
678 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
679 >                         "Error connecting to %s: %s",
680 >                         client_get_name(client_p, MASK_IP), comm_errstr(status));
681 >
682 >    /*
683 >     * If a fd goes bad, call dead_link() the socket is no
684 >     * longer valid for reading or writing.
685       */
686 <     if (ConfigServerHide.hide_server_ips)
687 <       sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1353 <                            "Error connecting to %s: %s",
1354 <                            client_p->name, comm_errstr(status));
1355 <     else
1356 <       sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1357 <                            "Error connecting to %s[%s]: %s", client_p->name,
1358 <                            client_p->host, comm_errstr(status));
1359 <
1360 <     sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1361 <                          "Error connecting to %s: %s",
1362 <                          client_p->name, comm_errstr(status));
1363 <
1364 <     /* If a fd goes bad, call dead_link() the socket is no
1365 <      * longer valid for reading or writing.
1366 <      */
1367 <     dead_link_on_write(client_p, 0);
1368 <     return;
686 >    dead_link_on_write(client_p, 0);
687 >    return;
688    }
689  
690    /* COMM_OK, so continue the connection procedure */
691 <  /* Get the C/N lines */
692 <  conf = find_conf_name(&client_p->localClient->confs,
693 <                        client_p->name, CONF_SERVER);
694 <  if (conf == NULL)
695 <  {
696 <    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
697 <                         "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
698 <    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
699 <                         "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
691 >  /* Get the connect {} block */
692 >  const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
693 >                                                client_p->name, CONF_SERVER);
694 >  if (!conf)
695 >  {
696 >    sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
697 >                         "Lost connect{} block for %s", client_get_name(client_p, SHOW_IP));
698 >    sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
699 >                         "Lost connect{} block for %s", client_get_name(client_p, MASK_IP));
700  
701 <    exit_client(client_p, &me, "Lost connect{} block");
701 >    exit_client(client_p, "Lost connect{} block");
702      return;
703    }
704  
705    /* Next, send the initial handshake */
706    SetHandshake(client_p);
707  
1389 #ifdef HAVE_LIBCRYPTO
708    if (IsConfSSL(conf))
709    {
710 <    ssl_connect_init(client_p, conf, fd);
710 >    server_tls_connect_init(client_p, conf, F);
711      return;
712    }
1395 #endif
713  
714 <  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
714 >  sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
715  
716 <  send_capabilities(client_p, 0);
716 >  sendto_one(client_p, "CAPAB :%s", capab_get(NULL));
717  
718    sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
719               ConfigServerHide.hidden ? "(H) " : "", me.info);
720  
1404  /* If we've been marked dead because a send failed, just exit
1405   * here now and save everyone the trouble of us ever existing.
1406   */
1407  if (IsDead(client_p))
1408  {
1409      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1410                           "%s[%s] went dead during handshake",
1411                           client_p->name,
1412                           client_p->host);
1413      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1414                           "%s went dead during handshake", client_p->name);
1415      return;
1416  }
1417
1418  /* don't move to serv_list yet -- we haven't sent a burst! */
721    /* If we get here, we're ok, so lets start reading some data */
722 <  comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
722 >  read_packet(client_p->connection->fd, client_p);
723   }
724  
725   struct Client *
726   find_servconn_in_progress(const char *name)
727   {
728    dlink_node *ptr;
1427  struct Client *cptr;
729  
730    DLINK_FOREACH(ptr, unknown_list.head)
731    {
732 <    cptr = ptr->data;
732 >    struct Client *cptr = ptr->data;
733  
734 <    if (cptr && cptr->name[0])
735 <      if (!match(name, cptr->name))
734 >    if (cptr->name[0])
735 >      if (!irccmp(name, cptr->name))
736          return cptr;
737    }
738  

Comparing:
ircd-hybrid/trunk/src/s_serv.c (property svn:keywords), Revision 3137 by michael, Tue Mar 11 19:01:43 2014 UTC vs.
ircd-hybrid/trunk/src/server.c (property svn:keywords), Revision 8512 by michael, Fri Apr 6 19:28:05 2018 UTC

# Line 1 | Line 1
1 < Id Revision
1 > Id

Diff Legend

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