summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 1e05799a00ca9ef64cdabec33ce3a51cc3fa3cdc (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. log "authorized_keys file updated."
  148. # destroy temporary directory
  149. rm -rf "$TMPDIR"
  150. done
  151. }
  152. # generate server gpg key
  153. gen_key() {
  154. local hostName
  155. local userID
  156. local keyParameters
  157. local fingerprint
  158. hostName=${1:-$(hostname --fqdn)}
  159. SERVICE=${SERVICE:-"ssh"}
  160. userID="${SERVICE}://${hostName}"
  161. if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
  162. failure "Key for '$userID' already exists"
  163. fi
  164. # set key defaults
  165. KEY_TYPE=${KEY_TYPE:-"RSA"}
  166. KEY_LENGTH=${KEY_LENGTH:-"2048"}
  167. KEY_USAGE=${KEY_USAGE:-"auth"}
  168. KEY_EXPIRE=${KEY_EXPIRE:-"0"}
  169. cat <<EOF
  170. Please specify how long the key should be valid.
  171. 0 = key does not expire
  172. <n> = key expires in n days
  173. <n>w = key expires in n weeks
  174. <n>m = key expires in n months
  175. <n>y = key expires in n years
  176. EOF
  177. read -p "Key is valid for? ($KEY_EXPIRE) " KEY_EXPIRE; KEY_EXPIRE=${KEY_EXPIRE:-"0"}
  178. # set key parameters
  179. keyParameters=$(cat <<EOF
  180. Key-Type: $KEY_TYPE
  181. Key-Length: $KEY_LENGTH
  182. Key-Usage: $KEY_USAGE
  183. Name-Real: $userID
  184. Expire-Date: $KEY_EXPIRE
  185. EOF
  186. )
  187. # add the revoker field if specified
  188. # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key. why?
  189. # FIXME: why is this marked "sensitive"? how will this signature ever
  190. # be transmitted to the expected revoker?
  191. if [ "$REVOKER" ] ; then
  192. keyParameters="${keyParameters}"$(cat <<EOF
  193. Revoker: 1:$REVOKER sensitive
  194. EOF
  195. )
  196. fi
  197. echo "The following key parameters will be used for the host private key:"
  198. echo "$keyParameters"
  199. read -p "Generate key? [Y|n]: " OK; OK=${OK:=Y}
  200. if [ ${OK/y/Y} != 'Y' ] ; then
  201. failure "aborting."
  202. fi
  203. # add commit command
  204. keyParameters="${keyParameters}"$(cat <<EOF
  205. %commit
  206. %echo done
  207. EOF
  208. )
  209. log "generating server key..."
  210. echo "$keyParameters" | gpg_host --batch --gen-key
  211. # output the server fingerprint
  212. fingerprint_server_key "=${userID}"
  213. # find the key fingerprint of the server primary key
  214. fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
  215. grep '^fpr:' | head -1 | cut -d: -f10)
  216. # translate the private key to ssh format, and export to a file
  217. # for sshs usage.
  218. # NOTE: assumes that the primary key is the proper key to use
  219. (umask 077 && \
  220. gpg_host --export-secret-key "$fingerprint" | \
  221. openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
  222. log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
  223. }
  224. # gpg output key fingerprint
  225. fingerprint_server_key() {
  226. gpg_host --fingerprint --list-secret-keys
  227. }
  228. # publish server key to keyserver
  229. publish_server_key() {
  230. read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
  231. if [ ${OK/y/Y} != 'Y' ] ; then
  232. failure "aborting."
  233. fi
  234. # publish host key
  235. # FIXME: need to figure out better way to identify host key
  236. # dummy command so as not to publish fakes keys during testing
  237. # eventually:
  238. #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
  239. failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
  240. }
  241. # retrieve key from web of trust, and set owner trust to "full"
  242. # if key is found.
  243. trust_key() {
  244. local keyID
  245. local trustLevel
  246. keyID="$1"
  247. # default values for trust depth and domain
  248. DEPTH=${DEPTH:-1}
  249. DOMAIN=${DOMAIN:-}
  250. if [ -z "$keyID" ] ; then
  251. failure "You must specify key to trust."
  252. fi
  253. export keyID
  254. # export host ownertrust to authentication keyring
  255. gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
  256. # get the key from the key server
  257. gpg_authentication "--keyserver $KEYSERVER --recv-key $keyID"
  258. # get the full fingerprint of a key ID
  259. fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
  260. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  261. if [ -z "$fingerprint" ] ; then
  262. failure "Could not find key '$keyID'."
  263. fi
  264. echo "key found:"
  265. gpg_authentication "--fingerprint $fingerprint"
  266. # export the key to the host keyring
  267. gpg_authentication "--export $keyID" | gpg_host --import
  268. # ltsign command
  269. # NOTE: *all* user IDs will be ltsigned
  270. ltsignCommand=$(cat <<EOF
  271. ltsign
  272. y
  273. 2
  274. $DEPTH
  275. $DOMAIN
  276. y
  277. save
  278. EOF
  279. )
  280. # ltsign the key
  281. echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  282. # update the trustdb for the authentication keyring
  283. gpg_authentication "--check-trustdb"
  284. }
  285. ########################################################################
  286. # MAIN
  287. ########################################################################
  288. COMMAND="$1"
  289. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  290. shift
  291. # load configuration file
  292. MS_CONF=${MS_CONF:-"${ETC}/monkeysphere-server.conf"}
  293. [ -e "$MS_CONF" ] && . "$MS_CONF"
  294. # set empty config variable with defaults
  295. MONKEYSPHERE_USER=${MONKEYSPHERE_USER:-"monkeysphere"}
  296. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  297. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  298. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
  299. RAW_AUTHORIZED_KEYS=${RAW_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  300. # other variables
  301. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  302. GNUPGHOME_HOST=${GNUPGHOME_HOST:-"${VARLIB}/gnupg-host"}
  303. GNUPGHOME_AUTHENTICATION=${GNUPGHOME_AUTHENTICATION:-"${VARLIB}/gnupg-authentication"}
  304. # export variables
  305. export DATE
  306. export MODE
  307. export MONKEYSPHERE_USER
  308. export KEYSERVER
  309. export CHECK_KEYSERVER
  310. export REQUIRED_USER_KEY_CAPABILITY
  311. export GNUPGHOME_HOST
  312. export GNUPGHOME_AUTHENTICATION
  313. export GNUPGHOME
  314. case $COMMAND in
  315. 'update-users'|'update-user'|'u')
  316. update_users "$@"
  317. ;;
  318. 'gen-key'|'g')
  319. gen_key "$@"
  320. ;;
  321. 'show-fingerprint'|'f')
  322. fingerprint_server_key
  323. ;;
  324. 'publish-key'|'p')
  325. publish_server_key
  326. ;;
  327. 'trust-key'|'t')
  328. trust_key "$@"
  329. ;;
  330. 'help'|'h'|'?')
  331. usage
  332. ;;
  333. *)
  334. failure "Unknown command: '$COMMAND'
  335. Type '$PGRM help' for usage."
  336. ;;
  337. esac
  338. exit "$ERR"