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>
37#ifdef WINPR_HAVE_UNISTD_H
41#define TAG WINPR_TAG("utils")
52static WINPR_SAM_ENTRY* SamEntryFromDataA(LPCSTR User, DWORD UserLength, LPCSTR Domain,
55 WINPR_SAM_ENTRY* entry = calloc(1,
sizeof(WINPR_SAM_ENTRY));
58 if (User && (UserLength > 0))
59 entry->User = _strdup(User);
60 entry->UserLength = UserLength;
61 if (Domain && (DomainLength > 0))
62 entry->Domain = _strdup(Domain);
63 entry->DomainLength = DomainLength;
67static BOOL SamAreEntriesEqual(
const WINPR_SAM_ENTRY* a,
const WINPR_SAM_ENTRY* b)
71 if (a->UserLength != b->UserLength)
73 if (a->DomainLength != b->DomainLength)
75 if (a->UserLength > 0)
77 if (!a->User || !b->User)
79 if (strncmp(a->User, b->User, a->UserLength) != 0)
82 if (a->DomainLength > 0)
84 if (!a->Domain || !b->Domain)
86 if (strncmp(a->Domain, b->Domain, a->DomainLength) != 0)
92WINPR_SAM* SamOpen(
const char* filename, BOOL readOnly)
95 WINPR_SAM* sam =
nullptr;
96 char* allocatedFileName =
nullptr;
100 allocatedFileName = winpr_GetConfigFilePath(TRUE,
"SAM");
101 filename = allocatedFileName;
105 fp = winpr_fopen(filename,
"r");
108 fp = winpr_fopen(filename,
"r+");
111 fp = winpr_fopen(filename,
"w+");
113 free(allocatedFileName);
117 sam = (WINPR_SAM*)calloc(1,
sizeof(WINPR_SAM));
125 sam->readOnly = readOnly;
130 WLog_DBG(TAG,
"Could not open SAM file!");
137static BOOL SamLookupStart(WINPR_SAM* sam)
142 if (!sam || !sam->fp)
145 if (_fseeki64(sam->fp, 0, SEEK_END) != 0)
147 fileSize = _ftelli64(sam->fp);
148 if (_fseeki64(sam->fp, 0, SEEK_SET) != 0)
154 sam->context =
nullptr;
155 sam->buffer = (
char*)calloc((
size_t)fileSize + 2, 1);
160 readSize = fread(sam->buffer, (
size_t)fileSize, 1, sam->fp);
164 if (!ferror(sam->fp))
165 readSize = (size_t)fileSize;
171 sam->buffer =
nullptr;
175 sam->buffer[fileSize] =
'\n';
176 sam->buffer[fileSize + 1] =
'\0';
177 sam->line = strtok_s(sam->buffer,
"\n", &sam->context);
181static void SamLookupFinish(WINPR_SAM* sam)
184 sam->buffer =
nullptr;
188static BOOL SamReadEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
190 char* p[5] = WINPR_C_ARRAY_INIT;
193 if (!sam || !entry || !sam->line)
196 char* cur = sam->line;
198 while ((cur = strchr(cur,
':')) !=
nullptr)
208 p[1] = strchr(p[0],
':') + 1;
209 p[2] = strchr(p[1],
':') + 1;
210 p[3] = strchr(p[2],
':') + 1;
211 p[4] = strchr(p[3],
':') + 1;
212 const size_t LmHashLength = WINPR_ASSERTING_INT_CAST(
size_t, (p[3] - p[2] - 1));
213 const size_t NtHashLength = WINPR_ASSERTING_INT_CAST(
size_t, (p[4] - p[3] - 1));
215 if ((LmHashLength != 0) && (LmHashLength != 32))
218 if ((NtHashLength != 0) && (NtHashLength != 32))
221 entry->UserLength = (UINT32)(p[1] - p[0] - 1);
222 entry->User = (LPSTR)malloc(entry->UserLength + 1);
227 entry->User[entry->UserLength] =
'\0';
228 entry->DomainLength = (UINT32)(p[2] - p[1] - 1);
229 memcpy(entry->User, p[0], entry->UserLength);
231 if (entry->DomainLength > 0)
233 entry->Domain = (LPSTR)malloc(entry->DomainLength + 1);
238 entry->User =
nullptr;
242 memcpy(entry->Domain, p[1], entry->DomainLength);
243 entry->Domain[entry->DomainLength] =
'\0';
246 entry->Domain =
nullptr;
248 if (LmHashLength == 32)
251 winpr_HexStringToBinBuffer(p[2], LmHashLength, entry->LmHash,
sizeof(entry->LmHash));
256 if (NtHashLength == 32)
258 const size_t rc = winpr_HexStringToBinBuffer(p[3], NtHashLength, (BYTE*)entry->NtHash,
259 sizeof(entry->NtHash));
267void SamFreeEntry(WINPR_ATTR_UNUSED WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
269 SamResetEntry(entry);
273void SamResetEntry(WINPR_SAM_ENTRY* entry)
278 if (entry->UserLength)
281 entry->User =
nullptr;
284 if (entry->DomainLength)
287 entry->Domain =
nullptr;
290 ZeroMemory(entry->LmHash,
sizeof(entry->LmHash));
291 ZeroMemory(entry->NtHash,
sizeof(entry->NtHash));
294WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPCSTR User, UINT32 UserLength, LPCSTR Domain,
299 WINPR_SAM_ENTRY* search = SamEntryFromDataA(User, UserLength, Domain, DomainLength);
300 WINPR_SAM_ENTRY* entry = (WINPR_SAM_ENTRY*)calloc(1,
sizeof(WINPR_SAM_ENTRY));
302 if (!entry || !search)
305 if (!SamLookupStart(sam))
308 while (sam->line !=
nullptr)
310 length = strlen(sam->line);
314 if (sam->line[0] !=
'#')
316 if (!SamReadEntry(sam, entry))
321 if (SamAreEntriesEqual(entry, search))
329 SamResetEntry(entry);
330 sam->line = strtok_s(
nullptr,
"\n", &sam->context);
334 SamLookupFinish(sam);
336 SamFreeEntry(sam, search);
340 SamFreeEntry(sam, entry);
347WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPCWSTR User, UINT32 UserLength, LPCWSTR Domain,
350 WINPR_SAM_ENTRY* entry =
nullptr;
351 char* utfUser =
nullptr;
352 char* utfDomain =
nullptr;
353 size_t userCharLen = 0;
354 size_t domainCharLen = 0;
356 utfUser = ConvertWCharNToUtf8Alloc(User, UserLength /
sizeof(WCHAR), &userCharLen);
359 if (DomainLength > 0)
361 utfDomain = ConvertWCharNToUtf8Alloc(Domain, DomainLength /
sizeof(WCHAR), &domainCharLen);
365 entry = SamLookupUserA(sam, utfUser, (UINT32)userCharLen, utfDomain, (UINT32)domainCharLen);
370 entry->User =
nullptr;
371 entry->Domain =
nullptr;
373 entry->User = (
char*)winpr_wcsndup(User, UserLength /
sizeof(WCHAR));
374 entry->UserLength = UserLength;
376 entry->Domain = (
char*)winpr_wcsndup(Domain, DomainLength /
sizeof(WCHAR));
377 entry->DomainLength = DomainLength;
385void SamClose(WINPR_SAM* sam)
390 (void)fclose(sam->fp);