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.

283 lines
8.5KB

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