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.

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