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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-03 07:31:15 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sam.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
|
2014-08-19 20:24:58 +04:00
|
|
|
#include "../log.h"
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2012-10-28 04:25:11 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-07-25 04:46:21 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WINPR_SAM_FILE "C:\\SAM"
|
|
|
|
#else
|
2012-06-03 07:31:15 +04: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
|
|
|
|
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)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2016-07-22 01:58:24 +03:00
|
|
|
fp = fopen(filename, "r");
|
2012-08-07 14:52:52 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-22 01:58:24 +03:00
|
|
|
fp = fopen(filename, "r+");
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2012-08-07 14:52:52 +04:00
|
|
|
if (!fp)
|
2016-07-22 01:58:24 +03:00
|
|
|
fp = 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)
|
|
|
|
{
|
|
|
|
sam = (WINPR_SAM*) malloc(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!");
|
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;
|
|
|
|
long int fileSize;
|
|
|
|
|
2012-06-03 07:31:15 +04:00
|
|
|
fseek(sam->fp, 0, SEEK_END);
|
2016-07-22 01:58:24 +03:00
|
|
|
fileSize = ftell(sam->fp);
|
2012-06-03 07:31:15 +04:00
|
|
|
fseek(sam->fp, 0, SEEK_SET);
|
|
|
|
|
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
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
sam->buffer = (char*) malloc(fileSize + 2);
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sam->buffer)
|
|
|
|
return FALSE;
|
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
readSize = fread(sam->buffer, 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))
|
2016-07-22 01:58:24 +03:00
|
|
|
readSize = 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';
|
2012-06-03 07:31:15 +04:00
|
|
|
sam->line = strtok(sam->buffer, "\n");
|
2016-07-22 01:58:24 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:26:49 +04:00
|
|
|
static void HexStrToBin(char* str, BYTE* bin, int length)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
CharUpperBuffA(str, length * 2);
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
bin[i] = 0;
|
|
|
|
|
|
|
|
if ((str[i * 2] >= '0') && (str[i * 2] <= '9'))
|
|
|
|
bin[i] |= (str[i * 2] - '0') << 4;
|
|
|
|
|
|
|
|
if ((str[i * 2] >= 'A') && (str[i * 2] <= 'F'))
|
|
|
|
bin[i] |= (str[i * 2] - 'A' + 10) << 4;
|
|
|
|
|
|
|
|
if ((str[i * 2 + 1] >= '0') && (str[i * 2 + 1] <= '9'))
|
|
|
|
bin[i] |= (str[i * 2 + 1] - '0');
|
|
|
|
|
|
|
|
if ((str[i * 2 + 1] >= 'A') && (str[i * 2 + 1] <= 'F'))
|
|
|
|
bin[i] |= (str[i * 2 + 1] - 'A' + 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
BOOL SamReadEntry(WINPR_SAM *sam, WINPR_SAM_ENTRY *entry)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2012-07-23 07:23:23 +04:00
|
|
|
char* p[7];
|
2012-06-03 07:31:15 +04:00
|
|
|
int LmHashLength;
|
|
|
|
int NtHashLength;
|
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;
|
|
|
|
p[5] = strchr(p[4], ':') + 1;
|
|
|
|
p[6] = p[0] + strlen(p[0]);
|
2014-08-19 20:24:58 +04:00
|
|
|
entry->UserLength = (UINT32)(p[1] - p[0] - 1);
|
2015-04-03 17:21:01 +03:00
|
|
|
entry->User = (LPSTR) malloc(entry->UserLength + 1);
|
|
|
|
if (!entry->User)
|
|
|
|
return FALSE;
|
|
|
|
entry->User[entry->UserLength] = '\0';
|
2014-08-19 20:24:58 +04:00
|
|
|
entry->DomainLength = (UINT32)(p[2] - p[1] - 1);
|
|
|
|
LmHashLength = (int)(p[3] - p[2] - 1);
|
|
|
|
NtHashLength = (int)(p[4] - p[3] - 1);
|
2012-06-03 07:31:15 +04:00
|
|
|
memcpy(entry->User, p[0], entry->UserLength);
|
|
|
|
|
|
|
|
if (entry->DomainLength > 0)
|
|
|
|
{
|
|
|
|
entry->Domain = (LPSTR) malloc(entry->DomainLength + 1);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!entry->Domain)
|
|
|
|
{
|
|
|
|
free(entry->User);
|
|
|
|
entry->User = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
HexStrToBin(p[2], (BYTE*) entry->LmHash, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NtHashLength == 32)
|
|
|
|
{
|
|
|
|
HexStrToBin(p[3], (BYTE*) entry->NtHash, 16);
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
|
2012-06-04 00:30:15 +04:00
|
|
|
WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPSTR User, UINT32 UserLength, LPSTR Domain, UINT32 DomainLength)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
int length;
|
2015-04-03 17:21:01 +03:00
|
|
|
BOOL found = FALSE;
|
2012-06-03 07:31:15 +04:00
|
|
|
WINPR_SAM_ENTRY* entry;
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
entry = (WINPR_SAM_ENTRY*) calloc(1, sizeof(WINPR_SAM_ENTRY));
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!entry)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!SamLookupStart(sam))
|
2015-06-23 22:29:21 +03:00
|
|
|
{
|
|
|
|
free(entry);
|
2015-04-03 17:21:01 +03:00
|
|
|
return NULL;
|
2015-06-23 22:29:21 +03:00
|
|
|
}
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
while (sam->line != NULL)
|
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
length = (int) 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
|
|
|
|
|
|
|
if (strcmp(User, entry->User) == 0)
|
|
|
|
{
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 17:21:01 +03:00
|
|
|
SamResetEntry(entry);
|
2012-06-03 07:31:15 +04:00
|
|
|
sam->line = strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
out_fail:
|
2012-06-03 07:31:15 +04:00
|
|
|
SamLookupFinish(sam);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
free(entry);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2012-06-04 00:30:15 +04:00
|
|
|
WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPWSTR User, UINT32 UserLength, LPWSTR Domain, UINT32 DomainLength)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
int length;
|
2015-04-03 17:21:01 +03:00
|
|
|
BOOL Found = FALSE;
|
2012-06-29 19:36:31 +04:00
|
|
|
BOOL UserMatch;
|
|
|
|
BOOL DomainMatch;
|
2015-04-03 17:21:01 +03:00
|
|
|
LPWSTR EntryUser = NULL;
|
2012-06-03 07:31:15 +04:00
|
|
|
UINT32 EntryUserLength;
|
2015-04-03 17:21:01 +03:00
|
|
|
LPWSTR EntryDomain = NULL;
|
2012-06-29 19:36:31 +04:00
|
|
|
UINT32 EntryDomainLength;
|
2012-06-03 07:31:15 +04:00
|
|
|
WINPR_SAM_ENTRY* entry;
|
2015-04-03 17:21:01 +03:00
|
|
|
|
|
|
|
if (!(entry = (WINPR_SAM_ENTRY*) calloc(1, sizeof(WINPR_SAM_ENTRY))))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!SamLookupStart(sam))
|
|
|
|
return NULL;
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
while (sam->line != NULL)
|
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
length = (int) strlen(sam->line);
|
2012-06-03 07:31:15 +04:00
|
|
|
|
|
|
|
if (length > 1)
|
|
|
|
{
|
|
|
|
if (sam->line[0] != '#')
|
|
|
|
{
|
2012-06-29 19:36:31 +04:00
|
|
|
DomainMatch = 0;
|
|
|
|
UserMatch = 0;
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!SamReadEntry(sam, entry))
|
|
|
|
goto out_fail;
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2012-06-29 19:36:31 +04:00
|
|
|
if (DomainLength > 0)
|
|
|
|
{
|
|
|
|
if (entry->DomainLength > 0)
|
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
EntryDomainLength = (UINT32) strlen(entry->Domain) * 2;
|
2012-06-29 19:36:31 +04:00
|
|
|
EntryDomain = (LPWSTR) malloc(EntryDomainLength + 2);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!EntryDomain)
|
|
|
|
goto out_fail;
|
2012-06-29 19:36:31 +04:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, entry->Domain, EntryDomainLength / 2,
|
2014-08-19 20:24:58 +04:00
|
|
|
(LPWSTR) EntryDomain, EntryDomainLength / 2);
|
2012-06-29 19:36:31 +04:00
|
|
|
|
|
|
|
if (DomainLength == EntryDomainLength)
|
|
|
|
{
|
|
|
|
if (memcmp(Domain, EntryDomain, DomainLength) == 0)
|
|
|
|
{
|
|
|
|
DomainMatch = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 20:24:58 +04:00
|
|
|
|
2013-08-29 12:02:24 +04:00
|
|
|
free(EntryDomain);
|
2012-06-29 19:36:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DomainMatch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DomainMatch = 1;
|
|
|
|
}
|
2012-06-03 07:31:15 +04:00
|
|
|
|
2012-06-29 19:36:31 +04:00
|
|
|
if (DomainMatch)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
EntryUserLength = (UINT32) strlen(entry->User) * 2;
|
2012-06-29 19:36:31 +04:00
|
|
|
EntryUser = (LPWSTR) malloc(EntryUserLength + 2);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!EntryUser)
|
|
|
|
goto out_fail;
|
2012-06-29 19:36:31 +04:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, entry->User, EntryUserLength / 2,
|
2014-08-19 20:24:58 +04:00
|
|
|
(LPWSTR) EntryUser, EntryUserLength / 2);
|
2012-06-29 19:36:31 +04:00
|
|
|
|
|
|
|
if (UserLength == EntryUserLength)
|
2012-06-04 03:59:35 +04:00
|
|
|
{
|
2012-06-29 19:36:31 +04:00
|
|
|
if (memcmp(User, EntryUser, UserLength) == 0)
|
|
|
|
{
|
|
|
|
UserMatch = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 09:18:47 +04:00
|
|
|
free(EntryUser);
|
|
|
|
|
2012-06-29 19:36:31 +04:00
|
|
|
if (UserMatch && DomainMatch)
|
|
|
|
{
|
2015-04-03 17:21:01 +03:00
|
|
|
Found = TRUE;
|
2012-06-04 03:59:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-06-03 07:31:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
SamResetEntry(entry);
|
2012-06-03 07:31:15 +04:00
|
|
|
sam->line = strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
out_fail:
|
2012-06-03 07:31:15 +04:00
|
|
|
SamLookupFinish(sam);
|
|
|
|
|
2012-06-29 19:36:31 +04:00
|
|
|
if (!Found)
|
2012-06-03 07:31:15 +04:00
|
|
|
{
|
|
|
|
free(entry);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SamClose(WINPR_SAM* sam)
|
|
|
|
{
|
|
|
|
if (sam != NULL)
|
|
|
|
{
|
|
|
|
fclose(sam->fp);
|
|
|
|
free(sam);
|
|
|
|
}
|
|
|
|
}
|