a95833cf6f57066efcc825c8bf0a41cded9fe713
[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 #ifndef LIBCAPTIVE
48
49 /* FIXME: This function is somehow buggy, at least it uses '0xfc' mask
50  * instead of '0xFFFFFFFC' mask as sometimes there are PAGE_SIZE sized structures.
51  */
52 ULONG STDCALL
53 RtlLengthSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor)
54 {
55    PSID Owner;
56    PSID Group;
57    ULONG Length;
58    PACL Dacl;
59    PACL Sacl;
60    
61    Length = sizeof(SECURITY_DESCRIPTOR);
62    
63    if (SecurityDescriptor->Owner != NULL)
64      {
65         Owner = SecurityDescriptor->Owner;
66         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
67           {
68              Owner = (PSID)((ULONG)Owner + 
69                             (ULONG)SecurityDescriptor);
70           }
71         Length = Length + ((sizeof(SID) + (Owner->SubAuthorityCount - 1) * 
72                            sizeof(ULONG) + 3) & 0xfc);
73      }
74    if (SecurityDescriptor->Group != NULL)
75      {
76         Group = SecurityDescriptor->Group;
77         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
78           {
79              Group = (PSID)((ULONG)Group + (ULONG)SecurityDescriptor);
80           }
81         Length = Length + ((sizeof(SID) + (Group->SubAuthorityCount - 1) *
82                            sizeof(ULONG) + 3) & 0xfc);
83      }
84       if (SecurityDescriptor->Control & SE_DACL_PRESENT &&
85        SecurityDescriptor->Dacl != NULL)
86      {
87         Dacl = SecurityDescriptor->Dacl;
88         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
89           {
90              Dacl = (PACL)((ULONG)Dacl + (PVOID)SecurityDescriptor);
91           }
92         Length = Length + ((Dacl->AclSize + 3) & 0xfc);
93      }
94    if (SecurityDescriptor->Control & SE_SACL_PRESENT &&
95        SecurityDescriptor->Sacl != NULL)
96      {
97         Sacl = SecurityDescriptor->Sacl;
98         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
99           {
100              Sacl = (PACL)((ULONG)Sacl + (PVOID)SecurityDescriptor);
101           }
102         Length = Length + ((Sacl->AclSize + 3) & 0xfc);
103      }
104    return(Length);
105 }
106
107
108 NTSTATUS STDCALL
109 RtlGetDaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
110                              PBOOLEAN DaclPresent,
111                              PACL* Dacl,
112                              PBOOLEAN DaclDefaulted)
113 {
114    if (SecurityDescriptor->Revision != 1)
115      {
116         return(STATUS_UNSUCCESSFUL);
117      }
118    if (!(SecurityDescriptor->Control & SE_DACL_PRESENT))
119      {
120         *DaclPresent = 0;
121         return(STATUS_SUCCESS);
122      }
123    *DaclPresent = 1;
124    if (SecurityDescriptor->Dacl == NULL)
125      {
126         *Dacl = NULL;
127      }
128    else
129      {
130         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
131           {
132              *Dacl = (PACL)((ULONG)SecurityDescriptor->Dacl +
133                             (PVOID)SecurityDescriptor);
134           }
135         else
136           {
137              *Dacl = SecurityDescriptor->Dacl;
138           }
139      }
140    if (SecurityDescriptor->Control & SE_DACL_DEFAULTED)
141      {
142         *DaclDefaulted = 1;
143      }
144    else
145      {
146         *DaclDefaulted = 0;
147      }
148    return(STATUS_SUCCESS);
149 }
150
151 #endif /* LIBCAPTIVE */
152
153 NTSTATUS STDCALL
154 RtlSetDaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
155                              BOOLEAN DaclPresent,
156                              PACL Dacl,
157                              BOOLEAN DaclDefaulted)
158 {
159    if (SecurityDescriptor->Revision != 1)
160      {
161         return(STATUS_UNSUCCESSFUL);
162      }
163    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
164      {
165         return(STATUS_UNSUCCESSFUL);
166      }
167    if (!DaclPresent)
168      {
169         SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_DACL_PRESENT);
170         return(STATUS_SUCCESS);
171      }
172    SecurityDescriptor->Control = SecurityDescriptor->Control | SE_DACL_PRESENT;
173    SecurityDescriptor->Dacl = Dacl;
174    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_DACL_DEFAULTED);
175    if (DaclDefaulted)
176      {
177         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_DACL_DEFAULTED;
178      }
179    return(STATUS_SUCCESS);
180 }
181
182 #ifndef LIBCAPTIVE
183
184 BOOLEAN STDCALL
185 RtlValidSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor)
186 {
187   UNIMPLEMENTED;
188 }
189
190
191 NTSTATUS STDCALL
192 RtlSetOwnerSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
193                               PSID Owner,
194                               BOOLEAN OwnerDefaulted)
195 {
196    if (SecurityDescriptor->Revision != 1)
197      {
198         return(STATUS_UNSUCCESSFUL);
199      }
200    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
201      {
202         return(STATUS_UNSUCCESSFUL);
203      }
204    SecurityDescriptor->Owner = Owner;
205    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_OWNER_DEFAULTED);
206    if (OwnerDefaulted)
207      {
208         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_OWNER_DEFAULTED;
209      }
210    return(STATUS_SUCCESS);
211 }
212
213
214 NTSTATUS STDCALL
215 RtlGetOwnerSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
216                               PSID* Owner,
217                               PBOOLEAN OwnerDefaulted)
218 {
219    if (SecurityDescriptor->Revision != 1)
220      {
221         return(STATUS_UNSUCCESSFUL);
222      }
223    if (SecurityDescriptor->Owner != NULL)
224      {
225         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
226           {
227              *Owner = (PSID)((ULONG)SecurityDescriptor->Owner +
228                              (PVOID)SecurityDescriptor);
229           }
230         else
231           {
232              *Owner = SecurityDescriptor->Owner;
233           }
234      }
235    else
236      {
237         *Owner = NULL;
238      }
239    if (SecurityDescriptor->Control & SE_OWNER_DEFAULTED)
240      {
241         *OwnerDefaulted = 1;
242      }
243    else
244      {
245         *OwnerDefaulted = 0;
246      }
247    return(STATUS_SUCCESS);
248 }
249
250
251 NTSTATUS STDCALL
252 RtlSetGroupSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
253                               PSID Group,
254                               BOOLEAN GroupDefaulted)
255 {
256    if (SecurityDescriptor->Revision != 1)
257      {
258         return(STATUS_UNSUCCESSFUL);
259      }
260    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
261      {
262         return(STATUS_UNSUCCESSFUL);
263      }
264    SecurityDescriptor->Group = Group;
265    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_GROUP_DEFAULTED);
266    if (GroupDefaulted)
267      {
268         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_GROUP_DEFAULTED;
269      }
270    return(STATUS_SUCCESS);
271 }
272
273
274 NTSTATUS STDCALL
275 RtlGetGroupSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
276                               PSID* Group,
277                               PBOOLEAN GroupDefaulted)
278 {
279    if (SecurityDescriptor->Revision != 1)
280      {
281         return(STATUS_UNSUCCESSFUL);
282      }
283    if (SecurityDescriptor->Group != NULL)
284      {
285         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
286           {
287              *Group = (PSID)((ULONG)SecurityDescriptor->Group +
288                              (PVOID)SecurityDescriptor);
289           }
290         else
291           {
292              *Group = SecurityDescriptor->Group;
293           }
294      }
295    else
296      {
297         *Group = NULL;
298      }
299    if (SecurityDescriptor->Control & SE_GROUP_DEFAULTED)
300      {
301         *GroupDefaulted = 1;
302      }
303    else
304      {
305         *GroupDefaulted = 0;
306      }
307    return(STATUS_SUCCESS);
308 }
309
310
311 NTSTATUS STDCALL
312 RtlGetSaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
313                              PBOOLEAN SaclPresent,
314                              PACL *Sacl,
315                              PBOOLEAN SaclDefaulted)
316 {
317    if (SecurityDescriptor->Revision != 1)
318      {
319         return(STATUS_UNSUCCESSFUL);
320      }
321    if (!(SecurityDescriptor->Control & SE_SACL_PRESENT))
322      {
323         *SaclPresent = 0;
324         return(STATUS_SUCCESS);
325      }
326    *SaclPresent = 1;
327    if (SecurityDescriptor->Sacl == NULL)
328      {
329         *Sacl = NULL;
330      }
331    else
332      {
333         if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
334           {
335              *Sacl = (PACL)((ULONG)SecurityDescriptor->Sacl +
336                             (PVOID)SecurityDescriptor);
337           }
338         else
339           {
340              *Sacl = SecurityDescriptor->Sacl;
341           }
342      }
343    if (SecurityDescriptor->Control & SE_SACL_DEFAULTED)
344      {
345         *SaclDefaulted = 1;
346      }
347    else
348      {
349         *SaclDefaulted = 0;
350      }
351    return(STATUS_SUCCESS);
352 }
353
354
355 NTSTATUS STDCALL
356 RtlSetSaclSecurityDescriptor(PSECURITY_DESCRIPTOR SecurityDescriptor,
357                              BOOLEAN SaclPresent,
358                              PACL Sacl,
359                              BOOLEAN SaclDefaulted)
360 {
361    if (SecurityDescriptor->Revision != 1)
362      {
363         return(STATUS_UNSUCCESSFUL);
364      }
365    if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
366      {
367         return(STATUS_UNSUCCESSFUL);
368      }
369    if (!SaclPresent)
370      {
371         SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_SACL_PRESENT);
372         return(STATUS_SUCCESS);
373      }
374    SecurityDescriptor->Control = SecurityDescriptor->Control | SE_SACL_PRESENT;
375    SecurityDescriptor->Sacl = Sacl;
376    SecurityDescriptor->Control = SecurityDescriptor->Control & ~(SE_SACL_DEFAULTED);
377    if (SaclDefaulted)
378      {
379         SecurityDescriptor->Control = SecurityDescriptor->Control | SE_SACL_DEFAULTED;
380      }
381    return(STATUS_SUCCESS);
382 }
383
384
385 NTSTATUS STDCALL
386 RtlAbsoluteToSelfRelativeSD(PSECURITY_DESCRIPTOR AbsSD,
387                             PSECURITY_DESCRIPTOR RelSD,
388                             PULONG BufferLength)
389 {
390   if (AbsSD->Control & SE_SELF_RELATIVE)
391     return(STATUS_BAD_DESCRIPTOR_FORMAT);
392
393 //  return(RtlPMakeSelfRelativeSD (AbsSD, RelSD, BufferLength));
394
395   UNIMPLEMENTED;
396   return(STATUS_NOT_IMPLEMENTED);
397 }
398
399 #endif /* LIBCAPTIVE */
400
401 /* EOF */