Release 970914
[wine] / win32 / security.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "windows.h"
6 #include "ntdll.h"
7 #include "xmalloc.h"
8 #include "stddebug.h"
9 #include "debug.h"
10
11 BOOL32 WINAPI IsValidSid (LPSID pSid);
12 BOOL32 WINAPI EqualSid (LPSID pSid1, LPSID pSid2);
13 BOOL32 WINAPI EqualPrefixSid (LPSID pSid1, LPSID pSid2);
14 DWORD  WINAPI GetSidLengthRequired (BYTE nSubAuthorityCount);
15 BOOL32 WINAPI AllocateAndInitializeSid(LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount, DWORD nSubAuthority0, DWORD nSubAuthority1, DWORD nSubAuthority2, DWORD nSubAuthority3,    DWORD nSubAuthority4, DWORD nSubAuthority5, DWORD nSubAuthority6, DWORD nSubAuthority7, LPSID *pSid);
16 VOID*  WINAPI FreeSid(LPSID pSid);
17 BOOL32 WINAPI InitializeSid (LPSID pSid, LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount);
18 LPSID_IDENTIFIER_AUTHORITY WINAPI GetSidIdentifierAuthority(LPSID pSid);
19 DWORD* WINAPI GetSidSubAuthority(LPSID pSid, DWORD nSubAuthority);
20 BYTE*  WINAPI GetSidSubAuthorityCount(LPSID pSid);
21 DWORD  WINAPI GetLengthSid(LPSID pSid);
22 BOOL32 WINAPI CopySid(DWORD nDestinationSidLength, LPSID pDestinationSid, LPSID pSourceSid);
23
24 /***********************************************************************
25  *           IsValidSid  (ADVAPI.80)
26  */
27 BOOL32 WINAPI IsValidSid (LPSID pSid) {
28     if (!pSid || pSid->Revision != SID_REVISION)
29         return FALSE;
30
31     return TRUE;
32 }
33
34 /***********************************************************************
35  *           EqualSid  (ADVAPI.40)
36  */
37 BOOL32 WINAPI EqualSid (LPSID pSid1, LPSID pSid2) {
38     if (!IsValidSid(pSid1) || !IsValidSid(pSid2))
39         return FALSE;
40
41     if (*GetSidSubAuthorityCount(pSid1) != *GetSidSubAuthorityCount(pSid2))
42         return FALSE;
43
44     if (memcmp(pSid1, pSid2, GetLengthSid(pSid1)) != 0)
45         return FALSE;
46
47     return TRUE;
48 }
49
50 /***********************************************************************
51  *           EqualPrefixSid  (ADVAPI.39)
52  */
53 BOOL32 WINAPI EqualPrefixSid (LPSID pSid1, LPSID pSid2) {
54     if (!IsValidSid(pSid1) || !IsValidSid(pSid2))
55         return FALSE;
56
57     if (*GetSidSubAuthorityCount(pSid1) != *GetSidSubAuthorityCount(pSid2))
58         return FALSE;
59
60     if (memcmp(pSid1, pSid2, GetSidLengthRequired(pSid1->SubAuthorityCount - 1))
61  != 0)
62         return FALSE;
63
64     return TRUE;
65 }
66
67 /***********************************************************************
68  *           GetSidLengthRequired  (ADVAPI.63)
69  */
70 DWORD WINAPI GetSidLengthRequired (BYTE nSubAuthorityCount) {
71     return sizeof (SID) + (nSubAuthorityCount - 1 * sizeof (DWORD));
72 }
73
74 /***********************************************************************
75  *           AllocateAndInitializeSid  (ADVAPI.11)
76  */
77 BOOL32 WINAPI AllocateAndInitializeSid(LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
78     BYTE nSubAuthorityCount,
79     DWORD nSubAuthority0, DWORD nSubAuthority1,
80     DWORD nSubAuthority2, DWORD nSubAuthority3,
81     DWORD nSubAuthority4, DWORD nSubAuthority5,
82     DWORD nSubAuthority6, DWORD nSubAuthority7,
83     LPSID *pSid) {
84
85     *pSid = xmalloc(GetSidLengthRequired(nSubAuthorityCount));
86     (*pSid)->Revision = SID_REVISION;
87     if (pIdentifierAuthority)
88         memcpy(&(*pSid)->IdentifierAuthority, pIdentifierAuthority,
89                sizeof (SID_IDENTIFIER_AUTHORITY));
90     *GetSidSubAuthorityCount(*pSid) = nSubAuthorityCount;
91
92     if (nSubAuthorityCount > 0)
93         *GetSidSubAuthority(*pSid, 0) = nSubAuthority0;
94     if (nSubAuthorityCount > 1)
95         *GetSidSubAuthority(*pSid, 1) = nSubAuthority1;
96     if (nSubAuthorityCount > 2)
97         *GetSidSubAuthority(*pSid, 2) = nSubAuthority2;
98     if (nSubAuthorityCount > 3)
99         *GetSidSubAuthority(*pSid, 3) = nSubAuthority3;
100     if (nSubAuthorityCount > 4)
101         *GetSidSubAuthority(*pSid, 4) = nSubAuthority4;
102     if (nSubAuthorityCount > 5)
103         *GetSidSubAuthority(*pSid, 5) = nSubAuthority5;
104     if (nSubAuthorityCount > 6)
105         *GetSidSubAuthority(*pSid, 6) = nSubAuthority6;
106     if (nSubAuthorityCount > 7)
107         *GetSidSubAuthority(*pSid, 7) = nSubAuthority7;
108
109     return TRUE;
110 }
111
112 /***********************************************************************
113  *           FreeSid  (ADVAPI.42)
114  */
115 VOID* WINAPI FreeSid(LPSID pSid) {
116     free(pSid);
117     return NULL;
118 }
119
120 /***********************************************************************
121  *           InitializeSid  (ADVAPI.74)
122  */
123 BOOL32 WINAPI InitializeSid (LPSID pSid, LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
124                     BYTE nSubAuthorityCount)
125 {
126     int i;
127
128     pSid->Revision = SID_REVISION;
129     if (pIdentifierAuthority)
130         memcpy(&pSid->IdentifierAuthority, pIdentifierAuthority,
131                sizeof (SID_IDENTIFIER_AUTHORITY));
132     *GetSidSubAuthorityCount(pSid) = nSubAuthorityCount;
133
134     for (i = 0; i < nSubAuthorityCount; i++)
135         *GetSidSubAuthority(pSid, i) = 0;
136
137     return TRUE;
138 }
139
140 /***********************************************************************
141  *           GetSidIdentifierAuthority  (ADVAPI.62)
142  */
143 LPSID_IDENTIFIER_AUTHORITY WINAPI GetSidIdentifierAuthority (LPSID pSid)
144 {
145     return &pSid->IdentifierAuthority;
146 }
147
148 /***********************************************************************
149  *           GetSidSubAuthority  (ADVAPI.64)
150  */
151 DWORD * WINAPI GetSidSubAuthority (LPSID pSid, DWORD nSubAuthority)
152 {
153     return &pSid->SubAuthority[nSubAuthority];
154 }
155
156 /***********************************************************************
157  *           GetSidSubAuthorityCount  (ADVAPI.65)
158  */
159 BYTE * WINAPI GetSidSubAuthorityCount (LPSID pSid)
160 {
161     return &pSid->SubAuthorityCount;
162 }
163
164 /***********************************************************************
165  *           GetLengthSid  (ADVAPI.48)
166  */
167 DWORD WINAPI GetLengthSid (LPSID pSid)
168 {
169     return GetSidLengthRequired(*GetSidSubAuthorityCount(pSid));
170 }
171
172 /***********************************************************************
173  *           CopySid  (ADVAPI.24)
174  */
175 BOOL32 WINAPI CopySid (DWORD nDestinationSidLength, LPSID pDestinationSid,
176                        LPSID pSourceSid)
177 {
178
179     if (!IsValidSid(pSourceSid))
180         return FALSE;
181
182     if (nDestinationSidLength < GetLengthSid(pSourceSid))
183         return FALSE;
184
185     memcpy(pDestinationSid, pSourceSid, GetLengthSid(pSourceSid));
186
187     return TRUE;
188 }