3d6bf7617cfdb3b4f04e8acb352e34c485b923af
[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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19  
20 #include "includes.h"
21 #include "utils/net.h"
22
23 /********************************************************************
24 ********************************************************************/
25
26 static NTSTATUS rpc_service_list_internal( const DOM_SID *domain_sid, const char *domain_name, 
27                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
28                                            int argc, const char **argv )
29 {
30         POLICY_HND hSCM;
31         ENUM_SERVICES_STATUS *services;
32         WERROR result = WERR_GENERAL_FAILURE;
33         fstring servicename;
34         fstring displayname;
35         uint32 num_services = 0;
36         int i;
37         
38         if (argc != 0 ) {
39                 d_printf("Usage: net rpc service list\n");
40                 return NT_STATUS_OK;
41         }
42
43         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
44         if ( !W_ERROR_IS_OK(result) ) {
45                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
46                 return werror_to_ntstatus(result);
47         }
48         
49         result = cli_svcctl_enumerate_services( cli, mem_ctx, &hSCM, SVCCTL_TYPE_WIN32,
50                 SVCCTL_STATE_ALL, &num_services, &services );
51         
52         if ( !W_ERROR_IS_OK(result) ) {
53                 d_printf("Failed to enumerate services.  [%s]\n", dos_errstr(result));
54                 goto done;
55         }
56         
57         if ( num_services == 0 )
58                 d_printf("No services returned\n");
59         
60         for ( i=0; i<num_services; i++ ) {
61                 rpcstr_pull( servicename, services[i].servicename.buffer, sizeof(servicename), -1, STR_TERMINATE );
62                 rpcstr_pull( displayname, services[i].displayname.buffer, sizeof(displayname), -1, STR_TERMINATE );
63                 
64                 d_printf("%s (%s)\n", displayname, servicename);
65         }
66
67 done:   
68         close_service_handle( cli, mem_ctx, &hSCM  );
69                 
70         return werror_to_ntstatus(result);
71 }       
72
73
74 /********************************************************************
75 ********************************************************************/
76
77 static int rpc_service_list( int argc, const char **argv )
78 {
79         return run_rpc_command( NULL, PI_SVCCTL, 0, 
80                 rpc_service_list_internal, argc, argv );
81 }
82
83 /********************************************************************
84 ********************************************************************/
85
86 static int rpc_service_start( int argc, const char **argv )
87 {
88         d_printf("not implemented\n");
89         return 0;
90 }
91
92 /********************************************************************
93 ********************************************************************/
94
95 static int rpc_service_stop( int argc, const char **argv )
96 {
97         d_printf("not implemented\n");
98         return 0;
99 }
100
101 /********************************************************************
102 ********************************************************************/
103
104 static int rpc_service_pause( int argc, const char **argv )
105 {
106         d_printf("not implemented\n");
107         return 0;
108 }
109
110 /********************************************************************
111 ********************************************************************/
112
113 static int rpc_service_status( int argc, const char **argv )
114 {
115         d_printf("not implemented\n");
116         return 0;
117 }
118
119 /********************************************************************
120 ********************************************************************/
121
122 static int net_help_service( int argc, const char **argv )
123 {
124         d_printf("net rpc service list               View configured Win32 services\n");
125         d_printf("net rpc service start <service>    Start a service\n");
126         d_printf("net rpc service stop <service>     Stop a service\n");
127         d_printf("net rpc service pause <service>    Pause a service\n");
128         d_printf("net rpc service status <service>   View the current status of a service\n");
129         
130         return -1;
131 }
132
133 /********************************************************************
134 ********************************************************************/
135
136 int net_rpc_service(int argc, const char **argv) 
137 {
138         struct functable func[] = {
139                 {"list", rpc_service_list},
140                 {"start", rpc_service_start},
141                 {"stop", rpc_service_stop},
142                 {"pause", rpc_service_pause},
143                 {"status", rpc_service_status},
144                 {NULL, NULL}
145         };
146         
147         if ( argc )
148                 return net_run_function( argc, argv, func, net_help_service );
149                 
150         return net_help_service( argc, argv );
151 }
152
153