install-freedombone.sh 55KB

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