rewrote policy handle code to be generic (it's needed for client-side too)
[kai/samba.git] / source3 / rpc_server / srv_reg.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  reg_reply_unknown_1
34  ********************************************************************/
35 static void reg_reply_close(REG_Q_CLOSE *q_r,
36                                 prs_struct *rdata)
37 {
38         REG_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,("reg_unknown_1: %d\n", __LINE__));
54
55         /* store the response in the SMB stream */
56         reg_io_r_close("", &r_u, rdata, 0);
57
58         DEBUG(5,("reg_unknown_1: %d\n", __LINE__));
59 }
60
61 /*******************************************************************
62  api_reg_close
63  ********************************************************************/
64 static void api_reg_close( pipes_struct *p, prs_struct *data,
65                                     prs_struct *rdata )
66 {
67         REG_Q_CLOSE q_r;
68
69         /* grab the reg unknown 1 */
70         reg_io_q_close("", &q_r, data, 0);
71
72         /* construct reply.  always indicate success */
73         reg_reply_close(&q_r, rdata);
74 }
75
76
77 /*******************************************************************
78  reg_reply_open
79  ********************************************************************/
80 static void reg_reply_open(REG_Q_OPEN_HKLM *q_r,
81                                 prs_struct *rdata)
82 {
83         REG_R_OPEN_HKLM r_u;
84
85         r_u.status = 0x0;
86         /* get a (unique) handle.  open a policy on it. */
87         if (r_u.status == 0x0 && !open_policy_hnd(&(r_u.pol)))
88         {
89                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
90         }
91
92         DEBUG(5,("reg_open: %d\n", __LINE__));
93
94         /* store the response in the SMB stream */
95         reg_io_r_open_hklm("", &r_u, rdata, 0);
96
97         DEBUG(5,("reg_open: %d\n", __LINE__));
98 }
99
100 /*******************************************************************
101  api_reg_open
102  ********************************************************************/
103 static void api_reg_open( pipes_struct *p, prs_struct *data,
104                                     prs_struct *rdata )
105 {
106         REG_Q_OPEN_HKLM q_u;
107
108         /* grab the reg open */
109         reg_io_q_open_hklm("", &q_u, data, 0);
110
111         /* construct reply.  always indicate success */
112         reg_reply_open(&q_u, rdata);
113 }
114
115
116 /*******************************************************************
117  reg_reply_open_entry
118  ********************************************************************/
119 static void reg_reply_open_entry(REG_Q_OPEN_ENTRY *q_u,
120                                 prs_struct *rdata)
121 {
122         uint32 status     = 0;
123         POLICY_HND pol;
124         REG_R_OPEN_ENTRY r_u;
125         fstring name;
126
127         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
128
129         if (status == 0 && find_policy_by_hnd(&(q_u->pol)) == -1)
130         {
131                 status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
132         }
133
134         if (status == 0x0 && !open_policy_hnd(&pol))
135         {
136                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
137         }
138
139         unistr2_to_ascii(name, &q_u->uni_name, sizeof(name)-1);
140
141         if (status == 0x0)
142         {
143                 DEBUG(5,("reg_open_entry: %s\n", name));
144                 /* lkcl XXXX do a check on the name, here */
145                 if (!strequal(name, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions") &&
146                     !strequal(name, "SYSTEM\\CurrentControlSet\\Services\\NETLOGON\\Parameters\\"))
147                 {
148                         status = 0xC000000 | NT_STATUS_ACCESS_DENIED;
149                 }
150         }
151
152         if (status == 0x0 && !set_policy_reg_name(&pol, name))
153         {
154                 status = 0xC000000 | NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
155         }
156
157         make_reg_r_open_entry(&r_u, &pol, status);
158
159         /* store the response in the SMB stream */
160         reg_io_r_open_entry("", &r_u, rdata, 0);
161
162         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
163 }
164
165 /*******************************************************************
166  api_reg_open_entry
167  ********************************************************************/
168 static void api_reg_open_entry( pipes_struct *p, prs_struct *data,
169                                     prs_struct *rdata )
170 {
171         REG_Q_OPEN_ENTRY q_u;
172
173         /* grab the reg open entry */
174         reg_io_q_open_entry("", &q_u, data, 0);
175
176         /* construct reply. */
177         reg_reply_open_entry(&q_u, rdata);
178 }
179
180
181 /*******************************************************************
182  reg_reply_info
183  ********************************************************************/
184 static void reg_reply_info(REG_Q_INFO *q_u,
185                                 prs_struct *rdata)
186 {
187         uint32 status     = 0;
188
189         REG_R_INFO r_u;
190         uint32 type = 0xcafeface;
191         BUFFER2 buf;
192         fstring name;
193
194         ZERO_STRUCT(buf);
195
196         DEBUG(5,("reg_info: %d\n", __LINE__));
197
198         if (status == 0x0 && !get_policy_reg_name(&q_u->pol, name))
199         {
200                 status = 0xC000000 | NT_STATUS_INVALID_HANDLE;
201         }
202
203         if (status == 0 &&
204            strequal(name, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions"))
205         {
206                 char *key = "LanmanNT";
207                 make_buffer2(&buf, key, strlen(key));
208                 type = 0x1;
209         }
210         else
211         {
212                 status = 0x2; /* Win32 status code.  ick */
213         }
214
215         make_reg_r_info(&r_u, &type, &buf, status);
216
217         /* store the response in the SMB stream */
218         reg_io_r_info("", &r_u, rdata, 0);
219
220         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
221 }
222
223 /*******************************************************************
224  api_reg_info
225  ********************************************************************/
226 static void api_reg_info( pipes_struct *p, prs_struct *data,
227                                     prs_struct *rdata )
228 {
229         REG_Q_INFO q_u;
230
231         /* grab the reg unknown 0x11*/
232         reg_io_q_info("", &q_u, data, 0);
233
234         /* construct reply.  always indicate success */
235         reg_reply_info(&q_u, rdata);
236 }
237
238
239 /*******************************************************************
240  array of \PIPE\reg operations
241  ********************************************************************/
242 static struct api_struct api_reg_cmds[] =
243 {
244         { "REG_CLOSE"        , REG_CLOSE        , api_reg_close        },
245         { "REG_OPEN_ENTRY"   , REG_OPEN_ENTRY   , api_reg_open_entry   },
246         { "REG_OPEN"         , REG_OPEN_HKLM    , api_reg_open         },
247         { "REG_INFO"         , REG_INFO         , api_reg_info         },
248         { NULL,                0                , NULL                 }
249 };
250
251 /*******************************************************************
252  receives a reg pipe and responds.
253  ********************************************************************/
254 BOOL api_reg_rpc(pipes_struct *p, prs_struct *data)
255 {
256         return api_rpcTNP(p, "api_reg_rpc", api_reg_cmds, data);
257 }
258