- doc clarification (identity manages egos, not identities)
[oweals/gnunet.git] / src / arm / gnunet-service-arm.c
index ab1fe6481846126d3d2a671bb8c62809e817b414..f5323c04240860c7c3f147e26de3dbcf59ea8d20 100644 (file)
@@ -36,6 +36,7 @@
  */
 #define MAX_NOTIFY_QUEUE 1024
 
+
 /**
  * List of our services.
  */
@@ -159,7 +160,7 @@ struct ServiceList
 
   /**
    * Is this service to be started by default (or did a client tell us explicitly
-   * to start it)?  GNUNET_NO if the service is started only upon 'accept' on a
+   * to start it)?  #GNUNET_NO if the service is started only upon 'accept' on a
    * listen socket or possibly explicitly by a client changing the value.
    */
   int is_default;
@@ -217,6 +218,16 @@ static struct GNUNET_DISK_PipeHandle *sigpipe;
  */
 static int in_shutdown;
 
+/**
+ * Are we starting user services?
+ */
+static int start_user = GNUNET_YES;
+
+/**
+ * Are we starting system services?
+ */
+static int start_system = GNUNET_YES;
+
 /**
  * Handle to our server instance.  Our server is a bit special in that
  * its service is not immediately stopped once we get a shutdown
@@ -259,7 +270,7 @@ write_result (void *cls, size_t size, void *buf)
     return 0;                  /* error, not much we can do */
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Sending status response %u to client\n", 
+             "Sending status response %u to client\n",
              (unsigned int) msg->result);
   msize = msg->arm_msg.header.size;
   GNUNET_assert (size >= msize);
@@ -275,7 +286,7 @@ write_result (void *cls, size_t size, void *buf)
 
 /**
  * Transmit the list of running services.
- * 
+ *
  * @param cls pointer to `struct GNUNET_ARM_ListResultMessage` with the message
  * @param size number of bytes available in @a buf
  * @param buf where to copy the message, NULL on error
@@ -286,7 +297,7 @@ write_list_result (void *cls, size_t size, void *buf)
 {
   struct GNUNET_ARM_ListResultMessage *msg = cls;
   size_t rslt_size;
-  
+
   if (buf == NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -294,14 +305,14 @@ write_list_result (void *cls, size_t size, void *buf)
     GNUNET_free (msg);
     return 0;                   /* error, not much we can do */
   }
-  
+
   rslt_size = msg->arm_msg.header.size;
   GNUNET_assert (size >= rslt_size);
   msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
   msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
   msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
   msg->count = htons (msg->count);
-  
+
   memcpy (buf, msg, rslt_size);
   GNUNET_free (msg);
   return rslt_size;
@@ -319,7 +330,7 @@ write_list_result (void *cls, size_t size, void *buf)
  * @return NULL if it was not found
  */
 static void
