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