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 static const char *fxp_error_message;
37 static int fxp_errtype;
39 /* ----------------------------------------------------------------------
40 * SFTP packet construction functions.
42 static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
44 if (pkt->maxlen < length) {
45 pkt->maxlen = length + 256;
46 pkt->data = srealloc(pkt->data, pkt->maxlen);
49 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
52 sftp_pkt_ensure(pkt, pkt->length);
53 memcpy(pkt->data + pkt->length - len, data, len);
55 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
57 sftp_pkt_adddata(pkt, &byte, 1);
59 static struct sftp_packet *sftp_pkt_init(int pkt_type)
61 struct sftp_packet *pkt;
62 pkt = smalloc(sizeof(struct sftp_packet));
67 sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
68 fxp_error_message = NULL;
71 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
73 sftp_pkt_adddata(pkt, &value, 1);
75 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
80 sftp_pkt_adddata(pkt, x, 4);
82 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
85 PUT_32BIT(x, value.hi);
86 PUT_32BIT(x + 4, value.lo);
87 sftp_pkt_adddata(pkt, x, 8);
89 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
91 sftp_pkt_adduint32(pkt, 0);
92 pkt->savedpos = pkt->length;
94 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
96 sftp_pkt_adddata(pkt, data, strlen(data));
97 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
99 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
102 sftp_pkt_adddata(pkt, data, len);
103 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
105 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
107 sftp_pkt_addstring_start(pkt);
108 sftp_pkt_addstring_str(pkt, data);
110 static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
112 sftp_pkt_adduint32(pkt, attrs.flags);
113 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
114 sftp_pkt_adduint32(pkt, attrs.size.hi);
115 sftp_pkt_adduint32(pkt, attrs.size.lo);
117 if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
118 sftp_pkt_adduint32(pkt, attrs.uid);
119 sftp_pkt_adduint32(pkt, attrs.gid);
121 if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
122 sftp_pkt_adduint32(pkt, attrs.permissions);
124 if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
125 sftp_pkt_adduint32(pkt, attrs.atime);
126 sftp_pkt_adduint32(pkt, attrs.mtime);
128 if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
130 * We currently don't support sending any extended
136 /* ----------------------------------------------------------------------
137 * SFTP packet decode functions.
140 static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
143 if (pkt->length - pkt->savedpos < 1)
144 return 0; /* arrgh, no way to decline (FIXME?) */
145 value = (unsigned char) pkt->data[pkt->savedpos];
149 static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
152 if (pkt->length - pkt->savedpos < 4)
153 return 0; /* arrgh, no way to decline (FIXME?) */
154 value = GET_32BIT(pkt->data + pkt->savedpos);
158 static void sftp_pkt_getstring(struct sftp_packet *pkt,
159 char **p, int *length)
162 if (pkt->length - pkt->savedpos < 4)
164 *length = GET_32BIT(pkt->data + pkt->savedpos);
166 if (pkt->length - pkt->savedpos < *length)
168 *p = pkt->data + pkt->savedpos;
169 pkt->savedpos += *length;
171 static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
173 struct fxp_attrs ret;
174 ret.flags = sftp_pkt_getuint32(pkt);
175 if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
176 unsigned long hi, lo;
177 hi = sftp_pkt_getuint32(pkt);
178 lo = sftp_pkt_getuint32(pkt);
179 ret.size = uint64_make(hi, lo);
181 if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
182 ret.uid = sftp_pkt_getuint32(pkt);
183 ret.gid = sftp_pkt_getuint32(pkt);
185 if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
186 ret.permissions = sftp_pkt_getuint32(pkt);
188 if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
189 ret.atime = sftp_pkt_getuint32(pkt);
190 ret.mtime = sftp_pkt_getuint32(pkt);
192 if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
194 count = sftp_pkt_getuint32(pkt);
199 * We should try to analyse these, if we ever find one
202 sftp_pkt_getstring(pkt, &str, &len);
203 sftp_pkt_getstring(pkt, &str, &len);
208 static void sftp_pkt_free(struct sftp_packet *pkt)
215 /* ----------------------------------------------------------------------
216 * Send and receive packet functions.
218 int sftp_send(struct sftp_packet *pkt)
222 PUT_32BIT(x, pkt->length);
223 ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
227 struct sftp_packet *sftp_recv(void)
229 struct sftp_packet *pkt;
232 if (!sftp_recvdata(x, 4))
235 pkt = smalloc(sizeof(struct sftp_packet));
237 pkt->length = pkt->maxlen = GET_32BIT(x);
238 pkt->data = smalloc(pkt->length);
240 if (!sftp_recvdata(pkt->data, pkt->length)) {
245 pkt->type = sftp_pkt_getbyte(pkt);
250 /* ----------------------------------------------------------------------
251 * String handling routines.
254 static char *mkstr(char *s, int len)
256 char *p = smalloc(len + 1);
262 /* ----------------------------------------------------------------------
267 * Deal with (and free) an FXP_STATUS packet. Return 1 if
268 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
269 * Also place the status into fxp_errtype.
271 static int fxp_got_status(struct sftp_packet *pktin)
273 static const char *const messages[] = {
274 /* SSH_FX_OK. The only time we will display a _message_ for this
275 * is if we were expecting something other than FXP_STATUS on
276 * success, so this is actually an error message! */
277 "unexpected OK response",
279 "no such file or directory",
285 "operation unsupported",
288 if (pktin->type != SSH_FXP_STATUS) {
289 fxp_error_message = "expected FXP_STATUS packet";
292 fxp_errtype = sftp_pkt_getuint32(pktin);
293 if (fxp_errtype < 0 ||
294 fxp_errtype >= sizeof(messages) / sizeof(*messages))
295 fxp_error_message = "unknown error code";
297 fxp_error_message = messages[fxp_errtype];
300 if (fxp_errtype == SSH_FX_OK)
302 else if (fxp_errtype == SSH_FX_EOF)
308 static void fxp_internal_error(char *msg)
310 fxp_error_message = msg;
314 const char *fxp_error(void)
316 return fxp_error_message;
319 int fxp_error_type(void)
325 * Perform exchange of init/version packets. Return 0 on failure.
329 struct sftp_packet *pktout, *pktin;
332 pktout = sftp_pkt_init(SSH_FXP_INIT);
333 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
338 fxp_internal_error("could not connect");
341 if (pktin->type != SSH_FXP_VERSION) {
342 fxp_internal_error("did not receive FXP_VERSION");
345 remotever = sftp_pkt_getuint32(pktin);
346 if (remotever > SFTP_PROTO_VERSION) {
348 ("remote protocol is more advanced than we support");
352 * In principle, this packet might also contain extension-
353 * string pairs. We should work through them and look for any
354 * we recognise. In practice we don't currently do so because
355 * we know we don't recognise _any_.
357 sftp_pkt_free(pktin);
363 * Canonify a pathname.
365 char *fxp_realpath(char *path)
367 struct sftp_packet *pktin, *pktout;
370 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
371 sftp_pkt_adduint32(pktout, 0x123); /* request id */
372 sftp_pkt_addstring_start(pktout);
373 sftp_pkt_addstring_str(pktout, path);
376 id = sftp_pkt_getuint32(pktin);
378 fxp_internal_error("request ID mismatch\n");
381 if (pktin->type == SSH_FXP_NAME) {
386 count = sftp_pkt_getuint32(pktin);
388 fxp_internal_error("REALPATH returned name count != 1\n");
391 sftp_pkt_getstring(pktin, &path, &len);
393 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
396 path = mkstr(path, len);
397 sftp_pkt_free(pktin);
400 fxp_got_status(pktin);
408 struct fxp_handle *fxp_open(char *path, int type)
410 struct sftp_packet *pktin, *pktout;
413 pktout = sftp_pkt_init(SSH_FXP_OPEN);
414 sftp_pkt_adduint32(pktout, 0x567); /* request id */
415 sftp_pkt_addstring(pktout, path);
416 sftp_pkt_adduint32(pktout, type);
417 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
420 id = sftp_pkt_getuint32(pktin);
422 fxp_internal_error("request ID mismatch\n");
425 if (pktin->type == SSH_FXP_HANDLE) {
427 struct fxp_handle *handle;
430 sftp_pkt_getstring(pktin, &hstring, &len);
432 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
435 handle = smalloc(sizeof(struct fxp_handle));
436 handle->hstring = mkstr(hstring, len);
438 sftp_pkt_free(pktin);
441 fxp_got_status(pktin);
449 struct fxp_handle *fxp_opendir(char *path)
451 struct sftp_packet *pktin, *pktout;
454 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
455 sftp_pkt_adduint32(pktout, 0x456); /* request id */
456 sftp_pkt_addstring(pktout, path);
459 id = sftp_pkt_getuint32(pktin);
461 fxp_internal_error("request ID mismatch\n");
464 if (pktin->type == SSH_FXP_HANDLE) {
466 struct fxp_handle *handle;
469 sftp_pkt_getstring(pktin, &hstring, &len);
471 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
474 handle = smalloc(sizeof(struct fxp_handle));
475 handle->hstring = mkstr(hstring, len);
477 sftp_pkt_free(pktin);
480 fxp_got_status(pktin);
488 void fxp_close(struct fxp_handle *handle)
490 struct sftp_packet *pktin, *pktout;
493 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
494 sftp_pkt_adduint32(pktout, 0x789); /* request id */
495 sftp_pkt_addstring_start(pktout);
496 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
499 id = sftp_pkt_getuint32(pktin);
501 fxp_internal_error("request ID mismatch\n");
504 fxp_got_status(pktin);
505 sfree(handle->hstring);
509 int fxp_mkdir(char *path)
511 struct sftp_packet *pktin, *pktout;
514 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
515 sftp_pkt_adduint32(pktout, 0x234); /* request id */
516 sftp_pkt_addstring(pktout, path);
517 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
520 id = sftp_pkt_getuint32(pktin);
522 fxp_internal_error("request ID mismatch\n");
525 id = fxp_got_status(pktin);
532 int fxp_rmdir(char *path)
534 struct sftp_packet *pktin, *pktout;
537 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
538 sftp_pkt_adduint32(pktout, 0x345); /* request id */
539 sftp_pkt_addstring(pktout, path);
542 id = sftp_pkt_getuint32(pktin);
544 fxp_internal_error("request ID mismatch\n");
547 id = fxp_got_status(pktin);
554 int fxp_remove(char *fname)
556 struct sftp_packet *pktin, *pktout;
559 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
560 sftp_pkt_adduint32(pktout, 0x678); /* request id */
561 sftp_pkt_addstring(pktout, fname);
564 id = sftp_pkt_getuint32(pktin);
566 fxp_internal_error("request ID mismatch\n");
569 id = fxp_got_status(pktin);
576 int fxp_rename(char *srcfname, char *dstfname)
578 struct sftp_packet *pktin, *pktout;
581 pktout = sftp_pkt_init(SSH_FXP_RENAME);
582 sftp_pkt_adduint32(pktout, 0x678); /* request id */
583 sftp_pkt_addstring(pktout, srcfname);
584 sftp_pkt_addstring(pktout, dstfname);
587 id = sftp_pkt_getuint32(pktin);
589 fxp_internal_error("request ID mismatch\n");
592 id = fxp_got_status(pktin);
600 * Retrieve the attributes of a file. We have fxp_stat which works
601 * on filenames, and fxp_fstat which works on open file handles.
603 int fxp_stat(char *fname, struct fxp_attrs *attrs)
605 struct sftp_packet *pktin, *pktout;
608 pktout = sftp_pkt_init(SSH_FXP_STAT);
609 sftp_pkt_adduint32(pktout, 0x678); /* request id */
610 sftp_pkt_addstring(pktout, fname);
613 id = sftp_pkt_getuint32(pktin);
615 fxp_internal_error("request ID mismatch\n");
619 if (pktin->type == SSH_FXP_ATTRS) {
620 *attrs = sftp_pkt_getattrs(pktin);
623 fxp_got_status(pktin);
628 int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs)
630 struct sftp_packet *pktin, *pktout;
633 pktout = sftp_pkt_init(SSH_FXP_FSTAT);
634 sftp_pkt_adduint32(pktout, 0x678); /* request id */
635 sftp_pkt_addstring_start(pktout);
636 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
639 id = sftp_pkt_getuint32(pktin);
641 fxp_internal_error("request ID mismatch\n");
645 if (pktin->type == SSH_FXP_ATTRS) {
646 *attrs = sftp_pkt_getattrs(pktin);
649 fxp_got_status(pktin);
655 * Set the attributes of a file.
657 int fxp_setstat(char *fname, struct fxp_attrs attrs)
659 struct sftp_packet *pktin, *pktout;
662 pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
663 sftp_pkt_adduint32(pktout, 0x678); /* request id */
664 sftp_pkt_addstring(pktout, fname);
665 sftp_pkt_addattrs(pktout, attrs);
668 id = sftp_pkt_getuint32(pktin);
670 fxp_internal_error("request ID mismatch\n");
673 id = fxp_got_status(pktin);
679 int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs)
681 struct sftp_packet *pktin, *pktout;
684 pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
685 sftp_pkt_adduint32(pktout, 0x678); /* request id */
686 sftp_pkt_addstring_start(pktout);
687 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
688 sftp_pkt_addattrs(pktout, attrs);
691 id = sftp_pkt_getuint32(pktin);
693 fxp_internal_error("request ID mismatch\n");
696 id = fxp_got_status(pktin);
704 * Read from a file. Returns the number of bytes read, or -1 on an
705 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
706 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
707 * error indicator. It might even depend on the SFTP server.)
709 int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
712 struct sftp_packet *pktin, *pktout;
715 pktout = sftp_pkt_init(SSH_FXP_READ);
716 sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
717 sftp_pkt_addstring_start(pktout);
718 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
719 sftp_pkt_adduint64(pktout, offset);
720 sftp_pkt_adduint32(pktout, len);
723 id = sftp_pkt_getuint32(pktin);
725 fxp_internal_error("request ID mismatch");
728 if (pktin->type == SSH_FXP_DATA) {
732 sftp_pkt_getstring(pktin, &str, &rlen);
734 if (rlen > len || rlen < 0) {
735 fxp_internal_error("READ returned more bytes than requested");
739 memcpy(buffer, str, rlen);
743 fxp_got_status(pktin);
749 * Read from a directory.
751 struct fxp_names *fxp_readdir(struct fxp_handle *handle)
753 struct sftp_packet *pktin, *pktout;
756 pktout = sftp_pkt_init(SSH_FXP_READDIR);
757 sftp_pkt_adduint32(pktout, 0xABC); /* request id */
758 sftp_pkt_addstring_start(pktout);
759 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
762 id = sftp_pkt_getuint32(pktin);
764 fxp_internal_error("request ID mismatch\n");
767 if (pktin->type == SSH_FXP_NAME) {
768 struct fxp_names *ret;
770 ret = smalloc(sizeof(struct fxp_names));
771 ret->nnames = sftp_pkt_getuint32(pktin);
772 ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
773 for (i = 0; i < ret->nnames; i++) {
776 sftp_pkt_getstring(pktin, &str, &len);
777 ret->names[i].filename = mkstr(str, len);
778 sftp_pkt_getstring(pktin, &str, &len);
779 ret->names[i].longname = mkstr(str, len);
780 ret->names[i].attrs = sftp_pkt_getattrs(pktin);
784 fxp_got_status(pktin);
790 * Write to a file. Returns 0 on error, 1 on OK.
792 int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
795 struct sftp_packet *pktin, *pktout;
798 pktout = sftp_pkt_init(SSH_FXP_WRITE);
799 sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
800 sftp_pkt_addstring_start(pktout);
801 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
802 sftp_pkt_adduint64(pktout, offset);
803 sftp_pkt_addstring_start(pktout);
804 sftp_pkt_addstring_data(pktout, buffer, len);
807 id = sftp_pkt_getuint32(pktin);
809 fxp_internal_error("request ID mismatch\n");
812 fxp_got_status(pktin);
813 return fxp_errtype == SSH_FX_OK;
817 * Free up an fxp_names structure.
819 void fxp_free_names(struct fxp_names *names)
823 for (i = 0; i < names->nnames; i++) {
824 sfree(names->names[i].filename);
825 sfree(names->names[i].longname);