WHATSNEW: Update changes since rc1.
[Samba/gbeck.git] / source3 / utils / net_rpc_service.c
blobc5d6954dfcf657c3317b5d0ecd38484b3f6a2099
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Gerald (Jerry) Carter 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "rpc_client/rpc_client.h"
22 #include "../librpc/gen_ndr/ndr_svcctl.h"
23 #include "../librpc/gen_ndr/ndr_svcctl_c.h"
25 struct svc_state_msg {
26 uint32 flag;
27 const char *message;
30 static struct svc_state_msg state_msg_table[] = {
31 { SVCCTL_STOPPED, N_("stopped") },
32 { SVCCTL_START_PENDING, N_("start pending") },
33 { SVCCTL_STOP_PENDING, N_("stop pending") },
34 { SVCCTL_RUNNING, N_("running") },
35 { SVCCTL_CONTINUE_PENDING, N_("resume pending") },
36 { SVCCTL_PAUSE_PENDING, N_("pause pending") },
37 { SVCCTL_PAUSED, N_("paused") },
38 { 0, NULL }
42 /********************************************************************
43 ********************************************************************/
44 const char *svc_status_string( uint32 state )
46 fstring msg;
47 int i;
49 fstr_sprintf( msg, _("Unknown State [%d]"), state );
51 for ( i=0; state_msg_table[i].message; i++ ) {
52 if ( state_msg_table[i].flag == state ) {
53 fstrcpy( msg, state_msg_table[i].message );
54 break;
58 return talloc_strdup(talloc_tos(), msg);
61 /********************************************************************
62 ********************************************************************/
64 static WERROR open_service(struct dcerpc_binding_handle *b,
65 TALLOC_CTX *mem_ctx,
66 struct policy_handle *hSCM,
67 const char *service,
68 uint32_t access_mask,
69 struct policy_handle *hService)
71 NTSTATUS status;
72 WERROR result;
74 status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
75 hSCM,
76 service,
77 access_mask,
78 hService,
79 &result);
80 if (!NT_STATUS_IS_OK(status)) {
81 result = ntstatus_to_werror(status);
82 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
83 nt_errstr(status));
84 return result;
86 if (!W_ERROR_IS_OK(result) ) {
87 d_fprintf(stderr, _("Failed to open service. [%s]\n"),
88 win_errstr(result));
89 return result;
92 return WERR_OK;
95 /********************************************************************
96 ********************************************************************/
98 static WERROR open_scm(struct dcerpc_binding_handle *b,
99 TALLOC_CTX *mem_ctx,
100 const char *server_name,
101 uint32_t access_mask,
102 struct policy_handle *hSCM)
104 NTSTATUS status;
105 WERROR result;
107 status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
108 server_name,
109 NULL,
110 access_mask,
111 hSCM,
112 &result);
113 if (!NT_STATUS_IS_OK(status)) {
114 result = ntstatus_to_werror(status);
115 d_fprintf(stderr,
116 _("Failed to open Service Control Manager. [%s]\n"),
117 nt_errstr(status));
118 return result;
120 if (!W_ERROR_IS_OK(result)) {
121 d_fprintf(stderr,
122 _("Failed to open Service Control Manager. [%s]\n"),
123 win_errstr(result));
124 return result;
127 return WERR_OK;
130 /********************************************************************
131 ********************************************************************/
133 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
134 TALLOC_CTX *mem_ctx,
135 struct policy_handle *hSCM,
136 const char *service,
137 uint32 *state )
139 struct policy_handle hService;
140 struct SERVICE_STATUS service_status;
141 WERROR result = WERR_GENERAL_FAILURE;
142 NTSTATUS status;
143 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
145 /* now cycle until the status is actually 'watch_state' */
147 result = open_service(b, mem_ctx, hSCM, service,
148 SC_RIGHT_SVC_QUERY_STATUS,
149 &hService);
150 if (!W_ERROR_IS_OK(result) ) {
151 return result;
154 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
155 &hService,
156 &service_status,
157 &result);
158 if (!NT_STATUS_IS_OK(status)) {
159 result = ntstatus_to_werror(status);
160 goto done;
162 if (!W_ERROR_IS_OK(result)) {
163 goto done;
166 *state = service_status.state;
168 done:
169 if (is_valid_policy_hnd(&hService)) {
170 WERROR _result;
171 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
174 return result;
177 /********************************************************************
178 ********************************************************************/
180 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
181 TALLOC_CTX *mem_ctx,
182 struct policy_handle *hSCM,
183 const char *service,
184 uint32 watch_state,
185 uint32 *final_state )
187 uint32 i;
188 uint32 state = 0;
189 WERROR result = WERR_GENERAL_FAILURE;
192 i = 0;
193 while ( (state != watch_state ) && i<30 ) {
194 /* get the status */
196 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
197 if ( !W_ERROR_IS_OK(result) ) {
198 break;
201 d_printf(".");
202 i++;
203 sys_usleep( 100 );
205 d_printf("\n");
207 *final_state = state;
209 return result;
212 /********************************************************************
213 ********************************************************************/
215 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
216 TALLOC_CTX *mem_ctx,
217 struct policy_handle *hSCM,
218 const char *service,
219 uint32 control,
220 uint32 watch_state )
222 struct policy_handle hService;
223 WERROR result = WERR_GENERAL_FAILURE;
224 NTSTATUS status;
225 struct SERVICE_STATUS service_status;
226 uint32 state = 0;
227 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
229 /* Open the Service */
231 result = open_service(b, mem_ctx, hSCM, service,
232 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
233 &hService);
234 if (!W_ERROR_IS_OK(result) ) {
235 return result;
238 /* get the status */
240 status = dcerpc_svcctl_ControlService(b, mem_ctx,
241 &hService,
242 control,
243 &service_status,
244 &result);
246 if (!NT_STATUS_IS_OK(status)) {
247 result = ntstatus_to_werror(status);
248 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
249 nt_errstr(status));
250 goto done;
252 if (!W_ERROR_IS_OK(result) ) {
253 d_fprintf(stderr, _("Control service request failed. [%s]\n"),
254 win_errstr(result));
255 goto done;
258 /* loop -- checking the state until we are where we want to be */
260 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
262 d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
264 done:
265 if (is_valid_policy_hnd(&hService)) {
266 WERROR _result;
267 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
270 return result;
273 /********************************************************************
274 ********************************************************************/
276 static NTSTATUS rpc_service_list_internal(struct net_context *c,
277 const struct dom_sid *domain_sid,
278 const char *domain_name,
279 struct cli_state *cli,
280 struct rpc_pipe_client *pipe_hnd,
281 TALLOC_CTX *mem_ctx,
282 int argc,
283 const char **argv )
285 struct policy_handle hSCM;
286 struct ENUM_SERVICE_STATUSW *services = NULL;
287 WERROR result = WERR_GENERAL_FAILURE;
288 NTSTATUS status;
289 int i;
290 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
292 uint8_t *buffer = NULL;
293 uint32_t buf_size = 0;
294 uint32_t bytes_needed = 0;
295 uint32_t num_services = 0;
296 uint32_t resume_handle = 0;
298 if (argc != 0 ) {
299 d_printf("%s net rpc service list\n", _("Usage:"));
300 return NT_STATUS_OK;
303 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
304 SC_RIGHT_MGR_ENUMERATE_SERVICE,
305 &hSCM);
306 if (!W_ERROR_IS_OK(result)) {
307 return werror_to_ntstatus(result);
310 do {
311 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
312 &hSCM,
313 SERVICE_TYPE_WIN32,
314 SERVICE_STATE_ALL,
315 buffer,
316 buf_size,
317 &bytes_needed,
318 &num_services,
319 &resume_handle,
320 &result);
322 if (!NT_STATUS_IS_OK(status)) {
323 d_fprintf(stderr,
324 _("Failed to enumerate services. [%s]\n"),
325 nt_errstr(status));
326 break;
329 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
330 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
331 buf_size = bytes_needed;
332 continue;
335 if (!W_ERROR_IS_OK(result)) {
336 status = werror_to_ntstatus(result);
337 d_fprintf(stderr,
338 _("Failed to enumerate services. [%s]\n"),
339 win_errstr(result));
340 break;
343 if ( num_services == 0 ) {
344 d_printf(_("No services returned\n"));
345 break;
349 enum ndr_err_code ndr_err;
350 DATA_BLOB blob;
351 struct ndr_pull *ndr;
353 blob.length = buf_size;
354 blob.data = talloc_steal(mem_ctx, buffer);
356 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
357 if (!services) {
358 status = NT_STATUS_NO_MEMORY;
359 break;
362 ndr = ndr_pull_init_blob(&blob, mem_ctx);
363 if (ndr == NULL) {
364 status = NT_STATUS_NO_MEMORY;
365 break;
368 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
369 ndr, num_services, services);
370 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
371 status = ndr_map_error2ntstatus(ndr_err);
372 break;
375 for ( i=0; i<num_services; i++ ) {
376 d_printf("%-20s \"%s\"\n",
377 services[i].service_name,
378 services[i].display_name);
382 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
384 if (is_valid_policy_hnd(&hSCM)) {
385 WERROR _result;
386 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
389 return status;
392 /********************************************************************
393 ********************************************************************/
395 static NTSTATUS rpc_service_status_internal(struct net_context *c,
396 const struct dom_sid *domain_sid,
397 const char *domain_name,
398 struct cli_state *cli,
399 struct rpc_pipe_client *pipe_hnd,
400 TALLOC_CTX *mem_ctx,
401 int argc,
402 const char **argv )
404 struct policy_handle hSCM, hService;
405 WERROR result = WERR_GENERAL_FAILURE;
406 NTSTATUS status;
407 struct SERVICE_STATUS service_status;
408 struct QUERY_SERVICE_CONFIG config;
409 uint32_t buf_size = sizeof(config);
410 uint32_t ret_size = 0;
411 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
413 if (argc != 1 ) {
414 d_printf("%s net rpc service status <service>\n", _("Usage:"));
415 return NT_STATUS_OK;
418 /* Open the Service Control Manager */
419 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
420 SC_RIGHT_MGR_ENUMERATE_SERVICE,
421 &hSCM);
422 if (!W_ERROR_IS_OK(result)) {
423 return werror_to_ntstatus(result);
426 /* Open the Service */
428 result = open_service(b, mem_ctx, &hSCM, argv[0],
429 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
430 &hService);
431 if (!W_ERROR_IS_OK(result) ) {
432 goto done;
435 /* get the status */
437 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
438 &hService,
439 &service_status,
440 &result);
441 if (!NT_STATUS_IS_OK(status)) {
442 result = ntstatus_to_werror(status);
443 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
444 nt_errstr(status));
445 goto done;
448 if (!W_ERROR_IS_OK(result) ) {
449 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
450 win_errstr(result));
451 goto done;
454 d_printf(_("%s service is %s.\n"), argv[0],
455 svc_status_string(service_status.state));
457 /* get the config */
459 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
460 &hService,
461 &config,
462 buf_size,
463 &ret_size,
464 &result);
465 if (!NT_STATUS_IS_OK(status)) {
466 result = ntstatus_to_werror(status);
467 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
468 nt_errstr(status));
469 goto done;
472 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
473 buf_size = ret_size;
474 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
475 &hService,
476 &config,
477 buf_size,
478 &ret_size,
479 &result);
480 if (!NT_STATUS_IS_OK(status)) {
481 result = ntstatus_to_werror(status);
482 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
483 nt_errstr(status));
484 goto done;
488 if (!W_ERROR_IS_OK(result) ) {
489 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
490 win_errstr(result));
491 goto done;
494 /* print out the configuration information for the service */
496 d_printf(_("Configuration details:\n"));
497 d_printf(_("\tControls Accepted = 0x%x\n"),
498 service_status.controls_accepted);
499 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
500 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
501 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
502 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
504 if (config.executablepath) {
505 d_printf(_("\tExecutable Path = %s\n"),
506 config.executablepath);
509 if (config.loadordergroup) {
510 d_printf(_("\tLoad Order Group = %s\n"),
511 config.loadordergroup);
514 if (config.dependencies) {
515 d_printf(_("\tDependencies = %s\n"),
516 config.dependencies);
519 if (config.startname) {
520 d_printf(_("\tStart Name = %s\n"), config.startname);
523 if (config.displayname) {
524 d_printf(_("\tDisplay Name = %s\n"),
525 config.displayname);
528 done:
529 if (is_valid_policy_hnd(&hService)) {
530 WERROR _result;
531 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
533 if (is_valid_policy_hnd(&hSCM)) {
534 WERROR _result;
535 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
538 return werror_to_ntstatus(result);
541 /********************************************************************
542 ********************************************************************/
544 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
545 const struct dom_sid *domain_sid,
546 const char *domain_name,
547 struct cli_state *cli,
548 struct rpc_pipe_client *pipe_hnd,
549 TALLOC_CTX *mem_ctx,
550 int argc,
551 const char **argv )
553 struct policy_handle hSCM;
554 WERROR result = WERR_GENERAL_FAILURE;
555 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
557 if (argc != 1 ) {
558 d_printf("%s net rpc service status <service>\n", _("Usage:"));
559 return NT_STATUS_OK;
562 /* Open the Service Control Manager */
563 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
564 SC_RIGHT_MGR_ENUMERATE_SERVICE,
565 &hSCM);
566 if (!W_ERROR_IS_OK(result)) {
567 return werror_to_ntstatus(result);
570 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
571 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
573 if (is_valid_policy_hnd(&hSCM)) {
574 WERROR _result;
575 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
578 return werror_to_ntstatus(result);
581 /********************************************************************
582 ********************************************************************/
584 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
585 const struct dom_sid *domain_sid,
586 const char *domain_name,
587 struct cli_state *cli,
588 struct rpc_pipe_client *pipe_hnd,
589 TALLOC_CTX *mem_ctx,
590 int argc,
591 const char **argv )
593 struct policy_handle hSCM;
594 WERROR result = WERR_GENERAL_FAILURE;
595 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
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 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
604 SC_RIGHT_MGR_ENUMERATE_SERVICE,
605 &hSCM);
606 if (!W_ERROR_IS_OK(result)) {
607 return werror_to_ntstatus(result);
610 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
611 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
613 if (is_valid_policy_hnd(&hSCM)) {
614 WERROR _result;
615 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
618 return werror_to_ntstatus(result);
621 /********************************************************************
622 ********************************************************************/
624 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
625 const struct dom_sid *domain_sid,
626 const char *domain_name,
627 struct cli_state *cli,
628 struct rpc_pipe_client *pipe_hnd,
629 TALLOC_CTX *mem_ctx,
630 int argc,
631 const char **argv )
633 struct policy_handle hSCM;
634 WERROR result = WERR_GENERAL_FAILURE;
635 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
637 if (argc != 1 ) {
638 d_printf("%s net rpc service status <service>\n", _("Usage:"));
639 return NT_STATUS_OK;
642 /* Open the Service Control Manager */
643 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
644 SC_RIGHT_MGR_ENUMERATE_SERVICE,
645 &hSCM);
646 if (!W_ERROR_IS_OK(result)) {
647 return werror_to_ntstatus(result);
650 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
651 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
653 if (is_valid_policy_hnd(&hSCM)) {
654 WERROR _result;
655 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
658 return werror_to_ntstatus(result);
661 /********************************************************************
662 ********************************************************************/
664 static NTSTATUS rpc_service_start_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;
676 uint32 state = 0;
677 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
679 if (argc != 1 ) {
680 d_printf("%s net rpc service status <service>\n", _("Usage:"));
681 return NT_STATUS_OK;
684 /* Open the Service Control Manager */
685 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
686 SC_RIGHT_MGR_ENUMERATE_SERVICE,
687 &hSCM);
688 if (!W_ERROR_IS_OK(result)) {
689 return werror_to_ntstatus(result);
693 /* Open the Service */
695 result = open_service(b, mem_ctx, &hSCM, argv[0],
696 SC_RIGHT_SVC_START,
697 &hService);
698 if (!W_ERROR_IS_OK(result) ) {
699 goto done;
702 /* get the status */
704 status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
705 &hService,
707 NULL,
708 &result);
710 if (!NT_STATUS_IS_OK(status)) {
711 result = ntstatus_to_werror(status);
712 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
713 nt_errstr(status));
714 goto done;
716 if (!W_ERROR_IS_OK(result) ) {
717 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
718 win_errstr(result));
719 goto done;
722 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
724 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
725 d_printf(_("Successfully started service: %s\n"),
726 argv[0] );
727 else
728 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
729 argv[0], win_errstr(result) );
731 done:
732 if (is_valid_policy_hnd(&hService)) {
733 WERROR _result;
734 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
736 if (is_valid_policy_hnd(&hSCM)) {
737 WERROR _result;
738 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
741 return werror_to_ntstatus(result);
744 /********************************************************************
745 ********************************************************************/
747 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
748 const struct dom_sid *domain_sid,
749 const char *domain_name,
750 struct cli_state *cli,
751 struct rpc_pipe_client *pipe_hnd,
752 TALLOC_CTX *mem_ctx,
753 int argc,
754 const char **argv)
756 struct policy_handle hSCM, hService;
757 WERROR result = WERR_GENERAL_FAILURE;
758 NTSTATUS status;
759 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
761 if (argc != 1 ) {
762 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
763 return NT_STATUS_OK;
766 ZERO_STRUCT(hSCM);
767 ZERO_STRUCT(hService);
769 /* Open the Service Control Manager */
770 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
771 SC_RIGHT_MGR_ENUMERATE_SERVICE,
772 &hSCM);
773 if (!W_ERROR_IS_OK(result)) {
774 return werror_to_ntstatus(result);
777 /* Open the Service */
779 result = open_service(b, mem_ctx, &hSCM, argv[0],
780 SERVICE_ALL_ACCESS,
781 &hService);
782 if (!W_ERROR_IS_OK(result) ) {
783 goto done;
786 /* Delete the Service */
788 status = dcerpc_svcctl_DeleteService(b, mem_ctx,
789 &hService,
790 &result);
792 if (!NT_STATUS_IS_OK(status)) {
793 result = ntstatus_to_werror(status);
794 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
795 nt_errstr(status));
796 goto done;
798 if (!W_ERROR_IS_OK(result)) {
799 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
800 win_errstr(result));
801 goto done;
804 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
806 done:
807 if (is_valid_policy_hnd(&hService)) {
808 WERROR _result;
809 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
811 if (is_valid_policy_hnd(&hSCM)) {
812 WERROR _result;
813 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
816 return werror_to_ntstatus(result);
819 /********************************************************************
820 ********************************************************************/
822 static NTSTATUS rpc_service_create_internal(struct net_context *c,
823 const struct dom_sid *domain_sid,
824 const char *domain_name,
825 struct cli_state *cli,
826 struct rpc_pipe_client *pipe_hnd,
827 TALLOC_CTX *mem_ctx,
828 int argc,
829 const char **argv)
831 struct policy_handle hSCM, hService;
832 WERROR result = WERR_GENERAL_FAILURE;
833 NTSTATUS status;
834 const char *ServiceName;
835 const char *DisplayName;
836 const char *binary_path;
837 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
839 if (argc != 3) {
840 d_printf("%s net rpc service create <service> "
841 "<displayname> <binarypath>\n", _("Usage:"));
842 return NT_STATUS_OK;
845 ZERO_STRUCT(hSCM);
846 ZERO_STRUCT(hService);
848 /* Open the Service Control Manager */
849 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
850 SC_RIGHT_MGR_CREATE_SERVICE,
851 &hSCM);
852 if (!W_ERROR_IS_OK(result)) {
853 return werror_to_ntstatus(result);
856 /* Create the service */
858 ServiceName = argv[0];
859 DisplayName = argv[1];
860 binary_path = argv[2];
862 status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
863 &hSCM,
864 ServiceName,
865 DisplayName,
866 SERVICE_ALL_ACCESS,
867 SERVICE_TYPE_WIN32_OWN_PROCESS,
868 SVCCTL_DEMAND_START,
869 SVCCTL_SVC_ERROR_NORMAL,
870 binary_path,
871 NULL, /* LoadOrderGroupKey */
872 NULL, /* TagId */
873 NULL, /* dependencies */
874 0, /* dependencies_size */
875 NULL, /* service_start_name */
876 NULL, /* password */
877 0, /* password_size */
878 &hService,
879 &result);
880 if (!NT_STATUS_IS_OK(status)) {
881 result = ntstatus_to_werror(status);
882 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
883 nt_errstr(status));
884 goto done;
886 if (!W_ERROR_IS_OK(result)) {
887 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
888 win_errstr(result));
889 goto done;
892 d_printf(_("Successfully created Service: %s\n"), argv[0]);
894 done:
895 if (is_valid_policy_hnd(&hService)) {
896 WERROR _result;
897 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
899 if (is_valid_policy_hnd(&hSCM)) {
900 WERROR _result;
901 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
904 return werror_to_ntstatus(result);
907 /********************************************************************
908 ********************************************************************/
910 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
912 if (c->display_usage) {
913 d_printf( "%s\n"
914 "net rpc service list\n"
915 " %s\n",
916 _("Usage:"),
917 _("View configured Win32 services"));
918 return 0;
921 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
922 rpc_service_list_internal, argc, argv );
925 /********************************************************************
926 ********************************************************************/
928 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
930 if (c->display_usage) {
931 d_printf( "%s\n"
932 "net rpc service start <service>\n"
933 " %s\n",
934 _("Usage:"),
935 _("Start a Win32 service"));
936 return 0;
939 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
940 rpc_service_start_internal, argc, argv );
943 /********************************************************************
944 ********************************************************************/
946 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
948 if (c->display_usage) {
949 d_printf( "%s\n"
950 "net rpc service stop <service>\n"
951 " %s\n",
952 _("Usage:"),
953 _("Stop a Win32 service"));
954 return 0;
957 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
958 rpc_service_stop_internal, argc, argv );
961 /********************************************************************
962 ********************************************************************/
964 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
966 if (c->display_usage) {
967 d_printf( "%s\n"
968 "net rpc service resume <service>\n"
969 " %s\n",
970 _("Usage:"),
971 _("Resume a Win32 service"));
972 return 0;
975 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
976 rpc_service_resume_internal, argc, argv );
979 /********************************************************************
980 ********************************************************************/
982 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
984 if (c->display_usage) {
985 d_printf( "%s\n"
986 "net rpc service pause <service>\n"
987 " %s\n",
988 _("Usage:"),
989 _("Pause a Win32 service"));
990 return 0;
993 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
994 rpc_service_pause_internal, argc, argv );
997 /********************************************************************
998 ********************************************************************/
1000 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
1002 if (c->display_usage) {
1003 d_printf( "%s\n"
1004 "net rpc service status <service>\n"
1005 " %s\n",
1006 _("Usage:"),
1007 _("Show the current status of a service"));
1008 return 0;
1011 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1012 rpc_service_status_internal, argc, argv );
1015 /********************************************************************
1016 ********************************************************************/
1018 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
1020 if (c->display_usage) {
1021 d_printf( "%s\n"
1022 "net rpc service delete <service>\n"
1023 " %s\n",
1024 _("Usage:"),
1025 _("Delete a Win32 service"));
1026 return 0;
1029 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1030 rpc_service_delete_internal, argc, argv);
1033 /********************************************************************
1034 ********************************************************************/
1036 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
1038 if (c->display_usage) {
1039 d_printf( "%s\n"
1040 "net rpc service create <service>\n"
1041 " %s\n",
1042 _("Usage:"),
1043 _("Create a Win32 service"));
1044 return 0;
1047 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1048 rpc_service_create_internal, argc, argv);
1051 /********************************************************************
1052 ********************************************************************/
1054 int net_rpc_service(struct net_context *c, int argc, const char **argv)
1056 struct functable func[] = {
1058 "list",
1059 rpc_service_list,
1060 NET_TRANSPORT_RPC,
1061 N_("View configured Win32 services"),
1062 N_("net rpc service list\n"
1063 " View configured Win32 services")
1066 "start",
1067 rpc_service_start,
1068 NET_TRANSPORT_RPC,
1069 N_("Start a service"),
1070 N_("net rpc service start\n"
1071 " Start a service")
1074 "stop",
1075 rpc_service_stop,
1076 NET_TRANSPORT_RPC,
1077 N_("Stop a service"),
1078 N_("net rpc service stop\n"
1079 " Stop a service")
1082 "pause",
1083 rpc_service_pause,
1084 NET_TRANSPORT_RPC,
1085 N_("Pause a service"),
1086 N_("net rpc service pause\n"
1087 " Pause a service")
1090 "resume",
1091 rpc_service_resume,
1092 NET_TRANSPORT_RPC,
1093 N_("Resume a paused service"),
1094 N_("net rpc service resume\n"
1095 " Resume a service")
1098 "status",
1099 rpc_service_status,
1100 NET_TRANSPORT_RPC,
1101 N_("View current status of a service"),
1102 N_("net rpc service status\n"
1103 " View current status of a service")
1106 "delete",
1107 rpc_service_delete,
1108 NET_TRANSPORT_RPC,
1109 N_("Delete a service"),
1110 N_("net rpc service delete\n"
1111 " Deletes a service")
1114 "create",
1115 rpc_service_create,
1116 NET_TRANSPORT_RPC,
1117 N_("Create a service"),
1118 N_("net rpc service create\n"
1119 " Creates a service")
1122 {NULL, NULL, 0, NULL, NULL}
1125 return net_run_function(c, argc, argv, "net rpc service",func);