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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "utils/net.h"
23 struct svc_state_msg {
28 static struct svc_state_msg state_msg_table[] = {
29 { SVCCTL_STOPPED, "stopped" },
30 { SVCCTL_START_PENDING, "start pending" },
31 { SVCCTL_STOP_PENDING, "stop pending" },
32 { SVCCTL_RUNNING, "running" },
33 { SVCCTL_CONTINUE_PENDING, "resume pending" },
34 { SVCCTL_PAUSE_PENDING, "pause pending" },
35 { SVCCTL_PAUSED, "paused" },
40 /********************************************************************
41 ********************************************************************/
42 const char *svc_status_string( uint32 state )
47 fstr_sprintf( msg, "Unknown State [%d]", state );
49 for ( i=0; state_msg_table[i].message; i++ ) {
50 if ( state_msg_table[i].flag == state ) {
51 fstrcpy( msg, state_msg_table[i].message );
56 return talloc_strdup(talloc_tos(), msg);
59 /********************************************************************
60 ********************************************************************/
62 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
69 struct SERVICE_STATUS service_status;
70 WERROR result = WERR_GENERAL_FAILURE;
73 /* now cycle until the status is actually 'watch_state' */
75 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
78 SC_RIGHT_SVC_QUERY_STATUS,
81 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
82 d_fprintf(stderr, "Failed to open service. [%s]\n", win_errstr(result));
86 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
91 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
92 *state = service_status.state;
95 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
100 /********************************************************************
101 ********************************************************************/
103 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
108 uint32 *final_state )
112 WERROR result = WERR_GENERAL_FAILURE;
116 while ( (state != watch_state ) && i<30 ) {
119 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
120 if ( !W_ERROR_IS_OK(result) ) {
130 *final_state = state;
135 /********************************************************************
136 ********************************************************************/
138 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
146 WERROR result = WERR_GENERAL_FAILURE;
148 struct SERVICE_STATUS service_status;
151 /* Open the Service */
153 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
156 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
160 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
161 d_fprintf(stderr, "Failed to open service. [%s]\n", win_errstr(result));
167 status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
173 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
174 d_fprintf(stderr, "Control service request failed. [%s]\n", win_errstr(result));
178 /* loop -- checking the state until we are where we want to be */
180 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
182 d_printf("%s service is %s.\n", service, svc_status_string(state));
185 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
190 /********************************************************************
191 ********************************************************************/
193 static NTSTATUS rpc_service_list_internal(struct net_context *c,
194 const DOM_SID *domain_sid,
195 const char *domain_name,
196 struct cli_state *cli,
197 struct rpc_pipe_client *pipe_hnd,
203 ENUM_SERVICES_STATUS *services;
204 WERROR result = WERR_GENERAL_FAILURE;
208 uint32 num_services = 0;
212 d_printf("Usage: net rpc service list\n");
216 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
217 pipe_hnd->srv_name_slash,
219 SC_RIGHT_MGR_ENUMERATE_SERVICE,
222 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
223 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", win_errstr(result));
224 return werror_to_ntstatus(result);
227 result = rpccli_svcctl_enumerate_services(pipe_hnd, mem_ctx, &hSCM, SERVICE_TYPE_WIN32,
228 SERVICE_STATE_ALL, &num_services, &services );
230 if ( !W_ERROR_IS_OK(result) ) {
231 d_fprintf(stderr, "Failed to enumerate services. [%s]\n", win_errstr(result));
235 if ( num_services == 0 )
236 d_printf("No services returned\n");
238 for ( i=0; i<num_services; i++ ) {
239 rpcstr_pull( servicename, services[i].servicename.buffer, sizeof(servicename), -1, STR_TERMINATE );
240 rpcstr_pull( displayname, services[i].displayname.buffer, sizeof(displayname), -1, STR_TERMINATE );
242 d_printf("%-20s \"%s\"\n", servicename, displayname);
246 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
248 return werror_to_ntstatus(result);
251 /********************************************************************
252 ********************************************************************/
254 static NTSTATUS rpc_service_status_internal(struct net_context *c,
255 const DOM_SID *domain_sid,
256 const char *domain_name,
257 struct cli_state *cli,
258 struct rpc_pipe_client *pipe_hnd,
263 POLICY_HND hSCM, hService;
264 WERROR result = WERR_GENERAL_FAILURE;
266 struct SERVICE_STATUS service_status;
267 struct QUERY_SERVICE_CONFIG config;
268 uint32_t buf_size = sizeof(config);
269 uint32_t ret_size = 0;
272 d_printf("Usage: net rpc service status <service>\n");
276 /* Open the Service Control Manager */
277 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
278 pipe_hnd->srv_name_slash,
280 SC_RIGHT_MGR_ENUMERATE_SERVICE,
283 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
284 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", win_errstr(result));
285 return werror_to_ntstatus(result);
288 /* Open the Service */
290 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
293 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
297 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
298 d_fprintf(stderr, "Failed to open service. [%s]\n", win_errstr(result));
304 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
309 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
310 d_fprintf(stderr, "Query status request failed. [%s]\n", win_errstr(result));
314 d_printf("%s service is %s.\n", argv[0], svc_status_string(service_status.state));
318 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
324 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
326 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
334 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
335 d_fprintf(stderr, "Query config request failed. [%s]\n", win_errstr(result));
339 /* print out the configuration information for the service */
341 d_printf("Configuration details:\n");
342 d_printf("\tControls Accepted = 0x%x\n", service_status.controls_accepted);
343 d_printf("\tService Type = 0x%x\n", config.service_type);
344 d_printf("\tStart Type = 0x%x\n", config.start_type);
345 d_printf("\tError Control = 0x%x\n", config.error_control);
346 d_printf("\tTag ID = 0x%x\n", config.tag_id);
348 if (config.executablepath) {
349 d_printf("\tExecutable Path = %s\n", config.executablepath);
352 if (config.loadordergroup) {
353 d_printf("\tLoad Order Group = %s\n", config.loadordergroup);
356 if (config.dependencies) {
357 d_printf("\tDependencies = %s\n", config.dependencies);
360 if (config.startname) {
361 d_printf("\tStart Name = %s\n", config.startname);
364 if (config.displayname) {
365 d_printf("\tDisplay Name = %s\n", config.displayname);
369 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
370 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
372 return werror_to_ntstatus(result);
375 /********************************************************************
376 ********************************************************************/
378 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
379 const DOM_SID *domain_sid,
380 const char *domain_name,
381 struct cli_state *cli,
382 struct rpc_pipe_client *pipe_hnd,
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 */
400 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
401 pipe_hnd->srv_name_slash,
403 SC_RIGHT_MGR_ENUMERATE_SERVICE,
406 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
407 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", win_errstr(result));
408 return werror_to_ntstatus(result);
411 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
412 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
414 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
416 return werror_to_ntstatus(result);
419 /********************************************************************
420 ********************************************************************/
422 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
423 const DOM_SID *domain_sid,
424 const char *domain_name,
425 struct cli_state *cli,
426 struct rpc_pipe_client *pipe_hnd,
432 WERROR result = WERR_GENERAL_FAILURE;
437 d_printf("Usage: net rpc service status <service>\n");
441 fstrcpy( servicename, argv[0] );
443 /* Open the Service Control Manager */
444 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
445 pipe_hnd->srv_name_slash,
447 SC_RIGHT_MGR_ENUMERATE_SERVICE,
450 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
451 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", win_errstr(result));
452 return werror_to_ntstatus(result);
455 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
456 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
458 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
460 return werror_to_ntstatus(result);
463 /********************************************************************
464 ********************************************************************/
466 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
467 const DOM_SID *domain_sid,
468 const char *domain_name,
469 struct cli_state *cli,
470 struct rpc_pipe_client *pipe_hnd,
476 WERROR result = WERR_GENERAL_FAILURE;
481 d_printf("Usage: net rpc service status <service>\n");
485 fstrcpy( servicename, argv[0] );
487 /* Open the Service Control Manager */
488 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
489 pipe_hnd->srv_name_slash,
491 SC_RIGHT_MGR_ENUMERATE_SERVICE,
494 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
495 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", win_errstr(result));
496 return werror_to_ntstatus(result);
499 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
500 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
502 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
504 return werror_to_ntstatus(result);
507 /********************************************************************
508 ********************************************************************/
510 static NTSTATUS rpc_service_start_internal(struct net_context *c,
511 const DOM_SID *domain_sid,
512 const char *domain_name,
513 struct cli_state *cli,
514 struct rpc_pipe_client *pipe_hnd,
519 POLICY_HND hSCM, hService;
520 WERROR result = WERR_GENERAL_FAILURE;
525 d_printf("Usage: net rpc service status <service>\n");
529 /* Open the Service Control Manager */
530 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
531 pipe_hnd->srv_name_slash,
533 SC_RIGHT_MGR_ENUMERATE_SERVICE,
536 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
537 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", win_errstr(result));
538 return werror_to_ntstatus(result);
541 /* Open the Service */
543 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
550 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
551 d_fprintf(stderr, "Failed to open service. [%s]\n", win_errstr(result));
557 status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
563 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
564 d_fprintf(stderr, "Query status request failed. [%s]\n", win_errstr(result));
568 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
570 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
571 d_printf("Successfully started service: %s\n", argv[0] );
573 d_fprintf(stderr, "Failed to start service: %s [%s]\n", argv[0], win_errstr(result) );
576 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
577 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
579 return werror_to_ntstatus(result);
582 /********************************************************************
583 ********************************************************************/
585 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
587 if (c->display_usage) {
589 "net rpc service list\n"
590 " View configured Win32 services\n");
594 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
595 rpc_service_list_internal, argc, argv );
598 /********************************************************************
599 ********************************************************************/
601 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
603 if (c->display_usage) {
605 "net rpc service start <service>\n"
606 " Start a Win32 service\n");
610 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
611 rpc_service_start_internal, argc, argv );
614 /********************************************************************
615 ********************************************************************/
617 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
619 if (c->display_usage) {
621 "net rpc service stop <service>\n"
622 " Stop a Win32 service\n");
626 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
627 rpc_service_stop_internal, argc, argv );
630 /********************************************************************
631 ********************************************************************/
633 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
635 if (c->display_usage) {
637 "net rpc service resume <service>\n"
638 " Resume a Win32 service\n");
642 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
643 rpc_service_resume_internal, argc, argv );
646 /********************************************************************
647 ********************************************************************/
649 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
651 if (c->display_usage) {
653 "net rpc service pause <service>\n"
654 " Pause a Win32 service\n");
658 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
659 rpc_service_pause_internal, argc, argv );
662 /********************************************************************
663 ********************************************************************/
665 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
667 if (c->display_usage) {
669 "net rpc service status <service>\n"
670 " Show the current status of a service\n");
674 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
675 rpc_service_status_internal, argc, argv );
678 /********************************************************************
679 ********************************************************************/
681 int net_rpc_service(struct net_context *c, int argc, const char **argv)
683 struct functable func[] = {
688 "View configured Win32 services",
689 "net rpc service list\n"
690 " View configured Win32 services"
697 "net rpc service start\n"
705 "net rpc service stop\n"
713 "net rpc service pause\n"
720 "Resume a paused service",
721 "net rpc service resume\n"
728 "View current status of a service",
729 "net rpc service status\n"
730 " View current status of a service"
732 {NULL, NULL, 0, NULL, NULL}
735 return net_run_function(c, argc, argv, "net rpc service",func);