samba.sh / README.md add generic section setting editing, fix #311

This commit is contained in:
David Personette 2020-05-26 00:03:18 -04:00
parent 7767ceb85a
commit 83a0ccc9b9
2 changed files with 35 additions and 7 deletions

View File

@ -32,8 +32,11 @@ OR set local storage:
-h This help -h This help
-c "<from:to>" setup character mapping for file/directory names -c "<from:to>" setup character mapping for file/directory names
required arg: "<from:to>" character mappings separated by ',' required arg: "<from:to>" character mappings separated by ','
-G "<section;parameter>" Provide generic section option for smb.conf
required arg: "<section>" - IE: "share"
required arg: "<parameter>" - IE: "log level = 2"
-g "<parameter>" Provide global option for smb.conf -g "<parameter>" Provide global option for smb.conf
required arg: "<parameter>" - IE: -g "log level = 2" required arg: "<parameter>" - IE: "log level = 2"
-i "<path>" Import smbpassword -i "<path>" Import smbpassword
required arg: "<path>" - full file path in container required arg: "<path>" - full file path in container
-n Start the 'nmbd' daemon to advertise the shares -n Start the 'nmbd' daemon to advertise the shares
@ -74,6 +77,7 @@ OR set local storage:
ENVIRONMENT VARIABLES ENVIRONMENT VARIABLES
* `CHARMAP` - As above, configure character mapping * `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) * `GLOBAL` - As above, configure a global option (See NOTE3 below)
* `IMPORT` - As above, import a smbpassword file * `IMPORT` - As above, import a smbpassword file
* `NMBD` - As above, enable nmbd * `NMBD` - As above, enable nmbd

View File

@ -33,15 +33,32 @@ charmap() { local chars="$1" file=/etc/samba/smb.conf
sed -i '/catia:mappings/s| =.*| = '"$chars"'|' $file 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 ### global: set a global config option
# Arguments: # Arguments:
# option) raw option # option) raw option
# Return: line added to smb.conf (replaces existing line with same key) # Return: line added to smb.conf (replaces existing line with same key)
global() { local key="${1%%=*}" value="${1#*=}" file=/etc/samba/smb.conf global() { local key="$(sed 's| *=.*||' <<< $1)" \
if grep -qE '^;*\s*'"$key" "$file"; then value="$(sed 's|.*= *||' <<< $1)" file=/etc/samba/smb.conf
sed -i 's|^;*\s*'"$key"'.*| '"${key% } = ${value# }"'|' "$file" if sed -n '/^\[global\]/,/^\[/p' $file | grep -qE '^;*\s*'"$key"; then
sed -i '/^\[global\]/,/^\[/s|^;*\s*\('"$key"' = \).*| \1'"$value"'|' \
"$file"
else else
sed -i '/\[global\]/a \ '"${key% } = ${value# }" "$file" sed -i '/\[global\]/a \ '"$key = $value" "$file"
fi fi
} }
@ -176,8 +193,11 @@ Options (fields in '[]' are optional, '<>' are required):
-h This help -h This help
-c \"<from:to>\" setup character mapping for file/directory names -c \"<from:to>\" setup character mapping for file/directory names
required arg: \"<from:to>\" character mappings separated by ',' required arg: \"<from:to>\" character mappings separated by ','
-G \"<section;parameter>\" Provide generic section option for smb.conf
required arg: \"<section>\" - IE: \"share\"
required arg: \"<parameter>\" - IE: \"log level = 2\"
-g \"<parameter>\" Provide global option for smb.conf -g \"<parameter>\" Provide global option for smb.conf
required arg: \"<parameter>\" - IE: -g \"log level = 2\" required arg: \"<parameter>\" - IE: \"log level = 2\"
-i \"<path>\" Import smbpassword -i \"<path>\" Import smbpassword
required arg: \"<path>\" - full file path in container required arg: \"<path>\" - full file path in container
-n Start the 'nmbd' daemon to advertise the shares -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 [[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o smbuser
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o smb [[ "${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 case "$opt" in
h) usage ;; h) usage ;;
c) charmap "$OPTARG" ;; c) charmap "$OPTARG" ;;
G) eval generic $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG) ;;
g) global "$OPTARG" ;; g) global "$OPTARG" ;;
i) import "$OPTARG" ;; i) import "$OPTARG" ;;
n) NMBD="true" ;; n) NMBD="true" ;;
@ -243,6 +264,9 @@ done
shift $(( OPTIND - 1 )) shift $(( OPTIND - 1 ))
[[ "${CHARMAP:-""}" ]] && charmap "$CHARMAP" [[ "${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 while read i; do
global "$i" global "$i"
done < <(env | awk '/^GLOBAL[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}') done < <(env | awk '/^GLOBAL[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')