idl: rebuild drsuapi.idl
[Samba/aatanasov.git] / source3 / utils / net_rpc_service.c
blob1ef7c3ed098526237b6417b2d73ccc9e0e22e3e3
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, N_("stopped") },
30 { SVCCTL_START_PENDING, N_("start pending") },
31 { SVCCTL_STOP_PENDING, N_("stop pending") },
32 { SVCCTL_RUNNING, N_("running") },
33 { SVCCTL_CONTINUE_PENDING, N_("resume pending") },
34 { SVCCTL_PAUSE_PENDING, N_("pause pending") },
35 { SVCCTL_PAUSED, N_("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 struct policy_handle *hSCM,
65 const char *service,
66 uint32 *state )
68 struct policy_handle hService;
69 struct 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"),
83 win_errstr(result));
84 return result;
87 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
88 &hService,
89 &service_status,
90 &result);
92 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
93 *state = service_status.state;
96 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
98 return result;
101 /********************************************************************
102 ********************************************************************/
104 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
105 TALLOC_CTX *mem_ctx,
106 struct policy_handle *hSCM,
107 const char *service,
108 uint32 watch_state,
109 uint32 *final_state )
111 uint32 i;
112 uint32 state = 0;
113 WERROR result = WERR_GENERAL_FAILURE;
116 i = 0;
117 while ( (state != watch_state ) && i<30 ) {
118 /* get the status */
120 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
121 if ( !W_ERROR_IS_OK(result) ) {
122 break;
125 d_printf(".");
126 i++;
127 sys_usleep( 100 );
129 d_printf("\n");
131 *final_state = state;
133 return result;
136 /********************************************************************
137 ********************************************************************/
139 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
140 TALLOC_CTX *mem_ctx,
141 struct policy_handle *hSCM,
142 const char *service,
143 uint32 control,
144 uint32 watch_state )
146 struct policy_handle hService;
147 WERROR result = WERR_GENERAL_FAILURE;
148 NTSTATUS status;
149 struct SERVICE_STATUS service_status;
150 uint32 state = 0;
152 /* Open the Service */
154 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
155 hSCM,
156 service,
157 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
158 &hService,
159 &result);
161 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
162 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
163 win_errstr(result));
164 goto done;
167 /* get the status */
169 status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
170 &hService,
171 control,
172 &service_status,
173 &result);
175 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
176 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
177 win_errstr(result));
178 goto done;
181 /* loop -- checking the state until we are where we want to be */
183 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
185 d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
187 done:
188 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
190 return result;
193 /********************************************************************
194 ********************************************************************/
196 static NTSTATUS rpc_service_list_internal(struct net_context *c,
197 const DOM_SID *domain_sid,
198 const char *domain_name,
199 struct cli_state *cli,
200 struct rpc_pipe_client *pipe_hnd,
201 TALLOC_CTX *mem_ctx,
202 int argc,
203 const char **argv )
205 struct policy_handle hSCM;
206 struct ENUM_SERVICE_STATUSW *services = NULL;
207 WERROR result = WERR_GENERAL_FAILURE;
208 NTSTATUS status;
209 int i;
211 uint8_t *buffer = NULL;
212 uint32_t buf_size = 0;
213 uint32_t bytes_needed = 0;
214 uint32_t num_services = 0;
215 uint32_t resume_handle = 0;
217 if (argc != 0 ) {
218 d_printf(_("Usage: net rpc service list\n"));
219 return NT_STATUS_OK;
222 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
223 pipe_hnd->srv_name_slash,
224 NULL,
225 SC_RIGHT_MGR_ENUMERATE_SERVICE,
226 &hSCM,
227 &result);
228 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
229 d_fprintf(stderr,
230 _("Failed to open Service Control Manager. [%s]\n"),
231 win_errstr(result));
232 return werror_to_ntstatus(result);
235 do {
236 status = rpccli_svcctl_EnumServicesStatusW(pipe_hnd, mem_ctx,
237 &hSCM,
238 SERVICE_TYPE_WIN32,
239 SERVICE_STATE_ALL,
240 buffer,
241 buf_size,
242 &bytes_needed,
243 &num_services,
244 &resume_handle,
245 &result);
247 if (NT_STATUS_IS_ERR(status)) {
248 d_fprintf(stderr,
249 _("Failed to enumerate services. [%s]\n"),
250 win_errstr(result));
251 break;
254 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
255 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
256 buf_size = bytes_needed;
257 continue;
260 if ( num_services == 0 ) {
261 d_printf(_("No services returned\n"));
262 break;
266 enum ndr_err_code ndr_err;
267 DATA_BLOB blob;
268 struct ndr_pull *ndr;
270 blob.length = buf_size;
271 blob.data = talloc_steal(mem_ctx, buffer);
273 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
274 if (!services) {
275 status = NT_STATUS_NO_MEMORY;
276 break;
279 ndr = ndr_pull_init_blob(&blob, mem_ctx, NULL);
280 if (ndr == NULL) {
281 status = NT_STATUS_NO_MEMORY;
282 break;
285 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
286 ndr, num_services, services);
287 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
288 status = ndr_map_error2ntstatus(ndr_err);
289 break;
292 for ( i=0; i<num_services; i++ ) {
293 d_printf("%-20s \"%s\"\n",
294 services[i].service_name,
295 services[i].display_name);
299 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
301 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
303 return status;
306 /********************************************************************
307 ********************************************************************/
309 static NTSTATUS rpc_service_status_internal(struct net_context *c,
310 const DOM_SID *domain_sid,
311 const char *domain_name,
312 struct cli_state *cli,
313 struct rpc_pipe_client *pipe_hnd,
314 TALLOC_CTX *mem_ctx,
315 int argc,
316 const char **argv )
318 struct policy_handle hSCM, hService;
319 WERROR result = WERR_GENERAL_FAILURE;
320 NTSTATUS status;
321 struct SERVICE_STATUS service_status;
322 struct QUERY_SERVICE_CONFIG config;
323 uint32_t buf_size = sizeof(config);
324 uint32_t ret_size = 0;
326 if (argc != 1 ) {
327 d_printf(_("Usage: net rpc service status <service>\n"));
328 return NT_STATUS_OK;
331 /* Open the Service Control Manager */
332 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
333 pipe_hnd->srv_name_slash,
334 NULL,
335 SC_RIGHT_MGR_ENUMERATE_SERVICE,
336 &hSCM,
337 &result);
338 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
339 d_fprintf(stderr,
340 _("Failed to open Service Control Manager. [%s]\n"),
341 win_errstr(result));
342 return werror_to_ntstatus(result);
345 /* Open the Service */
347 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
348 &hSCM,
349 argv[0],
350 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
351 &hService,
352 &result);
354 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
355 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
356 win_errstr(result));
357 goto done;
360 /* get the status */
362 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
363 &hService,
364 &service_status,
365 &result);
367 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
368 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
369 win_errstr(result));
370 goto done;
373 d_printf(_("%s service is %s.\n"), argv[0],
374 svc_status_string(service_status.state));
376 /* get the config */
378 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
379 &hService,
380 &config,
381 buf_size,
382 &ret_size,
383 &result);
384 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
385 buf_size = ret_size;
386 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
387 &hService,
388 &config,
389 buf_size,
390 &ret_size,
391 &result);
394 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
395 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
396 win_errstr(result));
397 goto done;
400 /* print out the configuration information for the service */
402 d_printf(_("Configuration details:\n"));
403 d_printf(_("\tControls Accepted = 0x%x\n"),
404 service_status.controls_accepted);
405 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
406 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
407 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
408 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
410 if (config.executablepath) {
411 d_printf(_("\tExecutable Path = %s\n"),
412 config.executablepath);
415 if (config.loadordergroup) {
416 d_printf(_("\tLoad Order Group = %s\n"),
417 config.loadordergroup);
420 if (config.dependencies) {
421 d_printf(_("\tDependencies = %s\n"),
422 config.dependencies);
425 if (config.startname) {
426 d_printf(_("\tStart Name = %s\n"), config.startname);
429 if (config.displayname) {
430 d_printf(_("\tDisplay Name = %s\n"),
431 config.displayname);
434 done:
435 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
436 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
438 return werror_to_ntstatus(result);
441 /********************************************************************
442 ********************************************************************/
444 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
445 const DOM_SID *domain_sid,
446 const char *domain_name,
447 struct cli_state *cli,
448 struct rpc_pipe_client *pipe_hnd,
449 TALLOC_CTX *mem_ctx,
450 int argc,
451 const char **argv )
453 struct policy_handle hSCM;
454 WERROR result = WERR_GENERAL_FAILURE;
455 NTSTATUS status;
456 fstring servicename;
458 if (argc != 1 ) {
459 d_printf(_("Usage: net rpc service status <service>\n"));
460 return NT_STATUS_OK;
463 fstrcpy( servicename, argv[0] );
465 /* Open the Service Control Manager */
466 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
467 pipe_hnd->srv_name_slash,
468 NULL,
469 SC_RIGHT_MGR_ENUMERATE_SERVICE,
470 &hSCM,
471 &result);
472 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
473 d_fprintf(stderr,
474 _("Failed to open Service Control Manager. [%s]\n"),
475 win_errstr(result));
476 return werror_to_ntstatus(result);
479 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
480 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
482 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
484 return werror_to_ntstatus(result);
487 /********************************************************************
488 ********************************************************************/
490 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
491 const DOM_SID *domain_sid,
492 const char *domain_name,
493 struct cli_state *cli,
494 struct rpc_pipe_client *pipe_hnd,
495 TALLOC_CTX *mem_ctx,
496 int argc,
497 const char **argv )
499 struct policy_handle hSCM;
500 WERROR result = WERR_GENERAL_FAILURE;
501 NTSTATUS status;
502 fstring servicename;
504 if (argc != 1 ) {
505 d_printf(_("Usage: net rpc service status <service>\n"));
506 return NT_STATUS_OK;
509 fstrcpy( servicename, argv[0] );
511 /* Open the Service Control Manager */
512 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
513 pipe_hnd->srv_name_slash,
514 NULL,
515 SC_RIGHT_MGR_ENUMERATE_SERVICE,
516 &hSCM,
517 &result);
518 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
519 d_fprintf(stderr,
520 _("Failed to open Service Control Manager. [%s]\n"),
521 win_errstr(result));
522 return werror_to_ntstatus(result);
525 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
526 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
528 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
530 return werror_to_ntstatus(result);
533 /********************************************************************
534 ********************************************************************/
536 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
537 const DOM_SID *domain_sid,
538 const char *domain_name,
539 struct cli_state *cli,
540 struct rpc_pipe_client *pipe_hnd,
541 TALLOC_CTX *mem_ctx,
542 int argc,
543 const char **argv )
545 struct policy_handle hSCM;
546 WERROR result = WERR_GENERAL_FAILURE;
547 NTSTATUS status;
548 fstring servicename;
550 if (argc != 1 ) {
551 d_printf(_("Usage: net rpc service status <service>\n"));
552 return NT_STATUS_OK;
555 fstrcpy( servicename, argv[0] );
557 /* Open the Service Control Manager */
558 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
559 pipe_hnd->srv_name_slash,
560 NULL,
561 SC_RIGHT_MGR_ENUMERATE_SERVICE,
562 &hSCM,
563 &result);
564 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
565 d_fprintf(stderr,
566 _("Failed to open Service Control Manager. [%s]\n"),
567 win_errstr(result));
568 return werror_to_ntstatus(result);
571 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
572 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
574 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
576 return werror_to_ntstatus(result);
579 /********************************************************************
580 ********************************************************************/
582 static NTSTATUS rpc_service_start_internal(struct net_context *c,
583 const DOM_SID *domain_sid,
584 const char *domain_name,
585 struct cli_state *cli,
586 struct rpc_pipe_client *pipe_hnd,
587 TALLOC_CTX *mem_ctx,
588 int argc,
589 const char **argv )
591 struct policy_handle hSCM, hService;
592 WERROR result = WERR_GENERAL_FAILURE;
593 NTSTATUS status;
594 uint32 state = 0;
596 if (argc != 1 ) {
597 d_printf(_("Usage: net rpc service status <service>\n"));
598 return NT_STATUS_OK;
601 /* Open the Service Control Manager */
602 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
603 pipe_hnd->srv_name_slash,
604 NULL,
605 SC_RIGHT_MGR_ENUMERATE_SERVICE,
606 &hSCM,
607 &result);
608 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
609 d_fprintf(stderr,
610 _("Failed to open Service Control Manager. [%s]\n"),
611 win_errstr(result));
612 return werror_to_ntstatus(result);
615 /* Open the Service */
617 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
618 &hSCM,
619 argv[0],
620 SC_RIGHT_SVC_START,
621 &hService,
622 &result);
624 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
625 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
626 win_errstr(result));
627 goto done;
630 /* get the status */
632 status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
633 &hService,
635 NULL,
636 &result);
638 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
639 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
640 win_errstr(result));
641 goto done;
644 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
646 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
647 d_printf(_("Successfully started service: %s\n"),
648 argv[0] );
649 else
650 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
651 argv[0], win_errstr(result) );
653 done:
654 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
655 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
657 return werror_to_ntstatus(result);
660 /********************************************************************
661 ********************************************************************/
663 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
664 const 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;
676 if (argc != 1 ) {
677 d_printf(_("Usage: net rpc service delete <service>\n"));
678 return NT_STATUS_OK;
681 /* Open the Service Control Manager */
682 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
683 pipe_hnd->srv_name_slash,
684 NULL,
685 SC_RIGHT_MGR_ENUMERATE_SERVICE,
686 &hSCM,
687 &result);
688 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
689 d_fprintf(stderr,
690 _("Failed to open Service Control Manager. [%s]\n"),
691 win_errstr(result));
692 return werror_to_ntstatus(result);
695 /* Open the Service */
697 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
698 &hSCM,
699 argv[0],
700 SERVICE_ALL_ACCESS,
701 &hService,
702 &result);
704 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
705 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
706 win_errstr(result));
707 goto done;
710 /* Delete the Service */
712 status = rpccli_svcctl_DeleteService(pipe_hnd, mem_ctx,
713 &hService,
714 &result);
716 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
717 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
718 win_errstr(result));
719 goto done;
722 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
724 done:
725 if (is_valid_policy_hnd(&hService)) {
726 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
728 if (is_valid_policy_hnd(&hSCM)) {
729 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
732 return werror_to_ntstatus(result);
735 /********************************************************************
736 ********************************************************************/
738 static NTSTATUS rpc_service_create_internal(struct net_context *c,
739 const DOM_SID *domain_sid,
740 const char *domain_name,
741 struct cli_state *cli,
742 struct rpc_pipe_client *pipe_hnd,
743 TALLOC_CTX *mem_ctx,
744 int argc,
745 const char **argv)
747 struct policy_handle hSCM, hService;
748 WERROR result = WERR_GENERAL_FAILURE;
749 NTSTATUS status;
750 const char *ServiceName;
751 const char *DisplayName;
752 const char *binary_path;
754 if (argc != 3) {
755 d_printf(_("Usage: net rpc service create <service> "
756 "<displayname> <binarypath>\n"));
757 return NT_STATUS_OK;
760 /* Open the Service Control Manager */
761 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
762 pipe_hnd->srv_name_slash,
763 NULL,
764 SC_RIGHT_MGR_CREATE_SERVICE,
765 &hSCM,
766 &result);
767 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
768 d_fprintf(stderr,
769 _("Failed to open Service Control Manager. [%s]\n"),
770 win_errstr(result));
771 return werror_to_ntstatus(result);
774 /* Create the service */
776 ServiceName = argv[0];
777 DisplayName = argv[1];
778 binary_path = argv[2];
780 status = rpccli_svcctl_CreateServiceW(pipe_hnd, mem_ctx,
781 &hSCM,
782 ServiceName,
783 DisplayName,
784 SERVICE_ALL_ACCESS,
785 SERVICE_TYPE_WIN32_OWN_PROCESS,
786 SVCCTL_DEMAND_START,
787 SVCCTL_SVC_ERROR_NORMAL,
788 binary_path,
789 NULL, /* LoadOrderGroupKey */
790 NULL, /* TagId */
791 NULL, /* dependencies */
792 0, /* dependencies_size */
793 NULL, /* service_start_name */
794 NULL, /* password */
795 0, /* password_size */
796 &hService,
797 &result);
799 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
800 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
801 win_errstr(result));
802 goto done;
805 d_printf(_("Successfully created Service: %s\n"), argv[0]);
807 done:
808 if (is_valid_policy_hnd(&hService)) {
809 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
811 if (is_valid_policy_hnd(&hSCM)) {
812 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
815 return werror_to_ntstatus(result);
818 /********************************************************************
819 ********************************************************************/
821 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
823 if (c->display_usage) {
824 d_printf(_("Usage:\n"
825 "net rpc service list\n"
826 " View configured Win32 services\n"));
827 return 0;
830 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
831 rpc_service_list_internal, argc, argv );
834 /********************************************************************
835 ********************************************************************/
837 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
839 if (c->display_usage) {
840 d_printf(_("Usage:\n"
841 "net rpc service start <service>\n"
842 " Start a Win32 service\n"));
843 return 0;
846 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
847 rpc_service_start_internal, argc, argv );
850 /********************************************************************
851 ********************************************************************/
853 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
855 if (c->display_usage) {
856 d_printf(_("Usage:\n"
857 "net rpc service stop <service>\n"
858 " Stop a Win32 service\n"));
859 return 0;
862 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
863 rpc_service_stop_internal, argc, argv );
866 /********************************************************************
867 ********************************************************************/
869 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
871 if (c->display_usage) {
872 d_printf(_("Usage:\n"
873 "net rpc service resume <service>\n"
874 " Resume a Win32 service\n"));
875 return 0;
878 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
879 rpc_service_resume_internal, argc, argv );
882 /********************************************************************
883 ********************************************************************/
885 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
887 if (c->display_usage) {
888 d_printf(_("Usage:\n"
889 "net rpc service pause <service>\n"
890 " Pause a Win32 service\n"));
891 return 0;
894 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
895 rpc_service_pause_internal, argc, argv );
898 /********************************************************************
899 ********************************************************************/
901 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
903 if (c->display_usage) {
904 d_printf(_("Usage:\n"
905 "net rpc service status <service>\n"
906 " Show the current status of a service\n"));
907 return 0;
910 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
911 rpc_service_status_internal, argc, argv );
914 /********************************************************************
915 ********************************************************************/
917 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
919 if (c->display_usage) {
920 d_printf(_("Usage:\n"
921 "net rpc service delete <service>\n"
922 " Delete a Win32 service\n"));
923 return 0;
926 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
927 rpc_service_delete_internal, argc, argv);
930 /********************************************************************
931 ********************************************************************/
933 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
935 if (c->display_usage) {
936 d_printf(_("Usage:\n"
937 "net rpc service create <service>\n"
938 " Create a Win32 service\n"));
939 return 0;
942 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
943 rpc_service_create_internal, argc, argv);
946 /********************************************************************
947 ********************************************************************/
949 int net_rpc_service(struct net_context *c, int argc, const char **argv)
951 struct functable func[] = {
953 "list",
954 rpc_service_list,
955 NET_TRANSPORT_RPC,
956 N_("View configured Win32 services"),
957 N_("net rpc service list\n"
958 " View configured Win32 services")
961 "start",
962 rpc_service_start,
963 NET_TRANSPORT_RPC,
964 N_("Start a service"),
965 N_("net rpc service start\n"
966 " Start a service")
969 "stop",
970 rpc_service_stop,
971 NET_TRANSPORT_RPC,
972 N_("Stop a service"),
973 N_("net rpc service stop\n"
974 " Stop a service")
977 "pause",
978 rpc_service_pause,
979 NET_TRANSPORT_RPC,
980 N_("Pause a service"),
981 N_("net rpc service pause\n"
982 " Pause a service")
985 "resume",
986 rpc_service_resume,
987 NET_TRANSPORT_RPC,
988 N_("Resume a paused service"),
989 N_("net rpc service resume\n"
990 " Resume a service")
993 "status",
994 rpc_service_status,
995 NET_TRANSPORT_RPC,
996 N_("View current status of a service"),
997 N_("net rpc service status\n"
998 " View current status of a service")
1001 "delete",
1002 rpc_service_delete,
1003 NET_TRANSPORT_RPC,
1004 N_("Delete a service"),
1005 N_("net rpc service delete\n"
1006 " Deletes a service")
1009 "create",
1010 rpc_service_create,
1011 NET_TRANSPORT_RPC,
1012 N_("Create a service"),
1013 N_("net rpc service create\n"
1014 " Creates a service")
1017 {NULL, NULL, 0, NULL, NULL}
1020 return net_run_function(c, argc, argv, "net rpc service",func);