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>
23 #include <libssh/sftp.h>
30
41
43 {
44 static const int verbosity = SSH_LOG_NOLOG;
45
46 if (!(libssh->
session = ssh_new())) {
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);
55 ssh_options_set(libssh->
session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
56 }
57
58 if (ssh_connect(libssh->
session) != SSH_OK) {
61 }
62
63 return 0;
64 }
65
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 if (ssh_userauth_none(libssh->
session, NULL) == SSH_AUTH_SUCCESS)
75 return 0;
76
77 auth_methods = ssh_userauth_list(libssh->
session, NULL);
78
79 if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
81 ssh_string pub_key;
82 ssh_private_key priv_key;
84 if (!ssh_try_publickey_from_file(libssh->
session, libssh->
priv_key, &pub_key, &type)) {
85 priv_key = privatekey_from_file(libssh->
session, libssh->
priv_key, type, password);
86 if (ssh_userauth_pubkey(libssh->
session, NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
87 av_log(libssh,
AV_LOG_DEBUG,
"Authentication successful with selected private key.\n");
88 authorized = 1;
89 }
90 } else {
93 }
94 }
else if (ssh_userauth_autopubkey(libssh->
session, password) == SSH_AUTH_SUCCESS) {
96 authorized = 1;
97 }
98 }
99
100 if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
101 if (ssh_userauth_password(libssh->
session, NULL, password) == SSH_AUTH_SUCCESS) {
103 authorized = 1;
104 }
105 }
106
107 if (!authorized) {
110 }
111
112 return 0;
113 }
114
116 {
120 }
121
122 if (sftp_init(libssh->
sftp) != SSH_OK) {
125 }
126
127 return 0;
128 }
129
131 {
132 int access;
133
135 access = O_CREAT | O_RDWR;
137 access |= O_TRUNC;
138 } else if (flags & AVIO_FLAG_WRITE) {
139 access = O_CREAT | O_WRONLY;
141 access |= O_TRUNC;
142 } else
143 access = O_RDONLY;
144
145 /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
146 if (!(libssh->
file = sftp_open(libssh->
sftp, file, access, 0666))) {
149 }
150
151 return 0;
152 }
153
155 {
156 sftp_attributes stat;
157
158 if (!(stat = sftp_fstat(libssh->
file))) {
161 } else {
163 sftp_attributes_free(stat);
164 }
165 }
166
168 {
171 sftp_close(libssh->
file);
173 }
175 sftp_free(libssh->
sftp);
177 }
179 ssh_disconnect(libssh->
session);
182 }
183 return 0;
184 }
185
187 {
189 char proto[10], path[
MAX_URL_SIZE], hostname[1024], credencials[1024];
191 const char *user = NULL, *
pass = NULL;
193
195 credencials, sizeof(credencials),
196 hostname, sizeof(hostname),
197 &port,
198 path, sizeof(path),
199 url);
200
201 if (port <= 0 || port > 65535)
202 port = 22;
203
205 goto fail;
206
207 user =
av_strtok(credencials,
":", &end);
209
211 goto fail;
212
214 goto fail;
215
217 goto fail;
218
220
221 return 0;
222
223 fail:
226 }
227
229 {
231 int64_t newpos;
232
236 }
237
238 switch(whence) {
241 case SEEK_SET:
242 newpos = pos;
243 break;
244 case SEEK_CUR:
245 newpos = sftp_tell64(libssh->
file) + pos;
246 break;
247 case SEEK_END:
249 break;
250 default:
252 }
253
254 if (newpos < 0) {
257 }
258
259 if (sftp_seek64(libssh->
file, newpos)) {
262 }
263
264 return newpos;
265 }
266
268 {
270 int bytes_read;
271
272 if ((bytes_read = sftp_read(libssh->
file, buf, size)) < 0) {
275 }
276 return bytes_read;
277 }
278
280 {
282 int bytes_written;
283
284 if ((bytes_written = sftp_write(libssh->
file, buf, size)) < 0) {
287 }
288 return bytes_written;
289 }
290
291 #define OFFSET(x) offsetof(LIBSSHContext, x)
292 #define D AV_OPT_FLAG_DECODING_PARAM
293 #define E AV_OPT_FLAG_ENCODING_PARAM
295 {
"timeout",
"set timeout of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
298 {NULL}
299 };
300
306 };
307
316 .priv_data_class = &libssh_context_class,
318 };