docs: Improve description of the share commands in man smb.conf.
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_service.c
blob7d1c4860aaa8d3d4ba81e37bbbb223939723baad
1 /*
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/>. */
19 #include "includes.h"
20 #include "utils/net.h"
23 struct svc_state_msg {
24 uint32 flag;
25 const char *message;
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" },
36 { 0, NULL }
40 /********************************************************************
41 ********************************************************************/
42 const char *svc_status_string( uint32 state )
44 fstring msg;
45 int i;
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 );
52 break;
56 return talloc_strdup(talloc_tos(), msg);
59 /********************************************************************
60 ********************************************************************/
62 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
63 TALLOC_CTX *mem_ctx,
64 POLICY_HND *hSCM,
65 const char *service,
66 uint32 *state )
68 POLICY_HND hService;
69 SERVICE_STATUS service_status;
70 WERROR result = WERR_GENERAL_FAILURE;
71 NTSTATUS status;
73 /* now cycle until the status is actually 'watch_state' */
75 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
76 hSCM,
77 service,
78 SC_RIGHT_SVC_QUERY_STATUS,
79 &hService,
80 &result);
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));
83 return result;
86 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
87 &hService,
88 &service_status,
89 &result);
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);
97 return result;
100 /********************************************************************
101 ********************************************************************/
103 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
104 TALLOC_CTX *mem_ctx,
105 POLICY_HND *hSCM,
106 const char *service,
107 uint32 watch_state,
108 uint32 *final_state )
110 uint32 i;
111 uint32 state = 0;
112 WERROR result = WERR_GENERAL_FAILURE;
115 i = 0;
116 while ( (state != watch_state ) && i<30 ) {
117 /* get the status */
119 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
120 if ( !W_ERROR_IS_OK(result) ) {
121 break;
124 d_printf(".");
125 i++;
126 sys_usleep( 100 );
128 d_printf("\n");
130 *final_state = state;
132 return result;
135 /********************************************************************
136 ********************************************************************/
138 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
139 TALLOC_CTX *mem_ctx,
140 POLICY_HND *hSCM,
141 const char *service,
142 uint32 control,
143 uint32 watch_state )
145 POLICY_HND hService;
146 WERROR result = WERR_GENERAL_FAILURE;
147 NTSTATUS status;
148 SERVICE_STATUS service_status;
149 uint32 state = 0;
151 /* Open the Service */
153 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
154 hSCM,
155 service,
156 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
157 &hService,
158 &result);
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));
162 goto done;
165 /* get the status */
167 status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
168 &hService,
169 control,
170 &service_status,
171 &result);
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));
175 goto done;
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));
184 done:
185 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
187 return result;
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,
198 TALLOC_CTX *mem_ctx,
199 int argc,
200 const char **argv )
202 POLICY_HND hSCM;
203 ENUM_SERVICES_STATUS *services;
204 WERROR result = WERR_GENERAL_FAILURE;
205 NTSTATUS status;
206 fstring servicename;
207 fstring displayname;
208 uint32 num_services = 0;
209 int i;
211 if (argc != 0 ) {
212 d_printf("Usage: net rpc service list\n");
213 return NT_STATUS_OK;
216 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
217 pipe_hnd->srv_name_slash,
218 NULL,
219 SC_RIGHT_MGR_ENUMERATE_SERVICE,
220 &hSCM,
221 &result);
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, 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", win_errstr(result));
232 goto done;
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);
245 done:
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,
259 TALLOC_CTX *mem_ctx,
260 int argc,
261 const char **argv )
263 POLICY_HND hSCM, hService;
264 WERROR result = WERR_GENERAL_FAILURE;
265 NTSTATUS status;
266 SERVICE_STATUS service_status;
267 struct QUERY_SERVICE_CONFIG config;
268 uint32_t buf_size = sizeof(config);
269 uint32_t ret_size = 0;
271 if (argc != 1 ) {
272 d_printf("Usage: net rpc service status <service>\n");
273 return NT_STATUS_OK;
276 /* Open the Service Control Manager */
277 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
278 pipe_hnd->srv_name_slash,
279 NULL,
280 SC_RIGHT_MGR_ENUMERATE_SERVICE,
281 &hSCM,
282 &result);
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,
291 &hSCM,
292 argv[0],
293 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
294 &hService,
295 &result);
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));
299 goto done;
302 /* get the status */
304 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
305 &hService,
306 &service_status,
307 &result);
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));
311 goto done;
314 d_printf("%s service is %s.\n", argv[0], svc_status_string(service_status.state));
316 /* get the config */
318 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
319 &hService,
320 &config,
321 buf_size,
322 &ret_size,
323 &result);
324 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
325 buf_size = ret_size;
326 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
327 &hService,
328 &config,
329 buf_size,
330 &ret_size,
331 &result);
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));
336 goto done;
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);
368 done:
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,
383 TALLOC_CTX *mem_ctx,
384 int argc,
385 const char **argv )
387 POLICY_HND hSCM;
388 WERROR result = WERR_GENERAL_FAILURE;
389 NTSTATUS status;
390 fstring servicename;
392 if (argc != 1 ) {
393 d_printf("Usage: net rpc service status <service>\n");
394 return NT_STATUS_OK;
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,
402 NULL,
403 SC_RIGHT_MGR_ENUMERATE_SERVICE,
404 &hSCM,
405 &result);
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,
427 TALLOC_CTX *mem_ctx,
428 int argc,
429 const char **argv )
431 POLICY_HND hSCM;
432 WERROR result = WERR_GENERAL_FAILURE;
433 NTSTATUS status;
434 fstring servicename;
436 if (argc != 1 ) {
437 d_printf("Usage: net rpc service status <service>\n");
438 return NT_STATUS_OK;
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,
446 NULL,
447 SC_RIGHT_MGR_ENUMERATE_SERVICE,
448 &hSCM,
449 &result);
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,
471 TALLOC_CTX *mem_ctx,
472 int argc,
473 const char **argv )
475 POLICY_HND hSCM;
476 WERROR result = WERR_GENERAL_FAILURE;
477 NTSTATUS status;
478 fstring servicename;
480 if (argc != 1 ) {
481 d_printf("Usage: net rpc service status <service>\n");
482 return NT_STATUS_OK;
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,
490 NULL,
491 SC_RIGHT_MGR_ENUMERATE_SERVICE,
492 &hSCM,
493 &result);
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,
515 TALLOC_CTX *mem_ctx,
516 int argc,
517 const char **argv )
519 POLICY_HND hSCM, hService;
520 WERROR result = WERR_GENERAL_FAILURE;
521 NTSTATUS status;
522 uint32 state = 0;
524 if (argc != 1 ) {
525 d_printf("Usage: net rpc service status <service>\n");
526 return NT_STATUS_OK;
529 /* Open the Service Control Manager */
530 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
531 pipe_hnd->srv_name_slash,
532 NULL,
533 SC_RIGHT_MGR_ENUMERATE_SERVICE,
534 &hSCM,
535 &result);
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,
544 &hSCM,
545 argv[0],
546 SC_RIGHT_SVC_START,
547 &hService,
548 &result);
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));
552 goto done;
555 /* get the status */
557 status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
558 &hService,
560 NULL,
561 &result);
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));
565 goto done;
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] );
572 else
573 d_fprintf(stderr, "Failed to start service: %s [%s]\n", argv[0], win_errstr(result) );
575 done:
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) {
588 d_printf("Usage:\n"
589 "net rpc service list\n"
590 " View configured Win32 services\n");
591 return 0;
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) {
604 d_printf("Usage:\n"
605 "net rpc service start <service>\n"
606 " Start a Win32 service\n");
607 return 0;
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) {
620 d_printf("Usage:\n"
621 "net rpc service stop <service>\n"
622 " Stop a Win32 service\n");
623 return 0;
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) {
636 d_printf("Usage:\n"
637 "net rpc service resume <service>\n"
638 " Resume a Win32 service\n");
639 return 0;
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) {
652 d_printf("Usage:\n"
653 "net rpc service pause <service>\n"
654 " Pause a Win32 service\n");
655 return 0;
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) {
668 d_printf("Usage:\n"
669 "net rpc service status <service>\n"
670 " Show the current status of a service\n");
671 return 0;
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[] = {
685 "list",
686 rpc_service_list,
687 NET_TRANSPORT_RPC,
688 "View configured Win32 services",
689 "net rpc service list\n"
690 " View configured Win32 services"
693 "start",
694 rpc_service_start,
695 NET_TRANSPORT_RPC,
696 "Start a service",
697 "net rpc service start\n"
698 " Start a service"
701 "stop",
702 rpc_service_stop,
703 NET_TRANSPORT_RPC,
704 "Stop a service",
705 "net rpc service stop\n"
706 " Stop a service"
709 "pause",
710 rpc_service_pause,
711 NET_TRANSPORT_RPC,
712 "Pause a service",
713 "net rpc service pause\n"
714 " Pause a service"
717 "resume",
718 rpc_service_resume,
719 NET_TRANSPORT_RPC,
720 "Resume a paused service",
721 "net rpc service resume\n"
722 " Resume a service"
725 "status",
726 rpc_service_status,
727 NET_TRANSPORT_RPC,
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);