xref: /reactos/dll/win32/samsrv/domain.c (revision f6f20487)
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     OBJECT_ATTRIBUTES ObjectAttributes;
25     UNICODE_STRING KeyName;
26     UNICODE_STRING ValueName;
27     HANDLE ContainerKeyHandle = NULL;
28     HANDLE NamesKeyHandle = NULL;
29     NTSTATUS Status;
30 
31     TRACE("SampSetAccountNameInDomain()\n");
32 
33     /* Open the container key */
34     RtlInitUnicodeString(&KeyName, lpContainerName);
35 
36     InitializeObjectAttributes(&ObjectAttributes,
37                                &KeyName,
38                                OBJ_CASE_INSENSITIVE,
39                                DomainObject->KeyHandle,
40                                NULL);
41 
42     Status = NtOpenKey(&ContainerKeyHandle,
43                        KEY_ALL_ACCESS,
44                        &ObjectAttributes);
45     if (!NT_SUCCESS(Status))
46         return Status;
47 
48     /* Open the 'Names' key */
49     RtlInitUnicodeString(&KeyName, L"Names");
50 
51     InitializeObjectAttributes(&ObjectAttributes,
52                                &KeyName,
53                                OBJ_CASE_INSENSITIVE,
54                                ContainerKeyHandle,
55                                NULL);
56 
57     Status = NtOpenKey(&NamesKeyHandle,
58                        KEY_ALL_ACCESS,
59                        &ObjectAttributes);
60     if (!NT_SUCCESS(Status))
61         goto done;
62 
63     /* Set the alias value */
64     RtlInitUnicodeString(&ValueName, lpAccountName);
65 
66     Status = NtSetValueKey(NamesKeyHandle,
67                            &ValueName,
68                            0,
69                            REG_DWORD,
70                            (LPVOID)&ulRelativeId,
71                            sizeof(ULONG));
72 
73 done:
74     if (NamesKeyHandle)
75         NtClose(NamesKeyHandle);
76 
77     if (ContainerKeyHandle)
78         NtClose(ContainerKeyHandle);
79 
80     return Status;
81 }
82 
83 
84 NTSTATUS
85 SampRemoveAccountNameFromDomain(IN PSAM_DB_OBJECT DomainObject,
86                                 IN LPCWSTR lpContainerName,
87                                 IN LPCWSTR lpAccountName)
88 {
89     OBJECT_ATTRIBUTES ObjectAttributes;
90     UNICODE_STRING KeyName;
91     HANDLE ContainerKeyHandle = NULL;
92     HANDLE NamesKeyHandle = NULL;
93     NTSTATUS Status;
94 
95     TRACE("(%S %S)\n", lpContainerName, lpAccountName);
96 
97     /* Open the container key */
98     RtlInitUnicodeString(&KeyName, lpContainerName);
99 
100     InitializeObjectAttributes(&ObjectAttributes,
101                                &KeyName,
102                                OBJ_CASE_INSENSITIVE,
103                                DomainObject->KeyHandle,
104                                NULL);
105 
106     Status = NtOpenKey(&ContainerKeyHandle,
107                        KEY_ALL_ACCESS,
108                        &ObjectAttributes);
109     if (!NT_SUCCESS(Status))
110         return Status;
111 
112     /* Open the 'Names' key */
113     RtlInitUnicodeString(&KeyName, L"Names");
114 
115     InitializeObjectAttributes(&ObjectAttributes,
116                                &KeyName,
117                                OBJ_CASE_INSENSITIVE,
118                                ContainerKeyHandle,
119                                NULL);
120 
121     Status = NtOpenKey(&NamesKeyHandle,
122                        KEY_SET_VALUE,
123                        &ObjectAttributes);
124     if (!NT_SUCCESS(Status))
125         goto done;
126 
127     /* Delete the account name value */
128     Status = SampRegDeleteValue(NamesKeyHandle,
129                                 lpAccountName);
130 
131 done:
132     if (NamesKeyHandle)
133         NtClose(NamesKeyHandle);
134 
135     if (ContainerKeyHandle)
136         NtClose(ContainerKeyHandle);
137 
138     return Status;
139 }
140 
141 
142 NTSTATUS
143 SampCheckAccountNameInDomain(IN PSAM_DB_OBJECT DomainObject,
144                              IN LPCWSTR lpAccountName)
145 {
146     HANDLE AccountKey;
147     HANDLE NamesKey;
148     NTSTATUS Status;
149 
150     TRACE("SampCheckAccountNameInDomain()\n");
151 
152     Status = SampRegOpenKey(DomainObject->KeyHandle,
153                             L"Aliases",
154                             KEY_READ,
155                             &AccountKey);
156     if (NT_SUCCESS(Status))
157     {
158         Status = SampRegOpenKey(AccountKey,
159                                 L"Names",
160                                 KEY_READ,
161                                 &NamesKey);
162         if (NT_SUCCESS(Status))
163         {
164             Status = SampRegQueryValue(NamesKey,
165                                        lpAccountName,
166                                        NULL,
167                                        NULL,
168                                        NULL);
169             if (Status == STATUS_SUCCESS)
170             {
171                 SampRegCloseKey(NamesKey);
172                 Status = STATUS_ALIAS_EXISTS;
173             }
174             else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
175                 Status = STATUS_SUCCESS;
176         }
177 
178         SampRegCloseKey(AccountKey);
179     }
180 
181     if (!NT_SUCCESS(Status))
182     {
183         TRACE("Checking for alias account failed (Status 0x%08lx)\n", Status);
184         return Status;
185     }
186 
187     Status = SampRegOpenKey(DomainObject->KeyHandle,
188                             L"Groups",
189                             KEY_READ,
190                             &AccountKey);
191     if (NT_SUCCESS(Status))
192     {
193         Status = SampRegOpenKey(AccountKey,
194                                 L"Names",
195                                 KEY_READ,
196                                 &NamesKey);
197         if (NT_SUCCESS(Status))
198         {
199             Status = SampRegQueryValue(NamesKey,
200                                        lpAccountName,
201                                        NULL,
202                                        NULL,
203                                        NULL);
204             if (Status == STATUS_SUCCESS)
205             {
206                 SampRegCloseKey(NamesKey);
207                 Status = STATUS_ALIAS_EXISTS;
208             }
209             else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
210                 Status = STATUS_SUCCESS;
211         }
212 
213         SampRegCloseKey(AccountKey);
214     }
215 
216     if (!NT_SUCCESS(Status))
217     {
218         TRACE("Checking for group account failed (Status 0x%08lx)\n", Status);
219         return Status;
220     }
221 
222     Status = SampRegOpenKey(DomainObject->KeyHandle,
223                             L"Users",
224                             KEY_READ,
225                             &AccountKey);
226     if (NT_SUCCESS(Status))
227     {
228         Status = SampRegOpenKey(AccountKey,
229                                 L"Names",
230                                 KEY_READ,
231                                 &NamesKey);
232         if (NT_SUCCESS(Status))
233         {
234             Status = SampRegQueryValue(NamesKey,
235                                        lpAccountName,
236                                        NULL,
237                                        NULL,
238                                        NULL);
239             if (Status == STATUS_SUCCESS)
240             {
241                 SampRegCloseKey(NamesKey);
242                 Status = STATUS_ALIAS_EXISTS;
243             }
244             else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
245                 Status = STATUS_SUCCESS;
246         }
247 
248         SampRegCloseKey(AccountKey);
249     }
250 
251     if (!NT_SUCCESS(Status))
252     {
253         TRACE("Checking for user account failed (Status 0x%08lx)\n", Status);
254     }
255 
256     return Status;
257 }
258 
259 /* EOF */
260