chgpasswd.c: Added comments to #ifdefs
[jra/samba/.git] / source3 / rpc_server / srv_lsa_hnd.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  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24
25 #include "includes.h"
26
27
28 extern int DEBUGLEVEL;
29
30 #ifndef MAX_OPEN_POLS
31 #define MAX_OPEN_POLS 50
32 #endif
33
34 struct reg_info
35 {
36     /* for use by \PIPE\winreg */
37         fstring name; /* name of registry key */
38 };
39
40 struct samr_info
41 {
42     /* for use by the \PIPE\samr policy */
43         DOM_SID sid;
44     uint32 rid; /* relative id associated with the pol_hnd */
45     uint32 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
46 };
47
48 static struct
49 {
50   BOOL open;
51   POLICY_HND pol_hnd;
52
53   union
54   {
55     struct samr_info samr;
56         struct reg_info reg;
57
58   } dev;
59
60 } Policy[MAX_OPEN_POLS];
61
62
63 #define VALID_POL(pnum)   (((pnum) >= 0) && ((pnum) < MAX_OPEN_POLS))
64 #define OPEN_POL(pnum)    (VALID_POL(pnum) && Policy[pnum].open)
65
66 /****************************************************************************
67   create a unique policy handle
68 ****************************************************************************/
69 void create_pol_hnd(POLICY_HND *hnd)
70 {
71         static uint32 pol_hnd_low  = 0;
72         static uint32 pol_hnd_high = 0;
73
74         if (hnd == NULL) return;
75
76         /* i severely doubt that pol_hnd_high will ever be non-zero... */
77         pol_hnd_low++;
78         if (pol_hnd_low == 0) pol_hnd_high++;
79
80         SIVAL(hnd->data, 0 , 0x0);  /* first bit must be null */
81         SIVAL(hnd->data, 4 , pol_hnd_low ); /* second bit is incrementing */
82         SIVAL(hnd->data, 8 , pol_hnd_high); /* second bit is incrementing */
83         SIVAL(hnd->data, 12, time(NULL)); /* something random */
84         SIVAL(hnd->data, 16, getpid()); /* something more random */
85 }
86
87 /****************************************************************************
88   initialise policy handle states...
89 ****************************************************************************/
90 void init_lsa_policy_hnd(void)
91 {
92         int i;
93         for (i = 0; i < MAX_OPEN_POLS; i++)
94         {
95                 Policy[i].open = False;
96         }
97
98         return;
99 }
100
101 /****************************************************************************
102   find first available policy slot.  creates a policy handle for you.
103 ****************************************************************************/
104 BOOL open_lsa_policy_hnd(POLICY_HND *hnd)
105 {
106         int i;
107
108         for (i = 0; i < MAX_OPEN_POLS; i++)
109         {
110                 if (!Policy[i].open)
111                 {
112                         Policy[i].open = True;
113                                 
114                         create_pol_hnd(hnd);
115                         memcpy(&(Policy[i].pol_hnd), hnd, sizeof(*hnd));
116
117                         DEBUG(4,("Opened policy hnd[%x] ", i));
118                         dump_data(4, (char *)hnd->data, sizeof(hnd->data));
119
120                         return True;
121                 }
122         }
123
124         /* i love obscure error messages. */
125 #if TERRY_PRATCHET_INTERESTING_TIMES
126         DEBUG(1,("+++ OUT OF CHEESE ERROR +++ REDO FROM START ... @?!*@@\n"));
127 #else
128         DEBUG(1,("ERROR - open_lsa_policy_hnd: out of Policy Handles!\n"));
129 #endif
130
131         return False;
132 }
133
134 /****************************************************************************
135   find policy index by handle
136 ****************************************************************************/
137 int find_lsa_policy_by_hnd(POLICY_HND *hnd)
138 {
139         int i;
140
141         for (i = 0; i < MAX_OPEN_POLS; i++)
142         {
143                 if (memcmp(&(Policy[i].pol_hnd), hnd, sizeof(*hnd)) == 0)
144                 {
145                         DEBUG(4,("Found policy hnd[%x] ", i));
146                         dump_data(4, (char *)hnd->data, sizeof(hnd->data));
147
148                         return i;
149                 }
150         }
151
152         DEBUG(4,("Policy not found: "));
153         dump_data(4, (char *)hnd->data, sizeof(hnd->data));
154
155         return -1;
156 }
157
158 /****************************************************************************
159   set samr rid
160 ****************************************************************************/
161 BOOL set_lsa_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
162 {
163         int pnum = find_lsa_policy_by_hnd(hnd);
164
165         if (OPEN_POL(pnum))
166         {
167                 DEBUG(3,("%s Setting policy device rid=%x pnum=%x\n",
168                           timestring(), rid, pnum));
169
170                 Policy[pnum].dev.samr.rid = rid;
171                 return True;
172         }
173         else
174         {
175                 DEBUG(3,("%s Error setting policy rid=%x (pnum=%x)\n",
176                           timestring(), rid, pnum));
177                 return False;
178         }
179 }
180
181 /****************************************************************************
182   set samr pol status.  absolutely no idea what this is.
183 ****************************************************************************/
184 BOOL set_lsa_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
185 {
186         int pnum = find_lsa_policy_by_hnd(hnd);
187
188         if (OPEN_POL(pnum))
189         {
190                 DEBUG(3,("%s Setting policy status=%x pnum=%x\n",
191                           timestring(), pol_status, pnum));
192
193                 Policy[pnum].dev.samr.status = pol_status;
194                 return True;
195         }
196         else
197         {
198                 DEBUG(3,("%s Error setting policy status=%x (pnum=%x)\n",
199                           timestring(), pol_status, pnum));
200                 return False;
201         }
202 }
203
204 /****************************************************************************
205   set samr sid
206 ****************************************************************************/
207 BOOL set_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
208 {
209   pstring sidstr;
210   int pnum = find_lsa_policy_by_hnd(hnd);
211
212   if (OPEN_POL(pnum))
213   {
214     DEBUG(3,("%s Setting policy sid=%s pnum=%x\n",
215           timestring(), sid_to_string(sidstr, sid), pnum));
216
217     memcpy(&(Policy[pnum].dev.samr.sid), sid, sizeof(*sid));
218     return True;
219   }
220   else
221   {
222     DEBUG(3,("%s Error setting policy sid=%s (pnum=%x)\n",
223           timestring(), sid_to_string(sidstr, sid), pnum));
224     return False;
225   }
226 }
227
228 /****************************************************************************
229   set samr rid
230 ****************************************************************************/
231 uint32 get_lsa_policy_samr_rid(POLICY_HND *hnd)
232 {
233         int pnum = find_lsa_policy_by_hnd(hnd);
234
235         if (OPEN_POL(pnum))
236         {
237                 uint32 rid = Policy[pnum].dev.samr.rid;
238                 DEBUG(3,("%s Getting policy device rid=%x pnum=%x\n",
239                           timestring(), rid, pnum));
240
241                 return rid;
242         }
243         else
244         {
245                 DEBUG(3,("%s Error getting policy (pnum=%x)\n",
246                           timestring(), pnum));
247                 return 0xffffffff;
248         }
249 }
250
251 /****************************************************************************
252   set reg name 
253 ****************************************************************************/
254 BOOL set_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
255 {
256         int pnum = find_lsa_policy_by_hnd(hnd);
257
258         if (OPEN_POL(pnum))
259         {
260                 DEBUG(3,("%s Setting policy pnum=%x name=%s\n",
261                           timestring(), pnum, name));
262
263                 fstrcpy(Policy[pnum].dev.reg.name, name);
264                 return True;
265         }
266         else
267         {
268                 DEBUG(3,("%s Error setting policy (pnum=%x) name=%s\n",
269                           timestring(), pnum, name));
270                 return False;
271         }
272 }
273
274 /****************************************************************************
275   get reg name 
276 ****************************************************************************/
277 BOOL get_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
278 {
279         int pnum = find_lsa_policy_by_hnd(hnd);
280
281         if (OPEN_POL(pnum))
282         {
283                 fstrcpy(name, Policy[pnum].dev.reg.name);
284
285                 DEBUG(3,("%s Getting policy pnum=%x name=%s\n",
286                           timestring(), pnum, name));
287
288                 return True;
289         }
290         else
291         {
292                 DEBUG(3,("%s Error getting policy (pnum=%x)\n",
293                           timestring(), pnum));
294                 return False;
295         }
296 }
297
298 /****************************************************************************
299   close an lsa policy
300 ****************************************************************************/
301 BOOL close_lsa_policy_hnd(POLICY_HND *hnd)
302 {
303         int pnum = find_lsa_policy_by_hnd(hnd);
304
305         if (OPEN_POL(pnum))
306         {
307                 DEBUG(3,("%s Closed policy name pnum=%x\n", timestring(), pnum));
308                 Policy[pnum].open = False;
309                 return True;
310         }
311         else
312         {
313                 DEBUG(3,("%s Error closing policy pnum=%x\n", timestring(), pnum));
314                 return False;
315         }
316 }
317