summaryrefslogtreecommitdiff
path: root/inimodify.sh
diff options
context:
space:
mode:
Diffstat (limited to 'inimodify.sh')
-rwxr-xr-xinimodify.sh36
1 files changed, 36 insertions, 0 deletions
diff --git a/inimodify.sh b/inimodify.sh
new file mode 100755
index 0000000..939e47a
--- /dev/null
+++ b/inimodify.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+# Dynamically modifies openssl config file
+# ./inimodify.sh [header] [variable] [value] < openssl.cnf
+# ./inimodify.sh "usr_cert" "extendedKeyUsage" "clientAuth, serverAuth" < openssl.cnf
+#requires bash 4.0 regexes
+
+set -x
+set -e
+[ -n "$1" ] && HEADER="$1" || exit 2
+[ -n "$2" ] && VARIABLE="$2" || exit 3
+[ -n "$3" ] && VALUE="$3" || exit 4
+
+IFS=$'\n'
+HEADER_STANZA=0
+VARIABLE_MODIFIED=0
+while read line; do
+ if [[ "$line" =~ ^\ *\[\ *"$HEADER"\ *\] ]]; then
+ HEADER_STANZA=1
+ echo "$line"
+ elif [ "$HEADER_STANZA" == 1 ] && [[ "$line" =~ ^\ *"$VARIABLE"\ *= ]]; then
+ echo -n "$line"
+ echo ", $VALUE"
+ VARIABLE_MODIFIED=1
+ elif [ "$HEADER_STANZA" == 1 ] && [[ "$line" =~ ^\ *\[\ .+\ \] ]]; then
+ if [ "$VARIABLE_MODIFIED" == 0 ]; then
+ echo "$VARIABLE = $VALUE"
+ echo
+ VARIABLE_MODIFIED=1
+ fi
+ HEADER_STANZA=0
+ echo "$line"
+ else
+ echo "$line"
+ fi
+
+done