956c4181f49aa5a0c8a2ee3f232e66e578513f9f
[reactos.git] / reactos / dll / win32 / samsrv / domain.c
1 /*
2 * PROJECT: Local Security Authority Server DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/samsrv/domain.c
5 * PURPOSE: Domain specific helper functions
6 * COPYRIGHT: Copyright 2013 Eric Kohl
7 */
8
9 /* INCLUDES ****************************************************************/
10
11 #include "samsrv.h"
12
13 WINE_DEFAULT_DEBUG_CHANNEL(samsrv);
14
15
16 /* FUNCTIONS ***************************************************************/
17
18 NTSTATUS
19 SampSetAccountNameInDomain(IN PSAM_DB_OBJECT DomainObject,
20 IN LPCWSTR lpContainerName,
21 IN LPCWSTR lpAccountName,
22 IN ULONG ulRelativeId)
23 {
24 HANDLE ContainerKeyHandle = NULL;
25 HANDLE NamesKeyHandle = NULL;
26 NTSTATUS Status;
27
28 TRACE("SampSetAccountNameInDomain()\n");
29
30 /* Open the container key */
31 Status = SampRegOpenKey(DomainObject->KeyHandle,
32 lpContainerName,
33 KEY_ALL_ACCESS,
34 &ContainerKeyHandle);
35 if (!NT_SUCCESS(Status))
36 return Status;
37
38 /* Open the 'Names' key */
39 Status = SampRegOpenKey(ContainerKeyHandle,
40 L"Names",
41 KEY_ALL_ACCESS,
42 &NamesKeyHandle);
43 if (!NT_SUCCESS(Status))
44 goto done;
45
46 /* Set the alias value */
47 Status = SampRegSetValue(NamesKeyHandle,
48 lpAccountName,
49 REG_DWORD,
50 (LPVOID)&ulRelativeId,
51 sizeof(ULONG));
52
53 done:
54 SampRegCloseKey(&NamesKeyHandle);
55 SampRegCloseKey(&ContainerKeyHandle);
56
57 return Status;
58 }
59
60
61 NTSTATUS
62 SampRemoveAccountNameFromDomain(IN PSAM_DB_OBJECT DomainObject,
63 IN LPCWSTR lpContainerName,
64 IN LPCWSTR lpAccountName)
65 {
66 HANDLE ContainerKeyHandle = NULL;
67 HANDLE NamesKeyHandle = NULL;
68 NTSTATUS Status;
69
70 TRACE("(%S %S)\n", lpContainerName, lpAccountName);
71
72 /* Open the container key */
73 Status = SampRegOpenKey(DomainObject->KeyHandle,
74 lpContainerName,
75 KEY_ALL_ACCESS,
76 &ContainerKeyHandle);
77 if (!NT_SUCCESS(Status))
78 return Status;
79
80 /* Open the 'Names' key */
81 Status = SampRegOpenKey(ContainerKeyHandle,
82 L"Names",
83 KEY_SET_VALUE,
84 &NamesKeyHandle);
85 if (!NT_SUCCESS(Status))
86 goto done;
87
88 /* Delete the account name value */
89 Status = SampRegDeleteValue(NamesKeyHandle,
90 lpAccountName);
91
92 done:
93 SampRegCloseKey(&NamesKeyHandle);
94 SampRegCloseKey(&ContainerKeyHandle);
95
96 return Status;
97 }
98
99
100 NTSTATUS
101 SampCheckAccountNameInDomain(IN PSAM_DB_OBJECT DomainObject,
102 IN LPCWSTR lpAccountName)
103 {
104 HANDLE AccountKey = NULL;
105 HANDLE NamesKey = NULL;
106 NTSTATUS Status;
107
108 TRACE("SampCheckAccountNameInDomain()\n");
109
110 Status = SampRegOpenKey(DomainObject->KeyHandle,
111 L"Aliases",
112 KEY_READ,
113 &AccountKey);
114 if (NT_SUCCESS(Status))
115 {
116 Status = SampRegOpenKey(AccountKey,
117 L"Names",
118 KEY_READ,
119 &NamesKey);
120 if (NT_SUCCESS(Status))
121 {
122 Status = SampRegQueryValue(NamesKey,
123 lpAccountName,
124 NULL,
125 NULL,
126 NULL);
127 if (Status == STATUS_SUCCESS)
128 {
129 SampRegCloseKey(&NamesKey);
130 Status = STATUS_ALIAS_EXISTS;
131 }
132 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
133 Status = STATUS_SUCCESS;
134 }
135
136 SampRegCloseKey(&AccountKey);
137 }
138
139 if (!NT_SUCCESS(Status))
140 {
141 TRACE("Checking for alias account failed (Status 0x%08lx)\n", Status);
142 return Status;
143 }
144
145 Status = SampRegOpenKey(DomainObject->KeyHandle,
146 L"Groups",
147 KEY_READ,
148 &AccountKey);
149 if (NT_SUCCESS(Status))
150 {
151 Status = SampRegOpenKey(AccountKey,
152 L"Names",
153 KEY_READ,
154 &NamesKey);
155 if (NT_SUCCESS(Status))
156 {
157 Status = SampRegQueryValue(NamesKey,
158 lpAccountName,
159 NULL,
160 NULL,
161 NULL);
162 if (Status == STATUS_SUCCESS)
163 {
164 SampRegCloseKey(&NamesKey);
165 Status = STATUS_ALIAS_EXISTS;
166 }
167 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
168 Status = STATUS_SUCCESS;
169 }
170
171 SampRegCloseKey(&AccountKey);
172 }
173
174 if (!NT_SUCCESS(Status))
175 {
176 TRACE("Checking for group account failed (Status 0x%08lx)\n", Status);
177 return Status;
178 }
179
180 Status = SampRegOpenKey(DomainObject->KeyHandle,
181 L"Users",
182 KEY_READ,
183 &AccountKey);
184 if (NT_SUCCESS(Status))
185 {
186 Status = SampRegOpenKey(AccountKey,
187 L"Names",
188 KEY_READ,
189 &NamesKey);
190 if (NT_SUCCESS(Status))
191 {
192 Status = SampRegQueryValue(NamesKey,
193 lpAccountName,
194 NULL,
195 NULL,
196 NULL);
197 if (Status == STATUS_SUCCESS)
198 {
199 SampRegCloseKey(&NamesKey);
200 Status = STATUS_ALIAS_EXISTS;
201 }
202 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
203 Status = STATUS_SUCCESS;
204 }
205
206 SampRegCloseKey(&AccountKey);
207 }
208
209 if (!NT_SUCCESS(Status))
210 {
211 TRACE("Checking for user account failed (Status 0x%08lx)\n", Status);
212 }
213
214 return Status;
215 }
216
217
218 NTSTATUS
219 SampRemoveMemberFromAllAliases(IN PSAM_DB_OBJECT DomainObject,
220 IN PRPC_SID MemberSid)
221 {
222 WCHAR AliasKeyName[64];
223 LPWSTR MemberSidString = NULL;
224 HANDLE AliasesKey = NULL;
225 HANDLE MembersKey = NULL;
226 HANDLE AliasKey = NULL;
227 ULONG Index;
228 NTSTATUS Status;
229
230 TRACE("(%p %p)\n", DomainObject, MemberSid);
231
232 ConvertSidToStringSidW(MemberSid, &MemberSidString);
233 TRACE("Member SID: %S\n", MemberSidString);
234
235 Status = SampRegOpenKey(DomainObject->KeyHandle,
236 L"Aliases",
237 KEY_READ,
238 &AliasesKey);
239 if (NT_SUCCESS(Status))
240 {
241 Index = 0;
242 while (TRUE)
243 {
244 Status = SampRegEnumerateSubKey(AliasesKey,
245 Index,
246 64,
247 AliasKeyName);
248 if (!NT_SUCCESS(Status))
249 {
250 if (Status == STATUS_NO_MORE_ENTRIES)
251 Status = STATUS_SUCCESS;
252 break;
253 }
254
255 TRACE("Alias key name: %S\n", AliasKeyName);
256
257 Status = SampRegOpenKey(AliasesKey,
258 AliasKeyName,
259 KEY_READ,
260 &AliasKey);
261 if (NT_SUCCESS(Status))
262 {
263 Status = SampRegOpenKey(AliasKey,
264 L"Members",
265 KEY_WRITE,
266 &MembersKey);
267 if (NT_SUCCESS(Status))
268 {
269 Status = SampRegDeleteValue(AliasKey,
270 MemberSidString);
271
272 SampRegCloseKey(&MembersKey);
273 }
274 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
275 Status = STATUS_SUCCESS;
276
277 SampRegCloseKey(&AliasKey);
278 }
279
280 Index++;
281 }
282
283 Status = SampRegOpenKey(AliasesKey,
284 L"Members",
285 KEY_WRITE,
286 &MembersKey);
287 if (NT_SUCCESS(Status))
288 {
289 Status = SampRegDeleteKey(MembersKey,
290 MemberSidString);
291 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
292 Status = STATUS_SUCCESS;
293
294 SampRegCloseKey(&MembersKey);
295 }
296
297 SampRegCloseKey(&AliasesKey);
298 }
299
300 if (MemberSidString != NULL)
301 LocalFree(MemberSidString);
302
303 return Status;
304 }
305
306 /* EOF */