| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
 | 3 | # **inc/rootwrap** - Rootwrap functions | 
 | 4 | # | 
 | 5 | # Handle rootwrap's foibles | 
 | 6 |  | 
 | 7 | # Uses: ``STACK_USER`` | 
 | 8 | # Defines: ``SUDO_SECURE_PATH_FILE`` | 
 | 9 |  | 
 | 10 | # Save trace setting | 
 | 11 | INC_ROOT_TRACE=$(set +o | grep xtrace) | 
 | 12 | set +o xtrace | 
 | 13 |  | 
 | 14 | # Accumulate all additions to sudo's ``secure_path`` in one file read last | 
 | 15 | # so they all work in a venv configuration | 
 | 16 | SUDO_SECURE_PATH_FILE=${SUDO_SECURE_PATH_FILE:-/etc/sudoers.d/zz-secure-path} | 
 | 17 |  | 
 | 18 | # Add a directory to the common sudo ``secure_path`` | 
 | 19 | # add_sudo_secure_path dir | 
 | 20 | function add_sudo_secure_path { | 
 | 21 |     local dir=$1 | 
 | 22 |     local line | 
 | 23 |  | 
 | 24 |     # This is pretty simplistic for now - assume only the first line is used | 
| Dmitry Guryanov | e0ac37c | 2015-11-30 18:48:23 +0300 | [diff] [blame] | 25 |     if [[ -r $SUDO_SECURE_PATH_FILE ]]; then | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 26 |         line=$(head -1 $SUDO_SECURE_PATH_FILE) | 
 | 27 |     else | 
 | 28 |         line="Defaults:$STACK_USER secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/usr/bin:/bin" | 
 | 29 |     fi | 
 | 30 |  | 
 | 31 |     # Only add ``dir`` if it is not already present | 
| Dmitry Guryanov | e0ac37c | 2015-11-30 18:48:23 +0300 | [diff] [blame] | 32 |     if [[ ! $line =~ $dir ]]; then | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 33 |         echo "${line}:$dir" | sudo tee $SUDO_SECURE_PATH_FILE | 
 | 34 |         sudo chmod 400 $SUDO_SECURE_PATH_FILE | 
 | 35 |         sudo chown root:root $SUDO_SECURE_PATH_FILE | 
 | 36 |     fi | 
 | 37 | } | 
 | 38 |  | 
 | 39 | # Configure rootwrap | 
 | 40 | # Make a load of assumptions otherwise we'll have 6 arguments | 
| Ian Wienand | c678241 | 2015-05-14 10:01:53 +1000 | [diff] [blame] | 41 | # configure_rootwrap project | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 42 | function configure_rootwrap { | 
| Ian Wienand | c678241 | 2015-05-14 10:01:53 +1000 | [diff] [blame] | 43 |     local project=$1 | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 44 |     local project_uc | 
 | 45 |     project_uc=$(echo $1|tr a-z A-Z) | 
| Ian Wienand | c678241 | 2015-05-14 10:01:53 +1000 | [diff] [blame] | 46 |     local bin_dir="${project_uc}_BIN_DIR" | 
 | 47 |     bin_dir="${!bin_dir}" | 
 | 48 |     local project_dir="${project_uc}_DIR" | 
 | 49 |     project_dir="${!project_dir}" | 
 | 50 |  | 
 | 51 |     local rootwrap_conf_src_dir="${project_dir}/etc/${project}" | 
 | 52 |     local rootwrap_bin="${bin_dir}/${project}-rootwrap" | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 53 |  | 
 | 54 |     # Start fresh with rootwrap filters | 
 | 55 |     sudo rm -rf /etc/${project}/rootwrap.d | 
 | 56 |     sudo install -d -o root -g root -m 755 /etc/${project}/rootwrap.d | 
 | 57 |     sudo install -o root -g root -m 644 $rootwrap_conf_src_dir/rootwrap.d/*.filters /etc/${project}/rootwrap.d | 
 | 58 |  | 
 | 59 |     # Set up rootwrap.conf, pointing to /etc/*/rootwrap.d | 
 | 60 |     sudo install -o root -g root -m 644 $rootwrap_conf_src_dir/rootwrap.conf /etc/${project}/rootwrap.conf | 
 | 61 |     sudo sed -e "s:^filters_path=.*$:filters_path=/etc/${project}/rootwrap.d:" -i /etc/${project}/rootwrap.conf | 
 | 62 |  | 
| Clark Boylan | a40f9cb | 2018-04-04 14:02:30 -0700 | [diff] [blame] | 63 |     # Rely on $PATH set by devstack to determine what is safe to execute | 
 | 64 |     # by rootwrap rather than use explicit whitelist of paths in | 
 | 65 |     # rootwrap.conf | 
 | 66 |     sudo sed -e 's/^exec_dirs=.*/#&/' -i /etc/${project}/rootwrap.conf | 
 | 67 |  | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 68 |     # Set up the rootwrap sudoers | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 69 |     local tempfile | 
 | 70 |     tempfile=$(mktemp) | 
| Davanum Srinivas | 8afbaa1 | 2015-05-13 20:53:08 -0400 | [diff] [blame] | 71 |     # Specify rootwrap.conf as first parameter to rootwrap | 
 | 72 |     rootwrap_sudo_cmd="${rootwrap_bin} /etc/${project}/rootwrap.conf *" | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 73 |     echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudo_cmd" >$tempfile | 
| Davanum Srinivas | 8afbaa1 | 2015-05-13 20:53:08 -0400 | [diff] [blame] | 74 |     if [ -f ${bin_dir}/${project}-rootwrap-daemon ]; then | 
 | 75 |         # rootwrap daemon does not need any parameters | 
 | 76 |         rootwrap_sudo_cmd="${rootwrap_bin}-daemon /etc/${project}/rootwrap.conf" | 
 | 77 |         echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudo_cmd" >>$tempfile | 
 | 78 |     fi | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 79 |     chmod 0440 $tempfile | 
 | 80 |     sudo chown root:root $tempfile | 
 | 81 |     sudo mv $tempfile /etc/sudoers.d/${project}-rootwrap | 
 | 82 |  | 
 | 83 |     # Add bin dir to sudo's secure_path because rootwrap is being called | 
 | 84 |     # without a path because BROKEN. | 
 | 85 |     add_sudo_secure_path $(dirname $rootwrap_bin) | 
 | 86 | } | 
 | 87 |  | 
 | 88 |  | 
 | 89 | # Restore xtrace | 
 | 90 | $INC_ROOT_TRACE | 
 | 91 |  | 
 | 92 | # Local variables: | 
 | 93 | # mode: shell-script | 
 | 94 | # End: |