forked from OpenVPN/easy-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyrsa.completion
300 lines (282 loc) · 9.84 KB
/
easyrsa.completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
#
# easyrsa completion
#
# Copyright (C) 2020 Eric Belhomme <[email protected]>
#
# This code released under version 2 of the GNU GPL; see COPYING and the
# Licensing/ directory of this project for full licensing details.
# locate easyrsa distribution directory
_find_easyrsa_dir() {
local EASYRSA
EASYRSA=$(which easyrsa)
if [ -L "$EASYRSA" ]; then
EASYRSA=$(readlink -f "$EASYRSA")
fi
echo -n "$(dirname "$EASYRSA")"
}
# retrieve easyrsa vars file
_find_vars() {
local VARS I ret
local COMP_WORDS
COMP_WORDS=($@)
# find for --vars parameter in current cmdline
for I in "${!COMP_WORDS[@]}"; do
if echo "${COMP_WORDS[$I]}" | grep -wq -- '--vars'; then
VARS="${COMP_WORDS[$((I+2))]}"
break
fi
done
ret=1
if [ -n "$VARS" ] && [ -e "$VARS" ]; then
# priority to vars parameter in cmdline if specified
ret=0
elif [ -n "$EASYRSA_VARS_FILE" ] && [ -e "$EASYRSA_VARS_FILE" ]; then
# EASYRSA_VARS_FILE environement
VARS=$EASYRSA_VARS_FILE
ret=0
else
# failback to search vars into easyrsa binary executable directory
VARS=
local EASYRSA
EASYRSA=$(_find_easyrsa_dir)
if [ -e "$EASYRSA/vars" ]; then
VARS="$EASYRSA/vars"
ret=0
fi
fi
echo -n "$VARS"
return $ret
}
# retrieve 'set_var' variable in vars file if defined
_get_set_var() {
if [ -z "$1" ]; then return 1; fi
local VAR
VAR=$(grep -P "^set_var +$2 +" "$1" | cut -d'"' -f2)
if [ "${PIPESTATUS[0]}" -eq 0 ]; then
echo -n "$VAR"
return 0
fi
return 1
}
# retrieve EASYRSA_PKI directory
_find_pki_dir() {
local VARS ret
if [ -n "$1" ]; then
VARS="$1"
else
VARS="$(_find_vars)"
fi
local PKI_DIR
if ! PKI_DIR="$( eval echo "$(_get_set_var "$VARS" 'EASYRSA_PKI')")"; then
if [ -n "$EASYRSA_PKI" ]; then
PKI_DIR="$EASYRSA_PKI"
ret=0
else
PKI_DIR=
ret=1
fi
else
ret=0
fi
echo -n "$PKI_DIR"
return $ret
}
# retrieve x509_types directory. Usually in distribution dir unless overidden in vars file
_get-x509_types() {
local VARS
if [ -n "$1" ]; then
VARS="$1"
else
VARS="$(_find_vars)"
fi
local X509_TYPES_PATH
if ! X509_TYPES_PATH=$(_get_set_var "$VARS" 'EASYRSA_EXT_DIR'); then
X509_TYPES_PATH="$(_find_easyrsa_dir)/x509-types"
fi
for f in "${X509_TYPES_PATH}"/*; do
basename "$f"
done
}
# retrieve files in PKI directories
_get_easyrsa_pki_files() {
local VARS subdir f ext
VARS="$1"
subdir="$2"
ext=
if [ -n "$3" ]; then ext=".$3"; fi
local PKI_DIR
PKI_DIR=$(_find_pki_dir "$VARS")
if ! PKI_DIR=$(_find_pki_dir "$VARS"); then return 1; fi
if [ ! -d "${PKI_DIR}/${subdir}" ]; then return 1; fi
for f in "${PKI_DIR}"/"${subdir}"/*"$ext"; do
if [ -n "$ext" ] && [[ "$f" =~ .*${ext} ]]; then
continue
fi
basename "$f"
done
}
_complete_easyrsa() {
local cur # prev words cword
_init_completion -n : || return
local EASYRSA_COMMANDS="init-pki build-ca gen-dh gen-req sign-req build-client-full build-server-full revoke renew build-serverClient-full gen-crl update-db show-req show-cert show-ca import-req export-p7 export-p8 export-p12 set-rsa-pass set-ec-pass upgrade"
local EASYRSA_OPTIONS="--batch --passin= --passout= --pki-dir= --vars= --version --days= --digest= --dn-mode= --keysize= --req-cn= --subca-len= --subject-alt-name= --use-algo= --curve= --copy-ext --req-c= --req-st= --req-city= --req-org= --req-email= --req-ou="
local I CMDIDX VARS
# retrieve easyrsa command index number in array
CMDIDX=0
for I in "${!COMP_WORDS[@]}"; do
if echo "$EASYRSA_COMMANDS" | grep -wq -- "${COMP_WORDS[$I]}"; then
CMDIDX=$I
break
fi
done
# until we don't get an identified easyrsa command, display both main commands and options
if [ "$CMDIDX" -eq 0 ]; then
if [ "${COMP_WORDS[$COMP_CWORD]}" == '' ]; then
COMPREPLY=( $(compgen -W "$EASYRSA_COMMANDS $EASYRSA_OPTIONS" -- "$cur") )
else
if [[ "${COMP_WORDS[$COMP_CWORD]}" =~ ^-.* ]]; then
compopt +o nospace
COMPREPLY=( $(compgen -W "$EASYRSA_OPTIONS" -- "$cur") )
else
COMPREPLY=( $(compgen -W "$EASYRSA_COMMANDS" -- "$cur") )
fi
fi
return 0
fi
# process main commands
case ${COMP_WORDS[$CMDIDX]} in
# no extra args for these commands
init-pki | gen-dh | gen-crl | update-db)
;;
build-ca) # [ cmd-opts ]
if [ "$COMP_CWORD" -eq $((CMDIDX +1)) ]; then
compopt +o nosort
COMPREPLY=( $(compgen -o -W "nopass subca intca" -- "$cur") )
fi
;;
gen-req) # <filename_base> [ cmd-opts ]
case $((COMP_CWORD - CMDIDX)) in
1)
;;
2)
COMPREPLY=( $(compgen -W "nopass" -- "$cur") )
;;
esac
;;
sign-req) # <type> <filename_base>
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get-x509_types $VARS)" -- "$cur") )
;;
2)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'reqs' 'req' )" -- "$cur") )
;;
3)
COMPREPLY=( $(compgen -W "nopass" -- "$cur") )
;;
esac
;;
build-client-full | build-server-full | build-serverClient-full) # <filename_base> [ cmd-opts ]
case $((COMP_CWORD - CMDIDX)) in
1)
;;
2 | 3)
COMPREPLY=( $(compgen -W "nopass inline" -- "$cur") )
;;
esac
;;
revoke) # <filename_base> [cmd-opts]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'issued' 'crt' )" -- "$cur") )
;;
2)
COMPREPLY=( $(compgen -W "unspecified keyCompromise CACompromise affiliationChanged superseded cessationOfOperation certificateHold" -- "$cur") )
;;
esac
;;
renew) # <filename_base> [cmd-opts]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'issued' 'crt' )" -- "$cur") )
;;
2)
COMPREPLY=( $(compgen -W "nopass" -- "$cur") )
;;
esac
;;
show-req) # <filename_base> [ cmd-opts ]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'reqs' 'req' )" -- "$cur") )
;;
2)
COMPREPLY=( $(compgen -W "full" -- "$cur") )
;;
esac
;;
show-cert) # <filename_base> [ cmd-opts ]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'issued' 'crt' )" -- "$cur") )
;;
2)
COMPREPLY=( $(compgen -W "full" -- "$cur") )
;;
esac
;;
show-ca) # [ cmd-opts ]
if [ $((COMP_CWORD - CMDIDX)) -eq 1 ]; then
COMPREPLY=( $(compgen -W "full" -- "$cur") )
fi
;;
import-req) # <request_file_path> <short_basename>
;;
export-p7 | export-p8) # <filename_base> [ cmd-opts ]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'issued' 'crt' )" -- "$cur") )
;;
2)
COMPREPLY=( $(compgen -W "noca" -- "$cur") )
;;
esac
;;
export-p12) # <filename_base> [ cmd-opts ]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'issued' 'crt' )" -- "$cur") )
;;
2 | 3)
COMPREPLY=( $(compgen -W "noca nokey" -- "$cur") )
;;
esac
;;
set-rsa-pass | set-ec-pass) # <filename_base> [ cmd-opts ]
if ! VARS=$(_find_vars "${COMP_WORDS[@]}"); then return 1; fi
case $((COMP_CWORD - CMDIDX)) in
1)
COMPREPLY=( $(compgen -W "$(_get_easyrsa_pki_files $VARS 'private' 'key' )" -- "$cur") )
;;
2 | 3)
COMPREPLY=( $(compgen -W "nopass file" -- "$cur") )
;;
esac
;;
upgrade) # <type>
if [ $((COMP_CWORD - CMDIDX)) -eq 1 ]; then
COMPREPLY=( $(compgen -W "pki ca" -- "$cur") )
fi
;;
esac
return 0
}
complete -F _complete_easyrsa easyrsa