2 * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libssh/sftp.h>
23 #include "libavutil/avstring.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/attributes.h"
41 static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
47 ssh_options_set(libssh->session, SSH_OPTIONS_USER, user);
49 auth_methods = ssh_userauth_list(libssh->session, NULL);
51 if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
52 if (libssh->priv_key) {
54 ssh_private_key priv_key;
56 if (!ssh_try_publickey_from_file(libssh->session, libssh->priv_key, &pub_key, &type)) {
57 priv_key = privatekey_from_file(libssh->session, libssh->priv_key, type, password);
58 if (ssh_userauth_pubkey(libssh->session, NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
59 av_log(libssh, AV_LOG_DEBUG, "Authentication successful with selected private key.\n");
63 av_log(libssh, AV_LOG_DEBUG, "Invalid key is provided.\n");
64 return AVERROR(EACCES);
66 } else if (ssh_userauth_autopubkey(libssh->session, password) == SSH_AUTH_SUCCESS) {
67 av_log(libssh, AV_LOG_DEBUG, "Authentication successful with auto selected key.\n");
72 if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
73 if (ssh_userauth_password(libssh->session, NULL, password) == SSH_AUTH_SUCCESS) {
74 av_log(libssh, AV_LOG_DEBUG, "Authentication successful with password.\n");
80 av_log(libssh, AV_LOG_ERROR, "Authentication failed.\n");
81 return AVERROR(EACCES);
87 static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
91 if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
92 access = O_CREAT | O_RDWR;
95 } else if (flags & AVIO_FLAG_WRITE) {
96 access = O_CREAT | O_WRONLY;
102 /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
103 if (!(libssh->file = sftp_open(libssh->sftp, file, access, 0666))) {
104 av_log(libssh, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(libssh->session));
111 static av_cold void libssh_stat_file(LIBSSHContext *libssh)
113 sftp_attributes stat;
115 if (!(stat = sftp_fstat(libssh->file))) {
116 av_log(libssh, AV_LOG_WARNING, "Cannot stat remote file.\n");
117 libssh->filesize = -1;
119 libssh->filesize = stat->size;
120 sftp_attributes_free(stat);
124 static int libssh_close(URLContext *h)
126 LIBSSHContext *s = h->priv_data;
132 ssh_disconnect(s->session);
133 ssh_free(s->session);
138 static int libssh_open(URLContext *h, const char *url, int flags)
140 static const int verbosity = SSH_LOG_NOLOG;
141 LIBSSHContext *s = h->priv_data;
142 char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
144 long timeout = s->rw_timeout * 1000;
145 const char *user = NULL, *pass = NULL;
148 av_url_split(proto, sizeof(proto),
149 credencials, sizeof(credencials),
150 hostname, sizeof(hostname),
155 if (port <= 0 || port > 65535)
158 if (!(s->session = ssh_new())) {
159 ret = AVERROR(ENOMEM);
162 user = av_strtok(credencials, ":", &end);
163 pass = av_strtok(end, ":", &end);
164 ssh_options_set(s->session, SSH_OPTIONS_HOST, hostname);
165 ssh_options_set(s->session, SSH_OPTIONS_PORT, &port);
166 ssh_options_set(s->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
168 ssh_options_set(s->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
170 if (ssh_connect(s->session) != SSH_OK) {
171 av_log(h, AV_LOG_ERROR, "Connection failed. %s\n", ssh_get_error(s->session));
176 if ((ret = libssh_authentication(s, user, pass)) < 0)
179 if (!(s->sftp = sftp_new(s->session))) {
180 av_log(h, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(s->session));
181 ret = AVERROR(ENOMEM);
185 if (sftp_init(s->sftp) != SSH_OK) {
186 av_log(h, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(s->session));
191 if ((ret = libssh_open_file(s, flags, path)) < 0)
203 static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
205 LIBSSHContext *s = h->priv_data;
208 if (s->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
209 av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
220 newpos = sftp_tell64(s->file);
223 newpos = s->filesize + pos;
226 return AVERROR(EINVAL);
229 if (sftp_seek64(s->file, newpos)) {
230 av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
237 static int libssh_read(URLContext *h, unsigned char *buf, int size)
239 LIBSSHContext *s = h->priv_data;
242 if ((bytes_read = sftp_read(s->file, buf, size)) < 0) {
243 av_log(h, AV_LOG_ERROR, "Read error.\n");
249 static int libssh_write(URLContext *h, const unsigned char *buf, int size)
251 LIBSSHContext *s = h->priv_data;
254 if ((bytes_written = sftp_write(s->file, buf, size)) < 0) {
255 av_log(h, AV_LOG_ERROR, "Write error.\n");
258 return bytes_written;
261 #define OFFSET(x) offsetof(LIBSSHContext, x)
262 #define D AV_OPT_FLAG_DECODING_PARAM
263 #define E AV_OPT_FLAG_ENCODING_PARAM
264 static const AVOption options[] = {
265 {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
266 {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
267 {"private_key", "set path to private key", OFFSET(priv_key), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D|E },
271 static const AVClass libssh_context_class = {
272 .class_name = "libssh",
273 .item_name = av_default_item_name,
275 .version = LIBAVUTIL_VERSION_INT,
278 URLProtocol ff_libssh_protocol = {
280 .url_open = libssh_open,
281 .url_read = libssh_read,
282 .url_write = libssh_write,
283 .url_seek = libssh_seek,
284 .url_close = libssh_close,
285 .priv_data_size = sizeof(LIBSSHContext),
286 .priv_data_class = &libssh_context_class,
287 .flags = URL_PROTOCOL_FLAG_NETWORK,