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/server.c (file contents):
Revision 6602 by michael, Thu Oct 22 18:07:38 2015 UTC vs.
Revision 7951 by michael, Thu Mar 2 17:54:43 2017 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 1997-2015 ircd-hybrid development team
4 > *  Copyright (c) 1997-2017 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 25 | Line 25
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"
29   #include "client.h"
30   #include "event.h"
# Line 37 | Line 33
33   #include "ircd.h"
34   #include "ircd_defs.h"
35   #include "s_bsd.h"
40 #include "numeric.h"
36   #include "packet.h"
37   #include "conf.h"
38   #include "server.h"
39   #include "log.h"
45 #include "user.h"
40   #include "send.h"
41   #include "memory.h"
48 #include "channel.h"
42   #include "parse.h"
43  
44  
# Line 82 | Line 75 | write_links_file(void *unused)
75    DLINK_FOREACH_SAFE(node, node_next, flatten_links.head)
76    {
77      dlinkDelete(node, &flatten_links);
78 <    MyFree(node->data);
78 >    xfree(node->data);
79      free_dlink_node(node);
80    }
81  
# Line 256 | Line 249 | try_connections(void *unused)
249   {
250    dlink_node *node = NULL;
251  
259  /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
252    if (GlobalSetOptions.autoconn == 0)
253      return;
254  
255 <  DLINK_FOREACH(node, server_items.head)
255 >  DLINK_FOREACH(node, connect_items.head)
256    {
257      struct MaskItem *conf = node->data;
258  
259      assert(conf->type == CONF_SERVER);
260  
261 <    /* Also when already connecting! (update holdtimes) --SRB
270 <     */
261 >    /* Also when already connecting! (update holdtimes) --SRB */
262      if (!conf->port || !IsConfAllowAutoConn(conf))
263        continue;
264  
265 <
266 <    /* Skip this entry if the use of it is still on hold until
265 >    /*
266 >     * Skip this entry if the use of it is still on hold until
267       * future. Otherwise handle this entry (and set it on hold
268       * until next time). Will reset only hold times, if already
269       * made one successfull connection... [this algorithm is
# Line 297 | Line 288 | try_connections(void *unused)
288        /* Move this entry to the end of the list, if not already last */
289        if (node->next)
290        {
291 <        dlinkDelete(node, &server_items);
292 <        dlinkAddTail(conf, &conf->node, &server_items);
291 >        dlinkDelete(node, &connect_items);
292 >        dlinkAddTail(conf, &conf->node, &connect_items);
293        }
294  
295        if (find_servconn_in_progress(conf->name))
# Line 347 | Line 338 | valid_servname(const char *name)
338    return dots && (p - name) <= HOSTLEN;
339   }
340  
341 < int
342 < check_server(const char *name, struct Client *client_p)
341 > /* server_capab_init()
342 > *
343 > * inputs       - none
344 > * output       - none
345 > */
346 > void
347 > server_capab_init(void)
348   {
349 <  dlink_node *node = NULL;
350 <  struct MaskItem *conf        = NULL;
351 <  struct MaskItem *server_conf = NULL;
352 <  int error = -1;
353 <
354 <  assert(client_p);
359 <
360 <  /* Loop through looking for all possible connect items that might work */
361 <  DLINK_FOREACH(node, server_items.head)
362 <  {
363 <    conf = node->data;
364 <
365 <    if (match(name, conf->name))
366 <      continue;
367 <
368 <    error = -3;
369 <
370 <    /* XXX: Fix me for IPv6                    */
371 <    /* XXX sockhost is the IPv4 ip as a string */
372 <    if (!match(conf->host, client_p->host) ||
373 <        !match(conf->host, client_p->sockhost))
374 <    {
375 <      error = -2;
376 <
377 <      if (!match_conf_password(client_p->connection->password, conf))
378 <        return -2;
379 <
380 <      if (!EmptyString(conf->certfp))
381 <        if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
382 <          return -4;
383 <
384 <      server_conf = conf;
385 <    }
386 <  }
387 <
388 <  if (server_conf == NULL)
389 <    return error;
390 <
391 <  attach_conf(client_p, server_conf);
392 <
393 <  switch (server_conf->aftype)
394 <  {
395 <    case AF_INET6:
396 <    {
397 <      const struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)&server_conf->addr;
398 <
399 <      if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
400 <        memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr));
401 <      break;
402 <    }
403 <    case AF_INET:
404 <    {
405 <      const struct sockaddr_in *v4 = (struct sockaddr_in *)&server_conf->addr;
406 <
407 <      if (v4->sin_addr.s_addr == INADDR_NONE)
408 <        memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr));
409 <      break;
410 <    }
411 <  }
412 <
413 <  return 0;
349 >  add_capability("QS", CAPAB_QS);
350 >  add_capability("EOB", CAPAB_EOB);
351 >  add_capability("CLUSTER", CAPAB_CLUSTER);
352 >  add_capability("SVS", CAPAB_SVS);
353 >  add_capability("CHW", CAPAB_CHW);
354 >  add_capability("HOPS", CAPAB_HOPS);
355   }
356  
357   /* add_capability()
# Line 425 | Line 366 | check_server(const char *name, struct Cl
366   void
367   add_capability(const char *name, unsigned int flag)
368   {
369 <  struct Capability *cap = MyCalloc(sizeof(*cap));
369 >  struct Capability *cap = xcalloc(sizeof(*cap));
370  
371    cap->name = xstrdup(name);
372    cap->cap = flag;
# Line 450 | Line 391 | delete_capability(const char *name)
391      if (!irccmp(cap->name, name))
392      {
393        dlinkDelete(node, &server_capabilities_list);
394 <      MyFree(cap->name);
395 <      MyFree(cap);
394 >      xfree(cap->name);
395 >      xfree(cap);
396      }
397    }
398   }
# Line 466 | Line 407 | delete_capability(const char *name)
407   unsigned int
408   find_capability(const char *name)
409   {
410 <  const dlink_node *node = NULL;
410 >  dlink_node *node;
411  
412    DLINK_FOREACH(node, server_capabilities_list.head)
413    {
# Line 479 | Line 420 | find_capability(const char *name)
420    return 0;
421   }
422  
482 /* send_capabilities()
483 *
484 * inputs       - Client pointer to send to
485 *              - int flag of capabilities that this server can send
486 * output       - NONE
487 * side effects - send the CAPAB line to a server  -orabidoo
488 *
489 */
490 void
491 send_capabilities(struct Client *client_p)
492 {
493  char buf[IRCD_BUFSIZE] = "";
494  const dlink_node *node = NULL;
495
496  DLINK_FOREACH(node, server_capabilities_list.head)
497  {
498    const struct Capability *cap = node->data;
499
500    strlcat(buf, cap->name, sizeof(buf));
501
502    if (node->next)
503      strlcat(buf, " ", sizeof(buf));
504  }
505
506  sendto_one(client_p, "CAPAB :%s", buf);
507 }
508
423   /*
424   * show_capabilities - show current server capabilities
425   *
# Line 514 | Line 428 | send_capabilities(struct Client *client_
428   * side effects - build up string representing capabilities of server listed
429   */
430   const char *
431 < show_capabilities(const struct Client *target_p)
431 > get_capabilities(const struct Client *client_p)
432   {
433 <  static char msgbuf[IRCD_BUFSIZE] = "";
434 <  const dlink_node *node = NULL;
433 >  static char buf[IRCD_BUFSIZE] = "";
434 >  dlink_node *node;
435  
436 <  strlcpy(msgbuf, "TS", sizeof(msgbuf));
436 >  buf[0] = '\0';
437  
438    DLINK_FOREACH(node, server_capabilities_list.head)
439    {
440      const struct Capability *cap = node->data;
441  
442 <    if (!IsCapable(target_p, cap->cap))
442 >    if (client_p && !IsCapable(client_p, cap->cap))
443        continue;
444  
445 <    strlcat(msgbuf,       " ", sizeof(msgbuf));
446 <    strlcat(msgbuf, cap->name, sizeof(msgbuf));
445 >    strlcat(buf, cap->name, sizeof(buf));
446 >
447 >    if (node->next)
448 >      strlcat(buf, " ", sizeof(buf));
449    }
450  
451 <  return msgbuf;
451 >  return buf;
452   }
453  
454   /* make_server()
# Line 546 | Line 462 | struct Server *
462   make_server(struct Client *client_p)
463   {
464    if (client_p->serv == NULL)
465 <    client_p->serv = MyCalloc(sizeof(struct Server));
465 >    client_p->serv = xcalloc(sizeof(struct Server));
466  
467    return client_p->serv;
468   }
# Line 606 | Line 522 | serv_connect(struct MaskItem *conf, stru
522      return 0;
523    }
524  
525 <  /* Make sure this server isn't already connected
526 <   * Note: conf should ALWAYS be a valid C: line
525 >  /*
526 >   * Make sure this server isn't already connected.
527 >   * Note: conf should ALWAYS be a valid connect {} block
528     */
529    if ((client_p = hash_find_server(conf->name)))
530    {
# Line 633 | Line 550 | serv_connect(struct MaskItem *conf, stru
550    /* We already converted the ip once, so lets use it - stu */
551    strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
552  
553 <  /* create a socket for the server connection */
553 >  /* Create a socket for the server connection */
554    if (comm_open(&client_p->connection->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0)
555    {
556      /* Eek, failure to create the socket */
# Line 644 | Line 561 | serv_connect(struct MaskItem *conf, stru
561      return 0;
562    }
563  
564 <  /* servernames are always guaranteed under HOSTLEN chars */
565 <  fd_note(&client_p->connection->fd, "Server: %s", conf->name);
564 >  /* Server names are always guaranteed under HOSTLEN chars */
565 >  fd_note(&client_p->connection->fd, "Server: %s", client_p->name);
566  
567 <  /* Attach config entries to client here rather than in
568 <   * serv_connect_callback(). This to avoid null pointer references.
567 >  /*
568 >   * Attach config entries to client here rather than in serv_connect_callback().
569 >   * This to avoid null pointer references.
570     */
571    if (!attach_connect_block(client_p, conf->name, conf->host))
572    {
# Line 663 | Line 581 | serv_connect(struct MaskItem *conf, stru
581      return 0;
582    }
583  
584 <  /* at this point we have a connection in progress and C/N lines
585 <   * attached to the client, the socket info should be saved in the
586 <   * client and it should either be resolved or have a valid address.
584 >  /*
585 >   * At this point we have a connection in progress and a connect {} block
586 >   * attached to the client, the socket info should be saved in the client
587 >   * and it should either be resolved or have a valid address.
588     *
589     * The socket has been connected or connect is in progress.
590     */
# Line 759 | Line 678 | serv_connect(struct MaskItem *conf, stru
678    return 1;
679   }
680  
762 #ifdef HAVE_LIBCRYPTO
681   static void
682   finish_ssl_server_handshake(struct Client *client_p)
683   {
684 <  struct MaskItem *conf = NULL;
685 <
686 <  conf = find_conf_name(&client_p->connection->confs,
769 <                        client_p->name, CONF_SERVER);
770 <  if (conf == NULL)
684 >  const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
685 >                                                client_p->name, CONF_SERVER);
686 >  if (!conf)
687    {
688      sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
689                           "Lost connect{} block for %s", get_client_name(client_p, SHOW_IP));
# Line 778 | Line 694 | finish_ssl_server_handshake(struct Clien
694      return;
695    }
696  
697 <  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
697 >  sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
698  
699 <  send_capabilities(client_p);
699 >  sendto_one(client_p, "CAPAB :%s", get_capabilities(NULL));
700  
701    sendto_one(client_p, "SERVER %s 1 :%s%s",
702               me.name, ConfigServerHide.hidden ? "(H) " : "",
# Line 808 | Line 724 | static void
724   ssl_server_handshake(fde_t *fd, void *data)
725   {
726    struct Client *client_p = data;
727 <  X509 *cert = NULL;
812 <  int ret = 0;
727 >  const char *sslerr = NULL;
728  
729 <  if ((ret = SSL_connect(client_p->connection->fd.ssl)) <= 0)
729 >  tls_handshake_status_t ret = tls_handshake(&client_p->connection->fd.ssl, TLS_ROLE_CLIENT, &sslerr);
730 >  if (ret != TLS_HANDSHAKE_DONE)
731    {
732      if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT)
733      {
734 <      exit_client(client_p, "Timeout during SSL handshake");
734 >      exit_client(client_p, "Timeout during TLS handshake");
735        return;
736      }
737  
738 <    switch (SSL_get_error(client_p->connection->fd.ssl, ret))
738 >    switch (ret)
739      {
740 <      case SSL_ERROR_WANT_WRITE:
740 >      case TLS_HANDSHAKE_WANT_WRITE:
741          comm_setselect(&client_p->connection->fd, COMM_SELECT_WRITE,
742                         ssl_server_handshake, client_p, CONNECTTIMEOUT);
743          return;
744 <      case SSL_ERROR_WANT_READ:
744 >      case TLS_HANDSHAKE_WANT_READ:
745          comm_setselect(&client_p->connection->fd, COMM_SELECT_READ,
746                         ssl_server_handshake, client_p, CONNECTTIMEOUT);
747          return;
748        default:
749        {
834        const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
750          sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
751                               "Error connecting to %s: %s", client_p->name,
752 <                             sslerr ? sslerr : "unknown SSL error");
753 <        exit_client(client_p, "Error during SSL handshake");
752 >                             sslerr ? sslerr : "unknown TLS error");
753 >        exit_client(client_p, "Error during TLS handshake");
754          return;
755        }
756      }
# Line 843 | Line 758 | ssl_server_handshake(fde_t *fd, void *da
758  
759    comm_settimeout(&client_p->connection->fd, 0, NULL, NULL);
760  
761 <  if ((cert = SSL_get_peer_certificate(client_p->connection->fd.ssl)))
762 <  {
763 <    int res = SSL_get_verify_result(client_p->connection->fd.ssl);
849 <    char buf[EVP_MAX_MD_SIZE * 2 + 1] = "";
850 <    unsigned char md[EVP_MAX_MD_SIZE] = "";
851 <
852 <    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
853 <        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
854 <        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
855 <    {
856 <      unsigned int n = 0;
857 <
858 <      if (X509_digest(cert, ConfigServerInfo.message_digest_algorithm, md, &n))
859 <      {
860 <        binary_to_hex(md, buf, n);
861 <        client_p->certfp = xstrdup(buf);
862 <      }
863 <    }
864 <    else
865 <      ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad SSL client certificate: %d",
866 <           client_p->name, client_p->username, client_p->host, res);
867 <    X509_free(cert);
868 <  }
761 >  if (!tls_verify_cert(&client_p->connection->fd.ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp))
762 >    ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad TLS client certificate",
763 >         client_p->name, client_p->username, client_p->host);
764  
765    finish_ssl_server_handshake(client_p);
766   }
# Line 873 | Line 768 | ssl_server_handshake(fde_t *fd, void *da
768   static void
769   ssl_connect_init(struct Client *client_p, const struct MaskItem *conf, fde_t *fd)
770   {
771 <  if ((client_p->connection->fd.ssl = SSL_new(ConfigServerInfo.client_ctx)) == NULL)
771 >  if (!tls_new(&client_p->connection->fd.ssl, fd->fd, TLS_ROLE_CLIENT))
772    {
878    ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
879         ERR_error_string(ERR_get_error(), NULL));
773      SetDead(client_p);
774 <    exit_client(client_p, "SSL_new failed");
774 >    exit_client(client_p, "TLS context initialization failed");
775      return;
776    }
777  
885  SSL_set_fd(fd->ssl, fd->fd);
886
778    if (!EmptyString(conf->cipher_list))
779 <    SSL_set_cipher_list(client_p->connection->fd.ssl, conf->cipher_list);
779 >    tls_set_ciphers(&client_p->connection->fd.ssl, conf->cipher_list);
780  
781    ssl_server_handshake(NULL, client_p);
782   }
892 #endif
783  
784   /* serv_connect_callback() - complete a server connection.
785   *
# Line 903 | Line 793 | static void
793   serv_connect_callback(fde_t *fd, int status, void *data)
794   {
795    struct Client *const client_p = data;
906  const struct MaskItem *conf = NULL;
796  
797    /* First, make sure it's a real client! */
798    assert(client_p);
799    assert(&client_p->connection->fd == fd);
800  
801    /* Next, for backward purposes, record the ip of the server */
802 <  memcpy(&client_p->connection->ip, &fd->connect.hostaddr,
914 <         sizeof(struct irc_ssaddr));
802 >  memcpy(&client_p->connection->ip, &fd->connect.hostaddr, sizeof(struct irc_ssaddr));
803  
804    /* Check the status */
805    if (status != COMM_OK)
# Line 933 | Line 821 | serv_connect_callback(fde_t *fd, int sta
821    }
822  
823    /* COMM_OK, so continue the connection procedure */
824 <  /* Get the C/N lines */
825 <  conf = find_conf_name(&client_p->connection->confs,
826 <                        client_p->name, CONF_SERVER);
827 <  if (conf == NULL)
824 >  /* Get the connect {} block */
825 >  const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
826 >                                                client_p->name, CONF_SERVER);
827 >  if (!conf)
828    {
829      sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
830                           "Lost connect{} block for %s", get_client_name(client_p, SHOW_IP));
# Line 950 | Line 838 | serv_connect_callback(fde_t *fd, int sta
838    /* Next, send the initial handshake */
839    SetHandshake(client_p);
840  
953 #ifdef HAVE_LIBCRYPTO
841    if (IsConfSSL(conf))
842    {
843      ssl_connect_init(client_p, conf, fd);
844      return;
845    }
959 #endif
846  
847 <  sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
847 >  sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
848  
849 <  send_capabilities(client_p);
849 >  sendto_one(client_p, "CAPAB :%s", get_capabilities(NULL));
850  
851    sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
852               ConfigServerHide.hidden ? "(H) " : "", me.info);
# Line 987 | Line 873 | struct Client *
873   find_servconn_in_progress(const char *name)
874   {
875    dlink_node *ptr;
990  struct Client *cptr;
876  
877    DLINK_FOREACH(ptr, unknown_list.head)
878    {
879 <    cptr = ptr->data;
879 >    struct Client *cptr = ptr->data;
880  
881 <    if (cptr && cptr->name[0])
882 <      if (!match(name, cptr->name))
881 >    if (cptr->name[0])
882 >      if (!irccmp(name, cptr->name))
883          return cptr;
884    }
885  

Comparing ircd-hybrid/trunk/src/server.c (property svn:keywords):
Revision 6602 by michael, Thu Oct 22 18:07:38 2015 UTC vs.
Revision 7951 by michael, Thu Mar 2 17:54:43 2017 UTC

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

Diff Legend

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