summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 9196c2fa0fd84f52cee50175c318873db7f0fdd7 (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=${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. GREP_OPTIONS=
  20. # default return code
  21. ERR=0
  22. ########################################################################
  23. # FUNCTIONS
  24. ########################################################################
  25. usage() {
  26. cat <<EOF
  27. usage: $PGRM <subcommand> [args]
  28. MonkeySphere server admin tool.
  29. subcommands:
  30. update-users (u) [USER]... update users authorized_keys files
  31. gen-key (g) [HOSTNAME] generate gpg key for the server
  32. show-fingerprint (f) show server's host key fingerprint
  33. publish-key (p) publish server's host key to keyserver
  34. trust-key (t) KEYID import and tsign a certification key
  35. help (h,?) this help
  36. EOF
  37. }
  38. su_monkeysphere_user() {
  39. su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
  40. }
  41. # function to interact with the host gnupg keyring
  42. gpg_host() {
  43. local returnCode
  44. GNUPGHOME="$GNUPGHOME_HOST"
  45. export GNUPGHOME
  46. # NOTE: we supress this warning because we need the monkeysphere
  47. # user to be able to read the host pubring. we realize this might
  48. # be problematic, but it's the simplest solution, without too much
  49. # loss of security.
  50. gpg --no-permission-warning "$@"
  51. returnCode="$?"
  52. # always reset the permissions on the host pubring so that the
  53. # monkeysphere user can read the trust signatures
  54. chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
  55. chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
  56. return "$returnCode"
  57. }
  58. # function to interact with the authentication gnupg keyring
  59. gpg_authentication() {
  60. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  61. export GNUPGHOME
  62. su_monkeysphere_user "gpg $@"
  63. }
  64. # update authorized_keys for users
  65. update_users() {
  66. if [ "$1" ] ; then
  67. # get users from command line
  68. unames="$@"
  69. else
  70. # or just look at all users if none specified
  71. unames=$(getent passwd | cut -d: -f1)
  72. fi
  73. # set mode
  74. MODE="authorized_keys"
  75. # set gnupg home
  76. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  77. # check to see if the gpg trust database has been initialized
  78. if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
  79. failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
  80. fi
  81. # make sure the authorized_keys directory exists
  82. mkdir -p "${VARLIB}/authorized_keys"
  83. # loop over users
  84. for uname in $unames ; do
  85. # check all specified users exist
  86. if ! getent passwd "$uname" >/dev/null ; then
  87. error "----- unknown user '$uname' -----"
  88. continue
  89. fi
  90. # set authorized_user_ids and raw authorized_keys variables,
  91. # translating ssh-style path variables
  92. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  93. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  94. # if neither is found, skip user
  95. if [ ! -s "$authorizedUserIDs" ] ; then
  96. if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
  97. continue
  98. fi
  99. fi
  100. log "----- user: $uname -----"
  101. # make temporary directory
  102. TMPDIR=$(mktemp -d)
  103. # trap to delete temporary directory on exit
  104. trap "rm -rf $TMPDIR" EXIT
  105. # create temporary authorized_user_ids file
  106. TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
  107. touch "$TMP_AUTHORIZED_USER_IDS"
  108. # create temporary authorized_keys file
  109. AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
  110. touch "$AUTHORIZED_KEYS"
  111. # set restrictive permissions on the temporary files
  112. # FIXME: is there a better way to do this?
  113. chmod 0700 "$TMPDIR"
  114. chmod 0600 "$AUTHORIZED_KEYS"
  115. chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
  116. chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
  117. # if the authorized_user_ids file exists...
  118. if [ -s "$authorizedUserIDs" ] ; then
  119. # copy user authorized_user_ids file to temporary
  120. # location
  121. cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
  122. # export needed variables
  123. export AUTHORIZED_KEYS
  124. export TMP_AUTHORIZED_USER_IDS
  125. # process authorized_user_ids file, as monkeysphere
  126. # user
  127. su_monkeysphere_user \
  128. ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
  129. ERR="$?"
  130. fi
  131. # add user-controlled authorized_keys file path if specified
  132. if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
  133. log -n "adding raw authorized_keys file... "
  134. cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  135. loge "done."
  136. fi
  137. # openssh appears to check the contents of the
  138. # authorized_keys file as the user in question, so the
  139. # file must be readable by that user at least.
  140. # FIXME: is there a better way to do this?
  141. chown root "$AUTHORIZED_KEYS"
  142. chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
  143. chmod g+r "$AUTHORIZED_KEYS"
  144. # if the resulting authorized_keys file is not empty, move
  145. # it into place
  146. mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
  147. # destroy temporary directory
  148. rm -rf "$TMPDIR"
  149. done
  150. }
  151. # generate server gpg key
  152. gen_key() {
  153. local hostName
  154. local userID
  155. local keyParameters
  156. local fingerprint
  157. hostName=${1:-$(hostname --fqdn)}
  158. SERVICE=${SERVICE:-"ssh"}
  159. userID="${SERVICE}://${hostName}"
  160. if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
  161. failure "Key for '$userID' already exists"
  162. fi
  163. # set key defaults
  164. KEY_TYPE=${KEY_TYPE:-"RSA"}
  165. KEY_LENGTH=${KEY_LENGTH:-"2048"}
  166. KEY_USAGE=${KEY_USAGE:-"auth"}
  167. KEY_EXPIRE=${KEY_EXPIRE:-"0"}
  168. cat <<EOF
  169. Please specify how long the key should be valid.
  170. 0 = key does not expire
  171. <n> = key expires in n days
  172. <n>w = key expires in n weeks
  173. <n>m = key expires in n months
  174. <n>y = key expires in n years
  175. EOF
  176. read -p "Key is valid for? ($KEY_EXPIRE) " KEY_EXPIRE; KEY_EXPIRE=${KEY_EXPIRE:-"0"}
  177. # set key parameters
  178. keyParameters=$(cat <<EOF
  179. Key-Type: $KEY_TYPE
  180. Key-Length: $KEY_LENGTH
  181. Key-Usage: $KEY_USAGE
  182. Name-Real: $userID
  183. Expire-Date: $KEY_EXPIRE
  184. EOF
  185. )
  186. # add the revoker field if specified
  187. # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key. why?
  188. # FIXME: why is this marked "sensitive"? how will this signature ever
  189. # be transmitted to the expected revoker?
  190. if [ "$REVOKER" ] ; then
  191. keyParameters="${keyParameters}"$(cat <<EOF
  192. Revoker: 1:$REVOKER sensitive
  193. EOF
  194. )
  195. fi
  196. echo "The following key parameters will be used for the host private key:"
  197. echo "$keyParameters"
  198. read -p "Generate key? [Y|n]: " OK; OK=${OK:=Y}
  199. if [ ${OK/y/Y} != 'Y' ] ; then
  200. failure "aborting."
  201. fi
  202. # add commit command
  203. keyParameters="${keyParameters}"$(cat <<EOF
  204. %commit
  205. %echo done
  206. EOF
  207. )
  208. log "generating server key..."
  209. echo "$keyParameters" | gpg_host --batch --gen-key
  210. # output the server fingerprint
  211. fingerprint_server_key "=${userID}"
  212. # find the key fingerprint of the server primary key
  213. fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
  214. grep '^fpr:' | head -1 | cut -d: -f10)
  215. # translate the private key to ssh format, and export to a file
  216. # for sshs usage.
  217. # NOTE: assumes that the primary key is the proper key to use
  218. (umask 077 && \
  219. gpg_host --export-secret-key "$fingerprint" | \
  220. openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
  221. log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
  222. }
  223. # gpg output key fingerprint
  224. fingerprint_server_key() {
  225. gpg_host --fingerprint --list-secret-keys
  226. }
  227. # publish server key to keyserver
  228. publish_server_key() {
  229. read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
  230. if [ ${OK/y/Y} != 'Y' ] ; then
  231. failure "aborting."
  232. fi
  233. # publish host key
  234. # FIXME: need to figure out better way to identify host key
  235. # dummy command so as not to publish fakes keys during testing
  236. # eventually:
  237. #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
  238. failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
  239. }
  240. # retrieve key from web of trust, and set owner trust to "full"
  241. # if key is found.
  242. trust_key() {
  243. local keyID
  244. local trustLevel
  245. keyID="$1"
  246. # default values for trust depth and domain
  247. DEPTH=${DEPTH:-1}
  248. DOMAIN=${DOMAIN:-}
  249. if [ -z "$keyID" ] ; then
  250. failure "You must specify key to trust."
  251. fi
  252. export keyID
  253. # export host ownertrust to authentication keyring
  254. gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
  255. # get the key from the key server
  256. gpg_authentication "--keyserver $KEYSERVER --recv-key $keyID"
  257. # get the full fingerprint of a key ID
  258. fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
  259. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  260. if [ -z "$fingerprint" ] ; then
  261. failure "Could not find key '$keyID'."
  262. fi
  263. echo "key found:"
  264. gpg_authentication "--fingerprint $fingerprint"
  265. # export the key to the host keyring
  266. gpg_authentication "--export $keyID" | gpg_host --import
  267. # ltsign command
  268. # NOTE: *all* user IDs will be ltsigned
  269. ltsignCommand=$(cat <<EOF
  270. ltsign
  271. y
  272. 2
  273. $DEPTH
  274. $DOMAIN
  275. y
  276. save
  277. EOF
  278. )
  279. # ltsign the key
  280. echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  281. # update the trustdb for the authentication keyring
  282. gpg_authentication "--check-trustdb"
  283. }
  284. ########################################################################
  285. # MAIN
  286. ########################################################################
  287. COMMAND="$1"
  288. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  289. shift
  290. # load configuration file
  291. MS_CONF=${MS_CONF:-"${ETC}/monkeysphere-server.conf"}
  292. [ -e "$MS_CONF" ] && . "$MS_CONF"
  293. # set empty config variable with defaults
  294. MONKEYSPHERE_USER=${MONKEYSPHERE_USER:-"monkeysphere"}
  295. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  296. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  297. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
  298. RAW_AUTHORIZED_KEYS=${RAW_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  299. # other variables
  300. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  301. GNUPGHOME_HOST=${GNUPGHOME_HOST:-"${VARLIB}/gnupg-host"}
  302. GNUPGHOME_AUTHENTICATION=${GNUPGHOME_AUTHENTICATION:-"${VARLIB}/gnupg-authentication"}
  303. # export variables
  304. export DATE
  305. export MODE
  306. export MONKEYSPHERE_USER
  307. export KEYSERVER
  308. export CHECK_KEYSERVER
  309. export REQUIRED_USER_KEY_CAPABILITY
  310. export GNUPGHOME_HOST
  311. export GNUPGHOME_AUTHENTICATION
  312. export GNUPGHOME
  313. case $COMMAND in
  314. 'update-users'|'update-user'|'u')
  315. update_users "$@"
  316. ;;
  317. 'gen-key'|'g')
  318. gen_key "$@"
  319. ;;
  320. 'show-fingerprint'|'f')
  321. fingerprint_server_key
  322. ;;
  323. 'publish-key'|'p')
  324. publish_server_key
  325. ;;
  326. 'trust-key'|'t')
  327. trust_key "$@"
  328. ;;
  329. 'help'|'h'|'?')
  330. usage
  331. ;;
  332. *)
  333. failure "Unknown command: '$COMMAND'
  334. Type '$PGRM help' for usage."
  335. ;;
  336. esac
  337. exit "$ERR"