libssh  0.7.2
include/libssh/sftp.h
00001 /*
00002  * This file is part of the SSH Library
00003  *
00004  * Copyright (c) 2003-2008 by Aris Adamantiadis
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00037 #ifndef SFTP_H
00038 #define SFTP_H
00039 
00040 #include <sys/types.h>
00041 
00042 #include "libssh.h"
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 #ifdef _WIN32
00049 #ifndef uid_t
00050   typedef uint32_t uid_t;
00051 #endif /* uid_t */
00052 #ifndef gid_t
00053   typedef uint32_t gid_t;
00054 #endif /* gid_t */
00055 #ifdef _MSC_VER
00056 #ifndef ssize_t
00057   typedef _W64 SSIZE_T ssize_t;
00058 #endif /* ssize_t */
00059 #endif /* _MSC_VER */
00060 #endif /* _WIN32 */
00061 
00062 #define LIBSFTP_VERSION 3
00063 
00064 typedef struct sftp_attributes_struct* sftp_attributes;
00065 typedef struct sftp_client_message_struct* sftp_client_message;
00066 typedef struct sftp_dir_struct* sftp_dir;
00067 typedef struct sftp_ext_struct *sftp_ext;
00068 typedef struct sftp_file_struct* sftp_file;
00069 typedef struct sftp_message_struct* sftp_message;
00070 typedef struct sftp_packet_struct* sftp_packet;
00071 typedef struct sftp_request_queue_struct* sftp_request_queue;
00072 typedef struct sftp_session_struct* sftp_session;
00073 typedef struct sftp_status_message_struct* sftp_status_message;
00074 typedef struct sftp_statvfs_struct* sftp_statvfs_t;
00075 
00076 struct sftp_session_struct {
00077     ssh_session session;
00078     ssh_channel channel;
00079     int server_version;
00080     int client_version;
00081     int version;
00082     sftp_request_queue queue;
00083     uint32_t id_counter;
00084     int errnum;
00085     void **handles;
00086     sftp_ext ext;
00087 };
00088 
00089 struct sftp_packet_struct {
00090     sftp_session sftp;
00091     uint8_t type;
00092     ssh_buffer payload;
00093 };
00094 
00095 /* file handler */
00096 struct sftp_file_struct {
00097     sftp_session sftp;
00098     char *name;
00099     uint64_t offset;
00100     ssh_string handle;
00101     int eof;
00102     int nonblocking;
00103 };
00104 
00105 struct sftp_dir_struct {
00106     sftp_session sftp;
00107     char *name;
00108     ssh_string handle; /* handle to directory */
00109     ssh_buffer buffer; /* contains raw attributes from server which haven't been parsed */
00110     uint32_t count; /* counts the number of following attributes structures into buffer */
00111     int eof; /* end of directory listing */
00112 };
00113 
00114 struct sftp_message_struct {
00115     sftp_session sftp;
00116     uint8_t packet_type;
00117     ssh_buffer payload;
00118     uint32_t id;
00119 };
00120 
00121 /* this is a bunch of all data that could be into a message */
00122 struct sftp_client_message_struct {
00123     sftp_session sftp;
00124     uint8_t type;
00125     uint32_t id;
00126     char *filename; /* can be "path" */
00127     uint32_t flags;
00128     sftp_attributes attr;
00129     ssh_string handle;
00130     uint64_t offset;
00131     uint32_t len;
00132     int attr_num;
00133     ssh_buffer attrbuf; /* used by sftp_reply_attrs */
00134     ssh_string data; /* can be newpath of rename() */
00135     ssh_buffer complete_message; /* complete message in case of retransmission*/
00136     char *str_data; /* cstring version of data */
00137 };
00138 
00139 struct sftp_request_queue_struct {
00140     sftp_request_queue next;
00141     sftp_message message;
00142 };
00143 
00144 /* SSH_FXP_MESSAGE described into .7 page 26 */
00145 struct sftp_status_message_struct {
00146     uint32_t id;
00147   uint32_t status;
00148     ssh_string error_unused; /* not used anymore */
00149     ssh_string lang_unused;  /* not used anymore */
00150     char *errormsg;
00151     char *langmsg;
00152 };
00153 
00154 struct sftp_attributes_struct {
00155     char *name;
00156     char *longname; /* ls -l output on openssh, not reliable else */
00157     uint32_t flags;
00158     uint8_t type;
00159     uint64_t size;
00160     uint32_t uid;
00161     uint32_t gid;
00162     char *owner; /* set if openssh and version 4 */
00163     char *group; /* set if openssh and version 4 */
00164     uint32_t permissions;
00165     uint64_t atime64;
00166     uint32_t atime;
00167     uint32_t atime_nseconds;
00168     uint64_t createtime;
00169     uint32_t createtime_nseconds;
00170     uint64_t mtime64;
00171     uint32_t mtime;
00172     uint32_t mtime_nseconds;
00173     ssh_string acl;
00174     uint32_t extended_count;
00175     ssh_string extended_type;
00176     ssh_string extended_data;
00177 };
00178 
00182 struct sftp_statvfs_struct {
00183   uint64_t f_bsize;   
00184   uint64_t f_frsize;  
00185   uint64_t f_blocks;  
00186   uint64_t f_bfree;   
00187   uint64_t f_bavail;  
00188   uint64_t f_files;   
00189   uint64_t f_ffree;   
00190   uint64_t f_favail;  
00191   uint64_t f_fsid;    
00192   uint64_t f_flag;    
00193   uint64_t f_namemax; 
00194 };
00195 
00205 LIBSSH_API sftp_session sftp_new(ssh_session session);
00206 
00217 LIBSSH_API sftp_session sftp_new_channel(ssh_session session, ssh_channel channel);
00218 
00219 
00225 LIBSSH_API void sftp_free(sftp_session sftp);
00226 
00236 LIBSSH_API int sftp_init(sftp_session sftp);
00237 
00250 LIBSSH_API int sftp_get_error(sftp_session sftp);
00251 
00260 LIBSSH_API unsigned int sftp_extensions_get_count(sftp_session sftp);
00261 
00271 LIBSSH_API const char *sftp_extensions_get_name(sftp_session sftp, unsigned int indexn);
00272 
00284 LIBSSH_API const char *sftp_extensions_get_data(sftp_session sftp, unsigned int indexn);
00285 
00303 LIBSSH_API int sftp_extension_supported(sftp_session sftp, const char *name,
00304     const char *data);
00305 
00318 LIBSSH_API sftp_dir sftp_opendir(sftp_session session, const char *path);
00319 
00333 LIBSSH_API sftp_attributes sftp_readdir(sftp_session session, sftp_dir dir);
00334 
00344 LIBSSH_API int sftp_dir_eof(sftp_dir dir);
00345 
00358 LIBSSH_API sftp_attributes sftp_stat(sftp_session session, const char *path);
00359 
00375 LIBSSH_API sftp_attributes sftp_lstat(sftp_session session, const char *path);
00376 
00387 LIBSSH_API sftp_attributes sftp_fstat(sftp_file file);
00388 
00394 LIBSSH_API void sftp_attributes_free(sftp_attributes file);
00395 
00403 LIBSSH_API int sftp_closedir(sftp_dir dir);
00404 
00414 LIBSSH_API int sftp_close(sftp_file file);
00415 
00444 LIBSSH_API sftp_file sftp_open(sftp_session session, const char *file, int accesstype,
00445     mode_t mode);
00446 
00452 LIBSSH_API void sftp_file_set_nonblocking(sftp_file handle);
00453 
00459 LIBSSH_API void sftp_file_set_blocking(sftp_file handle);
00460 
00475 LIBSSH_API ssize_t sftp_read(sftp_file file, void *buf, size_t count);
00476 
00508 LIBSSH_API int sftp_async_read_begin(sftp_file file, uint32_t len);
00509 
00533 LIBSSH_API int sftp_async_read(sftp_file file, void *data, uint32_t len, uint32_t id);
00534 
00551 LIBSSH_API ssize_t sftp_write(sftp_file file, const void *buf, size_t count);
00552 
00562 LIBSSH_API int sftp_seek(sftp_file file, uint32_t new_offset);
00563 
00574 LIBSSH_API int sftp_seek64(sftp_file file, uint64_t new_offset);
00575 
00585 LIBSSH_API unsigned long sftp_tell(sftp_file file);
00586 
00596 LIBSSH_API uint64_t sftp_tell64(sftp_file file);
00597 
00604 LIBSSH_API void sftp_rewind(sftp_file file);
00605 
00617 LIBSSH_API int sftp_unlink(sftp_session sftp, const char *file);
00618 
00630 LIBSSH_API int sftp_rmdir(sftp_session sftp, const char *directory);
00631 
00647 LIBSSH_API int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode);
00648 
00664 LIBSSH_API int sftp_rename(sftp_session sftp, const char *original, const  char *newname);
00665 
00680 LIBSSH_API int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr);
00681 
00697 LIBSSH_API int sftp_chown(sftp_session sftp, const char *file, uid_t owner, gid_t group);
00698 
00714 LIBSSH_API int sftp_chmod(sftp_session sftp, const char *file, mode_t mode);
00715 
00730 LIBSSH_API int sftp_utimes(sftp_session sftp, const char *file, const struct timeval *times);
00731 
00745 LIBSSH_API int sftp_symlink(sftp_session sftp, const char *target, const char *dest);
00746 
00758 LIBSSH_API char *sftp_readlink(sftp_session sftp, const char *path);
00759 
00771 LIBSSH_API sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path);
00772 
00782 LIBSSH_API sftp_statvfs_t sftp_fstatvfs(sftp_file file);
00783 
00789 LIBSSH_API void sftp_statvfs_free(sftp_statvfs_t statvfs_o);
00790 
00800 LIBSSH_API char *sftp_canonicalize_path(sftp_session sftp, const char *path);
00801 
00809 LIBSSH_API int sftp_server_version(sftp_session sftp);
00810 
00811 #ifdef WITH_SERVER
00812 
00821 LIBSSH_API sftp_session sftp_server_new(ssh_session session, ssh_channel chan);
00822 
00830 LIBSSH_API int sftp_server_init(sftp_session sftp);
00831 #endif  /* WITH_SERVER */
00832 
00833 /* this is not a public interface */
00834 #define SFTP_HANDLES 256
00835 sftp_packet sftp_packet_read(sftp_session sftp);
00836 int sftp_packet_write(sftp_session sftp,uint8_t type, ssh_buffer payload);
00837 void sftp_packet_free(sftp_packet packet);
00838 int buffer_add_attributes(ssh_buffer buffer, sftp_attributes attr);
00839 sftp_attributes sftp_parse_attr(sftp_session session, ssh_buffer buf,int expectname);
00840 /* sftpserver.c */
00841 
00842 LIBSSH_API sftp_client_message sftp_get_client_message(sftp_session sftp);
00843 LIBSSH_API void sftp_client_message_free(sftp_client_message msg);
00844 LIBSSH_API uint8_t sftp_client_message_get_type(sftp_client_message msg);
00845 LIBSSH_API const char *sftp_client_message_get_filename(sftp_client_message msg);
00846 LIBSSH_API void sftp_client_message_set_filename(sftp_client_message msg, const char *newname);
00847 LIBSSH_API const char *sftp_client_message_get_data(sftp_client_message msg);
00848 LIBSSH_API uint32_t sftp_client_message_get_flags(sftp_client_message msg);
00849 LIBSSH_API int sftp_send_client_message(sftp_session sftp, sftp_client_message msg);
00850 int sftp_reply_name(sftp_client_message msg, const char *name,
00851     sftp_attributes attr);
00852 int sftp_reply_handle(sftp_client_message msg, ssh_string handle);
00853 ssh_string sftp_handle_alloc(sftp_session sftp, void *info);
00854 int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr);
00855 void *sftp_handle(sftp_session sftp, ssh_string handle);
00856 int sftp_reply_status(sftp_client_message msg, uint32_t status, const char *message);
00857 int sftp_reply_names_add(sftp_client_message msg, const char *file,
00858     const char *longname, sftp_attributes attr);
00859 int sftp_reply_names(sftp_client_message msg);
00860 int sftp_reply_data(sftp_client_message msg, const void *data, int len);
00861 void sftp_handle_remove(sftp_session sftp, void *handle);
00862 
00863 /* SFTP commands and constants */
00864 #define SSH_FXP_INIT 1
00865 #define SSH_FXP_VERSION 2
00866 #define SSH_FXP_OPEN 3
00867 #define SSH_FXP_CLOSE 4
00868 #define SSH_FXP_READ 5
00869 #define SSH_FXP_WRITE 6
00870 #define SSH_FXP_LSTAT 7
00871 #define SSH_FXP_FSTAT 8
00872 #define SSH_FXP_SETSTAT 9
00873 #define SSH_FXP_FSETSTAT 10
00874 #define SSH_FXP_OPENDIR 11
00875 #define SSH_FXP_READDIR 12
00876 #define SSH_FXP_REMOVE 13
00877 #define SSH_FXP_MKDIR 14
00878 #define SSH_FXP_RMDIR 15
00879 #define SSH_FXP_REALPATH 16
00880 #define SSH_FXP_STAT 17
00881 #define SSH_FXP_RENAME 18
00882 #define SSH_FXP_READLINK 19
00883 #define SSH_FXP_SYMLINK 20
00884 
00885 #define SSH_FXP_STATUS 101
00886 #define SSH_FXP_HANDLE 102
00887 #define SSH_FXP_DATA 103
00888 #define SSH_FXP_NAME 104
00889 #define SSH_FXP_ATTRS 105
00890 
00891 #define SSH_FXP_EXTENDED 200
00892 #define SSH_FXP_EXTENDED_REPLY 201
00893 
00894 /* attributes */
00895 /* sftp draft is completely braindead : version 3 and 4 have different flags for same constants */
00896 /* and even worst, version 4 has same flag for 2 different constants */
00897 /* follow up : i won't develop any sftp4 compliant library before having a clarification */
00898 
00899 #define SSH_FILEXFER_ATTR_SIZE 0x00000001
00900 #define SSH_FILEXFER_ATTR_PERMISSIONS 0x00000004
00901 #define SSH_FILEXFER_ATTR_ACCESSTIME 0x00000008
00902 #define SSH_FILEXFER_ATTR_ACMODTIME  0x00000008
00903 #define SSH_FILEXFER_ATTR_CREATETIME 0x00000010
00904 #define SSH_FILEXFER_ATTR_MODIFYTIME 0x00000020
00905 #define SSH_FILEXFER_ATTR_ACL 0x00000040
00906 #define SSH_FILEXFER_ATTR_OWNERGROUP 0x00000080
00907 #define SSH_FILEXFER_ATTR_SUBSECOND_TIMES 0x00000100
00908 #define SSH_FILEXFER_ATTR_EXTENDED 0x80000000
00909 #define SSH_FILEXFER_ATTR_UIDGID 0x00000002
00910 
00911 /* types */
00912 #define SSH_FILEXFER_TYPE_REGULAR 1
00913 #define SSH_FILEXFER_TYPE_DIRECTORY 2
00914 #define SSH_FILEXFER_TYPE_SYMLINK 3
00915 #define SSH_FILEXFER_TYPE_SPECIAL 4
00916 #define SSH_FILEXFER_TYPE_UNKNOWN 5
00917 
00926 #define SSH_FX_OK 0
00927 
00928 #define SSH_FX_EOF 1
00929 
00930 #define SSH_FX_NO_SUCH_FILE 2
00931 
00932 #define SSH_FX_PERMISSION_DENIED 3
00933 
00934 #define SSH_FX_FAILURE 4
00935 
00936 #define SSH_FX_BAD_MESSAGE 5
00937 
00938 #define SSH_FX_NO_CONNECTION 6
00939 
00940 #define SSH_FX_CONNECTION_LOST 7
00941 
00942 #define SSH_FX_OP_UNSUPPORTED 8
00943 
00944 #define SSH_FX_INVALID_HANDLE 9
00945 
00946 #define SSH_FX_NO_SUCH_PATH 10
00947 
00948 #define SSH_FX_FILE_ALREADY_EXISTS 11
00949 
00950 #define SSH_FX_WRITE_PROTECT 12
00951 
00952 #define SSH_FX_NO_MEDIA 13
00953 
00956 /* file flags */
00957 #define SSH_FXF_READ 0x01
00958 #define SSH_FXF_WRITE 0x02
00959 #define SSH_FXF_APPEND 0x04
00960 #define SSH_FXF_CREAT 0x08
00961 #define SSH_FXF_TRUNC 0x10
00962 #define SSH_FXF_EXCL 0x20
00963 #define SSH_FXF_TEXT 0x40
00964 
00965 /* rename flags */
00966 #define SSH_FXF_RENAME_OVERWRITE  0x00000001
00967 #define SSH_FXF_RENAME_ATOMIC     0x00000002
00968 #define SSH_FXF_RENAME_NATIVE     0x00000004
00969 
00970 #define SFTP_OPEN SSH_FXP_OPEN
00971 #define SFTP_CLOSE SSH_FXP_CLOSE
00972 #define SFTP_READ SSH_FXP_READ
00973 #define SFTP_WRITE SSH_FXP_WRITE
00974 #define SFTP_LSTAT SSH_FXP_LSTAT
00975 #define SFTP_FSTAT SSH_FXP_FSTAT
00976 #define SFTP_SETSTAT SSH_FXP_SETSTAT
00977 #define SFTP_FSETSTAT SSH_FXP_FSETSTAT
00978 #define SFTP_OPENDIR SSH_FXP_OPENDIR
00979 #define SFTP_READDIR SSH_FXP_READDIR
00980 #define SFTP_REMOVE SSH_FXP_REMOVE
00981 #define SFTP_MKDIR SSH_FXP_MKDIR
00982 #define SFTP_RMDIR SSH_FXP_RMDIR
00983 #define SFTP_REALPATH SSH_FXP_REALPATH
00984 #define SFTP_STAT SSH_FXP_STAT
00985 #define SFTP_RENAME SSH_FXP_RENAME
00986 #define SFTP_READLINK SSH_FXP_READLINK
00987 #define SFTP_SYMLINK SSH_FXP_SYMLINK
00988 
00989 /* openssh flags */
00990 #define SSH_FXE_STATVFS_ST_RDONLY 0x1 /* read-only */
00991 #define SSH_FXE_STATVFS_ST_NOSUID 0x2 /* no setuid */
00992 
00993 #ifdef __cplusplus
00994 } ;
00995 #endif
00996 
00997 #endif /* SFTP_H */
00998 
01000 /* vim: set ts=2 sw=2 et cindent: */