install-freedombone.sh 76KB

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