summaryrefslogtreecommitdiff
path: root/source3/rpc_client/cli_svcctl.c
blob: 1e1fe884ff3b513328bc51c1240433071a430e7d (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

/* 
 *  Unix SMB/Netbios implementation.
 *  Version 1.9.
 *  RPC Pipe client / server routines
 *  Copyright (C) Andrew Tridgell              1992-1998,
 *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
 *  Copyright (C) Paul Ashton                  1997-1998.
 *  
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#ifdef SYSLOG
#undef SYSLOG
#endif

#include "includes.h"

extern int DEBUGLEVEL;

/****************************************************************************
do a SVC Open Policy
****************************************************************************/
BOOL svc_open_sc_man(struct cli_state *cli, uint16 fnum, 
				char *srv_name, char *db_name,
				uint32 des_access,
				POLICY_HND *hnd)
{
	prs_struct rbuf;
	prs_struct buf; 
	SVC_Q_OPEN_SC_MAN q_o;
	BOOL valid_pol = False;

	if (hnd == NULL) return False;

	prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
	prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );

	/* create and send a MSRPC command with api SVC_OPEN_SC_MAN */

	DEBUG(4,("SVC Open SC_MAN\n"));

	make_svc_q_open_sc_man(&q_o, srv_name, db_name, des_access);

	/* turn parameters into data stream */
	svc_io_q_open_sc_man("", &q_o, &buf, 0);

	/* send the data on \PIPE\ */
	if (rpc_api_pipe_req(cli, fnum, SVC_OPEN_SC_MAN, &buf, &rbuf))
	{
		SVC_R_OPEN_SC_MAN r_o;
		BOOL p;

		ZERO_STRUCT(r_o);

		svc_io_r_open_sc_man("", &r_o, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_o.status != 0)
		{
			/* report error code */
			DEBUG(1,("SVC_OPEN_SC_MAN: %s\n", get_nt_error_msg(r_o.status)));
			p = False;
		}

		if (p)
		{
			/* ok, at last: we're happy. return the policy handle */
			memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
			valid_pol = True;
		}
	}

	prs_mem_free(&rbuf);
	prs_mem_free(&buf );

	return valid_pol;
}


/****************************************************************************
do a SVC Open Service
****************************************************************************/
BOOL svc_open_service(struct cli_state *cli, uint16 fnum, 
				POLICY_HND *scm_hnd,
				const char *srv_name,
				uint32 des_access,
				POLICY_HND *hnd)
{
	prs_struct rbuf;
	prs_struct buf; 
	SVC_Q_OPEN_SERVICE q_o;
	BOOL valid_pol = False;

	if (hnd == NULL || scm_hnd == NULL) return False;

	prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
	prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );

	/* create and send a MSRPC command with api SVC_OPEN_SERVICE */

	DEBUG(4,("SVC Open Service\n"));

	make_svc_q_open_service(&q_o, scm_hnd, srv_name, des_access);

	/* turn parameters into data stream */
	svc_io_q_open_service("", &q_o, &buf, 0);

	/* send the data on \PIPE\ */
	if (rpc_api_pipe_req(cli, fnum, SVC_OPEN_SERVICE, &buf, &rbuf))
	{
		SVC_R_OPEN_SERVICE r_o;
		BOOL p;

		ZERO_STRUCT(r_o);

		svc_io_r_open_service("", &r_o, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_o.status != 0)
		{
			/* report error code */
			DEBUG(1,("SVC_OPEN_SC_MAN: %s\n", get_nt_error_msg(r_o.status)));
			p = False;
		}

		if (p)
		{
			/* ok, at last: we're happy. return the policy handle */
			memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
			valid_pol = True;
		}
	}

	prs_mem_free(&rbuf);
	prs_mem_free(&buf );

	return valid_pol;
}


/****************************************************************************
do a SVC Enumerate Services
****************************************************************************/
BOOL svc_enum_svcs(struct cli_state *cli, uint16 fnum, 
				POLICY_HND *hnd,
				uint32 services_type, uint32 services_state,
				uint32 *buf_size, uint32 *resume_hnd,
				uint32 *dos_error,
				ENUM_SRVC_STATUS **svcs, uint32 *num_svcs)
{
	prs_struct rbuf;
	prs_struct buf; 
	SVC_Q_ENUM_SVCS_STATUS q_o;
	BOOL valid_pol = False;

	if (hnd == NULL || buf_size == NULL || dos_error == NULL || num_svcs == NULL)
	{
		return False;
	}

	prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
	prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );

	/* create and send a MSRPC command with api SVC_ENUM_SVCS_STATUS */

	DEBUG(4,("SVC Enum Services Status\n"));

	make_svc_q_enum_svcs_status(&q_o, hnd,
	                            services_type, services_state,
	                            *buf_size, *resume_hnd);

	/* turn parameters into data stream */
	svc_io_q_enum_svcs_status("", &q_o, &buf, 0);

	/* send the data on \PIPE\ */
	if (rpc_api_pipe_req(cli, fnum, SVC_ENUM_SVCS_STATUS, &buf, &rbuf))
	{
		SVC_R_ENUM_SVCS_STATUS r_o;
		BOOL p;

		ZERO_STRUCT(r_o);

		svc_io_r_enum_svcs_status("", &r_o, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_o.dos_status != 0)
		{
			fstring errmsg;

			if (r_o.dos_status != ERRmoredata)
			{
				smb_safe_err_msg(ERRDOS, r_o.dos_status,
				                 errmsg, sizeof(errmsg));
				/* report error code */
				DEBUG(1,("SVC_ENUM_SVCS_STATUS: %s\n", errmsg));
			}
			p = r_o.dos_status == ERRmoredata;
		}

		if (p)
		{
			(*svcs) = r_o.svcs;
			(*num_svcs) = r_o.num_svcs;
			(*resume_hnd) = get_enum_hnd(&r_o.resume_hnd);
			(*buf_size) = r_o.more_buf_size;
			(*dos_error) = r_o.dos_status;
			valid_pol = True;
		}
	}

	prs_mem_free(&rbuf);
	prs_mem_free(&buf );

	return valid_pol;
}


