libssh 0.4.8
|
00001 /* 00002 * This file is part of the SSH Library 00003 * 00004 * Copyright (c) 2003-2008 by Aris Adamantiadis 00005 * 00006 * The SSH Library is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 2.1 of the License, or (at your 00009 * option) any later version. 00010 * 00011 * The SSH Library is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00013 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00014 * License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with the SSH Library; see the file COPYING. If not, write to 00018 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00019 * MA 02111-1307, USA. 00020 */ 00021 00039 #ifndef SFTP_H 00040 #define SFTP_H 00041 00042 #include <sys/types.h> 00043 00044 #include "libssh.h" 00045 00046 #ifdef __cplusplus 00047 extern "C" { 00048 #endif 00049 00050 #ifdef _WIN32 00051 #ifndef uid_t 00052 typedef uint32_t uid_t; 00053 #endif /* uid_t */ 00054 #ifndef gid_t 00055 typedef uint32_t gid_t; 00056 #endif /* gid_t */ 00057 #ifdef _MSC_VER 00058 #ifndef ssize_t 00059 typedef _W64 SSIZE_T ssize_t; 00060 #endif /* ssize_t */ 00061 #endif /* _MSC_VER */ 00062 #endif /* _WIN32 */ 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 }; 00136 00137 struct sftp_request_queue_struct { 00138 sftp_request_queue next; 00139 sftp_message message; 00140 }; 00141 00142 /* SSH_FXP_MESSAGE described into .7 page 26 */ 00143 struct sftp_status_message_struct { 00144 uint32_t id; 00145 uint32_t status; 00146 ssh_string error; 00147 ssh_string lang; 00148 char *errormsg; 00149 char *langmsg; 00150 }; 00151 00152 struct sftp_attributes_struct { 00153 char *name; 00154 char *longname; /* ls -l output on openssh, not reliable else */ 00155 uint32_t flags; 00156 uint8_t type; 00157 uint64_t size; 00158 uint32_t uid; 00159 uint32_t gid; 00160 char *owner; /* set if openssh and version 4 */ 00161 char *group; /* set if openssh and version 4 */ 00162 uint32_t permissions; 00163 uint64_t atime64; 00164 uint32_t atime; 00165 uint32_t atime_nseconds; 00166 uint64_t createtime; 00167 uint32_t createtime_nseconds; 00168 uint64_t mtime64; 00169 uint32_t mtime; 00170 uint32_t mtime_nseconds; 00171 ssh_string acl; 00172 uint32_t extended_count; 00173 ssh_string extended_type; 00174 ssh_string extended_data; 00175 }; 00176 00177 struct sftp_statvfs_struct { 00178 uint64_t f_bsize; /* file system block size */ 00179 uint64_t f_frsize; /* fundamental fs block size */ 00180 uint64_t f_blocks; /* number of blocks (unit f_frsize) */ 00181 uint64_t f_bfree; /* free blocks in file system */ 00182 uint64_t f_bavail; /* free blocks for non-root */ 00183 uint64_t f_files; /* total file inodes */ 00184 uint64_t f_ffree; /* free file inodes */ 00185 uint64_t f_favail; /* free file inodes for to non-root */ 00186 uint64_t f_fsid; /* file system id */ 00187 uint64_t f_flag; /* bit mask of f_flag values */ 00188 uint64_t f_namemax; /* maximum filename length */ 00189 }; 00190 00191 #define LIBSFTP_VERSION 3 00192 00200 LIBSSH_API sftp_session sftp_new(ssh_session session); 00201 00207 LIBSSH_API void sftp_free(sftp_session sftp); 00208 00216 LIBSSH_API int sftp_init(sftp_session sftp); 00217 00228 LIBSSH_API int sftp_get_error(sftp_session sftp); 00229 00238 LIBSSH_API unsigned int sftp_extensions_get_count(sftp_session sftp); 00239 00249 LIBSSH_API const char *sftp_extensions_get_name(sftp_session sftp, unsigned int indexn); 00250 00262 LIBSSH_API const char *sftp_extensions_get_data(sftp_session sftp, unsigned int indexn); 00263 00281 LIBSSH_API int sftp_extension_supported(sftp_session sftp, const char *name, 00282 const char *data); 00283 00296 LIBSSH_API sftp_dir sftp_opendir(sftp_session session, const char *path); 00297 00311 LIBSSH_API sftp_attributes sftp_readdir(sftp_session session, sftp_dir dir); 00312 00322 LIBSSH_API int sftp_dir_eof(sftp_dir dir); 00323 00334 LIBSSH_API sftp_attributes sftp_stat(sftp_session session, const char *path); 00335 00349 LIBSSH_API sftp_attributes sftp_lstat(sftp_session session, const char *path); 00350 00359 LIBSSH_API sftp_attributes sftp_fstat(sftp_file file); 00360 00366 LIBSSH_API void sftp_attributes_free(sftp_attributes file); 00367 00375 LIBSSH_API int sftp_closedir(sftp_dir dir); 00376 00386 LIBSSH_API int sftp_close(sftp_file file); 00387 00414 LIBSSH_API sftp_file sftp_open(sftp_session session, const char *file, int accesstype, 00415 mode_t mode); 00416 00417 LIBSSH_API void sftp_file_set_nonblocking(sftp_file handle); 00418 00419 LIBSSH_API void sftp_file_set_blocking(sftp_file handle); 00420 00433 LIBSSH_API ssize_t sftp_read(sftp_file file, void *buf, size_t count); 00434 00466 LIBSSH_API int sftp_async_read_begin(sftp_file file, uint32_t len); 00467 00491 LIBSSH_API int sftp_async_read(sftp_file file, void *data, uint32_t len, uint32_t id); 00492 00509 LIBSSH_API ssize_t sftp_write(sftp_file file, const void *buf, size_t count); 00510 00520 LIBSSH_API int sftp_seek(sftp_file file, uint32_t new_offset); 00521 00532 LIBSSH_API int sftp_seek64(sftp_file file, uint64_t new_offset); 00533 00543 LIBSSH_API unsigned long sftp_tell(sftp_file file); 00544 00554 LIBSSH_API uint64_t sftp_tell64(sftp_file file); 00555 00562 LIBSSH_API void sftp_rewind(sftp_file file); 00563 00573 LIBSSH_API int sftp_unlink(sftp_session sftp, const char *file); 00574 00584 LIBSSH_API int sftp_rmdir(sftp_session sftp, const char *directory); 00585 00599 LIBSSH_API int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode); 00600 00614 LIBSSH_API int sftp_rename(sftp_session sftp, const char *original, const char *newname); 00615 00628 LIBSSH_API int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr); 00629 00643 LIBSSH_API int sftp_chown(sftp_session sftp, const char *file, uid_t owner, gid_t group); 00644 00658 LIBSSH_API int sftp_chmod(sftp_session sftp, const char *file, mode_t mode); 00659 00672 LIBSSH_API int sftp_utimes(sftp_session sftp, const char *file, const struct timeval *times); 00673 00685 LIBSSH_API int sftp_symlink(sftp_session sftp, const char *target, const char *dest); 00686 00696 LIBSSH_API char *sftp_readlink(sftp_session sftp, const char *path); 00697 00707 LIBSSH_API sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path); 00708 00716 LIBSSH_API sftp_statvfs_t sftp_fstatvfs(sftp_file file); 00717 00723 LIBSSH_API void sftp_statvfs_free(sftp_statvfs_t statvfs_o); 00724 00734 LIBSSH_API char *sftp_canonicalize_path(sftp_session sftp, const char *path); 00735 00743 LIBSSH_API int sftp_server_version(sftp_session sftp); 00744 00745 #ifdef WITH_SERVER 00746 00755 LIBSSH_API sftp_session sftp_server_new(ssh_session session, ssh_channel chan); 00756 00764 LIBSSH_API int sftp_server_init(sftp_session sftp); 00765 #endif /* WITH_SERVER */ 00766 00767 /* this is not a public interface */ 00768 #define SFTP_HANDLES 256 00769 sftp_packet sftp_packet_read(sftp_session sftp); 00770 int sftp_packet_write(sftp_session sftp,uint8_t type, ssh_buffer payload); 00771 void sftp_packet_free(sftp_packet packet); 00772 int buffer_add_attributes(ssh_buffer buffer, sftp_attributes attr); 00773 sftp_attributes sftp_parse_attr(sftp_session session, ssh_buffer buf,int expectname); 00774 /* sftpserver.c */ 00775 00776 sftp_client_message sftp_get_client_message(sftp_session sftp); 00777 void sftp_client_message_free(sftp_client_message msg); 00778 int sftp_reply_name(sftp_client_message msg, const char *name, 00779 sftp_attributes attr); 00780 int sftp_reply_handle(sftp_client_message msg, ssh_string handle); 00781 ssh_string sftp_handle_alloc(sftp_session sftp, void *info); 00782 int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr); 00783 void *sftp_handle(sftp_session sftp, ssh_string handle); 00784 int sftp_reply_status(sftp_client_message msg, uint32_t status, const char *message); 00785 int sftp_reply_names_add(sftp_client_message msg, const char *file, 00786 const char *longname, sftp_attributes attr); 00787 int sftp_reply_names(sftp_client_message msg); 00788 int sftp_reply_data(sftp_client_message msg, const void *data, int len); 00789 void sftp_handle_remove(sftp_session sftp, void *handle); 00790 00791 /* SFTP commands and constants */ 00792 #define SSH_FXP_INIT 1 00793 #define SSH_FXP_VERSION 2 00794 #define SSH_FXP_OPEN 3 00795 #define SSH_FXP_CLOSE 4 00796 #define SSH_FXP_READ 5 00797 #define SSH_FXP_WRITE 6 00798 #define SSH_FXP_LSTAT 7 00799 #define SSH_FXP_FSTAT 8 00800 #define SSH_FXP_SETSTAT 9 00801 #define SSH_FXP_FSETSTAT 10 00802 #define SSH_FXP_OPENDIR 11 00803 #define SSH_FXP_READDIR 12 00804 #define SSH_FXP_REMOVE 13 00805 #define SSH_FXP_MKDIR 14 00806 #define SSH_FXP_RMDIR 15 00807 #define SSH_FXP_REALPATH 16 00808 #define SSH_FXP_STAT 17 00809 #define SSH_FXP_RENAME 18 00810 #define SSH_FXP_READLINK 19 00811 #define SSH_FXP_SYMLINK 20 00812 00813 #define SSH_FXP_STATUS 101 00814 #define SSH_FXP_HANDLE 102 00815 #define SSH_FXP_DATA 103 00816 #define SSH_FXP_NAME 104 00817 #define SSH_FXP_ATTRS 105 00818 00819 #define SSH_FXP_EXTENDED 200 00820 #define SSH_FXP_EXTENDED_REPLY 201 00821 00822 /* attributes */ 00823 /* sftp draft is completely braindead : version 3 and 4 have different flags for same constants */ 00824 /* and even worst, version 4 has same flag for 2 different constants */ 00825 /* follow up : i won't develop any sftp4 compliant library before having a clarification */ 00826 00827 #define SSH_FILEXFER_ATTR_SIZE 0x00000001 00828 #define SSH_FILEXFER_ATTR_PERMISSIONS 0x00000004 00829 #define SSH_FILEXFER_ATTR_ACCESSTIME 0x00000008 00830 #define SSH_FILEXFER_ATTR_ACMODTIME 0x00000008 00831 #define SSH_FILEXFER_ATTR_CREATETIME 0x00000010 00832 #define SSH_FILEXFER_ATTR_MODIFYTIME 0x00000020 00833 #define SSH_FILEXFER_ATTR_ACL 0x00000040 00834 #define SSH_FILEXFER_ATTR_OWNERGROUP 0x00000080 00835 #define SSH_FILEXFER_ATTR_SUBSECOND_TIMES 0x00000100 00836 #define SSH_FILEXFER_ATTR_EXTENDED 0x80000000 00837 #define SSH_FILEXFER_ATTR_UIDGID 0x00000002 00838 00839 /* types */ 00840 #define SSH_FILEXFER_TYPE_REGULAR 1 00841 #define SSH_FILEXFER_TYPE_DIRECTORY 2 00842 #define SSH_FILEXFER_TYPE_SYMLINK 3 00843 #define SSH_FILEXFER_TYPE_SPECIAL 4 00844 #define SSH_FILEXFER_TYPE_UNKNOWN 5 00845 00846 /* server responses */ 00847 #define SSH_FX_OK 0 00848 #define SSH_FX_EOF 1 00849 #define SSH_FX_NO_SUCH_FILE 2 00850 #define SSH_FX_PERMISSION_DENIED 3 00851 #define SSH_FX_FAILURE 4 00852 #define SSH_FX_BAD_MESSAGE 5 00853 #define SSH_FX_NO_CONNECTION 6 00854 #define SSH_FX_CONNECTION_LOST 7 00855 #define SSH_FX_OP_UNSUPPORTED 8 00856 #define SSH_FX_INVALID_HANDLE 9 00857 #define SSH_FX_NO_SUCH_PATH 10 00858 #define SSH_FX_FILE_ALREADY_EXISTS 11 00859 #define SSH_FX_WRITE_PROTECT 12 00860 #define SSH_FX_NO_MEDIA 13 00861 00862 /* file flags */ 00863 #define SSH_FXF_READ 0x01 00864 #define SSH_FXF_WRITE 0x02 00865 #define SSH_FXF_APPEND 0x04 00866 #define SSH_FXF_CREAT 0x08 00867 #define SSH_FXF_TRUNC 0x10 00868 #define SSH_FXF_EXCL 0x20 00869 #define SSH_FXF_TEXT 0x40 00870 00871 /* rename flags */ 00872 #define SSH_FXF_RENAME_OVERWRITE 0x00000001 00873 #define SSH_FXF_RENAME_ATOMIC 0x00000002 00874 #define SSH_FXF_RENAME_NATIVE 0x00000004 00875 00876 #define SFTP_OPEN SSH_FXP_OPEN 00877 #define SFTP_CLOSE SSH_FXP_CLOSE 00878 #define SFTP_READ SSH_FXP_READ 00879 #define SFTP_WRITE SSH_FXP_WRITE 00880 #define SFTP_LSTAT SSH_FXP_LSTAT 00881 #define SFTP_FSTAT SSH_FXP_FSTAT 00882 #define SFTP_SETSTAT SSH_FXP_SETSTAT 00883 #define SFTP_FSETSTAT SSH_FXP_FSETSTAT 00884 #define SFTP_OPENDIR SSH_FXP_OPENDIR 00885 #define SFTP_READDIR SSH_FXP_READDIR 00886 #define SFTP_REMOVE SSH_FXP_REMOVE 00887 #define SFTP_MKDIR SSH_FXP_MKDIR 00888 #define SFTP_RMDIR SSH_FXP_RMDIR 00889 #define SFTP_REALPATH SSH_FXP_REALPATH 00890 #define SFTP_STAT SSH_FXP_STAT 00891 #define SFTP_RENAME SSH_FXP_RENAME 00892 #define SFTP_READLINK SSH_FXP_READLINK 00893 #define SFTP_SYMLINK SSH_FXP_SYMLINK 00894 00895 /* openssh flags */ 00896 #define SSH_FXE_STATVFS_ST_RDONLY 0x1 /* read-only */ 00897 #define SSH_FXE_STATVFS_ST_NOSUID 0x2 /* no setuid */ 00898 00899 #ifdef __cplusplus 00900 } ; 00901 #endif 00902 00903 #endif /* SFTP_H */ 00904 00906 /* vim: set ts=2 sw=2 et cindent: */