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