Develop and Download Open Source Software

Browse Subversion Repository

Contents of /hamigaki/trunk/libs/archivers/example/untar.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1782 - (show annotations) (download) (as text)
Sun Jun 10 06:01:53 2018 UTC (5 years, 9 months ago) by hamigaki
File MIME type: text/x-c++src
File size: 4265 byte(s)
fix for Zip Slip
1 // untar.cpp: a simple ZIP decompressing program
2
3 // Copyright Takeshi Mouri 2006-2018.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // See http://hamigaki.sourceforge.jp/libs/archivers for library home page.
9
10
11 // Security warning:
12 // This program never check the validity of paths in the archive.
13 // See http://www.forest.impress.co.jp/article/2004/07/30/arcsecurity.html .
14 // (The above link is Japanese site)
15
16
17 #include <hamigaki/archivers/tar_file.hpp>
18 #include <hamigaki/filesystem/operations.hpp>
19 #include <boost/filesystem/convenience.hpp>
20 #include <boost/iostreams/copy.hpp>
21 #include <clocale>
22 #include <exception>
23 #include <iostream>
24
25 namespace ar = hamigaki::archivers;
26 namespace fs_ex = hamigaki::filesystem;
27 namespace io_ex = hamigaki::iostreams;
28 namespace fs = boost::filesystem;
29 namespace io = boost::iostreams;
30
31 template<class Path>
32 inline bool has_parent_path(const Path& ph)
33 {
34 #if BOOST_VERSION < 103600
35 return ph.has_branch_path();
36 #else
37 return ph.has_parent_path();
38 #endif
39 }
40
41 template<class Path>
42 bool is_valid_path(const Path& ph)
43 {
44 #if !defined(HAMIGAKI_ALLOW_DIRECTORY_TRAVERSAL)
45 if (ph.has_root_name() || ph.has_root_directory())
46 return false;
47 for (typename Path::iterator it = ph.begin(); it != ph.end(); ++it)
48 {
49 if (*it == "..")
50 return false;
51 }
52 #endif
53 return true;
54 }
55
56 int main(int argc, char* argv[])
57 {
58 try
59 {
60 if (argc != 2)
61 {
62 std::cerr << "Usage: untar (archive)" << std::endl;
63 return 1;
64 }
65
66 std::setlocale(LC_ALL, "");
67
68 ar::tar_file_source tar(argv[1]);
69
70 while (tar.next_entry())
71 {
72 const ar::tar::header& head = tar.header();
73
74 std::cout << head.path.string() << '\n';
75 if (!is_valid_path(head.path))
76 {
77 std::cerr << "Warning: invalid path" << '\n';
78 continue;
79 }
80
81 if (head.type_flag == ar::tar::type_flag::link)
82 {
83 if (::has_parent_path(head.path))
84 fs::create_directories(head.path.branch_path());
85
86 fs_ex::create_hard_link(head.link_path, head.path);
87 }
88 else if (head.type_flag == ar::tar::type_flag::symlink)
89 {
90 if (::has_parent_path(head.path))
91 fs::create_directories(head.path.branch_path());
92
93 fs_ex::create_symlink(head.link_path, head.path);
94 }
95 else if (head.type_flag == ar::tar::type_flag::directory)
96 fs::create_directories(head.path);
97 else if (head.type_flag == ar::tar::type_flag::char_device)
98 std::cerr << "Warning: skip character device\n";
99 else if (head.type_flag == ar::tar::type_flag::block_device)
100 std::cerr << "Warning: skip block device\n";
101 else if (head.type_flag == ar::tar::type_flag::fifo)
102 std::cerr << "Warning: skip FIFO file\n";
103 // Note: All unknown types are treated as a regular file.
104 else
105 {
106 if (::has_parent_path(head.path))
107 fs::create_directories(head.path.branch_path());
108
109 io::copy(
110 tar,
111 io_ex::file_sink(
112 head.path.file_string(), std::ios_base::binary)
113 );
114 }
115
116 // Note:
117 // The POSIX chown() clears S_ISUID/S_ISGID bits.
118 // So, we must call change_symlink_owner()
119 // before calling change_permissions().
120 fs_ex::error_code ec;
121 fs_ex::change_symlink_owner(head.path, head.uid, head.gid, ec);
122
123 fs_ex::change_permissions(head.path, head.permissions, ec);
124
125 if (head.modified_time)
126 fs_ex::last_write_time(head.path, *head.modified_time);
127 if (head.access_time)
128 fs_ex::last_access_time(head.path, *head.access_time);
129 }
130
131 return 0;
132 }
133 catch (const std::exception& e)
134 {
135 std::cerr << "Error: " << e.what() << std::endl;
136 }
137 return 1;
138 }

Properties

Name Value
svn:eol-style native

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26