1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
4
using System . IO ;
@@ -51,6 +51,7 @@ public async Task<bool> Handle(PamEventRequest request, CancellationToken cancel
51
51
if ( ! string . Equals ( request . EventType , "open_session" , StringComparison . OrdinalIgnoreCase ) ) return true ;
52
52
_logger . LogInformation ( "Preparing session for user '{user}'" , request . Username ) ;
53
53
await PrepareUserForSftp ( request . Username ) ;
54
+ _logger . LogInformation ( "Session prepared for user '{user}'" , request . Username ) ;
54
55
return true ;
55
56
}
56
57
@@ -378,24 +379,30 @@ private async Task PrepareUserForSftp(string username)
378
379
379
380
try
380
381
{
381
- var firstParentInChroot = directoryInfo ;
382
- while ( ( firstParentInChroot . Parent ??
383
- throw new InvalidOperationException ( "Cannot find first parent in chroot" ) ) . FullName !=
384
- chrootDirectory . FullName )
382
+ if ( IsSubDirectory ( chrootDirectory , directoryInfo ) )
385
383
{
386
- firstParentInChroot = firstParentInChroot . Parent ;
384
+ var dir = directoryInfo ;
385
+ while ( dir . FullName != chrootDirectory . FullName )
386
+ {
387
+ await ProcessUtil . QuickRun ( "chown" , $ "{ username } :{ SftpUserInventoryGroup } { dir . FullName } ") ;
388
+ dir = dir . Parent ?? chrootDirectory ;
389
+ }
390
+ }
391
+ else
392
+ {
393
+ _logger . LogWarning (
394
+ "Directory '{dir}' is not withing chroot path '{chroot}'. Setting direct permissions." ,
395
+ directoryInfo . FullName , chrootDirectory . FullName ) ;
396
+
397
+ await ProcessUtil . QuickRun ( "chown" ,
398
+ $ "{ username } :{ SftpUserInventoryGroup } { directoryInfo . FullName } ") ;
387
399
}
388
- await ProcessUtil . QuickRun ( "chown" , $ "{ username } :{ SftpUserInventoryGroup } { firstParentInChroot . FullName } ") ;
389
400
}
390
- catch ( Exception exception )
401
+ catch ( Exception exception )
391
402
{
392
- _logger . LogWarning ( exception ,
393
- "Could not determine first parent of '{dir}' in chroot '{chroot}' or failed to set permissions" ,
394
- directoryInfo . FullName , chrootDirectory . FullName ) ;
395
-
396
- await ProcessUtil . QuickRun ( "chown" , $ "{ username } :{ SftpUserInventoryGroup } { directoryInfo . FullName } ") ;
403
+ _logger . LogWarning ( exception , "Exception occured while setting permissions for '{dir}' " ,
404
+ directoryInfo . FullName ) ;
397
405
}
398
-
399
406
}
400
407
401
408
var sshDir = Path . Combine ( homeDirPath , ".ssh" ) ;
@@ -412,6 +419,8 @@ private async Task PrepareUserForSftp(string username)
412
419
await ProcessUtil . QuickRun ( "chmod" , $ "600 { sshAuthKeysPath } ") ;
413
420
}
414
421
422
+
423
+
415
424
private async Task StartOpenSSH ( )
416
425
{
417
426
var command = await ProcessUtil . QuickRun ( "killall" , "-q -w sshd" , false ) ;
@@ -448,5 +457,13 @@ private void OnSSHOutput(object sender, DataReceivedEventArgs e)
448
457
e . Data . Trim ( ) . StartsWith ( "Did not receive identification string from" ) ) return ;
449
458
_logger . LogTrace ( $ "sshd - { e . Data } ") ;
450
459
}
460
+
461
+ private static bool IsSubDirectory ( DirectoryInfo parent , DirectoryInfo directory )
462
+ {
463
+ if ( parent == null ) return false ;
464
+ if ( directory . Parent == null ) return false ;
465
+ if ( directory . Parent . FullName == parent . FullName ) return true ;
466
+ return IsSubDirectory ( parent , directory . Parent ) ;
467
+ }
451
468
}
452
469
}
0 commit comments