]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - psftp.h
General mechanism for ensuring a dodgy SFTP server can't return
[PuTTY.git] / psftp.h
1 /*
2  * psftp.h: interface between psftp.c / scp.c and each
3  * platform-specific SFTP module.
4  */
5
6 #ifndef PUTTY_PSFTP_H
7 #define PUTTY_PSFTP_H
8
9 /*
10  * psftp_getcwd returns the local current directory. The returned
11  * string must be freed by the caller.
12  */
13 char *psftp_getcwd(void);
14
15 /*
16  * psftp_lcd changes the local current directory. The return value
17  * is NULL on success, or else an error message which must be freed
18  * by the caller.
19  */
20 char *psftp_lcd(char *newdir);
21
22 /*
23  * Retrieve file times on a local file. Must return two unsigned
24  * longs in POSIX time_t format.
25  */
26 void get_file_times(char *filename, unsigned long *mtime,
27                     unsigned long *atime);
28
29 /*
30  * One iteration of the PSFTP event loop: wait for network data and
31  * process it, once.
32  */
33 int ssh_sftp_loop_iteration(void);
34
35 /*
36  * Read a command line for PSFTP from standard input. Caller must
37  * free.
38  * 
39  * If `backend_required' is TRUE, should also listen for activity
40  * at the backend (rekeys, clientalives, unexpected closures etc)
41  * and respond as necessary, and if the backend closes it should
42  * treat this as a failure condition. If `backend_required' is
43  * FALSE, a back end is not (intentionally) active at all (e.g.
44  * psftp before an `open' command).
45  */
46 char *ssh_sftp_get_cmdline(char *prompt, int backend_required);
47
48 /*
49  * The main program in psftp.c. Called from main() in the platform-
50  * specific code, after doing any platform-specific initialisation.
51  */
52 int psftp_main(int argc, char *argv[]);
53
54 /*
55  * These functions are used by PSCP to transmit progress updates
56  * and error information to a GUI window managing it. This will
57  * probably only ever be supported on Windows, so these functions
58  * can safely be stubs on all other platforms.
59  */
60 void gui_update_stats(char *name, unsigned long size,
61                       int percentage, unsigned long elapsed,
62                       unsigned long done, unsigned long eta,
63                       unsigned long ratebs);
64 void gui_send_errcount(int list, int errs);
65 void gui_send_char(int is_stderr, int c);
66 void gui_enable(char *arg);
67
68 /*
69  * It's likely that a given platform's implementation of file
70  * transfer utilities is going to want to do things with them that
71  * aren't present in stdio. Hence we supply an alternative
72  * abstraction for file access functions.
73  * 
74  * This abstraction tells you the size and access times when you
75  * open an existing file (platforms may choose the meaning of the
76  * file times if it's not clear; whatever they choose will be what
77  * PSCP sends to the server as mtime and atime), and lets you set
78  * the times when saving a new file.
79  * 
80  * On the other hand, the abstraction is pretty simple: it supports
81  * only opening a file and reading it, or creating a file and
82  * writing it. (FIXME: to use this in PSFTP it will also need to
83  * support seeking to a starting point for restarted transfers.)
84  * None of this read-and-write, seeking-back-and-forth stuff.
85  */
86 typedef struct RFile RFile;
87 typedef struct WFile WFile;
88 /* Output params size, mtime and atime can all be NULL if desired */
89 RFile *open_existing_file(char *name, unsigned long *size,
90                           unsigned long *mtime, unsigned long *atime);
91 /* Returns <0 on error, 0 on eof, or number of bytes read, as usual */
92 int read_from_file(RFile *f, void *buffer, int length);
93 /* Closes and frees the RFile */
94 void close_rfile(RFile *f);
95 WFile *open_new_file(char *name);
96 /* Returns <0 on error, 0 on eof, or number of bytes written, as usual */
97 int write_to_file(WFile *f, void *buffer, int length);
98 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime);
99 /* Closes and frees the WFile */
100 void close_wfile(WFile *f);
101
102 /*
103  * Determine the type of a file: nonexistent, file, directory or
104  * weird. `weird' covers anything else - named pipes, Unix sockets,
105  * device files, fish, badgers, you name it. Things marked `weird'
106  * will be skipped over in recursive file transfers, so the only
107  * real reason for not lumping them in with `nonexistent' is that
108  * it allows a slightly more sane error message.
109  */
110 enum {
111     FILE_TYPE_NONEXISTENT, FILE_TYPE_FILE, FILE_TYPE_DIRECTORY, FILE_TYPE_WEIRD
112 };
113 int file_type(char *name);
114
115 /*
116  * Read all the file names out of a directory.
117  */
118 typedef struct DirHandle DirHandle;
119 DirHandle *open_directory(char *name);
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 /*
125  * Test a filespec to see whether it's a local wildcard or not.
126  * Return values:
127  * 
128  *  - WCTYPE_WILDCARD (this is a wildcard).
129  *  - WCTYPE_FILENAME (this is a single file name).
130  *  - WCTYPE_NONEXISTENT (whichever it was, nothing of that name exists).
131  * 
132  * Some platforms may choose not to support local wildcards when
133  * they come from the command line; in this case they simply never
134  * return WCTYPE_WILDCARD, but still test the file's existence.
135  * (However, all platforms will probably want to support wildcards
136  * inside the PSFTP CLI.)
137  */
138 enum {
139     WCTYPE_NONEXISTENT, WCTYPE_FILENAME, WCTYPE_WILDCARD
140 };
141 int test_wildcard(char *name, int cmdline);
142
143 /*
144  * Actually return matching file names for a local wildcard.
145  */
146 typedef struct WildcardMatcher WildcardMatcher;
147 WildcardMatcher *begin_wildcard_matching(char *name);
148 /* The string returned from this will need freeing if not NULL */
149 char *wildcard_get_filename(WildcardMatcher *dir);
150 void finish_wildcard_matching(WildcardMatcher *dir);
151
152 /*
153  * Vet a filename returned from the remote host, to ensure it isn't
154  * in some way malicious. The idea is that this function is applied
155  * to filenames returned from FXP_READDIR, which means we can panic
156  * if we see _anything_ resembling a directory separator.
157  * 
158  * Returns TRUE if the filename is kosher, FALSE if dangerous.
159  */
160 int vet_filename(char *name);
161
162 /*
163  * Create a directory. Returns 0 on error, !=0 on success.
164  */
165 int create_directory(char *name);
166
167 /*
168  * Concatenate a directory name and a file name. The way this is
169  * done will depend on the OS.
170  */
171 char *dir_file_cat(char *dir, char *file);
172
173 #endif /* PUTTY_PSFTP_H */