r6303: Setting up for 3.0.15pre1
[Samba.git] / source / rpc_client / cli_svcctl.c
blob9f80bb79a399cfc59bf87af54db6f76b31dad6e8
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Gerald Carter 2005.
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.
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.
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.
22 #include "includes.h"
23 #include "rpc_client.h"
25 struct svc_state_msg {
26 uint32 flag;
27 const char *message;
30 static struct svc_state_msg state_msg_table[] = {
31 { SVCCTL_STOPPED, "stopped" },
32 { SVCCTL_START_PENDING, "start pending" },
33 { SVCCTL_STOP_PENDING, "stop pending" },
34 { SVCCTL_RUNNING, "running" },
35 { SVCCTL_CONTINUE_PENDING, "resume pending" },
36 { SVCCTL_PAUSE_PENDING, "pause pending" },
37 { SVCCTL_PAUSED, "paused" },
38 { 0, NULL }
42 /********************************************************************
43 ********************************************************************/
44 const char* svc_status_string( uint32 state )
46 static fstring msg;
47 int i;
49 fstr_sprintf( msg, "Unknown State [%d]", state );
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;
58 return msg;
61 /********************************************************************
62 ********************************************************************/
64 WERROR cli_svcctl_open_scm( struct cli_state *cli, TALLOC_CTX *mem_ctx,
65 POLICY_HND *hSCM, uint32 access_desired )
67 SVCCTL_Q_OPEN_SCMANAGER in;
68 SVCCTL_R_OPEN_SCMANAGER out;
69 prs_struct qbuf, rbuf;
70 fstring server;
72 ZERO_STRUCT(in);
73 ZERO_STRUCT(out);
75 /* leave the database name NULL to get the default service db */
77 in.database = NULL;
79 /* set the server name */
81 if ( !(in.servername = TALLOC_P( mem_ctx, UNISTR2 )) )
82 return WERR_NOMEM;
83 fstr_sprintf( server, "\\\\%s", cli->desthost );
84 init_unistr2( in.servername, server, UNI_STR_TERMINATE );
86 in.access = access_desired;
88 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_OPEN_SCMANAGER_W,
89 in, out,
90 qbuf, rbuf,
91 svcctl_io_q_open_scmanager,
92 svcctl_io_r_open_scmanager,
93 WERR_GENERAL_FAILURE );
95 if ( !W_ERROR_IS_OK( out.status ) )
96 return out.status;
98 memcpy( hSCM, &out.handle, sizeof(POLICY_HND) );
100 return out.status;
103 /********************************************************************
104 ********************************************************************/
106 WERROR cli_svcctl_open_service( struct cli_state *cli, TALLOC_CTX *mem_ctx,
107 POLICY_HND *hSCM, POLICY_HND *hService,
108 const char *servicename, uint32 access_desired )
110 SVCCTL_Q_OPEN_SERVICE in;
111 SVCCTL_R_OPEN_SERVICE out;
112 prs_struct qbuf, rbuf;
114 ZERO_STRUCT(in);
115 ZERO_STRUCT(out);
117 memcpy( &in.handle, hSCM, sizeof(POLICY_HND) );
118 init_unistr2( &in.servicename, servicename, UNI_STR_TERMINATE );
119 in.access = access_desired;
121 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_OPEN_SERVICE_W,
122 in, out,
123 qbuf, rbuf,
124 svcctl_io_q_open_service,
125 svcctl_io_r_open_service,
126 WERR_GENERAL_FAILURE );
128 if ( !W_ERROR_IS_OK( out.status ) )
129 return out.status;
131 memcpy( hService, &out.handle, sizeof(POLICY_HND) );
133 return out.status;
136 /********************************************************************
137 ********************************************************************/
139 WERROR cli_svcctl_close_service( struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *hService )
141 SVCCTL_Q_CLOSE_SERVICE in;
142 SVCCTL_R_CLOSE_SERVICE out;
143 prs_struct qbuf, rbuf;
145 ZERO_STRUCT(in);
146 ZERO_STRUCT(out);
148 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
150 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_CLOSE_SERVICE,
151 in, out,
152 qbuf, rbuf,
153 svcctl_io_q_close_service,
154 svcctl_io_r_close_service,
155 WERR_GENERAL_FAILURE );
157 return out.status;
160 /*******************************************************************
161 *******************************************************************/
163 WERROR cli_svcctl_enumerate_services( struct cli_state *cli, TALLOC_CTX *mem_ctx,
164 POLICY_HND *hSCM, uint32 type, uint32 state,
165 uint32 *returned, ENUM_SERVICES_STATUS **service_array )
167 SVCCTL_Q_ENUM_SERVICES_STATUS in;
168 SVCCTL_R_ENUM_SERVICES_STATUS out;
169 prs_struct qbuf, rbuf;
170 uint32 resume = 0;
171 ENUM_SERVICES_STATUS *services;
172 int i;
174 ZERO_STRUCT(in);
175 ZERO_STRUCT(out);
177 /* setup the request */
179 memcpy( &in.handle, hSCM, sizeof(POLICY_HND) );
181 in.type = type;
182 in.state = state;
183 in.resume = &resume;
185 /* first time is to get the buffer size */
186 in.buffer_size = 0;
188 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_ENUM_SERVICES_STATUS_W,
189 in, out,
190 qbuf, rbuf,
191 svcctl_io_q_enum_services_status,
192 svcctl_io_r_enum_services_status,
193 WERR_GENERAL_FAILURE );
195 /* second time with correct buffer size...should be ok */
197 if ( W_ERROR_EQUAL( out.status, WERR_MORE_DATA ) ) {
198 in.buffer_size = out.needed;
200 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_ENUM_SERVICES_STATUS_W,
201 in, out,
202 qbuf, rbuf,
203 svcctl_io_q_enum_services_status,
204 svcctl_io_r_enum_services_status,
205 WERR_GENERAL_FAILURE );
208 if ( !W_ERROR_IS_OK(out.status) )
209 return out.status;
211 /* pull out the data */
212 if ( !(services = TALLOC_ARRAY( mem_ctx, ENUM_SERVICES_STATUS, out.returned )) )
213 return WERR_NOMEM;
215 for ( i=0; i<out.returned; i++ ) {
216 svcctl_io_enum_services_status( "", &services[i], &out.buffer, 0 );
219 *service_array = services;
220 *returned = out.returned;
222 return out.status;
225 /*******************************************************************
226 *******************************************************************/
228 WERROR cli_svcctl_query_status( struct cli_state *cli, TALLOC_CTX *mem_ctx,
229 POLICY_HND *hService, SERVICE_STATUS *status )
231 SVCCTL_Q_QUERY_STATUS in;
232 SVCCTL_R_QUERY_STATUS out;
233 prs_struct qbuf, rbuf;
235 ZERO_STRUCT(in);
236 ZERO_STRUCT(out);
238 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
240 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_STATUS,
241 in, out,
242 qbuf, rbuf,
243 svcctl_io_q_query_status,
244 svcctl_io_r_query_status,
245 WERR_GENERAL_FAILURE );
247 if ( !W_ERROR_IS_OK( out.status ) )
248 return out.status;
250 memcpy( status, &out.svc_status, sizeof(SERVICE_STATUS) );
252 return out.status;
255 /*******************************************************************
256 *******************************************************************/
258 WERROR cli_svcctl_query_config(struct cli_state *cli, TALLOC_CTX *mem_ctx,
259 POLICY_HND *hService, SERVICE_CONFIG *config )
261 SVCCTL_Q_QUERY_SERVICE_CONFIG in;
262 SVCCTL_R_QUERY_SERVICE_CONFIG out;
263 prs_struct qbuf, rbuf;
265 ZERO_STRUCT(in);
266 ZERO_STRUCT(out);
268 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
269 in.buffer_size = 0;
272 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W,
273 in, out,
274 qbuf, rbuf,
275 svcctl_io_q_query_service_config,
276 svcctl_io_r_query_service_config,
277 WERR_GENERAL_FAILURE );
279 if ( W_ERROR_EQUAL( out.status, WERR_INSUFFICIENT_BUFFER ) ) {
280 in.buffer_size = out.needed;
282 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W,
283 in, out,
284 qbuf, rbuf,
285 svcctl_io_q_query_service_config,
286 svcctl_io_r_query_service_config,
287 WERR_GENERAL_FAILURE );
290 if ( !W_ERROR_IS_OK( out.status ) )
291 return out.status;
293 memcpy( config, &out.config, sizeof(SERVICE_CONFIG) );
295 config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
296 config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
297 config->dependencies = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
298 config->startname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
299 config->displayname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
301 copy_unistr2( config->executablepath, out.config.executablepath );
302 copy_unistr2( config->loadordergroup, out.config.loadordergroup );
303 copy_unistr2( config->dependencies, out.config.dependencies );
304 copy_unistr2( config->startname, out.config.startname );
305 copy_unistr2( config->displayname, out.config.displayname );
307 return out.status;
310 /*******************************************************************
311 *******************************************************************/
313 WERROR cli_svcctl_start_service( struct cli_state *cli, TALLOC_CTX *mem_ctx,
314 POLICY_HND *hService,
315 const char **parm_array, uint32 parmcount )
317 SVCCTL_Q_START_SERVICE in;
318 SVCCTL_R_START_SERVICE out;
319 prs_struct qbuf, rbuf;
321 ZERO_STRUCT(in);
322 ZERO_STRUCT(out);
324 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
326 in.parmcount = 0;
327 in.parameters = NULL;
329 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_START_SERVICE_W,
330 in, out,
331 qbuf, rbuf,
332 svcctl_io_q_start_service,
333 svcctl_io_r_start_service,
334 WERR_GENERAL_FAILURE );
336 return out.status;
339 /*******************************************************************
340 *******************************************************************/
342 WERROR cli_svcctl_control_service( struct cli_state *cli, TALLOC_CTX *mem_ctx,
343 POLICY_HND *hService, uint32 control,
344 SERVICE_STATUS *status )
346 SVCCTL_Q_CONTROL_SERVICE in;
347 SVCCTL_R_CONTROL_SERVICE out;
348 prs_struct qbuf, rbuf;
350 ZERO_STRUCT(in);
351 ZERO_STRUCT(out);
353 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
354 in.control = control;
356 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_CONTROL_SERVICE,
357 in, out,
358 qbuf, rbuf,
359 svcctl_io_q_control_service,
360 svcctl_io_r_control_service,
361 WERR_GENERAL_FAILURE );
363 if ( !W_ERROR_IS_OK( out.status ) )
364 return out.status;
366 memcpy( status, &out.svc_status, sizeof(SERVICE_STATUS) );
368 return out.status;
372 /*******************************************************************
373 *******************************************************************/
375 WERROR cli_svcctl_get_dispname( struct cli_state *cli, TALLOC_CTX *mem_ctx,
376 POLICY_HND *hService, fstring displayname )
378 SVCCTL_Q_GET_DISPLAY_NAME in;
379 SVCCTL_R_GET_DISPLAY_NAME out;
380 prs_struct qbuf, rbuf;
382 ZERO_STRUCT(in);
383 ZERO_STRUCT(out);
385 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
386 in.display_name_len = 0;
388 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_GET_DISPLAY_NAME,
389 in, out,
390 qbuf, rbuf,
391 svcctl_io_q_get_display_name,
392 svcctl_io_r_get_display_name,
393 WERR_GENERAL_FAILURE );
395 /* second time with correct buffer size...should be ok */
397 if ( W_ERROR_EQUAL( out.status, WERR_INSUFFICIENT_BUFFER ) ) {
398 in.display_name_len = out.display_name_len;
400 CLI_DO_RPC( cli, mem_ctx, PI_SVCCTL, SVCCTL_GET_DISPLAY_NAME,
401 in, out,
402 qbuf, rbuf,
403 svcctl_io_q_get_display_name,
404 svcctl_io_r_get_display_name,
405 WERR_GENERAL_FAILURE );
408 if ( !W_ERROR_IS_OK( out.status ) )
409 return out.status;
411 rpcstr_pull( displayname, out.displayname.buffer, sizeof(displayname), -1, STR_TERMINATE );
413 return out.status;