--- fetchmail 2011-11-08 18:55:58.000000000 +0700 +++ fetchmail.new 2011-11-08 18:56:14.000000000 +0700 @@ -2,18 +2,59 @@ piddir=${pid_dir:-/var/run/fetchmail} pid_file=${piddir}/${RC_SVCNAME}.pid -rcfile=/etc/${RC_SVCNAME}rc + +# Need to define this variable here to make it global instead of local +rcfile="" depend() { need net use mta } +getrc() { +# This function accepts a single (optional) parameter: quiet + if [ -z "${fetchmail_config}" ]; then + # Config is not explicitly set in the relevant conf.d file + if [ -z "${fetchmail_rc}" ]; then + # User does not specify which file to use. Let's go find the right one + if [ -f /etc/${RC_SVCNAME}.rc ]; then + # Newer naming system + rcfile="/etc/${RC_SVCNAME}.rc" + elif [ -f /etc/${RC_SVCNAME}rc ]; then + # Older naming system + rcfile="/etc/${RC_SVCNAME}rc" + else + [ "x$1" != "xquiet" ] && eerror "Can't find any proper configuration for ${RC_SVCNAME}" + return 1 + fi + else + if [ -f ${fetchmail_rc} ]; then + rcfile="${fetchmail_rc}" + else + [ "x$1" != "xquiet" ] && eerror "Can't find ${fetchmail_rc} configuration file for ${RC_SVCNAME}" + return 1 + fi + fi + [ "x$1" != "xquiet" ] && einfo "Using configuration file: ${rcfile}" + else + [ "x$1" != "xquiet" ] && einfo "Using provided configuration in /etc/conf.d/${RC_SVCNAME}" + # Config is explicitly specified. Let's dump it into a file to be read back by fetchmail + # Why dumping it into a file instead of other methods (e.g., named pipe)? This allows + # troubleshooting. + rcfile=${piddir}/${RC_SVCNAME}.rc + echo "${fetchmail_config}" > $rcfile + fi + # Ensure ownership and permissions to prevent fetchmail from complaining + # Only the user needs to be changed; fetchmail is okay with any group + chown fetchmail $rcfile + chmod 0700 $rcfile +} + checkconfig() { - if [ ! -f ${rcfile} ]; then - eerror "Configuration file ${rcfile} not found" - return 1 + if [ ! -d ${piddir} ]; then + checkpath -q -d -o fetchmail:fetchmail -m 0755 ${piddir} || return 1 fi + getrc || return 1 local fetchmail_instance fetchmail_instance=${RC_SVCNAME##*.} if [ -n "${fetchmail_instance}" -a "${RC_SVCNAME}" != "fetchmail" ]; then @@ -21,13 +62,10 @@ else fidfile=/var/lib/fetchmail/.fetchids fi - if [ ! -d ${piddir} ]; then - checkpath -q -d -o fetchmail:fetchmail -m 0755 ${piddir} || return 1 - fi } start() { - checkconfig || return 1 + checkconfig || return 1 ebegin "Starting ${RC_SVCNAME}" start-stop-daemon --start --pidfile ${pid_file} \ --user fetchmail --exec /usr/bin/fetchmail \ @@ -40,5 +78,12 @@ ebegin "Stopping ${RC_SVCNAME}" start-stop-daemon --stop --quiet --pidfile ${pid_file} eend ${?} -} + # To assist troubleshooting (e.g., starting from shell using + # 'fetchmail -v -f $rcfile'), we do the courtesy of changing + # $rcfile's owner. + if getrc ; then + chown root $rcfile + chmod 0700 $rcfile + fi +}