freedombone-controlpanel-user 30KB

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