[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 <stdarg.h>
13
14 #include "ntstatus.h"
15 #define WIN32_NO_STATUS
16 #include "windef.h"
17 #include "winbase.h"
18 #include "winerror.h"
19 #include "lmcons.h"
20 #include "ntsecapi.h"
21 #include "wine/debug.h"
22
23 #define NTOS_MODE_USER
24 #include <ndk/rtlfuncs.h>
25 #include "ntsam.h"
26 #include "netapi32.h"
27
28
29 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
30
31
32 /* GLOBALS *******************************************************************/
33
34 static SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
35
36
37 /* FUNCTIONS *****************************************************************/
38
39 NTSTATUS
40 GetAccountDomainSid(IN PUNICODE_STRING ServerName,
41 OUT PSID *AccountDomainSid)
42 {
43 PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo = NULL;
44 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
45 LSA_HANDLE PolicyHandle = NULL;
46 ULONG Length = 0;
47 NTSTATUS Status;
48
49 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
50
51 Status = LsaOpenPolicy(ServerName,
52 &ObjectAttributes,
53 POLICY_VIEW_LOCAL_INFORMATION,
54 &PolicyHandle);
55 if (!NT_SUCCESS(Status))
56 {
57 ERR("LsaOpenPolicy failed (Status %08lx)\n", Status);
58 return Status;
59 }
60
61 Status = LsaQueryInformationPolicy(PolicyHandle,
62 PolicyAccountDomainInformation,
63 (PVOID *)&AccountDomainInfo);
64 if (!NT_SUCCESS(Status))
65 {
66 ERR("LsaQueryInformationPolicy failed (Status %08lx)\n", Status);
67 goto done;
68 }
69
70 Length = RtlLengthSid(AccountDomainInfo->DomainSid);
71
72 *AccountDomainSid = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length);
73 if (*AccountDomainSid == NULL)
74 {
75 ERR("Failed to allocate SID\n");
76 Status = STATUS_INSUFFICIENT_RESOURCES;
77 goto done;
78 }
79
80 memcpy(*AccountDomainSid, AccountDomainInfo->DomainSid, Length);
81
82 done:
83 if (AccountDomainInfo != NULL)
84 LsaFreeMemory(AccountDomainInfo);
85
86 LsaClose(PolicyHandle);
87
88 return Status;
89 }
90
91
92 NTSTATUS
93 GetBuiltinDomainSid(OUT PSID *BuiltinDomainSid)
94 {
95 PSID Sid = NULL;
96 PULONG Ptr;
97 NTSTATUS Status = STATUS_SUCCESS;
98
99 *BuiltinDomainSid = NULL;
100
101 Sid = RtlAllocateHeap(RtlGetProcessHeap(),
102 0,
103 RtlLengthRequiredSid(1));
104 if (Sid == NULL)
105 return STATUS_INSUFFICIENT_RESOURCES;
106
107 Status = RtlInitializeSid(Sid,
108 &NtAuthority,
109 1);
110 if (!NT_SUCCESS(Status))
111 goto done;
112
113 Ptr = RtlSubAuthoritySid(Sid, 0);
114 *Ptr = SECURITY_BUILTIN_DOMAIN_RID;
115
116 *BuiltinDomainSid = Sid;
117
118 done:
119 if (!NT_SUCCESS(Status))
120 {
121 if (Sid != NULL)
122 RtlFreeHeap(RtlGetProcessHeap(), 0, Sid);
123 }
124
125 return Status;
126 }
127
128 /* EOF */