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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-05-28 17:02:48 +02:00
|
|
|
#include <windows.h>
|
2007-11-05 22:02:49 +01:00
|
|
|
#include <lm.h>
|
2007-07-09 18:25:48 +02:00
|
|
|
#include "resources.h"
|
2007-05-28 17:02:48 +02:00
|
|
|
|
|
|
|
#define NET_START 0001
|
2007-06-18 19:48:06 +02:00
|
|
|
#define NET_STOP 0002
|
|
|
|
|
2008-12-13 12:04:28 +01:00
|
|
|
static int output_string(int msg, ...)
|
2007-07-09 18:25:48 +02:00
|
|
|
{
|
|
|
|
char msg_buffer[8192];
|
|
|
|
va_list arguments;
|
|
|
|
|
2009-12-09 18:52:46 +01:00
|
|
|
LoadStringA(GetModuleHandleW(NULL), msg, msg_buffer, sizeof(msg_buffer));
|
2007-07-09 18:25:48 +02:00
|
|
|
va_start(arguments, msg);
|
|
|
|
vprintf(msg_buffer, arguments);
|
|
|
|
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
|
|
|
{
|
|
|
|
LPSTR pBuffer;
|
|
|
|
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
NULL, error, 0, (LPSTR)&pBuffer, 0, NULL))
|
|
|
|
{
|
|
|
|
fputs(pBuffer, stdout);
|
|
|
|
LocalFree(pBuffer);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-11-05 22:02:49 +01:00
|
|
|
static BOOL net_use(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
USE_INFO_2 *buffer, *connection;
|
|
|
|
DWORD read, total, resume_handle, rc, i;
|
|
|
|
const char *status_description[] = { "OK", "Paused", "Disconnected", "An error occurred",
|
|
|
|
"A network error occurred", "Connection is being made",
|
|
|
|
"Reconnecting" };
|
|
|
|
resume_handle = 0;
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
if(argc<3)
|
|
|
|
{
|
|
|
|
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_description[connection->ui2_status], connection->ui2_local,
|
|
|
|
connection->ui2_remote, connection->ui2_refcount);
|
|
|
|
|
|
|
|
if (buffer != NULL) NetApiBufferFree(buffer);
|
|
|
|
} while (rc == ERROR_MORE_DATA);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
static BOOL net_service(int operation, char *service_name)
|
|
|
|
{
|
|
|
|
SC_HANDLE SCManager, serviceHandle;
|
|
|
|
BOOL result = 0;
|
|
|
|
char service_display_name[4096];
|
|
|
|
DWORD buffer_size = sizeof(service_display_name);
|
|
|
|
|
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;
|
|
|
|
}
|
2009-12-09 18:52:46 +01:00
|
|
|
serviceHandle = OpenServiceA(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 18:52:46 +01:00
|
|
|
GetServiceDisplayNameA(SCManager, service_name, service_display_name, &buffer_size);
|
2007-05-28 17:02:48 +02:00
|
|
|
if (!service_display_name[0]) strcpy(service_display_name, service_name);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strcasecmp(argv[1], "help"))
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_HELP_USAGE);
|
2007-05-02 19:30:19 +02:00
|
|
|
}
|
2007-05-28 17:02:48 +02:00
|
|
|
|
|
|
|
if(!strcasecmp(argv[1], "start"))
|
|
|
|
{
|
|
|
|
if(argc < 3)
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_START_USAGE);
|
2007-05-28 17:02:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!net_service(NET_START, argv[2]))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:48:06 +02:00
|
|
|
if(!strcasecmp(argv[1], "stop"))
|
|
|
|
{
|
|
|
|
if(argc < 3)
|
|
|
|
{
|
2007-07-09 18:25:48 +02:00
|
|
|
output_string(STRING_STOP_USAGE);
|
2007-06-18 19:48:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!net_service(NET_STOP, argv[2]))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-05 22:02:49 +01:00
|
|
|
if(!strcasecmp(argv[1], "use"))
|
|
|
|
{
|
|
|
|
if(!net_use(argc, argv)) return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-28 17:02:48 +02:00
|
|
|
return 0;
|
2007-05-02 19:30:19 +02:00
|
|
|
}
|