]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Merge pull request #2749 from KlaraSystems/des/tempdir
authorTim Kientzle <kientzle@acm.org>
Mon, 13 Oct 2025 17:57:18 +0000 (10:57 -0700)
committerMartin Matuska <martin@matuska.de>
Mon, 13 Oct 2025 22:38:23 +0000 (00:38 +0200)
Unify temporary directory handling

(cherry picked from commit d207d816d065c79dc2cb992008c3ba9721c6a276)

CMakeLists.txt
configure.ac
libarchive/archive_private.h
libarchive/archive_read_disk_entry_from_file.c
libarchive/archive_read_disk_posix.c
libarchive/archive_util.c

index f44adc77725a30a26aaf367a8f88489bfe3be750..fc9aca4e966e1b41f312e30a34f6b25c4f9f689b 100644 (file)
@@ -1498,15 +1498,19 @@ CHECK_FUNCTION_EXISTS_GLIBC(ftruncate HAVE_FTRUNCATE)
 CHECK_FUNCTION_EXISTS_GLIBC(futimens HAVE_FUTIMENS)
 CHECK_FUNCTION_EXISTS_GLIBC(futimes HAVE_FUTIMES)
 CHECK_FUNCTION_EXISTS_GLIBC(futimesat HAVE_FUTIMESAT)
+CHECK_FUNCTION_EXISTS_GLIBC(getegid HAVE_GETEGID)
 CHECK_FUNCTION_EXISTS_GLIBC(geteuid HAVE_GETEUID)
 CHECK_FUNCTION_EXISTS_GLIBC(getgrgid_r HAVE_GETGRGID_R)
 CHECK_FUNCTION_EXISTS_GLIBC(getgrnam_r HAVE_GETGRNAM_R)
 CHECK_FUNCTION_EXISTS_GLIBC(getline HAVE_GETLINE)
+CHECK_FUNCTION_EXISTS_GLIBC(getpid HAVE_GETPID)
 CHECK_FUNCTION_EXISTS_GLIBC(getpwnam_r HAVE_GETPWNAM_R)
 CHECK_FUNCTION_EXISTS_GLIBC(getpwuid_r HAVE_GETPWUID_R)
-CHECK_FUNCTION_EXISTS_GLIBC(getpid HAVE_GETPID)
+CHECK_FUNCTION_EXISTS_GLIBC(getresgid HAVE_GETRESGID)
+CHECK_FUNCTION_EXISTS_GLIBC(getresuid HAVE_GETRESUID)
 CHECK_FUNCTION_EXISTS_GLIBC(getvfsbyname HAVE_GETVFSBYNAME)
 CHECK_FUNCTION_EXISTS_GLIBC(gmtime_r HAVE_GMTIME_R)
+CHECK_FUNCTION_EXISTS_GLIBC(issetugid HAVE_ISSETUGID)
 CHECK_FUNCTION_EXISTS_GLIBC(lchflags HAVE_LCHFLAGS)
 CHECK_FUNCTION_EXISTS_GLIBC(lchmod HAVE_LCHMOD)
 CHECK_FUNCTION_EXISTS_GLIBC(lchown HAVE_LCHOWN)
index aae0f381e31238bf79e1708ad006179fff9e725e..a1a8f380281c0a907979c66c5a8a819e6133af0b 100644 (file)
@@ -826,8 +826,10 @@ AC_CHECK_FUNCS([closefrom close_range ctime_r])
 AC_CHECK_FUNCS([fchdir fchflags fchmod fchown fcntl fdopendir fnmatch fork])
 AC_CHECK_FUNCS([fstat fstatat fstatfs fstatvfs ftruncate])
 AC_CHECK_FUNCS([futimens futimes futimesat])
-AC_CHECK_FUNCS([geteuid getline getpid getgrgid_r getgrnam_r])
-AC_CHECK_FUNCS([getpwnam_r getpwuid_r getvfsbyname gmtime_r])
+AC_CHECK_FUNCS([getegid geteuid getline getpid getresgid getresuid])
+AC_CHECK_FUNCS([getgrgid_r getgrnam_r getpwnam_r getpwuid_r])
+AC_CHECK_FUNCS([getvfsbyname gmtime_r])
+AC_CHECK_FUNCS([issetugid])
 AC_CHECK_FUNCS([lchflags lchmod lchown link linkat localtime_r lstat lutimes])
 AC_CHECK_FUNCS([mbrtowc memmove memset])
 AC_CHECK_FUNCS([mkdir mkfifo mknod mkstemp])
