summaryrefslogtreecommitdiff
path: root/usr.bin/locate
diff options
context:
space:
mode:
authorapb <apb@NetBSD.org>2011-07-10 13:42:49 +0000
committerapb <apb@NetBSD.org>2011-07-10 13:42:49 +0000
commit2d4ddddc260ba3c67f3046e2cde80efb62d9d12d (patch)
treecb669c332186a7dd98ed4cf8acacffd85d9d1b4e /usr.bin/locate
parent59fdbd407f045e25d1852b4476d40a5da16cf374 (diff)
Allow quoting of embedded shell metacharacters in locate.conf(5).
The shell_quote function here is identical to that in postinstall and etcupdate. This should fix PR 45130 from Greg Woods.
Diffstat (limited to 'usr.bin/locate')
-rw-r--r--usr.bin/locate/locate/updatedb.sh92
1 files changed, 62 insertions, 30 deletions
diff --git a/usr.bin/locate/locate/updatedb.sh b/usr.bin/locate/locate/updatedb.sh
index 2f4cb50131b..30ed69a1828 100644
--- a/usr.bin/locate/locate/updatedb.sh
+++ b/usr.bin/locate/locate/updatedb.sh
@@ -1,6 +1,6 @@
#!/bin/sh
#
-# $NetBSD: updatedb.sh,v 1.11 2006/04/23 03:04:08 christos Exp $
+# $NetBSD: updatedb.sh,v 1.12 2011/07/10 13:42:49 apb Exp $
#
# Copyright (c) 1989, 1993
# The Regents of the University of California. All rights reserved.
@@ -51,47 +51,78 @@ ignorefs='! -fstype local -o -fstype cd9660 -o -fstype fdesc -o -fstype kernfs -
ignore=
SRCHPATHS=
+# Quote args to make them safe in the shell.
+# Usage: quotedlist="$(shell_quote args...)"
+#
+# After building up a quoted list, use it by evaling it inside
+# double quotes, like this:
+# eval "set -- $quotedlist"
+# or like this:
+# eval "\$command $quotedlist \$filename"
+#
+shell_quote()
+{
+ local result=''
+ local arg
+ for arg in "$@" ; do
+ # Append a space if necessary
+ result="${result}${result:+ }"
+ # Convert each embedded ' to '\'',
+ # then insert ' at the beginning of the first line,
+ # and append ' at the end of the last line.
+ result="${result}$(printf "%s\n" "$arg" | \
+ sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/")"
+ done
+ printf "%s\n" "$result"
+}
+
# read configuration file
if [ -f "$CONF" ]; then
- exec 5<&0 < "$CONF"
- while read com args; do
- case "$com/$args" in /) continue;; esac # skip blank lines
+ while read -r com args; do
+ case "$com" in
+ ''|'#'*)
+ continue ;; # skip blank lines and comment lines
+ esac
+ eval "set -- $args"
case "$com" in
- '#'*) ;; # lines start with # is comment
searchpath)
- SRCHPATHS="$SRCHPATHS $args";;
+ SRCHPATHS="${SRCHPATHS}${SRCHPATHS:+ }$(shell_quote "$@")";;
ignorefs)
- for i in $args; do
+ for i in "$@"; do
+ fs=
case "$i" in
none) ignorefs=;;
- *) fs=`echo "$i" | sed -e 's/^!/! -fstype /' -e t -e 's/^/-fstype /'`
- ignorefs="${ignorefs:+${ignorefs} -o }${fs}"
+ \!*) fs="! -fstype $(shell_quote "$i")";;
+ *) fs="-fstype $(shell_quote "${i#?}")";;
+ esac
+ case "$fs" in
+ '') ;;
+ *) ignorefs="${ignorefs:+${ignorefs} -o }${fs}";;
esac
done;;
ignore)
- set -f
- for i in $args; do
- ignore="${ignore:+${ignore} -o }-path ${i}"
- done
- set +f;;
+ for i in "$@"; do
+ q="$(shell_quote "$i")"
+ ignore="${ignore:+${ignore} -o }-path ${q}"
+ done;;
ignorecontents)
- set -f
- for i in $args; do
- ignore="${ignore:+${ignore} -o }-path ${i} -print"
- done
- set +f;;
+ for i in "$@"; do
+ q="$(shell_quote "$i")"
+ ignore="${ignore:+${ignore} -o }-path ${q} -print"
+ done;;
workdir)
- if [ -d "$args" ]; then
- TMPDIR="$args"
+ if [ $# -ne 1 ]; then
+ echo "$CONF: workdir takes exactly one argument" >&2
+ elif [ -d "$1" ]; then
+ TMPDIR="$1"
else
- echo "$CONF: workdir: $args nonexistent" >&2
+ echo "$CONF: workdir: $1 nonexistent" >&2
fi;;
*)
- echo "$CONF: $com: unknown config command" >&2
+ echo "$CONF: $com: unknown config command" >&2
exit 1;;
esac
- done
- exec <&5 5>&-
+ done < "$CONF"
fi
: ${SRCHPATHS:=/} # directories to be put in the database
@@ -99,7 +130,7 @@ export TMPDIR
case "$ignorefs/$ignore" in
/) lp= ; rp= ;;
-*) lp='('; rp=') -prune -o' ;;
+*) lp='\('; rp='\) -prune -o' ;;
esac
# insert '-o' if neither $ignorefs or $ignore are empty
@@ -109,14 +140,14 @@ case "$ignorefs $ignore" in
esac
FILELIST=$(mktemp -t locate.list) || exit 1
-trap "rm -f '$FILELIST'" EXIT
-trap "rm -f '$FILELIST'; exit 1" INT QUIT TERM
+trap 'rm -f "$FILELIST"' EXIT
+trap 'rm -f "$FILELIST"; exit 1' INT QUIT TERM
# Make a file list and compute common bigrams.
# Entries of each directory shall be sorted (find -s).
-set -f
-(find -s ${SRCHPATHS} $lp $ignorefs $ignore $rp -print; true) | cat >> "$FILELIST"
+(eval "find -s ${SRCHPATHS} $lp $ignorefs $ignore $rp -print"; true) \
+ | cat >> "$FILELIST"
if [ $? != 0 ]
then
exit 1
@@ -127,6 +158,7 @@ BIGRAMS=$($LIBDIR/locate.bigram <"$FILELIST")
# code the file list
if [ -z "$BIGRAMS" ]; then
echo 'locate: updatedb failed' >&2
+ exit 1
else
$LIBDIR/locate.code "$BIGRAMS" <"$FILELIST" >"$FCODES"
chmod 644 "$FCODES"