]> err.no Git - util-linux/commitdiff
lib: [env] consolidate safe_getenv() usage
authorKarel Zak <kzak@redhat.com>
Fri, 24 Dec 2010 00:07:48 +0000 (01:07 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 3 Jan 2011 11:28:48 +0000 (12:28 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/env.h
lib/env.c
shlibs/blkid/src/Makefile.am
shlibs/blkid/src/cache.c
shlibs/blkid/src/config.c
shlibs/mount/src/Makefile.am
shlibs/mount/src/init.c
shlibs/mount/src/utils.c

index d69b4f295acd0e7ffd8aa481b925967f7f497a29..bcd0f7ead2614583519e0468460423c738cb9bd5 100644 (file)
@@ -1,2 +1,8 @@
+#ifndef UTIL_LINUX_ENV_H
+#define UTIL_LINUX_ENV_H
+
 extern void sanitize_env (void);
+extern char *safe_getenv(const char *arg);
+
+#endif /* UTIL_LINUX_ENV_H */
 
index 82fadf5241145a38b1d95c3bbe0b07940a5877a5..770cadff92e696dc01fdd563990b27ec895c05be 100644 (file)
--- a/lib/env.c
+++ b/lib/env.c
@@ -3,11 +3,22 @@
  * Added from shadow-utils package
  * by Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
  *
- */ 
+ */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#else
+#define PR_GET_DUMPABLE 3
+#endif
+#if (!defined(HAVE_PRCTL) && defined(linux))
+#include <sys/syscall.h>
+#endif
+#include <unistd.h>
+#include <sys/types.h>
+
 #include "env.h"
 
 extern char **environ;
@@ -71,3 +82,26 @@ sanitize_env(void)
         }
 }
 
+
+char *safe_getenv(const char *arg)
+{
+       uid_t ruid = getuid();
+
+       if (ruid != 0 || (ruid != geteuid()) || (getgid() != getegid()))
+               return NULL;
+#if HAVE_PRCTL
+       if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
+               return NULL;
+#else
+#if (defined(linux) && defined(SYS_prctl))
+       if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
+               return NULL;
+#endif
+#endif
+
+#ifdef HAVE___SECURE_GETENV
+       return __secure_getenv(arg);
+#else
+       return getenv(arg);
+#endif
+}
index 47d2a370a65fa8d258c3d723c501a7fcd8c803a5..b0701cb7b11ef9778c5ad6af7b98e6039057ad26 100644 (file)
@@ -32,7 +32,8 @@ libblkid_la_SOURCES = cache.c dev.c devname.c devno.c getsize.c llseek.c  \
                     $(top_srcdir)/lib/canonicalize.c \
                     $(top_srcdir)/lib/md5.c \
                     $(top_srcdir)/lib/crc32.c \
-                    $(top_srcdir)/include/list.h
+                    $(top_srcdir)/include/list.h \
+                    $(top_srcdir)/lib/env.c
 
 nodist_libblkid_la_SOURCES = blkid.h
 
index e71176b507cde5b127f52d859025f3b7416f9eb0..6f53dc99aa57d7a93f530268b2e4cd42f5c935d9 100644 (file)
 #endif
 #include <stdlib.h>
 #include <string.h>
-#ifdef HAVE_SYS_PRCTL_H
-#include <sys/prctl.h>
-#else
-#define PR_GET_DUMPABLE 3
-#endif
-#if (!defined(HAVE_PRCTL) && defined(linux))
-#include <sys/syscall.h>
-#endif
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
 #include "blkidP.h"
+#include "env.h"
 
 int blkid_debug_mask = 0;
 
@@ -58,27 +51,6 @@ int blkid_debug_mask = 0;
  * the cache file is required in this situation.
  */
 
