/[svn]/branches/newio/src/s_serv.c
ViewVC logotype

Contents of /branches/newio/src/s_serv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2415 - (show annotations)
Sun Jul 21 14:33:22 2013 UTC (6 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 36473 byte(s)
- ioengine changes as of 21JUL13

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * s_serv.c: Server related functions.
4 *
5 * Copyright (C) 2005 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #ifdef HAVE_LIBCRYPTO
27 #include <openssl/rsa.h>
28 #include "rsa.h"
29 #endif
30 #include "list.h"
31 #include "channel.h"
32 #include "channel_mode.h"
33 #include "client.h"
34 #include "dbuf.h"
35 #include "ioengine.h"
36 #include "hash.h"
37 #include "irc_string.h"
38 #include "ircd.h"
39 #include "ircd_defs.h"
40 #include "s_bsd.h"
41 #include "numeric.h"
42 #include "packet.h"
43 #include "irc_res.h"
44 #include "conf.h"
45 #include "s_serv.h"
46 #include "log.h"
47 #include "s_misc.h"
48 #include "s_user.h"
49 #include "send.h"
50 #include "memory.h"
51 #include "channel.h" /* chcap_usage_counts stuff...*/
52 #include "parse.h"
53
54
55 struct Timer connect_timer;
56
57 dlink_list flatten_links;
58 static dlink_list cap_list = { NULL, NULL, 0 };
59 static void server_burst(struct Client *);
60 static void burst_all(struct Client *);
61 static void send_tb(struct Client *client_p, struct Channel *chptr);
62 static void burst_members(struct Client *, struct Channel *);
63
64 /*
65 * write_links_file
66 *
67 * inputs - void pointer which is not used
68 * output - NONE
69 * side effects - called from an event, write out list of linked servers
70 * but in no particular order.
71 */
72 void
73 write_links_file(struct Event *ev)
74 {
75 FILE *file = NULL;
76 dlink_node *ptr = NULL, *ptr_next = NULL;
77 char buff[IRCD_BUFSIZE] = { '\0' };
78
79 if ((file = fopen(LIPATH, "w")) == NULL)
80 return;
81
82 DLINK_FOREACH_SAFE(ptr, ptr_next, flatten_links.head)
83 {
84 dlinkDelete(ptr, &flatten_links);
85 MyFree(ptr->data);
86 free_dlink_node(ptr);
87 }
88
89 DLINK_FOREACH(ptr, global_serv_list.head)
90 {
91 const struct Client *target_p = ptr->data;
92
93 /*
94 * Skip hidden servers, aswell as ourselves, since we already send
95 * ourselves in /links
96 */
97 if (IsHidden(target_p) || IsMe(target_p))
98 continue;
99
100 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
101 continue;
102
103 /*
104 * Attempt to format the file in such a way it follows the usual links output
105 * ie "servername uplink :hops info"
106 * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
107 * - madmax
108 */
109 snprintf(buff, sizeof(buff), "%s %s :1 %s", target_p->name,
110 me.name, target_p->info);
111 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
112 snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
113 me.name, target_p->info);
114
115 fputs(buff, file);
116 }
117
118 fclose(file);
119 }
120
121 void
122 read_links_file(void)
123 {
124 FILE *file = NULL;
125 char *p = NULL;
126 char buff[IRCD_BUFSIZE] = { '\0' };
127
128 if ((file = fopen(LIPATH, "r")) == NULL)
129 return;
130
131 while (fgets(buff, sizeof(buff), file))
132 {
133 if ((p = strchr(buff, '\n')) != NULL)
134 *p = '\0';
135
136 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
137 }
138
139 fclose(file);
140 }
141
142 /* hunt_server()
143 * Do the basic thing in delivering the message (command)
144 * across the relays to the specific server (server) for
145 * actions.
146 *
147 * Note: The command is a format string and *MUST* be
148 * of prefixed style (e.g. ":%s COMMAND %s ...").
149 * Command can have only max 8 parameters.
150 *
151 * server parv[server] is the parameter identifying the
152 * target server.
153 *
154 * *WARNING*
155 * parv[server] is replaced with the pointer to the
156 * real servername from the matched client (I'm lazy
157 * now --msa).
158 *
159 * returns: (see #defines)
160 */
161 int
162 hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
163 const int server, const int parc, char *parv[])
164 {
165 struct Client *target_p = NULL;
166 struct Client *target_tmp = NULL;
167 dlink_node *ptr;
168 int wilds;
169
170 /* Assume it's me, if no server */
171 if (parc <= server || EmptyString(parv[server]))
172 return HUNTED_ISME;
173
174 if (!strcmp(parv[server], me.id) || !match(parv[server], me.name))
175 return HUNTED_ISME;
176
177 /* These are to pickup matches that would cause the following
178 * message to go in the wrong direction while doing quick fast
179 * non-matching lookups.
180 */
181 if (MyClient(source_p))
182 target_p = hash_find_client(parv[server]);
183 else
184 target_p = find_person(client_p, parv[server]);
185
186 if (target_p)
187 if (target_p->from == source_p->from && !MyConnect(target_p))
188 target_p = NULL;
189
190 if (target_p == NULL && (target_p = hash_find_server(parv[server])))
191 if (target_p->from == source_p->from && !MyConnect(target_p))
192 target_p = NULL;
193
194 wilds = has_wildcards(parv[server]);
195
196 /* Again, if there are no wild cards involved in the server
197 * name, use the hash lookup
198 */
199 if (target_p == NULL)
200 {
201 if (!wilds)
202 {
203 if (!(target_p = hash_find_server(parv[server])))
204 {
205 sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
206 me.name, source_p->name, parv[server]);
207 return HUNTED_NOSUCH;
208 }
209 }
210 else
211 {
212 DLINK_FOREACH(ptr, global_client_list.head)
213 {
214 target_tmp = ptr->data;
215
216 if (!match(parv[server], target_tmp->name))
217 {
218 if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
219 continue;
220 target_p = ptr->data;
221
222 if (IsRegistered(target_p) && (target_p != client_p))
223 break;
224 }
225 }
226 }
227 }
228
229 if (target_p != NULL)
230 {
231 if(!IsRegistered(target_p))
232 {
233 sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
234 me.name, source_p->name, parv[server]);
235 return HUNTED_NOSUCH;
236 }
237
238 if (IsMe(target_p) || MyClient(target_p))
239 return HUNTED_ISME;
240
241 if (match(target_p->name, parv[server]))
242 parv[server] = target_p->name;
243
244 /* This is a little kludgy but should work... */
245 if (IsClient(source_p) &&
246 ((MyConnect(target_p) && IsCapable(target_p, CAP_TS6)) ||
247 (!MyConnect(target_p) && IsCapable(target_p->from, CAP_TS6))))
248 parv[0] = ID(source_p);
249
250 sendto_one(target_p, command, parv[0],
251 parv[1], parv[2], parv[3], parv[4],
252 parv[5], parv[6], parv[7], parv[8]);
253 return HUNTED_PASS;
254 }
255
256 sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
257 me.name, source_p->name, parv[server]);
258 return HUNTED_NOSUCH;
259 }
260
261 /* try_connections()
262 *
263 * inputs - void pointer which is not used
264 * output - NONE
265 * side effects -
266 * scan through configuration and try new connections.
267 * Returns the calendar time when the next call to this
268 * function should be made latest. (No harm done if this
269 * is called earlier or later...)
270 */
271 void
272 try_connections(struct Event *ev)
273 {
274 dlink_node *ptr = NULL;
275 time_t next = 0;
276 int hold = 0;
277 int done = 0;
278
279 assert(ET_EXPIRE == ev_type(ev));
280 assert(ev_timer(ev));
281
282 next = CurrentTime + DEFAULT_CONNECTFREQUENCY;
283
284 DLINK_FOREACH(ptr, server_items.head)
285 {
286 struct MaskItem *conf = ptr->data;
287
288 assert(conf->type == CONF_SERVER);
289
290 if (!conf->port ||!IsConfAllowAutoConn(conf))
291 continue;
292
293 /* Do we need to postpone this connection further? */
294 hold = conf->until > CurrentTime;
295
296 /* Update next possible connection check time. */
297 if (hold && next > conf->until)
298 next = conf->until;
299
300 if (hold || done || hash_find_server(conf->name) ||
301 !(conf->class->ref_count < conf->class->max_total))
302 continue;
303
304 dlinkDelete(ptr, &server_items);
305 dlinkAddTail(conf, &conf->node, &server_items);
306
307 /* Activate the connection itself. */
308 if (connect_server(conf, 0))
309 {
310 if (ConfigServerHide.hide_server_ips)
311 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
312 "Connection to %s activated.",
313 conf->name);
314 else
315 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
316 "Connection to %s[%s] activated.",
317 conf->name, conf->host);
318 }
319
320 /* And stop looking for further candidates. */
321 done = 1;
322 }
323
324 timer_add(&connect_timer, try_connections, 0, TT_ABSOLUTE, next);
325 }
326
327 int
328 valid_servname(const char *name)
329 {
330 unsigned int length = 0;
331 unsigned int dots = 0;
332 const char *p = name;
333
334 for (; *p; ++p)
335 {
336 if (!IsServChar(*p))
337 return 0;
338
339 ++length;
340
341 if (*p == '.')
342 ++dots;
343 }
344
345 return dots != 0 && length <= HOSTLEN;
346 }
347
348 int
349 check_server(const char *name, struct Client *client_p)
350 {
351 dlink_node *ptr;
352 struct MaskItem *conf = NULL;
353 struct MaskItem *server_conf = NULL;
354 int error = -1;
355
356 assert(client_p != NULL);
357
358 /* loop through looking for all possible connect items that might work */
359 DLINK_FOREACH(ptr, server_items.head)
360 {
361 conf = ptr->data;
362
363 if (match(name, conf->name))
364 continue;
365
366 error = -3;
367
368 /* XXX: Fix me for IPv6 */
369 /* XXX sockhost is the IPv4 ip as a string */
370 if (!match(conf->host, client_p->host) ||
371 !match(conf->host, client_p->sockhost))
372 {
373 error = -2;
374
375 if (!match_conf_password(client_p->localClient->passwd, conf))
376 return -2;
377
378 if (!EmptyString(conf->certfp))
379 if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
380 return -4;
381
382 server_conf = conf;
383 }
384 }
385
386 if (server_conf == NULL)
387 return error;
388
389 attach_conf(client_p, server_conf);
390
391
392 if (server_conf != NULL)
393 {
394 struct sockaddr_in *v4;
395 struct sockaddr_in6 *v6;
396
397 switch (server_conf->aftype)
398 {
399 case AF_INET6:
400 v6 = (struct sockaddr_in6 *)&server_conf->addr;
401
402 if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
403 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
404 break;
405
406 case AF_INET:
407 v4 = (struct sockaddr_in *)&server_conf->addr;
408
409 if (v4->sin_addr.s_addr == INADDR_NONE)
410 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
411 break;
412 }
413 }
414
415 return 0;
416 }
417
418 /* add_capability()
419 *
420 * inputs - string name of CAPAB
421 * - int flag of capability
422 * output - NONE
423 * side effects - Adds given capability name and bit mask to
424 * current supported capabilities. This allows
425 * modules to dynamically add or subtract their capability.
426 */
427 void
428 add_capability(const char *capab_name, int cap_flag, int add_to_default)
429 {
430 struct Capability *cap = MyMalloc(sizeof(*cap));
431
432 cap->name = xstrdup(capab_name);
433 cap->cap = cap_flag;
434 dlinkAdd(cap, &cap->node, &cap_list);
435
436 if (add_to_default)
437 default_server_capabs |= cap_flag;
438 }
439
440 /* delete_capability()
441 *
442 * inputs - string name of CAPAB
443 * output - NONE
444 * side effects - delete given capability from ones known.
445 */
446 int
447 delete_capability(const char *capab_name)
448 {
449 dlink_node *ptr;
450 dlink_node *next_ptr;
451 struct Capability *cap;
452
453 DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
454 {
455 cap = ptr->data;
456
457 if (cap->cap != 0)
458 {
459 if (irccmp(cap->name, capab_name) == 0)
460 {
461 default_server_capabs &= ~(cap->cap);
462 dlinkDelete(ptr, &cap_list);
463 MyFree(cap->name);
464 MyFree(cap);
465 }
466 }
467 }
468
469 return 0;
470 }
471
472 /*
473 * find_capability()
474 *
475 * inputs - string name of capab to find
476 * output - 0 if not found CAPAB otherwise
477 * side effects - none
478 */
479 unsigned int
480 find_capability(const char *capab)
481 {
482 const dlink_node *ptr = NULL;
483
484 DLINK_FOREACH(ptr, cap_list.head)
485 {
486 const struct Capability *cap = ptr->data;
487
488 if (cap->cap && !irccmp(cap->name, capab))
489 return cap->cap;
490 }
491
492 return 0;
493 }
494
495 /* send_capabilities()
496 *
497 * inputs - Client pointer to send to
498 * - int flag of capabilities that this server can send
499 * output - NONE
500 * side effects - send the CAPAB line to a server -orabidoo
501 *
502 */
503 void
504 send_capabilities(struct Client *client_p, int cap_can_send)
505 {
506 struct Capability *cap=NULL;
507 char msgbuf[IRCD_BUFSIZE];
508 char *t;
509 int tl;
510 dlink_node *ptr;
511
512 t = msgbuf;
513
514 DLINK_FOREACH(ptr, cap_list.head)
515 {
516 cap = ptr->data;
517
518 if (cap->cap & (cap_can_send|default_server_capabs))
519 {
520 tl = sprintf(t, "%s ", cap->name);
521 t += tl;
522 }
523 }
524
525 *(t - 1) = '\0';
526 sendto_one(client_p, "CAPAB :%s", msgbuf);
527 }
528
529 /* sendnick_TS()
530 *
531 * inputs - client (server) to send nick towards
532 * - client to send nick for
533 * output - NONE
534 * side effects - NICK message is sent towards given client_p
535 */
536 void
537 sendnick_TS(struct Client *client_p, struct Client *target_p)
538 {
539 static char ubuf[12];
540
541 if (!IsClient(target_p))
542 return;
543
544 send_umode(NULL, target_p, 0, SEND_UMODES, ubuf);
545
546 if (ubuf[0] == '\0')
547 {
548 ubuf[0] = '+';
549 ubuf[1] = '\0';
550 }
551
552 if (IsCapable(client_p, CAP_SVS))
553 {
554 if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
555 sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s %s :%s",
556 target_p->servptr->id,
557 target_p->name, target_p->hopcount + 1,
558 (unsigned long) target_p->tsinfo,
559 ubuf, target_p->username, target_p->host,
560 (MyClient(target_p) && IsIPSpoof(target_p)) ?
561 "0" : target_p->sockhost, target_p->id,
562 target_p->svid, target_p->info);
563 else
564 sendto_one(client_p, "NICK %s %d %lu %s %s %s %s %s :%s",
565 target_p->name, target_p->hopcount + 1,
566 (unsigned long) target_p->tsinfo,
567 ubuf, target_p->username, target_p->host,
568 target_p->servptr->name, target_p->svid,
569 target_p->info);
570 }
571 else
572 {
573 if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
574 sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
575 target_p->servptr->id,
576 target_p->name, target_p->hopcount + 1,
577 (unsigned long) target_p->tsinfo,
578 ubuf, target_p->username, target_p->host,
579 (MyClient(target_p) && IsIPSpoof(target_p)) ?
580 "0" : target_p->sockhost, target_p->id, target_p->info);
581 else
582 sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
583 target_p->name, target_p->hopcount + 1,
584 (unsigned long) target_p->tsinfo,
585 ubuf, target_p->username, target_p->host,
586 target_p->servptr->name, target_p->info);
587 }
588
589 if (!EmptyString(target_p->certfp))
590 sendto_one(client_p, ":%s CERTFP %s",
591 ID_or_name(target_p, client_p), target_p->certfp);
592
593 if (target_p->away[0])
594 sendto_one(client_p, ":%s AWAY :%s", ID_or_name(target_p, client_p),
595 target_p->away);
596
597 }
598
599 /*
600 * show_capabilities - show current server capabilities
601 *
602 * inputs - pointer to a struct Client
603 * output - pointer to static string
604 * side effects - build up string representing capabilities of server listed
605 */
606 const char *
607 show_capabilities(const struct Client *target_p)
608 {
609 static char msgbuf[IRCD_BUFSIZE] = "";
610 const dlink_node *ptr = NULL;
611
612 strlcpy(msgbuf, "TS", sizeof(msgbuf));
613
614 DLINK_FOREACH(ptr, cap_list.head)
615 {
616 const struct Capability *cap = ptr->data;
617
618 if (!IsCapable(target_p, cap->cap))
619 continue;
620
621 strlcat(msgbuf, " ", sizeof(msgbuf));
622 strlcat(msgbuf, cap->name, sizeof(msgbuf));
623 }
624
625 return msgbuf;
626 }
627
628 /* make_server()
629 *
630 * inputs - pointer to client struct
631 * output - pointer to struct Server
632 * side effects - add's an Server information block to a client
633 * if it was not previously allocated.
634 */
635 struct Server *
636 make_server(struct Client *client_p)
637 {
638 if (client_p->serv == NULL)
639 client_p->serv = MyMalloc(sizeof(struct Server));
640
641 client_p->serv->last_error_msg = xstrdup("<>"); /* String must be non-empty */
642
643 return client_p->serv;
644 }
645
646 /* server_estab()
647 *
648 * inputs - pointer to a struct Client
649 * output -
650 * side effects -
651 */
652 void
653 server_estab(struct Client *client_p)
654 {
655 struct Client *target_p;
656 struct MaskItem *conf = NULL;
657 char *host;
658 const char *inpath;
659 static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
660 dlink_node *ptr;
661 #ifdef HAVE_LIBCRYPTO
662 const COMP_METHOD *compression = NULL, *expansion = NULL;
663 #endif
664
665 assert(client_p != NULL);
666
667 strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
668
669 inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
670 host = client_p->name;
671
672 if ((conf = find_conf_name(&client_p->localClient->confs, host, CONF_SERVER))
673 == NULL)
674 {
675 /* This shouldn't happen, better tell the ops... -A1kmm */
676 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
677 "Warning: Lost connect{} block "
678 "for server %s(this shouldn't happen)!", host);
679 exit_client(client_p, &me, "Lost connect{} block!");
680 return;
681 }
682
683 MyFree(client_p->localClient->passwd);
684 client_p->localClient->passwd = NULL;
685
686 /* Its got identd, since its a server */
687 SetGotId(client_p);
688
689 /* If there is something in the serv_list, it might be this
690 * connecting server..
691 */
692 if (!ServerInfo.hub && serv_list.head)
693 {
694 if (client_p != serv_list.head->data || serv_list.head->next)
695 {
696 ++ServerStats.is_ref;
697 sendto_one(client_p, "ERROR :I'm a leaf not a hub");
698 exit_client(client_p, &me, "I'm a leaf");
699 return;
700 }
701 }
702
703 if (IsUnknown(client_p))
704 {
705 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
706
707 send_capabilities(client_p, 0);
708
709 sendto_one(client_p, "SERVER %s 1 :%s%s",
710 me.name, ConfigServerHide.hidden ? "(H) " : "", me.info);
711 }
712
713 sendto_one(client_p, "SVINFO %d %d 0 :%lu", TS_CURRENT, TS_MIN,
714 (unsigned long)CurrentTime);
715
716 /* assumption here is if they passed the correct TS version, they also passed an SID */
717 if (IsCapable(client_p, CAP_TS6))
718 hash_add_id(client_p);
719
720 /* XXX Does this ever happen? I don't think so -db */
721 detach_conf(client_p, CONF_OPER);
722
723 /* *WARNING*
724 ** In the following code in place of plain server's
725 ** name we send what is returned by get_client_name
726 ** which may add the "sockhost" after the name. It's
727 ** *very* *important* that there is a SPACE between
728 ** the name and sockhost (if present). The receiving
729 ** server will start the information field from this
730 ** first blank and thus puts the sockhost into info.
731 ** ...a bit tricky, but you have been warned, besides
732 ** code is more neat this way... --msa
733 */
734 client_p->servptr = &me;
735
736 if (IsClosing(client_p))
737 return;
738
739 SetServer(client_p);
740
741 /* Update the capability combination usage counts. -A1kmm */
742 set_chcap_usage_counts(client_p);
743
744 /* Some day, all these lists will be consolidated *sigh* */
745 dlinkAdd(client_p, &client_p->lnode, &me.serv->server_list);
746
747 assert(dlinkFind(&unknown_list, client_p));
748
749 dlink_move_node(&client_p->localClient->lclient_node,
750 &unknown_list, &serv_list);
751
752 Count.myserver++;
753
754 dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
755 hash_add_client(client_p);
756
757 /* doesnt duplicate client_p->serv if allocated this struct already */
758 make_server(client_p);
759
760 /* fixing eob timings.. -gnp */
761 client_p->localClient->firsttime = CurrentTime;
762
763 if (find_matching_name_conf(CONF_SERVICE, client_p->name, NULL, NULL, 0))
764 AddFlag(client_p, FLAGS_SERVICE);
765
766 /* Show the real host/IP to admins */
767 #ifdef XXX___
768 #ifdef HAVE_LIBCRYPTO
769 if (client_p->localClient->fd.ssl)
770 {
771 compression = SSL_get_current_compression(client_p->localClient->fd.ssl);
772 expansion = SSL_get_current_expansion(client_p->localClient->fd.ssl);
773
774 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
775 "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
776 inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
777 compression ? SSL_COMP_get_name(compression) : "NONE",
778 expansion ? SSL_COMP_get_name(expansion) : "NONE",
779 show_capabilities(client_p));
780 /* Now show the masked hostname/IP to opers */
781 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
782 "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
783 inpath, ssl_get_cipher(client_p->localClient->fd.ssl),
784 compression ? SSL_COMP_get_name(compression) : "NONE",
785 expansion ? SSL_COMP_get_name(expansion) : "NONE",
786 show_capabilities(client_p));
787 ilog(LOG_TYPE_IRCD, "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
788 inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
789 compression ? SSL_COMP_get_name(compression) : "NONE",
790 expansion ? SSL_COMP_get_name(expansion) : "NONE",
791 show_capabilities(client_p));
792 }
793 else
794 #endif
795 #endif
796 {
797 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
798 "Link with %s established: (Capabilities: %s)",
799 inpath_ip, show_capabilities(client_p));
800 /* Now show the masked hostname/IP to opers */
801 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
802 "Link with %s established: (Capabilities: %s)",
803 inpath, show_capabilities(client_p));
804 ilog(LOG_TYPE_IRCD, "Link with %s established: (Capabilities: %s)",
805 inpath_ip, show_capabilities(client_p));
806 }
807
808 /* Old sendto_serv_but_one() call removed because we now
809 ** need to send different names to different servers
810 ** (domain name matching) Send new server to other servers.
811 */
812 DLINK_FOREACH(ptr, serv_list.head)
813 {
814 target_p = ptr->data;
815
816 if (target_p == client_p)
817 continue;
818
819 if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
820 sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
821 me.id, client_p->name, client_p->id,
822 IsHidden(client_p) ? "(H) " : "",
823 client_p->info);
824 else
825 sendto_one(target_p,":%s SERVER %s 2 :%s%s",
826 me.name, client_p->name,
827 IsHidden(client_p) ? "(H) " : "",
828 client_p->info);
829 }
830
831 /*
832 * Pass on my client information to the new server
833 *
834 * First, pass only servers (idea is that if the link gets
835 * cancelled beacause the server was already there,
836 * there are no NICK's to be cancelled...). Of course,
837 * if cancellation occurs, all this info is sent anyway,
838 * and I guess the link dies when a read is attempted...? --msa
839 *
840 * Note: Link cancellation to occur at this point means
841 * that at least two servers from my fragment are building
842 * up connection this other fragment at the same time, it's
843 * a race condition, not the normal way of operation...
844 *
845 * ALSO NOTE: using the get_client_name for server names--
846 * see previous *WARNING*!!! (Also, original inpath
847 * is destroyed...)
848 */
849
850 DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
851 {
852 target_p = ptr->data;
853
854 /* target_p->from == target_p for target_p == client_p */
855 if (IsMe(target_p) || target_p->from == client_p)
856 continue;
857
858 if (IsCapable(client_p, CAP_TS6))
859 {
860 if (HasID(target_p))
861 sendto_one(client_p, ":%s SID %s %d %s :%s%s",
862 ID(target_p->servptr), target_p->name, target_p->hopcount+1,
863 target_p->id, IsHidden(target_p) ? "(H) " : "",
864 target_p->info);
865 else /* introducing non-ts6 server */
866 sendto_one(client_p, ":%s SERVER %s %d :%s%s",
867 ID(target_p->servptr), target_p->name, target_p->hopcount+1,
868 IsHidden(target_p) ? "(H) " : "", target_p->info);
869 }
870 else
871 sendto_one(client_p, ":%s SERVER %s %d :%s%s",
872 target_p->servptr->name, target_p->name, target_p->hopcount+1,
873 IsHidden(target_p) ? "(H) " : "", target_p->info);
874
875 if (HasFlag(target_p, FLAGS_EOB))
876 sendto_one(client_p, ":%s EOB", ID_or_name(client_p, target_p));
877 }
878
879 server_burst(client_p);
880 }
881
882 /* server_burst()
883 *
884 * inputs - struct Client pointer server
885 * -
886 * output - none
887 * side effects - send a server burst
888 * bugs - still too long
889 */
890 static void
891 server_burst(struct Client *client_p)
892 {
893 /* Send it in the shortened format with the TS, if
894 ** it's a TS server; walk the list of channels, sending
895 ** all the nicks that haven't been sent yet for each
896 ** channel, then send the channel itself -- it's less
897 ** obvious than sending all nicks first, but on the
898 ** receiving side memory will be allocated more nicely
899 ** saving a few seconds in the handling of a split
900 ** -orabidoo
901 */
902
903 burst_all(client_p);
904
905 /* EOB stuff is now in burst_all */
906 /* Always send a PING after connect burst is done */
907 sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
908 }
909
910 /* burst_all()
911 *
912 * inputs - pointer to server to send burst to
913 * output - NONE
914 * side effects - complete burst of channels/nicks is sent to client_p
915 */
916 static void
917 burst_all(struct Client *client_p)
918 {
919 dlink_node *ptr = NULL;
920
921 DLINK_FOREACH(ptr, global_channel_list.head)
922 {
923 struct Channel *chptr = ptr->data;
924
925 if (dlink_list_length(&chptr->members) != 0)
926 {
927 burst_members(client_p, chptr);
928 send_channel_modes(client_p, chptr);
929
930 if (IsCapable(client_p, CAP_TBURST))
931 send_tb(client_p, chptr);
932 }
933 }
934
935 /* also send out those that are not on any channel
936 */
937 DLINK_FOREACH(ptr, global_client_list.head)
938 {
939 struct Client *target_p = ptr->data;
940
941 if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
942 sendnick_TS(client_p, target_p);
943
944 DelFlag(target_p, FLAGS_BURSTED);
945 }
946
947 if (IsCapable(client_p, CAP_EOB))
948 sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
949 }
950
951 /*
952 * send_tb
953 *
954 * inputs - pointer to Client
955 * - pointer to channel
956 * output - NONE
957 * side effects - Called on a server burst when
958 * server is CAP_TBURST capable
959 */
960 static void
961 send_tb(struct Client *client_p, struct Channel *chptr)
962 {
963 /*
964 * We may also send an empty topic here, but only if topic_time isn't 0,
965 * i.e. if we had a topic that got unset. This is required for syncing
966 * topics properly.
967 *
968 * Imagine the following scenario: Our downlink introduces a channel
969 * to us with a TS that is equal to ours, but the channel topic on
970 * their side got unset while the servers were in splitmode, which means
971 * their 'topic' is newer. They simply wanted to unset it, so we have to
972 * deal with it in a more sophisticated fashion instead of just resetting
973 * it to their old topic they had before. Read m_tburst.c:ms_tburst
974 * for further information -Michael
975 */
976 if (chptr->topic_time != 0)
977 sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
978 ID_or_name(&me, client_p),
979 (unsigned long)chptr->channelts, chptr->chname,
980 (unsigned long)chptr->topic_time,
981 chptr->topic_info,
982 chptr->topic);
983 }
984
985 /* burst_members()
986 *
987 * inputs - pointer to server to send members to
988 * - dlink_list pointer to membership list to send
989 * output - NONE
990 * side effects -
991 */
992 static void
993 burst_members(struct Client *client_p, struct Channel *chptr)
994 {
995 struct Client *target_p;
996 struct Membership *ms;
997 dlink_node *ptr;
998
999 DLINK_FOREACH(ptr, chptr->members.head)
1000 {
1001 ms = ptr->data;
1002 target_p = ms->client_p;
1003
1004 if (!HasFlag(target_p, FLAGS_BURSTED))
1005 {
1006 AddFlag(target_p, FLAGS_BURSTED);
1007
1008 if (target_p->from != client_p)
1009 sendnick_TS(client_p, target_p);
1010 }
1011 }
1012 }
1013
1014 /* New server connection code
1015 * Based upon the stuff floating about in s_bsd.c
1016 * -- adrian
1017 */
1018
1019 /* serv_connect() - initiate a server connection
1020 *
1021 * inputs - pointer to conf
1022 * - pointer to client doing the connect
1023 * output -
1024 * side effects -
1025 *
1026 * This code initiates a connection to a server. It first checks to make
1027 * sure the given server exists. If this is the case, it creates a socket,
1028 * creates a client, saves the socket information in the client, and
1029 * initiates a connection to the server through comm_connect_tcp(). The
1030 * completion of this goes through serv_completed_connection().
1031 *
1032 * We return 1 if the connection is attempted, since we don't know whether
1033 * it suceeded or not, and 0 if it fails in here somewhere.
1034 */
1035 int
1036 serv_connect(struct MaskItem *conf, struct Client *by)
1037 {
1038 struct Client *client_p;
1039 char buf[HOSTIPLEN + 1];
1040
1041 /* conversion structs */
1042 struct sockaddr_in *v4;
1043 /* Make sure conf is useful */
1044 assert(conf != NULL);
1045
1046
1047 getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
1048 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
1049 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
1050 buf);
1051
1052 /* Still processing a DNS lookup? -> exit */
1053 if (conf->dns_pending)
1054 {
1055 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1056 "Error connecting to %s: DNS lookup for connect{} in progress.",
1057 conf->name);
1058 return (0);
1059 }
1060
1061 if (conf->dns_failed)
1062 {
1063 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1064 "Error connecting to %s: DNS lookup for connect{} failed.",
1065 conf->name);
1066 return (0);
1067 }
1068
1069 /* Make sure this server isn't already connected
1070 * Note: conf should ALWAYS be a valid C: line
1071 */
1072 if ((client_p = hash_find_server(conf->name)) != NULL)
1073 {
1074 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1075 "Server %s already present from %s",
1076 conf->name, get_client_name(client_p, SHOW_IP));
1077 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1078 "Server %s already present from %s",
1079 conf->name, get_client_name(client_p, MASK_IP));
1080 if (by && IsClient(by) && !MyClient(by))
1081 sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
1082 me.name, by->name, conf->name,
1083 get_client_name(client_p, MASK_IP));
1084 return 0;
1085 }
1086
1087 /* Create a local client */
1088 client_p = make_client(NULL);
1089
1090 /* Copy in the server, hostname, fd */
1091 strlcpy(client_p->name, conf->name, sizeof(client_p->name));
1092 strlcpy(client_p->host, conf->host, sizeof(client_p->host));
1093
1094 /* We already converted the ip once, so lets use it - stu */
1095 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
1096 #if 0
1097 /* create a socket for the server connection */
1098 if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family,
1099 SOCK_STREAM, 0, NULL) < 0)
1100 {
1101 /* Eek, failure to create the socket */
1102 report_error(L_ALL, "opening stream socket to %s: %s",
1103 conf->name, errno);
1104 SetDead(client_p);
1105 exit_client(client_p, &me, "Connection failed");
1106 return 0;
1107 }
1108 #endif
1109 /* Attach config entries to client here rather than in
1110 * serv_connect_callback(). This to avoid null pointer references.
1111 */
1112 if (!attach_connect_block(client_p, conf->name, conf->host))
1113 {
1114 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1115 "Host %s is not enabled for connecting: no connect{} block",
1116 conf->name);
1117 if (by && IsClient(by) && !MyClient(by))
1118 sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
1119 me.name, by->name, client_p->name);
1120 SetDead(client_p);
1121 exit_client(client_p, client_p, "Connection failed");
1122 return 0;
1123 }
1124
1125 /* at this point we have a connection in progress and C/N lines
1126 * attached to the client, the socket info should be saved in the
1127 * client and it should either be resolved or have a valid address.
1128 *
1129 * The socket has been connected or connect is in progress.
1130 */
1131 make_server(client_p);
1132
1133 if (by && IsClient(by))
1134 strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
1135 else
1136 strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
1137
1138 SetConnecting(client_p);
1139 dlinkAdd(client_p, &client_p->node, &global_client_list);
1140 /* from def_fam */
1141 client_p->localClient->aftype = conf->aftype;
1142 #if 0
1143 /* Now, initiate the connection */
1144 /* XXX assume that a non 0 type means a specific bind address
1145 * for this connect.
1146 */
1147 switch (conf->aftype)
1148 {
1149 case AF_INET:
1150 v4 = (struct sockaddr_in*)&conf->bind;
1151 if (v4->sin_addr.s_addr != 0)
1152 {
1153 struct irc_ssaddr ipn;
1154 memset(&ipn, 0, sizeof(struct irc_ssaddr));
1155 ipn.ss.ss_family = AF_INET;
1156 ipn.ss_port = 0;
1157 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1158 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1159 (struct sockaddr *)&ipn, ipn.ss_len,
1160 serv_connect_callback, client_p, conf->aftype,
1161 CONNECTTIMEOUT);
1162 }
1163 else if (ServerInfo.specific_ipv4_vhost)
1164 {
1165 struct irc_ssaddr ipn;
1166 memset(&ipn, 0, sizeof(struct irc_ssaddr));
1167 ipn.ss.ss_family = AF_INET;
1168 ipn.ss_port = 0;
1169 memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
1170 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1171 (struct sockaddr *)&ipn, ipn.ss_len,
1172 serv_connect_callback, client_p, conf->aftype,
1173 CONNECTTIMEOUT);
1174 }
1175 else
1176 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1177 NULL, 0, serv_connect_callback, client_p, conf->aftype,
1178 CONNECTTIMEOUT);
1179 break;
1180 case AF_INET6:
1181 {
1182 struct irc_ssaddr ipn;
1183 struct sockaddr_in6 *v6;
1184 struct sockaddr_in6 *v6conf;
1185
1186 memset(&ipn, 0, sizeof(struct irc_ssaddr));
1187 v6conf = (struct sockaddr_in6 *)&conf->bind;
1188 v6 = (struct sockaddr_in6 *)&ipn;
1189
1190 if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)) != 0)
1191 {
1192 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1193 ipn.ss.ss_family = AF_INET6;
1194 ipn.ss_port = 0;
1195 comm_connect_tcp(&client_p->localClient->fd,
1196 conf->host, conf->port,
1197 (struct sockaddr *)&ipn, ipn.ss_len,
1198 serv_connect_callback, client_p,
1199 conf->aftype, CONNECTTIMEOUT);
1200 }
1201 else if (ServerInfo.specific_ipv6_vhost)
1202 {
1203 memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
1204 ipn.ss.ss_family = AF_INET6;
1205 ipn.ss_port = 0;
1206 comm_connect_tcp(&client_p->localClient->fd,
1207 conf->host, conf->port,
1208 (struct sockaddr *)&ipn, ipn.ss_len,
1209 serv_connect_callback, client_p,
1210 conf->aftype, CONNECTTIMEOUT);
1211 }
1212 else
1213 comm_connect_tcp(&client_p->localClient->fd,
1214 conf->host, conf->port,
1215 NULL, 0, serv_connect_callback, client_p,
1216 conf->aftype, CONNECTTIMEOUT);
1217 }
1218 }
1219 #endif
1220 return 1;
1221 }
1222
1223 struct Client *
1224 find_servconn_in_progress(const char *name)
1225 {
1226 dlink_node *ptr;
1227 struct Client *cptr;
1228
1229 DLINK_FOREACH(ptr, unknown_list.head)
1230 {
1231 cptr = ptr->data;
1232
1233 if (cptr && cptr->name[0])
1234 if (!match(name, cptr->name))
1235 return cptr;
1236 }
1237
1238 return NULL;
1239 }

Properties

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

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