freedombone-controlpanel-user 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # User control panel
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 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. PROJECT_NAME='freedombone'
  31. export TEXTDOMAIN=${PROJECT_NAME}-controlpanel-user
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. MY_EMAIL_ADDRESS=$USER@$HOSTNAME
  34. GPG_ID=$(gpg --fingerprint $MY_EMAIL_ADDRESS | grep -i "pub" | head -n 1 | awk -F '/' '{print $2}' | awk -F ' ' '{print $1}')
  35. SYNCTHING_CONFIG_FILE=~/.syncthingids
  36. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  37. function any_key {
  38. echo ' '
  39. read -n1 -r -p $"Press any key to continue..." key
  40. }
  41. function remove_user_from_mailing_list {
  42. USER_MAILING_LISTS=$(cat "/home/$USER/.procmailrc" | grep '\[' | grep '\]' | awk -F '\[' '{print $2}' | awk -F '\\' '{print $1}')
  43. i=0
  44. W=()
  45. list_name=()
  46. while read -r listname; do
  47. i=$((i+1))
  48. W+=($i "$listname")
  49. list_name+=("$listname")
  50. echo $listname
  51. done <<< "$USER_MAILING_LISTS"
  52. i=$((i+1))
  53. W+=($i $"Exit back to user mainenance")
  54. list_selected=$(dialog --default-item "$i" --backtitle $"Freedombone User Control Panel" --title $"Remove yourself from a mailing list" --menu $"Select one of the following:" 24 50 17 "${W[@]}" 3>&2 2>&1 1>&3)
  55. if [ $? -eq 0 ]; then # Exit with OK
  56. if [ ${list_selected} -ne ${i} ]; then
  57. remove_list_name="${list_name[$((list_selected-1))]}"
  58. # find the line number where the list is defined
  59. line_number=0
  60. i=0
  61. while read -r line
  62. do
  63. if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
  64. line_number=${i}
  65. fi
  66. i=$((i+1))
  67. done < "/home/$USER/.procmailrc"
  68. if [ ${line_number} -eq 0 ]; then
  69. # no match was found
  70. return
  71. fi
  72. # recreate the file
  73. if [ -f /home/${USER}/.procmailrc_new ]; then
  74. rm /home/${USER}/.procmailrc_new
  75. fi
  76. i=0
  77. clip=0
  78. while read -r line
  79. do
  80. i=$((i+1))
  81. if [ ${i} -gt $((line_number-1)) ]; then
  82. if [ ${clip} -eq 0 ]; then
  83. clip=1
  84. fi
  85. if [ ${clip} -eq 1 ]; then
  86. if [ ${i} -lt $((line_number+2)) ]; then
  87. continue
  88. else
  89. if [ ${#line} -lt 1 ]; then
  90. clip=2
  91. continue
  92. fi
  93. if [[ "$line" == ":"* || "$line" == "#"* ]]; then
  94. clip=2
  95. else
  96. continue
  97. fi
  98. fi
  99. fi
  100. fi
  101. echo "$line" >> /home/${USER}/.procmailrc_new
  102. if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
  103. line_number=${i}
  104. fi
  105. done < "/home/$USER/.procmailrc"
  106. cp /home/${USER}/.procmailrc_new /home/${USER}/.procmailrc
  107. rm /home/${USER}/.procmailrc_new
  108. chown ${USER}:${USER} /home/${USER}/.procmailrc
  109. dialog --title $"Remove yourself from mailing list" \
  110. --msgbox $"You have been removed from ${remove_list_name}" 6 50
  111. fi
  112. fi
  113. }
  114. function add_to_mailing_list {
  115. data=$(tempfile 2>/dev/null)
  116. trap "rm -f $data" 0 1 2 5 15
  117. dialog --backtitle $"Freedombone User Control Panel" \
  118. --title $"Subscribe to a mailing list" \
  119. --form $"You can either enter a subject or an email address\n" 11 68 4 \
  120. $"List folder name:" 1 1 "" 1 35 26 25 \
  121. $"Name between [] on subject line:" 2 1 "" 2 35 26 25 \
  122. $"List email address:" 3 1 "" 3 35 26 25 \
  123. $"Public:" 4 1 $"yes" 4 35 4 25 \
  124. 2> $data
  125. sel=$?
  126. case $sel in
  127. 1) return;;
  128. 255) return;;
  129. esac
  130. LIST_NAME=$(cat $data | sed -n 1p)
  131. LIST_SUBJECT=$(cat $data | sed -n 2p)
  132. LIST_EMAIL=$(cat $data | sed -n 3p)
  133. LIST_PUBLIC=$(cat $data | sed -n 4p)
  134. if [ ${#LIST_PUBLIC} -lt 1 ]; then
  135. LIST_PUBLIC='no'
  136. fi
  137. if [[ $LIST_PUBLIC == $'y' || $LIST_PUBLIC == $'Y' || $LIST_PUBLIC == $'true' || $LIST_PUBLIC == $'True' || $LIST_PUBLIC == $'yes' || $LIST_PUBLIC == $'Yes' || $LIST_PUBLIC == $'YES' ]]; then
  138. LIST_PUBLIC='yes'
  139. else
  140. LIST_PUBLIC='no'
  141. fi
  142. if [ ${#LIST_NAME} -lt 2 ]; then
  143. dialog --title $"Add mailing list" \
  144. --msgbox $"No mailing list name was given" 6 40
  145. return
  146. fi
  147. if [ ${#LIST_SUBJECT} -lt 2 ]; then
  148. if [ ${#LIST_EMAIL} -lt 2 ]; then
  149. dialog --title $"Add mailing list" \
  150. --msgbox $"No mailing list subject or address was given" 6 40
  151. return
  152. fi
  153. fi
  154. if [ ${#LIST_SUBJECT} -gt 1 ]; then
  155. ${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
  156. -s "$LIST_SUBJECT" --public $LIST_PUBLIC
  157. else
  158. if [[ "$LIST_EMAIL" != *"@"* || "$LIST_EMAIL" != *"."* ]]; then
  159. dialog --title $"Add mailing list" \
  160. --msgbox $"Unrecognised email address" 6 40
  161. return
  162. else
  163. ${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
  164. -e "$LIST_EMAIL" --public $LIST_PUBLIC
  165. fi
  166. fi
  167. dialog --title $"Add mailing list" \
  168. --msgbox $"$LIST_NAME list was added" 6 40
  169. }
  170. function email_rule_address {
  171. data=$(tempfile 2>/dev/null)
  172. trap "rm -f $data" 0 1 2 5 15
  173. dialog --backtitle $"Freedombone User Control Panel" \
  174. --title $"Create an email rule" \
  175. --form "\n" 9 65 4 \
  176. $"When email arrives from address:" 1 1 "" 1 35 24 28 \
  177. $"Move to folder:" 2 1 "" 2 35 24 28 \
  178. $"Public:" 3 1 $"no" 3 35 4 25 \
  179. 2> $data
  180. sel=$?
  181. case $sel in
  182. 1) return;;
  183. 255) return;;
  184. esac
  185. RULE_EMAIL=$(cat $data | sed -n 1p)
  186. RULE_FOLDER=$(cat $data | sed -n 2p)
  187. RULE_PUBLIC=$(cat $data | sed -n 3p)
  188. if [ ${#RULE_PUBLIC} -lt 1 ]; then
  189. RULE_PUBLIC='no'
  190. fi
  191. if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
  192. RULE_PUBLIC='yes'
  193. else
  194. RULE_PUBLIC='no'
  195. fi
  196. if [ ${#RULE_EMAIL} -lt 2 ]; then
  197. dialog --title $"Create an email rule" \
  198. --msgbox $"No email address was given" 6 40
  199. return
  200. fi
  201. if [ ${#RULE_FOLDER} -lt 2 ]; then
  202. dialog --title $"Create an email rule" \
  203. --msgbox $"No folder name was given" 6 40
  204. return
  205. fi
  206. if [[ "$RULE_EMAIL" != *"@"* || "$RULE_EMAIL" != *"."* ]]; then
  207. dialog --title $"Create an email rule" \
  208. --msgbox $"Unrecognised email address" 6 40
  209. return
  210. fi
  211. ${PROJECT_NAME}-addemail -u $USER -e "$RULE_EMAIL" \
  212. -g "$RULE_FOLDER" --public $RULE_PUBLIC
  213. dialog --title $"Create an email rule" \
  214. --msgbox $"Email rule for $RULE_EMAIL was added" 6 40
  215. }
  216. function email_rule_subject {
  217. data=$(tempfile 2>/dev/null)
  218. trap "rm -f $data" 0 1 2 5 15
  219. dialog --backtitle $"Freedombone User Control Panel" \
  220. --title $"Create an email rule" \
  221. --form "\n" 9 75 4 \
  222. $"When email arrives with subject containing:" 1 1 "" 1 45 24 28 \
  223. $"Move to folder:" 2 1 "" 2 45 24 28 \
  224. $"Public:" 3 1 $"no" 3 45 4 25 \
  225. 2> $data
  226. sel=$?
  227. case $sel in
  228. 1) return;;
  229. 255) return;;
  230. esac
  231. RULE_SUBJECT=$(cat $data | sed -n 1p)
  232. RULE_FOLDER=$(cat $data | sed -n 2p)
  233. RULE_PUBLIC=$(cat $data | sed -n 3p)
  234. if [ ${#RULE_PUBLIC} -lt 1 ]; then
  235. RULE_PUBLIC='no'
  236. fi
  237. if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
  238. RULE_PUBLIC='yes'
  239. else
  240. RULE_PUBLIC='no'
  241. fi
  242. if [ ${#RULE_SUBJECT} -lt 2 ]; then
  243. dialog --title $"Create an email rule" \
  244. --msgbox $"No subject text was given" 6 40
  245. return
  246. fi
  247. if [ ${#RULE_FOLDER} -lt 2 ]; then
  248. dialog --title $"Create an email rule" \
  249. --msgbox $"No folder name was given" 6 40
  250. return
  251. fi
  252. ${PROJECT_NAME}-addemail -u $USER -s "$RULE_SUBJECT" \
  253. -g "$RULE_FOLDER" --public $RULE_PUBLIC
  254. dialog --title $"Create an email rule" \
  255. --msgbox $"Email rule for subject '$RULE_SUBJECT' was added" 6 40
  256. }
  257. function block_unblock_email {
  258. blockstr=$"Block or unblock emails from a given address"
  259. data=$(tempfile 2>/dev/null)
  260. trap "rm -f $data" 0 1 2 5 15
  261. dialog --backtitle $"Freedombone User Control Panel" \
  262. --title "$blockstr" \
  263. --form "\n" 8 65 3 \
  264. $"When email arrives from address:" 1 1 "" 1 35 24 100 \
  265. $"Block it:" 2 1 "yes" 2 35 4 4 \
  266. 2> $data
  267. sel=$?
  268. case $sel in
  269. 1) return;;
  270. 255) return;;
  271. esac
  272. BLOCK_EMAIL=$(cat $data | sed -n 1p)
  273. BLOCK=$(cat $data | sed -n 2p)
  274. if [ ${#BLOCK_EMAIL} -lt 2 ]; then
  275. dialog --title "$blockstr" \
  276. --msgbox $"No email address was given" 6 40
  277. return
  278. fi
  279. if [[ "$BLOCK_EMAIL" != *"@"* || "$BLOCK_EMAIL" != *"."* ]]; then
  280. dialog --title "$blockstr" \
  281. --msgbox $"Unrecognised email address" 6 40
  282. return
  283. fi
  284. if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
  285. ${PROJECT_NAME}-ignore -u $USER -e "$BLOCK_EMAIL"
  286. dialog --title $"Block an email" \
  287. --msgbox "Email from $BLOCK_EMAIL is now blocked" 6 75
  288. else
  289. ${PROJECT_NAME}-unignore -u $USER -e "$BLOCK_EMAIL"
  290. dialog --title $"Unblock an email" \
  291. --msgbox "Email from $BLOCK_EMAIL is now unblocked" 6 75
  292. fi
  293. }
  294. function block_unblock_subject {
  295. blockstr=$"Block or unblock emails with text in the subject line"
  296. data=$(tempfile 2>/dev/null)
  297. trap "rm -f $data" 0 1 2 5 15
  298. dialog --backtitle $"Freedombone User Control Panel" \
  299. --title "$blockstr" \
  300. --form "\n" 8 70 3 \
  301. $"When email arrives with subject text:" 1 1 "" 1 40 24 28 \
  302. $"Block it:" 2 1 "yes" 2 40 4 4 \
  303. 2> $data
  304. sel=$?
  305. case $sel in
  306. 1) return;;
  307. 255) return;;
  308. esac
  309. BLOCK_SUBJECT=$(cat $data | sed -n 1p)
  310. BLOCK=$(cat $data | sed -n 2p)
  311. if [ ${#BLOCK_SUBJECT} -lt 2 ]; then
  312. dialog --title "$blockstr" \
  313. --msgbox $"No subject was given" 6 40
  314. return
  315. fi
  316. if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
  317. ${PROJECT_NAME}-ignore -u $USER -t "$BLOCK_SUBJECT"
  318. dialog --title $"Block an email" \
  319. --msgbox $"Email with subject $BLOCK_SUBJECT is now blocked" 6 40
  320. else
  321. ${PROJECT_NAME}-unignore -u $USER -t "$BLOCK_SUBJECT"
  322. dialog --title $"Unblock an email" \
  323. --msgbox $"Email with subject $BLOCK_SUBJECT is now unblocked" 6 40
  324. fi
  325. }
  326. function show_gpg_key {
  327. GPG_FINGERPRINT=$(gpg --fingerprint $MY_EMAIL_ADDRESS | grep -i "key fingerprint" | head -n 1 | awk -F '= ' '{print $2}')
  328. GPG_DATE=$(gpg --fingerprint $MY_EMAIL_ADDRESS | grep -i "pub" | head -n 1 | awk -F '/' '{print $2}' | awk -F ' ' '{print $2}')
  329. dialog --title $"My PGP/GPG Key" \
  330. --backtitle $"Freedombone User Control Panel" \
  331. --msgbox $"Email Address: $MY_EMAIL_ADDRESS\n\nKey ID: $GPG_ID\n\nFingerprint: $GPG_FINGERPRINT\n\nCreated: $GPG_DATE" 12 70
  332. }
  333. function publish_gpg_key {
  334. gpg --send-key $GPG_ID
  335. dialog --title $"Publish your PGP/GPG key" \
  336. --msgbox $"Your key has now been published" 6 40
  337. }
  338. function refresh_gpg_keys {
  339. gpg --refresh-keys
  340. dialog --title $"Refresh PGP/GPG keys" \
  341. --msgbox $"Your keys have been refreshed" 6 40
  342. }
  343. function add_gpg_key {
  344. data=$(tempfile 2>/dev/null)
  345. trap "rm -f $data" 0 1 2 5 15
  346. dialog --title $"Add someone's PGP/GPG key" \
  347. --backtitle $"Freedombone User Control Panel" \
  348. --inputbox $"Enter their email address or Key ID below" 8 60 2>$data
  349. sel=$?
  350. case $sel in
  351. 0)
  352. ADD_EMAIL_ADDRESS=$(<$data)
  353. if [ ${#ADD_EMAIL_ADDRESS} -gt 2 ]; then
  354. address_is_valid=
  355. if [[ $ADD_EMAIL_ADDRESS == *"@"* && $ADD_EMAIL_ADDRESS == *"."* ]]; then
  356. address_is_valid=1
  357. else
  358. if [[ $ADD_EMAIL_ADDRESS == "0x"* ]]; then
  359. address_is_valid=1
  360. fi
  361. fi
  362. if [ $address_is_valid ]; then
  363. clear
  364. gpg --search-keys $ADD_EMAIL_ADDRESS
  365. else
  366. dialog --title $"Unrecognised email address" \
  367. --backtitle $"Freedombone User Control Panel" \
  368. --msgbox $"This doesn't look like an email address or key ID" 6 50
  369. fi
  370. fi
  371. ;;
  372. esac
  373. }
  374. function remove_gpg_key {
  375. data=$(tempfile 2>/dev/null)
  376. trap "rm -f $data" 0 1 2 5 15
  377. dialog --title $"Remove someone's PGP/GPG key" \
  378. --backtitle $"Freedombone User Control Panel" \
  379. --inputbox $"Enter their email address below" 8 60 2>$data
  380. sel=$?
  381. case $sel in
  382. 0)
  383. REMOVE_EMAIL_ADDRESS=$(<$data)
  384. if [ ${#REMOVE_EMAIL_ADDRESS} -gt 2 ]; then
  385. if [[ $REMOVE_EMAIL_ADDRESS == *"@"* && $REMOVE_EMAIL_ADDRESS == *"."* ]]; then
  386. if [[ $REMOVE_EMAIL_ADDRESS != $MY_EMAIL_ADDRESS ]]; then
  387. clear
  388. gpg --delete-key $REMOVE_EMAIL_ADDRESS
  389. else
  390. dialog --title $"Remove someone's PGP/GPG key" \
  391. --backtitle $"Freedombone User Control Panel" \
  392. --msgbox $"It's not a good idea to remove your own encryption key" 6 65
  393. fi
  394. else
  395. dialog --title $"Unrecognised email address" \
  396. --backtitle $"Freedombone User Control Panel" \
  397. --msgbox $"This doesn't look like an email address" 6 50
  398. fi
  399. fi
  400. ;;
  401. esac
  402. }
  403. function add_ssh_key {
  404. data=$(tempfile 2>/dev/null)
  405. trap "rm -f $data" 0 1 2 5 15
  406. dialog --title $"Add an ssh key for logging in" \
  407. --backtitle $"Freedombone User Control Panel" \
  408. --inputbox $"This will allow you to log into ${PROJECT_NAME} if you have an ssh key on your system, and provides much stronger security than simply using a login password.\n\nWARNING: If you make any mistakes here then you may not be able to log in and will need to get the administrator to clear your ssh authorized_keys file." 15 60 2>$data
  409. sel=$?
  410. case $sel in
  411. 0)
  412. SSH_PUBLIC_KEY=$(<$data)
  413. if [ ${#SSH_PUBLIC_KEY} -gt 20 ]; then
  414. if [[ "$SSH_PUBLIC_KEY" == "ssh-"* ]]; then
  415. if [ ! -d /home/$USER/.ssh ]; then
  416. mkdir /home/$USER/.ssh
  417. fi
  418. if [ ! -f /home/$USER/.ssh/authorized_keys ]; then
  419. touch /home/$USER/.ssh/authorized_keys
  420. fi
  421. if ! grep -q "$SSH_PUBLIC_KEY" /home/$USER/.ssh/authorized_keys; then
  422. echo "$SSH_PUBLIC_KEY" >> /home/$USER/.ssh/authorized_keys
  423. dialog --title $"New ssh key added" \
  424. --backtitle $"Freedombone User Control Panel" \
  425. --msgbox $"Your ssh key has now been added" 6 50
  426. else
  427. dialog --title $"ssh key already added" \
  428. --backtitle $"Freedombone User Control Panel" \
  429. --msgbox $"That ssh key has already been added" 6 50
  430. fi
  431. else
  432. dialog --title $"Unrecognised ssh public key" \
  433. --backtitle $"Freedombone User Control Panel" \
  434. --msgbox $"This doesn't look like an ssh key" 6 50
  435. fi
  436. fi
  437. ;;
  438. esac
  439. }
  440. function remove_ssh_key {
  441. data=$(tempfile 2>/dev/null)
  442. trap "rm -f $data" 0 1 2 5 15
  443. dialog --title $"Remove an ssh key for logging in" \
  444. --backtitle $"Freedombone User Control Panel" \
  445. --inputbox $"Enter the ssh public key which is to be removed. This can be just the address at the end.\n\nWARNING: If you make any mistakes here then you may not be able to log in and will need to get the administrator to clear your ssh authorized_keys file." 15 60 2>$data
  446. sel=$?
  447. case $sel in
  448. 0)
  449. SSH_PUBLIC_KEY=$(<$data)
  450. if [ ${#SSH_PUBLIC_KEY} -gt 5 ]; then
  451. if [ -f /home/$USER/.ssh/authorized_keys ]; then
  452. sed -i "s|.*${SSH_PUBLIC_KEY}.*||g" /home/$USER/.ssh/authorized_keys
  453. dialog --title $"Remove an ssh public key" \
  454. --backtitle $"Freedombone User Control Panel" \
  455. --msgbox $"The ssh key has been removed" 6 50
  456. fi
  457. fi
  458. ;;
  459. esac
  460. }
  461. function smtp_proxy {
  462. MUTTRC_FILE=/home/$USER/.muttrc
  463. if [ ! -f $MUTTRC_FILE ]; then
  464. return
  465. fi
  466. SMTP_PROXY_ENABLE=$'no'
  467. SMTP_PROXY_PROTOCOL='smtps'
  468. SMTP_PROXY_SERVER='mail.myispdomain'
  469. SMTP_PROXY_PORT=465
  470. SMTP_PROXY_USERNAME=''
  471. SMTP_PROXY_PASSWORD=''
  472. if grep -q "set smtp_url" $MUTTRC_FILE; then
  473. if grep -q "#set smtp_url" $MUTTRC_FILE; then
  474. SMTP_PROXY_ENABLE=$'no'
  475. else
  476. SMTP_PROXY_ENABLE=$'yes'
  477. fi
  478. SMTP_PROXY_PROTOCOL=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F ':' '{print $1}')
  479. SMTP_PROXY_SERVER=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $2}' | awk -F '@' '{print $2}')
  480. SMTP_PROXY_PORT=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F ':' '{print $4}' | awk -F '/' '{print $1}')
  481. SMTP_PROXY_USERNAME=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $1}')
  482. SMTP_PROXY_PASSWORD=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $2}' | awk -F '@' '{print $1}')
  483. fi
  484. data=$(tempfile 2>/dev/null)
  485. trap "rm -f $data" 0 1 2 5 15
  486. dialog --backtitle $"Freedombone Control Panel" \
  487. --title $"SMTP Proxy for $USER" \
  488. --form $"You may need to proxy outgoing email via your ISP's mail server. If so enter the details below." 14 75 6 \
  489. $"Enable proxy:" 1 1 "$SMTP_PROXY_ENABLE" 1 24 5 5 \
  490. $"Protocol (smtp/smtps):" 2 1 "$SMTP_PROXY_PROTOCOL" 2 24 5 5 \
  491. $"ISP mail server:" 3 1 "$SMTP_PROXY_SERVER" 3 24 40 10000 \
  492. $"Port:" 4 1 "$SMTP_PROXY_PORT" 4 24 5 5 \
  493. $"Username:" 5 1 "$SMTP_PROXY_USERNAME" 5 24 40 10000 \
  494. $"Password:" 6 1 "$SMTP_PROXY_PASSWORD" 6 24 40 10000 \
  495. 2> $data
  496. sel=$?
  497. case $sel in
  498. 1) return;;
  499. 255) return;;
  500. esac
  501. SMTP_PROXY_ENABLE=$(cat $data | sed -n 1p)
  502. SMTP_PROXY_PROTOCOL=$(cat $data | sed -n 2p)
  503. SMTP_PROXY_SERVER=$(cat $data | sed -n 3p)
  504. SMTP_PROXY_PORT=$(cat $data | sed -n 4p)
  505. SMTP_PROXY_USERNAME=$(cat $data | sed -n 5p)
  506. SMTP_PROXY_PASSWORD=$(cat $data | sed -n 6p)
  507. # change muttrc
  508. if [ $SMTP_PROXY_ENABLE != $'no' ]; then
  509. if ! grep -q "set smtp_url" $MUTTRC_FILE; then
  510. echo "set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"" >> $MUTTRC_FILE
  511. else
  512. sed -i "s|set smtp_url=.*|set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"|g" $MUTTRC_FILE
  513. fi
  514. sed -i 's|#set smtp_url|set smtp_url|g' $MUTTRC_FILE
  515. else
  516. if grep "set smtp_url" $MUTTRC_FILE; then
  517. sed -i 's|set smtp_url|#set smtp_url|g' $MUTTRC_FILE
  518. fi
  519. fi
  520. }
  521. function syncthing_create_ids_file {
  522. if [ ! -f ~/.syncthing-server-id ]; then
  523. return
  524. fi
  525. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  526. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  527. echo $'# Your syncthing configuration file' > $SYNCTHING_CONFIG_FILE
  528. echo '#' >> $SYNCTHING_CONFIG_FILE
  529. echo $"# The ${PROJECT_NAME} syncthing ID is: $SYNCTHING_ID" >> $SYNCTHING_CONFIG_FILE
  530. echo '#' >> $SYNCTHING_CONFIG_FILE
  531. echo '# Paste the IDs of your devices below' >> $SYNCTHING_CONFIG_FILE
  532. echo '#' >> $SYNCTHING_CONFIG_FILE
  533. fi
  534. }
  535. function syncthing_manual_edit {
  536. if [ ! -f ~/.syncthing-server-id ]; then
  537. return
  538. fi
  539. syncthing_create_ids_file
  540. editor $SYNCTHING_CONFIG_FILE
  541. # force an update of the configuration
  542. touch ~/.syncthing-update
  543. }
  544. function syncthing_show_id {
  545. if [ ! -f ~/.syncthing-server-id ]; then
  546. return
  547. fi
  548. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  549. dialog --title $"Device ID for ${PROJECT_NAME}" \
  550. --backtitle $"Freedombone User Control Panel" \
  551. --msgbox $"In a desktop terminal press shift and select the ID below,\nthen right click and copy.\n\nWithin Connectbot select Menu/Copy and then highlight the ID below\n\n$SYNCTHING_ID" 12 78
  552. }
  553. function syncthing_add_id {
  554. if [ ! -f ~/.syncthing-server-id ]; then
  555. return
  556. fi
  557. syncthing_create_ids_file
  558. data=$(tempfile 2>/dev/null)
  559. trap "rm -f $data" 0 1 2 5 15
  560. dialog --backtitle $"Freedombone User Control Panel" \
  561. --title $"Add a Syncthing device ID" \
  562. --form $"Paste the device ID for your laptop/desktop/netbook/phone/tablet below" 9 80 2 \
  563. $"Device ID:" 1 1 "" 1 26 80 80 \
  564. $"Description (optional):" 2 1 "" 2 26 80 80 \
  565. 2> $data
  566. sel=$?
  567. case $sel in
  568. 1) return;;
  569. 255) return;;
  570. esac
  571. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  572. SYNCTHING_DESCRIPTION=$(cat $data | sed -n 2p)
  573. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  574. return
  575. fi
  576. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  577. dialog --title $"Add a Syncthing device ID" \
  578. --backtitle $"Freedombone User Control Panel" \
  579. --msgbox $"That doesn't look like a device ID" 6 50
  580. return
  581. fi
  582. if grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  583. dialog --title $"Add a Syncthing device ID" \
  584. --backtitle $"Freedombone User Control Panel" \
  585. --msgbox $"That ID has already been added" 6 50
  586. return
  587. fi
  588. if [ ${#SYNCTHING_DESCRIPTION} -gt 0 ]; then
  589. echo "# $SYNCTHING_DESCRIPTION" >> $SYNCTHING_CONFIG_FILE
  590. fi
  591. echo "$SYNCTHING_DEVICE_ID" >> $SYNCTHING_CONFIG_FILE
  592. # force an update of the configuration
  593. touch ~/.syncthing-update
  594. dialog --title $"Add a Syncthing device ID" \
  595. --backtitle $"Freedombone User Control Panel" \
  596. --msgbox $"The ID was added" 6 50
  597. }
  598. function syncthing_remove_id {
  599. if [ ! -f ~/.syncthing-server-id ]; then
  600. return
  601. fi
  602. syncthing_create_ids_file
  603. data=$(tempfile 2>/dev/null)
  604. trap "rm -f $data" 0 1 2 5 15
  605. dialog --backtitle $"Freedombone User Control Panel" \
  606. --title $"Remove a Syncthing device ID" \
  607. --form $"Paste the device ID which is to be removed below" 8 80 1 \
  608. $"Device ID:" 1 1 "" 1 14 80 80 \
  609. 2> $data
  610. sel=$?
  611. case $sel in
  612. 1) return;;
  613. 255) return;;
  614. esac
  615. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  616. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  617. return
  618. fi
  619. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  620. dialog --title $"Remove a Syncthing device ID" \
  621. --backtitle $"Freedombone User Control Panel" \
  622. --msgbox $"That doesn't look like a device ID" 6 50
  623. return
  624. fi
  625. if ! grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  626. dialog --title $"Remove a Syncthing device ID" \
  627. --backtitle $"Freedombone User Control Panel" \
  628. --msgbox $"That ID wasn't registered anyway" 6 50
  629. return
  630. fi
  631. sed -i "/$SYNCTHING_DEVICE_ID/d" $SYNCTHING_CONFIG_FILE
  632. # force an update of the configuration
  633. touch ~/.syncthing-update
  634. dialog --title $"Remove a Syncthing device ID" \
  635. --backtitle $"Freedombone User Control Panel" \
  636. --msgbox $"The ID was removed" 6 50
  637. }
  638. function sign_gpg_key {
  639. data=$(tempfile 2>/dev/null)
  640. trap "rm -f $data" 0 1 2 5 15
  641. dialog --title $"Sign a PGP/GPG key or website domain" \
  642. --backtitle $"Freedombone User Control Panel" \
  643. --inputbox $"Enter the Key ID, address or domain to be signed.\n\nIf you are signing a website domain then include the https:// at the beginning.\n\nIf you are signing an ssh key then include ssh:// before the domain name." 14 75 2>$data
  644. sel=$?
  645. case $sel in
  646. 0)
  647. SIGN_ADDRESS=$(<$data)
  648. if [ ${#SIGN_ADDRESS} -gt 2 ]; then
  649. clear
  650. gpg --search "$SIGN_ADDRESS"
  651. fpr=$(gpg --with-colons --fingerprint "$SIGN_ADDRESS" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  652. if [ ${#fpr} -gt 2 ]; then
  653. gpg --sign-key $fpr
  654. if [ "$?" = "0" ]; then
  655. gpg --update-trustdb
  656. dialog --title $"Sign a PGP/GPG key" \
  657. --backtitle $"Freedombone User Control Panel" \
  658. --msgbox $"$SIGN_ADDRESS was signed" 6 50
  659. fi
  660. fi
  661. fi
  662. ;;
  663. esac
  664. }
  665. function gpg_key_trust {
  666. data=$(tempfile 2>/dev/null)
  667. trap "rm -f $data" 0 1 2 5 15
  668. dialog --title $"Trust a PGP/GPG key or website domain" \
  669. --backtitle $"Freedombone User Control Panel" \
  670. --inputbox $"Enter the Key ID, address or domain to be trusted.\n\nIf you are trusting a website domain then include the https:// at the beginning.\n\nIf you are trusting an ssh key then include ssh:// before the domain name." 14 75 2>$data
  671. sel=$?
  672. case $sel in
  673. 0)
  674. TRUST_ADDRESS=$(<$data)
  675. if [ ${#TRUST_ADDRESS} -gt 2 ]; then
  676. clear
  677. gpg --search "$TRUST_ADDRESS"
  678. fpr=$(gpg --with-colons --fingerprint "$TRUST_ADDRESS" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  679. if [ ${#fpr} -gt 2 ]; then
  680. data=$(tempfile 2>/dev/null)
  681. trap "rm -f $data" 0 1 2 5 15
  682. dialog --backtitle $"Freedombone User Control Panel" \
  683. --title $"Trust a PGP/GPG key or website domain" \
  684. --radiolist $"Set the trust level for $TRUST_ADDRESS:" 18 70 10 \
  685. 1 $"I don't know or won't say" off \
  686. 2 $"I do NOT trust" off \
  687. 3 $"I trust marginally" on \
  688. 4 $"I trust fully" off \
  689. 5 $"I trust ultimately" off 2> $data
  690. sel=$?
  691. case $sel in
  692. 1) return;;
  693. 255) return;;
  694. esac
  695. TRUST_LEVEL=$(cat $data)
  696. if [ ${TRUST_LEVEL} -ge 1 ] ; then
  697. if [ ${TRUST_LEVEL} -le 5 ] ; then
  698. echo -e "trust\n${TRUST_LEVEL}\ny\nsave\n" | gpg --command-fd 0 --edit-key $fpr
  699. if [ "$?" = "0" ]; then
  700. gpg --update-trustdb
  701. dialog --title $"Trust a PGP/GPG key or website domain" \
  702. --backtitle $"Freedombone User Control Panel" \
  703. --msgbox $"$TRUST_ADDRESS was set to trust level ${TRUST_LEVEL}" 6 50
  704. fi
  705. fi
  706. fi
  707. fi
  708. fi
  709. ;;
  710. esac
  711. }
  712. function menu_encryption_keys {
  713. while true
  714. do
  715. data=$(tempfile 2>/dev/null)
  716. trap "rm -f $data" 0 1 2 5 15
  717. dialog --backtitle $"Freedombone User Control Panel" \
  718. --title $"My Encryption Keys" \
  719. --radiolist $"Choose an operation:" 18 70 10 \
  720. 1 $"Show your PGP/GPG key" off \
  721. 2 $"Publish your PGP/GPG key so that others can find it" off \
  722. 3 $"Add someone's PGP/GPG key" off \
  723. 4 $"Remove someone's PGP/GPG key" off \
  724. 5 $"Sign a PGP/GPG key or website domain" off \
  725. 6 $"Refresh your PGP/GPG keys" off \
  726. 7 $"Add an ssh key for logging in" off \
  727. 8 $"Remove an ssh key for logging in" off \
  728. 9 $"Set the trust level for a PGP/GPG key" off \
  729. 10 $"Back to main menu" on 2> $data
  730. sel=$?
  731. case $sel in
  732. 1) break;;
  733. 255) break;;
  734. esac
  735. case $(cat $data) in
  736. 1) show_gpg_key;;
  737. 2) publish_gpg_key;;
  738. 3) add_gpg_key;;
  739. 4) remove_gpg_key;;
  740. 5) sign_gpg_key;;
  741. 6) refresh_gpg_keys;;
  742. 7) add_ssh_key;;
  743. 8) remove_ssh_key;;
  744. 9) gpg_key_trust;;
  745. 10) break;;
  746. esac
  747. done
  748. }
  749. function menu_email {
  750. while true
  751. do
  752. data=$(tempfile 2>/dev/null)
  753. trap "rm -f $data" 0 1 2 5 15
  754. dialog --backtitle $"Freedombone User Control Panel" \
  755. --title $"Change Email Filtering Rules" \
  756. --radiolist $"Choose an operation:" 14 70 7 \
  757. 1 $"Add yourself to a mailing list" off \
  758. 2 $"Remove yourself from a mailing list" off \
  759. 3 $"Add an email rule for an address" off \
  760. 4 $"Add an email rule for a subject" off \
  761. 5 $"Block or unblock an email address" off \
  762. 6 $"Block or unblock email with subject text" off \
  763. 7 $"Back to main menu" on 2> $data
  764. sel=$?
  765. case $sel in
  766. 1) break;;
  767. 255) break;;
  768. esac
  769. case $(cat $data) in
  770. 1) add_to_mailing_list;;
  771. 2) remove_user_from_mailing_list;;
  772. 3) email_rule_address;;
  773. 4) email_rule_subject;;
  774. 5) block_unblock_email;;
  775. 6) block_unblock_subject;;
  776. 7) break;;
  777. esac
  778. done
  779. }
  780. function menu_syncthing {
  781. while true
  782. do
  783. data=$(tempfile 2>/dev/null)
  784. trap "rm -f $data" 0 1 2 5 15
  785. dialog --backtitle $"Freedombone User Control Panel" \
  786. --title $"File Synchronization" \
  787. --radiolist $"Choose an operation:" 12 70 6 \
  788. 1 $"Show device ID for ${PROJECT_NAME}" off \
  789. 2 $"Add an ID for another machine or device" off \
  790. 3 $"Remove an ID for another machine or device" off \
  791. 4 $"Manually edit device IDs" off \
  792. 5 $"Back to main menu" on 2> $data
  793. sel=$?
  794. case $sel in
  795. 1) break;;
  796. 255) break;;
  797. esac
  798. case $(cat $data) in
  799. 1) syncthing_show_id;;
  800. 2) syncthing_add_id;;
  801. 3) syncthing_remove_id;;
  802. 4) syncthing_manual_edit;;
  803. 5) break;;
  804. esac
  805. done
  806. }
  807. function menu_admin {
  808. if [ ! -f /etc/sudoers ]; then
  809. clear
  810. exit 0
  811. fi
  812. sudo freedombone-controlpanel
  813. }
  814. function sign_keys {
  815. if [ ! -f /home/$USER/.monkeysphere/server_keys ]; then
  816. return
  817. fi
  818. dialog --title $"Monkeysphere sign server keys" \
  819. --backtitle $"Freedombone Security Configuration" \
  820. --defaultno \
  821. --yesno $"\nMonkeysphere has been enabled and you will need to sign and trust the server keys. Do you want to do that now?" 8 60
  822. sel=$?
  823. case $sel in
  824. 0) ${PROJECT_NAME}-sec --sign yes;;
  825. esac
  826. }
  827. function menu_top_level {
  828. while true
  829. do
  830. data=$(tempfile 2>/dev/null)
  831. trap "rm -f $data" 0 1 2 5 15
  832. dialog --backtitle $"Freedombone User Control Panel" \
  833. --title $"User Control Panel" \
  834. --radiolist $"Choose an operation:" 19 50 12 \
  835. 1 $"Use Email" off \
  836. 2 $"Change Email Filtering Rules" off \
  837. 3 $"Use Tox Chat" off \
  838. 4 $"Use XMPP Chat" off \
  839. 5 $"Use IRC" off \
  840. 6 $"Browse the Web" off \
  841. 7 $"File Synchronization" off \
  842. 8 $"My Encryption Keys" off \
  843. 9 $"Set an outgoing email proxy" off \
  844. 10 $"Administrator controls" off \
  845. 11 $"Exit to the command line" off \
  846. 12 $"Log out" on 2> $data
  847. sel=$?
  848. case $sel in
  849. 1) exit 1;;
  850. 255) exit 1;;
  851. esac
  852. case $(cat $data) in
  853. 1) mutt;;
  854. 2) menu_email;;
  855. 3) toxic --force-tcp --SOCKS5-proxy 127.0.0.1 9050;;
  856. 4) torify profanity;;
  857. 5) irssi;;
  858. 6) torify elinks;;
  859. 7) menu_syncthing;;
  860. 8) menu_encryption_keys;;
  861. 9) smtp_proxy;;
  862. 10) menu_admin;;
  863. 11) break;;
  864. 12) kill -HUP `pgrep -s 0 -o`;;
  865. esac
  866. done
  867. }
  868. sign_keys
  869. menu_top_level
  870. clear
  871. . ~/.bashrc
  872. exit 0