|
| 1 | + |
| 2 | +var_selinux_state="enforcing" |
| 3 | + |
| 4 | +function replace_or_append { |
| 5 | + local config_file=$1 |
| 6 | + local key=$2 |
| 7 | + local value=$3 |
| 8 | + local cce=$4 |
| 9 | + local format=$5 |
| 10 | + |
| 11 | + # Check sanity of the input |
| 12 | + if [ $# -lt "3" ] |
| 13 | + then |
| 14 | + echo "Usage: replace_or_append 'config_file_location' 'key_to_search' 'new_value'" |
| 15 | + echo |
| 16 | + echo "If symlinks need to be taken into account, add yes/no to the last argument" |
| 17 | + echo "to allow to 'follow_symlinks'." |
| 18 | + echo "Aborting." |
| 19 | + exit 1 |
| 20 | + fi |
| 21 | + |
| 22 | + # Test if the config_file is a symbolic link. If so, use --follow-symlinks with sed. |
| 23 | + # Otherwise, regular sed command will do. |
| 24 | + if test -L $config_file; then |
| 25 | + sed_command="sed -i --follow-symlinks" |
| 26 | + else |
| 27 | + sed_command="sed -i" |
| 28 | + fi |
| 29 | + |
| 30 | + # Test that the cce arg is not empty or does not equal @CCENUM@. |
| 31 | + # If @CCENUM@ exists, it means that there is no CCE assigned. |
| 32 | + if ! [ "x$cce" = x ] && [ "$cce" != '@CCENUM@' ]; then |
| 33 | + cce="CCE-${cce}" |
| 34 | + else |
| 35 | + cce="CCE" |
| 36 | + fi |
| 37 | + |
| 38 | + # Strip any search characters in the key arg so that the key can be replaced without |
| 39 | + # adding any search characters to the config file. |
| 40 | + stripped_key=$(sed "s/[\^=\$,;+]*//g" <<< $key) |
| 41 | + |
| 42 | + # If there is no print format specified in the last arg, use the default format. |
| 43 | + if ! [ "x$format" = x ] ; then |
| 44 | + printf -v formatted_output "$format" "$stripped_key" "$value" |
| 45 | + else |
| 46 | + formatted_output="$stripped_key = $value" |
| 47 | + fi |
| 48 | + |
| 49 | + # If the key exists, change it. Otherwise, add it to the config_file. |
| 50 | + if `grep -qi $key $config_file` ; then |
| 51 | + $sed_command "s/$key.*/$formatted_output/g" $config_file |
| 52 | + else |
| 53 | + # \n is precaution for case where file ends without trailing newline |
| 54 | + echo -e "\n# Per $cce: Set $formatted_output in $config_file" >> $config_file |
| 55 | + echo -e "$formatted_output" >> $config_file |
| 56 | + fi |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +replace_or_append '/etc/sysconfig/selinux' '^SELINUX=' $var_selinux_state 'CCE-27334-2' '%s=%s' |
| 61 | + |
| 62 | +fixfiles onboot |
| 63 | +fixfiles -f relabel |
0 commit comments