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 /* ******************* access control ******************** */
41 * @brief IPV4 network in CIDR notation.
45 struct in_addr network;
46 struct in_addr netmask;
50 * @brief network in CIDR notation for IPV6.
54 struct in6_addr network;
55 struct in6_addr netmask;
60 * Parse a network specification. The argument specifies
61 * a list of networks. The format is
62 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
63 * with a semicolon). The network must be given in dotted-decimal
64 * notation. The netmask can be given in CIDR notation (/16) or
65 * in dotted-decimal (/255.255.0.0).
67 * @param routeList a string specifying the forbidden networks
68 * @return the converted list, NULL if the synatx is flawed
70 static struct IPv4NetworkSet *
71 parse_ipv4_specification (const char *routeList)
79 unsigned int temps[8];
81 struct IPv4NetworkSet *result;
83 if (routeList == NULL)
85 len = strlen (routeList);
89 for (i = 0; i < len; i++)
90 if (routeList[i] == ';')
92 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
94 memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
99 cnt = sscanf (&routeList[pos],
100 "%u.%u.%u.%u/%u.%u.%u.%u;",
104 &temps[3], &temps[4], &temps[5], &temps[6], &temps[7]);
107 for (j = 0; j < 8; j++)
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 _("Invalid format for IP: `%s'\n"),
113 GNUNET_free (result);
116 result[i].network.s_addr
118 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
120 result[i].netmask.s_addr =
121 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
123 while (routeList[pos] != ';')
129 /* try second notation */
130 cnt = sscanf (&routeList[pos],
132 &temps[0], &temps[1], &temps[2], &temps[3], &slash);
135 for (j = 0; j < 4; j++)
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139 _("Invalid format for IP: `%s'\n"),
141 GNUNET_free (result);
144 result[i].network.s_addr
146 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
148 if ((slash <= 32) && (slash >= 0))
150 result[i].netmask.s_addr = 0;
153 result[i].netmask.s_addr
154 = (result[i].netmask.s_addr >> 1) + 0x80000000;
157 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
158 while (routeList[pos] != ';')
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
170 GNUNET_free (result);
171 return NULL; /* error */
174 /* try third notation */
176 cnt = sscanf (&routeList[pos],
178 &temps[0], &temps[1], &temps[2], &temps[3]);
181 for (j = 0; j < 4; j++)
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 _("Invalid format for IP: `%s'\n"),
187 GNUNET_free (result);
190 result[i].network.s_addr
192 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
194 result[i].netmask.s_addr = 0;
197 result[i].netmask.s_addr
198 = (result[i].netmask.s_addr >> 1) + 0x80000000;
201 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
202 while (routeList[pos] != ';')
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
210 GNUNET_free (result);
211 return NULL; /* error */
213 if (pos < strlen (routeList))
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
217 GNUNET_free (result);
218 return NULL; /* oops */
220 return result; /* ok */
225 * Parse a network specification. The argument specifies
226 * a list of networks. The format is
227 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
228 * with a semicolon). The network must be given in colon-hex
229 * notation. The netmask must be given in CIDR notation (/16) or
230 * can be omitted to specify a single host.
232 * @param routeList a string specifying the forbidden networks
233 * @return the converted list, NULL if the synatx is flawed
235 static struct IPv6NetworkSet *
236 parse_ipv6_specification (const char *routeListX)
246 struct IPv6NetworkSet *result;
251 if (routeListX == NULL)
253 len = strlen (routeListX);
256 routeList = GNUNET_strdup (routeListX);
258 for (i = 0; i < len; i++)
259 if (routeList[i] == ';')
261 if (routeList[len - 1] != ';')
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265 ("Invalid network notation (does not end with ';': `%s')\n"),
267 GNUNET_free (routeList);
271 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
272 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
278 while (routeList[pos] != ';')
281 while ((slash >= start) && (routeList[slash] != '/'))
285 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
290 routeList[pos] = '\0';
291 ret = inet_pton (AF_INET6,
292 &routeList[slash + 1], &result[i].netmask);
296 if ((1 != SSCANF (&routeList[slash + 1],
297 "%u", &bits)) || (bits >= 128))
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
301 _("Wrong format `%s' for netmask\n"),
302 &routeList[slash + 1]);
306 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
309 GNUNET_free (result);
310 GNUNET_free (routeList);
316 result[i].netmask.s6_addr[off++] = 0xFF;
321 result[i].netmask.s6_addr[off]
322 = (result[i].netmask.s6_addr[off] >> 1) + 0x80;
327 routeList[slash] = '\0';
328 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333 _("Wrong format `%s' for network\n"),
334 &routeList[slash + 1]);
336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
337 GNUNET_free (result);
338 GNUNET_free (routeList);
344 GNUNET_free (routeList);
350 * Check if the given IP address is in the list of IP addresses.
352 * @param list a list of networks
353 * @param ip the IP to check (in network byte order)
354 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
357 check_ipv4_listed (const struct IPv4NetworkSet *list,
358 const struct in_addr *add)
366 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
368 if ((add->s_addr & list[i].netmask.s_addr) ==
369 (list[i].network.s_addr & list[i].netmask.s_addr))
377 * Check if the given IP address is in the list of IP addresses.
379 * @param list a list of networks
380 * @param ip the IP to check (in network byte order)
381 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
384 check_ipv6_listed (const struct IPv6NetworkSet *list,
385 const struct in6_addr *ip)
389 struct in6_addr zero;
394 memset (&zero, 0, sizeof (struct in6_addr));
397 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
399 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
400 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
401 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
412 /* ****************** service struct ****************** */
416 * Context for "service_task".
418 struct GNUNET_SERVICE_Context
423 const struct GNUNET_CONFIGURATION_Handle *cfg;
426 * Handle for the server.
428 struct GNUNET_SERVER_Handle *server;
431 * Scheduler for the server.
433 struct GNUNET_SCHEDULER_Handle *sched;
436 * Address to bind to.
438 struct sockaddr *addr;
441 * Name of our service.
443 const char *serviceName;
446 * Main service-specific task to run.
448 GNUNET_SERVICE_Main task;
456 * IPv4 addresses that are not allowed to connect.
458 struct IPv4NetworkSet *v4_denied;
461 * IPv6 addresses that are not allowed to connect.
463 struct IPv6NetworkSet *v6_denied;
466 * IPv4 addresses that are allowed to connect (if not
467 * set, all are allowed).
469 struct IPv4NetworkSet *v4_allowed;
472 * IPv6 addresses that are allowed to connect (if not
473 * set, all are allowed).
475 struct IPv6NetworkSet *v6_allowed;
478 * My (default) message handlers. Adjusted copy
481 struct GNUNET_SERVER_MessageHandler *my_handlers;
484 * Idle timeout for server.
486 struct GNUNET_TIME_Relative timeout;
489 * Maximum buffer size for the server.
494 * Overall success/failure of the service start.
499 * If we are daemonizing, this FD is set to the
500 * pipe to the parent. Send '.' if we started
501 * ok, '!' if not. -1 if we are not daemonizing.
503 int ready_confirm_fd;
506 * Do we close connections if we receive messages
507 * for which we have no handler?
512 * Can clients ask us to initiate a shutdown?
524 /* ****************** message handlers ****************** */
527 write_test (void *cls, size_t size, void *buf)
529 struct GNUNET_SERVER_Client *client = cls;
530 struct GNUNET_MessageHeader *msg;
532 if (size < sizeof (struct GNUNET_MessageHeader))
534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
535 return 0; /* client disconnected */
537 msg = (struct GNUNET_MessageHeader *) buf;
538 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
539 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
541 return sizeof (struct GNUNET_MessageHeader);
545 * Handler for TEST message.
547 * @param cls closure (refers to service)
548 * @param client identification of the client
549 * @param message the actual message
552 handle_test (void *cls,
553 struct GNUNET_SERVER_Client *client,
554 const struct GNUNET_MessageHeader *message)
556 /* simply bounce message back to acknowledge */
557 if (NULL == GNUNET_SERVER_notify_transmit_ready (client,
559 GNUNET_MessageHeader),
560 GNUNET_TIME_UNIT_FOREVER_REL,
561 &write_test, client))
562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
567 * Handler for SHUTDOWN message.
569 * @param cls closure (refers to service)
570 * @param client identification of the client
571 * @param message the actual message
574 handle_shutdown (void *cls,
575 struct GNUNET_SERVER_Client *client,
576 const struct GNUNET_MessageHeader *message)
578 struct GNUNET_SERVICE_Context *service = cls;
579 if (!service->allow_shutdown)
581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
583 ("Received shutdown request, but configured to ignore!\n"));
584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
587 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
588 _("Initiating shutdown as requested by client.\n"));
589 GNUNET_assert (service->sched != NULL);
590 GNUNET_SCHEDULER_shutdown (service->sched);
591 GNUNET_SERVER_receive_done (client, GNUNET_OK);
596 * Default handlers for all services. Will be copied and the
597 * "callback_cls" fields will be replaced with the specific service
600 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
601 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
602 sizeof (struct GNUNET_MessageHeader)},
603 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_SHUTDOWN,
604 sizeof (struct GNUNET_MessageHeader)},
610 /* ****************** service core routines ************** */
614 * Check if access to the service is allowed from the given address.
617 check_access (void *cls, const struct sockaddr *addr, socklen_t addrlen)
619 struct GNUNET_SERVICE_Context *sctx = cls;
620 const struct sockaddr_in *i4;
621 const struct sockaddr_in6 *i6;
624 switch (addr->sa_family)
627 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
628 i4 = (const struct sockaddr_in *) addr;
629 ret = ((sctx->v4_allowed == NULL) ||
630 (check_ipv4_listed (sctx->v4_allowed,
632 && ((sctx->v4_denied == NULL) ||
633 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
636 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
637 i6 = (const struct sockaddr_in6 *) addr;
638 ret = ((sctx->v6_allowed == NULL) ||
639 (check_ipv6_listed (sctx->v6_allowed,
641 && ((sctx->v6_denied == NULL) ||
642 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 _("Unknown address family %d\n"), addr->sa_family);
647 return GNUNET_SYSERR;
649 if (ret != GNUNET_OK)
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
652 _("Access from `%s' denied to service `%s'\n"),
653 GNUNET_a2s(addr, addrlen), sctx->serviceName);
660 * Get the name of the file where we will
661 * write the PID of the service.
664 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
670 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
679 * Parse an IPv4 access control list.
682 process_acl4 (struct IPv4NetworkSet **ret,
683 struct GNUNET_SERVICE_Context *sctx, const char *option)
687 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
689 GNUNET_break (GNUNET_OK ==
690 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
693 if (NULL == (*ret = parse_ipv4_specification (opt)))
695 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
698 opt, sctx->serviceName, option);
700 return GNUNET_SYSERR;
708 * Parse an IPv4 access control list.
711 process_acl6 (struct IPv6NetworkSet **ret,
712 struct GNUNET_SERVICE_Context *sctx, const char *option)
715 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
717 GNUNET_break (GNUNET_OK ==
718 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
721 if (NULL == (*ret = parse_ipv6_specification (opt)))
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
725 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
726 opt, sctx->serviceName, option);
728 return GNUNET_SYSERR;
736 * Setup addr, addrlen, maxbuf, idle_timeout
737 * based on configuration!
739 * Configuration must specify a "PORT". It may
741 * - TIMEOUT (after how many ms does an inactive service timeout);
742 * - MAXBUF (maximum incoming message size supported)
743 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
744 * - ALLOW_SHUTDOWN (allow clients to shutdown this service)
745 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
746 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
747 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
748 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
749 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
751 * @return GNUNET_OK if configuration succeeded
754 setup_service (struct GNUNET_SERVICE_Context *sctx)
756 unsigned long long maxbuf;
757 struct GNUNET_TIME_Relative idleout;
759 unsigned long long port;
761 struct addrinfo hints;
762 struct addrinfo *res;
763 struct addrinfo *pos;
767 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
768 sctx->serviceName, "TIMEOUT"))
771 GNUNET_CONFIGURATION_get_value_time (sctx->cfg,
773 "TIMEOUT", &idleout))
774 return GNUNET_SYSERR;
776 sctx->timeout = idleout;
779 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
780 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
781 sctx->serviceName, "MAXBUF"))
784 GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
787 return GNUNET_SYSERR;
790 maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
791 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
792 sctx->serviceName, "DISABLEV6"))
795 (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
798 return GNUNET_SYSERR;
801 disablev6 = GNUNET_NO;
802 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
803 sctx->serviceName, "ALLOW_SHUTDOWN"))
806 (sctx->allow_shutdown =
807 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
809 return GNUNET_SYSERR;
812 sctx->allow_shutdown = GNUNET_NO;
816 /* probe IPv6 support */
817 ret = SOCKET (PF_INET6, SOCK_STREAM, 0);
820 if ((errno == ENOBUFS) ||
821 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
823 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
824 return GNUNET_SYSERR;
826 ret = SOCKET (PF_INET, SOCK_STREAM, 0);
829 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
831 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
832 sctx->serviceName, strerror (errno));
833 disablev6 = GNUNET_YES;
837 GNUNET_break (0 == CLOSE (ret));
842 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
843 sctx->serviceName, "TOLERANT"))
846 (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
849 return GNUNET_SYSERR;
852 tolerant = GNUNET_NO;
853 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
857 GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
860 &port)) || (port > 65535))
862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
864 ("Require valid port number for service `%s' in configuration!\n"),
866 return GNUNET_SYSERR;
868 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
869 sctx->serviceName, "BINDTO"))
871 GNUNET_break (GNUNET_OK ==
872 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
880 if (hostname != NULL)
882 memset (&hints, 0, sizeof (struct addrinfo));
884 hints.ai_family = AF_INET;
885 if ((0 != (ret = getaddrinfo (hostname,
886 NULL, &hints, &res))) || (res == NULL))
888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
889 _("Failed to resolve `%s': %s\n"),
890 hostname, gai_strerror (ret));
891 GNUNET_free (hostname);
892 return GNUNET_SYSERR;
895 while ((NULL != pos) &&
897 (pos->ai_family != AF_INET)) ||
898 ((pos->ai_family != AF_INET) && (pos->ai_family != AF_INET6))))
902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
903 _("Failed to find %saddress for `%s'.\n"),
904 disablev6 ? "IPv4 " : "", hostname);
906 GNUNET_free (hostname);
907 return GNUNET_SYSERR;
909 GNUNET_free (hostname);
910 if (pos->ai_family == AF_INET)
912 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
913 sctx->addrlen = pos->ai_addrlen;
914 sctx->addr = GNUNET_malloc (sctx->addrlen);
915 memcpy (sctx->addr, res->ai_addr, sctx->addrlen);
916 ((struct sockaddr_in *) sctx->addr)->sin_port = htons (port);
917 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
919 ("Configured to bind to %s address; %s connections to this service will fail!\n"),
924 GNUNET_assert (pos->ai_family == AF_INET6);
925 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
926 sctx->addrlen = pos->ai_addrlen;
927 sctx->addr = GNUNET_malloc (sctx->addrlen);
928 memcpy (sctx->addr, res->ai_addr, sctx->addrlen);
929 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
931 ("Configured to bind to %s address; %s connections to this service will fail!\n"),
933 ((struct sockaddr_in6 *) sctx->addr)->sin6_port = htons (port);
939 /* will bind against everything, just set port */
943 sctx->addrlen = sizeof (struct sockaddr_in6);
944 sctx->addr = GNUNET_malloc (sctx->addrlen);
945 ((struct sockaddr_in *) sctx->addr)->sin_family = AF_INET;
946 ((struct sockaddr_in *) sctx->addr)->sin_port = htons (port);
947 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
949 ("Configured to bind to %s address; %s connections to this service will fail!\n"),
955 sctx->addrlen = sizeof (struct sockaddr_in6);
956 sctx->addr = GNUNET_malloc (sctx->addrlen);
957 ((struct sockaddr_in6 *) sctx->addr)->sin6_family = AF_INET6;
958 ((struct sockaddr_in6 *) sctx->addr)->sin6_port = htons (port);
961 sctx->maxbuf = (size_t) maxbuf;
962 if (sctx->maxbuf != maxbuf)
964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
966 ("Value in configuration for `%s' and service `%s' too large!\n"),
967 "MAXBUF", sctx->serviceName);
968 return GNUNET_SYSERR;
973 process_acl4 (&sctx->v4_denied,
977 process_acl4 (&sctx->v4_allowed,
981 process_acl6 (&sctx->v6_denied,
984 (GNUNET_OK != process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6")))
985 return GNUNET_SYSERR;
991 * Get the name of the user that'll be used
992 * to provide the service.
995 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1001 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
1012 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1020 if (NULL == (pif = get_pid_file_name (sctx)))
1021 return GNUNET_OK; /* no file desired */
1022 user = get_user_name (sctx);
1023 rdir = GNUNET_strdup (pif);
1024 len = strlen (rdir);
1025 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1028 if (0 != ACCESS (rdir, F_OK))
1030 /* we get to create a directory -- and claim it
1032 GNUNET_DISK_directory_create (rdir);
1033 if ((user != NULL) && (0 < strlen (user)))
1034 GNUNET_DISK_file_change_owner (rdir, user);
1036 if (0 != ACCESS (rdir, W_OK | X_OK))
1038 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1040 GNUNET_free_non_null (user);
1042 return GNUNET_SYSERR;
1045 pidfd = FOPEN (pif, "w");
1048 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1050 GNUNET_free_non_null (user);
1051 return GNUNET_SYSERR;
1053 if (0 > FPRINTF (pidfd, "%u", pid))
1054 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1055 GNUNET_break (0 == fclose (pidfd));
1056 if ((user != NULL) && (0 < strlen (user)))
1057 GNUNET_DISK_file_change_owner (pif, user);
1058 GNUNET_free_non_null (user);
1065 * Initial task for the service.
1068 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 struct GNUNET_SERVICE_Context *sctx = cls;
1073 sctx->sched = tc->sched;
1074 sctx->server = GNUNET_SERVER_create (tc->sched,
1080 sctx->timeout, sctx->require_found);
1081 if (sctx->server == NULL)
1083 sctx->ret = GNUNET_SYSERR;
1086 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1087 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1089 while ((sctx->my_handlers[i].callback != NULL))
1090 sctx->my_handlers[i++].callback_cls = sctx;
1091 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1092 if (sctx->ready_confirm_fd != -1)
1094 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1095 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1096 sctx->ready_confirm_fd = -1;
1097 write_pid_file (sctx, getpid ());
1100 sctx->task (sctx->task_cls, tc->sched, sctx->server, sctx->cfg);
1105 * Detach from terminal.
1108 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1115 if (0 != PIPE (filedes))
1117 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
1118 return GNUNET_SYSERR;
1123 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
1124 return GNUNET_SYSERR;
1131 GNUNET_break (0 == CLOSE (filedes[1]));
1133 if (1 != READ (filedes[0], &c, sizeof (char)))
1134 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1141 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1142 _("Service process failed to initialize\n"));
1145 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1147 ("Service process could not initialize server function\n"));
1150 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1151 _("Service process failed to report status\n"));
1154 exit (1); /* child reported error */
1156 GNUNET_break (0 == CLOSE (0));
1157 GNUNET_break (0 == CLOSE (1));
1158 GNUNET_break (0 == CLOSE (filedes[0]));
1159 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1161 return GNUNET_SYSERR;
1162 /* set stdin/stdout to /dev/null */
1163 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1165 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
1166 return GNUNET_SYSERR;
1168 /* Detach from controlling terminal */
1171 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid");
1172 sctx->ready_confirm_fd = filedes[1];
1174 /* FIXME: we probably need to do something else
1175 elsewhere in order to fork the process itself... */
1186 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1190 if (NULL == (user = get_user_name (sctx)))
1191 return GNUNET_OK; /* keep */
1196 pws = getpwnam (user);
1199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1200 _("Cannot obtain information about user `%s': %s\n"),
1201 user, errno == 0 ? _("No such user") : STRERROR (errno));
1203 return GNUNET_SYSERR;
1205 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1207 (0 != initgroups (user, pws->pw_gid)) ||
1209 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1211 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1212 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1215 _("Cannot change user/group to `%s': %s\n"), user,
1218 return GNUNET_SYSERR;
1228 * Delete the PID file that was created by our parent.
1231 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1233 char *pif = get_pid_file_name (sctx);
1235 return; /* no PID file */
1236 if (0 != UNLINK (pif))
1237 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1242 * Run a standard GNUnet service startup sequence (initialize loggers
1243 * and configuration, parse options).
1245 * @param argc number of command line arguments
1246 * @param argv command line arguments
1247 * @param serviceName our service name
1248 * @param task main task of the service
1249 * @param task_cls closure for task
1250 * @param term termination task of the service
1251 * @param term_cls closure for term
1252 * @return GNUNET_SYSERR on error, GNUNET_OK
1253 * if we shutdown nicely
1256 GNUNET_SERVICE_run (int argc,
1258 const char *serviceName,
1259 GNUNET_SERVICE_Main task,
1260 void *task_cls, GNUNET_SERVICE_Term term, void *term_cls)
1266 struct GNUNET_SERVICE_Context sctx;
1267 struct GNUNET_CONFIGURATION_Handle *cfg;
1268 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1269 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1270 {'d', "daemonize", NULL,
1271 gettext_noop ("do daemonize (detach from terminal)"), 0,
1272 GNUNET_GETOPT_set_one, &do_daemonize},
1273 GNUNET_GETOPT_OPTION_HELP (serviceName),
1274 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1275 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1276 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1277 GNUNET_GETOPT_OPTION_END
1281 loglev = GNUNET_strdup ("WARNING");
1282 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_DAEMON_CONFIG_FILE);
1283 memset (&sctx, 0, sizeof (sctx));
1284 sctx.ready_confirm_fd = -1;
1285 sctx.ret = GNUNET_OK;
1286 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1287 sctx.maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1289 sctx.serviceName = serviceName;
1290 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1291 /* setup subsystems */
1292 if ((GNUNET_SYSERR ==
1293 GNUNET_GETOPT_run (serviceName,
1298 GNUNET_log_setup (serviceName, loglev, logfile)) ||
1300 GNUNET_CONFIGURATION_load (cfg, cfg_fn)) ||
1302 setup_service (&sctx)) ||
1303 ((do_daemonize == 1) &&
1304 (GNUNET_OK != detach_terminal (&sctx))) ||
1305 (GNUNET_OK != set_user_id (&sctx)))
1307 if (sctx.ready_confirm_fd != -1)
1309 if (1 != WRITE (sctx.ready_confirm_fd, "I", 1))
1310 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1311 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1313 GNUNET_CONFIGURATION_destroy (cfg);
1314 GNUNET_free_non_null (sctx.addr);
1315 GNUNET_free_non_null (logfile);
1316 GNUNET_free (loglev);
1317 GNUNET_free (cfg_fn);
1318 GNUNET_free_non_null (sctx.v4_denied);
1319 GNUNET_free_non_null (sctx.v6_denied);
1320 GNUNET_free_non_null (sctx.v4_allowed);
1321 GNUNET_free_non_null (sctx.v6_allowed);
1322 return GNUNET_SYSERR;
1325 /* actually run service */
1326 GNUNET_SCHEDULER_run (&service_task, &sctx);
1327 if (sctx.ready_confirm_fd != -1)
1329 if (1 != WRITE (sctx.ready_confirm_fd, "S", 1))
1330 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1331 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1336 term (term_cls, sctx.cfg);
1337 if ((do_daemonize == 1) && (sctx.server != NULL))
1338 pid_file_delete (&sctx);
1339 if (sctx.server != NULL)
1340 GNUNET_SERVER_destroy (sctx.server);
1341 GNUNET_free_non_null (sctx.my_handlers);
1342 GNUNET_CONFIGURATION_destroy (cfg);
1343 GNUNET_free_non_null (sctx.addr);
1344 GNUNET_free_non_null (logfile);
1345 GNUNET_free (loglev);
1346 GNUNET_free (cfg_fn);
1347 GNUNET_free_non_null (sctx.v4_denied);
1348 GNUNET_free_non_null (sctx.v6_denied);
1349 GNUNET_free_non_null (sctx.v4_allowed);
1350 GNUNET_free_non_null (sctx.v6_allowed);
1356 * Run a service startup sequence within an existing
1357 * initialized system.
1359 * @param serviceName our service name
1360 * @param sched scheduler to use
1361 * @param cfg configuration to use
1362 * @return NULL on error, service handle
1364 struct GNUNET_SERVICE_Context *
1365 GNUNET_SERVICE_start (const char *serviceName,
1366 struct GNUNET_SCHEDULER_Handle *sched,
1367 const struct GNUNET_CONFIGURATION_Handle *cfg)
1370 struct GNUNET_SERVICE_Context *sctx;
1372 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1373 sctx->ready_confirm_fd = -1; /* no daemonizing */
1374 sctx->ret = GNUNET_OK;
1375 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1376 sctx->maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1377 sctx->serviceName = serviceName;
1379 sctx->sched = sched;
1381 /* setup subsystems */
1382 if ((GNUNET_OK != setup_service (sctx)) ||
1383 (NULL == (sctx->server = GNUNET_SERVER_create (sched,
1390 sctx->require_found))))
1392 GNUNET_SERVICE_stop (sctx);
1395 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1396 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1398 while ((sctx->my_handlers[i].callback != NULL))
1399 sctx->my_handlers[i++].callback_cls = sctx;
1400 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1407 * Obtain the server used by a service. Note that the server must NOT
1408 * be destroyed by the caller.
1410 * @param ctx the service context returned from the start function
1411 * @return handle to the server for this service, NULL if there is none
1413 struct GNUNET_SERVER_Handle *
1414 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1421 * Stop a service that was started with "GNUNET_SERVICE_start".
1423 * @param ctx the service context returned from the start function
1426 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1428 if (NULL != sctx->server)
1429 GNUNET_SERVER_destroy (sctx->server);
1430 GNUNET_free_non_null (sctx->my_handlers);
1431 GNUNET_free_non_null (sctx->addr);
1432 GNUNET_free_non_null (sctx->v4_denied);
1433 GNUNET_free_non_null (sctx->v6_denied);
1434 GNUNET_free_non_null (sctx->v4_allowed);
1435 GNUNET_free_non_null (sctx->v6_allowed);
1440 /* end of service.c */