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.

231 lines
6.7KB

  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 "avformat.h"
  25. #include "internal.h"
  26. #include "url.h"
  27. typedef struct {
  28. const AVClass *class;
  29. ssh_session session;
  30. sftp_session sftp;
  31. sftp_file file;
  32. int64_t filesize;
  33. int rw_timeout;
  34. int trunc;
  35. } LIBSSHContext;
  36. static int libssh_close(URLContext *h)
  37. {
  38. LIBSSHContext *s = h->priv_data;
  39. if (s->file)
  40. sftp_close(s->file);
  41. if (s->sftp)
  42. sftp_free(s->sftp);
  43. if (s->session) {
  44. ssh_disconnect(s->session);
  45. ssh_free(s->session);
  46. }
  47. return 0;
  48. }
  49. static int libssh_open(URLContext *h, const char *url, int flags)
  50. {
  51. static const int verbosity = SSH_LOG_NOLOG;
  52. LIBSSHContext *s = h->priv_data;
  53. char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
  54. int port = 22, access, ret;
  55. long timeout = s->rw_timeout * 1000;
  56. const char *user = NULL, *pass = NULL;
  57. char *end = NULL;
  58. sftp_attributes stat;
  59. av_url_split(proto, sizeof(proto),
  60. credencials, sizeof(credencials),
  61. hostname, sizeof(hostname),
  62. &port,
  63. path, sizeof(path),
  64. url);
  65. if (port <= 0 || port > 65535)
  66. port = 22;
  67. if (!(s->session = ssh_new())) {
  68. ret = AVERROR(ENOMEM);
  69. goto fail;
  70. }
  71. user = av_strtok(credencials, ":", &end);
  72. pass = av_strtok(end, ":", &end);
  73. ssh_options_set(s->session, SSH_OPTIONS_HOST, hostname);
  74. ssh_options_set(s->session, SSH_OPTIONS_PORT, &port);
  75. ssh_options_set(s->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  76. if (timeout > 0)
  77. ssh_options_set(s->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
  78. if (user)
  79. ssh_options_set(s->session, SSH_OPTIONS_USER, user);
  80. if (ssh_connect(s->session) != SSH_OK) {
  81. av_log(h, AV_LOG_ERROR, "Connection failed. %s\n", ssh_get_error(s->session));
  82. ret = AVERROR(EIO);
  83. goto fail;
  84. }
  85. if (pass && ssh_userauth_password(s->session, NULL, pass) != SSH_AUTH_SUCCESS) {
  86. av_log(h, AV_LOG_ERROR, "Error authenticating with password: %s\n", ssh_get_error(s->session));
  87. ret = AVERROR(EACCES);
  88. goto fail;
  89. }
  90. if (!(s->sftp = sftp_new(s->session))) {
  91. av_log(h, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(s->session));
  92. ret = AVERROR(ENOMEM);
  93. goto fail;
  94. }
  95. if (sftp_init(s->sftp) != SSH_OK) {
  96. av_log(h, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(s->session));
  97. ret = AVERROR(EIO);
  98. goto fail;
  99. }
  100. if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
  101. access = O_CREAT | O_RDWR;
  102. if (s->trunc)
  103. access |= O_TRUNC;
  104. } else if (flags & AVIO_FLAG_WRITE) {
  105. access = O_CREAT | O_WRONLY;
  106. if (s->trunc)
  107. access |= O_TRUNC;
  108. } else {
  109. access = O_RDONLY;
  110. }
  111. /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
  112. if (!(s->file = sftp_open(s->sftp, path, access, 0666))) {
  113. av_log(h, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(s->session));
  114. ret = AVERROR(EIO);
  115. goto fail;
  116. }
  117. if (!(stat = sftp_fstat(s->file))) {
  118. av_log(h, AV_LOG_WARNING, "Cannot stat remote file %s.\n", path);
  119. s->filesize = -1;
  120. } else {
  121. s->filesize = stat->size;
  122. sftp_attributes_free(stat);
  123. }
  124. return 0;
  125. fail:
  126. libssh_close(h);
  127. return ret;
  128. }
  129. static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
  130. {
  131. LIBSSHContext *s = h->priv_data;
  132. int64_t newpos;
  133. if (s->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
  134. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  135. return AVERROR(EIO);
  136. }
  137. switch(whence) {
  138. case AVSEEK_SIZE:
  139. return s->filesize;
  140. case SEEK_SET:
  141. newpos = pos;
  142. break;
  143. case SEEK_CUR:
  144. newpos = sftp_tell64(s->file);
  145. break;
  146. case SEEK_END:
  147. newpos = s->filesize + pos;
  148. break;
  149. default:
  150. return AVERROR(EINVAL);
  151. }
  152. if (sftp_seek64(s->file, newpos)) {
  153. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  154. return AVERROR(EIO);
  155. }
  156. return newpos;
  157. }
  158. static int libssh_read(URLContext *h, unsigned char *buf, int size)
  159. {
  160. LIBSSHContext *s = h->priv_data;
  161. int bytes_read;
  162. if ((bytes_read = sftp_read(s->file, buf, size)) < 0) {
  163. av_log(h, AV_LOG_ERROR, "Read error.\n");
  164. return AVERROR(EIO);
  165. }
  166. return bytes_read;
  167. }
  168. static int libssh_write(URLContext *h, const unsigned char *buf, int size)
  169. {
  170. LIBSSHContext *s = h->priv_data;
  171. int bytes_written;
  172. if ((bytes_written = sftp_write(s->file, buf, size)) < 0) {
  173. av_log(h, AV_LOG_ERROR, "Write error.\n");
  174. return AVERROR(EIO);
  175. }
  176. return bytes_written;
  177. }
  178. #define OFFSET(x) offsetof(LIBSSHContext, x)
  179. #define D AV_OPT_FLAG_DECODING_PARAM
  180. #define E AV_OPT_FLAG_ENCODING_PARAM
  181. static const AVOption options[] = {
  182. {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  183. {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  184. {NULL}
  185. };
  186. static const AVClass libssh_context_class = {
  187. .class_name = "libssh",
  188. .item_name = av_default_item_name,
  189. .option = options,
  190. .version = LIBAVUTIL_VERSION_INT,
  191. };
  192. URLProtocol ff_libssh_protocol = {
  193. .name = "sftp",
  194. .url_open = libssh_open,
  195. .url_read = libssh_read,
  196. .url_write = libssh_write,
  197. .url_seek = libssh_seek,
  198. .url_close = libssh_close,
  199. .priv_data_size = sizeof(LIBSSHContext),
  200. .priv_data_class = &libssh_context_class,
  201. .flags = URL_PROTOCOL_FLAG_NETWORK,
  202. };