Always null-terminate strings.
[vlendec/samba-autobuild/.git] / source3 / 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_lsa_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( uint16 vuid, 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_lsa_policy_by_hnd(&q_u->scman_pol) == -1)
87         {
88                 status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
89         }
90
91         if (status == 0x0 && !open_lsa_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_lsa_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( uint16 vuid, 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_open_sc_man
130  ********************************************************************/
131 static void svc_reply_open_sc_man(SVC_Q_OPEN_SC_MAN *q_u,
132                                 prs_struct *rdata)
133 {
134         uint32 status     = 0;
135         POLICY_HND pol;
136         SVC_R_OPEN_SC_MAN r_u;
137         fstring name;
138
139         DEBUG(5,("svc_open_sc_man: %d\n", __LINE__));
140
141         if (status == 0x0 && !open_lsa_policy_hnd(&pol))
142         {
143                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
144         }
145
146         unistr2_to_ascii(name, &q_u->uni_srv_name, sizeof(name)-1);
147
148         if (status == 0x0)
149         {
150                 DEBUG(5,("svc_open_sc_man: %s\n", name));
151                 /* lkcl XXXX do a check on the name, here */
152         }
153
154         if (status == 0x0 && !set_lsa_policy_reg_name(&pol, name))
155         {
156                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
157         }
158
159         make_svc_r_open_sc_man(&r_u, &pol, status);
160
161         /* store the response in the SMB stream */
162         svc_io_r_open_sc_man("", &r_u, rdata, 0);
163
164         DEBUG(5,("svc_open_sc_man: %d\n", __LINE__));
165 }
166
167 /*******************************************************************
168  api_svc_open_sc_man
169  ********************************************************************/
170 static void api_svc_open_sc_man( uint16 vuid, prs_struct *data,
171                                     prs_struct *rdata )
172 {
173         SVC_Q_OPEN_SC_MAN q_u;
174         svc_io_q_open_sc_man("", &q_u, data, 0);
175         svc_reply_open_sc_man(&q_u, rdata);
176 }
177
178 static char *dummy_services[] =
179 {
180         "imapd",
181         "popd",
182         "smbd",
183         "nmbd",
184         "httpd",
185         "inetd",
186         "syslogd",
187         NULL
188 };
189
190 /*******************************************************************
191  svc_reply_enum_svcs_status
192  ********************************************************************/
193 static void svc_reply_enum_svcs_status(SVC_Q_ENUM_SVCS_STATUS *q_u,
194                                 prs_struct *rdata)
195 {
196         uint32 dos_status = 0;
197         SVC_R_ENUM_SVCS_STATUS r_u;
198         ENUM_SRVC_STATUS *svcs = NULL;
199         int num_svcs = 0;
200         int buf_size = 0;
201         int i = get_enum_hnd(&q_u->resume_hnd);
202         uint32 resume_hnd = 0;
203         int max_buf_size = 0x10000;
204
205         ZERO_STRUCT(r_u);
206
207         DEBUG(5,("svc_enum_svcs_status: %d\n", __LINE__));
208
209         if (dos_status == 0x0 && find_lsa_policy_by_hnd(&q_u->pol) == -1)
210         {
211                 dos_status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
212         }
213
214         if (dos_status == 0x0)
215         {
216                 DEBUG(5,("svc_enum_svcs_status:\n"));
217                 while (dummy_services[i] != NULL)
218                 {
219                         ENUM_SRVC_STATUS *svc = NULL;
220
221                         buf_size += strlen(dummy_services[i] + 1) * 2;
222                         buf_size += 9 * sizeof(uint32);
223
224                         DEBUG(10,("buf_size: %d q_u->buf_size: %d\n",
225                                    buf_size, q_u->buf_size));
226
227                         if (buf_size >= max_buf_size)
228                         {
229                                 resume_hnd = i;
230                                 break;
231                         }
232
233                         if (buf_size > q_u->buf_size)
234                         {
235                                 dos_status = ERRmoredata;
236                                 break;
237                         }
238
239                         num_svcs++;
240                         svcs = Realloc(svcs, num_svcs * sizeof(ENUM_SRVC_STATUS));
241                         if (svcs == NULL)
242                         {
243                                 dos_status = ERRnomem;
244                                 num_svcs = 0;
245                                 break;
246                         }
247
248                         svc = &svcs[num_svcs-1];
249                         ZERO_STRUCTP(svc);
250
251                         make_unistr(&svc->uni_srvc_name, dummy_services[i]);
252                         make_unistr(&svc->uni_disp_name, dummy_services[i]);
253
254                         DEBUG(10,("show service: %s\n", dummy_services[i]));
255                         i++;
256                 }
257         }
258
259         /*
260          * check for finished condition: no resume handle and last buffer fits
261          */
262
263         if (resume_hnd == 0 && buf_size <= q_u->buf_size)
264         {
265                 /* this indicates, along with resume_hnd of 0, an end. */
266                 max_buf_size = 0;
267         }
268
269         make_svc_r_enum_svcs_status(&r_u, svcs, max_buf_size, num_svcs, resume_hnd, dos_status);
270
271         /* store the response in the SMB stream */
272         svc_io_r_enum_svcs_status("", &r_u, rdata, 0);
273
274         if (svcs != NULL)
275         {
276                 free(svcs);
277         }
278
279         DEBUG(5,("svc_enum_svcs_status: %d\n", __LINE__));
280 }
281
282 /*******************************************************************
283  api_svc_enum_svcs_status
284  ********************************************************************/
285 static void api_svc_enum_svcs_status( uint16 vuid, prs_struct *data,
286                                     prs_struct *rdata )
287 {
288         SVC_Q_ENUM_SVCS_STATUS q_u;
289         svc_io_q_enum_svcs_status("", &q_u, data, 0);
290         svc_reply_enum_svcs_status(&q_u, rdata);
291 }
292
293 /*******************************************************************
294  array of \PIPE\svcctl operations
295  ********************************************************************/
296 static struct api_struct api_svc_cmds[] =
297 {
298         { "SVC_CLOSE"           , SVC_CLOSE           , api_svc_close            },
299         { "SVC_OPEN_SC_MAN"     , SVC_OPEN_SC_MAN     , api_svc_open_sc_man      },
300         { "SVC_OPEN_SERVICE"    , SVC_OPEN_SERVICE    , api_svc_open_service     },
301         { "SVC_ENUM_SVCS_STATUS", SVC_ENUM_SVCS_STATUS, api_svc_enum_svcs_status },
302         { NULL                  , 0                   , NULL                     }
303 };
304
305 /*******************************************************************
306  receives a svcctl pipe and responds.
307  ********************************************************************/
308 BOOL api_svcctl_rpc(pipes_struct *p, prs_struct *data)
309 {
310         return api_rpcTNP(p, "api_svc_rpc", api_svc_cmds, data);
311 }
312