:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / se / sd.c
1 /* $Id$
2  *
3  * COPYRIGHT:         See COPYING in the top level directory
4  * PROJECT:           ReactOS kernel
5  * PURPOSE:           Security manager
6  * FILE:              kernel/se/sd.c
7  * PROGRAMER:         David Welch <welch@cwcom.net>
8  * REVISION HISTORY:
9  *                 26/07/98: Added stubs for security functions
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/se.h>
16
17 #include <internal/debug.h>
18
19
20 /* FUNCTIONS ***************************************************************/
21
22 BOOLEAN
23 SepInitSDs(VOID)
24 {
25   return(TRUE);
26 }
27
28
29 NTSTATUS STDCALL
30 RtlCreateSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
31                             ULONG Revision)
32 {
33   if (Revision != 1)
34     return(STATUS_UNSUCCESSFUL);
35
36   SecurityDescriptor->Revision = 1;
37   SecurityDescriptor->Sbz1 = 0;
38   SecurityDescriptor->Control = 0;
39   SecurityDescriptor->Owner = NULL;
40   SecurityDescriptor->Group = NULL;
41   SecurityDescriptor->Sacl = NULL;
42   SecurityDescriptor->Dacl = NULL;
43
44   return(STATUS_SUCCESS);
45 }
46
47 ULONG STDCALL
48 RtlLengthSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor)
49 {
50    PSID Owner;
51    PSID Group;
52    ULONG Length;
53    PACL Dacl;
54    PACL Sacl;
55    
56    Length = sizeof(SECURITY_DESCRIPTOR);
57    
58    if (SecurityDescriptor->Owner != NULL)
59      {
60         Owner = SecurityDescriptor->Owner;
61         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
62           {
63              Owner = (PSID)((ULONG)Owner + 
64                             (ULONG)SecurityDescriptor);
65           }
66         Length = Length + ((sizeof(SID) + (Owner->SubAuthorityCount - 1) * 
67                            sizeof(ULONG) + 3) & 0xfc);
68      }
69    if (SecurityDescriptor->Group != NULL)
70      {
71         Group = SecurityDescriptor->Group;
72         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
73           {
74              Group = (PSID)((ULONG)Group + (ULONG)SecurityDescriptor);
75           }
76         Length = Length + ((sizeof(SID) + (Group->SubAuthorityCount - 1) *
77                            sizeof(ULONG) + 3) & 0xfc);
78      }
79       if (SecurityDescriptor->Control & SE_DACL_PRESENT &&
80        SecurityDescriptor->Dacl != NULL)
81      {
82         Dacl = SecurityDescriptor->Dacl;
83         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
84           {
85              Dacl = (PACL)((ULONG)Dacl + (PVOID)SecurityDescriptor);
86           }
87         Length = Length + ((Dacl->AclSize + 3) & 0xfc);
88      }
89    if (SecurityDescriptor->Control & SE_SACL_PRESENT &&
90        SecurityDescriptor->Sacl != NULL)
91      {
92         Sacl = SecurityDescriptor->Sacl;
93         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
94           {
95              Sacl = (PACL)((ULONG)Sacl + (PVOID)SecurityDescriptor);
96           }
97         Length = Length + ((Sacl->AclSize + 3) & 0xfc);
98      }
99    return(Length);
100 }
101
102
103 NTSTATUS STDCALL
104 RtlGetDaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
105                              PBOOLEAN DaclPresent,
106                              PACL* Dacl,
107                              PBOOLEAN DaclDefaulted)
108 {
109    if (SecurityDescriptor->Revision != 1)
110      {
111         return(STATUS_UNSUCCESSFUL);
112      }
113    if (!(SecurityDescriptor->Control & SE_DACL_PRESENT))
114      {
115         *DaclPresent = 0;
116         return(STATUS_SUCCESS);
117      }
118    *DaclPresent = 1;
119    if (SecurityDescriptor->Dacl == NULL)
120      {
121         *Dacl = NULL;
122      }
123    else
124      {
125         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
126           {
127              *Dacl = (PACL)((ULONG)SecurityDescriptor->Dacl +
128                             (PVOID)SecurityDescriptor);
129           }
130         else
131           {
132              *Dacl = SecurityDescriptor->Dacl;
133           }
134      }
135    if (SecurityDescriptor->Control & SE_DACL_DEFAULTED)
136      {
137         *DaclDefaulted = 1;
138      }
139    else
140      {
141         *DaclDefaulted = 0;
142      }
143    return(STATUS_SUCCESS);
144 }
145
146
147 NTSTATUS STDCALL
148 RtlSetDaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
149                              BOOLEAN DaclPresent,
150                              PACL Dacl,
151                              BOOLEAN DaclDefaulted)
152 {
153    if (SecurityDescriptor->Revision != 1)
154      {
155         return(STATUS_UNSUCCESSFUL);
156      }
157    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
158      {
159         return(STATUS_UNSUCCESSFUL);
160      }
161    if (!DaclPresent)
162      {
163         SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_DACL_PRESENT);
164         return(STATUS_SUCCESS);
165      }
166    SecurityDescriptor->Control = SecurityDescriptor->Control | SE_DACL_PRESENT;
167    SecurityDescriptor->Dacl = Dacl;
168    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_DACL_DEFAULTED);
169    if (DaclDefaulted)
170      {
171         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_DACL_DEFAULTED;
172      }
173    return(STATUS_SUCCESS);
174 }
175
176
177 BOOLEAN STDCALL
178 RtlValidSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor)
179 {
180   UNIMPLEMENTED;
181 }
182
183
184 NTSTATUS STDCALL
185 RtlSetOwnerSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
186                               PSID Owner,
187                               BOOLEAN OwnerDefaulted)
188 {
189    if (SecurityDescriptor->Revision != 1)
190      {
191         return(STATUS_UNSUCCESSFUL);
192      }
193    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
194      {
195         return(STATUS_UNSUCCESSFUL);
196      }
197    SecurityDescriptor->Owner = Owner;
198    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_OWNER_DEFAULTED);
199    if (OwnerDefaulted)
200      {
201         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_OWNER_DEFAULTED;
202      }
203    return(STATUS_SUCCESS);
204 }
205
206
207 NTSTATUS STDCALL
208 RtlGetOwnerSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
209                               PSID* Owner,
210                               PBOOLEAN OwnerDefaulted)
211 {
212    if (SecurityDescriptor->Revision != 1)
213      {
214         return(STATUS_UNSUCCESSFUL);
215      }
216    if (SecurityDescriptor->Owner != NULL)
217      {
218         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
219           {
220              *Owner = (PSID)((ULONG)SecurityDescriptor->Owner +
221                              (PVOID)SecurityDescriptor);
222           }
223         else
224           {
225              *Owner = SecurityDescriptor->Owner;
226           }
227      }
228    else
229      {
230         *Owner = NULL;
231      }
232    if (SecurityDescriptor->Control & SE_OWNER_DEFAULTED)
233      {
234         *OwnerDefaulted = 1;
235      }
236    else
237      {
238         *OwnerDefaulted = 0;
239      }
240    return(STATUS_SUCCESS);
241 }
242
243
244 NTSTATUS STDCALL
245 RtlSetGroupSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
246                               PSID Group,
247                               BOOLEAN GroupDefaulted)
248 {
249    if (SecurityDescriptor->Revision != 1)
250      {
251         return(STATUS_UNSUCCESSFUL);
252      }
253    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
254      {
255         return(STATUS_UNSUCCESSFUL);
256      }
257    SecurityDescriptor->Group = Group;
258    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_GROUP_DEFAULTED);
259    if (GroupDefaulted)
260      {
261         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_GROUP_DEFAULTED;
262      }
263    return(STATUS_SUCCESS);
264 }
265
266
267 NTSTATUS STDCALL
268 RtlGetGroupSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
269                               PSID* Group,
270                               PBOOLEAN GroupDefaulted)
271 {
272    if (SecurityDescriptor->Revision != 1)
273      {
274         return(STATUS_UNSUCCESSFUL);
275      }
276    if (SecurityDescriptor->Group != NULL)
277      {
278         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
279           {
280              *Group = (PSID)((ULONG)SecurityDescriptor->Group +
281                              (PVOID)SecurityDescriptor);
282           }
283         else
284           {
285              *Group = SecurityDescriptor->Group;
286           }
287      }
288    else
289      {
290         *Group = NULL;
291      }
292    if (SecurityDescriptor->Control & SE_GROUP_DEFAULTED)
293      {
294         *GroupDefaulted = 1;
295      }
296    else
297      {
298         *GroupDefaulted = 0;
299      }
300    return(STATUS_SUCCESS);
301 }
302
303
304 NTSTATUS STDCALL
305 RtlGetSaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
306                              PBOOLEAN SaclPresent,
307                              PACL *Sacl,
308                              PBOOLEAN SaclDefaulted)
309 {
310    if (SecurityDescriptor->Revision != 1)
311      {
312         return(STATUS_UNSUCCESSFUL);
313      }
314    if (!(SecurityDescriptor->Control & SE_SACL_PRESENT))
315      {
316         *SaclPresent = 0;
317         return(STATUS_SUCCESS);
318      }
319    *SaclPresent = 1;
320    if (SecurityDescriptor->Sacl == NULL)
321      {
322         *Sacl = NULL;
323      }
324    else
325      {
326         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
327           {
328              *Sacl = (PACL)((ULONG)SecurityDescriptor->Sacl +
329                             (PVOID)SecurityDescriptor);
330           }
331         else
332           {
333              *Sacl = SecurityDescriptor->Sacl;
334           }
335      }
336    if (SecurityDescriptor->Control & SE_SACL_DEFAULTED)
337      {
338         *SaclDefaulted = 1;
339      }
340    else
341      {
342         *SaclDefaulted = 0;
343      }
344    return(STATUS_SUCCESS);
345 }
346
347
348 NTSTATUS STDCALL
349 RtlSetSaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
350                              BOOLEAN SaclPresent,
351                              PACL Sacl,
352                              BOOLEAN SaclDefaulted)
353 {
354    if (SecurityDescriptor->Revision != 1)
355      {
356         return(STATUS_UNSUCCESSFUL);
357      }
358    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
359      {
360         return(STATUS_UNSUCCESSFUL);
361      }
362    if (!SaclPresent)
363      {
364         SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_SACL_PRESENT);
365         return(STATUS_SUCCESS);
366      }
367    SecurityDescriptor->Control = SecurityDescriptor->Control | SE_SACL_PRESENT;
368    SecurityDescriptor->Sacl = Sacl;
369    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_SACL_DEFAULTED);
370    if (SaclDefaulted)
371      {
372         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_SACL_DEFAULTED;
373      }
374    return(STATUS_SUCCESS);
375 }
376
377
378 NTSTATUS STDCALL
379 RtlAbsoluteToSelfRelativeSD(PSECURITY_DESCRIPTOR AbsSD,
380                             PSECURITY_DESCRIPTOR RelSD,
381                             PULONG BufferLength)
382 {
383   if (AbsSD->Control & SE_SELF_RELATIVE)
384     return(STATUS_BAD_DESCRIPTOR_FORMAT);
385
386 //  return(RtlPMakeSelfRelativeSD (AbsSD, RelSD, BufferLength));
387
388   UNIMPLEMENTED;
389   return(STATUS_NOT_IMPLEMENTED);
390 }
391
392
393
394 /* EOF */