| 1 | # Copyright (c) 2008, Christopher Cowart and contributors |
|---|
| 2 | # All rights reserved. |
|---|
| 3 | # |
|---|
| 4 | # Redistribution and use in source and binary forms, with or without |
|---|
| 5 | # modification, are permitted provided that the following conditions |
|---|
| 6 | # are met: |
|---|
| 7 | # * Redistributions of source code must retain the above copyright |
|---|
| 8 | # notice, this list of conditions and the following disclaimer. |
|---|
| 9 | # * Redistributions in binary form must reproduce the above copyright |
|---|
| 10 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 11 | # documentation and/or other materials provided with the distribution. |
|---|
| 12 | # |
|---|
| 13 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 14 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 15 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 16 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 17 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 18 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|---|
| 19 | # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 20 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|---|
| 21 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 22 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 23 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 24 | # |
|---|
| 25 | # $Id$ |
|---|
| 26 | # |
|---|
| 27 | # This file is a global include for all confman-based utilities. It |
|---|
| 28 | # provides the common interfaces for loading the shell library, setting |
|---|
| 29 | # up the use of syslog, and reading configurations. |
|---|
| 30 | |
|---|
| 31 | # Some global definitions. Defaults that can be overriden by options. |
|---|
| 32 | DEFAULTS="@sysconfdefaultsdir@/confman.conf" |
|---|
| 33 | UCONF="${HOME}/.confmanrc" |
|---|
| 34 | MYNAME=`basename $0` |
|---|
| 35 | if [ `id -u` != "0" ]; then |
|---|
| 36 | SUDO="@SUDO@" |
|---|
| 37 | fi |
|---|
| 38 | |
|---|
| 39 | # First things first. Check to see if the logging fd is open. If it's not, |
|---|
| 40 | # we re-spawn confman inside a pipe. |
|---|
| 41 | if (true >&5) 2>/dev/null ; then |
|---|
| 42 | exec 4>&2 2>&5 >&3 |
|---|
| 43 | else |
|---|
| 44 | exec 3>&1 |
|---|
| 45 | exec $0 "$@" 5>&1 1>&3 | logger -t "$MYNAME[$$]: $USER" -s |
|---|
| 46 | exit ${PIPESTATUS[0]} |
|---|
| 47 | fi |
|---|
| 48 | |
|---|
| 49 | # After the fork, let's MY_PID so subshells can throw errors that will |
|---|
| 50 | # actually kill the parent |
|---|
| 51 | MY_PID="$$" |
|---|
| 52 | |
|---|
| 53 | # Suck in the default configuration |
|---|
| 54 | if [ -f "$DEFAULTS" ] ; then |
|---|
| 55 | . "$DEFAULTS" |
|---|
| 56 | else |
|---|
| 57 | echo "$DEFAULTS couldn't be located" >&2 |
|---|
| 58 | exit 1 |
|---|
| 59 | fi |
|---|
| 60 | |
|---|
| 61 | # Get the global config |
|---|
| 62 | if [ -f "$GCONF" ] ; then |
|---|
| 63 | . "$GCONF" |
|---|
| 64 | else |
|---|
| 65 | echo "Global config file couldn't be located" >&2 |
|---|
| 66 | exit 1 |
|---|
| 67 | fi |
|---|
| 68 | |
|---|
| 69 | if [ -z "$UNAME" ] ; then |
|---|
| 70 | UNAME=`uname` |
|---|
| 71 | fi |
|---|
| 72 | case $UNAME in |
|---|
| 73 | FreeBSD) |
|---|
| 74 | install_cmd="install -Sp" |
|---|
| 75 | mktemp_file="mktemp -t confman" |
|---|
| 76 | mktemp_dir="mktemp -d -t confman" |
|---|
| 77 | sed_cmd="sed -E" |
|---|
| 78 | function sed_i_cmd { |
|---|
| 79 | ${sed_cmd} -i '' "$@" |
|---|
| 80 | } |
|---|
| 81 | stat_cmd="stat -f" |
|---|
| 82 | stat_opts="mode=%Mp%Lp owner=%Su group=%Sg" |
|---|
| 83 | readlink_cmd="@GREADLINK@" |
|---|
| 84 | ;; |
|---|
| 85 | Linux) |
|---|
| 86 | install_cmd="install -p" |
|---|
| 87 | mktemp_file="mktemp -t confman.XXXXXX" |
|---|
| 88 | mktemp_dir="mktemp -t -d confman.XXXXXX" |
|---|
| 89 | sed_cmd="sed -r" |
|---|
| 90 | function sed_i_cmd { |
|---|
| 91 | ${sed_cmd} -i "$@" |
|---|
| 92 | } |
|---|
| 93 | stat_cmd="stat -c" |
|---|
| 94 | stat_opts="mode=%a owner=%U group=%G" |
|---|
| 95 | readlink_cmd="readlink" |
|---|
| 96 | ;; |
|---|
| 97 | *) |
|---|
| 98 | install_cmd="install -Sp" |
|---|
| 99 | mktemp_file="mktemp -t confman" |
|---|
| 100 | mktemp_dir="mktemp -d -t confman" |
|---|
| 101 | sed_cmd="sed -E" |
|---|
| 102 | function sed_i_cmd { |
|---|
| 103 | ${sed_cmd} -i '' "$@" |
|---|
| 104 | } |
|---|
| 105 | stat_cmd="stat -f" |
|---|
| 106 | stat_opts="mode=%Mp%Lp owner=%Su group=%Sg" |
|---|
| 107 | readlink_cmd="@GREADLINK@" |
|---|
| 108 | ;; |
|---|
| 109 | esac |
|---|
| 110 | |
|---|
| 111 | # Things that don't vary by OS |
|---|
| 112 | fetch_cmd="@WGET@ -q -O" |
|---|
| 113 | |
|---|
| 114 | # Create a base temporary directory for this process |
|---|
| 115 | export TMPDIR=`$mktemp_dir` |
|---|
| 116 | |
|---|
| 117 | # Get the user config |
|---|
| 118 | if [ -f "$UCONF" ] ; then |
|---|
| 119 | . "$UCONF" |
|---|
| 120 | fi |
|---|
| 121 | |
|---|
| 122 | # Set debugging: |
|---|
| 123 | if [ -z $DEBUG ] ; then |
|---|
| 124 | DEBUG="false" |
|---|
| 125 | fi |
|---|
| 126 | |
|---|
| 127 | # Now, we source the library |
|---|
| 128 | if [ -f $REPO_LIBRARY ] ; then |
|---|
| 129 | . $REPO_LIBRARY |
|---|
| 130 | else |
|---|
| 131 | echo "Couldn't source the confman shell library" >&2 |
|---|
| 132 | exit 1 |
|---|
| 133 | fi |
|---|
| 134 | |
|---|
| 135 | # Source the filedb library |
|---|
| 136 | if [ -r "${FILEDB_LIBRARY}" ]; then |
|---|
| 137 | . "${FILEDB_LIBRARY}" |
|---|
| 138 | else |
|---|
| 139 | echo "Could not load filedb library: ${FILEDB_LIBRARY}" >&2 |
|---|
| 140 | exit 1 |
|---|
| 141 | fi |
|---|
| 142 | |
|---|
| 143 | # Create and/or fix permissions on our local state directory |
|---|
| 144 | if ! [ -d "$REPO_DB" ] ; then |
|---|
| 145 | $SUDO $install_cmd -d -o root -g $CONF_GROUP -m 2770 "$REPO_DB" |
|---|
| 146 | fi |
|---|
| 147 | find $REPO_DB -not -group $CONF_GROUP -exec $SUDO chgrp $CONF_GROUP {} \; \ |
|---|
| 148 | -exec chmod g+r {} \; |
|---|
| 149 | |
|---|
| 150 | # And we'll set a trap: |
|---|
| 151 | trap "conf_cleanExit" SIGINT SIGTERM SIGHUP EXIT |
|---|
| 152 | |
|---|