FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libssh.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <fcntl.h>
22 #define LIBSSH_STATIC
23 #include <libssh/sftp.h>
24 #include "libavutil/avstring.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/attributes.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "url.h"
30 
31 typedef struct {
32  const AVClass *class;
33  ssh_session session;
34  sftp_session sftp;
35  sftp_file file;
36  int64_t filesize;
38  int trunc;
39  char *priv_key;
41 
42 static av_cold int libssh_create_ssh_session(LIBSSHContext *libssh, const char* hostname, unsigned int port)
43 {
44  static const int verbosity = SSH_LOG_NOLOG;
45 
46  if (!(libssh->session = ssh_new())) {
47  av_log(libssh, AV_LOG_ERROR, "SSH session creation failed: %s\n", ssh_get_error(libssh->session));
48  return AVERROR(ENOMEM);
49  }
50  ssh_options_set(libssh->session, SSH_OPTIONS_HOST, hostname);
51  ssh_options_set(libssh->session, SSH_OPTIONS_PORT, &port);
52  ssh_options_set(libssh->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
53  if (libssh->rw_timeout > 0) {
54  long timeout = libssh->rw_timeout * 1000;
55  ssh_options_set(libssh->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
56  }
57 
58  if (ssh_options_parse_config(libssh->session, NULL) < 0) {
59  av_log(libssh, AV_LOG_WARNING, "Could not parse the config file.\n");
60  }
61 
62  if (ssh_connect(libssh->session) != SSH_OK) {
63  av_log(libssh, AV_LOG_ERROR, "Connection failed: %s\n", ssh_get_error(libssh->session));
64  return AVERROR(EIO);
65  }
66 
67  return 0;
68 }
69 
70 static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
71 {
72  int authorized = 0;
73  int auth_methods;
74 
75  if (user)
76  ssh_options_set(libssh->session, SSH_OPTIONS_USER, user);
77 
78  if (ssh_userauth_none(libssh->session, NULL) == SSH_AUTH_SUCCESS)
79  return 0;
80 
81  auth_methods = ssh_userauth_list(libssh->session, NULL);
82 
83  if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
84  if (libssh->priv_key) {
85  ssh_string pub_key;
86  ssh_private_key priv_key;
87  int type;
88  if (!ssh_try_publickey_from_file(libssh->session, libssh->priv_key, &pub_key, &type)) {
89  priv_key = privatekey_from_file(libssh->session, libssh->priv_key, type, password);
90  if (ssh_userauth_pubkey(libssh->session, NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
91  av_log(libssh, AV_LOG_DEBUG, "Authentication successful with selected private key.\n");
92  authorized = 1;
93  }
94  } else {
95  av_log(libssh, AV_LOG_DEBUG, "Invalid key is provided.\n");
96  return AVERROR(EACCES);
97  }
98  } else if (ssh_userauth_autopubkey(libssh->session, password) == SSH_AUTH_SUCCESS) {
99  av_log(libssh, AV_LOG_DEBUG, "Authentication successful with auto selected key.\n");
100  authorized = 1;
101  }
102  }
103 
104  if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
105  if (ssh_userauth_password(libssh->session, NULL, password) == SSH_AUTH_SUCCESS) {
106  av_log(libssh, AV_LOG_DEBUG, "Authentication successful with password.\n");
107  authorized = 1;
108  }
109  }
110 
111  if (!authorized) {
112  av_log(libssh, AV_LOG_ERROR, "Authentication failed.\n");
113  return AVERROR(EACCES);
114  }
115 
116  return 0;
117 }
118 
120 {
121  if (!(libssh->sftp = sftp_new(libssh->session))) {
122  av_log(libssh, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(libssh->session));
123  return AVERROR(ENOMEM);
124  }
125 
126  if (sftp_init(libssh->sftp) != SSH_OK) {
127  av_log(libssh, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(libssh->session));
128  return AVERROR(EIO);
129  }
130 
131  return 0;
132 }
133 
134 static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
135 {
136  int access;
137 
138  if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
139  access = O_CREAT | O_RDWR;
140  if (libssh->trunc)
141  access |= O_TRUNC;
142  } else if (flags & AVIO_FLAG_WRITE) {
143  access = O_CREAT | O_WRONLY;
144  if (libssh->trunc)
145  access |= O_TRUNC;
146  } else
147  access = O_RDONLY;
148 
149  /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
150  if (!(libssh->file = sftp_open(libssh->sftp, file, access, 0666))) {
151  av_log(libssh, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(libssh->session));
152  return AVERROR(EIO);
153  }
154 
155  return 0;
156 }
157 
159 {
160  sftp_attributes stat;
161 
162  if (!(stat = sftp_fstat(libssh->file))) {
163  av_log(libssh, AV_LOG_WARNING, "Cannot stat remote file.\n");
164  libssh->filesize = -1;
165  } else {
166  libssh->filesize = stat->size;
167  sftp_attributes_free(stat);
168  }
169 }
170 
172 {
173  LIBSSHContext *libssh = h->priv_data;
174  if (libssh->file) {
175  sftp_close(libssh->file);
176  libssh->file = NULL;
177  }
178  if (libssh->sftp) {
179  sftp_free(libssh->sftp);
180  libssh->sftp = NULL;
181  }
182  if (libssh->session) {
183  ssh_disconnect(libssh->session);
184  ssh_free(libssh->session);
185  libssh->session = NULL;
186  }
187  return 0;
188 }
189 
190 static av_cold int libssh_open(URLContext *h, const char *url, int flags)
191 {
192  LIBSSHContext *libssh = h->priv_data;
193  char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
194  int port, ret;
195  const char *user = NULL, *pass = NULL;
196  char *end = NULL;
197 
198  av_url_split(proto, sizeof(proto),
199  credencials, sizeof(credencials),
200  hostname, sizeof(hostname),
201  &port,
202  path, sizeof(path),
203  url);
204 
205  // a port of 0 will use a port from ~/.ssh/config or the default value 22
206  if (port < 0 || port > 65535)
207  port = 0;
208 
209  if ((ret = libssh_create_ssh_session(libssh, hostname, port)) < 0)
210  goto fail;
211 
212  user = av_strtok(credencials, ":", &end);
213  pass = av_strtok(end, ":", &end);
214 
215  if ((ret = libssh_authentication(libssh, user, pass)) < 0)
216  goto fail;
217 
218  if ((ret = libssh_create_sftp_session(libssh)) < 0)
219  goto fail;
220 
221  if ((ret = libssh_open_file(libssh, flags, path)) < 0)
222  goto fail;
223 
224  libssh_stat_file(libssh);
225 
226  return 0;
227 
228  fail:
229  libssh_close(h);
230  return ret;
231 }
232 
233 static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
234 {
235  LIBSSHContext *libssh = h->priv_data;
236  int64_t newpos;
237 
238  if (libssh->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
239  av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
240  return AVERROR(EIO);
241  }
242 
243  switch(whence) {
244  case AVSEEK_SIZE:
245  return libssh->filesize;
246  case SEEK_SET:
247  newpos = pos;
248  break;
249  case SEEK_CUR:
250  newpos = sftp_tell64(libssh->file) + pos;
251  break;
252  case SEEK_END:
253  newpos = libssh->filesize + pos;
254  break;
255  default:
256  return AVERROR(EINVAL);
257  }
258 
259  if (newpos < 0) {
260  av_log(h, AV_LOG_ERROR, "Seeking to nagative position.\n");
261  return AVERROR(EINVAL);
262  }
263 
264  if (sftp_seek64(libssh->file, newpos)) {
265  av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
266  return AVERROR(EIO);
267  }
268 
269  return newpos;
270 }
271 
272 static int libssh_read(URLContext *h, unsigned char *buf, int size)
273 {
274  LIBSSHContext *libssh = h->priv_data;
275  int bytes_read;
276 
277  if ((bytes_read = sftp_read(libssh->file, buf, size)) < 0) {
278  av_log(libssh, AV_LOG_ERROR, "Read error.\n");
279  return AVERROR(EIO);
280  }
281  return bytes_read;
282 }
283 
284 static int libssh_write(URLContext *h, const unsigned char *buf, int size)
285 {
286  LIBSSHContext *libssh = h->priv_data;
287  int bytes_written;
288 
289  if ((bytes_written = sftp_write(libssh->file, buf, size)) < 0) {
290  av_log(libssh, AV_LOG_ERROR, "Write error.\n");
291  return AVERROR(EIO);
292  }
293  return bytes_written;
294 }
295 
296 #define OFFSET(x) offsetof(LIBSSHContext, x)
297 #define D AV_OPT_FLAG_DECODING_PARAM
298 #define E AV_OPT_FLAG_ENCODING_PARAM
299 static const AVOption options[] = {
300  {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
301  {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
302  {"private_key", "set path to private key", OFFSET(priv_key), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D|E },
303  {NULL}
304 };
305 
307  .class_name = "libssh",
308  .item_name = av_default_item_name,
309  .option = options,
310  .version = LIBAVUTIL_VERSION_INT,
311 };
312 
314  .name = "sftp",
315  .url_open = libssh_open,
316  .url_read = libssh_read,
317  .url_write = libssh_write,
318  .url_seek = libssh_seek,
319  .url_close = libssh_close,
320  .priv_data_size = sizeof(LIBSSHContext),
321  .priv_data_class = &libssh_context_class,
323 };
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:3886
static av_cold int libssh_open(URLContext *h, const char *url, int flags)
Definition: libssh.c:190
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: libssh.c:296
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
static int libssh_write(URLContext *h, const unsigned char *buf, int size)
Definition: libssh.c:284
AVOption.
Definition: opt.h:255
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int rw_timeout
Definition: libssh.c:37
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
#define AVIO_FLAG_READ
read-only
Definition: avio.h:460
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:461
Macro definitions for various function/variable attributes.
#define MAX_URL_SIZE
Definition: internal.h:28
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_cold
Definition: attributes.h:74
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
#define D
Definition: libssh.c:297
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_cold int libssh_create_ssh_session(LIBSSHContext *libssh, const char *hostname, unsigned int port)
Definition: libssh.c:42
static av_cold int libssh_create_sftp_session(LIBSSHContext *libssh)
Definition: libssh.c:119
static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
Definition: libssh.c:70
sftp_session sftp
Definition: libssh.c:34
#define pass
Definition: fft_template.c:509
ret
Definition: avfilter.c:974
static av_always_inline av_const double trunc(double x)
Definition: libm.h:176
static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
Definition: libssh.c:134
static av_cold int libssh_close(URLContext *h)
Definition: libssh.c:171
static const AVClass libssh_context_class
Definition: libssh.c:306
ssh_session session
Definition: libssh.c:33
int64_t filesize
Definition: libssh.c:36
static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
Definition: libssh.c:233
#define E
Definition: libssh.c:298
char * priv_key
Definition: libssh.c:39
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
GLint GLenum type
Definition: opengl_enc.c:105
Describe the class of an AVClass context structure.
Definition: log.h:67
static const AVOption options[]
Definition: libssh.c:299
void * priv_data
Definition: url.h:42
static int libssh_read(URLContext *h, unsigned char *buf, int size)
Definition: libssh.c:272
const char * name
Definition: url.h:53
static int flags
Definition: cpu.c:47
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
Main libavformat public API header.
sftp_file file
Definition: libssh.c:35
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:339
int trunc
Definition: libssh.c:38
URLProtocol ff_libssh_protocol
Definition: libssh.c:313
static av_cold void libssh_stat_file(LIBSSHContext *libssh)
Definition: libssh.c:158
unbuffered private I/O API