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