freedombone-controlpanel-user 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 publish_gpg_key {
  376. gpg --send-key $GPG_ID
  377. if [ $GPG_BACKUP_ID ]; then
  378. gpg --send-key $GPG_BACKUP_ID
  379. fi
  380. dialog --title $"Publish your PGP/GPG key" \
  381. --msgbox $"Your key has now been published" 6 40
  382. }
  383. function refresh_gpg_keys {
  384. gpg --refresh-keys
  385. dialog --title $"Refresh PGP/GPG keys" \
  386. --msgbox $"Your keys have been refreshed" 6 40
  387. }
  388. function add_gpg_key {
  389. data=$(tempfile 2>/dev/null)
  390. trap "rm -f $data" 0 1 2 5 15
  391. dialog --title $"Add someone's PGP/GPG key" \
  392. --backtitle $"Freedombone User Control Panel" \
  393. --inputbox $"Enter their email address or Key ID below" 8 60 2>$data
  394. sel=$?
  395. case $sel in
  396. 0)
  397. ADD_EMAIL_ADDRESS=$(<$data)
  398. if [ ${#ADD_EMAIL_ADDRESS} -gt 2 ]; then
  399. address_is_valid=
  400. if [[ $ADD_EMAIL_ADDRESS == *"@"* && $ADD_EMAIL_ADDRESS == *"."* ]]; then
  401. address_is_valid=1
  402. else
  403. if [[ $ADD_EMAIL_ADDRESS == "0x"* ]]; then
  404. address_is_valid=1
  405. fi
  406. fi
  407. if [ $address_is_valid ]; then
  408. clear
  409. gpg --search-keys "$ADD_EMAIL_ADDRESS"
  410. gpg_set_trust "$ADD_EMAIL_ADDRESS"
  411. else
  412. dialog --title $"Unrecognised email address" \
  413. --backtitle $"Freedombone User Control Panel" \
  414. --msgbox $"This doesn't look like an email address or key ID" 6 50
  415. fi
  416. fi
  417. ;;
  418. esac
  419. }
  420. function remove_gpg_key {
  421. data=$(tempfile 2>/dev/null)
  422. trap "rm -f $data" 0 1 2 5 15
  423. dialog --title $"Remove someone's PGP/GPG key" \
  424. --backtitle $"Freedombone User Control Panel" \
  425. --inputbox $"Enter their email address below" 8 60 2>$data
  426. sel=$?
  427. case $sel in
  428. 0)
  429. REMOVE_EMAIL_ADDRESS=$(<$data)
  430. if [ ${#REMOVE_EMAIL_ADDRESS} -gt 2 ]; then
  431. if [[ $REMOVE_EMAIL_ADDRESS == *"@"* && $REMOVE_EMAIL_ADDRESS == *"."* ]]; then
  432. if [[ $REMOVE_EMAIL_ADDRESS != $MY_EMAIL_ADDRESS ]]; then
  433. clear
  434. gpg --delete-key $REMOVE_EMAIL_ADDRESS
  435. else
  436. dialog --title $"Remove someone's PGP/GPG key" \
  437. --backtitle $"Freedombone User Control Panel" \
  438. --msgbox $"It's not a good idea to remove your own encryption key" 6 65
  439. fi
  440. else
  441. dialog --title $"Unrecognised email address" \
  442. --backtitle $"Freedombone User Control Panel" \
  443. --msgbox $"This doesn't look like an email address" 6 50
  444. fi
  445. fi
  446. ;;
  447. esac
  448. }
  449. function add_ssh_key {
  450. data=$(tempfile 2>/dev/null)
  451. trap "rm -f $data" 0 1 2 5 15
  452. dialog --title $"Add an ssh key for logging in" \
  453. --backtitle $"Freedombone User Control Panel" \
  454. --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
  455. sel=$?
  456. case $sel in
  457. 0)
  458. SSH_PUBLIC_KEY=$(<$data)
  459. if [ ${#SSH_PUBLIC_KEY} -gt 20 ]; then
  460. if [[ "$SSH_PUBLIC_KEY" == "ssh-"* ]]; then
  461. if [ ! -d /home/$USER/.ssh ]; then
  462. mkdir /home/$USER/.ssh
  463. fi
  464. if [ ! -f /home/$USER/.ssh/authorized_keys ]; then
  465. touch /home/$USER/.ssh/authorized_keys
  466. fi
  467. if ! grep -q "$SSH_PUBLIC_KEY" /home/$USER/.ssh/authorized_keys; then
  468. echo "$SSH_PUBLIC_KEY" >> /home/$USER/.ssh/authorized_keys
  469. dialog --title $"New ssh key added" \
  470. --backtitle $"Freedombone User Control Panel" \
  471. --msgbox $"Your ssh key has now been added" 6 50
  472. else
  473. dialog --title $"ssh key already added" \
  474. --backtitle $"Freedombone User Control Panel" \
  475. --msgbox $"That ssh key has already been added" 6 50
  476. fi
  477. else
  478. dialog --title $"Unrecognised ssh public key" \
  479. --backtitle $"Freedombone User Control Panel" \
  480. --msgbox $"This doesn't look like an ssh key" 6 50
  481. fi
  482. fi
  483. ;;
  484. esac
  485. }
  486. function remove_ssh_key {
  487. data=$(tempfile 2>/dev/null)
  488. trap "rm -f $data" 0 1 2 5 15
  489. dialog --title $"Remove an ssh key for logging in" \
  490. --backtitle $"Freedombone User Control Panel" \
  491. --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
  492. sel=$?
  493. case $sel in
  494. 0)
  495. SSH_PUBLIC_KEY=$(<$data)
  496. if [ ${#SSH_PUBLIC_KEY} -gt 5 ]; then
  497. if [ -f /home/$USER/.ssh/authorized_keys ]; then
  498. sed -i "s|.*${SSH_PUBLIC_KEY}.*||g" /home/$USER/.ssh/authorized_keys
  499. dialog --title $"Remove an ssh public key" \
  500. --backtitle $"Freedombone User Control Panel" \
  501. --msgbox $"The ssh key has been removed" 6 50
  502. fi
  503. fi
  504. ;;
  505. esac
  506. }
  507. function smtp_proxy {
  508. MUTTRC_FILE=/home/$USER/.muttrc
  509. if [ ! -f $MUTTRC_FILE ]; then
  510. return
  511. fi
  512. SMTP_PROXY_ENABLE=$'no'
  513. SMTP_PROXY_PROTOCOL='smtps'
  514. SMTP_PROXY_SERVER='mail.myispdomain'
  515. SMTP_PROXY_PORT=465
  516. SMTP_PROXY_USERNAME=''
  517. SMTP_PROXY_PASSWORD=''
  518. if grep -q "set smtp_url" $MUTTRC_FILE; then
  519. if grep -q "#set smtp_url" $MUTTRC_FILE; then
  520. SMTP_PROXY_ENABLE=$'no'
  521. else
  522. SMTP_PROXY_ENABLE=$'yes'
  523. fi
  524. SMTP_PROXY_PROTOCOL=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F ':' '{print $1}')
  525. 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}')
  526. SMTP_PROXY_PORT=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F ':' '{print $4}' | awk -F '/' '{print $1}')
  527. SMTP_PROXY_USERNAME=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $1}')
  528. 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}')
  529. fi
  530. data=$(tempfile 2>/dev/null)
  531. trap "rm -f $data" 0 1 2 5 15
  532. dialog --backtitle $"Freedombone Control Panel" \
  533. --title $"SMTP Proxy for $USER" \
  534. --form $"You may need to proxy outgoing email via your ISP's mail server. If so enter the details below." 14 75 6 \
  535. $"Enable proxy:" 1 1 "$SMTP_PROXY_ENABLE" 1 24 5 5 \
  536. $"Protocol (smtp/smtps):" 2 1 "$SMTP_PROXY_PROTOCOL" 2 24 5 5 \
  537. $"ISP mail server:" 3 1 "$SMTP_PROXY_SERVER" 3 24 40 10000 \
  538. $"Port:" 4 1 "$SMTP_PROXY_PORT" 4 24 5 5 \
  539. $"Username:" 5 1 "$SMTP_PROXY_USERNAME" 5 24 40 10000 \
  540. $"Password:" 6 1 "$SMTP_PROXY_PASSWORD" 6 24 40 10000 \
  541. 2> $data
  542. sel=$?
  543. case $sel in
  544. 1) return;;
  545. 255) return;;
  546. esac
  547. SMTP_PROXY_ENABLE=$(cat $data | sed -n 1p)
  548. SMTP_PROXY_PROTOCOL=$(cat $data | sed -n 2p)
  549. SMTP_PROXY_SERVER=$(cat $data | sed -n 3p)
  550. SMTP_PROXY_PORT=$(cat $data | sed -n 4p)
  551. SMTP_PROXY_USERNAME=$(cat $data | sed -n 5p)
  552. SMTP_PROXY_PASSWORD=$(cat $data | sed -n 6p)
  553. # change muttrc
  554. if [ $SMTP_PROXY_ENABLE != $'no' ]; then
  555. if ! grep -q "set smtp_url" $MUTTRC_FILE; then
  556. echo "set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"" >> $MUTTRC_FILE
  557. else
  558. 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
  559. fi
  560. sed -i 's|#set smtp_url|set smtp_url|g' $MUTTRC_FILE
  561. else
  562. if grep "set smtp_url" $MUTTRC_FILE; then
  563. sed -i 's|set smtp_url|#set smtp_url|g' $MUTTRC_FILE
  564. fi
  565. fi
  566. }
  567. function sign_gpg_key {
  568. data=$(tempfile 2>/dev/null)
  569. trap "rm -f $data" 0 1 2 5 15
  570. dialog --title $"Sign a PGP/GPG key or website domain" \
  571. --backtitle $"Freedombone User Control Panel" \
  572. --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
  573. sel=$?
  574. case $sel in
  575. 0)
  576. SIGN_ADDRESS=$(<$data)
  577. if [ ${#SIGN_ADDRESS} -gt 2 ]; then
  578. clear
  579. gpg --search "$SIGN_ADDRESS"
  580. fpr=$(gpg --with-colons --fingerprint "$SIGN_ADDRESS" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  581. if [ ${#fpr} -gt 2 ]; then
  582. gpg --sign-key $fpr
  583. if [ "$?" = "0" ]; then
  584. gpg --update-trustdb
  585. dialog --title $"Sign a PGP/GPG key" \
  586. --backtitle $"Freedombone User Control Panel" \
  587. --msgbox $"$SIGN_ADDRESS was signed" 6 50
  588. fi
  589. fi
  590. fi
  591. ;;
  592. esac
  593. }
  594. function gpg_key_trust {
  595. data=$(tempfile 2>/dev/null)
  596. trap "rm -f $data" 0 1 2 5 15
  597. dialog --title $"Trust a PGP/GPG key or website domain" \
  598. --backtitle $"Freedombone User Control Panel" \
  599. --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
  600. sel=$?
  601. case $sel in
  602. 0)
  603. TRUST_ADDRESS=$(<$data)
  604. if [ ${#TRUST_ADDRESS} -gt 2 ]; then
  605. clear
  606. gpg --search "$TRUST_ADDRESS"
  607. gpg_set_trust "$TRUST_ADDRESS"
  608. fi
  609. ;;
  610. esac
  611. }
  612. function menu_encryption_keys {
  613. while true
  614. do
  615. data=$(tempfile 2>/dev/null)
  616. trap "rm -f $data" 0 1 2 5 15
  617. dialog --backtitle $"Freedombone User Control Panel" \
  618. --title $"My Encryption Keys" \
  619. --radiolist $"Choose an operation:" 18 70 10 \
  620. 1 $"Show your PGP/GPG key" off \
  621. 2 $"Publish your PGP/GPG key so that others can find it" off \
  622. 3 $"Add someone's PGP/GPG key" off \
  623. 4 $"Remove someone's PGP/GPG key" off \
  624. 5 $"Sign a PGP/GPG key or website domain" off \
  625. 6 $"Refresh your PGP/GPG keys" off \
  626. 7 $"Add an ssh key for logging in" off \
  627. 8 $"Remove an ssh key for logging in" off \
  628. 9 $"Set the trust level for a PGP/GPG key" off \
  629. 10 $"Back to main menu" on 2> $data
  630. sel=$?
  631. case $sel in
  632. 1) break;;
  633. 255) break;;
  634. esac
  635. case $(cat $data) in
  636. 1) show_gpg_key;;
  637. 2) publish_gpg_key;;
  638. 3) add_gpg_key;;
  639. 4) remove_gpg_key;;
  640. 5) sign_gpg_key;;
  641. 6) refresh_gpg_keys;;
  642. 7) add_ssh_key;;
  643. 8) remove_ssh_key;;
  644. 9) gpg_key_trust;;
  645. 10) break;;
  646. esac
  647. done
  648. }
  649. function menu_email {
  650. while true
  651. do
  652. data=$(tempfile 2>/dev/null)
  653. trap "rm -f $data" 0 1 2 5 15
  654. dialog --backtitle $"Freedombone User Control Panel" \
  655. --title $"Change Email Filtering Rules" \
  656. --radiolist $"Choose an operation:" 14 70 7 \
  657. 1 $"Add yourself to a mailing list" off \
  658. 2 $"Remove yourself from a mailing list" off \
  659. 3 $"Add an email rule for an address" off \
  660. 4 $"Add an email rule for a subject" off \
  661. 5 $"Block or unblock an email address" off \
  662. 6 $"Block or unblock email with subject text" off \
  663. 7 $"Back to main menu" on 2> $data
  664. sel=$?
  665. case $sel in
  666. 1) break;;
  667. 255) break;;
  668. esac
  669. case $(cat $data) in
  670. 1) add_to_mailing_list;;
  671. 2) remove_user_from_mailing_list;;
  672. 3) email_rule_address;;
  673. 4) email_rule_subject;;
  674. 5) block_unblock_email;;
  675. 6) block_unblock_subject;;
  676. 7) break;;
  677. esac
  678. done
  679. }
  680. function menu_admin {
  681. if [ ! -f /etc/sudoers ]; then
  682. clear
  683. exit 0
  684. fi
  685. sudo ${PROJECT_NAME}-controlpanel
  686. }
  687. function sign_keys {
  688. if [ ! -f /home/$USER/.monkeysphere/server_keys ]; then
  689. return
  690. fi
  691. dialog --title $"Monkeysphere sign server keys" \
  692. --backtitle $"Freedombone Security Configuration" \
  693. --defaultno \
  694. --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
  695. sel=$?
  696. case $sel in
  697. 0) ${PROJECT_NAME}-sec --sign yes;;
  698. esac
  699. }
  700. function menu_run_client_app {
  701. detect_installable_apps
  702. applist=""
  703. appnames=()
  704. n=1
  705. app_index=0
  706. for a in "${APPS_AVAILABLE[@]}"
  707. do
  708. if [[ ${APPS_INSTALLED[$app_index]} != "0" ]]; then
  709. if [[ $(function_exists run_client_${a}) == "1" ]]; then
  710. applist="$applist $n $a off"
  711. n=$[n+1]
  712. appnames+=("$a")
  713. fi
  714. fi
  715. app_index=$[app_index+1]
  716. done
  717. if [ $n -le 1 ]; then
  718. return
  719. fi
  720. backstr=$'Exit'
  721. applist="$applist $n $backstr on"
  722. appnames+=("Exit")
  723. choice=$(dialog --stdout --backtitle $"Freedombone" \
  724. --title $"Run an App" \
  725. --radiolist $'Choose:' \
  726. 16 40 20 $applist)
  727. if [ $? -eq 0 ]; then
  728. app_index=$[choice-1]
  729. chosen_app=${appnames[$app_index]}
  730. if [[ $chosen_app != "Exit" ]]; then
  731. run_client_${chosen_app}
  732. fi
  733. fi
  734. }
  735. function menu_top_level {
  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 $"User Control Panel" \
  742. --radiolist $"Choose an operation:" 19 50 12 \
  743. 1 $"Use Email" off \
  744. 2 $"Change Email Filtering Rules" off \
  745. 3 $"Run an App" off \
  746. 4 $"Browse the Web" off \
  747. 5 $"My Encryption Keys" off \
  748. 6 $"Set an outgoing email proxy" off \
  749. 7 $"Administrator controls" off \
  750. 8 $"Exit to the command line" off \
  751. 9 $"Log out" on 2> $data
  752. sel=$?
  753. case $sel in
  754. 1) exit 1;;
  755. 255) exit 1;;
  756. esac
  757. case $(cat $data) in
  758. 1) mutt;;
  759. 2) menu_email;;
  760. 3) menu_run_client_app;;
  761. 4) torify elinks;;
  762. 5) menu_encryption_keys;;
  763. 6) smtp_proxy;;
  764. 7) menu_admin;;
  765. 8) break;;
  766. 9) kill -HUP `pgrep -s 0 -o`;;
  767. esac
  768. done
  769. }
  770. sign_keys
  771. menu_top_level
  772. clear
  773. . ~/.bashrc
  774. exit 0