#include <libssh/sftp.h>
#include "libavutil/avstring.h"
#include "libavutil/opt.h"
+#include "libavutil/attributes.h"
#include "avformat.h"
#include "internal.h"
#include "url.h"
int64_t filesize;
int rw_timeout;
int trunc;
+ char *priv_key;
} LIBSSHContext;
+static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
+{
+ int authorized = 0;
+ int auth_methods;
+
+ if (user)
+ ssh_options_set(libssh->session, SSH_OPTIONS_USER, user);
+
+ auth_methods = ssh_userauth_list(libssh->session, NULL);
+
+ if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
+ if (libssh->priv_key) {
+ ssh_string pub_key;
+ ssh_private_key priv_key;
+ int type;
+ if (!ssh_try_publickey_from_file(libssh->session, libssh->priv_key, &pub_key, &type)) {
+ priv_key = privatekey_from_file(libssh->session, libssh->priv_key, type, password);
+ if (ssh_userauth_pubkey(libssh->session, NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
+ av_log(libssh, AV_LOG_DEBUG, "Authentication successful with selected private key.\n");
+ authorized = 1;
+ }
+ } else {
+ av_log(libssh, AV_LOG_DEBUG, "Invalid key is provided.\n");
+ return AVERROR(EACCES);
+ }
+ } else if (ssh_userauth_autopubkey(libssh->session, password) == SSH_AUTH_SUCCESS) {
+ av_log(libssh, AV_LOG_DEBUG, "Authentication successful with auto selected key.\n");
+ authorized = 1;
+ }
+ }
+
+ if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
+ if (ssh_userauth_password(libssh->session, NULL, password) == SSH_AUTH_SUCCESS) {
+ av_log(libssh, AV_LOG_DEBUG, "Authentication successful with password.\n");
+ authorized = 1;
+ }
+ }
+
+ if (!authorized) {
+ av_log(libssh, AV_LOG_ERROR, "Authentication failed.\n");
+ return AVERROR(EACCES);
+ }
+
+ return 0;
+}
+
+static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
+{
+ int access;
+
+ if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
+ access = O_CREAT | O_RDWR;
+ if (libssh->trunc)
+ access |= O_TRUNC;
+ } else if (flags & AVIO_FLAG_WRITE) {
+ access = O_CREAT | O_WRONLY;
+ if (libssh->trunc)
+ access |= O_TRUNC;
+ } else
+ access = O_RDONLY;
+
+ /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
+ if (!(libssh->file = sftp_open(libssh->sftp, file, access, 0666))) {
+ av_log(libssh, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(libssh->session));
+ return AVERROR(EIO);
+ }
+
+ return 0;
+}
+
static int libssh_close(URLContext *h)
{
LIBSSHContext *s = h->priv_data;
static const int verbosity = SSH_LOG_NOLOG;
LIBSSHContext *s = h->priv_data;
char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
- int port = 22, access, ret;
+ int port = 22, ret;
long timeout = s->rw_timeout * 1000;
const char *user = NULL, *pass = NULL;
char *end = NULL;
ssh_options_set(s->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
if (timeout > 0)
ssh_options_set(s->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
- if (user)
- ssh_options_set(s->session, SSH_OPTIONS_USER, user);
if (ssh_connect(s->session) != SSH_OK) {
av_log(h, AV_LOG_ERROR, "Connection failed. %s\n", ssh_get_error(s->session));
goto fail;
}
- if (ssh_userauth_autopubkey(s->session, pass) != SSH_AUTH_SUCCESS) {
- av_log(s, AV_LOG_DEBUG, "Authentication using public key failed, trying password method.\n");
- if (ssh_userauth_password(s->session, NULL, pass) != SSH_AUTH_SUCCESS) {
- av_log(h, AV_LOG_ERROR, "Authentication failed.\n");
- ret = AVERROR(EACCES);
- goto fail;
- }
- }
+ if ((ret = libssh_authentication(s, user, pass)) < 0)
+ goto fail;
if (!(s->sftp = sftp_new(s->session))) {
av_log(h, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(s->session));
goto fail;
}
- if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
- access = O_CREAT | O_RDWR;
- if (s->trunc)
- access |= O_TRUNC;
- } else if (flags & AVIO_FLAG_WRITE) {
- access = O_CREAT | O_WRONLY;
- if (s->trunc)
- access |= O_TRUNC;
- } else {
- access = O_RDONLY;
- }
-
- /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
- if (!(s->file = sftp_open(s->sftp, path, access, 0666))) {
- av_log(h, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(s->session));
- ret = AVERROR(EIO);
+ if ((ret = libssh_open_file(s, flags, path)) < 0)
goto fail;
- }
if (!(stat = sftp_fstat(s->file))) {
av_log(h, AV_LOG_WARNING, "Cannot stat remote file %s.\n", path);
static const AVOption options[] = {
{"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
{"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
+ {"private_key", "set path to private key", OFFSET(priv_key), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D|E },
{NULL}
};