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

Comparing:
ircd-hybrid/trunk/src/s_serv.c (file contents), Revision 3107 by michael, Thu Mar 6 12:09:42 2014 UTC vs.
ircd-hybrid/branches/8.2.x/src/server.c (file contents), Revision 4208 by michael, Sat Jul 12 18:13:06 2014 UTC

# Line 30 | Line 30
30   #include "rsa.h"
31   #endif
32   #include "list.h"
33 #include "channel.h"
34 #include "channel_mode.h"
33   #include "client.h"
34   #include "event.h"
37 #include "fdlist.h"
35   #include "hash.h"
36   #include "irc_string.h"
37   #include "ircd.h"
# Line 42 | Line 39
39   #include "s_bsd.h"
40   #include "numeric.h"
41   #include "packet.h"
45 #include "irc_res.h"
42   #include "conf.h"
43 < #include "s_serv.h"
43 > #include "server.h"
44   #include "log.h"
45 < #include "s_misc.h"
50 < #include "s_user.h"
45 > #include "user.h"
46   #include "send.h"
47   #include "memory.h"
48   #include "channel.h"
# Line 57 | Line 52
52  
53   dlink_list flatten_links;
54   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);
63
55   static CNCB serv_connect_callback;
56  
66 static void burst_members(struct Client *, struct Channel *);
57  
58   /*
59   * write_links_file
# Line 78 | Line 68 | write_links_file(void *notused)
68   {
69    FILE *file = NULL;
70    dlink_node *ptr = NULL, *ptr_next = NULL;
71 <  char buff[IRCD_BUFSIZE] = { '\0' };
71 >  char buff[IRCD_BUFSIZE] = "";
72  
73    if ((file = fopen(LIPATH, "w")) == NULL)
74      return;
# Line 90 | Line 80 | write_links_file(void *notused)
80      free_dlink_node(ptr);
81    }
82  
83 <  DLINK_FOREACH(ptr, global_serv_list.head)
83 >  DLINK_FOREACH(ptr, global_server_list.head)
84    {
85      const struct Client *target_p = ptr->data;
86  
# Line 127 | Line 117 | read_links_file(void)
117   {
118    FILE *file = NULL;
119    char *p = NULL;
120 <  char buff[IRCD_BUFSIZE] = { '\0' };
120 >  char buff[IRCD_BUFSIZE] = "";
121  
122    if ((file = fopen(LIPATH, "r")) == NULL)
123      return;
124  
125    while (fgets(buff, sizeof(buff), file))
126    {
127 <    if ((p = strchr(buff, '\n')) != NULL)
127 >    if ((p = strchr(buff, '\n')))
128        *p = '\0';
129  
130      dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
# Line 163 | Line 153 | read_links_file(void)
153   *      returns: (see #defines)
154   */
155   int
156 < hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
156 > hunt_server(struct Client *source_p, const char *command,
157              const int server, const int parc, char *parv[])
158   {
159    struct Client *target_p = NULL;
160 <  struct Client *target_tmp = NULL;
171 <  dlink_node *ptr;
172 <  int wilds;
160 >  dlink_node *ptr = NULL;
161  
162    /* Assume it's me, if no server */
163    if (parc <= server || EmptyString(parv[server]))
164      return HUNTED_ISME;
165  
166 <  if (!strcmp(parv[server], me.id) || !match(parv[server], me.name))
167 <    return HUNTED_ISME;
166 >  if ((target_p = find_person(source_p, parv[server])) == NULL)
167 >    target_p = hash_find_server(parv[server]);
168  
169 <  /* These are to pickup matches that would cause the following
169 >  /*
170 >   * These are to pickup matches that would cause the following
171     * message to go in the wrong direction while doing quick fast
172     * non-matching lookups.
173     */
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
174    if (target_p)
175      if (target_p->from == source_p->from && !MyConnect(target_p))
176        target_p = NULL;
177  
178 <  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)
178 >  if (!target_p && has_wildcards(parv[server]))
179    {
180 <    if (!wilds)
180 >    DLINK_FOREACH(ptr, global_client_list.head)
181      {
182 <      if (!(target_p = hash_find_server(parv[server])))
208 <      {
209 <        sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
210 <                   me.name, source_p->name, parv[server]);
211 <        return HUNTED_NOSUCH;
212 <      }
213 <    }
214 <    else
215 <    {
216 <      DLINK_FOREACH(ptr, global_client_list.head)
217 <      {
218 <        target_tmp = ptr->data;
182 >      struct Client *tmp = ptr->data;
183  
184 <        if (!match(parv[server], target_tmp->name))
185 <        {
186 <          if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
187 <            continue;
188 <          target_p = ptr->data;
184 >      assert(IsMe(tmp) || IsServer(tmp) || IsClient(tmp));
185 >      if (!match(parv[server], tmp->name))
186 >      {
187 >        if (tmp->from == source_p->from && !MyConnect(tmp))
188 >          continue;
189 >        target_p = ptr->data;
190  
191 <          if (IsRegistered(target_p) && (target_p != client_p))
192 <            break;
228 <        }
191 >        if (target_p != source_p->from)  /* TBV: is this needed? */
192 >          break;
193        }
194      }
195    }
196  
197 <  if (target_p != NULL)
197 >  if (target_p)
198    {
199 <    if(!IsRegistered(target_p))
236 <    {
237 <      sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
238 <                 me.name, source_p->name, parv[server]);
239 <      return HUNTED_NOSUCH;
240 <    }
241 <
199 >    assert(IsMe(target_p) || IsServer(target_p) || IsClient(target_p));
200      if (IsMe(target_p) || MyClient(target_p))
201        return HUNTED_ISME;
202  
203 <    if (match(target_p->name, parv[server]))
204 <      parv[server] = target_p->name;
247 <
248 <    sendto_one(target_p, command, ID_or_name(source_p, target_p),
203 >    parv[server] = target_p->id;
204 >    sendto_one(target_p, command, source_p->id,
205                 parv[1], parv[2], parv[3], parv[4],
206                 parv[5], parv[6], parv[7], parv[8]);
207      return HUNTED_PASS;
208    }
209  
210 <  sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
255 <             me.name, source_p->name, parv[server]);
210 >  sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
211    return HUNTED_NOSUCH;
212   }
213  
# Line 270 | Line 225 | void
225   try_connections(void *unused)
226   {
227    dlink_node *ptr = NULL;
228 <  struct MaskItem *conf;
274 <  int confrq;
228 >  int confrq = 0;
229  
230    /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
231    if (GlobalSetOptions.autoconn == 0)
# Line 279 | Line 233 | try_connections(void *unused)
233  
234    DLINK_FOREACH(ptr, server_items.head)
235    {
236 <    conf = ptr->data;
236 >    struct MaskItem *conf = ptr->data;
237  
238      assert(conf->type == CONF_SERVER);
239  
240      /* Also when already connecting! (update holdtimes) --SRB
241       */
242 <    if (!conf->port ||!IsConfAllowAutoConn(conf))
242 >    if (!conf->port || !IsConfAllowAutoConn(conf))
243        continue;
244  
245  
# Line 298 | Line 252 | try_connections(void *unused)
252      if (conf->until > CurrentTime)
253        continue;
254  
255 <    if (conf->class == NULL)
256 <      confrq = DEFAULT_CONNECTFREQUENCY;
257 <    else
258 <    {
259 <      confrq = conf->class->con_freq;
306 <      if (confrq < MIN_CONN_FREQ)
307 <        confrq = MIN_CONN_FREQ;
308 <    }
255 >    assert(conf->class);
256 >
257 >    confrq = conf->class->con_freq;
258 >    if (confrq < MIN_CONN_FREQ)
259 >      confrq = MIN_CONN_FREQ;
260  
261      conf->until = CurrentTime + confrq;
262  
263 <    /* Found a CONNECT config with port specified, scan clients
263 >    /*
264 >     * Found a CONNECT config with port specified, scan clients
265       * and see if this server is already connected?
266       */
267 <    if (hash_find_server(conf->name) != NULL)
267 >    if (hash_find_server(conf->name))
268        continue;
269  
270      if (conf->class->ref_count < conf->class->max_total)
271      {
272        /* Go to the end of the list, if not already last */
273 <      if (ptr->next != NULL)
273 >      if (ptr->next)
274        {
275          dlinkDelete(ptr, &server_items);
276          dlinkAddTail(conf, &conf->node, &server_items);
# Line 327 | Line 279 | try_connections(void *unused)
279        if (find_servconn_in_progress(conf->name))
280          return;
281  
282 <      /* We used to only print this if serv_connect() actually
282 >      /*
283 >       * We used to only print this if serv_connect() actually
284         * succeeded, but since comm_tcp_connect() can call the callback
285         * immediately if there is an error, we were getting error messages
286         * in the wrong order. SO, we just print out the activated line,
# Line 354 | Line 307 | try_connections(void *unused)
307   int
308   valid_servname(const char *name)
309   {
310 <  unsigned int length = 0;
358 <  unsigned int dots   = 0;
310 >  unsigned int dots = 0;
311    const char *p = name;
312  
313    for (; *p; ++p)
# Line 363 | Line 315 | valid_servname(const char *name)
315      if (!IsServChar(*p))
316        return 0;
317  
366    ++length;
367
318      if (*p == '.')
319        ++dots;
320    }
321  
322 <  return dots != 0 && length <= HOSTLEN;
322 >  return dots && (p - name) <= HOSTLEN;
323   }
324  
325   int
# Line 380 | Line 330 | check_server(const char *name, struct Cl
330    struct MaskItem *server_conf = NULL;
331    int error = -1;
332  
333 <  assert(client_p != NULL);
333 >  assert(client_p);
334  
335    /* loop through looking for all possible connect items that might work */
336    DLINK_FOREACH(ptr, server_items.head)
# Line 416 | Line 366 | check_server(const char *name, struct Cl
366    attach_conf(client_p, server_conf);
367  
368  
369 <  if (server_conf != NULL)
369 >  if (server_conf)
370    {
371      struct sockaddr_in *v4;
372   #ifdef IPV6
# Line 456 | Line 406 | check_server(const char *name, struct Cl
406   void
407   add_capability(const char *capab_name, int cap_flag, int add_to_default)
408   {
409 <  struct Capability *cap = MyMalloc(sizeof(*cap));
409 >  struct Capability *cap = MyCalloc(sizeof(*cap));
410  
411    cap->name = xstrdup(capab_name);
412    cap->cap = cap_flag;
# Line 475 | Line 425 | add_capability(const char *capab_name, i
425   int
426   delete_capability(const char *capab_name)
427   {
428 <  dlink_node *ptr;
479 <  dlink_node *next_ptr;
480 <  struct Capability *cap;
428 >  dlink_node *ptr = NULL, *ptr_next = NULL;
429  
430 <  DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
430 >  DLINK_FOREACH_SAFE(ptr, ptr_next, cap_list.head)
431    {
432 <    cap = ptr->data;
432 >    struct Capability *cap = ptr->data;
433  
434 <    if (cap->cap != 0)
434 >    if (cap->cap)
435      {
436 <      if (irccmp(cap->name, capab_name) == 0)
436 >      if (!irccmp(cap->name, capab_name))
437        {
438          default_server_capabs &= ~(cap->cap);
439          dlinkDelete(ptr, &cap_list);
# Line 532 | Line 480 | find_capability(const char *capab)
480   void
481   send_capabilities(struct Client *client_p, int cap_can_send)
482   {
483 <  struct Capability *cap=NULL;
484 <  char msgbuf[IRCD_BUFSIZE];
537 <  char *t;
538 <  int tl;
539 <  dlink_node *ptr;
540 <
541 <  t = msgbuf;
483 >  char buf[IRCD_BUFSIZE] = "";
484 >  const dlink_node *ptr = NULL;
485  
486    DLINK_FOREACH(ptr, cap_list.head)
487    {
488 <    cap = ptr->data;
488 >    const struct Capability *cap = ptr->data;
489  
490      if (cap->cap & (cap_can_send|default_server_capabs))
491      {
492 <      tl = sprintf(t, "%s ", cap->name);
493 <      t += tl;
492 >      strlcat(buf, cap->name, sizeof(buf));
493 >      if (ptr->next)
494 >        strlcat(buf, " ", sizeof(buf));
495      }
496    }
497  
498 <  *(t - 1) = '\0';
555 <  sendto_one(client_p, "CAPAB :%s", msgbuf);
556 < }
557 <
558 < /* sendnick_TS()
559 < *
560 < * inputs       - client (server) to send nick towards
561 < *          - client to send nick for
562 < * output       - NONE
563 < * side effects - NICK message is sent towards given client_p
564 < */
565 < void
566 < sendnick_TS(struct Client *client_p, struct Client *target_p)
567 < {
568 <  char ubuf[IRCD_BUFSIZE];
569 <
570 <  if (!IsClient(target_p))
571 <    return;
572 <
573 <  send_umode(NULL, target_p, 0, SEND_UMODES, ubuf);
574 <
575 <  if (ubuf[0] == '\0')
576 <  {
577 <    ubuf[0] = '+';
578 <    ubuf[1] = '\0';
579 <  }
580 <
581 <  if (IsCapable(client_p, CAP_SVS))
582 <  {
583 <    if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
584 <      sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s %s :%s",
585 <                 target_p->servptr->id,
586 <                 target_p->name, target_p->hopcount + 1,
587 <                 (unsigned long) target_p->tsinfo,
588 <                 ubuf, target_p->username, target_p->host,
589 <                 (MyClient(target_p) && IsIPSpoof(target_p)) ?
590 <                 "0" : target_p->sockhost, target_p->id,
591 <                 target_p->svid, target_p->info);
592 <    else
593 <      sendto_one(client_p, "NICK %s %d %lu %s %s %s %s %s :%s",
594 <                 target_p->name, target_p->hopcount + 1,
595 <                 (unsigned long) target_p->tsinfo,
596 <                 ubuf, target_p->username, target_p->host,
597 <                 target_p->servptr->name, target_p->svid,
598 <                 target_p->info);
599 <  }
600 <  else
601 <  {
602 <    if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
603 <      sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
604 <                 target_p->servptr->id,
605 <                 target_p->name, target_p->hopcount + 1,
606 <                 (unsigned long) target_p->tsinfo,
607 <                 ubuf, target_p->username, target_p->host,
608 <                 (MyClient(target_p) && IsIPSpoof(target_p)) ?
609 <                 "0" : target_p->sockhost, target_p->id, target_p->info);
610 <    else
611 <      sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
612 <                 target_p->name, target_p->hopcount + 1,
613 <                 (unsigned long) target_p->tsinfo,
614 <                 ubuf, target_p->username, target_p->host,
615 <                 target_p->servptr->name, target_p->info);
616 <  }
617 <
618 <  if (!EmptyString(target_p->certfp))
619 <    sendto_one(client_p, ":%s CERTFP %s",
620 <               ID_or_name(target_p, client_p), target_p->certfp);
621 <
622 <  if (target_p->away[0])
623 <    sendto_one(client_p, ":%s AWAY :%s", ID_or_name(target_p, client_p),
624 <               target_p->away);
625 <
498 >  sendto_one(client_p, "CAPAB :%s", buf);
499   }
500  
501   /*
# Line 665 | Line 538 | struct Server *
538   make_server(struct Client *client_p)
539   {
540    if (client_p->serv == NULL)
541 <    client_p->serv = MyMalloc(sizeof(struct Server));
541 >    client_p->serv = MyCalloc(sizeof(struct Server));
542  
543    return client_p->serv;
544   }
545  
673 /* server_estab()
674 *
675 * inputs       - pointer to a struct Client
676 * output       -
677 * side effects -
678 */
679 void
680 server_estab(struct Client *client_p)
681 {
682  struct Client *target_p;
683  struct MaskItem *conf = NULL;
684  char *host;
685  const char *inpath;
686  static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
687  dlink_node *ptr;
688 #ifdef HAVE_LIBCRYPTO
689  const COMP_METHOD *compression = NULL, *expansion = NULL;
690 #endif
691
692  assert(client_p != NULL);
693
694  strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
695
696  inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
697  host   = client_p->name;
698
699  if ((conf = find_conf_name(&client_p->localClient->confs, host, CONF_SERVER))
700      == NULL)
701  {
702    /* This shouldn't happen, better tell the ops... -A1kmm */
703    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
704                         "Warning: Lost connect{} block "
705                         "for server %s(this shouldn't happen)!", host);
706    exit_client(client_p, &me, "Lost connect{} block!");
707    return;
708  }
709
710  MyFree(client_p->localClient->passwd);
711  client_p->localClient->passwd = NULL;
712
713  /* Its got identd, since its a server */
714  SetGotId(client_p);
715
716  /* If there is something in the serv_list, it might be this
717   * connecting server..
718   */
719  if (!ServerInfo.hub && serv_list.head)
720  {
721    if (client_p != serv_list.head->data || serv_list.head->next)
722    {
723      ++ServerStats.is_ref;
724      sendto_one(client_p, "ERROR :I'm a leaf not a hub");
725      exit_client(client_p, &me, "I'm a leaf");
726      return;
727    }
728  }
729
730  if (IsUnknown(client_p))
731  {
732    sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
733
734    send_capabilities(client_p, 0);
735
736    sendto_one(client_p, "SERVER %s 1 :%s%s",
737               me.name, ConfigServerHide.hidden ? "(H) " : "", me.info);
738  }
739
740  sendto_one(client_p, "SVINFO %d %d 0 :%lu", TS_CURRENT, TS_MIN,
741             (unsigned long)CurrentTime);
742
743  /* assumption here is if they passed the correct TS version, they also passed an SID */
744  if (IsCapable(client_p, CAP_TS6))
745    hash_add_id(client_p);
746
747  /* XXX Does this ever happen? I don't think so -db */
748  detach_conf(client_p, CONF_OPER);
749
750  /* *WARNING*
751  **    In the following code in place of plain server's
752  **    name we send what is returned by get_client_name
753  **    which may add the "sockhost" after the name. It's
754  **    *very* *important* that there is a SPACE between
755  **    the name and sockhost (if present). The receiving
756  **    server will start the information field from this
757  **    first blank and thus puts the sockhost into info.
758  **    ...a bit tricky, but you have been warned, besides
759  **    code is more neat this way...  --msa
760  */
761  client_p->servptr = &me;
762
763  if (IsClosing(client_p))
764    return;
765
766  SetServer(client_p);
767
768  /* Update the capability combination usage counts. -A1kmm */
769  set_chcap_usage_counts(client_p);
770
771  /* Some day, all these lists will be consolidated *sigh* */
772  dlinkAdd(client_p, &client_p->lnode, &me.serv->server_list);
773
774  assert(dlinkFind(&unknown_list, client_p));
775
776  dlink_move_node(&client_p->localClient->lclient_node,
777                  &unknown_list, &serv_list);
778
779  Count.myserver++;
780
781  dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
782  hash_add_client(client_p);
783
784  /* doesnt duplicate client_p->serv if allocated this struct already */
785  make_server(client_p);
786
787  /* fixing eob timings.. -gnp */
788  client_p->localClient->firsttime = CurrentTime;
789
790  if (find_matching_name_conf(CONF_SERVICE, client_p->name, NULL, NULL, 0))
791    AddFlag(client_p, FLAGS_SERVICE);
792
793  /* Show the real host/IP to admins */
794 #ifdef HAVE_LIBCRYPTO
795  if (client_p->localClient->fd.ssl)
796  {
797    compression = SSL_get_current_compression(client_p->localClient->fd.ssl);
798    expansion   = SSL_get_current_expansion(client_p->localClient->fd.ssl);
799
800    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
801                         "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
802                         inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
803                         compression ? SSL_COMP_get_name(compression) : "NONE",
804                         expansion ? SSL_COMP_get_name(expansion) : "NONE",
805                         show_capabilities(client_p));
806    /* Now show the masked hostname/IP to opers */
807    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
808                         "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
809                         inpath, ssl_get_cipher(client_p->localClient->fd.ssl),
810                         compression ? SSL_COMP_get_name(compression) : "NONE",
811                         expansion ? SSL_COMP_get_name(expansion) : "NONE",
812                         show_capabilities(client_p));
813    ilog(LOG_TYPE_IRCD, "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
814         inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
815         compression ? SSL_COMP_get_name(compression) : "NONE",
816         expansion ? SSL_COMP_get_name(expansion) : "NONE",
817         show_capabilities(client_p));
818  }
819  else
820 #endif
821  {
822    sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
823                         "Link with %s established: (Capabilities: %s)",
824                         inpath_ip, show_capabilities(client_p));
825    /* Now show the masked hostname/IP to opers */
826    sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
827                         "Link with %s established: (Capabilities: %s)",
828                         inpath, show_capabilities(client_p));
829    ilog(LOG_TYPE_IRCD, "Link with %s established: (Capabilities: %s)",
830         inpath_ip, show_capabilities(client_p));
831  }
832
833  fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
834
835  /* Old sendto_serv_but_one() call removed because we now
836  ** need to send different names to different servers
837  ** (domain name matching) Send new server to other servers.
838  */
839  DLINK_FOREACH(ptr, serv_list.head)
840  {
841    target_p = ptr->data;
842
843    if (target_p == client_p)
844      continue;
845
846    if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
847      sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
848                 me.id, client_p->name, client_p->id,
849                 IsHidden(client_p) ? "(H) " : "",
850                 client_p->info);
851    else
852      sendto_one(target_p,":%s SERVER %s 2 :%s%s",
853                 me.name, client_p->name,
854                 IsHidden(client_p) ? "(H) " : "",
855                 client_p->info);
856  }
857
858  /*
859   * Pass on my client information to the new server
860   *
861   * First, pass only servers (idea is that if the link gets
862   * cancelled beacause the server was already there,
863   * there are no NICK's to be cancelled...). Of course,
864   * if cancellation occurs, all this info is sent anyway,
865   * and I guess the link dies when a read is attempted...? --msa
866   *
867   * Note: Link cancellation to occur at this point means
868   * that at least two servers from my fragment are building
869   * up connection this other fragment at the same time, it's
870   * a race condition, not the normal way of operation...
871   *
872   * ALSO NOTE: using the get_client_name for server names--
873   *    see previous *WARNING*!!! (Also, original inpath
874   *    is destroyed...)
875   */
876
877  DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
878  {
879    target_p = ptr->data;
880
881    /* target_p->from == target_p for target_p == client_p */
882    if (IsMe(target_p) || target_p->from == client_p)
883      continue;
884
885    if (IsCapable(client_p, CAP_TS6))
886    {
887      if (HasID(target_p))
888        sendto_one(client_p, ":%s SID %s %d %s :%s%s",
889                   ID(target_p->servptr), target_p->name, target_p->hopcount+1,
890                   target_p->id, IsHidden(target_p) ? "(H) " : "",
891                   target_p->info);
892      else  /* introducing non-ts6 server */
893        sendto_one(client_p, ":%s SERVER %s %d :%s%s",
894                   ID(target_p->servptr), target_p->name, target_p->hopcount+1,
895                   IsHidden(target_p) ? "(H) " : "", target_p->info);
896    }
897    else
898      sendto_one(client_p, ":%s SERVER %s %d :%s%s",
899                 target_p->servptr->name, target_p->name, target_p->hopcount+1,
900                 IsHidden(target_p) ? "(H) " : "", target_p->info);
901
902    if (HasFlag(target_p, FLAGS_EOB))
903      sendto_one(client_p, ":%s EOB", ID_or_name(target_p, client_p));
904  }
905
906  server_burst(client_p);
907 }
908
909 /* server_burst()
910 *
911 * inputs       - struct Client pointer server
912 *              -
913 * output       - none
914 * side effects - send a server burst
915 * bugs         - still too long
916 */
917 static void
918 server_burst(struct Client *client_p)
919 {
920  /* Send it in the shortened format with the TS, if
921  ** it's a TS server; walk the list of channels, sending
922  ** all the nicks that haven't been sent yet for each
923  ** channel, then send the channel itself -- it's less
924  ** obvious than sending all nicks first, but on the
925  ** receiving side memory will be allocated more nicely
926  ** saving a few seconds in the handling of a split
927  ** -orabidoo
928  */
929
930  burst_all(client_p);
931
932  /* EOB stuff is now in burst_all */
933  /* Always send a PING after connect burst is done */
934  sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
935 }
936
937 /* burst_all()
938 *
939 * inputs       - pointer to server to send burst to
940 * output       - NONE
941 * side effects - complete burst of channels/nicks is sent to client_p
942 */
943 static void
944 burst_all(struct Client *client_p)
945 {
946  dlink_node *ptr = NULL;
947
948  DLINK_FOREACH(ptr, global_channel_list.head)
949  {
950    struct Channel *chptr = ptr->data;
951
952    if (dlink_list_length(&chptr->members) != 0)
953    {
954      burst_members(client_p, chptr);
955      send_channel_modes(client_p, chptr);
956
957      if (IsCapable(client_p, CAP_TBURST))
958        send_tb(client_p, chptr);
959    }
960  }
961
962  /* also send out those that are not on any channel
963   */
964  DLINK_FOREACH(ptr, global_client_list.head)
965  {
966    struct Client *target_p = ptr->data;
967
968    if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
969      sendnick_TS(client_p, target_p);
970
971    DelFlag(target_p, FLAGS_BURSTED);
972  }
973
974  if (IsCapable(client_p, CAP_EOB))
975    sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
976 }
977
978 /*
979 * send_tb
980 *
981 * inputs       - pointer to Client
982 *              - pointer to channel
983 * output       - NONE
984 * side effects - Called on a server burst when
985 *                server is CAP_TBURST capable
986 */
987 static void
988 send_tb(struct Client *client_p, struct Channel *chptr)
989 {
990  /*
991   * We may also send an empty topic here, but only if topic_time isn't 0,
992   * i.e. if we had a topic that got unset.  This is required for syncing
993   * topics properly.
994   *
995   * Imagine the following scenario: Our downlink introduces a channel
996   * to us with a TS that is equal to ours, but the channel topic on
997   * their side got unset while the servers were in splitmode, which means
998   * their 'topic' is newer.  They simply wanted to unset it, so we have to
999   * deal with it in a more sophisticated fashion instead of just resetting
1000   * it to their old topic they had before.  Read m_tburst.c:ms_tburst
1001   * for further information   -Michael
1002   */
1003  if (chptr->topic_time != 0)
1004    sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
1005               ID_or_name(&me, client_p),
1006               (unsigned long)chptr->channelts, chptr->chname,
1007               (unsigned long)chptr->topic_time,
1008               chptr->topic_info,
1009               chptr->topic);
1010 }
1011
1012 /* burst_members()
1013 *
1014 * inputs       - pointer to server to send members to
1015 *              - dlink_list pointer to membership list to send
1016 * output       - NONE
1017 * side effects -
1018 */
1019 static void
1020 burst_members(struct Client *client_p, struct Channel *chptr)
1021 {
1022  struct Client *target_p;
1023  struct Membership *ms;
1024  dlink_node *ptr;
1025
1026  DLINK_FOREACH(ptr, chptr->members.head)
1027  {
1028    ms       = ptr->data;
1029    target_p = ms->client_p;
1030
1031    if (!HasFlag(target_p, FLAGS_BURSTED))
1032    {
1033      AddFlag(target_p, FLAGS_BURSTED);
1034
1035      if (target_p->from != client_p)
1036        sendnick_TS(client_p, target_p);
1037    }
1038  }
1039 }
1040
546   /* New server connection code
547   * Based upon the stuff floating about in s_bsd.c
548   *   -- adrian
# Line 1062 | Line 567 | burst_members(struct Client *client_p, s
567   int
568   serv_connect(struct MaskItem *conf, struct Client *by)
569   {
570 <  struct Client *client_p;
571 <  char buf[HOSTIPLEN + 1];
570 >  struct Client *client_p = NULL;
571 >  char buf[HOSTIPLEN + 1] = "";
572  
573    /* conversion structs */
574    struct sockaddr_in *v4;
1070  /* Make sure conf is useful */
1071  assert(conf != NULL);
575  
576 +  /* Make sure conf is useful */
577 +  assert(conf);
578  
579    getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
580                buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
# Line 1082 | Line 587 | serv_connect(struct MaskItem *conf, stru
587      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
588                           "Error connecting to %s: DNS lookup for connect{} in progress.",
589                           conf->name);
590 <    return (0);
590 >    return 0;
591    }
592  
593    if (conf->dns_failed)
# Line 1090 | Line 595 | serv_connect(struct MaskItem *conf, stru
595      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
596                           "Error connecting to %s: DNS lookup for connect{} failed.",
597                           conf->name);
598 <    return (0);
598 >    return 0;
599    }
600  
601    /* Make sure this server isn't already connected
602     * Note: conf should ALWAYS be a valid C: line
603     */
604 <  if ((client_p = hash_find_server(conf->name)) != NULL)
604 >  if ((client_p = hash_find_server(conf->name)))
605    {
606      sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
607                           "Server %s already present from %s",
# Line 1105 | Line 610 | serv_connect(struct MaskItem *conf, stru
610                           "Server %s already present from %s",
611                           conf->name, get_client_name(client_p, MASK_IP));
612      if (by && IsClient(by) && !MyClient(by))
613 <      sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
614 <                 me.name, by->name, conf->name,
1110 <                 get_client_name(client_p, MASK_IP));
613 >      sendto_one_notice(by, &me, ":Server %s already present from %s",
614 >                        conf->name, get_client_name(client_p, MASK_IP));
615      return 0;
616    }
617  
# Line 1122 | Line 626 | serv_connect(struct MaskItem *conf, stru
626    strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
627  
628    /* create a socket for the server connection */
629 <  if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family,
1126 <                SOCK_STREAM, 0, NULL) < 0)
629 >  if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0)
630    {
631      /* Eek, failure to create the socket */
632 <    report_error(L_ALL, "opening stream socket to %s: %s",
633 <                 conf->name, errno);
632 >    report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
633 >
634      SetDead(client_p);
635 <    exit_client(client_p, &me, "Connection failed");
635 >    exit_client(client_p, "Connection failed");
636      return 0;
637    }
638  
# Line 1145 | Line 648 | serv_connect(struct MaskItem *conf, stru
648                           "Host %s is not enabled for connecting: no connect{} block",
649                           conf->name);
650      if (by && IsClient(by) && !MyClient(by))
651 <      sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
652 <                 me.name, by->name, client_p->name);
651 >      sendto_one_notice(by, &me, ":Connect to host %s failed.", client_p->name);
652 >
653      SetDead(client_p);
654 <    exit_client(client_p, client_p, "Connection failed");
654 >    exit_client(client_p, "Connection failed");
655      return 0;
656    }
657  
# Line 1166 | Line 669 | serv_connect(struct MaskItem *conf, stru
669      strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
670  
671    SetConnecting(client_p);
1169  dlinkAdd(client_p, &client_p->node, &global_client_list);
1170  /* from def_fam */
672    client_p->localClient->aftype = conf->aftype;
673  
674    /* Now, initiate the connection */
# Line 1178 | Line 679 | serv_connect(struct MaskItem *conf, stru
679    {
680      case AF_INET:
681        v4 = (struct sockaddr_in*)&conf->bind;
682 <      if (v4->sin_addr.s_addr != 0)
682 >      if (v4->sin_addr.s_addr)
683        {
684          struct irc_ssaddr ipn;
685          memset(&ipn, 0, sizeof(struct irc_ssaddr));
# Line 1218 | Line 719 | serv_connect(struct MaskItem *conf, stru
719          v6conf = (struct sockaddr_in6 *)&conf->bind;
720          v6 = (struct sockaddr_in6 *)&ipn;
721  
722 <        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)) != 0)
722 >        if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
723          {
724            memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
725            ipn.ss.ss_family = AF_INET6;
# Line 1266 | Line 767 | finish_ssl_server_handshake(struct Clien
767      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
768                           "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
769  
770 <    exit_client(client_p, &me, "Lost connect{} block");
770 >    exit_client(client_p, "Lost connect{} block");
771      return;
772    }
773  
# Line 1321 | Line 822 | ssl_server_handshake(fde_t *fd, struct C
822          sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
823                               "Error connecting to %s: %s", client_p->name,
824                               sslerr ? sslerr : "unknown SSL error");
825 <        exit_client(client_p, client_p, "Error during SSL handshake");
825 >        exit_client(client_p, "Error during SSL handshake");
826          return;
827        }
828      }
# Line 1330 | Line 831 | ssl_server_handshake(fde_t *fd, struct C
831    if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
832    {
833      int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
834 <    char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
835 <    unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
834 >    char buf[EVP_MAX_MD_SIZE * 2 + 1] = "";
835 >    unsigned char md[EVP_MAX_MD_SIZE] = "";
836  
837      if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
838          res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
839          res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
840      {
841 <      unsigned int i = 0, n = 0;
841 >      unsigned int n = 0;
842  
843 <      if (X509_digest(cert, EVP_sha256(), md, &n))
843 >      if (X509_digest(cert, ServerInfo.message_digest_algorithm, md, &n))
844        {
845 <        for (; i < n; ++i)
1345 <          snprintf(buf + 2 * i, 3, "%02X", md[i]);
845 >        binary_to_hex(md, buf, n);
846          client_p->certfp = xstrdup(buf);
847        }
848      }
# Line 1363 | Line 863 | ssl_connect_init(struct Client *client_p
863      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
864           ERR_error_string(ERR_get_error(), NULL));
865      SetDead(client_p);
866 <    exit_client(client_p, client_p, "SSL_new failed");
866 >    exit_client(client_p, "SSL_new failed");
867      return;
868    }
869  
# Line 1391 | Line 891 | serv_connect_callback(fde_t *fd, int sta
891    struct MaskItem *conf = NULL;
892  
893    /* First, make sure its a real client! */
894 <  assert(client_p != NULL);
894 >  assert(client_p);
895    assert(&client_p->localClient->fd == fd);
896  
897    /* Next, for backward purposes, record the ip of the server */
898    memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
899           sizeof(struct irc_ssaddr));
900 +
901    /* Check the status */
902    if (status != COMM_OK)
903    {
# Line 1434 | Line 935 | serv_connect_callback(fde_t *fd, int sta
935      sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
936                           "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
937  
938 <    exit_client(client_p, &me, "Lost connect{} block");
938 >    exit_client(client_p, "Lost connect{} block");
939      return;
940    }
941  

Diff Legend

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