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