1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <chainparams.h>
7 #include <fs.h>
8 #include <logging.h>
9 #include <wallet/db.h>
10 
11 #include <string>
12 
ListDatabases(const fs::path & wallet_dir)13 std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
14 {
15     const size_t offset = wallet_dir.string().size() + (wallet_dir == wallet_dir.root_name() ? 0 : 1);
16     std::vector<fs::path> paths;
17     boost::system::error_code ec;
18 
19     for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
20         if (ec) {
21             if (fs::is_directory(*it)) {
22                 it.no_push();
23                 LogPrintf("%s: %s %s -- skipping.\n", __func__, ec.message(), it->path().string());
24             } else {
25                 LogPrintf("%s: %s %s\n", __func__, ec.message(), it->path().string());
26             }
27             continue;
28         }
29 
30         try {
31             // Get wallet path relative to walletdir by removing walletdir from the wallet path.
32             // This can be replaced by boost::filesystem::lexically_relative once boost is bumped to 1.60.
33             const fs::path path = it->path().string().substr(offset);
34 
35             if (it->status().type() == fs::directory_file &&
36                 (IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
37                 // Found a directory which contains wallet.dat btree file, add it as a wallet.
38                 paths.emplace_back(path);
39             } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBDBFile(it->path())) {
40                 if (it->path().filename() == "wallet.dat") {
41                     // Found top-level wallet.dat btree file, add top level directory ""
42                     // as a wallet.
43                     paths.emplace_back();
44                 } else {
45                     // Found top-level btree file not called wallet.dat. Current bitcoin
46                     // software will never create these files but will allow them to be
47                     // opened in a shared database environment for backwards compatibility.
48                     // Add it to the list of available wallets.
49                     paths.emplace_back(path);
50                 }
51             }
52         } catch (const std::exception& e) {
53             LogPrintf("%s: Error scanning %s: %s\n", __func__, it->path().string(), e.what());
54             it.no_push();
55         }
56     }
57 
58     return paths;
59 }
60 
BDBDataFile(const fs::path & wallet_path)61 fs::path BDBDataFile(const fs::path& wallet_path)
62 {
63     if (fs::is_regular_file(wallet_path)) {
64         // Special case for backwards compatibility: if wallet path points to an
65         // existing file, treat it as the path to a BDB data file in a parent
66         // directory that also contains BDB log files.
67         return wallet_path;
68     } else {
69         // Normal case: Interpret wallet path as a directory path containing
70         // data and log files.
71         return wallet_path / "wallet.dat";
72     }
73 }
74 
SQLiteDataFile(const fs::path & path)75 fs::path SQLiteDataFile(const fs::path& path)
76 {
77     return path / "wallet.dat";
78 }
79 
IsBDBFile(const fs::path & path)80 bool IsBDBFile(const fs::path& path)
81 {
82     if (!fs::exists(path)) return false;
83 
84     // A Berkeley DB Btree file has at least 4K.
85     // This check also prevents opening lock files.
86     boost::system::error_code ec;
87     auto size = fs::file_size(path, ec);
88     if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string());
89     if (size < 4096) return false;
90 
91     fsbridge::ifstream file(path, std::ios::binary);
92     if (!file.is_open()) return false;
93 
94     file.seekg(12, std::ios::beg); // Magic bytes start at offset 12
95     uint32_t data = 0;
96     file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic
97 
98     // Berkeley DB Btree magic bytes, from:
99     //  https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75
100     //  - big endian systems - 00 05 31 62
101     //  - little endian systems - 62 31 05 00
102     return data == 0x00053162 || data == 0x62310500;
103 }
104 
IsSQLiteFile(const fs::path & path)105 bool IsSQLiteFile(const fs::path& path)
106 {
107     if (!fs::exists(path)) return false;
108 
109     // A SQLite Database file is at least 512 bytes.
110     boost::system::error_code ec;
111     auto size = fs::file_size(path, ec);
112     if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string());
113     if (size < 512) return false;
114 
115     fsbridge::ifstream file(path, std::ios::binary);
116     if (!file.is_open()) return false;
117 
118     // Magic is at beginning and is 16 bytes long
119     char magic[16];
120     file.read(magic, 16);
121 
122     // Application id is at offset 68 and 4 bytes long
123     file.seekg(68, std::ios::beg);
124     char app_id[4];
125     file.read(app_id, 4);
126 
127     file.close();
128 
129     // Check the magic, see https://sqlite.org/fileformat2.html
130     std::string magic_str(magic, 16);
131     if (magic_str != std::string("SQLite format 3", 16)) {
132         return false;
133     }
134 
135     // Check the application id matches our network magic
136     return memcmp(Params().MessageStart(), app_id, 4) == 0;
137 }
138