PageRenderTime 1560ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tor/tor.SlackBuild

https://github.com/PhantomX/slackbuilds
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
  1. #!/bin/bash
  2. #-- tor for Slackware --
  3. # Build script by Phantom X <megaphantomx at bol.com.br>
  4. # Suggested usage: $ tor.SlackBuild 2>&1 | tee build.log
  5. #--
  6. # Copyright 2008-2015 Phantom X, Goiania, Brazil.
  7. # Copyright 2006 Martijn Dekker, Groningen, Netherlands.
  8. #
  9. # Redistribution and use of this script, with or without modification, is
  10. # permitted provided that the following conditions are met:
  11. #
  12. # 1. Redistributions of this script must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR IMPLIED
  16. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  18. # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. # http://tor.eff.org
  26. PACKAGER_ID=${PACKAGER_ID:-$USER}
  27. PACKAGER=${PACKAGER:-$USER@$HOSTNAME}
  28. # Set YES for native build with gcc >= 4.2
  29. SB_NATIVE=${SB_NATIVE:-NO}
  30. # Set to YES to replicate slackbuild and patches
  31. SB_REP=${SB_REP:-YES}
  32. CWD=$(pwd)
  33. TMP=${TMP:-/tmp}
  34. if [ ! -d ${TMP} ]; then
  35. mkdir -p ${TMP}
  36. fi
  37. NAME=tor
  38. PKG=${PKG:-${TMP}/package-${NAME}}
  39. VERSION=${VERSION:-0.2.6.10}
  40. if [ "${SB_NATIVE}" = "YES" ] ;then
  41. ARCH=${ARCH:-$(uname -m)}
  42. else
  43. ARCH=${ARCH:-x86_64}
  44. fi
  45. if [ "${ARCH}" = "x86_64" ] ;then
  46. SLKTARGET=${SLKTARGET:-x86_64}
  47. else
  48. SLKTARGET=${SLKTARGET:-i586}
  49. fi
  50. SLKDTARGET=${SLKDTARGET:-slackware}
  51. BUILD=${BUILD:-1}
  52. NJOBS=${NJOBS:-$(( $(getconf _NPROCESSORS_ONLN) + 1 ))}
  53. DOCDIR=${PKG}/usr/doc/${NAME}-${VERSION}
  54. SBDIR=${PKG}/usr/src/slackbuilds/${NAME}
  55. PKGDEST=${PKGDEST:-${CWD}}
  56. PKGFORMAT=${PKGFORMAT:-txz}
  57. PKGNAME=${NAME}-$(echo ${VERSION} | tr - . )-${ARCH}-${BUILD}${PACKAGER_ID}
  58. DATE=$(LC_ALL=C date +%d-%b-%Y)
  59. grep --quiet '^toranon:' /etc/group && SB_HGROUP=1
  60. grep --quiet '^toranon:' /etc/passwd && SB_HUSER=1
  61. SRCDIR=${NAME}-${VERSION}
  62. SRCARCHIVE=${SRCDIR}.tar.gz
  63. DL_PROG=${DL_PROG:-wget}
  64. DL_TO=${DL_TO:-5}
  65. DL_OPTS=${DL_OPTS:-"--timeout=${DL_TO}"}
  66. DL_URL="http://tor.eff.org/dist/${SRCARCHIVE}"
  67. # if source is not present, download in source rootdir if possible
  68. test -r ${CWD}/${SRCARCHIVE} || ${DL_PROG} ${DL_OPTS} ${DL_URL} || exit 1
  69. if [ "${SB_NATIVE}" = "YES" ] ;then
  70. SLKCFLAGS="-O2 -march=native -mtune=native -pipe"
  71. [ "${SB_ECFLAGS}" ] && SLKCFLAGS="${SLKCFLAGS} ${SB_ECFLAGS}"
  72. else
  73. case "${ARCH}" in
  74. i[3-6]86) SLKCFLAGS="-O2 -march=${ARCH} -mtune=i686"
  75. ;;
  76. x86_64) SLKCFLAGS="-O2 -fPIC"
  77. ;;
  78. s390|*) SLKCFLAGS="-O2"
  79. ;;
  80. esac
  81. fi
  82. if [ "${ARCH}" = "x86_64" ] ;then
  83. LIBDIRSUFFIX="64"
  84. SLKCFLAGS="${SLKCFLAGS} -fPIC"
  85. else
  86. LIBDIRSUFFIX=""
  87. fi
  88. if [ -d ${PKG} ]; then
  89. # Clean up a previous build
  90. rm -rf ${PKG}
  91. fi
  92. mkdir -p ${PKG}
  93. cd ${TMP}
  94. rm -rf ${SRCDIR}
  95. tar -xvf ${CWD}/${SRCARCHIVE} || exit 1
  96. cd ${SRCDIR} || exit 1
  97. chmod -R u+w,go+r-w,a-s .
  98. if [ -r ${CWD}/apply-patches.sh ]; then
  99. . ${CWD}/apply-patches.sh
  100. fi
  101. sed -i \
  102. -e '/^After/s|syslog.target ||g' \
  103. -e '/^ExecStart/s| -f | --defaults-torrc /usr/share/tor/defaults-torrc\0|g' \
  104. contrib/dist/tor.service.in || exit 1
  105. export LDFLAGS="-Wl,--as-needed ${LDFLAGS}"
  106. CFLAGS="${SLKCFLAGS}" \
  107. CXXFLAGS="${SLKCFLAGS}" \
  108. ./configure \
  109. --prefix=/usr \
  110. --libdir=/usr/lib${LIBDIRSUFFIX} \
  111. --sysconfdir=/etc \
  112. --localstatedir=/var \
  113. --infodir=/usr/info \
  114. --mandir=/usr/man \
  115. --docdir=/usr/doc/${NAME}-${VERSION} \
  116. --disable-silent-rules \
  117. --disable-dependency-tracking \
  118. --with-tor-user=toranon \
  119. --with-tor-group=toranon \
  120. --build=${SLKTARGET}-${SLKDTARGET}-linux || exit 1
  121. make -j${NJOBS} || make || exit 1
  122. make install DESTDIR=${PKG} || exit 1
  123. mv ${PKG}/etc/tor/torrc.{sample,new} || exit 1
  124. mkdir -p ${PKG}/etc/logrotate.d
  125. cat > ${PKG}/etc/logrotate.d/tor.new <<'EOF'
  126. /var/log/tor/*.log {
  127. daily
  128. rotate 5
  129. compress
  130. delaycompress
  131. missingok
  132. notifempty
  133. create 0640 toranon toranon
  134. sharedscripts
  135. postrotate
  136. /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 || :
  137. endscript
  138. }
  139. EOF
  140. mkdir -p ${PKG}/var/{lib,log,run}/${NAME}
  141. mkdir -p ${PKG}/etc/rc.d
  142. install -pm0755 ${CWD}/rc.${NAME} ${PKG}/etc/rc.d/rc.${NAME}.new || exit 1
  143. chmod 0644 ${PKG}/etc/rc.d/rc.${NAME}.new || exit 1
  144. mkdir -p ${PKG}/var/log/setup
  145. zcat ${CWD}/setup.onlyonce.rc${NAME}.gz \
  146. > ${PKG}/var/log/setup/setup.onlyonce.rc${NAME} || exit 1
  147. chmod 0755 ${PKG}/var/log/setup/setup.onlyonce.rc${NAME} || exit 1
  148. mkdir -p ${PKG}/lib/systemd/system
  149. install -pm0644 contrib/dist/tor.service ${PKG}/lib/systemd/system/ || exit 1
  150. chmod 0640 ${PKG}/etc/tor/torrc*
  151. chmod 0700 ${PKG}/var/lib/tor
  152. chmod 0700 ${PKG}/var/log/tor
  153. chmod 0755 ${PKG}/var/run/tor
  154. rm -f ${PKG}/usr/bin/torify
  155. rm -f ${PKG}/usr/man/man1/torify*
  156. rm -f ${PKG}/etc/tor/tor-tsocks.conf
  157. find ${PKG} | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  158. | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  159. cat > ${PKG}/usr/share/tor/defaults-torrc <<'EOF'
  160. DataDirectory /var/lib/tor
  161. User toranon
  162. Log notice syslog
  163. EOF
  164. mkdir -p ${PKG}/usr/lib/tmpfiles.d
  165. cat > ${PKG}/usr/lib/tmpfiles.d/${NAME}.conf <<EOF
  166. d /run/${NAME} 0755 toranon toranon
  167. EOF
  168. # Add a documentation directory:
  169. mkdir -p ${DOCDIR}
  170. cp -a \
  171. doc/{HACKING,TODO,spec/*.txt} \
  172. AUTHORS LICENSE README ReleaseNotes ${CWD}/ChangeLog.SB \
  173. ${DOCDIR}/
  174. [ -r ChangeLog ] && head -n 1000 ChangeLog > ${DOCDIR}/ChangeLog
  175. find ${DOCDIR}/ -type d -print0 | xargs -0 chmod 0755
  176. find ${DOCDIR}/ -type f -print0 | xargs -0 chmod 0644
  177. find ${DOCDIR}/ -type f -size 0 -print0 | xargs -0 rm -f
  178. # Compress and link manpages, if any:
  179. if [ -d ${PKG}/usr/share/man ]; then
  180. mv ${PKG}/usr/share/man ${PKG}/usr/man
  181. rmdir ${PKG}/usr/share
  182. fi
  183. if [ -d ${PKG}/usr/man ]; then
  184. ( cd ${PKG}/usr/man
  185. for manpagedir in $(find . -type d -name "man*") ; do
  186. ( cd ${manpagedir}
  187. for eachpage in $( find . -type l -maxdepth 1) ; do
  188. ln -s $( readlink ${eachpage} ).gz ${eachpage}.gz
  189. rm ${eachpage}
  190. done
  191. gzip -9 *.?
  192. # Prevent errors
  193. rm -f *.gz.gz
  194. )
  195. done
  196. )
  197. fi
  198. mkdir -p ${PKG}/install
  199. cat ${CWD}/slack-desc > ${PKG}/install/slack-desc
  200. cat ${CWD}/slack-required > ${PKG}/install/slack-required
  201. cat > ${PKG}/install/doinst.sh <<'EOF'
  202. function free_user_id {
  203. # Find a free user-ID >= 100 (should be < 1000 so it's not a normal user)
  204. local FREE_USER_ID=100
  205. while grep --quiet "^.*:.*:${FREE_USER_ID}:.*:.*:.*:" etc/passwd; do
  206. let FREE_USER_ID++
  207. done
  208. echo ${FREE_USER_ID}
  209. }
  210. function free_group_id {
  211. # Find a free group-ID >= 120 (should be < 1000 so it's not a normal group)
  212. local FREE_GROUP_ID=120
  213. while grep --quiet "^.*:.*:${FREE_GROUP_ID}:" etc/group; do
  214. let FREE_GROUP_ID++
  215. done
  216. echo ${FREE_GROUP_ID}
  217. }
  218. # Figure out our root directory
  219. ROOTDIR=$(pwd)
  220. unset CHROOT
  221. if test "${ROOTDIR}" != "/"; then
  222. CHROOT="chroot ${ROOTDIR} "
  223. ROOTDIR="${ROOTDIR}/"
  224. fi
  225. # Set up group.
  226. if ! grep --quiet '^toranon:' etc/group ;then
  227. ${CHROOT} /usr/sbin/groupadd \
  228. -g $(free_group_id) \
  229. toranon 2> /dev/null
  230. fi
  231. # Set up user: add it if it doesn't exist, update it if it already does.
  232. if OLD_ENTRY=$(grep --max-count=1 '^toranon:' etc/passwd) \
  233. || OLD_ENTRY=$(grep --max-count=1 \
  234. ':/var/lib/tor:[a-z/]*$' etc/passwd)
  235. then
  236. # Modify existing user
  237. OLD_USER=$(echo ${OLD_ENTRY} | cut --fields=1 --delimiter=':')
  238. USER_ID=$(echo ${OLD_ENTRY} | cut --fields=3 --delimiter=':')
  239. test ${USER_ID} -ge 1000 && USER_ID=$(free_user_id)
  240. if test "${OLD_USER}" = "toranon"; then
  241. echo -n "Updating unprivileged user" 1>&2
  242. else
  243. echo -ne "Changing unprivileged user \e[1m${OLD_USER}\e[0m to" 1>&2
  244. fi
  245. ${CHROOT} /usr/sbin/usermod \
  246. -d '/var/lib/tor' \
  247. -u ${USER_ID} \
  248. -s /bin/false \
  249. ${OLD_USER}
  250. else
  251. # Add new user
  252. ${CHROOT} /usr/sbin/useradd \
  253. -c 'TOR anonymizing user' \
  254. -u $(free_user_id) \
  255. -g toranon \
  256. -s /bin/false \
  257. -d '/var/lib/tor' \
  258. toranon 2> /dev/null
  259. fi
  260. EOF
  261. cat >> ${PKG}/install/doinst.sh <<EOF
  262. config() {
  263. NEW="\$1"
  264. OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  265. # If there's no config file by that name, mv it over:
  266. if [ ! -r \$OLD ]; then
  267. mv \$NEW \$OLD
  268. elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then
  269. # toss the redundant copy
  270. rm \$NEW
  271. fi
  272. # Otherwise, we leave the .new copy for the admin to consider...
  273. }
  274. # Keep same perms on rc.${NAME}.new:
  275. if [ -r etc/rc.d/rc.${NAME} -a -r etc/rc.d/rc.${NAME}.new ]; then
  276. chmod --reference=etc/rc.d/rc.${NAME} etc/rc.d/rc.${NAME}.new
  277. fi
  278. ## List of conf files to check. The conf files in your package should end in .new
  279. EOF
  280. ( cd ${PKG}
  281. find etc/ -name *.new -exec echo config {} ';' | sort >> ${PKG}/install/doinst.sh
  282. echo >> ${PKG}/install/doinst.sh
  283. )
  284. cat >> ${PKG}/install/doinst.sh <<'EOF'
  285. # Fix permissions
  286. ${CHROOT} /bin/chown root.toranon /etc/tor/torrc*
  287. ${CHROOT} /bin/chmod 0640 /etc/tor/torrc*
  288. ${CHROOT} /bin/chown toranon.toranon /var/lib/tor
  289. ${CHROOT} /bin/chmod 0700 /var/lib/tor
  290. ${CHROOT} /bin/chown toranon.toranon /var/log/tor
  291. ${CHROOT} /bin/chmod 0700 /var/log/tor
  292. ${CHROOT} /bin/chown toranon.toranon /var/run/tor
  293. if [ -x bin/systemctl ] ; then
  294. ${CHROOT} /bin/systemctl --system daemon-reload >/dev/null 2>&1
  295. fi
  296. EOF
  297. sed -i "s|_PACKAGER|${PACKAGER}|g; s|_BUILD_DATE|${DATE}|g" \
  298. ${PKG}/install/slack-desc
  299. if [ "${SB_REP}" = "YES" ] ;then
  300. # Replicate slackbuild and patches
  301. mkdir -p ${SBDIR}/paches
  302. install -m0644 ${CWD}/slack-desc ${CWD}/slack-required ${CWD}/ChangeLog.SB \
  303. ${CWD}/apply-patches.sh ${CWD}/{rc.,setup.onlyonce.rc}*.gz \
  304. ${SBDIR}/
  305. install -m0755 ${CWD}/${NAME}.SlackBuild \
  306. ${SBDIR}/${NAME}.SlackBuild
  307. install -m0644 ${CWD}/patches/*.* \
  308. ${SBDIR}/patches/
  309. rmdir ${SBDIR}/patches
  310. fi
  311. # Build package:
  312. set +o xtrace # no longer print commands upon execution
  313. set -e
  314. ROOTCOMMANDS="set -o errexit -o xtrace ; cd ${PKG} ;
  315. /bin/chown --recursive root:root . ;
  316. [ \"${SB_HGROUP}\" = 1 ] && /bin/chown root.toranon ./etc/tor/torrc* ;
  317. [ \"${SB_HGROUP}\" = 1 ] && [ \"${SB_HUSER}\" = 1 ] && /bin/chown toranon.toranon ./var/{run,lib,log}/tor ; "
  318. ROOTCOMMANDS="${ROOTCOMMANDS}
  319. /sbin/makepkg --prepend --linkadd y --chown n ${PKGDEST}/${PKGNAME}.${PKGFORMAT} "
  320. if test ${UID} = 0; then
  321. eval ${ROOTCOMMANDS}
  322. set +o xtrace
  323. elif test "$(type -t fakeroot)" = 'file'; then
  324. echo -e "\e[1mEntering fakeroot environment.\e[0m"
  325. echo ${ROOTCOMMANDS} | fakeroot
  326. else
  327. echo -e "\e[1mPlease enter your root password.\e[0m (Consider installing fakeroot.)"
  328. /bin/su -c "${ROOTCOMMANDS}"
  329. fi
  330. # Clean up the extra stuff:
  331. if [ "$1" = "--cleanup" ]; then
  332. echo "Cleaning..."
  333. if [ -d ${TMP}/${SRCDIR} ]; then
  334. rm -rf ${TMP}/${SRCDIR} && echo "${TMP}/${SRCDIR} cleanup completed"
  335. fi
  336. if [ -d ${PKG} ]; then
  337. rm -rf ${PKG} && echo "${PKG} cleanup completed"
  338. fi
  339. rmdir ${TMP} && echo "${TMP} cleanup completed"
  340. fi
  341. exit 0