ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/server.c
Revision: 6527
Committed: Fri Sep 11 16:33:29 2015 UTC (9 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 28798 byte(s)
Log Message:
- Fixed inconsistent style in several places

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

Properties

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