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.

234 lines
6.8KB

  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 (ssh_userauth_autopubkey(s->session, pass) != SSH_AUTH_SUCCESS) {
  86. av_log(s, AV_LOG_DEBUG, "Authentication using public key failed, trying password method.\n");
  87. if (ssh_userauth_password(s->session, NULL, pass) != SSH_AUTH_SUCCESS) {
  88. av_log(h, AV_LOG_ERROR, "Authentication failed.\n");
  89. ret = AVERROR(EACCES);
  90. goto fail;
  91. }
  92. }
  93. if (!(s->sftp = sftp_new(s->session))) {
  94. av_log(h, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(s->session));
  95. ret = AVERROR(ENOMEM);
  96. goto fail;
  97. }
  98. if (sftp_init(s->sftp) != SSH_OK) {
  99. av_log(h, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(s->session));
  100. ret = AVERROR(EIO);
  101. goto fail;
  102. }
  103. if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
  104. access = O_CREAT | O_RDWR;
  105. if (s->trunc)
  106. access |= O_TRUNC;
  107. } else if (flags & AVIO_FLAG_WRITE) {
  108. access = O_CREAT | O_WRONLY;
  109. if (s->trunc)
  110. access |= O_TRUNC;
  111. } else {
  112. access = O_RDONLY;
  113. }
  114. /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
  115. if (!(s->file = sftp_open(s->sftp, path, access, 0666))) {
  116. av_log(h, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(s->session));
  117. ret = AVERROR(EIO);
  118. goto fail;
  119. }
  120. if (!(stat = sftp_fstat(s->file))) {
  121. av_log(h, AV_LOG_WARNING, "Cannot stat remote file %s.\n", path);
  122. s->filesize = -1;
  123. } else {
  124. s->filesize = stat->size;
  125. sftp_attributes_free(stat);
  126. }
  127. return 0;
  128. fail:
  129. libssh_close(h);
  130. return ret;
  131. }
  132. static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
  133. {
  134. LIBSSHContext *s = h->priv_data;
  135. int64_t newpos;
  136. if (s->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
  137. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  138. return AVERROR(EIO);
  139. }
  140. switch(whence) {
  141. case AVSEEK_SIZE:
  142. return s->filesize;
  143. case SEEK_SET:
  144. newpos = pos;
  145. break;
  146. case SEEK_CUR:
  147. newpos = sftp_tell64(s->file);
  148. break;
  149. case SEEK_END:
  150. newpos = s->filesize + pos;
  151. break;
  152. default:
  153. return AVERROR(EINVAL);
  154. }
  155. if (sftp_seek64(s->file, newpos)) {
  156. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  157. return AVERROR(EIO);
  158. }
  159. return newpos;
  160. }
  161. static int libssh_read(URLContext *h, unsigned char *buf, int size)
  162. {
  163. LIBSSHContext *s = h->priv_data;
  164. int bytes_read;
  165. if ((bytes_read = sftp_read(s->file, buf, size)) < 0) {
  166. av_log(h, AV_LOG_ERROR, "Read error.\n");
  167. return AVERROR(EIO);
  168. }
  169. return bytes_read;
  170. }
  171. static int libssh_write(URLContext *h, const unsigned char *buf, int size)
  172. {
  173. LIBSSHContext *s = h->priv_data;
  174. int bytes_written;
  175. if ((bytes_written = sftp_write(s->file, buf, size)) < 0) {
  176. av_log(h, AV_LOG_ERROR, "Write error.\n");
  177. return AVERROR(EIO);
  178. }
  179. return bytes_written;
  180. }
  181. #define OFFSET(x) offsetof(LIBSSHContext, x)
  182. #define D AV_OPT_FLAG_DECODING_PARAM
  183. #define E AV_OPT_FLAG_ENCODING_PARAM
  184. static const AVOption options[] = {
  185. {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  186. {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  187. {NULL}
  188. };
  189. static const AVClass libssh_context_class = {
  190. .class_name = "libssh",
  191. .item_name = av_default_item_name,
  192. .option = options,
  193. .version = LIBAVUTIL_VERSION_INT,
  194. };
  195. URLProtocol ff_libssh_protocol = {
  196. .name = "sftp",
  197. .url_open = libssh_open,
  198. .url_read = libssh_read,
  199. .url_write = libssh_write,
  200. .url_seek = libssh_seek,
  201. .url_close = libssh_close,
  202. .priv_data_size = sizeof(LIBSSHContext),
  203. .priv_data_class = &libssh_context_class,
  204. .flags = URL_PROTOCOL_FLAG_NETWORK,
  205. };