]> jfr.im git - irc/rqf/shadowircd.git/blob - src/s_serv.c
Give proper error messages if connect fails for a server using SSL.
[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 3550 2007-08-09 06:47:26Z nenolod $
25 */
26
27 #include "stdinc.h"
28
29 #ifdef HAVE_LIBCRYPTO
30 #include <openssl/rsa.h>
31 #endif
32
33 #include "s_serv.h"
34 #include "class.h"
35 #include "client.h"
36 #include "common.h"
37 #include "hash.h"
38 #include "match.h"
39 #include "ircd.h"
40 #include "ircd_defs.h"
41 #include "numeric.h"
42 #include "packet.h"
43 #include "res.h"
44 #include "s_conf.h"
45 #include "s_newconf.h"
46 #include "logger.h"
47 #include "s_stats.h"
48 #include "s_user.h"
49 #include "scache.h"
50 #include "send.h"
51 #include "client.h"
52 #include "channel.h" /* chcap_usage_counts stuff... */
53 #include "hook.h"
54 #include "msg.h"
55 #include "reject.h"
56 #include "sslproc.h"
57
58 extern char *crypt();
59
60 #ifndef INADDR_NONE
61 #define INADDR_NONE ((unsigned int) 0xffffffff)
62 #endif
63
64 #ifndef HAVE_SOCKETPAIR
65 static int inet_socketpair(int d, int type, int protocol, int sv[2]);
66 #endif
67
68 int MaxConnectionCount = 1;
69 int MaxClientCount = 1;
70 int refresh_user_links = 0;
71
72 static char buf[BUFSIZE];
73
74 /*
75 * list of recognized server capabilities. "TS" is not on the list
76 * because all servers that we talk to already do TS, and the kludged
77 * extra argument to "PASS" takes care of checking that. -orabidoo
78 */
79 struct Capability captab[] = {
80 /* name cap */
81 { "QS", CAP_QS },
82 { "EX", CAP_EX },
83 { "CHW", CAP_CHW},
84 { "IE", CAP_IE},
85 { "KLN", CAP_KLN},
86 { "KNOCK", CAP_KNOCK},
87 { "ZIP", CAP_ZIP},
88 { "TB", CAP_TB},
89 { "UNKLN", CAP_UNKLN},
90 { "CLUSTER", CAP_CLUSTER},
91 { "ENCAP", CAP_ENCAP },
92 { "SERVICES", CAP_SERVICE },
93 { "RSFNC", CAP_RSFNC },
94 { "SAVE", CAP_SAVE },
95 { "EUID", CAP_EUID },
96 {0, 0}
97 };
98
99 static CNCB serv_connect_callback;
100 static CNCB serv_connect_ssl_callback;
101
102 /*
103 * hunt_server - Do the basic thing in delivering the message (command)
104 * across the relays to the specific server (server) for
105 * actions.
106 *
107 * Note: The command is a format string and *MUST* be
108 * of prefixed style (e.g. ":%s COMMAND %s ...").
109 * Command can have only max 8 parameters.
110 *
111 * server parv[server] is the parameter identifying the
112 * target server.
113 *
114 * *WARNING*
115 * parv[server] is replaced with the pointer to the
116 * real servername from the matched client (I'm lazy
117 * now --msa).
118 *
119 * returns: (see #defines)
120 */
121 int
122 hunt_server(struct Client *client_p, struct Client *source_p,
123 const char *command, int server, int parc, const char *parv[])
124 {
125 struct Client *target_p;
126 int wilds;
127 rb_dlink_node *ptr;
128 const char *old;
129 char *new;
130
131 /*
132 * Assume it's me, if no server
133 */
134 if(parc <= server || EmptyString(parv[server]) ||
135 match(parv[server], me.name) || (strcmp(parv[server], me.id) == 0))
136 return (HUNTED_ISME);
137
138 new = LOCAL_COPY(parv[server]);
139
140 /*
141 * These are to pickup matches that would cause the following
142 * message to go in the wrong direction while doing quick fast
143 * non-matching lookups.
144 */
145 if(MyClient(source_p))
146 target_p = find_named_client(new);
147 else
148 target_p = find_client(new);
149
150 if(target_p)
151 if(target_p->from == source_p->from && !MyConnect(target_p))
152 target_p = NULL;
153
154 collapse(new);
155 wilds = (strchr(new, '?') || strchr(new, '*'));
156
157 /*
158 * Again, if there are no wild cards involved in the server
159 * name, use the hash lookup
160 */
161 if(!target_p)
162 {
163 if(!wilds)
164 {
165 if(MyClient(source_p) || !IsDigit(parv[server][0]))
166 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
167 form_str(ERR_NOSUCHSERVER),
168 parv[server]);
169 return (HUNTED_NOSUCH);
170 }
171 else
172 {
173 target_p = NULL;
174
175 RB_DLINK_FOREACH(ptr, global_client_list.head)
176 {
177 if(match(new, ((struct Client *) (ptr->data))->name))
178 {
179 target_p = ptr->data;
180 break;
181 }
182 }
183 }
184 }
185
186 if(target_p)
187 {
188 if(!IsRegistered(target_p))
189 {
190 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
191 form_str(ERR_NOSUCHSERVER),
192 parv[server]);
193 return HUNTED_NOSUCH;
194 }
195
196 if(IsMe(target_p) || MyClient(target_p))
197 return HUNTED_ISME;
198
199 old = parv[server];
200 parv[server] = get_id(target_p, target_p);
201
202 sendto_one(target_p, command, get_id(source_p, target_p),
203 parv[1], parv[2], parv[3], parv[4], parv[5], parv[6], parv[7], parv[8]);
204 parv[server] = old;
205 return (HUNTED_PASS);
206 }
207
208 if(MyClient(source_p) || !IsDigit(parv[server][0]))
209 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
210 form_str(ERR_NOSUCHSERVER), parv[server]);
211 return (HUNTED_NOSUCH);
212 }
213
214 /*
215 * try_connections - scan through configuration and try new connections.
216 * Returns the calendar time when the next call to this
217 * function should be made latest. (No harm done if this
218 * is called earlier or later...)
219 */
220 void
221 try_connections(void *unused)
222 {
223 struct Client *client_p;
224 struct server_conf *server_p = NULL;
225 struct server_conf *tmp_p;
226 struct Class *cltmp;
227 rb_dlink_node *ptr;
228 int connecting = FALSE;
229 int confrq = 0;
230 time_t next = 0;
231
232 RB_DLINK_FOREACH(ptr, server_conf_list.head)
233 {
234 tmp_p = ptr->data;
235
236 if(ServerConfIllegal(tmp_p) || !ServerConfAutoconn(tmp_p))
237 continue;
238
239 /* don't allow ssl connections if ssl isn't setup */
240 if(ServerConfSSL(tmp_p) && (!ssl_ok || !get_ssld_count()))
241 continue;
242
243 cltmp = tmp_p->class;
244
245 /*
246 * Skip this entry if the use of it is still on hold until
247 * future. Otherwise handle this entry (and set it on hold
248 * until next time). Will reset only hold times, if already
249 * made one successfull connection... [this algorithm is
250 * a bit fuzzy... -- msa >;) ]
251 */
252 if(tmp_p->hold > rb_current_time())
253 {
254 if(next > tmp_p->hold || next == 0)
255 next = tmp_p->hold;
256 continue;
257 }
258
259 confrq = get_con_freq(cltmp);
260 tmp_p->hold = rb_current_time() + confrq;
261
262 /*
263 * Found a CONNECT config with port specified, scan clients
264 * and see if this server is already connected?
265 */
266 client_p = find_server(NULL, tmp_p->name);
267
268 if(!client_p && (CurrUsers(cltmp) < MaxUsers(cltmp)) && !connecting)
269 {
270 server_p = tmp_p;
271
272 /* We connect only one at time... */
273 connecting = TRUE;
274 }
275
276 if((next > tmp_p->hold) || (next == 0))
277 next = tmp_p->hold;
278 }
279
280 /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
281 if(GlobalSetOptions.autoconn == 0)
282 return;
283
284 if(!connecting)
285 return;
286
287 /* move this connect entry to end.. */
288 rb_dlinkDelete(&server_p->node, &server_conf_list);
289 rb_dlinkAddTail(server_p, &server_p->node, &server_conf_list);
290
291 /*
292 * We used to only print this if serv_connect() actually
293 * suceeded, but since rb_tcp_connect() can call the callback
294 * immediately if there is an error, we were getting error messages
295 * in the wrong order. SO, we just print out the activated line,
296 * and let serv_connect() / serv_connect_callback() print an
297 * error afterwards if it fails.
298 * -- adrian
299 */
300 sendto_realops_snomask(SNO_GENERAL, L_ALL,
301 "Connection to %s activated",
302 server_p->name);
303
304 serv_connect(server_p, 0);
305 }
306
307 int
308 check_server(const char *name, struct Client *client_p)
309 {
310 struct server_conf *server_p = NULL;
311 struct server_conf *tmp_p;
312 rb_dlink_node *ptr;
313 int error = -1;
314
315 s_assert(NULL != client_p);
316 if(client_p == NULL)
317 return error;
318
319 if(!(client_p->localClient->passwd))
320 return -2;
321
322 if(strlen(name) > HOSTLEN)
323 return -4;
324
325 RB_DLINK_FOREACH(ptr, server_conf_list.head)
326 {
327 tmp_p = ptr->data;
328
329 if(ServerConfIllegal(tmp_p))
330 continue;
331
332 if(!match(tmp_p->name, name))
333 continue;
334
335 error = -3;
336
337 /* XXX: Fix me for IPv6 */
338 /* XXX sockhost is the IPv4 ip as a string */
339 if(match(tmp_p->host, client_p->host) ||
340 match(tmp_p->host, client_p->sockhost))
341 {
342 error = -2;
343
344 if(ServerConfEncrypted(tmp_p))
345 {
346 if(!strcmp(tmp_p->passwd, crypt(client_p->localClient->passwd,
347 tmp_p->passwd)))
348 {
349 server_p = tmp_p;
350 break;
351 }
352 }
353 else if(!strcmp(tmp_p->passwd, client_p->localClient->passwd))
354 {
355 server_p = tmp_p;
356 break;
357 }
358 }
359 }
360
361 if(server_p == NULL)
362 return error;
363
364 if(ServerConfSSL(server_p) && client_p->localClient->ssl_ctl == NULL)
365 {
366 return -5;
367 }
368
369 attach_server_conf(client_p, server_p);
370
371 /* clear ZIP/TB if they support but we dont want them */
372 #ifdef HAVE_LIBZ
373 if(!ServerConfCompressed(server_p))
374 #endif
375 ClearCap(client_p, CAP_ZIP);
376
377 if(!ServerConfTb(server_p))
378 ClearCap(client_p, CAP_TB);
379
380 return 0;
381 }
382
383 /*
384 * send_capabilities
385 *
386 * inputs - Client pointer to send to
387 * - int flag of capabilities that this server has
388 * output - NONE
389 * side effects - send the CAPAB line to a server -orabidoo
390 *
391 */
392 void
393 send_capabilities(struct Client *client_p, int cap_can_send)
394 {
395 struct Capability *cap;
396 char msgbuf[BUFSIZE];
397 char *t;
398 int tl;
399
400 t = msgbuf;
401
402 for (cap = captab; cap->name; ++cap)
403 {
404 if(cap->cap & cap_can_send)
405 {
406 tl = rb_sprintf(t, "%s ", cap->name);
407 t += tl;
408 }
409 }
410
411 t--;
412 *t = '\0';
413
414 sendto_one(client_p, "CAPAB :%s", msgbuf);
415 }
416
417 /* burst_modes_TS6()
418 *
419 * input - client to burst to, channel name, list to burst, mode flag
420 * output -
421 * side effects - client is sent a list of +b, +e, or +I modes
422 */
423 static void
424 burst_modes_TS6(struct Client *client_p, struct Channel *chptr,
425 rb_dlink_list *list, char flag)
426 {
427 rb_dlink_node *ptr;
428 struct Ban *banptr;
429 char *t;
430 int tlen;
431 int mlen;
432 int cur_len;
433
434 cur_len = mlen = rb_sprintf(buf, ":%s BMASK %ld %s %c :",
435 me.id, (long) chptr->channelts, chptr->chname, flag);
436 t = buf + mlen;
437
438 RB_DLINK_FOREACH(ptr, list->head)
439 {
440 banptr = ptr->data;
441
442 tlen = strlen(banptr->banstr) + 1;
443
444 /* uh oh */
445 if(cur_len + tlen > BUFSIZE - 3)
446 {
447 /* the one we're trying to send doesnt fit at all! */
448 if(cur_len == mlen)
449 {
450 s_assert(0);
451 continue;
452 }
453
454 /* chop off trailing space and send.. */
455 *(t-1) = '\0';
456 sendto_one(client_p, "%s", buf);
457 cur_len = mlen;
458 t = buf + mlen;
459 }
460
461 rb_sprintf(t, "%s ", banptr->banstr);
462 t += tlen;
463 cur_len += tlen;
464 }
465
466 /* cant ever exit the loop above without having modified buf,
467 * chop off trailing space and send.
468 */
469 *(t-1) = '\0';
470 sendto_one(client_p, "%s", buf);
471 }
472
473 /*
474 * burst_TS6
475 *
476 * inputs - client (server) to send nick towards
477 * - client to send nick for
478 * output - NONE
479 * side effects - NICK message is sent towards given client_p
480 */
481 static void
482 burst_TS6(struct Client *client_p)
483 {
484 static char ubuf[12];
485 struct Client *target_p;
486 struct Channel *chptr;
487 struct membership *msptr;
488 hook_data_client hclientinfo;
489 hook_data_channel hchaninfo;
490 rb_dlink_node *ptr;
491 rb_dlink_node *uptr;
492 char *t;
493 int tlen, mlen;
494 int cur_len = 0;
495
496 hclientinfo.client = hchaninfo.client = client_p;
497
498 RB_DLINK_FOREACH(ptr, global_client_list.head)
499 {
500 target_p = ptr->data;
501
502 if(!IsPerson(target_p))
503 continue;
504
505 send_umode(NULL, target_p, 0, 0, ubuf);
506 if(!*ubuf)
507 {
508 ubuf[0] = '+';
509 ubuf[1] = '\0';
510 }
511
512 if(has_id(target_p) && IsCapable(client_p, CAP_EUID))
513 sendto_one(client_p, ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
514 target_p->servptr->id, target_p->name,
515 target_p->hopcount + 1,
516 (long) target_p->tsinfo, ubuf,
517 target_p->username, target_p->host,
518 IsIPSpoof(target_p) ? "0" : target_p->sockhost,
519 target_p->id,
520 IsDynSpoof(target_p) ? target_p->orighost : "*",
521 EmptyString(target_p->user->suser) ? "*" : target_p->user->suser,
522 target_p->info);
523 else if(has_id(target_p))
524 sendto_one(client_p, ":%s UID %s %d %ld %s %s %s %s %s :%s",
525 target_p->servptr->id, target_p->name,
526 target_p->hopcount + 1,
527 (long) target_p->tsinfo, ubuf,
528 target_p->username, target_p->host,
529 IsIPSpoof(target_p) ? "0" : target_p->sockhost,
530 target_p->id, target_p->info);
531 else
532 sendto_one(client_p, "NICK %s %d %ld %s %s %s %s :%s",
533 target_p->name,
534 target_p->hopcount + 1,
535 (long) target_p->tsinfo,
536 ubuf,
537 target_p->username, target_p->host,
538 target_p->servptr->name, target_p->info);
539
540 if(!has_id(target_p) || !IsCapable(client_p, CAP_EUID))
541 {
542 if(IsDynSpoof(target_p))
543 sendto_one(client_p, ":%s ENCAP * REALHOST %s",
544 use_id(target_p), target_p->orighost);
545 if(!EmptyString(target_p->user->suser))
546 sendto_one(client_p, ":%s ENCAP * LOGIN %s",
547 use_id(target_p), target_p->user->suser);
548 }
549
550 if(ConfigFileEntry.burst_away && !EmptyString(target_p->user->away))
551 sendto_one(client_p, ":%s AWAY :%s",
552 use_id(target_p),
553 target_p->user->away);
554
555 hclientinfo.target = target_p;
556 call_hook(h_burst_client, &hclientinfo);
557 }
558
559 RB_DLINK_FOREACH(ptr, global_channel_list.head)
560 {
561 chptr = ptr->data;
562
563 if(*chptr->chname != '#')
564 continue;
565
566 cur_len = mlen = rb_sprintf(buf, ":%s SJOIN %ld %s %s :", me.id,
567 (long) chptr->channelts, chptr->chname,
568 channel_modes(chptr, client_p));
569
570 t = buf + mlen;
571
572 RB_DLINK_FOREACH(uptr, chptr->members.head)
573 {
574 msptr = uptr->data;
575
576 tlen = strlen(use_id(msptr->client_p)) + 1;
577 if(is_chanop(msptr))
578 tlen++;
579 if(is_voiced(msptr))
580 tlen++;
581
582 if(cur_len + tlen >= BUFSIZE - 3)
583 {
584 *(t-1) = '\0';
585 sendto_one(client_p, "%s", buf);
586 cur_len = mlen;
587 t = buf + mlen;
588 }
589
590 rb_sprintf(t, "%s%s ", find_channel_status(msptr, 1),
591 use_id(msptr->client_p));
592
593 cur_len += tlen;
594 t += tlen;
595 }
596
597 if (rb_dlink_list_length(&chptr->members) > 0)
598 {
599 /* remove trailing space */
600 *(t-1) = '\0';
601 }
602 sendto_one(client_p, "%s", buf);
603
604 if(rb_dlink_list_length(&chptr->banlist) > 0)
605 burst_modes_TS6(client_p, chptr, &chptr->banlist, 'b');
606
607 if(IsCapable(client_p, CAP_EX) &&
608 rb_dlink_list_length(&chptr->exceptlist) > 0)
609 burst_modes_TS6(client_p, chptr, &chptr->exceptlist, 'e');
610
611 if(IsCapable(client_p, CAP_IE) &&
612 rb_dlink_list_length(&chptr->invexlist) > 0)
613 burst_modes_TS6(client_p, chptr, &chptr->invexlist, 'I');
614
615 if(rb_dlink_list_length(&chptr->quietlist) > 0)
616 burst_modes_TS6(client_p, chptr, &chptr->quietlist, 'q');
617
618 if(IsCapable(client_p, CAP_TB) && chptr->topic != NULL)
619 sendto_one(client_p, ":%s TB %s %ld %s%s:%s",
620 me.id, chptr->chname, (long) chptr->topic_time,
621 ConfigChannel.burst_topicwho ? chptr->topic_info : "",
622 ConfigChannel.burst_topicwho ? " " : "",
623 chptr->topic);
624
625 hchaninfo.chptr = chptr;
626 call_hook(h_burst_channel, &hchaninfo);
627 }
628
629 hclientinfo.target = NULL;
630 call_hook(h_burst_finished, &hclientinfo);
631 }
632
633 /*
634 * show_capabilities - show current server capabilities
635 *
636 * inputs - pointer to an struct Client
637 * output - pointer to static string
638 * side effects - build up string representing capabilities of server listed
639 */
640 const char *
641 show_capabilities(struct Client *target_p)
642 {
643 static char msgbuf[BUFSIZE];
644 struct Capability *cap;
645
646 if(has_id(target_p))
647 rb_strlcpy(msgbuf, " TS6", sizeof(msgbuf));
648
649 if(IsSSL(target_p))
650 rb_strlcat(msgbuf, " SSL", sizeof(msgbuf));
651
652 if(!IsServer(target_p) || !target_p->serv->caps) /* short circuit if no caps */
653 return msgbuf + 1;
654
655 for (cap = captab; cap->cap; ++cap)
656 {
657 if(cap->cap & target_p->serv->caps)
658 rb_snprintf_append(msgbuf, sizeof(msgbuf), " %s", cap->name);
659 }
660
661 return msgbuf + 1;
662 }
663
664 /*
665 * server_estab
666 *
667 * inputs - pointer to a struct Client
668 * output -
669 * side effects -
670 */
671 int
672 server_estab(struct Client *client_p)
673 {
674 struct Client *target_p;
675 struct server_conf *server_p;
676 hook_data_client hdata;
677 char *host;
678 rb_dlink_node *ptr;
679 char note[HOSTLEN + 15];
680
681 s_assert(NULL != client_p);
682 if(client_p == NULL)
683 return -1;
684
685 host = client_p->name;
686
687 if((server_p = client_p->localClient->att_sconf) == NULL)
688 {
689 /* This shouldn't happen, better tell the ops... -A1kmm */
690 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
691 "Warning: Lost connect{} block for server %s!", host);
692 return exit_client(client_p, client_p, client_p, "Lost connect{} block!");
693 }
694
695 /* We shouldn't have to check this, it should already done before
696 * server_estab is called. -A1kmm
697 */
698 if(client_p->localClient->passwd)
699 {
700 memset(client_p->localClient->passwd, 0, strlen(client_p->localClient->passwd));
701 rb_free(client_p->localClient->passwd);
702 client_p->localClient->passwd = NULL;
703 }
704
705 /* Its got identd , since its a server */
706 SetGotId(client_p);
707
708 /* If there is something in the serv_list, it might be this
709 * connecting server..
710 */
711 if(!ServerInfo.hub && serv_list.head)
712 {
713 if(client_p != serv_list.head->data || serv_list.head->next)
714 {
715 ServerStats.is_ref++;
716 sendto_one(client_p, "ERROR :I'm a leaf not a hub");
717 return exit_client(client_p, client_p, client_p, "I'm a leaf");
718 }
719 }
720
721 if(IsUnknown(client_p))
722 {
723 /*
724 * jdc -- 1. Use EmptyString(), not [0] index reference.
725 * 2. Check ->spasswd, not ->passwd.
726 */
727 if(!EmptyString(server_p->spasswd))
728 {
729 sendto_one(client_p, "PASS %s TS %d :%s",
730 server_p->spasswd, TS_CURRENT, me.id);
731 }
732
733 /* pass info to new server */
734 send_capabilities(client_p, default_server_capabs
735 | (ServerConfCompressed(server_p) ? CAP_ZIP_SUPPORTED : 0)
736 | (ServerConfTb(server_p) ? CAP_TB : 0));
737
738 sendto_one(client_p, "SERVER %s 1 :%s%s",
739 me.name,
740 ConfigServerHide.hidden ? "(H) " : "",
741 (me.info[0]) ? (me.info) : "IRCers United");
742 }
743
744 if(!rb_set_buffers(client_p->localClient->F, READBUF_SIZE))
745 ilog_error("rb_set_buffers failed for server");
746
747 /* Enable compression now */
748 if(IsCapable(client_p, CAP_ZIP))
749 {
750 start_zlib_session(client_p);
751 }
752 sendto_one(client_p, "SVINFO %d %d 0 :%ld", TS_CURRENT, TS_MIN, (long int)rb_current_time());
753
754 client_p->servptr = &me;
755
756 if(IsAnyDead(client_p))
757 return CLIENT_EXITED;
758
759 SetServer(client_p);
760
761 /* Update the capability combination usage counts */
762 set_chcap_usage_counts(client_p);
763
764 rb_dlinkAdd(client_p, &client_p->lnode, &me.serv->servers);
765 del_unknown_ip(client_p);
766 rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &serv_list);
767 rb_dlinkAddTailAlloc(client_p, &global_serv_list);
768
769 if(has_id(client_p))
770 add_to_id_hash(client_p->id, client_p);
771
772 add_to_client_hash(client_p->name, client_p);
773 /* doesnt duplicate client_p->serv if allocated this struct already */
774 make_server(client_p);
775
776 client_p->serv->caps = client_p->localClient->caps;
777
778 if(client_p->localClient->fullcaps)
779 {
780 client_p->serv->fullcaps = rb_strdup(client_p->localClient->fullcaps);
781 rb_free(client_p->localClient->fullcaps);
782 client_p->localClient->fullcaps = NULL;
783 }
784
785 client_p->serv->nameinfo = scache_connect(client_p->name, client_p->info, IsHidden(client_p));
786 client_p->localClient->firsttime = rb_current_time();
787 /* fixing eob timings.. -gnp */
788
789 if((rb_dlink_list_length(&lclient_list) + rb_dlink_list_length(&serv_list)) >
790 (unsigned long)MaxConnectionCount)
791 MaxConnectionCount = rb_dlink_list_length(&lclient_list) +
792 rb_dlink_list_length(&serv_list);
793
794 /* Show the real host/IP to admins */
795 sendto_realops_snomask(SNO_GENERAL, L_ALL,
796 "Link with %s established: (%s) link",
797 client_p->name,
798 show_capabilities(client_p));
799
800 ilog(L_SERVER, "Link with %s established: (%s) link",
801 log_client_name(client_p, SHOW_IP), show_capabilities(client_p));
802
803 hdata.client = &me;
804 hdata.target = client_p;
805 call_hook(h_server_introduced, &hdata);
806
807 rb_snprintf(note, sizeof(note), "Server: %s", client_p->name);
808 rb_note(client_p->localClient->F, note);
809
810 /*
811 ** Old sendto_serv_but_one() call removed because we now
812 ** need to send different names to different servers
813 ** (domain name matching) Send new server to other servers.
814 */
815 RB_DLINK_FOREACH(ptr, serv_list.head)
816 {
817 target_p = ptr->data;
818
819 if(target_p == client_p)
820 continue;
821
822 if(has_id(target_p) && has_id(client_p))
823 {
824 sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
825 me.id, client_p->name, client_p->id,
826 IsHidden(client_p) ? "(H) " : "", client_p->info);
827
828 if(IsCapable(target_p, CAP_ENCAP) &&
829 !EmptyString(client_p->serv->fullcaps))
830 sendto_one(target_p, ":%s ENCAP * GCAP :%s",
831 client_p->id, client_p->serv->fullcaps);
832 }
833 else
834 {
835 sendto_one(target_p, ":%s SERVER %s 2 :%s%s",
836 me.name, client_p->name,
837 IsHidden(client_p) ? "(H) " : "", client_p->info);
838
839 if(IsCapable(target_p, CAP_ENCAP) &&
840 !EmptyString(client_p->serv->fullcaps))
841 sendto_one(target_p, ":%s ENCAP * GCAP :%s",
842 client_p->name, client_p->serv->fullcaps);
843 }
844 }
845
846 /*
847 ** Pass on my client information to the new server
848 **
849 ** First, pass only servers (idea is that if the link gets
850 ** cancelled beacause the server was already there,
851 ** there are no NICK's to be cancelled...). Of course,
852 ** if cancellation occurs, all this info is sent anyway,
853 ** and I guess the link dies when a read is attempted...? --msa
854 **
855 ** Note: Link cancellation to occur at this point means
856 ** that at least two servers from my fragment are building
857 ** up connection this other fragment at the same time, it's
858 ** a race condition, not the normal way of operation...
859 **
860 ** ALSO NOTE: using the get_client_name for server names--
861 ** see previous *WARNING*!!! (Also, original inpath
862 ** is destroyed...)
863 */
864 RB_DLINK_FOREACH(ptr, global_serv_list.head)
865 {
866 target_p = ptr->data;
867
868 /* target_p->from == target_p for target_p == client_p */
869 if(IsMe(target_p) || target_p->from == client_p)
870 continue;
871
872 /* presumption, if target has an id, so does its uplink */
873 if(has_id(client_p) && has_id(target_p))
874 sendto_one(client_p, ":%s SID %s %d %s :%s%s",
875 target_p->servptr->id, target_p->name,
876 target_p->hopcount + 1, target_p->id,
877 IsHidden(target_p) ? "(H) " : "", target_p->info);
878 else
879 sendto_one(client_p, ":%s SERVER %s %d :%s%s",
880 target_p->servptr->name,
881 target_p->name, target_p->hopcount + 1,
882 IsHidden(target_p) ? "(H) " : "", target_p->info);
883
884 if(IsCapable(client_p, CAP_ENCAP) &&
885 !EmptyString(target_p->serv->fullcaps))
886 sendto_one(client_p, ":%s ENCAP * GCAP :%s",
887 get_id(target_p, client_p),
888 target_p->serv->fullcaps);
889 }
890
891 burst_TS6(client_p);
892
893 /* Always send a PING after connect burst is done */
894 sendto_one(client_p, "PING :%s", get_id(&me, client_p));
895
896 free_pre_client(client_p);
897
898 if (!IsCapable(client_p, CAP_ZIP))
899 send_pop_queue(client_p);
900
901 return 0;
902 }
903
904 /*
905 * New server connection code
906 * Based upon the stuff floating about in s_bsd.c
907 * -- adrian
908 */
909
910 static int
911 serv_connect_resolved(struct Client *client_p)
912 {
913 struct rb_sockaddr_storage myipnum;
914 char vhoststr[HOSTIPLEN];
915 struct server_conf *server_p;
916 uint16_t port;
917
918 if((server_p = client_p->localClient->att_sconf) == NULL)
919 {
920 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, "Lost connect{} block for %s",
921 client_p->name);
922 exit_client(client_p, client_p, &me, "Lost connect{} block");
923 return 0;
924 }
925
926 #ifdef RB_IPV6
927 if(client_p->localClient->ip.ss_family == AF_INET6)
928 port = ntohs(((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port);
929 else
930 #endif
931 port = ntohs(((struct sockaddr_in *)&client_p->localClient->ip)->sin_port);
932
933 if(ServerConfVhosted(server_p))
934 {
935 memcpy(&myipnum, &server_p->my_ipnum, sizeof(myipnum));
936 ((struct sockaddr_in *)&myipnum)->sin_port = 0;
937 myipnum.ss_family = server_p->aftype;
938
939 }
940 else if(server_p->aftype == AF_INET && ServerInfo.specific_ipv4_vhost)
941 {
942 memcpy(&myipnum, &ServerInfo.ip, sizeof(myipnum));
943 ((struct sockaddr_in *)&myipnum)->sin_port = 0;
944 myipnum.ss_family = AF_INET;
945 SET_SS_LEN(&myipnum, sizeof(struct sockaddr_in));
946 }
947
948 #ifdef RB_IPV6
949 else if((server_p->aftype == AF_INET6) && ServerInfo.specific_ipv6_vhost)
950 {
951 memcpy(&myipnum, &ServerInfo.ip6, sizeof(myipnum));
952 ((struct sockaddr_in6 *)&myipnum)->sin6_port = 0;
953 myipnum.ss_family = AF_INET6;
954 SET_SS_LEN(&myipnum, sizeof(struct sockaddr_in6));
955 }
956 #endif
957 else
958 {
959 /* log */
960 ilog(L_SERVER, "Connecting to %s[%s] port %d (%s)", client_p->name, client_p->sockhost, port,
961 #ifdef RB_IPV6
962 server_p->aftype == AF_INET6 ? "IPv6" :
963 #endif
964 (server_p->aftype == AF_INET ? "IPv4" : "?"));
965
966 if(ServerConfSSL(server_p))
967 {
968 rb_connect_tcp(client_p->localClient->F, (struct sockaddr *)&client_p->localClient->ip,
969 NULL, 0, serv_connect_ssl_callback,
970 client_p, ConfigFileEntry.connect_timeout);
971 }
972 else
973 rb_connect_tcp(client_p->localClient->F, (struct sockaddr *)&client_p->localClient->ip,
974 NULL, 0, serv_connect_callback,
975 client_p, ConfigFileEntry.connect_timeout);
976 return 1;
977 }
978
979 /* log */
980 rb_inet_ntop_sock((struct sockaddr *)&myipnum, vhoststr, sizeof vhoststr);
981 ilog(L_SERVER, "Connecting to %s[%s] port %d (%s) (vhost %s)", client_p->name, client_p->sockhost, port,
982 #ifdef RB_IPV6
983 server_p->aftype == AF_INET6 ? "IPv6" :
984 #endif
985 (server_p->aftype == AF_INET ? "IPv4" : "?"), vhoststr);
986
987
988 if(ServerConfSSL(server_p))
989 rb_connect_tcp(client_p->localClient->F, (struct sockaddr *)&client_p->localClient->ip,
990 (struct sockaddr *) &myipnum,
991 GET_SS_LEN(&myipnum), serv_connect_ssl_callback, client_p,
992 ConfigFileEntry.connect_timeout);
993 else
994 rb_connect_tcp(client_p->localClient->F, (struct sockaddr *)&client_p->localClient->ip,
995 (struct sockaddr *) &myipnum,
996 GET_SS_LEN(&myipnum), serv_connect_callback, client_p,
997 ConfigFileEntry.connect_timeout);
998
999 return 1;
1000 }
1001
1002 static void
1003 serv_connect_dns_callback(void *vptr, struct DNSReply *reply)
1004 {
1005 struct Client *client_p = vptr;
1006 uint16_t port;
1007
1008 rb_free(client_p->localClient->dnsquery);
1009 client_p->localClient->dnsquery = NULL;
1010
1011 if (reply == NULL)
1012 {
1013 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, "Cannot resolve hostname for %s",
1014 client_p->name);
1015 ilog(L_SERVER, "Cannot resolve hostname for %s",
1016 log_client_name(client_p, HIDE_IP));
1017 exit_client(client_p, client_p, &me, "Cannot resolve hostname");
1018 return;
1019 }
1020 #ifdef RB_IPV6
1021 if(reply->addr.ss_family == AF_INET6)
1022 port = ((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port;
1023 else
1024 #endif
1025 port = ((struct sockaddr_in *)&client_p->localClient->ip)->sin_port;
1026 memcpy(&client_p->localClient->ip, &reply->addr, sizeof(client_p->localClient->ip));
1027 #ifdef RB_IPV6
1028 if(reply->addr.ss_family == AF_INET6)
1029 ((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port = port;
1030 else
1031 #endif
1032 ((struct sockaddr_in *)&client_p->localClient->ip)->sin_port = port;
1033 /* Set sockhost properly now -- jilles */
1034 rb_inet_ntop_sock((struct sockaddr *)&client_p->localClient->ip,
1035 client_p->sockhost, sizeof client_p->sockhost);
1036 serv_connect_resolved(client_p);
1037 }
1038
1039 /*
1040 * serv_connect() - initiate a server connection
1041 *
1042 * inputs - pointer to conf
1043 * - pointer to client doing the connet
1044 * output -
1045 * side effects -
1046 *
1047 * This code initiates a connection to a server. It first checks to make
1048 * sure the given server exists. If this is the case, it creates a socket,
1049 * creates a client, saves the socket information in the client, and
1050 * initiates a connection to the server through rb_connect_tcp(). The
1051 * completion of this goes through serv_completed_connection().
1052 *
1053 * We return 1 if the connection is attempted, since we don't know whether
1054 * it suceeded or not, and 0 if it fails in here somewhere.
1055 */
1056 int
1057 serv_connect(struct server_conf *server_p, struct Client *by)
1058 {
1059 struct Client *client_p;
1060 struct rb_sockaddr_storage theiripnum;
1061 rb_fde_t *F;
1062 char note[HOSTLEN + 10];
1063
1064 s_assert(server_p != NULL);
1065 if(server_p == NULL)
1066 return 0;
1067
1068 /*
1069 * Make sure this server isn't already connected
1070 */
1071 if((client_p = find_server(NULL, server_p->name)))
1072 {
1073 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1074 "Server %s already present from %s",
1075 server_p->name, client_p->name);
1076 if(by && IsPerson(by) && !MyClient(by))
1077 sendto_one_notice(by, ":Server %s already present from %s",
1078 server_p->name, client_p->name);
1079 return 0;
1080 }
1081
1082 /* create a socket for the server connection */
1083 if((F = rb_socket(server_p->aftype, SOCK_STREAM, 0, NULL)) == NULL)
1084 {
1085 ilog_error("opening a stream socket");
1086 return 0;
1087 }
1088
1089 rb_snprintf(note, sizeof note, "Server: %s", server_p->name);
1090 rb_note(F, note);
1091
1092 /* Create a local client */
1093 client_p = make_client(NULL);
1094
1095 /* Copy in the server, hostname, fd
1096 * The sockhost may be a hostname, this will be corrected later
1097 * -- jilles
1098 */
1099 rb_strlcpy(client_p->name, server_p->name, sizeof(client_p->name));
1100 rb_strlcpy(client_p->host, server_p->host, sizeof(client_p->host));
1101 rb_strlcpy(client_p->sockhost, server_p->host, sizeof(client_p->sockhost));
1102 client_p->localClient->F = F;
1103 add_to_cli_fd_hash(client_p);
1104
1105 /*
1106 * Set up the initial server evilness, ripped straight from
1107 * connect_server(), so don't blame me for it being evil.
1108 * -- adrian
1109 */
1110
1111 if(!rb_set_buffers(client_p->localClient->F, READBUF_SIZE))
1112 {
1113 ilog_error("setting the buffer size for a server connection");
1114 }
1115
1116 /*
1117 * Attach config entries to client here rather than in
1118 * serv_connect_callback(). This to avoid null pointer references.
1119 */
1120 attach_server_conf(client_p, server_p);
1121
1122 /*
1123 * at this point we have a connection in progress and C/N lines
1124 * attached to the client, the socket info should be saved in the
1125 * client and it should either be resolved or have a valid address.
1126 *
1127 * The socket has been connected or connect is in progress.
1128 */
1129 make_server(client_p);
1130 if(by && IsPerson(by))
1131 {
1132 strcpy(client_p->serv->by, by->name);
1133 if(client_p->serv->user)
1134 free_user(client_p->serv->user, NULL);
1135 client_p->serv->user = by->user;
1136 by->user->refcnt++;
1137 }
1138 else
1139 {
1140 strcpy(client_p->serv->by, "AutoConn.");
1141 if(client_p->serv->user)
1142 free_user(client_p->serv->user, NULL);
1143 client_p->serv->user = NULL;
1144 }
1145 SetConnecting(client_p);
1146 rb_dlinkAddTail(client_p, &client_p->node, &global_client_list);
1147
1148 if (rb_inet_pton_sock(server_p->host, (struct sockaddr *)&theiripnum) > 0)
1149 {
1150 memcpy(&client_p->localClient->ip, &theiripnum, sizeof(client_p->localClient->ip));
1151 #ifdef RB_IPV6
1152 if(theiripnum.ss_family == AF_INET6)
1153 ((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port = htons(server_p->port);
1154 else
1155 #endif
1156 ((struct sockaddr_in *)&client_p->localClient->ip)->sin_port = htons(server_p->port);
1157
1158 return serv_connect_resolved(client_p);
1159 }
1160 else
1161 {
1162 #ifdef RB_IPV6
1163 if(theiripnum.ss_family == AF_INET6)
1164 ((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port = htons(server_p->port);
1165 else
1166 #endif
1167 ((struct sockaddr_in *)&client_p->localClient->ip)->sin_port = htons(server_p->port);
1168
1169 client_p->localClient->dnsquery = rb_malloc(sizeof(struct DNSQuery));
1170 client_p->localClient->dnsquery->ptr = client_p;
1171 client_p->localClient->dnsquery->callback = serv_connect_dns_callback;
1172 gethost_byname_type(server_p->host, client_p->localClient->dnsquery,
1173 #ifdef RB_IPV6
1174 server_p->aftype == AF_INET6 ? T_AAAA :
1175 #endif
1176 T_A);
1177 return 1;
1178 }
1179 }
1180
1181 static void
1182 serv_connect_ev(void *data)
1183 {
1184 struct Client *client_p = data;
1185 serv_connect_callback(client_p->localClient->F, RB_OK, client_p);
1186 }
1187
1188 static void
1189 serv_connect_ssl_callback(rb_fde_t *F, int status, void *data)
1190 {
1191 struct Client *client_p = data;
1192 rb_fde_t *xF[2];
1193 rb_connect_sockaddr(F, (struct sockaddr *)&client_p->localClient->ip, sizeof(client_p->localClient->ip));
1194 if(status != RB_OK)
1195 {
1196 /* Print error message, just like non-SSL. */
1197 serv_connect_callback(F, status, data);
1198 return;
1199 }
1200 rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Outgoing ssld connection");
1201 del_from_cli_fd_hash(client_p);
1202 client_p->localClient->F = xF[0];
1203 add_to_cli_fd_hash(client_p);
1204
1205 client_p->localClient->ssl_ctl = start_ssld_connect(F, xF[1], rb_get_fd(xF[0]));
1206 SetSSL(client_p);
1207 rb_event_addonce("serv_connect_ev", serv_connect_ev, client_p, 1);
1208 }
1209
1210 /*
1211 * serv_connect_callback() - complete a server connection.
1212 *
1213 * This routine is called after the server connection attempt has
1214 * completed. If unsucessful, an error is sent to ops and the client
1215 * is closed. If sucessful, it goes through the initialisation/check
1216 * procedures, the capabilities are sent, and the socket is then
1217 * marked for reading.
1218 */
1219 static void
1220 serv_connect_callback(rb_fde_t *F, int status, void *data)
1221 {
1222 struct Client *client_p = data;
1223 struct server_conf *server_p;
1224 char *errstr;
1225
1226 /* First, make sure its a real client! */
1227 s_assert(client_p != NULL);
1228 s_assert(client_p->localClient->F == F);
1229
1230 if(client_p == NULL)
1231 return;
1232
1233 /* while we were waiting for the callback, its possible this already
1234 * linked in.. --fl
1235 */
1236 if(find_server(NULL, client_p->name) != NULL)
1237 {
1238 exit_client(client_p, client_p, &me, "Server Exists");
1239 return;
1240 }
1241
1242 if(client_p->localClient->ssl_ctl == NULL)
1243 rb_connect_sockaddr(F, (struct sockaddr *)&client_p->localClient->ip, sizeof(client_p->localClient->ip));
1244
1245 /* Check the status */
1246 if(status != RB_OK)
1247 {
1248 /* COMM_ERR_TIMEOUT wont have an errno associated with it,
1249 * the others will.. --fl
1250 */
1251 if(status == RB_ERR_TIMEOUT)
1252 {
1253 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1254 "Error connecting to %s[%s]: %s",
1255 client_p->name,
1256 "255.255.255.255",
1257 rb_errstr(status));
1258 ilog(L_SERVER, "Error connecting to %s[%s]: %s",
1259 client_p->name, client_p->sockhost,
1260 rb_errstr(status));
1261 }
1262 else
1263 {
1264 errstr = strerror(rb_get_sockerr(F));
1265 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1266 "Error connecting to %s[%s]: %s (%s)",
1267 client_p->name,
1268 "255.255.255.255",
1269 rb_errstr(status), errstr);
1270 ilog(L_SERVER, "Error connecting to %s[%s]: %s (%s)",
1271 client_p->name, client_p->sockhost,
1272 rb_errstr(status), errstr);
1273 }
1274
1275 exit_client(client_p, client_p, &me, rb_errstr(status));
1276 return;
1277 }
1278
1279 /* COMM_OK, so continue the connection procedure */
1280 /* Get the C/N lines */
1281 if((server_p = client_p->localClient->att_sconf) == NULL)
1282 {
1283 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, "Lost connect{} block for %s",
1284 client_p->name);
1285 exit_client(client_p, client_p, &me, "Lost connect{} block");
1286 return;
1287 }
1288
1289 /* Next, send the initial handshake */
1290 SetHandshake(client_p);
1291
1292 /* kludge, if we're not using TS6, dont ever send
1293 * ourselves as being TS6 capable.
1294 */
1295 if(!EmptyString(server_p->spasswd))
1296 {
1297 sendto_one(client_p, "PASS %s TS %d :%s",
1298 server_p->spasswd, TS_CURRENT, me.id);
1299 }
1300
1301 /* pass my info to the new server */
1302 send_capabilities(client_p, default_server_capabs
1303 | (ServerConfCompressed(server_p) ? CAP_ZIP_SUPPORTED : 0)
1304 | (ServerConfTb(server_p) ? CAP_TB : 0));
1305
1306 sendto_one(client_p, "SERVER %s 1 :%s%s",
1307 me.name,
1308 ConfigServerHide.hidden ? "(H) " : "", me.info);
1309
1310 /*
1311 * If we've been marked dead because a send failed, just exit
1312 * here now and save everyone the trouble of us ever existing.
1313 */
1314 if(IsAnyDead(client_p))
1315 {
1316 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
1317 "%s went dead during handshake", client_p->name);
1318 exit_client(client_p, client_p, &me, "Went dead during handshake");
1319 return;
1320 }
1321
1322 /* don't move to serv_list yet -- we haven't sent a burst! */
1323
1324 /* If we get here, we're ok, so lets start reading some data */
1325 read_packet(F, client_p);
1326 }
1327
1328 #ifndef HAVE_SOCKETPAIR
1329 static int
1330 inet_socketpair(int d, int type, int protocol, int sv[2])
1331 {
1332 struct sockaddr_in addr1, addr2, addr3;
1333 int addr3_len = sizeof(addr3);
1334 int fd, rc;
1335 int port_no = 20000;
1336
1337 if(d != AF_INET || type != SOCK_STREAM || protocol)
1338 {
1339 errno = EAFNOSUPPORT;
1340 return -1;
1341 }
1342 if(((sv[0] = socket(AF_INET, SOCK_STREAM, 0)) < 0) || ((sv[1] = socket(AF_INET, SOCK_STREAM, 0)) < 0))
1343 return -1;
1344
1345 addr1.sin_port = htons(port_no);
1346 addr1.sin_family = AF_INET;
1347 addr1.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1348 while ((rc = bind (sv[0], (struct sockaddr *) &addr1, sizeof (addr1))) < 0 && errno == EADDRINUSE)
1349 addr1.sin_port = htons(++port_no);
1350
1351 if(rc < 0)
1352 return -1;
1353
1354 if(listen(sv[0], 1) < 0)
1355 {
1356 close(sv[0]);
1357 close(sv[1]);
1358 return -1;
1359 }
1360
1361 addr2.sin_port = htons(port_no);
1362 addr2.sin_family = AF_INET;
1363 addr2.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1364 if(connect (sv[1], (struct sockaddr *) &addr2, sizeof (addr2)) < 0)
1365 {
1366 close(sv[0]);
1367 close(sv[1]);
1368 return -1;
1369 }
1370
1371 if((fd = accept(sv[1], (struct sockaddr *) &addr3, &addr3_len)) < 0)
1372 {
1373 close(sv[0]);
1374 close(sv[1]);
1375 return -1;
1376 }
1377 close(sv[0]);
1378 sv[0] = fd;
1379
1380 return(0);
1381
1382 }
1383 #endif