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

Generated on Thu Apr 5 05:21:44 2007 for libssh by  doxygen 1.5.1