ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/server.c
Revision: 4208
Committed: Sat Jul 12 18:13:06 2014 UTC (12 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 28769 byte(s)
Log Message:
- Renammed global_serv_list to global_server_list

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file s_serv.c
23 * \brief Server related functions.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #ifdef HAVE_LIBCRYPTO
29 #include <openssl/rsa.h>
30 #include "rsa.h"
31 #endif
32 #include "list.h"
33 #include "client.h"
34 #include "event.h"
35 #include "hash.h"
36 #include "irc_string.h"
37 #include "ircd.h"
38 #include "ircd_defs.h"
39 #include "s_bsd.h"
40 #include "numeric.h"
41 #include "packet.h"
42 #include "conf.h"
43 #include "server.h"
44 #include "log.h"
45 #include "user.h"
46 #include "send.h"
47 #include "memory.h"
48 #include "channel.h"
49 #include "parse.h"
50
51 #define MIN_CONN_FREQ 300
52
53 dlink_list flatten_links;
54 static dlink_list cap_list = { NULL, NULL, 0 };
55 static CNCB serv_connect_callback;
56
57
58 /*
59 * write_links_file
60 *
61 * inputs - void pointer which is not used
62 * output - NONE
63 * side effects - called from an event, write out list of linked servers
64 * but in no particular order.
65 */
66 void
67 write_links_file(void *notused)
68 {
69 FILE *file = NULL;
70 dlink_node *ptr = NULL, *ptr_next = NULL;
71 char buff[IRCD_BUFSIZE] = "";
72
73 if ((file = fopen(LIPATH, "w")) == NULL)
74 return;
75
76 DLINK_FOREACH_SAFE(ptr, ptr_next, flatten_links.head)
77 {
78 dlinkDelete(ptr, &flatten_links);
79 MyFree(ptr->data);
80 free_dlink_node(ptr);
81 }
82
83 DLINK_FOREACH(ptr, global_server_list.head)
84 {
85 const struct Client *target_p = ptr->data;
86
87 /*
88 * Skip hidden servers, aswell as ourselves, since we already send
89 * ourselves in /links
90 */
91 if (IsHidden(target_p) || IsMe(target_p))
92 continue;
93
94 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
95 continue;
96
97 /*
98 * Attempt to format the file in such a way it follows the usual links output
99 * ie "servername uplink :hops info"
100 * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
101 * - madmax
102 */
103 snprintf(buff, sizeof(buff), "%s %s :1 %s", target_p->name,
104 me.name, target_p->info);
105 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
106 snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
107 me.name, target_p->info);
108
109 fputs(buff, file);
110 }
111
112 fclose(file);
113 }
114
115 void
116 read_links_file(void)
117 {
118 FILE *file = NULL;
119 char *p = NULL;
120 char buff[IRCD_BUFSIZE] = "";
121
122 if ((file = fopen(LIPATH, "r")) == NULL)
123 return;
124
125 while (fgets(buff, sizeof(buff), file))
126 {
127 if ((p = strchr(buff, '\n')))
128 *p = '\0';
129
130 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
131 }
132
133 fclose(file);
134 }
135
136 /* hunt_server()
137 * Do the basic thing in delivering the message (command)
138 * across the relays to the specific server (server) for
139 * actions.
140 *
141 * Note: The command is a format string and *MUST* be
142 * of prefixed style (e.g. ":%s COMMAND %s ...").
143 * Command can have only max 8 parameters.
144 *
145 * server parv[server] is the parameter identifying the
146 * target server.
147 *
148 * *WARNING*
149 * parv[server] is replaced with the pointer to the
150 * real servername from the matched client (I'm lazy
151 * now --msa).
152 *
153 * returns: (see #defines)
154 */
155 int
156 hunt_server(struct Client *source_p, const char *command,
157 const int server, const int parc, char *parv[])
158 {
159 struct Client *target_p = NULL;
160 dlink_node *ptr = NULL;
161
162 /* Assume it's me, if no server */
163 if (parc <= server || EmptyString(parv[server]))
164 return HUNTED_ISME;
165
166 if ((target_p = find_person(source_p, parv[server])) == NULL)
167 target_p = hash_find_server(parv[server]);
168
169 /*
170 * These are to pickup matches that would cause the following
171 * message to go in the wrong direction while doing quick fast
172 * non-matching lookups.
173 */
174 if (target_p)
175 if (target_p->from == source_p->from && !MyConnect(target_p))
176 target_p = NULL;
177
178 if (!target_p && has_wildcards(parv[server]))
179 {
180 DLINK_FOREACH(ptr, global_client_list.head)
181 {
182 struct Client *tmp = ptr->data;
183
184 assert(IsMe(tmp) || IsServer(tmp) || IsClient(tmp));
185 if (!match(parv[server], tmp->name))
186 {
187 if (tmp->from == source_p->from && !MyConnect(tmp))
188 continue;
189 target_p = ptr->data;
190
191 if (target_p != source_p->from) /* TBV: is this needed? */
192 break;
193 }
194 }
195 }
196
197 if (target_p)
198 {
199 assert(IsMe(target_p) || IsServer(target_p) || IsClient(target_p));
200 if (IsMe(target_p) || MyClient(target_p))
201 return HUNTED_ISME;
202
203 parv[server] = target_p->id;
204 sendto_one(target_p, command, source_p->id,
205 parv[1], parv[2], parv[3], parv[4],
206 parv[5], parv[6], parv[7], parv[8]);
207 return HUNTED_PASS;
208 }
209
210 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
211 return HUNTED_NOSUCH;
212 }
213
214 /* try_connections()
215 *
216 * inputs - void pointer which is not used
217 * output - NONE
218 * side effects -
219 * scan through configuration and try new connections.
220 * Returns the calendar time when the next call to this
221 * function should be made latest. (No harm done if this
222 * is called earlier or later...)
223 */
224 void
225 try_connections(void *unused)
226 {
227 dlink_node *ptr = NULL;
228 int confrq = 0;
229
230 /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
231 if (GlobalSetOptions.autoconn == 0)
232 return;
233
234 DLINK_FOREACH(ptr, server_items.head)
235 {
236 struct MaskItem *conf = ptr->data;
237
238 assert(conf->type == CONF_SERVER);
239
240 /* Also when already connecting! (update holdtimes) --SRB
241 */
242 if (!conf->port || !IsConfAllowAutoConn(conf))
243 continue;
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 (conf->until > CurrentTime)
253 continue;
254
255 assert(conf->class);
256
257 confrq = conf->class->con_freq;
258 if (confrq < MIN_CONN_FREQ)
259 confrq = MIN_CONN_FREQ;
260
261 conf->until = CurrentTime + confrq;
262
263 /*
264 * Found a CONNECT config with port specified, scan clients
265 * and see if this server is already connected?
266 */
267 if (hash_find_server(conf->name))
268 continue;
269
270 if (conf->class->ref_count < conf->class->max_total)
271 {
272 /* Go to the end of the list, if not already last */
273 if (ptr->next)
274 {
275 dlinkDelete(ptr, &server_items);
276 dlinkAddTail(conf, &conf->node, &server_items);
277 }
278
279 if (find_servconn_in_progress(conf->name))
280 return;
281
282 /*
283 * We used to only print this if serv_connect() actually
284 * succeeded, but since comm_tcp_connect() can call the callback
285 * immediately if there is an error, we were getting error messages
286 * in the wrong order. SO, we just print out the activated line,
287 * and let serv_connect() / serv_connect_callback() print an
288 * error afterwards if it fails.
289 * -- adrian
290 */
291 if (ConfigServerHide.hide_server_ips)
292 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
293 "Connection to %s activated.",
294 conf->name);
295 else
296 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
297 "Connection to %s[%s] activated.",
298 conf->name, conf->host);
299
300 serv_connect(conf, NULL);
301 /* We connect only one at time... */
302 return;
303 }
304 }
305 }
306
307 int
308 valid_servname(const char *name)
309 {
310 unsigned int dots = 0;
311 const char *p = name;
312
313 for (; *p; ++p)
314 {
315 if (!IsServChar(*p))
316 return 0;
317
318 if (*p == '.')
319 ++dots;
320 }
321
322 return dots && (p - name) <= HOSTLEN;
323 }
324
325 int
326 check_server(const char *name, struct Client *client_p)
327 {
328 dlink_node *ptr;
329 struct MaskItem *conf = NULL;
330 struct MaskItem *server_conf = NULL;
331 int error = -1;
332
333 assert(client_p);
334
335 /* loop through looking for all possible connect items that might work */
336 DLINK_FOREACH(ptr, server_items.head)
337 {
338 conf = ptr->data;
339
340 if (match(name, conf->name))
341 continue;
342
343 error = -3;
344
345 /* XXX: Fix me for IPv6 */
346 /* XXX sockhost is the IPv4 ip as a string */
347 if (!match(conf->host, client_p->host) ||
348 !match(conf->host, client_p->sockhost))
349 {
350 error = -2;
351
352 if (!match_conf_password(client_p->localClient->passwd, conf))
353 return -2;
354
355 if (!EmptyString(conf->certfp))
356 if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
357 return -4;
358
359 server_conf = conf;
360 }
361 }
362
363 if (server_conf == NULL)
364 return error;
365
366 attach_conf(client_p, server_conf);
367
368
369 if (server_conf)
370 {
371 struct sockaddr_in *v4;
372 #ifdef IPV6
373 struct sockaddr_in6 *v6;
374 #endif
375 switch (server_conf->aftype)
376 {
377 #ifdef IPV6
378 case AF_INET6:
379 v6 = (struct sockaddr_in6 *)&server_conf->addr;
380
381 if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
382 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
383 break;
384 #endif
385 case AF_INET:
386 v4 = (struct sockaddr_in *)&server_conf->addr;
387
388 if (v4->sin_addr.s_addr == INADDR_NONE)
389 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
390 break;
391 }
392 }
393
394 return 0;
395 }
396
397 /* add_capability()
398 *
399 * inputs - string name of CAPAB
400 * - int flag of capability
401 * output - NONE
402 * side effects - Adds given capability name and bit mask to
403 * current supported capabilities. This allows
404 * modules to dynamically add or subtract their capability.
405 */
406 void
407 add_capability(const char *capab_name, int cap_flag, int add_to_default)
408 {
409 struct Capability *cap = MyCalloc(sizeof(*cap));
410
411 cap->name = xstrdup(capab_name);
412 cap->cap = cap_flag;
413 dlinkAdd(cap, &cap->node, &cap_list);
414
415 if (add_to_default)
416 default_server_capabs |= cap_flag;
417 }
418
419 /* delete_capability()
420 *
421 * inputs - string name of CAPAB
422 * output - NONE
423 * side effects - delete given capability from ones known.
424 */
425 int
426 delete_capability(const char *capab_name)
427 {
428 dlink_node *ptr = NULL, *ptr_next = NULL;
429
430 DLINK_FOREACH_SAFE(ptr, ptr_next, cap_list.head)
431 {
432 struct Capability *cap = ptr->data;
433
434 if (cap->cap)
435 {
436 if (!irccmp(cap->name, capab_name))
437 {
438 default_server_capabs &= ~(cap->cap);
439 dlinkDelete(ptr, &cap_list);
440 MyFree(cap->name);
441 MyFree(cap);
442 }
443 }
444 }
445
446 return 0;
447 }
448
449 /*
450 * find_capability()
451 *
452 * inputs - string name of capab to find
453 * output - 0 if not found CAPAB otherwise
454 * side effects - none
455 */
456 unsigned int
457 find_capability(const char *capab)
458 {
459 const dlink_node *ptr = NULL;
460
461 DLINK_FOREACH(ptr, cap_list.head)
462 {
463 const struct Capability *cap = ptr->data;
464
465 if (cap->cap && !irccmp(cap->name, capab))
466 return cap->cap;
467 }
468
469 return 0;
470 }
471
472 /* send_capabilities()
473 *
474 * inputs - Client pointer to send to
475 * - int flag of capabilities that this server can send
476 * output - NONE
477 * side effects - send the CAPAB line to a server -orabidoo
478 *
479 */
480 void
481 send_capabilities(struct Client *client_p, int cap_can_send)
482 {
483 char buf[IRCD_BUFSIZE] = "";
484 const dlink_node *ptr = NULL;
485
486 DLINK_FOREACH(ptr, cap_list.head)
487 {
488 const struct Capability *cap = ptr->data;
489
490 if (cap->cap & (cap_can_send|default_server_capabs))
491 {
492 strlcat(buf, cap->name, sizeof(buf));
493 if (ptr->next)
494 strlcat(buf, " ", sizeof(buf));
495 }
496 }
497
498 sendto_one(client_p, "CAPAB :%s", buf);
499 }
500
501 /*
502 * show_capabilities - show current server capabilities
503 *
504 * inputs - pointer to a struct Client
505 * output - pointer to static string
506 * side effects - build up string representing capabilities of server listed
507 */
508 const char *
509 show_capabilities(const struct Client *target_p)
510 {
511 static char msgbuf[IRCD_BUFSIZE] = "";
512 const dlink_node *ptr = NULL;
513
514 strlcpy(msgbuf, "TS", sizeof(msgbuf));
515
516 DLINK_FOREACH(ptr, cap_list.head)
517 {
518 const struct Capability *cap = ptr->data;
519
520 if (!IsCapable(target_p, cap->cap))
521 continue;
522
523 strlcat(msgbuf, " ", sizeof(msgbuf));
524 strlcat(msgbuf, cap->name, sizeof(msgbuf));
525 }
526
527 return msgbuf;
528 }
529
530 /* make_server()
531 *
532 * inputs - pointer to client struct
533 * output - pointer to struct Server
534 * side effects - add's an Server information block to a client
535 * if it was not previously allocated.
536 */
537 struct Server *
538 make_server(struct Client *client_p)
539 {
540 if (client_p->serv == NULL)
541 client_p->serv = MyCalloc(sizeof(struct Server));
542
543 return client_p->serv;
544 }
545
546 /* New server connection code
547 * Based upon the stuff floating about in s_bsd.c
548 * -- adrian
549 */
550
551 /* serv_connect() - initiate a server connection
552 *
553 * inputs - pointer to conf
554 * - pointer to client doing the connect
555 * output -
556 * side effects -
557 *
558 * This code initiates a connection to a server. It first checks to make
559 * sure the given server exists. If this is the case, it creates a socket,
560 * creates a client, saves the socket information in the client, and
561 * initiates a connection to the server through comm_connect_tcp(). The
562 * completion of this goes through serv_completed_connection().
563 *
564 * We return 1 if the connection is attempted, since we don't know whether
565 * it suceeded or not, and 0 if it fails in here somewhere.
566 */
567 int
568 serv_connect(struct MaskItem *conf, struct Client *by)
569 {
570 struct Client *client_p = NULL;
571 char buf[HOSTIPLEN + 1] = "";
572
573 /* conversion structs */
574 struct sockaddr_in *v4;
575
576 /* Make sure conf is useful */
577 assert(conf);
578
579 getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
580 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
581 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
582 buf);
583
584 /* Still processing a DNS lookup? -> exit */
585 if (conf->dns_pending)
586 {
587 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
588 "Error connecting to %s: DNS lookup for connect{} in progress.",
589 conf->name);
590 return 0;
591 }
592
593 if (conf->dns_failed)
594 {
595 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
596 "Error connecting to %s: DNS lookup for connect{} failed.",
597 conf->name);
598 return 0;
599 }
600
601 /* Make sure this server isn't already connected
602 * Note: conf should ALWAYS be a valid C: line
603 */
604 if ((client_p = hash_find_server(conf->name)))
605 {
606 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
607 "Server %s already present from %s",
608 conf->name, get_client_name(client_p, SHOW_IP));
609 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
610 "Server %s already present from %s",
611 conf->name, get_client_name(client_p, MASK_IP));
612 if (by && IsClient(by) && !MyClient(by))
613 sendto_one_notice(by, &me, ":Server %s already present from %s",
614 conf->name, get_client_name(client_p, MASK_IP));
615 return 0;
616 }
617
618 /* Create a local client */
619 client_p = make_client(NULL);
620
621 /* Copy in the server, hostname, fd */
622 strlcpy(client_p->name, conf->name, sizeof(client_p->name));
623 strlcpy(client_p->host, conf->host, sizeof(client_p->host));
624
625 /* We already converted the ip once, so lets use it - stu */
626 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
627
628 /* create a socket for the server connection */
629 if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0)
630 {
631 /* Eek, failure to create the socket */
632 report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
633
634 SetDead(client_p);
635 exit_client(client_p, "Connection failed");
636 return 0;
637 }
638
639 /* servernames are always guaranteed under HOSTLEN chars */
640 fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
641
642 /* Attach config entries to client here rather than in
643 * serv_connect_callback(). This to avoid null pointer references.
644 */
645 if (!attach_connect_block(client_p, conf->name, conf->host))
646 {
647 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
648 "Host %s is not enabled for connecting: no connect{} block",
649 conf->name);
650 if (by && IsClient(by) && !MyClient(by))
651 sendto_one_notice(by, &me, ":Connect to host %s failed.", client_p->name);
652
653 SetDead(client_p);
654 exit_client(client_p, "Connection failed");
655 return 0;
656 }
657
658 /* at this point we have a connection in progress and C/N lines
659 * attached to the client, the socket info should be saved in the
660 * client and it should either be resolved or have a valid address.
661 *
662 * The socket has been connected or connect is in progress.
663 */
664 make_server(client_p);
665
666 if (by && IsClient(by))
667 strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
668 else
669 strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
670
671 SetConnecting(client_p);
672 client_p->localClient->aftype = conf->aftype;
673
674 /* Now, initiate the connection */
675 /* XXX assume that a non 0 type means a specific bind address
676 * for this connect.
677 */
678 switch (conf->aftype)
679 {
680 case AF_INET:
681 v4 = (struct sockaddr_in*)&conf->bind;
682 if (v4->sin_addr.s_addr)
683 {
684 struct irc_ssaddr ipn;
685 memset(&ipn, 0, sizeof(struct irc_ssaddr));
686 ipn.ss.ss_family = AF_INET;
687 ipn.ss_port = 0;
688 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
689 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
690 (struct sockaddr *)&ipn, ipn.ss_len,
691 serv_connect_callback, client_p, conf->aftype,
692 CONNECTTIMEOUT);
693 }
694 else if (ServerInfo.specific_ipv4_vhost)
695 {
696 struct irc_ssaddr ipn;
697 memset(&ipn, 0, sizeof(struct irc_ssaddr));
698 ipn.ss.ss_family = AF_INET;
699 ipn.ss_port = 0;
700 memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
701 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
702 (struct sockaddr *)&ipn, ipn.ss_len,
703 serv_connect_callback, client_p, conf->aftype,
704 CONNECTTIMEOUT);
705 }
706 else
707 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
708 NULL, 0, serv_connect_callback, client_p, conf->aftype,
709 CONNECTTIMEOUT);
710 break;
711 #ifdef IPV6
712 case AF_INET6:
713 {
714 struct irc_ssaddr ipn;
715 struct sockaddr_in6 *v6;
716 struct sockaddr_in6 *v6conf;
717
718 memset(&ipn, 0, sizeof(struct irc_ssaddr));
719 v6conf = (struct sockaddr_in6 *)&conf->bind;
720 v6 = (struct sockaddr_in6 *)&ipn;
721
722 if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
723 {
724 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
725 ipn.ss.ss_family = AF_INET6;
726 ipn.ss_port = 0;
727 comm_connect_tcp(&client_p->localClient->fd,
728 conf->host, conf->port,
729 (struct sockaddr *)&ipn, ipn.ss_len,
730 serv_connect_callback, client_p,
731 conf->aftype, CONNECTTIMEOUT);
732 }
733 else if (ServerInfo.specific_ipv6_vhost)
734 {
735 memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
736 ipn.ss.ss_family = AF_INET6;
737 ipn.ss_port = 0;
738 comm_connect_tcp(&client_p->localClient->fd,
739 conf->host, conf->port,
740 (struct sockaddr *)&ipn, ipn.ss_len,
741 serv_connect_callback, client_p,
742 conf->aftype, CONNECTTIMEOUT);
743 }
744 else
745 comm_connect_tcp(&client_p->localClient->fd,
746 conf->host, conf->port,
747 NULL, 0, serv_connect_callback, client_p,
748 conf->aftype, CONNECTTIMEOUT);
749 }
750 #endif
751 }
752 return 1;
753 }
754
755 #ifdef HAVE_LIBCRYPTO
756 static void
757 finish_ssl_server_handshake(struct Client *client_p)
758 {
759 struct MaskItem *conf = NULL;
760
761 conf = find_conf_name(&client_p->localClient->confs,
762 client_p->name, CONF_SERVER);
763 if (conf == NULL)
764 {
765 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
766 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
767 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
768 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
769
770 exit_client(client_p, "Lost connect{} block");
771 return;
772 }
773
774 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
775
776 send_capabilities(client_p, 0);
777
778 sendto_one(client_p, "SERVER %s 1 :%s%s",
779 me.name, ConfigServerHide.hidden ? "(H) " : "",
780 me.info);
781
782 /* If we've been marked dead because a send failed, just exit
783 * here now and save everyone the trouble of us ever existing.
784 */
785 if (IsDead(client_p))
786 {
787 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
788 "%s[%s] went dead during handshake",
789 client_p->name,
790 client_p->host);
791 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
792 "%s went dead during handshake", client_p->name);
793 return;
794 }
795
796 /* don't move to serv_list yet -- we haven't sent a burst! */
797 /* If we get here, we're ok, so lets start reading some data */
798 comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, read_packet, client_p, 0);
799 }
800
801 static void
802 ssl_server_handshake(fde_t *fd, struct Client *client_p)
803 {
804 X509 *cert = NULL;
805 int ret = 0;
806
807 if ((ret = SSL_connect(client_p->localClient->fd.ssl)) <= 0)
808 {
809 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
810 {
811 case SSL_ERROR_WANT_WRITE:
812 comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
813 (PF *)ssl_server_handshake, client_p, 0);
814 return;
815 case SSL_ERROR_WANT_READ:
816 comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
817 (PF *)ssl_server_handshake, client_p, 0);
818 return;
819 default:
820 {
821 const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
822 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
823 "Error connecting to %s: %s", client_p->name,
824 sslerr ? sslerr : "unknown SSL error");
825 exit_client(client_p, "Error during SSL handshake");
826 return;
827 }
828 }
829 }
830
831 if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
832 {
833 int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
834 char buf[EVP_MAX_MD_SIZE * 2 + 1] = "";
835 unsigned char md[EVP_MAX_MD_SIZE] = "";
836
837 if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
838 res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
839 res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
840 {
841 unsigned int n = 0;
842
843 if (X509_digest(cert, ServerInfo.message_digest_algorithm, md, &n))
844 {
845 binary_to_hex(md, buf, n);
846 client_p->certfp = xstrdup(buf);
847 }
848 }
849 else
850 ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad SSL client certificate: %d",
851 client_p->name, client_p->username, client_p->host, res);
852 X509_free(cert);
853 }
854
855 finish_ssl_server_handshake(client_p);
856 }
857
858 static void
859 ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd)
860 {
861 if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.client_ctx)) == NULL)
862 {
863 ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
864 ERR_error_string(ERR_get_error(), NULL));
865 SetDead(client_p);
866 exit_client(client_p, "SSL_new failed");
867 return;
868 }
869
870 SSL_set_fd(fd->ssl, fd->fd);
871
872 if (!EmptyString(conf->cipher_list))
873 SSL_set_cipher_list(client_p->localClient->fd.ssl, conf->cipher_list);
874
875 ssl_server_handshake(NULL, client_p);
876 }
877 #endif
878
879 /* serv_connect_callback() - complete a server connection.
880 *
881 * This routine is called after the server connection attempt has
882 * completed. If unsucessful, an error is sent to ops and the client
883 * is closed. If sucessful, it goes through the initialisation/check
884 * procedures, the capabilities are sent, and the socket is then
885 * marked for reading.
886 */
887 static void
888 serv_connect_callback(fde_t *fd, int status, void *data)
889 {
890 struct Client *client_p = data;
891 struct MaskItem *conf = NULL;
892
893 /* First, make sure its a real client! */
894 assert(client_p);
895 assert(&client_p->localClient->fd == fd);
896
897 /* Next, for backward purposes, record the ip of the server */
898 memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
899 sizeof(struct irc_ssaddr));
900
901 /* Check the status */
902 if (status != COMM_OK)
903 {
904 /* We have an error, so report it and quit
905 * Admins get to see any IP, mere opers don't *sigh*
906 */
907 if (ConfigServerHide.hide_server_ips)
908 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
909 "Error connecting to %s: %s",
910 client_p->name, comm_errstr(status));
911 else
912 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
913 "Error connecting to %s[%s]: %s", client_p->name,
914 client_p->host, comm_errstr(status));
915
916 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
917 "Error connecting to %s: %s",
918 client_p->name, comm_errstr(status));
919
920 /* If a fd goes bad, call dead_link() the socket is no
921 * longer valid for reading or writing.
922 */
923 dead_link_on_write(client_p, 0);
924 return;
925 }
926
927 /* COMM_OK, so continue the connection procedure */
928 /* Get the C/N lines */
929 conf = find_conf_name(&client_p->localClient->confs,
930 client_p->name, CONF_SERVER);
931 if (conf == NULL)
932 {
933 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
934 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
935 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
936 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
937
938 exit_client(client_p, "Lost connect{} block");
939 return;
940 }
941
942 /* Next, send the initial handshake */
943 SetHandshake(client_p);
944
945 #ifdef HAVE_LIBCRYPTO
946 if (IsConfSSL(conf))
947 {
948 ssl_connect_init(client_p, conf, fd);
949 return;
950 }
951 #endif
952
953 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
954
955 send_capabilities(client_p, 0);
956
957 sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
958 ConfigServerHide.hidden ? "(H) " : "", me.info);
959
960 /* If we've been marked dead because a send failed, just exit
961 * here now and save everyone the trouble of us ever existing.
962 */
963 if (IsDead(client_p))
964 {
965 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
966 "%s[%s] went dead during handshake",
967 client_p->name,
968 client_p->host);
969 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
970 "%s went dead during handshake", client_p->name);
971 return;
972 }
973
974 /* don't move to serv_list yet -- we haven't sent a burst! */
975 /* If we get here, we're ok, so lets start reading some data */
976 comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
977 }
978
979 struct Client *
980 find_servconn_in_progress(const char *name)
981 {
982 dlink_node *ptr;
983 struct Client *cptr;
984
985 DLINK_FOREACH(ptr, unknown_list.head)
986 {
987 cptr = ptr->data;
988
989 if (cptr && cptr->name[0])
990 if (!match(name, cptr->name))
991 return cptr;
992 }
993
994 return NULL;
995 }

Properties

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