2 * sftp.c: SFTP generic client code.
14 #define GET_32BIT(cp) \
15 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
16 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
17 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
18 ((unsigned long)(unsigned char)(cp)[3]))
20 #define PUT_32BIT(cp, value) { \
21 (cp)[0] = (unsigned char)((value) >> 24); \
22 (cp)[1] = (unsigned char)((value) >> 16); \
23 (cp)[2] = (unsigned char)((value) >> 8); \
24 (cp)[3] = (unsigned char)(value); }
33 static const char *fxp_error_message;
34 static int fxp_errtype;
36 /* ----------------------------------------------------------------------
37 * SFTP packet construction functions.
39 static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
41 if (pkt->maxlen < length) {
42 pkt->maxlen = length + 256;
43 pkt->data = srealloc(pkt->data, pkt->maxlen);
46 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
49 sftp_pkt_ensure(pkt, pkt->length);
50 memcpy(pkt->data + pkt->length - len, data, len);
52 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
54 sftp_pkt_adddata(pkt, &byte, 1);
56 static struct sftp_packet *sftp_pkt_init(int pkt_type)
58 struct sftp_packet *pkt;
59 pkt = smalloc(sizeof(struct sftp_packet));
64 sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
65 fxp_error_message = NULL;
68 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
70 sftp_pkt_adddata(pkt, &value, 1);
72 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
77 sftp_pkt_adddata(pkt, x, 4);
79 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
82 PUT_32BIT(x, value.hi);
83 PUT_32BIT(x + 4, value.lo);
84 sftp_pkt_adddata(pkt, x, 8);
86 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
88 sftp_pkt_adduint32(pkt, 0);
89 pkt->savedpos = pkt->length;
91 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
93 sftp_pkt_adddata(pkt, data, strlen(data));
94 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
96 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
99 sftp_pkt_adddata(pkt, data, len);
100 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
102 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
104 sftp_pkt_addstring_start(pkt);
105 sftp_pkt_addstring_str(pkt, data);
107 static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
109 sftp_pkt_adduint32(pkt, attrs.flags);
110 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
111 sftp_pkt_adduint32(pkt, attrs.size.hi);
112 sftp_pkt_adduint32(pkt, attrs.size.lo);
114 if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
115 sftp_pkt_adduint32(pkt, attrs.uid);
116 sftp_pkt_adduint32(pkt, attrs.gid);
118 if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
119 sftp_pkt_adduint32(pkt, attrs.permissions);
121 if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
122 sftp_pkt_adduint32(pkt, attrs.atime);
123 sftp_pkt_adduint32(pkt, attrs.mtime);
125 if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
127 * We currently don't support sending any extended
133 /* ----------------------------------------------------------------------
134 * SFTP packet decode functions.
137 static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
140 if (pkt->length - pkt->savedpos < 1)
141 return 0; /* arrgh, no way to decline (FIXME?) */
142 value = (unsigned char) pkt->data[pkt->savedpos];
146 static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
149 if (pkt->length - pkt->savedpos < 4)
150 return 0; /* arrgh, no way to decline (FIXME?) */
151 value = GET_32BIT(pkt->data + pkt->savedpos);
155 static void sftp_pkt_getstring(struct sftp_packet *pkt,
156 char **p, int *length)
159 if (pkt->length - pkt->savedpos < 4)
161 *length = GET_32BIT(pkt->data + pkt->savedpos);
163 if (pkt->length - pkt->savedpos < *length)
165 *p = pkt->data + pkt->savedpos;
166 pkt->savedpos += *length;
168 static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
170 struct fxp_attrs ret;
171 ret.flags = sftp_pkt_getuint32(pkt);
172 if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
173 unsigned long hi, lo;
174 hi = sftp_pkt_getuint32(pkt);
175 lo = sftp_pkt_getuint32(pkt);
176 ret.size = uint64_make(hi, lo);
178 if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
179 ret.uid = sftp_pkt_getuint32(pkt);
180 ret.gid = sftp_pkt_getuint32(pkt);
182 if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
183 ret.permissions = sftp_pkt_getuint32(pkt);
185 if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
186 ret.atime = sftp_pkt_getuint32(pkt);
187 ret.mtime = sftp_pkt_getuint32(pkt);
189 if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
191 count = sftp_pkt_getuint32(pkt);
196 * We should try to analyse these, if we ever find one
199 sftp_pkt_getstring(pkt, &str, &len);
200 sftp_pkt_getstring(pkt, &str, &len);
205 static void sftp_pkt_free(struct sftp_packet *pkt)
212 /* ----------------------------------------------------------------------
213 * Send and receive packet functions.
215 int sftp_send(struct sftp_packet *pkt)
219 PUT_32BIT(x, pkt->length);
220 ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
224 struct sftp_packet *sftp_recv(void)
226 struct sftp_packet *pkt;
229 if (!sftp_recvdata(x, 4))
232 pkt = smalloc(sizeof(struct sftp_packet));
234 pkt->length = pkt->maxlen = GET_32BIT(x);
235 pkt->data = smalloc(pkt->length);
237 if (!sftp_recvdata(pkt->data, pkt->length)) {
242 pkt->type = sftp_pkt_getbyte(pkt);
247 /* ----------------------------------------------------------------------
248 * String handling routines.
251 static char *mkstr(char *s, int len)
253 char *p = smalloc(len + 1);
259 /* ----------------------------------------------------------------------
264 * Deal with (and free) an FXP_STATUS packet. Return 1 if
265 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
266 * Also place the status into fxp_errtype.
268 static int fxp_got_status(struct sftp_packet *pktin)
270 static const char *const messages[] = {
271 /* SSH_FX_OK. The only time we will display a _message_ for this
272 * is if we were expecting something other than FXP_STATUS on
273 * success, so this is actually an error message! */
274 "unexpected OK response",
276 "no such file or directory",
282 "operation unsupported",
285 if (pktin->type != SSH_FXP_STATUS) {
286 fxp_error_message = "expected FXP_STATUS packet";
289 fxp_errtype = sftp_pkt_getuint32(pktin);
290 if (fxp_errtype < 0 ||
291 fxp_errtype >= sizeof(messages) / sizeof(*messages))
292 fxp_error_message = "unknown error code";
294 fxp_error_message = messages[fxp_errtype];
297 if (fxp_errtype == SSH_FX_OK)
299 else if (fxp_errtype == SSH_FX_EOF)
305 static void fxp_internal_error(char *msg)
307 fxp_error_message = msg;
311 const char *fxp_error(void)
313 return fxp_error_message;
316 int fxp_error_type(void)
322 * Perform exchange of init/version packets. Return 0 on failure.
326 struct sftp_packet *pktout, *pktin;
329 pktout = sftp_pkt_init(SSH_FXP_INIT);
330 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
335 fxp_internal_error("could not connect");
338 if (pktin->type != SSH_FXP_VERSION) {
339 fxp_internal_error("did not receive FXP_VERSION");
342 remotever = sftp_pkt_getuint32(pktin);
343 if (remotever > SFTP_PROTO_VERSION) {
345 ("remote protocol is more advanced than we support");
349 * In principle, this packet might also contain extension-
350 * string pairs. We should work through them and look for any
351 * we recognise. In practice we don't currently do so because
352 * we know we don't recognise _any_.
354 sftp_pkt_free(pktin);
360 * Canonify a pathname.
362 char *fxp_realpath(char *path)
364 struct sftp_packet *pktin, *pktout;
367 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
368 sftp_pkt_adduint32(pktout, 0x123); /* request id */
369 sftp_pkt_addstring_start(pktout);
370 sftp_pkt_addstring_str(pktout, path);
373 id = sftp_pkt_getuint32(pktin);
375 fxp_internal_error("request ID mismatch\n");
378 if (pktin->type == SSH_FXP_NAME) {
383 count = sftp_pkt_getuint32(pktin);
385 fxp_internal_error("REALPATH returned name count != 1\n");
388 sftp_pkt_getstring(pktin, &path, &len);
390 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
393 path = mkstr(path, len);
394 sftp_pkt_free(pktin);
397 fxp_got_status(pktin);
405 struct fxp_handle *fxp_open(char *path, int type)
407 struct sftp_packet *pktin, *pktout;
410 pktout = sftp_pkt_init(SSH_FXP_OPEN);
411 sftp_pkt_adduint32(pktout, 0x567); /* request id */
412 sftp_pkt_addstring(pktout, path);
413 sftp_pkt_adduint32(pktout, type);
414 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
417 id = sftp_pkt_getuint32(pktin);
419 fxp_internal_error("request ID mismatch\n");
422 if (pktin->type == SSH_FXP_HANDLE) {
424 struct fxp_handle *handle;
427 sftp_pkt_getstring(pktin, &hstring, &len);
429 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
432 handle = smalloc(sizeof(struct fxp_handle));
433 handle->hstring = mkstr(hstring, len);
435 sftp_pkt_free(pktin);
438 fxp_got_status(pktin);
446 struct fxp_handle *fxp_opendir(char *path)
448 struct sftp_packet *pktin, *pktout;
451 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
452 sftp_pkt_adduint32(pktout, 0x456); /* request id */
453 sftp_pkt_addstring(pktout, path);
456 id = sftp_pkt_getuint32(pktin);
458 fxp_internal_error("request ID mismatch\n");
461 if (pktin->type == SSH_FXP_HANDLE) {
463 struct fxp_handle *handle;
466 sftp_pkt_getstring(pktin, &hstring, &len);
468 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
471 handle = smalloc(sizeof(struct fxp_handle));
472 handle->hstring = mkstr(hstring, len);
474 sftp_pkt_free(pktin);
477 fxp_got_status(pktin);
485 void fxp_close(struct fxp_handle *handle)
487 struct sftp_packet *pktin, *pktout;
490 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
491 sftp_pkt_adduint32(pktout, 0x789); /* request id */
492 sftp_pkt_addstring_start(pktout);
493 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
496 id = sftp_pkt_getuint32(pktin);
498 fxp_internal_error("request ID mismatch\n");
501 fxp_got_status(pktin);
502 sfree(handle->hstring);
506 int fxp_mkdir(char *path)
508 struct sftp_packet *pktin, *pktout;
511 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
512 sftp_pkt_adduint32(pktout, 0x234); /* request id */
513 sftp_pkt_addstring(pktout, path);
514 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
517 id = sftp_pkt_getuint32(pktin);
519 fxp_internal_error("request ID mismatch\n");
522 id = fxp_got_status(pktin);
529 int fxp_rmdir(char *path)
531 struct sftp_packet *pktin, *pktout;
534 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
535 sftp_pkt_adduint32(pktout, 0x345); /* request id */
536 sftp_pkt_addstring(pktout, path);
539 id = sftp_pkt_getuint32(pktin);
541 fxp_internal_error("request ID mismatch\n");
544 id = fxp_got_status(pktin);
551 int fxp_remove(char *fname)
553 struct sftp_packet *pktin, *pktout;
556 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
557 sftp_pkt_adduint32(pktout, 0x678); /* request id */
558 sftp_pkt_addstring(pktout, fname);
561 id = sftp_pkt_getuint32(pktin);
563 fxp_internal_error("request ID mismatch\n");
566 id = fxp_got_status(pktin);
573 int fxp_rename(char *srcfname, char *dstfname)
575 struct sftp_packet *pktin, *pktout;
578 pktout = sftp_pkt_init(SSH_FXP_RENAME);
579 sftp_pkt_adduint32(pktout, 0x678); /* request id */
580 sftp_pkt_addstring(pktout, srcfname);
581 sftp_pkt_addstring(pktout, dstfname);
584 id = sftp_pkt_getuint32(pktin);
586 fxp_internal_error("request ID mismatch\n");
589 id = fxp_got_status(pktin);
597 * Retrieve the attributes of a file. We have fxp_stat which works
598 * on filenames, and fxp_fstat which works on open file handles.
600 int fxp_stat(char *fname, struct fxp_attrs *attrs)
602 struct sftp_packet *pktin, *pktout;
605 pktout = sftp_pkt_init(SSH_FXP_STAT);
606 sftp_pkt_adduint32(pktout, 0x678); /* request id */
607 sftp_pkt_addstring(pktout, fname);
610 id = sftp_pkt_getuint32(pktin);
612 fxp_internal_error("request ID mismatch\n");
616 if (pktin->type == SSH_FXP_ATTRS) {
617 *attrs = sftp_pkt_getattrs(pktin);
620 fxp_got_status(pktin);
625 int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs)
627 struct sftp_packet *pktin, *pktout;
630 pktout = sftp_pkt_init(SSH_FXP_FSTAT);
631 sftp_pkt_adduint32(pktout, 0x678); /* request id */
632 sftp_pkt_addstring_start(pktout);
633 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
636 id = sftp_pkt_getuint32(pktin);
638 fxp_internal_error("request ID mismatch\n");
642 if (pktin->type == SSH_FXP_ATTRS) {
643 *attrs = sftp_pkt_getattrs(pktin);
646 fxp_got_status(pktin);
652 * Set the attributes of a file.
654 int fxp_setstat(char *fname, struct fxp_attrs attrs)
656 struct sftp_packet *pktin, *pktout;
659 pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
660 sftp_pkt_adduint32(pktout, 0x678); /* request id */
661 sftp_pkt_addstring(pktout, fname);
662 sftp_pkt_addattrs(pktout, attrs);
665 id = sftp_pkt_getuint32(pktin);
667 fxp_internal_error("request ID mismatch\n");
670 id = fxp_got_status(pktin);
676 int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs)
678 struct sftp_packet *pktin, *pktout;
681 pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
682 sftp_pkt_adduint32(pktout, 0x678); /* request id */
683 sftp_pkt_addstring_start(pktout);
684 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
685 sftp_pkt_addattrs(pktout, attrs);
688 id = sftp_pkt_getuint32(pktin);
690 fxp_internal_error("request ID mismatch\n");
693 id = fxp_got_status(pktin);
701 * Read from a file. Returns the number of bytes read, or -1 on an
702 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
703 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
704 * error indicator. It might even depend on the SFTP server.)
706 int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
709 struct sftp_packet *pktin, *pktout;
712 pktout = sftp_pkt_init(SSH_FXP_READ);
713 sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
714 sftp_pkt_addstring_start(pktout);
715 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
716 sftp_pkt_adduint64(pktout, offset);
717 sftp_pkt_adduint32(pktout, len);
720 id = sftp_pkt_getuint32(pktin);
722 fxp_internal_error("request ID mismatch");
725 if (pktin->type == SSH_FXP_DATA) {
729 sftp_pkt_getstring(pktin, &str, &rlen);
731 if (rlen > len || rlen < 0) {
732 fxp_internal_error("READ returned more bytes than requested");
736 memcpy(buffer, str, rlen);
740 fxp_got_status(pktin);
746 * Read from a directory.
748 struct fxp_names *fxp_readdir(struct fxp_handle *handle)
750 struct sftp_packet *pktin, *pktout;
753 pktout = sftp_pkt_init(SSH_FXP_READDIR);
754 sftp_pkt_adduint32(pktout, 0xABC); /* request id */
755 sftp_pkt_addstring_start(pktout);
756 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
759 id = sftp_pkt_getuint32(pktin);
761 fxp_internal_error("request ID mismatch\n");
764 if (pktin->type == SSH_FXP_NAME) {
765 struct fxp_names *ret;
767 ret = smalloc(sizeof(struct fxp_names));
768 ret->nnames = sftp_pkt_getuint32(pktin);
769 ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
770 for (i = 0; i < ret->nnames; i++) {
773 sftp_pkt_getstring(pktin, &str, &len);
774 ret->names[i].filename = mkstr(str, len);
775 sftp_pkt_getstring(pktin, &str, &len);
776 ret->names[i].longname = mkstr(str, len);
777 ret->names[i].attrs = sftp_pkt_getattrs(pktin);
781 fxp_got_status(pktin);
787 * Write to a file. Returns 0 on error, 1 on OK.
789 int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
792 struct sftp_packet *pktin, *pktout;
795 pktout = sftp_pkt_init(SSH_FXP_WRITE);
796 sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
797 sftp_pkt_addstring_start(pktout);
798 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
799 sftp_pkt_adduint64(pktout, offset);
800 sftp_pkt_addstring_start(pktout);
801 sftp_pkt_addstring_data(pktout, buffer, len);
804 id = sftp_pkt_getuint32(pktin);
806 fxp_internal_error("request ID mismatch\n");
809 fxp_got_status(pktin);
810 return fxp_errtype == SSH_FX_OK;
814 * Free up an fxp_names structure.
816 void fxp_free_names(struct fxp_names *names)
820 for (i = 0; i < names->nnames; i++) {
821 sfree(names->names[i].filename);
822 sfree(names->names[i].longname);