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-05 12:19:06 +02:00
|
|
|
#include <wine/unicode.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-03 19:44:16 +02:00
|
|
|
static int output_write(WCHAR* str, int len)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, len, &count, FALSE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, strA);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-12-13 12:04:28 +01:00
|
|
|
static int output_string(int msg, ...)
|
2007-07-09 18:25:48 +02:00
|
|
|
{
|
2011-08-05 12:19:06 +02:00
|
|
|
WCHAR fmt[8192];
|
|
|
|
WCHAR str[8192];
|
|
|
|
int len;
|
2007-07-09 18:25:48 +02:00
|
|
|
va_list arguments;
|
|
|
|
|
2011-08-06 13:09:17 +02:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, fmt, sizeof(fmt)/sizeof(fmt[0]));
|
2007-07-09 18:25:48 +02:00
|
|
|
va_start(arguments, msg);
|
2011-08-05 12:19:06 +02:00
|
|
|
len = vsprintfW(str, fmt, arguments);
|
2011-08-03 19:44:16 +02:00
|
|
|
output_write(str, len);
|
2007-07-09 18:25:48 +02:00
|
|
|
va_end(arguments);
|
|
|
|
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 */
|
|
|
|
for (i = 0; i < sizeof(status)/sizeof(*status); i++)
|
|
|
|
{
|
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 */
|
|
|
|
for (i = 0; i < sizeof(status)/sizeof(*status); i++)
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
output_string(STRING_RUNNING, services[i].lpDisplayName);
|
|
|
|
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);
|
2007-06-18 19:48:06 +02:00
|
|
|
if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
|
2007-06-20 17:57:51 +02:00
|
|
|
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;
|
|
|
|
BOOL result = 0;
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-05 12:19:06 +02:00
|
|
|
buffer_size = sizeof(service_display_name)/sizeof(*service_display_name);
|
|
|
|
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
|
|
|
|
2011-08-05 12:19:06 +02:00
|
|
|
static int arg_is(const WCHAR* str1, const WCHAR* str2)
|
|
|
|
{
|
|
|
|
return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, -1, str2, -1) == CSTR_EQUAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmain(int argc, const WCHAR* argv[])
|
2007-05-02 19:30:19 +02:00
|
|
|
{
|
2011-08-05 12:19:06 +02:00
|
|
|
static const WCHAR helpW[]={'h','e','l','p',0};
|
2011-08-26 17:40:17 +02:00
|
|
|
static const WCHAR shelpW[]={'/','h','e','l','p',0};
|
2011-08-05 12:19:06 +02:00
|
|
|
static const WCHAR startW[]={'s','t','a','r','t',0};
|
|
|
|
static const WCHAR stopW[]={'s','t','o','p',0};
|
|
|
|
static const WCHAR useW[]={'u','s','e',0};
|
2011-08-26 17:40:17 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-05 12:19:06 +02:00
|
|
|
if(arg_is(argv[1], helpW))
|
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);
|
|
|
|
else if(arg_is(argv[2], startW))
|
|
|
|
output_string(STRING_START_USAGE);
|
|
|
|
else if(arg_is(argv[2], stopW))
|
|
|
|
output_string(STRING_STOP_USAGE);
|
|
|
|
else
|
|
|
|
output_string(STRING_USAGE);
|
2007-05-02 19:30:19 +02:00
|
|
|
}
|
2011-08-26 17:40:17 +02:00
|
|
|
else if(arg_is(argv[1], startW))
|
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;
|
|
|
|
}
|
|
|
|
else if(arg_is(argv[2], shelpW))
|
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;
|
|
|
|
}
|
2011-08-26 17:40:17 +02:00
|
|
|
else if(arg_is(argv[1], stopW))
|
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;
|
|
|
|
}
|
2011-08-26 17:40:17 +02:00
|
|
|
if(arg_is(argv[2], shelpW))
|
|
|
|
output_string(STRING_STOP_USAGE);
|
|
|
|
else if(!net_service(NET_STOP, argv[2]))
|
2007-06-18 19:48:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-08-26 17:40:17 +02:00
|
|
|
else if(arg_is(argv[1], useW))
|
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
|
|
|
}
|