Coverage Report

Created: 2026-05-11 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/utils/sam.c
Line
Count
Source
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Security Accounts Manager (SAM)
4
 *
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <winpr/config.h>
21
#include <winpr/path.h>
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
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>
33
34
#include "../log.h"
35
#include "../utils.h"
36
37
#ifdef WINPR_HAVE_UNISTD_H
38
#include <unistd.h>
39
#endif
40
41
#define TAG WINPR_TAG("utils")
42
43
struct winpr_sam
44
{
45
  FILE* fp;
46
  char* line;
47
  char* buffer;
48
  char* context;
49
  BOOL readOnly;
50
};
51
52
static WINPR_SAM_ENTRY* SamEntryFromDataA(LPCSTR User, DWORD UserLength, LPCSTR Domain,
53
                                          DWORD DomainLength)
54
0
{
55
0
  WINPR_SAM_ENTRY* entry = calloc(1, sizeof(WINPR_SAM_ENTRY));
56
0
  if (!entry)
57
0
    return nullptr;
58
0
  if (User && (UserLength > 0))
59
0
    entry->User = _strdup(User);
60
0
  entry->UserLength = UserLength;
61
0
  if (Domain && (DomainLength > 0))
62
0
    entry->Domain = _strdup(Domain);
63
0
  entry->DomainLength = DomainLength;
64
0
  return entry;
65
0
}
66
67
static BOOL SamAreEntriesEqual(const WINPR_SAM_ENTRY* a, const WINPR_SAM_ENTRY* b)
68
0
{
69
0
  if (!a || !b)
70
0
    return FALSE;
71
0
  if (a->UserLength != b->UserLength)
72
0
    return FALSE;
73
0
  if (a->DomainLength != b->DomainLength)
74
0
    return FALSE;
75
0
  if (a->UserLength > 0)
76
0
  {
77
0
    if (!a->User || !b->User)
78
0
      return FALSE;
79
0
    if (strncmp(a->User, b->User, a->UserLength) != 0)
80
0
      return FALSE;
81
0
  }
82
0
  if (a->DomainLength > 0)
83
0
  {
84
0
    if (!a->Domain || !b->Domain)
85
0
      return FALSE;
86
0
    if (strncmp(a->Domain, b->Domain, a->DomainLength) != 0)
87
0
      return FALSE;
88
0
  }
89
0
  return TRUE;
90
0
}
91
92
WINPR_SAM* SamOpen(const char* filename, BOOL readOnly)
93
0
{
94
0
  FILE* fp = nullptr;
95
0
  WINPR_SAM* sam = nullptr;
96
0
  char* allocatedFileName = nullptr;
97
98
0
  if (!filename)
99
0
  {
100
0
    allocatedFileName = winpr_GetConfigFilePath(TRUE, "SAM");
101
0
    filename = allocatedFileName;
102
0
  }
103
104
0
  if (readOnly)
105
0
    fp = winpr_fopen(filename, "r");
106
0
  else
107
0
  {
108
0
    fp = winpr_fopen(filename, "r+");
109
110
0
    if (!fp)
111
0
      fp = winpr_fopen(filename, "w+");
112
0
  }
113
0
  free(allocatedFileName);
114
115
0
  if (fp)
116
0
  {
117
0
    sam = (WINPR_SAM*)calloc(1, sizeof(WINPR_SAM));
118
119
0
    if (!sam)
120
0
    {
121
0
      (void)fclose(fp);
122
0
      return nullptr;
123
0
    }
124
125
0
    sam->readOnly = readOnly;
126
0
    sam->fp = fp;
127
0
  }
128
0
  else
129
0
  {
130
0
    WLog_DBG(TAG, "Could not open SAM file!");
131
0
    return nullptr;
132
0
  }
133
134
0
  return sam;
135
0
}
136
137
static BOOL SamLookupStart(WINPR_SAM* sam)
138
0
{
139
0
  size_t readSize = 0;
140
0
  INT64 fileSize = 0;
141
142
0
  if (!sam || !sam->fp)
143
0
    return FALSE;
144
145
0
  if (_fseeki64(sam->fp, 0, SEEK_END) != 0)
146
0
    return FALSE;
147
0
  fileSize = _ftelli64(sam->fp);
148
0
  if (_fseeki64(sam->fp, 0, SEEK_SET) != 0)
149
0
    return FALSE;
150
151
0
  if (fileSize < 1)
152
0
    return FALSE;
153
154
0
  sam->context = nullptr;
155
0
  sam->buffer = (char*)calloc((size_t)fileSize + 2, 1);
156
157
0
  if (!sam->buffer)
158
0
    return FALSE;
159
160
0
  readSize = fread(sam->buffer, (size_t)fileSize, 1, sam->fp);
161
162
0
  if (!readSize)
163
0
  {
164
0
    if (!ferror(sam->fp))
165
0
      readSize = (size_t)fileSize;
166
0
  }
167
168
0
  if (readSize < 1)
169
0
  {
170
0
    free(sam->buffer);
171
0
    sam->buffer = nullptr;
172
0
    return FALSE;
173
0
  }
174
175
0
  sam->buffer[fileSize] = '\n';
176
0
  sam->buffer[fileSize + 1] = '\0';
177
0
  sam->line = strtok_s(sam->buffer, "\n", &sam->context);
178
0
  return TRUE;
179
0
}
180
181
static void SamLookupFinish(WINPR_SAM* sam)
182
0
{
183
0
  free(sam->buffer);
184
0
  sam->buffer = nullptr;
185
0
  sam->line = nullptr;
186
0
}
187
188
static BOOL SamReadEntry(WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
189
0
{
190
0
  char* p[5] = WINPR_C_ARRAY_INIT;
191
0
  size_t count = 0;
192
193
0
  if (!sam || !entry || !sam->line)
194
0
    return FALSE;
195
196
0
  char* cur = sam->line;
197
198
0
  while ((cur = strchr(cur, ':')) != nullptr)
199
0
  {
200
0
    count++;
201
0
    cur++;
202
0
  }
203
204
0
  if (count < 4)
205
0
    return FALSE;
206
207
0
  p[0] = sam->line;
208
0
  p[1] = strchr(p[0], ':') + 1;
209
0
  p[2] = strchr(p[1], ':') + 1;
210
0
  p[3] = strchr(p[2], ':') + 1;
211
0
  p[4] = strchr(p[3], ':') + 1;
212
0
  const size_t LmHashLength = WINPR_ASSERTING_INT_CAST(size_t, (p[3] - p[2] - 1));
213
0
  const size_t NtHashLength = WINPR_ASSERTING_INT_CAST(size_t, (p[4] - p[3] - 1));
214
215
0
  if ((LmHashLength != 0) && (LmHashLength != 32))
216
0
    return FALSE;
217
218
0
  if ((NtHashLength != 0) && (NtHashLength != 32))
219
0
    return FALSE;
220
221
0
  entry->UserLength = (UINT32)(p[1] - p[0] - 1);
222
0
  entry->User = (LPSTR)malloc(entry->UserLength + 1);
223
224
0
  if (!entry->User)
225
0
    return FALSE;
226
227
0
  entry->User[entry->UserLength] = '\0';
228
0
  entry->DomainLength = (UINT32)(p[2] - p[1] - 1);
229
0
  memcpy(entry->User, p[0], entry->UserLength);
230
231
0
  if (entry->DomainLength > 0)
232
0
  {
233
0
    entry->Domain = (LPSTR)malloc(entry->DomainLength + 1);
234
235
0
    if (!entry->Domain)
236
0
    {
237
0
      free(entry->User);
238
0
      entry->User = nullptr;
239
0
      return FALSE;
240
0
    }
241
242
0
    memcpy(entry->Domain, p[1], entry->DomainLength);
243
0
    entry->Domain[entry->DomainLength] = '\0';
244
0
  }
245
0
  else
246
0
    entry->Domain = nullptr;
247
248
0
  if (LmHashLength == 32)
249
0
  {
250
0
    const size_t rc =
251
0
        winpr_HexStringToBinBuffer(p[2], LmHashLength, entry->LmHash, sizeof(entry->LmHash));
252
0
    if (rc != 16)
253
0
      return FALSE;
254
0
  }
255
256
0
  if (NtHashLength == 32)
257
0
  {
258
0
    const size_t rc = winpr_HexStringToBinBuffer(p[3], NtHashLength, (BYTE*)entry->NtHash,
259
0
                                                 sizeof(entry->NtHash));
260
0
    if (rc != 16)
261
0
      return FALSE;
262
0
  }
263
264
0
  return TRUE;
265
0
}
266
267
void SamFreeEntry(WINPR_ATTR_UNUSED WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
268
0
{
269
0
  SamResetEntry(entry);
270
0
  free(entry);
271
0
}
272
273
void SamResetEntry(WINPR_SAM_ENTRY* entry)
274
0
{
275
0
  if (!entry)
276
0
    return;
277
278
0
  if (entry->UserLength)
279
0
  {
280
0
    free(entry->User);
281
0
    entry->User = nullptr;
282
0
  }
283
284
0
  if (entry->DomainLength)
285
0
  {
286
0
    free(entry->Domain);
287
0
    entry->Domain = nullptr;
288
0
  }
289
290
0
  ZeroMemory(entry->LmHash, sizeof(entry->LmHash));
291
0
  ZeroMemory(entry->NtHash, sizeof(entry->NtHash));
292
0
}
293
294
WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPCSTR User, UINT32 UserLength, LPCSTR Domain,
295
                                UINT32 DomainLength)
296
0
{
297
0
  size_t length = 0;
298
0
  BOOL found = FALSE;
299
0
  WINPR_SAM_ENTRY* search = SamEntryFromDataA(User, UserLength, Domain, DomainLength);
300
0
  WINPR_SAM_ENTRY* entry = (WINPR_SAM_ENTRY*)calloc(1, sizeof(WINPR_SAM_ENTRY));
301
302
0
  if (!entry || !search)
303
0
    goto fail;
304
305
0
  if (!SamLookupStart(sam))
306
0
    goto fail;
307
308
0
  while (sam->line != nullptr)
309
0
  {
310
0
    length = strlen(sam->line);
311
312
0
    if (length > 1)
313
0
    {
314
0
      if (sam->line[0] != '#')
315
0
      {
316
0
        if (!SamReadEntry(sam, entry))
317
0
        {
318
0
          goto out_fail;
319
0
        }
320
321
0
        if (SamAreEntriesEqual(entry, search))
322
0
        {
323
0
          found = 1;
324
0
          break;
325
0
        }
326
0
      }
327
0
    }
328
329
0
    SamResetEntry(entry);
330
0
    sam->line = strtok_s(nullptr, "\n", &sam->context);
331
0
  }
332
333
0
out_fail:
334
0
  SamLookupFinish(sam);
335
0
fail:
336
0
  SamFreeEntry(sam, search);
337
338
0
  if (!found)
339
0
  {
340
0
    SamFreeEntry(sam, entry);
341
0
    return nullptr;
342
0
  }
343
344
0
  return entry;
345
0
}
346
347
WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPCWSTR User, UINT32 UserLength, LPCWSTR Domain,
348
                                UINT32 DomainLength)
