Coverage Report

Created: 2026-05-30 06:41

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
    goto fail;
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
    goto fail;
217
218
0
  if ((NtHashLength != 0) && (NtHashLength != 32))
219
0
    goto fail;
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
    goto fail;
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
      goto fail;
237
238
0
    memcpy(entry->Domain, p[1], entry->DomainLength);
239
0
    entry->Domain[entry->DomainLength] = '\0';
240
0
  }
241
0
  else
242
0
    entry->Domain = nullptr;
243
244
0
  if (LmHashLength == 32)
245
0
  {
246
0
    const size_t rc =
247
0
        winpr_HexStringToBinBuffer(p[2], LmHashLength, entry->LmHash, sizeof(entry->LmHash));
248
0
    if (rc != 16)
249
0
      goto fail;
250
0
  }
251
252
0
  if (NtHashLength == 32)
253
0
  {
254
0
    const size_t rc = winpr_HexStringToBinBuffer(p[3], NtHashLength, (BYTE*)entry->NtHash,
255
0
                                                 sizeof(entry->NtHash));
256
0
    if (rc != 16)
257
0
      goto fail;
258
0
  }
259
260
0
  return TRUE;
261
262
0
fail:
263
0
  free(entry->Domain);
264
0
  free(entry->User);
265
0
  entry->Domain = nullptr;
266
0
  entry->User = nullptr;
267
0
  entry->DomainLength = 0;
268
0
  entry->UserLength = 0;
269
0
  return FALSE;
270
0
}
271
272
void SamFreeEntry(WINPR_ATTR_UNUSED WINPR_SAM* sam, WINPR_SAM_ENTRY* entry)
273
0
{
274
0
  SamResetEntry(entry);
275
0
  free(entry);
276
0
}
277
278
void SamResetEntry(WINPR_SAM_ENTRY* entry)
279
0
{
280
0
  if (!entry)
281
0
    return;
282
283
0
  if (entry->UserLength)
284
0
  {
285
0
    free(entry->User);
286
0
    entry->User = nullptr;
287
0
  }
288
289
0
  if (entry->DomainLength)
290
0
  {
291
0
    free(entry->Domain);
292
0
    entry->Domain = nullptr;
293
0
  }
294
295
0
  ZeroMemory(entry->LmHash, sizeof(entry->LmHash));
296
0
  ZeroMemory(entry->NtHash, sizeof(entry->NtHash));
297
0
}
298
299
WINPR_SAM_ENTRY* SamLookupUserA(WINPR_SAM* sam, LPCSTR User, UINT32 UserLength, LPCSTR Domain,
300
                                UINT32 DomainLength)
301
0
{
302
0
  size_t length = 0;
303
0
  BOOL found = FALSE;
304
0
  WINPR_SAM_ENTRY* search = SamEntryFromDataA(User, UserLength, Domain, DomainLength);
305
0
  WINPR_SAM_ENTRY* entry = (WINPR_SAM_ENTRY*)calloc(1, sizeof(WINPR_SAM_ENTRY));
306
307
0
  if (!entry || !search)
308
0
    goto fail;
309
310
0
  if (!SamLookupStart(sam))
311
0
    goto fail;
312
313
0
  while (sam->line != nullptr)
314
0
  {
315
0
    length = strlen(sam->line);
316
317
0
    if (length > 1)
318
0
    {
319
0
      if (sam->line[0] != '#')
320
0
      {
321
0
        if (!SamReadEntry(sam, entry))
322
0
        {
323
0
          goto out_fail;
324
0
        }
325
326
0
        if (SamAreEntriesEqual(entry, search))
327
0
        {
328
0
          found = 1;
329
0
          break;
330
0
        }
331
0
      }
332
0
    }
333
334
0
    SamResetEntry(entry);
335
0
    sam->line = strtok_s(nullptr, "\n", &sam->context);
336
0
  }
337
338
0
out_fail:
339
0
  SamLookupFinish(sam);
340
0
fail:
341
0
  SamFreeEntry(sam, search);
342
343
0
  if (!found)
344
0
  {
345
0
    SamFreeEntry(sam, entry);
346
0
    return nullptr;
347
0
  }
348
349
0
  return entry;
350
0
}
351
352
WINPR_SAM_ENTRY* SamLookupUserW(WINPR_SAM* sam, LPCWSTR User, UINT32 UserLength, LPCWSTR Domain,
353
                                UINT32 DomainLength)
354
0
{
355
0
  WINPR_SAM_ENTRY* entry = nullptr;
356
0
  char* utfUser = nullptr;
357
0
  char* utfDomain = nullptr;
358
0
  size_t userCharLen = 0;
359
0
  size_t domainCharLen = 0;
360
361
0
  utfUser = ConvertWCharNToUtf8Alloc(User, UserLength / sizeof(WCHAR), &userCharLen);
362
0
  if (!utfUser)
363
0
    goto fail;
364
0
  if (DomainLength > 0)
365
0
  {
366
0
    utfDomain = ConvertWCharNToUtf8Alloc(Domain, DomainLength / sizeof(WCHAR), &domainCharLen);
367
0
    if (!utfDomain)
368
0
      goto fail;
369
0
  }
370
0
  entry = SamLookupUserA(sam, utfUser, (UINT32)userCharLen, utfDomain, (UINT32)domainCharLen);
371
0
  if (entry)
372
0
  {
373
0
    free(entry->User);
374
0
    free(entry->Domain);
375
0
    entry->User = nullptr;
376
0
    entry->Domain = nullptr;
377
0
    if (User)
378
0
      entry->User = (char*)winpr_wcsndup(User, UserLength / sizeof(WCHAR));
379
0
    entry->UserLength = UserLength;
380
0
    if (Domain)
381
0
      entry->Domain = (char*)winpr_wcsndup(Domain, DomainLength / sizeof(WCHAR));
382
0
    entry->DomainLength = DomainLength;
383
0
  }
384
0
fail:
385
0
  free(utfUser);
386
0
  free(utfDomain);
387
0
  return entry;
388
0
}
389
390
void SamClose(WINPR_SAM* sam)
391
0
{
392
0
  if (sam != nullptr)
393
0
  {
394
0
    if (sam->fp)
395
0
      (void)fclose(sam->fp);
396
0
    free(sam);
397
0
  }
398
0
}