- #!/usr/bin/ruby
- require 'etc'
- require 'net/smtp'
- class Checker
- @@apacheGroup = 'www-data'
- @@dirPermissions = '40775'
- @@safeVersions = ['3.8.1', '4.1.1', '4.0.6']
- @@from = 'Site Check <server@some_company.com>'
- @@to = 'Server dudes <server@some_company.com>'
- def getDocumentRoot(file)
- roots = []
- fh = File.new(file, "r")
- while (line = fh.gets)
- # skip empty lines
- next if line.strip.empty?
- line.strip!
- # skip comments
- next if line =~ /\#.*/
- if line =~ /DocumentRoot.*/
- roots.push(line.sub(/DocumentRoot\s+(.*)/, '\1'))
- end
- end
- fh.close
- # remove duplicates
- roots.uniq!
- roots
- end
- def getHardLinkedSites(sites)
- msg = ''
- sites.each {|x|
- srcDir = x + '/typo3_src'
- msg = msg + "- " + x + "\n" unless File.symlink?(srcDir)
- }
- if msg.empty?
- ''
- else
- msg = "The following installs contain hard linked TYPO3 sources:\n" + msg + "\n"
- end
- end
- def getLocallyLinkedSites(sites)
- msg = ''
- sites.each {|x|
- srcDir = x + '/typo3_src'
- dir = File.readlink(srcDir)
- if dir =~ /^[^\/].*/
- if dir =~ /^[^\.\.].*/
- msg = msg + x + " -> " + dir + "\n"
- end
- end
- }
- if msg.empty?
- ''
- else
- msg = "The following installs contain locally linked TYPO3 sources:\n" + msg + "\n"
- end
- end
- def getRoots(dir)
- roots = []
- handle = Dir.open(dir)
- handle.each {|x|
- # skip default site
- next if x =~ /000-default/
- if x != '.' and x != '..'
- roots.push(getDocumentRoot(dir + '/' + x))
- end
- }
- # remove duplicates
- roots.uniq!
- roots.flatten!
- roots.sort!
- roots
- end
- def getTYPO3Sites(dir)
- sites = []
- roots = []
- roots = getRoots(dir)
- roots.each {|x|
- srcDir = x + '/typo3_src'
- if File.exists?(srcDir)
- sites.push(x)
- end
- }
- sites
- end
- def getTYPO3Versions(sites)
- pre4 = msg = beta = rc = stable = unsafe = ''
- sites.each {|x|
- srcDir = x + '/typo3_src'
- version = File.readlink(srcDir)
- version.sub!(/.*typo3_src-([^\/]*).*/, '\1')
- case version
- when /.*beta.*/ then
- beta = beta + version + ' - ' + x + "\n"
- when /.*3\.8\.1.*/ then
- pre4 = pre4 + version + ' - ' + x + "\n"
- when /.*RC.*/ then
- rc = rc + version + ' - ' + x + "\n"
- else
- if not @@safeVersions.include?(version) then
- unsafe = unsafe + version + ' - ' + x + "\n"
- end
- end
- }
- if beta.empty?
- ''
- else
- beta = "The following installs run on beta sources:\n" + beta + "\n"
- end
- if pre4.empty?
- ''
- else
- pre4 = "The following installs run on pre 4 (3.8.1) sources:\n" + pre4 + "\n"
- end
- if rc.empty?
- ''
- else
- rc = "The following installs run on RC sources:\n" + rc + "\n"
- end
- if unsafe.empty?
- ''
- else
- unsafe = "The following installs run on unsafe sources:\n" + unsafe + "\n"
- end
- msg = beta + pre4 + rc + unsafe
- end
- def permissions(sites)
- dirs = ['uploads', 'typo3conf', 'typo3temp', 'fileadmin']
- permissionMsg = groupMsg = msg = ''
- map = ['---',
- '--x',
- '-w-',
- '-wx',
- 'r--',
- 'r-x',
- 'rw-',
- 'rwx']
- sites.each {|s|
- dirs.each {|d|
- path = s + '/' + d
- #uid = File.stat(path).uid
- gid = File.stat(path).gid
- #puts 'Owner name: ', Etc.getpwuid(uid).name
- group = Etc.getgrgid(gid).name
- permissions = File.stat(path).mode
- permissions = sprintf('%o', permissions)
- if @@dirPermissions != permissions then
- permissionMsg = permissionMsg + permissions + ' should be ' + @@dirPermissions + ' - ' + s + '/' + d + "\n"
- end
- if @@apacheGroup != group then
- groupMsg = groupMsg + 'owner is: ' + group + '- ' + s + '/' + d + "\n"
- end
- }
- }
- if groupMsg.empty?
- ''
- else
- groupMsg = "The following directories are not owned by #{@@apacheGroup}:\n" + groupMsg + "\n"
- end
- if permissionMsg.empty?
- ''
- else
- permissionMsg = "The following directories have incorrect permissions:\n" + permissionMsg + "\n"
- end
- msg = permissionMsg + groupMsg
- end
- def sendMail(subject, message)
- msg = "From: #{@@from}\n" +
- "To: #{@@to}\n" +
- "Subject: #{subject}\n" +
- "#{message}"
- Net::SMTP.start('localhost') do |smtp|
- smtp.send_message msg, @@from, @@to
- end
- end
- end
- check = Checker.new
- sites = check.getTYPO3Sites('/etc/apache2/sites-enabled')
- msg = ''
- msg = msg + check.getTYPO3Versions(sites)
- msg = msg + check.getHardLinkedSites(sites)
- msg = msg + check.getLocallyLinkedSites(sites)
- msg = msg + check.permissions(sites)
- if msg != ''
- check.sendMail('Site Check report', msg)
- end

