-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfreeSSHdUser.cpp
483 lines (369 loc) · 9.67 KB
/
freeSSHdUser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// freeSSHdUser.cpp: implementation of the CfreeSSHdUser class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "freeSSHdUser.h"
#ifndef _ISFREEOPEN
#include "..\..\Component\misc.h"
#endif
#ifdef _ISFREEEXE
#define _USE_OPENSSL
#endif
#ifdef _USE_OPENSSL
#include "..\..\Component\OpenSSL\sha.h"
#else
#include "sha1.h"
#endif
#include <string>
#include <cstdio>
#define HANDLER _Module.freeSSHdHandler
#ifdef _ISFREEOPEN
/* begin */
DWORD GetTextualSid(PSID pSid, LPTSTR *szSid)
{
PSID_IDENTIFIER_AUTHORITY psia;
DWORD dwSidSize, dwSubAuthorities;
bool bRtnVal = true;
DWORD RetVal = 0;
DWORD dwCounter = 0;
_ASSERTE(pSid);
// Validate the binary SID
if(!IsValidSid(pSid)){
RetVal = ERROR_INVALID_PARAMETER;
return RetVal;
}
// Get the identifier authority value from the SID
psia = GetSidIdentifierAuthority(pSid);
// Get the number of subauthorities in the SID.
dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
// Compute the buffer length
// S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL
dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
*szSid = (LPTSTR)malloc(dwSidSize);
if (szSid)
{
memset(*szSid,0,dwSidSize);
}
else
{
RetVal = ERROR_NOT_ENOUGH_MEMORY;
goto exitfunc;
}
// Add 'S' prefix and revision number to the string
dwSidSize = _stprintf(*szSid, _T("S-%lu-"), SID_REVISION);
// Add SID identifier authority to the string.
if((psia->Value[0] != 0) || (psia->Value[1] != 0))
{
dwSidSize += _stprintf(*szSid + lstrlen(*szSid),
_T("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
(USHORT)psia->Value[0],
(USHORT)psia->Value[1],
(USHORT)psia->Value[2],
(USHORT)psia->Value[3],
(USHORT)psia->Value[4],
(USHORT)psia->Value[5]);
}
else
{
dwSidSize += _stprintf(*szSid + lstrlen(*szSid),
_T("%lu"),
(ULONG)(psia->Value[5]) +
(ULONG)(psia->Value[4] << 8) +
(ULONG)(psia->Value[3] << 16) +
(ULONG)(psia->Value[2] << 24) );
}
// Add SID subauthorities to the string
for( dwCounter = 0; dwCounter < dwSubAuthorities; dwCounter++)
{
dwSidSize += _stprintf(*szSid + dwSidSize, _T("-%lu"),
*GetSidSubAuthority(pSid, dwCounter));
}
exitfunc:
return RetVal;
}
UINT FetchSID(LPTSTR strText/*,LPTSTR systemName*/,LPTSTR *SID/*,LPTSTR *domainName*/)
{
BOOL RetBln = FALSE;
DWORD sidSize = 0;
DWORD refDomainSize = 0;
DWORD returnValue = 0;
SID_NAME_USE snu;
PSID mySid = NULL;
LPTSTR tempdomainName = NULL;
sidSize = 0;
refDomainSize = 255;
//This is done just to know the buffer size for SID as well as Domain name to malloc
returnValue = LookupAccountName (/*systemName*/ NULL, strText, mySid, &sidSize, tempdomainName, &refDomainSize, &snu);
returnValue = GetLastError();
if (returnValue != ERROR_INSUFFICIENT_BUFFER)
return returnValue;
if(sidSize){
mySid = (PSID) malloc (sidSize);
memset(mySid,0,sidSize);
}
else{
returnValue = ERROR_INVALID_PARAMETER;
goto exitfunc;
}
if(refDomainSize){
tempdomainName = (LPTSTR) malloc (refDomainSize * sizeof(TCHAR));
memset(tempdomainName,0,refDomainSize * sizeof(TCHAR));
}
//Now get the SID and the domain name
if (!LookupAccountName (/*systemName*/ NULL,
strText,
mySid,
&sidSize,
tempdomainName,
&refDomainSize,
&snu))
{
returnValue = GetLastError();
goto exitfunc;
}
/* *domainName = tempdomainName;*/
//convert the PSID structure as a plain string SID
free(tempdomainName);
returnValue = GetTextualSid(mySid,SID);
if(returnValue){
goto exitfunc;;
}
exitfunc:
if(mySid){
free(mySid);
mySid = NULL;
}
return returnValue;
}
/* end */
DWORD PathFromLogin(LPTSTR Login, LPTSTR szPath)
{
CRegKey rkReg;
DWORD dwRet = 0;
DWORD dwCount = _MAX_PATH;
char tmp[_MAX_PATH];
LPTSTR szSID;
char *sysdrive = NULL;
szSID = NULL;
dwRet = FetchSID(Login,&szSID);
if (dwRet == 0 )
{
sprintf(tmp,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\%s",szSID);
if (szSID)
free(szSID);
szSID = NULL;
if ( rkReg.Open(HKEY_LOCAL_MACHINE,tmp,KEY_READ) == ERROR_SUCCESS )
{
if (rkReg.QueryValue(tmp, "ProfileImagePath", &dwCount) == ERROR_SUCCESS)
{
sysdrive = getenv("SYSTEMDRIVE");
strcpy(&(tmp[3]),&(tmp[14]));
tmp[0]=sysdrive[0];
tmp[1]=':';
tmp[2]='\\';
strcpy(szPath,tmp);
rkReg.Close();
return 1;
}
else
{
rkReg.Close();
return NULL;
}
}
else return NULL;
}
else
{
if (szSID)
free(szSID);
return NULL;
}
}
#endif
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CfreeSSHdUser::CfreeSSHdUser()
{
memset(&m_Data, 0, sizeof(m_Data));
}
CfreeSSHdUser::~CfreeSSHdUser()
{
}
BOOL CfreeSSHdUser::ConsumeData(freeSSHdUserInfo Data)
{
m_Data = Data;
return TRUE;
}
freeSSHdUserInfo CfreeSSHdUser::RetrieveData()
{
return m_Data;
}
const char * CfreeSSHdUser::GetName() const
{
return m_Data.Name;
}
const char * CfreeSSHdUser::GetDomain() const
{
return m_Data.Domain;
}
char * CfreeSSHdUser::GetHomeDir(char *Dir)
{
char SFTPHomeDir[MAX_PATH+1];
char WinHomeDir[MAX_PATH+1];
strncpy(SFTPHomeDir, HANDLER.Config.SFTPHomePath, MAX_PATH);
if (!Dir)
return NULL;
if ( strstr(SFTPHomeDir,"$HOME") )
{
PathFromLogin((LPTSTR) GetName(),WinHomeDir);
strncat(WinHomeDir,SFTPHomeDir+5, MAX_PATH-6);
strncpy(Dir, WinHomeDir, MAX_PATH);
return Dir;
}
else
{
strncpy(Dir, SFTPHomeDir, MAX_PATH);
return Dir;
}
return NULL;
}
const BOOL CfreeSSHdUser::IsPasswordSet() const
{
if (GetAuthType() == 1)
{
BOOL fSet = FALSE;
for (int i = 0; i < 20; i++)
if (m_Data.Password[i] != 0)
fSet = TRUE;
return fSet;
}
return TRUE;
}
const int CfreeSSHdUser::GetAuthType() const
{
return m_Data.Auth;
}
const BOOL CfreeSSHdUser::HasShellAccess() const
{
return m_Data.Shell ? TRUE:FALSE;
}
const BOOL CfreeSSHdUser::HasSFTPAccess() const
{
return m_Data.SFTP ? TRUE:FALSE;
}
const BOOL CfreeSSHdUser::HasTunnelAccess() const
{
return m_Data.Tunnel ? TRUE:FALSE;
}
BOOL CfreeSSHdUser::AuthPassword(char *Password, UINT Length)
{
if (m_Data.Auth == 1)
{
unsigned char shabuff[20];
#ifdef _USE_OPENSSL
SHA1((const unsigned char *)Password, strlen(Password), shabuff);
#else
SHA1Context sha;
SHA1Reset(&sha);
SHA1Input(&sha, (const unsigned char *) Password, strlen(Password));
SHA1Result(&sha, shabuff);
#endif
if (memcmp(shabuff,m_Data.Password, sizeof(char)*20))
return FALSE;
else
return TRUE;
}
return FALSE;
}
BOOL CfreeSSHdUser::AuthPublicKey(char *PublicKey, UINT Length)
{
FILE *fKeyFile = NULL;
char cBuff[16384];
int fOK = FALSE;
char PKeyPath[MAX_PATH+1];
char UserHomeDir[MAX_PATH+1];
string sKeyFile;
strncpy(PKeyPath, HANDLER.Config.SSHPublickeyPath, MAX_PATH);
if ( strstr(PKeyPath,"$HOME") )
{
PathFromLogin((LPTSTR) GetName(),UserHomeDir);
strncat(UserHomeDir, PKeyPath+5, MAX_PATH-6);
strncpy(PKeyPath, UserHomeDir, MAX_PATH);
}
sKeyFile = PKeyPath;
if (sKeyFile[sKeyFile.length()-1] != '\\')
sKeyFile += "\\";
sKeyFile += GetName();
fKeyFile = fopen(sKeyFile.c_str(), "rb");
if (fKeyFile)
{
int i = 0;
while (!feof(fKeyFile) && !fOK)
{
memset(cBuff, 0, 16384);
fgets(cBuff, 16380, fKeyFile);
i = 0;
i += 8;
while (cBuff[i] && cBuff[i]!=' ')
i++;
if (!strncmp(cBuff, PublicKey, i))
fOK = TRUE;
}
fclose(fKeyFile);
}
return fOK;
}
BOOL CfreeSSHdUser::SetName(char *Name)
{
strncpy(m_Data.Name, Name, 256);
m_Data.Name[255] = 0;
return TRUE;
}
BOOL CfreeSSHdUser::SetPassword(char *Password)
{
if (GetAuthType() == 1)
{
unsigned char shahash[20];
#ifdef _USE_OPENSSL
SHA1((const unsigned char *)Password, strlen(Password), shahash);
#else
SHA1Context sha;
SHA1Reset(&sha);
SHA1Input(&sha, (const unsigned char *) Password, strlen(Password));
SHA1Result(&sha, shahash);
#endif
memcpy(m_Data.Password, shahash, sizeof(char)*20);
}
return FALSE;
}
BOOL CfreeSSHdUser::SetAuthType(int AuthType)
{
if ( (AuthType < 0) || (AuthType > 2))
return FALSE;
m_Data.Auth = AuthType;
return TRUE;
}
BOOL CfreeSSHdUser::SetDomain(char *Domain)
{
strncpy(m_Data.Domain, Domain, 256);
m_Data.Domain[255] = 0;
return TRUE;
}
BOOL CfreeSSHdUser::SetShellAccess(BOOL Access)
{
m_Data.Shell = Access;
return TRUE;
}
BOOL CfreeSSHdUser::SetSFTPAccess(BOOL Access)
{
m_Data.SFTP = Access;
return TRUE;
}
BOOL CfreeSSHdUser::SetTunnelAccess(BOOL Access)
{
m_Data.Tunnel = Access;
return TRUE;
}