1 module libssh.c_bindings.sftp;
2 
3 import libssh.c_bindings.libssh;
4 import libssh.c_bindings.ctypes;
5 
6 version(Windows)
7 {
8 	public import core.stdc.stdio;
9     enum S_IRUSR    = 0x100;  // octal 0400
10     enum S_IWUSR    = 0x080;  // octal 0200
11     enum S_IXUSR    = 0x040;  // octal 0100
12     enum S_IRWXU    = S_IRUSR | S_IWUSR | S_IXUSR;
13 
14     enum S_IRGRP    = S_IRUSR >> 3;
15     enum S_IWGRP    = S_IWUSR >> 3;
16     enum S_IXGRP    = S_IXUSR >> 3;
17     enum S_IRWXG    = S_IRWXU >> 3;
18 
19     enum S_IROTH    = S_IRGRP >> 3;
20     enum S_IWOTH    = S_IWGRP >> 3;
21     enum S_IXOTH    = S_IXGRP >> 3;
22     enum S_IRWXO    = S_IRWXG >> 3;
23 
24     enum S_ISUID    = 0x800; // octal 04000
25     enum S_ISGID    = 0x400; // octal 02000
26 }
27 else
28 {
29 	public import core.sys.posix.fcntl;
30 }
31 
32 enum auto LIBSFTP_VERSION = 3;
33 
34 struct sftp_ext_struct { }
35 
36 alias sftp_attributes = sftp_attributes_struct*;
37 alias sftp_client_message = sftp_client_message_struct*;
38 alias sftp_dir = sftp_dir_struct*;
39 alias sftp_ext = sftp_ext_struct*;
40 alias sftp_file = sftp_file_struct*;
41 alias sftp_message = sftp_message_struct*;
42 alias sftp_packet = sftp_packet_struct*;
43 alias sftp_request_queue = sftp_request_queue_struct*;
44 alias sftp_session = sftp_session_struct*;
45 alias sftp_status_message = sftp_status_message_struct*;
46 alias sftp_statvfs_t = sftp_statvfs_struct*;
47 
48 struct sftp_session_struct {
49     ssh_session session;
50     ssh_channel channel;
51     int server_version;
52     int client_version;
53     int version_;
54     sftp_request_queue queue;
55     uint id_counter;
56     int errnum;
57     void **handles;
58     sftp_ext ext;
59 };
60 
61 struct sftp_packet_struct {
62     sftp_session sftp;
63     ubyte type;
64     ssh_buffer payload;
65 };
66 
67 /* file handler */
68 struct sftp_file_struct {
69     sftp_session sftp;
70     char *name;
71     ulong offset;
72     ssh_string handle;
73     int eof;
74     int nonblocking;
75 };
76 
77 struct sftp_dir_struct {
78     sftp_session sftp;
79     char *name;
80     ssh_string handle; /* handle to directory */
81     ssh_buffer buffer; /* contains raw attributes from server which haven't been parsed */
82     uint count; /* counts the number of following attributes structures into buffer */
83     int eof; /* end of directory listing */
84 };
85 
86 struct sftp_message_struct {
87     sftp_session sftp;
88     ubyte packet_type;
89     ssh_buffer payload;
90     uint id;
91 };
92 
93 /* this is a bunch of all data that could be into a message */
94 struct sftp_client_message_struct {
95     sftp_session sftp;
96     ubyte type;
97     uint id;
98     char *filename; /* can be "path" */
99     uint flags;
100     sftp_attributes attr;
101     ssh_string handle;
102     ulong offset;
103     uint len;
104     int attr_num;
105     ssh_buffer attrbuf; /* used by sftp_reply_attrs */
106     ssh_string data; /* can be newpath of rename() */
107     ssh_buffer complete_message; /* complete message in case of retransmission*/
108     char *str_data; /* cstring version of data */
109 };
110 
111 struct sftp_request_queue_struct {
112     sftp_request_queue next;
113     sftp_message message;
114 };
115 
116 /* SSH_FXP_MESSAGE described into .7 page 26 */
117 struct sftp_status_message_struct {
118     uint id;
119     uint status;
120     ssh_string error_unused; /* not used anymore */
121     ssh_string lang_unused;  /* not used anymore */
122     char *errormsg;
123     char *langmsg;
124 };
125 
126 struct sftp_attributes_struct {
127     char *name;
128     char *longname; /* ls -l output on openssh, not reliable else */
129     uint flags;
130     ubyte type;
131     ulong size;
132     uint uid;
133     uint gid;
134     char *owner; /* set if openssh and version 4 */
135     char *group; /* set if openssh and version 4 */
136     uint permissions;
137     ulong atime64;
138     uint atime;
139     uint atime_nseconds;
140     ulong createtime;
141     uint createtime_nseconds;
142     ulong mtime64;
143     uint mtime;
144     uint mtime_nseconds;
145     ssh_string acl;
146     uint extended_count;
147     ssh_string extended_type;
148     ssh_string extended_data;
149 };
150 
151 /**
152  * @brief SFTP statvfs structure.
153  */
154 struct sftp_statvfs_struct {
155     ulong f_bsize;   /** file system block size */
156     ulong f_frsize;  /** fundamental fs block size */
157     ulong f_blocks;  /** number of blocks (unit f_frsize) */
158     ulong f_bfree;   /** free blocks in file system */
159     ulong f_bavail;  /** free blocks for non-root */
160     ulong f_files;   /** total file inodes */
161     ulong f_ffree;   /** free file inodes */
162     ulong f_favail;  /** free file inodes for to non-root */
163     ulong f_fsid;    /** file system id */
164     ulong f_flag;    /** bit mask of f_flag values */
165     ulong f_namemax; /** maximum filename length */
166 };
167 
168 extern(C) {
169     /**
170     * @brief Start a new sftp session.
171     *
172     * @param session       The ssh session to use.
173     *
174     * @return              A new sftp session or NULL on error.
175     *
176     * @see sftp_free()
177     */
178     sftp_session sftp_new(ssh_session session);
179 
180     /**
181     * @brief Start a new sftp session with an existing channel.
182     *
183     * @param session       The ssh session to use.
184     * @param channel       An open session channel with subsystem already allocated
185     *
186     * @return              A new sftp session or NULL on error.
187     *
188     * @see sftp_free()
189     */
190     sftp_session sftp_new_channel(ssh_session session, ssh_channel channel);
191 
192 
193     /**
194     * @brief Close and deallocate a sftp session.
195     *
196     * @param sftp          The sftp session handle to free.
197     */
198     void sftp_free(sftp_session sftp);
199 
200     /**
201     * @brief Initialize the sftp session with the server.
202     *
203     * @param sftp          The sftp session to initialize.
204     *
205     * @return              0 on success, < 0 on error with ssh error set.
206     *
207     * @see sftp_new()
208     */
209     int sftp_init(sftp_session sftp);
210 
211     /**
212     * @brief Get the last sftp error.
213     *
214     * Use this function to get the latest error set by a posix like sftp function.
215     *
216     * @param sftp          The sftp session where the error is saved.
217     *
218     * @return              The saved error (see server responses), < 0 if an error
219     *                      in the function occured.
220     *
221     * @see Server responses
222     */
223     int sftp_get_error(sftp_session sftp);
224 
225     /**
226     * @brief Get the count of extensions provided by the server.
227     *
228     * @param  sftp         The sftp session to use.
229     *
230     * @return The count of extensions provided by the server, 0 on error or
231     *         not available.
232     */
233     uint sftp_extensions_get_count(sftp_session sftp);
234 
235     /**
236     * @brief Get the name of the extension provided by the server.
237     *
238     * @param  sftp         The sftp session to use.
239     *
240     * @param  indexn        The index number of the extension name you want.
241     *
242     * @return              The name of the extension.
243     */
244     const(char) *sftp_extensions_get_name(sftp_session sftp, uint indexn);
245 
246     /**
247     * @brief Get the data of the extension provided by the server.
248     *
249     * This is normally the version number of the extension.
250     *
251     * @param  sftp         The sftp session to use.
252     *
253     * @param  indexn        The index number of the extension data you want.
254     *
255     * @return              The data of the extension.
256     */
257     const(char) *sftp_extensions_get_data(sftp_session sftp, uint indexn);
258 
259     /**
260     * @brief Check if the given extension is supported.
261     *
262     * @param  sftp         The sftp session to use.
263     *
264     * @param  name         The name of the extension.
265     *
266     * @param  data         The data of the extension.
267     *
268     * @return 1 if supported, 0 if not.
269     *
270     * Example:
271     *
272     * @code
273     * sftp_extension_supported(sftp, "statvfs@openssh.com", "2");
274     * @endcode
275     */
276     int sftp_extension_supported(sftp_session sftp, const char *name,
277         const char *data);
278 
279     /**
280     * @brief Open a directory used to obtain directory entries.
281     *
282     * @param session       The sftp session handle to open the directory.
283     * @param path          The path of the directory to open.
284     *
285     * @return              A sftp directory handle or NULL on error with ssh and
286     *                      sftp error set.
287     *
288     * @see                 sftp_readdir
289     * @see                 sftp_closedir
290     */
291     sftp_dir sftp_opendir(sftp_session session, const char *path);
292 
293     /**
294     * @brief Get a single file attributes structure of a directory.
295     *
296     * @param session      The sftp session handle to read the directory entry.
297     * @param dir          The opened sftp directory handle to read from.
298     *
299     * @return             A file attribute structure or NULL at the end of the
300     *                     directory.
301     *
302     * @see                sftp_opendir()
303     * @see                sftp_attribute_free()
304     * @see                sftp_closedir()
305     */
306     sftp_attributes sftp_readdir(sftp_session session, sftp_dir dir);
307 
308     /**
309     * @brief Tell if the directory has reached EOF (End Of File).
310     *
311     * @param dir           The sftp directory handle.
312     *
313     * @return              1 if the directory is EOF, 0 if not.
314     *
315     * @see                 sftp_readdir()
316     */
317     int sftp_dir_eof(sftp_dir dir);
318 
319     /**
320     * @brief Get information about a file or directory.
321     *
322     * @param session       The sftp session handle.
323     * @param path          The path to the file or directory to obtain the
324     *                      information.
325     *
326     * @return              The sftp attributes structure of the file or directory,
327     *                      NULL on error with ssh and sftp error set.
328     *
329     * @see sftp_get_error()
330     */
331     sftp_attributes sftp_stat(sftp_session session, const char *path);
332 
333     /**
334     * @brief Get information about a file or directory.
335     *
336     * Identical to sftp_stat, but if the file or directory is a symbolic link,
337     * then the link itself is stated, not the file that it refers to.
338     *
339     * @param session       The sftp session handle.
340     * @param path          The path to the file or directory to obtain the
341     *                      information.
342     *
343     * @return              The sftp attributes structure of the file or directory,
344     *                      NULL on error with ssh and sftp error set.
345     *
346     * @see sftp_get_error()
347     */
348     sftp_attributes sftp_lstat(sftp_session session, const char *path);
349 
350     /**
351     * @brief Get information about a file or directory from a file handle.
352     *
353     * @param file          The sftp file handle to get the stat information.
354     *
355     * @return              The sftp attributes structure of the file or directory,
356     *                      NULL on error with ssh and sftp error set.
357     *
358     * @see sftp_get_error()
359     */
360     sftp_attributes sftp_fstat(sftp_file file);
361 
362     /**
363     * @brief Free a sftp attribute structure.
364     *
365     * @param file          The sftp attribute structure to free.
366     */
367     void sftp_attributes_free(sftp_attributes file);
368 
369     /**
370     * @brief Close a directory handle opened by sftp_opendir().
371     *
372     * @param dir           The sftp directory handle to close.
373     *
374     * @return              Returns SSH_NO_ERROR or SSH_ERROR if an error occured.
375     */
376     int sftp_closedir(sftp_dir dir);
377 
378     /**
379     * @brief Close an open file handle.
380     *
381     * @param file          The open sftp file handle to close.
382     *
383     * @return              Returns SSH_NO_ERROR or SSH_ERROR if an error occured.
384     *
385     * @see                 sftp_open()
386     */
387     int sftp_close(sftp_file file);
388 
389     /**
390     * @brief Open a file on the server.
391     *
392     * @param session       The sftp session handle.
393     *
394     * @param file          The file to be opened.
395     *
396     * @param accesstype    Is one of O_RDONLY, O_WRONLY or O_RDWR which request
397     *                      opening  the  file  read-only,write-only or read/write.
398     *                      Acesss may also be bitwise-or'd with one or  more of
399     *                      the following:
400     *                      O_CREAT - If the file does not exist it will be
401     *                      created.
402     *                      O_EXCL - When  used with O_CREAT, if the file already
403     *                      exists it is an error and the open will fail.
404     *                      O_TRUNC - If the file already exists it will be
405     *                      truncated.
406     *
407     * @param mode          Mode specifies the permissions to use if a new file is
408     *                      created.  It  is  modified  by  the process's umask in
409     *                      the usual way: The permissions of the created file are
410     *                      (mode & ~umask)
411     *
412     * @return              A sftp file handle, NULL on error with ssh and sftp
413     *                      error set.
414     *
415     * @see sftp_get_error()
416     */
417     sftp_file sftp_open(sftp_session session, const char *file, int accesstype,
418         mode_t mode);
419 
420     /**
421     * @brief Make the sftp communication for this file handle non blocking.
422     *
423     * @param[in]  handle   The file handle to set non blocking.
424     */
425     void sftp_file_set_nonblocking(sftp_file handle);
426 
427     /**
428     * @brief Make the sftp communication for this file handle blocking.
429     *
430     * @param[in]  handle   The file handle to set blocking.
431     */
432     void sftp_file_set_blocking(sftp_file handle);
433 
434     /**
435     * @brief Read from a file using an opened sftp file handle.
436     *
437     * @param file          The opened sftp file handle to be read from.
438     *
439     * @param buf           Pointer to buffer to recieve read data.
440     *
441     * @param count         Size of the buffer in bytes.
442     *
443     * @return              Number of bytes written, < 0 on error with ssh and sftp
444     *                      error set.
445     *
446     * @see sftp_get_error()
447     */
448     ptrdiff_t sftp_read(sftp_file file, void *buf, size_t count);
449 
450     /**
451     * @brief Start an asynchronous read from a file using an opened sftp file handle.
452     *
453     * Its goal is to avoid the slowdowns related to the request/response pattern
454     * of a synchronous read. To do so, you must call 2 functions:
455     *
456     * sftp_async_read_begin() and sftp_async_read().
457     *
458     * The first step is to call sftp_async_read_begin(). This function returns a
459     * request identifier. The second step is to call sftp_async_read() using the
460     * returned identifier.
461     *
462     * @param file          The opened sftp file handle to be read from.
463     *
464     * @param len           Size to read in bytes.
465     *
466     * @return              An identifier corresponding to the sent request, < 0 on
467     *                      error.
468     *
469     * @warning             When calling this function, the internal offset is
470     *                      updated corresponding to the len parameter.
471     *
472     * @warning             A call to sftp_async_read_begin() sends a request to
473     *                      the server. When the server answers, libssh allocates
474     *                      memory to store it until sftp_async_read() is called.
475     *                      Not calling sftp_async_read() will lead to memory
476     *                      leaks.
477     *
478     * @see                 sftp_async_read()
479     * @see                 sftp_open()
480     */
481     int sftp_async_read_begin(sftp_file file, uint len);
482 
483     /**
484     * @brief Wait for an asynchronous read to complete and save the data.
485     *
486     * @param file          The opened sftp file handle to be read from.
487     *
488     * @param data          Pointer to buffer to recieve read data.
489     *
490     * @param len           Size of the buffer in bytes. It should be bigger or
491     *                      equal to the length parameter of the
492     *                      sftp_async_read_begin() call.
493     *
494     * @param id            The identifier returned by the sftp_async_read_begin()
495     *                      function.
496     *
497     * @return              Number of bytes read, 0 on EOF, SSH_ERROR if an error
498     *                      occured, SSH_AGAIN if the file is opened in nonblocking
499     *                      mode and the request hasn't been executed yet.
500     *
501     * @warning             A call to this function with an invalid identifier
502     *                      will never return.
503     *
504     * @see sftp_async_read_begin()
505     */
506     int sftp_async_read(sftp_file file, void *data, uint len, uint id);
507 
508     /**
509     * @brief Write to a file using an opened sftp file handle.
510     *
511     * @param file          Open sftp file handle to write to.
512     *
513     * @param buf           Pointer to buffer to write data.
514     *
515     * @param count         Size of buffer in bytes.
516     *
517     * @return              Number of bytes written, < 0 on error with ssh and sftp
518     *                      error set.
519     *
520     * @see                 sftp_open()
521     * @see                 sftp_read()
522     * @see                 sftp_close()
523     */
524     ptrdiff_t sftp_write(sftp_file file, const void *buf, size_t count);
525 
526     /**
527     * @brief Seek to a specific location in a file.
528     *
529     * @param file         Open sftp file handle to seek in.
530     *
531     * @param new_offset   Offset in bytes to seek.
532     *
533     * @return             0 on success, < 0 on error.
534     */
535     int sftp_seek(sftp_file file, uint new_offset);
536 
537     /**
538     * @brief Seek to a specific location in a file. This is the
539     * 64bit version.
540     *
541     * @param file         Open sftp file handle to seek in.
542     *
543     * @param new_offset   Offset in bytes to seek.
544     *
545     * @return             0 on success, < 0 on error.
546     */
547     int sftp_seek64(sftp_file file, ulong new_offset);
548 
549     /**
550     * @brief Report current byte position in file.
551     *
552     * @param file          Open sftp file handle.
553     *
554     * @return              The offset of the current byte relative to the beginning
555     *                      of the file associated with the file descriptor. < 0 on
556     *                      error.
557     */
558     uint sftp_tell(sftp_file file);
559 
560     /**
561     * @brief Report current byte position in file.
562     *
563     * @param file          Open sftp file handle.
564     *
565     * @return              The offset of the current byte relative to the beginning
566     *                      of the file associated with the file descriptor. < 0 on
567     *                      error.
568     */
569     ulong sftp_tell64(sftp_file file);
570 
571     /**
572     * @brief Rewinds the position of the file pointer to the beginning of the
573     * file.
574     *
575     * @param file          Open sftp file handle.
576     */
577     void sftp_rewind(sftp_file file);
578 
579     /**
580     * @brief Unlink (delete) a file.
581     *
582     * @param sftp          The sftp session handle.
583     *
584     * @param file          The file to unlink/delete.
585     *
586     * @return              0 on success, < 0 on error with ssh and sftp error set.
587     *
588     * @see sftp_get_error()
589     */
590     int sftp_unlink(sftp_session sftp, const char *file);
591 
592     /**
593     * @brief Remove a directoy.
594     *
595     * @param sftp          The sftp session handle.
596     *
597     * @param directory     The directory to remove.
598     *
599     * @return              0 on success, < 0 on error with ssh and sftp error set.
600     *
601     * @see sftp_get_error()
602     */
603     int sftp_rmdir(sftp_session sftp, const char *directory);
604 
605     /**
606     * @brief Create a directory.
607     *
608     * @param sftp          The sftp session handle.
609     *
610     * @param directory     The directory to create.
611     *
612     * @param mode          Specifies the permissions to use. It is modified by the
613     *                      process's umask in the usual way:
614     *                      The permissions of the created file are (mode & ~umask)
615     *
616     * @return              0 on success, < 0 on error with ssh and sftp error set.
617     *
618     * @see sftp_get_error()
619     */
620     int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode);
621 
622     /**
623     * @brief Rename or move a file or directory.
624     *
625     * @param sftp          The sftp session handle.
626     *
627     * @param original      The original url (source url) of file or directory to
628     *                      be moved.
629     *
630     * @param newname       The new url (destination url) of the file or directory
631     *                      after the move.
632     *
633     * @return              0 on success, < 0 on error with ssh and sftp error set.
634     *
635     * @see sftp_get_error()
636     */
637     int sftp_rename(sftp_session sftp, const char *original, const  char *newname);
638 
639     /**
640     * @brief Set file attributes on a file, directory or symbolic link.
641     *
642     * @param sftp          The sftp session handle.
643     *
644     * @param file          The file which attributes should be changed.
645     *
646     * @param attr          The file attributes structure with the attributes set
647     *                      which should be changed.
648     *
649     * @return              0 on success, < 0 on error with ssh and sftp error set.
650     *
651     * @see sftp_get_error()
652     */
653     int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr);
654 
655     /**
656     * @brief Change the file owner and group
657     *
658     * @param sftp          The sftp session handle.
659     *
660     * @param file          The file which owner and group should be changed.
661     *
662     * @param owner         The new owner which should be set.
663     *
664     * @param group         The new group which should be set.
665     *
666     * @return              0 on success, < 0 on error with ssh and sftp error set.
667     *
668     * @see sftp_get_error()
669     */
670     int sftp_chown(sftp_session sftp, const char *file, uid_t owner, gid_t group);
671 
672     /**
673     * @brief Change permissions of a file
674     *
675     * @param sftp          The sftp session handle.
676     *
677     * @param file          The file which owner and group should be changed.
678     *
679     * @param mode          Specifies the permissions to use. It is modified by the
680     *                      process's umask in the usual way:
681     *                      The permissions of the created file are (mode & ~umask)
682     *
683     * @return              0 on success, < 0 on error with ssh and sftp error set.
684     *
685     * @see sftp_get_error()
686     */
687     int sftp_chmod(sftp_session sftp, const char *file, mode_t mode);
688 
689     /**
690     * @brief Change the last modification and access time of a file.
691     *
692     * @param sftp          The sftp session handle.
693     *
694     * @param file          The file which owner and group should be changed.
695     *
696     * @param times         A timeval structure which contains the desired access
697     *                      and modification time.
698     *
699     * @return              0 on success, < 0 on error with ssh and sftp error set.
700     *
701     * @see sftp_get_error()
702     */
703     int sftp_utimes(sftp_session sftp, const char *file, const timeval *times);
704 
705     /**
706     * @brief Create a symbolic link.
707     *
708     * @param  sftp         The sftp session handle.
709     *
710     * @param  target       Specifies the target of the symlink.
711     *
712     * @param  dest         Specifies the path name of the symlink to be created.
713     *
714     * @return              0 on success, < 0 on error with ssh and sftp error set.
715     *
716     * @see sftp_get_error()
717     */
718     int sftp_symlink(sftp_session sftp, const char *target, const char *dest);
719 
720     /**
721     * @brief Read the value of a symbolic link.
722     *
723     * @param  sftp         The sftp session handle.
724     *
725     * @param  path         Specifies the path name of the symlink to be read.
726     *
727     * @return              The target of the link, NULL on error.
728     *
729     * @see sftp_get_error()
730     */
731     char *sftp_readlink(sftp_session sftp, const char *path);
732 
733     /**
734     * @brief Get information about a mounted file system.
735     *
736     * @param  sftp         The sftp session handle.
737     *
738     * @param  path         The pathname of any file within the mounted file system.
739     *
740     * @return A statvfs structure or NULL on error.
741     *
742     * @see sftp_get_error()
743     */
744     sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path);
745 
746     /**
747     * @brief Get information about a mounted file system.
748     *
749     * @param  file         An opened file.
750     *
751     * @return A statvfs structure or NULL on error.
752     *
753     * @see sftp_get_error()
754     */
755     sftp_statvfs_t sftp_fstatvfs(sftp_file file);
756 
757     /**
758     * @brief Free the memory of an allocated statvfs.
759     *
760     * @param  statvfs_o      The statvfs to free.
761     */
762     void sftp_statvfs_free(sftp_statvfs_t statvfs_o);
763 
764     /**
765     * @brief Canonicalize a sftp path.
766     *
767     * @param sftp          The sftp session handle.
768     *
769     * @param path          The path to be canonicalized.
770     *
771     * @return              The canonicalize path, NULL on error.
772     */
773     char *sftp_canonicalize_path(sftp_session sftp, const char *path);
774 
775     /**
776     * @brief Get the version of the SFTP protocol supported by the server
777     *
778     * @param sftp          The sftp session handle.
779     *
780     * @return              The server version.
781     */
782     int sftp_server_version(sftp_session sftp);
783 
784     version (LIBSSH_WITH_SERVER) {
785         /**
786         * @brief Create a new sftp server session.
787         *
788         * @param session       The ssh session to use.
789         *
790         * @param chan          The ssh channel to use.
791         *
792         * @return              A new sftp server session.
793         */
794         sftp_session sftp_server_new(ssh_session session, ssh_channel chan);
795 
796         /**
797         * @brief Intialize the sftp server.
798         *
799         * @param sftp         The sftp session to init.
800         *
801         * @return             0 on success, < 0 on error.
802         */
803         int sftp_server_init(sftp_session sftp);
804     }
805 
806 //    /* this is not a public interface */
807 //    #define SFTP_HANDLES 256
808 //    sftp_packet sftp_packet_read(sftp_session sftp);
809 //    int sftp_packet_write(sftp_session sftp,uint8_t type, ssh_buffer payload);
810 //    void sftp_packet_free(sftp_packet packet);
811 //    int buffer_add_attributes(ssh_buffer buffer, sftp_attributes attr);
812 //    sftp_attributes sftp_parse_attr(sftp_session session, ssh_buffer buf,int expectname);
813 //    /* sftpserver.c */
814 //    
815 //    LIBSSH_API sftp_client_message sftp_get_client_message(sftp_session sftp);
816 //    LIBSSH_API void sftp_client_message_free(sftp_client_message msg);
817 //    LIBSSH_API uint8_t sftp_client_message_get_type(sftp_client_message msg);
818 //    LIBSSH_API const char *sftp_client_message_get_filename(sftp_client_message msg);
819 //    LIBSSH_API void sftp_client_message_set_filename(sftp_client_message msg, const char *newname);
820 //    LIBSSH_API const char *sftp_client_message_get_data(sftp_client_message msg);
821 //    LIBSSH_API uint32_t sftp_client_message_get_flags(sftp_client_message msg);
822 //    LIBSSH_API int sftp_send_client_message(sftp_session sftp, sftp_client_message msg);
823 //    int sftp_reply_name(sftp_client_message msg, const char *name,
824 //        sftp_attributes attr);
825 //    int sftp_reply_handle(sftp_client_message msg, ssh_string handle);
826 //    ssh_string sftp_handle_alloc(sftp_session sftp, void *info);
827 //    int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr);
828 //    void *sftp_handle(sftp_session sftp, ssh_string handle);
829 //    int sftp_reply_status(sftp_client_message msg, uint32_t status, const char *message);
830 //    int sftp_reply_names_add(sftp_client_message msg, const char *file,
831 //        const char *longname, sftp_attributes attr);
832 //    int sftp_reply_names(sftp_client_message msg);
833 //    int sftp_reply_data(sftp_client_message msg, const void *data, int len);
834 //    void sftp_handle_remove(sftp_session sftp, void *handle);
835 }
836 
837 /* SFTP commands and constants */
838 enum auto SSH_FXP_INIT = 1;
839 enum auto SSH_FXP_VERSION = 2;
840 enum auto SSH_FXP_OPEN = 3;
841 enum auto SSH_FXP_CLOSE = 4;
842 enum auto SSH_FXP_READ = 5;
843 enum auto SSH_FXP_WRITE = 6;
844 enum auto SSH_FXP_LSTAT = 7;
845 enum auto SSH_FXP_FSTAT = 8;
846 enum auto SSH_FXP_SETSTAT = 9;
847 enum auto SSH_FXP_FSETSTAT = 10;
848 enum auto SSH_FXP_OPENDIR = 11;
849 enum auto SSH_FXP_READDIR = 12;
850 enum auto SSH_FXP_REMOVE = 13;
851 enum auto SSH_FXP_MKDIR = 14;
852 enum auto SSH_FXP_RMDIR = 15;
853 enum auto SSH_FXP_REALPATH = 16;
854 enum auto SSH_FXP_STAT = 17;
855 enum auto SSH_FXP_RENAME = 18;
856 enum auto SSH_FXP_READLINK = 19;
857 enum auto SSH_FXP_SYMLINK = 20;
858 
859 enum auto SSH_FXP_STATUS = 101;
860 enum auto SSH_FXP_HANDLE = 102;
861 enum auto SSH_FXP_DATA = 103;
862 enum auto SSH_FXP_NAME = 104;
863 enum auto SSH_FXP_ATTRS = 105;
864 
865 enum auto SSH_FXP_EXTENDED = 200;
866 enum auto SSH_FXP_EXTENDED_REPLY = 201;
867 
868 /* attributes */
869 /* sftp draft is completely braindead : version 3 and 4 have different flags for same constants */
870 /* and even worst, version 4 has same flag for 2 different constants */
871 /* follow up : i won't develop any sftp4 compliant library before having a clarification */
872 
873 enum auto SSH_FILEXFER_ATTR_SIZE = 0x00000001;
874 enum auto SSH_FILEXFER_ATTR_PERMISSIONS = 0x00000004;
875 enum auto SSH_FILEXFER_ATTR_ACCESSTIME = 0x00000008;
876 enum auto SSH_FILEXFER_ATTR_ACMODTIME =  0x00000008;
877 enum auto SSH_FILEXFER_ATTR_CREATETIME = 0x00000010;
878 enum auto SSH_FILEXFER_ATTR_MODIFYTIME = 0x00000020;
879 enum auto SSH_FILEXFER_ATTR_ACL = 0x00000040;
880 enum auto SSH_FILEXFER_ATTR_OWNERGROUP = 0x00000080;
881 enum auto SSH_FILEXFER_ATTR_SUBSECOND_TIMES = 0x00000100;
882 enum auto SSH_FILEXFER_ATTR_EXTENDED = 0x80000000;
883 enum auto SSH_FILEXFER_ATTR_UIDGID = 0x00000002;
884 
885 /* types */
886 enum auto SSH_FILEXFER_TYPE_REGULAR = 1;
887 enum auto SSH_FILEXFER_TYPE_DIRECTORY = 2;
888 enum auto SSH_FILEXFER_TYPE_SYMLINK = 3;
889 enum auto SSH_FILEXFER_TYPE_SPECIAL = 4;
890 enum auto SSH_FILEXFER_TYPE_UNKNOWN = 5;
891 
892 /**
893  * @name Server responses
894  *
895  * @brief Responses returned by the sftp server.
896  * @{
897  */
898 
899 /** No error */
900 enum auto SSH_FX_OK = 0;
901 /** End-of-file encountered */
902 enum auto SSH_FX_EOF = 1;
903 /** File doesn't exist */
904 enum auto SSH_FX_NO_SUCH_FILE = 2;
905 /** Permission denied */
906 enum auto SSH_FX_PERMISSION_DENIED = 3;
907 /** Generic failure */
908 enum auto SSH_FX_FAILURE = 4;
909 /** Garbage received from server */
910 enum auto SSH_FX_BAD_MESSAGE = 5;
911 /** No connection has been set up */
912 enum auto SSH_FX_NO_CONNECTION = 6;
913 /** There was a connection, but we lost it */
914 enum auto SSH_FX_CONNECTION_LOST = 7;
915 /** Operation not supported by the server */
916 enum auto SSH_FX_OP_UNSUPPORTED = 8;
917 /** Invalid file handle */
918 enum auto SSH_FX_INVALID_HANDLE = 9;
919 /** No such file or directory path exists */
920 enum auto SSH_FX_NO_SUCH_PATH = 10;
921 /** An attempt to create an already existing file or directory has been made */
922 enum auto SSH_FX_FILE_ALREADY_EXISTS = 11;
923 /** We are trying to write on a write-protected filesystem */
924 enum auto SSH_FX_WRITE_PROTECT = 12;
925 /** No media in remote drive */
926 enum auto SSH_FX_NO_MEDIA = 13;
927 
928 /** @} */
929 
930 /* file flags */
931 enum auto SSH_FXF_READ = 0x01;
932 enum auto SSH_FXF_WRITE = 0x02;
933 enum auto SSH_FXF_APPEND = 0x04;
934 enum auto SSH_FXF_CREAT = 0x08;
935 enum auto SSH_FXF_TRUNC = 0x10;
936 enum auto SSH_FXF_EXCL = 0x20;
937 enum auto SSH_FXF_TEXT = 0x40;
938 
939 /* rename flags */
940 enum auto SSH_FXF_RENAME_OVERWRITE =  0x00000001;
941 enum auto SSH_FXF_RENAME_ATOMIC =     0x00000002;
942 enum auto SSH_FXF_RENAME_NATIVE =     0x00000004;
943 
944 enum auto SFTP_OPEN = SSH_FXP_OPEN;
945 enum auto SFTP_CLOSE = SSH_FXP_CLOSE;
946 enum auto SFTP_READ = SSH_FXP_READ;
947 enum auto SFTP_WRITE = SSH_FXP_WRITE;
948 enum auto SFTP_LSTAT = SSH_FXP_LSTAT;
949 enum auto SFTP_FSTAT = SSH_FXP_FSTAT;
950 enum auto SFTP_SETSTAT = SSH_FXP_SETSTAT;
951 enum auto SFTP_FSETSTAT = SSH_FXP_FSETSTAT;
952 enum auto SFTP_OPENDIR = SSH_FXP_OPENDIR;
953 enum auto SFTP_READDIR = SSH_FXP_READDIR;
954 enum auto SFTP_REMOVE = SSH_FXP_REMOVE;
955 enum auto SFTP_MKDIR = SSH_FXP_MKDIR;
956 enum auto SFTP_RMDIR = SSH_FXP_RMDIR;
957 enum auto SFTP_REALPATH = SSH_FXP_REALPATH;
958 enum auto SFTP_STAT = SSH_FXP_STAT;
959 enum auto SFTP_RENAME = SSH_FXP_RENAME;
960 enum auto SFTP_READLINK = SSH_FXP_READLINK;
961 enum auto SFTP_SYMLINK = SSH_FXP_SYMLINK;
962 
963 /* openssh flags */
964 enum auto SSH_FXE_STATVFS_ST_RDONLY = 0x1; /* read-only */
965 enum auto SSH_FXE_STATVFS_ST_NOSUID = 0x2; /* no setuid */