f1cd2a6b0d91e4313ec65421cad2b7550b3461b2
[samba.git] / source3 / utils / net_rpc_service.c
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/ndr_svcctl.h"
22 #include "../librpc/gen_ndr/ndr_svcctl_c.h"
23
24 struct svc_state_msg {
25         uint32 flag;
26         const char *message;
27 };
28
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 }
38 };
39
40
41 /********************************************************************
42 ********************************************************************/
43 const char *svc_status_string( uint32 state )
44 {
45         fstring msg;
46         int i;
47
48         fstr_sprintf( msg, _("Unknown State [%d]"), state );
49
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;
54                 }
55         }
56
57         return talloc_strdup(talloc_tos(), msg);
58 }
59
60 /********************************************************************
61 ********************************************************************/
62
63 static WERROR open_service(struct dcerpc_binding_handle *b,
64                            TALLOC_CTX *mem_ctx,
65                            struct policy_handle *hSCM,
66                            const char *service,
67                            uint32_t access_mask,
68                            struct policy_handle *hService)
69 {
70         NTSTATUS status;
71         WERROR result;
72
73         status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
74                                             hSCM,
75                                             service,
76                                             access_mask,
77                                             hService,
78                                             &result);
79         if (!NT_STATUS_IS_OK(status)) {
80                 result = ntstatus_to_werror(status);
81                 d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
82                           nt_errstr(status));
83                 return result;
84         }
85         if (!W_ERROR_IS_OK(result) ) {
86                 d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
87                           win_errstr(result));
88                 return result;
89         }
90
91         return WERR_OK;
92 }
93
94 /********************************************************************
95 ********************************************************************/
96
97 static WERROR open_scm(struct dcerpc_binding_handle *b,
98                        TALLOC_CTX *mem_ctx,
99                        const char *server_name,
100                        uint32_t access_mask,
101                        struct policy_handle *hSCM)
102 {
103         NTSTATUS status;
104         WERROR result;
105
106         status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
107                                               server_name,
108                                               NULL,
109                                               access_mask,
110                                               hSCM,
111                                               &result);
112         if (!NT_STATUS_IS_OK(status)) {
113                 result = ntstatus_to_werror(status);
114                 d_fprintf(stderr,
115                           _("Failed to open Service Control Manager. [%s]\n"),
116                           nt_errstr(status));
117                 return result;
118         }
119         if (!W_ERROR_IS_OK(result)) {
120                 d_fprintf(stderr,
121                           _("Failed to open Service Control Manager. [%s]\n"),
122                           win_errstr(result));
123                 return result;
124         }
125
126         return WERR_OK;
127 }
128
129 /********************************************************************
130 ********************************************************************/
131
132 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
133                                 TALLOC_CTX *mem_ctx,
134                                 struct policy_handle *hSCM,
135                                 const char *service,
136                                 uint32 *state )
137 {
138         struct policy_handle hService;
139         struct SERVICE_STATUS service_status;
140         WERROR result = WERR_GENERAL_FAILURE;
141         NTSTATUS status;
142         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
143
144         /* now cycle until the status is actually 'watch_state' */
145
146         result = open_service(b, mem_ctx, hSCM, service,
147                               SC_RIGHT_SVC_QUERY_STATUS,
148                               &hService);
149         if (!W_ERROR_IS_OK(result) ) {
150                 return result;
151         }
152
153         status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
154                                                   &hService,
155                                                   &service_status,
156                                                   &result);
157         if (!NT_STATUS_IS_OK(status)) {
158                 result = ntstatus_to_werror(status);
159                 goto done;
160         }
161         if (!W_ERROR_IS_OK(result)) {
162                 goto done;
163         }
164
165         *state = service_status.state;
166
167  done:
168         if (is_valid_policy_hnd(&hService)) {
169                 WERROR _result;
170                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
171         }
172
173         return result;
174 }
175
176 /********************************************************************
177 ********************************************************************/
178
179 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
180                                 TALLOC_CTX *mem_ctx,
181                                 struct policy_handle *hSCM,
182                                 const char *service,
183                                 uint32 watch_state,
184                                 uint32 *final_state )
185 {
186         uint32 i;
187         uint32 state = 0;
188         WERROR result = WERR_GENERAL_FAILURE;
189
190
191         i = 0;
192         while ( (state != watch_state ) && i<30 ) {
193                 /* get the status */
194
195                 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state  );
196                 if ( !W_ERROR_IS_OK(result) ) {
197                         break;
198                 }
199
200                 d_printf(".");
201                 i++;
202                 sys_usleep( 100 );
203         }
204         d_printf("\n");
205
206         *final_state = state;
207
208         return result;
209 }
210
211 /********************************************************************
212 ********************************************************************/
213
214 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
215                                 TALLOC_CTX *mem_ctx,
216                                 struct policy_handle *hSCM,
217                                 const char *service,
218                                 uint32 control,
219                                 uint32 watch_state )
220 {
221         struct policy_handle hService;
222         WERROR result = WERR_GENERAL_FAILURE;
223         NTSTATUS status;
224         struct SERVICE_STATUS service_status;
225         uint32 state = 0;
226         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
227
228         /* Open the Service */
229
230         result = open_service(b, mem_ctx, hSCM, service,
231                               (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
232                               &hService);
233         if (!W_ERROR_IS_OK(result) ) {
234                 return result;
235         }
236
237         /* get the status */
238
239         status = dcerpc_svcctl_ControlService(b, mem_ctx,
240                                               &hService,
241                                               control,
242                                               &service_status,
243                                               &result);
244
245         if (!NT_STATUS_IS_OK(status)) {
246                 result = ntstatus_to_werror(status);
247                 d_fprintf(stderr, _("Control service request failed.  [%s]\n"),
248                           nt_errstr(status));
249                 goto done;
250         }
251         if (!W_ERROR_IS_OK(result) ) {
252                 d_fprintf(stderr, _("Control service request failed.  [%s]\n"),
253                           win_errstr(result));
254                 goto done;
255         }
256
257         /* loop -- checking the state until we are where we want to be */
258
259         result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
260
261         d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
262
263 done:
264         if (is_valid_policy_hnd(&hService)) {
265                 WERROR _result;
266                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
267         }
268
269         return result;
270 }
271
272 /********************************************************************
273 ********************************************************************/
274
275 static NTSTATUS rpc_service_list_internal(struct net_context *c,
276                                         const struct dom_sid *domain_sid,
277                                         const char *domain_name,
278                                         struct cli_state *cli,
279                                         struct rpc_pipe_client *pipe_hnd,
280                                         TALLOC_CTX *mem_ctx,
281                                         int argc,
282                                         const char **argv )
283 {
284         struct policy_handle hSCM;
285         struct ENUM_SERVICE_STATUSW *services = NULL;
286         WERROR result = WERR_GENERAL_FAILURE;
287         NTSTATUS status;
288         int i;
289         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
290
291         uint8_t *buffer = NULL;
292         uint32_t buf_size = 0;
293         uint32_t bytes_needed = 0;
294         uint32_t num_services = 0;
295         uint32_t resume_handle = 0;
296
297         if (argc != 0 ) {
298                 d_printf("%s net rpc service list\n", _("Usage:"));
299                 return NT_STATUS_OK;
300         }
301
302         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
303                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
304                           &hSCM);
305         if (!W_ERROR_IS_OK(result)) {
306                 return werror_to_ntstatus(result);
307         }
308
309         do {
310                 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
311                                                            &hSCM,
312                                                            SERVICE_TYPE_WIN32,
313                                                            SERVICE_STATE_ALL,
314                                                            buffer,
315                                                            buf_size,
316                                                            &bytes_needed,
317                                                            &num_services,
318                                                            &resume_handle,
319                                                            &result);
320
321                 if (!NT_STATUS_IS_OK(status)) {
322                         d_fprintf(stderr,
323                                 _("Failed to enumerate services.  [%s]\n"),
324                                 nt_errstr(status));
325                         break;
326                 }
327
328                 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
329                         buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
330                         buf_size = bytes_needed;
331                         continue;
332                 }
333
334                 if (!W_ERROR_IS_OK(result)) {
335                         status = werror_to_ntstatus(result);
336                         d_fprintf(stderr,
337                                 _("Failed to enumerate services.  [%s]\n"),
338                                 win_errstr(result));
339                         break;
340                 }
341
342                 if ( num_services == 0 ) {
343                         d_printf(_("No services returned\n"));
344                         break;
345                 }
346
347                 {
348                         enum ndr_err_code ndr_err;
349                         DATA_BLOB blob;
350                         struct ndr_pull *ndr;
351
352                         blob.length = buf_size;
353                         blob.data = talloc_steal(mem_ctx, buffer);
354
355                         services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
356                         if (!services) {
357                                 status = NT_STATUS_NO_MEMORY;
358                                 break;
359                         }
360
361                         ndr = ndr_pull_init_blob(&blob, mem_ctx);
362                         if (ndr == NULL) {
363                                 status = NT_STATUS_NO_MEMORY;
364                                 break;
365                         }
366
367                         ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
368                                 ndr, num_services, services);
369                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
370                                 status = ndr_map_error2ntstatus(ndr_err);
371                                 break;
372                         }
373
374                         for ( i=0; i<num_services; i++ ) {
375                                 d_printf("%-20s    \"%s\"\n",
376                                         services[i].service_name,
377                                         services[i].display_name);
378                         }
379                 }
380
381         } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
382
383         if (is_valid_policy_hnd(&hSCM)) {
384                 WERROR _result;
385                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
386         }
387
388         return status;
389 }
390
391 /********************************************************************
392 ********************************************************************/
393
394 static NTSTATUS rpc_service_status_internal(struct net_context *c,
395                                                 const struct dom_sid *domain_sid,
396                                                 const char *domain_name,
397                                                 struct cli_state *cli,
398                                                 struct rpc_pipe_client *pipe_hnd,
399                                                 TALLOC_CTX *mem_ctx,
400                                                 int argc,
401                                                 const char **argv )
402 {
403         struct policy_handle hSCM, hService;
404         WERROR result = WERR_GENERAL_FAILURE;
405         NTSTATUS status;
406         struct SERVICE_STATUS service_status;
407         struct QUERY_SERVICE_CONFIG config;
408         uint32_t buf_size = sizeof(config);
409         uint32_t ret_size = 0;
410         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
411
412         if (argc != 1 ) {
413                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
414                 return NT_STATUS_OK;
415         }
416
417         /* Open the Service Control Manager */
418         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
419                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
420                           &hSCM);
421         if (!W_ERROR_IS_OK(result)) {
422                 return werror_to_ntstatus(result);
423         }
424
425         /* Open the Service */
426
427         result = open_service(b, mem_ctx, &hSCM, argv[0],
428                               (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
429                               &hService);
430         if (!W_ERROR_IS_OK(result) ) {
431                 goto done;
432         }
433
434         /* get the status */
435
436         status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
437                                                   &hService,
438                                                   &service_status,
439                                                   &result);
440         if (!NT_STATUS_IS_OK(status)) {
441                 result = ntstatus_to_werror(status);
442                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
443                           nt_errstr(status));
444                 goto done;
445         }
446
447         if (!W_ERROR_IS_OK(result) ) {
448                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
449                           win_errstr(result));
450                 goto done;
451         }
452
453         d_printf(_("%s service is %s.\n"), argv[0],
454                  svc_status_string(service_status.state));
455
456         /* get the config */
457
458         status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
459                                                    &hService,
460                                                    &config,
461                                                    buf_size,
462                                                    &ret_size,
463                                                    &result);
464         if (!NT_STATUS_IS_OK(status)) {
465                 result = ntstatus_to_werror(status);
466                 d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
467                           nt_errstr(status));
468                 goto done;
469         }
470
471         if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
472                 buf_size = ret_size;
473                 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
474                                                            &hService,
475                                                            &config,
476                                                            buf_size,
477                                                            &ret_size,
478                                                            &result);
479                 if (!NT_STATUS_IS_OK(status)) {
480                         result = ntstatus_to_werror(status);
481                         d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
482                                   nt_errstr(status));
483                         goto done;
484                 }
485         }
486
487         if (!W_ERROR_IS_OK(result) ) {
488                 d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
489                           win_errstr(result));
490                 goto done;
491         }
492
493         /* print out the configuration information for the service */
494
495         d_printf(_("Configuration details:\n"));
496         d_printf(_("\tControls Accepted    = 0x%x\n"),
497                  service_status.controls_accepted);
498         d_printf(_("\tService Type         = 0x%x\n"), config.service_type);
499         d_printf(_("\tStart Type           = 0x%x\n"), config.start_type);
500         d_printf(_("\tError Control        = 0x%x\n"), config.error_control);
501         d_printf(_("\tTag ID               = 0x%x\n"), config.tag_id);
502
503         if (config.executablepath) {
504                 d_printf(_("\tExecutable Path      = %s\n"),
505                          config.executablepath);
506         }
507
508         if (config.loadordergroup) {
509                 d_printf(_("\tLoad Order Group     = %s\n"),
510                          config.loadordergroup);
511         }
512
513         if (config.dependencies) {
514                 d_printf(_("\tDependencies         = %s\n"),
515                          config.dependencies);
516         }
517
518         if (config.startname) {
519                 d_printf(_("\tStart Name           = %s\n"), config.startname);
520         }
521
522         if (config.displayname) {
523                 d_printf(_("\tDisplay Name         = %s\n"),
524                          config.displayname);
525         }
526
527 done:
528         if (is_valid_policy_hnd(&hService)) {
529                 WERROR _result;
530                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
531         }
532         if (is_valid_policy_hnd(&hSCM)) {
533                 WERROR _result;
534                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
535         }
536
537         return werror_to_ntstatus(result);
538 }
539
540 /********************************************************************
541 ********************************************************************/
542
543 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
544                                         const struct dom_sid *domain_sid,
545                                         const char *domain_name,
546                                         struct cli_state *cli,
547                                         struct rpc_pipe_client *pipe_hnd,
548                                         TALLOC_CTX *mem_ctx,
549                                         int argc,
550                                         const char **argv )
551 {
552         struct policy_handle hSCM;
553         WERROR result = WERR_GENERAL_FAILURE;
554         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
555
556         if (argc != 1 ) {
557                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
558                 return NT_STATUS_OK;
559         }
560
561         /* Open the Service Control Manager */
562         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
563                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
564                           &hSCM);
565         if (!W_ERROR_IS_OK(result)) {
566                 return werror_to_ntstatus(result);
567         }
568
569         result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
570                 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
571
572         if (is_valid_policy_hnd(&hSCM)) {
573                 WERROR _result;
574                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
575         }
576
577         return werror_to_ntstatus(result);
578 }
579
580 /********************************************************************
581 ********************************************************************/
582
583 static NTSTATUS rpc_service_pause_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 )
591 {
592         struct policy_handle hSCM;
593         WERROR result = WERR_GENERAL_FAILURE;
594         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
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         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
603                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
604                           &hSCM);
605         if (!W_ERROR_IS_OK(result)) {
606                 return werror_to_ntstatus(result);
607         }
608
609         result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
610                 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
611
612         if (is_valid_policy_hnd(&hSCM)) {
613                 WERROR _result;
614                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
615         }
616
617         return werror_to_ntstatus(result);
618 }
619
620 /********************************************************************
621 ********************************************************************/
622
623 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
624                                         const struct dom_sid *domain_sid,
625                                         const char *domain_name,
626                                         struct cli_state *cli,
627                                         struct rpc_pipe_client *pipe_hnd,
628                                         TALLOC_CTX *mem_ctx,
629                                         int argc,
630                                         const char **argv )
631 {
632         struct policy_handle hSCM;
633         WERROR result = WERR_GENERAL_FAILURE;
634         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
635
636         if (argc != 1 ) {
637                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
638                 return NT_STATUS_OK;
639         }
640
641         /* Open the Service Control Manager */
642         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
643                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
644                           &hSCM);
645         if (!W_ERROR_IS_OK(result)) {
646                 return werror_to_ntstatus(result);
647         }
648
649         result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
650                 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
651
652         if (is_valid_policy_hnd(&hSCM)) {
653                 WERROR _result;
654                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
655         }
656
657         return werror_to_ntstatus(result);
658 }
659
660 /********************************************************************
661 ********************************************************************/
662
663 static NTSTATUS rpc_service_start_internal(struct net_context *c,
664                                         const struct 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         uint32 state = 0;
676         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
677
678         if (argc != 1 ) {
679                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
680                 return NT_STATUS_OK;
681         }
682
683         /* Open the Service Control Manager */
684         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
685                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
686                           &hSCM);
687         if (!W_ERROR_IS_OK(result)) {
688                 return werror_to_ntstatus(result);
689         }
690
691
692         /* Open the Service */
693
694         result = open_service(b, mem_ctx, &hSCM, argv[0],
695                               SC_RIGHT_SVC_START,
696                               &hService);
697         if (!W_ERROR_IS_OK(result) ) {
698                 goto done;
699         }
700
701         /* get the status */
702
703         status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
704                                              &hService,
705                                              0,
706                                              NULL,
707                                              &result);
708
709         if (!NT_STATUS_IS_OK(status)) {
710                 result = ntstatus_to_werror(status);
711                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
712                           nt_errstr(status));
713                 goto done;
714         }
715         if (!W_ERROR_IS_OK(result) ) {
716                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
717                           win_errstr(result));
718                 goto done;
719         }
720
721         result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state  );
722
723         if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
724                 d_printf(_("Successfully started service: %s\n"),
725                          argv[0] );
726         else
727                 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
728                           argv[0], win_errstr(result) );
729
730 done:
731         if (is_valid_policy_hnd(&hService)) {
732                 WERROR _result;
733                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
734         }
735         if (is_valid_policy_hnd(&hSCM)) {
736                 WERROR _result;
737                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
738         }
739
740         return werror_to_ntstatus(result);
741 }
742
743 /********************************************************************
744 ********************************************************************/
745
746 static NTSTATUS rpc_service_delete_internal(struct net_context *c,
747                                             const struct dom_sid *domain_sid,
748                                             const char *domain_name,
749                                             struct cli_state *cli,
750                                             struct rpc_pipe_client *pipe_hnd,
751                                             TALLOC_CTX *mem_ctx,
752                                             int argc,
753                                             const char **argv)
754 {
755         struct policy_handle hSCM, hService;
756         WERROR result = WERR_GENERAL_FAILURE;
757         NTSTATUS status;
758         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
759
760         if (argc != 1 ) {
761                 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
762                 return NT_STATUS_OK;
763         }
764
765         ZERO_STRUCT(hSCM);
766         ZERO_STRUCT(hService);
767
768         /* Open the Service Control Manager */
769         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
770                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
771                           &hSCM);
772         if (!W_ERROR_IS_OK(result)) {
773                 return werror_to_ntstatus(result);
774         }
775
776         /* Open the Service */
777
778         result = open_service(b, mem_ctx, &hSCM, argv[0],
779                               SERVICE_ALL_ACCESS,
780                               &hService);
781         if (!W_ERROR_IS_OK(result) ) {
782                 goto done;
783         }
784
785         /* Delete the Service */
786
787         status = dcerpc_svcctl_DeleteService(b, mem_ctx,
788                                              &hService,
789                                              &result);
790
791         if (!NT_STATUS_IS_OK(status)) {
792                 result = ntstatus_to_werror(status);
793                 d_fprintf(stderr, _("Delete service request failed.  [%s]\n"),
794                         nt_errstr(status));
795                 goto done;
796         }
797         if (!W_ERROR_IS_OK(result)) {
798                 d_fprintf(stderr, _("Delete service request failed.  [%s]\n"),
799                         win_errstr(result));
800                 goto done;
801         }
802
803         d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
804
805  done:
806         if (is_valid_policy_hnd(&hService)) {
807                 WERROR _result;
808                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
809         }
810         if (is_valid_policy_hnd(&hSCM)) {
811                 WERROR _result;
812                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
813         }
814
815         return werror_to_ntstatus(result);
816 }
817
818 /********************************************************************
819 ********************************************************************/
820
821 static NTSTATUS rpc_service_create_internal(struct net_context *c,
822                                             const struct dom_sid *domain_sid,
823                                             const char *domain_name,
824                                             struct cli_state *cli,
825                                             struct rpc_pipe_client *pipe_hnd,
826                                             TALLOC_CTX *mem_ctx,
827                                             int argc,
828                                             const char **argv)
829 {
830         struct policy_handle hSCM, hService;
831         WERROR result = WERR_GENERAL_FAILURE;
832         NTSTATUS status;
833         const char *ServiceName;
834         const char *DisplayName;
835         const char *binary_path;
836         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
837
838         if (argc != 3) {
839                 d_printf("%s net rpc service create <service> "
840                          "<displayname> <binarypath>\n", _("Usage:"));
841                 return NT_STATUS_OK;
842         }
843
844         ZERO_STRUCT(hSCM);
845         ZERO_STRUCT(hService);
846
847         /* Open the Service Control Manager */
848         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
849                           SC_RIGHT_MGR_CREATE_SERVICE,
850                           &hSCM);
851         if (!W_ERROR_IS_OK(result)) {
852                 return werror_to_ntstatus(result);
853         }
854
855         /* Create the service */
856
857         ServiceName = argv[0];
858         DisplayName = argv[1];
859         binary_path = argv[2];
860
861         status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
862                                               &hSCM,
863                                               ServiceName,
864                                               DisplayName,
865                                               SERVICE_ALL_ACCESS,
866                                               SERVICE_TYPE_WIN32_OWN_PROCESS,
867                                               SVCCTL_DEMAND_START,
868                                               SVCCTL_SVC_ERROR_NORMAL,
869                                               binary_path,
870                                               NULL, /* LoadOrderGroupKey */
871                                               NULL, /* TagId */
872                                               NULL, /* dependencies */
873                                               0, /* dependencies_size */
874                                               NULL, /* service_start_name */
875                                               NULL, /* password */
876                                               0, /* password_size */
877                                               &hService,
878                                               &result);
879         if (!NT_STATUS_IS_OK(status)) {
880                 result = ntstatus_to_werror(status);
881                 d_fprintf(stderr, _("Create service request failed.  [%s]\n"),
882                           nt_errstr(status));
883                 goto done;
884         }
885         if (!W_ERROR_IS_OK(result)) {
886                 d_fprintf(stderr, _("Create service request failed.  [%s]\n"),
887                           win_errstr(result));
888                 goto done;
889         }
890
891         d_printf(_("Successfully created Service: %s\n"), argv[0]);
892
893  done:
894         if (is_valid_policy_hnd(&hService)) {
895                 WERROR _result;
896                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
897         }
898         if (is_valid_policy_hnd(&hSCM)) {
899                 WERROR _result;
900                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
901         }
902
903         return werror_to_ntstatus(result);
904 }
905
906 /********************************************************************
907 ********************************************************************/
908
909 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
910 {
911         if (c->display_usage) {
912                 d_printf(  "%s\n"
913                            "net rpc service list\n"
914                            "    %s\n",
915                          _("Usage:"),
916                          _("View configured Win32 services"));
917                 return 0;
918         }
919
920         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
921                 rpc_service_list_internal, argc, argv );
922 }
923
924 /********************************************************************
925 ********************************************************************/
926
927 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
928 {
929         if (c->display_usage) {
930                 d_printf(  "%s\n"
931                            "net rpc service start <service>\n"
932                            "    %s\n",
933                          _("Usage:"),
934                          _("Start a Win32 service"));
935                 return 0;
936         }
937
938         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
939                 rpc_service_start_internal, argc, argv );
940 }
941
942 /********************************************************************
943 ********************************************************************/
944
945 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
946 {
947         if (c->display_usage) {
948                 d_printf(  "%s\n"
949                            "net rpc service stop <service>\n"
950                            "    %s\n",
951                          _("Usage:"),
952                          _("Stop a Win32 service"));
953                 return 0;
954         }
955
956         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
957                 rpc_service_stop_internal, argc, argv );
958 }
959
960 /********************************************************************
961 ********************************************************************/
962
963 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
964 {
965         if (c->display_usage) {
966                 d_printf(  "%s\n"
967                            "net rpc service resume <service>\n"
968                            "    %s\n",
969                          _("Usage:"),
970                          _("Resume a Win32 service"));
971                 return 0;
972         }
973
974         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
975                 rpc_service_resume_internal, argc, argv );
976 }
977
978 /********************************************************************
979 ********************************************************************/
980
981 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
982 {
983         if (c->display_usage) {
984                 d_printf(  "%s\n"
985                            "net rpc service pause <service>\n"
986                            "    %s\n",
987                          _("Usage:"),
988                          _("Pause a Win32 service"));
989                 return 0;
990         }
991
992         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
993                 rpc_service_pause_internal, argc, argv );
994 }
995
996 /********************************************************************
997 ********************************************************************/
998
999 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
1000 {
1001         if (c->display_usage) {
1002                 d_printf(  "%s\n"
1003                            "net rpc service status <service>\n"
1004                            "     %s\n",
1005                          _("Usage:"),
1006                          _("Show the current status of a service"));
1007                 return 0;
1008         }
1009
1010         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1011                 rpc_service_status_internal, argc, argv );
1012 }
1013
1014 /********************************************************************
1015 ********************************************************************/
1016
1017 static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
1018 {
1019         if (c->display_usage) {
1020                 d_printf(  "%s\n"
1021                            "net rpc service delete <service>\n"
1022                            "    %s\n",
1023                          _("Usage:"),
1024                          _("Delete a Win32 service"));
1025                 return 0;
1026         }
1027
1028         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1029                 rpc_service_delete_internal, argc, argv);
1030 }
1031
1032 /********************************************************************
1033 ********************************************************************/
1034
1035 static int rpc_service_create(struct net_context *c, int argc, const char **argv)
1036 {
1037         if (c->display_usage) {
1038                 d_printf(  "%s\n"
1039                            "net rpc service create <service>\n"
1040                            "    %s\n",
1041                          _("Usage:"),
1042                          _("Create a Win32 service"));
1043                 return 0;
1044         }
1045
1046         return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
1047                 rpc_service_create_internal, argc, argv);
1048 }
1049
1050 /********************************************************************
1051 ********************************************************************/
1052
1053 int net_rpc_service(struct net_context *c, int argc, const char **argv)
1054 {
1055         struct functable func[] = {
1056                 {
1057                         "list",
1058                         rpc_service_list,
1059                         NET_TRANSPORT_RPC,
1060                         N_("View configured Win32 services"),
1061                         N_("net rpc service list\n"
1062                            "    View configured Win32 services")
1063                 },
1064                 {
1065                         "start",
1066                         rpc_service_start,
1067                         NET_TRANSPORT_RPC,
1068                         N_("Start a service"),
1069                         N_("net rpc service start\n"
1070                            "    Start a service")
1071                 },
1072                 {
1073                         "stop",
1074                         rpc_service_stop,
1075                         NET_TRANSPORT_RPC,
1076                         N_("Stop a service"),
1077                         N_("net rpc service stop\n"
1078                            "    Stop a service")
1079                 },
1080                 {
1081                         "pause",
1082                         rpc_service_pause,
1083                         NET_TRANSPORT_RPC,
1084                         N_("Pause a service"),
1085                         N_("net rpc service pause\n"
1086                            "    Pause a service")
1087                 },
1088                 {
1089                         "resume",
1090                         rpc_service_resume,
1091                         NET_TRANSPORT_RPC,
1092                         N_("Resume a paused service"),
1093                         N_("net rpc service resume\n"
1094                            "    Resume a service")
1095                 },
1096                 {
1097                         "status",
1098                         rpc_service_status,
1099                         NET_TRANSPORT_RPC,
1100                         N_("View current status of a service"),
1101                         N_("net rpc service status\n"
1102                            "    View current status of a service")
1103                 },
1104                 {
1105                         "delete",
1106                         rpc_service_delete,
1107                         NET_TRANSPORT_RPC,
1108                         N_("Delete a service"),
1109                         N_("net rpc service delete\n"
1110                            "    Deletes a service")
1111                 },
1112                 {
1113                         "create",
1114                         rpc_service_create,
1115                         NET_TRANSPORT_RPC,
1116                         N_("Create a service"),
1117                         N_("net rpc service create\n"
1118                            "    Creates a service")
1119                 },
1120
1121                 {NULL, NULL, 0, NULL, NULL}
1122         };
1123
1124         return net_run_function(c, argc, argv, "net rpc service",func);
1125 }