s4-resolve: Add lmhosts support into the source4 name resolve code
[Samba.git] / source3 / utils / net_rpc_service.c
blob0c0995a1dcb2dbe26139f23f3962e8b474466e70
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 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;
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 buffer = talloc_array(mem_ctx, uint8_t, buf_size);
311 if (buffer == NULL) {
312 status = NT_STATUS_NO_MEMORY;
313 goto done;
316 do {
317 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
318 &hSCM,
319 SERVICE_TYPE_WIN32,
320 SERVICE_STATE_ALL,
321 buffer,
322 buf_size,
323 &bytes_needed,
324 &num_services,
325 &resume_handle,
326 &result);
328 if (!NT_STATUS_IS_OK(status)) {
329 d_fprintf(stderr,
330 _("Failed to enumerate services. [%s]\n"),
331 nt_errstr(status));
332 break;
335 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
336 buf_size = bytes_needed;
337 buffer = talloc_realloc(mem_ctx, buffer, uint8_t, bytes_needed);
338 if (buffer == NULL) {
339 status = NT_STATUS_NO_MEMORY;
340 break;
342 continue;
345 if (!W_ERROR_IS_OK(result)) {
346 status = werror_to_ntstatus(result);
347 d_fprintf(stderr,
348 _("Failed to enumerate services. [%s]\n"),
349 win_errstr(result));
350 break;
353 if ( num_services == 0 ) {
354 d_printf(_("No services returned\n"));
355 break;
359 enum ndr_err_code ndr_err;
360 DATA_BLOB blob;
361 struct ndr_pull *ndr;
363 blob.length = buf_size;
364 blob.data = talloc_steal(mem_ctx, buffer);
366 services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
367 if (!services) {
368 status = NT_STATUS_NO_MEMORY;
369 break;
372 ndr = ndr_pull_init_blob(&blob, mem_ctx);
373 if (ndr == NULL) {
374 status = NT_STATUS_NO_MEMORY;
375 break;
378 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
379 ndr, num_services, services);
380 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
381 status = ndr_map_error2ntstatus(ndr_err);
382 break;
385 for ( i=0; i<num_services; i++ ) {
386 d_printf("%-20s \"%s\"\n",
387 services[i].service_name,
388 services[i].display_name);
392 } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
394 done:
395 if (is_valid_policy_hnd(&hSCM)) {
396 WERROR _result;
397 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
400 return status;
403 /********************************************************************
404 ********************************************************************/
406 static NTSTATUS rpc_service_status_internal(struct net_context *c,
407 const struct dom_sid *domain_sid,
408 const char *domain_name,
409 struct cli_state *cli,
410 struct rpc_pipe_client *pipe_hnd,
411 TALLOC_CTX *mem_ctx,
412 int argc,
413 const char **argv )
415 struct policy_handle hSCM, hService;
416 WERROR result = WERR_GENERAL_FAILURE;
417 NTSTATUS status;
418 struct SERVICE_STATUS service_status;
419 struct QUERY_SERVICE_CONFIG config;
420 uint32_t buf_size = sizeof(config);
421 uint32_t ret_size = 0;
422 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
424 if (argc != 1 ) {
425 d_printf("%s net rpc service status <service>\n", _("Usage:"));
426 return NT_STATUS_OK;
429 /* Open the Service Control Manager */
430 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
431 SC_RIGHT_MGR_ENUMERATE_SERVICE,
432 &hSCM);
433 if (!W_ERROR_IS_OK(result)) {
434 return werror_to_ntstatus(result);
437 /* Open the Service */
439 result = open_service(b, mem_ctx, &hSCM, argv[0],
440 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
441 &hService);
442 if (!W_ERROR_IS_OK(result) ) {
443 goto done;
446 /* get the status */
448 status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
449 &hService,
450 &service_status,
451 &result);
452 if (!NT_STATUS_IS_OK(status)) {
453 result = ntstatus_to_werror(status);
454 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
455 nt_errstr(status));
456 goto done;
459 if (!W_ERROR_IS_OK(result) ) {
460 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
461 win_errstr(result));
462 goto done;
465 d_printf(_("%s service is %s.\n"), argv[0],
466 svc_status_string(service_status.state));
468 /* get the config */
470 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
471 &hService,
472 &config,
473 buf_size,
474 &ret_size,
475 &result);
476 if (!NT_STATUS_IS_OK(status)) {
477 result = ntstatus_to_werror(status);
478 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
479 nt_errstr(status));
480 goto done;
483 if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
484 buf_size = ret_size;
485 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
486 &hService,
487 &config,
488 buf_size,
489 &ret_size,
490 &result);
491 if (!NT_STATUS_IS_OK(status)) {
492 result = ntstatus_to_werror(status);
493 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
494 nt_errstr(status));
495 goto done;
499 if (!W_ERROR_IS_OK(result) ) {
500 d_fprintf(stderr, _("Query config request failed. [%s]\n"),
501 win_errstr(result));
502 goto done;
505 /* print out the configuration information for the service */
507 d_printf(_("Configuration details:\n"));
508 d_printf(_("\tControls Accepted = 0x%x\n"),
509 service_status.controls_accepted);
510 d_printf(_("\tService Type = 0x%x\n"), config.service_type);
511 d_printf(_("\tStart Type = 0x%x\n"), config.start_type);
512 d_printf(_("\tError Control = 0x%x\n"), config.error_control);
513 d_printf(_("\tTag ID = 0x%x\n"), config.tag_id);
515 if (config.executablepath) {
516 d_printf(_("\tExecutable Path = %s\n"),
517 config.executablepath);
520 if (config.loadordergroup) {
521 d_printf(_("\tLoad Order Group = %s\n"),
522 config.loadordergroup);
525 if (config.dependencies) {
526 d_printf(_("\tDependencies = %s\n"),
527 config.dependencies);
530 if (config.startname) {
531 d_printf(_("\tStart Name = %s\n"), config.startname);
534 if (config.displayname) {
535 d_printf(_("\tDisplay Name = %s\n"),
536 config.displayname);
539 done:
540 if (is_valid_policy_hnd(&hService)) {
541 WERROR _result;
542 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
544 if (is_valid_policy_hnd(&hSCM)) {
545 WERROR _result;
546 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
549 return werror_to_ntstatus(result);
552 /********************************************************************
553 ********************************************************************/
555 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
556 const struct dom_sid *domain_sid,
557 const char *domain_name,
558 struct cli_state *cli,
559 struct rpc_pipe_client *pipe_hnd,
560 TALLOC_CTX *mem_ctx,
561 int argc,
562 const char **argv )
564 struct policy_handle hSCM;
565 WERROR result = WERR_GENERAL_FAILURE;
566 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
568 if (argc != 1 ) {
569 d_printf("%s net rpc service status <service>\n", _("Usage:"));
570 return NT_STATUS_OK;
573 /* Open the Service Control Manager */
574 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
575 SC_RIGHT_MGR_ENUMERATE_SERVICE,
576 &hSCM);
577 if (!W_ERROR_IS_OK(result)) {
578 return werror_to_ntstatus(result);
581 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
582 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
584 if (is_valid_policy_hnd(&hSCM)) {
585 WERROR _result;
586 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
589 return werror_to_ntstatus(result);
592 /********************************************************************
593 ********************************************************************/
595 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
596 const struct dom_sid *domain_sid,
597 const char *domain_name,
598 struct cli_state *cli,
599 struct rpc_pipe_client *pipe_hnd,
600 TALLOC_CTX *mem_ctx,
601 int argc,
602 const char **argv )
604 struct policy_handle hSCM;
605 WERROR result = WERR_GENERAL_FAILURE;
606 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
608 if (argc != 1 ) {
609 d_printf("%s net rpc service status <service>\n", _("Usage:"));
610 return NT_STATUS_OK;
613 /* Open the Service Control Manager */
614 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
615 SC_RIGHT_MGR_ENUMERATE_SERVICE,
616 &hSCM);
617 if (!W_ERROR_IS_OK(result)) {
618 return werror_to_ntstatus(result);
621 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
622 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
624 if (is_valid_policy_hnd(&hSCM)) {
625 WERROR _result;
626 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
629 return werror_to_ntstatus(result);
632 /********************************************************************
633 ********************************************************************/
635 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
636 const struct dom_sid *domain_sid,
637 const char *domain_name,
638 struct cli_state *cli,
639 struct rpc_pipe_client *pipe_hnd,
640 TALLOC_CTX *mem_ctx,
641 int argc,
642 const char **argv )
644 struct policy_handle hSCM;
645 WERROR result = WERR_GENERAL_FAILURE;
646 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
648 if (argc != 1 ) {
649 d_printf("%s net rpc service status <service>\n", _("Usage:"));
650 return NT_STATUS_OK;
653 /* Open the Service Control Manager */
654 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
655 SC_RIGHT_MGR_ENUMERATE_SERVICE,
656 &hSCM);
657 if (!W_ERROR_IS_OK(result)) {
658 return werror_to_ntstatus(result);
661 result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
662 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
664 if (is_valid_policy_hnd(&hSCM)) {
665 WERROR _result;
666 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
669 return werror_to_ntstatus(result);
672 /********************************************************************
673 ********************************************************************/
675 static NTSTATUS rpc_service_start_internal(struct net_context *c,
676 const struct dom_sid *domain_sid,
677 const char *domain_name,
678 struct cli_state *cli,
679 struct rpc_pipe_client *pipe_hnd,
680 TALLOC_CTX *mem_ctx,
681 int argc,
682 const char **argv )
684 struct policy_handle hSCM, hService;
685 WERROR result = WERR_GENERAL_FAILURE;
686 NTSTATUS status;
687 uint32 state = 0;
688 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
690 if (argc != 1 ) {
691 d_printf("%s net rpc service status <service>\n", _("Usage:"));
692 return NT_STATUS_OK;
695 /* Open the Service Control Manager */
696 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
697 SC_RIGHT_MGR_ENUMERATE_SERVICE,
698 &hSCM);
699 if (!W_ERROR_IS_OK(result)) {
700 return werror_to_ntstatus(result);
704 /* Open the Service */
706 result = open_service(b, mem_ctx, &hSCM, argv[0],
707 SC_RIGHT_SVC_START,
708 &hService);
709 if (!W_ERROR_IS_OK(result) ) {
710 goto done;
713 /* get the status */
715 status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
716 &hService,
718 NULL,
719 &result);
721 if (!NT_STATUS_IS_OK(status)) {
722 result = ntstatus_to_werror(status);
723 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
724 nt_errstr(status));
725 goto done;
727 if (!W_ERROR_IS_OK(result) ) {
728 d_fprintf(stderr, _("Query status request failed. [%s]\n"),
729 win_errstr(result));
730 goto done;
733 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
735 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
736 d_printf(_("Successfully started service: %s\n"),
737 argv[0] );
738 else
739 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
740 argv[0], win_errstr(result) );
742 done:
743 if (is_valid_policy_hnd(&hService)) {
744 WERROR _result;
745 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
747 if (is_valid_policy_hnd(&hSCM)) {
748 WERROR _result;
749 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
752 return werror_to_ntstatus(result);
755 /********************************************************************
756 ********************************************************************/
758 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
759 const struct dom_sid *domain_sid,
760 const char *domain_name,
761 struct cli_state *cli,
762 struct rpc_pipe_client *pipe_hnd,
763 TALLOC_CTX *mem_ctx,
764 int argc,
765 const char **argv)
767 struct policy_handle hSCM, hService;
768 WERROR result = WERR_GENERAL_FAILURE;
769 NTSTATUS status;
770 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
772 if (argc != 1 ) {
773 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
774 return NT_STATUS_OK;
777 ZERO_STRUCT(hSCM);
778 ZERO_STRUCT(hService);
780 /* Open the Service Control Manager */
781 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
782 SC_RIGHT_MGR_ENUMERATE_SERVICE,
783 &hSCM);
784 if (!W_ERROR_IS_OK(result)) {
785 return werror_to_ntstatus(result);
788 /* Open the Service */
790 result = open_service(b, mem_ctx, &hSCM, argv[0],
791 SERVICE_ALL_ACCESS,
792 &hService);
793 if (!W_ERROR_IS_OK(result) ) {
794 goto done;
797 /* Delete the Service */
799 status = dcerpc_svcctl_DeleteService(b, mem_ctx,
800 &hService,
801 &result);
803 if (!NT_STATUS_IS_OK(status)) {
804 result = ntstatus_to_werror(status);
805 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
806 nt_errstr(status));
807 goto done;
809 if (!W_ERROR_IS_OK(result)) {
810 d_fprintf(stderr, _("Delete service request failed. [%s]\n"),
811 win_errstr(result));
812 goto done;
815 d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
817 done:
818 if (is_valid_policy_hnd(&hService)) {
819 WERROR _result;
820 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
822 if (is_valid_policy_hnd(&hSCM)) {
823 WERROR _result;
824 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
827 return werror_to_ntstatus(result);
830 /********************************************************************
831 ********************************************************************/
833 static NTSTATUS rpc_service_create_internal(struct net_context *c,
834 const struct dom_sid *domain_sid,
835 const char *domain_name,
836 struct cli_state *cli,
837 struct rpc_pipe_client *pipe_hnd,
838 TALLOC_CTX *mem_ctx,
839 int argc,
840 const char **argv)
842 struct policy_handle hSCM, hService;
843 WERROR result = WERR_GENERAL_FAILURE;
844 NTSTATUS status;
845 const char *ServiceName;
846 const char *DisplayName;
847 const char *binary_path;
848 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
850 if (argc != 3) {
851 d_printf("%s net rpc service create <service> "
852 "<displayname> <binarypath>\n", _("Usage:"));
853 return NT_STATUS_OK;
856 ZERO_STRUCT(hSCM);
857 ZERO_STRUCT(hService);
859 /* Open the Service Control Manager */
860 result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
861 SC_RIGHT_MGR_CREATE_SERVICE,
862 &hSCM);
863 if (!W_ERROR_IS_OK(result)) {
864 return werror_to_ntstatus(result);
867 /* Create the service */
869 ServiceName = argv[0];
870 DisplayName = argv[1];
871 binary_path = argv[2];
873 status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
874 &hSCM,
875 ServiceName,
876 DisplayName,
877 SERVICE_ALL_ACCESS,
878 SERVICE_TYPE_WIN32_OWN_PROCESS,
879 SVCCTL_DEMAND_START,
880 SVCCTL_SVC_ERROR_NORMAL,
881 binary_path,
882 NULL, /* LoadOrderGroupKey */
883 NULL, /* TagId */
884 NULL, /* dependencies */
885 0, /* dependencies_size */
886 NULL, /* service_start_name */
887 NULL, /* password */
888 0, /* password_size */
889 &hService,
890 &result);
891 if (!NT_STATUS_IS_OK(status)) {
892 result = ntstatus_to_werror(status);
893 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
894 nt_errstr(status));
895 goto done;
897 if (!W_ERROR_IS_OK(result)) {
898 d_fprintf(stderr, _("Create service request failed. [%s]\n"),
899 win_errstr(result));
900 goto done;
903 d_printf(_("Successfully created Service: %s\n"), argv[0]);
905 done:
906 if (is_valid_policy_hnd(&hService)) {
907 WERROR _result;
908 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
910 if (is_valid_policy_hnd(&hSCM)) {
911 WERROR _result;
912 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
915 return werror_to_ntstatus(result);
918 /********************************************************************
919 ********************************************************************/
921 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
923 if (c->display_usage) {
924 d_printf( "%s\n"
925 "net rpc service list\n"
926 " %s\n",
927 _("Usage:"),
928 _("View configured Win32 services"));
929 return 0;
932 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
933 rpc_service_list_internal, argc, argv );
936 /********************************************************************
937 ********************************************************************/
939 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
941 if (c->display_usage) {
942 d_printf( "%s\n"
943 "net rpc service start <service>\n"
944 " %s\n",
945 _("Usage:"),
946 _("Start a Win32 service"));
947 return 0;
950 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
951 rpc_service_start_internal, argc, argv );
954 /********************************************************************
955 ********************************************************************/
957 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
959 if (c->display_usage) {
960 d_printf( "%s\n"
961 "net rpc service stop <service>\n"
962 " %s\n",
963 _("Usage:"),
964 _("Stop a Win32 service"));
965 return 0;
968 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
969 rpc_service_stop_internal, argc, argv );
972 /********************************************************************
973 ********************************************************************/
975 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
977 if (c->display_usage) {
978 d_printf( "%s\n"
979 "net rpc service resume <service>\n"
980 " %s\n",
981 _("Usage:"),
982 _("Resume a Win32 service"));
983 return 0;
986 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
987 rpc_service_resume_internal, argc, argv );
990 /********************************************************************
991 ********************************************************************/
993 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
995 if (c->display_usage) {
996 d_printf( "%s\n"
997 "net rpc service pause <service>\n"
998 " %s\n",
999 _("Usage:"),
1000 _("Pause a Win32 service"));
1001 return 0;
1004 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1005 rpc_service_pause_internal, argc, argv );
1008 /********************************************************************
1009 ********************************************************************/
1011 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
1013 if (c->display_usage) {
1014 d_printf( "%s\n"
1015 "net rpc service status <service>\n"
1016 " %s\n",
1017 _("Usage:"),
1018 _("Show the current status of a service"));
1019 return 0;
1022 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1023 rpc_service_status_internal, argc, argv );
1026 /********************************************************************
1027 ********************************************************************/
1029 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
1031 if (c->display_usage) {
1032 d_printf( "%s\n"
1033 "net rpc service delete <service>\n"
1034 " %s\n",
1035 _("Usage:"),
1036 _("Delete a Win32 service"));
1037 return 0;
1040 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1041 rpc_service_delete_internal, argc, argv);
1044 /********************************************************************
1045 ********************************************************************/
1047 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
1049 if (c->display_usage) {
1050 d_printf( "%s\n"
1051 "net rpc service create <service>\n"
1052 " %s\n",
1053 _("Usage:"),
1054 _("Create a Win32 service"));
1055 return 0;
1058 return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
1059 rpc_service_create_internal, argc, argv);
1062 /********************************************************************
1063 ********************************************************************/
1065 int net_rpc_service(struct net_context *c, int argc, const char **argv)
1067 struct functable func[] = {
1069 "list",
1070 rpc_service_list,
1071 NET_TRANSPORT_RPC,
1072 N_("View configured Win32 services"),
1073 N_("net rpc service list\n"
1074 " View configured Win32 services")
1077 "start",
1078 rpc_service_start,
1079 NET_TRANSPORT_RPC,
1080 N_("Start a service"),
1081 N_("net rpc service start\n"
1082 " Start a service")
1085 "stop",
1086 rpc_service_stop,
1087 NET_TRANSPORT_RPC,
1088 N_("Stop a service"),
1089 N_("net rpc service stop\n"
1090 " Stop a service")
1093 "pause",
1094 rpc_service_pause,
1095 NET_TRANSPORT_RPC,
1096 N_("Pause a service"),
1097 N_("net rpc service pause\n"
1098 " Pause a service")
1101 "resume",
1102 rpc_service_resume,
1103 NET_TRANSPORT_RPC,
1104 N_("Resume a paused service"),
1105 N_("net rpc service resume\n"
1106 " Resume a service")
1109 "status",
1110 rpc_service_status,
1111 NET_TRANSPORT_RPC,
1112 N_("View current status of a service"),
1113 N_("net rpc service status\n"
1114 " View current status of a service")
1117 "delete",
1118 rpc_service_delete,
1119 NET_TRANSPORT_RPC,
1120 N_("Delete a service"),
1121 N_("net rpc service delete\n"
1122 " Deletes a service")
1125 "create",
1126 rpc_service_create,
1127 NET_TRANSPORT_RPC,
1128 N_("Create a service"),
1129 N_("net rpc service create\n"
1130 " Creates a service")
1133 {NULL, NULL, 0, NULL, NULL}
1136 return net_run_function(c, argc, argv, "net rpc service",func);