xref: /freebsd/crypto/openssh/sftp-client.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /* $OpenBSD: sftp-client.h,v 1.39 2023/09/08 05:56:13 djm Exp $ */
2 
3 /*
4  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Client side of SSH2 filexfer protocol */
20 
21 #ifndef _SFTP_CLIENT_H
22 #define _SFTP_CLIENT_H
23 
24 #ifdef USE_SYSTEM_GLOB
25 # include <glob.h>
26 #else
27 # include "openbsd-compat/glob.h"
28 #endif
29 
30 typedef struct SFTP_DIRENT SFTP_DIRENT;
31 
32 struct SFTP_DIRENT {
33 	char *filename;
34 	char *longname;
35 	Attrib a;
36 };
37 
38 /*
39  * Used for statvfs responses on the wire from the server, because the
40  * server's native format may be larger than the client's.
41  */
42 struct sftp_statvfs {
43 	u_int64_t f_bsize;
44 	u_int64_t f_frsize;
45 	u_int64_t f_blocks;
46 	u_int64_t f_bfree;
47 	u_int64_t f_bavail;
48 	u_int64_t f_files;
49 	u_int64_t f_ffree;
50 	u_int64_t f_favail;
51 	u_int64_t f_fsid;
52 	u_int64_t f_flag;
53 	u_int64_t f_namemax;
54 };
55 
56 /* Used for limits response on the wire from the server */
57 struct sftp_limits {
58 	u_int64_t packet_length;
59 	u_int64_t read_length;
60 	u_int64_t write_length;
61 	u_int64_t open_handles;
62 };
63 
64 /* print flag values */
65 #define SFTP_QUIET		0	/* be quiet during transfers */
66 #define SFTP_PRINT		1	/* list files and show progress bar */
67 #define SFTP_PROGRESS_ONLY	2	/* progress bar only */
68 
69 /*
70  * Initialise a SSH filexfer connection. Returns NULL on error or
71  * a pointer to a initialized sftp_conn struct on success.
72  */
73 struct sftp_conn *sftp_init(int, int, u_int, u_int, u_int64_t);
74 
75 u_int sftp_proto_version(struct sftp_conn *);
76 
77 /* Query server limits */
78 int sftp_get_limits(struct sftp_conn *, struct sftp_limits *);
79 
80 /* Close file referred to by 'handle' */
81 int sftp_close(struct sftp_conn *, const u_char *, u_int);
82 
83 /* Read contents of 'path' to NULL-terminated array 'dir' */
84 int sftp_readdir(struct sftp_conn *, const char *, SFTP_DIRENT ***);
85 
86 /* Frees a NULL-terminated array of SFTP_DIRENTs (eg. from sftp_readdir) */
87 void sftp_free_dirents(SFTP_DIRENT **);
88 
89 /* Delete file 'path' */
90 int sftp_rm(struct sftp_conn *, const char *);
91 
92 /* Create directory 'path' */
93 int sftp_mkdir(struct sftp_conn *, const char *, Attrib *, int);
94 
95 /* Remove directory 'path' */
96 int sftp_rmdir(struct sftp_conn *, const char *);
97 
98 /* Get file attributes of 'path' (follows symlinks) */
99 int sftp_stat(struct sftp_conn *, const char *, int, Attrib *);
100 
101 /* Get file attributes of 'path' (does not follow symlinks) */
102 int sftp_lstat(struct sftp_conn *, const char *, int, Attrib *);
103 
104 /* Set file attributes of 'path' */
105 int sftp_setstat(struct sftp_conn *, const char *, Attrib *);
106 
107 /* Set file attributes of open file 'handle' */
108 int sftp_fsetstat(struct sftp_conn *, const u_char *, u_int, Attrib *);
109 
110 /* Set file attributes of 'path', not following symlinks */
111 int sftp_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a);
112 
113 /* Canonicalise 'path' - caller must free result */
114 char *sftp_realpath(struct sftp_conn *, const char *);
115 
116 /* Canonicalisation with tilde expansion (requires server extension) */
117 char *sftp_expand_path(struct sftp_conn *, const char *);
118 
119 /* Returns non-zero if server can tilde-expand paths */
120 int sftp_can_expand_path(struct sftp_conn *);
121 
122 /* Get statistics for filesystem hosting file at "path" */
123 int sftp_statvfs(struct sftp_conn *, const char *, struct sftp_statvfs *, int);
124 
125 /* Rename 'oldpath' to 'newpath' */
126 int sftp_rename(struct sftp_conn *, const char *, const char *, int);
127 
128 /* Copy 'oldpath' to 'newpath' */
129 int sftp_copy(struct sftp_conn *, const char *, const char *);
130 
131 /* Link 'oldpath' to 'newpath' */
132 int sftp_hardlink(struct sftp_conn *, const char *, const char *);
133 
134 /* Rename 'oldpath' to 'newpath' */
135 int sftp_symlink(struct sftp_conn *, const char *, const char *);
136 
137 /* Call fsync() on open file 'handle' */
138 int sftp_fsync(struct sftp_conn *conn, u_char *, u_int);
139 
140 /*
141  * Download 'remote_path' to 'local_path'. Preserve permissions and times
142  * if 'pflag' is set
143  */
144 int sftp_download(struct sftp_conn *, const char *, const char *, Attrib *,
145     int, int, int, int);
146 
147 /*
148  * Recursively download 'remote_directory' to 'local_directory'. Preserve
149  * times if 'pflag' is set
150  */
151 int sftp_download_dir(struct sftp_conn *, const char *, const char *, Attrib *,
152     int, int, int, int, int, int);
153 
154 /*
155  * Upload 'local_path' to 'remote_path'. Preserve permissions and times
156  * if 'pflag' is set
157  */
158 int sftp_upload(struct sftp_conn *, const char *, const char *,
159     int, int, int, int);
160 
161 /*
162  * Recursively upload 'local_directory' to 'remote_directory'. Preserve
163  * times if 'pflag' is set
164  */
165 int sftp_upload_dir(struct sftp_conn *, const char *, const char *,
166     int, int, int, int, int, int);
167 
168 /*
169  * Download a 'from_path' from the 'from' connection and upload it to
170  * to 'to' connection at 'to_path'.
171  */
172 int sftp_crossload(struct sftp_conn *from, struct sftp_conn *to,
173     const char *from_path, const char *to_path,
174     Attrib *a, int preserve_flag);
175 
176 /*
177  * Recursively download a directory from 'from_path' from the 'from'
178  * connection and upload it to 'to' connection at 'to_path'.
179  */
180 int sftp_crossload_dir(struct sftp_conn *from, struct sftp_conn *to,
181     const char *from_path, const char *to_path,
182     Attrib *dirattrib, int preserve_flag, int print_flag,
183     int follow_link_flag);
184 
185 /*
186  * User/group ID to name translation.
187  */
188 int sftp_can_get_users_groups_by_id(struct sftp_conn *conn);
189 int sftp_get_users_groups_by_id(struct sftp_conn *conn,
190     const u_int *uids, u_int nuids,
191     const u_int *gids, u_int ngids,
192     char ***usernamesp, char ***groupnamesp);
193 
194 /* Concatenate paths, taking care of slashes. Caller must free result. */
195 char *sftp_path_append(const char *, const char *);
196 
197 /* Make absolute path if relative path and CWD is given. Does not modify
198  * original if the path is already absolute. */
199 char *sftp_make_absolute(char *, const char *);
200 
201 /* Check if remote path is directory */
202 int sftp_remote_is_dir(struct sftp_conn *conn, const char *path);
203 
204 /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
205 int sftp_globpath_is_dir(const char *pathname);
206 
207 #endif
208