Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting primary GID for users to be created from config. #89

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading