Skip to content

Commit 1986bc0

Browse files
author
Patrick Münch
committed
Merge pull request #111 from jmara/feature/sftp
Feature/sftp
2 parents 16abf35 + 5414fdf commit 1986bc0

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ This cookbook provides secure ssh-client and ssh-server configurations.
3939
* `['ssh']['use_pam']` - `false` to disable pam authentication
4040
* `['ssh']['print_motd']` - `false` to disable printing of the MOTD
4141
* `['ssh']['print_last_log']` - `false` to disable display of last login information
42-
* `default['ssh']['deny_users']` - `[]` to configure `DenyUsers`, if specified login is disallowed for user names that match one of the patterns.
43-
* `default['ssh']['allow_users']` - `[]` to configure `AllowUsers`, if specified, login is allowed only for user names that match one of the patterns.
44-
* `default['ssh']['deny_groups']` - `[]` to configure `DenyGroups`, if specified, login is disallowed for users whose primary group or supplementary group list matches one of the patterns.
45-
* `default['ssh']['allow_groups']` - `[]` to configure `AllowGroups`, if specified, login is allowed only for users whose primary group or supplementary group list matches one of the patterns.
46-
* `default['ssh']['use_dns']` - `nil` to configure if sshd should look up the remote host name and check that the resolved host name for the remote IP address maps back to the very same IP address.
42+
* `['ssh']['deny_users']` - `[]` to configure `DenyUsers`, if specified login is disallowed for user names that match one of the patterns.
43+
* `['ssh']['allow_users']` - `[]` to configure `AllowUsers`, if specified, login is allowed only for user names that match one of the patterns.
44+
* `['ssh']['deny_groups']` - `[]` to configure `DenyGroups`, if specified, login is disallowed for users whose primary group or supplementary group list matches one of the patterns.
45+
* `['ssh']['allow_groups']` - `[]` to configure `AllowGroups`, if specified, login is allowed only for users whose primary group or supplementary group list matches one of the patterns.
46+
* `['ssh']['use_dns']` - `nil` to configure if sshd should look up the remote host name and check that the resolved host name for the remote IP address maps back to the very same IP address.
47+
* `['ssh']['sftp']['enable']` - `false` to disable the SFTP feature of OpenSSHd. Set to `true` to enable SFTP.
48+
* `['ssh']['sftp']['group']` - `sftponly` to configure the `Match Group` option of SFTP to allow SFTP only for dedicated users
49+
* `['ssh']['sftp']['chroot']` - `/home/%u` to configure the directory where the SFTP user should be chrooted
4750

4851
## Data Bags
4952

@@ -97,6 +100,26 @@ Configure attributes:
97100

98101
**The default value for `listen_to` is `0.0.0.0`. It is highly recommended to change the value.**
99102

103+
## SFTP
104+
105+
To enable the SFTP configuration add one of the following recipes to the run_list:
106+
107+
"recipe[ssh-hardening]"
108+
or
109+
"recipe[ssh-hardening::server]"
110+
111+
Configure attributes:
112+
113+
"ssh" : {
114+
"sftp" : {
115+
"enable" : true,
116+
"chroot" : "/home/sftp/%u",
117+
"group" : "sftusers"
118+
}
119+
}
120+
121+
This will enable the SFTP Server and chroot every user in the `sftpusers` group to the `/home/sftp/%u` directory.
122+
100123
## Local Testing
101124

