6cf9b8cd2280da53daac3b6bf46ca30ec4f71af7
[samba.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_sc_man
75  ********************************************************************/
76 static void svc_reply_open_sc_man(SVC_Q_OPEN_SC_MAN *q_u,
77                                 prs_struct *rdata)
78 {
79         uint32 status     = 0;
80         POLICY_HND pol;
81         SVC_R_OPEN_SC_MAN r_u;
82         fstring name;
83
84         DEBUG(5,("svc_open_sc_man: %d\n", __LINE__));
85
86         if (status == 0x0 && !open_lsa_policy_hnd(&pol))
87         {
88                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
89         }
90
91         fstrcpy(name, unistr2_to_str(&q_u->uni_srv_name));
92
93         if (status == 0x0)
94         {
95                 DEBUG(5,("svc_open_sc_man: %s\n", name));
96                 /* lkcl XXXX do a check on the name, here */
97         }
98
99         if (status == 0x0 && !set_lsa_policy_reg_name(&pol, name))
100         {
101                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
102         }
103
104         make_svc_r_open_sc_man(&r_u, &pol, status);
105
106         /* store the response in the SMB stream */
107         svc_io_r_open_sc_man("", &r_u, rdata, 0);
108
109         DEBUG(5,("svc_open_sc_man: %d\n", __LINE__));
110 }
111
112 /*******************************************************************
113  api_svc_open_sc_man
114  ********************************************************************/
115 static void api_svc_open_sc_man( uint16 vuid, prs_struct *data,
116                                     prs_struct *rdata )
117 {
118         SVC_Q_OPEN_SC_MAN q_u;
119         svc_io_q_open_sc_man("", &q_u, data, 0);
120         svc_reply_open_sc_man(&q_u, rdata);
121 }
122
123 static char *dummy_services[] =
124 {
125         "imapd",
126         "popd",
127         "smbd",
128         "nmbd",
129         "httpd",
130         "inetd",
131         "syslogd",
132         NULL
133 };
134
135 /*******************************************************************
136  svc_reply_enum_svcs_status
137  ********************************************************************/
138 static void svc_reply_enum_svcs_status(SVC_Q_ENUM_SVCS_STATUS *q_u,
139                                 prs_struct *rdata)
140 {
141         uint32 dos_status = 0;
142         SVC_R_ENUM_SVCS_STATUS r_u;
143         ENUM_SRVC_STATUS *svcs = NULL;
144         int num_svcs = 0;
145         int buf_size = 0;
146         int i = get_enum_hnd(&q_u->resume_hnd);
147         uint32 resume_hnd = 0;
148         int max_buf_size = 0x80;
149
150         ZERO_STRUCT(r_u);
151
152         DEBUG(5,("svc_enum_svcs_status: %d\n", __LINE__));
153
154         if (dos_status == 0x0 && find_lsa_policy_by_hnd(&q_u->pol) == -1)
155         {
156                 dos_status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
157         }
158
159         if (dos_status == 0x0)
160         {
161                 DEBUG(5,("svc_enum_svcs_status:\n"));
162                 while (dummy_services[i] != NULL)
163                 {
164                 
165                         ENUM_SRVC_STATUS *svc = NULL;
166
167                         buf_size += strlen(dummy_services[i] + 1) * 2;
168                         buf_size += 9 * sizeof(uint32);
169
170                         DEBUG(10,("buf_size: %d q_u->buf_size: %d\n",
171                                    buf_size, q_u->buf_size));
172
173                         if (buf_size > q_u->buf_size)
174                         {
175                                 if (buf_size >= max_buf_size)
176                                 {
177                                         resume_hnd = i;
178                                 }
179                                 break;
180                         }
181
182                         num_svcs++;
183                         svcs = Realloc(svcs, num_svcs * sizeof(ENUM_SRVC_STATUS));
184                         if (svcs == NULL)
185                         {
186                                 dos_status = ERRnomem;
187                                 num_svcs = 0;
188                                 break;
189                         }
190
191                         svc = &svcs[num_svcs-1];
192                         ZERO_STRUCTP(svc);
193
194                         make_unistr(&svc->uni_srvc_name, dummy_services[i]);
195                         make_unistr(&svc->uni_disp_name, dummy_services[i]);
196
197                         DEBUG(10,("show service: %s\n", dummy_services[i]));
198                         i++;
199                 }
200         }
201
202         /*
203          * check for finished condition: no resume handle and last buffer fits
204          */
205
206         if (resume_hnd == 0 && buf_size <= q_u->buf_size)
207         {
208                 /* this indicates, along with resume_hnd of 0, an end. */
209                 max_buf_size = 0;
210         }
211
212         make_svc_r_enum_svcs_status(&r_u, svcs, max_buf_size, num_svcs, resume_hnd, dos_status);
213
214         /* store the response in the SMB stream */
215         svc_io_r_enum_svcs_status("", &r_u, rdata, 0);
216
217         if (svcs != NULL)
218         {
219                 free(svcs);
220         }
221
222         DEBUG(5,("svc_enum_svcs_status: %d\n", __LINE__));
223 }
224
225 /*******************************************************************
226  api_svc_enum_svcs_status
227  ********************************************************************/
228 static void api_svc_enum_svcs_status( uint16 vuid, prs_struct *data,
229                                     prs_struct *rdata )
230 {
231         SVC_Q_ENUM_SVCS_STATUS q_u;
232         svc_io_q_enum_svcs_status("", &q_u, data, 0);
233         svc_reply_enum_svcs_status(&q_u, rdata);
234 }
235
236 /*******************************************************************
237  array of \PIPE\svcctl operations
238  ********************************************************************/
239 static struct api_struct api_svc_cmds[] =
240 {
241         { "SVC_CLOSE"        , SVC_CLOSE        , api_svc_close        },
242         { "SVC_OPEN_SC_MAN"  , SVC_OPEN_SC_MAN  , api_svc_open_sc_man  },
243         { "SVC_ENUM_SVCS_STATUS", SVC_ENUM_SVCS_STATUS, api_svc_enum_svcs_status },
244         { NULL,                0                , NULL                 }
245 };
246
247 /*******************************************************************
248  receives a svcctl pipe and responds.
249  ********************************************************************/
250 BOOL api_svcctl_rpc(pipes_struct *p, prs_struct *data)
251 {
252         return api_rpcTNP(p, "api_svc_rpc", api_svc_cmds, data);
253 }
254