LibreOffice Module shell (master) 1
SysShExec.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <algorithm>
21#include <cassert>
22#include <cstddef>
23#include <string_view>
24
25#include <osl/diagnose.h>
26#include <osl/process.h>
27#include <sal/log.hxx>
28#include "SysShExec.hxx"
29#include <osl/file.hxx>
30#include <sal/macros.h>
31#include <com/sun/star/lang/IllegalArgumentException.hpp>
32#include <com/sun/star/security/AccessControlException.hpp>
33#include <com/sun/star/system/SystemShellExecuteException.hpp>
34#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
35#include <com/sun/star/uri/UriReferenceFactory.hpp>
40#include <o3tl/string_view.hxx>
41
42#include <prewin.h>
43#include <Shlobj.h>
44#include <systools/win32/comtools.hxx>
45#include <postwin.h>
46
47using namespace ::com::sun::star::system::SystemShellExecuteFlags;
48
49namespace
50{
51 /* This is the error table that defines the mapping between OS error
52 codes and errno values */
53
54 struct errentry {
55 unsigned long oscode; /* OS return value */
56 int errnocode; /* System V error code */
57 };
58
59 struct errentry errtable[] = {
60 { ERROR_SUCCESS, osl_File_E_None }, /* 0 */
61 { ERROR_INVALID_FUNCTION, osl_File_E_INVAL }, /* 1 */
62 { ERROR_FILE_NOT_FOUND, osl_File_E_NOENT }, /* 2 */
63 { ERROR_PATH_NOT_FOUND, osl_File_E_NOENT }, /* 3 */
64 { ERROR_TOO_MANY_OPEN_FILES, osl_File_E_MFILE }, /* 4 */
65 { ERROR_ACCESS_DENIED, osl_File_E_ACCES }, /* 5 */
66 { ERROR_INVALID_HANDLE, osl_File_E_BADF }, /* 6 */
67 { ERROR_ARENA_TRASHED, osl_File_E_NOMEM }, /* 7 */
68 { ERROR_NOT_ENOUGH_MEMORY, osl_File_E_NOMEM }, /* 8 */
69 { ERROR_INVALID_BLOCK, osl_File_E_NOMEM }, /* 9 */
70 { ERROR_BAD_ENVIRONMENT, osl_File_E_2BIG }, /* 10 */
71 { ERROR_BAD_FORMAT, osl_File_E_NOEXEC }, /* 11 */
72 { ERROR_INVALID_ACCESS, osl_File_E_INVAL }, /* 12 */
73 { ERROR_INVALID_DATA, osl_File_E_INVAL }, /* 13 */
74 { ERROR_INVALID_DRIVE, osl_File_E_NOENT }, /* 15 */
75 { ERROR_CURRENT_DIRECTORY, osl_File_E_ACCES }, /* 16 */
76 { ERROR_NOT_SAME_DEVICE, osl_File_E_XDEV }, /* 17 */
77 { ERROR_NO_MORE_FILES, osl_File_E_NOENT }, /* 18 */
78 { ERROR_LOCK_VIOLATION, osl_File_E_ACCES }, /* 33 */
79 { ERROR_BAD_NETPATH, osl_File_E_NOENT }, /* 53 */
80 { ERROR_NETWORK_ACCESS_DENIED, osl_File_E_ACCES }, /* 65 */
81 { ERROR_BAD_NET_NAME, osl_File_E_NOENT }, /* 67 */
82 { ERROR_FILE_EXISTS, osl_File_E_EXIST }, /* 80 */
83 { ERROR_CANNOT_MAKE, osl_File_E_ACCES }, /* 82 */
84 { ERROR_FAIL_I24, osl_File_E_ACCES }, /* 83 */
85 { ERROR_INVALID_PARAMETER, osl_File_E_INVAL }, /* 87 */
86 { ERROR_NO_PROC_SLOTS, osl_File_E_AGAIN }, /* 89 */
87 { ERROR_DRIVE_LOCKED, osl_File_E_ACCES }, /* 108 */
88 { ERROR_BROKEN_PIPE, osl_File_E_PIPE }, /* 109 */
89 { ERROR_DISK_FULL, osl_File_E_NOSPC }, /* 112 */
90 { ERROR_INVALID_TARGET_HANDLE, osl_File_E_BADF }, /* 114 */
91 { ERROR_INVALID_HANDLE, osl_File_E_INVAL }, /* 124 */
92 { ERROR_WAIT_NO_CHILDREN, osl_File_E_CHILD }, /* 128 */
93 { ERROR_CHILD_NOT_COMPLETE, osl_File_E_CHILD }, /* 129 */
94 { ERROR_DIRECT_ACCESS_HANDLE, osl_File_E_BADF }, /* 130 */
95 { ERROR_NEGATIVE_SEEK, osl_File_E_INVAL }, /* 131 */
96 { ERROR_SEEK_ON_DEVICE, osl_File_E_ACCES }, /* 132 */
97 { ERROR_DIR_NOT_EMPTY, osl_File_E_NOTEMPTY }, /* 145 */
98 { ERROR_NOT_LOCKED, osl_File_E_ACCES }, /* 158 */
99 { ERROR_BAD_PATHNAME, osl_File_E_NOENT }, /* 161 */
100 { ERROR_MAX_THRDS_REACHED, osl_File_E_AGAIN }, /* 164 */
101 { ERROR_LOCK_FAILED, osl_File_E_ACCES }, /* 167 */
102 { ERROR_ALREADY_EXISTS, osl_File_E_EXIST }, /* 183 */
103 { ERROR_FILENAME_EXCED_RANGE, osl_File_E_NOENT }, /* 206 */
104 { ERROR_NESTING_NOT_ALLOWED, osl_File_E_AGAIN }, /* 215 */
105 { ERROR_NOT_ENOUGH_QUOTA, osl_File_E_NOMEM } /* 1816 */
106 };
107
108 /* size of the table */
109 #define ERRTABLESIZE (SAL_N_ELEMENTS(errtable))
110
111 /* The following two constants must be the minimum and maximum
112 values in the (contiguous) range of osl_File_E_xec Failure errors. */
113 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
114 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
115
116 /* These are the low and high value in the range of errors that are
117 access violations */
118 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
119 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
120
121
122 /*******************************************************************************/
123
124 oslFileError _mapError( DWORD dwError )
125 {
126 unsigned i;
127
128 /* check the table for the OS error code */
129 for ( i = 0; i < ERRTABLESIZE; ++i )
130 {
131 if ( dwError == errtable[i].oscode )
132 return static_cast<oslFileError>(errtable[i].errnocode);
133 }
134
135 /* The error code wasn't in the table. We check for a range of */
136 /* osl_File_E_ACCES errors or exec failure errors (ENOEXEC). Otherwise */
137 /* osl_File_E_INVAL is returned. */
138
139 if ( dwError >= MIN_EACCES_RANGE && dwError <= MAX_EACCES_RANGE)
140 return osl_File_E_ACCES;
141 else if ( dwError >= MIN_EXEC_ERROR && dwError <= MAX_EXEC_ERROR)
142 return osl_File_E_NOEXEC;
143 else
144 return osl_File_E_INVAL;
145 }
146
147 #define MapError( oserror ) _mapError( oserror )
148
149 #define E_UNKNOWN_EXEC_ERROR -1
150}
151
152CSysShExec::CSysShExec( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
153 WeakComponentImplHelper< css::system::XSystemShellExecute, css::lang::XServiceInfo >( m_aMutex ),
154 m_xContext(xContext),
155 mnNbCallCoInitializeExForReinit(0)
156{
157 /*
158 * As this service is declared thread-affine, it is ensured to be called from a
159 * dedicated thread, so initialize COM here.
160 *
161 * We need COM to be initialized for STA, but osl thread get initialized for MTA.
162 * Once this changed, we can remove the uninitialize call.
163 */
164 o3tl::safeCoInitializeEx(COINIT_APARTMENTTHREADED, mnNbCallCoInitializeExForReinit);
165}
167{
168 o3tl::safeCoUninitializeReinit(COINIT_MULTITHREADED, mnNbCallCoInitializeExForReinit);
169}
170
171namespace
172{
173bool checkExtension(std::u16string_view extension, std::u16string_view denylist) {
174 assert(!extension.empty());
175 for (std::size_t i = 0; i != std::u16string_view::npos;) {
176 std::u16string_view tok = o3tl::getToken(denylist, ';', i);
177 o3tl::starts_with(tok, u'.', &tok);
178 if (o3tl::equalsIgnoreAsciiCase(extension, tok)) {
179 return false;
180 }
181 }
182 return true;
183}
184
185// This callback checks if the found window is the specified process's top-level window,
186// and activates the first found such window.
187BOOL CALLBACK FindAndActivateProcWnd(HWND hwnd, LPARAM lParam)
188{
189 if (!IsWindowVisible(hwnd))
190 return TRUE; // continue enumeration
191 if (GetWindow(hwnd, GW_OWNER)) // not a top-level window
192 return TRUE; // continue enumeration
193 const DWORD nParamProcId = static_cast<DWORD>(lParam);
194 assert(nParamProcId != 0);
195 DWORD nWndProcId = 0;
196 (void)GetWindowThreadProcessId(hwnd, &nWndProcId);
197 if (nWndProcId != nParamProcId)
198 return TRUE; // continue enumeration
199
200 // Found it! Bring it to front
201 if (IsIconic(hwnd))
202 {
203 ShowWindow(hwnd, SW_RESTORE);
204 }
205 SetForegroundWindow(hwnd);
206 SetActiveWindow(hwnd);
207 return FALSE; // stop enumeration
208}
209}
210
211void SAL_CALL CSysShExec::execute( const OUString& aCommand, const OUString& aParameter, sal_Int32 nFlags )
212{
213 // parameter checking
214 if (0 == aCommand.getLength())
215 throw css::lang::IllegalArgumentException(
216 "Empty command",
217 static_cast< css::system::XSystemShellExecute* >( this ),
218 1 );
219
220 if ((nFlags & ~(NO_SYSTEM_ERROR_MESSAGE | URIS_ONLY)) != 0)
221 throw css::lang::IllegalArgumentException(
222 "Invalid Flags specified",
223 static_cast< css::system::XSystemShellExecute* >( this ),
224 3 );
225
226 OUString preprocessed_command(aCommand);
227 if ((nFlags & URIS_ONLY) != 0)
228 {
229 css::uno::Reference< css::uri::XUriReference > uri(
230 css::uri::UriReferenceFactory::create(m_xContext)->parse(aCommand));
231 if (!(uri.is() && uri->isAbsolute()))
232 {
233 throw css::lang::IllegalArgumentException(
234 "XSystemShellExecute.execute URIS_ONLY with"
235 " non-absolute URI reference "
236 + aCommand,
237 getXWeak(), 0);
238 }
239 if (uri->getScheme().equalsIgnoreAsciiCase("file")) {
240 // ShellExecuteExW appears to ignore the fragment of a file URL anyway, so remove it:
241 uri->clearFragment();
242 OUString pathname;
243 auto const e1
244 = osl::FileBase::getSystemPathFromFileURL(uri->getUriReference(), pathname);
245 if (e1 != osl::FileBase::E_None) {
246 throw css::lang::IllegalArgumentException(
247 ("XSystemShellExecute.execute, getSystemPathFromFileURL <" + aCommand
248 + "> failed with " + OUString::number(e1)),
249 {}, 0);
250 }
251 const int MAX_LONG_PATH = 32767; // max longpath on WinNT
252 if (pathname.getLength() >= MAX_LONG_PATH)
253 {
254 throw css::lang::IllegalArgumentException(
255 "XSystemShellExecute.execute, path <" + pathname + "> too long", {}, 0);
256 }
257 preprocessed_command = pathname;
258 wchar_t path[MAX_LONG_PATH];
259 wcscpy_s(path, o3tl::toW(pathname.getStr()));
260 for (int i = 0;; ++i) {
261 // tdf#130216: normalize c:\path\to\something\..\else into c:\path\to\else
262 if (PathResolve(path, nullptr, PRF_VERIFYEXISTS | PRF_REQUIREABSOLUTE) == 0)
263 {
264 throw css::lang::IllegalArgumentException(
265 OUString::Concat(u"XSystemShellExecute.execute, PathResolve(") + o3tl::toU(path)
266 + ") failed",
267 {}, 0);
268 }
269 SHFILEINFOW info;
270 if (SHGetFileInfoW(path, 0, &info, sizeof info, SHGFI_EXETYPE) != 0)
271 {
272 throw css::security::AccessControlException(
273 "XSystemShellExecute.execute, cannot process <" + aCommand + ">", {}, {});
274 }
275 if (SHGetFileInfoW(path, 0, &info, sizeof info, SHGFI_ATTRIBUTES) == 0)
276 {
277 throw css::lang::IllegalArgumentException(
278 OUString::Concat(u"XSystemShellExecute.execute, SHGetFileInfoW(") + o3tl::toU(path) + ") failed", {},
279 0);
280 }
281 if ((info.dwAttributes & SFGAO_LINK) == 0) {
282 break;
283 }
284 sal::systools::COMReference<IShellLinkW> link;
285 try
286 {
287 link.CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER);
288 }
289 catch (sal::systools::ComError& e)
290 {
291 throw css::lang::IllegalArgumentException(
292 ("XSystemShellExecute.execute, CoCreateInstance failed with "
293 + OUString::number(e.GetHresult())),
294 {}, 0);
295 }
296 sal::systools::COMReference<IPersistFile> file;
297 try {
298 file = link.QueryInterface<IPersistFile>(sal::systools::COM_QUERY_THROW);
299 } catch(sal::systools::ComError & e3) {
300 throw css::lang::IllegalArgumentException(
301 ("XSystemShellExecute.execute, QueryInterface failed with: "
302 + o3tl::runtimeToOUString(e3.what())),
303 {}, 0);
304 }
305 HRESULT e2 = file->Load(path, STGM_READ);
306 if (FAILED(e2)) {
307 throw css::lang::IllegalArgumentException(
308 ("XSystemShellExecute.execute, IPersistFile.Load failed with "
309 + OUString::number(e2)),
310 {}, 0);
311 }
312 e2 = link->Resolve(nullptr, SLR_UPDATE | SLR_NO_UI);
313 if (FAILED(e2)) {
314 throw css::lang::IllegalArgumentException(
315 ("XSystemShellExecute.execute, IShellLink.Resolve failed with "
316 + OUString::number(e2)),
317 {}, 0);
318 }
319 WIN32_FIND_DATAW wfd;
320 e2 = link->GetPath(path, SAL_N_ELEMENTS(path), &wfd, SLGP_RAWPATH);
321 if (FAILED(e2)) {
322 throw css::lang::IllegalArgumentException(
323 ("XSystemShellExecute.execute, IShellLink.GetPath failed with "
324 + OUString::number(e2)),
325 {}, 0);
326 }
327 // Fail at some arbitrary nesting depth, to avoid an infinite loop:
328 if (i == 30) {
329 throw css::lang::IllegalArgumentException(
330 "XSystemShellExecute.execute, link depth exceeded for <" + aCommand + ">",
331 {}, 0);
332 }
333 }
334 pathname = o3tl::toU(path);
335 // ShellExecuteExW appears to ignore trailing dots, so remove them:
336 while (pathname.endsWith(".", &pathname)) {}
337 auto const n = pathname.lastIndexOf('.');
338 if (n > pathname.lastIndexOf('\\')) {
339 auto const ext = pathname.copy(n + 1);
340 if (!ext.isEmpty()) {
341 OUString env;
342 if (osl_getEnvironment(OUString("PATHEXT").pData, &env.pData)
343 != osl_Process_E_None)
344 {
345 SAL_INFO("shell", "osl_getEnvironment(PATHEXT) failed");
346 }
347 if (!(checkExtension(ext, env)
348 && checkExtension(
349 ext,
350 u".ADE;.ADP;.APK;.APPLICATION;.APPX;.APPXBUNDLE;.BAT;.CAB;.CHM;.CLASS;"
351 ".CMD;.COM;.CPL;.DLL;.DMG;.EX;.EX_;.EXE;.GADGET;.HTA;.INF;.INS;.IPA;"
352 ".ISO;.ISP;.JAR;.JS;.JSE;.LIB;.LNK;.MDE;.MSC;.MSH;.MSH1;.MSH2;.MSHXML;"
353 ".MSH1XML;.MSH2XML;.MSI;.MSIX;.MSIXBUNDLE;.MSP;.MST;.NSH;.PIF;.PS1;"
354 ".PS1XML;.PS2;.PS2XML;.PSC1;.PSC2;.PY;.REG;.SCF;.SCR;.SCT;.SHB;.SYS;"
355 ".VB;.VBE;.VBS;.VXD;.WS;.WSC;.WSF;.WSH;")))
356 {
357 throw css::security::AccessControlException(
358 "XSystemShellExecute.execute, cannot process <" + aCommand + ">", {},
359 {});
360 }
361 }
362 }
363 }
364 }
365
366 SHELLEXECUTEINFOW sei;
367 ZeroMemory(&sei, sizeof( sei));
368
369 sei.cbSize = sizeof(sei);
370 sei.lpFile = o3tl::toW(preprocessed_command.getStr());
371 sei.lpParameters = o3tl::toW(aParameter.getStr());
372 sei.nShow = SW_SHOWNORMAL;
373 sei.fMask = SEE_MASK_NOCLOSEPROCESS; // we need sei.hProcess
374
375 if (NO_SYSTEM_ERROR_MESSAGE & nFlags)
376 sei.fMask |= SEE_MASK_FLAG_NO_UI;
377
378 SetLastError( 0 );
379
380 bool bRet = ShellExecuteExW(&sei);
381
382 if (!bRet && (nFlags & NO_SYSTEM_ERROR_MESSAGE))
383 {
384 // ShellExecuteEx fails to set an error code
385 // we return osl_File_E_INVAL
386 sal_Int32 psxErr = GetLastError();
387 if (ERROR_SUCCESS == psxErr)
388 psxErr = E_UNKNOWN_EXEC_ERROR;
389 else
390 psxErr = MapError(psxErr);
391
392 throw css::system::SystemShellExecuteException(
393 "Error executing command",
394 static_cast< css::system::XSystemShellExecute* >(this),
395 psxErr);
396 }
397 else
398 {
399 // Get Permission make changes to the Window of the created Process
400 const DWORD procId = GetProcessId(sei.hProcess);
401 if (procId != 0)
402 {
403 AllowSetForegroundWindow(procId);
404 WaitForInputIdle(sei.hProcess, 1000); // so that main window is created; imperfect
405 EnumWindows(FindAndActivateProcWnd, static_cast<LPARAM>(procId));
406 }
407 }
408
409 // Close the handle for the created childprocess when we are done
410 CloseHandle(sei.hProcess);
411}
412
413// XServiceInfo
414
416{
417 return "com.sun.star.sys.shell.SystemShellExecute";
418}
419
420sal_Bool SAL_CALL CSysShExec::supportsService( const OUString& ServiceName )
421{
423}
424
425css::uno::Sequence< OUString > SAL_CALL CSysShExec::getSupportedServiceNames( )
426{
427 return { "com.sun.star.system.SystemShellExecute" };
428}
429
430extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
432 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
433{
434 return cppu::acquire(new CSysShExec(context));
435}
436/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
#define E_UNKNOWN_EXEC_ERROR
Definition: SysShExec.cxx:149
#define MapError(oserror)
Definition: SysShExec.cxx:147
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * shell_CSysShExec_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: SysShExec.cxx:431
#define MAX_EXEC_ERROR
Definition: SysShExec.cxx:114
#define MIN_EACCES_RANGE
Definition: SysShExec.cxx:118
#define MIN_EXEC_ERROR
Definition: SysShExec.cxx:113
#define MAX_EACCES_RANGE
Definition: SysShExec.cxx:119
#define ERRTABLESIZE
Definition: SysShExec.cxx:109
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: SysShExec.cxx:420
virtual OUString SAL_CALL getImplementationName() override
Definition: SysShExec.cxx:415
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: SysShExec.cxx:425
virtual void SAL_CALL execute(const OUString &aCommand, const OUString &aParameter, sal_Int32 nFlags) override
Definition: SysShExec.cxx:211
CSysShExec(const css::uno::Reference< css::uno::XComponentContext > &xContext)
Definition: SysShExec.cxx:152
int mnNbCallCoInitializeExForReinit
Definition: SysShExec.hxx:47
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: SysShExec.hxx:45
float u
std::mutex m_aMutex
#define TRUE
#define FALSE
sal_Int64 n
#define SAL_INFO(area, stream)
#define SAL_N_ELEMENTS(arr)
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
std::unique_ptr< sal_Int32[]> pData
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
OUString runtimeToOUString(char const *runtimeString)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
bool parse(OUString const &uri, SourceProviderScannerData *data)
#define CALLBACK
const wchar_t *typedef BOOL
OUString aCommand
unsigned char sal_Bool