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.

230 lines
6.6KB

  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. if (!(s->file = sftp_open(s->sftp, path, access, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH))) {
  112. av_log(h, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(s->session));
  113. ret = AVERROR(EIO);
  114. goto fail;
  115. }
  116. if (!(stat = sftp_fstat(s->file))) {
  117. av_log(h, AV_LOG_WARNING, "Cannot stat remote file %s.\n", path);
  118. s->filesize = -1;
  119. } else {
  120. s->filesize = stat->size;
  121. sftp_attributes_free(stat);
  122. }
  123. return 0;
  124. fail:
  125. libssh_close(h);
  126. return ret;
  127. }
  128. static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
  129. {
  130. LIBSSHContext *s = h->priv_data;
  131. int64_t newpos;
  132. if (s->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
  133. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  134. return AVERROR(EIO);
  135. }
  136. switch(whence) {
  137. case AVSEEK_SIZE:
  138. return s->filesize;
  139. case SEEK_SET:
  140. newpos = pos;
  141. break;
  142. case SEEK_CUR:
  143. newpos = sftp_tell64(s->file);
  144. break;
  145. case SEEK_END:
  146. newpos = s->filesize + pos;
  147. break;
  148. default:
  149. return AVERROR(EINVAL);
  150. }
  151. if (sftp_seek64(s->file, newpos)) {
  152. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  153. return AVERROR(EIO);
  154. }
  155. return newpos;
  156. }
  157. static int libssh_read(URLContext *h, unsigned char *buf, int size)
  158. {
  159. LIBSSHContext *s = h->priv_data;
  160. int bytes_read;
  161. if ((bytes_read = sftp_read(s->file, buf, size)) < 0) {
  162. av_log(h, AV_LOG_ERROR, "Read error.\n");
  163. return AVERROR(EIO);
  164. }
  165. return bytes_read;
  166. }
  167. static int libssh_write(URLContext *h, const unsigned char *buf, int size)
  168. {
  169. LIBSSHContext *s = h->priv_data;
  170. int bytes_written;
  171. if ((bytes_written = sftp_write(s->file, buf, size)) < 0) {
  172. av_log(h, AV_LOG_ERROR, "Write error.\n");
  173. return AVERROR(EIO);
  174. }
  175. return bytes_written;
  176. }
  177. #define OFFSET(x) offsetof(LIBSSHContext, x)
  178. #define D AV_OPT_FLAG_DECODING_PARAM
  179. #define E AV_OPT_FLAG_ENCODING_PARAM
  180. static const AVOption options[] = {
  181. {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  182. {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  183. {NULL}
  184. };
  185. static const AVClass libssh_context_class = {
  186. .class_name = "libssh",
  187. .item_name = av_default_item_name,
  188. .option = options,
  189. .version = LIBAVUTIL_VERSION_INT,
  190. };
  191. URLProtocol ff_libssh_protocol = {
  192. .name = "sftp",
  193. .url_open = libssh_open,
  194. .url_read = libssh_read,
  195. .url_write = libssh_write,
  196. .url_seek = libssh_seek,
  197. .url_close = libssh_close,
  198. .priv_data_size = sizeof(LIBSSHContext),
  199. .priv_data_class = &libssh_context_class,
  200. .flags = URL_PROTOCOL_FLAG_NETWORK,
  201. };