class ZabbixVhost::Config

Attributes

file_path[RW]
server_alias[R]
server_name[R]

Public Class Methods

dir_reader(path) click to toggle source
# File lib/zabbix_vhost/config.rb, line 37
def self.dir_reader(path)
  configuration_files = Dir.glob("#{path}/*")

  configurations = []

  configuration_files.each do |f|

    if File.file?(f)
      cfg = self.new(f)
      configurations << cfg
    end

  end

  configurations
end
find_in_dir(path, domain) click to toggle source

Cerca il dominio all'interno della cartella

# File lib/zabbix_vhost/config.rb, line 56
def self.find_in_dir(path, domain)
  dir_reader(path).select {|c| c.server_name == domain}.first
end
new(f) click to toggle source
# File lib/zabbix_vhost/config.rb, line 8
def initialize(f)

  @file_path = f
  @server_alias = []
  @server_name = nil
  @ssl_active = nil
  parse

end

Public Instance Methods

parse() click to toggle source
# File lib/zabbix_vhost/config.rb, line 18
def parse
  @config_content = File.read(@file_path)
  ## Controlliamo se siamo in apache
  if_apache do
    @server_name = @config_content.match(/^\s*ServerName (?<server_name>.*)/)[:server_name]

    if @config_content.match(/^\s*ServerAlias/)
      @server_alias = @config_content.match(/^\s*ServerAlias (?<names>.*)/)[:names].split(" ")
    end

    @ssl_active = !@config_content.match(/443/).nil?
  end

  if @ssl_active
    @ssl_data = read_ssl_data
  end
end
ssl_active() click to toggle source
# File lib/zabbix_vhost/config.rb, line 63
def ssl_active
  @ssl_active ? 1 : 0
end
ssl_issuer() click to toggle source
# File lib/zabbix_vhost/config.rb, line 72
def ssl_issuer
  return nil unless @ssl_data
  @ssl_data[:issuer]
end
ssl_until_days() click to toggle source
# File lib/zabbix_vhost/config.rb, line 67
def ssl_until_days
  return 0 unless @ssl_data
  @ssl_data[:days_until]
end

Private Instance Methods

if_apache() { || ... } click to toggle source
# File lib/zabbix_vhost/config.rb, line 80
def if_apache
  if @config_content.match(/^\s*ServerName/)
    yield
  end
end
read_ssl_data() click to toggle source
# File lib/zabbix_vhost/config.rb, line 86
def read_ssl_data

  if @ssl_active
    begin
      require "socket"
      require "openssl"

      host = @server_name

      # Il codice commentato si occupa di verificare veramente il certificato. non ci interessa in questo momento
      # ssl_context = OpenSSL::SSL::SSLContext.new
      # ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
      #
      # cert_store = OpenSSL::X509::Store.new
      # cert_store.set_default_paths
      # ssl_context.cert_store = cert_store

      tcp_client = TCPSocket.new(host, 443)
      # ssl_client = OpenSSL::SSL::SSLSocket.new(tcp_client, ssl_context)
      ssl_client = OpenSSL::SSL::SSLSocket.new(tcp_client)


      ssl_client.hostname = host
      ssl_client.connect
      cert = OpenSSL::X509::Certificate.new(ssl_client.peer_cert)
      ssl_client.sysclose
      tcp_client.close

      certprops = OpenSSL::X509::Name.new(cert.issuer).to_a
      issuer = certprops.select {|name, data, type| name == "O"}.first[1]
      {
        valid_on: cert.not_before,
        valid_until: cert.not_after,
        days_until: (cert.not_after - Time.now).to_i / 86400,
        issuer: issuer
        # valid: (ssl_client.verify_result == 0)
      }
    rescue Exception => e
      puts "PROBLEMI ELABORAZIONE #{@server_name} SSL #{e.message} - "
    end
  end

end