349
0
{
350
0
  WINPR_SAM_ENTRY* entry = nullptr;
351
0
  char* utfUser = nullptr;
352
0
  char* utfDomain = nullptr;
353
0
  size_t userCharLen = 0;
354
0
  size_t domainCharLen = 0;
355
356
0
  utfUser = ConvertWCharNToUtf8Alloc(User, UserLength / sizeof(WCHAR), &userCharLen);
357
0
  if (!utfUser)
358
0
    goto fail;
359
0
  if (DomainLength > 0)
360
0
  {
361
0
    utfDomain = ConvertWCharNToUtf8Alloc(Domain, DomainLength / sizeof(WCHAR), &domainCharLen);
362
0
    if (!utfDomain)
363
0
      goto fail;
364
0
  }
365
0
  entry = SamLookupUserA(sam, utfUser, (UINT32)userCharLen, utfDomain, (UINT32)domainCharLen);
366
0
  if (entry)
367
0
  {
368
0
    free(entry->User);
369
0
    free(entry->Domain);
370
0
    entry->User = nullptr;
371
0
    entry->Domain = nullptr;
372
0
    if (User)
373
0
      entry->User = (char*)winpr_wcsndup(User, UserLength / sizeof(WCHAR));
374
0
    entry->UserLength = UserLength;
375
0
    if (Domain)
376
0
      entry->Domain = (char*)winpr_wcsndup(Domain, DomainLength / sizeof(WCHAR));
377
0
    entry->DomainLength = DomainLength;
378
0
  }
379
0
fail:
380
0
  free(utfUser);
381
0
  free(utfDomain);
382
0
  return entry;
383
0
}
384
385
void SamClose(WINPR_SAM* sam)
386
0
{
387
0
  if (sam != nullptr)
388
0
  {
389
0
    if (sam->fp)
390
0
      (void)fclose(sam->fp);
391
0
    free(sam);
392
0
  }
393
0
}