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