ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/server.c
Revision: 6723
Committed: Tue Nov 3 19:49:29 2015 UTC (9 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 29067 byte(s)
Log Message:
- server.c:check_server(): removed oudated comment

File Contents

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

Properties

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