rpcclient "Service Control Manager" svcenum [-i] command.
[samba.git] / source / rpc_client / cli_svcctl.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1998,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8  *  Copyright (C) Paul Ashton                  1997-1998.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34 /****************************************************************************
35 do a SVC Open Policy
36 ****************************************************************************/
37 BOOL svc_open_sc_man(struct cli_state *cli, uint16 fnum, 
38                                 char *srv_name, char *db_name,
39                                 uint32 des_access,
40                                 POLICY_HND *hnd)
41 {
42         prs_struct rbuf;
43         prs_struct buf; 
44         SVC_Q_OPEN_SC_MAN q_o;
45         BOOL valid_pol = False;
46
47         if (hnd == NULL) return False;
48
49         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
50         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
51
52         /* create and send a MSRPC command with api SVC_OPEN_SC_MAN */
53
54         DEBUG(4,("SVC Open SC_MAN\n"));
55
56         make_svc_q_open_sc_man(&q_o, srv_name, db_name, des_access);
57
58         /* turn parameters into data stream */
59         svc_io_q_open_sc_man("", &q_o, &buf, 0);
60
61         /* send the data on \PIPE\ */
62         if (rpc_api_pipe_req(cli, fnum, SVC_OPEN_SC_MAN, &buf, &rbuf))
63         {
64                 SVC_R_OPEN_SC_MAN r_o;
65                 BOOL p;
66
67                 ZERO_STRUCT(r_o);
68
69                 svc_io_r_open_sc_man("", &r_o, &rbuf, 0);
70                 p = rbuf.offset != 0;
71
72                 if (p && r_o.status != 0)
73                 {
74                         /* report error code */
75                         DEBUG(0,("SVC_OPEN_SC_MAN: %s\n", get_nt_error_msg(r_o.status)));
76                         p = False;
77                 }
78
79                 if (p)
80                 {
81                         /* ok, at last: we're happy. return the policy handle */
82                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
83                         valid_pol = True;
84                 }
85         }
86
87         prs_mem_free(&rbuf);
88         prs_mem_free(&buf );
89
90         return valid_pol;
91 }
92
93
94 /****************************************************************************
95 do a SVC Open Service
96 ****************************************************************************/
97 BOOL svc_open_service(struct cli_state *cli, uint16 fnum, 
98                                 POLICY_HND *scm_hnd,
99                                 char *srv_name,
100                                 uint32 des_access,
101                                 POLICY_HND *hnd)
102 {
103         prs_struct rbuf;
104         prs_struct buf; 
105         SVC_Q_OPEN_SERVICE q_o;
106         BOOL valid_pol = False;
107
108         if (hnd == NULL || scm_hnd == NULL) return False;
109
110         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
111         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
112
113         /* create and send a MSRPC command with api SVC_OPEN_SERVICE */
114
115         DEBUG(4,("SVC Open Service\n"));
116
117         make_svc_q_open_service(&q_o, scm_hnd, srv_name, des_access);
118
119         /* turn parameters into data stream */
120         svc_io_q_open_service("", &q_o, &buf, 0);
121
122         /* send the data on \PIPE\ */
123         if (rpc_api_pipe_req(cli, fnum, SVC_OPEN_SERVICE, &buf, &rbuf))
124         {
125                 SVC_R_OPEN_SERVICE r_o;
126                 BOOL p;
127
128                 ZERO_STRUCT(r_o);
129
130                 svc_io_r_open_service("", &r_o, &rbuf, 0);
131                 p = rbuf.offset != 0;
132
133                 if (p && r_o.status != 0)
134                 {
135                         /* report error code */
136                         DEBUG(0,("SVC_OPEN_SC_MAN: %s\n", get_nt_error_msg(r_o.status)));
137                         p = False;
138                 }
139
140                 if (p)
141                 {
142                         /* ok, at last: we're happy. return the policy handle */
143                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
144                         valid_pol = True;
145                 }
146         }
147
148         prs_mem_free(&rbuf);
149         prs_mem_free(&buf );
150
151         return valid_pol;
152 }
153
154
155 /****************************************************************************
156 do a SVC Enumerate Services
157 ****************************************************************************/
158 BOOL svc_enum_svcs(struct cli_state *cli, uint16 fnum, 
159                                 POLICY_HND *hnd,
160                                 uint32 services_type, uint32 services_state,
161                                 uint32 *buf_size, uint32 *resume_hnd,
162                                 uint32 *dos_error,
163                                 ENUM_SRVC_STATUS **svcs, uint32 *num_svcs)
164 {
165         prs_struct rbuf;
166         prs_struct buf; 
167         SVC_Q_ENUM_SVCS_STATUS q_o;
168         BOOL valid_pol = False;
169
170         if (hnd == NULL || buf_size == NULL || dos_error == NULL || num_svcs == NULL)
171         {
172                 return False;
173         }
174
175         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
176         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
177
178         /* create and send a MSRPC command with api SVC_ENUM_SVCS_STATUS */
179
180         DEBUG(4,("SVC Enum Services Status\n"));
181
182         make_svc_q_enum_svcs_status(&q_o, hnd,
183                                     services_type, services_state,
184                                     *buf_size, *resume_hnd);
185
186         /* turn parameters into data stream */
187         svc_io_q_enum_svcs_status("", &q_o, &buf, 0);
188
189         /* send the data on \PIPE\ */
190         if (rpc_api_pipe_req(cli, fnum, SVC_ENUM_SVCS_STATUS, &buf, &rbuf))
191         {
192                 SVC_R_ENUM_SVCS_STATUS r_o;
193                 BOOL p;
194
195                 ZERO_STRUCT(r_o);
196
197                 svc_io_r_enum_svcs_status("", &r_o, &rbuf, 0);
198                 p = rbuf.offset != 0;
199
200                 if (p && r_o.dos_status != 0)
201                 {
202                         /* report error code */
203                         DEBUG(0,("SVC_ENUM_SVCS_STATUS: %s\n", smb_err_msg(ERRDOS, r_o.dos_status)));
204                         p = r_o.dos_status == ERRmoredata;
205                 }
206
207                 if (p)
208                 {
209                         (*svcs) = r_o.svcs;
210                         (*num_svcs) = r_o.num_svcs;
211                         (*resume_hnd) = get_enum_hnd(&r_o.resume_hnd);
212                         (*buf_size) = r_o.more_buf_size;
213                         (*dos_error) = r_o.dos_status;
214                         valid_pol = True;
215                 }
216         }
217
218         prs_mem_free(&rbuf);
219         prs_mem_free(&buf );
220
221         return valid_pol;
222 }
223
224
225 /****************************************************************************
226 do a SVC Query Service Config
227 ****************************************************************************/
228 BOOL svc_query_svc_cfg(struct cli_state *cli, uint16 fnum,
229                                 POLICY_HND *hnd,
230                                 QUERY_SERVICE_CONFIG *cfg,
231                                 uint32 *buf_size)
232 {
233         prs_struct rbuf;
234         prs_struct buf; 
235         SVC_Q_QUERY_SVC_CONFIG q_c;
236         BOOL valid_cfg = False;
237
238         if (hnd == NULL || buf_size == NULL) return False;
239
240         /* create and send a MSRPC command with api SVC_QUERY_SVC_CONFIG */
241
242         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
243         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
244
245         DEBUG(4,("SVC Query Service Config\n"));
246
247         /* store the parameters */
248         make_svc_q_query_svc_config(&q_c, hnd, *buf_size);
249
250         /* turn parameters into data stream */
251         svc_io_q_query_svc_config("", &q_c, &buf, 0);
252
253         /* send the data on \PIPE\ */
254         if (rpc_api_pipe_req(cli, fnum, SVC_QUERY_SVC_CONFIG, &buf, &rbuf))
255         {
256                 SVC_R_QUERY_SVC_CONFIG r_c;
257                 BOOL p;
258
259                 ZERO_STRUCT (r_c);
260                 ZERO_STRUCTP(cfg);
261
262                 r_c.cfg = cfg;
263
264                 svc_io_r_query_svc_config("", &r_c, &rbuf, 0);
265                 p = rbuf.offset != 0;
266
267                 if (p && r_c.status != 0)
268                 {
269                         /* report error code */
270                         DEBUG(0,("SVC_QUERY_SVC_CONFIG: %s\n", get_nt_error_msg(r_c.status)));
271                         p = False;
272                 }
273
274                 if (p)
275                 {
276                         valid_cfg = r_c.buf_size != 0;
277                 }
278         }
279
280         prs_mem_free(&rbuf);
281         prs_mem_free(&buf );
282
283         return valid_cfg;
284 }
285
286
287 /****************************************************************************
288 do a SVC Close
289 ****************************************************************************/
290 BOOL svc_close(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd)
291 {
292         prs_struct rbuf;
293         prs_struct buf; 
294         SVC_Q_CLOSE q_c;
295         BOOL valid_close = False;
296
297         if (hnd == NULL) return False;
298
299         /* create and send a MSRPC command with api SVC_CLOSE */
300
301         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
302         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
303
304         DEBUG(4,("SVC Close\n"));
305
306         /* store the parameters */
307         make_svc_q_close(&q_c, hnd);
308
309         /* turn parameters into data stream */
310         svc_io_q_close("", &q_c, &buf, 0);
311
312         /* send the data on \PIPE\ */
313         if (rpc_api_pipe_req(cli, fnum, SVC_CLOSE, &buf, &rbuf))
314         {
315                 SVC_R_CLOSE r_c;
316                 BOOL p;
317
318                 ZERO_STRUCT(r_c);
319
320                 svc_io_r_close("", &r_c, &rbuf, 0);
321                 p = rbuf.offset != 0;
322
323                 if (p && r_c.status != 0)
324                 {
325                         /* report error code */
326                         DEBUG(0,("SVC_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
327                         p = False;
328                 }
329
330                 if (p)
331                 {
332                         /* check that the returned policy handle is all zeros */
333                         int i;
334                         valid_close = True;
335
336                         for (i = 0; i < sizeof(r_c.pol.data); i++)
337                         {
338                                 if (r_c.pol.data[i] != 0)
339                                 {
340                                         valid_close = False;
341                                         break;
342                                 }
343                         }       
344                         if (!valid_close)
345                         {
346                                 DEBUG(0,("SVC_CLOSE: non-zero handle returned\n"));
347                         }
348                 }
349         }
350
351         prs_mem_free(&rbuf);
352         prs_mem_free(&buf );
353
354         return valid_close;
355 }
356
357