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