freedombone-controlpanel-user 32KB

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