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