Skip to content

Commit

Permalink
Setting primary GID for users to be created from config. (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: Romeo Dumitrescu <[email protected]>
  • Loading branch information
MrDonkey07 and winromulus authored Sep 30, 2023
1 parent 9fc3532 commit 0c9465b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/ES.SFTP/Security/UserManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ private async Task SyncUsersAndGroups()
await UserUtil.UserDelete(user, false);
}

//Create groups as specified by the GID value for each user
foreach (var user in config.Users)
{
if (user.GID.HasValue)
{
_logger.LogInformation("Processing GID for user '{user}'", user.Username);

var virtualGroup = $"sftp-gid-{user.GID.Value}";
if (!await GroupUtil.GroupExists(virtualGroup))
{
_logger.LogDebug("Creating group '{group}' with GID '{gid}'", virtualGroup, user.GID.Value);
await GroupUtil.GroupCreate(virtualGroup, true, user.GID.Value);
}
}
}

foreach (var user in config.Users)
{
Expand All @@ -79,7 +94,7 @@ private async Task SyncUsersAndGroups()
if (!await UserUtil.UserExists(user.Username))
{
_logger.LogDebug("Creating user '{user}'", user.Username);
await UserUtil.UserCreate(user.Username, true);
await UserUtil.UserCreate(user.Username, true, user.GID);
_logger.LogDebug("Adding user '{user}' to '{group}'", user.Username, SftpUserInventoryGroup);
await GroupUtil.GroupAddUser(SftpUserInventoryGroup, user.Username);
}
Expand All @@ -94,19 +109,6 @@ private async Task SyncUsersAndGroups()
await UserUtil.UserSetId(user.Username, user.UID.Value);
}

if (user.GID.HasValue)
{
var virtualGroup = $"sftp-gid-{user.GID.Value}";
if (!await GroupUtil.GroupExists(virtualGroup))
{
_logger.LogDebug("Creating group '{group}' with GID '{gid}'", virtualGroup, user.GID.Value);
await GroupUtil.GroupCreate(virtualGroup, true, user.GID.Value);
}

_logger.LogDebug("Adding user '{user}' to '{group}'", user.Username, virtualGroup);
await GroupUtil.GroupAddUser(virtualGroup, user.Username);
}

var homeDir = Directory.CreateDirectory(Path.Combine(HomeBasePath, user.Username));
await ProcessUtil.QuickRun("chown", $"root:root {homeDir.FullName}");
await ProcessUtil.QuickRun("chmod", $"711 {homeDir.FullName}");
Expand Down
4 changes: 2 additions & 2 deletions src/ES.SFTP/Security/UserUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public static async Task<bool> UserExists(string username)
return command.ExitCode == 0 && !string.IsNullOrWhiteSpace(command.Output);
}

public static async Task UserCreate(string username, bool noLoginShell = false)
public static async Task UserCreate(string username, bool noLoginShell = false, int? gid = null)
{
await ProcessUtil.QuickRun("useradd",
$"--comment {username} {(noLoginShell ? "-s /usr/sbin/nologin" : string.Empty)} {username}");
$"--comment {username} {(noLoginShell ? "-s /usr/sbin/nologin " : string.Empty)}{(gid.HasValue ? "-g " + gid.Value + " " : string.Empty)}{username}");
}

public static async Task UserDelete(string username, bool throwOnError = true)
Expand Down

0 comments on commit 0c9465b

Please sign in to comment.