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