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