102125
For local testing you can use vagrant and Virtualbox of VMWare to run tests locally. You will have to install Virtualbox and Vagrant on your system. See [Vagrant Downloads](http://downloads.vagrantup.com/) for a vagrant package suitable for your system. For all our tests we use `test-kitchen`. If you are not familiar with `test-kitchen` please have a look at [their guide](http://kitchen.ci/docs/getting-started).
@@ -154,6 +177,12 @@ We have seen some issues in applications (based on python and ruby) that are due
154177

155178
If you find this isn't enough, feel free to activate the attributes `cbc_requires` for ciphers, `weak_hmac` for MACs and `weak_kex`for KEX in the namespaces `['ssh']['client']` or `['ssh']['server']` based on where you want to support them.
156179

180+
**Why can't I log to the SFTP server after I added a user to my SFTP group?**
181+
182+
This is a ChrootDirectory ownership problem. sshd will reject SFTP connections to accounts that are set to chroot into any directory that has ownership/permissions that sshd considers insecure. sshd's strict ownership/permissions requirements dictate that every directory in the chroot path must be owned by root and only writable by the owner. So, for example, if the chroot environment is /home must be owned by root.
183+
184+
See [https://wiki.archlinux.org/index.php/SFTP_chroot](https://wiki.archlinux.org/index.php/SFTP_chroot)
185+
157186
## Deprecation Notices
158187

159188
* `node['ssh']['cbc_required']` has been deprecated in favour of `node['ssh']['client']['cbc_required']` and `node['ssh']['server']['cbc_required']`.

attributes/default.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@
7676
default['ssh']['server']['password_authentication'] = false # sshd
7777
# http://undeadly.org/cgi?action=article&sid=20160114142733
7878
default['ssh']['client']['roaming'] = false
79+
80+
# Define SFTP options
81+
default['ssh']['sftp']['enable'] = false
82+
default['ssh']['sftp']['group'] = 'sftponly'
83+
default['ssh']['sftp']['chroot'] = '/home/%u'

spec/recipes/server_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,53 @@
504504
with_content(/UseDNS yes/)
505505
end
506506
end
507+
508+
context 'without attribute ["sftp"]["enable"]' do
509+
it 'leaves SFTP Subsystem commented' do
510+
expect(chef_run).to render_file('/etc/ssh/sshd_config').
511+
with_content(/^#Subsystem sftp/)
512+
end
513+
end
514+
515+
context 'with attribute ["sftp"]["enable"] set to true' do
516+
cached(:chef_run) do
517+
ChefSpec::ServerRunner.new do |node|
518+
node.set['ssh']['sftp']['enable'] = true
519+
end.converge(described_recipe)
520+
end
521+
522+
it 'sets SFTP Subsystem correctly' do
523+
expect(chef_run).to render_file('/etc/ssh/sshd_config').
524+
with_content(/^Subsystem sftp/)
525+
end
526+
end
527+
528+
context 'with attribute ["sftp"]["enable"] set to true and ["sftp"]["group"] set to "testgroup"' do
529+
cached(:chef_run) do
530+
ChefSpec::ServerRunner.new do |node|
531+
node.set['ssh']['sftp']['enable'] = true
532+
node.set['ssh']['sftp']['group'] = 'testgroup'
533+
end.converge(described_recipe)
534+
end
535+
536+
it 'sets the SFTP Group correctly' do
537+
expect(chef_run).to render_file('/etc/ssh/sshd_config').
538+
with_content(/^Match Group testgroup$/)
539+
end
540+
end
541+
542+
context 'with attribute ["sftp"]["enable"] set to true and ["sftp"]["chroot"] set to "/export/home/%u"' do
543+
cached(:chef_run) do
544+
ChefSpec::ServerRunner.new do |node|
545+
node.set['ssh']['sftp']['enable'] = true
546+
node.set['ssh']['sftp']['chroot'] = 'test_home_dir'
547+
end.converge(described_recipe)
548+
end
549+
550+
it 'sets the SFTP chroot correctly' do
551+
expect(chef_run).to render_file('/etc/ssh/sshd_config').
552+
with_content(/^ChrootDirectory test_home_dir$/)
553+
end
554+
end
555+
507556
end

templates/default/opensshd.conf.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ UseDNS <%= ((@node['ssh']['use_dns']) ? 'yes' : 'no' ) %>
189189
#ChrootDirectory none
190190
#ChrootDirectory /home/%u
191191

192+
<% if @node['ssh']['sftp']['enable'] %>
193+
# Configuration, in case SFTP is used
194+
## override default of no subsystems
195+
## Subsystem sftp /opt/app/openssh5/libexec/sftp-server
196+
Subsystem sftp internal-sftp -l VERBOSE
197+
198+
## These lines must appear at the *end* of sshd_config
199+
Match Group <%= @node['ssh']['sftp']['group'] %>
200+
ForceCommand internal-sftp -l VERBOSE
201+
ChrootDirectory <%= @node['ssh']['sftp']['chroot'] %>
202+
AllowTcpForwarding no
203+
AllowAgentForwarding no
204+
PasswordAuthentication no
205+
PermitRootLogin no
206+
X11Forwarding no
207+
<% else %>
192208
# Configuration, in case SFTP is used
193209
## override default of no subsystems
194210
## Subsystem sftp /opt/app/openssh5/libexec/sftp-server
@@ -203,3 +219,4 @@ UseDNS <%= ((@node['ssh']['use_dns']) ? 'yes' : 'no' ) %>
203219
#PasswordAuthentication no
204220
#PermitRootLogin no
205221
#X11Forwarding no
222+
<% end %>

0 commit comments

Comments
 (0)