2012-06-03 07:31:15 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Security Accounts Manager (SAM)
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-08-15 01:20:53 +04:00
|
|
|
|
2012-06-03 07:31:15 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-08-29 10:09:38 +03:00
|
|
|
#include <winpr/wtypes.h>
|
2012-06-03 07:31:15 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sam.h>
|
|
|
|
#include <winpr/print.h>
|
2021-05-31 12:42:03 +03:00
|
|
|
#include <winpr/file.h>
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2014-08-19 20:24:58 +04:00
|
|
|
#include "../log.h"
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2012-10-28 04:25:11 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-07-25 04:46:21 +04:00
|
|
|
#ifdef _WIN32
|
2019-11-06 17:24:51 +03:00
|
|
|
#define WINPR_SAM_FILE "C:\\SAM"
|
2012-07-25 04:46:21 +04:00
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
#define WINPR_SAM_FILE "/etc/winpr/SAM"
|
2012-07-25 04:46:21 +04:00
|
|
|
#endif
|
2014-08-19 20:24:58 +04:00
|
|
|
#define TAG WINPR_TAG("utils")
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2020-05-18 13:07:59 +03:00
|
|
|
struct winpr_sam
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
char* line;
|
|
|
|
char* buffer;
|
|
|
|
char* context;
|
|
|
|
BOOL readOnly;
|
|
|
|
};
|
|
|
|
|
2021-04-16 16:30:54 +03:00
|
|
|
static WINPR_SAM_ENTRY* SamEntryFromDataA(LPCSTR User, DWORD UserLength, LPCSTR Domain,
|
|
|
|
DWORD DomainLength)
|
|
|
|
{
|
|
|
|
WINPR_SAM_ENTRY* entry = calloc(1, sizeof(WINPR_SAM_ENTRY));
|
|
|
|
if (!entry)
|
|
|
|
return NULL;
|
2023-01-11 23:07:59 +03:00
|
|
|
if (User && (UserLength > 0))
|
|
|
|
entry->User = _strdup(User);
|
2021-04-16 16:30:54 +03:00
|
|
|
entry->UserLength = UserLength;
|
2023-01-11 23:07:59 +03:00
|
|
|
if (Domain && (DomainLength > 0))
|
|
|
|
entry->Domain = _strdup(Domain);
|
2021-04-16 16:30:54 +03:00
|
|
|
entry->DomainLength = DomainLength;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL SamAreEntriesEqual(const WINPR_SAM_ENTRY* a, const WINPR_SAM_ENTRY* b)
|
|
|
|
{
|
|
|
|
if (!a || !b)
|
|
|
|
return FALSE;
|
|
|
|
if (a->UserLength != b->UserLength)
|
|
|
|
return FALSE;
|
|
|
|
if (a->DomainLength != b->DomainLength)
|
|
|
|
return FALSE;
|
|
|
|
if (strncmp(a->User, b->User, a->UserLength) != 0)
|
|
|
|
return FALSE;
|
|
|
|
if (strncmp(a->Domain, b->Domain, a->DomainLength) != 0)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
WINPR_SAM* SamOpen(const char* filename, BOOL readOnly)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2012-08-07 14:52:52 +04:00
|
|
|
FILE* fp = NULL;
|
|
|
|
WINPR_SAM* sam = NULL;
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
if (!filename)
|
|
|
|
filename = WINPR_SAM_FILE;
|
|
|
|
|
|
|
|
if (readOnly)
|
2021-05-31 12:42:03 +03:00
|
|
|
fp = winpr_fopen(filename, "r");
|
2012-08-07 14:52:52 +04:00
|
|
|
else
|
|
|
|
{
|
2021-05-31 12:42:03 +03:00
|
|
|
fp = winpr_fopen(filename, "r+");
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2012-08-07 14:52:52 +04:00
|
|
|
if (!fp)
|
2021-05-31 12:42:03 +03:00
|
|
|
fp = winpr_fopen(filename, "w+");
|
2012-08-07 14:52:52 +04:00
|
|
|
}
|
2012-07-25 04:46:21 +04:00
|
|
|
|
2012-08-07 14:52:52 +04:00
|
|
|
if (fp)
|
|
|
|
{
|
2021-04-16 16:30:54 +03:00
|
|
|
sam = (WINPR_SAM*)calloc(1, sizeof(WINPR_SAM));
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sam)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-22 01:58:24 +03:00
|
|
|
|
|
|
|
sam->readOnly = readOnly;
|
2012-08-07 14:52:52 +04:00
|
|
|
sam->fp = fp;
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
2012-08-07 14:52:52 +04:00
|
|
|
else
|
2016-07-22 01:58:24 +03:00
|
|
|
{
|
2015-11-09 20:27:38 +03:00
|
|
|
WLog_DBG(TAG, "Could not open SAM file!");
|
2018-01-08 15:07:11 +03:00
|
|
|
return NULL;
|
2016-07-22 01:58:24 +03:00
|
|
|
}
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
return sam;
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:26:49 +04:00
|
|
|
static BOOL SamLookupStart(WINPR_SAM* sam)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2016-07-22 01:58:24 +03:00
|
|
|
size_t readSize;
|
2017-08-11 11:07:46 +03:00
|
|
|
INT64 fileSize;
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2017-12-21 16:39:43 +03:00
|
|
|
if (!sam || !sam->fp)
|
|
|
|
return FALSE;
|
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
_fseeki64(sam->fp, 0, SEEK_END);
|
|
|
|
fileSize = _ftelli64(sam->fp);
|
|
|
|
_fseeki64(sam->fp, 0, SEEK_SET);
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
if (fileSize < 1)
|
2012-07-25 04:46:21 +04:00
|
|
|
return FALSE;
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2020-05-18 13:07:59 +03:00
|
|
|
sam->context = NULL;
|
2021-06-16 15:43:07 +03:00
|
|
|
sam->buffer = (char*)calloc((size_t)fileSize + 2, 1);
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sam->buffer)
|
|
|
|
return FALSE;
|
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
readSize = fread(sam->buffer, (size_t)fileSize, 1, sam->fp);
|
2012-07-25 04:46:21 +04:00
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
if (!readSize)
|
2012-07-25 04:46:21 +04:00
|
|
|
{
|
|
|
|
if (!ferror(sam->fp))
|
2021-06-16 15:43:07 +03:00
|
|
|
readSize = (size_t)fileSize;
|
2012-07-25 04:46:21 +04:00
|
|
|
}
|
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
if (readSize < 1)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
free(sam->buffer);
|
2012-07-25 04:46:21 +04:00
|
|
|
sam->buffer = NULL;
|
|
|
|
return FALSE;
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
sam->buffer[fileSize] = '\n';
|
|
|
|
sam->buffer[fileSize + 1] = '\0';
|
2020-05-18 13:07:59 +03:00
|
|
|
sam->line = strtok_s(sam->buffer, "\n", &sam->context);
|
2012-07-29 03:30:21 +04:00
|
|
|
return TRUE;
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-23 19:26:49 +04:00
|
|
|
static void SamLookupFinish(WINPR_SAM* sam)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
free(sam->buffer);
|
|
|
|
sam->buffer = NULL;
|
|
|
|
sam->line = NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static BOOL SamReadEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2017-12-21 16:39:43 +03:00
|
|
|
char* p[5];
|
|
|
|
size_t LmHashLength;
|
|
|
|
size_t NtHashLength;
|
|
|
|
size_t count = 0;
|
|
|
|
char* cur;
|
|
|
|
|
|
|
|
if (!sam || !entry || !sam->line)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
cur = sam->line;
|
|
|
|
|
|
|
|
while ((cur = strchr(cur, ':')) != NULL)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < 4)
|
|
|
|
return FALSE;
|
2015-04-03 17:21:01 +03:00
|
|
|
|
2012-06-03 07:31:15 +04:00
|
|
|
p[0] = sam->line;
|
|
|
|
p[1] = strchr(p[0], ':') + 1;
|
|
|
|
p[2] = strchr(p[1], ':') + 1;
|
|
|
|
p[3] = strchr(p[2], ':') + 1;
|
2012-07-23 07:23:23 +04:00
|
|
|
p[4] = strchr(p[3], ':') + 1;
|
2017-12-21 16:39:43 +03:00
|
|
|
LmHashLength = (p[3] - p[2] - 1);
|
|
|
|
NtHashLength = (p[4] - p[3] - 1);
|
|
|
|
|
|
|
|
if ((LmHashLength != 0) && (LmHashLength != 32))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ((NtHashLength != 0) && (NtHashLength != 32))
|
|
|
|
return FALSE;
|
|
|
|
|
2014-08-19 20:24:58 +04:00
|
|
|
entry->UserLength = (UINT32)(p[1] - p[0] - 1);
|
2019-11-06 17:24:51 +03:00
|
|
|
entry->User = (LPSTR)malloc(entry->UserLength + 1);
|
2017-12-21 16:39:43 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!entry->User)
|
|
|
|
return FALSE;
|
2017-12-21 16:39:43 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
entry->User[entry->UserLength] = '\0';
|
2014-08-19 20:24:58 +04:00
|
|
|
entry->DomainLength = (UINT32)(p[2] - p[1] - 1);
|
2012-06-03 07:31:15 +04:00
|
|
|
memcpy(entry->User, p[0], entry->UserLength);
|
|
|
|
|
|
|
|
if (entry->DomainLength > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
entry->Domain = (LPSTR)malloc(entry->DomainLength + 1);
|
2017-12-21 16:39:43 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!entry->Domain)
|
|
|
|
{
|
|
|
|
free(entry->User);
|
|
|
|
entry->User = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-12-21 16:39:43 +03:00
|
|
|
|
2012-06-03 07:31:15 +04:00
|
|
|
memcpy(entry->Domain, p[1], entry->DomainLength);
|
|
|
|
entry->Domain[entry->DomainLength] = '\0';
|
|
|
|
}
|
2012-06-29 19:36:31 +04:00
|
|
|
else
|
|
|
|
entry->Domain = NULL;
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
if (LmHashLength == 32)
|
2021-04-19 11:06:14 +03:00
|
|
|
winpr_HexStringToBinBuffer(p[2], LmHashLength, entry->LmHash, sizeof(entry->LmHash));
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
if (NtHashLength == 32)
|
2021-04-19 11:06:14 +03:00
|
|
|
winpr_HexStringToBinBuffer(p[3], NtHashLength, (BYTE*)entry->NtHash, sizeof(entry->NtHash));
|
2012-06-04 00:30:15 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
return TRUE;
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SamFreeEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
|
|
|
|
{
|
2012-06-04 00:30:15 +04:00
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (entry->UserLength > 0)
|
|
|
|
free(entry->User);
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2012-06-04 00:30:15 +04:00
|
|
|
if (entry->DomainLength > 0)
|
|
|
|
free(entry->Domain);
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2012-06-04 00:30:15 +04:00
|
|
|
free(entry);
|
|
|
|
}
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
void SamResetEntry(WINPR_SAM_ENTRY* entry)
|
|
|
|
{
|
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (entry->UserLength)
|
|
|
|
{
|
|
|
|
free(entry->User);
|
|
|
|
entry->User = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->DomainLength)
|
|
|
|
{
|
|
|
|
free(entry->Domain);
|
|
|
|
entry->Domain = NULL;
|
|
|
|
}
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
ZeroMemory(entry->LmHash, sizeof(entry->LmHash));
|
|
|
|
ZeroMemory(entry->NtHash, sizeof(entry->NtHash));
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPCSTR User, UINT32 UserLength, LPCSTR Domain,
|
2017-12-21 16:39:43 +03:00
|
|
|
UINT32 DomainLength)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2017-12-21 16:39:43 +03:00
|
|
|
size_t length;
|
2015-04-03 17:21:01 +03:00
|
|
|
BOOL found = FALSE;
|
2021-04-16 16:30:54 +03:00
|
|
|
WINPR_SAM_ENTRY* search = SamEntryFromDataA(User, UserLength, Domain, DomainLength);
|
|
|
|
WINPR_SAM_ENTRY* entry = (WINPR_SAM_ENTRY*)calloc(1, sizeof(WINPR_SAM_ENTRY));
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2021-04-16 16:30:54 +03:00
|
|
|
if (!entry || !search)
|
|
|
|
goto fail;
|
2015-04-03 17:21:01 +03:00
|
|
|
|
|
|
|
if (!SamLookupStart(sam))
|
2021-04-16 16:30:54 +03:00
|
|
|
goto fail;
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
while (sam->line != NULL)
|
|
|
|
{
|
2017-12-21 16:39:43 +03:00
|
|
|
length = strlen(sam->line);
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
if (length > 1)
|
|
|
|
{
|
|
|
|
if (sam->line[0] != '#')
|
|
|
|
{
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!SamReadEntry(sam, entry))
|
|
|
|
{
|
|
|
|
goto out_fail;
|
|
|
|
}
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2021-04-16 16:30:54 +03:00
|
|
|
if (SamAreEntriesEqual(entry, search))
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 16:39:43 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
SamResetEntry(entry);
|
2020-05-19 09:01:48 +03:00
|
|
|
sam->line = strtok_s(NULL, "\n", &sam->context);
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
out_fail:
|
2012-06-03 07:31:15 +04:00
|
|
|
SamLookupFinish(sam);
|
2021-04-16 16:30:54 +03:00
|
|
|
fail:
|
|
|
|
SamFreeEntry(sam, search);
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
2021-04-16 16:30:54 +03:00
|
|
|
SamFreeEntry(sam, entry);
|
2012-06-03 07:31:15 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPCWSTR User, UINT32 UserLength, LPCWSTR Domain,
|
2017-12-21 16:39:43 +03:00
|
|
|
UINT32 DomainLength)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2021-04-16 16:30:54 +03:00
|
|
|
WINPR_SAM_ENTRY* entry = NULL;
|
|
|
|
char* utfUser = NULL;
|
|
|
|
char* utfDomain = NULL;
|
2022-10-28 09:09:27 +03:00
|
|
|
size_t userCharLen = 0;
|
|
|
|
size_t domainCharLen = 0;
|
|
|
|
|
|
|
|
utfUser = ConvertWCharNToUtf8Alloc(User, UserLength / sizeof(WCHAR), &userCharLen);
|
|
|
|
if (!utfUser)
|
2021-04-16 16:30:54 +03:00
|
|
|
goto fail;
|
2023-01-11 21:52:33 +03:00
|
|
|
if (DomainLength > 0)
|
|
|
|
{
|
|
|
|
utfDomain = ConvertWCharNToUtf8Alloc(Domain, DomainLength / sizeof(WCHAR), &domainCharLen);
|
|
|
|
if (!utfDomain)
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-10-28 09:09:27 +03:00
|
|
|
entry = SamLookupUserA(sam, utfUser, userCharLen, utfDomain, domainCharLen);
|
2021-04-16 16:30:54 +03:00
|
|
|
fail:
|
|
|
|
free(utfUser);
|
|
|
|
free(utfDomain);
|
2012-06-03 07:31:15 +04:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SamClose(WINPR_SAM* sam)
|
|
|
|
{
|
|
|
|
if (sam != NULL)
|
|
|
|
{
|
2023-04-27 10:23:51 +03:00
|
|
|
if (sam->fp)
|
|
|
|
fclose(sam->fp);
|
2012-06-03 07:31:15 +04:00
|
|
|
free(sam);
|
|
|
|
}
|
|
|
|
}
|