-signal_result (struct GNUNET_SERVER_Client *client, 
+signal_result (struct GNUNET_SERVER_Client *client,
               const char *name,
               uint64_t request_id,
               enum GNUNET_ARM_Result result)
@@ -335,7 +346,7 @@ signal_result (struct GNUNET_SERVER_Client *client,
   msg->arm_msg.request_id = request_id;
 
   GNUNET_SERVER_notify_transmit_ready (client, msize,
-                                      GNUNET_TIME_UNIT_FOREVER_REL, 
+                                      GNUNET_TIME_UNIT_FOREVER_REL,
                                       write_result, msg);
 }
 
@@ -387,7 +398,9 @@ broadcast_status (const char *name,
  *                   being started. 0 if starting was not requested.
  */
 static void
-start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint64_t request_id)
+start_process (struct ServiceList *sl,
+               struct GNUNET_SERVER_Client *client,
+               uint64_t request_id)
 {
   char *loprefix;
   char *options;
@@ -492,7 +505,7 @@ start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint
     if (NULL == sl->config)
       sl->proc =
        do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
-                         lsocks, loprefix, binary, 
+                         lsocks, loprefix, binary,
                          options, NULL);
     else
       sl->proc =
@@ -582,34 +595,36 @@ create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
   static int on = 1;
   struct GNUNET_NETWORK_Handle *sock;
   struct ServiceListeningInfo *sli;
+  int match_uid;
+  int match_gid;
 
   switch (sa->sa_family)
-    {
-    case AF_INET:
-      sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
-      break;
-    case AF_INET6:
-      sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
-      break;
-    case AF_UNIX:
-      if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0)        /* Do not bind to blank UNIX path! */
-       return;
-      sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
-      break;
-    default:
-      GNUNET_break (0);
-      sock = NULL;
-      errno = EAFNOSUPPORT;
-      break;
-    }
-  if (NULL == sock)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Unable to create socket for service `%s': %s\n"),
-                 sl->name, STRERROR (errno));
-      GNUNET_free (sa);
+  {
+  case AF_INET:
+    sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
+    break;
+  case AF_INET6:
+    sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
+    break;
+  case AF_UNIX:
+    if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0)  /* Do not bind to blank UNIX path! */
       return;
-    }
+    sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
+    break;
+  default:
+    GNUNET_break (0);
+    sock = NULL;
+    errno = EAFNOSUPPORT;
+    break;
+  }
+  if (NULL == sock)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Unable to create socket for service `%s': %s\n"),
+                sl->name, STRERROR (errno));
+    GNUNET_free (sa);
+    return;
+  }
   if (GNUNET_NETWORK_socket_setsockopt
       (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
@@ -624,22 +639,37 @@ create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
 
   if (GNUNET_OK !=
       GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _
-                 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
-                 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
-      GNUNET_free (sa);
-      return;
-    }
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                _
+                ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
+                sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
+    GNUNET_free (sa);
+    return;
+  }
+#ifndef WINDOWS
+  if (AF_UNIX == sa->sa_family)
+  {
+    match_uid =
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
+                                            "UNIX_MATCH_UID");
+    match_gid =
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
+                                            "UNIX_MATCH_GID");
+    GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
+                                 match_uid,
+                                 match_gid);
+
+  }
+#endif
   if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
-    {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
-      GNUNET_free (sa);
-      return;
-    }
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
+    GNUNET_free (sa);
+    return;
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("ARM now monitors connections to service `%s' at `%s'\n"),
              sl->name, GNUNET_a2s (sa, addr_len));
@@ -693,7 +723,7 @@ handle_start (void *cls, struct GNUNET_SERVER_Client *client,
   uint64_t request_id;
   struct GNUNET_ARM_Message *amsg;
 
-  amsg = (struct GNUNET_ARM_Message *) message;  
+  amsg = (struct GNUNET_ARM_Message *) message;
   request_id = GNUNET_ntohll (amsg->request_id);
   size = ntohs (amsg->header.size);
   size -= sizeof (struct GNUNET_ARM_Message);
@@ -762,7 +792,7 @@ handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
   uint64_t request_id;
   struct GNUNET_ARM_Message *amsg;
 
-  amsg = (struct GNUNET_ARM_Message *) message;  
+  amsg = (struct GNUNET_ARM_Message *) message;
   request_id = GNUNET_ntohll (amsg->request_id);
   size = ntohs (amsg->header.size);
   size -= sizeof (struct GNUNET_ARM_Message);
@@ -821,7 +851,7 @@ handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
   broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
   /* no signal_start - only when it's STOPPED */
   sl->killed_at = GNUNET_TIME_absolute_get ();
-  if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
+  if (0 != GNUNET_OS_process_kill (sl->proc, GNUNET_TERM_SIG))
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
   sl->killing_client = client;
   sl->killing_client_request_id = request_id;
@@ -847,10 +877,10 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
   size_t total_size;
   struct ServiceList *sl;
   uint16_t count;
-  
+
   if (NULL == client)
     return;
-  
+
   request = (struct GNUNET_ARM_Message *) message;
   count = 0;
   string_list_size = 0;
@@ -866,16 +896,16 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
     }
   }
 
-  total_size = sizeof (struct GNUNET_ARM_ListResultMessage) 
+  total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
                + string_list_size;
   msg = GNUNET_malloc (total_size);
   msg->arm_msg.header.size = total_size;
   msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
   msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
   msg->count = count;
