-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuBaseLibSsh2.pas
668 lines (580 loc) · 17.2 KB
/
uBaseLibSsh2.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
unit uBaseLibSsh2;
interface
uses
SysUtils, Classes, AnsiStrings, SyncObjs,
//
Winapi.Winsock2,
//
libssh2;
type
TBaseLibSsh2 = class;
ELibSsh2Error = class(Exception)
private
FNum: Integer;
public
constructor Create(const ANum: Integer; const ATest: string);
property Num: Integer read FNum;
end;
TSshTraceEvent = procedure(const ASender: TBaseLibSsh2; const ATest: AnsiString) of object;
TFingerprintState = (fsNew, fsChanged);
TConnectHashAction = (chaCancel, chaIgnore, chaSave);
TFingerprintEvent = procedure(ASender: TBaseLibSsh2; AHexHash: AnsiString; const AState: TFingerprintState;
var AAction: TConnectHashAction) of object;
TSshAuthType = (SSH_AUTH_NONE, SSH_AUTH_PASSWORD, SSH_AUTH_PUBLICKEY, SSH_AUTH_KEYBOARD_INTERACTIVE);
TSshAuthTypeSet = set of TSshAuthType;
TBaseLibSsh2 = class
private
FTraceEvent: TSshTraceEvent;
FTraceEnabled: Boolean;
FInteractivePassword: AnsiString;
function GetTimeout: Integer;
procedure SetTimeout(const Value: Integer);
procedure SetTraceEnabled(const Value: Boolean);
procedure SetTraceEvent(const Value: TSshTraceEvent);
procedure SetTrace;
protected
FRaiseError: Boolean;
FSocket: TSocket;
FSession: PLIBSSH2_SESSION;
FHostKey: AnsiString;
FHostKeyType: Integer;
FHostKeyTypeStr: string;
FFingerprint: AnsiString;
FRemoteBanner: AnsiString;
FAuthTypeSet: TSshAuthTypeSet;
FAuthTypeStr: AnsiString;
FOnFingerprint: TFingerprintEvent;
function GetLibVer: AnsiString;
function DoHandshake: Boolean;
function GetHostkeyHash: AnsiString;
function GetSessionHostkey: Integer;
function GetRemoteBanner: AnsiString;
function GetUserAuthList(const AUsername: AnsiString): TSshAuthTypeSet;
procedure RaiseSshError_(const A: string);
procedure RaiseSshError(const A: AnsiString);
procedure DoOnFingerprint;
// ---
function ConnectAndAuth(const ASocket: TSocket; const AUsername, APassword: AnsiString): Boolean;
{$IFDEF UNICODE}overload; {$ENDIF}
{$IFDEF UNICODE}
function ConnectAndAuth(const ASocket: TSocket; const AUsername, APassword: string): Boolean; overload;
{$ENDIF}
public
constructor Create(const ABanner: AnsiString);
destructor Destroy; override;
function GetLastErrorNum: Integer;
function GetLastErrorStr: string;
function GetLastErrorStrA: AnsiString;
function GetFormatErrorStr: string;
// function GetSessionLastError: AnsiString;
function AuthPasswordSupport(const AUser: AnsiString): Boolean; overload;
function AuthPasswordSupport(const AUser: string): Boolean; overload;
function Auth(const AUsername, APassword: AnsiString; ATerminatedEvent: TEvent): Boolean; overload;
function Auth(const AUsername, APassword: string; ATerminatedEvent: TEvent): Boolean; overload;
function AuthKeyboardIntecactive(const AUsername, APassword: AnsiString; ATerminatedEvent: TEvent): Boolean;
function Connect(const ASocket: TSocket): Boolean;
function IsAuthenticated: Boolean;
function WaitSocket: Boolean;
function GetMethodTypeStr(const AType: Integer): AnsiString;
procedure SetDebug(const AEnabled: Boolean);
property Socket: TSocket read FSocket write FSocket;
property LibVersion: AnsiString read GetLibVer;
property Fingerprint: AnsiString read FFingerprint;
property HostKey: AnsiString read FHostKey;
property HostKeyType: Integer read FHostKeyType;
property HostKeyTypeStr: string read FHostKeyTypeStr;
property RemoteBanner: AnsiString read FRemoteBanner;
property AuthTypeSet: TSshAuthTypeSet read FAuthTypeSet;
property AuthTypeStr: AnsiString read FAuthTypeStr;
property RaiseError: Boolean read FRaiseError write FRaiseError;
property Timeout: Integer read GetTimeout write SetTimeout;
property TraceEvent: TSshTraceEvent read FTraceEvent write SetTraceEvent;
property TraceEnabled: Boolean read FTraceEnabled write SetTraceEnabled;
end;
function GetLibSsh2Ver: AnsiString;
implementation
uses
Windows;
function HostKeyTypeToStr(const AType: Integer): string;
begin
case AType of
LIBSSH2_HOSTKEY_TYPE_UNKNOWN:
Result := 'Unknow';
LIBSSH2_HOSTKEY_TYPE_RSA:
Result := 'RSA';
LIBSSH2_HOSTKEY_TYPE_DSS:
Result := 'DSS';
LIBSSH2_HOSTKEY_TYPE_ECDSA_256:
Result := 'ECDSA_256';
LIBSSH2_HOSTKEY_TYPE_ECDSA_384:
Result := 'ECDSA_384';
LIBSSH2_HOSTKEY_TYPE_ECDSA_521:
Result := 'ECDSA_521';
LIBSSH2_HOSTKEY_TYPE_ED25519:
Result := 'ED25519';
else
Result := '';
end;
end;
var
gLibVer: AnsiString;
function GetLibSsh2Ver: AnsiString;
begin
Result := gLibVer
end;
function sshAllocMem(count: UINT; _abstract: PPointer): Pointer; cdecl;
begin
// Result := GetMemory(count); FreeMemory
Result := AllocMem(count); // FreeMem
end;
procedure sshFreeMem(ptr: Pointer; _abstract: PPointer); cdecl;
begin
FreeMemory(ptr);
end;
function sshReallocMem(ptr: Pointer; count: UINT; _abstract: PPointer): Pointer; cdecl;
begin
Result := ReallocMemory(ptr, count);
end;
{ TBaseLibSsh2 }
constructor TBaseLibSsh2.Create(const ABanner: AnsiString);
var
ret: Integer;
begin
inherited Create;
// FSession := libssh2_session_init();
FSession := libssh2_session_init_ex(sshAllocMem, sshFreeMem, sshReallocMem, Self);
if FSession = nil then
RaiseSshError('libssh2_session_init ');
if ABanner <> '' then
begin
ret := libssh2_session_banner_set(FSession, PAnsiChar(ABanner));
if ret <> 0 then
RaiseSshError('libssh2_session_banner_set ');
end;
end;
destructor TBaseLibSsh2.Destroy;
var
ret: Integer;
begin
if FSession <> nil then
begin
// libssh2_session_disconnect(FSession, '');
ret := libssh2_session_free(FSession);
if ret <> 0 then
RaiseSshError('libssh2_session_free ');
end;
inherited;
end;
procedure TBaseLibSsh2.RaiseSshError_(const A: string);
begin
if FRaiseError then
begin
raise ELibSsh2Error.Create(GetLastErrorNum(), A + GetFormatErrorStr());
end;
end;
procedure TBaseLibSsh2.RaiseSshError(const A: AnsiString);
begin
if FRaiseError then
begin
RaiseSshError_(string(A));
end;
end;
function TBaseLibSsh2.DoHandshake: Boolean;
var
r: Integer;
begin
// ... start it up. This will trade welcome banners, exchange keys,
// and setup crypto, compression, and MAC layers
r := libssh2_session_handshake(FSession, FSocket);
if r <> 0 then
begin
RaiseSshError('libssh2_session_handshake ');
Result := False;
Exit;
end;
Result := True;
end;
procedure TBaseLibSsh2.DoOnFingerprint;
var
AAction: TConnectHashAction;
begin
if Assigned(FOnFingerprint) then
FOnFingerprint(Self, FFingerprint, fsNew, AAction)
end;
function TBaseLibSsh2.GetUserAuthList(const AUsername: AnsiString): TSshAuthTypeSet;
var
p: PAnsiChar;
begin
FAuthTypeStr := '';
FAuthTypeSet := [];
// * check what authentication methods are available */
p := libssh2_userauth_list(FSession, PAnsiChar(AUsername), Length(AUsername));
if p = nil then
RaiseSshError('libssh2_userauth_list ');
FAuthTypeStr := p;
FAuthTypeSet := [];
if p = nil then
begin
FAuthTypeStr := 'none';
FAuthTypeSet := [SSH_AUTH_NONE];
end
else
begin
if AnsiStrings.StrPos(p, 'password') <> nil then
FAuthTypeSet := FAuthTypeSet + [SSH_AUTH_PASSWORD];
if AnsiStrings.StrPos(p, 'publickey') <> nil then
FAuthTypeSet := FAuthTypeSet + [SSH_AUTH_PUBLICKEY];
if AnsiStrings.StrPos(p, 'keyboard-interactive') <> nil then
FAuthTypeSet := FAuthTypeSet + [SSH_AUTH_KEYBOARD_INTERACTIVE];
end;
Result := FAuthTypeSet;
end;
function TBaseLibSsh2.IsAuthenticated: Boolean;
begin
Result := libssh2_userauth_authenticated(FSession);
end;
function TBaseLibSsh2.GetLastErrorNum: Integer;
begin
Result := libssh2_session_last_errno(FSession)
end;
function TBaseLibSsh2.GetLastErrorStr: string;
begin
Result := string(GetLastErrorStrA()) // cast
end;
function TBaseLibSsh2.GetLastErrorStrA: AnsiString;
var
I: Integer;
p: PAnsiChar;
begin
I := 0;
p := nil;
if FSession <> nil then
begin
libssh2_session_last_error(FSession, p, I, 0);
end;
Result := AnsiString(p);
end;
function TBaseLibSsh2.GetFormatErrorStr: string;
begin
Result := Format('%d %s', [GetLastErrorNum(), GetLastErrorStr()])
end;
function TBaseLibSsh2.GetHostkeyHash: AnsiString;
const
HASH_ID = LIBSSH2_HOSTKEY_HASH_SHA1;
HASH_LEN = 20;
var
p: PAnsiChar;
l: Integer;
begin
FFingerprint := '';
{ At this point we havn't yet authenticated. The first thing to do
* is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the
* user, that's your call }
p := libssh2_hostkey_hash(FSession, HASH_ID);
if p = nil then
begin
RaiseSshError('libssh2_hostkey_hash ');
Exit;
end;
l := HASH_LEN;
SetLength(FFingerprint, l * 2);
BinToHex(p, PAnsiChar(FFingerprint), l);
DoOnFingerprint();
Result := FFingerprint;
end;
function TBaseLibSsh2.GetSessionHostkey: Integer;
var
p: PAnsiChar;
l: Cardinal;
t: Integer;
begin
FHostKey := '';
FHostKeyType := LIBSSH2_HOSTKEY_TYPE_UNKNOWN;
FHostKeyTypeStr := '';
// ---
p := libssh2_session_hostkey(FSession, l, t);
if p <> nil then
begin
SetString(FHostKey, p, l);
FHostKeyType := t;
FHostKeyTypeStr := HostKeyTypeToStr(t);
end;
Result := FHostKeyType;
end;
function TBaseLibSsh2.GetLibVer: AnsiString;
begin
Result := gLibVer
end;
function TBaseLibSsh2.GetMethodTypeStr(const AType: Integer): AnsiString;
var
p: PAnsiChar;
begin
p := libssh2_session_methods(FSession, AType);
Result := p;
end;
function TBaseLibSsh2.GetRemoteBanner: AnsiString;
var
p: PAnsiChar;
begin
p := libssh2_session_banner_get(FSession);
FRemoteBanner := AnsiString(p);
// FRemoteBanner := 'test' ;
Result := FRemoteBanner;
end;
function TBaseLibSsh2.GetTimeout: Integer;
begin
Result := libssh2_session_get_timeout(FSession)
end;
{
function libssh2_trace(session: PLIBSSH2_SESSION;
bitmask: Integer): Integer; cdecl;
const
LIBSSH2_TRACE_TRANS = (1 shl 1);
const
LIBSSH2_TRACE_KEX = (1 shl 2);
const
LIBSSH2_TRACE_AUTH = (1 shl 3);
const
LIBSSH2_TRACE_CONN = (1 shl 4);
const
LIBSSH2_TRACE_SCP = (1 shl 5);
const
LIBSSH2_TRACE_SFTP = (1shl 6);
const
LIBSSH2_TRACE_ERROR = (1 shl 7);
const
LIBSSH2_TRACE_PUBLICKEY = (1 shl 8);
const
LIBSSH2_TRACE_SOCKET = (1 shl 9);
}
procedure TBaseLibSsh2.SetDebug(const AEnabled: Boolean);
begin
end;
procedure TBaseLibSsh2.SetTimeout(const Value: Integer);
begin
libssh2_session_set_timeout(FSession, Value)
end;
procedure SshTraceHandler(session: PLIBSSH2_SESSION; P: Pointer;
const C: PAnsiChar; S: UINT); cdecl;
var z: AnsiString;
begin
if p = nil then
Exit;
if not (TObject(p) is TBaseLibSsh2) then
Exit;
if TBaseLibSsh2(p).FTraceEnabled then
if Assigned(TBaseLibSsh2(p).FTraceEvent) then
begin
SetString(z, C, S);
TBaseLibSsh2(p).FTraceEvent(TBaseLibSsh2(p), z)
end;
end;
procedure TBaseLibSsh2.SetTrace;
begin
if FTraceEnabled and Assigned(FTraceEvent) then
begin
libssh2_trace_sethandler(FSession, Self, SshTraceHandler);
libssh2_trace(FSession, $FFFF xor LIBSSH2_TRACE_SOCKET)
end
else
begin
libssh2_trace_sethandler(FSession, nil, nil);
libssh2_trace(FSession, 0)
end;
end;
procedure TBaseLibSsh2.SetTraceEnabled(const Value: Boolean);
begin
FTraceEnabled := Value;
SetTrace();
end;
procedure TBaseLibSsh2.SetTraceEvent(const Value: TSshTraceEvent);
begin
FTraceEvent := Value;
SetTrace();
end;
function TBaseLibSsh2.WaitSocket: Boolean;
var
Timeout: timeval;
rc: Integer;
fd: fd_set;
writefd: PFD_SET;
readfd: PFD_SET;
dir: Integer;
begin
writefd := nil;
readfd := nil;
Timeout.tv_sec := 10;
Timeout.tv_usec := 0;
FD_ZERO(fd);
_FD_SET(FSocket, fd);
/// * now make sure we wait in the correct direction */
dir := libssh2_session_block_directions(FSession);
if (dir and LIBSSH2_SESSION_BLOCK_INBOUND) <> 0 then
readfd := @fd;
if (dir and LIBSSH2_SESSION_BLOCK_OUTBOUND) <> 0 then
writefd := @fd;
rc := select(FSocket + 1, readfd, writefd, nil, @Timeout);
Result := (rc <> SOCKET_ERROR) and (rc <> 0); // !Error and !Timeout
end;
function sshStrDup(var A: AnsiString): PAnsiChar;
begin
Result := sshAllocMem(Length(A) + 1, nil);
AnsiStrings.StrCopy(Result, PAnsiChar(A));
end;
procedure kbd_callback(const name: PAnsiChar; name_len: Integer; const instruction: PAnsiChar; instruction_len: Integer;
num_prompts: Integer; const prompts: PLIBSSH2_USERAUTH_KBDINT_PROMPT; var responses: LIBSSH2_USERAUTH_KBDINT_RESPONSE;
abstract_: PPointer); cdecl;
var
ssh: TBaseLibSsh2;
// j: Integer;
// z: AnsiString;
begin
{
gApp.Log.Info('-------------------------------------------');
gApp.Log.Info('*kbd_callback*');
gApp.Log.Info('Name: ' + name);
gApp.Log.Info('instruction: ' + instruction);
for j := 0 to num_prompts - 1 do
begin
SetString(z, prompts[j].text, prompts[j].length);
gApp.Log.Info(prompts[j].echo.ToString + ' prompts[]: ' + string(z));
end;
gApp.Log.Info('-------------------------------------------');
}
if abstract_ = nil then
Exit;
if abstract_^ = nil then
Exit;
if not(TObject(abstract_^) is TBaseLibSsh2) then
Exit;
ssh := TBaseLibSsh2(abstract_^);
if (num_prompts > 0) then
begin
responses.text := sshStrDup(ssh.FInteractivePassword);
responses.Length := Length(ssh.FInteractivePassword);
end;
end;
function TBaseLibSsh2.AuthKeyboardIntecactive(const AUsername, APassword: AnsiString; ATerminatedEvent: TEvent)
: Boolean;
var
r: Integer;
begin
while True do
begin
FInteractivePassword := APassword;
r := libssh2_userauth_keyboard_interactive(FSession, PAnsiChar(AUsername), kbd_callback);
FInteractivePassword := '';
if r <> 0 then
begin
if r = LIBSSH2_ERROR_EAGAIN then
begin
if Assigned(ATerminatedEvent) and (ATerminatedEvent.WaitFor(0) = wrSignaled) then
begin
Result := False;
Exit;
end;
Sleep(0);
Continue;
end;
// ---
RaiseSshError('auth kb-interactive fail, ');
Result := False;
Exit;
end;
Break;
end;
Result := True;
end;
function TBaseLibSsh2.Auth(const AUsername, APassword: AnsiString; ATerminatedEvent: TEvent): Boolean;
var
r: Integer;
begin
while True do
begin
r := libssh2_userauth_password(FSession, PAnsiChar(AUsername), PAnsiChar(APassword));
if r <> 0 then
begin
if r = LIBSSH2_ERROR_EAGAIN then
begin
if Assigned(ATerminatedEvent) and (ATerminatedEvent.WaitFor(0) = wrSignaled) then
begin
Result := False;
Exit;
end;
Sleep(0);
Continue;
end;
// ---
RaiseSshError('auth fail, ');
Result := False;
Exit;
end;
Break;
end;
Result := True;
end;
function TBaseLibSsh2.Auth(const AUsername, APassword: string; ATerminatedEvent: TEvent): Boolean;
begin
Result := Auth(Utf8Encode(AUsername), Utf8Encode(APassword), ATerminatedEvent)
end;
function TBaseLibSsh2.AuthPasswordSupport(const AUser: string): Boolean;
begin
Result := AuthPasswordSupport(AnsiString(AUser))
end;
function TBaseLibSsh2.AuthPasswordSupport(const AUser: AnsiString): Boolean;
begin
if FAuthTypeStr = '' then
GetUserAuthList(AUser);
Result := (FAuthTypeSet = [SSH_AUTH_NONE]) or (SSH_AUTH_PASSWORD in FAuthTypeSet) or (SSH_AUTH_KEYBOARD_INTERACTIVE in FAuthTypeSet)
end;
function TBaseLibSsh2.Connect(const ASocket: TSocket): Boolean;
begin
Socket := ASocket;
if DoHandshake() then
begin
GetRemoteBanner();
GetHostkeyHash();
GetSessionHostkey();
Result := True;
Exit
end;
Result := False
end;
function TBaseLibSsh2.ConnectAndAuth(const ASocket: TSocket; const AUsername, APassword: AnsiString): Boolean;
begin
if Connect(ASocket) then
begin
Result := Auth(AUsername, APassword, nil);
Exit
end;
Result := False
end;
function TBaseLibSsh2.ConnectAndAuth(const ASocket: TSocket; const AUsername, APassword: string): Boolean;
begin
Result := ConnectAndAuth(ASocket, AnsiString(AUsername), AnsiString(APassword))
end;
procedure LibSshInit_;
var
r: Integer;
p: PAnsiChar;
begin
r := libssh2_init(0);
if r <> 0 then
raise ELibSsh2Error.CreateFmt('libssh2 initialization failed (%d)', [r]);
p := libssh2_version(LIBSSH2_VERSION_NUM);
if nil = p then
raise ELibSsh2Error.Create(0, 'libssh2.dll to old');
gLibVer := p;
end;
{ ELibSsh2Error }
constructor ELibSsh2Error.Create(const ANum: Integer; const ATest: string);
begin
FNum := ANum;
inherited Create(ATest);
end;
initialization
LibSshInit_();
finalization
libssh2_exit();
end.