2 * sftp.c: SFTP generic client code.
13 #define smalloc malloc
14 #define srealloc realloc
17 #define GET_32BIT(cp) \
18 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
19 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
20 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
21 ((unsigned long)(unsigned char)(cp)[3]))
23 #define PUT_32BIT(cp, value) { \
24 (cp)[0] = (unsigned char)((value) >> 24); \
25 (cp)[1] = (unsigned char)((value) >> 16); \
26 (cp)[2] = (unsigned char)((value) >> 8); \
27 (cp)[3] = (unsigned char)(value); }
36 /* ----------------------------------------------------------------------
37 * SFTP packet construction functions.
39 static void sftp_pkt_ensure(struct sftp_packet *pkt, int length) {
40 if (pkt->maxlen < length) {
41 pkt->maxlen = length + 256;
42 pkt->data = srealloc(pkt->data, pkt->maxlen);
45 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len) {
47 sftp_pkt_ensure(pkt, pkt->length);
48 memcpy(pkt->data+pkt->length-len, data, len);
50 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte) {
51 sftp_pkt_adddata(pkt, &byte, 1);
53 static struct sftp_packet *sftp_pkt_init(int pkt_type) {
54 struct sftp_packet *pkt;
55 pkt = smalloc(sizeof(struct sftp_packet));
60 sftp_pkt_addbyte(pkt, (unsigned char)pkt_type);
63 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value) {
64 sftp_pkt_adddata(pkt, &value, 1);
66 static void sftp_pkt_adduint32(struct sftp_packet *pkt, unsigned long value) {
69 sftp_pkt_adddata(pkt, x, 4);
71 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value) {
73 PUT_32BIT(x, value.hi);
74 PUT_32BIT(x+4, value.lo);
75 sftp_pkt_adddata(pkt, x, 8);
77 static void sftp_pkt_addstring_start(struct sftp_packet *pkt) {
78 sftp_pkt_adduint32(pkt, 0);
79 pkt->savedpos = pkt->length;
81 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data) {
82 sftp_pkt_adddata(pkt, data, strlen(data));
83 PUT_32BIT(pkt->data + pkt->savedpos - 4,
84 pkt->length - pkt->savedpos);
86 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
87 char *data, int len) {
88 sftp_pkt_adddata(pkt, data, len);
89 PUT_32BIT(pkt->data + pkt->savedpos - 4,
90 pkt->length - pkt->savedpos);
92 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data) {
93 sftp_pkt_addstring_start(pkt);
94 sftp_pkt_addstring_str(pkt, data);
97 /* ----------------------------------------------------------------------
98 * SFTP packet decode functions.
101 static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt) {
103 if (pkt->length - pkt->savedpos < 1)
104 return 0; /* arrgh, no way to decline (FIXME?) */
105 value = (unsigned char) pkt->data[pkt->savedpos];
109 static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt) {
111 if (pkt->length - pkt->savedpos < 4)
112 return 0; /* arrgh, no way to decline (FIXME?) */
113 value = GET_32BIT(pkt->data+pkt->savedpos);
117 static void sftp_pkt_getstring(struct sftp_packet *pkt,
118 char **p, int *length) {
120 if (pkt->length - pkt->savedpos < 4)
122 *length = GET_32BIT(pkt->data+pkt->savedpos);
124 if (pkt->length - pkt->savedpos < *length)
126 *p = pkt->data+pkt->savedpos;
127 pkt->savedpos += *length;
129 static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) {
130 struct fxp_attrs ret;
131 ret.flags = sftp_pkt_getuint32(pkt);
132 if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
133 unsigned long hi, lo;
134 hi = sftp_pkt_getuint32(pkt);
135 lo = sftp_pkt_getuint32(pkt);
136 ret.size = uint64_make(hi, lo);
138 if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
139 ret.uid = sftp_pkt_getuint32(pkt);
140 ret.gid = sftp_pkt_getuint32(pkt);
142 if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
143 ret.permissions = sftp_pkt_getuint32(pkt);
145 if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
146 ret.atime = sftp_pkt_getuint32(pkt);
147 ret.mtime = sftp_pkt_getuint32(pkt);
149 if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
151 count = sftp_pkt_getuint32(pkt);
156 * We should try to analyse these, if we ever find one
159 sftp_pkt_getstring(pkt, &str, &len);
160 sftp_pkt_getstring(pkt, &str, &len);
165 static void sftp_pkt_free(struct sftp_packet *pkt) {
166 if (pkt->data) sfree(pkt->data);
170 /* ----------------------------------------------------------------------
171 * Send and receive packet functions. FIXME: change for PuTTY.
178 assert(pipe(to) == 0);
179 assert(pipe(from) == 0);
183 /* We are child. Dup one end of each pipe to our std[io],
184 * close other end, exec. */
185 close(0); dup2(to[0], 0); close(to[1]);
186 close(1); dup2(from[1], 1); close(from[0]);
187 execl("/home/simon/src/openssh/openssh_cvs/prefix/bin/ssh", "ssh", "-2", "simon@localhost", "-s", "sftp", NULL);
188 //execl("/root/ssh-research/ssh-2.4.0/apps/ssh/sftp-server2", "sftp-server2", NULL);
189 //execl("/usr/lib/sftp-server", "sftp-server", NULL);
190 assert(0); /* bomb out if not */
192 /* We are parent. Close wrong end of each pipe, assign to glob vars. */
193 close(to[0]); tossh = to[1];
194 close(from[1]); fromssh = from[0];
197 int io_finish(void) {
203 int sftp_send(struct sftp_packet *pkt) {
205 PUT_32BIT(x, pkt->length);
206 assert(4 == write(tossh, x, 4));
207 assert(pkt->length = write(tossh, pkt->data, pkt->length));
210 struct sftp_packet *sftp_recv(void) {
211 struct sftp_packet *pkt;
215 for (p = 0; p < 4 ;) {
216 ret = read(fromssh, x+p, 4-p);
223 pkt = smalloc(sizeof(struct sftp_packet));
225 pkt->length = pkt->maxlen = GET_32BIT(x);
226 pkt->data = smalloc(pkt->length);
228 for (p = 0; p < pkt->length ;) {
229 ret = read(fromssh, pkt->data+p, pkt->length-p);
238 pkt->type = sftp_pkt_getbyte(pkt);
243 /* ----------------------------------------------------------------------
244 * String handling routines.
247 static char *mkstr(char *s, int len) {
248 char *p = smalloc(len+1);
254 /* ----------------------------------------------------------------------
258 static const char *fxp_error_message;
259 static int fxp_errtype;
262 * Deal with (and free) an FXP_STATUS packet. Return 1 if
263 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
264 * Also place the status into fxp_errtype.
266 static int fxp_got_status(struct sftp_packet *pktin) {
267 static const char *const messages[] = {
268 /* SSH_FX_OK. The only time we will display a _message_ for this
269 * is if we were expecting something other than FXP_STATUS on
270 * success, so this is actually an error message! */
271 "unexpected OK response",
273 "no such file or directory",
279 "operation unsupported",
282 if (pktin->type != SSH_FXP_STATUS) {
283 fxp_error_message = "expected FXP_STATUS packet";
286 fxp_errtype = sftp_pkt_getuint32(pktin);
287 if (fxp_errtype < 0 ||
288 fxp_errtype >= sizeof(messages)/sizeof(*messages))
289 fxp_error_message = "unknown error code";
291 fxp_error_message = messages[fxp_errtype];
294 if (fxp_errtype == SSH_FX_OK)
296 else if (fxp_errtype == SSH_FX_EOF)
302 static int fxp_internal_error(char *msg) {
303 fxp_error_message = msg;
307 const char *fxp_error(void) {
308 return fxp_error_message;
311 int fxp_error_type(void) {
316 * Perform exchange of init/version packets. Return 0 on failure.
319 struct sftp_packet *pktout, *pktin;
322 pktout = sftp_pkt_init(SSH_FXP_INIT);
323 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
327 if (pktin->type != SSH_FXP_VERSION) {
328 fxp_internal_error("did not receive FXP_VERSION");
331 remotever = sftp_pkt_getuint32(pktin);
332 if (remotever > SFTP_PROTO_VERSION) {
333 fxp_internal_error("remote protocol is more advanced than we support");
337 * In principle, this packet might also contain extension-
338 * string pairs. We should work through them and look for any
339 * we recognise. In practice we don't currently do so because
340 * we know we don't recognise _any_.
342 sftp_pkt_free(pktin);
348 * Canonify a pathname.
350 char *fxp_realpath(char *path) {
351 struct sftp_packet *pktin, *pktout;
354 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
355 sftp_pkt_adduint32(pktout, 0x123); /* request id */
356 sftp_pkt_addstring_start(pktout);
357 sftp_pkt_addstring_str(pktout, path);
360 id = sftp_pkt_getuint32(pktin);
362 fxp_internal_error("request ID mismatch\n");
365 if (pktin->type == SSH_FXP_NAME) {
370 count = sftp_pkt_getuint32(pktin);
372 fxp_internal_error("REALPATH returned name count != 1\n");
375 sftp_pkt_getstring(pktin, &path, &len);
377 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
380 path = mkstr(path, len);
381 sftp_pkt_free(pktin);
384 fxp_got_status(pktin);
392 struct fxp_handle *fxp_open(char *path, int type) {
393 struct sftp_packet *pktin, *pktout;
396 pktout = sftp_pkt_init(SSH_FXP_OPEN);
397 sftp_pkt_adduint32(pktout, 0x567); /* request id */
398 sftp_pkt_addstring(pktout, path);
399 sftp_pkt_adduint32(pktout, type);
400 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
403 id = sftp_pkt_getuint32(pktin);
405 fxp_internal_error("request ID mismatch\n");
408 if (pktin->type == SSH_FXP_HANDLE) {
411 struct fxp_handle *handle;
414 sftp_pkt_getstring(pktin, &hstring, &len);
416 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
419 handle = smalloc(sizeof(struct fxp_handle));
420 handle->hstring = mkstr(hstring, len);
422 sftp_pkt_free(pktin);
425 fxp_got_status(pktin);
433 struct fxp_handle *fxp_opendir(char *path) {
434 struct sftp_packet *pktin, *pktout;
437 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
438 sftp_pkt_adduint32(pktout, 0x456); /* request id */
439 sftp_pkt_addstring(pktout, path);
442 id = sftp_pkt_getuint32(pktin);
444 fxp_internal_error("request ID mismatch\n");
447 if (pktin->type == SSH_FXP_HANDLE) {
450 struct fxp_handle *handle;
453 sftp_pkt_getstring(pktin, &hstring, &len);
455 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
458 handle = smalloc(sizeof(struct fxp_handle));
459 handle->hstring = mkstr(hstring, len);
461 sftp_pkt_free(pktin);
464 fxp_got_status(pktin);
472 void fxp_close(struct fxp_handle *handle) {
473 struct sftp_packet *pktin, *pktout;
476 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
477 sftp_pkt_adduint32(pktout, 0x789); /* request id */
478 sftp_pkt_addstring_start(pktout);
479 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
482 id = sftp_pkt_getuint32(pktin);
484 fxp_internal_error("request ID mismatch\n");
487 fxp_got_status(pktin);
488 sfree(handle->hstring);
493 * Read from a file. Returns the number of bytes read, or -1 on an
494 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
495 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
496 * error indicator. It might even depend on the SFTP server.)
498 int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
499 struct sftp_packet *pktin, *pktout;
502 pktout = sftp_pkt_init(SSH_FXP_READ);
503 sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
504 sftp_pkt_addstring_start(pktout);
505 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
506 sftp_pkt_adduint64(pktout, offset);
507 sftp_pkt_adduint32(pktout, len);
510 id = sftp_pkt_getuint32(pktin);
512 fxp_internal_error("request ID mismatch");
515 if (pktin->type == SSH_FXP_DATA) {
519 sftp_pkt_getstring(pktin, &str, &rlen);
521 if (rlen > len || rlen < 0) {
522 fxp_internal_error("READ returned more bytes than requested");
526 memcpy(buffer, str, rlen);
530 fxp_got_status(pktin);
536 * Read from a directory.
538 struct fxp_names *fxp_readdir(struct fxp_handle *handle) {
539 struct sftp_packet *pktin, *pktout;
542 pktout = sftp_pkt_init(SSH_FXP_READDIR);
543 sftp_pkt_adduint32(pktout, 0xABC); /* request id */
544 sftp_pkt_addstring_start(pktout);
545 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
548 id = sftp_pkt_getuint32(pktin);
550 fxp_internal_error("request ID mismatch\n");
553 if (pktin->type == SSH_FXP_NAME) {
554 struct fxp_names *ret;
556 ret = smalloc(sizeof(struct fxp_names));
557 ret->nnames = sftp_pkt_getuint32(pktin);
558 ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
559 for (i = 0; i < ret->nnames; i++) {
562 sftp_pkt_getstring(pktin, &str, &len);
563 ret->names[i].filename = mkstr(str, len);
564 sftp_pkt_getstring(pktin, &str, &len);
565 ret->names[i].longname = mkstr(str, len);
566 ret->names[i].attrs = sftp_pkt_getattrs(pktin);
570 fxp_got_status(pktin);
576 * Write to a file. Returns 0 on error, 1 on OK.
578 int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
579 struct sftp_packet *pktin, *pktout;
582 pktout = sftp_pkt_init(SSH_FXP_WRITE);
583 sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
584 sftp_pkt_addstring_start(pktout);
585 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
586 sftp_pkt_adduint64(pktout, offset);
587 sftp_pkt_addstring_start(pktout);
588 sftp_pkt_addstring_data(pktout, buffer, len);
591 id = sftp_pkt_getuint32(pktin);
592 fxp_got_status(pktin);
593 return fxp_errtype == SSH_FX_OK;
597 * Free up an fxp_names structure.
599 void fxp_free_names(struct fxp_names *names) {
602 for (i = 0; i < names->nnames; i++) {
603 sfree(names->names[i].filename);
604 sfree(names->names[i].longname);