psftp.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * psftp.h: interface between psftp.c / pscp.c, psftpcommon.c, and
  3. * each platform-specific SFTP module.
  4. */
  5. #ifndef PUTTY_PSFTP_H
  6. #define PUTTY_PSFTP_H
  7. /*
  8. * psftp_getcwd returns the local current directory. The returned
  9. * string must be freed by the caller.
  10. */
  11. char *psftp_getcwd(void);
  12. /*
  13. * psftp_lcd changes the local current directory. The return value
  14. * is NULL on success, or else an error message which must be freed
  15. * by the caller.
  16. */
  17. char *psftp_lcd(char *newdir);
  18. /*
  19. * Retrieve file times on a local file. Must return two unsigned
  20. * longs in POSIX time_t format.
  21. */
  22. void get_file_times(char *filename, unsigned long *mtime,
  23. unsigned long *atime);
  24. /*
  25. * One iteration of the PSFTP event loop: wait for network data and
  26. * process it, once.
  27. */
  28. int ssh_sftp_loop_iteration(void);
  29. /*
  30. * Read a command line for PSFTP from standard input. Caller must
  31. * free.
  32. *
  33. * If `backend_required' is true, should also listen for activity
  34. * at the backend (rekeys, clientalives, unexpected closures etc)
  35. * and respond as necessary, and if the backend closes it should
  36. * treat this as a failure condition. If `backend_required' is
  37. * false, a back end is not (intentionally) active at all (e.g.
  38. * psftp before an `open' command).
  39. */
  40. char *ssh_sftp_get_cmdline(const char *prompt, bool backend_required);
  41. /*
  42. * Platform-specific function called when we're about to make a
  43. * network connection.
  44. */
  45. void platform_psftp_pre_conn_setup(LogPolicy *lp);
  46. /*
  47. * The main program in psftp.c. Called from main() in the platform-
  48. * specific code, after doing any platform-specific initialisation.
  49. */
  50. int psftp_main(CmdlineArgList *arglist);
  51. /*
  52. * These functions are used by PSCP to transmit progress updates
  53. * and error information to a GUI window managing it. This will
  54. * probably only ever be supported on Windows, so these functions
  55. * can safely be stubs on all other platforms.
  56. */
  57. void gui_update_stats(const char *name, unsigned long size,
  58. int percentage, unsigned long elapsed,
  59. unsigned long done, unsigned long eta,
  60. unsigned long ratebs);
  61. void gui_send_errcount(int list, int errs);
  62. void gui_send_char(int is_stderr, int c);
  63. void gui_enable(const char *arg);
  64. /*
  65. * It's likely that a given platform's implementation of file
  66. * transfer utilities is going to want to do things with them that
  67. * aren't present in stdio. Hence we supply an alternative
  68. * abstraction for file access functions.
  69. *
  70. * This abstraction tells you the size and access times when you
  71. * open an existing file (platforms may choose the meaning of the
  72. * file times if it's not clear; whatever they choose will be what
  73. * PSCP sends to the server as mtime and atime), and lets you set
  74. * the times when saving a new file.
  75. *
  76. * On the other hand, the abstraction is pretty simple: it supports
  77. * only opening a file and reading it, or creating a file and writing
  78. * it. None of this read-and-write, seeking-back-and-forth stuff.
  79. */
  80. typedef struct RFile RFile;
  81. typedef struct WFile WFile;
  82. /* Output params size, perms, mtime and atime can all be NULL if
  83. * desired. perms will be -1 if the OS does not support POSIX permissions. */
  84. RFile *open_existing_file(const char *name, uint64_t *size,
  85. unsigned long *mtime, unsigned long *atime,
  86. long *perms);
  87. WFile *open_existing_wfile(const char *name, uint64_t *size);
  88. /* Returns <0 on error, 0 on eof, or number of bytes read, as usual */
  89. int read_from_file(RFile *f, void *buffer, int length);
  90. /* Closes and frees the RFile */
  91. void close_rfile(RFile *f);
  92. WFile *open_new_file(const char *name, long perms);
  93. /* Returns <0 on error, 0 on eof, or number of bytes written, as usual */
  94. int write_to_file(WFile *f, void *buffer, int length);
  95. void set_file_times(WFile *f, unsigned long mtime, unsigned long atime);
  96. /* Closes and frees the WFile */
  97. void close_wfile(WFile *f);
  98. /* Seek offset bytes through file */
  99. enum { FROM_START, FROM_CURRENT, FROM_END };
  100. int seek_file(WFile *f, uint64_t offset, int whence);
  101. /* Get file position */
  102. uint64_t get_file_posn(WFile *f);
  103. /*
  104. * Determine the type of a file: nonexistent, file, directory or
  105. * weird. `weird' covers anything else - named pipes, Unix sockets,
  106. * device files, fish, badgers, you name it. Things marked `weird'
  107. * will be skipped over in recursive file transfers, so the only
  108. * real reason for not lumping them in with `nonexistent' is that
  109. * it allows a slightly more sane error message.
  110. */
  111. enum {
  112. FILE_TYPE_NONEXISTENT, FILE_TYPE_FILE, FILE_TYPE_DIRECTORY, FILE_TYPE_WEIRD
  113. };
  114. int file_type(const char *name);
  115. /*
  116. * Read all the file names out of a directory.
  117. */
  118. typedef struct DirHandle DirHandle;
  119. DirHandle *open_directory(const char *name, const char **errmsg);
  120. /* The string returned from this will need freeing if not NULL */
  121. char *read_filename(DirHandle *dir);
  122. void close_directory(DirHandle *dir);
  123. /*
  124. * Test a filespec to see whether it's a local wildcard or not.
  125. * Return values:
  126. *
  127. * - WCTYPE_WILDCARD (this is a wildcard).
  128. * - WCTYPE_FILENAME (this is a single file name).
  129. * - WCTYPE_NONEXISTENT (whichever it was, nothing of that name exists).
  130. *
  131. * Some platforms may choose not to support local wildcards when
  132. * they come from the command line; in this case they simply never
  133. * return WCTYPE_WILDCARD, but still test the file's existence.
  134. * (However, all platforms will probably want to support wildcards
  135. * inside the PSFTP CLI.)
  136. */
  137. enum {
  138. WCTYPE_NONEXISTENT, WCTYPE_FILENAME, WCTYPE_WILDCARD
  139. };
  140. int test_wildcard(const char *name, bool cmdline);
  141. /*
  142. * Actually return matching file names for a local wildcard.
  143. */
  144. typedef struct WildcardMatcher WildcardMatcher;
  145. WildcardMatcher *begin_wildcard_matching(const char *name);
  146. /* The string returned from this will need freeing if not NULL */
  147. char *wildcard_get_filename(WildcardMatcher *dir);
  148. void finish_wildcard_matching(WildcardMatcher *dir);
  149. /*
  150. * Vet a filename returned from the remote host, to ensure it isn't
  151. * in some way malicious. The idea is that this function is applied
  152. * to filenames returned from FXP_READDIR, which means we can panic
  153. * if we see _anything_ resembling a directory separator.
  154. *
  155. * Returns true if the filename is kosher, false if dangerous.
  156. */
  157. bool vet_filename(const char *name);
  158. /*
  159. * Create a directory. Returns true on success, false on error.
  160. */
  161. bool create_directory(const char *name);
  162. /*
  163. * Concatenate a directory name and a file name. The way this is
  164. * done will depend on the OS.
  165. */
  166. char *dir_file_cat(const char *dir, const char *file);
  167. /*
  168. * Return a pointer to the portion of str that comes after the last
  169. * path component separator.
  170. *
  171. * If 'local' is false, path component separators are taken to just be
  172. * '/', on the assumption that we're discussing the path syntax on the
  173. * server. But if 'local' is true, the separators are whatever the
  174. * local OS will treat that way - so that includes '\' and ':' on
  175. * Windows.
  176. *
  177. * This function has the annoying strstr() property of taking a const
  178. * char * and returning a char *. You should treat it as if it was a
  179. * pair of overloaded functions, one mapping mutable->mutable and the
  180. * other const->const :-(
  181. */
  182. char *stripslashes(const char *str, bool local);
  183. /* ----------------------------------------------------------------------
  184. * In psftpcommon.c
  185. */
  186. /*
  187. * qsort comparison routine for fxp_name structures. Sorts by real
  188. * file name.
  189. */
  190. int sftp_name_compare(const void *av, const void *bv);
  191. /*
  192. * Shared code for outputting a directory listing in response to a
  193. * stream of name structures from FXP_READDIR operations. Used by
  194. * psftp's ls command and pscp -ls.
  195. */
  196. struct list_directory_from_sftp_ctx;
  197. struct fxp_name; /* in sftp.h */
  198. struct list_directory_from_sftp_ctx *list_directory_from_sftp_new(void);
  199. void list_directory_from_sftp_feed(struct list_directory_from_sftp_ctx *ctx,
  200. struct fxp_name *name);
  201. void list_directory_from_sftp_finish(struct list_directory_from_sftp_ctx *ctx);
  202. void list_directory_from_sftp_free(struct list_directory_from_sftp_ctx *ctx);
  203. /* Callbacks provided by the tool front end */
  204. void list_directory_from_sftp_warn_unsorted(void);
  205. void list_directory_from_sftp_print(struct fxp_name *name);
  206. #endif /* PUTTY_PSFTP_H */