2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/service.c
23 * @brief functions related to starting services
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_resolver_service.h"
38 /* ******************* access control ******************** */
41 * Check if the given IP address is in the list of IP addresses.
43 * @param list a list of networks
44 * @param add the IP to check (in network byte order)
45 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
48 check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
49 const struct in_addr *add)
56 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
58 if ((add->s_addr & list[i].netmask.s_addr) ==
59 (list[i].network.s_addr & list[i].netmask.s_addr))
68 * Check if the given IP address is in the list of IP addresses.
70 * @param list a list of networks
71 * @param ip the IP to check (in network byte order)
72 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
75 check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
76 const struct in6_addr *ip)
84 memset (&zero, 0, sizeof (struct in6_addr));
87 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
89 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
90 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
91 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
102 /* ****************** service struct ****************** */
106 * Context for "service_task".
108 struct LEGACY_SERVICE_Context
113 const struct GNUNET_CONFIGURATION_Handle *cfg;
116 * Handle for the server.
118 struct GNUNET_SERVER_Handle *server;
121 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
124 struct sockaddr **addrs;
127 * Name of our service.
129 const char *service_name;
132 * Main service-specific task to run.
134 LEGACY_SERVICE_Main task;
137 * Closure for @e task.
142 * IPv4 addresses that are not allowed to connect.
144 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
147 * IPv6 addresses that are not allowed to connect.
149 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
152 * IPv4 addresses that are allowed to connect (if not
153 * set, all are allowed).
155 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
158 * IPv6 addresses that are allowed to connect (if not
159 * set, all are allowed).
161 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
164 * My (default) message handlers. Adjusted copy
167 struct GNUNET_SERVER_MessageHandler *my_handlers;
170 * Array of the lengths of the entries in addrs.
175 * NULL-terminated array of listen sockets we should take over.
177 struct GNUNET_NETWORK_Handle **lsocks;
180 * Task ID of the shutdown task.
182 struct GNUNET_SCHEDULER_Task *shutdown_task;
185 * Idle timeout for server.
187 struct GNUNET_TIME_Relative timeout;
190 * Overall success/failure of the service start.
195 * If we are daemonizing, this FD is set to the
196 * pipe to the parent. Send '.' if we started
197 * ok, '!' if not. -1 if we are not daemonizing.
199 int ready_confirm_fd;
202 * Do we close connections if we receive messages
203 * for which we have no handler?
208 * Do we require a matching UID for UNIX domain socket connections?
209 * #GNUNET_NO means that the UID does not have to match (however,
210 * @e match_gid may still impose other access control checks).
215 * Do we require a matching GID for UNIX domain socket connections?
216 * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
217 * checking that the client's UID is in our group OR that the
218 * client's GID is our GID. If both "match_gid" and @e match_uid are
219 * #GNUNET_NO, all users on the local system have access.
226 enum LEGACY_SERVICE_Options options;
231 /* ****************** message handlers ****************** */
234 * Send a 'TEST' message back to the client.
236 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
237 * @param size number of bytes available in 'buf'
238 * @param buf where to copy the message
239 * @return number of bytes written to 'buf'
242 write_test (void *cls, size_t size, void *buf)
244 struct GNUNET_SERVER_Client *client = cls;
245 struct GNUNET_MessageHeader *msg;
247 if (size < sizeof (struct GNUNET_MessageHeader))
249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
250 return 0; /* client disconnected */
252 msg = (struct GNUNET_MessageHeader *) buf;
253 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
254 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
255 GNUNET_SERVER_receive_done (client, GNUNET_OK);
256 return sizeof (struct GNUNET_MessageHeader);
261 * Handler for TEST message.
263 * @param cls closure (refers to service)
264 * @param client identification of the client
265 * @param message the actual message
268 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
269 const struct GNUNET_MessageHeader *message)
271 /* simply bounce message back to acknowledge */
273 GNUNET_SERVER_notify_transmit_ready (client,
274 sizeof (struct GNUNET_MessageHeader),
275 GNUNET_TIME_UNIT_FOREVER_REL,
276 &write_test, client))
277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
282 * Default handlers for all services. Will be copied and the
283 * "callback_cls" fields will be replaced with the specific service
286 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
287 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
288 sizeof (struct GNUNET_MessageHeader)},
293 /* ****************** service core routines ************** */
297 * Check if access to the service is allowed from the given address.
300 * @param uc credentials, if available, otherwise NULL
301 * @param addr address
302 * @param addrlen length of address
303 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
304 * for unknown address family (will be denied).
307 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
308 const struct sockaddr *addr, socklen_t addrlen)
310 struct LEGACY_SERVICE_Context *sctx = cls;
311 const struct sockaddr_in *i4;
312 const struct sockaddr_in6 *i6;
315 switch (addr->sa_family)
318 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
319 i4 = (const struct sockaddr_in *) addr;
320 ret = ((NULL == sctx->v4_allowed) ||
321 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
322 ((NULL == sctx->v4_denied) ||
323 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
326 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
327 i6 = (const struct sockaddr_in6 *) addr;
328 ret = ((NULL == sctx->v6_allowed) ||
329 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
330 ((NULL == sctx->v6_denied) ||
331 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
335 ret = GNUNET_OK; /* controlled using file-system ACL now */
339 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
341 return GNUNET_SYSERR;
343 if (GNUNET_OK != ret)
345 LOG (GNUNET_ERROR_TYPE_WARNING,
346 _("Access from `%s' denied to service `%s'\n"),
347 GNUNET_a2s (addr, addrlen),
355 * Get the name of the file where we will
356 * write the PID of the service.
358 * @param sctx service context
359 * @return name of the file for the process ID
362 get_pid_file_name (struct LEGACY_SERVICE_Context *sctx)
367 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
375 * Parse an IPv4 access control list.
377 * @param ret location where to write the ACL (set)
378 * @param sctx service context to use to get the configuration
379 * @param option name of the ACL option to parse
380 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
384 process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
385 struct LEGACY_SERVICE_Context *sctx,
390 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
395 GNUNET_break (GNUNET_OK ==
396 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
399 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
401 LOG (GNUNET_ERROR_TYPE_WARNING,
402 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
403 opt, sctx->service_name, option);
405 return GNUNET_SYSERR;
413 * Parse an IPv6 access control list.
415 * @param ret location where to write the ACL (set)
416 * @param sctx service context to use to get the configuration
417 * @param option name of the ACL option to parse
418 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
422 process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
423 struct LEGACY_SERVICE_Context *sctx,
428 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
433 GNUNET_break (GNUNET_OK ==
434 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
437 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
439 LOG (GNUNET_ERROR_TYPE_WARNING,
440 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
441 opt, sctx->service_name, option);
443 return GNUNET_SYSERR;
451 * Add the given UNIX domain path as an address to the
452 * list (as the first entry).
454 * @param saddrs array to update
455 * @param saddrlens where to store the address length
456 * @param unixpath path to add
457 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
458 * parameter is ignore on systems other than LINUX
461 add_unixpath (struct sockaddr **saddrs,
462 socklen_t *saddrlens,
463 const char *unixpath,
467 struct sockaddr_un *un;
469 un = GNUNET_new (struct sockaddr_un);
470 un->sun_family = AF_UNIX;
471 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
473 if (GNUNET_YES == abstract)
474 un->sun_path[0] = '\0';
476 #if HAVE_SOCKADDR_UN_SUN_LEN
477 un->sun_len = (u_char) sizeof (struct sockaddr_un);
479 *saddrs = (struct sockaddr *) un;
480 *saddrlens = sizeof (struct sockaddr_un);
482 /* this function should never be called
483 * unless AF_UNIX is defined! */
490 * Get the list of addresses that a server for the given service
493 * @param service_name name of the service
494 * @param cfg configuration (which specifies the addresses)
495 * @param addrs set (call by reference) to an array of pointers to the
496 * addresses the server should bind to and listen on; the
497 * array will be NULL-terminated (on success)
498 * @param addr_lens set (call by reference) to an array of the lengths
499 * of the respective `struct sockaddr` struct in the @a addrs
501 * @return number of addresses found on success,
502 * #GNUNET_SYSERR if the configuration
503 * did not specify reasonable finding information or
504 * if it specified a hostname that could not be resolved;
505 * #GNUNET_NO if the number of addresses configured is
506 * zero (in this case, `*addrs` and `*addr_lens` will be
510 LEGACY_SERVICE_get_server_addresses (const char *service_name,
511 const struct GNUNET_CONFIGURATION_Handle *cfg,
512 struct sockaddr ***addrs,
513 socklen_t ** addr_lens)
516 struct GNUNET_NETWORK_Handle *desc;
517 unsigned long long port;
519 struct addrinfo hints;
520 struct addrinfo *res;
521 struct addrinfo *pos;
522 struct addrinfo *next;
527 struct sockaddr **saddrs;
528 socklen_t *saddrlens;
534 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
538 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
539 return GNUNET_SYSERR;
542 disablev6 = GNUNET_NO;
546 /* probe IPv6 support */
547 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
550 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
553 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
554 return GNUNET_SYSERR;
556 LOG (GNUNET_ERROR_TYPE_INFO,
557 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
558 service_name, STRERROR (errno));
559 disablev6 = GNUNET_YES;
563 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
569 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
572 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
575 LOG (GNUNET_ERROR_TYPE_ERROR,
576 _("Require valid port number for service `%s' in configuration!\n"),
581 LOG (GNUNET_ERROR_TYPE_ERROR,
582 _("Require valid port number for service `%s' in configuration!\n"),
584 return GNUNET_SYSERR;
588 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
590 GNUNET_break (GNUNET_OK ==
591 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
592 "BINDTO", &hostname));
598 abstract = GNUNET_NO;
601 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
603 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH",
605 (0 < strlen (unixpath)))
607 /* probe UNIX support */
608 struct sockaddr_un s_un;
610 if (strlen (unixpath) >= sizeof (s_un.sun_path))
612 LOG (GNUNET_ERROR_TYPE_WARNING,
613 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
614 (unsigned long long) sizeof (s_un.sun_path));
615 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
616 LOG (GNUNET_ERROR_TYPE_INFO,
617 _("Using `%s' instead\n"),
621 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
623 "USE_ABSTRACT_SOCKETS");
624 if (GNUNET_SYSERR == abstract)
625 abstract = GNUNET_NO;
627 if ((GNUNET_YES != abstract)
629 GNUNET_DISK_directory_create_for_file (unixpath)))
630 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
634 if (NULL != unixpath)
636 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
639 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
642 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
643 GNUNET_free_non_null (hostname);
644 GNUNET_free (unixpath);
645 return GNUNET_SYSERR;
647 LOG (GNUNET_ERROR_TYPE_INFO,
648 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
651 GNUNET_free (unixpath);
656 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
662 if ((0 == port) && (NULL == unixpath))
664 LOG (GNUNET_ERROR_TYPE_ERROR,
665 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
667 GNUNET_free_non_null (hostname);
668 return GNUNET_SYSERR;
672 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
673 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
674 add_unixpath (saddrs, saddrlens, unixpath, abstract);
675 GNUNET_free_non_null (unixpath);
676 GNUNET_free_non_null (hostname);
678 *addr_lens = saddrlens;
682 if (NULL != hostname)
684 LOG (GNUNET_ERROR_TYPE_DEBUG,
685 "Resolving `%s' since that is where `%s' will bind to.\n",
688 memset (&hints, 0, sizeof (struct addrinfo));
690 hints.ai_family = AF_INET;
691 hints.ai_protocol = IPPROTO_TCP;
692 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
695 LOG (GNUNET_ERROR_TYPE_ERROR,
696 _("Failed to resolve `%s': %s\n"),
699 GNUNET_free (hostname);
700 GNUNET_free_non_null (unixpath);
701 return GNUNET_SYSERR;
705 while (NULL != (pos = next))
708 if ((disablev6) && (pos->ai_family == AF_INET6))
714 LOG (GNUNET_ERROR_TYPE_ERROR,
715 _("Failed to find %saddress for `%s'.\n"),
716 disablev6 ? "IPv4 " : "",
719 GNUNET_free (hostname);
720 GNUNET_free_non_null (unixpath);
721 return GNUNET_SYSERR;
724 if (NULL != unixpath)
726 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
727 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
729 if (NULL != unixpath)
731 add_unixpath (saddrs, saddrlens, unixpath, abstract);
735 while (NULL != (pos = next))
738 if ((disablev6) && (AF_INET6 == pos->ai_family))
740 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
741 continue; /* not TCP */
742 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
744 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
745 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
746 if (AF_INET == pos->ai_family)
748 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
749 saddrlens[i] = pos->ai_addrlen;
750 saddrs[i] = GNUNET_malloc (saddrlens[i]);
751 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
752 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
756 GNUNET_assert (AF_INET6 == pos->ai_family);
757 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
758 saddrlens[i] = pos->ai_addrlen;
759 saddrs[i] = GNUNET_malloc (saddrlens[i]);
760 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
761 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
765 GNUNET_free (hostname);
771 /* will bind against everything, just set port */
776 if (NULL != unixpath)
779 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
780 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
781 if (NULL != unixpath)
783 add_unixpath (saddrs, saddrlens, unixpath, abstract);
786 saddrlens[i] = sizeof (struct sockaddr_in);
787 saddrs[i] = GNUNET_malloc (saddrlens[i]);
788 #if HAVE_SOCKADDR_IN_SIN_LEN
789 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
791 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
792 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
798 if (NULL != unixpath)
800 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
801 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
803 if (NULL != unixpath)
805 add_unixpath (saddrs, saddrlens, unixpath, abstract);
808 saddrlens[i] = sizeof (struct sockaddr_in6);
809 saddrs[i] = GNUNET_malloc (saddrlens[i]);
810 #if HAVE_SOCKADDR_IN_SIN_LEN
811 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
813 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
814 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
816 saddrlens[i] = sizeof (struct sockaddr_in);
817 saddrs[i] = GNUNET_malloc (saddrlens[i]);
818 #if HAVE_SOCKADDR_IN_SIN_LEN
819 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
821 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
822 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
825 GNUNET_free_non_null (unixpath);
827 *addr_lens = saddrlens;
834 * Read listen sockets from the parent process (ARM).
836 * @param sctx service context to initialize
837 * @return #GNUNET_YES if ok, #GNUNET_NO if not ok (must bind yourself),
838 * and #GNUNET_SYSERR on error.
841 receive_sockets_from_parent (struct LEGACY_SERVICE_Context *sctx)
849 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
850 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
852 /* Using W32 API directly here, because this pipe will
853 * never be used outside of this function, and it's just too much of a bother
854 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
856 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
857 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
866 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
867 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
870 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
873 for (i = 0; i < count; i++)
875 WSAPROTOCOL_INFOA pi;
879 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
880 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
882 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
883 if ( (0 == ret) || (sizeof (pi) != rd))
885 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
886 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
887 if (NULL == sctx->lsocks[i])
889 else if (i == count - 1)
894 sctx->lsocks[count] = NULL;
899 CloseHandle (lsocks_pipe);
903 LOG (GNUNET_ERROR_TYPE_ERROR,
904 _("Could not access a pre-bound socket, will try to bind myself\n"));
905 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
906 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
907 GNUNET_free_non_null (sctx->lsocks);
917 * Setup addr, addrlen, idle_timeout
918 * based on configuration!
920 * Configuration may specify:
921 * - PORT (where to bind to for TCP)
922 * - UNIXPATH (where to bind to for UNIX domain sockets)
923 * - TIMEOUT (after how many ms does an inactive service timeout);
924 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
925 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
926 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
927 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
928 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
929 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
931 * @param sctx service context to initialize
932 * @return #GNUNET_OK if configuration succeeded
935 setup_service (struct LEGACY_SERVICE_Context *sctx)
937 struct GNUNET_TIME_Relative idleout;
946 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
949 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
950 "TIMEOUT", &idleout))
952 LOG (GNUNET_ERROR_TYPE_ERROR,
953 _("Specified value for `%s' of service `%s' is invalid\n"),
954 "TIMEOUT", sctx->service_name);
955 return GNUNET_SYSERR;
957 sctx->timeout = idleout;
960 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
962 if (GNUNET_CONFIGURATION_have_value
963 (sctx->cfg, sctx->service_name, "TOLERANT"))
967 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
970 LOG (GNUNET_ERROR_TYPE_ERROR,
971 _("Specified value for `%s' of service `%s' is invalid\n"),
972 "TOLERANT", sctx->service_name);
973 return GNUNET_SYSERR;
977 tolerant = GNUNET_NO;
981 if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
982 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
983 (cnt + 4 < FD_SETSIZE))
986 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
989 flags = fcntl (3 + cnt, F_GETFD);
990 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
992 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
994 LOG (GNUNET_ERROR_TYPE_ERROR,
996 ("Could not access pre-bound socket %u, will try to bind myself\n"),
997 (unsigned int) 3 + cnt);
999 while (sctx->lsocks[cnt] != NULL)
1000 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1001 GNUNET_free (sctx->lsocks);
1002 sctx->lsocks = NULL;
1006 unsetenv ("LISTEN_FDS");
1009 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1011 receive_sockets_from_parent (sctx);
1012 putenv ("GNUNET_OS_READ_LSOCKS=");
1016 if ((NULL == sctx->lsocks) &&
1018 LEGACY_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1019 &sctx->addrs, &sctx->addrlens)))
1020 return GNUNET_SYSERR;
1021 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1023 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1026 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1028 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1029 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1030 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1031 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1038 * Get the name of the user that'll be used
1039 * to provide the service.
1041 * @param sctx service context
1042 * @return value of the 'USERNAME' option
1045 get_user_name (struct LEGACY_SERVICE_Context *sctx)
1050 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1060 * @param sctx service context
1061 * @param pid PID to write (should be equal to 'getpid()'
1062 * @return #GNUNET_OK on success (including no work to be done)
1065 write_pid_file (struct LEGACY_SERVICE_Context *sctx, pid_t pid)
1073 if (NULL == (pif = get_pid_file_name (sctx)))
1074 return GNUNET_OK; /* no file desired */
1075 user = get_user_name (sctx);
1076 rdir = GNUNET_strdup (pif);
1077 len = strlen (rdir);
1078 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1081 if (0 != ACCESS (rdir, F_OK))
1083 /* we get to create a directory -- and claim it
1085 (void) GNUNET_DISK_directory_create (rdir);
1086 if ((NULL != user) && (0 < strlen (user)))
1087 GNUNET_DISK_file_change_owner (rdir, user);
1089 if (0 != ACCESS (rdir, W_OK | X_OK))
1091 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1093 GNUNET_free_non_null (user);
1095 return GNUNET_SYSERR;
1098 pidfd = FOPEN (pif, "w");
1101 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1103 GNUNET_free_non_null (user);
1104 return GNUNET_SYSERR;
1106 if (0 > FPRINTF (pidfd, "%u", pid))
1107 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1108 GNUNET_break (0 == FCLOSE (pidfd));
1109 if ((NULL != user) && (0 < strlen (user)))
1110 GNUNET_DISK_file_change_owner (pif, user);
1111 GNUNET_free_non_null (user);
1118 * Task run during shutdown. Stops the server/service.
1120 * @param cls the `struct LEGACY_SERVICE_Context`
1123 shutdown_task (void *cls)
1125 struct LEGACY_SERVICE_Context *service = cls;
1126 struct GNUNET_SERVER_Handle *server = service->server;
1128 service->shutdown_task = NULL;
1129 if (0 != (service->options & LEGACY_SERVICE_OPTION_SOFT_SHUTDOWN))
1130 GNUNET_SERVER_stop_listening (server);
1132 GNUNET_SERVER_destroy (server);
1137 * Initial task for the service.
1139 * @param cls service context
1142 service_task (void *cls)
1144 struct LEGACY_SERVICE_Context *sctx = cls;
1147 GNUNET_RESOLVER_connect (sctx->cfg);
1148 if (NULL != sctx->lsocks)
1150 = GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1151 sctx->timeout, sctx->require_found);
1154 = GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1155 sctx->timeout, sctx->require_found);
1156 if (NULL == sctx->server)
1158 if (NULL != sctx->addrs)
1159 for (i = 0; NULL != sctx->addrs[i]; i++)
1160 LOG (GNUNET_ERROR_TYPE_INFO,
1161 _("Failed to start `%s' at `%s'\n"),
1162 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1163 sctx->ret = GNUNET_SYSERR;
1167 if (NULL != sctx->addrs)
1168 for (i = 0; NULL != sctx->addrs[i]; i++)
1169 if ((AF_UNIX == sctx->addrs[i]->sa_family)
1170 && ('\0' != ((const struct sockaddr_un *)sctx->addrs[i])->sun_path[0]))
1171 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sctx->addrs[i])->sun_path,
1177 if (0 == (sctx->options & LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN))
1179 /* install a task that will kill the server
1180 * process if the scheduler ever gets a shutdown signal */
1181 sctx->shutdown_task = GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1184 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1185 GNUNET_memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1187 while (NULL != sctx->my_handlers[i].callback)
1188 sctx->my_handlers[i++].callback_cls = sctx;
1189 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1190 if (-1 != sctx->ready_confirm_fd)
1192 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1193 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1194 sctx->ready_confirm_fd = -1;
1195 write_pid_file (sctx, getpid ());
1197 if (NULL != sctx->addrs)
1200 while (NULL != sctx->addrs[i])
1202 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1203 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1207 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1212 * Detach from terminal.
1214 * @param sctx service context
1215 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1218 detach_terminal (struct LEGACY_SERVICE_Context *sctx)
1225 if (0 != PIPE (filedes))
1227 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1228 return GNUNET_SYSERR;
1233 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1234 return GNUNET_SYSERR;
1241 GNUNET_break (0 == CLOSE (filedes[1]));
1243 if (1 != READ (filedes[0], &c, sizeof (char)))
1244 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1251 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1254 LOG (GNUNET_ERROR_TYPE_INFO,
1255 _("Service process could not initialize server function\n"));
1258 LOG (GNUNET_ERROR_TYPE_INFO,
1259 _("Service process failed to report status\n"));
1262 exit (1); /* child reported error */
1264 GNUNET_break (0 == CLOSE (0));
1265 GNUNET_break (0 == CLOSE (1));
1266 GNUNET_break (0 == CLOSE (filedes[0]));
1267 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1269 return GNUNET_SYSERR;
1270 /* set stdin/stdout to /dev/null */
1271 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1273 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1274 (void) CLOSE (nullfd);
1275 return GNUNET_SYSERR;
1277 (void) CLOSE (nullfd);
1278 /* Detach from controlling terminal */
1281 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1282 sctx->ready_confirm_fd = filedes[1];
1284 /* FIXME: we probably need to do something else
1285 * elsewhere in order to fork the process itself... */
1295 * @param sctx service context
1296 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1299 set_user_id (struct LEGACY_SERVICE_Context *sctx)
1303 if (NULL == (user = get_user_name (sctx)))
1304 return GNUNET_OK; /* keep */
1309 pws = getpwnam (user);
1312 LOG (GNUNET_ERROR_TYPE_ERROR,
1313 _("Cannot obtain information about user `%s': %s\n"), user,
1314 errno == 0 ? _("No such user") : STRERROR (errno));
1316 return GNUNET_SYSERR;
1318 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1320 (0 != initgroups (user, pws->pw_gid)) ||
1322 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1324 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1325 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1327 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1328 user, STRERROR (errno));
1330 return GNUNET_SYSERR;
1340 * Delete the PID file that was created by our parent.
1342 * @param sctx service context
1345 pid_file_delete (struct LEGACY_SERVICE_Context *sctx)
1347 char *pif = get_pid_file_name (sctx);
1350 return; /* no PID file */
1351 if (0 != UNLINK (pif))
1352 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1358 * Run a standard GNUnet service startup sequence (initialize loggers
1359 * and configuration, parse options).
1361 * @param argc number of command line arguments
1362 * @param argv command line arguments
1363 * @param service_name our service name
1364 * @param options service options
1365 * @param task main task of the service
1366 * @param task_cls closure for @a task
1367 * @return #GNUNET_SYSERR on error, #GNUNET_OK
1368 * if we shutdown nicely
1371 LEGACY_SERVICE_run (int argc, char *const *argv,
1372 const char *service_name,
1373 enum LEGACY_SERVICE_Options options,
1374 LEGACY_SERVICE_Main task,
1377 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1387 unsigned long long skew_offset;
1388 unsigned long long skew_variance;
1389 long long clock_offset;
1390 struct LEGACY_SERVICE_Context sctx;
1391 struct GNUNET_CONFIGURATION_Handle *cfg;
1394 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1395 GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_fn),
1396 GNUNET_GETOPT_OPTION_SET_ONE ('d',
1398 gettext_noop ("do daemonize (detach from terminal)"),
1400 GNUNET_GETOPT_OPTION_HELP (NULL),
1401 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1402 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1403 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1404 GNUNET_GETOPT_OPTION_END
1411 xdg = getenv ("XDG_CONFIG_HOME");
1413 GNUNET_asprintf (&cfg_fn,
1417 GNUNET_OS_project_data_get ()->config_file);
1419 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
1420 memset (&sctx, 0, sizeof (sctx));
1421 sctx.options = options;
1422 sctx.ready_confirm_fd = -1;
1423 sctx.ret = GNUNET_OK;
1424 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1426 sctx.task_cls = task_cls;
1427 sctx.service_name = service_name;
1428 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1430 /* setup subsystems */
1431 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
1432 if (GNUNET_SYSERR == ret)
1434 if (GNUNET_NO == ret)
1439 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1441 if (NULL == opt_cfg_fn)
1442 opt_cfg_fn = GNUNET_strdup (cfg_fn);
1443 if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_fn))
1445 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, opt_cfg_fn))
1447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1448 _("Malformed configuration file `%s', exit ...\n"),
1455 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
1457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1458 _("Malformed configuration, exit ...\n"));
1461 if (0 != strcmp (opt_cfg_fn, cfg_fn))
1462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1463 _("Could not access configuration file `%s'\n"),
1466 if (GNUNET_OK != setup_service (&sctx))
1468 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1470 if (GNUNET_OK != set_user_id (&sctx))
1472 LOG (GNUNET_ERROR_TYPE_DEBUG,
1473 "Service `%s' runs with configuration from `%s'\n",
1477 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1478 "SKEW_OFFSET", &skew_offset)) &&
1480 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1481 "SKEW_VARIANCE", &skew_variance)))
1483 clock_offset = skew_offset - skew_variance;
1484 GNUNET_TIME_set_offset (clock_offset);
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1487 /* actually run service */
1489 GNUNET_SCHEDULER_run (&service_task, &sctx);
1491 if ((1 == do_daemonize) && (NULL != sctx.server))
1492 pid_file_delete (&sctx);
1493 GNUNET_free_non_null (sctx.my_handlers);
1496 if (-1 != sctx.ready_confirm_fd)
1498 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1499 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1500 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1507 GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
1510 GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
1517 GAUGER (service_name, counter, mi.usmblks, "blocks");
1518 GNUNET_free (counter);
1522 GNUNET_CONFIGURATION_destroy (cfg);
1524 if (NULL != sctx.addrs)
1525 while (NULL != sctx.addrs[i])
1526 GNUNET_free (sctx.addrs[i++]);
1527 GNUNET_free_non_null (sctx.addrs);
1528 GNUNET_free_non_null (sctx.addrlens);
1529 GNUNET_free_non_null (logfile);
1530 GNUNET_free_non_null (loglev);
1531 GNUNET_free (cfg_fn);
1532 GNUNET_free_non_null (opt_cfg_fn);
1533 GNUNET_free_non_null (sctx.v4_denied);
1534 GNUNET_free_non_null (sctx.v6_denied);
1535 GNUNET_free_non_null (sctx.v4_allowed);
1536 GNUNET_free_non_null (sctx.v6_allowed);
1538 return err ? GNUNET_SYSERR : sctx.ret;
1543 * Run a service startup sequence within an existing
1544 * initialized system.
1546 * @param service_name our service name
1547 * @param cfg configuration to use
1548 * @param options service options
1549 * @return NULL on error, service handle
1551 struct LEGACY_SERVICE_Context *
1552 LEGACY_SERVICE_start (const char *service_name,
1553 const struct GNUNET_CONFIGURATION_Handle *cfg,
1554 enum LEGACY_SERVICE_Options options)
1557 struct LEGACY_SERVICE_Context *sctx;
1559 sctx = GNUNET_new (struct LEGACY_SERVICE_Context);
1560 sctx->ready_confirm_fd = -1; /* no daemonizing */
1561 sctx->ret = GNUNET_OK;
1562 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1563 sctx->service_name = service_name;
1565 sctx->options = options;
1567 /* setup subsystems */
1568 if (GNUNET_OK != setup_service (sctx))
1570 LEGACY_SERVICE_stop (sctx);
1573 if (NULL != sctx->lsocks)
1575 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1576 sctx->timeout, sctx->require_found);
1579 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1580 sctx->timeout, sctx->require_found);
1582 if (NULL == sctx->server)
1584 LEGACY_SERVICE_stop (sctx);
1588 if (NULL != sctx->addrs)
1589 for (i = 0; NULL != sctx->addrs[i]; i++)
1590 if ((AF_UNIX == sctx->addrs[i]->sa_family)
1591 && ('\0' != ((const struct sockaddr_un *)sctx->addrs[i])->sun_path[0]))
1592 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sctx->addrs[i])->sun_path,
1596 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1597 GNUNET_memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1599 while ((sctx->my_handlers[i].callback != NULL))
1600 sctx->my_handlers[i++].callback_cls = sctx;
1601 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1607 * Obtain the server used by a service. Note that the server must NOT
1608 * be destroyed by the caller.
1610 * @param ctx the service context returned from the start function
1611 * @return handle to the server for this service, NULL if there is none
1613 struct GNUNET_SERVER_Handle *
1614 LEGACY_SERVICE_get_server (struct LEGACY_SERVICE_Context *ctx)
1621 * Get the NULL-terminated array of listen sockets for this service.
1623 * @param ctx service context to query
1624 * @return NULL if there are no listen sockets, otherwise NULL-terminated
1625 * array of listen sockets.
1627 struct GNUNET_NETWORK_Handle *const*
1628 LEGACY_SERVICE_get_listen_sockets (struct LEGACY_SERVICE_Context *ctx)
1635 * Stop a service that was started with "LEGACY_SERVICE_start".
1637 * @param sctx the service context returned from the start function
1640 LEGACY_SERVICE_stop (struct LEGACY_SERVICE_Context *sctx)
1649 GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
1652 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
1659 GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1660 GNUNET_free (counter);
1664 if (NULL != sctx->shutdown_task)
1666 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1667 sctx->shutdown_task = NULL;
1669 if (NULL != sctx->server)
1670 GNUNET_SERVER_destroy (sctx->server);
1671 GNUNET_free_non_null (sctx->my_handlers);
1672 if (NULL != sctx->addrs)
1675 while (NULL != sctx->addrs[i])
1676 GNUNET_free (sctx->addrs[i++]);
1677 GNUNET_free (sctx->addrs);
1679 GNUNET_free_non_null (sctx->addrlens);
1680 GNUNET_free_non_null (sctx->v4_denied);
1681 GNUNET_free_non_null (sctx->v6_denied);
1682 GNUNET_free_non_null (sctx->v4_allowed);
1683 GNUNET_free_non_null (sctx->v6_allowed);
1688 /* end of service.c */