20#include <winpr/config.h>
21#include <winpr/path.h>
27#include <winpr/wtypes.h>
30#include <winpr/cast.h>
31#include <winpr/print.h>
32#include <winpr/file.h>
36#ifdef WINPR_HAVE_UNISTD_H
40#define TAG WINPR_TAG("utils")
51static WINPR_SAM_ENTRY* SamEntryFromDataA(LPCSTR User, DWORD UserLength, LPCSTR Domain,
54 WINPR_SAM_ENTRY* entry = calloc(1,
sizeof(WINPR_SAM_ENTRY));
57 if (User && (UserLength > 0))
58 entry->User = _strdup(User);
59 entry->UserLength = UserLength;
60 if (Domain && (DomainLength > 0))
61 entry->Domain = _strdup(Domain);
62 entry->DomainLength = DomainLength;
66static BOOL SamAreEntriesEqual(
const WINPR_SAM_ENTRY* a,
const WINPR_SAM_ENTRY* b)
70 if (a->UserLength != b->UserLength)
72 if (a->DomainLength != b->DomainLength)
74 if (a->UserLength > 0)
76 if (!a->User || !b->User)
78 if (strncmp(a->User, b->User, a->UserLength) != 0)
81 if (a->DomainLength > 0)
83 if (!a->Domain || !b->Domain)
85 if (strncmp(a->Domain, b->Domain, a->DomainLength) != 0)
91WINPR_SAM* SamOpen(
const char* filename, BOOL readOnly)
94 WINPR_SAM* sam =
nullptr;
95 char* allocatedFileName =
nullptr;
99 allocatedFileName = winpr_GetConfigFilePath(TRUE,
"SAM");
100 filename = allocatedFileName;
104 fp = winpr_fopen(filename,
"r");
107 fp = winpr_fopen(filename,
"r+");
110 fp = winpr_fopen(filename,
"w+");
112 free(allocatedFileName);
116 sam = (WINPR_SAM*)calloc(1,
sizeof(WINPR_SAM));
124 sam->readOnly = readOnly;
129 WLog_DBG(TAG,
"Could not open SAM file!");
136static BOOL SamLookupStart(WINPR_SAM* sam)
141 if (!sam || !sam->fp)
144 if (_fseeki64(sam->fp, 0, SEEK_END) != 0)
146 fileSize = _ftelli64(sam->fp);
147 if (_fseeki64(sam->fp, 0, SEEK_SET) != 0)
153 sam->context =
nullptr;
154 sam->buffer = (
char*)calloc((
size_t)fileSize + 2, 1);
159 readSize = fread(sam->buffer, (
size_t)fileSize, 1, sam->fp);
163 if (!ferror(sam->fp))
164 readSize = (size_t)fileSize;
170 sam->buffer =
nullptr;
174 sam->buffer[fileSize] =
'\n';
175 sam->buffer[fileSize + 1] =
'\0';
176 sam->line = strtok_s(sam->buffer,
"\n", &sam->context);
180static void SamLookupFinish(WINPR_SAM* sam)
183 sam->buffer =
nullptr;
187static BOOL SamReadEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
189 char* p[5] = WINPR_C_ARRAY_INIT;
192 if (!sam || !entry || !sam->line)
195 char* cur = sam->line;
197 while ((cur = strchr(cur,
':')) !=
nullptr)
207 p[1] = strchr(p[0],
':') + 1;
208 p[2] = strchr(p[1],
':') + 1;
209 p[3] = strchr(p[2],
':') + 1;
210 p[4] = strchr(p[3],
':') + 1;
211 const size_t LmHashLength = WINPR_ASSERTING_INT_CAST(
size_t, (p[3] - p[2] - 1));
212 const size_t NtHashLength = WINPR_ASSERTING_INT_CAST(
size_t, (p[4] - p[3] - 1));
214 if ((LmHashLength != 0) && (LmHashLength != 32))
217 if ((NtHashLength != 0) && (NtHashLength != 32))
220 entry->UserLength = (UINT32)(p[1] - p[0] - 1);
221 entry->User = (LPSTR)malloc(entry->UserLength + 1);
226 entry->User[entry->UserLength] =
'\0';
227 entry->DomainLength = (UINT32)(p[2] - p[1] - 1);
228 memcpy(entry->User, p[0], entry->UserLength);
230 if (entry->DomainLength > 0)
232 entry->Domain = (LPSTR)malloc(entry->DomainLength + 1);
237 entry->User =
nullptr;
241 memcpy(entry->Domain, p[1], entry->DomainLength);
242 entry->Domain[entry->DomainLength] =
'\0';
245 entry->Domain =
nullptr;
247 if (LmHashLength == 32)
249 if (winpr_HexStringToBinBuffer(p[2], LmHashLength, entry->LmHash,
sizeof(entry->LmHash)) !=
254 if (NtHashLength == 32)
256 if (winpr_HexStringToBinBuffer(p[3], NtHashLength, (BYTE*)entry->NtHash,
257 sizeof(entry->NtHash)) != 32)
264void SamFreeEntry(WINPR_ATTR_UNUSED WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
268 if (entry->UserLength > 0)
271 if (entry->DomainLength > 0)
278void SamResetEntry(WINPR_SAM_ENTRY* entry)
283 if (entry->UserLength)
286 entry->User =
nullptr;
289 if (entry->DomainLength)
292 entry->Domain =
nullptr;
295 ZeroMemory(entry->LmHash,
sizeof(entry->LmHash));
296 ZeroMemory(entry->NtHash,
sizeof(entry->NtHash));
299WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPCSTR User, UINT32 UserLength, LPCSTR Domain,
304 WINPR_SAM_ENTRY* search = SamEntryFromDataA(User, UserLength, Domain, DomainLength);
305 WINPR_SAM_ENTRY* entry = (WINPR_SAM_ENTRY*)calloc(1,
sizeof(WINPR_SAM_ENTRY));
307 if (!entry || !search)
310 if (!SamLookupStart(sam))
313 while (sam->line !=
nullptr)
315 length = strlen(sam->line);
319 if (sam->line[0] !=
'#')
321 if (!SamReadEntry(sam, entry))
326 if (SamAreEntriesEqual(entry, search))
334 SamResetEntry(entry);
335 sam->line = strtok_s(
nullptr,
"\n", &sam->context);
339 SamLookupFinish(sam);
341 SamFreeEntry(sam, search);
345 SamFreeEntry(sam, entry);
352WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPCWSTR User, UINT32 UserLength, LPCWSTR Domain,
355 WINPR_SAM_ENTRY* entry =
nullptr;
356 char* utfUser =
nullptr;
357 char* utfDomain =
nullptr;
358 size_t userCharLen = 0;
359 size_t domainCharLen = 0;
361 utfUser = ConvertWCharNToUtf8Alloc(User, UserLength /
sizeof(WCHAR), &userCharLen);
364 if (DomainLength > 0)
366 utfDomain = ConvertWCharNToUtf8Alloc(Domain, DomainLength /
sizeof(WCHAR), &domainCharLen);
370 entry = SamLookupUserA(sam, utfUser, (UINT32)userCharLen, utfDomain, (UINT32)domainCharLen);
377void SamClose(WINPR_SAM* sam)
382 (void)fclose(sam->fp);