2008-02-18 13:48:51 +03:00
/*
Samba Unix / Linux SMB client library
Distributed SMB / CIFS Server Management Utility
2005-03-24 07:25:49 +03:00
Copyright ( C ) Gerald ( Jerry ) Carter 2005
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2005-03-24 07:25:49 +03:00
( at your option ) any later version .
2008-02-18 13:48:51 +03:00
2005-03-24 07:25:49 +03:00
This program 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 General Public License for more details .
2008-02-18 13:48:51 +03:00
2005-03-24 07:25:49 +03:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>. */
2008-02-18 13:48:51 +03:00
2005-03-24 07:25:49 +03:00
# include "includes.h"
# include "utils/net.h"
2005-03-25 03:38:30 +03:00
2008-02-18 14:10:54 +03:00
# define CLI_SERVER_NAME_SLASH(_ctx, _p, _cli) \
_p = talloc_asprintf ( _ctx , " \\ \\ %s " , _cli - > cli - > desthost ) ;
2005-03-25 03:38:30 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static WERROR query_service_state ( struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
POLICY_HND * hSCM ,
const char * service ,
uint32 * state )
2005-03-25 03:38:30 +03:00
{
POLICY_HND hService ;
SERVICE_STATUS service_status ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:45:13 +03:00
NTSTATUS status ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
/* now cycle until the status is actually 'watch_state' */
2008-02-18 13:48:51 +03:00
2008-02-18 14:45:13 +03:00
status = rpccli_svcctl_OpenServiceW ( pipe_hnd , mem_ctx ,
hSCM ,
service ,
SC_RIGHT_SVC_QUERY_STATUS ,
& hService ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open service. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
return result ;
}
2008-02-19 04:43:04 +03:00
status = rpccli_svcctl_QueryServiceStatus ( pipe_hnd , mem_ctx ,
& hService ,
& service_status ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2005-03-25 03:38:30 +03:00
* state = service_status . state ;
}
2008-02-18 13:48:51 +03:00
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hService , NULL ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
return result ;
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static WERROR watch_service_state ( struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
POLICY_HND * hSCM ,
2008-02-18 13:48:51 +03:00
const char * service ,
2005-09-30 21:13:37 +04:00
uint32 watch_state ,
uint32 * final_state )
2005-03-25 03:38:30 +03:00
{
uint32 i ;
uint32 state = 0 ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
i = 0 ;
while ( ( state ! = watch_state ) & & i < 30 ) {
/* get the status */
2005-09-30 21:13:37 +04:00
result = query_service_state ( pipe_hnd , mem_ctx , hSCM , service , & state ) ;
2005-03-25 03:38:30 +03:00
if ( ! W_ERROR_IS_OK ( result ) ) {
break ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
d_printf ( " . " ) ;
i + + ;
2006-09-27 15:37:02 +04:00
sys_usleep ( 100 ) ;
2005-03-25 03:38:30 +03:00
}
d_printf ( " \n " ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
* final_state = state ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
return result ;
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static WERROR control_service ( struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
POLICY_HND * hSCM ,
2008-02-18 13:48:51 +03:00
const char * service ,
2005-09-30 21:13:37 +04:00
uint32 control ,
uint32 watch_state )
2005-03-25 03:38:30 +03:00
{
POLICY_HND hService ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:45:13 +03:00
NTSTATUS status ;
2005-03-25 03:38:30 +03:00
SERVICE_STATUS service_status ;
uint32 state = 0 ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
/* Open the Service */
2008-02-18 13:48:51 +03:00
2008-02-18 14:45:13 +03:00
status = rpccli_svcctl_OpenServiceW ( pipe_hnd , mem_ctx ,
hSCM ,
service ,
( SC_RIGHT_SVC_STOP | SC_RIGHT_SVC_PAUSE_CONTINUE ) ,
& hService ,
& result ) ;
2005-03-25 03:38:30 +03:00
2008-02-18 14:45:13 +03:00
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open service. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
/* get the status */
2008-02-19 04:37:12 +03:00
status = rpccli_svcctl_ControlService ( pipe_hnd , mem_ctx ,
& hService ,
control ,
& service_status ,
& result ) ;
2008-02-18 13:48:51 +03:00
2008-02-19 04:37:12 +03:00
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Control service request failed. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
/* loop -- checking the state until we are where we want to be */
2008-02-18 13:48:51 +03:00
2005-09-30 21:13:37 +04:00
result = watch_service_state ( pipe_hnd , mem_ctx , hSCM , service , watch_state , & state ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
d_printf ( " %s service is %s. \n " , service , svc_status_string ( state ) ) ;
2008-02-18 13:48:51 +03:00
done :
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hService , NULL ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
return result ;
2008-02-18 13:48:51 +03:00
}
2005-03-25 03:38:30 +03:00
2005-03-24 19:11:23 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-03-24 07:25:49 +03:00
2005-09-30 21:13:37 +04:00
static NTSTATUS rpc_service_list_internal ( const DOM_SID * domain_sid ,
2008-02-18 13:48:51 +03:00
const char * domain_name ,
2005-09-30 21:13:37 +04:00
struct cli_state * cli ,
struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
int argc ,
const char * * argv )
2005-03-24 19:11:23 +03:00
{
POLICY_HND hSCM ;
2005-03-24 21:05:31 +03:00
ENUM_SERVICES_STATUS * services ;
2005-03-24 19:11:23 +03:00
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:10:54 +03:00
NTSTATUS status ;
2005-03-24 21:05:31 +03:00
fstring servicename ;
fstring displayname ;
uint32 num_services = 0 ;
2008-02-18 14:10:54 +03:00
const char * server_name ;
2005-03-24 21:05:31 +03:00
int i ;
2008-02-18 13:48:51 +03:00
2005-03-24 19:11:23 +03:00
if ( argc ! = 0 ) {
d_printf ( " Usage: net rpc service list \n " ) ;
return NT_STATUS_OK ;
}
2005-03-24 07:25:49 +03:00
2008-02-18 14:10:54 +03:00
CLI_SERVER_NAME_SLASH ( mem_ctx , server_name , pipe_hnd ) ;
NT_STATUS_HAVE_NO_MEMORY ( server_name ) ;
status = rpccli_svcctl_OpenSCManagerW ( pipe_hnd , mem_ctx ,
server_name ,
NULL ,
SC_RIGHT_MGR_ENUMERATE_SERVICE ,
& hSCM ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open Service Control Manager. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-24 19:11:23 +03:00
return werror_to_ntstatus ( result ) ;
2005-03-24 07:25:49 +03:00
}
2008-02-18 13:48:51 +03:00
2005-09-30 21:13:37 +04:00
result = rpccli_svcctl_enumerate_services ( pipe_hnd , mem_ctx , & hSCM , SVCCTL_TYPE_WIN32 ,
2005-03-24 21:05:31 +03:00
SVCCTL_STATE_ALL , & num_services , & services ) ;
2008-02-18 13:48:51 +03:00
2005-03-24 21:05:31 +03:00
if ( ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to enumerate services. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-24 21:05:31 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2005-03-24 21:05:31 +03:00
if ( num_services = = 0 )
d_printf ( " No services returned \n " ) ;
2008-02-18 13:48:51 +03:00
2005-03-24 21:05:31 +03:00
for ( i = 0 ; i < num_services ; i + + ) {
rpcstr_pull ( servicename , services [ i ] . servicename . buffer , sizeof ( servicename ) , - 1 , STR_TERMINATE ) ;
rpcstr_pull ( displayname , services [ i ] . displayname . buffer , sizeof ( displayname ) , - 1 , STR_TERMINATE ) ;
2008-02-18 13:48:51 +03:00
2005-03-24 21:13:51 +03:00
d_printf ( " %-20s \" %s \" \n " , servicename , displayname ) ;
2005-03-24 21:05:31 +03:00
}
2008-02-18 13:48:51 +03:00
done :
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hSCM , NULL ) ;
2008-02-18 13:48:51 +03:00
2005-03-24 21:05:31 +03:00
return werror_to_ntstatus ( result ) ;
2008-02-18 13:48:51 +03:00
}
2005-03-24 07:25:49 +03:00
2005-03-25 01:32:31 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static NTSTATUS rpc_service_status_internal ( const DOM_SID * domain_sid ,
2008-02-18 13:48:51 +03:00
const char * domain_name ,
2005-09-30 21:13:37 +04:00
struct cli_state * cli ,
struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
int argc ,
const char * * argv )
2005-03-25 01:32:31 +03:00
{
POLICY_HND hSCM , hService ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:10:54 +03:00
NTSTATUS status ;
2005-03-25 01:32:31 +03:00
SERVICE_STATUS service_status ;
SERVICE_CONFIG config ;
fstring ascii_string ;
2008-02-18 14:10:54 +03:00
const char * server_name ;
2008-02-18 13:48:51 +03:00
2005-03-25 01:32:31 +03:00
if ( argc ! = 1 ) {
d_printf ( " Usage: net rpc service status <service> \n " ) ;
return NT_STATUS_OK ;
}
/* Open the Service Control Manager */
2008-02-18 14:10:54 +03:00
CLI_SERVER_NAME_SLASH ( mem_ctx , server_name , pipe_hnd ) ;
NT_STATUS_HAVE_NO_MEMORY ( server_name ) ;
status = rpccli_svcctl_OpenSCManagerW ( pipe_hnd , mem_ctx ,
server_name ,
NULL ,
SC_RIGHT_MGR_ENUMERATE_SERVICE ,
& hSCM ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open Service Control Manager. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 01:32:31 +03:00
return werror_to_ntstatus ( result ) ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 01:32:31 +03:00
/* Open the Service */
2008-02-18 13:48:51 +03:00
2008-02-18 14:45:13 +03:00
status = rpccli_svcctl_OpenServiceW ( pipe_hnd , mem_ctx ,
& hSCM ,
argv [ 0 ] ,
( SC_RIGHT_SVC_QUERY_STATUS | SC_RIGHT_SVC_QUERY_CONFIG ) ,
& hService ,
& result ) ;
2005-03-25 01:32:31 +03:00
2008-02-18 14:45:13 +03:00
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open service. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 01:32:31 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 01:32:31 +03:00
/* get the status */
2008-02-19 04:43:04 +03:00
status = rpccli_svcctl_QueryServiceStatus ( pipe_hnd , mem_ctx ,
& hService ,
& service_status ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Query status request failed. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 01:32:31 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2008-02-18 14:45:13 +03:00
d_printf ( " %s service is %s. \n " , argv [ 0 ] , svc_status_string ( service_status . state ) ) ;
2005-03-25 01:32:31 +03:00
/* get the config */
2005-09-30 21:13:37 +04:00
result = rpccli_svcctl_query_config ( pipe_hnd , mem_ctx , & hService , & config ) ;
2005-03-25 01:32:31 +03:00
if ( ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Query config request failed. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 01:32:31 +03:00
goto done ;
}
/* print out the configuration information for the service */
d_printf ( " Configuration details: \n " ) ;
2005-12-11 07:21:34 +03:00
d_printf ( " \t Controls Accepted = 0x%x \n " , service_status . controls_accepted ) ;
2005-03-25 01:32:31 +03:00
d_printf ( " \t Service Type = 0x%x \n " , config . service_type ) ;
d_printf ( " \t Start Type = 0x%x \n " , config . start_type ) ;
d_printf ( " \t Error Control = 0x%x \n " , config . error_control ) ;
d_printf ( " \t Tag ID = 0x%x \n " , config . tag_id ) ;
if ( config . executablepath ) {
rpcstr_pull ( ascii_string , config . executablepath - > buffer , sizeof ( ascii_string ) , - 1 , STR_TERMINATE ) ;
d_printf ( " \t Executable Path = %s \n " , ascii_string ) ;
}
if ( config . loadordergroup ) {
rpcstr_pull ( ascii_string , config . loadordergroup - > buffer , sizeof ( ascii_string ) , - 1 , STR_TERMINATE ) ;
d_printf ( " \t Load Order Group = %s \n " , ascii_string ) ;
}
if ( config . dependencies ) {
rpcstr_pull ( ascii_string , config . dependencies - > buffer , sizeof ( ascii_string ) , - 1 , STR_TERMINATE ) ;
d_printf ( " \t Dependencies = %s \n " , ascii_string ) ;
}
if ( config . startname ) {
rpcstr_pull ( ascii_string , config . startname - > buffer , sizeof ( ascii_string ) , - 1 , STR_TERMINATE ) ;
d_printf ( " \t Start Name = %s \n " , ascii_string ) ;
}
if ( config . displayname ) {
rpcstr_pull ( ascii_string , config . displayname - > buffer , sizeof ( ascii_string ) , - 1 , STR_TERMINATE ) ;
d_printf ( " \t Display Name = %s \n " , ascii_string ) ;
}
2008-02-18 13:48:51 +03:00
done :
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hService , NULL ) ;
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hSCM , NULL ) ;
2005-09-30 21:13:37 +04:00
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
2008-02-18 13:48:51 +03:00
}
2005-03-25 03:38:30 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static NTSTATUS rpc_service_stop_internal ( const DOM_SID * domain_sid ,
2008-02-18 13:48:51 +03:00
const char * domain_name ,
2005-09-30 21:13:37 +04:00
struct cli_state * cli ,
struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
int argc ,
const char * * argv )
2005-03-25 03:38:30 +03:00
{
POLICY_HND hSCM ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:10:54 +03:00
NTSTATUS status ;
2005-03-25 03:38:30 +03:00
fstring servicename ;
2008-02-18 14:10:54 +03:00
const char * server_name ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
if ( argc ! = 1 ) {
d_printf ( " Usage: net rpc service status <service> \n " ) ;
return NT_STATUS_OK ;
}
fstrcpy ( servicename , argv [ 0 ] ) ;
/* Open the Service Control Manager */
2008-02-18 14:10:54 +03:00
CLI_SERVER_NAME_SLASH ( mem_ctx , server_name , pipe_hnd ) ;
NT_STATUS_HAVE_NO_MEMORY ( server_name ) ;
status = rpccli_svcctl_OpenSCManagerW ( pipe_hnd , mem_ctx ,
server_name ,
NULL ,
SC_RIGHT_MGR_ENUMERATE_SERVICE ,
& hSCM ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open Service Control Manager. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
}
2008-02-18 13:48:51 +03:00
result = control_service ( pipe_hnd , mem_ctx , & hSCM , servicename ,
2005-03-25 03:38:30 +03:00
SVCCTL_CONTROL_STOP , SVCCTL_STOPPED ) ;
2008-02-18 13:48:51 +03:00
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hSCM , NULL ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
2008-02-18 13:48:51 +03:00
}
2005-03-25 03:38:30 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static NTSTATUS rpc_service_pause_internal ( const DOM_SID * domain_sid ,
2008-02-18 13:48:51 +03:00
const char * domain_name ,
2005-09-30 21:13:37 +04:00
struct cli_state * cli ,
struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
int argc ,
const char * * argv )
2005-03-25 03:38:30 +03:00
{
POLICY_HND hSCM ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:10:54 +03:00
NTSTATUS status ;
2005-03-25 03:38:30 +03:00
fstring servicename ;
2008-02-18 14:10:54 +03:00
const char * server_name ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
if ( argc ! = 1 ) {
d_printf ( " Usage: net rpc service status <service> \n " ) ;
return NT_STATUS_OK ;
}
fstrcpy ( servicename , argv [ 0 ] ) ;
/* Open the Service Control Manager */
2008-02-18 14:10:54 +03:00
CLI_SERVER_NAME_SLASH ( mem_ctx , server_name , pipe_hnd ) ;
NT_STATUS_HAVE_NO_MEMORY ( server_name ) ;
status = rpccli_svcctl_OpenSCManagerW ( pipe_hnd , mem_ctx ,
server_name ,
NULL ,
SC_RIGHT_MGR_ENUMERATE_SERVICE ,
& hSCM ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open Service Control Manager. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
}
2008-02-18 13:48:51 +03:00
result = control_service ( pipe_hnd , mem_ctx , & hSCM , servicename ,
2005-03-25 03:38:30 +03:00
SVCCTL_CONTROL_PAUSE , SVCCTL_PAUSED ) ;
2008-02-18 13:48:51 +03:00
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hSCM , NULL ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 01:32:31 +03:00
return werror_to_ntstatus ( result ) ;
2008-02-18 13:48:51 +03:00
}
2005-03-24 07:25:49 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static NTSTATUS rpc_service_resume_internal ( const DOM_SID * domain_sid ,
2008-02-18 13:48:51 +03:00
const char * domain_name ,
2005-09-30 21:13:37 +04:00
struct cli_state * cli ,
struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
int argc ,
const char * * argv )
2005-03-25 03:38:30 +03:00
{
POLICY_HND hSCM ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:10:54 +03:00
NTSTATUS status ;
2005-03-25 03:38:30 +03:00
fstring servicename ;
2008-02-18 14:10:54 +03:00
const char * server_name ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
if ( argc ! = 1 ) {
d_printf ( " Usage: net rpc service status <service> \n " ) ;
return NT_STATUS_OK ;
}
fstrcpy ( servicename , argv [ 0 ] ) ;
/* Open the Service Control Manager */
2008-02-18 14:10:54 +03:00
CLI_SERVER_NAME_SLASH ( mem_ctx , server_name , pipe_hnd ) ;
NT_STATUS_HAVE_NO_MEMORY ( server_name ) ;
status = rpccli_svcctl_OpenSCManagerW ( pipe_hnd , mem_ctx ,
server_name ,
NULL ,
SC_RIGHT_MGR_ENUMERATE_SERVICE ,
& hSCM ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open Service Control Manager. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
}
2008-02-18 13:48:51 +03:00
result = control_service ( pipe_hnd , mem_ctx , & hSCM , servicename ,
2005-03-25 03:38:30 +03:00
SVCCTL_CONTROL_CONTINUE , SVCCTL_RUNNING ) ;
2008-02-18 13:48:51 +03:00
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hSCM , NULL ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
2008-02-18 13:48:51 +03:00
}
2005-03-25 03:38:30 +03:00
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
static NTSTATUS rpc_service_start_internal ( const DOM_SID * domain_sid ,
2008-02-18 13:48:51 +03:00
const char * domain_name ,
2005-09-30 21:13:37 +04:00
struct cli_state * cli ,
struct rpc_pipe_client * pipe_hnd ,
2008-02-18 13:48:51 +03:00
TALLOC_CTX * mem_ctx ,
2005-09-30 21:13:37 +04:00
int argc ,
const char * * argv )
2005-03-25 03:38:30 +03:00
{
POLICY_HND hSCM , hService ;
WERROR result = WERR_GENERAL_FAILURE ;
2008-02-18 14:10:54 +03:00
NTSTATUS status ;
2005-03-25 03:38:30 +03:00
uint32 state = 0 ;
2008-02-18 14:10:54 +03:00
const char * server_name ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
if ( argc ! = 1 ) {
d_printf ( " Usage: net rpc service status <service> \n " ) ;
return NT_STATUS_OK ;
}
/* Open the Service Control Manager */
2008-02-18 14:10:54 +03:00
CLI_SERVER_NAME_SLASH ( mem_ctx , server_name , pipe_hnd ) ;
NT_STATUS_HAVE_NO_MEMORY ( server_name ) ;
status = rpccli_svcctl_OpenSCManagerW ( pipe_hnd , mem_ctx ,
server_name ,
NULL ,
SC_RIGHT_MGR_ENUMERATE_SERVICE ,
& hSCM ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open Service Control Manager. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
/* Open the Service */
2008-02-18 13:48:51 +03:00
2008-02-18 14:45:13 +03:00
status = rpccli_svcctl_OpenServiceW ( pipe_hnd , mem_ctx ,
& hSCM ,
argv [ 0 ] ,
SC_RIGHT_SVC_START ,
& hService ,
& result ) ;
2005-03-25 03:38:30 +03:00
2008-02-18 14:45:13 +03:00
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Failed to open service. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
/* get the status */
2008-02-19 04:29:16 +03:00
status = rpccli_svcctl_StartServiceW ( pipe_hnd , mem_ctx ,
& hService ,
0 ,
NULL ,
& result ) ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! W_ERROR_IS_OK ( result ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Query status request failed. [%s] \n " , dos_errstr ( result ) ) ;
2005-03-25 03:38:30 +03:00
goto done ;
}
2008-02-18 13:48:51 +03:00
2008-02-18 14:45:13 +03:00
result = watch_service_state ( pipe_hnd , mem_ctx , & hSCM , argv [ 0 ] , SVCCTL_RUNNING , & state ) ;
2008-02-18 13:48:51 +03:00
2005-03-25 03:38:30 +03:00
if ( W_ERROR_IS_OK ( result ) & & ( state = = SVCCTL_RUNNING ) )
2008-02-18 14:45:13 +03:00
d_printf ( " Successfully started service: %s \n " , argv [ 0 ] ) ;
2005-03-25 03:38:30 +03:00
else
2008-02-18 14:45:13 +03:00
d_fprintf ( stderr , " Failed to start service: %s [%s] \n " , argv [ 0 ] , dos_errstr ( result ) ) ;
2008-02-18 13:48:51 +03:00
done :
2007-12-03 20:25:13 +03:00
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hService , NULL ) ;
rpccli_svcctl_CloseServiceHandle ( pipe_hnd , mem_ctx , & hSCM , NULL ) ;
2005-09-30 21:13:37 +04:00
2005-03-25 03:38:30 +03:00
return werror_to_ntstatus ( result ) ;
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-03-24 07:25:49 +03:00
static int rpc_service_list ( int argc , const char * * argv )
{
2008-02-18 13:48:51 +03:00
return run_rpc_command ( NULL , PI_SVCCTL , 0 ,
2005-03-24 07:25:49 +03:00
rpc_service_list_internal , argc , argv ) ;
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int rpc_service_start ( int argc , const char * * argv )
{
2008-02-18 13:48:51 +03:00
return run_rpc_command ( NULL , PI_SVCCTL , 0 ,
2005-03-25 03:38:30 +03:00
rpc_service_start_internal , argc , argv ) ;
2005-03-24 07:25:49 +03:00
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int rpc_service_stop ( int argc , const char * * argv )
{
2008-02-18 13:48:51 +03:00
return run_rpc_command ( NULL , PI_SVCCTL , 0 ,
2005-03-25 03:38:30 +03:00
rpc_service_stop_internal , argc , argv ) ;
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int rpc_service_resume ( int argc , const char * * argv )
{
2008-02-18 13:48:51 +03:00
return run_rpc_command ( NULL , PI_SVCCTL , 0 ,
2005-03-25 03:38:30 +03:00
rpc_service_resume_internal , argc , argv ) ;
2005-03-24 07:25:49 +03:00
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int rpc_service_pause ( int argc , const char * * argv )
{
2008-02-18 13:48:51 +03:00
return run_rpc_command ( NULL , PI_SVCCTL , 0 ,
2005-03-25 03:38:30 +03:00
rpc_service_pause_internal , argc , argv ) ;
2005-03-24 07:25:49 +03:00
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int rpc_service_status ( int argc , const char * * argv )
{
2008-02-18 13:48:51 +03:00
return run_rpc_command ( NULL , PI_SVCCTL , 0 ,
2005-03-25 01:32:31 +03:00
rpc_service_status_internal , argc , argv ) ;
2005-03-24 07:25:49 +03:00
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int net_help_service ( int argc , const char * * argv )
{
d_printf ( " net rpc service list View configured Win32 services \n " ) ;
d_printf ( " net rpc service start <service> Start a service \n " ) ;
d_printf ( " net rpc service stop <service> Stop a service \n " ) ;
d_printf ( " net rpc service pause <service> Pause a service \n " ) ;
2005-08-30 15:31:49 +04:00
d_printf ( " net rpc service resume <service> Resume a paused service \n " ) ;
2005-03-24 07:25:49 +03:00
d_printf ( " net rpc service status <service> View the current status of a service \n " ) ;
2008-02-18 13:48:51 +03:00
2005-03-24 07:25:49 +03:00
return - 1 ;
}
/********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-02-18 13:48:51 +03:00
int net_rpc_service ( int argc , const char * * argv )
2005-03-24 07:25:49 +03:00
{
struct functable func [ ] = {
{ " list " , rpc_service_list } ,
{ " start " , rpc_service_start } ,
{ " stop " , rpc_service_stop } ,
{ " pause " , rpc_service_pause } ,
2005-03-25 03:38:30 +03:00
{ " resume " , rpc_service_resume } ,
2005-03-24 07:25:49 +03:00
{ " status " , rpc_service_status } ,
{ NULL , NULL }
} ;
2008-02-18 13:48:51 +03:00
2005-03-24 07:25:49 +03:00
if ( argc )
return net_run_function ( argc , argv , func , net_help_service ) ;
2008-02-18 13:48:51 +03:00
2005-03-24 07:25:49 +03:00
return net_help_service ( argc , argv ) ;
}