install-freedombone.sh 63KB

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