[NETAPI32]
[reactos.git] / reactos / dll / win32 / netapi32 / misc.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: NetAPI DLL
4 * FILE: reactos/dll/win32/netapi32/misc.c
5 * PURPOSE: Helper functions
6 *
7 * PROGRAMMERS: Eric Kohl
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include "netapi32.h"
13
14
15 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
16
17
18 /* GLOBALS *******************************************************************/
19
20 static SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
21
22
23 /* FUNCTIONS *****************************************************************/
24
25 NTSTATUS
26 GetAccountDomainSid(IN PUNICODE_STRING ServerName,
27 OUT PSID *AccountDomainSid)
28 {
29 PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo = NULL;
30 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
31 LSA_HANDLE PolicyHandle = NULL;
32 ULONG Length = 0;
33 NTSTATUS Status;
34
35 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
36
37 Status = LsaOpenPolicy(ServerName,
38 &ObjectAttributes,
39 POLICY_VIEW_LOCAL_INFORMATION,
40 &PolicyHandle);
41 if (!NT_SUCCESS(Status))
42 {
43 ERR("LsaOpenPolicy failed (Status %08lx)\n", Status);
44 return Status;
45 }
46
47 Status = LsaQueryInformationPolicy(PolicyHandle,
48 PolicyAccountDomainInformation,
49 (PVOID *)&AccountDomainInfo);
50 if (!NT_SUCCESS(Status))
51 {
52 ERR("LsaQueryInformationPolicy failed (Status %08lx)\n", Status);
53 goto done;
54 }
55
56 Length = RtlLengthSid(AccountDomainInfo->DomainSid);
57
58 *AccountDomainSid = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length);
59 if (*AccountDomainSid == NULL)
60 {
61 ERR("Failed to allocate SID\n");
62 Status = STATUS_INSUFFICIENT_RESOURCES;
63 goto done;
64 }
65
66 memcpy(*AccountDomainSid, AccountDomainInfo->DomainSid, Length);
67
68 done:
69 if (AccountDomainInfo != NULL)
70 LsaFreeMemory(AccountDomainInfo);
71
72 LsaClose(PolicyHandle);
73
74 return Status;
75 }
76
77
78 NTSTATUS
79 GetBuiltinDomainSid(OUT PSID *BuiltinDomainSid)
80 {
81 PSID Sid = NULL;
82 PULONG Ptr;
83 NTSTATUS Status = STATUS_SUCCESS;
84
85 *BuiltinDomainSid = NULL;
86
87 Sid = RtlAllocateHeap(RtlGetProcessHeap(),
88 0,
89 RtlLengthRequiredSid(1));
90 if (Sid == NULL)
91 return STATUS_INSUFFICIENT_RESOURCES;
92
93 Status = RtlInitializeSid(Sid,
94 &NtAuthority,
95 1);
96 if (!NT_SUCCESS(Status))
97 goto done;
98
99 Ptr = RtlSubAuthoritySid(Sid, 0);
100 *Ptr = SECURITY_BUILTIN_DOMAIN_RID;
101
102 *BuiltinDomainSid = Sid;
103
104 done:
105 if (!NT_SUCCESS(Status))
106 {
107 if (Sid != NULL)
108 RtlFreeHeap(RtlGetProcessHeap(), 0, Sid);
109 }
110
111 return Status;
112 }
113
114
115 NTSTATUS
116 OpenAccountDomain(IN SAM_HANDLE ServerHandle,
117 IN PUNICODE_STRING ServerName,
118 IN ULONG DesiredAccess,
119 OUT PSAM_HANDLE DomainHandle)
120 {
121 PSID DomainSid = NULL;
122 NTSTATUS Status;
123
124 Status = GetAccountDomainSid(ServerName,
125 &DomainSid);
126 if (!NT_SUCCESS(Status))
127 {
128 ERR("GetAccountDomainSid failed (Status %08lx)\n", Status);
129 return Status;
130 }
131
132 Status = SamOpenDomain(ServerHandle,
133 DesiredAccess,
134 DomainSid,
135 DomainHandle);
136
137 RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid);
138
139 if (!NT_SUCCESS(Status))
140 {
141 ERR("SamOpenDomain failed (Status %08lx)\n", Status);
142 }
143
144 return Status;
145 }
146
147
148 NTSTATUS
149 OpenBuiltinDomain(IN SAM_HANDLE ServerHandle,
150 IN ULONG DesiredAccess,
151 OUT PSAM_HANDLE DomainHandle)
152 {
153 PSID DomainSid = NULL;
154 NTSTATUS Status;
155
156 Status = GetBuiltinDomainSid(&DomainSid);
157 if (!NT_SUCCESS(Status))
158 {
159 ERR("GetBuiltinDomainSid failed (Status %08lx)\n", Status);
160 return Status;
161 }
162
163 Status = SamOpenDomain(ServerHandle,
164 DesiredAccess,
165 DomainSid,
166 DomainHandle);
167
168 RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid);
169
170 if (!NT_SUCCESS(Status))
171 {
172 ERR("SamOpenDomain failed (Status %08lx)\n", Status);
173 }
174
175 return Status;
176 }
177
178 /* EOF */