You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
9.3KB

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