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 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", dos_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 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", dos_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", dos_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", dos_errstr(result));
224 return werror_to_ntstatus(result);
227 result = rpccli_svcctl_enumerate_services(pipe_hnd, mem_ctx, &hSCM, SVCCTL_TYPE_WIN32,
228 SVCCTL_STATE_ALL, &num_services, &services );
230 if ( !W_ERROR_IS_OK(result) ) {
231 d_fprintf(stderr, "Failed to enumerate services. [%s]\n", dos_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 SERVICE_STATUS service_status;
267 SERVICE_CONFIG config;
268 fstring ascii_string;
271 d_printf("Usage: net rpc service status <service>\n");
275 /* Open the Service Control Manager */
276 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
277 pipe_hnd->srv_name_slash,
279 SC_RIGHT_MGR_ENUMERATE_SERVICE,
282 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
283 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
284 return werror_to_ntstatus(result);
287 /* Open the Service */
289 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
292 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
296 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
297 d_fprintf(stderr, "Failed to open service. [%s]\n", dos_errstr(result));
303 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
308 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
309 d_fprintf(stderr, "Query status request failed. [%s]\n", dos_errstr(result));
313 d_printf("%s service is %s.\n", argv[0], svc_status_string(service_status.state));
317 result = rpccli_svcctl_query_config(pipe_hnd, mem_ctx, &hService, &config );
318 if ( !W_ERROR_IS_OK(result) ) {
319 d_fprintf(stderr, "Query config request failed. [%s]\n", dos_errstr(result));
323 /* print out the configuration information for the service */
325 d_printf("Configuration details:\n");
326 d_printf("\tControls Accepted = 0x%x\n", service_status.controls_accepted);
327 d_printf("\tService Type = 0x%x\n", config.service_type);
328 d_printf("\tStart Type = 0x%x\n", config.start_type);
329 d_printf("\tError Control = 0x%x\n", config.error_control);
330 d_printf("\tTag ID = 0x%x\n", config.tag_id);
332 if ( config.executablepath ) {
333 rpcstr_pull( ascii_string, config.executablepath->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
334 d_printf("\tExecutable Path = %s\n", ascii_string);
337 if ( config.loadordergroup ) {
338 rpcstr_pull( ascii_string, config.loadordergroup->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
339 d_printf("\tLoad Order Group = %s\n", ascii_string);
342 if ( config.dependencies ) {
343 rpcstr_pull( ascii_string, config.dependencies->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
344 d_printf("\tDependencies = %s\n", ascii_string);
347 if ( config.startname ) {
348 rpcstr_pull( ascii_string, config.startname->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
349 d_printf("\tStart Name = %s\n", ascii_string);
352 if ( config.displayname ) {
353 rpcstr_pull( ascii_string, config.displayname->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
354 d_printf("\tDisplay Name = %s\n", ascii_string);
358 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
359 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
361 return werror_to_ntstatus(result);
364 /********************************************************************
365 ********************************************************************/
367 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
368 const DOM_SID *domain_sid,
369 const char *domain_name,
370 struct cli_state *cli,
371 struct rpc_pipe_client *pipe_hnd,
377 WERROR result = WERR_GENERAL_FAILURE;
382 d_printf("Usage: net rpc service status <service>\n");
386 fstrcpy( servicename, argv[0] );
388 /* Open the Service Control Manager */
389 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
390 pipe_hnd->srv_name_slash,
392 SC_RIGHT_MGR_ENUMERATE_SERVICE,
395 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
396 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
397 return werror_to_ntstatus(result);
400 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
401 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
403 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
405 return werror_to_ntstatus(result);
408 /********************************************************************
409 ********************************************************************/
411 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
412 const DOM_SID *domain_sid,
413 const char *domain_name,
414 struct cli_state *cli,
415 struct rpc_pipe_client *pipe_hnd,
421 WERROR result = WERR_GENERAL_FAILURE;
426 d_printf("Usage: net rpc service status <service>\n");
430 fstrcpy( servicename, argv[0] );
432 /* Open the Service Control Manager */
433 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
434 pipe_hnd->srv_name_slash,
436 SC_RIGHT_MGR_ENUMERATE_SERVICE,
439 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
440 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
441 return werror_to_ntstatus(result);
444 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
445 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
447 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
449 return werror_to_ntstatus(result);
452 /********************************************************************
453 ********************************************************************/
455 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
456 const DOM_SID *domain_sid,
457 const char *domain_name,
458 struct cli_state *cli,
459 struct rpc_pipe_client *pipe_hnd,
465 WERROR result = WERR_GENERAL_FAILURE;
470 d_printf("Usage: net rpc service status <service>\n");
474 fstrcpy( servicename, argv[0] );
476 /* Open the Service Control Manager */
477 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
478 pipe_hnd->srv_name_slash,
480 SC_RIGHT_MGR_ENUMERATE_SERVICE,
483 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
484 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
485 return werror_to_ntstatus(result);
488 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
489 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
491 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
493 return werror_to_ntstatus(result);
496 /********************************************************************
497 ********************************************************************/
499 static NTSTATUS rpc_service_start_internal(struct net_context *c,
500 const DOM_SID *domain_sid,
501 const char *domain_name,
502 struct cli_state *cli,
503 struct rpc_pipe_client *pipe_hnd,
508 POLICY_HND hSCM, hService;
509 WERROR result = WERR_GENERAL_FAILURE;
514 d_printf("Usage: net rpc service status <service>\n");
518 /* Open the Service Control Manager */
519 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
520 pipe_hnd->srv_name_slash,
522 SC_RIGHT_MGR_ENUMERATE_SERVICE,
525 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
526 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
527 return werror_to_ntstatus(result);
530 /* Open the Service */
532 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
539 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
540 d_fprintf(stderr, "Failed to open service. [%s]\n", dos_errstr(result));
546 status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
552 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
553 d_fprintf(stderr, "Query status request failed. [%s]\n", dos_errstr(result));
557 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
559 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
560 d_printf("Successfully started service: %s\n", argv[0] );
562 d_fprintf(stderr, "Failed to start service: %s [%s]\n", argv[0], dos_errstr(result) );
565 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
566 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
568 return werror_to_ntstatus(result);
571 /********************************************************************
572 ********************************************************************/
574 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
576 if (c->display_usage) {
578 "net rpc service list\n"
579 " View configured Win32 services\n");
583 return run_rpc_command(c, NULL, PI_SVCCTL, 0,
584 rpc_service_list_internal, argc, argv );
587 /********************************************************************
588 ********************************************************************/
590 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
592 if (c->display_usage) {
594 "net rpc service start <service>\n"
595 " Start a Win32 service\n");
599 return run_rpc_command(c, NULL, PI_SVCCTL, 0,
600 rpc_service_start_internal, argc, argv );
603 /********************************************************************
604 ********************************************************************/
606 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
608 if (c->display_usage) {
610 "net rpc service stop <service>\n"
611 " Stop a Win32 service\n");
615 return run_rpc_command(c, NULL, PI_SVCCTL, 0,
616 rpc_service_stop_internal, argc, argv );
619 /********************************************************************
620 ********************************************************************/
622 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
624 if (c->display_usage) {
626 "net rpc service resume <service>\n"
627 " Resume a Win32 service\n");
631 return run_rpc_command(c, NULL, PI_SVCCTL, 0,
632 rpc_service_resume_internal, argc, argv );
635 /********************************************************************
636 ********************************************************************/
638 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
640 if (c->display_usage) {
642 "net rpc service pause <service>\n"
643 " Pause a Win32 service\n");
647 return run_rpc_command(c, NULL, PI_SVCCTL, 0,
648 rpc_service_pause_internal, argc, argv );
651 /********************************************************************
652 ********************************************************************/
654 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
656 if (c->display_usage) {
658 "net rpc service status <service>\n"
659 " Show the current status of a service\n");
663 return run_rpc_command(c, NULL, PI_SVCCTL, 0,
664 rpc_service_status_internal, argc, argv );
667 /********************************************************************
668 ********************************************************************/
670 int net_rpc_service(struct net_context *c, int argc, const char **argv)
672 struct functable func[] = {
677 "View configured Win32 services",
678 "net rpc service list\n"
679 " View configured Win32 services"
686 "net rpc service start\n"
694 "net rpc service stop\n"
702 "net rpc service pause\n"
709 "Resume a paused service",
710 "net rpc service resume\n"
717 "View current status of a service",
718 "net rpc service status\n"
719 " View current status of a service"
721 {NULL, NULL, 0, NULL, NULL}
724 return net_run_function(c, argc, argv, "net rpc service",func);