s3-net: restructure "net rpc service" and add open_service().
[Samba.git] / source3 / utils / net_rpc_service.c
blob22394ca3a06d4cd6b460c17635b5783b73cc006f
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 query_service_state(struct rpc_pipe_client *pipe_hnd,
98 TALLOC_CTX *mem_ctx,
99 struct policy_handle *hSCM,
100 const char *service,
101 uint32 *state )
103 struct policy_handle hService;
104 struct SERVICE_STATUS service_status;
105 WERROR result = WERR_GENERAL_FAILURE;
106 NTSTATUS status;
107 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
109 /* now cycle until the status is actually 'watch_state' */
111 result = open_service(b, mem_ctx, hSCM, service,
112 SC_RIGHT_SVC_QUERY_STATUS,
113 &hService);
114 if (!W_ERROR_IS_OK(result) ) {
115 return result;
118 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
119 &hService,
120 &service_status,
121 &result);
122 if (!NT_STATUS_IS_OK(status)) {
123 result = ntstatus_to_werror(status);
124 goto done;
126 if (!W_ERROR_IS_OK(result)) {
127 goto done;
130 *state = service_status.state;
132 done:
133 if (is_valid_policy_hnd(&hService)) {
134 WERROR _result;
135 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
138 return result;
141 /********************************************************************
142 ********************************************************************/
144 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
145 TALLOC_CTX *mem_ctx,
146 struct policy_handle *hSCM,
147 const char *service,
148 uint32 watch_state,
149 uint32 *final_state )
151 uint32 i;
152 uint32 state = 0;
153 WERROR result = WERR_GENERAL_FAILURE;
156 i = 0;
157 while ( (state != watch_state ) && i<30 ) {
158 /* get the status */
160 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
161 if ( !W_ERROR_IS_OK(result) ) {
162 break;
165 d_printf(".");
166 i++;
167 sys_usleep( 100 );
169 d_printf("\n");
171 *final_state = state;
173 return result;
176 /********************************************************************
177 ********************************************************************/
179 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
180 TALLOC_CTX *mem_ctx,
181 struct policy_handle *hSCM,
182 const char *service,
183 uint32 control,
184 uint32 watch_state )
186 struct policy_handle hService;
187 WERROR result = WERR_GENERAL_FAILURE;
188 NTSTATUS status;
189 struct SERVICE_STATUS service_status;
190 uint32 state = 0;
191 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
193 /* Open the Service */
195 result = open_service(b, mem_ctx, hSCM, service,
196 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
197 &hService);
198 if (!W_ERROR_IS_OK(result) ) {
199 return result;
202 /* get the status */
204 status = dcerpc_svcctl_ControlService(b, mem_ctx,
205 &hService,
206 control,
207 &service_status,
208 &result);
210 if (!NT_STATUS_IS_OK(status)) {
211 result = ntstatus_to_werror(status);
212 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
213 nt_errstr(status));
214 goto done;
216 if (!W_ERROR_IS_OK(result) ) {
217 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
218 win_errstr(result));
219 goto done;
222 /* loop -- checking the state until we are where we want to be */
224 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
226 d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
228 done:
229 if (is_valid_policy_hnd(&hService)) {
230 WERROR _result;
231 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
234 return result;
237 /********************************************************************
238 ********************************************************************/
240 static NTSTATUS rpc_service_list_internal(struct net_context *c,
241 const struct dom_sid *domain_sid,
242 const char *domain_name,
243 struct cli_state *cli,
244 struct rpc_pipe_client *pipe_hnd,
245 TALLOC_CTX *mem_ctx,
246 int argc,
247 const char **argv )
249 struct policy_handle hSCM;
250 struct ENUM_SERVICE_STATUSW *services = NULL;
251 WERROR result = WERR_GENERAL_FAILURE;
252 NTSTATUS status;
253 int i;
254 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
256 uint8_t *buffer = NULL;
257 uint32_t buf_size = 0;
258 uint32_t bytes_needed = 0;
259 uint32_t num_services = 0;
260 uint32_t resume_handle = 0;
262 if (argc != 0 ) {
263 d_printf("%s net rpc service list\n", _("Usage:"));
264 return NT_STATUS_OK;
267 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
268 pipe_hnd->srv_name_slash,
269 NULL,
270 SC_RIGHT_MGR_ENUMERATE_SERVICE,
271 &hSCM,
272 &result);
273 if (!NT_STATUS_IS_OK(status)) {
274 d_fprintf(stderr,
275 _("Failed to open Service Control Manager. [%s]\n"),
276 nt_errstr(status));
277 return status;
279 if (!W_ERROR_IS_OK(result)) {
280 d_fprintf(stderr,
281 _("Failed to open Service Control Manager. [%s]\n"),
282 win_errstr(result));
283 return werror_to_ntstatus(result);
286 do {
287 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
288 &hSCM,
289 SERVICE_TYPE_WIN32,
290 SERVICE_STATE_ALL,
291 buffer,
292 buf_size,
293 &bytes_needed,
294 &num_services,
295 &resume_handle,
296 &result);
298 if (!NT_STATUS_IS_OK(status)) {
299 d_fprintf(stderr,
300 _("Failed to enumerate services. [%s]\n"),
301 nt_errstr(status));
302 break;
305 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
306 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
307 buf_size = bytes_needed;
308 continue;
311 if (!W_ERROR_IS_OK(result)) {
312 status = werror_to_ntstatus(result);
313 d_fprintf(stderr,
314 _("Failed to enumerate services. [%s]\n"),
315 win_errstr(result));
316 break;
319 if ( num_services == 0 ) {
320 d_printf(_("No services returned\n"));
321 break;
325 enum ndr_err_code ndr_err;
326 DATA_BLOB blob;
327 struct ndr_pull *ndr;
329 blob.length = buf_size;
330 blob.data = talloc_steal(mem_ctx, buffer);
332 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
333 if (!services) {
334 status = NT_STATUS_NO_MEMORY;
335 break;
338 ndr = ndr_pull_init_blob(&blob, mem_ctx);
339 if (ndr == NULL) {
340 status = NT_STATUS_NO_MEMORY;
341 break;
344 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
345 ndr, num_services, services);
346 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
347 status = ndr_map_error2ntstatus(ndr_err);
348 break;
351 for ( i=0; i<num_services; i++ ) {
352 d_printf("%-20s \"%s\"\n",
353 services[i].service_name,
354 services[i].display_name);
358 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
360 if (is_valid_policy_hnd(&hSCM)) {
361 WERROR _result;
362 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
365 return status;
368 /********************************************************************
369 ********************************************************************/
371 static NTSTATUS rpc_service_status_internal(struct net_context *c,
372 const struct dom_sid *domain_sid,
373 const char *domain_name,
374 struct cli_state *cli,
375 struct rpc_pipe_client *pipe_hnd,
376 TALLOC_CTX *mem_ctx,
377 int argc,
378 const char **argv )
380 struct policy_handle hSCM, hService;
381 WERROR result = WERR_GENERAL_FAILURE;
382 NTSTATUS status;
383 struct SERVICE_STATUS service_status;
384 struct QUERY_SERVICE_CONFIG config;
385 uint32_t buf_size = sizeof(config);
386 uint32_t ret_size = 0;
387 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
389 if (argc != 1 ) {
390 d_printf("%s net rpc service status <service>\n", _("Usage:"));
391 return NT_STATUS_OK;
394 /* Open the Service Control Manager */
395 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
396 pipe_hnd->srv_name_slash,
397 NULL,
398 SC_RIGHT_MGR_ENUMERATE_SERVICE,
399 &hSCM,
400 &result);
401 if (!NT_STATUS_IS_OK(status)) {
402 d_fprintf(stderr,
403 _("Failed to open Service Control Manager. [%s]\n"),
404 nt_errstr(status));
405 return status;
407 if (!W_ERROR_IS_OK(result)) {
408 d_fprintf(stderr,
409 _("Failed to open Service Control Manager. [%s]\n"),
410 win_errstr(result));
411 return werror_to_ntstatus(result);
414 /* Open the Service */
416 result = open_service(b, mem_ctx, &hSCM, argv[0],
417 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
418 &hService);
419 if (!W_ERROR_IS_OK(result) ) {
420 goto done;
423 /* get the status */
425 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
426 &hService,
427 &service_status,
428 &result);
429 if (!NT_STATUS_IS_OK(status)) {
430 result = ntstatus_to_werror(status);
431 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
432 nt_errstr(status));
433 goto done;
436 if (!W_ERROR_IS_OK(result) ) {
437 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
438 win_errstr(result));
439 goto done;
442 d_printf(_("%s service is %s.\n"), argv[0],
443 svc_status_string(service_status.state));
445 /* get the config */
447 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
448 &hService,
449 &config,
450 buf_size,
451 &ret_size,
452 &result);
453 if (!NT_STATUS_IS_OK(status)) {
454 result = ntstatus_to_werror(status);
455 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
456 nt_errstr(status));
457 goto done;
460 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
461 buf_size = ret_size;
462 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
463 &hService,
464 &config,
465 buf_size,
466 &ret_size,
467 &result);
468 if (!NT_STATUS_IS_OK(status)) {
469 result = ntstatus_to_werror(status);
470 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
471 nt_errstr(status));
472 goto done;
476 if (!W_ERROR_IS_OK(result) ) {
477 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
478 win_errstr(result));
479 goto done;
482 /* print out the configuration information for the service */
484 d_printf(_("Configuration details:\n"));
485 d_printf(_("\tControls Accepted = 0x%x\n"),
486 service_status.controls_accepted);
487 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
488 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
489 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
490 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
492 if (config.executablepath) {
493 d_printf(_("\tExecutable Path = %s\n"),
494 config.executablepath);
497 if (config.loadordergroup) {
498 d_printf(_("\tLoad Order Group = %s\n"),
499 config.loadordergroup);
502 if (config.dependencies) {
503 d_printf(_("\tDependencies = %s\n"),
504 config.dependencies);
507 if (config.startname) {
508 d_printf(_("\tStart Name = %s\n"), config.startname);
511 if (config.displayname) {
512 d_printf(_("\tDisplay Name = %s\n"),
513 config.displayname);
516 done:
517 if (is_valid_policy_hnd(&hService)) {
518 WERROR _result;
519 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
521 if (is_valid_policy_hnd(&hSCM)) {
522 WERROR _result;
523 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
526 return werror_to_ntstatus(result);
529 /********************************************************************
530 ********************************************************************/
532 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
533 const struct dom_sid *domain_sid,
534 const char *domain_name,
535 struct cli_state *cli,
536 struct rpc_pipe_client *pipe_hnd,
537 TALLOC_CTX *mem_ctx,
538 int argc,
539 const char **argv )
541 struct policy_handle hSCM;
542 WERROR result = WERR_GENERAL_FAILURE;
543 NTSTATUS status;
544 fstring servicename;
545 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
547 if (argc != 1 ) {
548 d_printf("%s net rpc service status <service>\n", _("Usage:"));
549 return NT_STATUS_OK;
552 fstrcpy( servicename, argv[0] );
554 /* Open the Service Control Manager */
555 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
556 pipe_hnd->srv_name_slash,
557 NULL,
558 SC_RIGHT_MGR_ENUMERATE_SERVICE,
559 &hSCM,
560 &result);
561 if (!NT_STATUS_IS_OK(status)) {
562 d_fprintf(stderr,
563 _("Failed to open Service Control Manager. [%s]\n"),
564 nt_errstr(status));
565 return status;
567 if (!W_ERROR_IS_OK(result)) {
568 d_fprintf(stderr,
569 _("Failed to open Service Control Manager. [%s]\n"),
570 win_errstr(result));
571 return werror_to_ntstatus(result);
574 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
575 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
577 if (is_valid_policy_hnd(&hSCM)) {
578 WERROR _result;
579 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
582 return werror_to_ntstatus(result);
585 /********************************************************************
586 ********************************************************************/
588 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
589 const struct dom_sid *domain_sid,
590 const char *domain_name,
591 struct cli_state *cli,
592 struct rpc_pipe_client *pipe_hnd,
593 TALLOC_CTX *mem_ctx,
594 int argc,
595 const char **argv )
597 struct policy_handle hSCM;
598 WERROR result = WERR_GENERAL_FAILURE;
599 NTSTATUS status;
600 fstring servicename;
601 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
603 if (argc != 1 ) {
604 d_printf("%s net rpc service status <service>\n", _("Usage:"));
605 return NT_STATUS_OK;
608 fstrcpy( servicename, argv[0] );
610 /* Open the Service Control Manager */
611 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
612 pipe_hnd->srv_name_slash,
613 NULL,
614 SC_RIGHT_MGR_ENUMERATE_SERVICE,
615 &hSCM,
616 &result);
617 if (!NT_STATUS_IS_OK(status)) {
618 d_fprintf(stderr,
619 _("Failed to open Service Control Manager. [%s]\n"),
620 nt_errstr(status));
621 return status;
623 if (!W_ERROR_IS_OK(result)) {
624 d_fprintf(stderr,
625 _("Failed to open Service Control Manager. [%s]\n"),
626 win_errstr(result));
627 return werror_to_ntstatus(result);
630 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
631 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
633 if (is_valid_policy_hnd(&hSCM)) {
634 WERROR _result;
635 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
638 return werror_to_ntstatus(result);
641 /********************************************************************
642 ********************************************************************/
644 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
645 const struct dom_sid *domain_sid,
646 const char *domain_name,
647 struct cli_state *cli,
648 struct rpc_pipe_client *pipe_hnd,
649 TALLOC_CTX *mem_ctx,
650 int argc,
651 const char **argv )
653 struct policy_handle hSCM;
654 WERROR result = WERR_GENERAL_FAILURE;
655 NTSTATUS status;
656 fstring servicename;
657 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
659 if (argc != 1 ) {
660 d_printf("%s net rpc service status <service>\n", _("Usage:"));
661 return NT_STATUS_OK;
664 fstrcpy( servicename, argv[0] );
666 /* Open the Service Control Manager */
667 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
668 pipe_hnd->srv_name_slash,
669 NULL,
670 SC_RIGHT_MGR_ENUMERATE_SERVICE,
671 &hSCM,
672 &result);
673 if (!NT_STATUS_IS_OK(status)) {
674 d_fprintf(stderr,
675 _("Failed to open Service Control Manager. [%s]\n"),
676 nt_errstr(status));
677 return status;
679 if (!W_ERROR_IS_OK(result)) {
680 d_fprintf(stderr,
681 _("Failed to open Service Control Manager. [%s]\n"),
682 win_errstr(result));
683 return werror_to_ntstatus(result);
686 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
687 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
689 if (is_valid_policy_hnd(&hSCM)) {
690 WERROR _result;
691 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
694 return werror_to_ntstatus(result);
697 /********************************************************************
698 ********************************************************************/
700 static NTSTATUS rpc_service_start_internal(struct net_context *c,
701 const struct dom_sid *domain_sid,
702 const char *domain_name,
703 struct cli_state *cli,
704 struct rpc_pipe_client *pipe_hnd,
705 TALLOC_CTX *mem_ctx,
706 int argc,
707 const char **argv )
709 struct policy_handle hSCM, hService;
710 WERROR result = WERR_GENERAL_FAILURE;
711 NTSTATUS status;
712 uint32 state = 0;
713 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
715 if (argc != 1 ) {
716 d_printf("%s net rpc service status <service>\n", _("Usage:"));
717 return NT_STATUS_OK;
720 /* Open the Service Control Manager */
721 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
722 pipe_hnd->srv_name_slash,
723 NULL,
724 SC_RIGHT_MGR_ENUMERATE_SERVICE,
725 &hSCM,
726 &result);
727 if (!NT_STATUS_IS_OK(status)) {
728 d_fprintf(stderr,
729 _("Failed to open Service Control Manager. [%s]\n"),
730 nt_errstr(status));
731 return status;
733 if (!W_ERROR_IS_OK(result)) {
734 d_fprintf(stderr,
735 _("Failed to open Service Control Manager. [%s]\n"),
736 win_errstr(result));
737 return werror_to_ntstatus(result);
741 /* Open the Service */
743 result = open_service(b, mem_ctx, &hSCM, argv[0],
744 SC_RIGHT_SVC_START,
745 &hService);
746 if (!W_ERROR_IS_OK(result) ) {
747 goto done;
750 /* get the status */
752 status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
753 &hService,
755 NULL,
756 &result);
758 if (!NT_STATUS_IS_OK(status)) {
759 result = ntstatus_to_werror(status);
760 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
761 nt_errstr(status));
762 goto done;
764 if (!W_ERROR_IS_OK(result) ) {
765 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
766 win_errstr(result));
767 goto done;
770 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
772 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
773 d_printf(_("Successfully started service: %s\n"),
774 argv[0] );
775 else
776 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
777 argv[0], win_errstr(result) );
779 done:
780 if (is_valid_policy_hnd(&hService)) {
781 WERROR _result;
782 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
784 if (is_valid_policy_hnd(&hSCM)) {
785 WERROR _result;
786 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
789 return werror_to_ntstatus(result);
792 /********************************************************************
793 ********************************************************************/
795 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
796 const struct dom_sid *domain_sid,
797 const char *domain_name,
798 struct cli_state *cli,
799 struct rpc_pipe_client *pipe_hnd,
800 TALLOC_CTX *mem_ctx,
801 int argc,
802 const char **argv)
804 struct policy_handle hSCM, hService;
805 WERROR result = WERR_GENERAL_FAILURE;
806 NTSTATUS status;
807 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
809 if (argc != 1 ) {
810 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
811 return NT_STATUS_OK;
814 ZERO_STRUCT(hSCM);
815 ZERO_STRUCT(hService);
817 /* Open the Service Control Manager */
818 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
819 pipe_hnd->srv_name_slash,
820 NULL,
821 SC_RIGHT_MGR_ENUMERATE_SERVICE,
822 &hSCM,
823 &result);
824 if (!NT_STATUS_IS_OK(status)) {
825 d_fprintf(stderr,
826 _("Failed to open Service Control Manager. [%s]\n"),
827 nt_errstr(status));
828 return status;
830 if (!W_ERROR_IS_OK(result)) {
831 d_fprintf(stderr,
832 _("Failed to open Service Control Manager. [%s]\n"),
833 win_errstr(result));
834 return werror_to_ntstatus(result);
837 /* Open the Service */
839 result = open_service(b, mem_ctx, &hSCM, argv[0],
840 SERVICE_ALL_ACCESS,
841 &hService);
842 if (!W_ERROR_IS_OK(result) ) {
843 goto done;
846 /* Delete the Service */
848 status = dcerpc_svcctl_DeleteService(b, mem_ctx,
849 &hService,
850 &result);
852 if (!NT_STATUS_IS_OK(status)) {
853 result = ntstatus_to_werror(status);
854 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
855 nt_errstr(status));
856 goto done;
858 if (!W_ERROR_IS_OK(result)) {
859 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
860 win_errstr(result));
861 goto done;
864 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
866 done:
867 if (is_valid_policy_hnd(&hService)) {
868 WERROR _result;
869 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
871 if (is_valid_policy_hnd(&hSCM)) {
872 WERROR _result;
873 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
876 return werror_to_ntstatus(result);
879 /********************************************************************
880 ********************************************************************/
882 static NTSTATUS rpc_service_create_internal(struct net_context *c,
883 const struct dom_sid *domain_sid,
884 const char *domain_name,
885 struct cli_state *cli,
886 struct rpc_pipe_client *pipe_hnd,
887 TALLOC_CTX *mem_ctx,
888 int argc,
889 const char **argv)
891 struct policy_handle hSCM, hService;
892 WERROR result = WERR_GENERAL_FAILURE;
893 NTSTATUS status;
894 const char *ServiceName;
895 const char *DisplayName;
896 const char *binary_path;
897 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
899 if (argc != 3) {
900 d_printf("%s net rpc service create <service> "
901 "<displayname> <binarypath>\n", _("Usage:"));
902 return NT_STATUS_OK;
905 ZERO_STRUCT(hSCM);
906 ZERO_STRUCT(hService);
908 /* Open the Service Control Manager */
909 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
910 pipe_hnd->srv_name_slash,
911 NULL,
912 SC_RIGHT_MGR_CREATE_SERVICE,
913 &hSCM,
914 &result);
915 if (!NT_STATUS_IS_OK(status)) {
916 d_fprintf(stderr,
917 _("Failed to open Service Control Manager. [%s]\n"),
918 nt_errstr(status));
919 return status;
921 if (!W_ERROR_IS_OK(result)) {
922 d_fprintf(stderr,
923 _("Failed to open Service Control Manager. [%s]\n"),
924 win_errstr(result));
925 return werror_to_ntstatus(result);
928 /* Create the service */
930 ServiceName = argv[0];
931 DisplayName = argv[1];
932 binary_path = argv[2];
934 status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
935 &hSCM,
936 ServiceName,
937 DisplayName,
938 SERVICE_ALL_ACCESS,
939 SERVICE_TYPE_WIN32_OWN_PROCESS,
940 SVCCTL_DEMAND_START,
941 SVCCTL_SVC_ERROR_NORMAL,
942 binary_path,
943 NULL, /* LoadOrderGroupKey */
944 NULL, /* TagId */
945 NULL, /* dependencies */
946 0, /* dependencies_size */
947 NULL, /* service_start_name */
948 NULL, /* password */
949 0, /* password_size */
950 &hService,
951 &result);
952 if (!NT_STATUS_IS_OK(status)) {
953 result = ntstatus_to_werror(status);
954 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
955 nt_errstr(status));
956 goto done;
958 if (!W_ERROR_IS_OK(result)) {
959 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
960 win_errstr(result));
961 goto done;
964 d_printf(_("Successfully created Service: %s\n"), argv[0]);
966 done:
967 if (is_valid_policy_hnd(&hService)) {
968 WERROR _result;
969 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
971 if (is_valid_policy_hnd(&hSCM)) {
972 WERROR _result;
973 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
976 return werror_to_ntstatus(result);
979 /********************************************************************
980 ********************************************************************/
982 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
984 if (c->display_usage) {
985 d_printf( "%s\n"
986 "net rpc service list\n"
987 " %s\n",
988 _("Usage:"),
989 _("View configured Win32 services"));
990 return 0;
993 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
994 rpc_service_list_internal, argc, argv );
997 /********************************************************************
998 ********************************************************************/
1000 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
1002 if (c->display_usage) {
1003 d_printf( "%s\n"
1004 "net rpc service start <service>\n"
1005 " %s\n",
1006 _("Usage:"),
1007 _("Start a Win32 service"));
1008 return 0;
1011 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1012 rpc_service_start_internal, argc, argv );
1015 /********************************************************************
1016 ********************************************************************/
1018 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
1020 if (c->display_usage) {
1021 d_printf( "%s\n"
1022 "net rpc service stop <service>\n"
1023 " %s\n",
1024 _("Usage:"),
1025 _("Stop a Win32 service"));
1026 return 0;
1029 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1030 rpc_service_stop_internal, argc, argv );
1033 /********************************************************************
1034 ********************************************************************/
1036 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
1038 if (c->display_usage) {
1039 d_printf( "%s\n"
1040 "net rpc service resume <service>\n"
1041 " %s\n",
1042 _("Usage:"),
1043 _("Resume a Win32 service"));
1044 return 0;
1047 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1048 rpc_service_resume_internal, argc, argv );
1051 /********************************************************************
1052 ********************************************************************/
1054 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
1056 if (c->display_usage) {
1057 d_printf( "%s\n"
1058 "net rpc service pause <service>\n"
1059 " %s\n",
1060 _("Usage:"),
1061 _("Pause a Win32 service"));
1062 return 0;
1065 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1066 rpc_service_pause_internal, argc, argv );
1069 /********************************************************************
1070 ********************************************************************/
1072 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
1074 if (c->display_usage) {
1075 d_printf( "%s\n"
1076 "net rpc service status <service>\n"
1077 " %s\n",
1078 _("Usage:"),
1079 _("Show the current status of a service"));
1080 return 0;
1083 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1084 rpc_service_status_internal, argc, argv );
1087 /********************************************************************
1088 ********************************************************************/
1090 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
1092 if (c->display_usage) {
1093 d_printf( "%s\n"
1094 "net rpc service delete <service>\n"
1095 " %s\n",
1096 _("Usage:"),
1097 _("Delete a Win32 service"));
1098 return 0;
1101 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1102 rpc_service_delete_internal, argc, argv);
1105 /********************************************************************
1106 ********************************************************************/
1108 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
1110 if (c->display_usage) {
1111 d_printf( "%s\n"
1112 "net rpc service create <service>\n"
1113 " %s\n",
1114 _("Usage:"),
1115 _("Create a Win32 service"));
1116 return 0;
1119 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1120 rpc_service_create_internal, argc, argv);
1123 /********************************************************************
1124 ********************************************************************/
1126 int net_rpc_service(struct net_context *c, int argc, const char **argv)
1128 struct functable func[] = {
1130 "list",
1131 rpc_service_list,
1132 NET_TRANSPORT_RPC,
1133 N_("View configured Win32 services"),
1134 N_("net rpc service list\n"
1135 " View configured Win32 services")
1138 "start",
1139 rpc_service_start,
1140 NET_TRANSPORT_RPC,
1141 N_("Start a service"),
1142 N_("net rpc service start\n"
1143 " Start a service")
1146 "stop",
1147 rpc_service_stop,
1148 NET_TRANSPORT_RPC,
1149 N_("Stop a service"),
1150 N_("net rpc service stop\n"
1151 " Stop a service")
1154 "pause",
1155 rpc_service_pause,
1156 NET_TRANSPORT_RPC,
1157 N_("Pause a service"),
1158 N_("net rpc service pause\n"
1159 " Pause a service")
1162 "resume",
1163 rpc_service_resume,
1164 NET_TRANSPORT_RPC,
1165 N_("Resume a paused service"),
1166 N_("net rpc service resume\n"
1167 " Resume a service")
1170 "status",
1171 rpc_service_status,
1172 NET_TRANSPORT_RPC,
1173 N_("View current status of a service"),
1174 N_("net rpc service status\n"
1175 " View current status of a service")
1178 "delete",
1179 rpc_service_delete,
1180 NET_TRANSPORT_RPC,
1181 N_("Delete a service"),
1182 N_("net rpc service delete\n"
1183 " Deletes a service")
1186 "create",
1187 rpc_service_create,
1188 NET_TRANSPORT_RPC,
1189 N_("Create a service"),
1190 N_("net rpc service create\n"
1191 " Creates a service")
1194 {NULL, NULL, 0, NULL, NULL}
1197 return net_run_function(c, argc, argv, "net rpc service",func);