index 050fc63c0b2e4df13be24e44ccd7ffcddd2b2361..3a926c6886ad3b4d776d7ffa49b4915bf78addd4 100644 (file)
@@ -158,6 +158,7 @@ int __archive_check_magic(struct archive *, unsigned int magic,
 __LA_NORETURN void     __archive_errx(int retvalue, const char *msg);
 
 void   __archive_ensure_cloexec_flag(int fd);
+int    __archive_get_tempdir(struct archive_string *);
 int    __archive_mktemp(const char *tmpdir);
 #if defined(_WIN32) && !defined(__CYGWIN__)
 int    __archive_mkstemp(wchar_t *templates);
index 87389642db4ae3fb3eaace7fea348acdb1f6ef46..42af4034b07ea779969d24c125be41287356f7fe 100644 (file)
@@ -338,7 +338,7 @@ setup_mac_metadata(struct archive_read_disk *a,
        int ret = ARCHIVE_OK;
        void *buff = NULL;
        int have_attrs;
-       const char *name, *tempdir;
+       const char *name;
        struct archive_string tempfile;
 
        (void)fd; /* UNUSED */
@@ -357,14 +357,12 @@ setup_mac_metadata(struct archive_read_disk *a,
        if (have_attrs == 0)
                return (ARCHIVE_OK);
 
-       tempdir = NULL;
-       if (issetugid() == 0)
-               tempdir = getenv("TMPDIR");
-       if (tempdir == NULL)
-               tempdir = _PATH_TMP;
        archive_string_init(&tempfile);
-       archive_strcpy(&tempfile, tempdir);
-       archive_strcat(&tempfile, "/tar.md.XXXXXX");
+       if (__archive_get_tempdir(&tempfile) != ARCHIVE_OK) {
+               ret = ARCHIVE_WARN;
+               goto cleanup;
+       }
+       archive_strcat(&tempfile, "tar.md.XXXXXX");
        tempfd = mkstemp(tempfile.s);
        if (tempfd < 0) {
                archive_set_error(&a->archive, errno,
index ba0046d75454755899b55c9bc37ba6ae94a3290a..54a8e66188f875e914ed33df8503bd0819489329 100644 (file)
@@ -1571,9 +1571,6 @@ setup_current_filesystem(struct archive_read_disk *a)
 #  endif
 #endif
        int r, xr = 0;
-#if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
-       long nm;
-#endif
 
        t->current_filesystem->synthetic = -1;
        t->current_filesystem->remote = -1;
index 900abd0c3c62027c8ee1a71962a7c5313d29e752..d048bbc94650cc28ac1d658c241e602176b9e012 100644 (file)
@@ -445,11 +445,39 @@ __archive_mkstemp(wchar_t *template)
 #else
 
 static int
-get_tempdir(struct archive_string *temppath)
+__archive_issetugid(void)
 {
-       const char *tmp;
+#ifdef HAVE_ISSETUGID
+       return (issetugid());
+#elif HAVE_GETRESUID
+       uid_t ruid, euid, suid;
+       gid_t rgid, egid, sgid;
+       if (getresuid(&ruid, &euid, &suid) != 0)
+               return (-1);
+       if (ruid != euid || ruid != suid)
+               return (1);
+       if (getresgid(&ruid, &egid, &sgid) != 0)
+               return (-1);
+       if (rgid != egid || rgid != sgid)
+               return (1);
+#elif HAVE_GETEUID
+       if (geteuid() != getuid())
+               return (1);
+#if HAVE_GETEGID
+       if (getegid() != getgid())
+               return (1);
+#endif
+#endif
+       return (0);
+}
+
+int
+__archive_get_tempdir(struct archive_string *temppath)
+{
+       const char *tmp = NULL;
 
-       tmp = getenv("TMPDIR");
+       if (__archive_issetugid() == 0)
+               tmp = getenv("TMPDIR");
        if (tmp == NULL)
 #ifdef _PATH_TMP
                tmp = _PATH_TMP;
@@ -476,7 +504,7 @@ __archive_mktemp(const char *tmpdir)
 
        archive_string_init(&temp_name);
        if (tmpdir == NULL) {
-               if (get_tempdir(&temp_name) != ARCHIVE_OK)
+               if (__archive_get_tempdir(&temp_name) != ARCHIVE_OK)
                        goto exit_tmpfile;
        } else {
                archive_strcpy(&temp_name, tmpdir);
@@ -538,7 +566,7 @@ __archive_mktempx(const char *tmpdir, char *template)
        if (template == NULL) {
                archive_string_init(&temp_name);
                if (tmpdir == NULL) {
-                       if (get_tempdir(&temp_name) != ARCHIVE_OK)
+                       if (__archive_get_tempdir(&temp_name) != ARCHIVE_OK)
                                goto exit_tmpfile;
                } else
                        archive_strcpy(&temp_name, tmpdir);