Skip to content

Commit 2817fda

Browse files
committed
Improved directory permissions
1 parent 8c8d6c6 commit 2817fda

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

ES.SFTP.Host/Orchestrator.cs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
@@ -51,6 +51,7 @@ public async Task<bool> Handle(PamEventRequest request, CancellationToken cancel
5151
if (!string.Equals(request.EventType, "open_session", StringComparison.OrdinalIgnoreCase)) return true;
5252
_logger.LogInformation("Preparing session for user '{user}'", request.Username);
5353
await PrepareUserForSftp(request.Username);
54+
_logger.LogInformation("Session prepared for user '{user}'", request.Username);
5455
return true;
5556
}
5657

@@ -378,24 +379,30 @@ private async Task PrepareUserForSftp(string username)
378379

379380
try
380381
{
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))
385383
{
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}");
387399
}
388-
await ProcessUtil.QuickRun("chown", $"{username}:{SftpUserInventoryGroup} {firstParentInChroot.FullName}");
389400
}
390-
catch(Exception exception)
401+
catch (Exception exception)
391402
{
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);
397405
}
398-
399406
}
400407

401408
var sshDir = Path.Combine(homeDirPath, ".ssh");
@@ -412,6 +419,8 @@ private async Task PrepareUserForSftp(string username)
412419
await ProcessUtil.QuickRun("chmod", $"600 {sshAuthKeysPath}");
413420
}
414421

422+
423+
415424
private async Task StartOpenSSH()
416425
{
417426
var command = await ProcessUtil.QuickRun("killall", "-q -w sshd", false);
@@ -448,5 +457,13 @@ private void OnSSHOutput(object sender, DataReceivedEventArgs e)
448457
e.Data.Trim().StartsWith("Did not receive identification string from")) return;
449458
_logger.LogTrace($"sshd - {e.Data}");
450459
}
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+
}
451468
}
452469
}

0 commit comments

Comments
 (0)