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