jumpserver/templates/jperm/role_sudo.j2

61 lines
1.8 KiB
Plaintext
Raw Normal View History

2015-12-19 09:27:12 +00:00
#!/bin/bash
real_file=/etc/sudoers
tmp_file=$(mktemp /tmp/XXXXXXX)
# fixed sudoers file path in bsd
isbsd=$(uname -a | grep -i 'freebsd' &> /dev/null && echo "yes" || echo "no")
if [ $isbsd == "yes" ]; then
real_file=/usr/local/etc/sudoers
fi
2015-12-19 09:27:12 +00:00
# Backup sudoers file
2015-12-19 16:30:31 +00:00
cp ${real_file} ${tmp_file}
2015-12-19 09:27:12 +00:00
# Add Command Aliases
add_cmd_alias() {
sudo_file=$1
{% for sudo_name, sudo_cmd in sudo_alias.items %}
{% if sudo_name != 'ALL' %}
2015-12-19 16:30:31 +00:00
if $(grep '^Cmnd_Alias \<{{ sudo_name }}\>' ${sudo_file} &> /dev/null); then
if [ $isbsd == "yes" ]; then
sed -i .bk 's@^Cmnd_Alias \<{{ sudo_name }}\>.*@Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}@g' ${sudo_file}
else
sed -i 's@^Cmnd_Alias \<{{ sudo_name }}\>.*@Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}@g' ${sudo_file}
fi
2015-12-19 09:27:12 +00:00
else
echo "Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}" >> ${sudo_file}
fi
{% endif %}
{% endfor %}
}
# Add Command Aliases to role
add_role_chosen() {
sudo_file=$1
{% for user, alias in sudo_user.items %}
2015-12-19 16:30:31 +00:00
if $(grep '^{{ user }}\>' ${sudo_file} &> /dev/null); then
if [ $isbsd == "yes" ]; then
sed -i .bk 's@^{{ user }}\>.*@{{ user }} ALL = (root) NOPASSWD: {{ alias }}@g' ${sudo_file}
else
sed -i 's@^{{ user }}\>.*@{{ user }} ALL = (root) NOPASSWD: {{ alias }}@g' ${sudo_file}
fi
2015-12-19 09:27:12 +00:00
else
echo "{{ user }} ALL = (root) NOPASSWD: {{ alias }}" >> ${sudo_file}
fi
{% endfor %}
}
check_syntax(){
/usr/sbin/visudo -c -f $1
2015-12-19 09:27:12 +00:00
}
cp $real_file $tmp_file && add_cmd_alias $tmp_file && add_role_chosen $tmp_file || exit 1
check_syntax $tmp_file && add_cmd_alias $real_file && add_role_chosen $real_file && rm -f $tmp_file || exit 2
check_syntax $real_file