]> git.sesse.net Git - ffmpeg/blob - libavformat/libssh.c
flacdsp_lpc_template: add comment to explain the CONFIG_SMALL code
[ffmpeg] / libavformat / libssh.c
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;
37     int rw_timeout;
38     int trunc;
39     char *priv_key;
40 } LIBSSHContext;
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_connect(libssh->session) != SSH_OK) {
59         av_log(libssh, AV_LOG_ERROR, "Connection failed: %s\n", ssh_get_error(libssh->session));
60         return AVERROR(EIO);
61     }
62
63     return 0;
64 }
65
66 static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
67 {
68     int authorized = 0;
69     int auth_methods;
70
71     if (user)
72         ssh_options_set(libssh->session, SSH_OPTIONS_USER, user);
73
74     auth_methods = ssh_userauth_list(libssh->session, NULL);
75
76     if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
77         if (libssh->priv_key) {
78             ssh_string pub_key;
79             ssh_private_key priv_key;
80             int type;
81             if (!ssh_try_publickey_from_file(libssh->session, libssh->priv_key, &pub_key, &type)) {
82                 priv_key = privatekey_from_file(libssh->session, libssh->priv_key, type, password);
83                 if (ssh_userauth_pubkey(libssh->session, NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
84                     av_log(libssh, AV_LOG_DEBUG, "Authentication successful with selected private key.\n");
85                     authorized = 1;
86                 }
87             } else {
88                 av_log(libssh, AV_LOG_DEBUG, "Invalid key is provided.\n");
89                 return AVERROR(EACCES);
90             }
91         } else if (ssh_userauth_autopubkey(libssh->session, password) == SSH_AUTH_SUCCESS) {
92             av_log(libssh, AV_LOG_DEBUG, "Authentication successful with auto selected key.\n");
93             authorized = 1;
94         }
95     }
96
97     if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
98         if (ssh_userauth_password(libssh->session, NULL, password) == SSH_AUTH_SUCCESS) {
99             av_log(libssh, AV_LOG_DEBUG, "Authentication successful with password.\n");
100             authorized = 1;
101         }
102     }
103
104     if (!authorized) {
105         av_log(libssh, AV_LOG_ERROR, "Authentication failed.\n");
106         return AVERROR(EACCES);
107     }
108
109     return 0;
110 }
111
112 static av_cold int libssh_create_sftp_session(LIBSSHContext *libssh)
113 {
114     if (!(libssh->sftp = sftp_new(libssh->session))) {
115         av_log(libssh, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(libssh->session));
116         return AVERROR(ENOMEM);
117     }
118
119     if (sftp_init(libssh->sftp) != SSH_OK) {
120         av_log(libssh, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(libssh->session));
121         return AVERROR(EIO);
122     }
123
124     return 0;
125 }
126
127 static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
128 {
129     int access;
130
131     if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
132         access = O_CREAT | O_RDWR;
133         if (libssh->trunc)
134             access |= O_TRUNC;
135     } else if (flags & AVIO_FLAG_WRITE) {
136         access = O_CREAT | O_WRONLY;
137         if (libssh->trunc)
138             access |= O_TRUNC;
139     } else
140         access = O_RDONLY;
141
142     /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
143     if (!(libssh->file = sftp_open(libssh->sftp, file, access, 0666))) {
144         av_log(libssh, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(libssh->session));
145         return AVERROR(EIO);
146     }
147
148     return 0;
149 }
150
151 static av_cold void libssh_stat_file(LIBSSHContext *libssh)
152 {
153     sftp_attributes stat;
154
155     if (!(stat = sftp_fstat(libssh->file))) {
156         av_log(libssh, AV_LOG_WARNING, "Cannot stat remote file.\n");
157         libssh->filesize = -1;
158     } else {
159         libssh->filesize = stat->size;
160         sftp_attributes_free(stat);
161     }
162 }
163
164 static av_cold int libssh_close(URLContext *h)
165 {
166     LIBSSHContext *libssh = h->priv_data;
167     if (libssh->file)
168         sftp_close(libssh->file);
169     if (libssh->sftp)
170         sftp_free(libssh->sftp);
171     if (libssh->session) {
172         ssh_disconnect(libssh->session);
173         ssh_free(libssh->session);
174     }
175     return 0;
176 }
177
178 static av_cold int libssh_open(URLContext *h, const char *url, int flags)
179 {
180     LIBSSHContext *libssh = h->priv_data;
181     char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
182     int port = 22, ret;
183     const char *user = NULL, *pass = NULL;
184     char *end = NULL;
185
186     av_url_split(proto, sizeof(proto),
187                  credencials, sizeof(credencials),
188                  hostname, sizeof(hostname),
189                  &port,
190                  path, sizeof(path),
191                  url);
192
193     if (port <= 0 || port > 65535)
194         port = 22;
195
196     if ((ret = libssh_create_ssh_session(libssh, hostname, port)) < 0)
197         goto fail;
198
199     user = av_strtok(credencials, ":", &end);
200     pass = av_strtok(end, ":", &end);
201
202     if ((ret = libssh_authentication(libssh, user, pass)) < 0)
203         goto fail;
204
205     if ((ret = libssh_create_sftp_session(libssh)) < 0)
206         goto fail;
207
208     if ((ret = libssh_open_file(libssh, flags, path)) < 0)
209         goto fail;
210
211     libssh_stat_file(libssh);
212
213     return 0;
214
215   fail:
216     libssh_close(h);
217     return ret;
218 }
219
220 static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
221 {
222     LIBSSHContext *libssh = h->priv_data;
223     int64_t newpos;
224
225     if (libssh->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
226         av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
227         return AVERROR(EIO);
228     }
229
230     switch(whence) {
231     case AVSEEK_SIZE:
232         return libssh->filesize;
233     case SEEK_SET:
234         newpos = pos;
235         break;
236     case SEEK_CUR:
237         newpos = sftp_tell64(libssh->file) + pos;
238         break;
239     case SEEK_END:
240         newpos = libssh->filesize + pos;
241         break;
242     default:
243         return AVERROR(EINVAL);
244     }
245
246     if (sftp_seek64(libssh->file, newpos)) {
247         av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
248         return AVERROR(EIO);
249     }
250
251     return newpos;
252 }
253
254 static int libssh_read(URLContext *h, unsigned char *buf, int size)
255 {
256     LIBSSHContext *libssh = h->priv_data;
257     int bytes_read;
258
259     if ((bytes_read = sftp_read(libssh->file, buf, size)) < 0) {
260         av_log(libssh, AV_LOG_ERROR, "Read error.\n");
261         return AVERROR(EIO);
262     }
263     return bytes_read;
264 }
265
266 static int libssh_write(URLContext *h, const unsigned char *buf, int size)
267 {
268     LIBSSHContext *libssh = h->priv_data;
269     int bytes_written;
270
271     if ((bytes_written = sftp_write(libssh->file, buf, size)) < 0) {
272         av_log(libssh, AV_LOG_ERROR, "Write error.\n");
273         return AVERROR(EIO);
274     }
275     return bytes_written;
276 }
277
278 #define OFFSET(x) offsetof(LIBSSHContext, x)
279 #define D AV_OPT_FLAG_DECODING_PARAM
280 #define E AV_OPT_FLAG_ENCODING_PARAM
281 static const AVOption options[] = {
282     {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
283     {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
284     {"private_key", "set path to private key", OFFSET(priv_key), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D|E },
285     {NULL}
286 };
287
288 static const AVClass libssh_context_class = {
289     .class_name     = "libssh",
290     .item_name      = av_default_item_name,
291     .option         = options,
292     .version        = LIBAVUTIL_VERSION_INT,
293 };
294
295 URLProtocol ff_libssh_protocol = {
296     .name                = "sftp",
297     .url_open            = libssh_open,
298     .url_read            = libssh_read,
299     .url_write           = libssh_write,
300     .url_seek            = libssh_seek,
301     .url_close           = libssh_close,
302     .priv_data_size      = sizeof(LIBSSHContext),
303     .priv_data_class     = &libssh_context_class,
304     .flags               = URL_PROTOCOL_FLAG_NETWORK,
305 };