install-freedombone.sh 53KB

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