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

Comparing ircd-hybrid/branches/8.2.x/src/server.c (file contents):
Revision 8878 by michael, Sun Feb 24 09:12:52 2019 UTC vs.
Revision 9249 by michael, Sat Feb 1 13:35:32 2020 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 1997-2019 ircd-hybrid development team
4 > *  Copyright (c) 1997-2020 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 58 | Line 58 | static void server_connect_callback(fde_
58   void
59   write_links_file(void *unused)
60   {
61 <  FILE *file = NULL;
61 >  char buf[IRCD_BUFSIZE];
62    dlink_node *node, *node_next;
63  char buff[IRCD_BUFSIZE] = "";
63  
64    if (EmptyString(ConfigServerHide.flatten_links_file))
65      return;
66  
67 <  if ((file = fopen(ConfigServerHide.flatten_links_file, "w")) == NULL)
67 >  FILE *file = fopen(ConfigServerHide.flatten_links_file, "w");
68 >  if (file == NULL)
69    {
70      ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
71           strerror(errno));
# Line 99 | Line 99 | write_links_file(void *unused)
99       * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
100       * - madmax
101       */
102 <    snprintf(buff, sizeof(buff), "%s %s :1 %s",   target_p->name,
103 <             me.name, target_p->info);
104 <    dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
105 <    snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
106 <             me.name, target_p->info);
102 >    snprintf(buf, sizeof(buf), "%s %s :1 %s", target_p->name, me.name, target_p->info);
103 >    dlinkAddTail(xstrdup(buf), make_dlink_node(), &flatten_links);
104  
105 <    fputs(buff, file);
105 >    strlcat(buf, "\n", sizeof(buf));
106 >    fputs(buf, file);
107    }
108  
109    fclose(file);
# Line 114 | Line 112 | write_links_file(void *unused)
112   void
113   read_links_file(void)
114   {
115 <  FILE *file = NULL;
118 <  char *p = NULL;
119 <  char buff[IRCD_BUFSIZE] = "";
115 >  char buf[IRCD_BUFSIZE];
116  
117    if (EmptyString(ConfigServerHide.flatten_links_file))
118      return;
119  
120 <  if ((file = fopen(ConfigServerHide.flatten_links_file, "r")) == NULL)
120 >  FILE *file = fopen(ConfigServerHide.flatten_links_file, "r");
121 >  if (file == NULL)
122    {
123      ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
124           strerror(errno));
125      return;
126    }
127  
128 <  while (fgets(buff, sizeof(buff), file))
128 >  while (fgets(buf, sizeof(buf), file))
129    {
130 <    if ((p = strchr(buff, '\n')))
130 >    char *p = strchr(buf, '\n');
131 >    if (p)
132        *p = '\0';
133  
134 <    dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
134 >    dlinkAddTail(xstrdup(buf), make_dlink_node(), &flatten_links);
135    }
136  
137    fclose(file);
# Line 174 | Line 172 | server_hunt(struct Client *source_p, con
172      return h;
173    }
174  
175 <  if ((h->target_p = find_person(source_p, parv[server])) == NULL)
175 >  h->target_p = find_person(source_p, parv[server]);
176 >  if (h->target_p == NULL)
177      h->target_p = hash_find_server(parv[server]);
178  
179    /*
# Line 280 | Line 279 | try_connections(void *unused)
279       * made one successfull connection... [this algorithm is
280       * a bit fuzzy... -- msa >;) ]
281       */
282 <    if (conf->until > CurrentTime)
282 >    if (conf->until > event_base->time.sec_monotonic)
283        continue;
284  
285 <    conf->until = CurrentTime + conf->class->con_freq;
285 >    conf->until = event_base->time.sec_monotonic + conf->class->con_freq;
286  
287      /*
288       * Found a CONNECT config with port specified, scan clients
# Line 355 | Line 354 | server_valid_name(const char *name)
354   *                if it was not previously allocated.
355   */
356   struct Server *
357 < server_make(struct Client *client_p)
357 > server_make(struct Client *client)
358   {
359 <  if (client_p->serv == NULL)
360 <    client_p->serv = xcalloc(sizeof(*client_p->serv));
359 >  if (client->serv == NULL)
360 >    client->serv = xcalloc(sizeof(*client->serv));
361  
362 <  return client_p->serv;
362 >  return client->serv;
363   }
364  
365   /* server_connect() - initiate a server connection
# Line 382 | Line 381 | server_make(struct Client *client_p)
381   bool
382   server_connect(struct MaskItem *conf, struct Client *by)
383   {
384 <  char buf[HOSTIPLEN + 1] = "";
384 >  char buf[HOSTIPLEN + 1];
385  
386    /* Make sure conf is useful */
387    assert(conf);
# Line 420 | Line 419 | server_connect(struct MaskItem *conf, st
419    }
420  
421    /* Create a local client */
422 <  struct Client *client_p = client_make(NULL);
422 >  struct Client *client = client_make(NULL);
423  
424    /* Copy in the server, hostname, fd */
425 <  strlcpy(client_p->name, conf->name, sizeof(client_p->name));
426 <  strlcpy(client_p->host, conf->host, sizeof(client_p->host));
425 >  strlcpy(client->name, conf->name, sizeof(client->name));
426 >  strlcpy(client->host, conf->host, sizeof(client->host));
427  
428    /* We already converted the ip once, so lets use it - stu */
429 <  strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
429 >  strlcpy(client->sockhost, buf, sizeof(client->sockhost));
430  
431 <  memcpy(&client_p->ip, conf->addr, sizeof(client_p->ip));
432 <
434 <  client_p->connection->fd = fd_open(fd, true, NULL);
431 >  client->ip = *conf->addr;
432 >  client->connection->fd = fd_open(fd, true, NULL);
433  
434    /* Server names are always guaranteed under HOSTLEN chars */
435 <  fd_note(client_p->connection->fd, "Server: %s", client_p->name);
435 >  fd_note(client->connection->fd, "Server: %s", client->name);
436  
437    /*
438     * Attach config entries to client here rather than in server_connect_callback().
439     * This to avoid null pointer references.
440     */
441 <  conf_attach(client_p, conf);
441 >  conf_attach(client, conf);
442  
443 <  server_make(client_p);
443 >  server_make(client);
444  
445    if (by && IsClient(by))
446 <    strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
446 >    strlcpy(client->serv->by, by->name, sizeof(client->serv->by));
447    else
448 <    strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
448 >    strlcpy(client->serv->by, "AutoConn.", sizeof(client->serv->by));
449  
450 <  SetConnecting(client_p);
450 >  SetConnecting(client);
451  
452    /* Now, initiate the connection */
453 <  comm_connect_tcp(client_p->connection->fd, conf->addr, conf->port, conf->bind,
454 <                   server_connect_callback, client_p, CONNECTTIMEOUT);
453 >  comm_connect_tcp(client->connection->fd, conf->addr, conf->port, conf->bind,
454 >                   server_connect_callback, client, conf->timeout);
455  
456    /*
457     * At this point we have a connection in progress and a connect {} block
# Line 466 | Line 464 | server_connect(struct MaskItem *conf, st
464   }
465  
466   static void
467 < server_finish_tls_handshake(struct Client *client_p)
467 > server_finish_tls_handshake(struct Client *client)
468   {
469 <  const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
470 <                                                client_p->name, CONF_SERVER);
471 <  if (!conf)
469 >  const struct MaskItem *conf = find_conf_name(&client->connection->confs,
470 >                                                client->name, CONF_SERVER);
471 >  if (conf == NULL)
472    {
473      sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
474 <                         "Lost connect{} block for %s", client_get_name(client_p, SHOW_IP));
474 >                         "Lost connect{} block for %s", client_get_name(client, SHOW_IP));
475      sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
476 <                         "Lost connect{} block for %s", client_get_name(client_p, MASK_IP));
476 >                         "Lost connect{} block for %s", client_get_name(client, MASK_IP));
477  
478 <    exit_client(client_p, "Lost connect{} block");
478 >    exit_client(client, "Lost connect{} block");
479      return;
480    }
481  
482    /* Next, send the initial handshake */
483 <  SetHandshake(client_p);
483 >  SetHandshake(client);
484  
485 <  sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
485 >  sendto_one(client, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
486  
487 <  sendto_one(client_p, "CAPAB :%s", capab_get(NULL));
487 >  sendto_one(client, "CAPAB :%s", capab_get(NULL));
488  
489 <  sendto_one(client_p, "SERVER %s 1 :%s%s",
489 >  sendto_one(client, "SERVER %s 1 :%s%s",
490               me.name, ConfigServerHide.hidden ? "(H) " : "",
491               me.info);
492  
493    /* If we get here, we're ok, so lets start reading some data */
494 <  read_packet(client_p->connection->fd, client_p);
494 >  read_packet(client->connection->fd, client);
495   }
496  
497   static void
498   server_tls_handshake(fde_t *F, void *data)
499   {
500 <  struct Client *client_p = data;
500 >  struct Client *client = data;
501    const char *sslerr = NULL;
502  
503 <  assert(client_p);
504 <  assert(client_p->connection);
505 <  assert(client_p->connection->fd);
506 <  assert(client_p->connection->fd == F);
503 >  assert(client);
504 >  assert(client->connection);
505 >  assert(client->connection->fd);
506 >  assert(client->connection->fd == F);
507  
508 <  tls_handshake_status_t ret = tls_handshake(&F->ssl, TLS_ROLE_CLIENT, &sslerr);
508 >  tls_handshake_status_t ret = tls_handshake(&F->tls, TLS_ROLE_CLIENT, &sslerr);
509    if (ret != TLS_HANDSHAKE_DONE)
510    {
511 <    if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT)
511 >    if ((event_base->time.sec_monotonic - client->connection->created_monotonic) > TLS_HANDSHAKE_TIMEOUT)
512      {
513 <      exit_client(client_p, "Timeout during TLS handshake");
513 >      exit_client(client, "Timeout during TLS handshake");
514        return;
515      }
516  
# Line 520 | Line 518 | server_tls_handshake(fde_t *F, void *dat
518      {
519        case TLS_HANDSHAKE_WANT_WRITE:
520          comm_setselect(F, COMM_SELECT_WRITE,
521 <                       server_tls_handshake, client_p, CONNECTTIMEOUT);
521 >                       server_tls_handshake, client, TLS_HANDSHAKE_TIMEOUT);
522          return;
523        case TLS_HANDSHAKE_WANT_READ:
524          comm_setselect(F, COMM_SELECT_READ,
525 <                       server_tls_handshake, client_p, CONNECTTIMEOUT);
525 >                       server_tls_handshake, client, TLS_HANDSHAKE_TIMEOUT);
526          return;
527        default:
528        {
529          sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
530 <                             "Error connecting to %s: %s", client_p->name,
530 >                             "Error connecting to %s: %s", client->name,
531                               sslerr ? sslerr : "unknown TLS error");
532 <        exit_client(client_p, "Error during TLS handshake");
532 >        exit_client(client, "Error during TLS handshake");
533          return;
534        }
535      }
# Line 539 | Line 537 | server_tls_handshake(fde_t *F, void *dat
537  
538    comm_settimeout(F, 0, NULL, NULL);
539  
540 <  if (tls_verify_cert(&F->ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp) == false)
541 <    ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad TLS client certificate",
542 <         client_p->name, client_p->username, client_p->host);
540 >  if (tls_verify_certificate(&F->tls, ConfigServerInfo.message_digest_algorithm, &client->certfp) == false)
541 >    ilog(LOG_TYPE_IRCD, "Server %s gave bad TLS client certificate",
542 >         client_get_name(client, MASK_IP));
543  
544 <  server_finish_tls_handshake(client_p);
544 >  server_finish_tls_handshake(client);
545   }
546  
547   static void
548 < server_tls_connect_init(struct Client *client_p, const struct MaskItem *conf, fde_t *F)
548 > server_tls_connect_init(struct Client *client, const struct MaskItem *conf, fde_t *F)
549   {
550 <  assert(client_p);
551 <  assert(client_p->connection);
552 <  assert(client_p->connection->fd);
553 <  assert(client_p->connection->fd == F);
550 >  assert(client);
551 >  assert(client->connection);
552 >  assert(client->connection->fd);
553 >  assert(client->connection->fd == F);
554  
555 <  if (tls_new(&F->ssl, F->fd, TLS_ROLE_CLIENT) == false)
555 >  if (tls_new(&F->tls, F->fd, TLS_ROLE_CLIENT) == false)
556    {
557 <    SetDead(client_p);
558 <    exit_client(client_p, "TLS context initialization failed");
557 >    SetDead(client);
558 >    exit_client(client, "TLS context initialization failed");
559      return;
560    }
561  
562    if (!EmptyString(conf->cipher_list))
563 <    tls_set_ciphers(&F->ssl, conf->cipher_list);
563 >    tls_set_ciphers(&F->tls, conf->cipher_list);
564  
565 <  server_tls_handshake(F, client_p);
565 >  server_tls_handshake(F, client);
566   }
567  
568   /* server_connect_callback() - complete a server connection.
# Line 578 | Line 576 | server_tls_connect_init(struct Client *c
576   static void
577   server_connect_callback(fde_t *F, int status, void *data)
578   {
579 <  struct Client *const client_p = data;
579 >  struct Client *const client = data;
580  
581    /* First, make sure it's a real client! */
582 <  assert(client_p);
583 <  assert(client_p->connection);
584 <  assert(client_p->connection->fd);
585 <  assert(client_p->connection->fd == F);
582 >  assert(client);
583 >  assert(client->connection);
584 >  assert(client->connection->fd);
585 >  assert(client->connection->fd == F);
586  
587    /* Check the status */
588    if (status != COMM_OK)
# Line 592 | Line 590 | server_connect_callback(fde_t *F, int st
590      /* We have an error, so report it and quit */
591      sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
592                           "Error connecting to %s: %s",
593 <                         client_get_name(client_p, SHOW_IP), comm_errstr(status));
593 >                         client_get_name(client, SHOW_IP), comm_errstr(status));
594      sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
595                           "Error connecting to %s: %s",
596 <                         client_get_name(client_p, MASK_IP), comm_errstr(status));
596 >                         client_get_name(client, MASK_IP), comm_errstr(status));
597  
598      /*
599       * If a fd goes bad, call dead_link() the socket is no
600       * longer valid for reading or writing.
601       */
602 <    dead_link_on_write(client_p, 0);
602 >    dead_link_on_write(client, 0);
603      return;
604    }
605  
606    /* COMM_OK, so continue the connection procedure */
607    /* Get the connect {} block */
608 <  const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
609 <                                                client_p->name, CONF_SERVER);
610 <  if (!conf)
608 >  const struct MaskItem *conf = find_conf_name(&client->connection->confs,
609 >                                                client->name, CONF_SERVER);
610 >  if (conf == NULL)
611    {
612      sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
613 <                         "Lost connect{} block for %s", client_get_name(client_p, SHOW_IP));
613 >                         "Lost connect{} block for %s", client_get_name(client, SHOW_IP));
614      sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
615 <                         "Lost connect{} block for %s", client_get_name(client_p, MASK_IP));
615 >                         "Lost connect{} block for %s", client_get_name(client, MASK_IP));
616  
617 <    exit_client(client_p, "Lost connect{} block");
617 >    exit_client(client, "Lost connect{} block");
618      return;
619    }
620  
621 <  if (IsConfSSL(conf))
621 >  if (IsConfTLS(conf))
622    {
623 <    server_tls_connect_init(client_p, conf, F);
623 >    server_tls_connect_init(client, conf, F);
624      return;
625    }
626  
627    /* Next, send the initial handshake */
628 <  SetHandshake(client_p);
628 >  SetHandshake(client);
629  
630 <  sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
630 >  sendto_one(client, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
631  
632 <  sendto_one(client_p, "CAPAB :%s", capab_get(NULL));
632 >  sendto_one(client, "CAPAB :%s", capab_get(NULL));
633  
634 <  sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
634 >  sendto_one(client, "SERVER %s 1 :%s%s", me.name,
635               ConfigServerHide.hidden ? "(H) " : "", me.info);
636  
637    /* If we get here, we're ok, so lets start reading some data */
638 <  read_packet(client_p->connection->fd, client_p);
638 >  read_packet(client->connection->fd, client);
639   }
640  
641   struct Client *

Diff Legend

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