| 1 |
#! /bin/sh |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
set -euf |
| 5 |
|
| 6 |
d=$HOME/.ssh |
| 7 |
p=${2:-authorized_keys} |
| 8 |
f=$d/$p |
| 9 |
|
| 10 |
if ! test -f "$f"; then |
| 11 |
# shellcheck disable=SC2174 # yes, we know that -m only applies to the deepest directory |
| 12 |
mkdir -m 700 -p "$d" |
| 13 |
touch "$f" |
| 14 |
chmod 600 "$f" |
| 15 |
fi |
| 16 |
|
| 17 |
while read l; do |
| 18 |
if [ "$l" = "$1" ]; then |
| 19 |
exit 0 |
| 20 |
fi |
| 21 |
done <"$f" |
| 22 |
|
| 23 |
# Add newline if necessary |
| 24 |
! test -s "$f" || tail -c1 < "$f" | read -r _ || echo >> "$f" |
| 25 |
|
| 26 |
echo "$1" >>"$f" |