e67cafb9df8ace2d5a9c387769503d9ffc45eccc
[tprouty/samba.git] / source / rpc_server / srv_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-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *  Copyright (C) Paul Ashton                       1997.
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 #include "includes.h"
27 #include "nterr.h"
28
29 extern int DEBUGLEVEL;
30
31
32 /*******************************************************************
33  svc_reply_unknown_1
34  ********************************************************************/
35 static void svc_reply_close(SVC_Q_CLOSE *q_r,
36                                 prs_struct *rdata)
37 {
38         SVC_R_CLOSE r_u;
39
40         /* set up the REG unknown_1 response */
41         bzero(r_u.pol.data, POL_HND_SIZE);
42
43         /* close the policy handle */
44         if (close_policy_hnd(&(q_r->pol)))
45         {
46                 r_u.status = 0;
47         }
48         else
49         {
50                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_INVALID;
51         }
52
53         DEBUG(5,("svc_unknown_1: %d\n", __LINE__));
54
55         /* store the response in the SMB stream */
56         svc_io_r_close("", &r_u, rdata, 0);
57
58         DEBUG(5,("svc_unknown_1: %d\n", __LINE__));
59 }
60
61 /*******************************************************************
62  api_svc_close
63  ********************************************************************/
64 static void api_svc_close( pipes_struct *p, prs_struct *data,
65                                     prs_struct *rdata )
66 {
67         SVC_Q_CLOSE q_r;
68         svc_io_q_close("", &q_r, data, 0);
69         svc_reply_close(&q_r, rdata);
70 }
71
72
73 /*******************************************************************
74  svc_reply_open_service
75  ********************************************************************/
76 static void svc_reply_open_service(SVC_Q_OPEN_SERVICE *q_u,
77                                 prs_struct *rdata)
78 {
79         uint32 status     = 0;
80         POLICY_HND pol;
81         SVC_R_OPEN_SERVICE r_u;
82         fstring name;
83
84         DEBUG(5,("svc_open_service: %d\n", __LINE__));
85
86         if (status == 0x0 && find_policy_by_hnd(&q_u->scman_pol) == -1)
87         {
88                 status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
89         }
90
91         if (status == 0x0 && !open_policy_hnd(&pol))
92         {
93                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
94         }
95
96         unistr2_to_ascii(name, &q_u->uni_svc_name, sizeof(name)-1);
97
98         if (status == 0x0)
99         {
100                 DEBUG(5,("svc_open_service: %s\n", name));
101                 /* lkcl XXXX do a check on the name, here */
102         }
103
104         if (status == 0x0 && !set_policy_reg_name(&pol, name))
105         {
106                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
107         }
108
109         make_svc_r_open_service(&r_u, &pol, status);
110
111         /* store the response in the SMB stream */
112         svc_io_r_open_service("", &r_u, rdata, 0);
113
114         DEBUG(5,("svc_open_service: %d\n", __LINE__));
115 }
116
117 /*******************************************************************
118  api_svc_open_service
119  ********************************************************************/
120 static void api_svc_open_service( pipes_struct *p, prs_struct *data,
121                                     prs_struct *rdata )
122 {
123         SVC_Q_OPEN_SERVICE q_u;
124         svc_io_q_open_service("", &q_u, data, 0);
125         svc_reply_open_service(&q_u, rdata);
126 }
127
128 /*******************************************************************
129  svc_reply_start_service
130  ********************************************************************/
131 static void svc_reply_start_service(SVC_Q_START_SERVICE *q_s,
132                                 prs_struct *rdata)
133 {
134         SVC_R_START_SERVICE r_s;
135
136         DEBUG(5,("svc_start_service: %d\n", __LINE__));
137
138         r_s.status = 0x0;
139
140         if (find_policy_by_hnd(&q_s->pol) == -1)
141         {
142                 r_s.status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
143         }
144
145         /* start the service here */
146
147         /* store the response in the SMB stream */
148         svc_io_r_start_service("", &r_s, rdata, 0);
149
150         DEBUG(5,("svc_start_service: %d\n", __LINE__));
151 }
152
153 /*******************************************************************
154  api_svc_start_service
155  ********************************************************************/
156 static void api_svc_start_service( pipes_struct *p, prs_struct *data,
157                                     prs_struct *rdata )
158 {
159         SVC_Q_START_SERVICE q_u;
160         svc_io_q_start_service("", &q_u, data, 0);
161         svc_reply_start_service(&q_u, rdata);
162 }
163
164 /*******************************************************************
165  svc_reply_open_sc_man
166  ********************************************************************/
167 static void svc_reply_open_sc_man(SVC_Q_OPEN_SC_MAN *q_u,
168                                 prs_struct *rdata)
169 {
170         uint32 status     = 0;
171         POLICY_HND pol;
172         SVC_R_OPEN_SC_MAN r_u;
173         fstring name;
174
175         DEBUG(5,("svc_open_sc_man: %d\n", __LINE__));
176
177         if (status == 0x0 && !open_policy_hnd(&pol))
178         {
179                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
180         }
181
182         unistr2_to_ascii(name, &q_u->uni_srv_name, sizeof(name)-1);
183
184         if (status == 0x0)
185         {
186                 DEBUG(5,("svc_open_sc_man: %s\n", name));
187                 /* lkcl XXXX do a check on the name, here */
188         }
189
190         if (status == 0x0 && !set_policy_reg_name(&pol, name))
191         {
192                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
193         }
194
195         make_svc_r_open_sc_man(&r_u, &pol, status);
196
197         /* store the response in the SMB stream */
198         svc_io_r_open_sc_man("", &r_u, rdata, 0);
199
200         DEBUG(5,("svc_open_sc_man: %d\n", __LINE__));
201 }
202
203 /*******************************************************************
204  api_svc_open_sc_man
205  ********************************************************************/
206 static void api_svc_open_sc_man( pipes_struct *p, prs_struct *data,
207                                     prs_struct *rdata )
208 {
209         SVC_Q_OPEN_SC_MAN q_u;
210         svc_io_q_open_sc_man("", &q_u, data, 0);
211         svc_reply_open_sc_man(&q_u, rdata);
212 }
213
214 static char *dummy_services[] =
215 {
216         "imapd",
217         "popd",
218         "smbd",
219         "nmbd",
220         "httpd",
221         "inetd",
222         "syslogd",
223         NULL
224 };
225
226 /*******************************************************************
227  svc_reply_enum_svcs_status
228  ********************************************************************/
229 static void svc_reply_enum_svcs_status(SVC_Q_ENUM_SVCS_STATUS *q_u,
230                                 prs_struct *rdata)
231 {
232         uint32 dos_status = 0;
233         SVC_R_ENUM_SVCS_STATUS r_u;
234         ENUM_SRVC_STATUS *svcs = NULL;
235         int num_svcs = 0;
236         int buf_size = 0;
237         int i = get_enum_hnd(&q_u->resume_hnd);
238         uint32 resume_hnd = 0;
239         int max_buf_size = 0x10000;
240
241         ZERO_STRUCT(r_u);
242
243         DEBUG(5,("svc_enum_svcs_status: %d\n", __LINE__));
244
245         if (dos_status == 0x0 && find_policy_by_hnd(&q_u->pol) == -1)
246         {
247                 dos_status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
248         }
249
250         if (dos_status == 0x0)
251         {
252                 DEBUG(5,("svc_enum_svcs_status:\n"));
253                 while (dummy_services[i] != NULL)
254                 {
255                         ENUM_SRVC_STATUS *svc = NULL;
256
257                         buf_size += strlen(dummy_services[i] + 1) * 2;
258                         buf_size += 9 * sizeof(uint32);
259
260                         DEBUG(10,("buf_size: %d q_u->buf_size: %d\n",
261                                    buf_size, q_u->buf_size));
262
263                         if (buf_size >= max_buf_size)
264                         {
265                                 resume_hnd = i;
266                                 break;
267                         }
268
269                         if (buf_size > q_u->buf_size)
270                         {
271                                 dos_status = ERRmoredata;
272                                 break;
273                         }
274
275                         num_svcs++;
276                         svcs = Realloc(svcs, num_svcs * sizeof(ENUM_SRVC_STATUS));
277                         if (svcs == NULL)
278                         {
279                                 dos_status = ERRnomem;
280                                 num_svcs = 0;
281                                 break;
282                         }
283
284                         svc = &svcs[num_svcs-1];
285                         ZERO_STRUCTP(svc);
286
287                         make_unistr(&svc->uni_srvc_name, dummy_services[i]);
288                         make_unistr(&svc->uni_disp_name, dummy_services[i]);
289
290                         DEBUG(10,("show service: %s\n", dummy_services[i]));
291                         i++;
292                 }
293         }
294
295         /*
296          * check for finished condition: no resume handle and last buffer fits
297          */
298
299         if (resume_hnd == 0 && buf_size <= q_u->buf_size)
300         {
301                 /* this indicates, along with resume_hnd of 0, an end. */
302                 max_buf_size = 0;
303         }
304
305         make_svc_r_enum_svcs_status(&r_u, svcs, max_buf_size, num_svcs, resume_hnd, dos_status);
306
307         /* store the response in the SMB stream */
308         svc_io_r_enum_svcs_status("", &r_u, rdata, 0);
309
310         if (svcs != NULL)
311         {
312                 free(svcs);
313         }
314
315         DEBUG(5,("svc_enum_svcs_status: %d\n", __LINE__));
316 }
317
318 /*******************************************************************
319  api_svc_enum_svcs_status
320  ********************************************************************/
321 static void api_svc_enum_svcs_status( pipes_struct *p, prs_struct *data,
322                                     prs_struct *rdata )
323 {
324         SVC_Q_ENUM_SVCS_STATUS q_u;
325         svc_io_q_enum_svcs_status("", &q_u, data, 0);
326         svc_reply_enum_svcs_status(&q_u, rdata);
327 }
328
329 /*******************************************************************
330  svc_reply_query_disp_name
331  ********************************************************************/
332 static void svc_reply_query_disp_name(SVC_Q_QUERY_DISP_NAME *q_u,
333                                 prs_struct *rdata)
334 {
335         SVC_R_QUERY_DISP_NAME r_u;
336         fstring svc_name;
337         uint32 status = 0;
338
339         DEBUG(5,("svc_query_disp_name: %d\n", __LINE__));
340
341         if (find_policy_by_hnd(&q_u->scman_pol) == -1)
342         {
343                 status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
344         }
345
346         /* for now display name = service name */
347         unistr2_to_ascii(svc_name, &q_u->uni_svc_name, sizeof(svc_name)-1);
348         make_svc_r_query_disp_name(&r_u, svc_name, status);
349
350         /* store the response in the SMB stream */
351         svc_io_r_query_disp_name("", &r_u, rdata, 0);
352
353         DEBUG(5,("svc_query_disp_name: %d\n", __LINE__));
354 }
355
356 /*******************************************************************
357  api_svc_query_disp_name
358  ********************************************************************/
359 static void api_svc_query_disp_name( pipes_struct *p, prs_struct *data,
360                                     prs_struct *rdata )
361 {
362         SVC_Q_QUERY_DISP_NAME q_u;
363         svc_io_q_query_disp_name("", &q_u, data, 0);
364         svc_reply_query_disp_name(&q_u, rdata);
365 }
366
367 /*******************************************************************
368  array of \PIPE\svcctl operations
369  ********************************************************************/
370 static struct api_struct api_svc_cmds[] =
371 {
372         { "SVC_CLOSE"           , SVC_CLOSE           , api_svc_close            },
373         { "SVC_OPEN_SC_MAN"     , SVC_OPEN_SC_MAN     , api_svc_open_sc_man      },
374         { "SVC_OPEN_SERVICE"    , SVC_OPEN_SERVICE    , api_svc_open_service     },
375         { "SVC_ENUM_SVCS_STATUS", SVC_ENUM_SVCS_STATUS, api_svc_enum_svcs_status },
376         { "SVC_QUERY_DISP_NAME" , SVC_QUERY_DISP_NAME , api_svc_query_disp_name  },
377         { "SVC_START_SERVICE"   , SVC_START_SERVICE   , api_svc_start_service    },
378         { NULL                  , 0                   , NULL                     }
379 };
380
381 /*******************************************************************
382  receives a svcctl pipe and responds.
383  ********************************************************************/
384 BOOL api_svcctl_rpc(pipes_struct *p, prs_struct *data)
385 {
386         return api_rpcTNP(p, "api_svc_rpc", api_svc_cmds, data);
387 }
388