ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/server.c
Revision: 6157
Committed: Thu Jun 18 09:22:51 2015 UTC (8 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 29479 byte(s)
Log Message:
- Automatically generate sid on startup if none is configured. Patch provided by Adam

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 5346 * 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 4564 * 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     #define MIN_CONN_FREQ 300
52    
53 michael 2156 dlink_list flatten_links;
54 michael 5795 static dlink_list server_capabilities_list;
55 michael 4463 static void serv_connect_callback(fde_t *, int, void *);
56 adx 30
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 4438 write_links_file(void *unused)
68 adx 30 {
69 michael 2156 FILE *file = NULL;
70 michael 4816 dlink_node *node = NULL, *node_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 4816 DLINK_FOREACH_SAFE(node, node_next, flatten_links.head)
77 adx 30 {
78 michael 4816 dlinkDelete(node, &flatten_links);
79     MyFree(node->data);
80     free_dlink_node(node);
81 adx 30 }
82    
83 michael 4816 DLINK_FOREACH(node, global_server_list.head)
84 adx 30 {
85 michael 4816 const struct Client *target_p = node->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 4816 dlink_node *node = 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 6061 DLINK_FOREACH(node, global_server_list.head)
181 adx 30 {
182 michael 4816 struct Client *tmp = node->data;
183 michael 4207
184 michael 6061 assert(IsMe(tmp) || IsServer(tmp));
185 michael 4207 if (!match(parv[server], tmp->name))
186 adx 30 {
187 michael 4207 if (tmp->from == source_p->from && !MyConnect(tmp))
188     continue;
189 michael 4211
190 michael 4816 target_p = node->data;
191 michael 4211 break;
192 adx 30 }
193     }
194 michael 6061
195     if (!target_p)
196     {
197     DLINK_FOREACH(node, global_client_list.head)
198     {
199     struct Client *tmp = node->data;
200    
201     assert(IsMe(tmp) || IsServer(tmp) || IsClient(tmp));
202     if (!match(parv[server], tmp->name))
203     {
204     if (tmp->from == source_p->from && !MyConnect(tmp))
205     continue;
206    
207     target_p = node->data;
208     break;
209     }
210     }
211     }
212 adx 30 }
213    
214 michael 3246 if (target_p)
215 adx 30 {
216 michael 4207 assert(IsMe(target_p) || IsServer(target_p) || IsClient(target_p));
217 adx 30 if (IsMe(target_p) || MyClient(target_p))
218     return HUNTED_ISME;
219    
220 michael 4207 parv[server] = target_p->id;
221     sendto_one(target_p, command, source_p->id,
222 adx 30 parv[1], parv[2], parv[3], parv[4],
223     parv[5], parv[6], parv[7], parv[8]);
224 michael 2134 return HUNTED_PASS;
225 michael 2345 }
226 adx 30
227 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
228 michael 2134 return HUNTED_NOSUCH;
229 adx 30 }
230    
231     /* try_connections()
232     *
233     * inputs - void pointer which is not used
234     * output - NONE
235     * side effects -
236     * scan through configuration and try new connections.
237     * Returns the calendar time when the next call to this
238     * function should be made latest. (No harm done if this
239     * is called earlier or later...)
240     */
241     void
242     try_connections(void *unused)
243     {
244 michael 4816 dlink_node *node = NULL;
245 michael 3335 int confrq = 0;
246 adx 30
247     /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
248     if (GlobalSetOptions.autoconn == 0)
249     return;
250    
251 michael 4816 DLINK_FOREACH(node, server_items.head)
252 adx 30 {
253 michael 4816 struct MaskItem *conf = node->data;
254 adx 30
255 michael 1636 assert(conf->type == CONF_SERVER);
256 michael 1632
257 michael 2345 /* Also when already connecting! (update holdtimes) --SRB
258 adx 30 */
259 michael 4197 if (!conf->port || !IsConfAllowAutoConn(conf))
260 adx 30 continue;
261    
262    
263     /* Skip this entry if the use of it is still on hold until
264     * future. Otherwise handle this entry (and set it on hold
265     * until next time). Will reset only hold times, if already
266     * made one successfull connection... [this algorithm is
267     * a bit fuzzy... -- msa >;) ]
268     */
269 michael 1649 if (conf->until > CurrentTime)
270 adx 30 continue;
271    
272 michael 4029 assert(conf->class);
273 adx 30
274 michael 4029 confrq = conf->class->con_freq;
275     if (confrq < MIN_CONN_FREQ)
276     confrq = MIN_CONN_FREQ;
277    
278 michael 1649 conf->until = CurrentTime + confrq;
279 adx 30
280 michael 3246 /*
281     * Found a CONNECT config with port specified, scan clients
282 adx 30 * and see if this server is already connected?
283     */
284 michael 3246 if (hash_find_server(conf->name))
285 adx 30 continue;
286    
287 michael 1632 if (conf->class->ref_count < conf->class->max_total)
288 adx 30 {
289     /* Go to the end of the list, if not already last */
290 michael 4816 if (node->next)
291 adx 30 {
292 michael 4816 dlinkDelete(node, &server_items);
293 adx 30 dlinkAddTail(conf, &conf->node, &server_items);
294     }
295    
296     if (find_servconn_in_progress(conf->name))
297     return;
298    
299 michael 3246 /*
300     * We used to only print this if serv_connect() actually
301 adx 30 * succeeded, but since comm_tcp_connect() can call the callback
302     * immediately if there is an error, we were getting error messages
303     * in the wrong order. SO, we just print out the activated line,
304     * and let serv_connect() / serv_connect_callback() print an
305     * error afterwards if it fails.
306     * -- adrian
307     */
308     if (ConfigServerHide.hide_server_ips)
309 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
310     "Connection to %s activated.",
311 adx 30 conf->name);
312     else
313 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
314     "Connection to %s[%s] activated.",
315 michael 1632 conf->name, conf->host);
316 adx 30
317 michael 1632 serv_connect(conf, NULL);
318 adx 30 /* We connect only one at time... */
319     return;
320     }
321     }
322     }
323    
324     int
325 michael 1115 valid_servname(const char *name)
326     {
327 michael 3452 unsigned int dots = 0;
328 michael 1115 const char *p = name;
329    
330     for (; *p; ++p)
331     {
332     if (!IsServChar(*p))
333 michael 1118 return 0;
334 michael 1115
335     if (*p == '.')
336     ++dots;
337     }
338    
339 michael 3452 return dots && (p - name) <= HOSTLEN;
340 michael 1115 }
341    
342     int
343 michael 1302 check_server(const char *name, struct Client *client_p)
344 adx 30 {
345 michael 4816 dlink_node *node = NULL;
346 michael 1632 struct MaskItem *conf = NULL;
347     struct MaskItem *server_conf = NULL;
348 adx 30 int error = -1;
349    
350 michael 3246 assert(client_p);
351 adx 30
352     /* loop through looking for all possible connect items that might work */
353 michael 4816 DLINK_FOREACH(node, server_items.head)
354 adx 30 {
355 michael 4816 conf = node->data;
356 adx 30
357 michael 1652 if (match(name, conf->name))
358 adx 30 continue;
359    
360     error = -3;
361    
362     /* XXX: Fix me for IPv6 */
363     /* XXX sockhost is the IPv4 ip as a string */
364 michael 2345 if (!match(conf->host, client_p->host) ||
365 michael 1652 !match(conf->host, client_p->sockhost))
366 adx 30 {
367     error = -2;
368    
369 michael 4589 if (!match_conf_password(client_p->connection->password, conf))
370 michael 1414 return -2;
371 adx 30
372 michael 2228 if (!EmptyString(conf->certfp))
373 michael 2229 if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
374 michael 2228 return -4;
375    
376 michael 1414 server_conf = conf;
377 adx 30 }
378     }
379    
380     if (server_conf == NULL)
381 michael 2182 return error;
382 adx 30
383     attach_conf(client_p, server_conf);
384    
385    
386 michael 3246 if (server_conf)
387 adx 30 {
388     struct sockaddr_in *v4;
389     struct sockaddr_in6 *v6;
390 michael 4414
391 michael 1632 switch (server_conf->aftype)
392 adx 30 {
393 michael 2345 case AF_INET6:
394 michael 1632 v6 = (struct sockaddr_in6 *)&server_conf->addr;
395 adx 30
396     if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
397 michael 4589 memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr));
398 adx 30 break;
399     case AF_INET:
400 michael 1632 v4 = (struct sockaddr_in *)&server_conf->addr;
401 adx 30
402     if (v4->sin_addr.s_addr == INADDR_NONE)
403 michael 4589 memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr));
404 adx 30 break;
405     }
406     }
407    
408 michael 2182 return 0;
409 adx 30 }
410    
411     /* add_capability()
412     *
413     * inputs - string name of CAPAB
414     * - int flag of capability
415     * output - NONE
416     * side effects - Adds given capability name and bit mask to
417     * current supported capabilities. This allows
418     * modules to dynamically add or subtract their capability.
419     */
420     void
421 michael 5795 add_capability(const char *name, unsigned int flag)
422 adx 30 {
423 michael 3505 struct Capability *cap = MyCalloc(sizeof(*cap));
424 adx 30
425 michael 5795 cap->name = xstrdup(name);
426     cap->cap = flag;
427     dlinkAdd(cap, &cap->node, &server_capabilities_list);
428 adx 30 }
429    
430     /* delete_capability()
431     *
432     * inputs - string name of CAPAB
433     * output - NONE
434     * side effects - delete given capability from ones known.
435     */
436 michael 5775 void
437 michael 5795 delete_capability(const char *name)
438 adx 30 {
439 michael 4816 dlink_node *node = NULL, *node_next = NULL;
440 adx 30
441 michael 5795 DLINK_FOREACH_SAFE(node, node_next, server_capabilities_list.head)
442 adx 30 {
443 michael 4816 struct Capability *cap = node->data;
444 adx 30
445 michael 5795 if (!irccmp(cap->name, name))
446 adx 30 {
447 michael 5795 dlinkDelete(node, &server_capabilities_list);
448     MyFree(cap->name);
449     MyFree(cap);
450 adx 30 }
451     }
452     }
453    
454     /*
455     * find_capability()
456     *
457     * inputs - string name of capab to find
458     * output - 0 if not found CAPAB otherwise
459     * side effects - none
460     */
461 michael 1877 unsigned int
462 michael 5795 find_capability(const char *name)
463 adx 30 {
464 michael 4816 const dlink_node *node = NULL;
465 adx 30
466 michael 5795 DLINK_FOREACH(node, server_capabilities_list.head)
467 adx 30 {
468 michael 4816 const struct Capability *cap = node->data;
469 adx 30
470 michael 5795 if (!irccmp(cap->name, name))
471 michael 896 return cap->cap;
472 adx 30 }
473 michael 896
474     return 0;
475 adx 30 }
476    
477     /* send_capabilities()
478     *
479     * inputs - Client pointer to send to
480     * - int flag of capabilities that this server can send
481     * output - NONE
482     * side effects - send the CAPAB line to a server -orabidoo
483     *
484     */
485     void
486 michael 5795 send_capabilities(struct Client *client_p)
487 adx 30 {
488 michael 3747 char buf[IRCD_BUFSIZE] = "";
489 michael 4816 const dlink_node *node = NULL;
490 adx 30
491 michael 5795 DLINK_FOREACH(node, server_capabilities_list.head)
492 adx 30 {
493 michael 4816 const struct Capability *cap = node->data;
494 adx 30
495 michael 5795 strlcat(buf, cap->name, sizeof(buf));
496    
497     if (node->next)
498     strlcat(buf, " ", sizeof(buf));
499 adx 30 }
500    
501 michael 3747 sendto_one(client_p, "CAPAB :%s", buf);
502 adx 30 }
503    
504     /*
505     * show_capabilities - show current server capabilities
506     *
507     * inputs - pointer to a struct Client
508     * output - pointer to static string
509     * side effects - build up string representing capabilities of server listed
510     */
511     const char *
512 michael 2282 show_capabilities(const struct Client *target_p)
513 adx 30 {
514 michael 2309 static char msgbuf[IRCD_BUFSIZE] = "";
515 michael 4816 const dlink_node *node = NULL;
516 adx 30
517 michael 2309 strlcpy(msgbuf, "TS", sizeof(msgbuf));
518    
519 michael 5795 DLINK_FOREACH(node, server_capabilities_list.head)
520 adx 30 {
521 michael 4816 const struct Capability *cap = node->data;
522 adx 30
523 michael 2282 if (!IsCapable(target_p, cap->cap))
524     continue;
525    
526     strlcat(msgbuf, " ", sizeof(msgbuf));
527     strlcat(msgbuf, cap->name, sizeof(msgbuf));
528 adx 30 }
529 michael 1302
530     return msgbuf;
531 adx 30 }
532    
533     /* make_server()
534     *
535     * inputs - pointer to client struct
536     * output - pointer to struct Server
537     * side effects - add's an Server information block to a client
538     * if it was not previously allocated.
539     */
540     struct Server *
541     make_server(struct Client *client_p)
542     {
543     if (client_p->serv == NULL)
544 michael 3505 client_p->serv = MyCalloc(sizeof(struct Server));
545 adx 30
546     return client_p->serv;
547     }
548    
549     /* New server connection code
550     * Based upon the stuff floating about in s_bsd.c
551     * -- adrian
552     */
553    
554     /* serv_connect() - initiate a server connection
555     *
556 michael 2345 * inputs - pointer to conf
557 adx 30 * - pointer to client doing the connect
558     * output -
559     * side effects -
560     *
561     * This code initiates a connection to a server. It first checks to make
562     * sure the given server exists. If this is the case, it creates a socket,
563     * creates a client, saves the socket information in the client, and
564     * initiates a connection to the server through comm_connect_tcp(). The
565     * completion of this goes through serv_completed_connection().
566     *
567     * We return 1 if the connection is attempted, since we don't know whether
568     * it suceeded or not, and 0 if it fails in here somewhere.
569     */
570     int
571 michael 1632 serv_connect(struct MaskItem *conf, struct Client *by)
572 adx 30 {
573 michael 3246 struct Client *client_p = NULL;
574     char buf[HOSTIPLEN + 1] = "";
575 adx 30
576     /* conversion structs */
577     struct sockaddr_in *v4;
578 michael 3246
579 michael 1632 /* Make sure conf is useful */
580 michael 3246 assert(conf);
581 adx 30
582 michael 5050 getnameinfo((const struct sockaddr *)&conf->addr, conf->addr.ss_len,
583 michael 1123 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
584 michael 1632 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
585 adx 30 buf);
586    
587     /* Still processing a DNS lookup? -> exit */
588 michael 1632 if (conf->dns_pending)
589 adx 30 {
590 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
591 michael 992 "Error connecting to %s: DNS lookup for connect{} in progress.",
592     conf->name);
593 michael 3246 return 0;
594 adx 30 }
595    
596 michael 1632 if (conf->dns_failed)
597 michael 992 {
598 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
599 michael 992 "Error connecting to %s: DNS lookup for connect{} failed.",
600     conf->name);
601 michael 3246 return 0;
602 michael 992 }
603    
604 adx 30 /* Make sure this server isn't already connected
605 michael 1632 * Note: conf should ALWAYS be a valid C: line
606 adx 30 */
607 michael 3246 if ((client_p = hash_find_server(conf->name)))
608 michael 2345 {
609 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
610 michael 2134 "Server %s already present from %s",
611     conf->name, get_client_name(client_p, SHOW_IP));
612 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
613 michael 2134 "Server %s already present from %s",
614     conf->name, get_client_name(client_p, MASK_IP));
615 adx 30 if (by && IsClient(by) && !MyClient(by))
616 michael 3110 sendto_one_notice(by, &me, ":Server %s already present from %s",
617     conf->name, get_client_name(client_p, MASK_IP));
618 michael 2134 return 0;
619 adx 30 }
620 michael 2345
621 adx 30 /* Create a local client */
622     client_p = make_client(NULL);
623    
624     /* Copy in the server, hostname, fd */
625     strlcpy(client_p->name, conf->name, sizeof(client_p->name));
626 michael 1632 strlcpy(client_p->host, conf->host, sizeof(client_p->host));
627 adx 30
628     /* We already converted the ip once, so lets use it - stu */
629 michael 1115 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
630 adx 30
631 michael 2345 /* create a socket for the server connection */
632 michael 4589 if (comm_open(&client_p->connection->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0)
633 adx 30 {
634     /* Eek, failure to create the socket */
635 michael 3335 report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
636    
637 adx 30 SetDead(client_p);
638 michael 3171 exit_client(client_p, "Connection failed");
639 michael 2134 return 0;
640 adx 30 }
641    
642     /* servernames are always guaranteed under HOSTLEN chars */
643 michael 4589 fd_note(&client_p->connection->fd, "Server: %s", conf->name);
644 adx 30
645     /* Attach config entries to client here rather than in
646     * serv_connect_callback(). This to avoid null pointer references.
647     */
648 michael 1632 if (!attach_connect_block(client_p, conf->name, conf->host))
649 adx 30 {
650 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
651 michael 4676 "Host %s is not enabled for connecting: no connect {} block",
652 michael 2134 conf->name);
653 michael 2345 if (by && IsClient(by) && !MyClient(by))
654 michael 4676 sendto_one_notice(by, &me, ":Connect to host %s failed: no connect {} block", client_p->name);
655 michael 3110
656 adx 30 SetDead(client_p);
657 michael 3171 exit_client(client_p, "Connection failed");
658 michael 2134 return 0;
659 adx 30 }
660    
661     /* at this point we have a connection in progress and C/N lines
662     * attached to the client, the socket info should be saved in the
663     * client and it should either be resolved or have a valid address.
664     *
665     * The socket has been connected or connect is in progress.
666     */
667     make_server(client_p);
668    
669     if (by && IsClient(by))
670     strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
671     else
672     strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
673    
674     SetConnecting(client_p);
675 michael 4589 client_p->connection->aftype = conf->aftype;
676 adx 30
677     /* Now, initiate the connection */
678 michael 2345 /* XXX assume that a non 0 type means a specific bind address
679 adx 30 * for this connect.
680     */
681 michael 1632 switch (conf->aftype)
682 adx 30 {
683     case AF_INET:
684 michael 1632 v4 = (struct sockaddr_in*)&conf->bind;
685 michael 3246 if (v4->sin_addr.s_addr)
686 adx 30 {
687     struct irc_ssaddr ipn;
688     memset(&ipn, 0, sizeof(struct irc_ssaddr));
689     ipn.ss.ss_family = AF_INET;
690     ipn.ss_port = 0;
691 michael 1632 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
692 michael 4589 comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port,
693 michael 2345 (struct sockaddr *)&ipn, ipn.ss_len,
694 michael 2134 serv_connect_callback, client_p, conf->aftype,
695     CONNECTTIMEOUT);
696 adx 30 }
697 michael 4341 else if (ConfigServerInfo.specific_ipv4_vhost)
698 adx 30 {
699     struct irc_ssaddr ipn;
700     memset(&ipn, 0, sizeof(struct irc_ssaddr));
701     ipn.ss.ss_family = AF_INET;
702     ipn.ss_port = 0;
703 michael 4341 memcpy(&ipn, &ConfigServerInfo.ip, sizeof(struct irc_ssaddr));
704 michael 4589 comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port,
705 adx 30 (struct sockaddr *)&ipn, ipn.ss_len,
706 michael 1632 serv_connect_callback, client_p, conf->aftype,
707 michael 2134 CONNECTTIMEOUT);
708 adx 30 }
709     else
710 michael 4589 comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port,
711 michael 2345 NULL, 0, serv_connect_callback, client_p, conf->aftype,
712 adx 30 CONNECTTIMEOUT);
713     break;
714     case AF_INET6:
715     {
716 michael 2182 struct irc_ssaddr ipn;
717     struct sockaddr_in6 *v6;
718     struct sockaddr_in6 *v6conf;
719 adx 30
720 michael 2182 memset(&ipn, 0, sizeof(struct irc_ssaddr));
721     v6conf = (struct sockaddr_in6 *)&conf->bind;
722     v6 = (struct sockaddr_in6 *)&ipn;
723 adx 30
724 michael 3246 if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
725 adx 30 {
726 michael 2182 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
727     ipn.ss.ss_family = AF_INET6;
728     ipn.ss_port = 0;
729 michael 4589 comm_connect_tcp(&client_p->connection->fd,
730 michael 2182 conf->host, conf->port,
731 michael 2345 (struct sockaddr *)&ipn, ipn.ss_len,
732 michael 2182 serv_connect_callback, client_p,
733     conf->aftype, CONNECTTIMEOUT);
734     }
735 michael 4341 else if (ConfigServerInfo.specific_ipv6_vhost)
736 michael 2182 {
737 michael 4341 memcpy(&ipn, &ConfigServerInfo.ip6, sizeof(struct irc_ssaddr));
738 michael 2182 ipn.ss.ss_family = AF_INET6;
739     ipn.ss_port = 0;
740 michael 4589 comm_connect_tcp(&client_p->connection->fd,
741 michael 2182 conf->host, conf->port,
742     (struct sockaddr *)&ipn, ipn.ss_len,
743     serv_connect_callback, client_p,
744     conf->aftype, CONNECTTIMEOUT);
745     }
746     else
747 michael 4589 comm_connect_tcp(&client_p->connection->fd,
748 michael 2345 conf->host, conf->port,
749 michael 2182 NULL, 0, serv_connect_callback, client_p,
750     conf->aftype, CONNECTTIMEOUT);
751 adx 30 }
752     }
753 michael 4414
754 michael 2182 return 1;
755 adx 30 }
756    
757 michael 1303 #ifdef HAVE_LIBCRYPTO
758     static void
759     finish_ssl_server_handshake(struct Client *client_p)
760     {
761 michael 1632 struct MaskItem *conf = NULL;
762 michael 1303
763 michael 4589 conf = find_conf_name(&client_p->connection->confs,
764 michael 1632 client_p->name, CONF_SERVER);
765 michael 1303 if (conf == NULL)
766     {
767 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
768 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
769 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
770 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
771    
772 michael 3171 exit_client(client_p, "Lost connect{} block");
773 michael 1303 return;
774     }
775    
776 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
777 michael 1303
778 michael 5795 send_capabilities(client_p);
779 michael 1303
780     sendto_one(client_p, "SERVER %s 1 :%s%s",
781     me.name, ConfigServerHide.hidden ? "(H) " : "",
782     me.info);
783    
784     /* If we've been marked dead because a send failed, just exit
785     * here now and save everyone the trouble of us ever existing.
786     */
787     if (IsDead(client_p))
788     {
789 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
790 michael 1303 "%s[%s] went dead during handshake",
791     client_p->name,
792     client_p->host);
793 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
794 michael 1303 "%s went dead during handshake", client_p->name);
795     return;
796     }
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 4589 comm_setselect(&client_p->connection->fd, COMM_SELECT_READ, read_packet, client_p, 0);
801 michael 1303 }
802    
803     static void
804 michael 4462 ssl_server_handshake(fde_t *fd, void *data)
805 michael 1303 {
806 michael 4462 struct Client *client_p = data;
807 michael 2463 X509 *cert = NULL;
808     int ret = 0;
809 michael 1303
810 michael 4589 if ((ret = SSL_connect(client_p->connection->fd.ssl)) <= 0)
811 michael 1303 {
812 michael 4738 if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT)
813 michael 4731 {
814     exit_client(client_p, "Timeout during SSL handshake");
815     return;
816     }
817    
818 michael 4589 switch (SSL_get_error(client_p->connection->fd.ssl, ret))
819 michael 1303 {
820     case SSL_ERROR_WANT_WRITE:
821 michael 4589 comm_setselect(&client_p->connection->fd, COMM_SELECT_WRITE,
822 michael 4738 ssl_server_handshake, client_p, CONNECTTIMEOUT);
823 michael 1303 return;
824     case SSL_ERROR_WANT_READ:
825 michael 4589 comm_setselect(&client_p->connection->fd, COMM_SELECT_READ,
826 michael 4738 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 1618 sendto_realops_flags(UMODE_ALL, 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 4731 comm_settimeout(&client_p->connection->fd, 0, NULL, NULL);
841    
842 michael 4589 if ((cert = SSL_get_peer_certificate(client_p->connection->fd.ssl)))
843 michael 2463 {
844 michael 4589 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 4341 if (X509_digest(cert, ConfigServerInfo.message_digest_algorithm, md, &n))
855 michael 2463 {
856 michael 4112 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 1632 ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd)
871 michael 1303 {
872 michael 4589 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 4589 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     struct Client *client_p = data;
902 michael 1632 struct MaskItem *conf = NULL;
903 adx 30
904 michael 4299 /* First, make sure it's a real client! */
905 michael 3246 assert(client_p);
906 michael 4589 assert(&client_p->connection->fd == fd);
907 adx 30
908     /* Next, for backward purposes, record the ip of the server */
909 michael 4589 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     /* We have an error, so report it and quit
916     * Admins get to see any IP, mere opers don't *sigh*
917     */
918     if (ConfigServerHide.hide_server_ips)
919 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
920 adx 30 "Error connecting to %s: %s",
921     client_p->name, comm_errstr(status));
922     else
923 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
924 michael 2134 "Error connecting to %s[%s]: %s", client_p->name,
925     client_p->host, comm_errstr(status));
926 adx 30
927 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
928 michael 2134 "Error connecting to %s: %s",
929     client_p->name, comm_errstr(status));
930 adx 30
931     /* If a fd goes bad, call dead_link() the socket is no
932     * longer valid for reading or writing.
933     */
934     dead_link_on_write(client_p, 0);
935     return;
936     }
937    
938     /* COMM_OK, so continue the connection procedure */
939     /* Get the C/N lines */
940 michael 4589 conf = find_conf_name(&client_p->connection->confs,
941 michael 2134 client_p->name, CONF_SERVER);
942 adx 30 if (conf == NULL)
943     {
944 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
945 michael 2134 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
946 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
947 michael 2134 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
948 adx 30
949 michael 3171 exit_client(client_p, "Lost connect{} block");
950 adx 30 return;
951     }
952    
953     /* Next, send the initial handshake */
954     SetHandshake(client_p);
955    
956     #ifdef HAVE_LIBCRYPTO
957 michael 1632 if (IsConfSSL(conf))
958 michael 1303 {
959 michael 1632 ssl_connect_init(client_p, conf, fd);
960 michael 1303 return;
961     }
962 adx 30 #endif
963    
964 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
965 adx 30
966 michael 5795 send_capabilities(client_p);
967 adx 30
968 michael 2134 sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
969     ConfigServerHide.hidden ? "(H) " : "", me.info);
970 adx 30
971     /* If we've been marked dead because a send failed, just exit
972     * here now and save everyone the trouble of us ever existing.
973     */
974 michael 2345 if (IsDead(client_p))
975 adx 30 {
976 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
977 michael 2134 "%s[%s] went dead during handshake",
978 adx 30 client_p->name,
979 michael 2134 client_p->host);
980 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
981 michael 2134 "%s went dead during handshake", client_p->name);
982 adx 30 return;
983     }
984    
985     /* don't move to serv_list yet -- we haven't sent a burst! */
986     /* If we get here, we're ok, so lets start reading some data */
987     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
988     }
989    
990     struct Client *
991     find_servconn_in_progress(const char *name)
992     {
993     dlink_node *ptr;
994     struct Client *cptr;
995    
996     DLINK_FOREACH(ptr, unknown_list.head)
997     {
998     cptr = ptr->data;
999    
1000     if (cptr && cptr->name[0])
1001 michael 1652 if (!match(name, cptr->name))
1002 adx 30 return cptr;
1003     }
1004 michael 2345
1005 adx 30 return NULL;
1006     }
1007 michael 6157
1008     void
1009     generate_sid(void)
1010     {
1011     unsigned int sid = 0;
1012     const char *p;
1013     char sidbuf[IRC_MAXSID + 1];
1014    
1015     for (p = me.name; *p; ++p)
1016     sid = 5 * sid + *p;
1017     for (p = me.info; *p; ++p)
1018     sid = 5 * sid + *p;
1019    
1020     snprintf(sidbuf, sizeof(sidbuf), "%03d", sid % 1000);
1021     strlcpy(me.id, sidbuf, sizeof(me.id));
1022     }

Properties

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