1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file s_serv.c |
23 |
* \brief Server related functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#ifdef HAVE_LIBCRYPTO |
29 |
#include <openssl/rsa.h> |
30 |
#include "rsa.h" |
31 |
#endif |
32 |
#include "list.h" |
33 |
#include "client.h" |
34 |
#include "event.h" |
35 |
#include "hash.h" |
36 |
#include "irc_string.h" |
37 |
#include "ircd.h" |
38 |
#include "ircd_defs.h" |
39 |
#include "s_bsd.h" |
40 |
#include "numeric.h" |
41 |
#include "packet.h" |
42 |
#include "conf.h" |
43 |
#include "server.h" |
44 |
#include "log.h" |
45 |
#include "user.h" |
46 |
#include "send.h" |
47 |
#include "memory.h" |
48 |
#include "channel.h" |
49 |
#include "parse.h" |
50 |
|
51 |
#define MIN_CONN_FREQ 300 |
52 |
|
53 |
dlink_list flatten_links; |
54 |
static dlink_list server_capabilities_list; |
55 |
static void serv_connect_callback(fde_t *, int, void *); |
56 |
|
57 |
|
58 |
/* |
59 |
* write_links_file |
60 |
* |
61 |
* inputs - void pointer which is not used |
62 |
* output - NONE |
63 |
* side effects - called from an event, write out list of linked servers |
64 |
* but in no particular order. |
65 |
*/ |
66 |
void |
67 |
write_links_file(void *unused) |
68 |
{ |
69 |
FILE *file = NULL; |
70 |
dlink_node *node = NULL, *node_next = NULL; |
71 |
char buff[IRCD_BUFSIZE] = ""; |
72 |
|
73 |
if ((file = fopen(LIPATH, "w")) == NULL) |
74 |
return; |
75 |
|
76 |
DLINK_FOREACH_SAFE(node, node_next, flatten_links.head) |
77 |
{ |
78 |
dlinkDelete(node, &flatten_links); |
79 |
MyFree(node->data); |
80 |
free_dlink_node(node); |
81 |
} |
82 |
|
83 |
DLINK_FOREACH(node, global_server_list.head) |
84 |
{ |
85 |
const struct Client *target_p = node->data; |
86 |
|
87 |
/* |
88 |
* Skip hidden servers, aswell as ourselves, since we already send |
89 |
* ourselves in /links |
90 |
*/ |
91 |
if (IsHidden(target_p) || IsMe(target_p)) |
92 |
continue; |
93 |
|
94 |
if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services) |
95 |
continue; |
96 |
|
97 |
/* |
98 |
* Attempt to format the file in such a way it follows the usual links output |
99 |
* ie "servername uplink :hops info" |
100 |
* Mostly for aesthetic reasons - makes it look pretty in mIRC ;) |
101 |
* - madmax |
102 |
*/ |
103 |
snprintf(buff, sizeof(buff), "%s %s :1 %s", target_p->name, |
104 |
me.name, target_p->info); |
105 |
dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links); |
106 |
snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name, |
107 |
me.name, target_p->info); |
108 |
|
109 |
fputs(buff, file); |
110 |
} |
111 |
|
112 |
fclose(file); |
113 |
} |
114 |
|
115 |
void |
116 |
read_links_file(void) |
117 |
{ |
118 |
FILE *file = NULL; |
119 |
char *p = NULL; |
120 |
char buff[IRCD_BUFSIZE] = ""; |
121 |
|
122 |
if ((file = fopen(LIPATH, "r")) == NULL) |
123 |
return; |
124 |
|
125 |
while (fgets(buff, sizeof(buff), file)) |
126 |
{ |
127 |
if ((p = strchr(buff, '\n'))) |
128 |
*p = '\0'; |
129 |
|
130 |
dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links); |
131 |
} |
132 |
|
133 |
fclose(file); |
134 |
} |
135 |
|
136 |
/* hunt_server() |
137 |
* Do the basic thing in delivering the message (command) |
138 |
* across the relays to the specific server (server) for |
139 |
* actions. |
140 |
* |
141 |
* Note: The command is a format string and *MUST* be |
142 |
* of prefixed style (e.g. ":%s COMMAND %s ..."). |
143 |
* Command can have only max 8 parameters. |
144 |
* |
145 |
* server parv[server] is the parameter identifying the |
146 |
* target server. |
147 |
* |
148 |
* *WARNING* |
149 |
* parv[server] is replaced with the pointer to the |
150 |
* real servername from the matched client (I'm lazy |
151 |
* now --msa). |
152 |
* |
153 |
* returns: (see #defines) |
154 |
*/ |
155 |
int |
156 |
hunt_server(struct Client *source_p, const char *command, |
157 |
const int server, const int parc, char *parv[]) |
158 |
{ |
159 |
struct Client *target_p = NULL; |
160 |
dlink_node *node = NULL; |
161 |
|
162 |
/* Assume it's me, if no server */ |
163 |
if (parc <= server || EmptyString(parv[server])) |
164 |
return HUNTED_ISME; |
165 |
|
166 |
if ((target_p = find_person(source_p, parv[server])) == NULL) |
167 |
target_p = hash_find_server(parv[server]); |
168 |
|
169 |
/* |
170 |
* These are to pickup matches that would cause the following |
171 |
* message to go in the wrong direction while doing quick fast |
172 |
* non-matching lookups. |
173 |
*/ |
174 |
if (target_p) |
175 |
if (target_p->from == source_p->from && !MyConnect(target_p)) |
176 |
target_p = NULL; |
177 |
|
178 |
if (!target_p && has_wildcards(parv[server])) |
179 |
{ |
180 |
DLINK_FOREACH(node, global_server_list.head) |
181 |
{ |
182 |
struct Client *tmp = node->data; |
183 |
|
184 |
assert(IsMe(tmp) || IsServer(tmp)); |
185 |
if (!match(parv[server], tmp->name)) |
186 |
{ |
187 |
if (tmp->from == source_p->from && !MyConnect(tmp)) |
188 |
continue; |
189 |
|
190 |
target_p = node->data; |
191 |
break; |
192 |
} |
193 |
} |
194 |
|
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 |
} |
213 |
|
214 |
if (target_p) |
215 |
{ |
216 |
assert(IsMe(target_p) || IsServer(target_p) || IsClient(target_p)); |
217 |
if (IsMe(target_p) || MyClient(target_p)) |
218 |
return HUNTED_ISME; |
219 |
|
220 |
parv[server] = target_p->id; |
221 |
sendto_one(target_p, command, source_p->id, |
222 |
parv[1], parv[2], parv[3], parv[4], |
223 |
parv[5], parv[6], parv[7], parv[8]); |
224 |
return HUNTED_PASS; |
225 |
} |
226 |
|
227 |
sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]); |
228 |
return HUNTED_NOSUCH; |
229 |
} |
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 |
dlink_node *node = NULL; |
245 |
int confrq = 0; |
246 |
|
247 |
/* TODO: change this to set active flag to 0 when added to event! --Habeeb */ |
248 |
if (GlobalSetOptions.autoconn == 0) |
249 |
return; |
250 |
|
251 |
DLINK_FOREACH(node, server_items.head) |
252 |
{ |
253 |
struct MaskItem *conf = node->data; |
254 |
|
255 |
assert(conf->type == CONF_SERVER); |
256 |
|
257 |
/* Also when already connecting! (update holdtimes) --SRB |
258 |
*/ |
259 |
if (!conf->port || !IsConfAllowAutoConn(conf)) |
260 |
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 |
if (conf->until > CurrentTime) |
270 |
continue; |
271 |
|
272 |
assert(conf->class); |
273 |
|
274 |
confrq = conf->class->con_freq; |
275 |
if (confrq < MIN_CONN_FREQ) |
276 |
confrq = MIN_CONN_FREQ; |
277 |
|
278 |
conf->until = CurrentTime + confrq; |
279 |
|
280 |
/* |
281 |
* Found a CONNECT config with port specified, scan clients |
282 |
* and see if this server is already connected? |
283 |
*/ |
284 |
if (hash_find_server(conf->name)) |
285 |
continue; |
286 |
|
287 |
if (conf->class->ref_count < conf->class->max_total) |
288 |
{ |
289 |
/* Go to the end of the list, if not already last */ |
290 |
if (node->next) |
291 |
{ |
292 |
dlinkDelete(node, &server_items); |
293 |
dlinkAddTail(conf, &conf->node, &server_items); |
294 |
} |
295 |
|
296 |
if (find_servconn_in_progress(conf->name)) |
297 |
return; |
298 |
|
299 |
/* |
300 |
* We used to only print this if serv_connect() actually |
301 |
* 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 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
310 |
"Connection to %s activated.", |
311 |
conf->name); |
312 |
else |
313 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
314 |
"Connection to %s[%s] activated.", |
315 |
conf->name, conf->host); |
316 |
|
317 |
serv_connect(conf, NULL); |
318 |
/* We connect only one at time... */ |
319 |
return; |
320 |
} |
321 |
} |
322 |
} |
323 |
|
324 |
int |
325 |
valid_servname(const char *name) |
326 |
{ |
327 |
unsigned int dots = 0; |
328 |
const char *p = name; |
329 |
|
330 |
for (; *p; ++p) |
331 |
{ |
332 |
if (!IsServChar(*p)) |
333 |
return 0; |
334 |
|
335 |
if (*p == '.') |
336 |
++dots; |
337 |
} |
338 |
|
339 |
return dots && (p - name) <= HOSTLEN; |
340 |
} |
341 |
|
342 |
int |
343 |
check_server(const char *name, struct Client *client_p) |
344 |
{ |
345 |
dlink_node *node = NULL; |
346 |
struct MaskItem *conf = NULL; |
347 |
struct MaskItem *server_conf = NULL; |
348 |
int error = -1; |
349 |
|
350 |
assert(client_p); |
351 |
|
352 |
/* loop through looking for all possible connect items that might work */ |
353 |
DLINK_FOREACH(node, server_items.head) |
354 |
{ |
355 |
conf = node->data; |
356 |
|
357 |
if (match(name, conf->name)) |
358 |
continue; |
359 |
|
360 |
error = -3; |
361 |
|
362 |
/* XXX: Fix me for IPv6 */ |
363 |
/* XXX sockhost is the IPv4 ip as a string */ |
364 |
if (!match(conf->host, client_p->host) || |
365 |
!match(conf->host, client_p->sockhost)) |
366 |
{ |
367 |
error = -2; |
368 |
|
369 |
if (!match_conf_password(client_p->connection->password, conf)) |
370 |
return -2; |
371 |
|
372 |
if (!EmptyString(conf->certfp)) |
373 |
if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp)) |
374 |
return -4; |
375 |
|
376 |
server_conf = conf; |
377 |
} |
378 |
} |
379 |
|
380 |
if (server_conf == NULL) |
381 |
return error; |
382 |
|
383 |
attach_conf(client_p, server_conf); |
384 |
|
385 |
|
386 |
if (server_conf) |
387 |
{ |
388 |
struct sockaddr_in *v4; |
389 |
struct sockaddr_in6 *v6; |
390 |
|
391 |
switch (server_conf->aftype) |
392 |
{ |
393 |
case AF_INET6: |
394 |
v6 = (struct sockaddr_in6 *)&server_conf->addr; |
395 |
|
396 |
if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr)) |
397 |
memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr)); |
398 |
break; |
399 |
case AF_INET: |
400 |
v4 = (struct sockaddr_in *)&server_conf->addr; |
401 |
|
402 |
if (v4->sin_addr.s_addr == INADDR_NONE) |
403 |
memcpy(&server_conf->addr, &client_p->connection->ip, sizeof(struct irc_ssaddr)); |
404 |
break; |
405 |
} |
406 |
} |
407 |
|
408 |
return 0; |
409 |
} |
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 |
add_capability(const char *name, unsigned int flag) |
422 |
{ |
423 |
struct Capability *cap = MyCalloc(sizeof(*cap)); |
424 |
|
425 |
cap->name = xstrdup(name); |
426 |
cap->cap = flag; |
427 |
dlinkAdd(cap, &cap->node, &server_capabilities_list); |
428 |
} |
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 |
void |
437 |
delete_capability(const char *name) |
438 |
{ |
439 |
dlink_node *node = NULL, *node_next = NULL; |
440 |
|
441 |
DLINK_FOREACH_SAFE(node, node_next, server_capabilities_list.head) |
442 |
{ |
443 |
struct Capability *cap = node->data; |
444 |
|
445 |
if (!irccmp(cap->name, name)) |
446 |
{ |
447 |
dlinkDelete(node, &server_capabilities_list); |
448 |
MyFree(cap->name); |
449 |
MyFree(cap); |
450 |
} |
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 |
unsigned int |
462 |
find_capability(const char *name) |
463 |
{ |
464 |
const dlink_node *node = NULL; |
465 |
|
466 |
DLINK_FOREACH(node, server_capabilities_list.head) |
467 |
{ |
468 |
const struct Capability *cap = node->data; |
469 |
|
470 |
if (!irccmp(cap->name, name)) |
471 |
return cap->cap; |
472 |
} |
473 |
|
474 |
return 0; |
475 |
} |
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 |
send_capabilities(struct Client *client_p) |
487 |
{ |
488 |
char buf[IRCD_BUFSIZE] = ""; |
489 |
const dlink_node *node = NULL; |
490 |
|
491 |
DLINK_FOREACH(node, server_capabilities_list.head) |
492 |
{ |
493 |
const struct Capability *cap = node->data; |
494 |
|
495 |
strlcat(buf, cap->name, sizeof(buf)); |
496 |
|
497 |
if (node->next) |
498 |
strlcat(buf, " ", sizeof(buf)); |
499 |
} |
500 |
|
501 |
sendto_one(client_p, "CAPAB :%s", buf); |
502 |
} |
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 |
show_capabilities(const struct Client *target_p) |
513 |
{ |
514 |
static char msgbuf[IRCD_BUFSIZE] = ""; |
515 |
const dlink_node *node = NULL; |
516 |
|
517 |
strlcpy(msgbuf, "TS", sizeof(msgbuf)); |
518 |
|
519 |
DLINK_FOREACH(node, server_capabilities_list.head) |
520 |
{ |
521 |
const struct Capability *cap = node->data; |
522 |
|
523 |
if (!IsCapable(target_p, cap->cap)) |
524 |
continue; |
525 |
|
526 |
strlcat(msgbuf, " ", sizeof(msgbuf)); |
527 |
strlcat(msgbuf, cap->name, sizeof(msgbuf)); |
528 |
} |
529 |
|
530 |
return msgbuf; |
531 |
} |
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 |
client_p->serv = MyCalloc(sizeof(struct Server)); |
545 |
|
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 |
* inputs - pointer to conf |
557 |
* - 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 |
serv_connect(struct MaskItem *conf, struct Client *by) |
572 |
{ |
573 |
struct Client *client_p = NULL; |
574 |
char buf[HOSTIPLEN + 1] = ""; |
575 |
|
576 |
/* conversion structs */ |
577 |
struct sockaddr_in *v4; |
578 |
|
579 |
/* Make sure conf is useful */ |
580 |
assert(conf); |
581 |
|
582 |
getnameinfo((const struct sockaddr *)&conf->addr, conf->addr.ss_len, |
583 |
buf, sizeof(buf), NULL, 0, NI_NUMERICHOST); |
584 |
ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host, |
585 |
buf); |
586 |
|
587 |
/* Still processing a DNS lookup? -> exit */ |
588 |
if (conf->dns_pending) |
589 |
{ |
590 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
591 |
"Error connecting to %s: DNS lookup for connect{} in progress.", |
592 |
conf->name); |
593 |
return 0; |
594 |
} |
595 |
|
596 |
if (conf->dns_failed) |
597 |
{ |
598 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
599 |
"Error connecting to %s: DNS lookup for connect{} failed.", |
600 |
conf->name); |
601 |
return 0; |
602 |
} |
603 |
|
604 |
/* Make sure this server isn't already connected |
605 |
* Note: conf should ALWAYS be a valid C: line |
606 |
*/ |
607 |
if ((client_p = hash_find_server(conf->name))) |
608 |
{ |
609 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
610 |
"Server %s already present from %s", |
611 |
conf->name, get_client_name(client_p, SHOW_IP)); |
612 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
613 |
"Server %s already present from %s", |
614 |
conf->name, get_client_name(client_p, MASK_IP)); |
615 |
if (by && IsClient(by) && !MyClient(by)) |
616 |
sendto_one_notice(by, &me, ":Server %s already present from %s", |
617 |
conf->name, get_client_name(client_p, MASK_IP)); |
618 |
return 0; |
619 |
} |
620 |
|
621 |
/* 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 |
strlcpy(client_p->host, conf->host, sizeof(client_p->host)); |
627 |
|
628 |
/* We already converted the ip once, so lets use it - stu */ |
629 |
strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost)); |
630 |
|
631 |
/* create a socket for the server connection */ |
632 |
if (comm_open(&client_p->connection->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0) |
633 |
{ |
634 |
/* Eek, failure to create the socket */ |
635 |
report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno); |
636 |
|
637 |
SetDead(client_p); |
638 |
exit_client(client_p, "Connection failed"); |
639 |
return 0; |
640 |
} |
641 |
|
642 |
/* servernames are always guaranteed under HOSTLEN chars */ |
643 |
fd_note(&client_p->connection->fd, "Server: %s", conf->name); |
644 |
|
645 |
/* Attach config entries to client here rather than in |
646 |
* serv_connect_callback(). This to avoid null pointer references. |
647 |
*/ |
648 |
if (!attach_connect_block(client_p, conf->name, conf->host)) |
649 |
{ |
650 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
651 |
"Host %s is not enabled for connecting: no connect {} block", |
652 |
conf->name); |
653 |
if (by && IsClient(by) && !MyClient(by)) |
654 |
sendto_one_notice(by, &me, ":Connect to host %s failed: no connect {} block", client_p->name); |
655 |
|
656 |
SetDead(client_p); |
657 |
exit_client(client_p, "Connection failed"); |
658 |
return 0; |
659 |
} |
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 |
client_p->connection->aftype = conf->aftype; |
676 |
|
677 |
/* Now, initiate the connection */ |
678 |
/* XXX assume that a non 0 type means a specific bind address |
679 |
* for this connect. |
680 |
*/ |
681 |
switch (conf->aftype) |
682 |
{ |
683 |
case AF_INET: |
684 |
v4 = (struct sockaddr_in*)&conf->bind; |
685 |
if (v4->sin_addr.s_addr) |
686 |
{ |
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 |
memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr)); |
692 |
comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port, |
693 |
(struct sockaddr *)&ipn, ipn.ss_len, |
694 |
serv_connect_callback, client_p, conf->aftype, |
695 |
CONNECTTIMEOUT); |
696 |
} |
697 |
else if (ConfigServerInfo.specific_ipv4_vhost) |
698 |
{ |
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 |
memcpy(&ipn, &ConfigServerInfo.ip, sizeof(struct irc_ssaddr)); |
704 |
comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port, |
705 |
(struct sockaddr *)&ipn, ipn.ss_len, |
706 |
serv_connect_callback, client_p, conf->aftype, |
707 |
CONNECTTIMEOUT); |
708 |
} |
709 |
else |
710 |
comm_connect_tcp(&client_p->connection->fd, conf->host, conf->port, |
711 |
NULL, 0, serv_connect_callback, client_p, conf->aftype, |
712 |
CONNECTTIMEOUT); |
713 |
break; |
714 |
case AF_INET6: |
715 |
{ |
716 |
struct irc_ssaddr ipn; |
717 |
struct sockaddr_in6 *v6; |
718 |
struct sockaddr_in6 *v6conf; |
719 |
|
720 |
memset(&ipn, 0, sizeof(struct irc_ssaddr)); |
721 |
v6conf = (struct sockaddr_in6 *)&conf->bind; |
722 |
v6 = (struct sockaddr_in6 *)&ipn; |
723 |
|
724 |
if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr))) |
725 |
{ |
726 |
memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr)); |
727 |
ipn.ss.ss_family = AF_INET6; |
728 |
ipn.ss_port = 0; |
729 |
comm_connect_tcp(&client_p->connection->fd, |
730 |
conf->host, conf->port, |
731 |
(struct sockaddr *)&ipn, ipn.ss_len, |
732 |
serv_connect_callback, client_p, |
733 |
conf->aftype, CONNECTTIMEOUT); |
734 |
} |
735 |
else if (ConfigServerInfo.specific_ipv6_vhost) |
736 |
{ |
737 |
memcpy(&ipn, &ConfigServerInfo.ip6, sizeof(struct irc_ssaddr)); |
738 |
ipn.ss.ss_family = AF_INET6; |
739 |
ipn.ss_port = 0; |
740 |
comm_connect_tcp(&client_p->connection->fd, |
741 |
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 |
comm_connect_tcp(&client_p->connection->fd, |
748 |
conf->host, conf->port, |
749 |
NULL, 0, serv_connect_callback, client_p, |
750 |
conf->aftype, CONNECTTIMEOUT); |
751 |
} |
752 |
} |
753 |
|
754 |
return 1; |
755 |
} |
756 |
|
757 |
#ifdef HAVE_LIBCRYPTO |
758 |
static void |
759 |
finish_ssl_server_handshake(struct Client *client_p) |
760 |
{ |
761 |
struct MaskItem *conf = NULL; |
762 |
|
763 |
conf = find_conf_name(&client_p->connection->confs, |
764 |
client_p->name, CONF_SERVER); |
765 |
if (conf == NULL) |
766 |
{ |
767 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
768 |
"Lost connect{} block for %s", get_client_name(client_p, HIDE_IP)); |
769 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
770 |
"Lost connect{} block for %s", get_client_name(client_p, MASK_IP)); |
771 |
|
772 |
exit_client(client_p, "Lost connect{} block"); |
773 |
return; |
774 |
} |
775 |
|
776 |
sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id); |
777 |
|
778 |
send_capabilities(client_p); |
779 |
|
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 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
790 |
"%s[%s] went dead during handshake", |
791 |
client_p->name, |
792 |
client_p->host); |
793 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
794 |
"%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 |
comm_setselect(&client_p->connection->fd, COMM_SELECT_READ, read_packet, client_p, 0); |
801 |
} |
802 |
|
803 |
static void |
804 |
ssl_server_handshake(fde_t *fd, void *data) |
805 |
{ |
806 |
struct Client *client_p = data; |
807 |
X509 *cert = NULL; |
808 |
int ret = 0; |
809 |
|
810 |
if ((ret = SSL_connect(client_p->connection->fd.ssl)) <= 0) |
811 |
{ |
812 |
if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT) |
813 |
{ |
814 |
exit_client(client_p, "Timeout during SSL handshake"); |
815 |
return; |
816 |
} |
817 |
|
818 |
switch (SSL_get_error(client_p->connection->fd.ssl, ret)) |
819 |
{ |
820 |
case SSL_ERROR_WANT_WRITE: |
821 |
comm_setselect(&client_p->connection->fd, COMM_SELECT_WRITE, |
822 |
ssl_server_handshake, client_p, CONNECTTIMEOUT); |
823 |
return; |
824 |
case SSL_ERROR_WANT_READ: |
825 |
comm_setselect(&client_p->connection->fd, COMM_SELECT_READ, |
826 |
ssl_server_handshake, client_p, CONNECTTIMEOUT); |
827 |
return; |
828 |
default: |
829 |
{ |
830 |
const char *sslerr = ERR_error_string(ERR_get_error(), NULL); |
831 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
832 |
"Error connecting to %s: %s", client_p->name, |
833 |
sslerr ? sslerr : "unknown SSL error"); |
834 |
exit_client(client_p, "Error during SSL handshake"); |
835 |
return; |
836 |
} |
837 |
} |
838 |
} |
839 |
|
840 |
comm_settimeout(&client_p->connection->fd, 0, NULL, NULL); |
841 |
|
842 |
if ((cert = SSL_get_peer_certificate(client_p->connection->fd.ssl))) |
843 |
{ |
844 |
int res = SSL_get_verify_result(client_p->connection->fd.ssl); |
845 |
char buf[EVP_MAX_MD_SIZE * 2 + 1] = ""; |
846 |
unsigned char md[EVP_MAX_MD_SIZE] = ""; |
847 |
|
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 |
unsigned int n = 0; |
853 |
|
854 |
if (X509_digest(cert, ConfigServerInfo.message_digest_algorithm, md, &n)) |
855 |
{ |
856 |
binary_to_hex(md, buf, n); |
857 |
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 |
finish_ssl_server_handshake(client_p); |
867 |
} |
868 |
|
869 |
static void |
870 |
ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd) |
871 |
{ |
872 |
if ((client_p->connection->fd.ssl = SSL_new(ConfigServerInfo.client_ctx)) == NULL) |
873 |
{ |
874 |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
875 |
ERR_error_string(ERR_get_error(), NULL)); |
876 |
SetDead(client_p); |
877 |
exit_client(client_p, "SSL_new failed"); |
878 |
return; |
879 |
} |
880 |
|
881 |
SSL_set_fd(fd->ssl, fd->fd); |
882 |
|
883 |
if (!EmptyString(conf->cipher_list)) |
884 |
SSL_set_cipher_list(client_p->connection->fd.ssl, conf->cipher_list); |
885 |
|
886 |
ssl_server_handshake(NULL, client_p); |
887 |
} |
888 |
#endif |
889 |
|
890 |
/* serv_connect_callback() - complete a server connection. |
891 |
* |
892 |
* 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 |
struct MaskItem *conf = NULL; |
903 |
|
904 |
/* First, make sure it's a real client! */ |
905 |
assert(client_p); |
906 |
assert(&client_p->connection->fd == fd); |
907 |
|
908 |
/* Next, for backward purposes, record the ip of the server */ |
909 |
memcpy(&client_p->connection->ip, &fd->connect.hostaddr, |
910 |
sizeof(struct irc_ssaddr)); |
911 |
|
912 |
/* 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 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
920 |
"Error connecting to %s: %s", |
921 |
client_p->name, comm_errstr(status)); |
922 |
else |
923 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
924 |
"Error connecting to %s[%s]: %s", client_p->name, |
925 |
client_p->host, comm_errstr(status)); |
926 |
|
927 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
928 |
"Error connecting to %s: %s", |
929 |
client_p->name, comm_errstr(status)); |
930 |
|
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 |
conf = find_conf_name(&client_p->connection->confs, |
941 |
client_p->name, CONF_SERVER); |
942 |
if (conf == NULL) |
943 |
{ |
944 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
945 |
"Lost connect{} block for %s", get_client_name(client_p, HIDE_IP)); |
946 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
947 |
"Lost connect{} block for %s", get_client_name(client_p, MASK_IP)); |
948 |
|
949 |
exit_client(client_p, "Lost connect{} block"); |
950 |
return; |
951 |
} |
952 |
|
953 |
/* Next, send the initial handshake */ |
954 |
SetHandshake(client_p); |
955 |
|
956 |
#ifdef HAVE_LIBCRYPTO |
957 |
if (IsConfSSL(conf)) |
958 |
{ |
959 |
ssl_connect_init(client_p, conf, fd); |
960 |
return; |
961 |
} |
962 |
#endif |
963 |
|
964 |
sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id); |
965 |
|
966 |
send_capabilities(client_p); |
967 |
|
968 |
sendto_one(client_p, "SERVER %s 1 :%s%s", me.name, |
969 |
ConfigServerHide.hidden ? "(H) " : "", me.info); |
970 |
|
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 |
if (IsDead(client_p)) |
975 |
{ |
976 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
977 |
"%s[%s] went dead during handshake", |
978 |
client_p->name, |
979 |
client_p->host); |
980 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
981 |
"%s went dead during handshake", client_p->name); |
982 |
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 |
if (!match(name, cptr->name)) |
1002 |
return cptr; |
1003 |
} |
1004 |
|
1005 |
return NULL; |
1006 |
} |