gp: Fix startup scripts add not always set runonce
[Samba.git] / source3 / utils / net_rpc_service.c
bloba0fbc51b95586ac7ccd7f361405797b116373595
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"
21 #include "rpc_client/rpc_client.h"
22 #include "../librpc/gen_ndr/ndr_svcctl.h"
23 #include "../librpc/gen_ndr/ndr_svcctl_c.h"
24 #include "lib/util/string_wrappers.h"
26 struct svc_state_msg {
27 uint32_t flag;
28 const char *message;
31 static struct svc_state_msg state_msg_table[] = {
32 { SVCCTL_STOPPED, N_("stopped") },
33 { SVCCTL_START_PENDING, N_("start pending") },
34 { SVCCTL_STOP_PENDING, N_("stop pending") },
35 { SVCCTL_RUNNING, N_("running") },
36 { SVCCTL_CONTINUE_PENDING, N_("resume pending") },
37 { SVCCTL_PAUSE_PENDING, N_("pause pending") },
38 { SVCCTL_PAUSED, N_("paused") },
39 { 0, NULL }
43 /********************************************************************
44 ********************************************************************/
45 const char *svc_status_string( uint32_t state )
47 fstring msg;
48 int i;
50 fstr_sprintf( msg, _("Unknown State [%d]"), state );
52 for ( i=0; state_msg_table[i].message; i++ ) {
53 if ( state_msg_table[i].flag == state ) {
54 fstrcpy( msg, state_msg_table[i].message );
55 break;
59 return talloc_strdup(talloc_tos(), msg);
62 /********************************************************************
63 ********************************************************************/
65 static WERROR open_service(struct dcerpc_binding_handle *b,
66 TALLOC_CTX *mem_ctx,
67 struct policy_handle *hSCM,
68 const char *service,
69 uint32_t access_mask,
70 struct policy_handle *hService)
72 NTSTATUS status;
73 WERROR result;
75 status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
76 hSCM,
77 service,
78 access_mask,
79 hService,
80 &result);
81 if (!NT_STATUS_IS_OK(status)) {
82 result = ntstatus_to_werror(status);
83 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
84 nt_errstr(status));
85 return result;
87 if (!W_ERROR_IS_OK(result) ) {
88 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
89 win_errstr(result));
90 return result;
93 return WERR_OK;
96 /********************************************************************
97 ********************************************************************/
99 static WERROR open_scm(struct dcerpc_binding_handle *b,
100 TALLOC_CTX *mem_ctx,
101 const char *server_name,
102 uint32_t access_mask,
103 struct policy_handle *hSCM)
105 NTSTATUS status;
106 WERROR result;
108 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
109 server_name,
110 NULL,
111 access_mask,
112 hSCM,
113 &result);
114 if (!NT_STATUS_IS_OK(status)) {
115 result = ntstatus_to_werror(status);
116 d_fprintf(stderr,
117 _("Failed to open Service Control Manager. [%s]\n"),
118 nt_errstr(status));
119 return result;
121 if (!W_ERROR_IS_OK(result)) {
122 d_fprintf(stderr,
123 _("Failed to open Service Control Manager. [%s]\n"),
124 win_errstr(result));
125 return result;
128 return WERR_OK;
131 /********************************************************************
132 ********************************************************************/
134 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
135 TALLOC_CTX *mem_ctx,
136 struct policy_handle *hSCM,
137 const char *service,
138 uint32_t *state )
140 struct policy_handle hService;
141 struct SERVICE_STATUS service_status;
142 WERROR result = WERR_GEN_FAILURE;
143 NTSTATUS status;
144 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
146 /* now cycle until the status is actually 'watch_state' */
148 result = open_service(b, mem_ctx, hSCM, service,
149 SC_RIGHT_SVC_QUERY_STATUS,
150 &hService);
151 if (!W_ERROR_IS_OK(result) ) {
152 return result;
155 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
156 &hService,
157 &service_status,
158 &result);
159 if (!NT_STATUS_IS_OK(status)) {
160 result = ntstatus_to_werror(status);
161 goto done;
163 if (!W_ERROR_IS_OK(result)) {
164 goto done;
167 *state = service_status.state;
169 done:
170 if (is_valid_policy_hnd(&hService)) {
171 WERROR _result;
172 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
175 return result;
178 /********************************************************************
179 ********************************************************************/
181 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
182 TALLOC_CTX *mem_ctx,
183 struct policy_handle *hSCM,
184 const char *service,
185 uint32_t watch_state,
186 uint32_t *final_state )
188 uint32_t i;
189 uint32_t state = 0;
190 WERROR result = WERR_GEN_FAILURE;
193 i = 0;
194 while ( (state != watch_state ) && i<30 ) {
195 /* get the status */
197 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
198 if ( !W_ERROR_IS_OK(result) ) {
199 break;
202 d_printf(".");
203 i++;
204 usleep( 100 );
206 d_printf("\n");
208 *final_state = state;
210 return result;
213 /********************************************************************
214 ********************************************************************/
216 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
217 TALLOC_CTX *mem_ctx,
218 struct policy_handle *hSCM,
219 const char *service,
220 uint32_t control,
221 uint32_t watch_state )
223 struct policy_handle hService;
224 WERROR result = WERR_GEN_FAILURE;
225 NTSTATUS status;
226 struct SERVICE_STATUS service_status;
227 uint32_t state = 0;
228 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
230 /* Open the Service */
232 result = open_service(b, mem_ctx, hSCM, service,
233 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
234 &hService);
235 if (!W_ERROR_IS_OK(result) ) {
236 return result;
239 /* get the status */
241 status = dcerpc_svcctl_ControlService(b, mem_ctx,
242 &hService,
243 control,
244 &service_status,
245 &result);
247 if (!NT_STATUS_IS_OK(status)) {
248 result = ntstatus_to_werror(status);
249 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
250 nt_errstr(status));
251 goto done;
253 if (!W_ERROR_IS_OK(result) ) {
254 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
255 win_errstr(result));
256 goto done;
259 /* loop -- checking the state until we are where we want to be */
261 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
263 d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
265 done:
266 if (is_valid_policy_hnd(&hService)) {
267 WERROR _result;
268 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
271 return result;
274 /********************************************************************
275 ********************************************************************/
277 static NTSTATUS rpc_service_list_internal(struct net_context *c,
278 const struct dom_sid *domain_sid,
279 const char *domain_name,
280 struct cli_state *cli,
281 struct rpc_pipe_client *pipe_hnd,
282 TALLOC_CTX *mem_ctx,
283 int argc,
284 const char **argv )
286 struct policy_handle hSCM;
287 struct ENUM_SERVICE_STATUSW *services = NULL;
288 WERROR result = WERR_GEN_FAILURE;
289 NTSTATUS status;
290 int i;
291 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
293 uint8_t *buffer;
294 uint32_t buf_size = 0;
295 uint32_t bytes_needed = 0;
296 uint32_t num_services = 0;
297 uint32_t resume_handle = 0;
299 if (argc != 0 ) {
300 d_printf("%s net rpc service list\n", _("Usage:"));
301 return NT_STATUS_OK;
304 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
305 SC_RIGHT_MGR_ENUMERATE_SERVICE,
306 &hSCM);
307 if (!W_ERROR_IS_OK(result)) {
308 return werror_to_ntstatus(result);
311 buffer = talloc_array(mem_ctx, uint8_t, buf_size);
312 if (buffer == NULL) {
313 status = NT_STATUS_NO_MEMORY;
314 goto done;
317 do {
318 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
319 &hSCM,
320 SERVICE_TYPE_WIN32,
321 SERVICE_STATE_ALL,
322 buffer,
323 buf_size,
324 &bytes_needed,
325 &num_services,
326 &resume_handle,
327 &result);
329 if (!NT_STATUS_IS_OK(status)) {
330 d_fprintf(stderr,
331 _("Failed to enumerate services. [%s]\n"),
332 nt_errstr(status));
333 break;
336 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
337 buf_size = bytes_needed;
338 buffer = talloc_realloc(mem_ctx, buffer, uint8_t, bytes_needed);
339 if (buffer == NULL) {
340 status = NT_STATUS_NO_MEMORY;
341 break;
343 continue;
346 if (!W_ERROR_IS_OK(result)) {
347 status = werror_to_ntstatus(result);
348 d_fprintf(stderr,
349 _("Failed to enumerate services. [%s]\n"),
350 win_errstr(result));
351 break;
354 if ( num_services == 0 ) {
355 d_printf(_("No services returned\n"));
356 break;
360 enum ndr_err_code ndr_err;
361 DATA_BLOB blob;
362 struct ndr_pull *ndr;
364 blob.length = buf_size;
365 blob.data = talloc_steal(mem_ctx, buffer);
367 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
368 if (!services) {
369 status = NT_STATUS_NO_MEMORY;
370 break;
373 ndr = ndr_pull_init_blob(&blob, mem_ctx);
374 if (ndr == NULL) {
375 status = NT_STATUS_NO_MEMORY;
376 break;
379 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
380 ndr, num_services, services);
381 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
382 status = ndr_map_error2ntstatus(ndr_err);
383 break;
386 for ( i=0; i<num_services; i++ ) {
387 d_printf("%-20s \"%s\"\n",
388 services[i].service_name,
389 services[i].display_name);
393 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
395 done:
396 if (is_valid_policy_hnd(&hSCM)) {
397 WERROR _result;
398 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
401 return status;
404 /********************************************************************
405 ********************************************************************/
407 static NTSTATUS rpc_service_status_internal(struct net_context *c,
408 const struct dom_sid *domain_sid,
409 const char *domain_name,
410 struct cli_state *cli,
411 struct rpc_pipe_client *pipe_hnd,
412 TALLOC_CTX *mem_ctx,
413 int argc,
414 const char **argv )
416 struct policy_handle hSCM, hService;
417 WERROR result = WERR_GEN_FAILURE;
418 NTSTATUS status;
419 struct SERVICE_STATUS service_status;
420 struct QUERY_SERVICE_CONFIG config;
421 uint32_t buf_size = sizeof(config);
422 uint32_t ret_size = 0;
423 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
425 if (argc != 1 ) {
426 d_printf("%s net rpc service status <service>\n", _("Usage:"));
427 return NT_STATUS_OK;
430 /* Open the Service Control Manager */
431 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
432 SC_RIGHT_MGR_ENUMERATE_SERVICE,
433 &hSCM);
434 if (!W_ERROR_IS_OK(result)) {
435 return werror_to_ntstatus(result);
438 /* Open the Service */
440 result = open_service(b, mem_ctx, &hSCM, argv[0],
441 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
442 &hService);
443 if (!W_ERROR_IS_OK(result) ) {
444 goto done;
447 /* get the status */
449 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
450 &hService,
451 &service_status,
452 &result);
453 if (!NT_STATUS_IS_OK(status)) {
454 result = ntstatus_to_werror(status);
455 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
456 nt_errstr(status));
457 goto done;
460 if (!W_ERROR_IS_OK(result) ) {
461 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
462 win_errstr(result));
463 goto done;
466 d_printf(_("%s service is %s.\n"), argv[0],
467 svc_status_string(service_status.state));
469 /* get the config */
471 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
472 &hService,
473 &config,
474 buf_size,
475 &ret_size,
476 &result);
477 if (!NT_STATUS_IS_OK(status)) {
478 result = ntstatus_to_werror(status);
479 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
480 nt_errstr(status));
481 goto done;
484 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
485 buf_size = ret_size;
486 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
487 &hService,
488 &config,
489 buf_size,
490 &ret_size,
491 &result);
492 if (!NT_STATUS_IS_OK(status)) {
493 result = ntstatus_to_werror(status);
494 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
495 nt_errstr(status));
496 goto done;
500 if (!W_ERROR_IS_OK(result) ) {
501 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
502 win_errstr(result));
503 goto done;
506 /* print out the configuration information for the service */
508 d_printf(_("Configuration details:\n"));
509 d_printf(_("\tControls Accepted = 0x%x\n"),
510 service_status.controls_accepted);
511 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
512 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
513 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
514 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
516 if (config.executablepath) {
517 d_printf(_("\tExecutable Path = %s\n"),
518 config.executablepath);
521 if (config.loadordergroup) {
522 d_printf(_("\tLoad Order Group = %s\n"),
523 config.loadordergroup);
526 if (config.dependencies) {
527 d_printf(_("\tDependencies = %s\n"),
528 config.dependencies);
531 if (config.startname) {
532 d_printf(_("\tStart Name = %s\n"), config.startname);
535 if (config.displayname) {
536 d_printf(_("\tDisplay Name = %s\n"),
537 config.displayname);
540 done:
541 if (is_valid_policy_hnd(&hService)) {
542 WERROR _result;
543 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
545 if (is_valid_policy_hnd(&hSCM)) {
546 WERROR _result;
547 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
550 return werror_to_ntstatus(result);
553 /********************************************************************
554 ********************************************************************/
556 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
557 const struct dom_sid *domain_sid,
558 const char *domain_name,
559 struct cli_state *cli,
560 struct rpc_pipe_client *pipe_hnd,
561 TALLOC_CTX *mem_ctx,
562 int argc,
563 const char **argv )
565 struct policy_handle hSCM;
566 WERROR result = WERR_GEN_FAILURE;
567 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
569 if (argc != 1 ) {
570 d_printf("%s net rpc service status <service>\n", _("Usage:"));
571 return NT_STATUS_OK;
574 /* Open the Service Control Manager */
575 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
576 SC_RIGHT_MGR_ENUMERATE_SERVICE,
577 &hSCM);
578 if (!W_ERROR_IS_OK(result)) {
579 return werror_to_ntstatus(result);
582 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
583 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
585 if (is_valid_policy_hnd(&hSCM)) {
586 WERROR _result;
587 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
590 return werror_to_ntstatus(result);
593 /********************************************************************
594 ********************************************************************/
596 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
597 const struct dom_sid *domain_sid,
598 const char *domain_name,
599 struct cli_state *cli,
600 struct rpc_pipe_client *pipe_hnd,
601 TALLOC_CTX *mem_ctx,
602 int argc,
603 const char **argv )
605 struct policy_handle hSCM;
606 WERROR result = WERR_GEN_FAILURE;
607 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
609 if (argc != 1 ) {
610 d_printf("%s net rpc service status <service>\n", _("Usage:"));
611 return NT_STATUS_OK;
614 /* Open the Service Control Manager */
615 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
616 SC_RIGHT_MGR_ENUMERATE_SERVICE,
617 &hSCM);
618 if (!W_ERROR_IS_OK(result)) {
619 return werror_to_ntstatus(result);
622 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
623 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
625 if (is_valid_policy_hnd(&hSCM)) {
626 WERROR _result;
627 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
630 return werror_to_ntstatus(result);
633 /********************************************************************
634 ********************************************************************/
636 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
637 const struct dom_sid *domain_sid,
638 const char *domain_name,
639 struct cli_state *cli,
640 struct rpc_pipe_client *pipe_hnd,
641 TALLOC_CTX *mem_ctx,
642 int argc,
643 const char **argv )
645 struct policy_handle hSCM;
646 WERROR result = WERR_GEN_FAILURE;
647 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
649 if (argc != 1 ) {
650 d_printf("%s net rpc service status <service>\n", _("Usage:"));
651 return NT_STATUS_OK;
654 /* Open the Service Control Manager */
655 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
656 SC_RIGHT_MGR_ENUMERATE_SERVICE,
657 &hSCM);
658 if (!W_ERROR_IS_OK(result)) {
659 return werror_to_ntstatus(result);
662 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
663 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
665 if (is_valid_policy_hnd(&hSCM)) {
666 WERROR _result;
667 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
670 return werror_to_ntstatus(result);
673 /********************************************************************
674 ********************************************************************/
676 static NTSTATUS rpc_service_start_internal(struct net_context *c,
677 const struct dom_sid *domain_sid,
678 const char *domain_name,
679 struct cli_state *cli,
680 struct rpc_pipe_client *pipe_hnd,
681 TALLOC_CTX *mem_ctx,
682 int argc,
683 const char **argv )
685 struct policy_handle hSCM, hService;
686 WERROR result = WERR_GEN_FAILURE;
687 NTSTATUS status;
688 uint32_t state = 0;
689 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
691 if (argc != 1 ) {
692 d_printf("%s net rpc service status <service>\n", _("Usage:"));
693 return NT_STATUS_OK;
696 /* Open the Service Control Manager */
697 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
698 SC_RIGHT_MGR_ENUMERATE_SERVICE,
699 &hSCM);
700 if (!W_ERROR_IS_OK(result)) {
701 return werror_to_ntstatus(result);
705 /* Open the Service */
707 result = open_service(b, mem_ctx, &hSCM, argv[0],
708 SC_RIGHT_SVC_START,
709 &hService);
710 if (!W_ERROR_IS_OK(result) ) {
711 goto done;
714 /* get the status */
716 status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
717 &hService,
719 NULL,
720 &result);
722 if (!NT_STATUS_IS_OK(status)) {
723 result = ntstatus_to_werror(status);
724 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
725 nt_errstr(status));
726 goto done;
728 if (!W_ERROR_IS_OK(result) ) {
729 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
730 win_errstr(result));
731 goto done;
734 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
736 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
737 d_printf(_("Successfully started service: %s\n"),
738 argv[0] );
739 else
740 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
741 argv[0], win_errstr(result) );
743 done:
744 if (is_valid_policy_hnd(&hService)) {
745 WERROR _result;
746 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
748 if (is_valid_policy_hnd(&hSCM)) {
749 WERROR _result;
750 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
753 return werror_to_ntstatus(result);
756 /********************************************************************
757 ********************************************************************/
759 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
760 const struct dom_sid *domain_sid,
761 const char *domain_name,
762 struct cli_state *cli,
763 struct rpc_pipe_client *pipe_hnd,
764 TALLOC_CTX *mem_ctx,
765 int argc,
766 const char **argv)
768 struct policy_handle hSCM, hService;
769 WERROR result = WERR_GEN_FAILURE;
770 NTSTATUS status;
771 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
773 if (argc != 1 ) {
774 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
775 return NT_STATUS_OK;
778 ZERO_STRUCT(hSCM);
779 ZERO_STRUCT(hService);
781 /* Open the Service Control Manager */
782 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
783 SC_RIGHT_MGR_ENUMERATE_SERVICE,
784 &hSCM);
785 if (!W_ERROR_IS_OK(result)) {
786 return werror_to_ntstatus(result);
789 /* Open the Service */
791 result = open_service(b, mem_ctx, &hSCM, argv[0],
792 SERVICE_ALL_ACCESS,
793 &hService);
794 if (!W_ERROR_IS_OK(result) ) {
795 goto done;
798 /* Delete the Service */
800 status = dcerpc_svcctl_DeleteService(b, mem_ctx,
801 &hService,
802 &result);
804 if (!NT_STATUS_IS_OK(status)) {
805 result = ntstatus_to_werror(status);
806 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
807 nt_errstr(status));
808 goto done;
810 if (!W_ERROR_IS_OK(result)) {
811 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
812 win_errstr(result));
813 goto done;
816 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
818 done:
819 if (is_valid_policy_hnd(&hService)) {
820 WERROR _result;
821 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
823 if (is_valid_policy_hnd(&hSCM)) {
824 WERROR _result;
825 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
828 return werror_to_ntstatus(result);
831 /********************************************************************
832 ********************************************************************/
834 static NTSTATUS rpc_service_create_internal(struct net_context *c,
835 const struct dom_sid *domain_sid,
836 const char *domain_name,
837 struct cli_state *cli,
838 struct rpc_pipe_client *pipe_hnd,
839 TALLOC_CTX *mem_ctx,
840 int argc,
841 const char **argv)
843 struct policy_handle hSCM, hService;
844 WERROR result = WERR_GEN_FAILURE;
845 NTSTATUS status;
846 const char *ServiceName;
847 const char *DisplayName;
848 const char *binary_path;
849 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
851 if (argc != 3) {
852 d_printf("%s net rpc service create <service> "
853 "<displayname> <binarypath>\n", _("Usage:"));
854 return NT_STATUS_OK;
857 ZERO_STRUCT(hSCM);
858 ZERO_STRUCT(hService);
860 /* Open the Service Control Manager */
861 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
862 SC_RIGHT_MGR_CREATE_SERVICE,
863 &hSCM);
864 if (!W_ERROR_IS_OK(result)) {
865 return werror_to_ntstatus(result);
868 /* Create the service */
870 ServiceName = argv[0];
871 DisplayName = argv[1];
872 binary_path = argv[2];
874 status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
875 &hSCM,
876 ServiceName,
877 DisplayName,
878 SERVICE_ALL_ACCESS,
879 SERVICE_TYPE_WIN32_OWN_PROCESS,
880 SVCCTL_DEMAND_START,
881 SVCCTL_SVC_ERROR_NORMAL,
882 binary_path,
883 NULL, /* LoadOrderGroupKey */
884 NULL, /* TagId */
885 NULL, /* dependencies */
886 0, /* dependencies_size */
887 NULL, /* service_start_name */
888 NULL, /* password */
889 0, /* password_size */
890 &hService,
891 &result);
892 if (!NT_STATUS_IS_OK(status)) {
893 result = ntstatus_to_werror(status);
894 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
895 nt_errstr(status));
896 goto done;
898 if (!W_ERROR_IS_OK(result)) {
899 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
900 win_errstr(result));
901 goto done;
904 d_printf(_("Successfully created Service: %s\n"), argv[0]);
906 done:
907 if (is_valid_policy_hnd(&hService)) {
908 WERROR _result;
909 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
911 if (is_valid_policy_hnd(&hSCM)) {
912 WERROR _result;
913 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
916 return werror_to_ntstatus(result);
919 /********************************************************************
920 ********************************************************************/
922 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
924 if (c->display_usage) {
925 d_printf( "%s\n"
926 "net rpc service list\n"
927 " %s\n",
928 _("Usage:"),
929 _("View configured Win32 services"));
930 return 0;
933 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
934 rpc_service_list_internal, argc, argv );
937 /********************************************************************
938 ********************************************************************/
940 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
942 if (c->display_usage) {
943 d_printf( "%s\n"
944 "net rpc service start <service>\n"
945 " %s\n",
946 _("Usage:"),
947 _("Start a Win32 service"));
948 return 0;
951 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
952 rpc_service_start_internal, argc, argv );
955 /********************************************************************
956 ********************************************************************/
958 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
960 if (c->display_usage) {
961 d_printf( "%s\n"
962 "net rpc service stop <service>\n"
963 " %s\n",
964 _("Usage:"),
965 _("Stop a Win32 service"));
966 return 0;
969 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
970 rpc_service_stop_internal, argc, argv );
973 /********************************************************************
974 ********************************************************************/
976 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
978 if (c->display_usage) {
979 d_printf( "%s\n"
980 "net rpc service resume <service>\n"
981 " %s\n",
982 _("Usage:"),
983 _("Resume a Win32 service"));
984 return 0;
987 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
988 rpc_service_resume_internal, argc, argv );
991 /********************************************************************
992 ********************************************************************/
994 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
996 if (c->display_usage) {
997 d_printf( "%s\n"
998 "net rpc service pause <service>\n"
999 " %s\n",
1000 _("Usage:"),
1001 _("Pause a Win32 service"));
1002 return 0;
1005 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1006 rpc_service_pause_internal, argc, argv );
1009 /********************************************************************
1010 ********************************************************************/
1012 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
1014 if (c->display_usage) {
1015 d_printf( "%s\n"
1016 "net rpc service status <service>\n"
1017 " %s\n",
1018 _("Usage:"),
1019 _("Show the current status of a service"));
1020 return 0;
1023 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1024 rpc_service_status_internal, argc, argv );
1027 /********************************************************************
1028 ********************************************************************/
1030 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
1032 if (c->display_usage) {
1033 d_printf( "%s\n"
1034 "net rpc service delete <service>\n"
1035 " %s\n",
1036 _("Usage:"),
1037 _("Delete a Win32 service"));
1038 return 0;
1041 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1042 rpc_service_delete_internal, argc, argv);
1045 /********************************************************************
1046 ********************************************************************/
1048 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
1050 if (c->display_usage) {
1051 d_printf( "%s\n"
1052 "net rpc service create <service>\n"
1053 " %s\n",
1054 _("Usage:"),
1055 _("Create a Win32 service"));
1056 return 0;
1059 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1060 rpc_service_create_internal, argc, argv);
1063 /********************************************************************
1064 ********************************************************************/
1066 int net_rpc_service(struct net_context *c, int argc, const char **argv)
1068 struct functable func[] = {
1070 "list",
1071 rpc_service_list,
1072 NET_TRANSPORT_RPC,
1073 N_("View configured Win32 services"),
1074 N_("net rpc service list\n"
1075 " View configured Win32 services")
1078 "start",
1079 rpc_service_start,
1080 NET_TRANSPORT_RPC,
1081 N_("Start a service"),
1082 N_("net rpc service start\n"
1083 " Start a service")
1086 "stop",
1087 rpc_service_stop,
1088 NET_TRANSPORT_RPC,
1089 N_("Stop a service"),
1090 N_("net rpc service stop\n"
1091 " Stop a service")
1094 "pause",
1095 rpc_service_pause,
1096 NET_TRANSPORT_RPC,
1097 N_("Pause a service"),
1098 N_("net rpc service pause\n"
1099 " Pause a service")
1102 "resume",
1103 rpc_service_resume,
1104 NET_TRANSPORT_RPC,
1105 N_("Resume a paused service"),
1106 N_("net rpc service resume\n"
1107 " Resume a service")
1110 "status",
1111 rpc_service_status,
1112 NET_TRANSPORT_RPC,
1113 N_("View current status of a service"),
1114 N_("net rpc service status\n"
1115 " View current status of a service")
1118 "delete",
1119 rpc_service_delete,
1120 NET_TRANSPORT_RPC,
1121 N_("Delete a service"),
1122 N_("net rpc service delete\n"
1123 " Deletes a service")
1126 "create",
1127 rpc_service_create,
1128 NET_TRANSPORT_RPC,
1129 N_("Create a service"),
1130 N_("net rpc service create\n"
1131 " Creates a service")
1134 {NULL, NULL, 0, NULL, NULL}
1137 return net_run_function(c, argc, argv, "net rpc service",func);