summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 598c3f7444593d06f67f82e10a517928e62a5db3 (plain)
  1. #!/bin/bash
  2. # monkeysphere-server: MonkeySphere server admin tool
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. #
  7. # They are Copyright 2008, and are all released under the GPL, version 3
  8. # or later.
  9. ########################################################################
  10. PGRM=$(basename $0)
  11. SHARE=${MONKEYSPHERE_SHARE:="/usr/share/monkeysphere"}
  12. export SHARE
  13. . "${SHARE}/common" || exit 1
  14. VARLIB="/var/lib/monkeysphere"
  15. export VARLIB
  16. # date in UTF format if needed
  17. DATE=$(date -u '+%FT%T')
  18. # unset some environment variables that could screw things up
  19. unset GREP_OPTIONS
  20. # default return code
  21. RETURN=0
  22. ########################################################################
  23. # FUNCTIONS
  24. ########################################################################
  25. usage() {
  26. cat <<EOF
  27. usage: $PGRM <subcommand> [options] [args]
  28. MonkeySphere server admin tool.
  29. subcommands:
  30. update-users (u) [USER]... update user authorized_keys files
  31. gen-key (g) [NAME[:PORT]] generate gpg key for the server
  32. -l|--length BITS key length in bits (2048)
  33. -e|--expire EXPIRE date to expire
  34. -r|--revoker FINGERPRINT add a revoker
  35. add-hostname (n+) NAME[:PORT] add hostname user ID to server key
  36. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  37. show-key (s) output all server host key information
  38. fingerprint (f) output just the key fingerprint
  39. publish-key (p) publish server host key to keyserver
  40. diagnostics (d) report on server monkeysphere status
  41. add-id-certifier (c+) KEYID import and tsign a certification key
  42. -n|--domain DOMAIN limit ID certifications to DOMAIN
  43. -t|--trust TRUST trust level of certifier (full)
  44. -d|--depth DEPTH trust depth for certifier (1)
  45. remove-id-certifier (c-) KEYID remove a certification key
  46. list-id-certifiers (c) list certification keys
  47. gpg-authentication-cmd CMD gnupg-authentication command
  48. -h|--help|help (h,?) this help
  49. EOF
  50. }
  51. su_monkeysphere_user() {
  52. su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
  53. }
  54. # function to interact with the host gnupg keyring
  55. gpg_host() {
  56. local returnCode
  57. GNUPGHOME="$GNUPGHOME_HOST"
  58. export GNUPGHOME
  59. # NOTE: we supress this warning because we need the monkeysphere
  60. # user to be able to read the host pubring. we realize this might
  61. # be problematic, but it's the simplest solution, without too much
  62. # loss of security.
  63. gpg --no-permission-warning "$@"
  64. returnCode="$?"
  65. # always reset the permissions on the host pubring so that the
  66. # monkeysphere user can read the trust signatures
  67. chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
  68. chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
  69. return "$returnCode"
  70. }
  71. # function to interact with the authentication gnupg keyring
  72. # FIXME: this function requires basically accepts only a single
  73. # argument because of problems with quote expansion. this needs to be
  74. # fixed/improved.
  75. gpg_authentication() {
  76. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  77. export GNUPGHOME
  78. su_monkeysphere_user "gpg $@"
  79. }
  80. # output key information
  81. show_server_key() {
  82. gpg_host --list-secret-keys --fingerprint
  83. }
  84. # output just key fingerprint
  85. fingerprint_server_key() {
  86. gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode | \
  87. grep '^fpr:' | head -1 | cut -d: -f10
  88. }
  89. # update authorized_keys for users
  90. update_users() {
  91. if [ "$1" ] ; then
  92. # get users from command line
  93. unames="$@"
  94. else
  95. # or just look at all users if none specified
  96. unames=$(getent passwd | cut -d: -f1)
  97. fi
  98. # set mode
  99. MODE="authorized_keys"
  100. # set gnupg home
  101. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  102. # check to see if the gpg trust database has been initialized
  103. if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
  104. failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
  105. fi
  106. # make sure the authorized_keys directory exists
  107. mkdir -p "${VARLIB}/authorized_keys"
  108. # loop over users
  109. for uname in $unames ; do
  110. # check all specified users exist
  111. if ! getent passwd "$uname" >/dev/null ; then
  112. log "----- unknown user '$uname' -----"
  113. continue
  114. fi
  115. # set authorized_user_ids and raw authorized_keys variables,
  116. # translating ssh-style path variables
  117. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  118. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  119. # if neither is found, skip user
  120. if [ ! -s "$authorizedUserIDs" ] ; then
  121. if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
  122. continue
  123. fi
  124. fi
  125. log "----- user: $uname -----"
  126. # exit if the authorized_user_ids file is empty
  127. if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
  128. log "Improper permissions on authorized_user_ids file path."
  129. continue
  130. fi
  131. # check permissions on the authorized_keys file path
  132. if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
  133. log "Improper permissions on authorized_keys file path path."
  134. continue
  135. fi
  136. # make temporary directory
  137. TMPDIR=$(mktemp -d)
  138. # trap to delete temporary directory on exit
  139. trap "rm -rf $TMPDIR" EXIT
  140. # create temporary authorized_user_ids file
  141. TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
  142. touch "$TMP_AUTHORIZED_USER_IDS"
  143. # create temporary authorized_keys file
  144. AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
  145. touch "$AUTHORIZED_KEYS"
  146. # set restrictive permissions on the temporary files
  147. # FIXME: is there a better way to do this?
  148. chmod 0700 "$TMPDIR"
  149. chmod 0600 "$AUTHORIZED_KEYS"
  150. chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
  151. chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
  152. # if the authorized_user_ids file exists...
  153. if [ -s "$authorizedUserIDs" ] ; then
  154. # copy user authorized_user_ids file to temporary
  155. # location
  156. cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
  157. # export needed variables
  158. export AUTHORIZED_KEYS
  159. export TMP_AUTHORIZED_USER_IDS
  160. # process authorized_user_ids file, as monkeysphere
  161. # user
  162. su_monkeysphere_user \
  163. ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
  164. RETURN="$?"
  165. fi
  166. # add user-controlled authorized_keys file path if specified
  167. if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
  168. log -n "adding raw authorized_keys file... "
  169. cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  170. loge "done."
  171. fi
  172. # openssh appears to check the contents of the
  173. # authorized_keys file as the user in question, so the
  174. # file must be readable by that user at least.
  175. # FIXME: is there a better way to do this?
  176. chown root "$AUTHORIZED_KEYS"
  177. chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
  178. chmod g+r "$AUTHORIZED_KEYS"
  179. # move the resulting authorized_keys file into place
  180. mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
  181. # destroy temporary directory
  182. rm -rf "$TMPDIR"
  183. done
  184. }
  185. # generate server gpg key
  186. gen_key() {
  187. local keyType
  188. local keyLength
  189. local keyUsage
  190. local keyExpire
  191. local revoker
  192. local hostName
  193. local userID
  194. local keyParameters
  195. local fingerprint
  196. # set default key parameter values
  197. keyType="RSA"
  198. keyLength="2048"
  199. keyUsage="auth"
  200. keyExpire=
  201. revoker=
  202. # get options
  203. TEMP=$(getopt -o e:l:r -l expire:,length:,revoker: -n "$PGRM" -- "$@")
  204. if [ $? != 0 ] ; then
  205. exit 1
  206. fi
  207. # Note the quotes around `$TEMP': they are essential!
  208. eval set -- "$TEMP"
  209. while true ; do
  210. case "$1" in
  211. -l|--length)
  212. keyLength="$2"
  213. shift 2
  214. ;;
  215. -e|--expire)
  216. keyExpire="$2"
  217. shift 2
  218. ;;
  219. -r|--revoker)
  220. revoker="$2"
  221. shift 2
  222. ;;
  223. --)
  224. shift
  225. ;;
  226. *)
  227. break
  228. ;;
  229. esac
  230. done
  231. hostName=${1:-$(hostname --fqdn)}
  232. userID="ssh://${hostName}"
  233. # check for presense of key with user ID
  234. if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
  235. failure "Key for '$userID' already exists"
  236. fi
  237. # prompt about key expiration if not specified
  238. if [ -z "$keyExpire" ] ; then
  239. cat <<EOF
  240. Please specify how long the key should be valid.
  241. 0 = key does not expire
  242. <n> = key expires in n days
  243. <n>w = key expires in n weeks
  244. <n>m = key expires in n months
  245. <n>y = key expires in n years
  246. EOF
  247. while [ -z "$keyExpire" ] ; do
  248. read -p "Key is valid for? (0) " keyExpire
  249. if ! test_gpg_expire ${keyExpire:=0} ; then
  250. echo "invalid value"
  251. unset keyExpire
  252. fi
  253. done
  254. elif ! test_gpg_expire "$keyExpire" ; then
  255. failure "invalid key expiration value '$keyExpire'."
  256. fi
  257. # set key parameters
  258. keyParameters=$(cat <<EOF
  259. Key-Type: $keyType
  260. Key-Length: $keyLength
  261. Key-Usage: $keyUsage
  262. Name-Real: $userID
  263. Expire-Date: $keyExpire
  264. EOF
  265. )
  266. # add the revoker field if specified
  267. # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
  268. # FIXME: key is marked "sensitive"? is this appropriate?
  269. if [ "$revoker" ] ; then
  270. keyParameters="${keyParameters}"$(cat <<EOF
  271. Revoker: 1:$revoker sensitive
  272. EOF
  273. )
  274. fi
  275. echo "The following key parameters will be used for the host private key:"
  276. echo "$keyParameters"
  277. read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
  278. if [ ${OK/y/Y} != 'Y' ] ; then
  279. failure "aborting."
  280. fi
  281. # add commit command
  282. keyParameters="${keyParameters}"$(cat <<EOF
  283. %commit
  284. %echo done
  285. EOF
  286. )
  287. log "generating server key..."
  288. echo "$keyParameters" | gpg_host --batch --gen-key
  289. # output the server fingerprint
  290. fingerprint_server_key "=${userID}"
  291. # find the key fingerprint of the newly generated key
  292. fingerprint=$(fingerprint_server_key)
  293. # export host ownertrust to authentication keyring
  294. log "setting ultimate owner trust for server key..."
  295. echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
  296. # translate the private key to ssh format, and export to a file
  297. # for sshs usage.
  298. # NOTE: assumes that the primary key is the proper key to use
  299. (umask 077 && \
  300. gpg_host --export-secret-key "$fingerprint" | \
  301. openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
  302. log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
  303. }
  304. # add hostname user ID to server key
  305. add_hostname() {
  306. if [ -z "$1" ] ; then
  307. failure "You must specify a hostname to add."
  308. fi
  309. userID="ssh://${1}"
  310. if [ "$(gpg_host --list-key "=${userID}")" ] ; then
  311. failure "Host userID '$userID' already exists."
  312. fi
  313. fingerprint=$(fingerprint_server_key)
  314. adduidCommand=$(cat <<EOF
  315. adduid
  316. $userID
  317. O
  318. save
  319. EOF
  320. )
  321. # add uid
  322. echo "$adduidCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  323. echo "NOTE: new host userID has not been published."
  324. echo "Use '$PGRM publish-key' to publish these changes."
  325. }
  326. # revoke hostname user ID to server key
  327. revoke_hostname() {
  328. if [ -z "$1" ] ; then
  329. failure "You must specify a hostname to revoke."
  330. fi
  331. failure "Sorry, not yet implemented."
  332. echo "NOTE: host userID revokation has not been published."
  333. echo "Use '$PGRM publish-key' to publish these changes."
  334. }
  335. # publish server key to keyserver
  336. publish_server_key() {
  337. read -p "Really publish key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
  338. if [ ${OK/y/Y} != 'Y' ] ; then
  339. failure "aborting."
  340. fi
  341. # publish host key
  342. # FIXME: need to figure out better way to identify host key
  343. # dummy command so as not to publish fakes keys during testing
  344. # eventually:
  345. #gpg_authentication "--keyserver $KEYSERVER --send-keys =ssh://$(hostname -f)"
  346. echo "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
  347. echo "The following command should publish the key:"
  348. echo "monkeysphere-server gpg-authentication-cmd '--keyserver $KEYSERVER --send-keys =ssh://$(hostname -f)'"
  349. exit 255
  350. }
  351. diagnostics() {
  352. # * check on the status and validity of the key and public certificates
  353. local seckey
  354. local keysfound
  355. local curdate
  356. local warnwindow
  357. local warndate
  358. local create
  359. local expire
  360. local uid
  361. local fingerprint
  362. local badhostkeys
  363. seckey=$(fingerprint_server_key)
  364. keysfound=$(echo "$seckey" | grep -c ^sec:)
  365. curdate=$(date +%s)
  366. # warn when anything is 2 months away from expiration
  367. warnwindow='2 months'
  368. warndate=$(date +%s -d "$warnwindow")
  369. echo "Checking host GPG key..."
  370. if (( "$keysfound" < 1 )); then
  371. echo "! No host key found."
  372. echo " - Recommendation: run 'monkeysphere-server gen-key'"
  373. elif (( "$keysfound" > 1 )); then
  374. echo "! More than one host key found?"
  375. # FIXME: recommend a way to resolve this
  376. else
  377. create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
  378. expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
  379. fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
  380. # check for key expiration:
  381. if [ "$expire" ]; then
  382. if (( "$expire" < "$curdate" )); then
  383. echo "! Host key is expired."
  384. # FIXME: recommend a way to resolve this other than re-keying?
  385. elif (( "$expire" < "$warndate" )); then
  386. echo "! Host key expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
  387. # FIXME: recommend a way to resolve this?
  388. fi
  389. fi
  390. # and weirdnesses:
  391. if [ "$create" ] && (( "$create" > "$curdate" )); then
  392. echo "! Host key was created in the future(?!). Is your clock correct?"
  393. echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
  394. fi
  395. # check for UserID expiration:
  396. echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
  397. while IFS=: read create expire uid ; do
  398. # FIXME: should we be doing any checking on the form
  399. # of the User ID? Should we be unmangling it somehow?
  400. if [ "$create" ] && (( "$create" > "$curdate" )); then
  401. echo "! User ID '$uid' was created in the future(?!). Is your clock correct?"
  402. echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
  403. fi
  404. if [ "$expire" ] ; then
  405. if (( "$expire" < "$curdate" )); then
  406. echo "! User ID '$uid' is expired."
  407. # FIXME: recommend a way to resolve this
  408. elif (( "$expire" < "$warndate" )); then
  409. echo "! User ID '$uid' expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
  410. # FIXME: recommend a way to resolve this
  411. fi
  412. fi
  413. done
  414. # FIXME: verify that the host key is properly published to the
  415. # keyservers (do this with the non-privileged user)
  416. # FIXME: check that there are valid, non-expired certifying signatures
  417. # attached to the host key after fetching from the public keyserver
  418. # (do this with the non-privileged user as well)
  419. # FIXME: propose adding a revoker to the host key if none exist (do we
  420. # have a way to do that after key generation?)
  421. # Ensure that the ssh_host_rsa_key file is present and non-empty:
  422. echo "Checking host SSH key..."
  423. if [ ! -s "${VARLIB}/ssh_host_rsa_key" ] ; then
  424. echo "! The host key as prepared for SSH (${VARLIB}/ssh_host_rsa_key) is missing or empty."
  425. else
  426. if [ $(stat -c '%a' "${VARLIB}/ssh_host_rsa_key") != 600 ] ; then
  427. echo "! Permissions seem wrong for ${VARLIB}/ssh_host_rsa_key -- should be 0600."
  428. fi
  429. # propose changes needed for sshd_config (if any)
  430. if ! grep -q "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$" /etc/ssh/sshd_config; then
  431. echo "! /etc/ssh/sshd_config does not point to the monkeysphere host key (${VARLIB}/ssh_host_rsa_key)."
  432. echo " - Recommendation: add a line to /etc/ssh/sshd_config: 'HostKey ${VARLIB}/ssh_host_rsa_key'"
  433. fi
  434. if badhostkeys=$(grep -i '^HostKey' | grep -q -v "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$") ; then
  435. echo "! /etc/sshd_config refers to some non-monkeysphere host keys:"
  436. echo "$badhostkeys"
  437. echo " - Recommendation: remove the above HostKey lines from /etc/ssh/sshd_config"
  438. fi
  439. fi
  440. fi
  441. # FIXME: look at the ownership/privileges of the various keyrings,
  442. # directories housing them, etc (what should those values be? can
  443. # we make them as minimal as possible?)
  444. # FIXME: look to see that the ownertrust rules are set properly on the
  445. # authentication keyring
  446. # FIXME: make sure that at least one identity certifier exists
  447. echo "Checking for MonkeySphere-enabled public-key authentication for users ..."
  448. # Ensure that User ID authentication is enabled:
  449. if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$" /etc/ssh/sshd_config; then
  450. echo "! /etc/ssh/sshd_config does not point to monkeysphere authorized keys."
  451. echo " - Recommendation: add a line to /etc/ssh/sshd_config: 'AuthorizedKeysFile ${VARLIB}/authorized_keys/%u'"
  452. fi
  453. if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' | grep -q -v "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$") ; then
  454. echo "! /etc/sshd_config refers to non-monkeysphere authorized_keys files:"
  455. echo "$badauthorizedkeys"
  456. echo " - Recommendation: remove the above AuthorizedKeysFile lines from /etc/ssh/sshd_config"
  457. fi
  458. }
  459. # retrieve key from web of trust, import it into the host keyring, and
  460. # ltsign the key in the host keyring so that it may certify other keys
  461. add_certifier() {
  462. local domain
  463. local trust
  464. local depth
  465. local keyID
  466. local fingerprint
  467. local ltsignCommand
  468. local trustval
  469. # set default values for trust depth and domain
  470. domain=
  471. trust=full
  472. depth=1
  473. # get options
  474. TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
  475. if [ $? != 0 ] ; then
  476. exit 1
  477. fi
  478. # Note the quotes around `$TEMP': they are essential!
  479. eval set -- "$TEMP"
  480. while true ; do
  481. case "$1" in
  482. -n|--domain)
  483. domain="$2"
  484. shift 2
  485. ;;
  486. -t|--trust)
  487. trust="$2"
  488. shift 2
  489. ;;
  490. -d|--depth)
  491. depth="$2"
  492. shift 2
  493. ;;
  494. --)
  495. shift
  496. ;;
  497. *)
  498. break
  499. ;;
  500. esac
  501. done
  502. keyID="$1"
  503. if [ -z "$keyID" ] ; then
  504. failure "You must specify the key ID of a key to add."
  505. fi
  506. export keyID
  507. # get the key from the key server
  508. gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
  509. # get the full fingerprint of a key ID
  510. fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
  511. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  512. echo "key found:"
  513. gpg_authentication "--fingerprint $fingerprint"
  514. echo "Are you sure you want to add this key as a certifier of"
  515. read -p "users on this system? (y/N) " OK; OK=${OK:-N}
  516. if [ "${OK/y/Y}" != 'Y' ] ; then
  517. failure "aborting."
  518. fi
  519. # export the key to the host keyring
  520. gpg_authentication "--export $keyID" | gpg_host --import
  521. if [ "$trust" == marginal ]; then
  522. trustval=1
  523. elif [ "$trust" == full ]; then
  524. trustval=2
  525. else
  526. failure "trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)"
  527. fi
  528. # ltsign command
  529. # NOTE: *all* user IDs will be ltsigned
  530. ltsignCommand=$(cat <<EOF
  531. ltsign
  532. y
  533. $trustval
  534. $depth
  535. $domain
  536. y
  537. save
  538. EOF
  539. )
  540. # ltsign the key
  541. echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  542. # update the trustdb for the authentication keyring
  543. gpg_authentication "--check-trustdb"
  544. }
  545. # delete a certifiers key from the host keyring
  546. remove_certifier() {
  547. local keyID
  548. local fingerprint
  549. keyID="$1"
  550. if [ -z "$keyID" ] ; then
  551. failure "You must specify the key ID of a key to remove."
  552. fi
  553. # delete the requested key (with prompting)
  554. gpg_host --delete-key "$keyID"
  555. # update the trustdb for the authentication keyring
  556. gpg_authentication "--check-trustdb"
  557. }
  558. # list the host certifiers
  559. list_certifiers() {
  560. gpg_host --list-keys
  561. }
  562. # issue command to gpg-authentication keyring
  563. gpg_authentication_cmd() {
  564. gpg_authentication "$@"
  565. }
  566. ########################################################################
  567. # MAIN
  568. ########################################################################
  569. # unset variables that should be defined only in config file
  570. unset KEYSERVER
  571. unset AUTHORIZED_USER_IDS
  572. unset RAW_AUTHORIZED_KEYS
  573. unset MONKEYSPHERE_USER
  574. # load configuration file
  575. [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
  576. # set empty config variable with ones from the environment, or with
  577. # defaults
  578. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
  579. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
  580. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  581. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  582. # other variables
  583. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  584. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  585. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
  586. GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
  587. # export variables needed in su invocation
  588. export DATE
  589. export MODE
  590. export MONKEYSPHERE_USER
  591. export KEYSERVER
  592. export CHECK_KEYSERVER
  593. export REQUIRED_USER_KEY_CAPABILITY
  594. export GNUPGHOME_HOST
  595. export GNUPGHOME_AUTHENTICATION
  596. export GNUPGHOME
  597. # get subcommand
  598. COMMAND="$1"
  599. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  600. shift
  601. case $COMMAND in
  602. 'update-users'|'update-user'|'u')
  603. update_users "$@"
  604. ;;
  605. 'gen-key'|'g')
  606. gen_key "$@"
  607. ;;
  608. 'add-hostname'|'add-name'|'n+')
  609. add_hostname "$@"
  610. ;;
  611. 'revoke-hostname'|'revoke-name'|'n-')
  612. revoke_hostname "$@"
  613. ;;
  614. 'show-key'|'show'|'s')
  615. show_server_key
  616. ;;
  617. 'show-fingerprint'|'fingerprint'|'f')
  618. fingerprint_server_key
  619. ;;
  620. 'publish-key'|'publish'|'p')
  621. publish_server_key
  622. ;;
  623. 'diagnostics'|'d')
  624. diagnostics
  625. ;;
  626. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  627. add_certifier "$@"
  628. ;;
  629. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  630. remove_certifier "$@"
  631. ;;
  632. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  633. list_certifiers "$@"
  634. ;;
  635. 'gpg-authentication-cmd')
  636. gpg_authentication_cmd "$@"
  637. ;;
  638. '--help'|'help'|'-h'|'h'|'?')
  639. usage
  640. ;;
  641. *)
  642. failure "Unknown command: '$COMMAND'
  643. Type '$PGRM help' for usage."
  644. ;;
  645. esac
  646. exit "$RETURN"