From 8094893877c4c176d1d647fee5adf5a25db60244 Mon Sep 17 00:00:00 2001 From: "Carsten Haitzler (Rasterman)" Date: Wed, 8 Feb 2017 17:35:26 +0900 Subject: [PATCH] eina utils - home and tmp environ - store statitcally and handle setuid if setuod we dont want to trust HOME environ at all and get it from passwd file... also we dont want to keep re-getting too... so store statically as well as tmp. this also kind of helps CID 1366469 --- src/lib/eina/eina_util.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/lib/eina/eina_util.c b/src/lib/eina/eina_util.c index 62e1a79b42..1515172694 100644 --- a/src/lib/eina/eina_util.c +++ b/src/lib/eina/eina_util.c @@ -24,6 +24,10 @@ #include #ifdef _WIN32 # include +#else +# include +# include +# include #endif #include "eina_config.h" @@ -51,9 +55,10 @@ static char home_storage[8]; EAPI const char * eina_environment_home_get(void) { -#ifdef _WIN32 - char *home; + static char *home = NULL; + if (home) return home; +#ifdef _WIN32 home = getenv("USERPROFILE"); if (!home) home = getenv("WINDIR"); if (!home && @@ -66,17 +71,36 @@ eina_environment_home_get(void) } if (!home) home = "C:\\"; - return home; #else - return getenv("HOME"); +# if defined(HAVE_GETUID) && defined(HAVE_GETEUID) + if (getuid() == geteuid()) home = getenv("HOME"); +# endif + if (!home) + { +# ifdef HAVE_GETPWENT + struct passwd pwent, *pwent2 = NULL; + char pwbuf[8129]; + + if (!getpwuid_r(getuid(), &pwent, pwbuf, sizeof(pwbuf), &pwent2)) + { + if ((pwent2) && (pwent.pw_dir)) + home = strdup(pwent.pw_dir); + } + if (!home) home = strdup("/tmp"); + return home; + } +# endif #endif + home = strdup(home); + return home; } EAPI const char * eina_environment_tmp_get(void) { - char *tmp = NULL; + static char *tmp = NULL; + if (tmp) return tmp; #ifdef _WIN32 tmp = getenv("TMP"); if (!tmp) tmp = getenv("TEMP");