source: branches/samba-3.0/source/rpc_client/cli_svcctl.c

Last change on this file was 39, checked in by Paul Smedley, 18 years ago

Upgrade source to 3.0.25a

File size: 13.1 KB
Line 
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 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
21
22#include "includes.h"
23#include "rpc_client.h"
24
25struct svc_state_msg {
26 uint32 flag;
27 const char *message;
28};
29
30static 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 }
39};
40
41
42/********************************************************************
43********************************************************************/
44const char* svc_status_string( uint32 state )
45{
46 static 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 msg;
59}
60
61/********************************************************************
62********************************************************************/
63
64WERROR rpccli_svcctl_open_scm(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
65 POLICY_HND *hSCM, uint32 access_desired )
66{
67 SVCCTL_Q_OPEN_SCMANAGER in;
68 SVCCTL_R_OPEN_SCMANAGER out;
69 prs_struct qbuf, rbuf;
70 fstring server;
71
72 ZERO_STRUCT(in);
73 ZERO_STRUCT(out);
74
75 /* leave the database name NULL to get the default service db */
76
77 in.database = NULL;
78
79 /* set the server name */
80
81 if ( !(in.servername = TALLOC_P( mem_ctx, UNISTR2 )) )
82 return WERR_NOMEM;
83 fstr_sprintf( server, "\\\\%s", cli->cli->desthost );
84 init_unistr2( in.servername, server, UNI_STR_TERMINATE );
85
86 in.access = access_desired;
87
88 CLI_DO_RPC_WERR( 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 );
94
95 if ( !W_ERROR_IS_OK( out.status ) )
96 return out.status;
97
98 memcpy( hSCM, &out.handle, sizeof(POLICY_HND) );
99
100 return out.status;
101}
102
103/********************************************************************
104********************************************************************/
105
106WERROR rpccli_svcctl_open_service( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
107 POLICY_HND *hSCM, POLICY_HND *hService,
108 const char *servicename, uint32 access_desired )
109{
110 SVCCTL_Q_OPEN_SERVICE in;
111 SVCCTL_R_OPEN_SERVICE out;
112 prs_struct qbuf, rbuf;
113
114 ZERO_STRUCT(in);
115 ZERO_STRUCT(out);
116
117 memcpy( &in.handle, hSCM, sizeof(POLICY_HND) );
118 init_unistr2( &in.servicename, servicename, UNI_STR_TERMINATE );
119 in.access = access_desired;
120
121 CLI_DO_RPC_WERR( 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 );
127
128 if ( !W_ERROR_IS_OK( out.status ) )
129 return out.status;
130
131 memcpy( hService, &out.handle, sizeof(POLICY_HND) );
132
133 return out.status;
134}
135
136/********************************************************************
137********************************************************************/
138
139WERROR rpccli_svcctl_close_service(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, POLICY_HND *hService )
140{
141 SVCCTL_Q_CLOSE_SERVICE in;
142 SVCCTL_R_CLOSE_SERVICE out;
143 prs_struct qbuf, rbuf;
144
145 ZERO_STRUCT(in);
146 ZERO_STRUCT(out);
147
148 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
149
150 CLI_DO_RPC_WERR( 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 );
156
157 return out.status;
158}
159
160/*******************************************************************
161*******************************************************************/
162
163WERROR rpccli_svcctl_enumerate_services( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
164 POLICY_HND *hSCM, uint32 type, uint32 state,
165 uint32 *returned, ENUM_SERVICES_STATUS **service_array )
166{
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;
173
174 ZERO_STRUCT(in);
175 ZERO_STRUCT(out);
176
177 /* setup the request */
178
179 memcpy( &in.handle, hSCM, sizeof(POLICY_HND) );
180
181 in.type = type;
182 in.state = state;
183 in.resume = &resume;
184
185 /* first time is to get the buffer size */
186 in.buffer_size = 0;
187
188 CLI_DO_RPC_WERR( 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 );
194
195 /* second time with correct buffer size...should be ok */
196
197 if ( W_ERROR_EQUAL( out.status, WERR_MORE_DATA ) ) {
198 in.buffer_size = out.needed;
199
200 CLI_DO_RPC_WERR( 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 );
206 }
207
208 if ( !W_ERROR_IS_OK(out.status) )
209 return out.status;
210
211 /* pull out the data */
212 if (out.returned) {
213 if ( !(services = TALLOC_ARRAY( mem_ctx, ENUM_SERVICES_STATUS, out.returned )) )
214 return WERR_NOMEM;
215 } else {
216 services = NULL;
217 }
218
219 for ( i=0; i<out.returned; i++ ) {
220 svcctl_io_enum_services_status( "", &services[i], &out.buffer, 0 );
221 }
222
223 *service_array = services;
224 *returned = out.returned;
225
226 return out.status;
227}
228
229/*******************************************************************
230*******************************************************************/
231
232WERROR rpccli_svcctl_query_status( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
233 POLICY_HND *hService, SERVICE_STATUS *status )
234{
235 SVCCTL_Q_QUERY_STATUS in;
236 SVCCTL_R_QUERY_STATUS out;
237 prs_struct qbuf, rbuf;
238
239 ZERO_STRUCT(in);
240 ZERO_STRUCT(out);
241
242 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
243
244 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_STATUS,
245 in, out,
246 qbuf, rbuf,
247 svcctl_io_q_query_status,
248 svcctl_io_r_query_status,
249 WERR_GENERAL_FAILURE );
250
251 if ( !W_ERROR_IS_OK( out.status ) )
252 return out.status;
253
254 memcpy( status, &out.svc_status, sizeof(SERVICE_STATUS) );
255
256 return out.status;
257}
258
259/*******************************************************************
260*******************************************************************/
261
262WERROR rpccli_svcctl_query_config(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
263 POLICY_HND *hService, SERVICE_CONFIG *config )
264{
265 SVCCTL_Q_QUERY_SERVICE_CONFIG in;
266 SVCCTL_R_QUERY_SERVICE_CONFIG out;
267 prs_struct qbuf, rbuf;
268
269 ZERO_STRUCT(in);
270 ZERO_STRUCT(out);
271
272 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
273 in.buffer_size = 0;
274
275
276 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W,
277 in, out,
278 qbuf, rbuf,
279 svcctl_io_q_query_service_config,
280 svcctl_io_r_query_service_config,
281 WERR_GENERAL_FAILURE );
282
283 if ( W_ERROR_EQUAL( out.status, WERR_INSUFFICIENT_BUFFER ) ) {
284 in.buffer_size = out.needed;
285
286 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W,
287 in, out,
288 qbuf, rbuf,
289 svcctl_io_q_query_service_config,
290 svcctl_io_r_query_service_config,
291 WERR_GENERAL_FAILURE );
292 }
293
294 if ( !W_ERROR_IS_OK( out.status ) )
295 return out.status;
296
297 memcpy( config, &out.config, sizeof(SERVICE_CONFIG) );
298
299 config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
300 config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
301 config->dependencies = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
302 config->startname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
303 config->displayname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
304
305 if ( out.config.executablepath ) {
306 config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
307 copy_unistr2( config->executablepath, out.config.executablepath );
308 }
309
310 if ( out.config.loadordergroup ) {
311 config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
312 copy_unistr2( config->loadordergroup, out.config.loadordergroup );
313 }
314
315 if ( out.config.dependencies ) {
316 config->dependencies = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
317 copy_unistr2( config->dependencies, out.config.dependencies );
318 }
319
320 if ( out.config.startname ) {
321 config->startname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
322 copy_unistr2( config->startname, out.config.startname );
323 }
324
325 if ( out.config.displayname ) {
326 config->displayname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
327 copy_unistr2( config->displayname, out.config.displayname );
328 }
329
330 return out.status;
331}
332
333/*******************************************************************
334*******************************************************************/
335
336WERROR rpccli_svcctl_start_service( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
337 POLICY_HND *hService,
338 const char **parm_array, uint32 parmcount )
339{
340 SVCCTL_Q_START_SERVICE in;
341 SVCCTL_R_START_SERVICE out;
342 prs_struct qbuf, rbuf;
343
344 ZERO_STRUCT(in);
345 ZERO_STRUCT(out);
346
347 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
348
349 in.parmcount = 0;
350 in.parameters = NULL;
351
352 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_START_SERVICE_W,
353 in, out,
354 qbuf, rbuf,
355 svcctl_io_q_start_service,
356 svcctl_io_r_start_service,
357 WERR_GENERAL_FAILURE );
358
359 return out.status;
360}
361
362/*******************************************************************
363*******************************************************************/
364
365WERROR rpccli_svcctl_control_service( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
366 POLICY_HND *hService, uint32 control,
367 SERVICE_STATUS *status )
368{
369 SVCCTL_Q_CONTROL_SERVICE in;
370 SVCCTL_R_CONTROL_SERVICE out;
371 prs_struct qbuf, rbuf;
372
373 ZERO_STRUCT(in);
374 ZERO_STRUCT(out);
375
376 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
377 in.control = control;
378
379 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_CONTROL_SERVICE,
380 in, out,
381 qbuf, rbuf,
382 svcctl_io_q_control_service,
383 svcctl_io_r_control_service,
384 WERR_GENERAL_FAILURE );
385
386 if ( !W_ERROR_IS_OK( out.status ) )
387 return out.status;
388
389 memcpy( status, &out.svc_status, sizeof(SERVICE_STATUS) );
390
391 return out.status;
392}
393
394
395/*******************************************************************
396*******************************************************************/
397
398WERROR rpccli_svcctl_get_dispname( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
399 POLICY_HND *hService, fstring displayname )
400{
401 SVCCTL_Q_GET_DISPLAY_NAME in;
402 SVCCTL_R_GET_DISPLAY_NAME out;
403 prs_struct qbuf, rbuf;
404
405 ZERO_STRUCT(in);
406 ZERO_STRUCT(out);
407
408 memcpy( &in.handle, hService, sizeof(POLICY_HND) );
409 in.display_name_len = 0;
410
411 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_GET_DISPLAY_NAME,
412 in, out,
413 qbuf, rbuf,
414 svcctl_io_q_get_display_name,
415 svcctl_io_r_get_display_name,
416 WERR_GENERAL_FAILURE );
417
418 /* second time with correct buffer size...should be ok */
419
420 if ( W_ERROR_EQUAL( out.status, WERR_INSUFFICIENT_BUFFER ) ) {
421 in.display_name_len = out.display_name_len;
422
423 CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_GET_DISPLAY_NAME,
424 in, out,
425 qbuf, rbuf,
426 svcctl_io_q_get_display_name,
427 svcctl_io_r_get_display_name,
428 WERR_GENERAL_FAILURE );
429 }
430
431 if ( !W_ERROR_IS_OK( out.status ) )
432 return out.status;
433
434 rpcstr_pull( displayname, out.displayname.buffer, sizeof(displayname), -1, STR_TERMINATE );
435
436 return out.status;
437}
Note: See TracBrowser for help on using the repository browser.