如何正确检查将文件添加到 sudoers.d 中是否会与其他文件冲突?

如何正确检查将文件添加到 sudoers.d 中是否会与其他文件冲突?

我已经编写了一个 shell 脚本来写入 /etc/sudoers.d/aptget -file,但如果在 /etc/sudoers 或 /etc/sudoers.d/ 中的任何文件中已经定义了别名,它将破坏 sudo。我正在使用 visudo 编辑文件,但它没有捕获错误,因为它似乎只检查当前正在编辑的文件。

之前我手动将以下内容添加到 /etc/sudoers 中

Cmnd_Alias APTGETUPDATE = /usr/bin/apt-get update *
Cmnd_Alias APTGETINSTALL = /usr/bin/apt-get -y --force-yes install *

%aptgroup ALL = (root) NOPASSWD: APTGETUPDATE, APTGETINSTALL
Defaults!APTGETUPDATE !requiretty
Defaults!APTGETINSTALL !requiretty

一切都很好。制作一个脚本将 a 写入 /etc/sudoers.d/aptget 或手动添加上述内容可以通过 visudo 检查,但会导致 sudo 无法运行。

脚本.sh:

#!/bin/bash

updaterSudoers="$(cat <<EOF
Cmnd_Alias APTGETUPDATE = /usr/bin/apt-get update *
Cmnd_Alias APTGETINSTALL = /usr/bin/apt-get -y --force-yes install *

%aptgroup ALL = (root) NOPASSWD: APTGETUPDATE, APTGETINSTALL
Defaults!APTGETUPDATE !requiretty
Defaults!APTGETINSTALL !requiretty
EOF
)"

echo "$updaterSudoers" | (EDITOR="tee -a" visudo -f /etc/sudoers.d/aptget)

错误:

>>> /etc/sudoers: Alias `APTGETUPDATE' already defined near line 31 <<<
>>> /etc/sudoers: Alias `APTGETINSTALL' already defined near line 31 <<<
sudo: parse error in /etc/sudoers near line 31
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

sudoers 中从 31 行开始:

31  Cmnd_Alias APTGETUPDATE = /usr/bin/apt-get update *
32  Cmnd_Alias APTGETINSTALL = /usr/bin/apt-get -y --force-yes install *
33  
34  %aptgroup ALL = (root) NOPASSWD: APTGETUPDATE, APTGETINSTALL
35  Defaults!APTGETUPDATE !requiretty
36  Defaults!APTGETINSTALL !requiretty

答案1

暂时包含原始 sudoers 文件并清除旧文件似乎可以解决问题。

includeCurrent=$'#include /etc/sudoers\n'
echo "$includeCurrent$updaterSudoers" | visudo -c -q -f -

我最终得到了整个 sudoers.sh 。

#!/bin/bash

#Clear the current file if it exsists 
#Warning: this might break stuff if you edit current user visudo rights
if [[ -f "/etc/sudoers.d/aptget" ]]
then
  echo -n > "/etc/sudoers.d/aptget"
fi

#New settings to be added
updaterSudoers="$(cat <<EOF
Cmnd_Alias APTGETUPDATE = /usr/bin/apt-get update *
Cmnd_Alias APTGETINSTALL = /usr/bin/apt-get -y --force-yes install *

%aptgroup ALL = (root) NOPASSWD: APTGETUPDATE, APTGETINSTALL
Defaults!APTGETUPDATE !requiretty
Defaults!APTGETINSTALL !requiretty
EOF
)"

#Include current sudoers file
includeCurrent=$'#include /etc/sudoers\n'

#Validate new settings with current settings
echo "$includeCurrent$updaterSudoers" | visudo -c -q -f -
if [ "$?" -eq 0 ]
then
  #Write /etc/sudoers.d/aptget with updateSudoers value
  echo "$updaterSudoers" | (EDITOR="tee" visudo -f /etc/sudoers.d/aptget) &> /dev/null
else
  #Yell and exit in case of error
  echo "ERROR CHECKING SUDOERS FILE:"
  echo "$includeCurrent$updaterSudoers" | visudo -c -f -
  exit 1
fi

#If sudoers.d or sudoers.d/aptget is not included include only sudoers.d/aptget
grep -xq "#includedir /etc/sudoers\.d/\?" /etc/sudoers || grep -xq "#include /etc/sudoers\.d/aptget" /etc/sudoers
if [ "$?" -eq 1 ]
then
  echo "#include /etc/sudoers.d/aptget" | (EDITOR="tee -a" visudo -f /etc/sudoers) &> /dev/null
fi

相关内容