Sweden-Number/programs/net/net.c

354 lines
10 KiB
C
Raw Normal View History

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>
#include <lm.h>
#include <wine/debug.h>
2011-08-05 12:19:06 +02:00
#include "resources.h"
2007-05-28 17:02:48 +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
static int output_write(const WCHAR* str, int len)
{
DWORD count;
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL))
{
DWORD lenA;
char* strA;
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
* back to WriteFile() with OEM code page.
*/
lenA = WideCharToMultiByte(GetOEMCP(), 0, str, len,
NULL, 0, NULL, NULL);
strA = HeapAlloc(GetProcessHeap(), 0, lenA);
if (!strA)
return 0;
WideCharToMultiByte(GetOEMCP(), 0, str, len, strA, lenA, NULL, NULL);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
HeapFree(GetProcessHeap(), 0, strA);
}
return count;
}
static int output_vprintf(const WCHAR* fmt, va_list va_args)
{
2011-08-05 12:19:06 +02:00
WCHAR str[8192];
int len;
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt, 0, 0, str, ARRAY_SIZE(str), &va_args);
if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
else
output_write(str, len);
return 0;
}
static int WINAPIV output_printf(const WCHAR* fmt, ...)
{
va_list arguments;
va_start(arguments, fmt);
output_vprintf(fmt, arguments);
va_end(arguments);
return 0;
}
static int WINAPIV output_string(int msg, ...)
{
WCHAR fmt[8192];
va_list arguments;
LoadStringW(GetModuleHandleW(NULL), msg, fmt, ARRAY_SIZE(fmt));
va_start(arguments, msg);
output_vprintf(fmt, arguments);
va_end(arguments);
return 0;
}
static BOOL output_error_string(DWORD error)
{
2011-08-05 12:19:06 +02:00
LPWSTR pBuffer;
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
2011-08-05 12:19:06 +02:00
NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
{
output_write(pBuffer, lstrlenW(pBuffer));
LocalFree(pBuffer);
return TRUE;
}
return FALSE;
}
2011-08-05 12:19:06 +02:00
static BOOL net_use(int argc, const WCHAR* argv[])
{
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];
resume_handle = 0;
buffer = NULL;
if(argc<3)
{
HMODULE hmod = GetModuleHandleW(NULL);
/* Load the status strings */
for (i = 0; i < ARRAY_SIZE(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);
}
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)
output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
connection->ui2_remote, connection->ui2_refcount);
if (buffer != NULL) NetApiBufferFree(buffer);
} while (rc == ERROR_MORE_DATA);
/* Release the status strings */
for (i = 0; i < ARRAY_SIZE(status); i++)
HeapFree(GetProcessHeap(), 0, status[i]);
return TRUE;
}
return FALSE;
}
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_printf(L" %1\n", services[i].lpDisplayName);
WINE_TRACE("service=%s state=%ld controls=%lx\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)
{
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;
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);
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++)
{
output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
if(dependent_serviceHandle)
{
result = StopService(SCManager, dependent_serviceHandle);
CloseServiceHandle(dependent_serviceHandle);
}
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
SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
2007-05-28 17:02:48 +02:00
if(!SCManager)
{
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)
{
output_string(STRING_NO_SVCHANDLE);
2007-05-28 17:02:48 +02:00
CloseServiceHandle(SCManager);
return FALSE;
}
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:
output_string(STRING_START_SVC, service_display_name);
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);
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:
output_string(STRING_STOP_SVC, service_display_name);
2007-06-18 19:48:06 +02:00
result = StopService(SCManager, serviceHandle);
if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
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;
}
int __cdecl wmain(int argc, const WCHAR* argv[])
2007-05-02 19:30:19 +02:00
{
if (argc < 2)
{
output_string(STRING_USAGE);
2007-05-02 19:30:19 +02:00
return 1;
}
if(arg_is(argv[1], L"help"))
2007-05-02 19:30:19 +02:00
{
if(argc > 3)
{
output_string(STRING_USAGE);
return 1;
}
if(argc == 2)
output_string(STRING_USAGE);
else if(arg_is(argv[2], L"start"))
output_string(STRING_START_USAGE);
else if(arg_is(argv[2], L"stop"))
output_string(STRING_STOP_USAGE);
else
output_string(STRING_USAGE);
2007-05-02 19:30:19 +02:00
}
else if(arg_is(argv[1], L"start"))
2007-05-28 17:02:48 +02:00
{
if(argc > 3)
2007-05-28 17:02:48 +02:00
{
output_string(STRING_START_USAGE);
2007-05-28 17:02:48 +02:00
return 1;
}
if (argc == 2)
{
if (!net_enum_services())
return 1;
}
else if(arg_is(argv[2], L"/help"))
output_string(STRING_START_USAGE);
else if(!net_service(NET_START, argv[2]))
2007-05-28 17:02:48 +02:00
return 1;
}
else if(arg_is(argv[1], L"stop"))
2007-06-18 19:48:06 +02:00
{
if(argc != 3)
2007-06-18 19:48:06 +02:00
{
output_string(STRING_STOP_USAGE);
2007-06-18 19:48:06 +02:00
return 1;
}
if(arg_is(argv[2], L"/help"))
output_string(STRING_STOP_USAGE);
else if(!net_service(NET_STOP, argv[2]))
2007-06-18 19:48:06 +02:00
return 1;
}
else if(arg_is(argv[1], L"use"))
{
if(!net_use(argc, argv)) return 1;
}
else
output_string(STRING_USAGE);
2007-05-28 17:02:48 +02:00
return 0;
2007-05-02 19:30:19 +02:00
}