install-freedombone.sh 45KB

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