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