• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/samba-3.5.8/source3/utils/
1/*
2   Samba Unix/Linux SMB client library
3   Distributed SMB/CIFS Server Management Utility
4   Copyright (C) Gerald (Jerry) Carter          2005
5
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.
10
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.
15
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/>.  */
18
19#include "includes.h"
20#include "utils/net.h"
21#include "../librpc/gen_ndr/cli_svcctl.h"
22
23struct svc_state_msg {
24	uint32 flag;
25	const char *message;
26};
27
28static 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 }
37};
38
39
40/********************************************************************
41********************************************************************/
42const char *svc_status_string( uint32 state )
43{
44	fstring msg;
45	int i;
46
47	fstr_sprintf( msg, _("Unknown State [%d]"), state );
48
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;
53		}
54	}
55
56	return talloc_strdup(talloc_tos(), msg);
57}
58
59/********************************************************************
60********************************************************************/
61
62static 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 )
67{
68	struct policy_handle hService;
69	struct SERVICE_STATUS service_status;
70	WERROR result = WERR_GENERAL_FAILURE;
71	NTSTATUS status;
72
73	/* now cycle until the status is actually 'watch_state' */
74
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;
85	}
86
87	status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
88						  &hService,
89						  &service_status,
90						  &result);
91
92	if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
93		*state = service_status.state;
94	}
95
96	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
97
98	return result;
99}
100
101/********************************************************************
102********************************************************************/
103
104static 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 )
110{
111	uint32 i;
112	uint32 state = 0;
113	WERROR result = WERR_GENERAL_FAILURE;
114
115
116	i = 0;
117	while ( (state != watch_state ) && i<30 ) {
118		/* get the status */
119
120		result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state  );
121		if ( !W_ERROR_IS_OK(result) ) {
122			break;
123		}
124
125		d_printf(".");
126		i++;
127		sys_usleep( 100 );
128	}
129	d_printf("\n");
130
131	*final_state = state;
132
133	return result;
134}
135
136/********************************************************************
137********************************************************************/
138
139static 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 )
145{
146	struct policy_handle hService;
147	WERROR result = WERR_GENERAL_FAILURE;
148	NTSTATUS status;
149	struct SERVICE_STATUS service_status;
150	uint32 state = 0;
151
152	/* Open the Service */
153
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);
160
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;
165	}
166
167	/* get the status */
168
169	status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
170					      &hService,
171					      control,
172					      &service_status,
173					      &result);
174
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;
179	}
180
181	/* loop -- checking the state until we are where we want to be */
182
183	result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
184
185	d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
186
187done:
188	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
189
190	return result;
191}
192
193/********************************************************************
194********************************************************************/
195
196static 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 )
204{
205	struct policy_handle hSCM;
206	struct ENUM_SERVICE_STATUSW *services = NULL;
207	WERROR result = WERR_GENERAL_FAILURE;
208	NTSTATUS status;
209	int i;
210
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;
216
217	if (argc != 0 ) {
218		d_printf("%s net rpc service list\n", _("Usage:"));
219		return NT_STATUS_OK;
220	}
221
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);
233	}
234
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);
246
247		if (NT_STATUS_IS_ERR(status)) {
248			d_fprintf(stderr,
249				_("Failed to enumerate services.  [%s]\n"),
250				win_errstr(result));
251			break;
252		}
253
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;
258		}
259
260		if ( num_services == 0 ) {
261			d_printf(_("No services returned\n"));
262			break;
263		}
264
265		{
266			enum ndr_err_code ndr_err;
267			DATA_BLOB blob;
268			struct ndr_pull *ndr;
269
270			blob.length = buf_size;
271			blob.data = talloc_steal(mem_ctx, buffer);
272
273			services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
274			if (!services) {
275				status = NT_STATUS_NO_MEMORY;
276				break;
277			}
278
279			ndr = ndr_pull_init_blob(&blob, mem_ctx, NULL);
280			if (ndr == NULL) {
281				status = NT_STATUS_NO_MEMORY;
282				break;
283			}
284
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;
290			}
291
292			for ( i=0; i<num_services; i++ ) {
293				d_printf("%-20s    \"%s\"\n",
294					services[i].service_name,
295					services[i].display_name);
296			}
297		}
298
299	} while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
300
301	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
302
303	return status;
304}
305
306/********************************************************************
307********************************************************************/
308
309static 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 )
317{
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;
325
326	if (argc != 1 ) {
327		d_printf("%s net rpc service status <service>\n", _("Usage:"));
328		return NT_STATUS_OK;
329	}
330
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);
343	}
344
345	/* Open the Service */
346
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);
353
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;
358	}
359
360	/* get the status */
361
362	status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
363						  &hService,
364						  &service_status,
365						  &result);
366
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;
371	}
372
373	d_printf(_("%s service is %s.\n"), argv[0],
374		 svc_status_string(service_status.state));
375
376	/* get the config */
377
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);
392	}
393
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;
398	}
399
400	/* print out the configuration information for the service */
401
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);
409
410	if (config.executablepath) {
411		d_printf(_("\tExecutable Path      = %s\n"),
412			 config.executablepath);
413	}
414
415	if (config.loadordergroup) {
416		d_printf(_("\tLoad Order Group     = %s\n"),
417			 config.loadordergroup);
418	}
419
420	if (config.dependencies) {
421		d_printf(_("\tDependencies         = %s\n"),
422			 config.dependencies);
423	}
424
425	if (config.startname) {
426		d_printf(_("\tStart Name           = %s\n"), config.startname);
427	}
428
429	if (config.displayname) {
430		d_printf(_("\tDisplay Name         = %s\n"),
431			 config.displayname);
432	}
433
434done:
435        rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
436	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
437
438	return werror_to_ntstatus(result);
439}
440
441/********************************************************************
442********************************************************************/
443
444static 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 )
452{
453	struct policy_handle hSCM;
454	WERROR result = WERR_GENERAL_FAILURE;
455	NTSTATUS status;
456	fstring servicename;
457
458	if (argc != 1 ) {
459		d_printf("%s net rpc service status <service>\n", _("Usage:"));
460		return NT_STATUS_OK;
461	}
462
463	fstrcpy( servicename, argv[0] );
464
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);
477	}
478
479	result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
480		SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
481
482	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
483
484	return werror_to_ntstatus(result);
485}
486
487/********************************************************************
488********************************************************************/
489
490static 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 )
498{
499	struct policy_handle hSCM;
500	WERROR result = WERR_GENERAL_FAILURE;
501	NTSTATUS status;
502	fstring servicename;
503
504	if (argc != 1 ) {
505		d_printf("%s net rpc service status <service>\n", _("Usage:"));
506		return NT_STATUS_OK;
507	}
508
509	fstrcpy( servicename, argv[0] );
510
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);
523	}
524
525	result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
526		SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
527
528	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
529
530	return werror_to_ntstatus(result);
531}
532
533/********************************************************************
534********************************************************************/
535
536static 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 )
544{
545	struct policy_handle hSCM;
546	WERROR result = WERR_GENERAL_FAILURE;
547	NTSTATUS status;
548	fstring servicename;
549
550	if (argc != 1 ) {
551		d_printf("%s net rpc service status <service>\n", _("Usage:"));
552		return NT_STATUS_OK;
553	}
554
555	fstrcpy( servicename, argv[0] );
556
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);
569	}
570
571	result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
572		SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
573
574	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
575
576	return werror_to_ntstatus(result);
577}
578
579/********************************************************************
580********************************************************************/
581
582static 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 )
590{
591	struct policy_handle hSCM, hService;
592	WERROR result = WERR_GENERAL_FAILURE;
593	NTSTATUS status;
594	uint32 state = 0;
595
596	if (argc != 1 ) {
597		d_printf("%s net rpc service status <service>\n", _("Usage:"));
598		return NT_STATUS_OK;
599	}
600
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);
613	}
614
615	/* Open the Service */
616
617	status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
618					    &hSCM,
619					    argv[0],
620					    SC_RIGHT_SVC_START,
621					    &hService,
622					    &result);
623
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;
628	}
629
630	/* get the status */
631
632	status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
633					     &hService,
634					     0,
635					     NULL,
636					     &result);
637
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;
642	}
643
644	result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state  );
645
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) );
652
653done:
654	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
655	rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
656
657	return werror_to_ntstatus(result);
658}
659
660/********************************************************************
661********************************************************************/
662
663static 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)
671{
672	struct policy_handle hSCM, hService;
673	WERROR result = WERR_GENERAL_FAILURE;
674	NTSTATUS status;
675
676	if (argc != 1 ) {
677		d_printf("%s net rpc service delete <service>\n", _("Usage:"));
678		return NT_STATUS_OK;
679	}
680
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);
693	}
694
695	/* Open the Service */
696
697	status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
698					    &hSCM,
699					    argv[0],
700					    SERVICE_ALL_ACCESS,
701					    &hService,
702					    &result);
703
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;
708	}
709
710	/* Delete the Service */
711
712	status = rpccli_svcctl_DeleteService(pipe_hnd, mem_ctx,
713					     &hService,
714					     &result);
715
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;
720	}
721
722	d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
723
724 done:
725	if (is_valid_policy_hnd(&hService)) {
726		rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
727	}
728	if (is_valid_policy_hnd(&hSCM)) {
729		rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
730	}
731
732	return werror_to_ntstatus(result);
733}
734
735/********************************************************************
736********************************************************************/
737
738static 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)
746{
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;
753
754	if (argc != 3) {
755		d_printf("%s net rpc service create <service> "
756			 "<displayname> <binarypath>\n", _("Usage:"));
757		return NT_STATUS_OK;
758	}
759
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);
772	}
773
774	/* Create the service */
775
776	ServiceName = argv[0];
777	DisplayName = argv[1];
778	binary_path = argv[2];
779
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);
798
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;
803	}
804
805	d_printf(_("Successfully created Service: %s\n"), argv[0]);
806
807 done:
808	if (is_valid_policy_hnd(&hService)) {
809		rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
810	}
811	if (is_valid_policy_hnd(&hSCM)) {
812		rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
813	}
814
815	return werror_to_ntstatus(result);
816}
817
818/********************************************************************
819********************************************************************/
820
821static int rpc_service_list(struct net_context *c, int argc, const char **argv )
822{
823	if (c->display_usage) {
824		d_printf(  "%s\n"
825			   "net rpc service list\n"
826			   "    %s\n",
827			 _("Usage:"),
828			 _("View configured Win32 services"));
829		return 0;
830	}
831
832	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
833		rpc_service_list_internal, argc, argv );
834}
835
836/********************************************************************
837********************************************************************/
838
839static int rpc_service_start(struct net_context *c, int argc, const char **argv )
840{
841	if (c->display_usage) {
842		d_printf(  "%s\n"
843			   "net rpc service start <service>\n"
844			   "    %s\n",
845			 _("Usage:"),
846			 _("Start a Win32 service"));
847		return 0;
848	}
849
850	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
851		rpc_service_start_internal, argc, argv );
852}
853
854/********************************************************************
855********************************************************************/
856
857static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
858{
859	if (c->display_usage) {
860		d_printf(  "%s\n"
861			   "net rpc service stop <service>\n"
862			   "    %s\n",
863			 _("Usage:"),
864			 _("Stop a Win32 service"));
865		return 0;
866	}
867
868	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
869		rpc_service_stop_internal, argc, argv );
870}
871
872/********************************************************************
873********************************************************************/
874
875static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
876{
877	if (c->display_usage) {
878		d_printf(  "%s\n"
879			   "net rpc service resume <service>\n"
880			   "    %s\n",
881			 _("Usage:"),
882			 _("Resume a Win32 service"));
883		return 0;
884	}
885
886	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
887		rpc_service_resume_internal, argc, argv );
888}
889
890/********************************************************************
891********************************************************************/
892
893static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
894{
895	if (c->display_usage) {
896		d_printf(  "%s\n"
897			   "net rpc service pause <service>\n"
898			   "    %s\n",
899			 _("Usage:"),
900			 _("Pause a Win32 service"));
901		return 0;
902	}
903
904	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
905		rpc_service_pause_internal, argc, argv );
906}
907
908/********************************************************************
909********************************************************************/
910
911static int rpc_service_status(struct net_context *c, int argc, const char **argv )
912{
913	if (c->display_usage) {
914		d_printf(  "%s\n"
915			   "net rpc service status <service>\n"
916			   "     %s\n",
917			 _("Usage:"),
918			 _("Show the current status of a service"));
919		return 0;
920	}
921
922	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
923		rpc_service_status_internal, argc, argv );
924}
925
926/********************************************************************
927********************************************************************/
928
929static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
930{
931	if (c->display_usage) {
932		d_printf(  "%s\n"
933			   "net rpc service delete <service>\n"
934			   "    %s\n",
935			 _("Usage:"),
936			 _("Delete a Win32 service"));
937		return 0;
938	}
939
940	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
941		rpc_service_delete_internal, argc, argv);
942}
943
944/********************************************************************
945********************************************************************/
946
947static int rpc_service_create(struct net_context *c, int argc, const char **argv)
948{
949	if (c->display_usage) {
950		d_printf(  "%s\n"
951			   "net rpc service create <service>\n"
952			   "    %s\n",
953			 _("Usage:"),
954			 _("Create a Win32 service"));
955		return 0;
956	}
957
958	return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
959		rpc_service_create_internal, argc, argv);
960}
961
962/********************************************************************
963********************************************************************/
964
965int net_rpc_service(struct net_context *c, int argc, const char **argv)
966{
967	struct functable func[] = {
968		{
969			"list",
970			rpc_service_list,
971			NET_TRANSPORT_RPC,
972			N_("View configured Win32 services"),
973			N_("net rpc service list\n"
974			   "    View configured Win32 services")
975		},
976		{
977			"start",
978			rpc_service_start,
979			NET_TRANSPORT_RPC,
980			N_("Start a service"),
981			N_("net rpc service start\n"
982			   "    Start a service")
983		},
984		{
985			"stop",
986			rpc_service_stop,
987			NET_TRANSPORT_RPC,
988			N_("Stop a service"),
989			N_("net rpc service stop\n"
990			   "    Stop a service")
991		},
992		{
993			"pause",
994			rpc_service_pause,
995			NET_TRANSPORT_RPC,
996			N_("Pause a service"),
997			N_("net rpc service pause\n"
998			   "    Pause a service")
999		},
1000		{
1001			"resume",
1002			rpc_service_resume,
1003			NET_TRANSPORT_RPC,
1004			N_("Resume a paused service"),
1005			N_("net rpc service resume\n"
1006			   "    Resume a service")
1007		},
1008		{
1009			"status",
1010			rpc_service_status,
1011			NET_TRANSPORT_RPC,
1012			N_("View current status of a service"),
1013			N_("net rpc service status\n"
1014			   "    View current status of a service")
1015		},
1016		{
1017			"delete",
1018			rpc_service_delete,
1019			NET_TRANSPORT_RPC,
1020			N_("Delete a service"),
1021			N_("net rpc service delete\n"
1022			   "    Deletes a service")
1023		},
1024		{
1025			"create",
1026			rpc_service_create,
1027			NET_TRANSPORT_RPC,
1028			N_("Create a service"),
1029			N_("net rpc service create\n"
1030			   "    Creates a service")
1031		},
1032
1033		{NULL, NULL, 0, NULL, NULL}
1034	};
1035
1036	return net_run_function(c, argc, argv, "net rpc service",func);
1037}
1038