-
Notifications
You must be signed in to change notification settings - Fork 10
/
hda-add-apache-sudoers
executable file
·64 lines (52 loc) · 2.09 KB
/
hda-add-apache-sudoers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
#
# insert all the apache capabilities in sudoers
#
require 'augeas'
require 'fileutils'
def do_basic_checks
ruid = Process.uid
euid = Process.euid
if euid != 0
puts "ERROR: this program must be run as \"root\". type \"su\" (to Switch User to root),"
puts " then run it again.\n"
exit 1
end
end
def in_file?(file, string)
! File.open(file){|file| file.grep(/#{string}/) }.empty?
end
def do_sudoers
puts "Configuring sudoers file ... "
sudofile = '/etc/sudoers'
Augeas::open do |aug|
# Cmnd_Alias
unless in_file?(sudofile, "Cmnd_Alias HDAAPS")
aug.set("/files/etc/sudoers/Cmnd_Alias[last()1]/alias/name", "HDAAPPS")
aug.set("/files/etc/sudoers/Cmnd_Alias[last()]/alias/command[last()1]", "/var/hda/web-apps/*/elevated/")
aug.set("/files/etc/sudoers/Cmnd_Alias[last()]/alias/command[last()1]", "/var/hda/apps/*/elevated/")
end
# Defaults
unless in_file?(sudofile, "Defaults:apache !requiretty")
aug.set("/files/etc/sudoers/Defaults[last()1]/type", ":apache")
aug.set("/files/etc/sudoers/Defaults[last()]/requiretty/negate", "")
end
# Users
unless in_file?(sudofile, "NOPASSWD : HDAAPPS")
aug.set("/files/etc/sudoers/spec[last()1]/user", "apache")
aug.set("/files/etc/sudoers/spec[last()]/host_group/host", "ALL")
aug.set("/files/etc/sudoers/spec[last()]/host_group/command", "HDAAPPS")
aug.set("/files/etc/sudoers/spec[last()]/host_group/command/runas_user", "root")
aug.set("/files/etc/sudoers/spec[last()]/host_group/command/tag", "NOPASSWD")
end
unless aug.save
puts "Failed to save changes to /etc/sudoers!"
end
end
puts "Configuring sudoers file done."
end
def main
do_basic_checks
do_sudoers
end
main