Skip to content

Commit

Permalink
Support readonly vtysh for sudoers (sonic-net#7383)
Browse files Browse the repository at this point in the history
Why I did it
Support readonly version of the command vtysh

How I did it
Check if the command starting with "show", and verify only contains single command in script.
  • Loading branch information
xumia authored Apr 25, 2021
1 parent 1d81c38 commit 56bdd75
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dockers/docker-fpm-frr/base_image_files/rvtysh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# The command rvtysh can be run as root priviledge by any user without password, only allow to execute readonly commands.

# The options in the show command cannot contains any charactors to run multiple sub-commands potentially, such as "\n", "\r", "|", "&", "$" and ";".
if printf -- "$*" | grep -qPz '[\n\r|&$;]'; then
echo "Not allow to run the command, please use the comand 'sudo vtysh' instead." 1>&2
exit 1
fi

# The sub commands must start with "show"
LAST_PARA=
for para in "$@"
do
if [ "$LAST_PARA" == "-c" ] && [[ "$para" != show* ]]; then
echo "Not allow to run the command '$para', please use the comand 'sudo vtysh' instead." 1>&2
exit 1
fi
LAST_PARA=$para
done

vtysh "$@"
1 change: 1 addition & 0 deletions dockers/docker-fpm-quagga/base_image_files/rvtysh
1 change: 1 addition & 0 deletions files/image_config/sudoers/sudoers
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Cmnd_Alias READ_ONLY_CMDS = /bin/cat /var/log/syslog*, \
/usr/bin/lldpctl, \
/usr/bin/sensors, \
/usr/bin/tail -F /var/log/syslog, \
/usr/bin/rvtysh *, \
/usr/bin/vtysh -c show *, \
/usr/bin/vtysh -n [0-9] -c show *, \
/usr/local/bin/decode-syseeprom, \
Expand Down
1 change: 1 addition & 0 deletions rules/docker-fpm-frr.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $(DOCKER_FPM_FRR)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro
$(DOCKER_FPM_FRR)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)

$(DOCKER_FPM_FRR)_BASE_IMAGE_FILES += vtysh:/usr/bin/vtysh
$(DOCKER_FPM_FRR)_BASE_IMAGE_FILES += rvtysh:/usr/bin/rvtysh
$(DOCKER_FPM_FRR)_BASE_IMAGE_FILES += TSA:/usr/bin/TSA
$(DOCKER_FPM_FRR)_BASE_IMAGE_FILES += TSB:/usr/bin/TSB
$(DOCKER_FPM_FRR)_BASE_IMAGE_FILES += TSC:/usr/bin/TSC
Expand Down
1 change: 1 addition & 0 deletions rules/docker-fpm-quagga.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ $(DOCKER_FPM_QUAGGA)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro
$(DOCKER_FPM_QUAGGA)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)

$(DOCKER_FPM_QUAGGA)_BASE_IMAGE_FILES += vtysh:/usr/bin/vtysh
$(DOCKER_FPM_QUAGGA)_BASE_IMAGE_FILES += rvtysh:/usr/bin/rvtysh

0 comments on commit 56bdd75

Please sign in to comment.