2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Gerald (Jerry) Carter 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "utils/net.h"
24 /********************************************************************
25 ********************************************************************/
27 static WERROR
query_service_state( struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
28 POLICY_HND
*hSCM
, const char *service
, uint32
*state
)
31 SERVICE_STATUS service_status
;
32 WERROR result
= WERR_GENERAL_FAILURE
;
34 /* now cycle until the status is actually 'watch_state' */
36 result
= cli_svcctl_open_service( cli
, mem_ctx
, hSCM
, &hService
,
37 service
, SC_RIGHT_SVC_QUERY_STATUS
);
39 if ( !W_ERROR_IS_OK(result
) ) {
40 d_printf("Failed to open service. [%s]\n", dos_errstr(result
));
44 result
= cli_svcctl_query_status( cli
, mem_ctx
, &hService
, &service_status
);
45 if ( W_ERROR_IS_OK(result
) ) {
46 *state
= service_status
.state
;
49 cli_svcctl_close_service( cli
, mem_ctx
, &hService
);
54 /********************************************************************
55 ********************************************************************/
57 static WERROR
watch_service_state( struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
58 POLICY_HND
*hSCM
, const char *service
,
59 uint32 watch_state
, uint32
*final_state
)
63 WERROR result
= WERR_GENERAL_FAILURE
;
67 while ( (state
!= watch_state
) && i
<30 ) {
70 result
= query_service_state( cli
, mem_ctx
, hSCM
, service
, &state
);
71 if ( !W_ERROR_IS_OK(result
) ) {
86 /********************************************************************
87 ********************************************************************/
89 static WERROR
control_service( struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
90 POLICY_HND
*hSCM
, const char *service
,
91 uint32 control
, uint32 watch_state
)
94 WERROR result
= WERR_GENERAL_FAILURE
;
95 SERVICE_STATUS service_status
;
98 /* Open the Service */
100 result
= cli_svcctl_open_service( cli
, mem_ctx
, hSCM
, &hService
,
101 service
, (SC_RIGHT_SVC_STOP
|SC_RIGHT_SVC_PAUSE_CONTINUE
) );
103 if ( !W_ERROR_IS_OK(result
) ) {
104 d_printf("Failed to open service. [%s]\n", dos_errstr(result
));
110 result
= cli_svcctl_control_service( cli
, mem_ctx
, &hService
,
111 control
, &service_status
);
113 if ( !W_ERROR_IS_OK(result
) ) {
114 d_printf("Control service request failed. [%s]\n", dos_errstr(result
));
118 /* loop -- checking the state until we are where we want to be */
120 result
= watch_service_state( cli
, mem_ctx
, hSCM
, service
, watch_state
, &state
);
122 d_printf("%s service is %s.\n", service
, svc_status_string(state
));
125 cli_svcctl_close_service( cli
, mem_ctx
, &hService
);
130 /********************************************************************
131 ********************************************************************/
133 static NTSTATUS
rpc_service_list_internal( const DOM_SID
*domain_sid
, const char *domain_name
,
134 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
135 int argc
, const char **argv
)
138 ENUM_SERVICES_STATUS
*services
;
139 WERROR result
= WERR_GENERAL_FAILURE
;
142 uint32 num_services
= 0;
146 d_printf("Usage: net rpc service list\n");
150 result
= cli_svcctl_open_scm( cli
, mem_ctx
, &hSCM
, SC_RIGHT_MGR_ENUMERATE_SERVICE
);
151 if ( !W_ERROR_IS_OK(result
) ) {
152 d_printf("Failed to open Service Control Manager. [%s]\n", dos_errstr(result
));
153 return werror_to_ntstatus(result
);
156 result
= cli_svcctl_enumerate_services( cli
, mem_ctx
, &hSCM
, SVCCTL_TYPE_WIN32
,
157 SVCCTL_STATE_ALL
, &num_services
, &services
);
159 if ( !W_ERROR_IS_OK(result
) ) {
160 d_printf("Failed to enumerate services. [%s]\n", dos_errstr(result
));
164 if ( num_services
== 0 )
165 d_printf("No services returned\n");
167 for ( i
=0; i
<num_services
; i
++ ) {
168 rpcstr_pull( servicename
, services
[i
].servicename
.buffer
, sizeof(servicename
), -1, STR_TERMINATE
);
169 rpcstr_pull( displayname
, services
[i
].displayname
.buffer
, sizeof(displayname
), -1, STR_TERMINATE
);
171 d_printf("%-20s \"%s\"\n", servicename
, displayname
);
175 cli_svcctl_close_service( cli
, mem_ctx
, &hSCM
);
177 return werror_to_ntstatus(result
);
180 /********************************************************************
181 ********************************************************************/
183 static NTSTATUS
rpc_service_status_internal( const DOM_SID
*domain_sid
, const char *domain_name
,
184 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
185 int argc
, const char **argv
)
187 POLICY_HND hSCM
, hService
;
188 WERROR result
= WERR_GENERAL_FAILURE
;
190 SERVICE_STATUS service_status
;
191 SERVICE_CONFIG config
;
192 fstring ascii_string
;
195 d_printf("Usage: net rpc service status <service>\n");
199 fstrcpy( servicename
, argv
[0] );
201 /* Open the Service Control Manager */
203 result
= cli_svcctl_open_scm( cli
, mem_ctx
, &hSCM
, SC_RIGHT_MGR_ENUMERATE_SERVICE
);
204 if ( !W_ERROR_IS_OK(result
) ) {
205 d_printf("Failed to open Service Control Manager. [%s]\n", dos_errstr(result
));
206 return werror_to_ntstatus(result
);
209 /* Open the Service */
211 result
= cli_svcctl_open_service( cli
, mem_ctx
, &hSCM
, &hService
, servicename
,
212 (SC_RIGHT_SVC_QUERY_STATUS
|SC_RIGHT_SVC_QUERY_CONFIG
) );
214 if ( !W_ERROR_IS_OK(result
) ) {
215 d_printf("Failed to open service. [%s]\n", dos_errstr(result
));
221 result
= cli_svcctl_query_status( cli
, mem_ctx
, &hService
, &service_status
);
222 if ( !W_ERROR_IS_OK(result
) ) {
223 d_printf("Query status request failed. [%s]\n", dos_errstr(result
));
227 d_printf("%s service is %s.\n", servicename
, svc_status_string(service_status
.state
));
231 result
= cli_svcctl_query_config( cli
, mem_ctx
, &hService
, &config
);
232 if ( !W_ERROR_IS_OK(result
) ) {
233 d_printf("Query config request failed. [%s]\n", dos_errstr(result
));
237 /* print out the configuration information for the service */
239 d_printf("Configuration details:\n");
240 d_printf("\tService Type = 0x%x\n", config
.service_type
);
241 d_printf("\tStart Type = 0x%x\n", config
.start_type
);
242 d_printf("\tError Control = 0x%x\n", config
.error_control
);
243 d_printf("\tTag ID = 0x%x\n", config
.tag_id
);
245 if ( config
.executablepath
) {
246 rpcstr_pull( ascii_string
, config
.executablepath
->buffer
, sizeof(ascii_string
), -1, STR_TERMINATE
);
247 d_printf("\tExecutable Path = %s\n", ascii_string
);
250 if ( config
.loadordergroup
) {
251 rpcstr_pull( ascii_string
, config
.loadordergroup
->buffer
, sizeof(ascii_string
), -1, STR_TERMINATE
);
252 d_printf("\tLoad Order Group = %s\n", ascii_string
);
255 if ( config
.dependencies
) {
256 rpcstr_pull( ascii_string
, config
.dependencies
->buffer
, sizeof(ascii_string
), -1, STR_TERMINATE
);
257 d_printf("\tDependencies = %s\n", ascii_string
);
260 if ( config
.startname
) {
261 rpcstr_pull( ascii_string
, config
.startname
->buffer
, sizeof(ascii_string
), -1, STR_TERMINATE
);
262 d_printf("\tStart Name = %s\n", ascii_string
);
265 if ( config
.displayname
) {
266 rpcstr_pull( ascii_string
, config
.displayname
->buffer
, sizeof(ascii_string
), -1, STR_TERMINATE
);
267 d_printf("\tDisplay Name = %s\n", ascii_string
);
271 cli_svcctl_close_service( cli
, mem_ctx
, &hService
);
272 cli_svcctl_close_service( cli
, mem_ctx
, &hSCM
);
274 return werror_to_ntstatus(result
);
278 /********************************************************************
279 ********************************************************************/
281 static NTSTATUS
rpc_service_stop_internal( const DOM_SID
*domain_sid
, const char *domain_name
,
282 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
283 int argc
, const char **argv
)
286 WERROR result
= WERR_GENERAL_FAILURE
;
290 d_printf("Usage: net rpc service status <service>\n");
294 fstrcpy( servicename
, argv
[0] );
296 /* Open the Service Control Manager */
298 result
= cli_svcctl_open_scm( cli
, mem_ctx
, &hSCM
, SC_RIGHT_MGR_ENUMERATE_SERVICE
);
299 if ( !W_ERROR_IS_OK(result
) ) {
300 d_printf("Failed to open Service Control Manager. [%s]\n", dos_errstr(result
));
301 return werror_to_ntstatus(result
);
304 result
= control_service( cli
, mem_ctx
, &hSCM
, servicename
,
305 SVCCTL_CONTROL_STOP
, SVCCTL_STOPPED
);
307 cli_svcctl_close_service( cli
, mem_ctx
, &hSCM
);
309 return werror_to_ntstatus(result
);
312 /********************************************************************
313 ********************************************************************/
315 static NTSTATUS
rpc_service_pause_internal( const DOM_SID
*domain_sid
, const char *domain_name
,
316 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
317 int argc
, const char **argv
)
320 WERROR result
= WERR_GENERAL_FAILURE
;
324 d_printf("Usage: net rpc service status <service>\n");
328 fstrcpy( servicename
, argv
[0] );
330 /* Open the Service Control Manager */
332 result
= cli_svcctl_open_scm( cli
, mem_ctx
, &hSCM
, SC_RIGHT_MGR_ENUMERATE_SERVICE
);
333 if ( !W_ERROR_IS_OK(result
) ) {
334 d_printf("Failed to open Service Control Manager. [%s]\n", dos_errstr(result
));
335 return werror_to_ntstatus(result
);
338 result
= control_service( cli
, mem_ctx
, &hSCM
, servicename
,
339 SVCCTL_CONTROL_PAUSE
, SVCCTL_PAUSED
);
341 cli_svcctl_close_service( cli
, mem_ctx
, &hSCM
);
343 return werror_to_ntstatus(result
);
346 /********************************************************************
347 ********************************************************************/
349 static NTSTATUS
rpc_service_resume_internal( const DOM_SID
*domain_sid
, const char *domain_name
,
350 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
351 int argc
, const char **argv
)
354 WERROR result
= WERR_GENERAL_FAILURE
;
358 d_printf("Usage: net rpc service status <service>\n");
362 fstrcpy( servicename
, argv
[0] );
364 /* Open the Service Control Manager */
366 result
= cli_svcctl_open_scm( cli
, mem_ctx
, &hSCM
, SC_RIGHT_MGR_ENUMERATE_SERVICE
);
367 if ( !W_ERROR_IS_OK(result
) ) {
368 d_printf("Failed to open Service Control Manager. [%s]\n", dos_errstr(result
));
369 return werror_to_ntstatus(result
);
372 result
= control_service( cli
, mem_ctx
, &hSCM
, servicename
,
373 SVCCTL_CONTROL_CONTINUE
, SVCCTL_RUNNING
);
375 cli_svcctl_close_service( cli
, mem_ctx
, &hSCM
);
377 return werror_to_ntstatus(result
);
380 /********************************************************************
381 ********************************************************************/
383 static NTSTATUS
rpc_service_start_internal( const DOM_SID
*domain_sid
, const char *domain_name
,
384 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
385 int argc
, const char **argv
)
387 POLICY_HND hSCM
, hService
;
388 WERROR result
= WERR_GENERAL_FAILURE
;
393 d_printf("Usage: net rpc service status <service>\n");
397 fstrcpy( servicename
, argv
[0] );
399 /* Open the Service Control Manager */
401 result
= cli_svcctl_open_scm( cli
, mem_ctx
, &hSCM
, SC_RIGHT_MGR_ENUMERATE_SERVICE
);
402 if ( !W_ERROR_IS_OK(result
) ) {
403 d_printf("Failed to open Service Control Manager. [%s]\n", dos_errstr(result
));
404 return werror_to_ntstatus(result
);
407 /* Open the Service */
409 result
= cli_svcctl_open_service( cli
, mem_ctx
, &hSCM
, &hService
,
410 servicename
, SC_RIGHT_SVC_START
);
412 if ( !W_ERROR_IS_OK(result
) ) {
413 d_printf("Failed to open service. [%s]\n", dos_errstr(result
));
419 result
= cli_svcctl_start_service( cli
, mem_ctx
, &hService
, NULL
, 0 );
420 if ( !W_ERROR_IS_OK(result
) ) {
421 d_printf("Query status request failed. [%s]\n", dos_errstr(result
));
425 result
= watch_service_state( cli
, mem_ctx
, &hSCM
, servicename
, SVCCTL_RUNNING
, &state
);
427 if ( W_ERROR_IS_OK(result
) && (state
== SVCCTL_RUNNING
) )
428 d_printf("Successfully started service: %s\n", servicename
);
430 d_printf("Failed to start service: %s [%s]\n", servicename
, dos_errstr(result
) );
433 cli_svcctl_close_service( cli
, mem_ctx
, &hService
);
434 cli_svcctl_close_service( cli
, mem_ctx
, &hSCM
);
436 return werror_to_ntstatus(result
);
439 /********************************************************************
440 ********************************************************************/
442 static int rpc_service_list( int argc
, const char **argv
)
444 return run_rpc_command( NULL
, PI_SVCCTL
, 0,
445 rpc_service_list_internal
, argc
, argv
);
448 /********************************************************************
449 ********************************************************************/
451 static int rpc_service_start( int argc
, const char **argv
)
453 return run_rpc_command( NULL
, PI_SVCCTL
, 0,
454 rpc_service_start_internal
, argc
, argv
);
457 /********************************************************************
458 ********************************************************************/
460 static int rpc_service_stop( int argc
, const char **argv
)
462 return run_rpc_command( NULL
, PI_SVCCTL
, 0,
463 rpc_service_stop_internal
, argc
, argv
);
466 /********************************************************************
467 ********************************************************************/
469 static int rpc_service_resume( int argc
, const char **argv
)
471 return run_rpc_command( NULL
, PI_SVCCTL
, 0,
472 rpc_service_resume_internal
, argc
, argv
);
475 /********************************************************************
476 ********************************************************************/
478 static int rpc_service_pause( int argc
, const char **argv
)
480 return run_rpc_command( NULL
, PI_SVCCTL
, 0,
481 rpc_service_pause_internal
, argc
, argv
);
484 /********************************************************************
485 ********************************************************************/
487 static int rpc_service_status( int argc
, const char **argv
)
489 return run_rpc_command( NULL
, PI_SVCCTL
, 0,
490 rpc_service_status_internal
, argc
, argv
);
493 /********************************************************************
494 ********************************************************************/
496 static int net_help_service( int argc
, const char **argv
)
498 d_printf("net rpc service list View configured Win32 services\n");
499 d_printf("net rpc service start <service> Start a service\n");
500 d_printf("net rpc service stop <service> Stop a service\n");
501 d_printf("net rpc service pause <service> Pause a service\n");
502 d_printf("net rpc service resume <service> Resume a paused a service\n");
503 d_printf("net rpc service status <service> View the current status of a service\n");
508 /********************************************************************
509 ********************************************************************/
511 int net_rpc_service(int argc
, const char **argv
)
513 struct functable func
[] = {
514 {"list", rpc_service_list
},
515 {"start", rpc_service_start
},
516 {"stop", rpc_service_stop
},
517 {"pause", rpc_service_pause
},
518 {"resume", rpc_service_resume
},
519 {"status", rpc_service_status
},
524 return net_run_function( argc
, argv
, func
, net_help_service
);
526 return net_help_service( argc
, argv
);