diff --git a/README.md b/README.md index 78351c5..771e3a5 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,11 @@ OR set local storage: -h This help -c "" setup character mapping for file/directory names required arg: "" character mappings separated by ',' + -G "" Provide generic section option for smb.conf + required arg: "
" - IE: "share" + required arg: "" - IE: "log level = 2" -g "" Provide global option for smb.conf - required arg: "" - IE: -g "log level = 2" + required arg: "" - IE: "log level = 2" -i "" Import smbpassword required arg: "" - full file path in container -n Start the 'nmbd' daemon to advertise the shares @@ -74,6 +77,7 @@ OR set local storage: ENVIRONMENT VARIABLES * `CHARMAP` - As above, configure character mapping + * `GENERIC` - As above, configure a generic section option (See NOTE3 below) * `GLOBAL` - As above, configure a global option (See NOTE3 below) * `IMPORT` - As above, import a smbpassword file * `NMBD` - As above, enable nmbd diff --git a/samba.sh b/samba.sh index be8019c..e9c1027 100755 --- a/samba.sh +++ b/samba.sh @@ -33,15 +33,32 @@ charmap() { local chars="$1" file=/etc/samba/smb.conf sed -i '/catia:mappings/s| =.*| = '"$chars"'|' $file } +### generic: set a generic config option in a section +# Arguments: +# section) section of config file +# option) raw option +# Return: line added to smb.conf (replaces existing line with same key) +generic() { local section="$1" key="$(sed 's| *=.*||' <<< $2)" \ + value="$(sed 's|.*= *||' <<< $2)" file=/etc/samba/smb.conf + if sed -n '/^\['"$section"'\]/,/^\[/p' $file | grep -qE '^;*\s*'"$key"; then + sed -i '/^\['"$1"'\]/,/^\[/s|^;*\s*\('"$key"' = \).*| \1'"$value"'|' \ + "$file" + else + sed -i '/\['"$section"'\]/a \ '"$key = $value" "$file" + fi +} + ### global: set a global config option # Arguments: # option) raw option # Return: line added to smb.conf (replaces existing line with same key) -global() { local key="${1%%=*}" value="${1#*=}" file=/etc/samba/smb.conf - if grep -qE '^;*\s*'"$key" "$file"; then - sed -i 's|^;*\s*'"$key"'.*| '"${key% } = ${value# }"'|' "$file" +global() { local key="$(sed 's| *=.*||' <<< $1)" \ + value="$(sed 's|.*= *||' <<< $1)" file=/etc/samba/smb.conf + if sed -n '/^\[global\]/,/^\[/p' $file | grep -qE '^;*\s*'"$key"; then + sed -i '/^\[global\]/,/^\[/s|^;*\s*\('"$key"' = \).*| \1'"$value"'|' \ + "$file" else - sed -i '/\[global\]/a \ '"${key% } = ${value# }" "$file" + sed -i '/\[global\]/a \ '"$key = $value" "$file" fi } @@ -176,8 +193,11 @@ Options (fields in '[]' are optional, '<>' are required): -h This help -c \"\" setup character mapping for file/directory names required arg: \"\" character mappings separated by ',' + -G \"\" Provide generic section option for smb.conf + required arg: \"
\" - IE: \"share\" + required arg: \"\" - IE: \"log level = 2\" -g \"\" Provide global option for smb.conf - required arg: \"\" - IE: -g \"log level = 2\" + required arg: \"\" - IE: \"log level = 2\" -i \"\" Import smbpassword required arg: \"\" - full file path in container -n Start the 'nmbd' daemon to advertise the shares @@ -221,10 +241,11 @@ The 'command' (if provided and valid) will be run instead of samba [[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o smbuser [[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o smb -while getopts ":hc:g:i:nprs:Su:Ww:I:" opt; do +while getopts ":hc:G:g:i:nprs:Su:Ww:I:" opt; do case "$opt" in h) usage ;; c) charmap "$OPTARG" ;; + G) eval generic $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG) ;; g) global "$OPTARG" ;; i) import "$OPTARG" ;; n) NMBD="true" ;; @@ -243,6 +264,9 @@ done shift $(( OPTIND - 1 )) [[ "${CHARMAP:-""}" ]] && charmap "$CHARMAP" +while read i; do + eval generic $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $i) +done < <(env | awk '/^GENERIC[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}') while read i; do global "$i" done < <(env | awk '/^GLOBAL[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')