1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qstandardpaths.h"
41 
42 #include <qdir.h>
43 #include <private/qsystemlibrary_p.h>
44 #include <qstringlist.h>
45 
46 #ifndef QT_BOOTSTRAPPED
47 #include <qcoreapplication.h>
48 #endif
49 
50 #include <qoperatingsystemversion.h>
51 #include <qt_windows.h>
52 #include <shlobj.h>
53 #include <intshcut.h>
54 #include <qvarlengtharray.h>
55 
56 #ifndef QT_NO_STANDARDPATHS
57 
58 QT_BEGIN_NAMESPACE
59 
convertCharArray(const wchar_t * path)60 static QString convertCharArray(const wchar_t *path)
61 {
62     return QDir::fromNativeSeparators(QString::fromWCharArray(path));
63 }
64 
isGenericConfigLocation(QStandardPaths::StandardLocation type)65 static inline bool isGenericConfigLocation(QStandardPaths::StandardLocation type)
66 {
67     return type == QStandardPaths::GenericConfigLocation || type == QStandardPaths::GenericDataLocation;
68 }
69 
isConfigLocation(QStandardPaths::StandardLocation type)70 static inline bool isConfigLocation(QStandardPaths::StandardLocation type)
71 {
72     return type == QStandardPaths::ConfigLocation || type == QStandardPaths::AppConfigLocation
73         || type == QStandardPaths::AppDataLocation || type == QStandardPaths::AppLocalDataLocation
74         || isGenericConfigLocation(type);
75 }
76 
appendOrganizationAndApp(QString & path)77 static void appendOrganizationAndApp(QString &path) // Courtesy qstandardpaths_unix.cpp
78 {
79 #ifndef QT_BOOTSTRAPPED
80     const QString &org = QCoreApplication::organizationName();
81     if (!org.isEmpty())
82         path += QLatin1Char('/') + org;
83     const QString &appName = QCoreApplication::applicationName();
84     if (!appName.isEmpty())
85         path += QLatin1Char('/') + appName;
86 #else // !QT_BOOTSTRAPPED
87     Q_UNUSED(path)
88 #endif
89 }
90 
appendTestMode(QString & path)91 static inline void appendTestMode(QString &path)
92 {
93     if (QStandardPaths::isTestModeEnabled())
94         path += QLatin1String("/qttest");
95 }
96 
isProcessLowIntegrity()97 static bool isProcessLowIntegrity() {
98 #ifdef Q_CC_MINGW
99     // GetCurrentProcessToken was introduced in MinGW w64 in v7
100     // Disable function until Qt CI is updated
101     return false;
102 #else
103     if (QOperatingSystemVersion::current() < QOperatingSystemVersion::Windows8)
104         return false;
105     // non-leaking pseudo-handle. Expanded inline function GetCurrentProcessToken()
106     // (was made an inline function in Windows 8).
107     const auto process_token = HANDLE(quintptr(-4));
108 
109     QVarLengthArray<char,256> token_info_buf(256);
110     auto* token_info = reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_info_buf.data());
111     DWORD token_info_length = token_info_buf.size();
112     if (!GetTokenInformation(process_token, TokenIntegrityLevel, token_info, token_info_length, &token_info_length)) {
113         // grow bufer and retry GetTokenInformation
114         token_info_buf.resize(token_info_length);
115         token_info = reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_info_buf.data());
116         if (!GetTokenInformation(process_token, TokenIntegrityLevel, token_info, token_info_length, &token_info_length))
117             return false; // assume "normal" process
118     }
119 
120     // The GetSidSubAuthorityCount return-code is undefined on failure, so
121     // there's no point in checking before dereferencing
122     DWORD integrity_level = *GetSidSubAuthority(token_info->Label.Sid, *GetSidSubAuthorityCount(token_info->Label.Sid) - 1);
123     return (integrity_level < SECURITY_MANDATORY_MEDIUM_RID);
124 #endif
125 }
126 
127 // Map QStandardPaths::StandardLocation to KNOWNFOLDERID of SHGetKnownFolderPath()
writableSpecialFolderId(QStandardPaths::StandardLocation type)128 static GUID writableSpecialFolderId(QStandardPaths::StandardLocation type)
129 {
130     // folders for medium & high integrity processes
131     static const GUID folderIds[] = {
132         FOLDERID_Desktop,       // DesktopLocation
133         FOLDERID_Documents,     // DocumentsLocation
134         FOLDERID_Fonts,         // FontsLocation
135         FOLDERID_Programs,      // ApplicationsLocation
136         FOLDERID_Music,         // MusicLocation
137         FOLDERID_Videos,        // MoviesLocation
138         FOLDERID_Pictures,      // PicturesLocation
139         GUID(), GUID(),         // TempLocation/HomeLocation
140         FOLDERID_LocalAppData,  // AppLocalDataLocation ("Local" path), AppLocalDataLocation = DataLocation
141         GUID(),                 // CacheLocation
142         FOLDERID_LocalAppData,  // GenericDataLocation ("Local" path)
143         GUID(),                 // RuntimeLocation
144         FOLDERID_LocalAppData,  // ConfigLocation ("Local" path)
145         GUID(), GUID(),         // DownloadLocation/GenericCacheLocation
146         FOLDERID_LocalAppData,  // GenericConfigLocation ("Local" path)
147         FOLDERID_RoamingAppData,// AppDataLocation ("Roaming" path)
148         FOLDERID_LocalAppData,  // AppConfigLocation ("Local" path)
149     };
150     Q_STATIC_ASSERT(sizeof(folderIds) / sizeof(folderIds[0]) == size_t(QStandardPaths::AppConfigLocation + 1));
151 
152     // folders for low integrity processes
153     static const GUID folderIds_li[] = {
154         FOLDERID_Desktop,        // DesktopLocation
155         FOLDERID_Documents,      // DocumentsLocation
156         FOLDERID_Fonts,          // FontsLocation
157         FOLDERID_Programs,       // ApplicationsLocation
158         FOLDERID_Music,          // MusicLocation
159         FOLDERID_Videos,         // MoviesLocation
160         FOLDERID_Pictures,       // PicturesLocation
161         GUID(), GUID(),          // TempLocation/HomeLocation
162         FOLDERID_LocalAppDataLow,// AppLocalDataLocation ("Local" path), AppLocalDataLocation = DataLocation
163         GUID(),                  // CacheLocation
164         FOLDERID_LocalAppDataLow,// GenericDataLocation ("Local" path)
165         GUID(),                  // RuntimeLocation
166         FOLDERID_LocalAppDataLow,// ConfigLocation ("Local" path)
167         GUID(), GUID(),          // DownloadLocation/GenericCacheLocation
168         FOLDERID_LocalAppDataLow,// GenericConfigLocation ("Local" path)
169         FOLDERID_RoamingAppData, // AppDataLocation ("Roaming" path)
170         FOLDERID_LocalAppDataLow,// AppConfigLocation ("Local" path)
171     };
172     Q_STATIC_ASSERT(sizeof(folderIds_li) == sizeof(folderIds));
173 
174     static bool low_integrity_process = isProcessLowIntegrity();
175     if (size_t(type) < sizeof(folderIds) / sizeof(folderIds[0]))
176         return low_integrity_process ? folderIds_li[type] : folderIds[type];
177     return GUID();
178 }
179 
180 // Convenience for SHGetKnownFolderPath().
sHGetKnownFolderPath(const GUID & clsid)181 static QString sHGetKnownFolderPath(const GUID &clsid)
182 {
183     QString result;
184     typedef HRESULT (WINAPI *GetKnownFolderPath)(const GUID&, DWORD, HANDLE, LPWSTR*);
185 
186     static const GetKnownFolderPath sHGetKnownFolderPath = // Vista onwards.
187         reinterpret_cast<GetKnownFolderPath>(QSystemLibrary::resolve(QLatin1String("shell32"), "SHGetKnownFolderPath"));
188 
189     LPWSTR path;
190     if (Q_LIKELY(sHGetKnownFolderPath && SUCCEEDED(sHGetKnownFolderPath(clsid, KF_FLAG_DONT_VERIFY, 0, &path)))) {
191         result = convertCharArray(path);
192         CoTaskMemFree(path);
193     }
194     return result;
195 }
196 
writableLocation(StandardLocation type)197 QString QStandardPaths::writableLocation(StandardLocation type)
198 {
199     QString result;
200     switch (type) {
201     case DownloadLocation:
202         result = sHGetKnownFolderPath(FOLDERID_Downloads);
203         if (result.isEmpty())
204             result = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
205         break;
206 
207     case CacheLocation:
208         // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache
209         // location for everyone.  Most applications seem to be using a
210         // cache directory located in their AppData directory
211         result = sHGetKnownFolderPath(writableSpecialFolderId(AppLocalDataLocation));
212         if (!result.isEmpty()) {
213             appendTestMode(result);
214             appendOrganizationAndApp(result);
215             result += QLatin1String("/cache");
216         }
217         break;
218 
219     case GenericCacheLocation:
220         result = sHGetKnownFolderPath(writableSpecialFolderId(GenericDataLocation));
221         if (!result.isEmpty()) {
222             appendTestMode(result);
223             result += QLatin1String("/cache");
224         }
225         break;
226 
227     case RuntimeLocation:
228     case HomeLocation:
229         result = QDir::homePath();
230         break;
231 
232     case TempLocation:
233         result = QDir::tempPath();
234         break;
235 
236     default:
237         result = sHGetKnownFolderPath(writableSpecialFolderId(type));
238         if (!result.isEmpty() && isConfigLocation(type)) {
239             appendTestMode(result);
240             if (!isGenericConfigLocation(type))
241                 appendOrganizationAndApp(result);
242         }
243         break;
244     }
245     return result;
246 }
247 
248 #ifndef QT_BOOTSTRAPPED
249 extern QString qAppFileName();
250 #endif
251 
standardLocations(StandardLocation type)252 QStringList QStandardPaths::standardLocations(StandardLocation type)
253 {
254     QStringList dirs;
255     const QString localDir = writableLocation(type);
256     if (!localDir.isEmpty())
257         dirs.append(localDir);
258 
259     // type-specific handling goes here
260     if (isConfigLocation(type)) {
261         QString programData = sHGetKnownFolderPath(FOLDERID_ProgramData);
262         if (!programData.isEmpty()) {
263             if (!isGenericConfigLocation(type))
264                 appendOrganizationAndApp(programData);
265             dirs.append(programData);
266         }
267 #ifndef QT_BOOTSTRAPPED
268         // Note: QCoreApplication::applicationDirPath(), while static, requires
269         // an application instance. But we might need to resolve the standard
270         // locations earlier than that, so we fall back to qAppFileName().
271         QString applicationDirPath = qApp ? QCoreApplication::applicationDirPath()
272             : QFileInfo(qAppFileName()).path();
273         dirs.append(applicationDirPath);
274         const QString dataDir = applicationDirPath + QLatin1String("/data");
275         dirs.append(dataDir);
276 
277         if (!isGenericConfigLocation(type)) {
278             QString appDataDir = dataDir;
279             appendOrganizationAndApp(appDataDir);
280             if (appDataDir != dataDir)
281                 dirs.append(appDataDir);
282         }
283 #endif // !QT_BOOTSTRAPPED
284     } // isConfigLocation()
285 
286     return dirs;
287 }
288 
289 QT_END_NAMESPACE
290 
291 #endif // QT_NO_STANDARDPATHS
292