Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / shell / source / win32 / SysShExec.cxx
blobccf932e71d03e3096f040139fcd73087edee9a73
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
21 #include <cassert>
23 #include <osl/diagnose.h>
24 #include <osl/process.h>
25 #include <sal/log.hxx>
26 #include "SysShExec.hxx"
27 #include <osl/file.hxx>
28 #include <sal/macros.h>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/system/SystemShellExecuteException.hpp>
31 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
32 #include <com/sun/star/uri/UriReferenceFactory.hpp>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <o3tl/char16_t2wchar_t.hxx>
35 #include <o3tl/runtimetooustring.hxx>
36 #include <rtl/uri.hxx>
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h>
40 #include <shellapi.h>
41 #include <Shobjidl.h>
42 #include <objbase.h>
44 #include <systools/win32/comtools.hxx>
46 using com::sun::star::uno::Reference;
47 using com::sun::star::uno::RuntimeException;
48 using com::sun::star::uno::Sequence;
49 using com::sun::star::lang::XServiceInfo;
50 using com::sun::star::lang::IllegalArgumentException;
51 using com::sun::star::system::XSystemShellExecute;
52 using com::sun::star::system::SystemShellExecuteException;
54 using namespace ::com::sun::star::system::SystemShellExecuteFlags;
55 using namespace cppu;
57 #define SYSSHEXEC_IMPL_NAME "com.sun.star.sys.shell.SystemShellExecute"
59 namespace
61 Sequence< OUString > SysShExec_getSupportedServiceNames()
63 Sequence< OUString > aRet { "com.sun.star.system.SystemShellExecute" };
64 return aRet;
67 /* This is the error table that defines the mapping between OS error
68 codes and errno values */
70 struct errentry {
71 unsigned long oscode; /* OS return value */
72 int errnocode; /* System V error code */
75 struct errentry errtable[] = {
76 { ERROR_SUCCESS, osl_File_E_None }, /* 0 */
77 { ERROR_INVALID_FUNCTION, osl_File_E_INVAL }, /* 1 */
78 { ERROR_FILE_NOT_FOUND, osl_File_E_NOENT }, /* 2 */
79 { ERROR_PATH_NOT_FOUND, osl_File_E_NOENT }, /* 3 */
80 { ERROR_TOO_MANY_OPEN_FILES, osl_File_E_MFILE }, /* 4 */
81 { ERROR_ACCESS_DENIED, osl_File_E_ACCES }, /* 5 */
82 { ERROR_INVALID_HANDLE, osl_File_E_BADF }, /* 6 */
83 { ERROR_ARENA_TRASHED, osl_File_E_NOMEM }, /* 7 */
84 { ERROR_NOT_ENOUGH_MEMORY, osl_File_E_NOMEM }, /* 8 */
85 { ERROR_INVALID_BLOCK, osl_File_E_NOMEM }, /* 9 */
86 { ERROR_BAD_ENVIRONMENT, osl_File_E_2BIG }, /* 10 */
87 { ERROR_BAD_FORMAT, osl_File_E_NOEXEC }, /* 11 */
88 { ERROR_INVALID_ACCESS, osl_File_E_INVAL }, /* 12 */
89 { ERROR_INVALID_DATA, osl_File_E_INVAL }, /* 13 */
90 { ERROR_INVALID_DRIVE, osl_File_E_NOENT }, /* 15 */
91 { ERROR_CURRENT_DIRECTORY, osl_File_E_ACCES }, /* 16 */
92 { ERROR_NOT_SAME_DEVICE, osl_File_E_XDEV }, /* 17 */
93 { ERROR_NO_MORE_FILES, osl_File_E_NOENT }, /* 18 */
94 { ERROR_LOCK_VIOLATION, osl_File_E_ACCES }, /* 33 */
95 { ERROR_BAD_NETPATH, osl_File_E_NOENT }, /* 53 */
96 { ERROR_NETWORK_ACCESS_DENIED, osl_File_E_ACCES }, /* 65 */
97 { ERROR_BAD_NET_NAME, osl_File_E_NOENT }, /* 67 */
98 { ERROR_FILE_EXISTS, osl_File_E_EXIST }, /* 80 */
99 { ERROR_CANNOT_MAKE, osl_File_E_ACCES }, /* 82 */
100 { ERROR_FAIL_I24, osl_File_E_ACCES }, /* 83 */
101 { ERROR_INVALID_PARAMETER, osl_File_E_INVAL }, /* 87 */
102 { ERROR_NO_PROC_SLOTS, osl_File_E_AGAIN }, /* 89 */
103 { ERROR_DRIVE_LOCKED, osl_File_E_ACCES }, /* 108 */
104 { ERROR_BROKEN_PIPE, osl_File_E_PIPE }, /* 109 */
105 { ERROR_DISK_FULL, osl_File_E_NOSPC }, /* 112 */
106 { ERROR_INVALID_TARGET_HANDLE, osl_File_E_BADF }, /* 114 */
107 { ERROR_INVALID_HANDLE, osl_File_E_INVAL }, /* 124 */
108 { ERROR_WAIT_NO_CHILDREN, osl_File_E_CHILD }, /* 128 */
109 { ERROR_CHILD_NOT_COMPLETE, osl_File_E_CHILD }, /* 129 */
110 { ERROR_DIRECT_ACCESS_HANDLE, osl_File_E_BADF }, /* 130 */
111 { ERROR_NEGATIVE_SEEK, osl_File_E_INVAL }, /* 131 */
112 { ERROR_SEEK_ON_DEVICE, osl_File_E_ACCES }, /* 132 */
113 { ERROR_DIR_NOT_EMPTY, osl_File_E_NOTEMPTY }, /* 145 */
114 { ERROR_NOT_LOCKED, osl_File_E_ACCES }, /* 158 */
115 { ERROR_BAD_PATHNAME, osl_File_E_NOENT }, /* 161 */
116 { ERROR_MAX_THRDS_REACHED, osl_File_E_AGAIN }, /* 164 */
117 { ERROR_LOCK_FAILED, osl_File_E_ACCES }, /* 167 */
118 { ERROR_ALREADY_EXISTS, osl_File_E_EXIST }, /* 183 */
119 { ERROR_FILENAME_EXCED_RANGE, osl_File_E_NOENT }, /* 206 */
120 { ERROR_NESTING_NOT_ALLOWED, osl_File_E_AGAIN }, /* 215 */
121 { ERROR_NOT_ENOUGH_QUOTA, osl_File_E_NOMEM } /* 1816 */
124 /* size of the table */
125 #define ERRTABLESIZE (SAL_N_ELEMENTS(errtable))
127 /* The following two constants must be the minimum and maximum
128 values in the (contiguous) range of osl_File_E_xec Failure errors. */
129 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
130 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
132 /* These are the low and high value in the range of errors that are
133 access violations */
134 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
135 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
138 /*******************************************************************************/
140 oslFileError _mapError( DWORD dwError )
142 unsigned i;
144 /* check the table for the OS error code */
145 for ( i = 0; i < ERRTABLESIZE; ++i )
147 if ( dwError == errtable[i].oscode )
148 return static_cast<oslFileError>(errtable[i].errnocode);
151 /* The error code wasn't in the table. We check for a range of */
152 /* osl_File_E_ACCES errors or exec failure errors (ENOEXEC). Otherwise */
153 /* osl_File_E_INVAL is returned. */
155 if ( dwError >= MIN_EACCES_RANGE && dwError <= MAX_EACCES_RANGE)
156 return osl_File_E_ACCES;
157 else if ( dwError >= MIN_EXEC_ERROR && dwError <= MAX_EXEC_ERROR)
158 return osl_File_E_NOEXEC;
159 else
160 return osl_File_E_INVAL;
163 #define MapError( oserror ) _mapError( oserror )
165 #define E_UNKNOWN_EXEC_ERROR -1
168 bool is_system_path(const OUString& path_or_uri)
170 OUString url;
171 osl::FileBase::RC rc = osl::FileBase::getFileURLFromSystemPath(path_or_uri, url);
172 return (rc == osl::FileBase::E_None);
176 // trying to identify a jump mark
179 const OUString JUMP_MARK_HTM(".htm#");
180 const OUString JUMP_MARK_HTML(".html#");
181 const sal_Unicode HASH_MARK = '#';
183 bool has_jump_mark(const OUString& system_path, sal_Int32* jmp_mark_start = nullptr)
185 sal_Int32 jmp_mark = std::max<int>(
186 system_path.lastIndexOf(JUMP_MARK_HTM),
187 system_path.lastIndexOf(JUMP_MARK_HTML));
189 if (jmp_mark_start)
190 *jmp_mark_start = jmp_mark;
192 return (jmp_mark > -1);
196 bool is_existing_file(const OUString& file_name)
198 OSL_ASSERT(is_system_path(file_name));
200 bool exist = false;
202 OUString file_url;
203 osl::FileBase::RC rc = osl::FileBase::getFileURLFromSystemPath(file_name, file_url);
205 if (osl::FileBase::E_None == rc)
207 osl::DirectoryItem dir_item;
208 rc = osl::DirectoryItem::get(file_url, dir_item);
209 exist = (osl::FileBase::E_None == rc);
211 return exist;
215 // Jump marks in file urls are illegal.
218 void remove_jump_mark(OUString* p_command)
220 OSL_PRECOND(p_command, "invalid parameter");
222 sal_Int32 pos;
223 if (has_jump_mark(*p_command, &pos))
225 const sal_Unicode* p_jmp_mark = p_command->getStr() + pos;
226 while (*p_jmp_mark && (*p_jmp_mark != HASH_MARK))
227 p_jmp_mark++;
229 *p_command = OUString(p_command->getStr(), p_jmp_mark - p_command->getStr());
235 CSysShExec::CSysShExec( const Reference< css::uno::XComponentContext >& xContext ) :
236 WeakComponentImplHelper< XSystemShellExecute, XServiceInfo >( m_aMutex ),
237 m_xContext(xContext)
240 * As this service is declared thread-affine, it is ensured to be called from a
241 * dedicated thread, so initialize COM here.
243 * We need COM to be initialized for STA, but osl thread get initialized for MTA.
244 * Once this changed, we can remove the uninitialize call.
246 CoUninitialize();
247 CoInitialize( nullptr );
250 namespace
252 bool checkExtension(OUString const & extension, OUString const & blacklist) {
253 assert(!extension.isEmpty());
254 for (sal_Int32 i = 0; i != -1;) {
255 OUString tok = blacklist.getToken(0, ';', i);
256 tok.startsWith(".", &tok);
257 if (extension.equalsIgnoreAsciiCase(tok)) {
258 return false;
261 return true;
264 // This callback checks if the found window is the specified process's top-level window,
265 // and activates the first found such window.
266 BOOL CALLBACK FindAndActivateProcWnd(HWND hwnd, LPARAM lParam)
268 if (!IsWindowVisible(hwnd))
269 return TRUE; // continue enumeration
270 if (GetWindow(hwnd, GW_OWNER)) // not a top-level window
271 return TRUE; // continue enumeration
272 const DWORD nParamProcId = static_cast<DWORD>(lParam);
273 assert(nParamProcId != 0);
274 DWORD nWndProcId = 0;
275 (void)GetWindowThreadProcessId(hwnd, &nWndProcId);
276 if (nWndProcId != nParamProcId)
277 return TRUE; // continue enumeration
279 // Found it! Bring it to front
280 if (IsIconic(hwnd))
282 ShowWindow(hwnd, SW_RESTORE);
284 SetForegroundWindow(hwnd);
285 SetActiveWindow(hwnd);
286 return FALSE; // stop enumeration
290 void SAL_CALL CSysShExec::execute( const OUString& aCommand, const OUString& aParameter, sal_Int32 nFlags )
292 // parameter checking
293 if (0 == aCommand.getLength())
294 throw IllegalArgumentException(
295 "Empty command",
296 static_cast< XSystemShellExecute* >( this ),
297 1 );
299 if ((nFlags & ~(NO_SYSTEM_ERROR_MESSAGE | URIS_ONLY)) != 0)
300 throw IllegalArgumentException(
301 "Invalid Flags specified",
302 static_cast< XSystemShellExecute* >( this ),
303 3 );
305 if ((nFlags & URIS_ONLY) != 0)
307 css::uno::Reference< css::uri::XUriReference > uri(
308 css::uri::UriReferenceFactory::create(m_xContext)->parse(aCommand));
309 if (!(uri.is() && uri->isAbsolute()))
311 throw css::lang::IllegalArgumentException(
312 "XSystemShellExecute.execute URIS_ONLY with"
313 " non-absolute URI reference "
314 + aCommand,
315 static_cast< cppu::OWeakObject * >(this), 0);
317 if (uri->getScheme().equalsIgnoreAsciiCase("file")) {
318 OUString pathname;
319 uri->clearFragment(); // getSystemPathFromFileURL fails for URLs with fragment
320 auto const e1
321 = osl::FileBase::getSystemPathFromFileURL(uri->getUriReference(), pathname);
322 if (e1 != osl::FileBase::E_None) {
323 throw css::lang::IllegalArgumentException(
324 ("XSystemShellExecute.execute, getSystemPathFromFileURL <" + aCommand
325 + "> failed with " + OUString::number(e1)),
326 {}, 0);
328 for (int i = 0;; ++i) {
329 SHFILEINFOW info;
330 if (SHGetFileInfoW(
331 o3tl::toW(pathname.getStr()), 0, &info, sizeof info, SHGFI_EXETYPE)
332 != 0)
334 throw css::lang::IllegalArgumentException(
335 "XSystemShellExecute.execute, cannot process <" + aCommand + ">", {}, 0);
337 if (SHGetFileInfoW(
338 o3tl::toW(pathname.getStr()), 0, &info, sizeof info, SHGFI_ATTRIBUTES)
339 == 0)
341 throw css::lang::IllegalArgumentException(
342 "XSystemShellExecute.execute, SHGetFileInfoW(" + pathname + ") failed", {},
345 if ((info.dwAttributes & SFGAO_LINK) == 0) {
346 break;
348 sal::systools::COMReference<IShellLinkW> link;
349 auto e2 = CoCreateInstance(
350 CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLinkW,
351 reinterpret_cast<LPVOID *>(&link));
352 if (FAILED(e2)) {
353 throw css::lang::IllegalArgumentException(
354 ("XSystemShellExecute.execute, CoCreateInstance failed with "
355 + OUString::number(e2)),
356 {}, 0);
358 sal::systools::COMReference<IPersistFile> file;
359 try {
360 file = link.QueryInterface<IPersistFile>(IID_IPersistFile);
361 } catch(sal::systools::ComError & e3) {
362 throw css::lang::IllegalArgumentException(
363 ("XSystemShellExecute.execute, QueryInterface failed with: "
364 + o3tl::runtimeToOUString(e3.what())),
365 {}, 0);
367 e2 = file->Load(o3tl::toW(pathname.getStr()), STGM_READ);
368 if (FAILED(e2)) {
369 throw css::lang::IllegalArgumentException(
370 ("XSystemShellExecute.execute, IPersistFile.Load failed with "
371 + OUString::number(e2)),
372 {}, 0);
374 e2 = link->Resolve(nullptr, SLR_UPDATE | SLR_NO_UI);
375 if (FAILED(e2)) {
376 throw css::lang::IllegalArgumentException(
377 ("XSystemShellExecute.execute, IShellLink.Resolve failed with "
378 + OUString::number(e2)),
379 {}, 0);
381 wchar_t path[MAX_PATH];
382 WIN32_FIND_DATAW wfd;
383 e2 = link->GetPath(path, MAX_PATH, &wfd, SLGP_RAWPATH);
384 if (FAILED(e2)) {
385 throw css::lang::IllegalArgumentException(
386 ("XSystemShellExecute.execute, IShellLink.GetPath failed with "
387 + OUString::number(e2)),
388 {}, 0);
390 pathname = o3tl::toU(path);
391 // Fail at some arbitrary nesting depth, to avoid an infinite loop:
392 if (i == 30) {
393 throw css::lang::IllegalArgumentException(
394 "XSystemShellExecute.execute, link depth exceeded for <" + aCommand + ">",
395 {}, 0);
398 auto const n = pathname.lastIndexOf('.');
399 if (n > pathname.lastIndexOf('\\')) {
400 auto const ext = pathname.copy(n + 1);
401 OUString env;
402 if (osl_getEnvironment(OUString("PATHEXT").pData, &env.pData) != osl_Process_E_None)
404 SAL_INFO("shell", "osl_getEnvironment(PATHEXT) failed");
406 if (!(checkExtension(ext, env)
407 && checkExtension(
408 ext, ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY")))
410 throw css::lang::IllegalArgumentException(
411 "XSystemShellExecute.execute, cannot process <" + aCommand + ">", {}, 0);
417 /* #i4789#; jump mark detection on system paths
418 if the given command is a system path (not http or
419 other uri schemes) and seems to have a jump mark
420 and names no existing file (remember the jump mark
421 sign '#' is a valid file name character we remove
422 the jump mark, else ShellExecuteEx fails */
423 OUString preprocessed_command(aCommand);
424 if (is_system_path(preprocessed_command))
426 if (has_jump_mark(preprocessed_command) && !is_existing_file(preprocessed_command))
427 remove_jump_mark(&preprocessed_command);
429 /* Convert file uris to system paths */
430 else
432 OUString aSystemPath;
433 if (::osl::FileBase::E_None == ::osl::FileBase::getSystemPathFromFileURL(preprocessed_command, aSystemPath))
434 preprocessed_command = aSystemPath;
435 else if (preprocessed_command.startsWithIgnoreAsciiCase("file:"))
436 //I use ToIUri conversion instead of the translateToExternal method of the css.uri.ExternalUriReferenceTranslator
437 //UNO service, because the translateToExternal method only supports characters covered by the current Windows code page.
438 preprocessed_command = rtl::Uri::decode(preprocessed_command, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
441 SHELLEXECUTEINFOW sei;
442 ZeroMemory(&sei, sizeof( sei));
444 sei.cbSize = sizeof(sei);
445 sei.lpFile = o3tl::toW(preprocessed_command.getStr());
446 sei.lpParameters = o3tl::toW(aParameter.getStr());
447 sei.nShow = SW_SHOWNORMAL;
448 sei.fMask = SEE_MASK_NOCLOSEPROCESS; // we need sei.hProcess
450 if (NO_SYSTEM_ERROR_MESSAGE & nFlags)
451 sei.fMask |= SEE_MASK_FLAG_NO_UI;
453 SetLastError( 0 );
455 bool bRet = ShellExecuteExW(&sei);
457 if (!bRet && (nFlags & NO_SYSTEM_ERROR_MESSAGE))
459 // ShellExecuteEx fails to set an error code
460 // we return osl_File_E_INVAL
461 sal_Int32 psxErr = GetLastError();
462 if (ERROR_SUCCESS == psxErr)
463 psxErr = E_UNKNOWN_EXEC_ERROR;
464 else
465 psxErr = MapError(psxErr);
467 throw SystemShellExecuteException(
468 "Error executing command",
469 static_cast< XSystemShellExecute* >(this),
470 psxErr);
472 else
474 // Get Permission make changes to the Window of the created Process
475 const DWORD procId = GetProcessId(sei.hProcess);
476 if (procId != 0)
478 AllowSetForegroundWindow(procId);
479 WaitForInputIdle(sei.hProcess, 1000); // so that main window is created; imperfect
480 EnumWindows(FindAndActivateProcWnd, static_cast<LPARAM>(procId));
484 // Close the handle for the created childprocess when we are done
485 CloseHandle(sei.hProcess);
488 // XServiceInfo
490 OUString SAL_CALL CSysShExec::getImplementationName( )
492 return SYSSHEXEC_IMPL_NAME;
495 sal_Bool SAL_CALL CSysShExec::supportsService( const OUString& ServiceName )
497 return cppu::supportsService(this, ServiceName);
500 Sequence< OUString > SAL_CALL CSysShExec::getSupportedServiceNames( )
502 return SysShExec_getSupportedServiceNames();
505 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */