install-freedombone.sh 52KB

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