Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SCP username and destination path handling #1350

Merged
merged 6 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix SQL errors in SCAP and CERT update [#1343](https://github.com/greenbone/gvmd/pull/1343)
- Check private key when modifying credential [#1351](https://github.com/greenbone/gvmd/pull/1351)
- Clean up hosts strings before using them [#1352](https://github.com/greenbone/gvmd/pull/1352)
- Improve SCP username and destination path handling [#1350](https://github.com/greenbone/gvmd/pull/1350)


### Removed
Expand Down
24 changes: 20 additions & 4 deletions src/alert_methods/SCP/alert
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
# Copyright (C) 2016-2018 Greenbone Networks GmbH
#
# SPDX-License-Identifier: AGPL-3.0-or-later
Expand Down Expand Up @@ -39,12 +39,25 @@ log_error() {
echo "$MESSAGE" >&2
}

# Escape destination twice because it is also expanded on the remote end.
shell_esc() {
printf "%q" "$1"
}

if [ -z "$GVMD_SCP_ALERT_TIMEOUT" ]
then
TIMEOUT="15m"
else
TIMEOUT="$GVMD_SCP_ALERT_TIMEOUT"
fi

# Escape destination because it is also expanded on the remote end.
DEST_ESC=`shell_esc "$DEST"`

if [ -z "$PRIVATE_KEY_FILE" ]
then
sshpass -f ${PASSWORD_FILE} scp -o HashKnownHosts=no -o UserKnownHostsFile="${KNOWN_HOSTS_FILE} ~/.ssh/known_hosts ~/.ssh/known_hosts2 /etc/ssh/ssh_known_hosts" "${REPORT_FILE}" "${USERNAME}@${HOST}:'${DEST}'" 2>$ERROR_FILE
timeout $TIMEOUT sshpass -f ${PASSWORD_FILE} scp -o HashKnownHosts=no -o UserKnownHostsFile="${KNOWN_HOSTS_FILE} ~/.ssh/known_hosts ~/.ssh/known_hosts2 /etc/ssh/ssh_known_hosts" "${REPORT_FILE}" "${USERNAME}@${HOST}:${DEST_ESC}" 2>$ERROR_FILE
else
sshpass -f ${PASSWORD_FILE} -P "passphrase" scp -i "$PRIVATE_KEY_FILE" -o PasswordAuthentication=no -o HashKnownHosts=no -o UserKnownHostsFile="${KNOWN_HOSTS_FILE} ~/.ssh/known_hosts ~/.ssh/known_hosts2 /etc/ssh/ssh_known_hosts" "${REPORT_FILE}" "${USERNAME}@${HOST}:'${DEST}'" 2>$ERROR_FILE
timeout $TIMEOUT sshpass -f ${PASSWORD_FILE} -P "passphrase" scp -i "$PRIVATE_KEY_FILE" -o PasswordAuthentication=no -o HashKnownHosts=no -o UserKnownHostsFile="${KNOWN_HOSTS_FILE} ~/.ssh/known_hosts ~/.ssh/known_hosts2 /etc/ssh/ssh_known_hosts" "${REPORT_FILE}" "${USERNAME}@${HOST}:${DEST_ESC}" 2>$ERROR_FILE
fi

EXIT_CODE=$?
Expand All @@ -69,6 +82,9 @@ then
elif [ $EXIT_CODE -eq 6 ]
then
log_error "sshpass failed with exit code ${EXIT_CODE}: Host public key is unknown: $ERROR_SHORT"
elif [ $EXIT_CODE -eq 124 ]
then
log_error "sshpass failed with exit code ${EXIT_CODE}: Timeout after $TIMEOUT: $ERROR_SHORT"
elif [ $EXIT_CODE -eq 127 ]
then
log_error "sshpass failed with exit code ${EXIT_CODE}: Command not found: $ERROR_SHORT"
Expand Down
2 changes: 1 addition & 1 deletion src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -6764,7 +6764,7 @@ validate_scp_data (alert_method_t method, const gchar *name, gchar **data)
return 18;
}

if (strchr (username, '@') || strchr (username, ':'))
if (strchr (username, ':'))
{
g_free (username);
return 18;
Expand Down