/tor/tor.SlackBuild
Bash | 394 lines | 341 code | 53 blank | 0 comment | 0 complexity | e99f65563ddf2cd5eac5e4089d9b4353 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, GPL-3.0
- #!/bin/bash
- #-- tor for Slackware --
- # Build script by Phantom X <megaphantomx at bol.com.br>
- # Suggested usage: $ tor.SlackBuild 2>&1 | tee build.log
- #--
- # Copyright 2008-2015 Phantom X, Goiania, Brazil.
- # Copyright 2006 Martijn Dekker, Groningen, Netherlands.
- #
- # Redistribution and use of this script, with or without modification, is
- # permitted provided that the following conditions are met:
- #
- # 1. Redistributions of this script must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- #
- # THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR IMPLIED
- # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
- # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- # http://tor.eff.org
- PACKAGER_ID=${PACKAGER_ID:-$USER}
- PACKAGER=${PACKAGER:-$USER@$HOSTNAME}
- # Set YES for native build with gcc >= 4.2
- SB_NATIVE=${SB_NATIVE:-NO}
- # Set to YES to replicate slackbuild and patches
- SB_REP=${SB_REP:-YES}
- CWD=$(pwd)
- TMP=${TMP:-/tmp}
- if [ ! -d ${TMP} ]; then
- mkdir -p ${TMP}
- fi
- NAME=tor
- PKG=${PKG:-${TMP}/package-${NAME}}
- VERSION=${VERSION:-0.2.6.10}
- if [ "${SB_NATIVE}" = "YES" ] ;then
- ARCH=${ARCH:-$(uname -m)}
- else
- ARCH=${ARCH:-x86_64}
- fi
- if [ "${ARCH}" = "x86_64" ] ;then
- SLKTARGET=${SLKTARGET:-x86_64}
- else
- SLKTARGET=${SLKTARGET:-i586}
- fi
- SLKDTARGET=${SLKDTARGET:-slackware}
- BUILD=${BUILD:-1}
- NJOBS=${NJOBS:-$(( $(getconf _NPROCESSORS_ONLN) + 1 ))}
- DOCDIR=${PKG}/usr/doc/${NAME}-${VERSION}
- SBDIR=${PKG}/usr/src/slackbuilds/${NAME}
- PKGDEST=${PKGDEST:-${CWD}}
- PKGFORMAT=${PKGFORMAT:-txz}
- PKGNAME=${NAME}-$(echo ${VERSION} | tr - . )-${ARCH}-${BUILD}${PACKAGER_ID}
- DATE=$(LC_ALL=C date +%d-%b-%Y)
- grep --quiet '^toranon:' /etc/group && SB_HGROUP=1
- grep --quiet '^toranon:' /etc/passwd && SB_HUSER=1
- SRCDIR=${NAME}-${VERSION}
- SRCARCHIVE=${SRCDIR}.tar.gz
- DL_PROG=${DL_PROG:-wget}
- DL_TO=${DL_TO:-5}
- DL_OPTS=${DL_OPTS:-"--timeout=${DL_TO}"}
- DL_URL="http://tor.eff.org/dist/${SRCARCHIVE}"
- # if source is not present, download in source rootdir if possible
- test -r ${CWD}/${SRCARCHIVE} || ${DL_PROG} ${DL_OPTS} ${DL_URL} || exit 1
- if [ "${SB_NATIVE}" = "YES" ] ;then
- SLKCFLAGS="-O2 -march=native -mtune=native -pipe"
- [ "${SB_ECFLAGS}" ] && SLKCFLAGS="${SLKCFLAGS} ${SB_ECFLAGS}"
- else
- case "${ARCH}" in
- i[3-6]86) SLKCFLAGS="-O2 -march=${ARCH} -mtune=i686"
- ;;
- x86_64) SLKCFLAGS="-O2 -fPIC"
- ;;
- s390|*) SLKCFLAGS="-O2"
- ;;
- esac
- fi
- if [ "${ARCH}" = "x86_64" ] ;then
- LIBDIRSUFFIX="64"
- SLKCFLAGS="${SLKCFLAGS} -fPIC"
- else
- LIBDIRSUFFIX=""
- fi
- if [ -d ${PKG} ]; then
- # Clean up a previous build
- rm -rf ${PKG}
- fi
- mkdir -p ${PKG}
- cd ${TMP}
- rm -rf ${SRCDIR}
- tar -xvf ${CWD}/${SRCARCHIVE} || exit 1
- cd ${SRCDIR} || exit 1
- chmod -R u+w,go+r-w,a-s .
- if [ -r ${CWD}/apply-patches.sh ]; then
- . ${CWD}/apply-patches.sh
- fi
- sed -i \
- -e '/^After/s|syslog.target ||g' \
- -e '/^ExecStart/s| -f | --defaults-torrc /usr/share/tor/defaults-torrc\0|g' \
- contrib/dist/tor.service.in || exit 1
- export LDFLAGS="-Wl,--as-needed ${LDFLAGS}"
- CFLAGS="${SLKCFLAGS}" \
- CXXFLAGS="${SLKCFLAGS}" \
- ./configure \
- --prefix=/usr \
- --libdir=/usr/lib${LIBDIRSUFFIX} \
- --sysconfdir=/etc \
- --localstatedir=/var \
- --infodir=/usr/info \
- --mandir=/usr/man \
- --docdir=/usr/doc/${NAME}-${VERSION} \
- --disable-silent-rules \
- --disable-dependency-tracking \
- --with-tor-user=toranon \
- --with-tor-group=toranon \
- --build=${SLKTARGET}-${SLKDTARGET}-linux || exit 1
- make -j${NJOBS} || make || exit 1
- make install DESTDIR=${PKG} || exit 1
- mv ${PKG}/etc/tor/torrc.{sample,new} || exit 1
- mkdir -p ${PKG}/etc/logrotate.d
- cat > ${PKG}/etc/logrotate.d/tor.new <<'EOF'
- /var/log/tor/*.log {
- daily
- rotate 5
- compress
- delaycompress
- missingok
- notifempty
- create 0640 toranon toranon
- sharedscripts
- postrotate
- /bin/systemctl reload tor.service > /dev/null 2>/dev/null || test -x /etc/rc.d/rc.tor && /etc/rc.d/rc.tor reload >/dev/null 2>/dev/null || :
- endscript
- }
- EOF
-
- mkdir -p ${PKG}/var/{lib,log,run}/${NAME}
- mkdir -p ${PKG}/etc/rc.d
- install -pm0755 ${CWD}/rc.${NAME} ${PKG}/etc/rc.d/rc.${NAME}.new || exit 1
- chmod 0644 ${PKG}/etc/rc.d/rc.${NAME}.new || exit 1
- mkdir -p ${PKG}/var/log/setup
- zcat ${CWD}/setup.onlyonce.rc${NAME}.gz \
- > ${PKG}/var/log/setup/setup.onlyonce.rc${NAME} || exit 1
- chmod 0755 ${PKG}/var/log/setup/setup.onlyonce.rc${NAME} || exit 1
- mkdir -p ${PKG}/lib/systemd/system
- install -pm0644 contrib/dist/tor.service ${PKG}/lib/systemd/system/ || exit 1
- chmod 0640 ${PKG}/etc/tor/torrc*
- chmod 0700 ${PKG}/var/lib/tor
- chmod 0700 ${PKG}/var/log/tor
- chmod 0755 ${PKG}/var/run/tor
- rm -f ${PKG}/usr/bin/torify
- rm -f ${PKG}/usr/man/man1/torify*
- rm -f ${PKG}/etc/tor/tor-tsocks.conf
- find ${PKG} | xargs file | grep -e "executable" -e "shared object" | grep ELF \
- | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
- cat > ${PKG}/usr/share/tor/defaults-torrc <<'EOF'
- DataDirectory /var/lib/tor
- User toranon
- Log notice syslog
- EOF
- mkdir -p ${PKG}/usr/lib/tmpfiles.d
- cat > ${PKG}/usr/lib/tmpfiles.d/${NAME}.conf <<EOF
- d /run/${NAME} 0755 toranon toranon
- EOF
- # Add a documentation directory:
- mkdir -p ${DOCDIR}
- cp -a \
- doc/{HACKING,TODO,spec/*.txt} \
- AUTHORS LICENSE README ReleaseNotes ${CWD}/ChangeLog.SB \
- ${DOCDIR}/
- [ -r ChangeLog ] && head -n 1000 ChangeLog > ${DOCDIR}/ChangeLog
- find ${DOCDIR}/ -type d -print0 | xargs -0 chmod 0755
- find ${DOCDIR}/ -type f -print0 | xargs -0 chmod 0644
- find ${DOCDIR}/ -type f -size 0 -print0 | xargs -0 rm -f
- # Compress and link manpages, if any:
- if [ -d ${PKG}/usr/share/man ]; then
- mv ${PKG}/usr/share/man ${PKG}/usr/man
- rmdir ${PKG}/usr/share
- fi
- if [ -d ${PKG}/usr/man ]; then
- ( cd ${PKG}/usr/man
- for manpagedir in $(find . -type d -name "man*") ; do
- ( cd ${manpagedir}
- for eachpage in $( find . -type l -maxdepth 1) ; do
- ln -s $( readlink ${eachpage} ).gz ${eachpage}.gz
- rm ${eachpage}
- done
- gzip -9 *.?
- # Prevent errors
- rm -f *.gz.gz
- )
- done
- )
- fi
- mkdir -p ${PKG}/install
- cat ${CWD}/slack-desc > ${PKG}/install/slack-desc
- cat ${CWD}/slack-required > ${PKG}/install/slack-required
- cat > ${PKG}/install/doinst.sh <<'EOF'
- function free_user_id {
- # Find a free user-ID >= 100 (should be < 1000 so it's not a normal user)
- local FREE_USER_ID=100
- while grep --quiet "^.*:.*:${FREE_USER_ID}:.*:.*:.*:" etc/passwd; do
- let FREE_USER_ID++
- done
- echo ${FREE_USER_ID}
- }
- function free_group_id {
- # Find a free group-ID >= 120 (should be < 1000 so it's not a normal group)
- local FREE_GROUP_ID=120
- while grep --quiet "^.*:.*:${FREE_GROUP_ID}:" etc/group; do
- let FREE_GROUP_ID++
- done
- echo ${FREE_GROUP_ID}
- }
- # Figure out our root directory
- ROOTDIR=$(pwd)
- unset CHROOT
- if test "${ROOTDIR}" != "/"; then
- CHROOT="chroot ${ROOTDIR} "
- ROOTDIR="${ROOTDIR}/"
- fi
- # Set up group.
- if ! grep --quiet '^toranon:' etc/group ;then
- ${CHROOT} /usr/sbin/groupadd \
- -g $(free_group_id) \
- toranon 2> /dev/null
- fi
- # Set up user: add it if it doesn't exist, update it if it already does.
- if OLD_ENTRY=$(grep --max-count=1 '^toranon:' etc/passwd) \
- || OLD_ENTRY=$(grep --max-count=1 \
- ':/var/lib/tor:[a-z/]*$' etc/passwd)
- then
- # Modify existing user
- OLD_USER=$(echo ${OLD_ENTRY} | cut --fields=1 --delimiter=':')
- USER_ID=$(echo ${OLD_ENTRY} | cut --fields=3 --delimiter=':')
- test ${USER_ID} -ge 1000 && USER_ID=$(free_user_id)
- if test "${OLD_USER}" = "toranon"; then
- echo -n "Updating unprivileged user" 1>&2
- else
- echo -ne "Changing unprivileged user \e[1m${OLD_USER}\e[0m to" 1>&2
- fi
- ${CHROOT} /usr/sbin/usermod \
- -d '/var/lib/tor' \
- -u ${USER_ID} \
- -s /bin/false \
- ${OLD_USER}
- else
- # Add new user
- ${CHROOT} /usr/sbin/useradd \
- -c 'TOR anonymizing user' \
- -u $(free_user_id) \
- -g toranon \
- -s /bin/false \
- -d '/var/lib/tor' \
- toranon 2> /dev/null
- fi
- EOF
- cat >> ${PKG}/install/doinst.sh <<EOF
- config() {
- NEW="\$1"
- OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
- # If there's no config file by that name, mv it over:
- if [ ! -r \$OLD ]; then
- mv \$NEW \$OLD
- elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then
- # toss the redundant copy
- rm \$NEW
- fi
- # Otherwise, we leave the .new copy for the admin to consider...
- }
- # Keep same perms on rc.${NAME}.new:
- if [ -r etc/rc.d/rc.${NAME} -a -r etc/rc.d/rc.${NAME}.new ]; then
- chmod --reference=etc/rc.d/rc.${NAME} etc/rc.d/rc.${NAME}.new
- fi
- ## List of conf files to check. The conf files in your package should end in .new
- EOF
- ( cd ${PKG}
- find etc/ -name *.new -exec echo config {} ';' | sort >> ${PKG}/install/doinst.sh
- echo >> ${PKG}/install/doinst.sh
- )
- cat >> ${PKG}/install/doinst.sh <<'EOF'
- # Fix permissions
- ${CHROOT} /bin/chown root.toranon /etc/tor/torrc*
- ${CHROOT} /bin/chmod 0640 /etc/tor/torrc*
- ${CHROOT} /bin/chown toranon.toranon /var/lib/tor
- ${CHROOT} /bin/chmod 0700 /var/lib/tor
- ${CHROOT} /bin/chown toranon.toranon /var/log/tor
- ${CHROOT} /bin/chmod 0700 /var/log/tor
- ${CHROOT} /bin/chown toranon.toranon /var/run/tor
- if [ -x bin/systemctl ] ; then
- ${CHROOT} /bin/systemctl --system daemon-reload >/dev/null 2>&1
- fi
- EOF
- sed -i "s|_PACKAGER|${PACKAGER}|g; s|_BUILD_DATE|${DATE}|g" \
- ${PKG}/install/slack-desc
- if [ "${SB_REP}" = "YES" ] ;then
- # Replicate slackbuild and patches
- mkdir -p ${SBDIR}/paches
- install -m0644 ${CWD}/slack-desc ${CWD}/slack-required ${CWD}/ChangeLog.SB \
- ${CWD}/apply-patches.sh ${CWD}/{rc.,setup.onlyonce.rc}*.gz \
- ${SBDIR}/
- install -m0755 ${CWD}/${NAME}.SlackBuild \
- ${SBDIR}/${NAME}.SlackBuild
- install -m0644 ${CWD}/patches/*.* \
- ${SBDIR}/patches/
- rmdir ${SBDIR}/patches
- fi
- # Build package:
- set +o xtrace # no longer print commands upon execution
- set -e
- ROOTCOMMANDS="set -o errexit -o xtrace ; cd ${PKG} ;
- /bin/chown --recursive root:root . ;
- [ \"${SB_HGROUP}\" = 1 ] && /bin/chown root.toranon ./etc/tor/torrc* ;
- [ \"${SB_HGROUP}\" = 1 ] && [ \"${SB_HUSER}\" = 1 ] && /bin/chown toranon.toranon ./var/{run,lib,log}/tor ; "
- ROOTCOMMANDS="${ROOTCOMMANDS}
- /sbin/makepkg --prepend --linkadd y --chown n ${PKGDEST}/${PKGNAME}.${PKGFORMAT} "
- if test ${UID} = 0; then
- eval ${ROOTCOMMANDS}
- set +o xtrace
- elif test "$(type -t fakeroot)" = 'file'; then
- echo -e "\e[1mEntering fakeroot environment.\e[0m"
- echo ${ROOTCOMMANDS} | fakeroot
- else
- echo -e "\e[1mPlease enter your root password.\e[0m (Consider installing fakeroot.)"
- /bin/su -c "${ROOTCOMMANDS}"
- fi
- # Clean up the extra stuff:
- if [ "$1" = "--cleanup" ]; then
- echo "Cleaning..."
- if [ -d ${TMP}/${SRCDIR} ]; then
- rm -rf ${TMP}/${SRCDIR} && echo "${TMP}/${SRCDIR} cleanup completed"
- fi
- if [ -d ${PKG} ]; then
- rm -rf ${PKG} && echo "${PKG} cleanup completed"
- fi
- rmdir ${TMP} && echo "${TMP} cleanup completed"
- fi
- exit 0