install-freedombone.sh 76KB

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