ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/server.c
Revision: 4207
Committed: Sat Jul 12 17:52:08 2014 UTC (9 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 28767 byte(s)
Log Message:
- server.c: cleaned up hunt_server()

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 2916 * Copyright (c) 1997-2014 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * 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     #define MIN_CONN_FREQ 300
52    
53 michael 2156 dlink_list flatten_links;
54 adx 30 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 michael 2156 write_links_file(void *notused)
68 adx 30 {
69 michael 2156 FILE *file = NULL;
70 michael 2216 dlink_node *ptr = NULL, *ptr_next = NULL;
71 michael 3215 char buff[IRCD_BUFSIZE] = "";
72 adx 30
73 michael 2156 if ((file = fopen(LIPATH, "w")) == NULL)
74 adx 30 return;
75    
76 michael 2156 DLINK_FOREACH_SAFE(ptr, ptr_next, flatten_links.head)
77 adx 30 {
78 michael 2156 dlinkDelete(ptr, &flatten_links);
79     MyFree(ptr->data);
80     free_dlink_node(ptr);
81 adx 30 }
82    
83     DLINK_FOREACH(ptr, global_serv_list.head)
84     {
85 michael 1325 const struct Client *target_p = ptr->data;
86 adx 30
87 michael 2156 /*
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 adx 30 continue;
93    
94 michael 1851 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
95     continue;
96    
97 michael 1545 /*
98     * Attempt to format the file in such a way it follows the usual links output
99 adx 30 * ie "servername uplink :hops info"
100     * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
101     * - madmax
102     */
103 michael 2156 snprintf(buff, sizeof(buff), "%s %s :1 %s", target_p->name,
104     me.name, target_p->info);
105 michael 2212 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
106 michael 2156 snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
107     me.name, target_p->info);
108 adx 30
109 michael 1325 fputs(buff, file);
110 adx 30 }
111    
112 michael 1325 fclose(file);
113 adx 30 }
114    
115 michael 2216 void
116     read_links_file(void)
117     {
118     FILE *file = NULL;
119     char *p = NULL;
120 michael 3215 char buff[IRCD_BUFSIZE] = "";
121 michael 2216
122     if ((file = fopen(LIPATH, "r")) == NULL)
123     return;
124    
125     while (fgets(buff, sizeof(buff), file))
126     {
127 michael 3246 if ((p = strchr(buff, '\n')))
128 michael 2216 *p = '\0';
129    
130     dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
131     }
132    
133     fclose(file);
134     }
135    
136 adx 30 /* 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 michael 3156 hunt_server(struct Client *source_p, const char *command,
157 michael 1857 const int server, const int parc, char *parv[])
158 adx 30 {
159     struct Client *target_p = NULL;
160 michael 4207 dlink_node *ptr = NULL;
161 adx 30
162 michael 1344 /* Assume it's me, if no server */
163     if (parc <= server || EmptyString(parv[server]))
164     return HUNTED_ISME;
165 adx 30
166 michael 4207 if ((target_p = find_person(source_p, parv[server])) == NULL)
167     target_p = hash_find_server(parv[server]);
168 michael 1344
169 michael 4207 /*
170     * These are to pickup matches that would cause the following
171 adx 30 * 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 michael 4207 if (!target_p && has_wildcards(parv[server]))
179 adx 30 {
180 michael 4207 DLINK_FOREACH(ptr, global_client_list.head)
181 adx 30 {
182 michael 4207 struct Client *tmp = ptr->data;
183    
184     assert(IsMe(tmp) || IsServer(tmp) || IsClient(tmp));
185     if (!match(parv[server], tmp->name))
186 adx 30 {
187 michael 4207 if (tmp->from == source_p->from && !MyConnect(tmp))
188     continue;
189     target_p = ptr->data;
190 adx 30
191 michael 4207 if (target_p != source_p->from) /* TBV: is this needed? */
192     break;
193 adx 30 }
194     }
195     }
196    
197 michael 3246 if (target_p)
198 adx 30 {
199 michael 4207 assert(IsMe(target_p) || IsServer(target_p) || IsClient(target_p));
200 adx 30 if (IsMe(target_p) || MyClient(target_p))
201     return HUNTED_ISME;
202    
203 michael 4207 parv[server] = target_p->id;
204     sendto_one(target_p, command, source_p->id,
205 adx 30 parv[1], parv[2], parv[3], parv[4],
206     parv[5], parv[6], parv[7], parv[8]);
207 michael 2134 return HUNTED_PASS;
208 michael 2345 }
209 adx 30
210 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
211 michael 2134 return HUNTED_NOSUCH;
212 adx 30 }
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 michael 1632 dlink_node *ptr = NULL;
228 michael 3335 int confrq = 0;
229 adx 30
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 michael 3335 struct MaskItem *conf = ptr->data;
237 adx 30
238 michael 1636 assert(conf->type == CONF_SERVER);
239 michael 1632
240 michael 2345 /* Also when already connecting! (update holdtimes) --SRB
241 adx 30 */
242 michael 4197 if (!conf->port || !IsConfAllowAutoConn(conf))
243 adx 30 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 michael 1649 if (conf->until > CurrentTime)
253 adx 30 continue;
254    
255 michael 4029 assert(conf->class);
256 adx 30
257 michael 4029 confrq = conf->class->con_freq;
258     if (confrq < MIN_CONN_FREQ)
259     confrq = MIN_CONN_FREQ;
260    
261 michael 1649 conf->until = CurrentTime + confrq;
262 adx 30
263 michael 3246 /*
264     * Found a CONNECT config with port specified, scan clients
265 adx 30 * and see if this server is already connected?
266     */
267 michael 3246 if (hash_find_server(conf->name))
268 adx 30 continue;
269    
270 michael 1632 if (conf->class->ref_count < conf->class->max_total)
271 adx 30 {
272     /* Go to the end of the list, if not already last */
273 michael 3246 if (ptr->next)
274 adx 30 {
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 michael 3246 /*
283     * We used to only print this if serv_connect() actually
284 adx 30 * 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 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
293     "Connection to %s activated.",
294 adx 30 conf->name);
295     else
296 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
297     "Connection to %s[%s] activated.",
298 michael 1632 conf->name, conf->host);
299 adx 30
300 michael 1632 serv_connect(conf, NULL);
301 adx 30 /* We connect only one at time... */
302     return;
303     }
304     }
305     }
306    
307     int
308 michael 1115 valid_servname(const char *name)
309     {
310 michael 3452 unsigned int dots = 0;
311 michael 1115 const char *p = name;
312    
313     for (; *p; ++p)
314     {
315     if (!IsServChar(*p))
316 michael 1118 return 0;
317 michael 1115
318     if (*p == '.')
319     ++dots;
320     }
321    
322 michael 3452 return dots && (p - name) <= HOSTLEN;
323 michael 1115 }
324    
325     int
326 michael 1302 check_server(const char *name, struct Client *client_p)
327 adx 30 {
328     dlink_node *ptr;
329 michael 1632 struct MaskItem *conf = NULL;
330     struct MaskItem *server_conf = NULL;
331 adx 30 int error = -1;
332    
333 michael 3246 assert(client_p);
334 adx 30
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 michael 1652 if (match(name, conf->name))
341 adx 30 continue;
342    
343     error = -3;
344    
345     /* XXX: Fix me for IPv6 */
346     /* XXX sockhost is the IPv4 ip as a string */
347 michael 2345 if (!match(conf->host, client_p->host) ||
348 michael 1652 !match(conf->host, client_p->sockhost))
349 adx 30 {
350     error = -2;
351    
352 michael 1632 if (!match_conf_password(client_p->localClient->passwd, conf))
353 michael 1414 return -2;
354 adx 30
355 michael 2228 if (!EmptyString(conf->certfp))
356 michael 2229 if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
357 michael 2228 return -4;
358    
359 michael 1414 server_conf = conf;
360 adx 30 }
361     }
362    
363     if (server_conf == NULL)
364 michael 2182 return error;
365 adx 30
366     attach_conf(client_p, server_conf);
367    
368    
369 michael 3246 if (server_conf)
370 adx 30 {
371     struct sockaddr_in *v4;
372     #ifdef IPV6
373     struct sockaddr_in6 *v6;
374     #endif
375 michael 1632 switch (server_conf->aftype)
376 adx 30 {
377     #ifdef IPV6
378 michael 2345 case AF_INET6:
379 michael 1632 v6 = (struct sockaddr_in6 *)&server_conf->addr;
380 adx 30
381     if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
382 michael 1632 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
383 adx 30 break;
384     #endif
385     case AF_INET:
386 michael 1632 v4 = (struct sockaddr_in *)&server_conf->addr;
387 adx 30
388     if (v4->sin_addr.s_addr == INADDR_NONE)
389 michael 2345 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
390 adx 30 break;
391     }
392     }
393    
394 michael 2182 return 0;
395 adx 30 }
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 michael 3505 struct Capability *cap = MyCalloc(sizeof(*cap));
410 adx 30
411 michael 1646 cap->name = xstrdup(capab_name);
412 adx 30 cap->cap = cap_flag;
413     dlinkAdd(cap, &cap->node, &cap_list);
414 michael 885
415 adx 30 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 michael 3335 dlink_node *ptr = NULL, *ptr_next = NULL;
429 adx 30
430 michael 3335 DLINK_FOREACH_SAFE(ptr, ptr_next, cap_list.head)
431 adx 30 {
432 michael 3335 struct Capability *cap = ptr->data;
433 adx 30
434 michael 3335 if (cap->cap)
435 adx 30 {
436 michael 3335 if (!irccmp(cap->name, capab_name))
437 adx 30 {
438 michael 2134 default_server_capabs &= ~(cap->cap);
439     dlinkDelete(ptr, &cap_list);
440     MyFree(cap->name);
441     MyFree(cap);
442 adx 30 }
443     }
444     }
445    
446 michael 896 return 0;
447 adx 30 }
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 michael 1877 unsigned int
457 adx 30 find_capability(const char *capab)
458     {
459 michael 896 const dlink_node *ptr = NULL;
460 adx 30
461     DLINK_FOREACH(ptr, cap_list.head)
462     {
463 michael 896 const struct Capability *cap = ptr->data;
464 adx 30
465 michael 896 if (cap->cap && !irccmp(cap->name, capab))
466     return cap->cap;
467 adx 30 }
468 michael 896
469     return 0;
470 adx 30 }
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 michael 1632 send_capabilities(struct Client *client_p, int cap_can_send)
482 adx 30 {
483 michael 3747 char buf[IRCD_BUFSIZE] = "";
484     const dlink_node *ptr = NULL;
485 adx 30
486     DLINK_FOREACH(ptr, cap_list.head)
487     {
488 michael 3747 const struct Capability *cap = ptr->data;
489 adx 30
490     if (cap->cap & (cap_can_send|default_server_capabs))
491     {
492 michael 3747 strlcat(buf, cap->name, sizeof(buf));
493     if (ptr->next)
494     strlcat(buf, " ", sizeof(buf));
495 adx 30 }
496     }
497    
498 michael 3747 sendto_one(client_p, "CAPAB :%s", buf);
499 adx 30 }
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 michael 2282 show_capabilities(const struct Client *target_p)
510 adx 30 {
511 michael 2309 static char msgbuf[IRCD_BUFSIZE] = "";
512 michael 2282 const dlink_node *ptr = NULL;
513 adx 30
514 michael 2309 strlcpy(msgbuf, "TS", sizeof(msgbuf));
515    
516 adx 30 DLINK_FOREACH(ptr, cap_list.head)
517     {
518     const struct Capability *cap = ptr->data;
519    
520 michael 2282 if (!IsCapable(target_p, cap->cap))
521     continue;
522    
523     strlcat(msgbuf, " ", sizeof(msgbuf));
524     strlcat(msgbuf, cap->name, sizeof(msgbuf));
525 adx 30 }
526 michael 1302
527     return msgbuf;
528 adx 30 }
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 michael 3505 client_p->serv = MyCalloc(sizeof(struct Server));
542 adx 30
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 michael 2345 * inputs - pointer to conf
554 adx 30 * - 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 michael 1632 serv_connect(struct MaskItem *conf, struct Client *by)
569 adx 30 {
570 michael 3246 struct Client *client_p = NULL;
571     char buf[HOSTIPLEN + 1] = "";
572 adx 30
573     /* conversion structs */
574     struct sockaddr_in *v4;
575 michael 3246
576 michael 1632 /* Make sure conf is useful */
577 michael 3246 assert(conf);
578 adx 30
579 michael 1632 getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
580 michael 1123 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
581 michael 1632 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
582 adx 30 buf);
583    
584     /* Still processing a DNS lookup? -> exit */
585 michael 1632 if (conf->dns_pending)
586 adx 30 {
587 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
588 michael 992 "Error connecting to %s: DNS lookup for connect{} in progress.",
589     conf->name);
590 michael 3246 return 0;
591 adx 30 }
592    
593 michael 1632 if (conf->dns_failed)
594 michael 992 {
595 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
596 michael 992 "Error connecting to %s: DNS lookup for connect{} failed.",
597     conf->name);
598 michael 3246 return 0;
599 michael 992 }
600    
601 adx 30 /* Make sure this server isn't already connected
602 michael 1632 * Note: conf should ALWAYS be a valid C: line
603 adx 30 */
604 michael 3246 if ((client_p = hash_find_server(conf->name)))
605 michael 2345 {
606 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
607 michael 2134 "Server %s already present from %s",
608     conf->name, get_client_name(client_p, SHOW_IP));
609 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
610 michael 2134 "Server %s already present from %s",
611     conf->name, get_client_name(client_p, MASK_IP));
612 adx 30 if (by && IsClient(by) && !MyClient(by))
613 michael 3110 sendto_one_notice(by, &me, ":Server %s already present from %s",
614     conf->name, get_client_name(client_p, MASK_IP));
615 michael 2134 return 0;
616 adx 30 }
617 michael 2345
618 adx 30 /* 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 michael 1632 strlcpy(client_p->host, conf->host, sizeof(client_p->host));
624 adx 30
625     /* We already converted the ip once, so lets use it - stu */
626 michael 1115 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
627 adx 30
628 michael 2345 /* create a socket for the server connection */
629 michael 3335 if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0)
630 adx 30 {
631     /* Eek, failure to create the socket */
632 michael 3335 report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
633    
634 adx 30 SetDead(client_p);
635 michael 3171 exit_client(client_p, "Connection failed");
636 michael 2134 return 0;
637 adx 30 }
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 michael 1632 if (!attach_connect_block(client_p, conf->name, conf->host))
646 adx 30 {
647 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
648 michael 2134 "Host %s is not enabled for connecting: no connect{} block",
649     conf->name);
650 michael 2345 if (by && IsClient(by) && !MyClient(by))
651 michael 3110 sendto_one_notice(by, &me, ":Connect to host %s failed.", client_p->name);
652    
653 adx 30 SetDead(client_p);
654 michael 3171 exit_client(client_p, "Connection failed");
655 michael 2134 return 0;
656 adx 30 }
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 michael 1632 client_p->localClient->aftype = conf->aftype;
673 adx 30
674     /* Now, initiate the connection */
675 michael 2345 /* XXX assume that a non 0 type means a specific bind address
676 adx 30 * for this connect.
677     */
678 michael 1632 switch (conf->aftype)
679 adx 30 {
680     case AF_INET:
681 michael 1632 v4 = (struct sockaddr_in*)&conf->bind;
682 michael 3246 if (v4->sin_addr.s_addr)
683 adx 30 {
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 michael 1632 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
689 michael 2134 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
690 michael 2345 (struct sockaddr *)&ipn, ipn.ss_len,
691 michael 2134 serv_connect_callback, client_p, conf->aftype,
692     CONNECTTIMEOUT);
693 adx 30 }
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 michael 1632 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
702 adx 30 (struct sockaddr *)&ipn, ipn.ss_len,
703 michael 1632 serv_connect_callback, client_p, conf->aftype,
704 michael 2134 CONNECTTIMEOUT);
705 adx 30 }
706     else
707 michael 2345 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
708     NULL, 0, serv_connect_callback, client_p, conf->aftype,
709 adx 30 CONNECTTIMEOUT);
710     break;
711     #ifdef IPV6
712     case AF_INET6:
713     {
714 michael 2182 struct irc_ssaddr ipn;
715     struct sockaddr_in6 *v6;
716     struct sockaddr_in6 *v6conf;
717 adx 30
718 michael 2182 memset(&ipn, 0, sizeof(struct irc_ssaddr));
719     v6conf = (struct sockaddr_in6 *)&conf->bind;
720     v6 = (struct sockaddr_in6 *)&ipn;
721 adx 30
722 michael 3246 if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
723 adx 30 {
724 michael 2182 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 michael 2345 (struct sockaddr *)&ipn, ipn.ss_len,
730 michael 2182 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 michael 2345 conf->host, conf->port,
747 michael 2182 NULL, 0, serv_connect_callback, client_p,
748     conf->aftype, CONNECTTIMEOUT);
749 adx 30 }
750     #endif
751     }
752 michael 2182 return 1;
753 adx 30 }
754    
755 michael 1303 #ifdef HAVE_LIBCRYPTO
756     static void
757     finish_ssl_server_handshake(struct Client *client_p)
758     {
759 michael 1632 struct MaskItem *conf = NULL;
760 michael 1303
761     conf = find_conf_name(&client_p->localClient->confs,
762 michael 1632 client_p->name, CONF_SERVER);
763 michael 1303 if (conf == NULL)
764     {
765 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
766 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
767 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
768 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
769    
770 michael 3171 exit_client(client_p, "Lost connect{} block");
771 michael 1303 return;
772     }
773    
774 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
775 michael 1303
776 michael 1632 send_capabilities(client_p, 0);
777 michael 1303
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 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
788 michael 1303 "%s[%s] went dead during handshake",
789     client_p->name,
790     client_p->host);
791 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
792 michael 1303 "%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 michael 1306 ssl_server_handshake(fde_t *fd, struct Client *client_p)
803 michael 1303 {
804 michael 2463 X509 *cert = NULL;
805     int ret = 0;
806 michael 1303
807 michael 2463 if ((ret = SSL_connect(client_p->localClient->fd.ssl)) <= 0)
808 michael 1303 {
809 michael 2463 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
810 michael 1303 {
811     case SSL_ERROR_WANT_WRITE:
812     comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
813 michael 1308 (PF *)ssl_server_handshake, client_p, 0);
814 michael 1303 return;
815     case SSL_ERROR_WANT_READ:
816     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
817 michael 1308 (PF *)ssl_server_handshake, client_p, 0);
818 michael 1303 return;
819     default:
820 michael 1308 {
821     const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
822 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
823 michael 1308 "Error connecting to %s: %s", client_p->name,
824     sslerr ? sslerr : "unknown SSL error");
825 michael 3171 exit_client(client_p, "Error during SSL handshake");
826 michael 1303 return;
827 michael 1308 }
828 michael 1303 }
829     }
830    
831 michael 2463 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 michael 3375 char buf[EVP_MAX_MD_SIZE * 2 + 1] = "";
835     unsigned char md[EVP_MAX_MD_SIZE] = "";
836 michael 2463
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 michael 3375 unsigned int n = 0;
842 michael 2463
843 michael 4115 if (X509_digest(cert, ServerInfo.message_digest_algorithm, md, &n))
844 michael 2463 {
845 michael 4112 binary_to_hex(md, buf, n);
846 michael 2463 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 michael 1303 finish_ssl_server_handshake(client_p);
856     }
857    
858     static void
859 michael 1632 ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd)
860 michael 1303 {
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 michael 3171 exit_client(client_p, "SSL_new failed");
867 michael 1303 return;
868     }
869    
870     SSL_set_fd(fd->ssl, fd->fd);
871 michael 1306
872 michael 1632 if (!EmptyString(conf->cipher_list))
873     SSL_set_cipher_list(client_p->localClient->fd.ssl, conf->cipher_list);
874 michael 1306
875     ssl_server_handshake(NULL, client_p);
876 michael 1303 }
877     #endif
878    
879 adx 30 /* serv_connect_callback() - complete a server connection.
880 michael 2345 *
881 adx 30 * 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 michael 1632 struct MaskItem *conf = NULL;
892 adx 30
893     /* First, make sure its a real client! */
894 michael 3246 assert(client_p);
895 adx 30 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 michael 3246
901 adx 30 /* 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 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
909 adx 30 "Error connecting to %s: %s",
910     client_p->name, comm_errstr(status));
911     else
912 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
913 michael 2134 "Error connecting to %s[%s]: %s", client_p->name,
914     client_p->host, comm_errstr(status));
915 adx 30
916 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
917 michael 2134 "Error connecting to %s: %s",
918     client_p->name, comm_errstr(status));
919 adx 30
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 michael 2134 client_p->name, CONF_SERVER);
931 adx 30 if (conf == NULL)
932     {
933 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
934 michael 2134 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
935 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
936 michael 2134 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
937 adx 30
938 michael 3171 exit_client(client_p, "Lost connect{} block");
939 adx 30 return;
940     }
941    
942     /* Next, send the initial handshake */
943     SetHandshake(client_p);
944    
945     #ifdef HAVE_LIBCRYPTO
946 michael 1632 if (IsConfSSL(conf))
947 michael 1303 {
948 michael 1632 ssl_connect_init(client_p, conf, fd);
949 michael 1303 return;
950     }
951 adx 30 #endif
952    
953 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
954 adx 30
955 michael 1632 send_capabilities(client_p, 0);
956 adx 30
957 michael 2134 sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
958     ConfigServerHide.hidden ? "(H) " : "", me.info);
959 adx 30
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 michael 2345 if (IsDead(client_p))
964 adx 30 {
965 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
966 michael 2134 "%s[%s] went dead during handshake",
967 adx 30 client_p->name,
968 michael 2134 client_p->host);
969 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
970 michael 2134 "%s went dead during handshake", client_p->name);
971 adx 30 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 michael 1652 if (!match(name, cptr->name))
991 adx 30 return cptr;
992     }
993 michael 2345
994 adx 30 return NULL;
995     }

Properties

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