/****************************************************************************
do a SVC Start Service 
****************************************************************************/
BOOL svc_start_service(struct cli_state *cli, uint16 fnum,
				POLICY_HND *hnd,
				uint32 argc,
				char **argv)
{
	prs_struct rbuf;
	prs_struct buf; 
	SVC_Q_START_SERVICE q_c;
	BOOL valid_cfg = False;

	if (hnd == NULL) return False;

	/* create and send a MSRPC command with api SVC_START_SERVICE */

	prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
	prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );

	DEBUG(4,("SVC Start Service\n"));

	/* store the parameters */
	make_svc_q_start_service(&q_c, hnd, argc, argv);

	/* turn parameters into data stream */
	svc_io_q_start_service("", &q_c, &buf, 0);

	/* send the data on \PIPE\ */
	if (rpc_api_pipe_req(cli, fnum, SVC_START_SERVICE, &buf, &rbuf))
	{
		SVC_R_START_SERVICE r_c;
		BOOL p;

		ZERO_STRUCT (r_c);

		svc_io_r_start_service("", &r_c, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_c.status != 0)
		{
			/* report error code */
			DEBUG(1,("SVC_START_SERVICE: %s\n", get_nt_error_msg(r_c.status)));
			p = False;
		}

		if (p)
		{
			valid_cfg = True;
		}
	}

	prs_mem_free(&rbuf);
	prs_mem_free(&buf );

	return valid_cfg;
}


/****************************************************************************
do a SVC Query Service Config
****************************************************************************/
BOOL svc_query_svc_cfg(struct cli_state *cli, uint16 fnum,
				POLICY_HND *hnd,
				QUERY_SERVICE_CONFIG *cfg,
				uint32 *buf_size)
{
	prs_struct rbuf;
	prs_struct buf; 
	SVC_Q_QUERY_SVC_CONFIG q_c;
	BOOL valid_cfg = False;

	if (hnd == NULL || buf_size == NULL) return False;

	/* create and send a MSRPC command with api SVC_QUERY_SVC_CONFIG */

	prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
	prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );

	DEBUG(4,("SVC Query Service Config\n"));

	/* store the parameters */
	make_svc_q_query_svc_config(&q_c, hnd, *buf_size);

	/* turn parameters into data stream */
	svc_io_q_query_svc_config("", &q_c, &buf, 0);

	/* send the data on \PIPE\ */
	if (rpc_api_pipe_req(cli, fnum, SVC_QUERY_SVC_CONFIG, &buf, &rbuf))
	{
		SVC_R_QUERY_SVC_CONFIG r_c;
		BOOL p;

		ZERO_STRUCT (r_c);
		ZERO_STRUCTP(cfg);

		r_c.cfg = cfg;

		svc_io_r_query_svc_config("", &r_c, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_c.status != 0)
		{
			/* report error code */
			DEBUG(1,("SVC_QUERY_SVC_CONFIG: %s\n", get_nt_error_msg(r_c.status)));
			p = False;
		}

		if (p)
		{
			valid_cfg = r_c.buf_size != 0;
		}
	}

	prs_mem_free(&rbuf);
	prs_mem_free(&buf );

	return valid_cfg;
}


/****************************************************************************
do a SVC Close
****************************************************************************/
BOOL svc_close(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd)
{
	prs_struct rbuf;
	prs_struct buf; 
	SVC_Q_CLOSE q_c;
	BOOL valid_close = False;

	if (hnd == NULL) return False;

	/* create and send a MSRPC command with api SVC_CLOSE */

	prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
	prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );

	DEBUG(4,("SVC Close\n"));

	/* store the parameters */
	make_svc_q_close(&q_c, hnd);

	/* turn parameters into data stream */
	svc_io_q_close("", &q_c, &buf, 0);

	/* send the data on \PIPE\ */
	if (rpc_api_pipe_req(cli, fnum, SVC_CLOSE, &buf, &rbuf))
	{
		SVC_R_CLOSE r_c;
		BOOL p;

		ZERO_STRUCT(r_c);

		svc_io_r_close("", &r_c, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_c.status != 0)
		{
			/* report error code */
			DEBUG(1,("SVC_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
			p = False;
		}

		if (p)
		{
			/* check that the returned policy handle is all zeros */
			uint32 i;
			valid_close = True;

			for (i = 0; i < sizeof(r_c.pol.data); i++)
			{
				if (r_c.pol.data[i] != 0)
				{
					valid_close = False;
					break;
				}
			}	
			if (!valid_close)
			{
				DEBUG(1,("SVC_CLOSE: non-zero handle returned\n"));
			}
		}
	}

	prs_mem_free(&rbuf);
	prs_mem_free(&buf );

	return valid_close;
}