summaryrefslogtreecommitdiff
path: root/source3/rpc_client/cli_svcctl.c
blob: 9889d131f33241da1fe96f6d305852bc515dd960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *  Unix SMB/CIFS implementation.
 *  RPC Pipe client / server routines
 *  Copyright (C) Gerald Carter                   2005.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


#include "includes.h"
#include "rpc_client.h"

struct svc_state_msg {
	uint32 flag;
	const char *message;
};

static struct svc_state_msg state_msg_table[] = {
	{ SVCCTL_STOPPED,            "stopped" },
	{ SVCCTL_START_PENDING,      "start pending" },
	{ SVCCTL_STOP_PENDING,       "stop pending" },
	{ SVCCTL_RUNNING,            "running" },
	{ SVCCTL_CONTINUE_PENDING,   "resume pending" },
	{ SVCCTL_PAUSE_PENDING,      "pause pending" },
	{ SVCCTL_PAUSED,             "paused" },
	{ 0,                          NULL }
};
	

/********************************************************************
********************************************************************/
const char* svc_status_string( uint32 state )
{
	fstring msg;
	int i;
	
	fstr_sprintf( msg, "Unknown State [%d]", state );
	
	for ( i=0; state_msg_table[i].message; i++ ) {
		if ( state_msg_table[i].flag == state ) {
			fstrcpy( msg, state_msg_table[i].message );
			break;	
		}
	}
	
	return talloc_strdup(talloc_tos(), msg);
}

/*******************************************************************
*******************************************************************/

WERROR rpccli_svcctl_enumerate_services( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                      POLICY_HND *hSCM, uint32 type, uint32 state, 
				      uint32 *returned, ENUM_SERVICES_STATUS **service_array  )
{
	SVCCTL_Q_ENUM_SERVICES_STATUS in;
	SVCCTL_R_ENUM_SERVICES_STATUS out;
	prs_struct qbuf, rbuf;
	uint32 resume = 0;
	ENUM_SERVICES_STATUS *services;
	int i;

	ZERO_STRUCT(in);
	ZERO_STRUCT(out);
	
	/* setup the request */
	
	memcpy( &in.handle, hSCM, sizeof(POLICY_HND) );
	
	in.type        = type;
	in.state       = state;
	in.resume      = &resume;
	
	/* first time is to get the buffer size */
	in.buffer_size = 0;

	CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_ENUM_SERVICES_STATUS_W, 
	            in, out, 
	            qbuf, rbuf,
	            svcctl_io_q_enum_services_status,
	            svcctl_io_r_enum_services_status, 
	            WERR_GENERAL_FAILURE );

	/* second time with correct buffer size...should be ok */
	
	if ( W_ERROR_EQUAL( out.status, WERR_MORE_DATA ) ) {
		in.buffer_size = out.needed;

		CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_ENUM_SERVICES_STATUS_W, 
		            in, out, 
		            qbuf, rbuf,
		            svcctl_io_q_enum_services_status,
		            svcctl_io_r_enum_services_status, 
		            WERR_GENERAL_FAILURE );
	}
	
	if ( !W_ERROR_IS_OK(out.status) ) 
		return out.status;
		
	/* pull out the data */
	if (out.returned) {
		if ( !(services = TALLOC_ARRAY( mem_ctx, ENUM_SERVICES_STATUS, out.returned )) ) 
			return WERR_NOMEM;
	} else {
		services = NULL;
	}
		
	for ( i=0; i<out.returned; i++ ) {
		svcctl_io_enum_services_status( "", &services[i], &out.buffer, 0 );
	}
	
	*service_array = services;
	*returned      = out.returned;
	
	return out.status;
}

/*******************************************************************
*******************************************************************/

WERROR rpccli_svcctl_query_status( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                POLICY_HND *hService, SERVICE_STATUS *status )
{
	SVCCTL_Q_QUERY_STATUS in;
	SVCCTL_R_QUERY_STATUS out;
	prs_struct qbuf, rbuf;
	
	ZERO_STRUCT(in);
	ZERO_STRUCT(out);
	
	memcpy( &in.handle, hService, sizeof(POLICY_HND) );
	
	CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_STATUS, 
	            in, out, 
	            qbuf, rbuf,
	            svcctl_io_q_query_status,
	            svcctl_io_r_query_status, 
	            WERR_GENERAL_FAILURE );
	
	if ( !W_ERROR_IS_OK( out.status ) )
		return out.status;

	memcpy( status, &out.svc_status, sizeof(SERVICE_STATUS) );
	
	return out.status;
}

/*******************************************************************
*******************************************************************/

WERROR rpccli_svcctl_query_config(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                POLICY_HND *hService, SERVICE_CONFIG *config )
{
	SVCCTL_Q_QUERY_SERVICE_CONFIG in;
	SVCCTL_R_QUERY_SERVICE_CONFIG out;
	prs_struct qbuf, rbuf;
	
	ZERO_STRUCT(in);
	ZERO_STRUCT(out);
	
	memcpy( &in.handle, hService, sizeof(POLICY_HND) );
	in.buffer_size = 0;
	
	
	CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W, 
	            in, out, 
	            qbuf, rbuf,
	            svcctl_io_q_query_service_config,
	            svcctl_io_r_query_service_config, 
	            WERR_GENERAL_FAILURE );
	
	if ( W_ERROR_EQUAL( out.status, WERR_INSUFFICIENT_BUFFER ) ) {
		in.buffer_size = out.needed;

		CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_QUERY_SERVICE_CONFIG_W,
		            in, out, 
		            qbuf, rbuf,
		            svcctl_io_q_query_service_config,
		            svcctl_io_r_query_service_config, 
		            WERR_GENERAL_FAILURE );
	}
	
	if ( !W_ERROR_IS_OK( out.status ) )
		return out.status;

	memcpy( config, &out.config, sizeof(SERVICE_CONFIG) );
	
	config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
	config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
	config->dependencies   = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
	config->startname      = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
	config->displayname    = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
	
	if ( out.config.executablepath ) {
		config->executablepath = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
		copy_unistr2( config->executablepath, out.config.executablepath );
	}

	if ( out.config.loadordergroup ) {
		config->loadordergroup = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
		copy_unistr2( config->loadordergroup, out.config.loadordergroup );
	}

	if ( out.config.dependencies ) {
		config->dependencies = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
		copy_unistr2( config->dependencies, out.config.dependencies );
	}

	if ( out.config.startname ) {
		config->startname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
		copy_unistr2( config->startname, out.config.startname );
	}

	if ( out.config.displayname ) {
		config->displayname = TALLOC_ZERO_P( mem_ctx, UNISTR2 );
		copy_unistr2( config->displayname, out.config.displayname );
	}
	
	return out.status;
}

/*******************************************************************
*******************************************************************/

WERROR rpccli_svcctl_control_service( struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                   POLICY_HND *hService, uint32 control,
				   SERVICE_STATUS *status )
{
	SVCCTL_Q_CONTROL_SERVICE in;
	SVCCTL_R_CONTROL_SERVICE out;
	prs_struct qbuf, rbuf;
	
	ZERO_STRUCT(in);
	ZERO_STRUCT(out);
	
	memcpy( &in.handle, hService, sizeof(POLICY_HND) );
	in.control = control;
	
	CLI_DO_RPC_WERR( cli, mem_ctx, PI_SVCCTL, SVCCTL_CONTROL_SERVICE, 
	            in, out, 
	            qbuf, rbuf,
	            svcctl_io_q_control_service,
	            svcctl_io_r_control_service,
	            WERR_GENERAL_FAILURE );
	
	if ( !W_ERROR_IS_OK( out.status ) )
		return out.status;

	memcpy( status, &out.svc_status, sizeof(SERVICE_STATUS) );
	
	return out.status;
}