-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ssh
More file actions
executable file
·478 lines (399 loc) · 11.7 KB
/
Copy pathgit-ssh
File metadata and controls
executable file
·478 lines (399 loc) · 11.7 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/sh
# Copyright (C) 2025 pyamsoft
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Export the LC as the default C so that we do not run into locale based quirks
LC_ALL=C
export LC_ALL
readonly APP_NAME="git-ssh"
readonly VERSION="3.2.5"
readonly CONFIG_VERSION=2
# Where our configs live
readonly CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/${APP_NAME}"
##
# Log
#
# Accepts arguments in the same format as printf
# $1 message
# $@ arguments
##
_log() {
_msg="$1"
shift
# We log to stderr to avoid polluting a shell in $(eval) mode
printf -- "[%s]: ${_msg}\n" "${APP_NAME}" "$@" 1>&2
unset _msg
return 0
}
##
# Print help
##
_print_usage() {
_print_version || return 1
cat 1>&2 <<EOF
[Environment]
CONFIG_DIR=${CONFIG_DIR}
CONFIG_VERSION=${CONFIG_VERSION}
[Commands]
create <config> <path> Create a new <config> pointing to file name <path>
delete <config> Delete a <config>
list [-v|--verbose] List all configs in CONFIG_DIR
export [-a|--add] <config> Export a config into the environment as GIT_SSH_COMMAND
reset Reset GIT_SSH_COMMAND, unsetting an exported configs
help | -H | --help This help
version | -V | --version Display application version
EOF
return 0
}
##
# List configs
##
_list_configs() {
list_configs__verbose="$1"
list_configs__spacecount=0
# Only perform counting if we are NOT in verbose mode
if ! { [ -n "${list_configs__verbose}" ] && { [ "${list_configs__verbose}" = "-v" ] || [ "${list_configs__verbose}" = "--verbose" ]; }; }; then
for _config in "${CONFIG_DIR}"/*."${CONFIG_VERSION}"; do
# Make sure a file exists
if [ ! -e "${_config}" ]; then
unset _config
continue
fi
_config_name="${_config##*/}"
_config_namelen="${#_config_name}"
unset _config_name
if [ "${_config_namelen}" -gt "${list_configs__spacecount}" ]; then
list_configs__spacecount="${_config_namelen}"
fi
unset _config
done
unset _config
fi
for _config in "${CONFIG_DIR}"/*."${CONFIG_VERSION}"; do
# Make sure a file exists
if [ ! -e "${_config}" ]; then
unset _config
continue
fi
_config_name="${_config##*/}"
if { [ -n "${list_configs__verbose}" ] && { [ "${list_configs__verbose}" = "-v" ] || [ "${list_configs__verbose}" = "--verbose" ]; }; }; then
_log 'Config: %s [%s]\n' "${_config_name}" "${_config}"
cat "${_config}" 1>&2
printf -- '\n' 1>&2
else
_config_namelen="${#_config_name}"
_config_spacelen="$((list_configs__spacecount - _config_namelen))"
_log "%s %s [%s]" "${_config_name}" "$(printf -- '%*s ' "${_config_spacelen}" '')" "${_config}"
unset _config_spacelen
unset _config_namelen
fi
unset _config_name
unset _config
done
unset _config
unset list_configs__verbose
unset list_configs__spacecount
return 0
}
##
# Help for the _create function
_usage_create() {
cat 1>&2 <<EOF
Usage:
${APP_NAME} create <config> <path>
Arguments:
<config> The name of the ${APP_NAME} config to create.
<path> The absolute file path to the SSH private key for this configuration.
EOF
}
##
# Create a new config
#
# $1 config name
# $2 ssh key path
##
_create_config() {
create_config__name="$1"
create_config__path="$2"
if [ -z "${create_config__name}" ]; then
_log 'Missing config name, cannot create.'
_usage_create || return 1
return 1
fi
if [ -z "${create_config__path}" ]; then
_log 'Missing config path, cannot create.'
_usage_create || return 1
return 1
fi
# Use the absolute path
create_config__path_orig="${create_config__path}"
create_config__path="$(realpath -- "${create_config__path}")" || {
_log 'Failed to locate ssh keyfile: %s, cannot create config' "${create_config__path_orig}"
unset create_config__path_orig
return 1
}
unset create_config__path_orig
if [ ! -e "${create_config__path}" ]; then
_log 'ssh keyfile does not exist at path: %s, cannot create config' "${create_config__path}"
return 1
fi
if [ ! -r "${create_config__path}" ]; then
_log 'ssh keyfile is not readable at path: %s, cannot create config' "${create_config__path}"
return 1
fi
create_config__file="${CONFIG_DIR}/${create_config__name}.${CONFIG_VERSION}"
if [ -e "${create_config__file}" ]; then
_log 'Config already exists at %s' "${create_config__file}"
return 1
fi
cat >"${create_config__file}" <<EOF || return 1
# Created by ${APP_NAME} ${VERSION}
#
# You may modify this file, but it may be overwritten without warning.
Host *
IdentityFile ${create_config__path}
IdentitiesOnly yes
AddKeysToAgent yes
##
# Use git with SSH over an HTTP proxy
#
# You must have the "socat" command installed
#
# Replace <server ip address> with your server IP address
# Replace <port> with your port
#
# For more info: https://gist.github.com/ozbillwang/005bd1dfc597a2f3a00148834ad3e551
##
# ProxyCommand /usr/bin/socat STDIO PROXY:<hostname>:%h:%p,proxyport=<port>
# ProxyCommand /usr/bin/socat STDIO SOCKS4A:<hostname>:%h:%p,socksport=<port>
EOF
_log 'Created new config %s at %s' "${create_config__name}" "${create_config__file}"
unset create_config__name
unset create_config__path
unset create_config__file
return 0
}
##
# Help for the _delete function
_usage_delete() {
cat 1>&2 <<EOF
Usage:
${APP_NAME} delete <config>
Arguments:
<config> The name of the ${APP_NAME} config to delete.
EOF
}
##
# Delete an existing config
#
# $1 config name
##
_delete_config() {
delete_config__name="$1"
if [ -z "${delete_config__name}" ]; then
_log 'Missing config name, cannot delete.'
_usage_delete || return 1
return 1
fi
delete_config__file="${CONFIG_DIR}/${delete_config__name}.${CONFIG_VERSION}"
if [ ! -e "${delete_config__file}" ]; then
_log 'Config for %s does not exist at %s' "${delete_config__name}" "${delete_config__file}"
return 1
fi
rm -f "${delete_config__file}" || return 1
_log 'Deleted config %s at %s' "${delete_config__name}" "${delete_config__file}"
unset delete_config__name
unset delete_config__file
return 0
}
##
# Ensure the agent knows the keyfile
#
# $1 config keyfile path
_ensure_agent_added_key() {
ensure_agent__path="$1"
# Locate the IdentityFile from the ssh configuration
ensure_agent__priv=$(awk '/^[[:space:]]*IdentityFile[[:space:]]+/ { print $2; exit; }' "${ensure_agent__path}")
# Do not evaluate in a shell since that could lead to command execution
# instead, we replace ~/ with the expanded ${HOME} variable
ensure_agent__priv="$(printf '%s\n' "${ensure_agent__priv}" | sed "s|^~|$HOME|")"
ensure_agent__priv="$(realpath -- "${ensure_agent__priv}")"
ensure_agent__pub="${ensure_agent__priv}.pub"
# If the pubkey exists, check if it's already in the agent
if [ -f "${ensure_agent__pub}" ]; then
ensure_agent__key="$(awk '{ print $2 }' <"${ensure_agent__pub}")"
# Dump all known keys. If agent is not running, this loop will not run.
ensure_agent__allkeys="$(ssh-add -L 2>/dev/null | awk '{ print $2 }')"
if [ -n "${ensure_agent__allkeys}" ]; then
for _key in ${ensure_agent__allkeys}; do
# Found key, don't need anything
if [ "${ensure_agent__key}" = "${_key}" ]; then
return 0
fi
unset _key
done
unset _key
fi
# Not added, so add the key
ssh-add "${ensure_agent__priv}" >/dev/null || return 1
unset ensure_agent__allkeys
unset ensure_agent__key
fi
unset ensure_agent__pub
unset ensure_agent__priv
unset ensure_agent__path
return 0
}
##
# Help for the _export function
_usage_export() {
cat 1>&2 <<EOF
Usage:
${APP_NAME} export [options] <config>
Arguments:
<config> The name of a ${APP_NAME} configuration file.
This name should not include any file extensions.
Options:
[-a | --add] Add the exported SSH key to the agent if it is not already present.
EOF
}
##
# Output an environment for a config
#
# $1 config name
# $2 add key
##
_export_config() {
export_config__name="$1"
export_config__addkey="$2"
if [ -z "${export_config__name}" ]; then
_log 'Missing config name, cannot export.'
_usage_export || return 1
return 1
fi
export_config__file="${CONFIG_DIR}/${export_config__name}.${CONFIG_VERSION}"
if [ ! -e "${export_config__file}" ]; then
_log 'Missing config at: %s' "${export_config__file}"
_usage_export || return 1
return 1
fi
if [ ! -r "${export_config__file}" ]; then
_log 'Could not read config at: %s' "${export_config__file}"
_usage_export || return 1
return 1
fi
# Output the environment
export_config__env="export GIT_SSH_COMMAND='ssh -F \"${export_config__file}\"'"
printf -- '%s\n' "${export_config__env}"
if [ -n "${export_config__addkey}" ] && { [ "${export_config__addkey}" = "-a" ] || [ "${export_config__addkey}" = "--add" ]; }; then
_ensure_agent_added_key "${export_config__file}" || {
_log 'Failed to add %s SSH key to agent' "${export_config__name}"
_usage_export || return 1
return 1
}
fi
unset export_config__addkey
unset export_config__env
unset export_config__file
unset export_config__name
return 0
}
_reset_environment() {
# Reset the environment
printf -- 'export GIT_SSH_COMMAND=\nunset GIT_SSH_COMMAND\n'
return 0
}
_print_version() {
_log '%s' "${VERSION}"
return 0
}
main() {
mkdir -p "${CONFIG_DIR}" || {
_log 'Unable to make config directory at: %s' "${CONFIG_DIR}"
return 1
}
# Commands
main__command="$1"
main__export_add_key=""
if [ -z "${main__command}" ]; then
_print_usage || return 1
return 0
fi
while [ -n "${main__command}" ]; do
case "${main__command}" in
help | -H | --help)
_print_usage || return 1
return 0
;;
version | -V | --version)
_print_version || return 1
return 0
;;
list)
if [ -z "${main__list_configs_verbose}" ]; then
main__list_configs_verbose="$2"
fi
_list_configs "${main__list_configs_verbose}" || return 1
return 0
;;
export)
# -a can be before the config or after the config like the old way
case "$2" in
-a | --add)
main__export_add_key="$2"
main__config="$3"
;;
*)
main__config="$2"
main__export_add_key="$3"
;;
esac
_export_config "${main__config}" "${main__export_add_key}" || return 1
return 0
;;
create)
main__config="$2"
main__file_path="$3"
_create_config "${main__config}" "${main__file_path}" || return 1
return 0
;;
delete)
main__config="$2"
_delete_config "${main__config}" || return 1
return 0
;;
reset)
_reset_environment || return 1
return 0
;;
-v | --verbose)
main__list_configs_verbose="${main__command}"
shift
;;
*)
_print_usage || return 1
return 1
;;
esac
main__command="$1"
done
if [ -z "${main__command}" ]; then
_print_usage || return 1
return 0
fi
return 0
}
main "$@" || exit 1
exit 0