-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathSSHService.cs
More file actions
253 lines (223 loc) · 9.22 KB
/
SSHService.cs
File metadata and controls
253 lines (223 loc) · 9.22 KB
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
using System.Diagnostics;
using ES.SFTP.Configuration.Elements;
using ES.SFTP.Interop;
using ES.SFTP.Messages.Configuration;
using ES.SFTP.Messages.Events;
using ES.SFTP.SSH.Configuration;
using MediatR;
namespace ES.SFTP.SSH;
public class SSHService : IHostedService, INotificationHandler<ConfigurationChanged>
{
private const string SshDirPath = "/etc/ssh";
private static readonly string KeysImportDirPath = Path.Combine(SshDirPath, "keys");
private static readonly string ConfigFilePath = Path.Combine(SshDirPath, "sshd_config");
private readonly ILogger<SSHService> _logger;
private readonly IMediator _mediator;
private bool _loggingIgnoreNoIdentificationString;
private Process _serverProcess;
private Action _serviceProcessExitAction;
public SSHService(ILogger<SSHService> logger, IMediator mediator)
{
_logger = logger;
_mediator = mediator;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogDebug("Starting");
await RestartService(true);
_logger.LogInformation("Started");
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogDebug("Stopping");
await StopOpenSSH();
_logger.LogInformation("Stopped");
}
public async Task Handle(ConfigurationChanged notification, CancellationToken cancellationToken)
{
await RestartService();
}
private async Task RestartService(bool forceStop = false)
{
await StopOpenSSH(forceStop);
await UpdateHostKeyFiles();
await UpdateConfiguration();
await StartOpenSSH();
}
private async Task UpdateConfiguration()
{
var sftpConfig = await _mediator.Send(new SftpConfigurationRequest());
_loggingIgnoreNoIdentificationString = sftpConfig.Global.Logging.IgnoreNoIdentificationString;
var sshdConfig = new SSHConfiguration
{
Ciphers = sftpConfig.Global.Ciphers,
HostKeyAlgorithms = sftpConfig.Global.HostKeyAlgorithms,
KexAlgorithms = sftpConfig.Global.KexAlgorithms,
MACs = sftpConfig.Global.MACs,
PKIandPassword = sftpConfig.Global.PKIandPassword
};
var exceptionalChrootUsers = sftpConfig.Users.Where(s => s.Chroot != null).ToList();
var exceptionalUmaskUsers = sftpConfig.Users.Where(s => !string.IsNullOrWhiteSpace(s.Umask)).ToList();
var standardDeclarations = new[]
{
"X11Forwarding no",
"AllowTcpForwarding no"
};
sshdConfig.AllowUsers.AddRange(sftpConfig.Users.Select(s =>
s.AllowedHosts.Any()
? $"{s.Username}@{string.Join(",", s.AllowedHosts)}"
: s.Username)
);
sshdConfig.MatchBlocks.AddRange(exceptionalUmaskUsers.Select(s => new MatchBlock
{
Criteria = MatchBlock.MatchCriteria.User,
Match = {s.Username},
Declarations = new List<string>(standardDeclarations)
{
$"ForceCommand internal-sftp -u {s.Umask}"
}
}));
sshdConfig.MatchBlocks.AddRange(exceptionalChrootUsers.Select(s => new MatchBlock
{
Criteria = MatchBlock.MatchCriteria.User,
Match = {s.Username},
Declarations = new List<string>(standardDeclarations)
{
$"ChrootDirectory {s.Chroot.Directory}",
!string.IsNullOrWhiteSpace(s.Chroot.StartPath)
? $"ForceCommand internal-sftp -d {s.Chroot.StartPath}"
: "ForceCommand internal-sftp"
}
}));
sshdConfig.MatchBlocks.Add(new MatchBlock
{
Criteria = MatchBlock.MatchCriteria.User,
Match = {"*"},
//Except = exceptionalChrootUsers.Select(s => s.Username).ToList(),
Declarations = new List<string>(standardDeclarations)
{
$"ChrootDirectory {sftpConfig.Global.Chroot.Directory}",
!string.IsNullOrWhiteSpace(sftpConfig.Global.Chroot.StartPath)
? $"ForceCommand internal-sftp -d {sftpConfig.Global.Chroot.StartPath}"
: "ForceCommand internal-sftp"
}
});
var resultingConfig = sshdConfig.ToString();
await File.WriteAllTextAsync(ConfigFilePath, resultingConfig);
}
private async Task UpdateHostKeyFiles()
{
var config = await _mediator.Send(new SftpConfigurationRequest());
_logger.LogDebug("Updating host key files");
Directory.CreateDirectory(KeysImportDirPath);
var hostKeys = new[]
{
new
{
Type = nameof(HostKeysDefinition.Ed25519),
KeygenArgs = "-t ed25519 -f {0} -N \"\"",
File = "ssh_host_ed25519_key"
},
new
{
Type = nameof(HostKeysDefinition.Rsa),
KeygenArgs = "-t rsa -b 4096 -f {0} -N \"\"",
File = "ssh_host_rsa_key"
}
};
foreach (var hostKeyType in hostKeys)
{
var filePath = Path.Combine(KeysImportDirPath, hostKeyType.File);
if (File.Exists(filePath)) continue;
var configValue = (string) config.Global.HostKeys.GetType().GetProperty(hostKeyType.Type)
?.GetValue(config.Global.HostKeys, null);
if (!string.IsNullOrWhiteSpace(configValue))
{
_logger.LogDebug("Writing host key file '{file}' from config", filePath);
await File.WriteAllTextAsync(filePath, configValue);
}
else
{
_logger.LogDebug("Generating host key file '{file}'", filePath);
var keygenArgs = string.Format(hostKeyType.KeygenArgs, filePath);
await ProcessUtil.QuickRun("ssh-keygen", keygenArgs);
}
}
foreach (var file in Directory.GetFiles(KeysImportDirPath))
{
var targetFile = Path.Combine(SshDirPath, Path.GetFileName(file));
_logger.LogDebug("Copying '{sourceFile}' to '{targetFile}'", file, targetFile);
File.Copy(file, targetFile, true);
await ProcessUtil.QuickRun("chown", $"root:root \"{targetFile}\"");
await ProcessUtil.QuickRun("chmod", $"700 \"{targetFile}\"");
}
}
private async Task StartOpenSSH()
{
_logger.LogInformation("Starting 'sshd' process");
_serviceProcessExitAction = () =>
{
_logger.LogWarning("'sshd' process has stopped. Restarting process.");
RestartService().Wait();
};
void ListenForExit()
{
//Use this approach since the Exited event does not trigger on process crash
Task.Run(() =>
{
_serverProcess.WaitForExit();
_serviceProcessExitAction?.Invoke();
});
}
_serverProcess = new Process
{
StartInfo =
{
FileName = "/usr/sbin/sshd",
Arguments = "-D -e",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
_serverProcess.OutputDataReceived -= OnSSHOutput;
_serverProcess.ErrorDataReceived -= OnSSHOutput;
_serverProcess.OutputDataReceived += OnSSHOutput;
_serverProcess.ErrorDataReceived += OnSSHOutput;
_serverProcess.Start();
ListenForExit();
_serverProcess.BeginOutputReadLine();
_serverProcess.BeginErrorReadLine();
await _mediator.Publish(new ServerStartupEvent());
}
private void OnSSHOutput(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data)) return;
if (_loggingIgnoreNoIdentificationString &&
e.Data.Trim().StartsWith("Did not receive identification string from")) return;
_logger.LogTrace($"sshd - {e.Data}");
}
private async Task StopOpenSSH(bool force = false)
{
if (_serverProcess != null)
{
_logger.LogDebug("Stopping 'sshd' process");
_serviceProcessExitAction = null;
_serverProcess.Kill(true);
_serverProcess.OutputDataReceived -= OnSSHOutput;
_serverProcess.ErrorDataReceived -= OnSSHOutput;
_logger.LogInformation("Stopped 'sshd' process");
_serverProcess.Dispose();
_serverProcess = null;
}
if (force)
{
var arguments = Debugger.IsAttached ? "-q sshd" : "-q -w sshd";
var command = await ProcessUtil.QuickRun("killall", arguments, false);
if (command.ExitCode != 0 && command.ExitCode != 1 && !string.IsNullOrWhiteSpace(command.Output))
throw new Exception(
$"Could not stop existing sshd processes.{Environment.NewLine}{command.Output}");
}
}
}