freedombone-controlpanel-user 34KB

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