freedombone-controlpanel-user 34KB

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