sftp_server 0.5.0

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