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