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