tdb: Fix Coverity ID 2192: NO_EFFECT
[Samba.git] / source3 / utils / net_rpc_service.c
blobf1cd2a6b0d91e4313ec65421cad2b7550b3461b2
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 "../librpc/gen_ndr/ndr_svcctl.h"
22 #include "../librpc/gen_ndr/ndr_svcctl_c.h"
24 struct svc_state_msg {
25 uint32 flag;
26 const char *message;
29 static struct svc_state_msg state_msg_table[] = {
30 { SVCCTL_STOPPED, N_("stopped") },
31 { SVCCTL_START_PENDING, N_("start pending") },
32 { SVCCTL_STOP_PENDING, N_("stop pending") },
33 { SVCCTL_RUNNING, N_("running") },
34 { SVCCTL_CONTINUE_PENDING, N_("resume pending") },
35 { SVCCTL_PAUSE_PENDING, N_("pause pending") },
36 { SVCCTL_PAUSED, N_("paused") },
37 { 0, NULL }
41 /********************************************************************
42 ********************************************************************/
43 const char *svc_status_string( uint32 state )
45 fstring msg;
46 int i;
48 fstr_sprintf( msg, _("Unknown State [%d]"), state );
50 for ( i=0; state_msg_table[i].message; i++ ) {
51 if ( state_msg_table[i].flag == state ) {
52 fstrcpy( msg, state_msg_table[i].message );
53 break;
57 return talloc_strdup(talloc_tos(), msg);
60 /********************************************************************
61 ********************************************************************/
63 static WERROR open_service(struct dcerpc_binding_handle *b,
64 TALLOC_CTX *mem_ctx,
65 struct policy_handle *hSCM,
66 const char *service,
67 uint32_t access_mask,
68 struct policy_handle *hService)
70 NTSTATUS status;
71 WERROR result;
73 status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
74 hSCM,
75 service,
76 access_mask,
77 hService,
78 &result);
79 if (!NT_STATUS_IS_OK(status)) {
80 result = ntstatus_to_werror(status);
81 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
82 nt_errstr(status));
83 return result;
85 if (!W_ERROR_IS_OK(result) ) {
86 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
87 win_errstr(result));
88 return result;
91 return WERR_OK;
94 /********************************************************************
95 ********************************************************************/
97 static WERROR open_scm(struct dcerpc_binding_handle *b,
98 TALLOC_CTX *mem_ctx,
99 const char *server_name,
100 uint32_t access_mask,
101 struct policy_handle *hSCM)
103 NTSTATUS status;
104 WERROR result;
106 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
107 server_name,
108 NULL,
109 access_mask,
110 hSCM,
111 &result);
112 if (!NT_STATUS_IS_OK(status)) {
113 result = ntstatus_to_werror(status);
114 d_fprintf(stderr,
115 _("Failed to open Service Control Manager. [%s]\n"),
116 nt_errstr(status));
117 return result;
119 if (!W_ERROR_IS_OK(result)) {
120 d_fprintf(stderr,
121 _("Failed to open Service Control Manager. [%s]\n"),
122 win_errstr(result));
123 return result;
126 return WERR_OK;
129 /********************************************************************
130 ********************************************************************/
132 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
133 TALLOC_CTX *mem_ctx,
134 struct policy_handle *hSCM,
135 const char *service,
136 uint32 *state )
138 struct policy_handle hService;
139 struct SERVICE_STATUS service_status;
140 WERROR result = WERR_GENERAL_FAILURE;
141 NTSTATUS status;
142 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
144 /* now cycle until the status is actually 'watch_state' */
146 result = open_service(b, mem_ctx, hSCM, service,
147 SC_RIGHT_SVC_QUERY_STATUS,
148 &hService);
149 if (!W_ERROR_IS_OK(result) ) {
150 return result;
153 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
154 &hService,
155 &service_status,
156 &result);
157 if (!NT_STATUS_IS_OK(status)) {
158 result = ntstatus_to_werror(status);
159 goto done;
161 if (!W_ERROR_IS_OK(result)) {
162 goto done;
165 *state = service_status.state;
167 done:
168 if (is_valid_policy_hnd(&hService)) {
169 WERROR _result;
170 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
173 return result;
176 /********************************************************************
177 ********************************************************************/
179 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
180 TALLOC_CTX *mem_ctx,
181 struct policy_handle *hSCM,
182 const char *service,
183 uint32 watch_state,
184 uint32 *final_state )
186 uint32 i;
187 uint32 state = 0;
188 WERROR result = WERR_GENERAL_FAILURE;
191 i = 0;
192 while ( (state != watch_state ) && i<30 ) {
193 /* get the status */
195 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
196 if ( !W_ERROR_IS_OK(result) ) {
197 break;
200 d_printf(".");
201 i++;
202 sys_usleep( 100 );
204 d_printf("\n");
206 *final_state = state;
208 return result;
211 /********************************************************************
212 ********************************************************************/
214 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
215 TALLOC_CTX *mem_ctx,
216 struct policy_handle *hSCM,
217 const char *service,
218 uint32 control,
219 uint32 watch_state )
221 struct policy_handle hService;
222 WERROR result = WERR_GENERAL_FAILURE;
223 NTSTATUS status;
224 struct SERVICE_STATUS service_status;
225 uint32 state = 0;
226 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
228 /* Open the Service */
230 result = open_service(b, mem_ctx, hSCM, service,
231 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
232 &hService);
233 if (!W_ERROR_IS_OK(result) ) {
234 return result;
237 /* get the status */
239 status = dcerpc_svcctl_ControlService(b, mem_ctx,
240 &hService,
241 control,
242 &service_status,
243 &result);
245 if (!NT_STATUS_IS_OK(status)) {
246 result = ntstatus_to_werror(status);
247 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
248 nt_errstr(status));
249 goto done;
251 if (!W_ERROR_IS_OK(result) ) {
252 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
253 win_errstr(result));
254 goto done;
257 /* loop -- checking the state until we are where we want to be */
259 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
261 d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
263 done:
264 if (is_valid_policy_hnd(&hService)) {
265 WERROR _result;
266 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
269 return result;
272 /********************************************************************
273 ********************************************************************/
275 static NTSTATUS rpc_service_list_internal(struct net_context *c,
276 const struct dom_sid *domain_sid,
277 const char *domain_name,
278 struct cli_state *cli,
279 struct rpc_pipe_client *pipe_hnd,
280 TALLOC_CTX *mem_ctx,
281 int argc,
282 const char **argv )
284 struct policy_handle hSCM;
285 struct ENUM_SERVICE_STATUSW *services = NULL;
286 WERROR result = WERR_GENERAL_FAILURE;
287 NTSTATUS status;
288 int i;
289 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
291 uint8_t *buffer = NULL;
292 uint32_t buf_size = 0;
293 uint32_t bytes_needed = 0;
294 uint32_t num_services = 0;
295 uint32_t resume_handle = 0;
297 if (argc != 0 ) {
298 d_printf("%s net rpc service list\n", _("Usage:"));
299 return NT_STATUS_OK;
302 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
303 SC_RIGHT_MGR_ENUMERATE_SERVICE,
304 &hSCM);
305 if (!W_ERROR_IS_OK(result)) {
306 return werror_to_ntstatus(result);
309 do {
310 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
311 &hSCM,
312 SERVICE_TYPE_WIN32,
313 SERVICE_STATE_ALL,
314 buffer,
315 buf_size,
316 &bytes_needed,
317 &num_services,
318 &resume_handle,
319 &result);
321 if (!NT_STATUS_IS_OK(status)) {
322 d_fprintf(stderr,
323 _("Failed to enumerate services. [%s]\n"),
324 nt_errstr(status));
325 break;
328 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
329 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
330 buf_size = bytes_needed;
331 continue;
334 if (!W_ERROR_IS_OK(result)) {
335 status = werror_to_ntstatus(result);
336 d_fprintf(stderr,
337 _("Failed to enumerate services. [%s]\n"),
338 win_errstr(result));
339 break;
342 if ( num_services == 0 ) {
343 d_printf(_("No services returned\n"));
344 break;
348 enum ndr_err_code ndr_err;
349 DATA_BLOB blob;
350 struct ndr_pull *ndr;
352 blob.length = buf_size;
353 blob.data = talloc_steal(mem_ctx, buffer);
355 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
356 if (!services) {
357 status = NT_STATUS_NO_MEMORY;
358 break;
361 ndr = ndr_pull_init_blob(&blob, mem_ctx);
362 if (ndr == NULL) {
363 status = NT_STATUS_NO_MEMORY;
364 break;
367 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
368 ndr, num_services, services);
369 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
370 status = ndr_map_error2ntstatus(ndr_err);
371 break;
374 for ( i=0; i<num_services; i++ ) {
375 d_printf("%-20s \"%s\"\n",
376 services[i].service_name,
377 services[i].display_name);
381 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
383 if (is_valid_policy_hnd(&hSCM)) {
384 WERROR _result;
385 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
388 return status;
391 /********************************************************************
392 ********************************************************************/
394 static NTSTATUS rpc_service_status_internal(struct net_context *c,
395 const struct dom_sid *domain_sid,
396 const char *domain_name,
397 struct cli_state *cli,
398 struct rpc_pipe_client *pipe_hnd,
399 TALLOC_CTX *mem_ctx,
400 int argc,
401 const char **argv )
403 struct policy_handle hSCM, hService;
404 WERROR result = WERR_GENERAL_FAILURE;
405 NTSTATUS status;
406 struct SERVICE_STATUS service_status;
407 struct QUERY_SERVICE_CONFIG config;
408 uint32_t buf_size = sizeof(config);
409 uint32_t ret_size = 0;
410 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
412 if (argc != 1 ) {
413 d_printf("%s net rpc service status <service>\n", _("Usage:"));
414 return NT_STATUS_OK;
417 /* Open the Service Control Manager */
418 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
419 SC_RIGHT_MGR_ENUMERATE_SERVICE,
420 &hSCM);
421 if (!W_ERROR_IS_OK(result)) {
422 return werror_to_ntstatus(result);
425 /* Open the Service */
427 result = open_service(b, mem_ctx, &hSCM, argv[0],
428 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
429 &hService);
430 if (!W_ERROR_IS_OK(result) ) {
431 goto done;
434 /* get the status */
436 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
437 &hService,
438 &service_status,
439 &result);
440 if (!NT_STATUS_IS_OK(status)) {
441 result = ntstatus_to_werror(status);
442 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
443 nt_errstr(status));
444 goto done;
447 if (!W_ERROR_IS_OK(result) ) {
448 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
449 win_errstr(result));
450 goto done;
453 d_printf(_("%s service is %s.\n"), argv[0],
454 svc_status_string(service_status.state));
456 /* get the config */
458 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
459 &hService,
460 &config,
461 buf_size,
462 &ret_size,
463 &result);
464 if (!NT_STATUS_IS_OK(status)) {
465 result = ntstatus_to_werror(status);
466 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
467 nt_errstr(status));
468 goto done;
471 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
472 buf_size = ret_size;
473 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
474 &hService,
475 &config,
476 buf_size,
477 &ret_size,
478 &result);
479 if (!NT_STATUS_IS_OK(status)) {
480 result = ntstatus_to_werror(status);
481 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
482 nt_errstr(status));
483 goto done;
487 if (!W_ERROR_IS_OK(result) ) {
488 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
489 win_errstr(result));
490 goto done;
493 /* print out the configuration information for the service */
495 d_printf(_("Configuration details:\n"));
496 d_printf(_("\tControls Accepted = 0x%x\n"),
497 service_status.controls_accepted);
498 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
499 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
500 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
501 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
503 if (config.executablepath) {
504 d_printf(_("\tExecutable Path = %s\n"),
505 config.executablepath);
508 if (config.loadordergroup) {
509 d_printf(_("\tLoad Order Group = %s\n"),
510 config.loadordergroup);
513 if (config.dependencies) {
514 d_printf(_("\tDependencies = %s\n"),
515 config.dependencies);
518 if (config.startname) {
519 d_printf(_("\tStart Name = %s\n"), config.startname);
522 if (config.displayname) {
523 d_printf(_("\tDisplay Name = %s\n"),
524 config.displayname);
527 done:
528 if (is_valid_policy_hnd(&hService)) {
529 WERROR _result;
530 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
532 if (is_valid_policy_hnd(&hSCM)) {
533 WERROR _result;
534 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
537 return werror_to_ntstatus(result);
540 /********************************************************************
541 ********************************************************************/
543 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
544 const struct dom_sid *domain_sid,
545 const char *domain_name,
546 struct cli_state *cli,
547 struct rpc_pipe_client *pipe_hnd,
548 TALLOC_CTX *mem_ctx,
549 int argc,
550 const char **argv )
552 struct policy_handle hSCM;
553 WERROR result = WERR_GENERAL_FAILURE;
554 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
556 if (argc != 1 ) {
557 d_printf("%s net rpc service status <service>\n", _("Usage:"));
558 return NT_STATUS_OK;
561 /* Open the Service Control Manager */
562 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
563 SC_RIGHT_MGR_ENUMERATE_SERVICE,
564 &hSCM);
565 if (!W_ERROR_IS_OK(result)) {
566 return werror_to_ntstatus(result);
569 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
570 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
572 if (is_valid_policy_hnd(&hSCM)) {
573 WERROR _result;
574 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
577 return werror_to_ntstatus(result);
580 /********************************************************************
581 ********************************************************************/
583 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
584 const struct dom_sid *domain_sid,
585 const char *domain_name,
586 struct cli_state *cli,
587 struct rpc_pipe_client *pipe_hnd,
588 TALLOC_CTX *mem_ctx,
589 int argc,
590 const char **argv )
592 struct policy_handle hSCM;
593 WERROR result = WERR_GENERAL_FAILURE;
594 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
596 if (argc != 1 ) {
597 d_printf("%s net rpc service status <service>\n", _("Usage:"));
598 return NT_STATUS_OK;
601 /* Open the Service Control Manager */
602 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
603 SC_RIGHT_MGR_ENUMERATE_SERVICE,
604 &hSCM);
605 if (!W_ERROR_IS_OK(result)) {
606 return werror_to_ntstatus(result);
609 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
610 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
612 if (is_valid_policy_hnd(&hSCM)) {
613 WERROR _result;
614 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
617 return werror_to_ntstatus(result);
620 /********************************************************************
621 ********************************************************************/
623 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
624 const struct dom_sid *domain_sid,
625 const char *domain_name,
626 struct cli_state *cli,
627 struct rpc_pipe_client *pipe_hnd,
628 TALLOC_CTX *mem_ctx,
629 int argc,
630 const char **argv )
632 struct policy_handle hSCM;
633 WERROR result = WERR_GENERAL_FAILURE;
634 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
636 if (argc != 1 ) {
637 d_printf("%s net rpc service status <service>\n", _("Usage:"));
638 return NT_STATUS_OK;
641 /* Open the Service Control Manager */
642 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
643 SC_RIGHT_MGR_ENUMERATE_SERVICE,
644 &hSCM);
645 if (!W_ERROR_IS_OK(result)) {
646 return werror_to_ntstatus(result);
649 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
650 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
652 if (is_valid_policy_hnd(&hSCM)) {
653 WERROR _result;
654 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
657 return werror_to_ntstatus(result);
660 /********************************************************************
661 ********************************************************************/
663 static NTSTATUS rpc_service_start_internal(struct net_context *c,
664 const struct dom_sid *domain_sid,
665 const char *domain_name,
666 struct cli_state *cli,
667 struct rpc_pipe_client *pipe_hnd,
668 TALLOC_CTX *mem_ctx,
669 int argc,
670 const char **argv )
672 struct policy_handle hSCM, hService;
673 WERROR result = WERR_GENERAL_FAILURE;
674 NTSTATUS status;
675 uint32 state = 0;
676 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
678 if (argc != 1 ) {
679 d_printf("%s net rpc service status <service>\n", _("Usage:"));
680 return NT_STATUS_OK;
683 /* Open the Service Control Manager */
684 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
685 SC_RIGHT_MGR_ENUMERATE_SERVICE,
686 &hSCM);
687 if (!W_ERROR_IS_OK(result)) {
688 return werror_to_ntstatus(result);
692 /* Open the Service */
694 result = open_service(b, mem_ctx, &hSCM, argv[0],
695 SC_RIGHT_SVC_START,
696 &hService);
697 if (!W_ERROR_IS_OK(result) ) {
698 goto done;
701 /* get the status */
703 status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
704 &hService,
706 NULL,
707 &result);
709 if (!NT_STATUS_IS_OK(status)) {
710 result = ntstatus_to_werror(status);
711 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
712 nt_errstr(status));
713 goto done;
715 if (!W_ERROR_IS_OK(result) ) {
716 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
717 win_errstr(result));
718 goto done;
721 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
723 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
724 d_printf(_("Successfully started service: %s\n"),
725 argv[0] );
726 else
727 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
728 argv[0], win_errstr(result) );
730 done:
731 if (is_valid_policy_hnd(&hService)) {
732 WERROR _result;
733 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
735 if (is_valid_policy_hnd(&hSCM)) {
736 WERROR _result;
737 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
740 return werror_to_ntstatus(result);
743 /********************************************************************
744 ********************************************************************/
746 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
747 const struct dom_sid *domain_sid,
748 const char *domain_name,
749 struct cli_state *cli,
750 struct rpc_pipe_client *pipe_hnd,
751 TALLOC_CTX *mem_ctx,
752 int argc,
753 const char **argv)
755 struct policy_handle hSCM, hService;
756 WERROR result = WERR_GENERAL_FAILURE;
757 NTSTATUS status;
758 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
760 if (argc != 1 ) {
761 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
762 return NT_STATUS_OK;
765 ZERO_STRUCT(hSCM);
766 ZERO_STRUCT(hService);
768 /* Open the Service Control Manager */
769 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
770 SC_RIGHT_MGR_ENUMERATE_SERVICE,
771 &hSCM);
772 if (!W_ERROR_IS_OK(result)) {
773 return werror_to_ntstatus(result);
776 /* Open the Service */
778 result = open_service(b, mem_ctx, &hSCM, argv[0],
779 SERVICE_ALL_ACCESS,
780 &hService);
781 if (!W_ERROR_IS_OK(result) ) {
782 goto done;
785 /* Delete the Service */
787 status = dcerpc_svcctl_DeleteService(b, mem_ctx,
788 &hService,
789 &result);
791 if (!NT_STATUS_IS_OK(status)) {
792 result = ntstatus_to_werror(status);
793 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
794 nt_errstr(status));
795 goto done;
797 if (!W_ERROR_IS_OK(result)) {
798 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
799 win_errstr(result));
800 goto done;
803 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
805 done:
806 if (is_valid_policy_hnd(&hService)) {
807 WERROR _result;
808 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
810 if (is_valid_policy_hnd(&hSCM)) {
811 WERROR _result;
812 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
815 return werror_to_ntstatus(result);
818 /********************************************************************
819 ********************************************************************/
821 static NTSTATUS rpc_service_create_internal(struct net_context *c,
822 const struct dom_sid *domain_sid,
823 const char *domain_name,
824 struct cli_state *cli,
825 struct rpc_pipe_client *pipe_hnd,
826 TALLOC_CTX *mem_ctx,
827 int argc,
828 const char **argv)
830 struct policy_handle hSCM, hService;
831 WERROR result = WERR_GENERAL_FAILURE;
832 NTSTATUS status;
833 const char *ServiceName;
834 const char *DisplayName;
835 const char *binary_path;
836 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
838 if (argc != 3) {
839 d_printf("%s net rpc service create <service> "
840 "<displayname> <binarypath>\n", _("Usage:"));
841 return NT_STATUS_OK;
844 ZERO_STRUCT(hSCM);
845 ZERO_STRUCT(hService);
847 /* Open the Service Control Manager */
848 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
849 SC_RIGHT_MGR_CREATE_SERVICE,
850 &hSCM);
851 if (!W_ERROR_IS_OK(result)) {
852 return werror_to_ntstatus(result);
855 /* Create the service */
857 ServiceName = argv[0];
858 DisplayName = argv[1];
859 binary_path = argv[2];
861 status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
862 &hSCM,
863 ServiceName,
864 DisplayName,
865 SERVICE_ALL_ACCESS,
866 SERVICE_TYPE_WIN32_OWN_PROCESS,
867 SVCCTL_DEMAND_START,
868 SVCCTL_SVC_ERROR_NORMAL,
869 binary_path,
870 NULL, /* LoadOrderGroupKey */
871 NULL, /* TagId */
872 NULL, /* dependencies */
873 0, /* dependencies_size */
874 NULL, /* service_start_name */
875 NULL, /* password */
876 0, /* password_size */
877 &hService,
878 &result);
879 if (!NT_STATUS_IS_OK(status)) {
880 result = ntstatus_to_werror(status);
881 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
882 nt_errstr(status));
883 goto done;
885 if (!W_ERROR_IS_OK(result)) {
886 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
887 win_errstr(result));
888 goto done;
891 d_printf(_("Successfully created Service: %s\n"), argv[0]);
893 done:
894 if (is_valid_policy_hnd(&hService)) {
895 WERROR _result;
896 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
898 if (is_valid_policy_hnd(&hSCM)) {
899 WERROR _result;
900 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
903 return werror_to_ntstatus(result);
906 /********************************************************************
907 ********************************************************************/
909 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
911 if (c->display_usage) {
912 d_printf( "%s\n"
913 "net rpc service list\n"
914 " %s\n",
915 _("Usage:"),
916 _("View configured Win32 services"));
917 return 0;
920 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
921 rpc_service_list_internal, argc, argv );
924 /********************************************************************
925 ********************************************************************/
927 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
929 if (c->display_usage) {
930 d_printf( "%s\n"
931 "net rpc service start <service>\n"
932 " %s\n",
933 _("Usage:"),
934 _("Start a Win32 service"));
935 return 0;
938 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
939 rpc_service_start_internal, argc, argv );
942 /********************************************************************
943 ********************************************************************/
945 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
947 if (c->display_usage) {
948 d_printf( "%s\n"
949 "net rpc service stop <service>\n"
950 " %s\n",
951 _("Usage:"),
952 _("Stop a Win32 service"));
953 return 0;
956 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
957 rpc_service_stop_internal, argc, argv );
960 /********************************************************************
961 ********************************************************************/
963 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
965 if (c->display_usage) {
966 d_printf( "%s\n"
967 "net rpc service resume <service>\n"
968 " %s\n",
969 _("Usage:"),
970 _("Resume a Win32 service"));
971 return 0;
974 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
975 rpc_service_resume_internal, argc, argv );
978 /********************************************************************
979 ********************************************************************/
981 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
983 if (c->display_usage) {
984 d_printf( "%s\n"
985 "net rpc service pause <service>\n"
986 " %s\n",
987 _("Usage:"),
988 _("Pause a Win32 service"));
989 return 0;
992 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
993 rpc_service_pause_internal, argc, argv );
996 /********************************************************************
997 ********************************************************************/
999 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
1001 if (c->display_usage) {
1002 d_printf( "%s\n"
1003 "net rpc service status <service>\n"
1004 " %s\n",
1005 _("Usage:"),
1006 _("Show the current status of a service"));
1007 return 0;
1010 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1011 rpc_service_status_internal, argc, argv );
1014 /********************************************************************
1015 ********************************************************************/
1017 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
1019 if (c->display_usage) {
1020 d_printf( "%s\n"
1021 "net rpc service delete <service>\n"
1022 " %s\n",
1023 _("Usage:"),
1024 _("Delete a Win32 service"));
1025 return 0;
1028 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1029 rpc_service_delete_internal, argc, argv);
1032 /********************************************************************
1033 ********************************************************************/
1035 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
1037 if (c->display_usage) {
1038 d_printf( "%s\n"
1039 "net rpc service create <service>\n"
1040 " %s\n",
1041 _("Usage:"),
1042 _("Create a Win32 service"));
1043 return 0;
1046 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1047 rpc_service_create_internal, argc, argv);
1050 /********************************************************************
1051 ********************************************************************/
1053 int net_rpc_service(struct net_context *c, int argc, const char **argv)
1055 struct functable func[] = {
1057 "list",
1058 rpc_service_list,
1059 NET_TRANSPORT_RPC,
1060 N_("View configured Win32 services"),
1061 N_("net rpc service list\n"
1062 " View configured Win32 services")
1065 "start",
1066 rpc_service_start,
1067 NET_TRANSPORT_RPC,
1068 N_("Start a service"),
1069 N_("net rpc service start\n"
1070 " Start a service")
1073 "stop",
1074 rpc_service_stop,
1075 NET_TRANSPORT_RPC,
1076 N_("Stop a service"),
1077 N_("net rpc service stop\n"
1078 " Stop a service")
1081 "pause",
1082 rpc_service_pause,
1083 NET_TRANSPORT_RPC,
1084 N_("Pause a service"),
1085 N_("net rpc service pause\n"
1086 " Pause a service")
1089 "resume",
1090 rpc_service_resume,
1091 NET_TRANSPORT_RPC,
1092 N_("Resume a paused service"),
1093 N_("net rpc service resume\n"
1094 " Resume a service")
1097 "status",
1098 rpc_service_status,
1099 NET_TRANSPORT_RPC,
1100 N_("View current status of a service"),
1101 N_("net rpc service status\n"
1102 " View current status of a service")
1105 "delete",
1106 rpc_service_delete,
1107 NET_TRANSPORT_RPC,
1108 N_("Delete a service"),
1109 N_("net rpc service delete\n"
1110 " Deletes a service")
1113 "create",
1114 rpc_service_create,
1115 NET_TRANSPORT_RPC,
1116 N_("Create a service"),
1117 N_("net rpc service create\n"
1118 " Creates a service")
1121 {NULL, NULL, 0, NULL, NULL}
1124 return net_run_function(c, argc, argv, "net rpc service",func);