install-freedombone.sh 77KB

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