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.

508 lines
15KB

  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 "libavformat/avio.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. #include "url.h"
  30. typedef struct {
  31. const AVClass *class;
  32. ssh_session session;
  33. sftp_session sftp;
  34. sftp_file file;
  35. sftp_dir dir;
  36. int64_t filesize;
  37. int rw_timeout;
  38. int trunc;
  39. char *priv_key;
  40. } LIBSSHContext;
  41. static av_cold int libssh_create_ssh_session(LIBSSHContext *libssh, const char* hostname, unsigned int port)
  42. {
  43. static const int verbosity = SSH_LOG_NOLOG;
  44. if (!(libssh->session = ssh_new())) {
  45. av_log(libssh, AV_LOG_ERROR, "SSH session creation failed: %s\n", ssh_get_error(libssh->session));
  46. return AVERROR(ENOMEM);
  47. }
  48. ssh_options_set(libssh->session, SSH_OPTIONS_HOST, hostname);
  49. ssh_options_set(libssh->session, SSH_OPTIONS_PORT, &port);
  50. ssh_options_set(libssh->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  51. if (libssh->rw_timeout > 0) {
  52. long timeout = libssh->rw_timeout * 1000;
  53. ssh_options_set(libssh->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
  54. }
  55. if (ssh_options_parse_config(libssh->session, NULL) < 0) {
  56. av_log(libssh, AV_LOG_WARNING, "Could not parse the config file.\n");
  57. }
  58. if (ssh_connect(libssh->session) != SSH_OK) {
  59. av_log(libssh, AV_LOG_ERROR, "Connection failed: %s\n", ssh_get_error(libssh->session));
  60. return AVERROR(EIO);
  61. }
  62. return 0;
  63. }
  64. static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
  65. {
  66. int authorized = 0;
  67. int auth_methods;
  68. if (user)
  69. ssh_options_set(libssh->session, SSH_OPTIONS_USER, user);
  70. if (ssh_userauth_none(libssh->session, NULL) == SSH_AUTH_SUCCESS)
  71. return 0;
  72. auth_methods = ssh_userauth_list(libssh->session, NULL);
  73. if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
  74. if (libssh->priv_key) {
  75. ssh_string pub_key;
  76. ssh_private_key priv_key;
  77. int type;
  78. if (!ssh_try_publickey_from_file(libssh->session, libssh->priv_key, &pub_key, &type)) {
  79. priv_key = privatekey_from_file(libssh->session, libssh->priv_key, type, password);
  80. if (ssh_userauth_pubkey(libssh->session, NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
  81. av_log(libssh, AV_LOG_DEBUG, "Authentication successful with selected private key.\n");
  82. authorized = 1;
  83. }
  84. } else {
  85. av_log(libssh, AV_LOG_DEBUG, "Invalid key is provided.\n");
  86. return AVERROR(EACCES);
  87. }
  88. } else if (ssh_userauth_autopubkey(libssh->session, password) == SSH_AUTH_SUCCESS) {
  89. av_log(libssh, AV_LOG_DEBUG, "Authentication successful with auto selected key.\n");
  90. authorized = 1;
  91. }
  92. }
  93. if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
  94. if (ssh_userauth_password(libssh->session, NULL, password) == SSH_AUTH_SUCCESS) {
  95. av_log(libssh, AV_LOG_DEBUG, "Authentication successful with password.\n");
  96. authorized = 1;
  97. }
  98. }
  99. if (!authorized) {
  100. av_log(libssh, AV_LOG_ERROR, "Authentication failed.\n");
  101. return AVERROR(EACCES);
  102. }
  103. return 0;
  104. }
  105. static av_cold int libssh_create_sftp_session(LIBSSHContext *libssh)
  106. {
  107. if (!(libssh->sftp = sftp_new(libssh->session))) {
  108. av_log(libssh, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(libssh->session));
  109. return AVERROR(ENOMEM);
  110. }
  111. if (sftp_init(libssh->sftp) != SSH_OK) {
  112. av_log(libssh, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(libssh->session));
  113. return AVERROR(EIO);
  114. }
  115. return 0;
  116. }
  117. static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
  118. {
  119. int access;
  120. if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
  121. access = O_CREAT | O_RDWR;
  122. if (libssh->trunc)
  123. access |= O_TRUNC;
  124. } else if (flags & AVIO_FLAG_WRITE) {
  125. access = O_CREAT | O_WRONLY;
  126. if (libssh->trunc)
  127. access |= O_TRUNC;
  128. } else
  129. access = O_RDONLY;
  130. /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
  131. if (!(libssh->file = sftp_open(libssh->sftp, file, access, 0666))) {
  132. av_log(libssh, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(libssh->session));
  133. return AVERROR(EIO);
  134. }
  135. return 0;
  136. }
  137. static av_cold void libssh_stat_file(LIBSSHContext *libssh)
  138. {
  139. sftp_attributes stat;
  140. if (!(stat = sftp_fstat(libssh->file))) {
  141. av_log(libssh, AV_LOG_WARNING, "Cannot stat remote file.\n");
  142. libssh->filesize = -1;
  143. } else {
  144. libssh->filesize = stat->size;
  145. sftp_attributes_free(stat);
  146. }
  147. }
  148. static av_cold int libssh_close(URLContext *h)
  149. {
  150. LIBSSHContext *libssh = h->priv_data;
  151. if (libssh->file) {
  152. sftp_close(libssh->file);
  153. libssh->file = NULL;
  154. }
  155. if (libssh->sftp) {
  156. sftp_free(libssh->sftp);
  157. libssh->sftp = NULL;
  158. }
  159. if (libssh->session) {
  160. ssh_disconnect(libssh->session);
  161. ssh_free(libssh->session);
  162. libssh->session = NULL;
  163. }
  164. return 0;
  165. }
  166. static av_cold int libssh_connect(URLContext *h, const char *url, char *path, size_t path_size)
  167. {
  168. LIBSSHContext *libssh = h->priv_data;
  169. char proto[10], hostname[1024], credencials[1024];
  170. int port = 22, ret;
  171. const char *user = NULL, *pass = NULL;
  172. char *end = NULL;
  173. av_url_split(proto, sizeof(proto),
  174. credencials, sizeof(credencials),
  175. hostname, sizeof(hostname),
  176. &port,
  177. path, path_size,
  178. url);
  179. if (!(*path))
  180. av_strlcpy(path, "/", path_size);
  181. // a port of 0 will use a port from ~/.ssh/config or the default value 22
  182. if (port < 0 || port > 65535)
  183. port = 0;
  184. if ((ret = libssh_create_ssh_session(libssh, hostname, port)) < 0)
  185. return ret;
  186. user = av_strtok(credencials, ":", &end);
  187. pass = av_strtok(end, ":", &end);
  188. if ((ret = libssh_authentication(libssh, user, pass)) < 0)
  189. return ret;
  190. if ((ret = libssh_create_sftp_session(libssh)) < 0)
  191. return ret;
  192. return 0;
  193. }
  194. static av_cold int libssh_open(URLContext *h, const char *url, int flags)
  195. {
  196. int ret;
  197. LIBSSHContext *libssh = h->priv_data;
  198. char path[MAX_URL_SIZE];
  199. if ((ret = libssh_connect(h, url, path, sizeof(path))) < 0)
  200. goto fail;
  201. if ((ret = libssh_open_file(libssh, flags, path)) < 0)
  202. goto fail;
  203. libssh_stat_file(libssh);
  204. return 0;
  205. fail:
  206. libssh_close(h);
  207. return ret;
  208. }
  209. static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
  210. {
  211. LIBSSHContext *libssh = h->priv_data;
  212. int64_t newpos;
  213. if (libssh->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
  214. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  215. return AVERROR(EIO);
  216. }
  217. switch(whence) {
  218. case AVSEEK_SIZE:
  219. return libssh->filesize;
  220. case SEEK_SET:
  221. newpos = pos;
  222. break;
  223. case SEEK_CUR:
  224. newpos = sftp_tell64(libssh->file) + pos;
  225. break;
  226. case SEEK_END:
  227. newpos = libssh->filesize + pos;
  228. break;
  229. default:
  230. return AVERROR(EINVAL);
  231. }
  232. if (newpos < 0) {
  233. av_log(h, AV_LOG_ERROR, "Seeking to nagative position.\n");
  234. return AVERROR(EINVAL);
  235. }
  236. if (sftp_seek64(libssh->file, newpos)) {
  237. av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
  238. return AVERROR(EIO);
  239. }
  240. return newpos;
  241. }
  242. static int libssh_read(URLContext *h, unsigned char *buf, int size)
  243. {
  244. LIBSSHContext *libssh = h->priv_data;
  245. int bytes_read;
  246. if ((bytes_read = sftp_read(libssh->file, buf, size)) < 0) {
  247. av_log(libssh, AV_LOG_ERROR, "Read error.\n");
  248. return AVERROR(EIO);
  249. }
  250. return bytes_read;
  251. }
  252. static int libssh_write(URLContext *h, const unsigned char *buf, int size)
  253. {
  254. LIBSSHContext *libssh = h->priv_data;
  255. int bytes_written;
  256. if ((bytes_written = sftp_write(libssh->file, buf, size)) < 0) {
  257. av_log(libssh, AV_LOG_ERROR, "Write error.\n");
  258. return AVERROR(EIO);
  259. }
  260. return bytes_written;
  261. }
  262. static int libssh_open_dir(URLContext *h)
  263. {
  264. LIBSSHContext *libssh = h->priv_data;
  265. int ret;
  266. char path[MAX_URL_SIZE];
  267. if ((ret = libssh_connect(h, h->filename, path, sizeof(path))) < 0)
  268. goto fail;
  269. if (!(libssh->dir = sftp_opendir(libssh->sftp, path))) {
  270. av_log(libssh, AV_LOG_ERROR, "Error opening sftp dir: %s\n", ssh_get_error(libssh->session));
  271. ret = AVERROR(EIO);
  272. goto fail;
  273. }
  274. return 0;
  275. fail:
  276. libssh_close(h);
  277. return ret;
  278. }
  279. static int libssh_read_dir(URLContext *h, AVIODirEntry **next)
  280. {
  281. LIBSSHContext *libssh = h->priv_data;
  282. sftp_attributes attr = NULL;
  283. AVIODirEntry *entry;
  284. *next = entry = ff_alloc_dir_entry();
  285. if (!entry)
  286. return AVERROR(ENOMEM);
  287. do {
  288. if (attr)
  289. sftp_attributes_free(attr);
  290. attr = sftp_readdir(libssh->sftp, libssh->dir);
  291. if (!attr) {
  292. av_freep(next);
  293. if (sftp_dir_eof(libssh->dir))
  294. return 0;
  295. return AVERROR(EIO);
  296. }
  297. } while (!strcmp(attr->name, ".") || !strcmp(attr->name, ".."));
  298. entry->name = av_strdup(attr->name);
  299. entry->group_id = attr->gid;
  300. entry->user_id = attr->uid;
  301. entry->size = attr->size;
  302. entry->access_timestamp = INT64_C(1000000) * attr->atime;
  303. entry->modification_timestamp = INT64_C(1000000) * attr->mtime;
  304. entry->filemode = attr->permissions & 0777;
  305. switch(attr->type) {
  306. case SSH_FILEXFER_TYPE_REGULAR:
  307. entry->type = AVIO_ENTRY_FILE;
  308. break;
  309. case SSH_FILEXFER_TYPE_DIRECTORY:
  310. entry->type = AVIO_ENTRY_DIRECTORY;
  311. break;
  312. case SSH_FILEXFER_TYPE_SYMLINK:
  313. entry->type = AVIO_ENTRY_SYMBOLIC_LINK;
  314. break;
  315. case SSH_FILEXFER_TYPE_SPECIAL:
  316. /* Special type includes: sockets, char devices, block devices and pipes.
  317. It is probably better to return unknown type, to not confuse anybody. */
  318. case SSH_FILEXFER_TYPE_UNKNOWN:
  319. default:
  320. entry->type = AVIO_ENTRY_UNKNOWN;
  321. }
  322. sftp_attributes_free(attr);
  323. return 0;
  324. }
  325. static int libssh_close_dir(URLContext *h)
  326. {
  327. LIBSSHContext *libssh = h->priv_data;
  328. if (libssh->dir)
  329. sftp_closedir(libssh->dir);
  330. libssh->dir = NULL;
  331. libssh_close(h);
  332. return 0;
  333. }
  334. static int libssh_delete(URLContext *h)
  335. {
  336. int ret;
  337. LIBSSHContext *libssh = h->priv_data;
  338. sftp_attributes attr = NULL;
  339. char path[MAX_URL_SIZE];
  340. if ((ret = libssh_connect(h, h->filename, path, sizeof(path))) < 0)
  341. goto cleanup;
  342. if (!(attr = sftp_stat(libssh->sftp, path))) {
  343. ret = AVERROR(sftp_get_error(libssh->sftp));
  344. goto cleanup;
  345. }
  346. if (attr->type == SSH_FILEXFER_TYPE_DIRECTORY) {
  347. if (sftp_rmdir(libssh->sftp, path) < 0) {
  348. ret = AVERROR(sftp_get_error(libssh->sftp));
  349. goto cleanup;
  350. }
  351. } else {
  352. if (sftp_unlink(libssh->sftp, path) < 0) {
  353. ret = AVERROR(sftp_get_error(libssh->sftp));
  354. goto cleanup;
  355. }
  356. }
  357. ret = 0;
  358. cleanup:
  359. if (attr)
  360. sftp_attributes_free(attr);
  361. libssh_close(h);
  362. return ret;
  363. }
  364. static int libssh_move(URLContext *h_src, URLContext *h_dst)
  365. {
  366. int ret;
  367. LIBSSHContext *libssh = h_src->priv_data;
  368. char path_src[MAX_URL_SIZE], path_dst[MAX_URL_SIZE];
  369. char hostname_src[1024], hostname_dst[1024];
  370. char credentials_src[1024], credentials_dst[1024];
  371. int port_src = 22, port_dst = 22;
  372. av_url_split(NULL, 0,
  373. credentials_src, sizeof(credentials_src),
  374. hostname_src, sizeof(hostname_src),
  375. &port_src,
  376. path_src, sizeof(path_src),
  377. h_src->filename);
  378. av_url_split(NULL, 0,
  379. credentials_dst, sizeof(credentials_dst),
  380. hostname_dst, sizeof(hostname_dst),
  381. &port_dst,
  382. path_dst, sizeof(path_dst),
  383. h_dst->filename);
  384. if (strcmp(credentials_src, credentials_dst) ||
  385. strcmp(hostname_src, hostname_dst) ||
  386. port_src != port_dst) {
  387. return AVERROR(EINVAL);
  388. }
  389. if ((ret = libssh_connect(h_src, h_src->filename, path_src, sizeof(path_src))) < 0)
  390. goto cleanup;
  391. if (sftp_rename(libssh->sftp, path_src, path_dst) < 0) {
  392. ret = AVERROR(sftp_get_error(libssh->sftp));
  393. goto cleanup;
  394. }
  395. ret = 0;
  396. cleanup:
  397. libssh_close(h_src);
  398. return ret;
  399. }
  400. #define OFFSET(x) offsetof(LIBSSHContext, x)
  401. #define D AV_OPT_FLAG_DECODING_PARAM
  402. #define E AV_OPT_FLAG_ENCODING_PARAM
  403. static const AVOption options[] = {
  404. {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  405. {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  406. {"private_key", "set path to private key", OFFSET(priv_key), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D|E },
  407. {NULL}
  408. };
  409. static const AVClass libssh_context_class = {
  410. .class_name = "libssh",
  411. .item_name = av_default_item_name,
  412. .option = options,
  413. .version = LIBAVUTIL_VERSION_INT,
  414. };
  415. URLProtocol ff_libssh_protocol = {
  416. .name = "sftp",
  417. .url_open = libssh_open,
  418. .url_read = libssh_read,
  419. .url_write = libssh_write,
  420. .url_seek = libssh_seek,
  421. .url_close = libssh_close,
  422. .url_delete = libssh_delete,
  423. .url_move = libssh_move,
  424. .url_open_dir = libssh_open_dir,
  425. .url_read_dir = libssh_read_dir,
  426. .url_close_dir = libssh_close_dir,
  427. .priv_data_size = sizeof(LIBSSHContext),
  428. .priv_data_class = &libssh_context_class,
  429. .flags = URL_PROTOCOL_FLAG_NETWORK,
  430. };