| 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 |  | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 63 | # Set up the rootwrap sudoers | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 64 | local tempfile | 
|  | 65 | tempfile=$(mktemp) | 
| Davanum Srinivas | 8afbaa1 | 2015-05-13 20:53:08 -0400 | [diff] [blame] | 66 | # Specify rootwrap.conf as first parameter to rootwrap | 
|  | 67 | rootwrap_sudo_cmd="${rootwrap_bin} /etc/${project}/rootwrap.conf *" | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 68 | echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudo_cmd" >$tempfile | 
| Davanum Srinivas | 8afbaa1 | 2015-05-13 20:53:08 -0400 | [diff] [blame] | 69 | if [ -f ${bin_dir}/${project}-rootwrap-daemon ]; then | 
|  | 70 | # rootwrap daemon does not need any parameters | 
|  | 71 | rootwrap_sudo_cmd="${rootwrap_bin}-daemon /etc/${project}/rootwrap.conf" | 
|  | 72 | echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudo_cmd" >>$tempfile | 
|  | 73 | fi | 
| Dean Troyer | 32d6bc6 | 2015-03-29 14:16:44 -0500 | [diff] [blame] | 74 | chmod 0440 $tempfile | 
|  | 75 | sudo chown root:root $tempfile | 
|  | 76 | sudo mv $tempfile /etc/sudoers.d/${project}-rootwrap | 
|  | 77 |  | 
|  | 78 | # Add bin dir to sudo's secure_path because rootwrap is being called | 
|  | 79 | # without a path because BROKEN. | 
|  | 80 | add_sudo_secure_path $(dirname $rootwrap_bin) | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 |  | 
|  | 84 | # Restore xtrace | 
|  | 85 | $INC_ROOT_TRACE | 
|  | 86 |  | 
|  | 87 | # Local variables: | 
|  | 88 | # mode: shell-script | 
|  | 89 | # End: |