/[svn]/ircd-hybrid/src/s_serv.c
ViewVC logotype

Contents of /ircd-hybrid/src/s_serv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Sun Dec 25 10:42:33 2005 UTC (14 years, 8 months ago) by michael
File MIME type: text/x-chdr
File size: 69887 byte(s)
- Fixed bug where we wouldn't send the HUB capability token when
  using cryptlinks

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * s_serv.c: Server related functions.
4 *
5 * Copyright (C) 2005 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #ifdef HAVE_LIBCRYPTO
27 #include <openssl/rsa.h>
28 #include "rsa.h"
29 #endif
30 #include "channel.h"
31 #include "channel_mode.h"
32 #include "client.h"
33 #include "common.h"
34 #include "hash.h"
35 #include "ircd.h"
36 #include "ircd_defs.h"
37 #include "numeric.h"
38 #include "packet.h"
39 #include "s_conf.h"
40 #include "s_serv.h"
41 #include "s_stats.h"
42 #include "s_user.h"
43 #include "send.h"
44 #include "channel.h" /* chcap_usage_counts stuff...*/
45
46 #define MIN_CONN_FREQ 300
47
48 struct Client *uplink = NULL;
49
50 static dlink_list cap_list = { NULL, NULL, 0 };
51 static unsigned long freeMask;
52 static void server_burst(struct Client *);
53 static int fork_server(struct Client *);
54 static void burst_all(struct Client *);
55 static void cjoin_all(struct Client *);
56 static void send_tb(struct Client *, const struct Channel *);
57
58 static CNCB serv_connect_callback;
59
60 static void start_io(struct Client *);
61 static void burst_members(struct Client *, struct Channel *);
62 static void burst_ll_members(struct Client *, struct Channel *);
63 static void add_lazylinkchannel(struct Client *, struct Channel *);
64
65 static SlinkRplHnd slink_error;
66 static SlinkRplHnd slink_zipstats;
67
68
69 #ifdef HAVE_LIBCRYPTO
70 struct EncCapability CipherTable[] =
71 {
72 #ifdef HAVE_EVP_BF_CFB
73 { "BF/168", CAP_ENC_BF_168, 24, CIPHER_BF },
74 { "BF/128", CAP_ENC_BF_128, 16, CIPHER_BF },
75 #endif
76 #ifdef HAVE_EVP_CAST5_CFB
77 { "CAST/128", CAP_ENC_CAST_128, 16, CIPHER_CAST },
78 #endif
79 #ifdef HAVE_EVP_IDEA_CFB
80 { "IDEA/128", CAP_ENC_IDEA_128, 16, CIPHER_IDEA },
81 #endif
82 #ifdef HAVE_EVP_RC5_32_12_16_CFB
83 { "RC5.16/128", CAP_ENC_RC5_16_128, 16, CIPHER_RC5_16 },
84 { "RC5.12/128", CAP_ENC_RC5_12_128, 16, CIPHER_RC5_12 },
85 { "RC5.8/128", CAP_ENC_RC5_8_128, 16, CIPHER_RC5_8 },
86 #endif
87 #ifdef HAVE_EVP_DES_EDE3_CFB
88 { "3DES/168", CAP_ENC_3DES_168, 24, CIPHER_3DES },
89 #endif
90 #ifdef HAVE_EVP_DES_CFB
91 { "DES/56", CAP_ENC_DES_56, 8, CIPHER_DES },
92 #endif
93 { 0, 0, 0, 0 }
94 };
95 #endif
96
97 struct SlinkRplDef slinkrpltab[] = {
98 { SLINKRPL_ERROR, slink_error, SLINKRPL_FLAG_DATA },
99 { SLINKRPL_ZIPSTATS, slink_zipstats, SLINKRPL_FLAG_DATA },
100 { 0, 0, 0 },
101 };
102
103
104 void
105 slink_error(unsigned int rpl, unsigned int len, unsigned char *data,
106 struct Client *server_p)
107 {
108 assert(rpl == SLINKRPL_ERROR);
109 assert(len < 256);
110
111 data[len-1] = '\0';
112
113 sendto_realops_flags(UMODE_ALL, L_ALL, "SlinkError for %s: %s",
114 server_p->name, data);
115 /* XXX should this be exit_client? */
116 exit_client(server_p, &me, "servlink error -- terminating link");
117 }
118
119 /*
120 * slink_zipstats
121 *
122 * inputs - rpl reply
123 * len length of reply
124 * data pointer to data
125 * pointer to server
126 * output - none
127 * side effects - ziplink stats are calculated
128 */
129 void
130 slink_zipstats(unsigned int rpl, unsigned int len, unsigned char *data,
131 struct Client *server_p)
132 {
133 struct ZipStats zipstats;
134 unsigned long in = 0, in_wire = 0, out = 0, out_wire = 0;
135 int i = 0;
136
137 assert(rpl == SLINKRPL_ZIPSTATS);
138 assert(len == 16);
139 assert(IsCapable(server_p, CAP_ZIP));
140
141 /* Yes, it needs to be done this way, no we cannot let the compiler
142 * work with the pointer to the structure. This works around a GCC
143 * bug on SPARC that affects all versions at the time of this writing.
144 * I will feed you to the creatures living in RMS's beard if you do
145 * not leave this as is, without being sure that you are not causing
146 * regression for most of our installed SPARC base.
147 * -jmallett, 04/27/2002
148 */
149 memcpy(&zipstats, &server_p->localClient->zipstats, sizeof(struct ZipStats));
150
151 in |= (data[i++] << 24);
152 in |= (data[i++] << 16);
153 in |= (data[i++] << 8);
154 in |= (data[i++] );
155
156 in_wire |= (data[i++] << 24);
157 in_wire |= (data[i++] << 16);
158 in_wire |= (data[i++] << 8);
159 in_wire |= (data[i++] );
160
161 out |= (data[i++] << 24);
162 out |= (data[i++] << 16);
163 out |= (data[i++] << 8);
164 out |= (data[i++] );
165
166 out_wire |= (data[i++] << 24);
167 out_wire |= (data[i++] << 16);
168 out_wire |= (data[i++] << 8);
169 out_wire |= (data[i++] );
170
171 /* This macro adds b to a if a plus b is not an overflow, and sets the
172 * value of a to b if it is.
173 * Add and Set if No Overflow.
174 */
175 #define ASNO(a, b) a = (a + b >= a ? a + b : b)
176
177 ASNO(zipstats.in, in);
178 ASNO(zipstats.out, out);
179 ASNO(zipstats.in_wire, in_wire);
180 ASNO(zipstats.out_wire, out_wire);
181
182 if (zipstats.in > 0)
183 zipstats.in_ratio = (((double)(zipstats.in - zipstats.in_wire) /
184 (double)zipstats.in) * 100.00);
185 else
186 zipstats.in_ratio = 0;
187
188 if (zipstats.out > 0)
189 zipstats.out_ratio = (((double)(zipstats.out - zipstats.out_wire) /
190 (double)zipstats.out) * 100.00);
191 else
192 zipstats.out_ratio = 0;
193
194 memcpy(&server_p->localClient->zipstats, &zipstats, sizeof (struct ZipStats));
195 }
196
197 void
198 collect_zipstats(void *unused)
199 {
200 dlink_node *ptr;
201 struct Client *target_p;
202
203 DLINK_FOREACH(ptr, serv_list.head)
204 {
205 target_p = ptr->data;
206
207 if (IsCapable(target_p, CAP_ZIP))
208 {
209 /* only bother if we haven't already got something queued... */
210 if (target_p->localClient->slinkq == NULL)
211 {
212 target_p->localClient->slinkq = MyMalloc(1); /* sigh.. */
213 target_p->localClient->slinkq[0] = SLINKCMD_ZIPSTATS;
214 target_p->localClient->slinkq_ofs = 0;
215 target_p->localClient->slinkq_len = 1;
216 send_queued_slink_write(target_p);
217 }
218 }
219 }
220 }
221
222 #ifdef HAVE_LIBCRYPTO
223 struct EncCapability *
224 check_cipher(struct Client *client_p, struct AccessItem *aconf)
225 {
226 struct EncCapability *epref = NULL;
227
228 /* Use connect{} specific info if available */
229 if (aconf->cipher_preference)
230 epref = aconf->cipher_preference;
231 else if (ConfigFileEntry.default_cipher_preference)
232 epref = ConfigFileEntry.default_cipher_preference;
233
234 /* If the server supports the capability in hand, return the matching
235 * conf struct. Otherwise, return NULL (an error).
236 */
237 if (epref && IsCapableEnc(client_p, epref->cap))
238 return epref;
239
240 return NULL;
241 }
242 #endif /* HAVE_LIBCRYPTO */
243
244 /* my_name_for_link()
245 * return wildcard name of my server name
246 * according to given config entry --Jto
247 */
248 const char *
249 my_name_for_link(struct AccessItem *aconf)
250 {
251 if (aconf->fakename != NULL)
252 return(aconf->fakename);
253 else
254 return(me.name);
255 }
256
257 /*
258 * write_links_file
259 *
260 * inputs - void pointer which is not used
261 * output - NONE
262 * side effects - called from an event, write out list of linked servers
263 * but in no particular order.
264 */
265 void
266 write_links_file(void* notused)
267 {
268 MessageFileLine *next_mptr = 0;
269 MessageFileLine *mptr = 0;
270 MessageFileLine *currentMessageLine = 0;
271 MessageFileLine *newMessageLine = 0;
272 MessageFile *MessageFileptr;
273 const char *p;
274 FBFILE *file;
275 char buff[512];
276 dlink_node *ptr;
277
278 MessageFileptr = &ConfigFileEntry.linksfile;
279
280 if ((file = fbopen(MessageFileptr->fileName, "w")) == NULL)
281 return;
282
283 for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr)
284 {
285 next_mptr = mptr->next;
286 MyFree(mptr);
287 }
288
289 MessageFileptr->contentsOfFile = NULL;
290 currentMessageLine = NULL;
291
292 DLINK_FOREACH(ptr, global_serv_list.head)
293 {
294 size_t nbytes = 0;
295 struct Client *target_p = ptr->data;
296
297 /* skip ourselves, we send ourselves in /links */
298 if (IsMe(target_p))
299 continue;
300
301 /* skip hidden servers */
302 if (IsHidden(target_p) && !ConfigServerHide.disable_hidden)
303 continue;
304
305 if (target_p->info[0])
306 p = target_p->info;
307 else
308 p = "(Unknown Location)";
309
310 newMessageLine = MyMalloc(sizeof(MessageFileLine));
311
312 /* Attempt to format the file in such a way it follows the usual links output
313 * ie "servername uplink :hops info"
314 * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
315 * - madmax
316 */
317
318 /*
319 * For now, check this ircsprintf wont overflow - it shouldnt on a
320 * default config but it is configurable..
321 * This should be changed to an snprintf at some point, but I'm wanting to
322 * know if this is a cause of a bug - cryogen
323 */
324 assert(strlen(target_p->name) + strlen(me.name) + 6 + strlen(p) <=
325 MESSAGELINELEN);
326 ircsprintf(newMessageLine->line, "%s %s :1 %s",
327 target_p->name, me.name, p);
328 newMessageLine->next = NULL;
329
330 if (MessageFileptr->contentsOfFile)
331 {
332 if (currentMessageLine)
333 currentMessageLine->next = newMessageLine;
334 currentMessageLine = newMessageLine;
335 }
336 else
337 {
338 MessageFileptr->contentsOfFile = newMessageLine;
339 currentMessageLine = newMessageLine;
340 }
341
342 nbytes = ircsprintf(buff, "%s %s :1 %s\n", target_p->name, me.name, p);
343 fbputs(buff, file, nbytes);
344 }
345
346 fbclose(file);
347 }
348
349 /* hunt_server()
350 * Do the basic thing in delivering the message (command)
351 * across the relays to the specific server (server) for
352 * actions.
353 *
354 * Note: The command is a format string and *MUST* be
355 * of prefixed style (e.g. ":%s COMMAND %s ...").
356 * Command can have only max 8 parameters.
357 *
358 * server parv[server] is the parameter identifying the
359 * target server.
360 *
361 * *WARNING*
362 * parv[server] is replaced with the pointer to the
363 * real servername from the matched client (I'm lazy
364 * now --msa).
365 *
366 * returns: (see #defines)
367 */
368 int
369 hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
370 int server, int parc, char *parv[])
371 {
372 struct Client *target_p = NULL;
373 struct Client *target_tmp = NULL;
374 dlink_node *ptr;
375 int wilds;
376
377 /* Assume it's me, if no server
378 */
379 if (parc <= server || EmptyString(parv[server]) ||
380 match(me.name, parv[server]) ||
381 match(parv[server], me.name) ||
382 !strcmp(parv[server], me.id))
383 return(HUNTED_ISME);
384
385 /* These are to pickup matches that would cause the following
386 * message to go in the wrong direction while doing quick fast
387 * non-matching lookups.
388 */
389 if (MyClient(source_p))
390 target_p = find_client(parv[server]);
391 else
392 target_p = find_person(client_p, parv[server]);
393
394 if (target_p)
395 if (target_p->from == source_p->from && !MyConnect(target_p))
396 target_p = NULL;
397
398 if (target_p == NULL && (target_p = find_server(parv[server])))
399 if (target_p->from == source_p->from && !MyConnect(target_p))
400 target_p = NULL;
401
402 collapse(parv[server]);
403 wilds = (strchr(parv[server], '?') || strchr(parv[server], '*'));
404
405 /* Again, if there are no wild cards involved in the server
406 * name, use the hash lookup
407 */
408 if (target_p == NULL)
409 {
410 if (!wilds)
411 {
412 if (!(target_p = find_server(parv[server])))
413 {
414 sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
415 me.name, parv[0], parv[server]);
416 return(HUNTED_NOSUCH);
417 }
418 }
419 else
420 {
421 DLINK_FOREACH(ptr, global_client_list.head)
422 {
423 target_tmp = ptr->data;
424
425 if (match(parv[server], target_tmp->name))
426 {
427 if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
428 continue;
429 target_p = ptr->data;
430
431 if (IsRegistered(target_p) && (target_p != client_p))
432 break;
433 }
434 }
435 }
436 }
437
438 if (target_p != NULL)
439 {
440 if(!IsRegistered(target_p))
441 {
442 sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
443 me.name, parv[0], parv[server]);
444 return HUNTED_NOSUCH;
445 }
446
447 if (IsMe(target_p) || MyClient(target_p))
448 return HUNTED_ISME;
449
450 if (!match(target_p->name, parv[server]))
451 parv[server] = target_p->name;
452
453 /* Deal with lazylinks */
454 client_burst_if_needed(target_p, source_p);
455
456 /* This is a little kludgy but should work... */
457 if (IsClient(source_p) &&
458 ((MyConnect(target_p) && IsCapable(target_p, CAP_TS6)) ||
459 (!MyConnect(target_p) && IsCapable(target_p->from, CAP_TS6))))
460 parv[0] = ID(source_p);
461
462 sendto_one(target_p, command, parv[0],
463 parv[1], parv[2], parv[3], parv[4],
464 parv[5], parv[6], parv[7], parv[8]);
465 return(HUNTED_PASS);
466 }
467
468 sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
469 me.name, parv[0], parv[server]);
470 return(HUNTED_NOSUCH);
471 }
472
473 /* try_connections()
474 *
475 * inputs - void pointer which is not used
476 * output - NONE
477 * side effects -
478 * scan through configuration and try new connections.
479 * Returns the calendar time when the next call to this
480 * function should be made latest. (No harm done if this
481 * is called earlier or later...)
482 */
483 void
484 try_connections(void *unused)
485 {
486 dlink_node *ptr;
487 struct ConfItem *conf;
488 struct AccessItem *aconf;
489 struct ClassItem *cltmp;
490 int confrq;
491
492 /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
493 if (GlobalSetOptions.autoconn == 0)
494 return;
495
496 DLINK_FOREACH(ptr, server_items.head)
497 {
498 conf = ptr->data;
499 aconf = &conf->conf.AccessItem;
500
501 /* Also when already connecting! (update holdtimes) --SRB
502 */
503 if (!(aconf->status & CONF_SERVER) || aconf->port <= 0 ||
504 !(IsConfAllowAutoConn(aconf)))
505 continue;
506
507 cltmp = &((struct ConfItem *)aconf->class_ptr)->conf.ClassItem;
508
509 /* Skip this entry if the use of it is still on hold until
510 * future. Otherwise handle this entry (and set it on hold
511 * until next time). Will reset only hold times, if already
512 * made one successfull connection... [this algorithm is
513 * a bit fuzzy... -- msa >;) ]
514 */
515 if (aconf->hold > CurrentTime)
516 continue;
517
518 if (cltmp == NULL)
519 confrq = DEFAULT_CONNECTFREQUENCY;
520 else
521 {
522 confrq = ConFreq(cltmp);
523 if (confrq < MIN_CONN_FREQ )
524 confrq = MIN_CONN_FREQ;
525 }
526
527 aconf->hold = CurrentTime + confrq;
528
529 /* Found a CONNECT config with port specified, scan clients
530 * and see if this server is already connected?
531 */
532 if (find_server(conf->name) != NULL)
533 continue;
534
535 if (CurrUserCount(cltmp) < MaxTotal(cltmp))
536 {
537 /* Go to the end of the list, if not already last */
538 if (ptr->next != NULL)
539 {
540 dlinkDelete(ptr, &server_items);
541 dlinkAddTail(conf, &conf->node, &server_items);
542 }
543
544 if (find_servconn_in_progress(conf->name))
545 return;
546
547 /* We used to only print this if serv_connect() actually
548 * succeeded, but since comm_tcp_connect() can call the callback
549 * immediately if there is an error, we were getting error messages
550 * in the wrong order. SO, we just print out the activated line,
551 * and let serv_connect() / serv_connect_callback() print an
552 * error afterwards if it fails.
553 * -- adrian
554 */
555 if (ConfigServerHide.hide_server_ips)
556 sendto_realops_flags(UMODE_ALL, L_ALL, "Connection to %s activated.",
557 conf->name);
558 else
559 sendto_realops_flags(UMODE_ALL, L_ALL, "Connection to %s[%s] activated.",
560 conf->name, aconf->host);
561
562 serv_connect(aconf, NULL);
563 /* We connect only one at time... */
564 return;
565 }
566 }
567 }
568
569 /*
570 * check_server
571 *
572 * inputs - name
573 * - pointer to client struct
574 * - int flag for cryptlink
575 * output - return -'ve value for error
576 * side effects -
577 *
578 * Called from mr_server when a client claiming to be a server
579 * connects. check_server() verifies this client is or isn't a server and
580 * returns an error if it isn't a server, it otherwise attaches the appropriate
581 * server connect block to this client.
582 */
583 int
584 check_server(const char *name, struct Client *client_p, int cryptlink)
585 {
586 dlink_node *ptr;
587 struct ConfItem *conf = NULL;
588 struct ConfItem *server_conf = NULL;
589 struct AccessItem *server_aconf = NULL;
590 struct AccessItem *aconf = NULL;
591 int error = -1;
592
593 assert(client_p != NULL);
594
595 if (client_p == NULL)
596 return(error);
597
598 if (strlen(name) > HOSTLEN)
599 return(-4);
600
601 /* loop through looking for all possible connect items that might work */
602 DLINK_FOREACH(ptr, server_items.head)
603 {
604 conf = ptr->data;
605 aconf = &conf->conf.AccessItem;
606
607 if (!match(name, conf->name))
608 continue;
609
610 error = -3;
611
612 /* XXX: Fix me for IPv6 */
613 /* XXX sockhost is the IPv4 ip as a string */
614 if (match(aconf->host, client_p->host) ||
615 match(aconf->host, client_p->sockhost))
616 {
617 error = -2;
618 #ifdef HAVE_LIBCRYPTO
619 if (cryptlink && IsConfCryptLink(aconf))
620 {
621 if (aconf->rsa_public_key)
622 server_conf = conf;
623 }
624 else if (!(cryptlink || IsConfCryptLink(aconf)))
625 #endif /* HAVE_LIBCRYPTO */
626 {
627 /* A NULL password is as good as a bad one */
628 if (EmptyString(client_p->localClient->passwd))
629 return(-2);
630
631 /* code in s_conf.c should not have allowed this to be NULL */
632 if (aconf->passwd == NULL)
633 return(-2);
634
635 if (IsConfEncrypted(aconf))
636 {
637 if (strcmp(aconf->passwd,
638 (const char *)crypt(client_p->localClient->passwd,
639 aconf->passwd)) == 0)
640 server_conf = conf;
641 }
642 else
643 {
644 if (strcmp(aconf->passwd, client_p->localClient->passwd) == 0)
645 server_conf = conf;
646 }
647 }
648 }
649 }
650
651 if (server_conf == NULL)
652 return(error);
653
654 make_server(client_p);
655 attach_server_conf(client_p, server_conf);
656
657 /* Now find all leaf or hub config items for this server */
658 DLINK_FOREACH(ptr, hub_items.head)
659 {
660 conf = ptr->data;
661
662 if (match(name, conf->name) == 0)
663 continue;
664 attach_leaf_hub(client_p, conf);
665 }
666
667 DLINK_FOREACH(ptr, leaf_items.head)
668 {
669 conf = ptr->data;
670
671 if (match(name, conf->name) == 0)
672 continue;
673 attach_leaf_hub(client_p, conf);
674 }
675
676 server_aconf = &server_conf->conf.AccessItem;
677
678 if (!IsConfLazyLink(server_aconf))
679 ClearCap(client_p, CAP_LL);
680 #ifdef HAVE_LIBZ /* otherwise, clear it unconditionally */
681 if (!IsConfCompressed(server_aconf))
682 #endif
683 ClearCap(client_p, CAP_ZIP);
684 if (!IsConfCryptLink(server_aconf))
685 ClearCap(client_p, CAP_ENC);
686 if (!IsConfTopicBurst(server_aconf))
687 {
688 ClearCap(client_p, CAP_TB);
689 ClearCap(client_p, CAP_TBURST);
690 }
691
692 /* Don't unset CAP_HUB here even if the server isn't a hub,
693 * it only indicates if the server thinks its lazylinks are
694 * leafs or not.. if you unset it, bad things will happen
695 */
696 if (aconf != NULL)
697 {
698 struct sockaddr_in *v4;
699 #ifdef IPV6
700 struct sockaddr_in6 *v6;
701 #endif
702 switch (aconf->aftype)
703 {
704 #ifdef IPV6
705 case AF_INET6:
706 v6 = (struct sockaddr_in6 *)&aconf->ipnum;
707
708 if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
709 memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
710 break;
711 #endif
712 case AF_INET:
713 v4 = (struct sockaddr_in *)&aconf->ipnum;
714
715 if (v4->sin_addr.s_addr == INADDR_NONE)
716 memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
717 break;
718 }
719 }
720
721 return(0);
722 }
723
724 /* add_capability()
725 *
726 * inputs - string name of CAPAB
727 * - int flag of capability
728 * output - NONE
729 * side effects - Adds given capability name and bit mask to
730 * current supported capabilities. This allows
731 * modules to dynamically add or subtract their capability.
732 */
733 void
734 add_capability(const char *capab_name, int cap_flag, int add_to_default)
735 {
736 struct Capability *cap;
737
738 cap = (struct Capability *)MyMalloc(sizeof(*cap));
739 DupString(cap->name, capab_name);
740 cap->cap = cap_flag;
741 dlinkAdd(cap, &cap->node, &cap_list);
742 if (add_to_default)
743 default_server_capabs |= cap_flag;
744 }
745
746 /* delete_capability()
747 *
748 * inputs - string name of CAPAB
749 * output - NONE
750 * side effects - delete given capability from ones known.
751 */
752 int
753 delete_capability(const char *capab_name)
754 {
755 dlink_node *ptr;
756 dlink_node *next_ptr;
757 struct Capability *cap;
758
759 DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
760 {
761 cap = ptr->data;
762
763 if (cap->cap != 0)
764 {
765 if (irccmp(cap->name, capab_name) == 0)
766 {
767 default_server_capabs &= ~(cap->cap);
768 dlinkDelete(ptr, &cap_list);
769 MyFree(cap->name);
770 cap->name = NULL;
771 MyFree(cap);
772 }
773 }
774 }
775
776 return(0);
777 }
778
779 /*
780 * find_capability()
781 *
782 * inputs - string name of capab to find
783 * output - 0 if not found CAPAB otherwise
784 * side effects - none
785 */
786 int
787 find_capability(const char *capab)
788 {
789 dlink_node *ptr;
790 struct Capability *cap;
791
792 DLINK_FOREACH(ptr, cap_list.head)
793 {
794 cap = ptr->data;
795
796 if (cap->cap != 0)
797 {
798 if (irccmp(cap->name, capab) == 0)
799 return(cap->cap);
800 }
801 }
802 return(0);
803 }
804
805 /* send_capabilities()
806 *
807 * inputs - Client pointer to send to
808 * - Pointer to AccessItem (for crypt)
809 * - int flag of capabilities that this server can send
810 * - int flag of encryption capabilities
811 * output - NONE
812 * side effects - send the CAPAB line to a server -orabidoo
813 *
814 */
815 void
816 send_capabilities(struct Client *client_p, struct AccessItem *aconf,
817 int cap_can_send, int enc_can_send)
818 {
819 struct Capability *cap=NULL;
820 char msgbuf[IRCD_BUFSIZE];
821 char *t;
822 int tl;
823 dlink_node *ptr;
824 #ifdef HAVE_LIBCRYPTO
825 const struct EncCapability *epref = NULL;
826 char *capend;
827 int sent_cipher = 0;
828 #endif
829
830 t = msgbuf;
831
832 DLINK_FOREACH(ptr, cap_list.head)
833 {
834 cap = ptr->data;
835
836 if (cap->cap & (cap_can_send|default_server_capabs))
837 {
838 tl = ircsprintf(t, "%s ", cap->name);
839 t += tl;
840 }
841 }
842 #ifdef HAVE_LIBCRYPTO
843 if (enc_can_send)
844 {
845 capend = t;
846 strcpy(t, "ENC:");
847 t += 4;
848
849 /* use connect{} specific info if available */
850 if (aconf->cipher_preference)
851 epref = aconf->cipher_preference;
852 else if (ConfigFileEntry.default_cipher_preference)
853 epref = ConfigFileEntry.default_cipher_preference;
854
855 if (epref && (epref->cap & enc_can_send))
856 {
857 /* Leave the space -- it is removed later. */
858 tl = ircsprintf(t, "%s ", epref->name);
859 t += tl;
860 sent_cipher = 1;
861 }
862
863 if (!sent_cipher)
864 t = capend; /* truncate string before ENC:, below */
865 }
866 #endif
867 *(t - 1) = '\0';
868 sendto_one(client_p, "CAPAB :%s", msgbuf);
869 }
870
871 /* sendnick_TS()
872 *
873 * inputs - client (server) to send nick towards
874 * - client to send nick for
875 * output - NONE
876 * side effects - NICK message is sent towards given client_p
877 */
878 void
879 sendnick_TS(struct Client *client_p, struct Client *target_p)
880 {
881 static char ubuf[12];
882
883 if (!IsClient(target_p))
884 return;
885
886 send_umode(NULL, target_p, 0, IsOperHiddenAdmin(target_p) ?
887 SEND_UMODES & ~UMODE_ADMIN : SEND_UMODES, ubuf);
888
889 if (ubuf[0] == '\0')
890 {
891 ubuf[0] = '+';
892 ubuf[1] = '\0';
893 }
894
895 /* XXX Both of these need to have a :me.name or :mySID!?!?! */
896 if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
897 sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
898 target_p->servptr->id,
899 target_p->name, target_p->hopcount + 1,
900 (unsigned long) target_p->tsinfo,
901 ubuf, target_p->username, target_p->host,
902 ((MyClient(target_p)&&!IsIPSpoof(target_p))?target_p->sockhost:"0"),
903 target_p->id, target_p->info);
904 else
905 sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
906 target_p->name, target_p->hopcount + 1,
907 (unsigned long) target_p->tsinfo,
908 ubuf, target_p->username, target_p->host,
909 target_p->servptr->name, target_p->info);
910 if (IsConfAwayBurst(client_p->serv->sconf))
911 if (!EmptyString(target_p->away))
912 sendto_one(client_p, ":%s AWAY :%s", target_p->name,
913 target_p->away);
914
915 }
916
917 /* client_burst_if_needed()
918 *
919 * inputs - pointer to server
920 * - pointer to client to add
921 * output - NONE
922 * side effects - If this client is not known by this lazyleaf, send it
923 */
924 void
925 client_burst_if_needed(struct Client *client_p, struct Client *target_p)
926 {
927 if (!ServerInfo.hub)
928 return;
929 if (!MyConnect(client_p))
930 return;
931 if (!IsCapable(client_p,CAP_LL))
932 return;
933
934 if ((target_p->lazyLinkClientExists & client_p->localClient->serverMask) == 0)
935 {
936 sendnick_TS(client_p, target_p);
937 add_lazylinkclient(client_p,target_p);
938 }
939 }
940
941 /*
942 * show_capabilities - show current server capabilities
943 *
944 * inputs - pointer to a struct Client
945 * output - pointer to static string
946 * side effects - build up string representing capabilities of server listed
947 */
948 const char *
949 show_capabilities(struct Client *target_p)
950 {
951 static char msgbuf[IRCD_BUFSIZE];
952 char *t = msgbuf;
953 dlink_node *ptr;
954
955 t += ircsprintf(msgbuf, "TS ");
956
957 DLINK_FOREACH(ptr, cap_list.head)
958 {
959 const struct Capability *cap = ptr->data;
960
961 if (IsCapable(target_p, cap->cap))
962 t += ircsprintf(t, "%s ", cap->name);
963 }
964 #ifdef HAVE_LIBCRYPTO
965 if (IsCapable(target_p, CAP_ENC) &&
966 target_p->localClient->in_cipher &&
967 target_p->localClient->out_cipher)
968 t += ircsprintf(t, "ENC:%s ",
969 target_p->localClient->in_cipher->name);
970 #endif
971 *(t - 1) = '\0';
972
973 return msgbuf;
974 }
975
976 /* make_server()
977 *
978 * inputs - pointer to client struct
979 * output - pointer to struct Server
980 * side effects - add's an Server information block to a client
981 * if it was not previously allocated.
982 */
983 struct Server *
984 make_server(struct Client *client_p)
985 {
986 if (client_p->serv == NULL)
987 {
988 client_p->serv = MyMalloc(sizeof(struct Server));
989 client_p->serv->dep_servers = 1;
990 }
991
992 return client_p->serv;
993 }
994
995 /* server_estab()
996 *
997 * inputs - pointer to a struct Client
998 * output -
999 * side effects -
1000 */
1001 void
1002 server_estab(struct Client *client_p)
1003 {
1004 struct Client *target_p;
1005 struct ConfItem *conf;
1006 struct AccessItem *aconf=NULL;
1007 char *host;
1008 const char *inpath;
1009 static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
1010 dlink_node *m;
1011 dlink_node *ptr;
1012
1013 assert(client_p != NULL);
1014
1015 strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
1016
1017 inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
1018 host = client_p->name;
1019
1020 if ((conf = client_p->serv->sconf) == NULL)
1021 {
1022 /* This shouldn't happen, better tell the ops... -A1kmm */
1023 sendto_realops_flags(UMODE_ALL, L_ALL, "Warning: Lost connect{} block "
1024 "for server %s(this shouldn't happen)!", host);
1025 exit_client(client_p, &me, "Lost connect{} block!");
1026 return;
1027 }
1028
1029 /* XXX */
1030 aconf = &conf->conf.AccessItem;
1031 MyFree(client_p->localClient->passwd);
1032 client_p->localClient->passwd = NULL;
1033
1034 /* Its got identd, since its a server */
1035 SetGotId(client_p);
1036
1037 /* If there is something in the serv_list, it might be this
1038 * connecting server..
1039 */
1040 if (!ServerInfo.hub && serv_list.head)
1041 {
1042 if (client_p != serv_list.head->data || serv_list.head->next)
1043 {
1044 ServerStats->is_ref++;
1045 sendto_one(client_p, "ERROR :I'm a leaf not a hub");
1046 exit_client(client_p, &me, "I'm a leaf");
1047 return;
1048 }
1049 }
1050
1051 if (IsUnknown(client_p) && !IsConfCryptLink(aconf))
1052 {
1053 /* jdc -- 1. Use EmptyString(), not [0] index reference.
1054 * 2. Check aconf->spasswd, not aconf->passwd.
1055 */
1056 if (!EmptyString(aconf->spasswd))
1057 {
1058 /* only send ts6 format PASS if we have ts6 enabled */
1059 if (me.id[0] != '\0') /* Send TS 6 form only if id */
1060 sendto_one(client_p, "PASS %s TS %d %s",
1061 aconf->spasswd, TS_CURRENT, me.id);
1062 else
1063 sendto_one(client_p, "PASS %s TS 5",
1064 aconf->spasswd);
1065 }
1066
1067 /* Pass my info to the new server
1068 *
1069 * If trying to negotiate LazyLinks, pass on CAP_LL
1070 * If this is a HUB, pass on CAP_HUB
1071 * Pass on ZIP if supported
1072 * Pass on TB if supported.
1073 * - Dianora
1074 */
1075
1076 send_capabilities(client_p, aconf, (ServerInfo.hub ? CAP_HUB : 0)
1077 | (IsConfLazyLink(aconf) ? CAP_LL : 0)
1078 | (IsConfCompressed(aconf) ? CAP_ZIP : 0)
1079 | (IsConfTopicBurst(aconf) ? CAP_TBURST|CAP_TB : 0), 0);
1080
1081 /* SERVER is the last command sent before switching to ziplinks.
1082 * We set TCPNODELAY on the socket to make sure it gets sent out
1083 * on the wire immediately. Otherwise, it could be sitting in
1084 * a kernel buffer when we start sending zipped data, and the
1085 * parser on the receiving side can't hand both unzipped and zipped
1086 * data in one packet. --Rodder
1087 *
1088 * currently we only need to call send_queued_write,
1089 * Nagle is already disabled at this point --adx
1090 */
1091 sendto_one(client_p, "SERVER %s 1 :%s%s",
1092 my_name_for_link(aconf), /* XXX */
1093 ConfigServerHide.hidden ? "(H) " : "",
1094 (me.info[0]) ? (me.info) : "IRCers United");
1095 send_queued_write(client_p);
1096 }
1097
1098 /* Hand the server off to servlink now */
1099 if (IsCapable(client_p, CAP_ENC) || IsCapable(client_p, CAP_ZIP))
1100 {
1101 if (fork_server(client_p) < 0)
1102 {
1103 sendto_realops_flags(UMODE_ALL, L_ADMIN,
1104 "Warning: fork failed for server %s -- check servlink_path (%s)",
1105 get_client_name(client_p, HIDE_IP), ConfigFileEntry.servlink_path);
1106 sendto_realops_flags(UMODE_ALL, L_OPER, "Warning: fork failed for server "
1107 "%s -- check servlink_path (%s)",
1108 get_client_name(client_p, MASK_IP),
1109 ConfigFileEntry.servlink_path);
1110 exit_client(client_p, &me, "fork failed");
1111 return;
1112 }
1113
1114 start_io(client_p);
1115 SetServlink(client_p);
1116 }
1117
1118 /* only send ts6 format SVINFO if we have ts6 enabled */
1119 sendto_one(client_p, "SVINFO %d %d 0 :%lu",
1120 (me.id[0] ? TS_CURRENT : 5), TS_MIN,
1121 (unsigned long)CurrentTime);
1122
1123 /* assumption here is if they passed the correct TS version, they also passed an SID */
1124 if (IsCapable(client_p, CAP_TS6))
1125 hash_add_id(client_p);
1126
1127 /* *WARNING*
1128 ** In the following code in place of plain server's
1129 ** name we send what is returned by get_client_name
1130 ** which may add the "sockhost" after the name. It's
1131 ** *very* *important* that there is a SPACE between
1132 ** the name and sockhost (if present). The receiving
1133 ** server will start the information field from this
1134 ** first blank and thus puts the sockhost into info.
1135 ** ...a bit tricky, but you have been warned, besides
1136 ** code is more neat this way... --msa
1137 */
1138 client_p->servptr = &me;
1139
1140 if (IsClosing(client_p))
1141 return;
1142
1143 SetServer(client_p);
1144
1145 /* Update the capability combination usage counts. -A1kmm */
1146 set_chcap_usage_counts(client_p);
1147
1148 dlinkAdd(client_p, &client_p->lnode, &me.serv->servers);
1149
1150 m = dlinkFind(&unknown_list, client_p);
1151 assert(NULL != m);
1152
1153 dlinkDelete(m, &unknown_list);
1154 dlinkAdd(client_p, m, &serv_list);
1155
1156 Count.myserver++;
1157
1158 dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
1159 hash_add_client(client_p);
1160
1161 /* doesnt duplicate client_p->serv if this struct is allocated already */
1162 make_server(client_p);
1163
1164 /* fixing eob timings.. -gnp */
1165 client_p->firsttime = CurrentTime;
1166
1167 /* Show the real host/IP to admins */
1168 sendto_realops_flags(UMODE_ALL, L_ADMIN,
1169 "Link with %s established: (%s) link",
1170 inpath_ip,show_capabilities(client_p));
1171 /* Now show the masked hostname/IP to opers */
1172 sendto_realops_flags(UMODE_ALL, L_OPER,
1173 "Link with %s established: (%s) link",
1174 inpath,show_capabilities(client_p));
1175 ilog(L_NOTICE, "Link with %s established: (%s) link",
1176 inpath_ip, show_capabilities(client_p));
1177
1178 /* XXX */
1179 attach_server_conf(client_p, conf);
1180
1181 if (HasServlink(client_p))
1182 {
1183 /* we won't overflow FD_DESC_SZ here, as it can hold
1184 * client_p->name + 64
1185 */
1186 fd_note(&client_p->localClient->fd, "slink data: %s", client_p->name);
1187 fd_note(&client_p->localClient->ctrlfd, "slink ctrl: %s", client_p->name);
1188 }
1189 else
1190 fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
1191
1192 /* Old sendto_serv_but_one() call removed because we now
1193 ** need to send different names to different servers
1194 ** (domain name matching) Send new server to other servers.
1195 */
1196 DLINK_FOREACH(ptr, serv_list.head)
1197 {
1198 target_p = ptr->data;
1199
1200 if (target_p == client_p)
1201 continue;
1202
1203 if ((conf = target_p->serv->sconf) &&
1204 match(my_name_for_link(&conf->conf.AccessItem), client_p->name))
1205 continue;
1206
1207 if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
1208 sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
1209 me.id, client_p->name, client_p->id,
1210 IsHidden(client_p) ? "(H) " : "",
1211 client_p->info);
1212 else
1213 sendto_one(target_p,":%s SERVER %s 2 :%s%s",
1214 me.name, client_p->name,
1215 IsHidden(client_p) ? "(H) " : "",
1216 client_p->info);
1217 }
1218
1219 /* Pass on my client information to the new server
1220 **
1221 ** First, pass only servers (idea is that if the link gets
1222 ** cancelled beacause the server was already there,
1223 ** there are no NICK's to be cancelled...). Of course,
1224 ** if cancellation occurs, all this info is sent anyway,
1225 ** and I guess the link dies when a read is attempted...? --msa
1226 **
1227 ** Note: Link cancellation to occur at this point means
1228 ** that at least two servers from my fragment are building
1229 ** up connection this other fragment at the same time, it's
1230 ** a race condition, not the normal way of operation...
1231 **
1232 ** ALSO NOTE: using the get_client_name for server names--
1233 ** see previous *WARNING*!!! (Also, original inpath
1234 ** is destroyed...)
1235 */
1236
1237 conf = client_p->serv->sconf;
1238
1239 DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
1240 {
1241 target_p = ptr->data;
1242
1243 /* target_p->from == target_p for target_p == client_p */
1244 if (target_p->from == client_p)
1245 continue;
1246
1247 if (match(my_name_for_link(&conf->conf.AccessItem), target_p->name))
1248 continue;
1249
1250 if (IsCapable(client_p, CAP_TS6))
1251 {
1252 if (HasID(target_p))
1253 sendto_one(client_p, ":%s SID %s %d %s :%s%s",
1254 ID(target_p->servptr), target_p->name, target_p->hopcount+1,
1255 target_p->id, IsHidden(target_p) ? "(H) " : "",
1256 target_p->info);
1257 else /* introducing non-ts6 server */
1258 sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1259 ID(target_p->servptr), target_p->name, target_p->hopcount+1,
1260 IsHidden(target_p) ? "(H) " : "", target_p->info);
1261 }
1262 else
1263 sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1264 target_p->servptr->name, target_p->name, target_p->hopcount+1,
1265 IsHidden(target_p) ? "(H) " : "", target_p->info);
1266 }
1267
1268 if (!ServerInfo.hub && MyConnect(client_p))
1269 uplink = client_p;
1270
1271 server_burst(client_p);
1272 }
1273
1274 static void
1275 start_io(struct Client *server)
1276 {
1277 struct LocalUser *lserver = server->localClient;
1278 int alloclen = 1;
1279 char *buf;
1280 dlink_node *ptr;
1281 struct dbuf_block *block;
1282
1283 /* calculate how many bytes to allocate */
1284 if (IsCapable(server, CAP_ZIP))
1285 alloclen += 6;
1286 #ifdef HAVE_LIBCRYPTO
1287 if (IsCapable(server, CAP_ENC))
1288 alloclen += 16 + lserver->in_cipher->keylen + lserver->out_cipher->keylen;
1289 #endif
1290 alloclen += dbuf_length(&lserver->buf_recvq);
1291 alloclen += dlink_list_length(&lserver->buf_recvq.blocks) * 3;
1292 alloclen += dbuf_length(&lserver->buf_sendq);
1293 alloclen += dlink_list_length(&lserver->buf_sendq.blocks) * 3;
1294
1295 /* initialize servlink control sendq */
1296 lserver->slinkq = buf = MyMalloc(alloclen);
1297 lserver->slinkq_ofs = 0;
1298 lserver->slinkq_len = alloclen;
1299
1300 if (IsCapable(server, CAP_ZIP))
1301 {
1302 /* ziplink */
1303 *buf++ = SLINKCMD_SET_ZIP_OUT_LEVEL;
1304 *buf++ = 0; /* | */
1305 *buf++ = 1; /* \ len is 1 */
1306 *buf++ = ConfigFileEntry.compression_level;
1307 *buf++ = SLINKCMD_START_ZIP_IN;
1308 *buf++ = SLINKCMD_START_ZIP_OUT;
1309 }
1310 #ifdef HAVE_LIBCRYPTO
1311 if (IsCapable(server, CAP_ENC))
1312 {
1313 /* Decryption settings */
1314 *buf++ = SLINKCMD_SET_CRYPT_IN_CIPHER;
1315 *buf++ = 0; /* / (upper 8-bits of len) */
1316 *buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */
1317 *buf++ = lserver->in_cipher->cipherid;
1318 *buf++ = SLINKCMD_SET_CRYPT_IN_KEY;
1319 *buf++ = 0; /* keylen < 256 */
1320 *buf++ = lserver->in_cipher->keylen;
1321 memcpy(buf, lserver->in_key, lserver->in_cipher->keylen);
1322 buf += lserver->in_cipher->keylen;
1323 /* Encryption settings */
1324 *buf++ = SLINKCMD_SET_CRYPT_OUT_CIPHER;
1325 *buf++ = 0; /* / (upper 8-bits of len) */
1326 *buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */
1327 *buf++ = lserver->out_cipher->cipherid;
1328 *buf++ = SLINKCMD_SET_CRYPT_OUT_KEY;
1329 *buf++ = 0; /* keylen < 256 */
1330 *buf++ = lserver->out_cipher->keylen;
1331 memcpy(buf, lserver->out_key, lserver->out_cipher->keylen);
1332 buf += lserver->out_cipher->keylen;
1333 *buf++ = SLINKCMD_START_CRYPT_IN;
1334 *buf++ = SLINKCMD_START_CRYPT_OUT;
1335 }
1336 #endif
1337
1338 /* pass the whole recvq to servlink */
1339 DLINK_FOREACH (ptr, lserver->buf_recvq.blocks.head)
1340 {
1341 block = ptr->data;
1342 *buf++ = SLINKCMD_INJECT_RECVQ;
1343 *buf++ = (block->size >> 8);
1344 *buf++ = (block->size & 0xff);
1345 memcpy(buf, &block->data[0], block->size);
1346 buf += block->size;
1347 }
1348 dbuf_clear(&lserver->buf_recvq);
1349
1350 /* pass the whole sendq to servlink */
1351 DLINK_FOREACH (ptr, lserver->buf_sendq.blocks.head)
1352 {
1353 block = ptr->data;
1354 *buf++ = SLINKCMD_INJECT_SENDQ;
1355 *buf++ = (block->size >> 8);
1356 *buf++ = (block->size & 0xff);
1357 memcpy(buf, &block->data[0], block->size);
1358 buf += block->size;
1359 }
1360 dbuf_clear(&lserver->buf_sendq);
1361
1362 /* start io */
1363 *buf++ = SLINKCMD_INIT;
1364
1365 /* schedule a write */
1366 send_queued_slink_write(server);
1367 }
1368
1369 /* fork_server()
1370 *
1371 * inputs - struct Client *server
1372 * output - success: 0 / failure: -1
1373 * side effect - fork, and exec SERVLINK to handle this connection
1374 */
1375 static int
1376 fork_server(struct Client *server)
1377 {
1378 #ifndef HAVE_SOCKETPAIR
1379 return -1;
1380 #else
1381 int i;
1382 int slink_fds[2][2];
1383 /* 0? - ctrl | 1? - data
1384 * ?0 - child | ?1 - parent */
1385
1386 if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[0]) < 0)
1387 return -1;
1388 if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[1]) < 0)
1389 goto free_ctrl_fds;
1390
1391 if ((i = fork()) < 0)
1392 {
1393 close(slink_fds[1][0]); close(slink_fds[1][1]);
1394 free_ctrl_fds:
1395 close(slink_fds[0][0]); close(slink_fds[0][1]);
1396 return -1;
1397 }
1398
1399 if (i == 0)
1400 {
1401 char fd_str[3][6]; /* store 3x sizeof("65535") */
1402 char *kid_argv[7];
1403
1404 #ifdef O_ASYNC
1405 fcntl(server->localClient->fd.fd, F_SETFL,
1406 fcntl(server->localClient->fd.fd, F_GETFL, 0) & ~O_ASYNC);
1407 #endif
1408 close_fds(&server->localClient->fd);
1409 close(slink_fds[0][1]);
1410 close(slink_fds[1][1]);
1411
1412 sprintf(fd_str[0], "%d", slink_fds[0][0]);
1413 sprintf(fd_str[1], "%d", slink_fds[1][0]);
1414 sprintf(fd_str[2], "%d", server->localClient->fd.fd);
1415
1416 kid_argv[0] = "-slink";
1417 kid_argv[1] = kid_argv[2] = fd_str[0]; /* ctrl */
1418 kid_argv[3] = kid_argv[4] = fd_str[1]; /* data */
1419 kid_argv[5] = fd_str[2]; /* network */
1420 kid_argv[6] = NULL;
1421
1422 execv(ConfigFileEntry.servlink_path, kid_argv);
1423
1424 _exit(1);
1425 }
1426
1427 /* close the network fd and the child ends of the pipes */
1428 fd_close(&server->localClient->fd);
1429 close(slink_fds[0][0]);
1430 close(slink_fds[1][0]);
1431
1432 execute_callback(setup_socket_cb, slink_fds[0][1]);
1433 execute_callback(setup_socket_cb, slink_fds[1][1]);
1434
1435 fd_open(&server->localClient->ctrlfd, slink_fds[0][1], 1, "slink ctrl");
1436 fd_open(&server->localClient->fd, slink_fds[1][1], 1, "slink data");
1437
1438 read_ctrl_packet(&server->localClient->ctrlfd, server);
1439 read_packet(&server->localClient->fd, server);
1440
1441 return 0;
1442 #endif
1443 }
1444
1445 /* server_burst()
1446 *
1447 * inputs - struct Client pointer server
1448 * -
1449 * output - none
1450 * side effects - send a server burst
1451 * bugs - still too long
1452 */
1453 static void
1454 server_burst(struct Client *client_p)
1455 {
1456 /* Send it in the shortened format with the TS, if
1457 ** it's a TS server; walk the list of channels, sending
1458 ** all the nicks that haven't been sent yet for each
1459 ** channel, then send the channel itself -- it's less
1460 ** obvious than sending all nicks first, but on the
1461 ** receiving side memory will be allocated more nicely
1462 ** saving a few seconds in the handling of a split
1463 ** -orabidoo
1464 */
1465
1466 /* On a "lazy link" hubs send nothing.
1467 * Leafs always have to send nicks plus channels
1468 */
1469 if (IsCapable(client_p, CAP_LL))
1470 {
1471 if (!ServerInfo.hub)
1472 {
1473 /* burst all our info */
1474 burst_all(client_p);
1475
1476 /* Now, ask for channel info on all our current channels */
1477 cjoin_all(client_p);
1478 }
1479 }
1480 else
1481 {
1482 burst_all(client_p);
1483 }
1484
1485 /* EOB stuff is now in burst_all */
1486 /* Always send a PING after connect burst is done */
1487 sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
1488 }
1489
1490 /* burst_all()
1491 *
1492 * inputs - pointer to server to send burst to
1493 * output - NONE
1494 * side effects - complete burst of channels/nicks is sent to client_p
1495 */
1496 static void
1497 burst_all(struct Client *client_p)
1498 {
1499 dlink_node *ptr = NULL;
1500
1501 DLINK_FOREACH(ptr, global_channel_list.head)
1502 {
1503 struct Channel *chptr = ptr->data;
1504
1505 if (dlink_list_length(&chptr->members) != 0)
1506 {
1507 burst_members(client_p, chptr);
1508 send_channel_modes(client_p, chptr);
1509
1510 if (IsCapable(client_p, CAP_TBURST) ||
1511 IsCapable(client_p, CAP_TB))
1512 send_tb(client_p, chptr);
1513 }
1514 }
1515
1516 /* also send out those that are not on any channel
1517 */
1518 DLINK_FOREACH(ptr, global_client_list.head)
1519 {
1520 struct Client *target_p = ptr->data;
1521
1522 if (!IsBursted(target_p) && target_p->from != client_p)
1523 sendnick_TS(client_p, target_p);
1524
1525 ClearBursted(target_p);
1526 }
1527
1528 /* We send the time we started the burst, and let the remote host determine an EOB time,
1529 ** as otherwise we end up sending a EOB of 0 Sending here means it gets sent last -- fl
1530 */
1531 /* Its simpler to just send EOB and use the time its been connected.. --fl_ */
1532 if (IsCapable(client_p, CAP_EOB))
1533 sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
1534 }
1535
1536 /*
1537 * send_tb
1538 *
1539 * inputs - pointer to Client
1540 * - pointer to channel
1541 * output - NONE
1542 * side effects - Called on a server burst when
1543 * server is CAP_TB|CAP_TBURST capable
1544 */
1545 static void
1546 send_tb(struct Client *client_p, const struct Channel *chptr)
1547 {
1548 /*
1549 * XXX - Logic here isn't right either. What if the topic got unset?
1550 * We would need to send an empty TOPIC to reset the topic from the
1551 * other side
1552 */
1553 if (chptr->topic != NULL)
1554 {
1555 if (IsCapable(client_p, CAP_TBURST))
1556 sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
1557 me.name, (unsigned long)chptr->channelts, chptr->chname,
1558 (unsigned long)chptr->topic_time, chptr->topic_info,
1559 chptr->topic);
1560 else if (IsCapable(client_p, CAP_TB))
1561 {
1562 if (ConfigChannel.burst_topicwho)
1563 {
1564 sendto_one(client_p, ":%s TB %s %lu %s :%s",
1565 me.name, chptr->chname,
1566 (unsigned long)chptr->topic_time,
1567 chptr->topic_info, chptr->topic);
1568 }
1569 else
1570 {
1571 sendto_one(client_p, ":%s TB %s %lu :%s",
1572 me.name, chptr->chname,
1573 (unsigned long)chptr->topic_time, chptr->topic);
1574 }
1575 }
1576 }
1577 }
1578
1579 /* cjoin_all()
1580 *
1581 * inputs - server to ask for channel info from
1582 * output - NONE
1583 * side effects - CJOINS for all the leafs known channels is sent
1584 */
1585 static void
1586 cjoin_all(struct Client *client_p)
1587 {
1588 const dlink_node *gptr = NULL;
1589
1590 DLINK_FOREACH(gptr, global_channel_list.head)
1591 {
1592 const struct Channel *chptr = gptr->data;
1593 sendto_one(client_p, ":%s CBURST %s",
1594 me.name, chptr->chname);
1595 }
1596 }
1597
1598 /* burst_channel()
1599 *
1600 * inputs - pointer to server to send sjoins to
1601 * - channel pointer
1602 * output - none
1603 * side effects - All sjoins for channel(s) given by chptr are sent
1604 * for all channel members. ONLY called by hub on
1605 * behalf of a lazylink so client_p is always guarunteed
1606 * to be a LL leaf.
1607 */
1608 void
1609 burst_channel(struct Client *client_p, struct Channel *chptr)
1610 {
1611 burst_ll_members(client_p, chptr);
1612
1613 send_channel_modes(client_p, chptr);
1614 add_lazylinkchannel(client_p,chptr);
1615
1616 if (chptr->topic != NULL && chptr->topic_info != NULL)
1617 {
1618 sendto_one(client_p, ":%s TOPIC %s %s %lu :%s",
1619 me.name, chptr->chname, chptr->topic_info,
1620 (unsigned long)chptr->topic_time, chptr->topic);
1621 }
1622 }
1623
1624 /* add_lazlinkchannel()
1625 *
1626 * inputs - pointer to directly connected leaf server
1627 * being introduced to this hub
1628 * - pointer to channel structure being introduced
1629 * output - NONE
1630 * side effects - The channel pointed to by chptr is now known
1631 * to be on lazyleaf server given by local_server_p.
1632 * mark that in the bit map and add to the list
1633 * of channels to examine after this newly introduced
1634 * server is squit off.
1635 */
1636 static void
1637 add_lazylinkchannel(struct Client *local_server_p, struct Channel *chptr)
1638 {
1639 assert(MyConnect(local_server_p));
1640
1641 chptr->lazyLinkChannelExists |= local_server_p->localClient->serverMask;
1642 dlinkAdd(chptr, make_dlink_node(), &lazylink_channels);
1643 }
1644
1645 /* add_lazylinkclient()
1646 *
1647 * inputs - pointer to directly connected leaf server
1648 * being introduced to this hub
1649 * - pointer to client being introduced
1650 * output - NONE
1651 * side effects - The client pointed to by client_p is now known
1652 * to be on lazyleaf server given by local_server_p.
1653 * mark that in the bit map and add to the list
1654 * of clients to examine after this newly introduced
1655 * server is squit off.
1656 */
1657 void
1658 add_lazylinkclient(struct Client *local_server_p, struct Client *client_p)
1659 {
1660 assert(MyConnect(local_server_p));
1661 client_p->lazyLinkClientExists |= local_server_p->localClient->serverMask;
1662 }
1663
1664 /* remove_lazylink_flags()
1665 *
1666 * inputs - pointer to server quitting
1667 * output - NONE
1668 * side effects - All the channels on the lazylink channel list are examined
1669 * If they hold a bit corresponding to the servermask
1670 * attached to client_p, clear that bit. If this bitmask
1671 * goes to 0, then the channel is no longer known to
1672 * be on any lazylink server, and can be removed from the
1673 * link list.
1674 *
1675 * Similar is done for lazylink clients
1676 *
1677 * This function must be run by the HUB on any exiting
1678 * lazylink leaf server, while the pointer is still valid.
1679 * Hence must be run from client.c in exit_one_client()
1680 *
1681 * The old code scanned all channels, this code only
1682 * scans channels/clients on the lazylink_channels
1683 * lazylink_clients lists.
1684 */
1685 void
1686 remove_lazylink_flags(unsigned long mask)
1687 {
1688 dlink_node *ptr;
1689 dlink_node *next_ptr;
1690 struct Channel *chptr;
1691 struct Client *target_p;
1692 unsigned long clear_mask;
1693
1694 if (!mask) /* On 0 mask, don't do anything */
1695 return;
1696
1697 clear_mask = ~mask;
1698 freeMask |= mask;
1699
1700 DLINK_FOREACH_SAFE(ptr, next_ptr, lazylink_channels.head)
1701 {
1702 chptr = ptr->data;
1703
1704 chptr->lazyLinkChannelExists &= clear_mask;
1705
1706 if (chptr->lazyLinkChannelExists == 0)
1707 {
1708 dlinkDelete(ptr, &lazylink_channels);
1709 free_dlink_node(ptr);
1710 }
1711 }
1712
1713 DLINK_FOREACH(ptr, global_client_list.head)
1714 {
1715 target_p = ptr->data;
1716 target_p->lazyLinkClientExists &= clear_mask;
1717 }
1718 }
1719
1720 /* burst_members()
1721 *
1722 * inputs - pointer to server to send members to
1723 * - dlink_list pointer to membership list to send
1724 * output - NONE
1725 * side effects -
1726 */
1727 static void
1728 burst_members(struct Client *client_p, struct Channel *chptr)
1729 {
1730 struct Client *target_p;
1731 struct Membership *ms;
1732 dlink_node *ptr;
1733
1734 DLINK_FOREACH(ptr, chptr->members.head)
1735 {
1736 ms = ptr->data;
1737 target_p = ms->client_p;
1738
1739 if (!IsBursted(target_p))
1740 {
1741 SetBursted(target_p);
1742
1743 if (target_p->from != client_p)
1744 sendnick_TS(client_p, target_p);
1745 }
1746 }
1747 }
1748
1749 /* burst_ll_members()
1750 *
1751 * inputs - pointer to server to send members to
1752 * - dlink_list pointer to membership list to send
1753 * output - NONE
1754 * side effects - This version also has to check the bitmap for lazylink
1755 */
1756 static void
1757 burst_ll_members(struct Client *client_p, struct Channel *chptr)
1758 {
1759 struct Client *target_p;
1760 struct Membership *ms;
1761 dlink_node *ptr;
1762
1763 DLINK_FOREACH(ptr, chptr->members.head)
1764 {
1765 ms = ptr->data;
1766 target_p = ms->client_p;
1767
1768 if ((target_p->lazyLinkClientExists & client_p->localClient->serverMask) == 0)
1769 {
1770 if (target_p->from != client_p)
1771 {
1772 add_lazylinkclient(client_p,target_p);
1773 sendnick_TS(client_p, target_p);
1774 }
1775 }
1776 }
1777 }
1778
1779 /* set_autoconn()
1780 *
1781 * inputs - struct Client pointer to oper requesting change
1782 * output - none
1783 * side effects - set autoconnect mode
1784 */
1785 void
1786 set_autoconn(struct Client *source_p, const char *name, int newval)
1787 {
1788 struct ConfItem *conf;
1789 struct AccessItem *aconf;
1790
1791 if (name != NULL)
1792 {
1793 conf = find_exact_name_conf(SERVER_TYPE, name, NULL, NULL);
1794 if (conf != NULL)
1795 {
1796 aconf = &conf->conf.AccessItem;
1797 if (newval)
1798 SetConfAllowAutoConn(aconf);
1799 else
1800 ClearConfAllowAutoConn(aconf);
1801
1802 sendto_realops_flags(UMODE_ALL, L_ALL,
1803 "%s has changed AUTOCONN for %s to %i",
1804 source_p->name, name, newval);
1805 sendto_one(source_p,
1806 ":%s NOTICE %s :AUTOCONN for %s is now set to %i",
1807 me.name, source_p->name, name, newval);
1808 }
1809 else
1810 {
1811 sendto_one(source_p, ":%s NOTICE %s :Can't find %s",
1812 me.name, source_p->name, name);
1813 }
1814 }
1815 else
1816 {
1817 sendto_one(source_p, ":%s NOTICE %s :Please specify a server name!",
1818 me.name, source_p->name);
1819 }
1820 }
1821
1822 void
1823 initServerMask(void)
1824 {
1825 freeMask = 0xFFFFFFFFUL;
1826 }
1827
1828 /* nextFreeMask()
1829 *
1830 * inputs - NONE
1831 * output - unsigned long next unused mask for use in LL
1832 * side effects -
1833 */
1834 unsigned long
1835 nextFreeMask(void)
1836 {
1837 int i;
1838 unsigned long mask = 1;
1839
1840 for (i = 0; i < 32; i++)
1841 {
1842 if (mask & freeMask)
1843 {
1844 freeMask &= ~mask;
1845 return(mask);
1846 }
1847
1848 mask <<= 1;
1849 }
1850
1851 return(0L); /* watch this special case ... */
1852 }
1853
1854 /* New server connection code
1855 * Based upon the stuff floating about in s_bsd.c
1856 * -- adrian
1857 */
1858
1859 /* serv_connect() - initiate a server connection
1860 *
1861 * inputs - pointer to conf
1862 * - pointer to client doing the connect
1863 * output -
1864 * side effects -
1865 *
1866 * This code initiates a connection to a server. It first checks to make
1867 * sure the given server exists. If this is the case, it creates a socket,
1868 * creates a client, saves the socket information in the client, and
1869 * initiates a connection to the server through comm_connect_tcp(). The
1870 * completion of this goes through serv_completed_connection().
1871 *
1872 * We return 1 if the connection is attempted, since we don't know whether
1873 * it suceeded or not, and 0 if it fails in here somewhere.
1874 */
1875 int
1876 serv_connect(struct AccessItem *aconf, struct Client *by)
1877 {
1878 struct ConfItem *conf;
1879 struct Client *client_p;
1880 char buf[HOSTIPLEN];
1881
1882 /* conversion structs */
1883 struct sockaddr_in *v4;
1884 /* Make sure aconf is useful */
1885 assert(aconf != NULL);
1886
1887 if(aconf == NULL)
1888 return (0);
1889
1890 conf = aconf->conf_ptr;
1891
1892 /* log */
1893 irc_getnameinfo((struct sockaddr*)&aconf->ipnum, aconf->ipnum.ss_len,
1894 buf, HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
1895 ilog(L_NOTICE, "Connect to %s[%s] @%s", aconf->user, aconf->host,
1896 buf);
1897
1898 /* Still processing a DNS lookup? -> exit */
1899 if (aconf->dns_query != NULL)
1900 {
1901 sendto_realops_flags(UMODE_ALL, L_OPER,
1902 "Error connecting to %s: Error during DNS lookup", conf->name);
1903 return (0);
1904 }
1905
1906 /* Make sure this server isn't already connected
1907 * Note: aconf should ALWAYS be a valid C: line
1908 */
1909 if ((client_p = find_server(conf->name)) != NULL)
1910 {
1911 sendto_realops_flags(UMODE_ALL, L_ADMIN,
1912 "Server %s already present from %s",
1913 conf->name, get_client_name(client_p, SHOW_IP));
1914 sendto_realops_flags(UMODE_ALL, L_OPER,
1915 "Server %s already present from %s",
1916 conf->name, get_client_name(client_p, MASK_IP));
1917 if (by && IsClient(by) && !MyClient(by))
1918 sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
1919 me.name, by->name, conf->name,
1920 get_client_name(client_p, MASK_IP));
1921 return (0);
1922 }
1923
1924 /* Create a local client */
1925 client_p = make_client(NULL);
1926
1927 /* Copy in the server, hostname, fd */
1928 strlcpy(client_p->name, conf->name, sizeof(client_p->name));
1929 strlcpy(client_p->host, aconf->host, sizeof(client_p->host));
1930
1931 /* We already converted the ip once, so lets use it - stu */
1932 strlcpy(client_p->sockhost, buf, HOSTIPLEN);
1933
1934 /* create a socket for the server connection */
1935 if (comm_open(&client_p->localClient->fd, aconf->ipnum.ss.ss_family,
1936 SOCK_STREAM, 0, NULL) < 0)
1937 {
1938 /* Eek, failure to create the socket */
1939 report_error(L_ALL,
1940 "opening stream socket to %s: %s", conf->name, errno);
1941 SetDead(client_p);
1942 exit_client(client_p, &me, "Connection failed");
1943 return (0);
1944 }
1945
1946 /* servernames are always guaranteed under HOSTLEN chars */
1947 fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
1948
1949 /* Attach config entries to client here rather than in
1950 * serv_connect_callback(). This to avoid null pointer references.
1951 */
1952
1953 /* make_server() has to be called here to have something to attach to */
1954
1955 make_server(client_p);
1956
1957 if (!attach_connect_block(client_p, conf->name, aconf->host))
1958 {
1959 sendto_realops_flags(UMODE_ALL, L_ALL,
1960 "Host %s is not enabled for connecting:no connect-block",
1961 conf->name);
1962 if (by && IsClient(by) && !MyClient(by))
1963 sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
1964 me.name, by->name, client_p->name);
1965 SetDead(client_p);
1966 exit_client(client_p, client_p, "Connection failed");
1967 return (0);
1968 }
1969
1970 /* at this point we have a connection in progress and a connect block
1971 * attached to the client, the socket info should be saved in the
1972 * client and it should either be resolved or have a valid address.
1973 *
1974 * The socket has been connected or connect is in progress.
1975 */
1976
1977 if (by && IsClient(by))
1978 strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
1979 else
1980 strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
1981
1982 SetConnecting(client_p);
1983 dlinkAdd(client_p, &client_p->node, &global_client_list);
1984 /* from def_fam */
1985 client_p->localClient->aftype = aconf->aftype;
1986
1987 /* Now, initiate the connection */
1988 /* XXX assume that a non 0 type means a specific bind address
1989 * for this connect.
1990 */
1991 switch (aconf->aftype)
1992 {
1993 case AF_INET:
1994 v4 = (struct sockaddr_in*)&aconf->my_ipnum;
1995 if (v4->sin_addr.s_addr != 0)
1996 {
1997 struct irc_ssaddr ipn;
1998 memset(&ipn, 0, sizeof(struct irc_ssaddr));
1999 ipn.ss.ss_family = AF_INET;
2000 ipn.ss_port = 0;
2001 memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr));
2002 comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
2003 (struct sockaddr *)&ipn, ipn.ss_len,
2004 serv_connect_callback, client_p, aconf->aftype,
2005 CONNECTTIMEOUT);
2006 }
2007 else if (ServerInfo.specific_ipv4_vhost)
2008 {
2009 struct irc_ssaddr ipn;
2010 memset(&ipn, 0, sizeof(struct irc_ssaddr));
2011 ipn.ss.ss_family = AF_INET;
2012 ipn.ss_port = 0;
2013 memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
2014 comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
2015 (struct sockaddr *)&ipn, ipn.ss_len,
2016 serv_connect_callback, client_p, aconf->aftype,
2017 CONNECTTIMEOUT);
2018 }
2019 else
2020 comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
2021 NULL, 0, serv_connect_callback, client_p, aconf->aftype,
2022 CONNECTTIMEOUT);
2023 break;
2024 #ifdef IPV6
2025 case AF_INET6:
2026 {
2027 struct irc_ssaddr ipn;
2028 struct sockaddr_in6 *v6;
2029 struct sockaddr_in6 *v6conf;
2030
2031 memset(&ipn, 0, sizeof(struct irc_ssaddr));
2032 v6conf = (struct sockaddr_in6 *)&aconf->my_ipnum;
2033 v6 = (struct sockaddr_in6 *)&ipn;
2034
2035 if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr,
2036 sizeof(struct in6_addr)) != 0)
2037 {
2038 memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr));
2039 ipn.ss.ss_family = AF_INET6;
2040 ipn.ss_port = 0;
2041 comm_connect_tcp(&client_p->localClient->fd,
2042 aconf->host, aconf->port,
2043 (struct sockaddr *)&ipn, ipn.ss_len,
2044 serv_connect_callback, client_p,
2045 aconf->aftype, CONNECTTIMEOUT);
2046 }
2047 else if (ServerInfo.specific_ipv6_vhost)
2048 {
2049 memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
2050 ipn.ss.ss_family = AF_INET6;
2051 ipn.ss_port = 0;
2052 comm_connect_tcp(&client_p->localClient->fd,
2053 aconf->host, aconf->port,
2054 (struct sockaddr *)&ipn, ipn.ss_len,
2055 serv_connect_callback, client_p,
2056 aconf->aftype, CONNECTTIMEOUT);
2057 }
2058 else
2059 comm_connect_tcp(&client_p->localClient->fd,
2060 aconf->host, aconf->port,
2061 NULL, 0, serv_connect_callback, client_p,
2062 aconf->aftype, CONNECTTIMEOUT);
2063 }
2064 #endif
2065 }
2066 return (1);
2067 }
2068
2069 /* serv_connect_callback() - complete a server connection.
2070 *
2071 * This routine is called after the server connection attempt has
2072 * completed. If unsucessful, an error is sent to ops and the client
2073 * is closed. If sucessful, it goes through the initialisation/check
2074 * procedures, the capabilities are sent, and the socket is then
2075 * marked for reading.
2076 */
2077 static void
2078 serv_connect_callback(fde_t *fd, int status, void *data)
2079 {
2080 struct Client *client_p = data;
2081 struct ConfItem *conf;
2082 struct AccessItem *aconf=NULL;
2083
2084 /* First, make sure its a real client! */
2085 assert(client_p != NULL);
2086 assert(&client_p->localClient->fd == fd);
2087
2088 /* Next, for backward purposes, record the ip of the server */
2089 memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
2090 sizeof(struct irc_ssaddr));
2091 /* Check the status */
2092 if (status != COMM_OK)
2093 {
2094 /* We have an error, so report it and quit
2095 * Admins get to see any IP, mere opers don't *sigh*
2096 */
2097 if (ConfigServerHide.hide_server_ips)
2098 sendto_realops_flags(UMODE_ALL, L_ADMIN,
2099 "Error connecting to %s: %s",
2100 client_p->name, comm_errstr(status));
2101 else
2102 sendto_realops_flags(UMODE_ALL, L_ADMIN,
2103 "Error connecting to %s[%s]: %s", client_p->name,
2104 client_p->host, comm_errstr(status));
2105
2106 sendto_realops_flags(UMODE_ALL, L_OPER,
2107 "Error connecting to %s: %s",
2108 client_p->name, comm_errstr(status));
2109
2110 /* If a fd goes bad, call dead_link() the socket is no
2111 * longer valid for reading or writing.
2112 */
2113 dead_link_on_write(client_p, 0);
2114 return;
2115 }
2116
2117 /* COMM_OK, so continue the connection procedure */
2118 /* Get the connect block */
2119 conf = client_p->serv->sconf;
2120 if (conf == NULL)
2121 {
2122 sendto_realops_flags(UMODE_ALL, L_ADMIN,
2123 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
2124 sendto_realops_flags(UMODE_ALL, L_OPER,
2125 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
2126
2127 exit_client(client_p, &me, "Lost connect{} block");
2128 return;
2129 }
2130 aconf = &conf->conf.AccessItem;
2131
2132 /* Next, send the initial handshake */
2133 SetHandshake(client_p);
2134
2135 #ifdef HAVE_LIBCRYPTO
2136 /* Handle all CRYPTLINK links in cryptlink_init */
2137 if (IsConfCryptLink(aconf))
2138 {
2139 cryptlink_init(client_p, &conf->conf.AccessItem, fd);
2140 return;
2141 }
2142 #endif
2143
2144 /* jdc -- Check and send spasswd, not passwd. */
2145 if (!EmptyString(aconf->spasswd) && (me.id[0] != '\0'))
2146 /* Send TS 6 form only if id */
2147 sendto_one(client_p, "PASS %s TS %d %s",
2148 aconf->spasswd, TS_CURRENT, me.id);
2149 else
2150 sendto_one(client_p, "PASS %s TS 5",
2151 aconf->spasswd);
2152
2153 /* Pass my info to the new server
2154 *
2155 * If trying to negotiate LazyLinks, pass on CAP_LL
2156 * If this is a HUB, pass on CAP_HUB
2157 * Pass on ZIP if supported
2158 * Pass on TB if supported.
2159 * - Dianora
2160 */
2161 send_capabilities(client_p, aconf, (ServerInfo.hub ? CAP_HUB : 0)
2162 | (IsConfLazyLink(aconf) ? CAP_LL : 0)
2163 | (IsConfCompressed(aconf) ? CAP_ZIP : 0)
2164 | (IsConfTopicBurst(aconf) ? CAP_TBURST|CAP_TB : 0), 0);
2165
2166 sendto_one(client_p, "SERVER %s 1 :%s%s",
2167 my_name_for_link(aconf), /* XXX */
2168 ConfigServerHide.hidden ? "(H) " : "",
2169 me.info);
2170
2171 /* If we've been marked dead because a send failed, just exit
2172 * here now and save everyone the trouble of us ever existing.
2173 */
2174 if (IsDead(client_p))
2175 {
2176 sendto_realops_flags(UMODE_ALL, L_ADMIN,
2177 "%s[%s] went dead during handshake",
2178 client_p->name,
2179 client_p->host);
2180 sendto_realops_flags(UMODE_ALL, L_OPER,
2181 "%s went dead during handshake", client_p->name);
2182 return;
2183 }
2184
2185 /* don't move to serv_list yet -- we haven't sent a burst! */
2186 /* If we get here, we're ok, so lets start reading some data */
2187 comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
2188 }
2189
2190 struct Client *
2191 find_servconn_in_progress(const char *name)
2192 {
2193 dlink_node *ptr;
2194 struct Client *cptr;
2195
2196 DLINK_FOREACH(ptr, unknown_list.head)
2197 {
2198 cptr = ptr->data;
2199
2200 if (cptr && cptr->name[0])
2201 if (match(cptr->name, name) || match(name, cptr->name))
2202 return cptr;
2203 }
2204
2205 return NULL;
2206 }
2207
2208 #ifdef HAVE_LIBCRYPTO
2209 /*
2210 * sends a CRYPTLINK SERV command.
2211 */
2212 void
2213 cryptlink_init(struct Client *client_p, struct AccessItem *aconf, fde_t *fd)
2214 {
2215 char *encrypted;
2216 unsigned char *key_to_send;
2217 char randkey[CIPHERKEYLEN];
2218 int enc_len;
2219
2220 /* get key */
2221 if ((!ServerInfo.rsa_private_key) ||
2222 (!RSA_check_key(ServerInfo.rsa_private_key)) )
2223 {
2224 cryptlink_error(client_p, "SERV", "Invalid RSA private key",
2225 "Invalid RSA private key");
2226 return;
2227 }
2228
2229 if (aconf->rsa_public_key == NULL)
2230 {
2231 cryptlink_error(client_p, "SERV", "Invalid RSA public key",
2232 "Invalid RSA public key");
2233 return;
2234 }
2235
2236 if (get_randomness((unsigned char *)randkey, CIPHERKEYLEN) != 1)
2237 {
2238 cryptlink_error(client_p, "SERV", "Couldn't generate keyphrase",
2239 "Couldn't generate keyphrase");
2240 return;
2241 }
2242
2243 encrypted = MyMalloc(RSA_size(ServerInfo.rsa_private_key));
2244 enc_len = RSA_public_encrypt(CIPHERKEYLEN,
2245 (unsigned char *)randkey,
2246 (unsigned char *)encrypted,
2247 aconf->rsa_public_key,
2248 RSA_PKCS1_PADDING);
2249
2250 memcpy(client_p->localClient->in_key, randkey, CIPHERKEYLEN);
2251
2252 if (enc_len <= 0)
2253 {
2254 report_crypto_errors();
2255 MyFree(encrypted);
2256 cryptlink_error(client_p, "SERV", "Couldn't encrypt data",
2257 "Couldn't encrypt data");
2258 return;
2259 }
2260
2261 if (!(base64_block(&key_to_send, encrypted, enc_len)))
2262 {
2263 MyFree(encrypted);
2264 cryptlink_error(client_p, "SERV", "Couldn't base64 encode key",
2265 "Couldn't base64 encode key");
2266 return;
2267 }
2268
2269 send_capabilities(client_p, aconf, (ServerInfo.hub ? CAP_HUB : 0)
2270 | (IsConfLazyLink(aconf) ? CAP_LL : 0)
2271 | (IsConfCompressed(aconf) ? CAP_ZIP : 0)
2272 | (IsConfTopicBurst(aconf) ? CAP_TBURST|CAP_TB : 0), CAP_ENC_MASK);
2273
2274 if (me.id[0] != '\0')
2275 sendto_one(client_p, "PASS . TS %d %s", TS_CURRENT, me.id);
2276
2277 sendto_one(client_p, "CRYPTLINK SERV %s %s :%s%s",
2278 my_name_for_link(aconf), key_to_send, /* XXX */
2279 ConfigServerHide.hidden ? "(H) " : "", me.info);
2280
2281 SetHandshake(client_p);
2282 SetWaitAuth(client_p);
2283
2284 MyFree(encrypted);
2285 MyFree(key_to_send);
2286
2287 if (IsDead(client_p))
2288 cryptlink_error(client_p, "SERV", "Went dead during handshake",
2289 "Went dead during handshake");
2290 else if (fd != NULL)
2291 /* If we get here, we're ok, so lets start reading some data */
2292 comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
2293 }
2294
2295 void
2296 cryptlink_error(struct Client *client_p, const char *type,
2297 const char *reason, const char *client_reason)
2298 {
2299 sendto_realops_flags(UMODE_ALL, L_ADMIN, "%s: CRYPTLINK %s error - %s",
2300 get_client_name(client_p, SHOW_IP), type, reason);
2301 sendto_realops_flags(UMODE_ALL, L_OPER, "%s: CRYPTLINK %s error - %s",
2302 get_client_name(client_p, MASK_IP), type, reason);
2303 ilog(L_ERROR, "%s: CRYPTLINK %s error - %s",
2304 get_client_name(client_p, SHOW_IP), type, reason);
2305
2306 /* If client_reason isn't NULL, then exit the client with the message
2307 * defined in the call.
2308 */
2309 if ((client_reason != NULL) && (!IsDead(client_p)))
2310 exit_client(client_p, &me, client_reason);
2311 }
2312
2313 static char base64_chars[] =
2314 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
2315
2316 static char base64_values[] =
2317 {
2318 /* 00-15 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2319 /* 16-31 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2320 /* 32-47 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
2321 /* 48-63 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
2322 /* 64-79 */ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
2323 /* 80-95 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
2324 /* 96-111 */ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2325 /* 112-127 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
2326 /* 128-143 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2327 /* 144-159 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2328 /* 160-175 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2329 /* 186-191 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2330 /* 192-207 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2331 /* 208-223 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2332 /* 224-239 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2333 /* 240-255 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
2334 };
2335
2336 /*
2337 * base64_block will allocate and return a new block of memory
2338 * using MyMalloc(). It should be freed after use.
2339 */
2340 int
2341 base64_block(unsigned char **output, char *data, int len)
2342 {
2343 unsigned char *out;
2344 unsigned char *in = (unsigned char*)data;
2345 unsigned long int q_in;
2346 int i;
2347 int count = 0;
2348
2349 out = MyMalloc(((((len + 2) - ((len + 2) % 3)) / 3) * 4) + 1);
2350
2351 /* process 24 bits at a time */
2352 for( i = 0; i < len; i += 3)
2353 {
2354 q_in = 0;
2355
2356 if ( i + 2 < len )
2357 {
2358 q_in = (in[i+2] & 0xc0) << 2;
2359 q_in |= in[i+2];
2360 }
2361
2362 if ( i + 1 < len )
2363 {
2364 q_in |= (in[i+1] & 0x0f) << 10;
2365 q_in |= (in[i+1] & 0xf0) << 12;
2366 }
2367
2368 q_in |= (in[i] & 0x03) << 20;
2369 q_in |= in[i] << 22;
2370
2371 q_in &= 0x3f3f3f3f;
2372
2373 out[count++] = base64_chars[((q_in >> 24) )];
2374 out[count++] = base64_chars[((q_in >> 16) & 0xff)];
2375 out[count++] = base64_chars[((q_in >> 8) & 0xff)];
2376 out[count++] = base64_chars[((q_in ) & 0xff)];
2377 }
2378 if ( (i - len) > 0 )
2379 {
2380 out[count-1] = '=';
2381 if ( (i - len) > 1 )
2382 out[count-2] = '=';
2383 }
2384
2385 out[count] = '\0';
2386 *output = out;
2387 return (count);
2388 }
2389
2390 /*
2391 * unbase64_block will allocate and return a new block of memory
2392 * using MyMalloc(). It should be freed after use.
2393 */
2394 int
2395 unbase64_block(unsigned char **output, char *data, int len)
2396 {
2397 unsigned char *out;
2398 unsigned char *in = (unsigned char*)data;
2399 unsigned long int q_in;
2400 int i;
2401 int count = 0;
2402
2403 if ((len % 4) != 0)
2404 return (0);
2405
2406 out = MyMalloc(((len / 4) * 3) + 1);
2407
2408 /* process 32 bits at a time */
2409 for( i = 0; (i + 3) < len; i+=4)
2410 {
2411 /* compress input (chars a, b, c and d) as follows:
2412 * (after converting ascii -> base64 value)
2413 *
2414 * |00000000aaaaaabbbbbbccccccdddddd|
2415 * | 765432 107654 321076 543210|
2416 */
2417
2418 q_in = 0;
2419
2420 if (base64_values[in[i+3]] > -1)
2421 q_in |= base64_values[in[i+3]] ;
2422 if (base64_values[in[i+2]] > -1)
2423 q_in |= base64_values[in[i+2]] << 6;
2424 if (base64_values[in[i+1]] > -1)
2425 q_in |= base64_values[in[i+1]] << 12;
2426 if (base64_values[in[i ]] > -1)
2427 q_in |= base64_values[in[i ]] << 18;
2428
2429 out[count++] = (q_in >> 16) & 0xff;
2430 out[count++] = (q_in >> 8) & 0xff;
2431 out[count++] = (q_in ) & 0xff;
2432 }
2433
2434 if (in[i-1] == '=') count--;
2435 if (in[i-2] == '=') count--;
2436
2437 out[count] = '\0';
2438 *output = out;
2439 return (count);
2440 }
2441
2442 #endif /* HAVE_LIBCRYPTO */
2443

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28