-char *blkid_safe_getenv(const char *arg)
-{
-       if ((getuid() != geteuid()) || (getgid() != getegid()))
-               return NULL;
-#if HAVE_PRCTL
-       if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
-               return NULL;
-#else
-#if (defined(linux) && defined(SYS_prctl))
-       if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
-               return NULL;
-#endif
-#endif
-
-#ifdef HAVE___SECURE_GETENV
-       return __secure_getenv(arg);
-#else
-       return getenv(arg);
-#endif
-}
-
 #if 0 /* ifdef CONFIG_BLKID_DEBUG */
 static blkid_debug_dump_cache(int mask, blkid_cache cache)
 {
@@ -126,7 +98,7 @@ char *blkid_get_cache_filename(struct blkid_config *conf)
 {
        char *filename;
 
-       filename = blkid_safe_getenv("BLKID_FILE");
+       filename = safe_getenv("BLKID_FILE");
        if (filename)
                filename = blkid_strdup(filename);
        else if (conf)
index fcc7b6f125ee126a189efbed6078cb5da0fd71a6..e4d25ba466329bef6ee7689eda667d681a26bcd5 100644 (file)
@@ -28,7 +28,7 @@
 
 #include "blkdev.h"
 #include "blkidP.h"
-
+#include "env.h"
 
 static int parse_evaluate(struct blkid_config *conf, char *s)
 {
@@ -120,7 +120,7 @@ struct blkid_config *blkid_read_config(const char *filename)
        FILE *f;
 
        if (!filename)
-               filename = blkid_safe_getenv("BLKID_CONF");
+               filename = safe_getenv("BLKID_CONF");
        if (!filename)
                filename = BLKID_CONFIG_FILE;
 
index 6c241ba2dff38c103d98c3794827bc74b6b7206c..f552ebee966ba4c50c6cf0f25241e0bf73c104b6 100644 (file)
@@ -18,7 +18,8 @@ libmount_la_SOURCES = mountP.h version.c utils.c test.c init.c cache.c \
                        $(top_srcdir)/include/list.h \
                        $(top_srcdir)/lib/mangle.c \
                        $(top_srcdir)/lib/canonicalize.c \
-                       $(top_srcdir)/lib/strutils.c
+                       $(top_srcdir)/lib/strutils.c \
+                       $(top_srcdir)/lib/env.c
 
 nodist_libmount_la_SOURCES = mountP.h
 
index 2dfab58f33c3d3fe40ec4258ab33b476c45a34f6..d80a2d8ca2cdfc66b1e8aa71d2ffaad27f2c5f64 100644 (file)
@@ -33,7 +33,7 @@ void mnt_init_debug(int mask)
        if (libmount_debug_mask & MNT_DEBUG_INIT)
                return;
        if (!mask) {
-               char *str = mnt_getenv_safe("LIBMOUNT_DEBUG");
+               char *str = getenv("LIBMOUNT_DEBUG");
                if (str)
                        libmount_debug_mask = strtoul(str, 0, 0);
        } else
index ff3c90cdb43bb05ad48b7ad5c95c68c0e5d57fd2..c97144dcba0ab30461823e378aeb015668ce1d4e 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#ifdef HAVE_SYS_PRCTL_H
-#include <sys/prctl.h>
-#else
-#define PR_GET_DUMPABLE 3
-#endif
-#if (!defined(HAVE_PRCTL) && defined(linux))
-#include <sys/syscall.h>
-#endif
 #include <sys/stat.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include "mountP.h"
 #include "mangle.h"
 #include "canonicalize.h"
-
-char *mnt_getenv_safe(const char *arg)
-{
-       return getenv(arg);
-
-       if ((getuid() != geteuid()) || (getgid() != getegid()))
-               return NULL;
-#if HAVE_PRCTL
-       if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
-               return NULL;
-#else
-#if (defined(linux) && defined(SYS_prctl))
-       if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
-               return NULL;
-#endif
-#endif
-
-#ifdef HAVE___SECURE_GETENV
-       return __secure_getenv(arg);
-#else
-       return getenv(arg);
-#endif
-}
+#include "env.h"
 
 int endswith(const char *s, const char *sx)
 {
@@ -635,7 +605,7 @@ done:
  */
 const char *mnt_get_fstab_path(void)
 {
-       const char *p = mnt_getenv_safe("LIBMOUNT_FSTAB");
+       const char *p = safe_getenv("LIBMOUNT_FSTAB");
        return p ? : _PATH_MNTTAB;
 }
 
@@ -649,7 +619,7 @@ const char *mnt_get_fstab_path(void)
  */
 const char *mnt_get_mtab_path(void)
 {
-       const char *p = mnt_getenv_safe("LIBMOUNT_MTAB");
+       const char *p = safe_getenv("LIBMOUNT_MTAB");
        return p ? : _PATH_MOUNTED;
 }
 
@@ -660,7 +630,7 @@ const char *mnt_get_mtab_path(void)
  */
 const char *mnt_get_utab_path(void)
 {
-       const char *p = mnt_getenv_safe("LIBMOUNT_UTAB");
+       const char *p = safe_getenv("LIBMOUNT_UTAB");
        return p ? : MNT_PATH_UTAB;
 }