2007-05-02 19:30:19 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Tim Schwartz
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2007-05-28 17:02:48 +02:00
|
|
|
#include <windows.h>
|
2007-11-05 22:02:49 +01:00
|
|
|
#include <lm.h>
|
2011-08-26 17:41:52 +02:00
|
|
|
#include <wine/debug.h>
|
2011-08-05 12:19:06 +02:00
|
|
|
|
2007-07-09 18:25:48 +02:00
|
|
|
#include "resources.h"
|
2007-05-28 17:02:48 +02:00
|
|
|
|
2011-08-26 17:41:52 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(net);
|
|
|
|
|
2007-05-28 17:02:48 +02:00
|
|
|
#define NET_START 0001
|
2007-06-18 19:48:06 +02:00
|
|
|
#define NET_STOP 0002
|
|
|
|
|
2011-08-29 20:30:46 +02:00
|
|
|
static int output_write(const WCHAR* str, int len)
|
2011-08-03 19:44:16 +02:00
|
|
|
{
|
|
|
|
DWORD ret, count;
|
|
|
|
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD lenA;
|
|
|
|
char* strA;
|
|
|
|
|
|
|
|
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
|
|
|
|
* back to WriteFile(), assuming the console encoding is still the right
|
|
|
|
* one in that case.
|
|
|
|
*/
|
|
|
|
lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len,
|
|
|
|
NULL, 0, NULL, NULL);
|
|
|
|
strA = HeapAlloc(GetProcessHeap(), 0, lenA);
|
|
|
|
if (!strA)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA,
|
|
|
|
NULL, NULL);
|
2015-06-19 03:35:58 +02:00
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
|
2011-08-03 19:44:16 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, strA);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-10-21 09:43:20 +02:00
|
|
|
static int output_vprintf(const WCHAR* fmt, __ms_va_list va_args)
|
2007-07-09 18:25:48 +02:00
|
|
|
{
|
2011-08-05 12:19:06 +02:00
|
|
|
WCHAR str[8192];
|
|
|
|
int len;
|
2011-08-29 20:30:46 +02:00
|
|
|
|
2018-07-24 00:01:27 +02:00
|
|
|
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt, 0, 0, str, ARRAY_SIZE(str), &va_args);
|
2020-05-13 22:06:46 +02:00
|
|
|
if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
|
2011-10-21 09:43:20 +02:00
|
|
|
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
|
2011-08-29 20:30:46 +02:00
|
|
|
else
|
|
|
|
output_write(str, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-02 10:21:10 +01:00
|
|
|
static int WINAPIV output_printf(const WCHAR* fmt, ...)
|
2011-08-29 20:30:46 +02:00
|
|
|
{
|
2011-10-21 09:43:20 +02:00
|
|
|
__ms_va_list arguments;
|
2011-08-29 20:30:46 +02:00
|
|
|
|
2011-10-21 09:43:20 +02:00
|
|
|
__ms_va_start(arguments, fmt);
|
2011-08-29 20:30:46 +02:00
|
|
|
output_vprintf(fmt, arguments);
|
2011-10-21 09:43:20 +02:00
|
|
|
__ms_va_end(arguments);
|
2011-08-29 20:30:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-02 10:21:10 +01:00
|
|
|
static int WINAPIV output_string(int msg, ...)
|
2011-08-29 20:30:46 +02:00
|
|
|
{
|
|
|
|
WCHAR fmt[8192];
|
2011-10-21 09:43:20 +02:00
|
|
|
__ms_va_list arguments;
|
2007-07-09 18:25:48 +02:00
|
|
|
|
2018-07-24 00:01:27 +02:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, fmt, ARRAY_SIZE(fmt));
|
2011-10-21 09:43:20 +02:00
|
|
|
__ms_va_start(arguments, msg);
|
2011-08-29 20:30:46 +02:00
|
|
|
output_vprintf(fmt, arguments);
|
2011-10-21 09:43:20 +02:00
|
|
|
__ms_va_end(arguments);
|
2007-07-09 18:25:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-13 12:04:28 +01:00
|
|
|
static BOOL output_error_string(DWORD error)
|
2008-04-01 13:49:29 +02:00
|
|
|
{
|
2011-08-05 12:19:06 +02:00
|
|
|
LPWSTR pBuffer;
|
|
|
|
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
|
2008-04-01 13:49:29 +02:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
2011-08-05 12:19:06 +02:00
|
|
|
NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
|
2008-04-01 13:49:29 +02:00
|
|
|
{
|
2011-08-03 19:44:16 +02:00
|
|
|
output_write(pBuffer, lstrlenW(pBuffer));
|
2008-04-01 13:49:29 +02:00
|
|
|
LocalFree(pBuffer);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-05 12:19:06 +02:00
|
|
|
static BOOL net_use(int argc, const WCHAR* argv[])
|
2007-11-05 22:02:49 +01:00
|
|
|
{
|
|
|
|
USE_INFO_2 *buffer, *connection;
|
|
|
|
DWORD read, total, resume_handle, rc, i;
|
2011-08-05 12:19:06 +02:00
|
|
|
WCHAR* status[STRING_RECONN-STRING_OK+1];
|
2007-11-05 22:02:49 +01:00
|
|
|
resume_handle = 0;
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
if(argc<3)
|
|
|
|
{
|
2011-08-01 15:55:01 +02:00
|
|
|
HMODULE hmod = GetModuleHandleW(NULL);
|
|
|
|
|
|
|
|
/* Load the status strings */
|
2018-07-24 00:01:27 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(status); i++)
|
2011-08-01 15:55:01 +02:00
|
|
|
{
|
2011-08-05 12:19:06 +02:00
|
|
|
status[i] = HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(**status));
|
|
|
|
LoadStringW(hmod, STRING_OK+i, status[i], 1024);
|
2011-08-01 15:55:01 +02:00
|
|
|
}
|
|
|
|
|
2007-11-05 22:02:49 +01:00
|
|
|
do {
|
|
|
|
rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
|
|
|
|
if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(total == 0)
|
|
|
|
{
|
|
|
|
output_string(STRING_NO_ENTRIES);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
output_string(STRING_USE_HEADER);
|
|
|
|
for (i = 0, connection = buffer; i < read; ++i, ++connection)
|
2011-08-01 15:55:01 +02:00
|
|
|
output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
|
2007-11-05 22:02:49 +01:00
|
|
|
connection->ui2_remote, connection->ui2_refcount);
|
|
|
|
|
|
|
|
if (buffer != NULL) NetApiBufferFree(buffer);
|
|
|
|
} while (rc == ERROR_MORE_DATA);
|
|
|
|
|
2011-08-01 15:55:01 +02:00
|
|
|
/* Release the status strings */
|
2018-07-24 00:01:27 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(status); i++)
|
2011-08-01 15:55:01 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, status[i]);
|
|
|
|
|
2007-11-05 22:02:49 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-26 17:41:52 +02:00
|
|
|
static BOOL net_enum_services(void)
|
|
|
|
{
|
|
|
|
SC_HANDLE SCManager;
|
|
|
|
LPENUM_SERVICE_STATUS_PROCESSW services;
|
|
|
|
DWORD size, i, count, resume;
|
|
|
|
BOOL success = FALSE;
|
|
|
|
|
|
|
|
SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
|
|
|
if(!SCManager)
|
|
|
|
{
|
|
|
|
output_string(STRING_NO_SCM);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0, &size, &count, NULL, NULL);
|
|
|
|
if(GetLastError() != ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
output_error_string(GetLastError());
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
services = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
resume = 0;
|
|
|
|
if(!EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, (LPBYTE)services, size, &size, &count, &resume, NULL))
|
|
|
|
{
|
|
|
|
output_error_string(GetLastError());
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
output_string(STRING_RUNNING_HEADER);
|
|
|
|
for(i = 0; i < count; i++)
|
|
|
|
{
|
2020-10-05 23:22:53 +02:00
|
|
|
output_printf(L" %1\n", services[i].lpDisplayName);
|
2011-08-26 17:41:52 +02:00
|
|
|
WINE_TRACE("service=%s state=%d controls=%x\n",
|
|
|
|
wine_dbgstr_w(services[i].lpServiceName),
|
|
|
|
services[i].ServiceStatusProcess.dwCurrentState,
|
|
|
|
services[i].ServiceStatusProcess.dwControlsAccepted);
|
|
|
|
}
|
|
|
|
success = TRUE;
|
|
|
|
|
|
|
|
end:
|
|
|
|
CloseServiceHandle(SCManager);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:48:06 +02:00
|
|
|
static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
|
|
|
|
{
|
2009-12-09 18:52:46 +01:00
|
|
|
LPENUM_SERVICE_STATUSW dependencies = NULL;
|
2007-06-18 19:48:06 +02:00
|
|
|
DWORD buffer_size = 0;
|
|
|
|
DWORD count = 0, counter;
|
|
|
|
BOOL result;
|
|
|
|
SC_HANDLE dependent_serviceHandle;
|
|
|
|
SERVICE_STATUS_PROCESS ssp;
|
|
|
|
|
2009-12-09 18:52:46 +01:00
|
|
|
result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
|
2007-06-18 19:48:06 +02:00
|
|
|
|
|
|
|
if(!result && (GetLastError() == ERROR_MORE_DATA))
|
|
|
|
{
|
|
|
|
dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
|
2009-12-09 18:52:46 +01:00
|
|
|
if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
|
2007-06-18 19:48:06 +02:00
|
|
|
{
|
|
|
|
for(counter = 0; counter < count; counter++)
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
|
2009-12-09 18:52:46 +01:00
|
|
|
dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
|
2015-07-16 12:56:07 +02:00
|
|
|
if(dependent_serviceHandle)
|
|
|
|
{
|
|
|
|
result = StopService(SCManager, dependent_serviceHandle);
|
|
|
|
CloseServiceHandle(dependent_serviceHandle);
|
|
|
|
}
|
2007-07-09 18:25:48 +02:00
|
|
|
if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
|
2007-06-18 19:48:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
|
|
|
|
HeapFree(GetProcessHeap(), 0, dependencies);
|
|
|
|
return result;
|
|
|
|
}
|
2007-05-28 17:02:48 +02:00
|
|
|
|
2011-08-05 12:19:06 +02:00
|
|
|
static BOOL net_service(int operation, const WCHAR* service_name)
|
2007-05-28 17:02:48 +02:00
|
|
|
{
|
|
|
|
SC_HANDLE SCManager, serviceHandle;
|
2013-10-15 00:27:15 +02:00
|
|
|
BOOL result = FALSE;
|
2011-08-05 12:19:06 +02:00
|
|
|
WCHAR service_display_name[4096];
|
|
|
|
DWORD buffer_size;
|
2007-05-28 17:02:48 +02:00
|
|
|
|
2009-12-09 18:52:46 +01:00
|
|
|
SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
2007-05-28 17:02:48 +02:00
|
|
|
if(!SCManager)
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_NO_SCM);
|
2007-05-28 17:02:48 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-05 12:19:06 +02:00
|
|
|
serviceHandle = OpenServiceW(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
|
2007-05-28 17:02:48 +02:00
|
|
|
if(!serviceHandle)
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_NO_SVCHANDLE);
|
2007-05-28 17:02:48 +02:00
|
|
|
CloseServiceHandle(SCManager);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-07-24 00:01:27 +02:00
|
|
|
buffer_size = ARRAY_SIZE(service_display_name);
|
2011-08-05 12:19:06 +02:00
|
|
|
GetServiceDisplayNameW(SCManager, service_name, service_display_name, &buffer_size);
|
|
|
|
if (!service_display_name[0]) lstrcpyW(service_display_name, service_name);
|
2007-05-28 17:02:48 +02:00
|
|
|
|
|
|
|
switch(operation)
|
|
|
|
{
|
|
|
|
case NET_START:
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_START_SVC, service_display_name);
|
2009-12-09 18:52:46 +01:00
|
|
|
result = StartServiceW(serviceHandle, 0, NULL);
|
2007-05-28 17:02:48 +02:00
|
|
|
|
2007-10-02 19:00:45 +02:00
|
|
|
if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
|
2008-04-01 13:49:29 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!output_error_string(GetLastError()))
|
|
|
|
output_string(STRING_START_SVC_FAIL, service_display_name);
|
|
|
|
}
|
2007-05-28 17:02:48 +02:00
|
|
|
break;
|
2007-06-18 19:48:06 +02:00
|
|
|
case NET_STOP:
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_STOP_SVC, service_display_name);
|
2007-06-18 19:48:06 +02:00
|
|
|
result = StopService(SCManager, serviceHandle);
|
|
|
|
|
2007-07-09 18:25:48 +02:00
|
|
|
if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
|
2008-04-01 13:49:29 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!output_error_string(GetLastError()))
|
|
|
|
output_string(STRING_STOP_SVC_FAIL, service_display_name);
|
|
|
|
}
|
2007-06-18 19:48:06 +02:00
|
|
|
break;
|
2007-05-28 17:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CloseServiceHandle(serviceHandle);
|
|
|
|
CloseServiceHandle(SCManager);
|
|
|
|
return result;
|
|
|
|
}
|
2007-05-02 19:30:19 +02:00
|
|
|
|
2013-10-15 00:27:15 +02:00
|
|
|
static BOOL arg_is(const WCHAR* str1, const WCHAR* str2)
|
2011-08-05 12:19:06 +02:00
|
|
|
{
|
|
|
|
return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, -1, str2, -1) == CSTR_EQUAL;
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain(int argc, const WCHAR* argv[])
|
2007-05-02 19:30:19 +02:00
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_USAGE);
|
2007-05-02 19:30:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:22:53 +02:00
|
|
|
if(arg_is(argv[1], L"help"))
|
2007-05-02 19:30:19 +02:00
|
|
|
{
|
2011-08-26 17:40:17 +02:00
|
|
|
if(argc > 3)
|
|
|
|
{
|
|
|
|
output_string(STRING_USAGE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(argc == 2)
|
|
|
|
output_string(STRING_USAGE);
|
2020-10-05 23:22:53 +02:00
|
|
|
else if(arg_is(argv[2], L"start"))
|
2011-08-26 17:40:17 +02:00
|
|
|
output_string(STRING_START_USAGE);
|
2020-10-05 23:22:53 +02:00
|
|
|
else if(arg_is(argv[2], L"stop"))
|
2011-08-26 17:40:17 +02:00
|
|
|
output_string(STRING_STOP_USAGE);
|
|
|
|
else
|
|
|
|
output_string(STRING_USAGE);
|
2007-05-02 19:30:19 +02:00
|
|
|
}
|
2020-10-05 23:22:53 +02:00
|
|
|
else if(arg_is(argv[1], L"start"))
|
2007-05-28 17:02:48 +02:00
|
|
|
{
|
2011-08-26 17:41:52 +02:00
|
|
|
if(argc > 3)
|
2007-05-28 17:02:48 +02:00
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_START_USAGE);
|
2007-05-28 17:02:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-08-26 17:41:52 +02:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
if (!net_enum_services())
|
|
|
|
return 1;
|
|
|
|
}
|
2020-10-05 23:22:53 +02:00
|
|
|
else if(arg_is(argv[2], L"/help"))
|
2011-08-26 17:40:17 +02:00
|
|
|
output_string(STRING_START_USAGE);
|
|
|
|
else if(!net_service(NET_START, argv[2]))
|
2007-05-28 17:02:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-10-05 23:22:53 +02:00
|
|
|
else if(arg_is(argv[1], L"stop"))
|
2007-06-18 19:48:06 +02:00
|
|
|
{
|
2011-08-26 17:40:17 +02:00
|
|
|
if(argc != 3)
|
2007-06-18 19:48:06 +02:00
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_STOP_USAGE);
|
2007-06-18 19:48:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-10-05 23:22:53 +02:00
|
|
|
if(arg_is(argv[2], L"/help"))
|
2011-08-26 17:40:17 +02:00
|
|
|
output_string(STRING_STOP_USAGE);
|
|
|
|
else if(!net_service(NET_STOP, argv[2]))
|
2007-06-18 19:48:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-10-05 23:22:53 +02:00
|
|
|
else if(arg_is(argv[1], L"use"))
|
2007-11-05 22:02:49 +01:00
|
|
|
{
|
|
|
|
if(!net_use(argc, argv)) return 1;
|
|
|
|
}
|
2011-08-26 17:40:17 +02:00
|
|
|
else
|
|
|
|
output_string(STRING_USAGE);
|
2007-11-05 22:02:49 +01:00
|
|
|
|
2007-05-28 17:02:48 +02:00
|
|
|
return 0;
|
2007-05-02 19:30:19 +02:00
|
|
|
}
|