s3-dcerpc: Pull packet in the caller, before validation
[Samba.git] / source3 / utils / net_rpc_service.c
blob631a5a130d63c433e77d729242e9afcbc85c7f80
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/cli_svcctl.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 query_service_state(struct rpc_pipe_client *pipe_hnd,
64 TALLOC_CTX *mem_ctx,
65 struct policy_handle *hSCM,
66 const char *service,
67 uint32 *state )
69 struct policy_handle hService;
70 struct SERVICE_STATUS service_status;
71 WERROR result = WERR_GENERAL_FAILURE;
72 NTSTATUS status;
74 /* now cycle until the status is actually 'watch_state' */
76 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
77 hSCM,
78 service,
79 SC_RIGHT_SVC_QUERY_STATUS,
80 &hService,
81 &result);
82 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
83 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
84 win_errstr(result));
85 return result;
88 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
89 &hService,
90 &service_status,
91 &result);
93 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
94 *state = service_status.state;
97 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
99 return result;
102 /********************************************************************
103 ********************************************************************/
105 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
106 TALLOC_CTX *mem_ctx,
107 struct policy_handle *hSCM,
108 const char *service,
109 uint32 watch_state,
110 uint32 *final_state )
112 uint32 i;
113 uint32 state = 0;
114 WERROR result = WERR_GENERAL_FAILURE;
117 i = 0;
118 while ( (state != watch_state ) && i<30 ) {
119 /* get the status */
121 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
122 if ( !W_ERROR_IS_OK(result) ) {
123 break;
126 d_printf(".");
127 i++;
128 sys_usleep( 100 );
130 d_printf("\n");
132 *final_state = state;
134 return result;
137 /********************************************************************
138 ********************************************************************/
140 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
141 TALLOC_CTX *mem_ctx,
142 struct policy_handle *hSCM,
143 const char *service,
144 uint32 control,
145 uint32 watch_state )
147 struct policy_handle hService;
148 WERROR result = WERR_GENERAL_FAILURE;
149 NTSTATUS status;
150 struct SERVICE_STATUS service_status;
151 uint32 state = 0;
153 /* Open the Service */
155 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
156 hSCM,
157 service,
158 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
159 &hService,
160 &result);
162 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
163 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
164 win_errstr(result));
165 goto done;
168 /* get the status */
170 status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
171 &hService,
172 control,
173 &service_status,
174 &result);
176 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
177 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
178 win_errstr(result));
179 goto done;
182 /* loop -- checking the state until we are where we want to be */
184 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
186 d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
188 done:
189 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
191 return result;
194 /********************************************************************
195 ********************************************************************/
197 static NTSTATUS rpc_service_list_internal(struct net_context *c,
198 const struct dom_sid *domain_sid,
199 const char *domain_name,
200 struct cli_state *cli,
201 struct rpc_pipe_client *pipe_hnd,
202 TALLOC_CTX *mem_ctx,
203 int argc,
204 const char **argv )
206 struct policy_handle hSCM;
207 struct ENUM_SERVICE_STATUSW *services = NULL;
208 WERROR result = WERR_GENERAL_FAILURE;
209 NTSTATUS status;
210 int i;
212 uint8_t *buffer = NULL;
213 uint32_t buf_size = 0;
214 uint32_t bytes_needed = 0;
215 uint32_t num_services = 0;
216 uint32_t resume_handle = 0;
218 if (argc != 0 ) {
219 d_printf("%s net rpc service list\n", _("Usage:"));
220 return NT_STATUS_OK;
223 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
224 pipe_hnd->srv_name_slash,
225 NULL,
226 SC_RIGHT_MGR_ENUMERATE_SERVICE,
227 &hSCM,
228 &result);
229 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
230 d_fprintf(stderr,
231 _("Failed to open Service Control Manager. [%s]\n"),
232 win_errstr(result));
233 return werror_to_ntstatus(result);
236 do {
237 status = rpccli_svcctl_EnumServicesStatusW(pipe_hnd, mem_ctx,
238 &hSCM,
239 SERVICE_TYPE_WIN32,
240 SERVICE_STATE_ALL,
241 buffer,
242 buf_size,
243 &bytes_needed,
244 &num_services,
245 &resume_handle,
246 &result);
248 if (NT_STATUS_IS_ERR(status)) {
249 d_fprintf(stderr,
250 _("Failed to enumerate services. [%s]\n"),
251 win_errstr(result));
252 break;
255 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
256 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
257 buf_size = bytes_needed;
258 continue;
261 if ( num_services == 0 ) {
262 d_printf(_("No services returned\n"));
263 break;
267 enum ndr_err_code ndr_err;
268 DATA_BLOB blob;
269 struct ndr_pull *ndr;
271 blob.length = buf_size;
272 blob.data = talloc_steal(mem_ctx, buffer);
274 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
275 if (!services) {
276 status = NT_STATUS_NO_MEMORY;
277 break;
280 ndr = ndr_pull_init_blob(&blob, mem_ctx);
281 if (ndr == NULL) {
282 status = NT_STATUS_NO_MEMORY;
283 break;
286 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
287 ndr, num_services, services);
288 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
289 status = ndr_map_error2ntstatus(ndr_err);
290 break;
293 for ( i=0; i<num_services; i++ ) {
294 d_printf("%-20s \"%s\"\n",
295 services[i].service_name,
296 services[i].display_name);
300 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
302 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
304 return status;
307 /********************************************************************
308 ********************************************************************/
310 static NTSTATUS rpc_service_status_internal(struct net_context *c,
311 const struct dom_sid *domain_sid,
312 const char *domain_name,
313 struct cli_state *cli,
314 struct rpc_pipe_client *pipe_hnd,
315 TALLOC_CTX *mem_ctx,
316 int argc,
317 const char **argv )
319 struct policy_handle hSCM, hService;
320 WERROR result = WERR_GENERAL_FAILURE;
321 NTSTATUS status;
322 struct SERVICE_STATUS service_status;
323 struct QUERY_SERVICE_CONFIG config;
324 uint32_t buf_size = sizeof(config);
325 uint32_t ret_size = 0;
327 if (argc != 1 ) {
328 d_printf("%s net rpc service status <service>\n", _("Usage:"));
329 return NT_STATUS_OK;
332 /* Open the Service Control Manager */
333 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
334 pipe_hnd->srv_name_slash,
335 NULL,
336 SC_RIGHT_MGR_ENUMERATE_SERVICE,
337 &hSCM,
338 &result);
339 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
340 d_fprintf(stderr,
341 _("Failed to open Service Control Manager. [%s]\n"),
342 win_errstr(result));
343 return werror_to_ntstatus(result);
346 /* Open the Service */
348 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
349 &hSCM,
350 argv[0],
351 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
352 &hService,
353 &result);
355 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
356 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
357 win_errstr(result));
358 goto done;
361 /* get the status */
363 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
364 &hService,
365 &service_status,
366 &result);
368 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
369 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
370 win_errstr(result));
371 goto done;
374 d_printf(_("%s service is %s.\n"), argv[0],
375 svc_status_string(service_status.state));
377 /* get the config */
379 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
380 &hService,
381 &config,
382 buf_size,
383 &ret_size,
384 &result);
385 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
386 buf_size = ret_size;
387 status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
388 &hService,
389 &config,
390 buf_size,
391 &ret_size,
392 &result);
395 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
396 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
397 win_errstr(result));
398 goto done;
401 /* print out the configuration information for the service */
403 d_printf(_("Configuration details:\n"));
404 d_printf(_("\tControls Accepted = 0x%x\n"),
405 service_status.controls_accepted);
406 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
407 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
408 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
409 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
411 if (config.executablepath) {
412 d_printf(_("\tExecutable Path = %s\n"),
413 config.executablepath);
416 if (config.loadordergroup) {
417 d_printf(_("\tLoad Order Group = %s\n"),
418 config.loadordergroup);
421 if (config.dependencies) {
422 d_printf(_("\tDependencies = %s\n"),
423 config.dependencies);
426 if (config.startname) {
427 d_printf(_("\tStart Name = %s\n"), config.startname);
430 if (config.displayname) {
431 d_printf(_("\tDisplay Name = %s\n"),
432 config.displayname);
435 done:
436 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
437 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
439 return werror_to_ntstatus(result);
442 /********************************************************************
443 ********************************************************************/
445 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
446 const struct dom_sid *domain_sid,
447 const char *domain_name,
448 struct cli_state *cli,
449 struct rpc_pipe_client *pipe_hnd,
450 TALLOC_CTX *mem_ctx,
451 int argc,
452 const char **argv )
454 struct policy_handle hSCM;
455 WERROR result = WERR_GENERAL_FAILURE;
456 NTSTATUS status;
457 fstring servicename;
459 if (argc != 1 ) {
460 d_printf("%s net rpc service status <service>\n", _("Usage:"));
461 return NT_STATUS_OK;
464 fstrcpy( servicename, argv[0] );
466 /* Open the Service Control Manager */
467 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
468 pipe_hnd->srv_name_slash,
469 NULL,
470 SC_RIGHT_MGR_ENUMERATE_SERVICE,
471 &hSCM,
472 &result);
473 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
474 d_fprintf(stderr,
475 _("Failed to open Service Control Manager. [%s]\n"),
476 win_errstr(result));
477 return werror_to_ntstatus(result);
480 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
481 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
483 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
485 return werror_to_ntstatus(result);
488 /********************************************************************
489 ********************************************************************/
491 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
492 const struct dom_sid *domain_sid,
493 const char *domain_name,
494 struct cli_state *cli,
495 struct rpc_pipe_client *pipe_hnd,
496 TALLOC_CTX *mem_ctx,
497 int argc,
498 const char **argv )
500 struct policy_handle hSCM;
501 WERROR result = WERR_GENERAL_FAILURE;
502 NTSTATUS status;
503 fstring servicename;
505 if (argc != 1 ) {
506 d_printf("%s net rpc service status <service>\n", _("Usage:"));
507 return NT_STATUS_OK;
510 fstrcpy( servicename, argv[0] );
512 /* Open the Service Control Manager */
513 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
514 pipe_hnd->srv_name_slash,
515 NULL,
516 SC_RIGHT_MGR_ENUMERATE_SERVICE,
517 &hSCM,
518 &result);
519 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
520 d_fprintf(stderr,
521 _("Failed to open Service Control Manager. [%s]\n"),
522 win_errstr(result));
523 return werror_to_ntstatus(result);
526 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
527 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
529 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
531 return werror_to_ntstatus(result);
534 /********************************************************************
535 ********************************************************************/
537 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
538 const struct dom_sid *domain_sid,
539 const char *domain_name,
540 struct cli_state *cli,
541 struct rpc_pipe_client *pipe_hnd,
542 TALLOC_CTX *mem_ctx,
543 int argc,
544 const char **argv )
546 struct policy_handle hSCM;
547 WERROR result = WERR_GENERAL_FAILURE;
548 NTSTATUS status;
549 fstring servicename;
551 if (argc != 1 ) {
552 d_printf("%s net rpc service status <service>\n", _("Usage:"));
553 return NT_STATUS_OK;
556 fstrcpy( servicename, argv[0] );
558 /* Open the Service Control Manager */
559 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
560 pipe_hnd->srv_name_slash,
561 NULL,
562 SC_RIGHT_MGR_ENUMERATE_SERVICE,
563 &hSCM,
564 &result);
565 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
566 d_fprintf(stderr,
567 _("Failed to open Service Control Manager. [%s]\n"),
568 win_errstr(result));
569 return werror_to_ntstatus(result);
572 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
573 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
575 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
577 return werror_to_ntstatus(result);
580 /********************************************************************
581 ********************************************************************/
583 static NTSTATUS rpc_service_start_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, hService;
593 WERROR result = WERR_GENERAL_FAILURE;
594 NTSTATUS status;
595 uint32 state = 0;
597 if (argc != 1 ) {
598 d_printf("%s net rpc service status <service>\n", _("Usage:"));
599 return NT_STATUS_OK;
602 /* Open the Service Control Manager */
603 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
604 pipe_hnd->srv_name_slash,
605 NULL,
606 SC_RIGHT_MGR_ENUMERATE_SERVICE,
607 &hSCM,
608 &result);
609 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
610 d_fprintf(stderr,
611 _("Failed to open Service Control Manager. [%s]\n"),
612 win_errstr(result));
613 return werror_to_ntstatus(result);
616 /* Open the Service */
618 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
619 &hSCM,
620 argv[0],
621 SC_RIGHT_SVC_START,
622 &hService,
623 &result);
625 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
626 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
627 win_errstr(result));
628 goto done;
631 /* get the status */
633 status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
634 &hService,
636 NULL,
637 &result);
639 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
640 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
641 win_errstr(result));
642 goto done;
645 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
647 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
648 d_printf(_("Successfully started service: %s\n"),
649 argv[0] );
650 else
651 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
652 argv[0], win_errstr(result) );
654 done:
655 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
656 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
658 return werror_to_ntstatus(result);
661 /********************************************************************
662 ********************************************************************/
664 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
665 const struct dom_sid *domain_sid,
666 const char *domain_name,
667 struct cli_state *cli,
668 struct rpc_pipe_client *pipe_hnd,
669 TALLOC_CTX *mem_ctx,
670 int argc,
671 const char **argv)
673 struct policy_handle hSCM, hService;
674 WERROR result = WERR_GENERAL_FAILURE;
675 NTSTATUS status;
677 if (argc != 1 ) {
678 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
679 return NT_STATUS_OK;
682 /* Open the Service Control Manager */
683 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
684 pipe_hnd->srv_name_slash,
685 NULL,
686 SC_RIGHT_MGR_ENUMERATE_SERVICE,
687 &hSCM,
688 &result);
689 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
690 d_fprintf(stderr,
691 _("Failed to open Service Control Manager. [%s]\n"),
692 win_errstr(result));
693 return werror_to_ntstatus(result);
696 /* Open the Service */
698 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
699 &hSCM,
700 argv[0],
701 SERVICE_ALL_ACCESS,
702 &hService,
703 &result);
705 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
706 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
707 win_errstr(result));
708 goto done;
711 /* Delete the Service */
713 status = rpccli_svcctl_DeleteService(pipe_hnd, mem_ctx,
714 &hService,
715 &result);
717 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
718 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
719 win_errstr(result));
720 goto done;
723 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
725 done:
726 if (is_valid_policy_hnd(&hService)) {
727 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
729 if (is_valid_policy_hnd(&hSCM)) {
730 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
733 return werror_to_ntstatus(result);
736 /********************************************************************
737 ********************************************************************/
739 static NTSTATUS rpc_service_create_internal(struct net_context *c,
740 const struct dom_sid *domain_sid,
741 const char *domain_name,
742 struct cli_state *cli,
743 struct rpc_pipe_client *pipe_hnd,
744 TALLOC_CTX *mem_ctx,
745 int argc,
746 const char **argv)
748 struct policy_handle hSCM, hService;
749 WERROR result = WERR_GENERAL_FAILURE;
750 NTSTATUS status;
751 const char *ServiceName;
752 const char *DisplayName;
753 const char *binary_path;
755 if (argc != 3) {
756 d_printf("%s net rpc service create <service> "
757 "<displayname> <binarypath>\n", _("Usage:"));
758 return NT_STATUS_OK;
761 /* Open the Service Control Manager */
762 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
763 pipe_hnd->srv_name_slash,
764 NULL,
765 SC_RIGHT_MGR_CREATE_SERVICE,
766 &hSCM,
767 &result);
768 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
769 d_fprintf(stderr,
770 _("Failed to open Service Control Manager. [%s]\n"),
771 win_errstr(result));
772 return werror_to_ntstatus(result);
775 /* Create the service */
777 ServiceName = argv[0];
778 DisplayName = argv[1];
779 binary_path = argv[2];
781 status = rpccli_svcctl_CreateServiceW(pipe_hnd, mem_ctx,
782 &hSCM,
783 ServiceName,
784 DisplayName,
785 SERVICE_ALL_ACCESS,
786 SERVICE_TYPE_WIN32_OWN_PROCESS,
787 SVCCTL_DEMAND_START,
788 SVCCTL_SVC_ERROR_NORMAL,
789 binary_path,
790 NULL, /* LoadOrderGroupKey */
791 NULL, /* TagId */
792 NULL, /* dependencies */
793 0, /* dependencies_size */
794 NULL, /* service_start_name */
795 NULL, /* password */
796 0, /* password_size */
797 &hService,
798 &result);
800 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
801 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
802 win_errstr(result));
803 goto done;
806 d_printf(_("Successfully created Service: %s\n"), argv[0]);
808 done:
809 if (is_valid_policy_hnd(&hService)) {
810 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
812 if (is_valid_policy_hnd(&hSCM)) {
813 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
816 return werror_to_ntstatus(result);
819 /********************************************************************
820 ********************************************************************/
822 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
824 if (c->display_usage) {
825 d_printf( "%s\n"
826 "net rpc service list\n"
827 " %s\n",
828 _("Usage:"),
829 _("View configured Win32 services"));
830 return 0;
833 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
834 rpc_service_list_internal, argc, argv );
837 /********************************************************************
838 ********************************************************************/
840 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
842 if (c->display_usage) {
843 d_printf( "%s\n"
844 "net rpc service start <service>\n"
845 " %s\n",
846 _("Usage:"),
847 _("Start a Win32 service"));
848 return 0;
851 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
852 rpc_service_start_internal, argc, argv );
855 /********************************************************************
856 ********************************************************************/
858 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
860 if (c->display_usage) {
861 d_printf( "%s\n"
862 "net rpc service stop <service>\n"
863 " %s\n",
864 _("Usage:"),
865 _("Stop a Win32 service"));
866 return 0;
869 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
870 rpc_service_stop_internal, argc, argv );
873 /********************************************************************
874 ********************************************************************/
876 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
878 if (c->display_usage) {
879 d_printf( "%s\n"
880 "net rpc service resume <service>\n"
881 " %s\n",
882 _("Usage:"),
883 _("Resume a Win32 service"));
884 return 0;
887 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
888 rpc_service_resume_internal, argc, argv );
891 /********************************************************************
892 ********************************************************************/
894 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
896 if (c->display_usage) {
897 d_printf( "%s\n"
898 "net rpc service pause <service>\n"
899 " %s\n",
900 _("Usage:"),
901 _("Pause a Win32 service"));
902 return 0;
905 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
906 rpc_service_pause_internal, argc, argv );
909 /********************************************************************
910 ********************************************************************/
912 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
914 if (c->display_usage) {
915 d_printf( "%s\n"
916 "net rpc service status <service>\n"
917 " %s\n",
918 _("Usage:"),
919 _("Show the current status of a service"));
920 return 0;
923 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
924 rpc_service_status_internal, argc, argv );
927 /********************************************************************
928 ********************************************************************/
930 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
932 if (c->display_usage) {
933 d_printf( "%s\n"
934 "net rpc service delete <service>\n"
935 " %s\n",
936 _("Usage:"),
937 _("Delete a Win32 service"));
938 return 0;
941 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
942 rpc_service_delete_internal, argc, argv);
945 /********************************************************************
946 ********************************************************************/
948 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
950 if (c->display_usage) {
951 d_printf( "%s\n"
952 "net rpc service create <service>\n"
953 " %s\n",
954 _("Usage:"),
955 _("Create a Win32 service"));
956 return 0;
959 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
960 rpc_service_create_internal, argc, argv);
963 /********************************************************************
964 ********************************************************************/
966 int net_rpc_service(struct net_context *c, int argc, const char **argv)
968 struct functable func[] = {
970 "list",
971 rpc_service_list,
972 NET_TRANSPORT_RPC,
973 N_("View configured Win32 services"),
974 N_("net rpc service list\n"
975 " View configured Win32 services")
978 "start",
979 rpc_service_start,
980 NET_TRANSPORT_RPC,
981 N_("Start a service"),
982 N_("net rpc service start\n"
983 " Start a service")
986 "stop",
987 rpc_service_stop,
988 NET_TRANSPORT_RPC,
989 N_("Stop a service"),
990 N_("net rpc service stop\n"
991 " Stop a service")
994 "pause",
995 rpc_service_pause,
996 NET_TRANSPORT_RPC,
997 N_("Pause a service"),
998 N_("net rpc service pause\n"
999 " Pause a service")
1002 "resume",
1003 rpc_service_resume,
1004 NET_TRANSPORT_RPC,
1005 N_("Resume a paused service"),
1006 N_("net rpc service resume\n"
1007 " Resume a service")
1010 "status",
1011 rpc_service_status,
1012 NET_TRANSPORT_RPC,
1013 N_("View current status of a service"),
1014 N_("net rpc service status\n"
1015 " View current status of a service")
1018 "delete",
1019 rpc_service_delete,
1020 NET_TRANSPORT_RPC,
1021 N_("Delete a service"),
1022 N_("net rpc service delete\n"
1023 " Deletes a service")
1026 "create",
1027 rpc_service_create,
1028 NET_TRANSPORT_RPC,
1029 N_("Create a service"),
1030 N_("net rpc service create\n"
1031 " Creates a service")
1034 {NULL, NULL, 0, NULL, NULL}
1037 return net_run_function(c, argc, argv, "net rpc service",func);