freedombone-app-irc 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # IRC server application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. IRC_PORT=6697
  31. IRC_ONION_PORT=6697
  32. # An optional password to log into IRC. This applies to all users
  33. IRC_PASSWORD=
  34. function remove_irc_server {
  35. if ! grep -Fxq "install_irc_server" $COMPLETION_FILE; then
  36. return
  37. fi
  38. systemctl stop ngircd
  39. apt-get -y remove --purge ngircd
  40. if [ -d /etc/ngircd ]; then
  41. rm -rf /etc/ngircd
  42. fi
  43. iptables -D INPUT -p tcp --dport $IRC_PORT -j ACCEPT
  44. iptables -D INPUT -p tcp --dport 1024:65535 --sport $IRC_PORT -j ACCEPT
  45. function_check save_firewall_settings
  46. save_firewall_settings
  47. sed -i '/install_irc_server/d' $COMPLETION_FILE
  48. sed -i '/IRC /d' $COMPLETION_FILE
  49. }
  50. function configure_firewall_for_irc {
  51. if [ ! -d /etc/ngircd ]; then
  52. return
  53. fi
  54. if grep -Fxq "configure_firewall_for_irc" $COMPLETION_FILE; then
  55. return
  56. fi
  57. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  58. # docker does its own firewalling
  59. return
  60. fi
  61. if [[ $ONION_ONLY != "no" ]]; then
  62. return
  63. fi
  64. iptables -A INPUT -p tcp --dport $IRC_PORT -j ACCEPT
  65. iptables -I INPUT -p tcp --dport 1024:65535 --sport $IRC_PORT -j ACCEPT
  66. function_check save_firewall_settings
  67. save_firewall_settings
  68. OPEN_PORTS+=("IRC $IRC_PORT")
  69. echo 'configure_firewall_for_irc' >> $COMPLETION_FILE
  70. }
  71. function install_irc_server {
  72. if [[ $SYSTEM_TYPE == "$VARIANT_WRITER" || $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CLOUD" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  73. return
  74. fi
  75. if grep -Fxq "install_irc_server" $COMPLETION_FILE; then
  76. return
  77. fi
  78. apt-get -y install ngircd
  79. if [ ! -d /etc/ngircd ]; then
  80. echo $"ERROR: ngircd does not appear to have installed. $CHECK_MESSAGE"
  81. exit 53
  82. fi
  83. if [ ! -f /etc/ssl/certs/ngircd.dhparam ]; then
  84. ${PROJECT_NAME}-addcert -h ngircd --dhkey $DH_KEYLENGTH
  85. function_check check_certificates
  86. check_certificates ngircd
  87. fi
  88. DEFAULTDOMAIN=$DEFAULT_DOMAIN_NAME
  89. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  90. DEFAULTDOMAIN="${DEFAULT_DOMAIN_NAME}.local"
  91. fi
  92. # create a login password if needed
  93. if [ ! $IRC_PASSWORD ]; then
  94. IRC_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  95. fi
  96. echo '**************************************************' > /etc/ngircd/motd
  97. echo $'* F R E E D O M B O N E I R C *' >> /etc/ngircd/motd
  98. echo '* *' >> /etc/ngircd/motd
  99. echo $'* Freedom in the Cloud *' >> /etc/ngircd/motd
  100. echo '**************************************************' >> /etc/ngircd/motd
  101. sed -i 's|MotdFile = /etc/ngircd/ngircd.motd|MotdFile = /etc/ngircd/motd|g' /etc/ngircd/ngircd.conf
  102. sed -i "s/irc@irc.example.com/$MY_EMAIL_ADDRESS/g" /etc/ngircd/ngircd.conf
  103. sed -i "s/irc.example.net/$DEFAULTDOMAIN/g" /etc/ngircd/ngircd.conf
  104. sed -i "s|Yet another IRC Server running on Debian GNU/Linux|IRC Server of $DEFAULTDOMAIN|g" /etc/ngircd/ngircd.conf
  105. sed -i 's/;Password = wealllikedebian/Password =/g' /etc/ngircd/ngircd.conf
  106. sed -i 's|;CertFile = /etc/ssl/certs/server.crt|CertFile = /etc/ssl/certs/ngircd.crt|g' /etc/ngircd/ngircd.conf
  107. sed -i 's|;DHFile = /etc/ngircd/dhparams.pem|DHFile = /etc/ssl/certs/ngircd.dhparam|g' /etc/ngircd/ngircd.conf
  108. sed -i 's|;KeyFile = /etc/ssl/private/server.key|KeyFile = /etc/ssl/private/ngircd.key|g' /etc/ngircd/ngircd.conf
  109. sed -i "s/;Ports =.*/Ports = $IRC_PORT/1" /etc/ngircd/ngircd.conf
  110. sed -i "s/;Ports =.*/Ports = $IRC_PORT/2" /etc/ngircd/ngircd.conf
  111. sed -i "s/;Name = #ngircd/Name = #${PROJECT_NAME}/g" /etc/ngircd/ngircd.conf
  112. sed -i "s/;Topic = Our ngircd testing channel/Topic = ${PROJECT_NAME} chat channel/g" /etc/ngircd/ngircd.conf
  113. sed -i 's/;MaxUsers = 23/MaxUsers = 23/g' /etc/ngircd/ngircd.conf
  114. sed -i "s|;KeyFile = /etc/ngircd/#chan.key|KeyFile = /etc/ngircd/#${PROJECT_NAME}.key|g" /etc/ngircd/ngircd.conf
  115. sed -i "s/;CloakHost = cloaked.host/CloakHost = ${PROJECT_NAME}/g" /etc/ngircd/ngircd.conf
  116. IRC_SALT="$(create_password 30)"
  117. if [ -f $IMAGE_PASSWORD_FILE ]; then
  118. IRC_OPERATOR_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  119. else
  120. IRC_OPERATOR_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  121. fi
  122. sed -i "s|;CloakHostSalt = abcdefghijklmnopqrstuvwxyz|CloakHostSalt = $IRC_SALT|g" /etc/ngircd/ngircd.conf
  123. sed -i 's/;ConnectIPv4 = yes/ConnectIPv4 = yes/g' /etc/ngircd/ngircd.conf
  124. sed -i 's/;MorePrivacy = no/MorePrivacy = yes/g' /etc/ngircd/ngircd.conf
  125. sed -i 's/;RequireAuthPing = no/RequireAuthPing = no/g' /etc/ngircd/ngircd.conf
  126. sed -i "s/;Name = TheOper/Name = $MY_USERNAME/g" /etc/ngircd/ngircd.conf
  127. sed -i "s/;Password = ThePwd/Password = $IRC_OPERATOR_PASSWORD/g" /etc/ngircd/ngircd.conf
  128. sed -i 's|;Listen =.*|Listen = 0.0.0.0,0.0.0.0:9050,127.0.0.1,127.0.0.1:9050|g' /etc/ngircd/ngircd.conf
  129. if [ $IRC_PASSWORD ]; then
  130. sed -i "0,/RE/s/Password =.*/Password =$IRC_PASSWORD/" /etc/ngircd/ngircd.conf
  131. fi
  132. # If we are on a mesh then DNS is not available
  133. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  134. sed -i "s/;DNS =.*/DNS = no/g" /etc/ngircd/ngircd.conf
  135. fi
  136. # upgrade a cypher
  137. sed -i 's|SECURE128|SECURE256|g' /etc/ngircd/ngircd.conf
  138. mkdir /var/run/ircd
  139. chown -R irc:irc /var/run/ircd
  140. mkdir /var/run/ngircd
  141. touch /var/run/ngircd/ngircd.pid
  142. chown -R irc:irc /var/run/ngircd
  143. IRC_ONION_HOSTNAME=$(add_onion_service irc ${IRC_PORT} ${IRC_ONION_PORT})
  144. if ! grep -q $"IRC onion domain" $COMPLETION_FILE; then
  145. echo "IRC onion domain:$IRC_ONION_HOSTNAME" >> $COMPLETION_FILE
  146. fi
  147. systemctl restart ngircd
  148. # keep the daemon running
  149. echo '' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  150. echo '# keep irc daemon running' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  151. echo 'IRC_RUNNING=$(pgrep ngircd > /dev/null && echo Running)' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  152. echo 'if [ ! $IRC_RUNNING ]; then' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  153. echo ' systemctl start ngircd' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  154. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  155. echo ' echo " IRC daemon restarted" >> $LOGFILE' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  156. echo 'fi' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
  157. if ! grep -q $"IRC Server" /home/$MY_USERNAME/README; then
  158. echo '' >> /home/$MY_USERNAME/README
  159. echo '' >> /home/$MY_USERNAME/README
  160. echo $'IRC Server' >> /home/$MY_USERNAME/README
  161. echo '==========' >> /home/$MY_USERNAME/README
  162. echo $'To connect to your IRC server in irssi:' >> /home/$MY_USERNAME/README
  163. echo '' >> /home/$MY_USERNAME/README
  164. if [[ $ONION_ONLY != 'yes' ]]; then
  165. echo " irssi" >> /home/$MY_USERNAME/README
  166. echo " /server add -auto -ssl $DEFAULTDOMAIN $IRC_PORT" >> /home/$MY_USERNAME/README
  167. echo " /connect $DEFAULT_DOMAIN_NAME" >> /home/$MY_USERNAME/README
  168. else
  169. echo " usetorwith irssi" >> /home/$MY_USERNAME/README
  170. echo " /server add -auto $IRC_ONION_HOSTNAME $IRC_PORT" >> /home/$MY_USERNAME/README
  171. echo " /connect $IRC_ONION_HOSTNAME" >> /home/$MY_USERNAME/README
  172. fi
  173. echo " /join #${PROJECT_NAME}" >> /home/$MY_USERNAME/README
  174. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  175. chmod 600 /home/$MY_USERNAME/README
  176. fi
  177. function_check configure_firewall_for_irc
  178. configure_firewall_for_irc
  179. echo 'install_irc_server' >> $COMPLETION_FILE
  180. }
  181. function install_irc_client {
  182. if [[ $SYSTEM_TYPE == "$VARIANT_WRITER" || $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CLOUD" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  183. return
  184. fi
  185. if grep -Fxq "install_irc_client" $COMPLETION_FILE; then
  186. return
  187. fi
  188. apt-get -y install irssi
  189. if [ ! -d /home/$MY_USERNAME/.irssi ]; then
  190. mkdir /home/$MY_USERNAME/.irssi
  191. fi
  192. echo 'servers = (' > /home/$MY_USERNAME/.irssi/config
  193. echo ' {' >> /home/$MY_USERNAME/.irssi/config
  194. echo ' address = "chat.freenode.net";' >> /home/$MY_USERNAME/.irssi/config
  195. echo ' chatnet = "Freenode";' >> /home/$MY_USERNAME/.irssi/config
  196. echo ' port = "6667";' >> /home/$MY_USERNAME/.irssi/config
  197. echo ' autoconnect = "no";' >> /home/$MY_USERNAME/.irssi/config
  198. echo ' },' >> /home/$MY_USERNAME/.irssi/config
  199. echo ' {' >> /home/$MY_USERNAME/.irssi/config
  200. echo ' address = "irc.oftc.net";' >> /home/$MY_USERNAME/.irssi/config
  201. echo ' chatnet = "OFTC";' >> /home/$MY_USERNAME/.irssi/config
  202. echo ' port = "6667";' >> /home/$MY_USERNAME/.irssi/config
  203. echo ' autoconnect = "yes";' >> /home/$MY_USERNAME/.irssi/config
  204. echo ' },' >> /home/$MY_USERNAME/.irssi/config
  205. echo ' {' >> /home/$MY_USERNAME/.irssi/config
  206. echo " address = \"127.0.0.1\";" >> /home/$MY_USERNAME/.irssi/config
  207. if [[ $ONION_ONLY == 'no' ]]; then
  208. echo " port = \"${IRC_PORT}\";" >> /home/$MY_USERNAME/.irssi/config
  209. echo ' use_ssl = "yes";' >> /home/$MY_USERNAME/.irssi/config
  210. else
  211. IRC_ONION_HOSTNAME=$(cat $COMPLETION_FILE | grep "IRC onion domain" | awk -F ':' '{print $2}')
  212. echo " port = \"${IRC_ONION_PORT}\";" >> /home/$MY_USERNAME/.irssi/config
  213. echo ' use_ssl = "no";' >> /home/$MY_USERNAME/.irssi/config
  214. fi
  215. echo ' chatnet = "Freedombone";' >> /home/$MY_USERNAME/.irssi/config
  216. echo ' ssl_verify = "no";' >> /home/$MY_USERNAME/.irssi/config
  217. echo ' autoconnect = "yes";' >> /home/$MY_USERNAME/.irssi/config
  218. if [ $IRC_PASSWORD ]; then
  219. echo " password = \"${IRC_PASSWORD}\";" >> /home/$MY_USERNAME/.irssi/config
  220. fi
  221. echo ' }' >> /home/$MY_USERNAME/.irssi/config
  222. echo ');' >> /home/$MY_USERNAME/.irssi/config
  223. echo '' >> /home/$MY_USERNAME/.irssi/config
  224. echo 'chatnets = {' >> /home/$MY_USERNAME/.irssi/config
  225. echo ' Freedombone = {' >> /home/$MY_USERNAME/.irssi/config
  226. echo ' type = "IRC";' >> /home/$MY_USERNAME/.irssi/config
  227. echo ' max_kicks = "1";' >> /home/$MY_USERNAME/.irssi/config
  228. echo ' max_msgs = "4";' >> /home/$MY_USERNAME/.irssi/config
  229. echo ' max_whois = "1";' >> /home/$MY_USERNAME/.irssi/config
  230. echo ' };' >> /home/$MY_USERNAME/.irssi/config
  231. echo ' Freenode = {' >> /home/$MY_USERNAME/.irssi/config
  232. echo ' type = "IRC";' >> /home/$MY_USERNAME/.irssi/config
  233. echo ' max_kicks = "1";' >> /home/$MY_USERNAME/.irssi/config
  234. echo ' max_msgs = "4";' >> /home/$MY_USERNAME/.irssi/config
  235. echo ' max_whois = "1";' >> /home/$MY_USERNAME/.irssi/config
  236. echo ' };' >> /home/$MY_USERNAME/.irssi/config
  237. echo ' OFTC = {' >> /home/$MY_USERNAME/.irssi/config
  238. echo ' type = "IRC";' >> /home/$MY_USERNAME/.irssi/config
  239. echo ' max_kicks = "1";' >> /home/$MY_USERNAME/.irssi/config
  240. echo ' max_msgs = "1";' >> /home/$MY_USERNAME/.irssi/config
  241. echo ' max_whois = "1";' >> /home/$MY_USERNAME/.irssi/config
  242. echo ' };' >> /home/$MY_USERNAME/.irssi/config
  243. echo '};' >> /home/$MY_USERNAME/.irssi/config
  244. echo '' >> /home/$MY_USERNAME/.irssi/config
  245. echo 'channels = (' >> /home/$MY_USERNAME/.irssi/config
  246. echo ' { name = "#freedombone"; chatnet = "Freedombone"; autojoin = "Yes"; },' >> /home/$MY_USERNAME/.irssi/config
  247. echo ');' >> /home/$MY_USERNAME/.irssi/config
  248. echo '' >> /home/$MY_USERNAME/.irssi/config
  249. echo 'settings = {' >> /home/$MY_USERNAME/.irssi/config
  250. echo " core = { real_name = \"$MY_NAME\"; user_name = \"$MY_USERNAME\"; nick = \"$MY_USERNAME\"; };" >> /home/$MY_USERNAME/.irssi/config
  251. echo ' "fe-text" = { actlist_sort = "refnum"; };' >> /home/$MY_USERNAME/.irssi/config
  252. echo '};' >> /home/$MY_USERNAME/.irssi/config
  253. echo 'ignores = ( { level = "CTCPS"; } );' >> /home/$MY_USERNAME/.irssi/config
  254. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.irssi
  255. echo 'install_irc_client' >> $COMPLETION_FILE
  256. }
  257. # NOTE: deliberately no exit 0