summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 9205b1d315bce9b59cdfecb85df952012425d38a (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. # 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. RETURN="$?"
  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'" || failure
  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. echo "key found:"
  276. gpg_authentication "--fingerprint $fingerprint"
  277. 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}
  278. if [ "${OK/y/Y}" != 'Y' ] ; then
  279. failure "aborting."
  280. fi
  281. # export the key to the host keyring
  282. gpg_authentication "--export $keyID" | gpg_host --import
  283. # default values for trust depth and domain
  284. DOMAIN=${DOMAIN:-}
  285. TRUST=${TRUST:-2}
  286. DEPTH=${DEPTH:-1}
  287. # ltsign command
  288. # NOTE: *all* user IDs will be ltsigned
  289. ltsignCommand=$(cat <<EOF
  290. ltsign
  291. y
  292. $TRUST
  293. $DEPTH
  294. $DOMAIN
  295. y
  296. save
  297. EOF
  298. )
  299. # ltsign the key
  300. echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
  301. # update the trustdb for the authentication keyring
  302. gpg_authentication "--check-trustdb"
  303. }
  304. # delete a certifiers key from the host keyring
  305. remove_certifier() {
  306. local keyID
  307. local fingerprint
  308. keyID="$1"
  309. # delete the requested key (with prompting)
  310. gpg_host --delete-key "$keyID"
  311. # update the trustdb for the authentication keyring
  312. gpg_authentication "--check-trustdb"
  313. }
  314. # list the host certifiers
  315. list_certifiers() {
  316. gpg_host --list-keys
  317. }
  318. ########################################################################
  319. # MAIN
  320. ########################################################################
  321. # unset variables that should be defined only in config file
  322. unset KEYSERVER
  323. unset AUTHORIZED_USER_IDS
  324. unset RAW_AUTHORIZED_KEYS
  325. unset MONKEYSPHERE_USER
  326. # load configuration file
  327. [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
  328. # set empty config variable with ones from the environment, or with
  329. # defaults
  330. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
  331. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
  332. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  333. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  334. # other variables
  335. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  336. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  337. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
  338. GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
  339. # export variables needed in su invocation
  340. export DATE
  341. export MODE
  342. export MONKEYSPHERE_USER
  343. export KEYSERVER
  344. export CHECK_KEYSERVER
  345. export REQUIRED_USER_KEY_CAPABILITY
  346. export GNUPGHOME_HOST
  347. export GNUPGHOME_AUTHENTICATION
  348. export GNUPGHOME
  349. # get subcommand
  350. COMMAND="$1"
  351. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  352. shift
  353. # unset option variables
  354. unset KEY_LENGTH
  355. unset KEY_EXPIRE
  356. unset REVOKER
  357. unset DOMAIN
  358. unset TRUST
  359. unset DEPTH
  360. # get options for key generation and add-certifier functions
  361. TEMP=$(getopt -o l:e:r:n:t:d: -l length:,expire:,revoker:,domain:,trust:,depth: -n "$PGRM" -- "$@")
  362. if [ $? != 0 ] ; then
  363. usage
  364. exit 1
  365. fi
  366. # Note the quotes around `$TEMP': they are essential!
  367. eval set -- "$TEMP"
  368. while true ; do
  369. case "$1" in
  370. -l|--length)
  371. KEY_LENGTH="$2"
  372. shift 2
  373. ;;
  374. -e|--expire)
  375. KEY_EXPIRE="$2"
  376. shift 2
  377. ;;
  378. -r|--revoker)
  379. REVOKER="$2"
  380. shift 2
  381. ;;
  382. -n|--domain)
  383. DOMAIN="$2"
  384. shift 2
  385. ;;
  386. -t|--trust)
  387. TRUST="$2"
  388. shift 2
  389. ;;
  390. -d|--depth)
  391. DEPTH="$2"
  392. shift 2
  393. ;;
  394. --)
  395. shift
  396. ;;
  397. *)
  398. break
  399. ;;
  400. esac
  401. done
  402. case $COMMAND in
  403. 'update-users'|'update-user'|'u')
  404. update_users "$@"
  405. ;;
  406. 'gen-key'|'g')
  407. gen_key "$@"
  408. ;;
  409. 'show-fingerprint'|'f')
  410. fingerprint_server_key
  411. ;;
  412. 'publish-key'|'p')
  413. publish_server_key
  414. ;;
  415. 'add-identity-certifier'|'add-certifier'|'a')
  416. if [ -z "$1" ] ; then
  417. failure "You must specify a key ID."
  418. fi
  419. add_certifier "$1"
  420. ;;
  421. 'remove-identity-certifier'|'remove-certifier'|'r')
  422. if [ -z "$1" ] ; then
  423. failure "You must specify a key ID."
  424. fi
  425. remove_certifier "$1"
  426. ;;
  427. 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
  428. list_certifiers "$@"
  429. ;;
  430. 'help'|'h'|'?')
  431. usage
  432. ;;
  433. *)
  434. failure "Unknown command: '$COMMAND'
  435. Type '$PGRM help' for usage."
  436. ;;
  437. esac
  438. exit "$RETURN"