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