20 #include <winpr/config.h>
21 #include <winpr/path.h>
27 #include <winpr/wtypes.h>
28 #include <winpr/crt.h>
29 #include <winpr/sam.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")
51 static 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;
66 static 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)
91 WINPR_SAM* SamOpen(
const char* filename, BOOL readOnly)
94 WINPR_SAM* sam = NULL;
95 char* allocatedFileName = NULL;
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!");
136 static 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)
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;
174 sam->buffer[fileSize] =
'\n';
175 sam->buffer[fileSize + 1] =
'\0';
176 sam->line = strtok_s(sam->buffer,
"\n", &sam->context);
180 static void SamLookupFinish(WINPR_SAM* sam)
187 static BOOL SamReadEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
192 if (!sam || !entry || !sam->line)
195 char* cur = sam->line;
197 while ((cur = strchr(cur,
':')) != NULL)
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);
241 memcpy(entry->Domain, p[1], entry->DomainLength);
242 entry->Domain[entry->DomainLength] =
'\0';
245 entry->Domain = NULL;
247 if (LmHashLength == 32)
248 winpr_HexStringToBinBuffer(p[2], LmHashLength, entry->LmHash,
sizeof(entry->LmHash));
250 if (NtHashLength == 32)
251 winpr_HexStringToBinBuffer(p[3], NtHashLength, (BYTE*)entry->NtHash,
sizeof(entry->NtHash));
256 void SamFreeEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
260 if (entry->UserLength > 0)
263 if (entry->DomainLength > 0)
270 void SamResetEntry(WINPR_SAM_ENTRY* entry)
275 if (entry->UserLength)
281 if (entry->DomainLength)
284 entry->Domain = NULL;
287 ZeroMemory(entry->LmHash,
sizeof(entry->LmHash));
288 ZeroMemory(entry->NtHash,
sizeof(entry->NtHash));
291 WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPCSTR User, UINT32 UserLength, LPCSTR Domain,
296 WINPR_SAM_ENTRY* search = SamEntryFromDataA(User, UserLength, Domain, DomainLength);
297 WINPR_SAM_ENTRY* entry = (WINPR_SAM_ENTRY*)calloc(1,
sizeof(WINPR_SAM_ENTRY));
299 if (!entry || !search)
302 if (!SamLookupStart(sam))
305 while (sam->line != NULL)
307 length = strlen(sam->line);
311 if (sam->line[0] !=
'#')
313 if (!SamReadEntry(sam, entry))
318 if (SamAreEntriesEqual(entry, search))
326 SamResetEntry(entry);
327 sam->line = strtok_s(NULL,
"\n", &sam->context);
331 SamLookupFinish(sam);
333 SamFreeEntry(sam, search);
337 SamFreeEntry(sam, entry);
344 WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPCWSTR User, UINT32 UserLength, LPCWSTR Domain,
347 WINPR_SAM_ENTRY* entry = NULL;
348 char* utfUser = NULL;
349 char* utfDomain = NULL;
350 size_t userCharLen = 0;
351 size_t domainCharLen = 0;
353 utfUser = ConvertWCharNToUtf8Alloc(User, UserLength /
sizeof(WCHAR), &userCharLen);
356 if (DomainLength > 0)
358 utfDomain = ConvertWCharNToUtf8Alloc(Domain, DomainLength /
sizeof(WCHAR), &domainCharLen);
362 entry = SamLookupUserA(sam, utfUser, (UINT32)userCharLen, utfDomain, (UINT32)domainCharLen);
369 void SamClose(WINPR_SAM* sam)
374 (void)fclose(sam->fp);