]> jfr.im git - irc/rqf/shadowircd.git/blame - src/s_serv.c
[svn] Change two occurances like match(userinput, target_p->name) ||
[irc/rqf/shadowircd.git] / src / s_serv.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_serv.c: Server related functions.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
4d7a1ee5 24 * $Id: s_serv.c 3183 2007-02-01 01:07:42Z jilles $
212380e3 25 */
26
27#include "stdinc.h"
28
29#ifdef HAVE_LIBCRYPTO
30#include <openssl/rsa.h>
31#endif
32
33#include "tools.h"
34#include "s_serv.h"
35#include "class.h"
36#include "client.h"
37#include "common.h"
38#include "event.h"
39#include "hash.h"
40#include "irc_string.h"
41#include "sprintf_irc.h"
42#include "ircd.h"
43#include "ircd_defs.h"
44#include "numeric.h"
45#include "packet.h"
46#include "res.h"
47#include "commio.h"
48#include "s_conf.h"
49#include "s_newconf.h"
50#include "s_log.h"
51#include "s_stats.h"
52#include "s_user.h"
53#include "scache.h"
54#include "send.h"
55#include "client.h"
56#include "memory.h"
57#include "channel.h" /* chcap_usage_counts stuff... */
58#include "hook.h"
59#include "msg.h"
60
61extern char *crypt();
62
63#ifndef INADDR_NONE
64#define INADDR_NONE ((unsigned int) 0xffffffff)
65#endif
66
67#ifndef HAVE_SOCKETPAIR
68static int inet_socketpair(int d, int type, int protocol, int sv[2]);
69#endif
70
71int MaxConnectionCount = 1;
72int MaxClientCount = 1;
73int refresh_user_links = 0;
74
75static char buf[BUFSIZE];
76
77static void start_io(struct Client *server);
78
79static SlinkRplHnd slink_error;
80static SlinkRplHnd slink_zipstats;
81/*
82 * list of recognized server capabilities. "TS" is not on the list
83 * because all servers that we talk to already do TS, and the kludged
84 * extra argument to "PASS" takes care of checking that. -orabidoo
85 */
86struct Capability captab[] = {
87/* name cap */
88 { "QS", CAP_QS },
89 { "EX", CAP_EX },
90 { "CHW", CAP_CHW},
91 { "IE", CAP_IE},
92 { "KLN", CAP_KLN},
93 { "GLN", CAP_GLN},
94 { "KNOCK", CAP_KNOCK},
95 { "ZIP", CAP_ZIP},
96 { "TB", CAP_TB},
97 { "UNKLN", CAP_UNKLN},
98 { "CLUSTER", CAP_CLUSTER},
99 { "ENCAP", CAP_ENCAP },
100 { "SERVICES", CAP_SERVICE },
101 { "RSFNC", CAP_RSFNC },
102 { "SAVE", CAP_SAVE },
103 { "EUID", CAP_EUID },
104 {0, 0}
105};
106
107struct SlinkRplDef slinkrpltab[] = {
108 {SLINKRPL_ERROR, slink_error, SLINKRPL_FLAG_DATA},
109 {SLINKRPL_ZIPSTATS, slink_zipstats, SLINKRPL_FLAG_DATA},
110 {0, 0, 0},
111};
112
113static int fork_server(struct Client *client_p);
114
115static CNCB serv_connect_callback;
116
117void
118slink_error(unsigned int rpl, unsigned int len, unsigned char *data, struct Client *server_p)
119{
120 char squitreason[256];
121
122 s_assert(rpl == SLINKRPL_ERROR);
123
124 s_assert(len < 256);
125 data[len - 1] = '\0';
126
127 sendto_realops_snomask(SNO_GENERAL, L_ALL, "SlinkError for %s: %s", server_p->name, data);
128 snprintf(squitreason, sizeof squitreason, "servlink error: %s", data);
129 exit_client(server_p, server_p, &me, squitreason);
130}
131
132void
133slink_zipstats(unsigned int rpl, unsigned int len, unsigned char *data, struct Client *server_p)
134{
135 struct ZipStats zipstats;
136 u_int32_t in = 0, in_wire = 0, out = 0, out_wire = 0;
137 int i = 0;
138
139 s_assert(rpl == SLINKRPL_ZIPSTATS);
140 s_assert(len == 16);
141 s_assert(IsCapable(server_p, CAP_ZIP));
142
143 /* Yes, it needs to be done this way, no we cannot let the compiler
144 * work with the pointer to the structure. This works around a GCC
145 * bug on SPARC that affects all versions at the time of this writing.
146 * I will feed you to the creatures living in RMS's beard if you do
147 * not leave this as is, without being sure that you are not causing
148 * regression for most of our installed SPARC base.
149 * -jmallett, 04/27/2002
150 */
151 memcpy(&zipstats, &server_p->localClient->zipstats, sizeof(struct ZipStats));
152
153 in |= (data[i++] << 24);
154 in |= (data[i++] << 16);
155 in |= (data[i++] << 8);
156 in |= (data[i++]);
157
158 in_wire |= (data[i++] << 24);
159 in_wire |= (data[i++] << 16);
160 in_wire |= (data[i++] << 8);
161 in_wire |= (data[i++]);
162
163 out |= (data[i++] << 24);
164 out |= (data[i++] << 16);
165 out |= (data[i++] << 8);
166 out |= (data[i++]);
167
168 out_wire |= (data[i++] << 24);
169 out_wire |= (data[i++] << 16);
170 out_wire |= (data[i++] << 8);
171 out_wire |= (data[i++]);
172
173 zipstats.in += in;
174 zipstats.inK += zipstats.in >> 10;
175 zipstats.in &= 0x03ff;
176
177 zipstats.in_wire += in_wire;
178 zipstats.inK_wire += zipstats.in_wire >> 10;
179 zipstats.in_wire &= 0x03ff;
180
181 zipstats.out += out;
182 zipstats.outK += zipstats.out >> 10;
183 zipstats.out &= 0x03ff;
184
185 zipstats.out_wire += out_wire;
186 zipstats.outK_wire += zipstats.out_wire >> 10;
187 zipstats.out_wire &= 0x03ff;
188
189 if(zipstats.inK > 0)
190 zipstats.in_ratio =
191 (((double) (zipstats.inK - zipstats.inK_wire) /
192 (double) zipstats.inK) * 100.00);
193 else
194 zipstats.in_ratio = 0;
195
196 if(zipstats.outK > 0)
197 zipstats.out_ratio =
198 (((double) (zipstats.outK - zipstats.outK_wire) /
199 (double) zipstats.outK) * 100.00);
200 else
201 zipstats.out_ratio = 0;
202
203 memcpy(&server_p->localClient->zipstats, &zipstats, sizeof(struct ZipStats));
204}
205
206void
207collect_zipstats(void *unused)
208{
209 dlink_node *ptr;
210 struct Client *target_p;
211
212 DLINK_FOREACH(ptr, serv_list.head)
213 {
214 target_p = ptr->data;
215 if(IsCapable(target_p, CAP_ZIP))
216 {
217 /* only bother if we haven't already got something queued... */
218 if(!target_p->localClient->slinkq)
219 {
220 target_p->localClient->slinkq = MyMalloc(1); /* sigh.. */
221 target_p->localClient->slinkq[0] = SLINKCMD_ZIPSTATS;
222 target_p->localClient->slinkq_ofs = 0;
223 target_p->localClient->slinkq_len = 1;
224 send_queued_slink_write(target_p->localClient->ctrlfd, target_p);
225 }
226 }
227 }
228}
229
230/*
231 * hunt_server - Do the basic thing in delivering the message (command)
232 * across the relays to the specific server (server) for
233 * actions.
234 *
235 * Note: The command is a format string and *MUST* be
236 * of prefixed style (e.g. ":%s COMMAND %s ...").
237 * Command can have only max 8 parameters.
238 *
239 * server parv[server] is the parameter identifying the
240 * target server.
241 *
242 * *WARNING*
243 * parv[server] is replaced with the pointer to the
244 * real servername from the matched client (I'm lazy
245 * now --msa).
246 *
247 * returns: (see #defines)
248 */
249int
250hunt_server(struct Client *client_p, struct Client *source_p,
251 const char *command, int server, int parc, const char *parv[])
252{
253 struct Client *target_p;
254 int wilds;
255 dlink_node *ptr;
256 const char *old;
257 char *new;
258
259 /*
260 * Assume it's me, if no server
261 */
262 if(parc <= server || EmptyString(parv[server]) ||
4d7a1ee5 263 match(parv[server], me.name) || (strcmp(parv[server], me.id) == 0))
212380e3 264 return (HUNTED_ISME);
265
266 new = LOCAL_COPY(parv[server]);
267
268 /*
269 * These are to pickup matches that would cause the following
270 * message to go in the wrong direction while doing quick fast
271 * non-matching lookups.
272 */
273 if(MyClient(source_p))
274 target_p = find_named_client(new);
275 else
276 target_p = find_client(new);
277
278 if(target_p)
279 if(target_p->from == source_p->from && !MyConnect(target_p))
280 target_p = NULL;
281
212380e3 282 collapse(new);
283 wilds = (strchr(new, '?') || strchr(new, '*'));
284
285 /*
286 * Again, if there are no wild cards involved in the server
287 * name, use the hash lookup
288 */
289 if(!target_p)
290 {
291 if(!wilds)
292 {
293 if(MyClient(source_p) || !IsDigit(parv[server][0]))
294 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
295 form_str(ERR_NOSUCHSERVER),
296 parv[server]);
297 return (HUNTED_NOSUCH);
298 }
299 else
300 {
301 target_p = NULL;
302
303 DLINK_FOREACH(ptr, global_client_list.head)
304 {
305 if(match(new, ((struct Client *) (ptr->data))->name))
306 {
307 target_p = ptr->data;
308 break;
309 }
310 }
311 }
312 }
313
314 if(target_p)
315 {
316 if(!IsRegistered(target_p))
317 {
318 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
319 form_str(ERR_NOSUCHSERVER),
320 parv[server]);
321 return HUNTED_NOSUCH;
322 }
323
324 if(IsMe(target_p) || MyClient(target_p))
325 return HUNTED_ISME;
326
327 old = parv[server];
328 parv[server] = get_id(target_p, target_p);
329
330 sendto_one(target_p, command, get_id(source_p, target_p),
331 parv[1], parv[2], parv[3], parv[4], parv[5], parv[6], parv[7], parv[8]);
332 parv[server] = old;
333 return (HUNTED_PASS);
334 }
335
336 if(!IsDigit(parv[server][0]))
337 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
338 form_str(ERR_NOSUCHSERVER), parv[server]);
339 return (HUNTED_NOSUCH);
340}
341
342/*
343 * try_connections - scan through configuration and try new connections.
344 * Returns the calendar time when the next call to this
345 * function should be made latest. (No harm done if this
346 * is called earlier or later...)
347 */
348void
349try_connections(void *unused)
350{
351 struct Client *client_p;
352 struct server_conf *server_p = NULL;
353 struct server_conf *tmp_p;
354 struct Class *cltmp;
355 dlink_node *ptr;
356 int connecting = FALSE;
357 int confrq = 0;
358 time_t next = 0;
359
360 DLINK_FOREACH(ptr, server_conf_list.head)
361 {
362 tmp_p = ptr->data;
363
364 if(ServerConfIllegal(tmp_p) || !ServerConfAutoconn(tmp_p))
365 continue;
366
367 cltmp = tmp_p->class;
368
369 /*
370 * Skip this entry if the use of it is still on hold until
371 * future. Otherwise handle this entry (and set it on hold
372 * until next time). Will reset only hold times, if already
373 * made one successfull connection... [this algorithm is
374 * a bit fuzzy... -- msa >;) ]
375 */
376 if(tmp_p->hold > CurrentTime)
377 {
378 if(next > tmp_p->hold || next == 0)
379 next = tmp_p->hold;
380 continue;
381 }
382
383 confrq = get_con_freq(cltmp);
384 tmp_p->hold = CurrentTime + confrq;
385
386 /*
387 * Found a CONNECT config with port specified, scan clients
388 * and see if this server is already connected?
389 */
390 client_p = find_server(NULL, tmp_p->name);
391
392 if(!client_p && (CurrUsers(cltmp) < MaxUsers(cltmp)) && !connecting)
393 {
394 server_p = tmp_p;
395
396 /* We connect only one at time... */
397 connecting = TRUE;
398 }
399
400 if((next > tmp_p->hold) || (next == 0))
401 next = tmp_p->hold;
402 }
403
404 /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
405 if(GlobalSetOptions.autoconn == 0)
406 return;
407
408 if(!connecting)
409 return;
410
411 /* move this connect entry to end.. */
412 dlinkDelete(&server_p->node, &server_conf_list);
413 dlinkAddTail(server_p, &server_p->node, &server_conf_list);
414
415 /*
416 * We used to only print this if serv_connect() actually
417 * suceeded, but since comm_tcp_connect() can call the callback
418 * immediately if there is an error, we were getting error messages
419 * in the wrong order. SO, we just print out the activated line,
420 * and let serv_connect() / serv_connect_callback() print an
421 * error afterwards if it fails.
422 * -- adrian
423 */
424#ifndef HIDE_SERVERS_IPS
425 sendto_realops_snomask(SNO_GENERAL, L_ALL,
426 "Connection to %s[%s] activated.",
427 server_p->name, server_p->host);
428#else
429 sendto_realops_snomask(SNO_GENERAL, L_ALL,
430 "Connection to %s activated",
431 server_p->name);
432#endif
433
434 serv_connect(server_p, 0);
435}
436
437int
438check_server(const char *name, struct Client *client_p)
439{
440 struct server_conf *server_p = NULL;
441 struct server_conf *tmp_p;
442 dlink_node *ptr;
443 int error = -1;
444
445 s_assert(NULL != client_p);
446 if(client_p == NULL)
447 return error;
448
449 if(!(client_p->localClient->passwd))
450 return -2;
451
452 if(strlen(name) > HOSTLEN)
453 return -4;
454
455 DLINK_FOREACH(ptr, server_conf_list.head)
456 {
457 tmp_p = ptr->data;
458
459 if(ServerConfIllegal(tmp_p))
460 continue;
461
462 if(!match(tmp_p->name, name))
463 continue;
464
465 error = -3;
466
467 /* XXX: Fix me for IPv6 */
468 /* XXX sockhost is the IPv4 ip as a string */
469 if(match(tmp_p->host, client_p->host) ||
470 match(tmp_p->host, client_p->sockhost))
471 {
472 error = -2;
473
474 if(ServerConfEncrypted(tmp_p))
475 {
476 if(!strcmp(tmp_p->passwd, crypt(client_p->localClient->passwd,
477 tmp_p->passwd)))
478 {
479 server_p = tmp_p;
480 break;
481 }
482 }
483 else if(!strcmp(tmp_p->passwd, client_p->localClient->passwd))
484 {
485 server_p = tmp_p;
486 break;
487 }
488 }
489 }
490
491 if(server_p == NULL)
492 return error;
493
494 attach_server_conf(client_p, server_p);
495
496 /* clear ZIP/TB if they support but we dont want them */
497#ifdef HAVE_LIBZ
498 if(!ServerConfCompressed(server_p))
499#endif
500 ClearCap(client_p, CAP_ZIP);
501
502 if(!ServerConfTb(server_p))
503 ClearCap(client_p, CAP_TB);
504
505 return 0;
506}
507
508/*
509 * send_capabilities
510 *
511 * inputs - Client pointer to send to
512 * - int flag of capabilities that this server has
513 * output - NONE
514 * side effects - send the CAPAB line to a server -orabidoo
515 *
516 */
517void
518send_capabilities(struct Client *client_p, int cap_can_send)
519{
520 struct Capability *cap;
521 char msgbuf[BUFSIZE];
522 char *t;
523 int tl;
524
525 t = msgbuf;
526
527 for (cap = captab; cap->name; ++cap)
528 {
529 if(cap->cap & cap_can_send)
530 {
531 tl = ircsprintf(t, "%s ", cap->name);
532 t += tl;
533 }
534 }
535
536 t--;
537 *t = '\0';
538
539 sendto_one(client_p, "CAPAB :%s", msgbuf);
540}
541
542/* burst_modes_TS5()
543 *
544 * input - client to burst to, channel name, list to burst, mode flag
545 * output -
546 * side effects - client is sent a list of +b, or +e, or +I modes
547 */
548static void
549burst_modes_TS5(struct Client *client_p, char *chname, dlink_list *list, char flag)
550{
551 dlink_node *ptr;
552 struct Ban *banptr;
553 char mbuf[MODEBUFLEN];
554 char pbuf[BUFSIZE];
555 int tlen;
556 int mlen;
557 int cur_len;
558 char *mp;
559 char *pp;
560 int count = 0;
561
562 mlen = ircsprintf(buf, ":%s MODE %s +", me.name, chname);
563 cur_len = mlen;
564
565 mp = mbuf;
566 pp = pbuf;
567
568 DLINK_FOREACH(ptr, list->head)
569 {
570 banptr = ptr->data;
571 tlen = strlen(banptr->banstr) + 3;
572
573 /* uh oh */
574 if(tlen > MODEBUFLEN)
575 continue;
576
577 if((count >= MAXMODEPARAMS) || ((cur_len + tlen + 2) > (BUFSIZE - 3)))
578 {
579 sendto_one(client_p, "%s%s %s", buf, mbuf, pbuf);
580
581 mp = mbuf;
582 pp = pbuf;
583 cur_len = mlen;
584 count = 0;
585 }
586
587 *mp++ = flag;
588 *mp = '\0';
589 pp += ircsprintf(pp, "%s ", banptr->banstr);
590 cur_len += tlen;
591 count++;
592 }
593
594 if(count != 0)
595 sendto_one(client_p, "%s%s %s", buf, mbuf, pbuf);
596}
597
598/* burst_modes_TS6()
599 *
600 * input - client to burst to, channel name, list to burst, mode flag
601 * output -
602 * side effects - client is sent a list of +b, +e, or +I modes
603 */
604static void
605burst_modes_TS6(struct Client *client_p, struct Channel *chptr,
606 dlink_list *list, char flag)
607{
608 dlink_node *ptr;
609 struct Ban *banptr;
610 char *t;
611 int tlen;
612 int mlen;
613 int cur_len;
614
615 cur_len = mlen = ircsprintf(buf, ":%s BMASK %ld %s %c :",
616 me.id, (long) chptr->channelts, chptr->chname, flag);
617 t = buf + mlen;
618
619 DLINK_FOREACH(ptr, list->head)
620 {
621 banptr = ptr->data;
622
623 tlen = strlen(banptr->banstr) + 1;
624
625 /* uh oh */
626 if(cur_len + tlen > BUFSIZE - 3)
627 {
628 /* the one we're trying to send doesnt fit at all! */
629 if(cur_len == mlen)
630 {
631 s_assert(0);
632 continue;
633 }
634
635 /* chop off trailing space and send.. */
636 *(t-1) = '\0';
637 sendto_one(client_p, "%s", buf);
638 cur_len = mlen;
639 t = buf + mlen;
640 }
641
642 ircsprintf(t, "%s ", banptr->banstr);
643 t += tlen;
644 cur_len += tlen;
645 }
646
647 /* cant ever exit the loop above without having modified buf,
648 * chop off trailing space and send.
649 */
650 *(t-1) = '\0';
651 sendto_one(client_p, "%s", buf);
652}
653
654/*
655 * burst_TS5
656 *
657 * inputs - client (server) to send nick towards
658 * - client to send nick for
659 * output - NONE
660 * side effects - NICK message is sent towards given client_p
661 */
662static void
663burst_TS5(struct Client *client_p)
664{
665 static char ubuf[12];
666 struct Client *target_p;
667 struct Channel *chptr;
668 struct membership *msptr;
669 hook_data_client hclientinfo;
670 hook_data_channel hchaninfo;
671 dlink_node *ptr;
672 dlink_node *uptr;
673 char *t;
674 int tlen, mlen;
675 int cur_len = 0;
676
677 hclientinfo.client = hchaninfo.client = client_p;
678
679 DLINK_FOREACH(ptr, global_client_list.head)
680 {
681 target_p = ptr->data;
682
683 if(!IsPerson(target_p))
684 continue;
685
686 send_umode(NULL, target_p, 0, 0, ubuf);
687 if(!*ubuf)
688 {
689 ubuf[0] = '+';
690 ubuf[1] = '\0';
691 }
692
693 sendto_one(client_p, "NICK %s %d %ld %s %s %s %s :%s",
694 target_p->name, target_p->hopcount + 1,
695 (long) target_p->tsinfo, ubuf,
696 target_p->username, target_p->host,
697 target_p->user->server, target_p->info);
698
699 if(IsDynSpoof(target_p))
700 sendto_one(client_p, ":%s ENCAP * REALHOST %s",
701 target_p->name, target_p->orighost);
702 if(!EmptyString(target_p->user->suser))
703 sendto_one(client_p, ":%s ENCAP * LOGIN %s",
704 target_p->name, target_p->user->suser);
705
706 if(ConfigFileEntry.burst_away && !EmptyString(target_p->user->away))
707 sendto_one(client_p, ":%s AWAY :%s",
708 target_p->name, target_p->user->away);
709
710 hclientinfo.target = target_p;
711 call_hook(h_burst_client, &hclientinfo);
712 }
713
714 DLINK_FOREACH(ptr, global_channel_list.head)
715 {
716 chptr = ptr->data;
717
718 if(*chptr->chname != '#')
719 continue;
720
721 cur_len = mlen = ircsprintf(buf, ":%s SJOIN %ld %s %s :", me.name,
722 (long) chptr->channelts, chptr->chname,
723 channel_modes(chptr, client_p));
724
725 t = buf + mlen;
726
727 DLINK_FOREACH(uptr, chptr->members.head)
728 {
729 msptr = uptr->data;
730
731 tlen = strlen(msptr->client_p->name) + 1;
732 if(is_chanop(msptr))
733 tlen++;
734 if(is_voiced(msptr))
735 tlen++;
736
737 if(cur_len + tlen >= BUFSIZE - 3)
738 {
739 t--;
740 *t = '\0';
741 sendto_one(client_p, "%s", buf);
742 cur_len = mlen;
743 t = buf + mlen;
744 }
745
746 ircsprintf(t, "%s%s ", find_channel_status(msptr, 1),
747 msptr->client_p->name);
748
749 cur_len += tlen;
750 t += tlen;
751 }
752
753 if (dlink_list_length(&chptr->members) > 0)
754 {
755 /* remove trailing space */
756 t--;
757 *t = '\0';
758 }
759 sendto_one(client_p, "%s", buf);
760
761 burst_modes_TS5(client_p, chptr->chname, &chptr->banlist, 'b');
762
763 if(IsCapable(client_p, CAP_EX))
764 burst_modes_TS5(client_p, chptr->chname, &chptr->exceptlist, 'e');
765
766 if(IsCapable(client_p, CAP_IE))
767 burst_modes_TS5(client_p, chptr->chname, &chptr->invexlist, 'I');
768
769 burst_modes_TS5(client_p, chptr->chname, &chptr->quietlist, 'q');
770
771 if(IsCapable(client_p, CAP_TB) && chptr->topic != NULL)
772 sendto_one(client_p, ":%s TB %s %ld %s%s:%s",
773 me.name, chptr->chname, (long) chptr->topic_time,
774 ConfigChannel.burst_topicwho ? chptr->topic_info : "",
775 ConfigChannel.burst_topicwho ? " " : "",
776 chptr->topic);
777
778 hchaninfo.chptr = chptr;
779 call_hook(h_burst_channel, &hchaninfo);
780 }
781
782 hclientinfo.target = NULL;
783 call_hook(h_burst_finished, &hclientinfo);
784}
785
786/*
787 * burst_TS6
788 *
789 * inputs - client (server) to send nick towards
790 * - client to send nick for
791 * output - NONE
792 * side effects - NICK message is sent towards given client_p
793 */
794static void
795burst_TS6(struct Client *client_p)
796{
797 static char ubuf[12];
798 struct Client *target_p;
799 struct Channel *chptr;
800 struct membership *msptr;
801 hook_data_client hclientinfo;
802 hook_data_channel hchaninfo;
803 dlink_node *ptr;
804 dlink_node *uptr;
805 char *t;
806 int tlen, mlen;
807 int cur_len = 0;
808
809 hclientinfo.client = hchaninfo.client = client_p;
810
811 DLINK_FOREACH(ptr, global_client_list.head)
812 {
813 target_p = ptr->data;
814
815 if(!IsPerson(target_p))
816 continue;
817
818 send_umode(NULL, target_p, 0, 0, ubuf);
819 if(!*ubuf)
820 {
821 ubuf[0] = '+';
822 ubuf[1] = '\0';
823 }
824
825 if(has_id(target_p) && IsCapable(client_p, CAP_EUID))
826 sendto_one(client_p, ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
827 target_p->servptr->id, target_p->name,
828 target_p->hopcount + 1,
829 (long) target_p->tsinfo, ubuf,
830 target_p->username, target_p->host,
831 IsIPSpoof(target_p) ? "0" : target_p->sockhost,
832 target_p->id,
833 IsDynSpoof(target_p) ? target_p->orighost : "*",
834 EmptyString(target_p->user->suser) ? "*" : target_p->user->suser,
835 target_p->info);
836 else if(has_id(target_p))
837 sendto_one(client_p, ":%s UID %s %d %ld %s %s %s %s %s :%s",
838 target_p->servptr->id, target_p->name,
839 target_p->hopcount + 1,
840 (long) target_p->tsinfo, ubuf,
841 target_p->username, target_p->host,
842 IsIPSpoof(target_p) ? "0" : target_p->sockhost,
843 target_p->id, target_p->info);
844 else
845 sendto_one(client_p, "NICK %s %d %ld %s %s %s %s :%s",
846 target_p->name,
847 target_p->hopcount + 1,
848 (long) target_p->tsinfo,
849 ubuf,
850 target_p->username, target_p->host,
851 target_p->user->server, target_p->info);
852
853 if(!has_id(target_p) || !IsCapable(client_p, CAP_EUID))
854 {
855 if(IsDynSpoof(target_p))
856 sendto_one(client_p, ":%s ENCAP * REALHOST %s",
857 use_id(target_p), target_p->orighost);
858 if(!EmptyString(target_p->user->suser))
859 sendto_one(client_p, ":%s ENCAP * LOGIN %s",
860 use_id(target_p), target_p->user->suser);
861 }
862
863 if(ConfigFileEntry.burst_away && !EmptyString(target_p->user->away))
864 sendto_one(client_p, ":%s AWAY :%s",
865 use_id(target_p),
866 target_p->user->away);
867
868 hclientinfo.target = target_p;
869 call_hook(h_burst_client, &hclientinfo);
870 }
871
872 DLINK_FOREACH(ptr, global_channel_list.head)
873 {
874 chptr = ptr->data;
875
876 if(*chptr->chname != '#')
877 continue;
878
879 cur_len = mlen = ircsprintf(buf, ":%s SJOIN %ld %s %s :", me.id,
880 (long) chptr->channelts, chptr->chname,
881 channel_modes(chptr, client_p));
882
883 t = buf + mlen;
884
885 DLINK_FOREACH(uptr, chptr->members.head)
886 {
887 msptr = uptr->data;
888
889 tlen = strlen(use_id(msptr->client_p)) + 1;
890 if(is_chanop(msptr))
891 tlen++;
892 if(is_voiced(msptr))
893 tlen++;
894
895 if(cur_len + tlen >= BUFSIZE - 3)
896 {
897 *(t-1) = '\0';
898 sendto_one(client_p, "%s", buf);
899 cur_len = mlen;
900 t = buf + mlen;
901 }
902
903 ircsprintf(t, "%s%s ", find_channel_status(msptr, 1),
904 use_id(msptr->client_p));
905
906 cur_len += tlen;
907 t += tlen;
908 }
909
910 if (dlink_list_length(&chptr->members) > 0)
911 {
912 /* remove trailing space */
913 *(t-1) = '\0';
914 }
915 sendto_one(client_p, "%s", buf);
916
917 if(dlink_list_length(&chptr->banlist) > 0)
918 burst_modes_TS6(client_p, chptr, &chptr->banlist, 'b');
919
920 if(IsCapable(client_p, CAP_EX) &&
921 dlink_list_length(&chptr->exceptlist) > 0)
922 burst_modes_TS6(client_p, chptr, &chptr->exceptlist, 'e');
923
924 if(IsCapable(client_p, CAP_IE) &&
925 dlink_list_length(&chptr->invexlist) > 0)
926 burst_modes_TS6(client_p, chptr, &chptr->invexlist, 'I');
927
928 if(dlink_list_length(&chptr->quietlist) > 0)
929 burst_modes_TS6(client_p, chptr, &chptr->quietlist, 'q');
930
931 if(IsCapable(client_p, CAP_TB) && chptr->topic != NULL)
932 sendto_one(client_p, ":%s TB %s %ld %s%s:%s",
933 me.id, chptr->chname, (long) chptr->topic_time,
934 ConfigChannel.burst_topicwho ? chptr->topic_info : "",
935 ConfigChannel.burst_topicwho ? " " : "",
936 chptr->topic);
937
938 hchaninfo.chptr = chptr;
939 call_hook(h_burst_channel, &hchaninfo);
940 }
941
942 hclientinfo.target = NULL;
943 call_hook(h_burst_finished, &hclientinfo);
944}
945
946/*
947 * show_capabilities - show current server capabilities
948 *
949 * inputs - pointer to an struct Client
950 * output - pointer to static string
951 * side effects - build up string representing capabilities of server listed
952 */
953const char *
954show_capabilities(struct Client *target_p)
955{
956 static char msgbuf[BUFSIZE];
957 struct Capability *cap;
958 char *t;
959 int tl;
960
961 t = msgbuf;
962 tl = ircsprintf(msgbuf, "TS ");
963 t += tl;
964
965 if(!IsServer(target_p) || !target_p->serv->caps) /* short circuit if no caps */
966 {
967 msgbuf[2] = '\0';
968 return msgbuf;
969 }
970
971 for (cap = captab; cap->cap; ++cap)
972 {
973 if(cap->cap & target_p->serv->caps)
974 {
975 tl = ircsprintf(t, "%s ", cap->name);
976 t += tl;
977 }
978 }
979
980 t--;
981 *t = '\0';
982
983 return msgbuf;
984}
985
986/*
987 * server_estab
988 *
989 * inputs - pointer to a struct Client
990 * output -
991 * side effects -
992 */
993int
994server_estab(struct Client *client_p)
995{
996 struct Client *target_p;
997 struct server_conf *server_p;
998 hook_data_client hdata;
999 char *host;
1000 dlink_node *ptr;
1001
1002 s_assert(NULL != client_p);
1003 if(client_p == NULL)
1004 return -1;
1005 ClearAccess(client_p);
1006
1007 host = client_p->name;
1008
1009 if((server_p = client_p->localClient->att_sconf) == NULL)
1010 {
1011 /* This shouldn't happen, better tell the ops... -A1kmm */
1012 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1013 "Warning: Lost connect{} block for server %s!", host);
1014 return exit_client(client_p, client_p, client_p, "Lost connect{} block!");
1015 }
1016
1017 /* We shouldn't have to check this, it should already done before
1018 * server_estab is called. -A1kmm
1019 */
1020 if(client_p->localClient->passwd)
1021 {
1022 memset(client_p->localClient->passwd, 0, strlen(client_p->localClient->passwd));
1023 MyFree(client_p->localClient->passwd);
1024 client_p->localClient->passwd = NULL;
1025 }
1026
1027 /* Its got identd , since its a server */
1028 SetGotId(client_p);
1029
1030 /* If there is something in the serv_list, it might be this
1031 * connecting server..
1032 */
1033 if(!ServerInfo.hub && serv_list.head)
1034 {
1035 if(client_p != serv_list.head->data || serv_list.head->next)
1036 {
1037 ServerStats->is_ref++;
1038 sendto_one(client_p, "ERROR :I'm a leaf not a hub");
1039 return exit_client(client_p, client_p, client_p, "I'm a leaf");
1040 }
1041 }
1042
1043 if(IsUnknown(client_p))
1044 {
1045 /*
1046 * jdc -- 1. Use EmptyString(), not [0] index reference.
1047 * 2. Check ->spasswd, not ->passwd.
1048 */
1049 if(!EmptyString(server_p->spasswd))
1050 {
1051 /* kludge, if we're not using TS6, dont ever send
1052 * ourselves as being TS6 capable.
1053 */
1054 if(ServerInfo.use_ts6)
1055 sendto_one(client_p, "PASS %s TS %d :%s",
1056 server_p->spasswd, TS_CURRENT, me.id);
1057 else
1058 sendto_one(client_p, "PASS %s :TS",
1059 server_p->spasswd);
1060 }
1061
1062 /* pass info to new server */
1063 send_capabilities(client_p, default_server_capabs
1064 | (ServerConfCompressed(server_p) ? CAP_ZIP_SUPPORTED : 0)
1065 | (ServerConfTb(server_p) ? CAP_TB : 0));
1066
1067 sendto_one(client_p, "SERVER %s 1 :%s%s",
1068 me.name,
1069 ConfigServerHide.hidden ? "(H) " : "",
1070 (me.info[0]) ? (me.info) : "IRCers United");
1071 }
1072
1073 if(!comm_set_buffers(client_p->localClient->fd, READBUF_SIZE))
1074 report_error(SETBUF_ERROR_MSG,
1075 get_server_name(client_p, SHOW_IP),
1076 log_client_name(client_p, SHOW_IP), errno);
1077
1078 /* Hand the server off to servlink now */
1079 if(IsCapable(client_p, CAP_ZIP))
1080 {
1081 if(fork_server(client_p) < 0)
1082 {
1083 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1084 "Warning: fork failed for server %s -- check servlink_path (%s)",
1085 get_server_name(client_p, HIDE_IP),
1086 ConfigFileEntry.servlink_path);
1087 return exit_client(client_p, client_p, client_p, "Fork failed");
1088 }
1089 start_io(client_p);
1090 SetServlink(client_p);
1091 }
1092
1093 sendto_one(client_p, "SVINFO %d %d 0 :%ld", TS_CURRENT, TS_MIN, CurrentTime);
1094
1095 client_p->servptr = &me;
1096
1097 if(IsAnyDead(client_p))
1098 return CLIENT_EXITED;
1099
1100 SetServer(client_p);
1101
1102 /* Update the capability combination usage counts */
1103 set_chcap_usage_counts(client_p);
1104
1105 dlinkAdd(client_p, &client_p->lnode, &me.serv->servers);
1106 dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &serv_list);
1107 dlinkAddTailAlloc(client_p, &global_serv_list);
1108
1109 if(has_id(client_p))
1110 add_to_id_hash(client_p->id, client_p);
1111
1112 add_to_client_hash(client_p->name, client_p);
1113 /* doesnt duplicate client_p->serv if allocated this struct already */
1114 make_server(client_p);
1115 client_p->serv->up = me.name;
1116 client_p->serv->upid = me.id;
1117
1118 client_p->serv->caps = client_p->localClient->caps;
1119
1120 if(client_p->localClient->fullcaps)
1121 {
1122 DupString(client_p->serv->fullcaps, client_p->localClient->fullcaps);
1123 MyFree(client_p->localClient->fullcaps);
1124 client_p->localClient->fullcaps = NULL;
1125 }
1126
1127 /* add it to scache */
1128 find_or_add(client_p->name);
1129 client_p->localClient->firsttime = CurrentTime;
1130 /* fixing eob timings.. -gnp */
1131
1132 /* Show the real host/IP to admins */
1133 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1134 "Link with %s established: (%s) link",
1135 get_server_name(client_p, SHOW_IP),
1136 show_capabilities(client_p));
1137
1138 ilog(L_SERVER, "Link with %s established: (%s) link",
1139 log_client_name(client_p, SHOW_IP), show_capabilities(client_p));
1140
1141 hdata.client = &me;
1142 hdata.target = client_p;
1143 call_hook(h_server_introduced, &hdata);
1144
1145 if(HasServlink(client_p))
1146 {
1147 /* we won't overflow FD_DESC_SZ here, as it can hold
1148 * client_p->name + 64
1149 */
1150 comm_note(client_p->localClient->fd, "slink data: %s", client_p->name);
1151 comm_note(client_p->localClient->ctrlfd, "slink ctrl: %s", client_p->name);
1152 }
1153 else
1154 comm_note(client_p->localClient->fd, "Server: %s", client_p->name);
1155
1156 /*
1157 ** Old sendto_serv_but_one() call removed because we now
1158 ** need to send different names to different servers
1159 ** (domain name matching) Send new server to other servers.
1160 */
1161 DLINK_FOREACH(ptr, serv_list.head)
1162 {
1163 target_p = ptr->data;
1164
1165 if(target_p == client_p)
1166 continue;
1167
1168 if(has_id(target_p) && has_id(client_p))
1169 {
1170 sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
1171 me.id, client_p->name, client_p->id,
1172 IsHidden(client_p) ? "(H) " : "", client_p->info);
1173
1174 if(IsCapable(target_p, CAP_ENCAP) &&
1175 !EmptyString(client_p->serv->fullcaps))
1176 sendto_one(target_p, ":%s ENCAP * GCAP :%s",
1177 client_p->id, client_p->serv->fullcaps);
1178 }
1179 else
1180 {
1181 sendto_one(target_p, ":%s SERVER %s 2 :%s%s",
1182 me.name, client_p->name,
1183 IsHidden(client_p) ? "(H) " : "", client_p->info);
1184
1185 if(IsCapable(target_p, CAP_ENCAP) &&
1186 !EmptyString(client_p->serv->fullcaps))
1187 sendto_one(target_p, ":%s ENCAP * GCAP :%s",
1188 client_p->name, client_p->serv->fullcaps);
1189 }
1190 }
1191
1192 /*
1193 ** Pass on my client information to the new server
1194 **
1195 ** First, pass only servers (idea is that if the link gets
1196 ** cancelled beacause the server was already there,
1197 ** there are no NICK's to be cancelled...). Of course,
1198 ** if cancellation occurs, all this info is sent anyway,
1199 ** and I guess the link dies when a read is attempted...? --msa
1200 **
1201 ** Note: Link cancellation to occur at this point means
1202 ** that at least two servers from my fragment are building
1203 ** up connection this other fragment at the same time, it's
1204 ** a race condition, not the normal way of operation...
1205 **
1206 ** ALSO NOTE: using the get_client_name for server names--
1207 ** see previous *WARNING*!!! (Also, original inpath
1208 ** is destroyed...)
1209 */
1210 DLINK_FOREACH(ptr, global_serv_list.head)
1211 {
1212 target_p = ptr->data;
1213
1214 /* target_p->from == target_p for target_p == client_p */
1215 if(IsMe(target_p) || target_p->from == client_p)
1216 continue;
1217
1218 /* presumption, if target has an id, so does its uplink */
1219 if(has_id(client_p) && has_id(target_p))
1220 sendto_one(client_p, ":%s SID %s %d %s :%s%s",
1221 target_p->serv->upid, target_p->name,
1222 target_p->hopcount + 1, target_p->id,
1223 IsHidden(target_p) ? "(H) " : "", target_p->info);
1224 else
1225 sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1226 target_p->serv->up,
1227 target_p->name, target_p->hopcount + 1,
1228 IsHidden(target_p) ? "(H) " : "", target_p->info);
1229
1230 if(IsCapable(client_p, CAP_ENCAP) &&
1231 !EmptyString(target_p->serv->fullcaps))
1232 sendto_one(client_p, ":%s ENCAP * GCAP :%s",
1233 get_id(target_p, client_p),
1234 target_p->serv->fullcaps);
1235 }
1236
1237 if(has_id(client_p))
1238 burst_TS6(client_p);
1239 else
1240 burst_TS5(client_p);
1241
1242 /* Always send a PING after connect burst is done */
1243 sendto_one(client_p, "PING :%s", get_id(&me, client_p));
1244
1245 free_pre_client(client_p);
1246
1247 return 0;
1248}
1249
1250static void
1251start_io(struct Client *server)
1252{
1253 unsigned char *iobuf;
1254 int c = 0;
1255 int linecount = 0;
1256 int linelen;
1257
1258 iobuf = MyMalloc(256); /* XXX: This seems arbitrary. Perhaps make it IRCD_BUFSIZE? --nenolod */
1259
1260 if(IsCapable(server, CAP_ZIP))
1261 {
1262 /* ziplink */
1263 iobuf[c++] = SLINKCMD_SET_ZIP_OUT_LEVEL;
1264 iobuf[c++] = 0; /* | */
1265 iobuf[c++] = 1; /* \ len is 1 */
1266 iobuf[c++] = ConfigFileEntry.compression_level;
1267 iobuf[c++] = SLINKCMD_START_ZIP_IN;
1268 iobuf[c++] = SLINKCMD_START_ZIP_OUT;
1269 }
1270
1271 while (MyConnect(server))
1272 {
1273 linecount++;
1274
1275 iobuf = MyRealloc(iobuf, (c + READBUF_SIZE + 64));
1276
1277 /* store data in c+3 to allow for SLINKCMD_INJECT_RECVQ and len u16 */
1278 linelen = linebuf_get(&server->localClient->buf_recvq, (char *) (iobuf + c + 3), READBUF_SIZE, LINEBUF_PARTIAL, LINEBUF_RAW); /* include partial lines */
1279
1280 if(linelen)
1281 {
1282 iobuf[c++] = SLINKCMD_INJECT_RECVQ;
1283 iobuf[c++] = (linelen >> 8);
1284 iobuf[c++] = (linelen & 0xff);
1285 c += linelen;
1286 }
1287 else
1288 break;
1289 }
1290
1291 while (MyConnect(server))
1292 {
1293 linecount++;
1294
1295 iobuf = MyRealloc(iobuf, (c + BUF_DATA_SIZE + 64));
1296
1297 /* store data in c+3 to allow for SLINKCMD_INJECT_RECVQ and len u16 */
1298 linelen = linebuf_get(&server->localClient->buf_sendq,
1299 (char *) (iobuf + c + 3), READBUF_SIZE,
1300 LINEBUF_PARTIAL, LINEBUF_PARSED); /* include partial lines */
1301
1302 if(linelen)
1303 {
1304 iobuf[c++] = SLINKCMD_INJECT_SENDQ;
1305 iobuf[c++] = (linelen >> 8);
1306 iobuf[c++] = (linelen & 0xff);
1307 c += linelen;
1308 }
1309 else
1310 break;
1311 }
1312
1313 /* start io */
1314 iobuf[c++] = SLINKCMD_INIT;
1315
1316 server->localClient->slinkq = iobuf;
1317 server->localClient->slinkq_ofs = 0;
1318 server->localClient->slinkq_len = c;
1319
1320 /* schedule a write */
1321 send_queued_slink_write(server->localClient->ctrlfd, server);
1322}
1323
1324/*
1325 * fork_server
1326 *
1327 * inputs - struct Client *server
1328 * output - success: 0 / failure: -1
1329 * side effect - fork, and exec SERVLINK to handle this connection
1330 */
1331static int
1332fork_server(struct Client *server)
1333{
1334 int ret;
1335 int i;
1336 int ctrl_fds[2];
1337 int data_fds[2];
1338
1339 char fd_str[4][6];
1340 char *kid_argv[7];
1341 char slink[] = "-slink";
1342
1343
1344 /* ctrl */
1345#ifdef HAVE_SOCKETPAIR
1346 if(socketpair(AF_UNIX, SOCK_STREAM, 0, ctrl_fds) < 0)
1347#else
1348 if(inet_socketpair(AF_INET,SOCK_STREAM, 0, ctrl_fds) < 0)
1349#endif
1350 goto fork_error;
1351
1352
1353
1354 /* data */
1355#ifdef HAVE_SOCKETPAIR
1356 if(socketpair(AF_UNIX, SOCK_STREAM, 0, data_fds) < 0)
1357#else
1358 if(inet_socketpair(AF_INET,SOCK_STREAM, 0, data_fds) < 0)
1359#endif
1360 goto fork_error;
1361
1362
1363#ifdef __CYGWIN__
1364 if((ret = vfork()) < 0)
1365#else
1366 if((ret = fork()) < 0)
1367#endif
1368 goto fork_error;
1369 else if(ret == 0)
1370 {
1371 /* set our fds as non blocking and close everything else */
1372 for (i = 0; i < HARD_FDLIMIT; i++)
1373 {
1374
1375
1376 if((i == ctrl_fds[1]) || (i == data_fds[1]) || (i == server->localClient->fd))
1377 {
1378 comm_set_nb(i);
1379 }
1380 else
1381 {
1382#ifdef __CYGWIN__
1383 if(i > 2) /* don't close std* */
1384#endif
1385 close(i);
1386 }
1387 }
1388
1389 ircsnprintf(fd_str[0], sizeof(fd_str[0]), "%d", ctrl_fds[1]);
1390 ircsnprintf(fd_str[1], sizeof(fd_str[1]), "%d", data_fds[1]);
1391 ircsnprintf(fd_str[2], sizeof(fd_str[2]), "%d", server->localClient->fd);
1392 kid_argv[0] = slink;
1393 kid_argv[1] = fd_str[0];
1394 kid_argv[2] = fd_str[1];
1395 kid_argv[3] = fd_str[2];
1396 kid_argv[4] = NULL;
1397
1398 /* exec servlink program */
1399 execv(ConfigFileEntry.servlink_path, kid_argv);
1400
1401 /* We're still here, abort. */
1402 _exit(1);
1403 }
1404 else
1405 {
1406 comm_close(server->localClient->fd);
1407
1408 /* close the childs end of the pipes */
1409 close(ctrl_fds[1]);
1410 close(data_fds[1]);
1411
1412 s_assert(server->localClient);
1413 server->localClient->ctrlfd = ctrl_fds[0];
1414 server->localClient->fd = data_fds[0];
1415
1416 if(!comm_set_nb(server->localClient->fd))
1417 {
1418 report_error(NONB_ERROR_MSG,
1419 get_server_name(server, SHOW_IP),
1420 log_client_name(server, SHOW_IP),
1421 errno);
1422 }
1423
1424 if(!comm_set_nb(server->localClient->ctrlfd))
1425 {
1426 report_error(NONB_ERROR_MSG,
1427 get_server_name(server, SHOW_IP),
1428 log_client_name(server, SHOW_IP),
1429 errno);
1430 }
1431
1432 comm_open(server->localClient->ctrlfd, FD_SOCKET, NULL);
1433 comm_open(server->localClient->fd, FD_SOCKET, NULL);
1434
1435 read_ctrl_packet(server->localClient->ctrlfd, server);
1436 read_packet(server->localClient->fd, server);
1437 }
1438
1439 return 0;
1440
1441 fork_error:
1442 /* this is ugly, but nicer than repeating
1443 * about 50 close() statements everywhre... */
1444 close(data_fds[0]);
1445 close(data_fds[1]);
1446 close(ctrl_fds[0]);
1447 close(ctrl_fds[1]);
1448 return -1;
1449}
1450
1451/*
1452 * New server connection code
1453 * Based upon the stuff floating about in s_bsd.c
1454 * -- adrian
1455 */
1456
1457/*
1458 * serv_connect() - initiate a server connection
1459 *
1460 * inputs - pointer to conf
1461 * - pointer to client doing the connet
1462 * output -
1463 * side effects -
1464 *
1465 * This code initiates a connection to a server. It first checks to make
1466 * sure the given server exists. If this is the case, it creates a socket,
1467 * creates a client, saves the socket information in the client, and
1468 * initiates a connection to the server through comm_connect_tcp(). The
1469 * completion of this goes through serv_completed_connection().
1470 *
1471 * We return 1 if the connection is attempted, since we don't know whether
1472 * it suceeded or not, and 0 if it fails in here somewhere.
1473 */
1474int
1475serv_connect(struct server_conf *server_p, struct Client *by)
1476{
1477 struct Client *client_p;
1478 struct irc_sockaddr_storage myipnum;
1479 int fd;
1480
1481 s_assert(server_p != NULL);
1482 if(server_p == NULL)
1483 return 0;
1484
1485 /*
1486 * Make sure this server isn't already connected
1487 */
1488 if((client_p = find_server(NULL, server_p->name)))
1489 {
1490 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1491 "Server %s already present from %s",
1492 server_p->name, get_server_name(client_p, SHOW_IP));
1493 if(by && IsPerson(by) && !MyClient(by))
1494 sendto_one_notice(by, ":Server %s already present from %s",
1495 server_p->name, get_server_name(client_p, SHOW_IP));
1496 return 0;
1497 }
1498
1499 /* create a socket for the server connection */
1500 if((fd = comm_socket(server_p->aftype, SOCK_STREAM, 0, NULL)) < 0)
1501 {
1502 /* Eek, failure to create the socket */
1503 report_error("opening stream socket to %s: %s",
1504 server_p->name, server_p->name, errno);
1505 return 0;
1506 }
1507
1508 /* servernames are always guaranteed under HOSTLEN chars */
1509 comm_note(fd, "Server: %s", server_p->name);
1510
1511 /* Create a local client */
1512 client_p = make_client(NULL);
1513
1514 /* Copy in the server, hostname, fd
1515 * The sockhost may be a hostname, this will be corrected later
1516 * -- jilles
1517 */
1518 strlcpy(client_p->name, server_p->name, sizeof(client_p->name));
1519 strlcpy(client_p->host, server_p->host, sizeof(client_p->host));
1520 strlcpy(client_p->sockhost, server_p->host, sizeof(client_p->sockhost));
1521 client_p->localClient->fd = fd;
1522
1523 /*
1524 * Set up the initial server evilness, ripped straight from
1525 * connect_server(), so don't blame me for it being evil.
1526 * -- adrian
1527 */
1528
1529 if(!comm_set_buffers(client_p->localClient->fd, READBUF_SIZE))
1530 {
1531 report_error(SETBUF_ERROR_MSG,
1532 get_server_name(client_p, SHOW_IP),
1533 log_client_name(client_p, SHOW_IP),
1534 errno);
1535 }
1536
1537 /*
1538 * Attach config entries to client here rather than in
1539 * serv_connect_callback(). This to avoid null pointer references.
1540 */
1541 attach_server_conf(client_p, server_p);
1542
1543 /*
1544 * at this point we have a connection in progress and C/N lines
1545 * attached to the client, the socket info should be saved in the
1546 * client and it should either be resolved or have a valid address.
1547 *
1548 * The socket has been connected or connect is in progress.
1549 */
1550 make_server(client_p);
1551 if(by && IsPerson(by))
1552 {
1553 strcpy(client_p->serv->by, by->name);
1554 if(client_p->serv->user)
1555 free_user(client_p->serv->user, NULL);
1556 client_p->serv->user = by->user;
1557 by->user->refcnt++;
1558 }
1559 else
1560 {
1561 strcpy(client_p->serv->by, "AutoConn.");
1562 if(client_p->serv->user)
1563 free_user(client_p->serv->user, NULL);
1564 client_p->serv->user = NULL;
1565 }
1566 client_p->serv->up = me.name;
1567 client_p->serv->upid = me.id;
1568 SetConnecting(client_p);
1569 dlinkAddTail(client_p, &client_p->node, &global_client_list);
1570
1571 /* log */
1572 ilog(L_SERVER, "Connecting to %s[%s] port %d (%s)", server_p->name, server_p->host, server_p->port,
1573#ifdef IPV6
1574 server_p->aftype == AF_INET6 ? "IPv6" :
1575#endif
1576 (server_p->aftype == AF_INET ? "IPv4" : "?"));
1577
1578 if(ServerConfVhosted(server_p))
1579 {
1580 memcpy(&myipnum, &server_p->my_ipnum, sizeof(myipnum));
1581 ((struct sockaddr_in *)&myipnum)->sin_port = 0;
1582 myipnum.ss_family = server_p->aftype;
1583
1584 }
1585 else if(server_p->aftype == AF_INET && ServerInfo.specific_ipv4_vhost)
1586 {
1587 memcpy(&myipnum, &ServerInfo.ip, sizeof(myipnum));
1588 ((struct sockaddr_in *)&myipnum)->sin_port = 0;
1589 myipnum.ss_family = AF_INET;
1590 SET_SS_LEN(myipnum, sizeof(struct sockaddr_in));
1591 }
1592
1593#ifdef IPV6
1594 else if((server_p->aftype == AF_INET6) && ServerInfo.specific_ipv6_vhost)
1595 {
1596 memcpy(&myipnum, &ServerInfo.ip6, sizeof(myipnum));
1597 ((struct sockaddr_in6 *)&myipnum)->sin6_port = 0;
1598 myipnum.ss_family = AF_INET6;
1599 SET_SS_LEN(myipnum, sizeof(struct sockaddr_in6));
1600 }
1601#endif
1602 else
1603 {
1604 comm_connect_tcp(client_p->localClient->fd, server_p->host,
1605 server_p->port, NULL, 0, serv_connect_callback,
1606 client_p, server_p->aftype,
1607 ConfigFileEntry.connect_timeout);
1608 return 1;
1609 }
1610
1611 comm_connect_tcp(client_p->localClient->fd, server_p->host,
1612 server_p->port, (struct sockaddr *) &myipnum,
1613 GET_SS_LEN(myipnum), serv_connect_callback, client_p,
1614 myipnum.ss_family, ConfigFileEntry.connect_timeout);
1615
1616 return 1;
1617}
1618
1619/*
1620 * serv_connect_callback() - complete a server connection.
1621 *
1622 * This routine is called after the server connection attempt has
1623 * completed. If unsucessful, an error is sent to ops and the client
1624 * is closed. If sucessful, it goes through the initialisation/check
1625 * procedures, the capabilities are sent, and the socket is then
1626 * marked for reading.
1627 */
1628static void
1629serv_connect_callback(int fd, int status, void *data)
1630{
1631 struct Client *client_p = data;
1632 struct server_conf *server_p;
1633 char *errstr;
1634
1635 /* First, make sure its a real client! */
1636 s_assert(client_p != NULL);
1637 s_assert(client_p->localClient->fd == fd);
1638
1639 if(client_p == NULL)
1640 return;
1641
1642 /* while we were waiting for the callback, its possible this already
1643 * linked in.. --fl
1644 */
1645 if(find_server(NULL, client_p->name) != NULL)
1646 {
1647 exit_client(client_p, client_p, &me, "Server Exists");
1648 return;
1649 }
1650
1651 /* Next, for backward purposes, record the ip of the server */
1652 memcpy(&client_p->localClient->ip, &fd_table[fd].connect.hostaddr, sizeof client_p->localClient->ip);
1653 /* Set sockhost properly now -- jilles */
1654 inetntop_sock((struct sockaddr *)&fd_table[fd].connect.hostaddr,
1655 client_p->sockhost, sizeof client_p->sockhost);
1656
1657 /* Check the status */
1658 if(status != COMM_OK)
1659 {
1660 /* COMM_ERR_TIMEOUT wont have an errno associated with it,
1661 * the others will.. --fl
1662 */
1663 if(status == COMM_ERR_TIMEOUT)
1664 {
1665 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1666 "Error connecting to %s[%s]: %s",
1667 client_p->name,
1668#ifdef HIDE_SERVERS_IPS
1669 "255.255.255.255",
1670#else
1671 client_p->host,
1672#endif
1673 comm_errstr(status));
1674 ilog(L_SERVER, "Error connecting to %s[%s]: %s",
1675 client_p->name, client_p->sockhost,
1676 comm_errstr(status));
1677 }
1678 else
1679 {
1680 errstr = strerror(comm_get_sockerr(fd));
1681 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1682 "Error connecting to %s[%s]: %s (%s)",
1683 client_p->name,
1684#ifdef HIDE_SERVERS_IPS
1685 "255.255.255.255",
1686#else
1687 client_p->host,
1688#endif
1689 comm_errstr(status), errstr);
1690 ilog(L_SERVER, "Error connecting to %s[%s]: %s (%s)",
1691 client_p->name, client_p->sockhost,
1692 comm_errstr(status), errstr);
1693 }
1694
1695 exit_client(client_p, client_p, &me, comm_errstr(status));
1696 return;
1697 }
1698
1699 /* COMM_OK, so continue the connection procedure */
1700 /* Get the C/N lines */
1701 if((server_p = client_p->localClient->att_sconf) == NULL)
1702 {
1703 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, "Lost connect{} block for %s",
1704 get_server_name(client_p, HIDE_IP));
1705 exit_client(client_p, client_p, &me, "Lost connect{} block");
1706 return;
1707 }
1708
1709 /* Next, send the initial handshake */
1710 SetHandshake(client_p);
1711
1712 /* kludge, if we're not using TS6, dont ever send
1713 * ourselves as being TS6 capable.
1714 */
1715 if(!EmptyString(server_p->spasswd))
1716 {
1717 if(ServerInfo.use_ts6)
1718 sendto_one(client_p, "PASS %s TS %d :%s",
1719 server_p->spasswd, TS_CURRENT, me.id);
1720 else
1721 sendto_one(client_p, "PASS %s :TS",
1722 server_p->spasswd);
1723 }
1724
1725 /* pass my info to the new server */
1726 send_capabilities(client_p, default_server_capabs
1727 | (ServerConfCompressed(server_p) ? CAP_ZIP_SUPPORTED : 0)
1728 | (ServerConfTb(server_p) ? CAP_TB : 0));
1729
1730 sendto_one(client_p, "SERVER %s 1 :%s%s",
1731 me.name,
1732 ConfigServerHide.hidden ? "(H) " : "", me.info);
1733
1734 /*
1735 * If we've been marked dead because a send failed, just exit
1736 * here now and save everyone the trouble of us ever existing.
1737 */
1738 if(IsAnyDead(client_p))
1739 {
1740 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1741 "%s went dead during handshake", client_p->name);
1742 exit_client(client_p, client_p, &me, "Went dead during handshake");
1743 return;
1744 }
1745
1746 /* don't move to serv_list yet -- we haven't sent a burst! */
1747
1748 /* If we get here, we're ok, so lets start reading some data */
1749 read_packet(fd, client_p);
1750}
1751
1752#ifndef HAVE_SOCKETPAIR
1753static int
1754inet_socketpair(int d, int type, int protocol, int sv[2])
1755{
1756 struct sockaddr_in addr1, addr2, addr3;
1757 int addr3_len = sizeof(addr3);
1758 int fd, rc;
1759 int port_no = 20000;
1760
1761 if(d != AF_INET || type != SOCK_STREAM || protocol)
1762 {
1763 errno = EAFNOSUPPORT;
1764 return -1;
1765 }
1766 if(((sv[0] = socket(AF_INET, SOCK_STREAM, 0)) < 0) || ((sv[1] = socket(AF_INET, SOCK_STREAM, 0)) < 0))
1767 return -1;
1768
1769 addr1.sin_port = htons(port_no);
1770 addr1.sin_family = AF_INET;
1771 addr1.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1772 while ((rc = bind (sv[0], (struct sockaddr *) &addr1, sizeof (addr1))) < 0 && errno == EADDRINUSE)
1773 addr1.sin_port = htons(++port_no);
1774
1775 if(rc < 0)
1776 return -1;
1777
1778 if(listen(sv[0], 1) < 0)
1779 {
1780 close(sv[0]);
1781 close(sv[1]);
1782 return -1;
1783 }
1784
1785 addr2.sin_port = htons(port_no);
1786 addr2.sin_family = AF_INET;
1787 addr2.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1788 if(connect (sv[1], (struct sockaddr *) &addr2, sizeof (addr2)) < 0)
1789 {
1790 close(sv[0]);
1791 close(sv[1]);
1792 return -1;
1793 }
1794
1795 if((fd = accept(sv[1], (struct sockaddr *) &addr3, &addr3_len)) < 0)
1796 {
1797 close(sv[0]);
1798 close(sv[1]);
1799 return -1;
1800 }
1801 close(sv[0]);
1802 sv[0] = fd;
1803
1804 return(0);
1805
1806}
1807#endif