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