Commit | Line | Data |
---|---|---|
c47f2aba MW |
1 | #! /bin/sh |
2 | ### | |
3 | ### Generate a user key | |
4 | ### | |
5 | ### (c) 2011 Mark Wooding | |
6 | ### | |
7 | ||
8 | ###----- Licensing notice --------------------------------------------------- | |
9 | ### | |
10 | ### This file is part of the distorted.org.uk key management suite. | |
11 | ### | |
12 | ### distorted-keys is free software; you can redistribute it and/or modify | |
13 | ### it under the terms of the GNU General Public License as published by | |
14 | ### the Free Software Foundation; either version 2 of the License, or | |
15 | ### (at your option) any later version. | |
16 | ### | |
17 | ### distorted-keys is distributed in the hope that it will be useful, | |
18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ### GNU General Public License for more details. | |
21 | ### | |
22 | ### You should have received a copy of the GNU General Public License | |
23 | ### along with distorted-keys; if not, write to the Free Software Foundation, | |
24 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | set -e | |
27 | case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac | |
28 | . "$KEYSLIB"/keyfunc.sh | |
29 | ||
30 | defhelp <<HELP | |
31 | [-f] KEY PROFILE [ARGUMENTS ...] | |
32 | Generate a named KEY, according to the PROFILE. | |
33 | ||
34 | The ARGUMENTS are passed to the key-type handler selected by the profile. | |
35 | ||
36 | Options: | |
37 | -f Force overwriting an existing key. | |
38 | HELP | |
39 | ||
40 | genhook_recov () { | |
41 | base=$1 nub=$2 | |
42 | for recov in $kopt_recovery; do | |
43 | (stash $recov $kowner/$klabel <"$nub") | |
44 | done | |
45 | } | |
46 | ||
47 | ## Parse command-line arguments. | |
48 | forcep=nil | |
49 | while getopts "f" opt; do | |
50 | case "$opt" in | |
51 | f) forcep=t ;; | |
52 | *) usage_err ;; | |
53 | esac | |
54 | done | |
55 | shift $(( $OPTIND - 1 )) | |
56 | case $# in 0 | 1) usage_err ;; esac | |
57 | key=$1 profile=$2; shift 2 | |
58 | ||
59 | ## Check the key name carefully. This is where we actually create files, | |
60 | ## so it's reallly important to make sure that we don't make any stupid | |
61 | ## mistakes. | |
62 | mktmp | |
63 | parse_keylabel "$key" | |
64 | case "$kowner" in | |
65 | "$USERV_USER") ;; | |
66 | *) echo >&2 "$quis: invalid owner \`$kowner' for key"; exit 1 ;; | |
67 | esac | |
68 | case $forcep in | |
69 | nil) | |
70 | if [ -d $kdir ]; then | |
71 | echo >&2 "$quis: key \`$key' already exists" | |
72 | exit 1 | |
73 | fi | |
74 | ;; | |
75 | esac | |
76 | ||
77 | ## Check the profile name. We shouldn't allow users to refer to each | |
78 | ## other's profiles. | |
79 | case "$profile" in | |
80 | :*) | |
81 | label=${profile#:} | |
82 | ;; | |
83 | *:*) | |
84 | powner=${profile%%:*} label=${profile#*:} | |
85 | case "$powner" in | |
86 | "$USERV_USER") ;; | |
87 | *) echo >&2 "$quis: invalid owner \`$powner' for profile"; exit 1 ;; | |
88 | esac | |
89 | ;; | |
90 | *) | |
91 | label=$profile | |
92 | ;; | |
93 | esac | |
94 | checkword "profile label" "$label" | |
95 | read_profile $profile | |
96 | ||
97 | ## Generate the key. | |
98 | c_genkey $profile $kdir $knub genhook_recov "$@" | |
99 | ||
100 | ###----- That's all, folks -------------------------------------------------- |