From a6c153b54654b639307f462baf6b4ce32da3014e Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Wed, 6 May 2020 00:40:36 +0200 Subject: [PATCH] Copy files if destination exists --- CHANGELOG.md | 1 + shellpki | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2554e03..b7bc4bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * CA key length is configurable (minimum 4096) * Add `--non-interactive` command line option * Add `--replace-existing` command line option +* Copy files if destination exists ### Changed diff --git a/shellpki b/shellpki index 8521024..63d80df 100755 --- a/shellpki +++ b/shellpki @@ -630,6 +630,33 @@ EOF chmod 640 "${ovpn_file}" echo "The OpenVPN config file is available at \`${ovpn_file}'" fi + + # Copy files if destination exists + if [ -d "${COPY_DIR}" ]; then + for file in "${crt_file}" "${key_file}" "${pkcs12_file}" "${ovpn_file}"; do + if [ -f "${file}" ]; then + new_file="${COPY_DIR}/$(basename "${file}")" + if [ "${replace_existing}" -eq 1 ]; then + cp -f "${file}" "${COPY_DIR}/" + else + if [ "${non_interactive}" -eq 1 ]; then + if [ -f "${new_file}" ]; then + echo "File \`${file}' has not been copied to \`${new_file}', it already exists" >&2 + continue + else + cp "${file}" "${COPY_DIR}/" + fi + else + cp -i "${file}" "${COPY_DIR}/" + fi + fi + echo "File \`${file}' has been copied to \`${new_file}'" + fi + done + + chown -R ${PKI_USER}:${PKI_USER} "${COPY_DIR}/" + chmod -R u=rwX,g=rwX,o= "${COPY_DIR}/" + fi fi } @@ -790,6 +817,8 @@ main() { PKCS12_DIR="${CA_DIR}/pkcs12" OVPN_DIR="${CA_DIR}/openvpn" + COPY_DIR="$(dirname "${CONF_FILE}")/copy_output" + CA_KEY_LENGTH=4096 if [ "${CA_KEY_LENGTH}" -lt 4096 ]; then error "CA key must be at least 4096 bits long."