--- /dev/null
+#! /bin/sh
+###
+### Generate a user key
+###
+### (c) 2011 Mark Wooding
+###
+
+###----- Licensing notice ---------------------------------------------------
+###
+### This file is part of the distorted.org.uk key management suite.
+###
+### distorted-keys is free software; you can redistribute it and/or modify
+### it under the terms of the GNU General Public License as published by
+### the Free Software Foundation; either version 2 of the License, or
+### (at your option) any later version.
+###
+### distorted-keys is distributed in the hope that it will be useful,
+### but WITHOUT ANY WARRANTY; without even the implied warranty of
+### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+### GNU General Public License for more details.
+###
+### You should have received a copy of the GNU General Public License
+### along with distorted-keys; if not, write to the Free Software Foundation,
+### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+set -e
+case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac
+. "$KEYSLIB"/keyfunc.sh
+
+defhelp <<HELP
+[-f] KEY PROFILE [ARGUMENTS ...]
+Generate a named KEY, according to the PROFILE.
+
+The ARGUMENTS are passed to the key-type handler selected by the profile.
+
+Options:
+ -f Force overwriting an existing key.
+HELP
+
+genhook_recov () {
+ base=$1 nub=$2
+ for recov in $kopt_recovery; do
+ (stash $recov $kowner/$klabel <"$nub")
+ done
+}
+
+## Parse command-line arguments.
+forcep=nil
+while getopts "f" opt; do
+ case "$opt" in
+ f) forcep=t ;;
+ *) usage_err ;;
+ esac
+done
+shift $(( $OPTIND - 1 ))
+case $# in 0 | 1) usage_err ;; esac
+key=$1 profile=$2; shift 2
+
+## Check the key name carefully. This is where we actually create files,
+## so it's reallly important to make sure that we don't make any stupid
+## mistakes.
+mktmp
+parse_keylabel "$key"
+case "$kowner" in
+ "$USERV_USER") ;;
+ *) echo >&2 "$quis: invalid owner \`$kowner' for key"; exit 1 ;;
+esac
+case $forcep in
+ nil)
+ if [ -d $kdir ]; then
+ echo >&2 "$quis: key \`$key' already exists"
+ exit 1
+ fi
+ ;;
+esac
+
+## Check the profile name. We shouldn't allow users to refer to each
+## other's profiles.
+case "$profile" in
+ :*)
+ label=${profile#:}
+ ;;
+ *:*)
+ powner=${profile%%:*} label=${profile#*:}
+ case "$powner" in
+ "$USERV_USER") ;;
+ *) echo >&2 "$quis: invalid owner \`$powner' for profile"; exit 1 ;;
+ esac
+ ;;
+ *)
+ label=$profile
+ ;;
+esac
+checkword "profile label" "$label"
+read_profile $profile
+
+## Generate the key.
+c_genkey $profile $kdir $knub genhook_recov "$@"
+
+###----- That's all, folks --------------------------------------------------