Skip to content

Commit

Permalink
extconf.rb now knows how to find /usr/lib/modsecurity which is the de…
Browse files Browse the repository at this point in the history
…fault location
  • Loading branch information
djellemah committed Sep 27, 2018
1 parent 6defa33 commit cd3be4a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# local c extensions
lib/rodsec/*.so
lib/rodsec/modsec_lib.rb
78 changes: 77 additions & 1 deletion ext/msc_intervention/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
require 'mkmf'
pkg_config 'modsecurity'

MODSECURITY_LIB_NAME = 'modsecurity'
SO_FUNC = 'msc_init'

def so_name
@so_name ||= "lib#{MODSECURITY_LIB_NAME}.#{CONFIG['DLEXT']}"
end

def intervention_header_name
@intervention_header_name ||= 'modsecurity/intervention.h'
end

# raise an exception if one of the paths doesn't exist
def confirm_include_lib include_dir, lib_dir
File.realpath(File.join include_dir, intervention_header_name)
File.realpath(File.join lib_dir, so_name)
end

def create_modsec_lib_rb so_lib_path, dst_ruby_file: 'modsec_lib.rb'
puts "runtime loads #{so_lib_path}"

# create a direct path to the library, because it's often in a nonstandard location
ruby_lib_dir = File.join CONFIG['srcdir'], '..', '..', 'lib', 'rodsec'
File.open File.join(ruby_lib_dir,dst_ruby_file), 'w' do |io|
io.write <<~EOS
# Generated by extconf.rb
module Rodsec
MODSECURITY_SO_PATH = '#{so_lib_path}'
end
EOS
end
end

def find_header_and_library include_dir, lib_dir
ok = true
ok &&= find_header intervention_header_name, include_dir
ok &&= find_library MODSECURITY_LIB_NAME, SO_FUNC, lib_dir
ok or abort
end

STANDARD_DIRS = [
%w[/usr/local/include /usr/local/lib],
%w[/usr/include /usr/lib],
]

# get the user config, or failing that the default ModSecurity installation paths
custom_dirs = dir_config 'modsecurity', '/usr/local/modsecurity'

found_dirs = ([custom_dirs] + STANDARD_DIRS).find do |(include_dir, lib_dir)|
# try to find files from config
begin
confirm_include_lib include_dir, lib_dir
[include_dir, lib_dir]
rescue
puts "#{[include_dir, lib_dir]} not found. Continuing..."
end
end

unless found_dirs
puts 'Cannot locate libmodsecurity.so'
abort
end

so_lib_path =
if found_dirs == custom_dirs
# we have custom dirs, set a specific path for the dlload in Wrapper
File.join found_dirs.last, so_name
else
# standard dirs, so do not use a specific path in Wrapper
so_name
end

# create the file so Wrapper knows where to load the .so
create_modsec_lib_rb so_lib_path

# finally, use find_header and find_library to add the paths to the Makefile
find_header_and_library *found_dirs

# don't need piles of debug info. Or maybe we do. Dunno.
CONFIG['debugflags'] = ''
Expand Down
2 changes: 1 addition & 1 deletion lib/rodsec/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rodsec
VERSION = '0.0.1'
VERSION = '0.0.2'
end
4 changes: 3 additions & 1 deletion lib/rodsec/wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require 'fiddle'
require 'fiddle/import'

require_relative 'modsec_lib.rb'

module Rodsec
module Wrapper
extend Fiddle::Importer

dlext = RbConfig::CONFIG['DLEXT']
msc_intervention = dlopen File.join __dir__, "msc_intervention.#{dlext}"
dlload msc_intervention, "libmodsecurity.#{dlext}"
dlload msc_intervention, MODSECURITY_SO_PATH

###########################
# from modsecurity/modsecurity.h
Expand Down
2 changes: 1 addition & 1 deletion rodsec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
end

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec(?!/config)|features)/})
f.match(%r{^(test|spec(?!/config)|features|modsec_lib)/})
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
Expand Down

0 comments on commit cd3be4a

Please sign in to comment.