adding "Service Control Manager" commands to rpcclient.
[vlendec/samba-autobuild/.git] / source3 / 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 do_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 Enumerate Services
96 ****************************************************************************/
97 BOOL do_svc_enum_svcs(struct cli_state *cli, uint16 fnum, 
98                                 POLICY_HND *hnd,
99                                 uint32 services_type, uint32 services_state,
100                                 uint32 buf_size, uint32 *resume_hnd)
101 {
102         prs_struct rbuf;
103         prs_struct buf; 
104         SVC_Q_ENUM_SVCS_STATUS q_o;
105         BOOL valid_pol = False;
106
107         if (hnd == NULL) return False;
108
109         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
110         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
111
112         /* create and send a MSRPC command with api SVC_ENUM_SVCS_STATUS */
113
114         DEBUG(4,("SVC Enum Services Status\n"));
115
116         make_svc_q_enum_svcs_status(&q_o, hnd,
117                                     services_type, services_state,
118                                     buf_size, *resume_hnd);
119
120         /* turn parameters into data stream */
121         svc_io_q_enum_svcs_status("", &q_o, &buf, 0);
122
123         /* send the data on \PIPE\ */
124         if (rpc_api_pipe_req(cli, fnum, SVC_ENUM_SVCS_STATUS, &buf, &rbuf))
125         {
126 #if 0
127                 SVC_R_ENUM_SVCS_STATUS r_o;
128                 BOOL p;
129
130                 ZERO_STRUCT(r_o);
131
132                 svc_io_r_enum_svcs_status("", &r_o, &rbuf, 0);
133                 p = rbuf.offset != 0;
134
135                 if (p && r_o.status != 0)
136                 {
137                         /* report error code */
138                         DEBUG(0,("SVC_ENUM_SVCS_STATUS: %s\n", get_nt_error_msg(r_o.status)));
139                         p = False;
140                 }
141
142                 if (p)
143                 {
144                         /* ok, at last: we're happy. return the policy handle */
145                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
146                         valid_pol = True;
147                 }
148 #else
149         valid_pol = True;
150 #endif
151         }
152
153         prs_mem_free(&rbuf);
154         prs_mem_free(&buf );
155
156         return valid_pol;
157 }
158
159
160 /****************************************************************************
161 do a SVC Close
162 ****************************************************************************/
163 BOOL do_svc_close(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd)
164 {
165         prs_struct rbuf;
166         prs_struct buf; 
167         SVC_Q_CLOSE q_c;
168         BOOL valid_close = False;
169
170         if (hnd == NULL) return False;
171
172         /* create and send a MSRPC command with api SVC_CLOSE */
173
174         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
175         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
176
177         DEBUG(4,("SVC Close\n"));
178
179         /* store the parameters */
180         make_svc_q_close(&q_c, hnd);
181
182         /* turn parameters into data stream */
183         svc_io_q_close("", &q_c, &buf, 0);
184
185         /* send the data on \PIPE\ */
186         if (rpc_api_pipe_req(cli, fnum, SVC_CLOSE, &buf, &rbuf))
187         {
188                 SVC_R_CLOSE r_c;
189                 BOOL p;
190
191                 ZERO_STRUCT(r_c);
192
193                 svc_io_r_close("", &r_c, &rbuf, 0);
194                 p = rbuf.offset != 0;
195
196                 if (p && r_c.status != 0)
197                 {
198                         /* report error code */
199                         DEBUG(0,("SVC_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
200                         p = False;
201                 }
202
203                 if (p)
204                 {
205                         /* check that the returned policy handle is all zeros */
206                         int i;
207                         valid_close = True;
208
209                         for (i = 0; i < sizeof(r_c.pol.data); i++)
210                         {
211                                 if (r_c.pol.data[i] != 0)
212                                 {
213                                         valid_close = False;
214                                         break;
215                                 }
216                         }       
217                         if (!valid_close)
218                         {
219                                 DEBUG(0,("SVC_CLOSE: non-zero handle returned\n"));
220                         }
221                 }
222         }
223
224         prs_mem_free(&rbuf);
225         prs_mem_free(&buf );
226
227         return valid_close;
228 }
229
230