Debian Codegraph @

libexec/container/key

raw symbols SCIP
1 #!/bin/sh
2
3 # Copyright (C) 2014-2026 Daniel Baumann <[email protected]>
4 #
5 # SPDX-License-Identifier: GPL-3.0+
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 set -e
21
22 PROJECT="open-infrastructure"
23 SOFTWARE="compute-tools"
24 PROGRAM="container"
25 COMMAND="$(basename ${0})"
26
27 KEYS="/etc/${SOFTWARE}/keys"
28
29 Parameters ()
30 {
31 GETOPT_LONGOPTIONS="add:,list,remove:,"
32 GETOPT_OPTIONS="a:,l,r:,"
33
34 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
35
36 if [ "${?}" != "0" ]
37 then
38 echo "'${COMMAND}': getopt exit" >&2
39 exit 1
40 fi
41
42 eval set -- "${PARAMETERS}"
43
44 while true
45 do
46 case "${1}" in
47 -a|--add)
48 ADD="${2}"
49 ACTION="add"
50 shift 2
51 ;;
52
53 -l|--list)
54 ACTION="list"
55 shift 1
56 ;;
57
58 -r|--remove)
59 REMOVE="${2}"
60 ACTION="remove"
61 shift 2
62 ;;
63
64 --)
65 shift 1
66 break
67 ;;
68
69 *)
70 echo "'${COMMAND}': getopt error" >&2
71 exit 1
72 ;;
73 esac
74 done
75 }
76
77 Usage ()
78 {
79 echo "Usage: ${PROGRAM} ${COMMAND} [-a|--add KEY_FILE|KEY_ID] [-l|--list] [-r|--remove KEY|KEY_ID]" >&2
80 echo
81 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
82
83 exit 1
84 }
85
86 Parameters "${@}"
87
88 if [ -z "${ACTION}" ]
89 then
90 Usage
91 fi
92
93 if [ ! -w "${KEYS}" ]
94 then
95 if [ "$(id -u)" -ne 0 ]
96 then
97 echo "'${COMMAND}': need root privileges (or write permissions to '${KEYS}')" >&2
98 exit 1
99 fi
100 fi
101
102 # Pre hooks
103 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
104 do
105 if [ -x "${FILE}" ]
106 then
107 "${FILE}"
108 fi
109 done
110
111 # Run
112 if [ ! -e "${KEYS}" ]
113 then
114 mkdir -p "${KEYS}"
115
116 chown root:root "${KEYS}"
117 chmod 0700 "${KEYS}"
118
119 cat > "${KEYS}/gnupg.conf" << EOF
120 keyserver hkps://keys.openpgp.org
121 keyserver-options include-revoked
122 keyserver-options no-honor-keyserver-url
123
124 cert-digest-algo SHA512
125 default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES ZLIB ZIP Uncompressed
126 personal-cipher-preferences AES256 AES192 AES
127 personal-compress-preferences ZLIB ZIP Uncompressed
128 personal-digest-preferences SHA512 SHA384 SHA256 SHA224
129
130 no-comments
131 no-emit-version
132 no-greeting
133 keyid-format 0xlong
134 list-options show-keyring
135 list-options show-uid-validity
136 verify-options show-uid-validity
137 with-fingerprint
138
139 charset utf-8
140 EOF
141
142 fi
143
144 case "${ACTION}" in
145 add)
146 if [ -e "${ADD}" ]
147 then
148 gpg --homedir "${KEYS}" --import "${ADD}"
149 elif [ -e "/usr/share/${SOFTWARE}/keys/${ADD}" ]
150 then
151 gpg --homedir "${KEYS}" --import "/usr/share/${SOFTWARE}/keys/${ADD}"
152 elif [ -e "/usr/share/${SOFTWARE}/keys/${ADD}.pub" ]
153 then
154 gpg --homedir "${KEYS}" --import "/usr/share/${SOFTWARE}/keys/${ADD}.pub"
155 else
156 gpg --homedir "${KEYS}" --recv "${ADD}"
157 fi
158 ;;
159
160 list)
161 gpg --homedir "${KEYS}" --list-keys
162 ;;
163
164 remove)
165 gpg --homedir "${KEYS}" --delete-keys "${REMOVE}"
166 ;;
167 esac
168
169 # Post hooks
170 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
171 do
172 if [ -x "${FILE}" ]
173 then
174 "${FILE}"
175 fi
176 done