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