summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 68c4b243ce5e50b8541f0ece0e7498c0621a4570 (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 "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
  305. failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
  306. }
  307. # retrieve key from web of trust, import it into the host keyring, and
  308. # ltsign the key in the host keyring so that it may certify other keys
  309. add_certifier() {
  310. local domain
  311. local trust
  312. local depth
  313. local keyID
  314. local fingerprint
  315. local ltsignCommand
  316. # set default values for trust depth and domain
  317. domain=
  318. trust=2
  319. depth=1
  320. # get options
  321. TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
  322. if [ $? != 0 ] ; then
  323. exit 1
  324. fi
  325. # Note the quotes around `$TEMP': they are essential!
  326. eval set -- "$TEMP"
  327. while true ; do
  328. case "$1" in
  329. -n|--domain)
  330. domain="$2"
  331. shift 2
  332. ;;
  333. -t|--trust)
  334. trust="$2"
  335. shift 2
  336. ;;
  337. -d|--depth)
  338. depth="$2"
  339. shift 2
  340. ;;
  341. --)
  342. shift
  343. ;;
  344. *)
  345. break
  346. ;;
  347. esac
  348. done
  349. keyID="$1"
  350. export keyID
  351. # export host ownertrust to authentication keyring
  352. gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
  353. # get the key from the key server
  354. gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
  355. # get the full fingerprint of a key ID
  356. fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
  357. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  358. echo "key found:"
  359. gpg_authentication "--fingerprint $fingerprint"
  360. 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}
  361. if [ "${OK/y/Y}" != 'Y' ] ; then
  362. failure "aborting."
  363. fi
  364. # export the key to the host keyring
  365. gpg_authentication "--export $keyID" | gpg_host --import
  366. # ltsign command
  367. # NOTE: *all* user IDs will be ltsigned
  368. ltsignCommand=$(cat <<EOF
  369. ltsign
  370. y
  371. $trust
  372. $depth
  373. $domain
  374. y
  375. save
  376. EOF
  377. )
  378. # ltsign the key
  379. echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  380. # update the trustdb for the authentication keyring
  381. gpg_authentication "--check-trustdb"
  382. }
  383. # delete a certifiers key from the host keyring
  384. remove_certifier() {
  385. local keyID
  386. local fingerprint
  387. keyID="$1"
  388. # delete the requested key (with prompting)
  389. gpg_host --delete-key "$keyID"
  390. # update the trustdb for the authentication keyring
  391. gpg_authentication "--check-trustdb"
  392. }
  393. # list the host certifiers
  394. list_certifiers() {
  395. gpg_host --list-keys
  396. }
  397. # issue command to gpg-authentication keyring
  398. gpg_authentication_cmd() {
  399. gpg_authentication "$@"
  400. }
  401. ########################################################################
  402. # MAIN
  403. ########################################################################
  404. # unset variables that should be defined only in config file
  405. unset KEYSERVER
  406. unset AUTHORIZED_USER_IDS
  407. unset RAW_AUTHORIZED_KEYS
  408. unset MONKEYSPHERE_USER
  409. # load configuration file
  410. [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
  411. # set empty config variable with ones from the environment, or with
  412. # defaults
  413. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
  414. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
  415. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  416. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  417. # other variables
  418. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  419. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  420. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
  421. GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
  422. # export variables needed in su invocation
  423. export DATE
  424. export MODE
  425. export MONKEYSPHERE_USER
  426. export KEYSERVER
  427. export CHECK_KEYSERVER
  428. export REQUIRED_USER_KEY_CAPABILITY
  429. export GNUPGHOME_HOST
  430. export GNUPGHOME_AUTHENTICATION
  431. export GNUPGHOME
  432. # get subcommand
  433. COMMAND="$1"
  434. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  435. shift
  436. case $COMMAND in
  437. 'update-users'|'update-user'|'u')
  438. update_users "$@"
  439. ;;
  440. 'gen-key'|'g')
  441. gen_key "$@"
  442. ;;
  443. 'show-fingerprint'|'f')
  444. fingerprint_server_key
  445. ;;
  446. 'publish-key'|'p')
  447. publish_server_key
  448. ;;
  449. 'add-identity-certifier'|'add-certifier'|'a')
  450. if [ -z "$1" ] ; then
  451. failure "You must specify a key ID."
  452. fi
  453. add_certifier "$1"
  454. ;;
  455. 'remove-identity-certifier'|'remove-certifier'|'r')
  456. if [ -z "$1" ] ; then
  457. failure "You must specify a key ID."
  458. fi
  459. remove_certifier "$1"
  460. ;;
  461. 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
  462. list_certifiers "$@"
  463. ;;
  464. 'gpg-authentication-cmd')
  465. gpg_authentication_cmd "$@"
  466. ;;
  467. 'help'|'h'|'?')
  468. usage
  469. ;;
  470. *)
  471. failure "Unknown command: '$COMMAND'
  472. Type '$PGRM help' for usage."
  473. ;;
  474. esac
  475. exit "$RETURN"