install-freedombone.sh 61KB

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