summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: a5497c29e06a36b94e827a9407dc8a4095d35184 (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 users authorized_keys files
  31. gen-key (g) [HOSTNAME] 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. show-fingerprint (f) show server's host key fingerprint
  36. publish-key (p) publish server's host key to keyserver
  37. add-identity-certifier (a) KEYID import and tsign a certification key
  38. -n|--domain DOMAIN domain of certifier ()
  39. -t|--trust TRUST trust level of certifier ('full')
  40. -d|--depth DEPTH trust depth for certifier (1)
  41. remove-identity-certifier (r) KEYID remove a certification key
  42. list-identity-certifiers (l) list certification keys
  43. help (h,?) this help
  44. EOF
  45. }
  46. su_monkeysphere_user() {
  47. su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
  48. }
  49. # function to interact with the host gnupg keyring
  50. gpg_host() {
  51. local returnCode
  52. GNUPGHOME="$GNUPGHOME_HOST"
  53. export GNUPGHOME
  54. # NOTE: we supress this warning because we need the monkeysphere
  55. # user to be able to read the host pubring. we realize this might
  56. # be problematic, but it's the simplest solution, without too much
  57. # loss of security.
  58. gpg --no-permission-warning "$@"
  59. returnCode="$?"
  60. # always reset the permissions on the host pubring so that the
  61. # monkeysphere user can read the trust signatures
  62. chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
  63. chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
  64. return "$returnCode"
  65. }
  66. # function to interact with the authentication gnupg keyring
  67. # FIXME: this function requires basically accepts only a single
  68. # argument because of problems with quote expansion. this needs to be
  69. # fixed/improved.
  70. gpg_authentication() {
  71. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  72. export GNUPGHOME
  73. su_monkeysphere_user "gpg $@"
  74. }
  75. # update authorized_keys for users
  76. update_users() {
  77. if [ "$1" ] ; then
  78. # get users from command line
  79. unames="$@"
  80. else
  81. # or just look at all users if none specified
  82. unames=$(getent passwd | cut -d: -f1)
  83. fi
  84. # set mode
  85. MODE="authorized_keys"
  86. # set gnupg home
  87. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  88. # check to see if the gpg trust database has been initialized
  89. if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
  90. failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
  91. fi
  92. # make sure the authorized_keys directory exists
  93. mkdir -p "${VARLIB}/authorized_keys"
  94. # loop over users
  95. for uname in $unames ; do
  96. # check all specified users exist
  97. if ! getent passwd "$uname" >/dev/null ; then
  98. log "----- unknown user '$uname' -----"
  99. continue
  100. fi
  101. # set authorized_user_ids and raw authorized_keys variables,
  102. # translating ssh-style path variables
  103. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  104. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  105. # if neither is found, skip user
  106. if [ ! -s "$authorizedUserIDs" ] ; then
  107. if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
  108. continue
  109. fi
  110. fi
  111. log "----- user: $uname -----"
  112. if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
  113. log "Improper permissions on authorized_user_ids file."
  114. continue
  115. fi
  116. if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
  117. log "Improper permissions on authorized_keys file."
  118. continue
  119. fi
  120. # make temporary directory
  121. TMPDIR=$(mktemp -d)
  122. # trap to delete temporary directory on exit
  123. trap "rm -rf $TMPDIR" EXIT
  124. # create temporary authorized_user_ids file
  125. TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
  126. touch "$TMP_AUTHORIZED_USER_IDS"
  127. # create temporary authorized_keys file
  128. AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
  129. touch "$AUTHORIZED_KEYS"
  130. # set restrictive permissions on the temporary files
  131. # FIXME: is there a better way to do this?
  132. chmod 0700 "$TMPDIR"
  133. chmod 0600 "$AUTHORIZED_KEYS"
  134. chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
  135. chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
  136. # if the authorized_user_ids file exists...
  137. if [ -s "$authorizedUserIDs" ] ; then
  138. # copy user authorized_user_ids file to temporary
  139. # location
  140. cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
  141. # export needed variables
  142. export AUTHORIZED_KEYS
  143. export TMP_AUTHORIZED_USER_IDS
  144. # process authorized_user_ids file, as monkeysphere
  145. # user
  146. su_monkeysphere_user \
  147. ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
  148. RETURN="$?"
  149. fi
  150. # add user-controlled authorized_keys file path if specified
  151. if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
  152. log -n "adding raw authorized_keys file... "
  153. cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  154. loge "done."
  155. fi
  156. # openssh appears to check the contents of the
  157. # authorized_keys file as the user in question, so the
  158. # file must be readable by that user at least.
  159. # FIXME: is there a better way to do this?
  160. chown root "$AUTHORIZED_KEYS"
  161. chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
  162. chmod g+r "$AUTHORIZED_KEYS"
  163. # if the resulting authorized_keys file is not empty, move
  164. # it into place
  165. mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
  166. # destroy temporary directory
  167. rm -rf "$TMPDIR"
  168. done
  169. }
  170. # generate server gpg key
  171. gen_key() {
  172. local hostName
  173. local userID
  174. local keyParameters
  175. local fingerprint
  176. hostName=${1:-$(hostname --fqdn)}
  177. userID="ssh://${hostName}"
  178. # check for presense of key with user ID
  179. if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
  180. failure "Key for '$userID' already exists"
  181. fi
  182. # set key variables
  183. KEY_TYPE="RSA"
  184. KEY_LENGTH=${KEY_LENGTH:="2048"}
  185. KEY_USAGE="auth"
  186. # prompt about key expiration if not specified
  187. if [ -z "$KEY_EXPIRE" ] ; then
  188. cat <<EOF
  189. Please specify how long the key should be valid.
  190. 0 = key does not expire
  191. <n> = key expires in n days
  192. <n>w = key expires in n weeks
  193. <n>m = key expires in n months
  194. <n>y = key expires in n years
  195. EOF
  196. while [ -z "$KEY_EXPIRE" ] ; do
  197. read -p "Key is valid for? (0) " KEY_EXPIRE
  198. if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
  199. echo "invalid value"
  200. unset KEY_EXPIRE
  201. fi
  202. done
  203. elif ! test_gpg_expire "$KEY_EXPIRE" ; then
  204. failure "invalid key expiration value '$KEY_EXPIRE'."
  205. fi
  206. # set key parameters
  207. keyParameters=$(cat <<EOF
  208. Key-Type: $KEY_TYPE
  209. Key-Length: $KEY_LENGTH
  210. Key-Usage: $KEY_USAGE
  211. Name-Real: $userID
  212. Expire-Date: $KEY_EXPIRE
  213. EOF
  214. )
  215. # add the revoker field if specified
  216. # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
  217. # FIXME: key is marked "sensitive"? is this appropriate?
  218. if [ "$REVOKER" ] ; then
  219. keyParameters="${keyParameters}"$(cat <<EOF
  220. Revoker: 1:$REVOKER sensitive
  221. EOF
  222. )
  223. fi
  224. echo "The following key parameters will be used for the host private key:"
  225. echo "$keyParameters"
  226. read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
  227. if [ ${OK/y/Y} != 'Y' ] ; then
  228. failure "aborting."
  229. fi
  230. # add commit command
  231. keyParameters="${keyParameters}"$(cat <<EOF
  232. %commit
  233. %echo done
  234. EOF
  235. )
  236. log "generating server key..."
  237. echo "$keyParameters" | gpg_host --batch --gen-key
  238. # output the server fingerprint
  239. fingerprint_server_key "=${userID}"
  240. # find the key fingerprint of the server primary key
  241. fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
  242. grep '^fpr:' | head -1 | cut -d: -f10)
  243. # translate the private key to ssh format, and export to a file
  244. # for sshs usage.
  245. # NOTE: assumes that the primary key is the proper key to use
  246. (umask 077 && \
  247. gpg_host --export-secret-key "$fingerprint" | \
  248. openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
  249. log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
  250. }
  251. # gpg output key fingerprint
  252. fingerprint_server_key() {
  253. gpg_host --fingerprint --list-secret-keys
  254. }
  255. # publish server key to keyserver
  256. publish_server_key() {
  257. read -p "really publish key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
  258. if [ ${OK/y/Y} != 'Y' ] ; then
  259. failure "aborting."
  260. fi
  261. # publish host key
  262. # FIXME: need to figure out better way to identify host key
  263. # dummy command so as not to publish fakes keys during testing
  264. # eventually:
  265. #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
  266. failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
  267. }
  268. # retrieve key from web of trust, import it into the host keyring, and
  269. # ltsign the key in the host keyring so that it may certify other keys
  270. add_certifier() {
  271. local keyID
  272. local fingerprint
  273. local ltsignCommand
  274. keyID="$1"
  275. export keyID
  276. # export host ownertrust to authentication keyring
  277. gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
  278. # get the key from the key server
  279. gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'" || failure
  280. # get the full fingerprint of a key ID
  281. fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
  282. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  283. echo "key found:"
  284. gpg_authentication "--fingerprint $fingerprint"
  285. read -p "Are you sure you want to add this key as a certifier of users on this system? (y/N) " OK; OK=${OK:-N}
  286. if [ "${OK/y/Y}" != 'Y' ] ; then
  287. failure "aborting."
  288. fi
  289. # export the key to the host keyring
  290. gpg_authentication "--export $keyID" | gpg_host --import
  291. # default values for trust depth and domain
  292. DOMAIN=${DOMAIN:-}
  293. TRUST=${TRUST:-2}
  294. DEPTH=${DEPTH:-1}
  295. # ltsign command
  296. # NOTE: *all* user IDs will be ltsigned
  297. ltsignCommand=$(cat <<EOF
  298. ltsign
  299. y
  300. $TRUST
  301. $DEPTH
  302. $DOMAIN
  303. y
  304. save
  305. EOF
  306. )
  307. # ltsign the key
  308. echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  309. # update the trustdb for the authentication keyring
  310. gpg_authentication "--check-trustdb"
  311. }
  312. # delete a certifiers key from the host keyring
  313. remove_certifier() {
  314. local keyID
  315. local fingerprint
  316. keyID="$1"
  317. # delete the requested key (with prompting)
  318. gpg_host --delete-key "$keyID"
  319. # update the trustdb for the authentication keyring
  320. gpg_authentication "--check-trustdb"
  321. }
  322. # list the host certifiers
  323. list_certifiers() {
  324. gpg_host --list-keys
  325. }
  326. ########################################################################
  327. # MAIN
  328. ########################################################################
  329. # unset variables that should be defined only in config file
  330. unset KEYSERVER
  331. unset AUTHORIZED_USER_IDS
  332. unset RAW_AUTHORIZED_KEYS
  333. unset MONKEYSPHERE_USER
  334. # load configuration file
  335. [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
  336. # set empty config variable with ones from the environment, or with
  337. # defaults
  338. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
  339. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
  340. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  341. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  342. # other variables
  343. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  344. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  345. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
  346. GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
  347. # export variables needed in su invocation
  348. export DATE
  349. export MODE
  350. export MONKEYSPHERE_USER
  351. export KEYSERVER
  352. export CHECK_KEYSERVER
  353. export REQUIRED_USER_KEY_CAPABILITY
  354. export GNUPGHOME_HOST
  355. export GNUPGHOME_AUTHENTICATION
  356. export GNUPGHOME
  357. # get subcommand
  358. COMMAND="$1"
  359. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  360. shift
  361. # unset option variables
  362. unset KEY_LENGTH
  363. unset KEY_EXPIRE
  364. unset REVOKER
  365. unset DOMAIN
  366. unset TRUST
  367. unset DEPTH
  368. # get options for key generation and add-certifier functions
  369. TEMP=$(getopt -o l:e:r:n:t:d: -l length:,expire:,revoker:,domain:,trust:,depth: -n "$PGRM" -- "$@")
  370. if [ $? != 0 ] ; then
  371. usage
  372. exit 1
  373. fi
  374. # Note the quotes around `$TEMP': they are essential!
  375. eval set -- "$TEMP"
  376. while true ; do
  377. case "$1" in
  378. -l|--length)
  379. KEY_LENGTH="$2"
  380. shift 2
  381. ;;
  382. -e|--expire)
  383. KEY_EXPIRE="$2"
  384. shift 2
  385. ;;
  386. -r|--revoker)
  387. REVOKER="$2"
  388. shift 2
  389. ;;
  390. -n|--domain)
  391. DOMAIN="$2"
  392. shift 2
  393. ;;
  394. -t|--trust)
  395. TRUST="$2"
  396. shift 2
  397. ;;
  398. -d|--depth)
  399. DEPTH="$2"
  400. shift 2
  401. ;;
  402. --)
  403. shift
  404. ;;
  405. *)
  406. break
  407. ;;
  408. esac
  409. done
  410. case $COMMAND in
  411. 'update-users'|'update-user'|'u')
  412. update_users "$@"
  413. ;;
  414. 'gen-key'|'g')
  415. gen_key "$@"
  416. ;;
  417. 'show-fingerprint'|'f')
  418. fingerprint_server_key
  419. ;;
  420. 'publish-key'|'p')
  421. publish_server_key
  422. ;;
  423. 'add-identity-certifier'|'add-certifier'|'a')
  424. if [ -z "$1" ] ; then
  425. failure "You must specify a key ID."
  426. fi
  427. add_certifier "$1"
  428. ;;
  429. 'remove-identity-certifier'|'remove-certifier'|'r')
  430. if [ -z "$1" ] ; then
  431. failure "You must specify a key ID."
  432. fi
  433. remove_certifier "$1"
  434. ;;
  435. 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
  436. list_certifiers "$@"
  437. ;;
  438. 'help'|'h'|'?')
  439. usage
  440. ;;
  441. *)
  442. failure "Unknown command: '$COMMAND'
  443. Type '$PGRM help' for usage."
  444. ;;
  445. esac
  446. exit "$RETURN"