run indent twice, it alternates between two 'canonical' forms, also run whitespace...
[oweals/gnunet.git] / src / util / os_installation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file src/util/os_installation.c
23  * @brief get paths used by the program
24  * @author Milan
25  */
26 #include <sys/stat.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "platform.h"
32 #include "gnunet_common.h"
33 #include "gnunet_configuration_lib.h"
34 #include "gnunet_disk_lib.h"
35 #include "gnunet_os_lib.h"
36 #if DARWIN
37 #include <mach-o/ldsyms.h>
38 #include <mach-o/dyld.h>
39 #endif
40
41 #if LINUX
42 /**
43  * Try to determine path by reading /proc/PID/exe
44  */
45 static char *
46 get_path_from_proc_maps ()
47 {
48   char fn[64];
49   char line[1024];
50   char dir[1024];
51   FILE *f;
52   char *lgu;
53
54   GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
55   f = fopen (fn, "r");
56   if (f == NULL)
57     return NULL;
58   while (NULL != fgets (line, sizeof (line), f))
59   {
60     if ((1 ==
61          sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
62         (NULL != (lgu = strstr (dir, "libgnunetutil"))))
63     {
64       lgu[0] = '\0';
65       fclose (f);
66       return GNUNET_strdup (dir);
67     }
68   }
69   fclose (f);
70   return NULL;
71 }
72
73 /**
74  * Try to determine path by reading /proc/PID/exe
75  */
76 static char *
77 get_path_from_proc_exe ()
78 {
79   char fn[64];
80   char lnk[1024];
81   ssize_t size;
82
83   GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
84   size = readlink (fn, lnk, sizeof (lnk) - 1);
85   if (size <= 0)
86   {
87     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
88     return NULL;
89   }
90   GNUNET_assert (size < sizeof (lnk));
91   lnk[size] = '\0';
92   while ((lnk[size] != '/') && (size > 0))
93     size--;
94   if ((size < 4) || (lnk[size - 4] != '/'))
95   {
96     /* not installed in "/bin/" -- binary path probably useless */
97     return NULL;
98   }
99   lnk[size] = '\0';
100   return GNUNET_strdup (lnk);
101 }
102 #endif
103
104 #if WINDOWS
105 /**
106  * Try to determine path with win32-specific function
107  */
108 static char *
109 get_path_from_module_filename ()
110 {
111   char path[4097];
112   char *idx;
113
114   GetModuleFileName (NULL, path, sizeof (path) - 1);
115   idx = path + strlen (path);
116   while ((idx > path) && (*idx != '\\') && (*idx != '/'))
117     idx--;
118   *idx = '\0';
119   return GNUNET_strdup (path);
120 }
121 #endif
122
123 #if DARWIN
124 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
125
126 static char *
127 get_path_from_NSGetExecutablePath ()
128 {
129   static char zero = '\0';
130   char *path;
131   size_t len;
132   MyNSGetExecutablePathProto func;
133   int ret;
134
135   path = NULL;
136   func =
137       (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
138   if (!func)
139     return NULL;
140   path = &zero;
141   len = 0;
142   /* get the path len, including the trailing \0 */
143   func (path, &len);
144   if (len == 0)
145     return NULL;
146   path = GNUNET_malloc (len);
147   ret = func (path, &len);
148   if (ret != 0)
149   {
150     GNUNET_free (path);
151     return NULL;
152   }
153   len = strlen (path);
154   while ((path[len] != '/') && (len > 0))
155     len--;
156   path[len] = '\0';
157   return path;
158 }
159
160 static char *
161 get_path_from_dyld_image ()
162 {
163   const char *path;
164   char *p, *s;
165   int i;
166   int c;
167
168   p = NULL;
169   c = _dyld_image_count ();
170   for (i = 0; i < c; i++)
171   {
172     if (_dyld_get_image_header (i) == &_mh_dylib_header)
173     {
174       path = _dyld_get_image_name (i);
175       if (path != NULL && strlen (path) > 0)
176       {
177         p = strdup (path);
178         s = p + strlen (p);
179         while ((s > p) && (*s != '/'))
180           s--;
181         s++;
182         *s = '\0';
183       }
184       break;
185     }
186   }
187   return p;
188 }
189 #endif
190
191 /**
192  * Return the actual path to a file found in the current
193  * PATH environment variable.
194  *
195  * @param binary the name of the file to find
196  * @return path to binary, NULL if not found
197  */
198 static char *
199 get_path_from_PATH (const char *binary)
200 {
201   char *path;
202   char *pos;
203   char *end;
204   char *buf;
205   const char *p;
206
207   p = getenv ("PATH");
208   if (p == NULL)
209     return NULL;
210   path = GNUNET_strdup (p);     /* because we write on it */
211   buf = GNUNET_malloc (strlen (path) + 20);
212   pos = path;
213   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
214   {
215     *end = '\0';
216     sprintf (buf, "%s/%s", pos, binary);
217     if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
218     {
219       pos = GNUNET_strdup (pos);
220       GNUNET_free (buf);
221       GNUNET_free (path);
222       return pos;
223     }
224     pos = end + 1;
225   }
226   sprintf (buf, "%s/%s", pos, binary);
227   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
228   {
229     pos = GNUNET_strdup (pos);
230     GNUNET_free (buf);
231     GNUNET_free (path);
232     return pos;
233   }
234   GNUNET_free (buf);
235   GNUNET_free (path);
236   return NULL;
237 }
238
239 static char *
240 get_path_from_GNUNET_PREFIX ()
241 {
242   const char *p;
243
244   p = getenv ("GNUNET_PREFIX");
245   if (p != NULL)
246     return GNUNET_strdup (p);
247   return NULL;
248 }
249
250 /*
251  * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
252  * @author Milan
253  *
254  * @return a pointer to the executable path, or NULL on error
255  */
256 static char *
257 os_get_gnunet_path ()
258 {
259   char *ret;
260
261   ret = get_path_from_GNUNET_PREFIX ();
262   if (ret != NULL)
263     return ret;
264 #if LINUX
265   ret = get_path_from_proc_maps ();
266   if (ret != NULL)
267     return ret;
268   ret = get_path_from_proc_exe ();
269   if (ret != NULL)
270     return ret;
271 #endif
272 #if WINDOWS
273   ret = get_path_from_module_filename ();
274   if (ret != NULL)
275     return ret;
276 #endif
277 #if DARWIN
278   ret = get_path_from_dyld_image ();
279   if (ret != NULL)
280     return ret;
281   ret = get_path_from_NSGetExecutablePath ();
282   if (ret != NULL)
283     return ret;
284 #endif
285   ret = get_path_from_PATH ("gnunet-arm");
286   if (ret != NULL)
287     return ret;
288   /* other attempts here */
289   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290               _
291               ("Could not determine installation path for %s.  Set `%s' environment variable.\n"),
292               "GNUnet", "GNUNET_PREFIX");
293   return NULL;
294 }
295
296 /*
297  * @brief get the path to current app's bin/
298  * @author Milan
299  *
300  * @return a pointer to the executable path, or NULL on error
301  */
302 static char *
303 os_get_exec_path ()
304 {
305   char *ret;
306
307   ret = NULL;
308 #if LINUX
309   ret = get_path_from_proc_exe ();
310   if (ret != NULL)
311     return ret;
312 #endif
313 #if WINDOWS
314   ret = get_path_from_module_filename ();
315   if (ret != NULL)
316     return ret;
317 #endif
318 #if DARWIN
319   ret = get_path_from_NSGetExecutablePath ();
320   if (ret != NULL)
321     return ret;
322 #endif
323   /* other attempts here */
324   return ret;
325 }
326
327
328
329 /**
330  * @brief get the path to a specific GNUnet installation directory or,
331  * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
332  * @author Milan
333  * @return a pointer to the dir path (to be freed by the caller)
334  */
335 char *
336 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
337 {
338   size_t n;
339   const char *dirname;
340   char *execpath = NULL;
341   char *tmp;
342   int isbasedir;
343
344   /* if wanted, try to get the current app's bin/ */
345   if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
346     execpath = os_get_exec_path ();
347
348   /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
349    * guess for the current app */
350   if (execpath == NULL)
351     execpath = os_get_gnunet_path ();
352
353   if (execpath == NULL)
354     return NULL;
355
356   n = strlen (execpath);
357   if (n == 0)
358   {
359     /* should never happen, but better safe than sorry */
360     GNUNET_free (execpath);
361     return NULL;
362   }
363   /* remove filename itself */
364   while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
365     execpath[--n] = '\0';
366
367   isbasedir = 1;
368   if ((n > 5) &&
369       ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
370        (0 == strcasecmp (&execpath[n - 5], "lib64"))))
371   {
372     if (dirkind != GNUNET_OS_IPK_LIBDIR)
373     {
374       /* strip '/lib32' or '/lib64' */
375       execpath[n - 5] = '\0';
376       n -= 5;
377     }
378     else
379       isbasedir = 0;
380   }
381   else if ((n > 3) &&
382            ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
383             (0 == strcasecmp (&execpath[n - 3], "lib"))))
384   {
385     /* strip '/bin' or '/lib' */
386     execpath[n - 3] = '\0';
387     n -= 3;
388   }
389   /* in case this was a directory named foo-bin, remove "foo-" */
390   while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
391     execpath[--n] = '\0';
392   switch (dirkind)
393   {
394   case GNUNET_OS_IPK_PREFIX:
395   case GNUNET_OS_IPK_SELF_PREFIX:
396     dirname = DIR_SEPARATOR_STR;
397     break;
398   case GNUNET_OS_IPK_BINDIR:
399     dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
400     break;
401   case GNUNET_OS_IPK_LIBDIR:
402     if (isbasedir)
403       dirname =
404           DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
405     else
406       dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
407     break;
408   case GNUNET_OS_IPK_DATADIR:
409     dirname =
410         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
411     break;
412   case GNUNET_OS_IPK_LOCALEDIR:
413     dirname =
414         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
415     break;
416   case GNUNET_OS_IPK_ICONDIR:
417     dirname =
418         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
419     break;
420   default:
421     GNUNET_free (execpath);
422     return NULL;
423   }
424   tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
425   sprintf (tmp, "%s%s", execpath, dirname);
426   GNUNET_free (execpath);
427   return tmp;
428 }
429
430
431 /**
432  * Check whether an executable exists and possibly
433  * if the suid bit is set on the file.
434  * Attempts to find the file using the current
435  * PATH environment variable as a search path.
436  *
437  * @param binary the name of the file to check
438  * @return GNUNET_YES if the file is SUID,
439  *         GNUNET_NO if not SUID (but binary exists)
440  *         GNUNET_SYSERR on error (no such binary or not executable)
441  */
442 int
443 GNUNET_OS_check_helper_binary (const char *binary)
444 {
445   struct stat statbuf;
446   char *p;
447   char *pf;
448
449 #ifdef MINGW
450   SOCKET rawsock;
451   char *binaryexe;
452
453   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
454   p = get_path_from_PATH (binaryexe);
455   if (p != NULL)
456   {
457     GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
458     GNUNET_free (p);
459     p = pf;
460   }
461   free (binaryexe);
462 #else
463   p = get_path_from_PATH (binary);
464   if (p != NULL)
465   {
466     GNUNET_asprintf (&pf, "%s/%s", p, binary);
467     GNUNET_free (p);
468     p = pf;
469   }
470 #endif
471   if (p == NULL)
472   {
473     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
474                 _("Could not find binary `%s' in PATH!\n"), binary);
475     return GNUNET_SYSERR;
476   }
477   if (0 != STAT (p, &statbuf))
478   {
479     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
480                 STRERROR (errno));
481     GNUNET_free (p);
482     return GNUNET_SYSERR;
483   }
484 #ifndef MINGW
485   if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
486   {
487     GNUNET_free (p);
488     return GNUNET_YES;
489   }
490   if (0 == ACCESS (p, X_OK))
491   {
492     GNUNET_free (p);
493     return GNUNET_NO;
494   }
495   GNUNET_free (p);
496   return GNUNET_SYSERR;
497 #else
498   GNUNET_free (p);
499   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
500   if (INVALID_SOCKET == rawsock)
501   {
502     DWORD err = GetLastError ();
503
504     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
505                 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n",
506                 err);
507     return GNUNET_NO;           /* not running as administrator */
508   }
509   closesocket (rawsock);
510   return GNUNET_YES;
511 #endif
512 }
513
514
515 /* end of os_installation.c */