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