include/libssh/sftp.h

00001 /* sftp headers */
00002 /*
00003 Copyright 2003-2005 Aris Adamantiadis
00004 
00005 This file is part of the SSH Library
00006 
00007 The SSH Library is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation; either version 2.1 of the License, or (at your
00010 option) any later version.
00011 
00012 The SSH Library is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00014 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00015 License for more details.
00016 
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with the SSH Library; see the file COPYING.  If not, write to
00019 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00020 MA 02111-1307, USA. */
00021 
00022 #ifndef SFTP_H
00023 #define SFTP_H
00024 #include <libssh/libssh.h>
00025 #ifdef __cplusplus
00026 extern "C" {
00027 #endif
00028 
00029 typedef struct sftp_session_struct {
00030     SSH_SESSION *session;
00031     CHANNEL *channel;
00032     int server_version;
00033     int client_version;
00034     int version;
00035     struct request_queue *queue;
00036     u32 id_counter;
00037     int errnum;
00038     void **handles;
00039 } SFTP_SESSION ;
00040 
00041 typedef struct {
00042     SFTP_SESSION *sftp;
00043     u8 type;
00044     BUFFER *payload;
00045 } SFTP_PACKET;
00046 
00047 /* file handler */
00048 typedef struct sftp_file{
00049     SFTP_SESSION *sftp;
00050     char *name;
00051     u64 offset;
00052     STRING *handle;
00053     int eof;
00054     int nonblocking;
00055 }  SFTP_FILE ;
00056 
00057 typedef struct sftp_dir {
00058     SFTP_SESSION *sftp;
00059     char *name;
00060     STRING *handle; /* handle to directory */
00061     BUFFER *buffer; /* contains raw attributes from server which haven't been parsed */
00062     u32 count; /* counts the number of following attributes structures into buffer */
00063     int eof; /* end of directory listing */
00064 } SFTP_DIR;
00065 
00066 typedef struct {
00067     SFTP_SESSION *sftp;
00068     u8 packet_type;
00069     BUFFER *payload;
00070     u32 id;
00071 } SFTP_MESSAGE;
00072 
00073 /* this is a bunch of all data that could be into a message */
00074 typedef struct sftp_client_message{
00075     SFTP_SESSION *sftp;
00076     u8 type;
00077     u32 id;
00078     char *filename; /* can be "path" */
00079     u32 flags;
00080     struct sftp_attributes *attr;
00081     STRING *handle;
00082     u64 offset;
00083     u32 len;
00084     int attr_num;
00085     BUFFER *attrbuf; /* used by sftp_reply_attrs */
00086     STRING *data; /* can be newpath of rename() */
00087 } SFTP_CLIENT_MESSAGE;
00088 
00089 typedef struct request_queue{
00090     struct request_queue *next;
00091     SFTP_MESSAGE *message;
00092 } REQUEST_QUEUE;
00093 
00094 /* SSH_FXP_MESSAGE described into .7 page 26 */
00095 typedef struct {
00096     u32 id;
00097     u32 status;
00098     STRING *error;
00099     STRING *lang;
00100     char *errormsg;
00101     char *langmsg;
00102 } STATUS_MESSAGE;
00103 
00104 /* don't worry much of these aren't really used */
00105 typedef struct sftp_attributes{
00106     char *name;
00107     char *longname; /* some weird stuff */
00108     u32 flags;
00109     u8 type;
00110     u64 size;
00111     u32 uid;
00112     u32 gid;
00113     char *owner;
00114     char *group;
00115     u32 permissions;
00116     u64 atime64;
00117     u32 atime;
00118     u32 atime_nseconds;
00119     u64 createtime;
00120     u32 createtime_nseconds;
00121     u64 mtime64;
00122     u32 mtime;
00123     u32 mtime_nseconds;
00124     STRING *acl;
00125     u32 extended_count;
00126     STRING *extended_type;
00127     STRING *extended_data;
00128 } SFTP_ATTRIBUTES;
00129 
00130 #define LIBSFTP_VERSION 3
00131 
00132 SFTP_SESSION *sftp_new(SSH_SESSION *session);
00133 void sftp_free(SFTP_SESSION *sftp);
00134 int sftp_init(SFTP_SESSION *sftp);
00135 int sftp_get_error(SFTP_SESSION *sftp);
00136 SFTP_DIR *sftp_opendir(SFTP_SESSION *session, const char *path);
00137 /* reads one file and attribute from opened directory. fails at end */
00138 SFTP_ATTRIBUTES *sftp_readdir(SFTP_SESSION *session, SFTP_DIR *dir);
00139 /* returns 1 if the directory was EOF */
00140 int sftp_dir_eof(SFTP_DIR *dir);
00141 SFTP_ATTRIBUTES *sftp_stat(SFTP_SESSION *session, const char *path);
00142 SFTP_ATTRIBUTES *sftp_lstat(SFTP_SESSION *session, const char *path);
00143 /* sftp_lstat stats a file but doesn't follow symlinks */
00144 SFTP_ATTRIBUTES *sftp_fstat(SFTP_FILE *file);
00145 void sftp_attributes_free(SFTP_ATTRIBUTES *file);
00146 int sftp_dir_close(SFTP_DIR *dir);
00147 int sftp_file_close(SFTP_FILE *file);
00148 /* access are the sames than the ones from ansi fopen() */
00149 SFTP_FILE *sftp_open(SFTP_SESSION *session, const char *file, int access, SFTP_ATTRIBUTES *attr);
00150 int sftp_read(SFTP_FILE *file, void *dest, int len);
00151 u32 sftp_async_read_begin(SFTP_FILE *file, int len);
00152 int sftp_async_read(SFTP_FILE *file, void *data, int len, u32 id);
00153 int sftp_write(SFTP_FILE *file, const void *source, int len);
00154 void sftp_seek(SFTP_FILE *file, int new_offset);
00155 void sftp_seek64(SFTP_FILE *file, u64 new_offset);
00156 unsigned long sftp_tell(SFTP_FILE *file);
00157 void sftp_rewind(SFTP_FILE *file);
00158 int sftp_rm(SFTP_SESSION *sftp, char *file);
00159 int sftp_rmdir(SFTP_SESSION *sftp, const char *directory);
00160 int sftp_mkdir(SFTP_SESSION *sftp, const char *directory, SFTP_ATTRIBUTES *attr);
00161 int sftp_rename(SFTP_SESSION *sftp, const char *original, const  char *newname);
00162 int sftp_setstat(SFTP_SESSION *sftp, const char *file, SFTP_ATTRIBUTES *attr);
00163 char *sftp_canonicalize_path(SFTP_SESSION *sftp, const char *path);
00164 
00165 #ifndef NO_SERVER
00166 SFTP_SESSION *sftp_server_new(SSH_SESSION *session, CHANNEL *chan);
00167 int sftp_server_init(SFTP_SESSION *sftp);
00168 #endif
00169 
00170 /* this is not a public interface */
00171 #define SFTP_HANDLES 256
00172 SFTP_PACKET *sftp_packet_read(SFTP_SESSION *sftp);
00173 int sftp_packet_write(SFTP_SESSION *sftp,u8 type, BUFFER *payload);
00174 void sftp_packet_free(SFTP_PACKET *packet);
00175 void buffer_add_attributes(BUFFER *buffer, SFTP_ATTRIBUTES *attr);
00176 SFTP_ATTRIBUTES *sftp_parse_attr(SFTP_SESSION *session, BUFFER *buf,int expectname);
00177 /* sftpserver.c */
00178 
00179 SFTP_CLIENT_MESSAGE *sftp_get_client_message(SFTP_SESSION *sftp);
00180 void sftp_client_message_free(SFTP_CLIENT_MESSAGE *msg);
00181 int sftp_reply_name(SFTP_CLIENT_MESSAGE *msg, char *name, SFTP_ATTRIBUTES *attr);
00182 int sftp_reply_handle(SFTP_CLIENT_MESSAGE *msg, STRING *handle);
00183 STRING *sftp_handle_alloc(SFTP_SESSION *sftp, void *info);
00184 int sftp_reply_attr(SFTP_CLIENT_MESSAGE *msg, SFTP_ATTRIBUTES *attr);
00185 void *sftp_handle(SFTP_SESSION *sftp, STRING *handle);
00186 int sftp_reply_status(SFTP_CLIENT_MESSAGE *msg, u32 status, char *message);
00187 int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, char *file, char *longname,
00188                          SFTP_ATTRIBUTES *attr);
00189 int sftp_reply_names(SFTP_CLIENT_MESSAGE *msg);
00190 int sftp_reply_data(SFTP_CLIENT_MESSAGE *msg, void *data, int len);
00191 void sftp_handle_remove(SFTP_SESSION *sftp, void *handle);
00192 
00193 /* SFTP commands and constants */
00194 #define SSH_FXP_INIT 1
00195 #define SSH_FXP_VERSION 2
00196 #define SSH_FXP_OPEN 3
00197 #define SSH_FXP_CLOSE 4
00198 #define SSH_FXP_READ 5
00199 #define SSH_FXP_WRITE 6
00200 #define SSH_FXP_LSTAT 7
00201 #define SSH_FXP_FSTAT 8
00202 #define SSH_FXP_SETSTAT 9
00203 #define SSH_FXP_FSETSTAT 10
00204 #define SSH_FXP_OPENDIR 11
00205 #define SSH_FXP_READDIR 12
00206 #define SSH_FXP_REMOVE 13
00207 #define SSH_FXP_MKDIR 14
00208 #define SSH_FXP_RMDIR 15
00209 #define SSH_FXP_REALPATH 16
00210 #define SSH_FXP_STAT 17
00211 #define SSH_FXP_RENAME 18
00212 #define SSH_FXP_READLINK 19
00213 #define SSH_FXP_SYMLINK 20
00214 
00215 #define SSH_FXP_STATUS 101
00216 #define SSH_FXP_HANDLE 102
00217 #define SSH_FXP_DATA 103
00218 #define SSH_FXP_NAME 104
00219 #define SSH_FXP_ATTRS 105
00220 
00221 #define SSH_FXP_EXTENDED 200
00222 #define SSH_FXP_EXTENDED_REPLY 201
00223 
00224 /* attributes */
00225 /* sftp draft is completely braindead : version 3 and 4 have different flags for same constants */
00226 /* and even worst, version 4 has same flag for 2 different constants */
00227 /* follow up : i won't develop any sftp4 compliant library before having a clarification */
00228 
00229 #define SSH_FILEXFER_ATTR_SIZE 0x00000001
00230 #define SSH_FILEXFER_ATTR_PERMISSIONS 0x00000004
00231 #define SSH_FILEXFER_ATTR_ACCESSTIME 0x00000008
00232 #define SSH_FILEXFER_ATTR_ACMODTIME  0x00000008
00233 #define SSH_FILEXFER_ATTR_CREATETIME 0x00000010
00234 #define SSH_FILEXFER_ATTR_MODIFYTIME 0x00000020
00235 #define SSH_FILEXFER_ATTR_ACL 0x00000040
00236 #define SSH_FILEXFER_ATTR_OWNERGROUP 0x00000080
00237 #define SSH_FILEXFER_ATTR_SUBSECOND_TIMES 0x00000100
00238 #define SSH_FILEXFER_ATTR_EXTENDED 0x80000000
00239 #define SSH_FILEXFER_ATTR_UIDGID 0x00000002
00240 
00241 /* types */
00242 #define SSH_FILEXFER_TYPE_REGULAR 1
00243 #define SSH_FILEXFER_TYPE_DIRECTORY 2
00244 #define SSH_FILEXFER_TYPE_SYMLINK 3
00245 #define SSH_FILEXFER_TYPE_SPECIAL 4
00246 #define SSH_FILEXFER_TYPE_UNKNOWN 5
00247 
00248 /* server responses */
00249 #define SSH_FX_OK 0
00250 #define SSH_FX_EOF 1
00251 #define SSH_FX_NO_SUCH_FILE 2
00252 #define SSH_FX_PERMISSION_DENIED 3
00253 #define SSH_FX_FAILURE 4
00254 #define SSH_FX_BAD_MESSAGE 5
00255 #define SSH_FX_NO_CONNECTION 6
00256 #define SSH_FX_CONNECTION_LOST 7
00257 #define SSH_FX_OP_UNSUPPORTED 8
00258 #define SSH_FX_INVALID_HANDLE 9
00259 #define SSH_FX_NO_SUCH_PATH 10
00260 #define SSH_FX_FILE_ALREADY_EXISTS 11
00261 #define SSH_FX_WRITE_PROTECT 12
00262 #define SSH_FX_NO_MEDIA 13
00263 
00264 /* file flags */
00265 #define SSH_FXF_READ 0x01
00266 #define SSH_FXF_WRITE 0x02
00267 #define SSH_FXF_APPEND 0x04
00268 #define SSH_FXF_CREAT 0x08
00269 #define SSH_FXF_TRUNC 0x10
00270 #define SSH_FXF_EXCL 0x20
00271 #define SSH_FXF_TEXT 0x40
00272 
00273 #define SFTP_OPEN SSH_FXP_OPEN
00274 #define SFTP_CLOSE SSH_FXP_CLOSE
00275 #define SFTP_READ SSH_FXP_READ
00276 #define SFTP_WRITE SSH_FXP_WRITE
00277 #define SFTP_LSTAT SSH_FXP_LSTAT
00278 #define SFTP_FSTAT SSH_FXP_FSTAT
00279 #define SFTP_SETSTAT SSH_FXP_SETSTAT
00280 #define SFTP_FSETSTAT SSH_FXP_FSETSTAT
00281 #define SFTP_OPENDIR SSH_FXP_OPENDIR
00282 #define SFTP_READDIR SSH_FXP_READDIR
00283 #define SFTP_REMOVE SSH_FXP_REMOVE
00284 #define SFTP_MKDIR SSH_FXP_MKDIR
00285 #define SFTP_RMDIR SSH_FXP_RMDIR
00286 #define SFTP_REALPATH SSH_FXP_REALPATH
00287 #define SFTP_STAT SSH_FXP_STAT
00288 #define SFTP_RENAME SSH_FXP_RENAME
00289 #define SFTP_READLINK SSH_FXP_READLINK
00290 #define SFTP_SYMLINK SSH_FXP_SYMLINK
00291 
00292 
00293 #ifdef __cplusplus
00294 } ;
00295 #endif
00296 
00297 #endif /* SFTP_H */

Generated on Sun Aug 23 08:55:35 2009 for libssh by  doxygen 1.4.7