2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/service.c
23 * @brief functions related to starting services
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_directories.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_service_lib.h"
38 #define DEBUG_SERVICE GNUNET_NO
40 /* ******************* access control ******************** */
43 * @brief IPV4 network in CIDR notation.
47 struct in_addr network;
48 struct in_addr netmask;
52 * @brief network in CIDR notation for IPV6.
56 struct in6_addr network;
57 struct in6_addr netmask;
62 * Parse a network specification. The argument specifies
63 * a list of networks. The format is
64 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
65 * with a semicolon). The network must be given in dotted-decimal
66 * notation. The netmask can be given in CIDR notation (/16) or
67 * in dotted-decimal (/255.255.0.0).
69 * @param routeList a string specifying the forbidden networks
70 * @return the converted list, NULL if the synatx is flawed
72 static struct IPv4NetworkSet *
73 parse_ipv4_specification (const char *routeList)
81 unsigned int temps[8];
83 struct IPv4NetworkSet *result;
85 if (routeList == NULL)
87 len = strlen (routeList);
91 for (i = 0; i < len; i++)
92 if (routeList[i] == ';')
94 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
96 memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
101 cnt = sscanf (&routeList[pos],
102 "%u.%u.%u.%u/%u.%u.%u.%u;",
106 &temps[3], &temps[4], &temps[5], &temps[6], &temps[7]);
109 for (j = 0; j < 8; j++)
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113 _("Invalid format for IP: `%s'\n"),
115 GNUNET_free (result);
118 result[i].network.s_addr
120 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
122 result[i].netmask.s_addr =
123 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
125 while (routeList[pos] != ';')
131 /* try second notation */
132 cnt = sscanf (&routeList[pos],
134 &temps[0], &temps[1], &temps[2], &temps[3], &slash);
137 for (j = 0; j < 4; j++)
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 _("Invalid format for IP: `%s'\n"),
143 GNUNET_free (result);
146 result[i].network.s_addr
148 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
150 if ((slash <= 32) && (slash >= 0))
152 result[i].netmask.s_addr = 0;
155 result[i].netmask.s_addr
156 = (result[i].netmask.s_addr >> 1) + 0x80000000;
159 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
160 while (routeList[pos] != ';')
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
172 GNUNET_free (result);
173 return NULL; /* error */
176 /* try third notation */
178 cnt = sscanf (&routeList[pos],
180 &temps[0], &temps[1], &temps[2], &temps[3]);
183 for (j = 0; j < 4; j++)
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187 _("Invalid format for IP: `%s'\n"),
189 GNUNET_free (result);
192 result[i].network.s_addr
194 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
196 result[i].netmask.s_addr = 0;
199 result[i].netmask.s_addr
200 = (result[i].netmask.s_addr >> 1) + 0x80000000;
203 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
204 while (routeList[pos] != ';')
210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
212 GNUNET_free (result);
213 return NULL; /* error */
215 if (pos < strlen (routeList))
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
219 GNUNET_free (result);
220 return NULL; /* oops */
222 return result; /* ok */
227 * Parse a network specification. The argument specifies
228 * a list of networks. The format is
229 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
230 * with a semicolon). The network must be given in colon-hex
231 * notation. The netmask must be given in CIDR notation (/16) or
232 * can be omitted to specify a single host.
234 * @param routeListX a string specifying the forbidden networks
235 * @return the converted list, NULL if the synatx is flawed
237 static struct IPv6NetworkSet *
238 parse_ipv6_specification (const char *routeListX)
248 struct IPv6NetworkSet *result;
253 if (routeListX == NULL)
255 len = strlen (routeListX);
258 routeList = GNUNET_strdup (routeListX);
260 for (i = 0; i < len; i++)
261 if (routeList[i] == ';')
263 if (routeList[len - 1] != ';')
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 ("Invalid network notation (does not end with ';': `%s')\n"),
269 GNUNET_free (routeList);
273 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
274 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
280 while (routeList[pos] != ';')
283 while ((slash >= start) && (routeList[slash] != '/'))
287 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
292 routeList[pos] = '\0';
293 ret = inet_pton (AF_INET6,
294 &routeList[slash + 1], &result[i].netmask);
298 if ((1 != SSCANF (&routeList[slash + 1],
299 "%u", &bits)) || (bits >= 128))
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 _("Wrong format `%s' for netmask\n"),
304 &routeList[slash + 1]);
308 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
311 GNUNET_free (result);
312 GNUNET_free (routeList);
318 result[i].netmask.s6_addr[off++] = 0xFF;
323 result[i].netmask.s6_addr[off]
324 = (result[i].netmask.s6_addr[off] >> 1) + 0x80;
329 routeList[slash] = '\0';
330 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 _("Wrong format `%s' for network\n"),
336 &routeList[slash + 1]);
338 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "inet_pton");
339 GNUNET_free (result);
340 GNUNET_free (routeList);
346 GNUNET_free (routeList);
352 * Check if the given IP address is in the list of IP addresses.
354 * @param list a list of networks
355 * @param add the IP to check (in network byte order)
356 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
359 check_ipv4_listed (const struct IPv4NetworkSet *list,
360 const struct in_addr *add)
368 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
370 if ((add->s_addr & list[i].netmask.s_addr) ==
371 (list[i].network.s_addr & list[i].netmask.s_addr))
379 * Check if the given IP address is in the list of IP addresses.
381 * @param list a list of networks
382 * @param ip the IP to check (in network byte order)
383 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
386 check_ipv6_listed (const struct IPv6NetworkSet *list,
387 const struct in6_addr *ip)
391 struct in6_addr zero;
396 memset (&zero, 0, sizeof (struct in6_addr));
399 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
401 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
402 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
403 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
414 /* ****************** service struct ****************** */
418 * Context for "service_task".
420 struct GNUNET_SERVICE_Context
425 const struct GNUNET_CONFIGURATION_Handle *cfg;
428 * Handle for the server.
430 struct GNUNET_SERVER_Handle *server;
433 * Scheduler for the server.
435 struct GNUNET_SCHEDULER_Handle *sched;
438 * NULL-terminated array of addresses to bind to.
440 struct sockaddr **addrs;
443 * Name of our service.
445 const char *serviceName;
448 * Main service-specific task to run.
450 GNUNET_SERVICE_Main task;
458 * IPv4 addresses that are not allowed to connect.
460 struct IPv4NetworkSet *v4_denied;
463 * IPv6 addresses that are not allowed to connect.
465 struct IPv6NetworkSet *v6_denied;
468 * IPv4 addresses that are allowed to connect (if not
469 * set, all are allowed).
471 struct IPv4NetworkSet *v4_allowed;
474 * IPv6 addresses that are allowed to connect (if not
475 * set, all are allowed).
477 struct IPv6NetworkSet *v6_allowed;
480 * My (default) message handlers. Adjusted copy
483 struct GNUNET_SERVER_MessageHandler *my_handlers;
486 * Idle timeout for server.
488 struct GNUNET_TIME_Relative timeout;
491 * Maximum buffer size for the server.
496 * Overall success/failure of the service start.
501 * If we are daemonizing, this FD is set to the
502 * pipe to the parent. Send '.' if we started
503 * ok, '!' if not. -1 if we are not daemonizing.
505 int ready_confirm_fd;
508 * Do we close connections if we receive messages
509 * for which we have no handler?
514 * Can clients ask us to initiate a shutdown?
521 enum GNUNET_SERVICE_Options options;
524 * Array of the lengths of the entries in addrs.
526 socklen_t * addrlens;
531 /* ****************** message handlers ****************** */
534 write_test (void *cls, size_t size, void *buf)
536 struct GNUNET_SERVER_Client *client = cls;
537 struct GNUNET_MessageHeader *msg;
539 if (size < sizeof (struct GNUNET_MessageHeader))
541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
542 return 0; /* client disconnected */
544 msg = (struct GNUNET_MessageHeader *) buf;
545 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
546 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
548 return sizeof (struct GNUNET_MessageHeader);
552 * Handler for TEST message.
554 * @param cls closure (refers to service)
555 * @param client identification of the client
556 * @param message the actual message
559 handle_test (void *cls,
560 struct GNUNET_SERVER_Client *client,
561 const struct GNUNET_MessageHeader *message)
563 /* simply bounce message back to acknowledge */
564 if (NULL == GNUNET_SERVER_notify_transmit_ready (client,
566 GNUNET_MessageHeader),
567 GNUNET_TIME_UNIT_FOREVER_REL,
568 &write_test, client))
569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 * Default handlers for all services. Will be copied and the
575 * "callback_cls" fields will be replaced with the specific service
578 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
579 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
580 sizeof (struct GNUNET_MessageHeader)},
586 /* ****************** service core routines ************** */
590 * Check if access to the service is allowed from the given address.
593 check_access (void *cls, const struct sockaddr *addr, socklen_t addrlen)
595 struct GNUNET_SERVICE_Context *sctx = cls;
596 const struct sockaddr_in *i4;
597 const struct sockaddr_in6 *i6;
600 switch (addr->sa_family)
603 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
604 i4 = (const struct sockaddr_in *) addr;
605 ret = ((sctx->v4_allowed == NULL) ||
606 (check_ipv4_listed (sctx->v4_allowed,
608 && ((sctx->v4_denied == NULL) ||
609 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
612 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
613 i6 = (const struct sockaddr_in6 *) addr;
614 ret = ((sctx->v6_allowed == NULL) ||
615 (check_ipv6_listed (sctx->v6_allowed,
617 && ((sctx->v6_denied == NULL) ||
618 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
622 _("Unknown address family %d\n"), addr->sa_family);
623 return GNUNET_SYSERR;
625 if (ret != GNUNET_OK)
627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
628 _("Access from `%s' denied to service `%s'\n"),
629 GNUNET_a2s (addr, addrlen), sctx->serviceName);
636 * Get the name of the file where we will
637 * write the PID of the service.
640 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
646 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
655 * Parse an IPv4 access control list.
658 process_acl4 (struct IPv4NetworkSet **ret,
659 struct GNUNET_SERVICE_Context *sctx, const char *option)
663 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
665 GNUNET_break (GNUNET_OK ==
666 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
669 if (NULL == (*ret = parse_ipv4_specification (opt)))
671 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
673 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
674 opt, sctx->serviceName, option);
676 return GNUNET_SYSERR;
684 * Parse an IPv4 access control list.
687 process_acl6 (struct IPv6NetworkSet **ret,
688 struct GNUNET_SERVICE_Context *sctx, const char *option)
691 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
693 GNUNET_break (GNUNET_OK ==
694 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
697 if (NULL == (*ret = parse_ipv6_specification (opt)))
699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
701 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
702 opt, sctx->serviceName, option);
704 return GNUNET_SYSERR;
712 * Get the list of addresses that a server for the given service
715 * @param serviceName name of the service
716 * @param cfg configuration (which specifies the addresses)
717 * @param addrs set (call by reference) to an array of pointers to the
718 * addresses the server should bind to and listen on; the
719 * array will be NULL-terminated (on success)
720 * @param addr_lens set (call by reference) to an array of the lengths
721 * of the respective 'struct sockaddr' struct in the 'addrs'
723 * @return number of addresses found on success,
724 * GNUNET_SYSERR if the configuration
725 * did not specify reasonable finding information or
726 * if it specified a hostname that could not be resolved;
727 * GNUNET_NO if the number of addresses configured is
728 * zero (in this case, '*addrs' and '*addr_lens' will be
732 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
733 const struct GNUNET_CONFIGURATION_Handle *cfg,
734 struct sockaddr ***addrs,
735 socklen_t **addr_lens)
738 struct GNUNET_NETWORK_Handle *desc;
739 unsigned long long port;
740 struct addrinfo hints;
741 struct addrinfo *res;
742 struct addrinfo *pos;
743 struct addrinfo *next;
747 struct sockaddr **saddrs;
748 socklen_t *saddrlens;
753 if (GNUNET_CONFIGURATION_have_value (cfg,
754 serviceName, "DISABLEV6"))
757 (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
760 return GNUNET_SYSERR;
763 disablev6 = GNUNET_NO;
767 /* probe IPv6 support */
768 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
771 if ((errno == ENOBUFS) ||
772 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
774 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
775 return GNUNET_SYSERR;
777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
779 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
780 serviceName, STRERROR (errno));
781 disablev6 = GNUNET_YES;
785 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
791 GNUNET_CONFIGURATION_get_value_number (cfg,
794 &port)) || (port > 65535))
796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
798 ("Require valid port number for service `%s' in configuration!\n"),
800 return GNUNET_SYSERR;
802 if (GNUNET_CONFIGURATION_have_value (cfg,
803 serviceName, "BINDTO"))
805 GNUNET_break (GNUNET_OK ==
806 GNUNET_CONFIGURATION_get_value_string (cfg,
814 if (hostname != NULL)
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Resolving `%s' since that is where `%s' will bind to.\n",
822 memset (&hints, 0, sizeof (struct addrinfo));
824 hints.ai_family = AF_INET;
825 if ((0 != (ret = getaddrinfo (hostname,
826 NULL, &hints, &res))) || (res == NULL))
828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
829 _("Failed to resolve `%s': %s\n"),
830 hostname, gai_strerror (ret));
831 GNUNET_free (hostname);
832 return GNUNET_SYSERR;
836 while (NULL != (pos = next))
839 if ( (disablev6) && (pos->ai_family == AF_INET6))
845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
846 _("Failed to find %saddress for `%s'.\n"),
847 disablev6 ? "IPv4 " : "", hostname);
849 GNUNET_free (hostname);
850 return GNUNET_SYSERR;
853 saddrs = GNUNET_malloc ((i+1) * sizeof(struct sockaddr*));
854 saddrlens = GNUNET_malloc ((i+1) * sizeof (socklen_t));
857 while (NULL != (pos = next))
860 if ( (disablev6) && (pos->ai_family == AF_INET6))
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Service `%s' will bind to `%s'\n",
866 GNUNET_a2s (pos->ai_addr,
869 if (pos->ai_family == AF_INET)
871 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
872 saddrlens[i] = pos->ai_addrlen;
873 saddrs[i] = GNUNET_malloc (saddrlens[i]);
874 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
875 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
879 GNUNET_assert (pos->ai_family == AF_INET6);
880 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
881 saddrlens[i] = pos->ai_addrlen;
882 saddrs[i] = GNUNET_malloc (saddrlens[i]);
883 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
884 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
888 GNUNET_free (hostname);
893 /* will bind against everything, just set port */
898 saddrs = GNUNET_malloc (2 * sizeof(struct sockaddr*));
899 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
900 saddrlens[0] = sizeof (struct sockaddr_in);
901 saddrs[0] = GNUNET_malloc (saddrlens[0]);
902 #if HAVE_SOCKADDR_IN_SIN_LEN
903 ((struct sockaddr_in *) saddrs[0])->sin_len = saddrlens[0];
905 ((struct sockaddr_in *) saddrs[0])->sin_family = AF_INET;
906 ((struct sockaddr_in *) saddrs[0])->sin_port = htons (port);
912 saddrs = GNUNET_malloc (3 * sizeof(struct sockaddr*));
913 saddrlens = GNUNET_malloc (3 * sizeof (socklen_t));
915 saddrlens[0] = sizeof (struct sockaddr_in6);
916 saddrs[0] = GNUNET_malloc (saddrlens[0]);
917 #if HAVE_SOCKADDR_IN_SIN_LEN
918 ((struct sockaddr_in6 *) saddrs[0])->sin6_len = saddrlens[0];
920 ((struct sockaddr_in6 *) saddrs[0])->sin6_family = AF_INET6;
921 ((struct sockaddr_in6 *) saddrs[0])->sin6_port = htons (port);
923 saddrlens[1] = sizeof (struct sockaddr_in);
924 saddrs[1] = GNUNET_malloc (saddrlens[1]);
925 #if HAVE_SOCKADDR_IN_SIN_LEN
926 ((struct sockaddr_in *) saddrs[1])->sin_len = saddrlens[1];
928 ((struct sockaddr_in *) saddrs[1])->sin_family = AF_INET;
929 ((struct sockaddr_in *) saddrs[1])->sin_port = htons (port);
933 *addr_lens = saddrlens;
939 * Setup addr, addrlen, maxbuf, idle_timeout
940 * based on configuration!
942 * Configuration must specify a "PORT". It may
944 * - TIMEOUT (after how many ms does an inactive service timeout);
945 * - MAXBUF (maximum incoming message size supported)
946 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
947 * - ALLOW_SHUTDOWN (allow clients to shutdown this service)
948 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
949 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
950 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
951 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
952 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
954 * @return GNUNET_OK if configuration succeeded
957 setup_service (struct GNUNET_SERVICE_Context *sctx)
959 unsigned long long maxbuf;
960 struct GNUNET_TIME_Relative idleout;
963 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
964 sctx->serviceName, "TIMEOUT"))
967 GNUNET_CONFIGURATION_get_value_time (sctx->cfg,
969 "TIMEOUT", &idleout))
971 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
972 _("Specified value for `%s' of service `%s' is invalid\n"),
975 return GNUNET_SYSERR;
977 sctx->timeout = idleout;
980 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
981 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
982 sctx->serviceName, "MAXBUF"))
985 GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
990 _("Specified value for `%s' of service `%s' is invalid\n"),
993 return GNUNET_SYSERR;
997 maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
998 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
999 sctx->serviceName, "ALLOW_SHUTDOWN"))
1001 if (GNUNET_SYSERR ==
1002 (sctx->allow_shutdown =
1003 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1007 _("Specified value for `%s' of service `%s' is invalid\n"),
1010 return GNUNET_SYSERR;
1014 sctx->allow_shutdown = GNUNET_NO;
1017 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1018 sctx->serviceName, "TOLERANT"))
1020 if (GNUNET_SYSERR ==
1021 (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1026 _("Specified value for `%s' of service `%s' is invalid\n"),
1029 return GNUNET_SYSERR;
1033 tolerant = GNUNET_NO;
1035 if (GNUNET_SYSERR ==
1036 GNUNET_SERVICE_get_server_addresses (sctx->serviceName,
1040 return GNUNET_SYSERR;
1041 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1042 sctx->maxbuf = (size_t) maxbuf;
1043 if (sctx->maxbuf != maxbuf)
1045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1047 ("Value in configuration for `%s' and service `%s' too large!\n"),
1048 "MAXBUF", sctx->serviceName);
1049 return GNUNET_SYSERR;
1052 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1053 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1054 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1055 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1062 * Get the name of the user that'll be used
1063 * to provide the service.
1066 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1072 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
1083 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1091 if (NULL == (pif = get_pid_file_name (sctx)))
1092 return GNUNET_OK; /* no file desired */
1093 user = get_user_name (sctx);
1094 rdir = GNUNET_strdup (pif);
1095 len = strlen (rdir);
1096 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1099 if (0 != ACCESS (rdir, F_OK))
1101 /* we get to create a directory -- and claim it
1103 GNUNET_DISK_directory_create (rdir);
1104 if ((user != NULL) && (0 < strlen (user)))
1105 GNUNET_DISK_file_change_owner (rdir, user);
1107 if (0 != ACCESS (rdir, W_OK | X_OK))
1109 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1111 GNUNET_free_non_null (user);
1113 return GNUNET_SYSERR;
1116 pidfd = FOPEN (pif, "w");
1119 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1121 GNUNET_free_non_null (user);
1122 return GNUNET_SYSERR;
1124 if (0 > FPRINTF (pidfd, "%u", pid))
1125 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1126 GNUNET_break (0 == fclose (pidfd));
1127 if ((user != NULL) && (0 < strlen (user)))
1128 GNUNET_DISK_file_change_owner (pif, user);
1129 GNUNET_free_non_null (user);
1136 * Task run during shutdown.
1142 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1144 struct GNUNET_SERVER_Handle *server = cls;
1146 GNUNET_SERVER_destroy (server);
1151 * Initial task for the service.
1154 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1156 struct GNUNET_SERVICE_Context *sctx = cls;
1159 sctx->sched = tc->sched;
1160 sctx->server = GNUNET_SERVER_create (tc->sched,
1166 sctx->timeout, sctx->require_found);
1167 if (sctx->server == NULL)
1170 while (sctx->addrs[i] != NULL)
1172 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1173 _("Failed to start `%s' at `%s'\n"),
1175 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1178 sctx->ret = GNUNET_SYSERR;
1181 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1183 /* install a task that will kill the server
1184 process if the scheduler ever gets a shutdown signal */
1185 GNUNET_SCHEDULER_add_delayed (tc->sched,
1186 GNUNET_TIME_UNIT_FOREVER_REL,
1187 &shutdown_task, sctx->server);
1189 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1190 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1192 while ((sctx->my_handlers[i].callback != NULL))
1193 sctx->my_handlers[i++].callback_cls = sctx;
1194 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1195 if (sctx->ready_confirm_fd != -1)
1197 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1198 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1199 sctx->ready_confirm_fd = -1;
1200 write_pid_file (sctx, getpid ());
1203 while (sctx->addrs[i] != NULL)
1205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1206 _("Service `%s' runs at %s\n"),
1208 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1211 sctx->task (sctx->task_cls, tc->sched, sctx->server, sctx->cfg);
1216 * Detach from terminal.
1219 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1226 if (0 != PIPE (filedes))
1228 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
1229 return GNUNET_SYSERR;
1234 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
1235 return GNUNET_SYSERR;
1242 GNUNET_break (0 == CLOSE (filedes[1]));
1244 if (1 != READ (filedes[0], &c, sizeof (char)))
1245 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1253 _("Service process failed to initialize\n"));
1256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1258 ("Service process could not initialize server function\n"));
1261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1262 _("Service process failed to report status\n"));
1265 exit (1); /* child reported error */
1267 GNUNET_break (0 == CLOSE (0));
1268 GNUNET_break (0 == CLOSE (1));
1269 GNUNET_break (0 == CLOSE (filedes[0]));
1270 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1272 return GNUNET_SYSERR;
1273 /* set stdin/stdout to /dev/null */
1274 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1276 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
1277 return GNUNET_SYSERR;
1279 /* Detach from controlling terminal */
1282 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid");
1283 sctx->ready_confirm_fd = filedes[1];
1285 /* FIXME: we probably need to do something else
1286 elsewhere in order to fork the process itself... */
1297 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1301 if (NULL == (user = get_user_name (sctx)))
1302 return GNUNET_OK; /* keep */
1307 pws = getpwnam (user);
1310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1311 _("Cannot obtain information about user `%s': %s\n"),
1312 user, errno == 0 ? _("No such user") : STRERROR (errno));
1314 return GNUNET_SYSERR;
1316 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1318 (0 != initgroups (user, pws->pw_gid)) ||
1320 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1322 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1323 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1326 _("Cannot change user/group to `%s': %s\n"), user,
1329 return GNUNET_SYSERR;
1339 * Delete the PID file that was created by our parent.
1342 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1344 char *pif = get_pid_file_name (sctx);
1346 return; /* no PID file */
1347 if (0 != UNLINK (pif))
1348 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1354 * Run a standard GNUnet service startup sequence (initialize loggers
1355 * and configuration, parse options).
1357 * @param argc number of command line arguments
1358 * @param argv command line arguments
1359 * @param serviceName our service name
1360 * @param opt service options
1361 * @param task main task of the service
1362 * @param task_cls closure for task
1363 * @return GNUNET_SYSERR on error, GNUNET_OK
1364 * if we shutdown nicely
1367 GNUNET_SERVICE_run (int argc,
1369 const char *serviceName,
1370 enum GNUNET_SERVICE_Options opt,
1371 GNUNET_SERVICE_Main task, void *task_cls)
1373 #define HANDLE_ERROR do { err = 1; GNUNET_break (0); goto shutdown; } while (0)
1381 struct GNUNET_SERVICE_Context sctx;
1382 struct GNUNET_CONFIGURATION_Handle *cfg;
1383 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1384 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1385 {'d', "daemonize", NULL,
1386 gettext_noop ("do daemonize (detach from terminal)"), 0,
1387 GNUNET_GETOPT_set_one, &do_daemonize},
1388 GNUNET_GETOPT_OPTION_HELP (serviceName),
1389 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1390 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1391 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1392 GNUNET_GETOPT_OPTION_END
1397 loglev = GNUNET_strdup ("WARNING");
1398 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1399 memset (&sctx, 0, sizeof (sctx));
1401 sctx.ready_confirm_fd = -1;
1402 sctx.ret = GNUNET_OK;
1403 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1404 sctx.maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1406 sctx.serviceName = serviceName;
1407 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1408 /* setup subsystems */
1409 if (GNUNET_SYSERR == GNUNET_GETOPT_run (serviceName, service_options, argc,
1412 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1414 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1416 if (GNUNET_OK != setup_service (&sctx))
1418 if ( (do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx)))
1420 if (GNUNET_OK != set_user_id (&sctx))
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Service `%s' runs with configuration from `%s'\n",
1425 serviceName, cfg_fn);
1427 /* actually run service */
1428 GNUNET_SCHEDULER_run (&service_task, &sctx);
1431 if ((do_daemonize == 1) && (sctx.server != NULL))
1432 pid_file_delete (&sctx);
1433 GNUNET_free_non_null (sctx.my_handlers);
1436 if (sctx.ready_confirm_fd != -1)
1438 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1439 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1440 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1443 GNUNET_CONFIGURATION_destroy (cfg);
1445 if (sctx.addrs != NULL)
1446 while (sctx.addrs[i] != NULL)
1447 GNUNET_free (sctx.addrs[i++]);
1448 GNUNET_free_non_null (sctx.addrs);
1449 GNUNET_free_non_null (sctx.addrlens);
1450 GNUNET_free_non_null (logfile);
1451 GNUNET_free (loglev);
1452 GNUNET_free (cfg_fn);
1453 GNUNET_free_non_null (sctx.v4_denied);
1454 GNUNET_free_non_null (sctx.v6_denied);
1455 GNUNET_free_non_null (sctx.v4_allowed);
1456 GNUNET_free_non_null (sctx.v6_allowed);
1458 return err ? GNUNET_SYSERR : sctx.ret;
1463 * Run a service startup sequence within an existing
1464 * initialized system.
1466 * @param serviceName our service name
1467 * @param sched scheduler to use
1468 * @param cfg configuration to use
1469 * @return NULL on error, service handle
1471 struct GNUNET_SERVICE_Context *
1472 GNUNET_SERVICE_start (const char *serviceName,
1473 struct GNUNET_SCHEDULER_Handle *sched,
1474 const struct GNUNET_CONFIGURATION_Handle *cfg)
1477 struct GNUNET_SERVICE_Context *sctx;
1479 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1480 sctx->ready_confirm_fd = -1; /* no daemonizing */
1481 sctx->ret = GNUNET_OK;
1482 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1483 sctx->maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1484 sctx->serviceName = serviceName;
1486 sctx->sched = sched;
1488 /* setup subsystems */
1489 if ((GNUNET_OK != setup_service (sctx)) ||
1490 (NULL == (sctx->server = GNUNET_SERVER_create (sched,
1497 sctx->require_found))))
1499 GNUNET_SERVICE_stop (sctx);
1502 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1503 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1505 while ((sctx->my_handlers[i].callback != NULL))
1506 sctx->my_handlers[i++].callback_cls = sctx;
1507 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1512 * Obtain the server used by a service. Note that the server must NOT
1513 * be destroyed by the caller.
1515 * @param ctx the service context returned from the start function
1516 * @return handle to the server for this service, NULL if there is none
1518 struct GNUNET_SERVER_Handle *
1519 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1526 * Stop a service that was started with "GNUNET_SERVICE_start".
1528 * @param sctx the service context returned from the start function
1531 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1534 if (NULL != sctx->server)
1535 GNUNET_SERVER_destroy (sctx->server);
1536 GNUNET_free_non_null (sctx->my_handlers);
1538 while (sctx->addrs[i] != NULL)
1539 GNUNET_free (sctx->addrs[i++]);
1540 GNUNET_free_non_null (sctx->addrs);
1541 GNUNET_free_non_null (sctx->addrlens);
1542 GNUNET_free_non_null (sctx->v4_denied);
1543 GNUNET_free_non_null (sctx->v6_denied);
1544 GNUNET_free_non_null (sctx->v4_allowed);
1545 GNUNET_free_non_null (sctx->v6_allowed);
1550 /* end of service.c */