518b8f4a2ccd1f22c877c77be21af478b5f8c270
[reactos.git] / reactos / dll / win32 / samsrv / utils.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: Security Account Manager (SAM) Server
4 * FILE: reactos/dll/win32/samsrv/utils.c
5 * PURPOSE: Utility functions
6 *
7 * PROGRAMMERS: Eric Kohl
8 */
9
10 /* INCLUDES ****************************************************************/
11
12 #include "samsrv.h"
13
14 WINE_DEFAULT_DEBUG_CHANNEL(samsrv);
15
16
17 /* GLOBALS *****************************************************************/
18
19
20 /* FUNCTIONS ***************************************************************/
21
22 INT
23 SampLoadString(HINSTANCE hInstance,
24 UINT uId,
25 LPWSTR lpBuffer,
26 INT nBufferMax)
27 {
28 HGLOBAL hmem;
29 HRSRC hrsrc;
30 WCHAR *p;
31 int string_num;
32 int i;
33
34 /* Use loword (incremented by 1) as resourceid */
35 hrsrc = FindResourceW(hInstance,
36 MAKEINTRESOURCEW((LOWORD(uId) >> 4) + 1),
37 (LPWSTR)RT_STRING);
38 if (!hrsrc)
39 return 0;
40
41 hmem = LoadResource(hInstance, hrsrc);
42 if (!hmem)
43 return 0;
44
45 p = LockResource(hmem);
46 string_num = uId & 0x000f;
47 for (i = 0; i < string_num; i++)
48 p += *p + 1;
49
50 i = min(nBufferMax - 1, *p);
51 if (i > 0)
52 {
53 memcpy(lpBuffer, p + 1, i * sizeof(WCHAR));
54 lpBuffer[i] = 0;
55 }
56 else
57 {
58 if (nBufferMax > 1)
59 {
60 lpBuffer[0] = 0;
61 return 0;
62 }
63 }
64
65 return i;
66 }
67
68
69 BOOL
70 SampIsSetupRunning(VOID)
71 {
72 DWORD dwError;
73 HKEY hKey;
74 DWORD dwType;
75 DWORD dwSize;
76 DWORD dwSetupType;
77
78 TRACE("SampIsSetupRunning()\n");
79
80 /* Open key */
81 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
82 L"SYSTEM\\Setup",
83 0,
84 KEY_QUERY_VALUE,
85 &hKey);
86 if (dwError != ERROR_SUCCESS)
87 return FALSE;
88
89 /* Read key */
90 dwSize = sizeof(DWORD);
91 dwError = RegQueryValueExW(hKey,
92 L"SetupType",
93 NULL,
94 &dwType,
95 (LPBYTE)&dwSetupType,
96 &dwSize);
97
98 /* Close key, and check if returned values are correct */
99 RegCloseKey(hKey);
100 if (dwError != ERROR_SUCCESS || dwType != REG_DWORD || dwSize != sizeof(DWORD))
101 return FALSE;
102
103 TRACE("SampIsSetupRunning() returns %s\n", (dwSetupType != 0) ? "TRUE" : "FALSE");
104 return (dwSetupType != 0);
105 }
106
107
108 PSID
109 AppendRidToSid(PSID SrcSid,
110 ULONG Rid)
111 {
112 ULONG Rids[8] = {0, 0, 0, 0, 0, 0, 0, 0};
113 UCHAR RidCount;
114 PSID DstSid;
115 ULONG i;
116
117 RidCount = *RtlSubAuthorityCountSid(SrcSid);
118 if (RidCount >= 8)
119 return NULL;
120
121 for (i = 0; i < RidCount; i++)
122 Rids[i] = *RtlSubAuthoritySid(SrcSid, i);
123
124 Rids[RidCount] = Rid;
125 RidCount++;
126
127 RtlAllocateAndInitializeSid(RtlIdentifierAuthoritySid(SrcSid),
128 RidCount,
129 Rids[0],
130 Rids[1],
131 Rids[2],
132 Rids[3],
133 Rids[4],
134 Rids[5],
135 Rids[6],
136 Rids[7],
137 &DstSid);
138
139 return DstSid;
140 }
141
142
143 NTSTATUS
144 SampGetRidFromSid(IN PSID Sid,
145 OUT PULONG Rid)
146 {
147 UCHAR RidCount;
148
149 RidCount = *RtlSubAuthorityCountSid(Sid);
150 if (RidCount < 1)
151 return STATUS_INVALID_SID;
152
153 *Rid = *RtlSubAuthoritySid(Sid, RidCount - 1);
154
155 return STATUS_SUCCESS;
156 }
157
158
159 NTSTATUS
160 SampCheckAccountName(IN PRPC_UNICODE_STRING AccountName,
161 IN USHORT MaxLength)
162 {
163 if (AccountName->Length > MaxLength * sizeof(WCHAR))
164 return STATUS_INVALID_ACCOUNT_NAME;
165
166 return STATUS_SUCCESS;
167 }
168
169 /* EOF */