-  
+
   char *pos = (char *)&msg[1];
-  for (sl = running_head; sl != NULL; sl = sl->next) 
+  for (sl = running_head; sl != NULL; sl = sl->next)
   {
     if (sl->proc != NULL)
     {
@@ -884,7 +914,7 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
       pos += s;
     }
   }
-  
+
   GNUNET_SERVER_notify_transmit_ready (client,
                                        total_size,
                                        GNUNET_TIME_UNIT_FOREVER_REL,
@@ -926,7 +956,7 @@ list_count (struct ServiceList *running_head)
   unsigned int res = 0;
 
   for (res = 0, i = running_head; i; i = i->next, res++)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "%s\n",
                i->name);
   return res;
@@ -946,7 +976,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct ServiceList *nxt;
   struct ServiceListeningInfo *sli;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "First shutdown phase\n");
   if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
   {
@@ -979,11 +1009,11 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     nxt = pos->next;
     if (pos->proc != NULL)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Stopping service `%s'\n",
                  pos->name);
       pos->killed_at = GNUNET_TIME_absolute_get ();
-      if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
+      if (0 != GNUNET_OS_process_kill (pos->proc, GNUNET_TERM_SIG))
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
     }
     else
@@ -996,7 +1026,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     do_shutdown ();
   else
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Delaying shutdown, have %u childs still running\n", 
+               "Delaying shutdown, have %u childs still running\n",
                list_count (running_head));
 }
 
@@ -1063,12 +1093,12 @@ delayed_restart_task (void *cls,
   }
   if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Will restart process in %s\n",
                GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
     child_restart_task =
       GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
-                                                 GNUNET_SCHEDULER_PRIORITY_IDLE, 
+                                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
                                                  &delayed_restart_task, NULL);
   }
 }
@@ -1256,6 +1286,19 @@ setup_service (void *cls, const char *section)
       /* not a service section */
       return;
     }
+  if ((GNUNET_YES ==
+       GNUNET_CONFIGURATION_have_value (cfg, section, "USER_SERVICE")) &&
+      (GNUNET_YES ==
+       GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "USER_SERVICE")))
+  {
+    if (GNUNET_NO == start_user)
+      return; /* user service, and we don't deal with those */
+  }
+  else
+  {
+    if (GNUNET_NO == start_system)
+      return; /* system service, and we don't deal with those */
+  }
   sl = find_service (section);
   if (NULL != sl)
   {
@@ -1275,7 +1318,7 @@ setup_service (void *cls, const char *section)
   {
     if (NULL != config)
     {
-      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 
+      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
                                 section, "CONFIG",
                                 STRERROR (errno));
       GNUNET_free (config);
@@ -1293,8 +1336,9 @@ setup_service (void *cls, const char *section)
 #else
   if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
     sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
-#endif  
+#endif
   GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
+
   if (GNUNET_YES !=
       GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
     return;
@@ -1363,9 +1407,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *serv,
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
     {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
-    {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR, 
+    {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
      sizeof (struct GNUNET_MessageHeader)},
-    {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST, 
+    {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
      sizeof (struct GNUNET_ARM_Message)},
     {NULL, NULL, 0, 0}
   };
@@ -1392,7 +1436,20 @@ run (void *cls, struct GNUNET_SERVER_Handle *serv,
       GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
                                             &final_option))
     final_option = GNUNET_strdup ("");
-
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "USER_ONLY"))
+  {
+    GNUNET_break (GNUNET_YES == start_user);
+    start_system = GNUNET_NO;
+    return;
+  }
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "SYSTEM_ONLY"))
+  {
+    GNUNET_break (GNUNET_YES == start_system);
+    start_user = GNUNET_NO;
+    return;
+  }
   GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
 
   /* start default services... */
@@ -1456,7 +1513,7 @@ main (int argc, char *const *argv)
     GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
   ret =
     (GNUNET_OK ==
-     GNUNET_SERVICE_run (argc, argv, "arm", 
+     GNUNET_SERVICE_run (argc, argv, "arm",
                         GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
   GNUNET_SIGNAL_handler_uninstall (shc_chld);
   shc_chld = NULL;