install-freedombone.sh 47KB

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