From c5e79fc4273c748722c0fd10b723f44dcdc54490 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Thu, 6 Oct 2016 19:31:31 -0500 Subject: [PATCH] service object to write out config files --- _vsftpd/ftpd.passwd | 3 +++ _vsftpd/users/client-one | 1 + _vsftpd/users/client-three | 1 + _vsftpd/users/client-two | 1 + app/services/ftp_config.rb | 36 ++++++++++++++++++++++++++++++++ config/application.yml.sample | 3 +++ test/fixtures/accounts.yml | 12 +++++++++++ test/services/ftp_config_test.rb | 21 +++++++++++++++++++ 8 files changed, 78 insertions(+) create mode 100644 _vsftpd/ftpd.passwd create mode 100644 _vsftpd/users/client-one create mode 100644 _vsftpd/users/client-three create mode 100644 _vsftpd/users/client-two create mode 100644 app/services/ftp_config.rb create mode 100644 test/services/ftp_config_test.rb diff --git a/_vsftpd/ftpd.passwd b/_vsftpd/ftpd.passwd new file mode 100644 index 0000000..4957595 --- /dev/null +++ b/_vsftpd/ftpd.passwd @@ -0,0 +1,3 @@ +client-two:$1$Uoqdsd1f$A39luV6N91OtA/VvcdBfC0 +client-one:$1$cVA6ZMIU$K/ITsDMZWeEDFEvoWk0op. +client-three:$1$pGYnsuhu$3MPEsgikbEhX1mZQE/qDc/ diff --git a/_vsftpd/users/client-one b/_vsftpd/users/client-one new file mode 100644 index 0000000..72f531f --- /dev/null +++ b/_vsftpd/users/client-one @@ -0,0 +1 @@ +local_root=_vsftpd/root/client_one \ No newline at end of file diff --git a/_vsftpd/users/client-three b/_vsftpd/users/client-three new file mode 100644 index 0000000..6dcf1db --- /dev/null +++ b/_vsftpd/users/client-three @@ -0,0 +1 @@ +local_root=_vsftpd/root/client_three \ No newline at end of file diff --git a/_vsftpd/users/client-two b/_vsftpd/users/client-two new file mode 100644 index 0000000..bc2b378 --- /dev/null +++ b/_vsftpd/users/client-two @@ -0,0 +1 @@ +local_root=_vsftpd/root/client_two \ No newline at end of file diff --git a/app/services/ftp_config.rb b/app/services/ftp_config.rb new file mode 100644 index 0000000..dce8684 --- /dev/null +++ b/app/services/ftp_config.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true +class FtpConfig + def build_password_list accounts + File.open(password_file, "w+") do |file| + Array(accounts).each do |account| + file.write "#{account.username}:#{hash_password(account.password)}" + end + end + end + + def build_user_configs accounts + Array(accounts).each do |account| + File.open("#{config_path}#{account.username}", "w+") do |file| + file.write "local_root=#{ftp_root}#{account.home_folder}" + end + end + end + + private + + def password_file + AppConfig.htpasswd + end + + def config_path + AppConfig.ftpusers + end + + def ftp_root + AppConfig.ftproot + end + + def hash_password password + `openssl passwd -1 -noverify -quiet #{password}` + end +end diff --git a/config/application.yml.sample b/config/application.yml.sample index 8612fb5..400a78c 100644 --- a/config/application.yml.sample +++ b/config/application.yml.sample @@ -10,6 +10,9 @@ defaults: &defaults client_key: client-id secret_key: client-secret callback_url: local-callback + htpasswd: "/path/to/vsftpd/password.file" + ftpusers: "/path/to/vsftpd/users/configs" + ftproot: "/path/to/root/ftp/prefix/" development: <<: *defaults diff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.yml index ded4486..e3db3ba 100644 --- a/test/fixtures/accounts.yml +++ b/test/fixtures/accounts.yml @@ -5,3 +5,15 @@ account1: password: <%= CryptSerializer.dump('1q2w3e4r5t6y7u') %> home_folder: client_one contact_email: ftp-user@mailinator.com + +account2: + username: client-two + password: <%= CryptSerializer.dump('azsxdcfvgbhnjmk,l.;/') %> + home_folder: client_two + contact_email: ftp-user@mailinator.com + +account3: + username: client-three + password: <%= CryptSerializer.dump('p0o9i8u7y6t5r4e3w2q1') %> + home_folder: client_three + contact_email: ftp-user@mailinator.com diff --git a/test/services/ftp_config_test.rb b/test/services/ftp_config_test.rb new file mode 100644 index 0000000..6735eaf --- /dev/null +++ b/test/services/ftp_config_test.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true +require 'test_helper' + +class FtpConfigTest < ActiveSupport::TestCase + test 'should write new password file' do + config = FtpConfig.new + config.build_password_list Account.all + + assert_match "#{accounts(:account2).username}:", File.read(AppConfig.htpasswd) + end + + test 'should build user config files' do + config = FtpConfig.new + config.build_user_configs Account.all + + account = accounts(:account1) + fconfig = File.read("#{AppConfig.ftpusers}#{account.username}") + + assert_match account.home_folder, fconfig + end +end