ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/server.c
Revision: 9003
Committed: Sun May 26 00:00:31 2019 UTC (6 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 19113 byte(s)
Log Message:
- Kill a few memcpy() not needed in some places

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2019 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 server.c
23 * \brief Server related functions.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "client.h"
30 #include "event.h"
31 #include "hash.h"
32 #include "irc_string.h"
33 #include "ircd.h"
34 #include "ircd_defs.h"
35 #include "s_bsd.h"
36 #include "packet.h"
37 #include "conf.h"
38 #include "server.h"
39 #include "server_capab.h"
40 #include "log.h"
41 #include "send.h"
42 #include "memory.h"
43 #include "parse.h"
44
45
46 dlink_list flatten_links;
47 static void server_connect_callback(fde_t *, int, void *);
48
49
50 /*
51 * write_links_file
52 *
53 * inputs - void pointer which is not used
54 * output - NONE
55 * side effects - called from an event, write out list of linked servers
56 * but in no particular order.
57 */
58 void
59 write_links_file(void *unused)
60 {
61 FILE *file = NULL;
62 dlink_node *node, *node_next;
63 char buff[IRCD_BUFSIZE] = "";
64
65 if (EmptyString(ConfigServerHide.flatten_links_file))
66 return;
67
68 if ((file = fopen(ConfigServerHide.flatten_links_file, "w")) == NULL)
69 {
70 ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
71 strerror(errno));
72 return;
73 }
74
75 DLINK_FOREACH_SAFE(node, node_next, flatten_links.head)
76 {
77 dlinkDelete(node, &flatten_links);
78 xfree(node->data);
79 free_dlink_node(node);
80 }
81
82 DLINK_FOREACH(node, global_server_list.head)
83 {
84 const struct Client *target_p = node->data;
85
86 /*
87 * Skip hidden servers, aswell as ourselves, since we already send
88 * ourselves in /links
89 */
90 if (IsHidden(target_p) || IsMe(target_p))
91 continue;
92
93 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
94 continue;
95
96 /*
97 * Attempt to format the file in such a way it follows the usual links output
98 * ie "servername uplink :hops info"
99 * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
100 * - madmax
101 */
102 snprintf(buff, sizeof(buff), "%s %s :1 %s", target_p->name,
103 me.name, target_p->info);
104 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
105 snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
106 me.name, target_p->info);
107
108 fputs(buff, file);
109 }
110
111 fclose(file);
112 }
113
114 void
115 read_links_file(void)
116 {
117 FILE *file = NULL;
118 char *p = NULL;
119 char buff[IRCD_BUFSIZE] = "";
120
121 if (EmptyString(ConfigServerHide.flatten_links_file))
122 return;
123
124 if ((file = fopen(ConfigServerHide.flatten_links_file, "r")) == NULL)
125 {
126 ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", ConfigServerHide.flatten_links_file,
127 strerror(errno));
128 return;
129 }
130
131 while (fgets(buff, sizeof(buff), file))
132 {
133 if ((p = strchr(buff, '\n')))
134 *p = '\0';
135
136 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
137 }
138
139 fclose(file);
140 }
141
142 /* server_hunt()
143 * Do the basic thing in delivering the message (command)
144 * across the relays to the specific server (server) for
145 * actions.
146 *
147 * Note: The command is a format string and *MUST* be
148 * of prefixed style (e.g. ":%s COMMAND %s ...").
149 * Command can have only max 8 parameters.
150 *
151 * server parv[server] is the parameter identifying the
152 * target server.
153 *
154 * *WARNING*
155 * parv[server] is replaced with the pointer to the
156 * real servername from the matched client (I'm lazy
157 * now --msa).
158 *
159 * returns: (see #defines)
160 */
161 const struct server_hunt *
162 server_hunt(struct Client *source_p, const char *command,
163 const int server, const int parc, char *parv[])
164 {
165 static struct server_hunt hunt;
166 struct server_hunt *const h = &hunt;
167 dlink_node *node;
168
169 /* Assume it's me, if no server */
170 if (parc <= server || EmptyString(parv[server]))
171 {
172 h->target_p = &me;
173 h->ret = HUNTED_ISME;
174 return h;
175 }
176
177 if ((h->target_p = find_person(source_p, parv[server])) == NULL)
178 h->target_p = hash_find_server(parv[server]);
179
180 /*
181 * These are to pickup matches that would cause the following
182 * message to go in the wrong direction while doing quick fast
183 * non-matching lookups.
184 */
185 if (h->target_p)
186 if (h->target_p->from == source_p->from && !MyConnect(h->target_p))
187 h->target_p = NULL;
188
189 if (h->target_p == NULL && has_wildcards(parv[server]))
190 {
191 DLINK_FOREACH(node, global_server_list.head)
192 {
193 struct Client *tmp = node->data;
194
195 assert(IsMe(tmp) || IsServer(tmp));
196 if (match(parv[server], tmp->name) == 0)
197 {
198 if (tmp->from == source_p->from && !MyConnect(tmp))
199 continue;
200
201 h->target_p = tmp;
202 break;
203 }
204 }
205
206 if (h->target_p == NULL)
207 {
208 DLINK_FOREACH(node, global_client_list.head)
209 {
210 struct Client *tmp = node->data;
211
212 assert(IsClient(tmp));
213 if (match(parv[server], tmp->name) == 0)
214 {
215 if (tmp->from == source_p->from && !MyConnect(tmp))
216 continue;
217
218 h->target_p = tmp;
219 break;
220 }
221 }
222 }
223 }
224
225 if (h->target_p)
226 {
227 assert(IsMe(h->target_p) || IsServer(h->target_p) || IsClient(h->target_p));
228 if (IsMe(h->target_p) || MyClient(h->target_p))
229 {
230 h->ret = HUNTED_ISME;
231 return h;
232 }
233
234 parv[server] = h->target_p->id;
235 sendto_one(h->target_p, command, source_p->id,
236 parv[1], parv[2], parv[3], parv[4],
237 parv[5], parv[6], parv[7], parv[8]);
238 h->ret = HUNTED_PASS;
239 return h;
240 }
241
242 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
243 h->ret = HUNTED_NOSUCH;
244 return h;
245 }
246
247 /* try_connections()
248 *
249 * inputs - void pointer which is not used
250 * output - NONE
251 * side effects -
252 * scan through configuration and try new connections.
253 * Returns the calendar time when the next call to this
254 * function should be made latest. (No harm done if this
255 * is called earlier or later...)
256 */
257 void
258 try_connections(void *unused)
259 {
260 dlink_node *node;
261
262 if (GlobalSetOptions.autoconn == false)
263 return;
264
265 DLINK_FOREACH(node, connect_items.head)
266 {
267 struct MaskItem *conf = node->data;
268
269 assert(conf->type == CONF_SERVER);
270 assert(conf->class);
271
272 /* Also when already connecting! (update holdtimes) --SRB */
273 if (conf->port == 0 || !IsConfAllowAutoConn(conf))
274 continue;
275
276 /*
277 * Skip this entry if the use of it is still on hold until
278 * future. Otherwise handle this entry (and set it on hold
279 * until next time). Will reset only hold times, if already
280 * made one successfull connection... [this algorithm is
281 * a bit fuzzy... -- msa >;) ]
282 */
283 if (conf->until > event_base->time.sec_monotonic)
284 continue;
285
286 conf->until = event_base->time.sec_monotonic + conf->class->con_freq;
287
288 /*
289 * Found a CONNECT config with port specified, scan clients
290 * and see if this server is already connected?
291 */
292 if (hash_find_server(conf->name))
293 continue;
294
295 if (conf->class->ref_count < conf->class->max_total)
296 {
297 /* Move this entry to the end of the list, if not already last */
298 if (node->next)
299 {
300 dlinkDelete(node, &connect_items);
301 dlinkAddTail(conf, &conf->node, &connect_items);
302 }
303
304 if (find_servconn_in_progress(conf->name))
305 return;
306
307 /*
308 * We used to only print this if server_connect() actually
309 * succeeded, but since comm_tcp_connect() can call the callback
310 * immediately if there is an error, we were getting error messages
311 * in the wrong order. SO, we just print out the activated line,
312 * and let server_connect() / server_connect_callback() print an
313 * error afterwards if it fails.
314 * -- adrian
315 */
316 if (ConfigServerHide.hide_server_ips)
317 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
318 "Connection to %s activated.",
319 conf->name);
320 else
321 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
322 "Connection to %s[%s] activated.",
323 conf->name, conf->host);
324
325 server_connect(conf, NULL);
326 /* We connect only one at time... */
327 return;
328 }
329 }
330 }
331
332 bool
333 server_valid_name(const char *name)
334 {
335 unsigned int dots = 0;
336 const char *p = name;
337
338 for (; *p; ++p)
339 {
340 if (!IsServChar(*p))
341 return false;
342
343 if (*p == '.')
344 ++dots;
345 }
346
347 return dots && (p - name) <= HOSTLEN;
348 }
349
350 /* server_make()
351 *
352 * inputs - pointer to client struct
353 * output - pointer to struct Server
354 * side effects - add's an Server information block to a client
355 * if it was not previously allocated.
356 */
357 struct Server *
358 server_make(struct Client *client_p)
359 {
360 if (client_p->serv == NULL)
361 client_p->serv = xcalloc(sizeof(*client_p->serv));
362
363 return client_p->serv;
364 }
365
366 /* server_connect() - initiate a server connection
367 *
368 * inputs - pointer to conf
369 * - pointer to client doing the connect
370 * output -
371 * side effects -
372 *
373 * This code initiates a connection to a server. It first checks to make
374 * sure the given server exists. If this is the case, it creates a socket,
375 * creates a client, saves the socket information in the client, and
376 * initiates a connection to the server through comm_connect_tcp(). The
377 * completion of this goes through serv_completed_connection().
378 *
379 * We return 1 if the connection is attempted, since we don't know whether
380 * it suceeded or not, and 0 if it fails in here somewhere.
381 */
382 bool
383 server_connect(struct MaskItem *conf, struct Client *by)
384 {
385 char buf[HOSTIPLEN + 1] = "";
386
387 /* Make sure conf is useful */
388 assert(conf);
389 assert(conf->type == CONF_SERVER);
390 assert(hash_find_server(conf->name) == NULL); /* This should have been checked by the caller */
391
392 /* Still processing a DNS lookup? -> exit */
393 if (conf->dns_pending == true)
394 {
395 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
396 "Error connecting to %s: DNS lookup for connect{} in progress.",
397 conf->name);
398 return false;
399 }
400
401 if (conf->dns_failed == true)
402 {
403 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
404 "Error connecting to %s: DNS lookup for connect{} failed.",
405 conf->name);
406 return false;
407 }
408
409 getnameinfo((const struct sockaddr *)conf->addr, conf->addr->ss_len,
410 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
411 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host, buf);
412
413 /* Create a socket for the server connection */
414 int fd = comm_socket(conf->addr->ss.ss_family, SOCK_STREAM, 0);
415 if (fd == -1)
416 {
417 /* Eek, failure to create the socket */
418 report_error(L_ALL, "opening stream socket to %s: %s", conf->name, errno);
419 return false;
420 }
421
422 /* Create a local client */
423 struct Client *client_p = client_make(NULL);
424
425 /* Copy in the server, hostname, fd */
426 strlcpy(client_p->name, conf->name, sizeof(client_p->name));
427 strlcpy(client_p->host, conf->host, sizeof(client_p->host));
428
429 /* We already converted the ip once, so lets use it - stu */
430 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
431
432 client_p->ip = *conf->addr;
433 client_p->connection->fd = fd_open(fd, true, NULL);
434
435 /* Server names are always guaranteed under HOSTLEN chars */
436 fd_note(client_p->connection->fd, "Server: %s", client_p->name);
437
438 /*
439 * Attach config entries to client here rather than in server_connect_callback().
440 * This to avoid null pointer references.
441 */
442 conf_attach(client_p, conf);
443
444 server_make(client_p);
445
446 if (by && IsClient(by))
447 strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
448 else
449 strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
450
451 SetConnecting(client_p);
452
453 /* Now, initiate the connection */
454 comm_connect_tcp(client_p->connection->fd, conf->addr, conf->port, conf->bind,
455 server_connect_callback, client_p, CONNECTTIMEOUT);
456
457 /*
458 * At this point we have a connection in progress and a connect {} block
459 * attached to the client, the socket info should be saved in the client
460 * and it should either be resolved or have a valid address.
461 *
462 * The socket has been connected or connect is in progress.
463 */
464 return true;
465 }
466
467 static void
468 server_finish_tls_handshake(struct Client *client_p)
469 {
470 const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
471 client_p->name, CONF_SERVER);
472 if (!conf)
473 {
474 sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
475 "Lost connect{} block for %s", client_get_name(client_p, SHOW_IP));
476 sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
477 "Lost connect{} block for %s", client_get_name(client_p, MASK_IP));
478
479 exit_client(client_p, "Lost connect{} block");
480 return;
481 }
482
483 /* Next, send the initial handshake */
484 SetHandshake(client_p);
485
486 sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
487
488 sendto_one(client_p, "CAPAB :%s", capab_get(NULL));
489
490 sendto_one(client_p, "SERVER %s 1 :%s%s",
491 me.name, ConfigServerHide.hidden ? "(H) " : "",
492 me.info);
493
494 /* If we get here, we're ok, so lets start reading some data */
495 read_packet(client_p->connection->fd, client_p);
496 }
497
498 static void
499 server_tls_handshake(fde_t *F, void *data)
500 {
501 struct Client *client_p = data;
502 const char *sslerr = NULL;
503
504 assert(client_p);
505 assert(client_p->connection);
506 assert(client_p->connection->fd);
507 assert(client_p->connection->fd == F);
508
509 tls_handshake_status_t ret = tls_handshake(&F->ssl, TLS_ROLE_CLIENT, &sslerr);
510 if (ret != TLS_HANDSHAKE_DONE)
511 {
512 if ((event_base->time.sec_monotonic - client_p->connection->created_monotonic) > TLS_HANDSHAKE_TIMEOUT)
513 {
514 exit_client(client_p, "Timeout during TLS handshake");
515 return;
516 }
517
518 switch (ret)
519 {
520 case TLS_HANDSHAKE_WANT_WRITE:
521 comm_setselect(F, COMM_SELECT_WRITE,
522 server_tls_handshake, client_p, TLS_HANDSHAKE_TIMEOUT);
523 return;
524 case TLS_HANDSHAKE_WANT_READ:
525 comm_setselect(F, COMM_SELECT_READ,
526 server_tls_handshake, client_p, TLS_HANDSHAKE_TIMEOUT);
527 return;
528 default:
529 {
530 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
531 "Error connecting to %s: %s", client_p->name,
532 sslerr ? sslerr : "unknown TLS error");
533 exit_client(client_p, "Error during TLS handshake");
534 return;
535 }
536 }
537 }
538
539 comm_settimeout(F, 0, NULL, NULL);
540
541 if (tls_verify_cert(&F->ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp) == false)
542 ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad TLS client certificate",
543 client_p->name, client_p->username, client_p->host);
544
545 server_finish_tls_handshake(client_p);
546 }
547
548 static void
549 server_tls_connect_init(struct Client *client_p, const struct MaskItem *conf, fde_t *F)
550 {
551 assert(client_p);
552 assert(client_p->connection);
553 assert(client_p->connection->fd);
554 assert(client_p->connection->fd == F);
555
556 if (tls_new(&F->ssl, F->fd, TLS_ROLE_CLIENT) == false)
557 {
558 SetDead(client_p);
559 exit_client(client_p, "TLS context initialization failed");
560 return;
561 }
562
563 if (!EmptyString(conf->cipher_list))
564 tls_set_ciphers(&F->ssl, conf->cipher_list);
565
566 server_tls_handshake(F, client_p);
567 }
568
569 /* server_connect_callback() - complete a server connection.
570 *
571 * This routine is called after the server connection attempt has
572 * completed. If unsucessful, an error is sent to ops and the client
573 * is closed. If sucessful, it goes through the initialisation/check
574 * procedures, the capabilities are sent, and the socket is then
575 * marked for reading.
576 */
577 static void
578 server_connect_callback(fde_t *F, int status, void *data)
579 {
580 struct Client *const client_p = data;
581
582 /* First, make sure it's a real client! */
583 assert(client_p);
584 assert(client_p->connection);
585 assert(client_p->connection->fd);
586 assert(client_p->connection->fd == F);
587
588 /* Check the status */
589 if (status != COMM_OK)
590 {
591 /* We have an error, so report it and quit */
592 sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
593 "Error connecting to %s: %s",
594 client_get_name(client_p, SHOW_IP), comm_errstr(status));
595 sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
596 "Error connecting to %s: %s",
597 client_get_name(client_p, MASK_IP), comm_errstr(status));
598
599 /*
600 * If a fd goes bad, call dead_link() the socket is no
601 * longer valid for reading or writing.
602 */
603 dead_link_on_write(client_p, 0);
604 return;
605 }
606
607 /* COMM_OK, so continue the connection procedure */
608 /* Get the connect {} block */
609 const struct MaskItem *conf = find_conf_name(&client_p->connection->confs,
610 client_p->name, CONF_SERVER);
611 if (!conf)
612 {
613 sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
614 "Lost connect{} block for %s", client_get_name(client_p, SHOW_IP));
615 sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
616 "Lost connect{} block for %s", client_get_name(client_p, MASK_IP));
617
618 exit_client(client_p, "Lost connect{} block");
619 return;
620 }
621
622 if (IsConfSSL(conf))
623 {
624 server_tls_connect_init(client_p, conf, F);
625 return;
626 }
627
628 /* Next, send the initial handshake */
629 SetHandshake(client_p);
630
631 sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id);
632
633 sendto_one(client_p, "CAPAB :%s", capab_get(NULL));
634
635 sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
636 ConfigServerHide.hidden ? "(H) " : "", me.info);
637
638 /* If we get here, we're ok, so lets start reading some data */
639 read_packet(client_p->connection->fd, client_p);
640 }
641
642 struct Client *
643 find_servconn_in_progress(const char *name)
644 {
645 dlink_node *ptr;
646
647 DLINK_FOREACH(ptr, unknown_list.head)
648 {
649 struct Client *cptr = ptr->data;
650
651 if (cptr->name[0])
652 if (!irccmp(name, cptr->name))
653 return cptr;
654 }
655
656 return NULL;
657 }

Properties

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