install-freedombone.sh 64KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. #!/bin/bash
  2. # Freedombone install script intended for use with Debian Jessie
  3. #
  4. # Note on dynamic dns
  5. # ===================
  6. #
  7. # I'm not particularly trying to promote freedns.afraid.org
  8. # as a service, it just happens to be a dynamic DNS system which
  9. # provides free (as in beer) accounts, and I'm trying to make the
  10. # process of setting up a working server as trivial as possible.
  11. # Other dynamic DNS systems are available, and if you're using
  12. # something different then comment out the section within
  13. # argument_checks and the call to dynamic_dns_freedns.
  14. #
  15. # Prerequisites
  16. # =============
  17. #
  18. # cd ~/
  19. # wget http://freedombone.uk.to/debian-jessie-console-armhf-2014-08-13.tar.xz
  20. #
  21. # Verify it.
  22. #
  23. # sha256sum debian-jessie-console-armhf-2014-08-13.tar.xz
  24. # fc225cfb3c2dfad92cccafa97e92c3cd3db9d94f4771af8da364ef59609f43de
  25. #
  26. # Uncompress it.
  27. #
  28. # tar xJf debian-jessie-console-armhf-2014-08-13.tar.xz
  29. # cd debian-jessie-console-armhf-2014-08-13
  30. #
  31. # sudo apt-get install u-boot-tools dosfstools git-core kpartx wget parted
  32. # sudo ./setup_sdcard.sh --mmc /dev/sdX --dtb beaglebone
  33. #
  34. # When finished eject the micrtoSD then reinsert it
  35. #
  36. # sudo cp /media/$USER/BOOT/bbb-uEnv.txt /media/$USER/BOOT/uEnv.txt
  37. # sync
  38. #
  39. # Eject microSD, insert into BBB, attach USB cable between BBB and laptop.
  40. # On Ubuntu wait until you see the "connected" message.
  41. #
  42. # ssh-keygen -f "/home/$USER/.ssh/known_hosts" -R 192.168.7.2
  43. # ssh debian@192.168.7.2 (password "temppwd")
  44. # su (password "root")
  45. # passwd
  46. # adduser $MY_USERNAME
  47. # sed -i '/iface eth0 inet dhcp/a\iface eth0 inet static' /etc/network/interfaces
  48. # sed -i '/iface eth0 inet static/a\ dns-nameservers 213.73.91.35 85.214.20.141' /etc/network/interfaces
  49. # sed -i "/iface eth0 inet static/a\ gateway $MY_ROUTER_IP" /etc/network/interfaces
  50. # sed -i '/iface eth0 inet static/a\ netmask 255.255.255.0' /etc/network/interfaces
  51. # sed -i "/iface eth0 inet static/a\ address $MY_BBB_STATIC_IP" /etc/network/interfaces
  52. # sed -i '/iface usb0 inet static/,/ gateway 192.168.7.1/ s/^/#/' /etc/network/interfaces
  53. # shutdown now
  54. #
  55. # Connect BBB to router
  56. #
  57. # scp install-freedombone.sh $MY_USERNAME@$MY_BBB_STATIC_IP:/home/$MY_USERNAME
  58. # ssh $MY_USERNAME@$MY_BBB_STATIC_IP
  59. # su
  60. # ./install-freedombone.sh [DOMAIN_NAME] [MY_USERNAME]
  61. DOMAIN_NAME=$1
  62. MY_USERNAME=$2
  63. FREEDNS_SUBDOMAIN_CODE=$3
  64. SYSTEM_TYPE=$4
  65. SSH_PORT=2222
  66. KERNEL_VERSION="v3.15.10-bone7"
  67. USE_HWRNG="yes"
  68. INSTALLED_WITHIN_DOCKER="no"
  69. # If you want to run an encrypted mailing list specify its name here.
  70. # There should be no spaces in the name
  71. PRIVATE_MAILING_LIST=
  72. GPG_KEYSERVER="hkp://keys.gnupg.net"
  73. # optionally you can provide your exported GPG key pair here
  74. # Note that the private key file will be deleted after use
  75. # If these are unspecified then a new GPG key will be created
  76. MY_GPG_PUBLIC_KEY=
  77. MY_GPG_PRIVATE_KEY=
  78. # If you have existing mail within a Maildir
  79. # you can specify the directory here and the files
  80. # will be imported
  81. IMPORT_MAILDIR=
  82. # The Debian package repository to use.
  83. DEBIAN_REPO="ftp.de.debian.org"
  84. DEBIAN_VERSION="jessie"
  85. # Directory where source code is downloaded and compiled
  86. INSTALL_DIR=$HOME/build
  87. # device name for an attached usb drive
  88. USB_DRIVE=/dev/sda1
  89. export DEBIAN_FRONTEND=noninteractive
  90. # File which keeps track of what has already been installed
  91. COMPLETION_FILE=$HOME/freedombone-completed.txt
  92. if [ ! -f $COMPLETION_FILE ]; then
  93. touch $COMPLETION_FILE
  94. fi
  95. function argument_checks {
  96. SYNTAX='./install-freedombone.sh [domain] [username] [subdomain code]'
  97. if [ ! -d /home/$MY_USERNAME ]; then
  98. echo "There is no user '$MY_USERNAME' on the system. Use 'adduser $MY_USERNAME' to create the user."
  99. exit 1
  100. fi
  101. if [ ! $DOMAIN_NAME ]; then
  102. echo ''
  103. echo $SYNTAX
  104. echo 'Please specify your domain name'
  105. exit 2
  106. fi
  107. if [ ! $MY_USERNAME ]; then
  108. echo ''
  109. echo $SYNTAX
  110. echo 'Please specify your username'
  111. exit 3
  112. fi
  113. if [ ! $FREEDNS_SUBDOMAIN_CODE ]; then
  114. echo ''
  115. echo $SYNTAX
  116. echo 'Please specify the freedns subdomain code. To find it from '
  117. echo "https://freedns.afraid.org select 'Dynamic DNS', then 'quick "
  118. echo "cron example' and copy the code located between '?' and '=='."
  119. exit 4
  120. fi
  121. }
  122. function change_login_message {
  123. if grep -Fxq "change_login_message" $COMPLETION_FILE; then
  124. return
  125. fi
  126. echo '' > /etc/motd
  127. echo ".---. . . " >> /etc/motd
  128. echo "| | | " >> /etc/motd
  129. echo "|--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. " >> /etc/motd
  130. echo "| | (.-' (.-' ( | ( )| | | | )( )| | (.-' " >> /etc/motd
  131. echo "' ' --' --' -' - -' ' ' -' -' -' ' - --'" >> /etc/motd
  132. if [[ $SYSTEM_TYPE == "cloud" ]]; then
  133. echo ' .--.. . ' >> /etc/motd
  134. echo ' : | | ' >> /etc/motd
  135. echo ' | | .-. . . .-.| ' >> /etc/motd
  136. echo ' : |( )| |( | ' >> /etc/motd
  137. echo " --' - -' -- - -' -" >> /etc/motd
  138. fi
  139. if [[ $SYSTEM_TYPE == "chat" ]]; then
  140. echo ' .--.. . ' >> /etc/motd
  141. echo ' : | _|_ ' >> /etc/motd
  142. echo ' | |--. .-. | ' >> /etc/motd
  143. echo ' : | |( ) | ' >> /etc/motd
  144. echo " --'' - -' - -' " >> /etc/motd
  145. fi
  146. if [[ $SYSTEM_TYPE == "social" ]]; then
  147. echo ' .-. . ' >> /etc/motd
  148. echo ' ( ) o | ' >> /etc/motd
  149. echo ' -. .-. .-. . .-. | ' >> /etc/motd
  150. echo ' ( )( )( | ( ) | ' >> /etc/motd
  151. echo " -' -' -'-' - -' - - " >> /etc/motd
  152. fi
  153. if [[ $SYSTEM_TYPE == "email" || $SYSTEM_TYPE == "mailbox" ]]; then
  154. echo ' . . . . ' >> /etc/motd
  155. echo ' |\ /| o | | ' >> /etc/motd
  156. echo ' | \/ | .-. . | |.-. .-.-. ,- ' >> /etc/motd
  157. echo ' | |( ) | | | )( ) : ' >> /etc/motd
  158. echo ' ' ' -' --' - -' -' -'-' - ' >> /etc/motd
  159. fi
  160. echo '' >> /etc/motd
  161. echo ' Freedom in the Cloud' >> /etc/motd
  162. echo '' >> /etc/motd
  163. echo 'change_login_message' >> $COMPLETION_FILE
  164. }
  165. function search_for_attached_usb_drive {
  166. # If a USB drive is attached then search for email,
  167. # gpg, ssh keys and emacs configuration
  168. if grep -Fxq "search_for_attached_usb_drive" $COMPLETION_FILE; then
  169. return
  170. fi
  171. if [ -b $USB_DRIVE ]; then
  172. if [ ! -d /media/usb ]; then
  173. echo 'Mounting USB drive'
  174. mkdir /media/usb
  175. mount $USB_DRIVE /media/usb
  176. fi
  177. if [ -d /media/usb/Maildir ]; then
  178. echo 'Maildir found on USB drive'
  179. IMPORT_MAILDIR=/media/usb/Maildir
  180. fi
  181. if [ -d /media/usb/.gnupg ]; then
  182. echo 'Importing GPG keyring'
  183. cp -r /media/usb/.gnupg /home/$MY_USERNAME
  184. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  185. if [ -f /home/$MY_USERNAME/.gnupg/secring.gpg ]; then
  186. shred -zu /media/usb/.gnupg/secring.gpg
  187. shred -zu /media/usb/.gnupg/random_seed
  188. shred -zu /media/usb/.gnupg/trustdb.gpg
  189. rm -rf /media/usb/.gnupg
  190. else
  191. echo 'GPG files did not copy'
  192. exit 7
  193. fi
  194. fi
  195. if [ -f /media/usb/private_key.gpg ]; then
  196. echo 'GPG private key found on USB drive'
  197. MY_GPG_PRIVATE_KEY=/media/usb/private_key.gpg
  198. fi
  199. if [ -f /media/usb/public_key.gpg ]; then
  200. echo 'GPG public key found on USB drive'
  201. MY_GPG_PUBLIC_KEY=/media/usb/public_key.gpg
  202. fi
  203. if [ -d /media/usb/.ssh ]; then
  204. echo 'Importing ssh keys'
  205. cp -r /media/usb/.ssh /home/$MY_USERNAME
  206. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  207. # for security delete the ssh keys from the usb drive
  208. if [ -f /home/$MY_USERNAME/.ssh/id_rsa ]; then
  209. shred -zu /media/usb/.ssh/id_rsa
  210. shred -zu /media/usb/.ssh/id_rsa.pub
  211. shred -zu /media/usb/.ssh/known_hosts
  212. rm -rf /media/usb/.ssh
  213. else
  214. echo 'ssh files did not copy'
  215. exit 8
  216. fi
  217. fi
  218. if [ -f /media/usb/.emacs ]; then
  219. echo 'Importing .emacs file'
  220. cp -f /media/usb/.emacs /home/$MY_USERNAME/.emacs
  221. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs
  222. fi
  223. if [ -d /media/usb/.emacs.d ]; then
  224. echo 'Importing .emacs.d directory'
  225. cp -r /media/usb/.emacs.d /home/$MY_USERNAME
  226. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs.d
  227. fi
  228. if [ -d /media/usb/personal ]; then
  229. echo 'Importing personal directory'
  230. cp -r /media/usb/personal /home/$MY_USERNAME
  231. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/personal
  232. fi
  233. else
  234. if [ -d /media/usb ]; then
  235. umount /media/usb
  236. rm -rf /media/usb
  237. fi
  238. echo 'No USB drive attached'
  239. fi
  240. echo 'search_for_attached_usb_drive' >> $COMPLETION_FILE
  241. }
  242. function remove_proprietary_repos {
  243. if grep -Fxq "remove_proprietary_repos" $COMPLETION_FILE; then
  244. return
  245. fi
  246. sed -i 's/ non-free//g' /etc/apt/sources.list
  247. echo 'remove_proprietary_repos' >> $COMPLETION_FILE
  248. }
  249. function change_debian_repos {
  250. if grep -Fxq "change_debian_repos" $COMPLETION_FILE; then
  251. return
  252. fi
  253. rm -rf /var/lib/apt/lists/*
  254. apt-get clean
  255. sed -i "s/ftp.us.debian.org/$DEBIAN_REPO/g" /etc/apt/sources.list
  256. # ensure that there is a security repo
  257. if ! grep -q "security" /etc/apt/sources.list; then
  258. if grep -q "jessie" /etc/apt/sources.list; then
  259. echo "deb http://security.debian.org/ jessie/updates main contrib" >> /etc/apt/sources.list
  260. echo "#deb-src http://security.debian.org/ jessie/updates main contrib" >> /etc/apt/sources.list
  261. else
  262. if grep -q "wheezy" /etc/apt/sources.list; then
  263. echo "deb http://security.debian.org/ wheezy/updates main contrib" >> /etc/apt/sources.list
  264. echo "#deb-src http://security.debian.org/ wheezy/updates main contrib" >> /etc/apt/sources.list
  265. fi
  266. fi
  267. fi
  268. apt-get update
  269. apt-get -y --force-yes install apt-transport-https
  270. echo 'change_debian_repos' >> $COMPLETION_FILE
  271. }
  272. function initial_setup {
  273. if grep -Fxq "initial_setup" $COMPLETION_FILE; then
  274. return
  275. fi
  276. apt-get -y remove --purge apache*
  277. apt-get -y dist-upgrade
  278. apt-get -y install ca-certificates emacs24
  279. echo 'initial_setup' >> $COMPLETION_FILE
  280. }
  281. function install_editor {
  282. if grep -Fxq "install_editor" $COMPLETION_FILE; then
  283. return
  284. fi
  285. update-alternatives --set editor /usr/bin/emacs24
  286. echo 'install_editor' >> $COMPLETION_FILE
  287. }
  288. function enable_backports {
  289. if grep -Fxq "enable_backports" $COMPLETION_FILE; then
  290. return
  291. fi
  292. if ! grep -Fxq "deb http://$DEBIAN_REPO/debian jessie-backports main" /etc/apt/sources.list; then
  293. echo "deb http://$DEBIAN_REPO/debian jessie-backports main" >> /etc/apt/sources.list
  294. fi
  295. echo 'enable_backports' >> $COMPLETION_FILE
  296. }
  297. function update_the_kernel {
  298. if grep -Fxq "update_the_kernel" $COMPLETION_FILE; then
  299. return
  300. fi
  301. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  302. return
  303. fi
  304. cd /opt/scripts/tools
  305. ./update_kernel.sh --kernel $KERNEL_VERSION
  306. echo 'update_the_kernel' >> $COMPLETION_FILE
  307. }
  308. function enable_zram {
  309. if grep -Fxq "enable_zram" $COMPLETION_FILE; then
  310. return
  311. fi
  312. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  313. return
  314. fi
  315. if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
  316. echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
  317. fi
  318. echo '#!/bin/bash' > /etc/init.d/zram
  319. echo '### BEGIN INIT INFO' >> /etc/init.d/zram
  320. echo '# Provides: zram' >> /etc/init.d/zram
  321. echo '# Required-Start:' >> /etc/init.d/zram
  322. echo '# Required-Stop:' >> /etc/init.d/zram
  323. echo '# Default-Start: 2 3 4 5' >> /etc/init.d/zram
  324. echo '# Default-Stop: 0 1 6' >> /etc/init.d/zram
  325. echo '# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)' >> /etc/init.d/zram
  326. echo '# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram' >> /etc/init.d/zram
  327. echo '### END INIT INFO' >> /etc/init.d/zram
  328. echo 'start() {' >> /etc/init.d/zram
  329. echo ' # get the number of CPUs' >> /etc/init.d/zram
  330. echo ' num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
  331. echo ' # if something goes wrong, assume we have 1' >> /etc/init.d/zram
  332. echo ' [ "$num_cpus" != 0 ] || num_cpus=1' >> /etc/init.d/zram
  333. echo ' # set decremented number of CPUs' >> /etc/init.d/zram
  334. echo ' decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
  335. echo ' # get the amount of memory in the machine' >> /etc/init.d/zram
  336. echo ' mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching "[[:digit:]]+")' >> /etc/init.d/zram
  337. echo ' mem_total=$((mem_total_kb * 1024))' >> /etc/init.d/zram
  338. echo ' # load dependency modules' >> /etc/init.d/zram
  339. echo ' modprobe zram num_devices=$num_cpus' >> /etc/init.d/zram
  340. echo ' # initialize the devices' >> /etc/init.d/zram
  341. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  342. echo ' echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize' >> /etc/init.d/zram
  343. echo ' done' >> /etc/init.d/zram
  344. echo ' # Creating swap filesystems' >> /etc/init.d/zram
  345. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  346. echo ' mkswap /dev/zram$i' >> /etc/init.d/zram
  347. echo ' done' >> /etc/init.d/zram
  348. echo ' # Switch the swaps on' >> /etc/init.d/zram
  349. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  350. echo ' swapon -p 100 /dev/zram$i' >> /etc/init.d/zram
  351. echo ' done' >> /etc/init.d/zram
  352. echo '}' >> /etc/init.d/zram
  353. echo 'stop() {' >> /etc/init.d/zram
  354. echo ' # get the number of CPUs' >> /etc/init.d/zram
  355. echo ' num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
  356. echo ' # set decremented number of CPUs' >> /etc/init.d/zram
  357. echo ' decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
  358. echo ' # Switching off swap' >> /etc/init.d/zram
  359. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  360. echo ' if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then' >> /etc/init.d/zram
  361. echo ' swapoff /dev/zram$i' >> /etc/init.d/zram
  362. echo ' sleep 1' >> /etc/init.d/zram
  363. echo ' fi' >> /etc/init.d/zram
  364. echo ' done' >> /etc/init.d/zram
  365. echo ' sleep 1' >> /etc/init.d/zram
  366. echo ' rmmod zram' >> /etc/init.d/zram
  367. echo '}' >> /etc/init.d/zram
  368. echo 'case "$1" in' >> /etc/init.d/zram
  369. echo ' start)' >> /etc/init.d/zram
  370. echo ' start' >> /etc/init.d/zram
  371. echo ' ;;' >> /etc/init.d/zram
  372. echo ' stop)' >> /etc/init.d/zram
  373. echo ' stop' >> /etc/init.d/zram
  374. echo ' ;;' >> /etc/init.d/zram
  375. echo ' restart)' >> /etc/init.d/zram
  376. echo ' stop' >> /etc/init.d/zram
  377. echo ' sleep 3' >> /etc/init.d/zram
  378. echo ' start' >> /etc/init.d/zram
  379. echo ' ;;' >> /etc/init.d/zram
  380. echo ' *)' >> /etc/init.d/zram
  381. echo ' echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/zram
  382. echo ' RETVAL=1' >> /etc/init.d/zram
  383. echo 'esac' >> /etc/init.d/zram
  384. echo 'exit $RETVAL' >> /etc/init.d/zram
  385. chmod +x /etc/init.d/zram
  386. update-rc.d zram defaults
  387. echo 'enable_zram' >> $COMPLETION_FILE
  388. }
  389. function random_number_generator {
  390. if grep -Fxq "random_number_generator" $COMPLETION_FILE; then
  391. return
  392. fi
  393. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  394. # it is assumed that docker uses the random number
  395. # generator of the host system
  396. return
  397. fi
  398. if [[ $USE_HWRNG == "yes" ]]; then
  399. apt-get -y --force-yes install rng-tools
  400. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' /etc/default/rng-tools
  401. else
  402. apt-get -y --force-yes install haveged
  403. fi
  404. echo 'random_number_generator' >> $COMPLETION_FILE
  405. }
  406. function configure_ssh {
  407. if grep -Fxq "configure_ssh" $COMPLETION_FILE; then
  408. return
  409. fi
  410. sed -i "s/Port 22/Port $SSH_PORT/g" /etc/ssh/sshd_config
  411. sed -i 's/PermitRootLogin without-password/PermitRootLogin no/g' /etc/ssh/sshd_config
  412. sed -i 's/X11Forwarding yes/X11Forwarding no/g' /etc/ssh/sshd_config
  413. sed -i 's/ServerKeyBits 1024/ServerKeyBits 4096/g' /etc/ssh/sshd_config
  414. sed -i 's/TCPKeepAlive yes/TCPKeepAlive no/g' /etc/ssh/sshd_config
  415. sed -i 's|HostKey /etc/ssh/ssh_host_dsa_key|#HostKey /etc/ssh/ssh_host_dsa_key|g' /etc/ssh/sshd_config
  416. sed -i 's|HostKey /etc/ssh/ssh_host_ecdsa_key|#HostKey /etc/ssh/ssh_host_ecdsa_key|g' /etc/ssh/sshd_config
  417. echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config
  418. echo 'ClientAliveCountMax 3' >> /etc/ssh/sshd_config
  419. echo 'Ciphers aes256-ctr,aes128-ctr' >> /etc/ssh/sshd_config
  420. echo 'MACs hmac-sha2-512,hmac-sha2-256,hmac-ripemd160
  421. KexAlgorithms diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1' >> /etc/ssh/sshd_config
  422. apt-get -y --force-yes install fail2ban
  423. echo 'configure_ssh' >> $COMPLETION_FILE
  424. echo ''
  425. echo ''
  426. echo ' *** Rebooting to initialise ssh settings and random number generator ***'
  427. echo ''
  428. echo " *** Reconnect via ssh on port $SSH_PORT, then run this script again ***"
  429. echo ''
  430. reboot
  431. }
  432. function regenerate_ssh_keys {
  433. if grep -Fxq "regenerate_ssh_keys" $COMPLETION_FILE; then
  434. return
  435. fi
  436. rm -f /etc/ssh/ssh_host_*
  437. dpkg-reconfigure openssh-server
  438. service ssh restart
  439. echo 'regenerate_ssh_keys' >> $COMPLETION_FILE
  440. }
  441. function configure_dns {
  442. if grep -Fxq "configure_dns" $COMPLETION_FILE; then
  443. return
  444. fi
  445. echo 'domain localdomain' > /etc/resolv.conf
  446. echo 'search localdomain' >> /etc/resolv.conf
  447. echo 'nameserver 213.73.91.35' >> /etc/resolv.conf
  448. echo 'nameserver 85.214.20.141' >> /etc/resolv.conf
  449. echo 'configure_dns' >> $COMPLETION_FILE
  450. }
  451. function set_your_domain_name {
  452. if grep -Fxq "set_your_domain_name" $COMPLETION_FILE; then
  453. return
  454. fi
  455. echo "$DOMAIN_NAME" > /etc/hostname
  456. hostname $DOMAIN_NAME
  457. sed -i "s/127.0.1.1 arm/127.0.1.1 $DOMAIN_NAME/g" /etc/hosts
  458. echo "127.0.1.1 $DOMAIN_NAME" >> /etc/hosts
  459. echo 'set_your_domain_name' >> $COMPLETION_FILE
  460. }
  461. function time_synchronisation {
  462. if grep -Fxq "time_synchronisation" $COMPLETION_FILE; then
  463. return
  464. fi
  465. apt-get -y --force-yes install tlsdate
  466. apt-get -y remove ntpdate
  467. echo '#!/bin/bash' > /usr/bin/updatedate
  468. echo 'TIMESOURCE=google.com' >> /usr/bin/updatedate
  469. echo 'TIMESOURCE2=www.ptb.de' >> /usr/bin/updatedate
  470. echo 'LOGFILE=/var/log/tlsdate.log' >> /usr/bin/updatedate
  471. echo 'TIMEOUT=5' >> /usr/bin/updatedate
  472. echo "EMAIL=$MY_USERNAME@$DOMAIN_NAME" >> /usr/bin/updatedate
  473. echo '# File which contains the previous date as a number' >> /usr/bin/updatedate
  474. echo 'BEFORE_DATE_FILE=/var/log/tlsdateprevious.txt' >> /usr/bin/updatedate
  475. echo '# File which contains the previous date as a string' >> /usr/bin/updatedate
  476. echo 'BEFORE_FULLDATE_FILE=/var/log/tlsdate.txt' >> /usr/bin/updatedate
  477. echo 'DATE_BEFORE=$(date)' >> /usr/bin/updatedate
  478. echo 'BEFORE=$(date -d "$Y-$M-$D" "+%s")' >> /usr/bin/updatedate
  479. echo 'BACKWARDS_BETWEEN=0' >> /usr/bin/updatedate
  480. echo '# If the date was previously set' >> /usr/bin/updatedate
  481. echo 'if [[ -f "$BEFORE_DATE_FILE" ]]; then' >> /usr/bin/updatedate
  482. echo ' BEFORE_FILE=$(cat $BEFORE_DATE_FILE)' >> /usr/bin/updatedate
  483. echo ' BEFORE_FULLDATE=$(cat $BEFORE_FULLDATE_FILE)' >> /usr/bin/updatedate
  484. echo ' # is the date going backwards?' >> /usr/bin/updatedate
  485. echo ' if (( BEFORE_FILE > BEFORE )); then' >> /usr/bin/updatedate
  486. echo ' echo -n "Date went backwards between tlsdate updates. " >> $LOGFILE' >> /usr/bin/updatedate
  487. echo ' echo -n "$BEFORE_FILE > $BEFORE, " >> $LOGFILE' >> /usr/bin/updatedate
  488. echo ' echo "$BEFORE_FULLDATE > $DATE_BEFORE" >> $LOGFILE' >> /usr/bin/updatedate
  489. echo ' # Send a warning email' > /usr/bin/updatedate
  490. echo ' echo $(tail $LOGFILE -n 2) | mail -s "tlsdate anomaly" $EMAIL' >> /usr/bin/updatedate
  491. echo ' # Try another time source' >> /usr/bin/updatedate
  492. echo ' TIMESOURCE=$TIMESOURCE2' >> /usr/bin/updatedate
  493. echo ' # try running without any parameters' >> /usr/bin/updatedate
  494. echo ' tlsdate >> $LOGFILE' >> /usr/bin/updatedate
  495. echo ' BACKWARDS_BETWEEN=1' >> /usr/bin/updatedate
  496. echo ' fi' >> /usr/bin/updatedate
  497. echo 'fi' >> /usr/bin/updatedate
  498. echo '# Set the date' >> /usr/bin/updatedate
  499. echo '/usr/bin/timeout $TIMEOUT tlsdate -l -t -H $TIMESOURCE -p 443 >> $LOGFILE' >> /usr/bin/updatedate
  500. echo 'DATE_AFTER=$(date)' >> /usr/bin/updatedate
  501. echo 'AFTER=$(date -d "$Y-$M-$D" '+%s')' >> /usr/bin/updatedate
  502. echo '# After setting the date did it go backwards?' >> /usr/bin/updatedate
  503. echo 'if (( AFTER < BEFORE )); then' >> /usr/bin/updatedate
  504. echo ' echo "Incorrect date: $DATE_BEFORE -> $DATE_AFTER" >> $LOGFILE' >> /usr/bin/updatedate
  505. echo ' # Send a warning email' >> /usr/bin/updatedate
  506. echo ' echo $(tail $LOGFILE -n 2) | mail -s "tlsdate anomaly" $EMAIL' >> /usr/bin/updatedate
  507. echo ' # Try resetting the date from another time source' >> /usr/bin/updatedate
  508. echo ' /usr/bin/timeout $TIMEOUT tlsdate -l -t -H $TIMESOURCE2 -p 443 >> $LOGFILE' >> /usr/bin/updatedate
  509. echo ' DATE_AFTER=$(date)' >> /usr/bin/updatedate
  510. echo ' AFTER=$(date -d "$Y-$M-$D" "+%s")' >> /usr/bin/updatedate
  511. echo 'else' >> /usr/bin/updatedate
  512. echo ' echo -n $TIMESOURCE >> $LOGFILE' >> /usr/bin/updatedate
  513. echo ' if [[ -f "$BEFORE_DATE_FILE" ]]; then' >> /usr/bin/updatedate
  514. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  515. echo ' echo -n $BEFORE_FILE >> $LOGFILE' >> /usr/bin/updatedate
  516. echo ' fi' >> /usr/bin/updatedate
  517. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  518. echo ' echo -n $BEFORE >> $LOGFILE' >> /usr/bin/updatedate
  519. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  520. echo ' echo -n $AFTER >> $LOGFILE' >> /usr/bin/updatedate
  521. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  522. echo ' echo $DATE_AFTER >> $LOGFILE' >> /usr/bin/updatedate
  523. echo 'fi' >> /usr/bin/updatedate
  524. echo '# Log the last date' >> /usr/bin/updatedate
  525. echo 'if [ BACKWARDS_BETWEEN == 0 ]; then' >> /usr/bin/updatedate
  526. echo ' echo "$AFTER" > $BEFORE_DATE_FILE' >> /usr/bin/updatedate
  527. echo ' echo "$DATE_AFTER" > $BEFORE_FULLDATE_FILE' >> /usr/bin/updatedate
  528. echo ' exit 0' >> /usr/bin/updatedate
  529. echo 'else' >> /usr/bin/updatedate
  530. echo ' exit 1' >> /usr/bin/updatedate
  531. echo 'fi' >> /usr/bin/updatedate
  532. chmod +x /usr/bin/updatedate
  533. echo '*/15 * * * * root /usr/bin/updatedate' >> /etc/crontab
  534. service cron restart
  535. echo '#!/bin/bash' > /etc/init.d/tlsdate
  536. echo '# /etc/init.d/tlsdate' >> /etc/init.d/tlsdate
  537. echo '### BEGIN INIT INFO' >> /etc/init.d/tlsdate
  538. echo '# Provides: tlsdate' >> /etc/init.d/tlsdate
  539. echo '# Required-Start: $remote_fs $syslog' >> /etc/init.d/tlsdate
  540. echo '# Required-Stop: $remote_fs $syslog' >> /etc/init.d/tlsdate
  541. echo '# Default-Start: 2 3 4 5' >> /etc/init.d/tlsdate
  542. echo '# Default-Stop: 0 1 6' >> /etc/init.d/tlsdate
  543. echo '# Short-Description: Initially calls tlsdate with the timewarp option' >> /etc/init.d/tlsdate
  544. echo '# Description: Initially calls tlsdate with the timewarp option' >> /etc/init.d/tlsdate
  545. echo '### END INIT INFO' >> /etc/init.d/tlsdate
  546. echo '# Author: Bob Mottram <bob@robotics.uk.to>' >> /etc/init.d/tlsdate
  547. echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin"' >> /etc/init.d/tlsdate
  548. echo 'LOGFILE="/var/log/tlsdate.log"' >> /etc/init.d/tlsdate
  549. echo 'TLSDATECOMMAND="tlsdate --timewarp -l -H www.ptb.de -p 443 >> $LOGFILE"' >> /etc/init.d/tlsdate
  550. echo '#Start-Stop here' >> /etc/init.d/tlsdate
  551. echo 'case "$1" in' >> /etc/init.d/tlsdate
  552. echo ' start)' >> /etc/init.d/tlsdate
  553. echo ' echo "tlsdate started"' >> /etc/init.d/tlsdate
  554. echo ' $TLSDATECOMMAND' >> /etc/init.d/tlsdate
  555. echo ' ;;' >> /etc/init.d/tlsdate
  556. echo ' stop)' >> /etc/init.d/tlsdate
  557. echo ' echo "tlsdate stopped"' >> /etc/init.d/tlsdate
  558. echo ' ;;' >> /etc/init.d/tlsdate
  559. echo ' restart)' >> /etc/init.d/tlsdate
  560. echo ' echo "tlsdate restarted"' >> /etc/init.d/tlsdate
  561. echo ' $TLSDATECOMMAND' >> /etc/init.d/tlsdate
  562. echo ' ;;' >> /etc/init.d/tlsdate
  563. echo ' *)' >> /etc/init.d/tlsdate
  564. echo ' echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/tlsdate
  565. echo ' exit 1' >> /etc/init.d/tlsdate
  566. echo ' ;;' >> /etc/init.d/tlsdate
  567. echo 'esac' >> /etc/init.d/tlsdate
  568. echo 'exit 0' >> /etc/init.d/tlsdate
  569. chmod +x /etc/init.d/tlsdate
  570. update-rc.d tlsdate defaults
  571. echo 'time_synchronisation' >> $COMPLETION_FILE
  572. }
  573. function configure_firewall {
  574. if grep -Fxq "configure_firewall" $COMPLETION_FILE; then
  575. return
  576. fi
  577. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  578. # docker does its own firewalling
  579. return
  580. fi
  581. iptables -P INPUT ACCEPT
  582. ip6tables -P INPUT ACCEPT
  583. iptables -F
  584. ip6tables -F
  585. iptables -X
  586. ip6tables -X
  587. iptables -P INPUT DROP
  588. ip6tables -P INPUT DROP
  589. iptables -A INPUT -i lo -j ACCEPT
  590. iptables -A INPUT -i eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  591. echo 'configure_firewall' >> $COMPLETION_FILE
  592. }
  593. function save_firewall_settings {
  594. iptables-save > /etc/firewall.conf
  595. ip6tables-save > /etc/firewall6.conf
  596. printf '#!/bin/sh\n' > /etc/network/if-up.d/iptables
  597. printf 'iptables-restore < /etc/firewall.conf\n' >> /etc/network/if-up.d/iptables
  598. printf 'ip6tables-restore < /etc/firewall6.conf\n' >> /etc/network/if-up.d/iptables
  599. chmod +x /etc/network/if-up.d/iptables
  600. }
  601. function configure_firewall_for_dns {
  602. if grep -Fxq "configure_firewall_for_dns" $COMPLETION_FILE; then
  603. return
  604. fi
  605. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  606. # docker does its own firewalling
  607. return
  608. fi
  609. iptables -A INPUT -i eth0 -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT
  610. save_firewall_settings
  611. echo 'configure_firewall_for_dns' >> $COMPLETION_FILE
  612. }
  613. function configure_firewall_for_ftp {
  614. if grep -Fxq "configure_firewall_for_ftp" $COMPLETION_FILE; then
  615. return
  616. fi
  617. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  618. # docker does its own firewalling
  619. return
  620. fi
  621. iptables -I INPUT -i eth0 -p tcp --dport 1024:65535 --sport 20:21 -j ACCEPT
  622. save_firewall_settings
  623. echo 'configure_firewall_for_ftp' >> $COMPLETION_FILE
  624. }
  625. function configure_firewall_for_web_access {
  626. if grep -Fxq "configure_firewall_for_web_access" $COMPLETION_FILE; then
  627. return
  628. fi
  629. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  630. # docker does its own firewalling
  631. return
  632. fi
  633. iptables -A INPUT -i eth0 -p tcp --dport 32768:61000 --sport 80 -j ACCEPT
  634. iptables -A INPUT -i eth0 -p tcp --dport 32768:61000 --sport 443 -j ACCEPT
  635. save_firewall_settings
  636. echo 'configure_firewall_for_web_access' >> $COMPLETION_FILE
  637. }
  638. function configure_firewall_for_web_server {
  639. if grep -Fxq "configure_firewall_for_web_server" $COMPLETION_FILE; then
  640. return
  641. fi
  642. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  643. # docker does its own firewalling
  644. return
  645. fi
  646. iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
  647. iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
  648. save_firewall_settings
  649. echo 'configure_firewall_for_web_server' >> $COMPLETION_FILE
  650. }
  651. function configure_firewall_for_ssh {
  652. if grep -Fxq "configure_firewall_for_ssh" $COMPLETION_FILE; then
  653. return
  654. fi
  655. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  656. # docker does its own firewalling
  657. return
  658. fi
  659. iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
  660. iptables -A INPUT -i eth0 -p tcp --dport $SSH_PORT -j ACCEPT
  661. save_firewall_settings
  662. echo 'configure_firewall_for_ssh' >> $COMPLETION_FILE
  663. }
  664. function configure_firewall_for_git {
  665. if grep -Fxq "configure_firewall_for_git" $COMPLETION_FILE; then
  666. return
  667. fi
  668. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  669. # docker does its own firewalling
  670. return
  671. fi
  672. iptables -A INPUT -i eth0 -p tcp --dport 9418 -j ACCEPT
  673. save_firewall_settings
  674. echo 'configure_firewall_for_git' >> $COMPLETION_FILE
  675. }
  676. function configure_firewall_for_email {
  677. if grep -Fxq "configure_firewall_for_email" $COMPLETION_FILE; then
  678. return
  679. fi
  680. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  681. # docker does its own firewalling
  682. return
  683. fi
  684. iptables -A INPUT -i eth0 -p tcp --dport 25 -j ACCEPT
  685. iptables -A INPUT -i eth0 -p tcp --dport 587 -j ACCEPT
  686. iptables -A INPUT -i eth0 -p tcp --dport 465 -j ACCEPT
  687. iptables -A INPUT -i eth0 -p tcp --dport 993 -j ACCEPT
  688. save_firewall_settings
  689. echo 'configure_firewall_for_email' >> $COMPLETION_FILE
  690. }
  691. function configure_internet_protocol {
  692. if grep -Fxq "configure_internet_protocol" $COMPLETION_FILE; then
  693. return
  694. fi
  695. sed -i "s/#net.ipv4.tcp_syncookies=1/net.ipv4.tcp_syncookies=1/g" /etc/sysctl.conf
  696. sed -i "s/#net.ipv4.conf.all.accept_redirects = 0/net.ipv4.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  697. sed -i "s/#net.ipv6.conf.all.accept_redirects = 0/net.ipv6.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  698. sed -i "s/#net.ipv4.conf.all.send_redirects = 0/net.ipv4.conf.all.send_redirects = 0/g" /etc/sysctl.conf
  699. sed -i "s/#net.ipv4.conf.all.accept_source_route = 0/net.ipv4.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  700. sed -i "s/#net.ipv6.conf.all.accept_source_route = 0/net.ipv6.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  701. sed -i "s/#net.ipv4.conf.default.rp_filter=1/net.ipv4.conf.default.rp_filter=1/g" /etc/sysctl.conf
  702. sed -i "s/#net.ipv4.conf.all.rp_filter=1/net.ipv4.conf.all.rp_filter=1/g" /etc/sysctl.conf
  703. sed -i "s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=0/g" /etc/sysctl.conf
  704. sed -i "s/#net.ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=0/g" /etc/sysctl.conf
  705. echo '# ignore pings' >> /etc/sysctl.conf
  706. echo 'net.ipv4.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  707. echo 'net.ipv6.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  708. echo '# disable ipv6' >> /etc/sysctl.conf
  709. echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
  710. echo 'net.ipv4.tcp_synack_retries = 2' >> /etc/sysctl.conf
  711. echo 'net.ipv4.tcp_syn_retries = 1' >> /etc/sysctl.conf
  712. echo '# keepalive' >> /etc/sysctl.conf
  713. echo 'net.ipv4.tcp_keepalive_probes = 9' >> /etc/sysctl.conf
  714. echo 'net.ipv4.tcp_keepalive_intvl = 75' >> /etc/sysctl.conf
  715. echo 'net.ipv4.tcp_keepalive_time = 7200' >> /etc/sysctl.conf
  716. echo 'configure_internet_protocol' >> $COMPLETION_FILE
  717. }
  718. function script_to_make_self_signed_certificates {
  719. if grep -Fxq "script_to_make_self_signed_certificates" $COMPLETION_FILE; then
  720. return
  721. fi
  722. echo '#!/bin/bash' > /usr/bin/makecert
  723. echo 'HOSTNAME=$1' >> /usr/bin/makecert
  724. echo 'COUNTRY_CODE="US"' >> /usr/bin/makecert
  725. echo 'AREA="Free Speech Zone"' >> /usr/bin/makecert
  726. echo 'LOCATION="Freedomville"' >> /usr/bin/makecert
  727. echo 'ORGANISATION="Freedombone"' >> /usr/bin/makecert
  728. echo 'UNIT="Freedombone Unit"' >> /usr/bin/makecert
  729. echo 'if ! which openssl > /dev/null ;then' >> /usr/bin/makecert
  730. echo ' echo "$0: openssl is not installed, exiting" 1>&2' >> /usr/bin/makecert
  731. echo ' exit 1' >> /usr/bin/makecert
  732. echo 'fi' >> /usr/bin/makecert
  733. echo 'openssl req -x509 -nodes -days 3650 -sha256 -subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" -newkey rsa:4096 -keyout /etc/ssl/private/$HOSTNAME.key -out /etc/ssl/certs/$HOSTNAME.crt' >> /usr/bin/makecert
  734. echo 'openssl dhparam -check -text -5 1024 -out /etc/ssl/certs/$HOSTNAME.dhparam' >> /usr/bin/makecert
  735. echo 'chmod 400 /etc/ssl/private/$HOSTNAME.key' >> /usr/bin/makecert
  736. echo 'chmod 640 /etc/ssl/certs/$HOSTNAME.crt' >> /usr/bin/makecert
  737. echo 'chmod 640 /etc/ssl/certs/$HOSTNAME.dhparam' >> /usr/bin/makecert
  738. echo 'if [ -f /etc/init.d/nginx ]; then' >> /usr/bin/makecert
  739. echo ' /etc/init.d/nginx reload' >> /usr/bin/makecert
  740. echo 'fi' >> /usr/bin/makecert
  741. echo '# add the public certificate to a separate directory' >> /usr/bin/makecert
  742. echo '# so that we can redistribute it easily' >> /usr/bin/makecert
  743. echo 'if [ ! -d /etc/ssl/mycerts ]; then' >> /usr/bin/makecert
  744. echo ' mkdir /etc/ssl/mycerts' >> /usr/bin/makecert
  745. echo 'fi' >> /usr/bin/makecert
  746. echo 'cp /etc/ssl/certs/$HOSTNAME.crt /etc/ssl/mycerts' >> /usr/bin/makecert
  747. echo '# Create a bundle of your certificates' >> /usr/bin/makecert
  748. echo 'cat /etc/ssl/mycerts/*.crt > /etc/ssl/freedombone-bundle.crt' >> /usr/bin/makecert
  749. echo 'tar -czvf /etc/ssl/freedombone-certs.tar.gz /etc/ssl/mycerts/*.crt' >> /usr/bin/makecert
  750. chmod +x /usr/bin/makecert
  751. echo 'script_to_make_self_signed_certificates' >> $COMPLETION_FILE
  752. }
  753. function configure_email {
  754. if grep -Fxq "configure_email" $COMPLETION_FILE; then
  755. return
  756. fi
  757. apt-get -y remove postfix
  758. apt-get -y --force-yes install exim4 sasl2-bin swaks libnet-ssleay-perl procmail
  759. echo 'dc_eximconfig_configtype="internet"' > /etc/exim4/update-exim4.conf.conf
  760. echo "dc_other_hostnames='$DOMAIN_NAME'" >> /etc/exim4/update-exim4.conf.conf
  761. echo "dc_local_interfaces=''" >> /etc/exim4/update-exim4.conf.conf
  762. echo "dc_readhost=''" >> /etc/exim4/update-exim4.conf.conf
  763. echo "dc_relay_domains=''" >> /etc/exim4/update-exim4.conf.conf
  764. echo "dc_minimaldns='false'" >> /etc/exim4/update-exim4.conf.conf
  765. echo "dc_relay_nets='192.168.1.0/24'" >> /etc/exim4/update-exim4.conf.conf
  766. echo "dc_smarthost=''" >> /etc/exim4/update-exim4.conf.conf
  767. echo "CFILEMODE='644'" >> /etc/exim4/update-exim4.conf.conf
  768. echo "dc_use_split_config='false'" >> /etc/exim4/update-exim4.conf.conf
  769. echo "dc_hide_mailname=''" >> /etc/exim4/update-exim4.conf.conf
  770. echo "dc_mailname_in_oh='true'" >> /etc/exim4/update-exim4.conf.conf
  771. echo "dc_localdelivery='maildir_home'" >> /etc/exim4/update-exim4.conf.conf
  772. update-exim4.conf
  773. sed -i "s/START=no/START=yes/g" /etc/default/saslauthd
  774. /etc/init.d/saslauthd start
  775. # make a tls certificate for email
  776. makecert exim
  777. mv /etc/ssl/private/exim.key /etc/exim4
  778. mv /etc/ssl/certs/exim.crt /etc/exim4
  779. mv /etc/ssl/certs/exim.dhparam /etc/exim4
  780. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  781. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  782. sed -i '/login_saslauthd_server/,/.endif/ s/# *//' /etc/exim4/exim4.conf.template
  783. sed -i "/.ifdef MAIN_HARDCODE_PRIMARY_HOSTNAME/i\MAIN_HARDCODE_PRIMARY_HOSTNAME = $DOMAIN_NAME\nMAIN_TLS_ENABLE = true" /etc/exim4/exim4.conf.template
  784. sed -i "s|SMTPLISTENEROPTIONS=''|SMTPLISTENEROPTIONS='-oX 465:25:587 -oP /var/run/exim4/exim.pid'|g" /etc/default/exim4
  785. if ! grep -q "tls_on_connect_ports=465" /etc/exim4/exim4.conf.template; then
  786. sed -i '/SSL configuration for exim/i\tls_on_connect_ports=465' /etc/exim4/exim4.conf.template
  787. fi
  788. adduser $MY_USERNAME sasl
  789. addgroup Debian-exim sasl
  790. /etc/init.d/exim4 restart
  791. if [ ! -d /etc/skel/Maildir ]; then
  792. mkdir -m 700 /etc/skel/Maildir
  793. mkdir -m 700 /etc/skel/Maildir/Sent
  794. mkdir -m 700 /etc/skel/Maildir/Sent/tmp
  795. mkdir -m 700 /etc/skel/Maildir/Sent/cur
  796. mkdir -m 700 /etc/skel/Maildir/Sent/new
  797. mkdir -m 700 /etc/skel/Maildir/.learn-spam
  798. mkdir -m 700 /etc/skel/Maildir/.learn-spam/cur
  799. mkdir -m 700 /etc/skel/Maildir/.learn-spam/new
  800. mkdir -m 700 /etc/skel/Maildir/.learn-spam/tmp
  801. mkdir -m 700 /etc/skel/Maildir/.learn-ham
  802. mkdir -m 700 /etc/skel/Maildir/.learn-ham/cur
  803. mkdir -m 700 /etc/skel/Maildir/.learn-ham/new
  804. mkdir -m 700 /etc/skel/Maildir/.learn-ham/tmp
  805. ln -s /etc/skel/Maildir/.learn-spam /etc/skel/Maildir/spam
  806. ln -s /etc/skel/Maildir/.learn-ham /etc/skel/Maildir/ham
  807. fi
  808. if [ ! -d /home/$MY_USERNAME/Maildir ]; then
  809. mkdir -m 700 /home/$MY_USERNAME/Maildir
  810. mkdir -m 700 /home/$MY_USERNAME/Maildir/cur
  811. mkdir -m 700 /home/$MY_USERNAME/Maildir/tmp
  812. mkdir -m 700 /home/$MY_USERNAME/Maildir/new
  813. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent
  814. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent/cur
  815. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent/tmp
  816. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent/new
  817. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam
  818. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam/cur
  819. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam/new
  820. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam/tmp
  821. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham
  822. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham/cur
  823. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham/new
  824. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham/tmp
  825. ln -s /home/$MY_USERNAME/Maildir/.learn-spam /home/$MY_USERNAME/Maildir/spam
  826. ln -s /home/$MY_USERNAME/Maildir/.learn-ham /home/$MY_USERNAME/Maildir/ham
  827. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/Maildir
  828. fi
  829. echo 'configure_email' >> $COMPLETION_FILE
  830. }
  831. function spam_filtering {
  832. # NOTE: spamassassin installation currently doesn't work, sa-compile fails with a make error 23/09/2014
  833. if grep -Fxq "spam_filtering" $COMPLETION_FILE; then
  834. return
  835. fi
  836. apt-get -y --force-yes install exim4-daemon-heavy
  837. apt-get -y --force-yes install spamassassin
  838. sa-update -v
  839. sed -i 's/ENABLED=0/ENABLED=1/g' /etc/default/spamassassin
  840. sed -i 's/# spamd_address = 127.0.0.1 783/spamd_address = 127.0.0.1 783/g' /etc/exim4/exim4.conf.template
  841. # This configuration is based on https://wiki.debian.org/DebianSpamAssassin
  842. sed -i 's/local_parts = postmaster/local_parts = postmaster:abuse/g' /etc/exim4/conf.d/acl/30_exim4-config_check_rcpt
  843. sed -i '/domains = +local_domains : +relay_to_domains/a\ set acl_m0 = rfcnames' /etc/exim4/conf.d/acl/30_exim4-config_check_rcpt
  844. sed -i 's/accept/accept condition = ${if eq{$acl_m0}{rfcnames} {1}{0}}/g' /etc/exim4/conf.d/acl/40_exim4-config_check_data
  845. echo 'warn message = X-Spam-Score: $spam_score ($spam_bar)' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  846. echo ' spam = nobody:true' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  847. echo 'warn message = X-Spam-Flag: YES' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  848. echo ' spam = nobody' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  849. echo 'warn message = X-Spam-Report: $spam_report' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  850. echo ' spam = nobody' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  851. echo '# reject spam at high scores (> 12)' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  852. echo 'deny message = This message scored $spam_score spam points.' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  853. echo ' spam = nobody:true' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  854. echo ' condition = ${if >{$spam_score_int}{120}{1}{0}}' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  855. # procmail configuration
  856. echo 'MAILDIR=$HOME/Maildir' > /home/$MY_USERNAME/.procmailrc
  857. echo 'DEFAULT=$MAILDIR/' >> /home/$MY_USERNAME/.procmailrc
  858. echo 'LOGFILE=$HOME/log/procmail.log' >> /home/$MY_USERNAME/.procmailrc
  859. echo 'LOGABSTRACT=all' >> /home/$MY_USERNAME/.procmailrc
  860. echo '# get spamassassin to check emails' >> /home/$MY_USERNAME/.procmailrc
  861. echo ':0fw: .spamassassin.lock' >> /home/$MY_USERNAME/.procmailrc
  862. echo ' * < 256000' >> /home/$MY_USERNAME/.procmailrc
  863. echo '| spamc' >> /home/$MY_USERNAME/.procmailrc
  864. echo '# strong spam are discarded' >> /home/$MY_USERNAME/.procmailrc
  865. echo ':0' >> /home/$MY_USERNAME/.procmailrc
  866. echo ' * ^X-Spam-Level: \*\*\*\*\*\*' >> /home/$MY_USERNAME/.procmailrc
  867. echo '/dev/null' >> /home/$MY_USERNAME/.procmailrc
  868. echo '# weak spam are kept just in case - clear this out every now and then' >> /home/$MY_USERNAME/.procmailrc
  869. echo ':0' >> /home/$MY_USERNAME/.procmailrc
  870. echo ' * ^X-Spam-Level: \*\*\*\*\*' >> /home/$MY_USERNAME/.procmailrc
  871. echo '.0-spam/' >> /home/$MY_USERNAME/.procmailrc
  872. echo '# otherwise, marginal spam goes here for revision' >> /home/$MY_USERNAME/.procmailrc
  873. echo ':0' >> /home/$MY_USERNAME/.procmailrc
  874. echo ' * ^X-Spam-Level: \*\*' >> /home/$MY_USERNAME/.procmailrc
  875. echo '.spam/' >> /home/$MY_USERNAME/.procmailrc
  876. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.procmailrc
  877. # filtering scripts
  878. echo '#!/bin/bash' > /usr/bin/filterspam
  879. echo 'USERNAME=$1' >> /usr/bin/filterspam
  880. echo 'MAILDIR=/home/$USERNAME/Maildir/.learn-spam' >> /usr/bin/filterspam
  881. echo 'if [ ! -d "$MAILDIR" ]; then' >> /usr/bin/filterspam
  882. echo ' exit' >> /usr/bin/filterspam
  883. echo 'fi' >> /usr/bin/filterspam
  884. echo 'for f in `ls $MAILDIR/cur`' >> /usr/bin/filterspam
  885. echo 'do' >> /usr/bin/filterspam
  886. echo ' spamc -L spam < "$MAILDIR/cur/$f" > /dev/null' >> /usr/bin/filterspam
  887. echo ' rm "$MAILDIR/cur/$f"' >> /usr/bin/filterspam
  888. echo 'done' >> /usr/bin/filterspam
  889. echo 'for f in `ls $MAILDIR/new`' >> /usr/bin/filterspam
  890. echo 'do' >> /usr/bin/filterspam
  891. echo ' spamc -L spam < "$MAILDIR/new/$f" > /dev/null' >> /usr/bin/filterspam
  892. echo ' rm "$MAILDIR/new/$f"' >> /usr/bin/filterspam
  893. echo 'done' >> /usr/bin/filterspam
  894. echo '#!/bin/bash' > /usr/bin/filterham
  895. echo 'USERNAME=$1' >> /usr/bin/filterham
  896. echo 'MAILDIR=/home/$USERNAME/Maildir/.learn-ham' >> /usr/bin/filterham
  897. echo 'if [ ! -d "$MAILDIR" ]; then' >> /usr/bin/filterham
  898. echo ' exit' >> /usr/bin/filterham
  899. echo 'fi' >> /usr/bin/filterham
  900. echo 'for f in `ls $MAILDIR/cur`' >> /usr/bin/filterham
  901. echo 'do' >> /usr/bin/filterham
  902. echo ' spamc -L ham < "$MAILDIR/cur/$f" > /dev/null' >> /usr/bin/filterham
  903. echo ' rm "$MAILDIR/cur/$f"' >> /usr/bin/filterham
  904. echo 'done' >> /usr/bin/filterham
  905. echo 'for f in `ls $MAILDIR/new`' >> /usr/bin/filterham
  906. echo 'do' >> /usr/bin/filterham
  907. echo ' spamc -L ham < "$MAILDIR/new/$f" > /dev/null' >> /usr/bin/filterham
  908. echo ' rm "$MAILDIR/new/$f"' >> /usr/bin/filterham
  909. echo 'done' >> /usr/bin/filterham
  910. if ! grep -q "filterspam" /etc/crontab; then
  911. echo "*/3 * * * * root /usr/bin/timeout 120 /usr/bin/filterspam $MY_USERNAME" >> /etc/crontab
  912. fi
  913. if ! grep -q "filterham" /etc/crontab; then
  914. echo "*/3 * * * * root /usr/bin/timeout 120 /usr/bin/filterham $MY_USERNAME" >> /etc/crontab
  915. fi
  916. chmod 655 /usr/bin/filterspam /usr/bin/filterham
  917. sed -i 's/# use_bayes 1/use_bayes 1/g' /etc/mail/spamassassin/local.cf
  918. sed -i 's/# bayes_auto_learn 1/bayes_auto_learn 1/g' /etc/mail/spamassassin/local.cf
  919. service spamassassin restart
  920. service exim4 restart
  921. service cron restart
  922. echo 'spam_filtering' >> $COMPLETION_FILE
  923. }
  924. function configure_imap {
  925. if grep -Fxq "configure_imap" $COMPLETION_FILE; then
  926. return
  927. fi
  928. apt-get -y --force-yes install dovecot-common dovecot-imapd
  929. makecert dovecot
  930. chown root:dovecot /etc/ssl/certs/dovecot.crt
  931. chown root:dovecot /etc/ssl/private/dovecot.key
  932. chown root:dovecot /etc/ssl/private/dovecot.dhparams
  933. sed -i 's|#ssl = yes|ssl = yes|g' /etc/dovecot/conf.d/10-ssl.conf
  934. sed -i 's|ssl_cert = </etc/dovecot/dovecot.pem|ssl_cert = </etc/ssl/certs/dovecot.crt|g' /etc/dovecot/conf.d/10-ssl.conf
  935. sed -i 's|ssl_key = </etc/dovecot/private/dovecot.pem|/etc/ssl/private/dovecot.key|g' /etc/dovecot/conf.d/10-ssl.conf
  936. sed -i 's|#ssl_dh_parameters_length = 1024|ssl_dh_parameters_length = 1024|g' /etc/dovecot/conf.d/10-ssl.conf
  937. sed -i 's/#ssl_prefer_server_ciphers = no/ssl_prefer_server_ciphers = yes/g' /etc/dovecot/conf.d/10-ssl.conf
  938. echo "ssl_cipher_list = 'EDH+CAMELLIA:EDH+aRSA:EECDH+aRSA+AESGCM:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH:+CAMELLIA256:+AES256:+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!DSS:!RC4:!SEED:!ECDSA:CAMELLIA256-SHA:AES256-SHA:CAMELLIA128-SHA:AES128-SHA'" >> /etc/dovecot/conf.d/10-ssl.conf
  939. sed -i 's/#listen = *, ::/listen = */g' /etc/dovecot/dovecot.conf
  940. sed -i 's/#disable_plaintext_auth = yes/disable_plaintext_auth = no/g' /etc/dovecot/conf.d/10-auth.conf
  941. sed -i 's/auth_mechanisms = plain/auth_mechanisms = plain login/g' /etc/dovecot/conf.d/10-auth.conf
  942. sed -i 's|# mail_location = maildir:~/Maildir| mail_location = maildir:~/Maildir:LAYOUT=fs|g' /etc/dovecot/conf.d/10-mail.conf
  943. echo 'configure_imap' >> $COMPLETION_FILE
  944. }
  945. function configure_gpg {
  946. if grep -Fxq "configure_gpg" $COMPLETION_FILE; then
  947. return
  948. fi
  949. apt-get -y --force-yes install gnupg
  950. if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
  951. mkdir /home/$MY_USERNAME/.gnupg
  952. echo 'keyserver hkp://keys.gnupg.net' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  953. echo 'keyserver-options auto-key-retrieve' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  954. fi
  955. sed -i "s|keyserver hkp://keys.gnupg.net|keyserver $GPG_KEYSERVER|g" /home/$MY_USERNAME/.gnupg/gpg.conf
  956. if ! grep -q "# default preferences" /home/$MY_USERNAME/.gnupg/gpg.conf; then
  957. echo '' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  958. echo '# default preferences' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  959. echo 'personal-digest-preferences SHA256' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  960. echo 'cert-digest-algo SHA256' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  961. echo 'default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  962. fi
  963. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  964. if [[ $MY_GPG_PUBLIC_KEY && $MY_GPG_PRIVATE_KEY ]]; then
  965. # use your existing GPG keys which were exported
  966. if [ ! -f $MY_GPG_PUBLIC_KEY ]; then
  967. echo "GPG public key file $MY_GPG_PUBLIC_KEY was not found"
  968. exit 5
  969. fi
  970. if [ ! -f $MY_GPG_PRIVATE_KEY ]; then
  971. echo "GPG private key file $MY_GPG_PRIVATE_KEY was not found"
  972. exit 6
  973. fi
  974. su -c "gpg --import $MY_GPG_PUBLIC_KEY" - $MY_USERNAME
  975. su -c "gpg --allow-secret-key-import --import $MY_GPG_PRIVATE_KEY" - $MY_USERNAME
  976. # for security ensure that the private key file doesn't linger around
  977. shred -zu $MY_GPG_PRIVATE_KEY
  978. else
  979. # Generate a GPG key
  980. echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
  981. echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  982. echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
  983. echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  984. echo "Name-Real: $MY_USERNAME@$DOMAIN_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  985. echo "Name-Email: $MY_USERNAME@$DOMAIN_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  986. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  987. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  988. su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  989. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  990. MY_GPG_PUBLIC_KEY_ID=$(su -c "gpg --list-keys $DOMAIN_NAME | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}'" - $MY_USERNAME)
  991. MY_GPG_PUBLIC_KEY=/tmp/public_key.gpg
  992. su -c "gpg --output $MY_GPG_PUBLIC_KEY --armor --export $MY_GPG_PUBLIC_KEY_ID" - $MY_USERNAME
  993. fi
  994. echo 'configure_gpg' >> $COMPLETION_FILE
  995. }
  996. function email_client {
  997. if grep -Fxq "email_client" $COMPLETION_FILE; then
  998. return
  999. fi
  1000. apt-get -y --force-yes install mutt-patched lynx abook
  1001. if [ ! -d /home/$MY_USERNAME/.mutt ]; then
  1002. mkdir /home/$MY_USERNAME/.mutt
  1003. fi
  1004. echo "text/html; lynx -dump -width=78 -nolist %s | sed ‘s/^ //’; copiousoutput; needsterminal; nametemplate=%s.html" > /home/$MY_USERNAME/.mutt/mailcap
  1005. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt
  1006. echo 'set mbox_type=Maildir' >> /etc/Muttrc
  1007. echo 'set folder="~/Maildir"' >> /etc/Muttrc
  1008. echo 'set mask="!^\\.[^.]"' >> /etc/Muttrc
  1009. echo 'set mbox="~/Maildir"' >> /etc/Muttrc
  1010. echo 'set record="+Sent"' >> /etc/Muttrc
  1011. echo 'set postponed="+Drafts"' >> /etc/Muttrc
  1012. echo 'set trash="+Trash"' >> /etc/Muttrc
  1013. echo 'set spoolfile="~/Maildir"' >> /etc/Muttrc
  1014. echo 'auto_view text/x-vcard text/html text/enriched' >> /etc/Muttrc
  1015. echo 'set editor="emacs"' >> /etc/Muttrc
  1016. echo 'set header_cache="+.cache"' >> /etc/Muttrc
  1017. echo '' >> /etc/Muttrc
  1018. echo 'macro index S "<tag-prefix><save-message>=.learn-spam<enter>" "move to learn-spam"' >> /etc/Muttrc
  1019. echo 'macro pager S "<save-message>=.learn-spam<enter>" "move to learn-spam"' >> /etc/Muttrc
  1020. echo 'macro index H "<tag-prefix><copy-message>=.learn-ham<enter>" "copy to learn-ham"' >> /etc/Muttrc
  1021. echo 'macro pager H "<copy-message>=.learn-ham<enter>" "copy to learn-ham"' >> /etc/Muttrc
  1022. echo '' >> /etc/Muttrc
  1023. echo '# set up the sidebar' >> /etc/Muttrc
  1024. echo 'set sidebar_width=12' >> /etc/Muttrc
  1025. echo 'set sidebar_visible=yes' >> /etc/Muttrc
  1026. echo "set sidebar_delim='|'" >> /etc/Muttrc
  1027. echo 'set sidebar_sort=yes' >> /etc/Muttrc
  1028. echo '' >> /etc/Muttrc
  1029. echo 'set rfc2047_parameters' >> /etc/Muttrc
  1030. echo '' >> /etc/Muttrc
  1031. echo '# Show inbox and sent items' >> /etc/Muttrc
  1032. echo 'mailboxes = =Sent' >> /etc/Muttrc
  1033. echo '' >> /etc/Muttrc
  1034. echo '# Alter these colours as needed for maximum bling' >> /etc/Muttrc
  1035. echo 'color sidebar_new yellow default' >> /etc/Muttrc
  1036. echo 'color normal white default' >> /etc/Muttrc
  1037. echo 'color hdrdefault brightcyan default' >> /etc/Muttrc
  1038. echo 'color signature green default' >> /etc/Muttrc
  1039. echo 'color attachment brightyellow default' >> /etc/Muttrc
  1040. echo 'color quoted green default' >> /etc/Muttrc
  1041. echo 'color quoted1 white default' >> /etc/Muttrc
  1042. echo 'color tilde blue default' >> /etc/Muttrc
  1043. echo '' >> /etc/Muttrc
  1044. echo '# ctrl-n, ctrl-p to select next, prev folder' >> /etc/Muttrc
  1045. echo '# ctrl-o to open selected folder' >> /etc/Muttrc
  1046. echo 'bind index \Cp sidebar-prev' >> /etc/Muttrc
  1047. echo 'bind index \Cn sidebar-next' >> /etc/Muttrc
  1048. echo 'bind index \Co sidebar-open' >> /etc/Muttrc
  1049. echo 'bind pager \Cp sidebar-prev' >> /etc/Muttrc
  1050. echo 'bind pager \Cn sidebar-next' >> /etc/Muttrc
  1051. echo 'bind pager \Co sidebar-open' >> /etc/Muttrc
  1052. echo '' >> /etc/Muttrc
  1053. echo '# ctrl-b toggles sidebar visibility' >> /etc/Muttrc
  1054. echo "macro index,pager \Cb '<enter-command>toggle sidebar_visible<enter><redraw-screen>' 'toggle sidebar'" >> /etc/Muttrc
  1055. echo '' >> /etc/Muttrc
  1056. echo '# esc-m Mark new messages as read' >> /etc/Muttrc
  1057. echo 'macro index <esc>m "T~N<enter>;WNT~O<enter>;WO\CT~T<enter>" "mark all messages read"' >> /etc/Muttrc
  1058. echo '' >> /etc/Muttrc
  1059. echo '# Collapsing threads' >> /etc/Muttrc
  1060. echo 'macro index [ "<collapse-thread>" "collapse/uncollapse thread"' >> /etc/Muttrc
  1061. echo 'macro index ] "<collapse-all>" "collapse/uncollapse all threads"' >> /etc/Muttrc
  1062. echo '' >> /etc/Muttrc
  1063. echo '# threads containing new messages' >> /etc/Muttrc
  1064. echo 'uncolor index "~(~N)"' >> /etc/Muttrc
  1065. echo 'color index brightblue default "~(~N)"' >> /etc/Muttrc
  1066. echo '' >> /etc/Muttrc
  1067. echo '# new messages themselves' >> /etc/Muttrc
  1068. echo 'uncolor index "~N"' >> /etc/Muttrc
  1069. echo 'color index brightyellow default "~N"' >> /etc/Muttrc
  1070. echo '' >> /etc/Muttrc
  1071. echo '# GPG/PGP integration' >> /etc/Muttrc
  1072. echo '# this set the number of seconds to keep in memory the passphrase used to encrypt/sign' >> /etc/Muttrc
  1073. echo 'set pgp_timeout=60' >> /etc/Muttrc
  1074. echo '' >> /etc/Muttrc
  1075. echo '# automatically sign and encrypt with PGP/MIME' >> /etc/Muttrc
  1076. echo 'set pgp_autosign # autosign all outgoing mails' >> /etc/Muttrc
  1077. echo 'set pgp_autoencrypt # Try to encrypt automatically' >> /etc/Muttrc
  1078. echo 'set pgp_replyencrypt # autocrypt replies to crypted' >> /etc/Muttrc
  1079. echo 'set pgp_replysign # autosign replies to signed' >> /etc/Muttrc
  1080. echo 'set pgp_auto_decode=yes # decode attachments' >> /etc/Muttrc
  1081. echo 'set fcc_clear # Keep cleartext copy of sent encrypted mail' >> /etc/Muttrc
  1082. echo 'unset smime_is_default' >> /etc/Muttrc
  1083. echo '' >> /etc/Muttrc
  1084. echo 'set alias_file=~/.mutt-alias' >> /etc/Muttrc
  1085. echo 'source ~/.mutt-alias' >> /etc/Muttrc
  1086. echo 'set query_command= "abook --mutt-query \"%s\""' >> /etc/Muttrc
  1087. echo 'macro index,pager A "<pipe-message>abook --add-email-quiet<return>" "add the sender address to abook"' >> /etc/Muttrc
  1088. cp -f /etc/Muttrc /home/$MY_USERNAME/.muttrc
  1089. touch /home/$MY_USERNAME/.mutt-alias
  1090. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.muttrc
  1091. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt-alias
  1092. echo 'email_client' >> $COMPLETION_FILE
  1093. }
  1094. function folders_for_mailing_lists {
  1095. if grep -Fxq "folders_for_mailing_lists" $COMPLETION_FILE; then
  1096. return
  1097. fi
  1098. echo '#!/bin/bash' > /usr/bin/mailinglistrule
  1099. echo 'MYUSERNAME=$1' >> /usr/bin/mailinglistrule
  1100. echo 'MAILINGLIST=$2' >> /usr/bin/mailinglistrule
  1101. echo 'SUBJECTTAG=$3' >> /usr/bin/mailinglistrule
  1102. echo 'MUTTRC=/home/$MYUSERNAME/.muttrc' >> /usr/bin/mailinglistrule
  1103. echo 'PM=/home/$MYUSERNAME/.procmailrc' >> /usr/bin/mailinglistrule
  1104. echo 'LISTDIR=/home/$MYUSERNAME/Maildir/$MAILINGLIST' >> /usr/bin/mailinglistrule
  1105. echo 'if ! [[ $MYUSERNAME && $MAILINGLIST && $SUBJECTTAG ]]; then' >> /usr/bin/mailinglistrule
  1106. echo ' echo "mailinglistsrule [user name] [mailing list name] [subject tag]"' >> /usr/bin/mailinglistrule
  1107. echo ' exit 1' >> /usr/bin/mailinglistrule
  1108. echo 'fi' >> /usr/bin/mailinglistrule
  1109. echo 'if [ ! -d "$LISTDIR" ]; then' >> /usr/bin/mailinglistrule
  1110. echo ' mkdir -m 700 $LISTDIR' >> /usr/bin/mailinglistrule
  1111. echo ' mkdir -m 700 $LISTDIR/tmp' >> /usr/bin/mailinglistrule
  1112. echo ' mkdir -m 700 $LISTDIR/new' >> /usr/bin/mailinglistrule
  1113. echo ' mkdir -m 700 $LISTDIR/cur' >> /usr/bin/mailinglistrule
  1114. echo 'fi' >> /usr/bin/mailinglistrule
  1115. echo 'chown -R $MYUSERNAME:$MYUSERNAME $LISTDIR' >> /usr/bin/mailinglistrule
  1116. echo 'echo "" >> $PM' >> /usr/bin/mailinglistrule
  1117. echo 'echo ":0" >> $PM' >> /usr/bin/mailinglistrule
  1118. echo 'echo " * ^Subject:.*()\[$SUBJECTTAG\]" >> $PM' >> /usr/bin/mailinglistrule
  1119. echo 'echo "$LISTDIR/new" >> $PM' >> /usr/bin/mailinglistrule
  1120. echo 'chown $MYUSERNAME:$MYUSERNAME $PM' >> /usr/bin/mailinglistrule
  1121. echo 'if [ ! -f "$MUTTRC" ]; then' >> /usr/bin/mailinglistrule
  1122. echo ' cp /etc/Muttrc $MUTTRC' >> /usr/bin/mailinglistrule
  1123. echo ' chown $MYUSERNAME:$MYUSERNAME $MUTTRC' >> /usr/bin/mailinglistrule
  1124. echo 'fi' >> /usr/bin/mailinglistrule
  1125. echo 'PROCMAILLOG=/home/$MYUSERNAME/log' >> /usr/bin/mailinglistrule
  1126. echo 'if [ ! -d $PROCMAILLOG ]; then' >> /usr/bin/mailinglistrule
  1127. echo ' mkdir $PROCMAILLOG' >> /usr/bin/mailinglistrule
  1128. echo ' chown -R $MYUSERNAME:$MYUSERNAME $PROCMAILLOG' >> /usr/bin/mailinglistrule
  1129. echo 'fi' >> /usr/bin/mailinglistrule
  1130. echo 'MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)' >> /usr/bin/mailinglistrule
  1131. echo 'if [[ $MUTT_MAILBOXES != *$MAILINGLIST* ]]; then' >> /usr/bin/mailinglistrule
  1132. echo ' sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$MAILINGLIST|g" $MUTTRC' >> /usr/bin/mailinglistrule
  1133. echo ' chown $MYUSERNAME:$MYUSERNAME $MUTTRC' >> /usr/bin/mailinglistrule
  1134. echo 'fi' >> /usr/bin/mailinglistrule
  1135. chmod +x /usr/bin/mailinglistrule
  1136. echo 'folders_for_mailing_lists' >> $COMPLETION_FILE
  1137. }
  1138. function folders_for_email_addresses {
  1139. if grep -Fxq "folders_for_email_addresses" $COMPLETION_FILE; then
  1140. return
  1141. fi
  1142. echo '#!/bin/bash' > /usr/bin/emailrule
  1143. echo 'MYUSERNAME=$1' >> /usr/bin/emailrule
  1144. echo 'EMAILADDRESS=$2' >> /usr/bin/emailrule
  1145. echo 'MAILINGLIST=$3' >> /usr/bin/emailrule
  1146. echo 'MUTTRC=/home/$MYUSERNAME/.muttrc' >> /usr/bin/emailrule
  1147. echo 'PM=/home/$MYUSERNAME/.procmailrc' >> /usr/bin/emailrule
  1148. echo 'LISTDIR=/home/$MYUSERNAME/Maildir/$MAILINGLIST' >> /usr/bin/emailrule
  1149. echo 'if ! [[ $MYUSERNAME && $EMAILADDRESS && $MAILINGLIST ]]; then' >> /usr/bin/emailrule
  1150. echo ' echo "emailrule [user name] [email address] [mailing list name]"' >> /usr/bin/emailrule
  1151. echo ' exit 1' >> /usr/bin/emailrule
  1152. echo 'fi' >> /usr/bin/emailrule
  1153. echo 'if [ ! -d "$LISTDIR" ]; then' >> /usr/bin/emailrule
  1154. echo ' mkdir -m 700 $LISTDIR' >> /usr/bin/emailrule
  1155. echo ' mkdir -m 700 $LISTDIR/tmp' >> /usr/bin/emailrule
  1156. echo ' mkdir -m 700 $LISTDIR/new' >> /usr/bin/emailrule
  1157. echo ' mkdir -m 700 $LISTDIR/cur' >> /usr/bin/emailrule
  1158. echo 'fi' >> /usr/bin/emailrule
  1159. echo 'chown -R $MYUSERNAME:$MYUSERNAME $LISTDIR' >> /usr/bin/emailrule
  1160. echo 'echo "" >> $PM' >> /usr/bin/emailrule
  1161. echo 'echo ":0" >> $PM' >> /usr/bin/emailrule
  1162. echo 'echo " * ^From: $EMAILADDRESS" >> $PM' >> /usr/bin/emailrule
  1163. echo 'echo "$LISTDIR/new" >> $PM' >> /usr/bin/emailrule
  1164. echo 'chown $MYUSERNAME:$MYUSERNAME $PM' >> /usr/bin/emailrule
  1165. echo 'if [ ! -f "$MUTTRC" ]; then' >> /usr/bin/emailrule
  1166. echo ' cp /etc/Muttrc $MUTTRC' >> /usr/bin/emailrule
  1167. echo ' chown $MYUSERNAME:$MYUSERNAME $MUTTRC' >> /usr/bin/emailrule
  1168. echo 'fi' >> /usr/bin/emailrule
  1169. echo 'PROCMAILLOG=/home/$MYUSERNAME/log' >> /usr/bin/emailrule
  1170. echo 'if [ ! -d $PROCMAILLOG ]; then' >> /usr/bin/emailrule
  1171. echo ' mkdir $PROCMAILLOG' >> /usr/bin/emailrule
  1172. echo ' chown -R $MYUSERNAME:$MYUSERNAME $PROCMAILLOG' >> /usr/bin/emailrule
  1173. echo 'fi' >> /usr/bin/emailrule
  1174. echo 'MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)' >> /usr/bin/emailrule
  1175. echo 'if [[ $MUTT_MAILBOXES != *$MAILINGLIST* ]]; then' >> /usr/bin/emailrule
  1176. echo ' sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$MAILINGLIST|g" $MUTTRC' >> /usr/bin/emailrule
  1177. echo ' chown $MYUSERNAME:$MYUSERNAME $MUTTRC' >> /usr/bin/emailrule
  1178. echo 'fi' >> /usr/bin/emailrule
  1179. chmod +x /usr/bin/emailrule
  1180. echo 'folders_for_email_addresses' >> $COMPLETION_FILE
  1181. }
  1182. function dynamic_dns_freedns {
  1183. if grep -Fxq "dynamic_dns_freedns" $COMPLETION_FILE; then
  1184. return
  1185. fi
  1186. echo '#!/bin/bash' > /usr/bin/dynamicdns
  1187. echo '# subdomain name 1' >> /usr/bin/dynamicdns
  1188. echo "wget -O - https://freedns.afraid.org/dynamic/update.php?$FREEDNS_SUBDOMAIN_CODE== >> /dev/null 2>&1" >> /usr/bin/dynamicdns
  1189. echo '# add any other subdomains below' >> /usr/bin/dynamicdns
  1190. chmod 600 /usr/bin/dynamicdns
  1191. chmod +x /usr/bin/dynamicdns
  1192. if ! grep -q "dynamicdns" /etc/crontab; then
  1193. sed -i '/# m h dom mon dow user command/a\*/5 * * * * root /usr/bin/timeout 240 /usr/bin/dynamicdns' /etc/crontab
  1194. fi
  1195. service cron restart
  1196. echo 'dynamic_dns_freedns' >> $COMPLETION_FILE
  1197. }
  1198. function create_private_mailing_list {
  1199. # This installation doesn't work, results in ruby errors
  1200. # There is currently no schleuder package for Debian jessie
  1201. if grep -Fxq "create_private_mailing_list" $COMPLETION_FILE; then
  1202. return
  1203. fi
  1204. if [ ! $PRIVATE_MAILING_LIST ]; then
  1205. return
  1206. fi
  1207. if [ $PRIVATE_MAILING_LIST == $MY_USERNAME ]; then
  1208. echo 'The name of the private mailing list should not be the'
  1209. echo 'same as your username'
  1210. exit 10
  1211. fi
  1212. if [ ! $MY_GPG_PUBLIC_KEY ]; then
  1213. echo 'To create a private mailing list you need to specify a file'
  1214. echo 'containing your exported GPG key within MY_GPG_PUBLIC_KEY at'
  1215. echo 'the top of the script'
  1216. exit 11
  1217. fi
  1218. apt-get -y --force-yes install ruby ruby-dev ruby-gpgme libgpgme11-dev libmagic-dev
  1219. gem install schleuder
  1220. schleuder-fix-gem-dependencies
  1221. schleuder-init-setup --gem
  1222. # NOTE: this is version number sensitive and so might need changing
  1223. ln -s /var/lib/gems/2.1.0/gems/schleuder-2.2.4 /var/lib/schleuder
  1224. sed -i 's/#smtp_port: 25/smtp_port: 465/g' /etc/schleuder/schleuder.conf
  1225. sed -i 's/#superadminaddr: root@localhost/superadminaddr: root@localhost' /etc/schleuder/schleuder.conf
  1226. schleuder-newlist $PRIVATE_MAILING_LIST@$DOMAIN_NAME -realname "$PRIVATE_MAILING_LIST" -adminaddress $MY_USERNAME@$DOMAIN_NAME -initmember $MY_USERNAME@$DOMAIN_NAME -initmemberkey $MY_GPG_PUBLIC_KEY -nointeractive
  1227. emailrule $MY_USERNAME $PRIVATE_MAILING_LIST@$DOMAIN_NAME $PRIVATE_MAILING_LIST
  1228. echo 'schleuder:' > /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1229. echo ' debug_print = "R: schleuder for $local_part@$domain"' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1230. echo ' driver = accept' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1231. echo ' local_part_suffix_optional' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1232. echo ' local_part_suffix = +* : -bounce : -sendkey' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1233. echo ' domains = +local_domains' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1234. echo ' user = schleuder' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1235. echo ' group = schleuder' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1236. echo ' require_files = schleuder:+/var/lib/schleuder/$domain/${local_part}' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1237. echo ' transport = schleuder_transport' >> /etc/exim4/conf.d/router/550_exim4-config_schleuder
  1238. echo 'schleuder_transport:' > /etc/exim4/conf.d/transport/30_exim4-config_schleuder
  1239. echo ' debug_print = "T: schleuder_transport for $local_part@$domain"' >> /etc/exim4/conf.d/transport/30_exim4-config_schleuder
  1240. echo ' driver = pipe' >> /etc/exim4/conf.d/transport/30_exim4-config_schleuder
  1241. echo ' home_directory = "/var/lib/schleuder/$domain/$local_part"' >> /etc/exim4/conf.d/transport/30_exim4-config_schleuder
  1242. echo ' command = "/usr/bin/schleuder $local_part@$domain"' >> /etc/exim4/conf.d/transport/30_exim4-config_schleuder
  1243. chown -R schleuder:schleuder /var/lib/schleuder
  1244. update-exim4.conf.template -r
  1245. update-exim4.conf
  1246. service exim4 restart
  1247. useradd -d /var/schleuderlists -s /bin/false schleuder
  1248. adduser Debian-exim schleuder
  1249. usermod -a -G mail schleuder
  1250. #exim -d -bt $PRIVATE_MAILING_LIST@$DOMAIN_NAME
  1251. echo 'create_private_mailing_list' >> $COMPLETION_FILE
  1252. }
  1253. function import_email {
  1254. EMAIL_COMPLETE_MSG=' *** Freedombone mailbox installation is complete ***'
  1255. if grep -Fxq "import_email" $COMPLETION_FILE; then
  1256. if [[ $SYSTEM_TYPE == "email" || $SYSTEM_TYPE == "mailbox" ]]; then
  1257. echo $EMAIL_COMPLETE_MSG
  1258. if [ -d /media/usb ]; then
  1259. umount /media/usb
  1260. rm -rf /media/usb
  1261. echo ' You can now remove the USB drive'
  1262. fi
  1263. exit 0
  1264. fi
  1265. return
  1266. fi
  1267. if [ $IMPORT_MAILDIR ]; then
  1268. if [ -d $IMPORT_MAILDIR ]; then
  1269. echo 'Transfering email files'
  1270. cp -r $IMPORT_MAILDIR /home/$MY_USERNAME
  1271. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/Maildir
  1272. else
  1273. echo "Email import directory $IMPORT_MAILDIR not found"
  1274. exit 9
  1275. fi
  1276. fi
  1277. echo 'import_email' >> $COMPLETION_FILE
  1278. if [[ $SYSTEM_TYPE == "email" || $SYSTEM_TYPE == "mailbox" ]]; then
  1279. apt-get -y --force-yes autoremove
  1280. # unmount any attached usb drive
  1281. echo ''
  1282. echo $EMAIL_COMPLETE_MSG
  1283. echo ''
  1284. if [ -d /media/usb ]; then
  1285. umount /media/usb
  1286. rm -rf /media/usb
  1287. echo ' You can now remove the USB drive'
  1288. fi
  1289. exit 0
  1290. fi
  1291. }
  1292. function install_web_server {
  1293. if grep -Fxq "install_web_server" $COMPLETION_FILE; then
  1294. return
  1295. fi
  1296. # remove apache
  1297. apt-get -y remove --purge apache2
  1298. if [ -d /etc/apache2 ]; then
  1299. rm -rf /etc/apache2
  1300. fi
  1301. # install nginx
  1302. apt-get -y --force-yes install nginx php5-fpm git
  1303. # install a script to easily enable and disable nginx virtual hosts
  1304. cd $INSTALL_DIR
  1305. git clone https://github.com/perusio/nginx_ensite
  1306. cd $INSTALL_DIR/nginx_ensite
  1307. cp nginx_* /usr/sbin
  1308. echo 'install_web_server' >> $COMPLETION_FILE
  1309. }
  1310. function install_final {
  1311. if grep -Fxq "install_final" $COMPLETION_FILE; then
  1312. return
  1313. fi
  1314. # unmount any attached usb drive
  1315. if [ -d /media/usb ]; then
  1316. umount /media/usb
  1317. rm -rf /media/usb
  1318. fi
  1319. apt-get -y --force-yes autoremove
  1320. echo 'install_final' >> $COMPLETION_FILE
  1321. echo ''
  1322. echo ' *** Freedombone installation is complete. Rebooting... ***'
  1323. echo ''
  1324. reboot
  1325. }
  1326. argument_checks
  1327. configure_firewall
  1328. configure_firewall_for_ssh
  1329. configure_firewall_for_dns
  1330. configure_firewall_for_ftp
  1331. configure_firewall_for_web_access
  1332. remove_proprietary_repos
  1333. change_debian_repos
  1334. enable_backports
  1335. configure_dns
  1336. initial_setup
  1337. install_editor
  1338. change_login_message
  1339. update_the_kernel
  1340. enable_zram
  1341. random_number_generator
  1342. set_your_domain_name
  1343. time_synchronisation
  1344. configure_internet_protocol
  1345. configure_ssh
  1346. search_for_attached_usb_drive
  1347. regenerate_ssh_keys
  1348. script_to_make_self_signed_certificates
  1349. configure_email
  1350. #spam_filtering
  1351. configure_imap
  1352. configure_gpg
  1353. email_client
  1354. configure_firewall_for_email
  1355. folders_for_mailing_lists
  1356. folders_for_email_addresses
  1357. dynamic_dns_freedns
  1358. #create_private_mailing_list
  1359. import_email
  1360. install_web_server
  1361. configure_firewall_for_web_server
  1362. install_final
  1363. echo 'Freedombone installation is complete'
  1364. exit 0