-
Notifications
You must be signed in to change notification settings - Fork 1
/
uRemotes.pas
397 lines (353 loc) · 9.41 KB
/
uRemotes.pas
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
unit uRemotes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, IniFiles, fgl, JwaWindows;
type
TRemoteList = class;
TRemoteStatus = (
rsNone, // all clear, no leftovers
rsDriveTaken, // drive letter is occupied and PID does not hint at corresponding process
rsDriveMissing, // drive letter is unoccupied but PID is valid
rsConnected // successfully connected, PID valid
);
TRemoteAuthMethod = (
amPassword,
amPubkey
);
{ TRemote }
TRemote = class
private
fStatus: TRemoteStatus;
public
// saved attribs
Name: String;
Drive: String;
Host: String;
Port: integer;
User: String;
Path: String;
Auth: TRemoteAuthMethod;
AuthSecKey: String;
AuthPassword: String;
Options: String;
AutoMount: Boolean;
public
// ephemeral
InfoStart: String;
public
PID: SizeUInt;
function Status: TRemoteStatus;
function GetConnectStr(IncludePort: boolean=false): String;
function GetDriveStr: String;
procedure UpdateStatus;
constructor Create;
procedure ReadSection(ini: TIniFile; section: String);
procedure WriteSection(ini: TIniFile; section: String);
procedure CopyFrom(Source: TRemote);
end;
{ TRemoteList }
TRemoteList = class(specialize TFPGObjectList<TRemote>)
private
public
constructor Create;
destructor Destroy; override;
public
procedure LoadFromIni(ini: TIniFile);
procedure SaveToIni(ini: TIniFile);
end;
const
StatusNames: Array[TRemoteStatus] of String = (
'Not mounted',
'Drive already used',
'Error: Drive missing',
'Connected'
);
const
STILL_ACTIVE = JwaWindows.STILL_ACTIVE;
function GetProcessInfo(PID: SizeUInt; out ProcName: String): Integer;
function KillProcess(PID:SizeUInt; Status:integer = 0): boolean;
function KillProcessTree(PID: SizeUInt; Status: Integer = 0): Boolean;
function FindSubprocess(PID: SizeUInt; ProcessName: String; out ChildPID: SizeUInt): boolean;
function GetFreeDriveLetters: string;
function DPProtect(Data: TBytes): String;
function DPUnprotect(Encoded: String): TBytes;
implementation
function GetProcessInfo(PID: SizeUInt; out ProcName: String): Integer;
var
hproc: HANDLE;
ec: DWORD;
buf: array[0..4096] of char;
begin
ec:= 0;
ProcName:= '';
hproc:= OpenProcess(PROCESS_QUERY_INFORMATION, false, PID);
if hproc = INVALID_HANDLE_VALUE then
Exit(INVALID_HANDLE_VALUE);
try
// still active?
if not GetExitCodeProcess(hproc, ec) then
exit(INVALID_HANDLE_VALUE);
Result:= ec;
if ec = STILL_ACTIVE then begin
GetProcessImageFileName(hproc, buf, Length(buf)-1);
ProcName:= StrPas(buf);
end;
finally
CloseHandle(hproc);
end;
end;
function KillProcess(PID:SizeUInt; Status:integer = 0): boolean;
var
hproc: HANDLE;
begin
Result:= False;
hproc:= OpenProcess(PROCESS_TERMINATE, false, PID);
if hproc = INVALID_HANDLE_VALUE then
Exit;
try
// Writeln('KillProcess ',PID);
Result:= TerminateProcess(hproc, Status);
finally
CloseHandle(hproc);
end;
end;
function KillProcessTree(PID: SizeUInt; Status: Integer = 0): Boolean;
var
hSnap: HANDLE;
pe: PROCESSENTRY32;
begin
Result:= False;
hSnap:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe:= Default(PROCESSENTRY32);
pe.dwSize:= SizeOf(pe);
if Process32First(hSnap, pe) then begin
repeat
if pe.th32ParentProcessID = PID then begin
// Writeln('KillProcessTree ',StrPas(pe.szExeFile),':',pe.th32ProcessID);
if not KillProcessTree(pe.th32ProcessID, Status) then
Exit(False);
end;
until not Process32Next(hSnap, pe);
Result:= KillProcess(PID, Status);
end;
end;
function FindSubprocess(PID: SizeUInt; ProcessName: String; out ChildPID: SizeUInt): boolean;
var
hSnap: HANDLE;
pe: PROCESSENTRY32;
begin
Result:= False;
hSnap:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe:= Default(PROCESSENTRY32);
pe.dwSize:= SizeOf(pe);
if Process32First(hSnap, pe) then begin
repeat
if (pe.th32ParentProcessID = PID) and (pe.szExeFile = ProcessName) then begin
ChildPID:= pe.th32ProcessID;
Exit(True);
end;
until not Process32Next(hSnap, pe);
end;
end;
function GetFreeDriveLetters: string;
var
d: byte;
bits: DWORD;
begin
bits:= GetLogicalDrives;
Result:= '';
for d:= ord('A') to ord('Z') do begin
if (bits and 1) = 0 then
Result += Chr(d);
bits:= bits shr 1;
end;
end;
function DPProtect(Data: TBytes): String;
var
blobin, blobout: DATA_BLOB;
L: DWORD;
begin
Result:= '';
blobin.cbData:= Length(Data);
blobin.pbData:= @Data[0];
if CryptProtectData(@blobin, nil, nil, nil, nil, CRYPTPROTECT_UI_FORBIDDEN, @blobout) then begin
L:= blobout.cbData * 4;
SetLength(Result, L);
if CryptBinaryToStringA(blobout.pbData, blobout.cbData, CRYPT_STRING_BASE64 or CRYPT_STRING_NOCR, @Result[1], L) then begin
SetLength(Result, L);
Result:= Result.Replace(#10, '');
end else
Result:= '';
LocalFree(HLOCAL(blobout.pbData));
end;
end;
function DPUnprotect(Encoded: String): TBytes;
var
buf: TBytes;
blobin, blobout: DATA_BLOB;
L: DWORD;
begin
Result:= nil;
buf:= nil;
L:= Length(Encoded);
SetLength(buf, L);
if CryptStringToBinaryA(@Encoded[1], Length(Encoded), CRYPT_STRING_BASE64, @buf[0], L, nil) then begin
blobin.cbData:= L;
blobin.pbData:= @buf[0];
if CryptUnprotectData(@blobin, nil, nil, nil, nil, CRYPTPROTECT_UI_FORBIDDEN, @blobout) then begin
SetLength(Result, blobout.cbData);
Move(blobout.pbData^, Result[0], Length(Result));
LocalFree(HLOCAL(blobout.pbData));
end;
end;
end;
{ TRemote }
function TRemote.GetConnectStr(IncludePort: boolean = false): String;
begin
Result:= User + '@' + Host;
if IncludePort and (Port <> 22) then
Result:= Result + '!' + Inttostr(Port);
if Path > '' then
Result:= Result + ':' + Path
else
Result:= Result + ':.';
end;
function TRemote.GetDriveStr: String;
begin
Result:= Format('%s (%s)', [Name, Drive])
end;
function TRemote.Status: TRemoteStatus;
begin
UpdateStatus;
Result:= fStatus;
end;
procedure TRemote.UpdateStatus;
var
drivepresent: boolean;
a: UINT;
pn: String;
begin
a:= GetDriveType(PChar(Drive + DirectorySeparator));
drivepresent:= a <> DRIVE_NO_ROOT_DIR;
if PID > 0 then begin
if GetProcessInfo(PID, pn) <> STILL_ACTIVE then
PID:= 0;
end;
if PID > 0 then begin
if drivepresent then begin
fStatus:= rsConnected;
end
else
fStatus:= rsDriveMissing;
end else begin
if drivepresent then
fStatus:= rsDriveTaken
else
fStatus:= rsNone;
end;
end;
constructor TRemote.Create;
begin
inherited;
Name:= '';
Drive:= 'Z:';
Host:= 'host';
Port:= 22;
User:= 'user';
Path:= '.';
PID:= 0;
Auth:= amPassword;
AuthPassword:= '';
AuthSecKey:= '';
Options:= '';
AutoMount:= false;
end;
procedure TRemote.ReadSection(ini: TIniFile; section: String);
begin
Name:= ini.ReadString(section, 'Name', '');
Drive:= ini.ReadString(section, 'Drive', '');
Host:= ini.ReadString(section, 'Host', '');
Port:= ini.ReadInteger(section, 'Port', 22);
User:= ini.ReadString(section, 'User', '');
Path:= ini.ReadString(section, 'Path', '.');
PID:= ini.ReadInteger(section, 'PID', 0);
case ini.ReadString(section, 'Auth', 'password').ToLower of
'password': Auth:= amPassword;
'pubkey': Auth:= amPubkey;
end;
AuthPassword:= TEncoding.Default.GetAnsiString(DPUnprotect(ini.ReadString(section, 'AuthPassword', '')));
AuthSecKey:= ini.ReadString(section, 'AuthSecKey', '');
Options:= ini.ReadString(section, 'Options', '');
AutoMount:= ini.ReadBool(section, 'AutoMount', false);
end;
procedure TRemote.WriteSection(ini: TIniFile; section: String);
begin
ini.WriteString(section, 'Name', Name);
ini.WriteString(section, 'Drive', Drive);
ini.WriteString(section, 'Host', Host);
ini.WriteInteger(section, 'Port', Port);
ini.WriteString(section, 'User', User);
ini.WriteString(section, 'Path', Path);
ini.WriteInteger(section, 'PID', PID);
case Auth of
amPassword: ini.WriteString(section, 'Auth', 'password');
amPubkey: ini.WriteString(section, 'Auth', 'pubkey');
end;
ini.WriteString(section, 'AuthPassword', DPProtect(TEncoding.Default.GetAnsiBytes(AuthPassword)));
ini.WriteString(section, 'AuthSecKey', AuthSecKey);
ini.WriteString(section, 'Options', Options);
ini.WriteBool(section, 'AutoMount', AutoMount);
end;
procedure TRemote.CopyFrom(Source: TRemote);
var
ini: TMemIniFile;
begin
ini:= TMemIniFile.Create('');
try
Source.WriteSection(ini, 'A');
ReadSection(ini, 'A');
// PID should not be copied
PID:= 0;
finally
FreeAndNil(ini);
end;
end;
{ TRemoteList }
constructor TRemoteList.Create;
begin
inherited Create(True);
end;
destructor TRemoteList.Destroy;
begin
inherited Destroy;
end;
procedure TRemoteList.LoadFromIni(ini: TIniFile);
var
i: Integer;
sec: String;
r: TRemote;
begin
Clear;
for i:= 0 to ini.ReadInteger('Config', 'Remotes', 0) - 1 do begin
sec:= Format('Remote%d',[i]);
r:= TRemote.Create;
r.ReadSection(ini, sec);
Add(r);
r.UpdateStatus;
end;
end;
procedure TRemoteList.SaveToIni(ini: TIniFile);
var
i: Integer;
sec: String;
r: TRemote;
begin
ini.WriteInteger('Config', 'Remotes', Count);
for i:= 0 to Count - 1 do begin
sec:= Format('Remote%d',[i]);
r:= Items[i];
r.WriteSection(ini, sec);
end;
end;
end.