Move svc_status_string() out of rpc_client/cli_svcctl.c
[samba.git] / source / rpc_client / cli_svcctl.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Gerald 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
20
21 #include "includes.h"
22 #include "rpc_client.h"
23
24 /*******************************************************************
25 *******************************************************************/
26
27 WERROR rpccli_svcctl_enumerate_services( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
28                                       POLICY_HND *hSCM, uint32 type, uint32 state, 
29                                       uint32 *returned, ENUM_SERVICES_STATUS **service_array  )
30 {
31         SVCCTL_Q_ENUM_SERVICES_STATUS in;
32         SVCCTL_R_ENUM_SERVICES_STATUS out;
33         prs_struct qbuf, rbuf;
34         uint32 resume = 0;
35         ENUM_SERVICES_STATUS *services;
36         int i;
37
38         ZERO_STRUCT(in);
39         ZERO_STRUCT(out);
40         
41         /* setup the request */
42         
43         memcpy( &in.handle, hSCM, sizeof(POLICY_HND) );
44         
45         in.type        = type;
46         in.state       = state;
47         in.resume      = &resume;
48         
49         /* first time is to get the buffer size */
50         in.buffer_size = 0;
51
52         CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_ENUM_SERVICES_STATUS_W, 
53                     in, out, 
54                     qbuf, rbuf,
55                     svcctl_io_q_enum_services_status,
56                     svcctl_io_r_enum_services_status, 
57                     WERR_GENERAL_FAILURE );
58
59         /* second time with correct buffer size...should be ok */
60         
61         if ( W_ERROR_EQUAL( out.status, WERR_MORE_DATA ) ) {
62                 in.buffer_size = out.needed;
63
64                 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_ENUM_SERVICES_STATUS_W, 
65                             in, out, 
66                             qbuf, rbuf,
67                             svcctl_io_q_enum_services_status,
68                             svcctl_io_r_enum_services_status, 
69                             WERR_GENERAL_FAILURE );
70         }
71         
72         if ( !W_ERROR_IS_OK(out.status) ) 
73                 return out.status;
74                 
75         /* pull out the data */
76         if (out.returned) {
77                 if ( !(services = TALLOC_ARRAY( mem_ctx, ENUM_SERVICES_STATUS, out.returned )) ) 
78                         return WERR_NOMEM;
79         } else {
80                 services = NULL;
81         }
82                 
83         for ( i=0; i<out.returned; i++ ) {
84                 svcctl_io_enum_services_status( "", &services[i], &out.buffer, 0 );
85         }
86         
87         *service_array = services;
88         *returned      = out.returned;
89         
90         return out.status;
91 }
92
93 /*******************************************************************
94 *******************************************************************/
95
96 WERROR rpccli_svcctl_query_config(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
97                                 POLICY_HND *hService, SERVICE_CONFIG *config )
98 {
99         SVCCTL_Q_QUERY_SERVICE_CONFIG in;
100         SVCCTL_R_QUERY_SERVICE_CONFIG out;
101         prs_struct qbuf, rbuf;
102         
103         ZERO_STRUCT(in);
104         ZERO_STRUCT(out);
105         
106         memcpy( &in.handle, hService, sizeof(POLICY_HND) );
107         in.buffer_size = 0;
108         
109         
110         CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W, 
111                     in, out, 
112                     qbuf, rbuf,
113                     svcctl_io_q_query_service_config,
114                     svcctl_io_r_query_service_config, 
115                     WERR_GENERAL_FAILURE );
116         
117         if ( W_ERROR_EQUAL( out.status, WERR_INSUFFICIENT_BUFFER ) ) {
118                 in.buffer_size = out.needed;
119
120                 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W,
121                             in, out, 
122                             qbuf, rbuf,
123                             svcctl_io_q_query_service_config,
124                             svcctl_io_r_query_service_config, 
125                             WERR_GENERAL_FAILURE );
126         }
127         
128         if ( !W_ERROR_IS_OK( out.status ) )
129                 return out.status;
130
131         memcpy( config, &out.config, sizeof(SERVICE_CONFIG) );
132         
133         config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
134         config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
135         config->dependencies   = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
136         config->startname      = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
137         config->displayname    = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
138         
139         if ( out.config.executablepath ) {
140                 config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
141                 copy_unistr2( config->executablepath, out.config.executablepath );
142         }
143
144         if ( out.config.loadordergroup ) {
145                 config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
146                 copy_unistr2( config->loadordergroup, out.config.loadordergroup );
147         }
148
149         if ( out.config.dependencies ) {
150                 config->dependencies = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
151                 copy_unistr2( config->dependencies, out.config.dependencies );
152         }
153
154         if ( out.config.startname ) {
155                 config->startname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
156                 copy_unistr2( config->startname, out.config.startname );
157         }
158
159         if ( out.config.displayname ) {
160                 config->displayname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
161                 copy_unistr2( config->displayname, out.config.displayname );
162         }
163         
164         return out.status;
165 }