fix for starting problems of SUID binaries
[oweals/gnunet.git] / src / util / gnunet-uri.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file util/gnunet-uri.c
23  * @brief tool to dispatch URIs to the appropriate GNUnet helper process
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 /**
30  * Handler exit code
31  */
32 static long unsigned int exit_code = 1;
33
34 /**
35  * Helper process we started.
36  */
37 static struct GNUNET_OS_Process *p;
38
39 /**
40  * Pipe used to communicate shutdown via signal.
41  */
42 static struct GNUNET_DISK_PipeHandle *sigpipe;
43
44
45 /**
46  * Task triggered whenever we receive a SIGCHLD (child
47  * process died) or when user presses CTRL-C.
48  *
49  * @param cls closure, NULL
50  */
51 static void
52 maint_child_death (void *cls)
53 {
54   enum GNUNET_OS_ProcessStatusType type;
55
56   (void) cls;
57   if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
58       (type != GNUNET_OS_PROCESS_EXITED))
59     GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
60   GNUNET_OS_process_destroy (p);
61 }
62
63
64 /**
65  * Main function that will be run by the scheduler.
66  *
67  * @param cls closure
68  * @param args remaining command-line arguments
69  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
70  * @param cfg configuration
71  */
72 static void
73 run (void *cls,
74      char *const *args,
75      const char *cfgfile,
76      const struct GNUNET_CONFIGURATION_Handle *cfg)
77 {
78   const char *uri;
79   const char *slash;
80   char *subsystem;
81   char *program;
82   struct GNUNET_SCHEDULER_Task *rt;
83
84   (void) cls;
85   (void) cfgfile;
86   if (NULL == (uri = args[0]))
87   {
88     fprintf (stderr, _ ("No URI specified on command line\n"));
89     return;
90   }
91   if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
92   {
93     fprintf (stderr,
94              _ ("Invalid URI: does not start with `%s'\n"),
95              "gnunet://");
96     return;
97   }
98   uri += strlen ("gnunet://");
99   if (NULL == (slash = strchr (uri, '/')))
100   {
101     fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
102     return;
103   }
104   subsystem = GNUNET_strndup (uri, slash - uri);
105   if (GNUNET_OK !=
106       GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
107   {
108     fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
109     GNUNET_free (subsystem);
110     return;
111   }
112   GNUNET_free (subsystem);
113   rt = GNUNET_SCHEDULER_add_read_file (
114     GNUNET_TIME_UNIT_FOREVER_REL,
115     GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
116     &maint_child_death,
117     NULL);
118   p = GNUNET_OS_start_process (GNUNET_NO,
119                                0,
120                                NULL,
121                                NULL,
122                                NULL,
123                                program,
124                                program,
125                                args[0],
126                                NULL);
127   GNUNET_free (program);
128   if (NULL == p)
129     GNUNET_SCHEDULER_cancel (rt);
130 }
131
132
133 /**
134  * Signal handler called for SIGCHLD.  Triggers the
135  * respective handler by writing to the trigger pipe.
136  */
137 static void
138 sighandler_child_death ()
139 {
140   static char c;
141   int old_errno = errno; /* back-up errno */
142
143   GNUNET_break (
144     1 ==
145     GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
146                                                      GNUNET_DISK_PIPE_END_WRITE),
147                             &c,
148                             sizeof (c)));
149   errno = old_errno; /* restore errno */
150 }
151
152
153 /**
154  * The main function to handle gnunet://-URIs.
155  *
156  * @param argc number of arguments from the command line
157  * @param argv command line arguments
158  * @return 0 ok, 1 on error
159  */
160 int
161 main (int argc, char *const *argv)
162 {
163   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
164     GNUNET_GETOPT_OPTION_END};
165   struct GNUNET_SIGNAL_Context *shc_chld;
166   int ret;
167
168   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
169     return 2;
170   sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
171   GNUNET_assert (sigpipe != NULL);
172   shc_chld =
173     GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
174   ret = GNUNET_PROGRAM_run (argc,
175                             argv,
176                             "gnunet-uri URI",
177                             gettext_noop (
178                               "Perform default-actions for GNUnet URIs"),
179                             options,
180                             &run,
181                             NULL);
182   GNUNET_SIGNAL_handler_uninstall (shc_chld);
183   shc_chld = NULL;
184   GNUNET_DISK_pipe_close (sigpipe);
185   sigpipe = NULL;
186   GNUNET_free ((void *) argv);
187   return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
188 }
189
190 /* end of gnunet-uri.c */