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.

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