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);
111 /* ----------------------------------------------------------------------
112 * SFTP packet decode functions.
115 static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
118 if (pkt->length - pkt->savedpos < 1)
119 return 0; /* arrgh, no way to decline (FIXME?) */
120 value = (unsigned char) pkt->data[pkt->savedpos];
124 static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
127 if (pkt->length - pkt->savedpos < 4)
128 return 0; /* arrgh, no way to decline (FIXME?) */
129 value = GET_32BIT(pkt->data + pkt->savedpos);
133 static void sftp_pkt_getstring(struct sftp_packet *pkt,
134 char **p, int *length)
137 if (pkt->length - pkt->savedpos < 4)
139 *length = GET_32BIT(pkt->data + pkt->savedpos);
141 if (pkt->length - pkt->savedpos < *length)
143 *p = pkt->data + pkt->savedpos;
144 pkt->savedpos += *length;
146 static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
148 struct fxp_attrs ret;
149 ret.flags = sftp_pkt_getuint32(pkt);
150 if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
151 unsigned long hi, lo;
152 hi = sftp_pkt_getuint32(pkt);
153 lo = sftp_pkt_getuint32(pkt);
154 ret.size = uint64_make(hi, lo);
156 if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
157 ret.uid = sftp_pkt_getuint32(pkt);
158 ret.gid = sftp_pkt_getuint32(pkt);
160 if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
161 ret.permissions = sftp_pkt_getuint32(pkt);
163 if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
164 ret.atime = sftp_pkt_getuint32(pkt);
165 ret.mtime = sftp_pkt_getuint32(pkt);
167 if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
169 count = sftp_pkt_getuint32(pkt);
174 * We should try to analyse these, if we ever find one
177 sftp_pkt_getstring(pkt, &str, &len);
178 sftp_pkt_getstring(pkt, &str, &len);
183 static void sftp_pkt_free(struct sftp_packet *pkt)
190 /* ----------------------------------------------------------------------
191 * Send and receive packet functions.
193 int sftp_send(struct sftp_packet *pkt)
197 PUT_32BIT(x, pkt->length);
198 ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
202 struct sftp_packet *sftp_recv(void)
204 struct sftp_packet *pkt;
207 if (!sftp_recvdata(x, 4))
210 pkt = smalloc(sizeof(struct sftp_packet));
212 pkt->length = pkt->maxlen = GET_32BIT(x);
213 pkt->data = smalloc(pkt->length);
215 if (!sftp_recvdata(pkt->data, pkt->length)) {
220 pkt->type = sftp_pkt_getbyte(pkt);
225 /* ----------------------------------------------------------------------
226 * String handling routines.
229 static char *mkstr(char *s, int len)
231 char *p = smalloc(len + 1);
237 /* ----------------------------------------------------------------------
242 * Deal with (and free) an FXP_STATUS packet. Return 1 if
243 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
244 * Also place the status into fxp_errtype.
246 static int fxp_got_status(struct sftp_packet *pktin)
248 static const char *const messages[] = {
249 /* SSH_FX_OK. The only time we will display a _message_ for this
250 * is if we were expecting something other than FXP_STATUS on
251 * success, so this is actually an error message! */
252 "unexpected OK response",
254 "no such file or directory",
260 "operation unsupported",
263 if (pktin->type != SSH_FXP_STATUS) {
264 fxp_error_message = "expected FXP_STATUS packet";
267 fxp_errtype = sftp_pkt_getuint32(pktin);
268 if (fxp_errtype < 0 ||
269 fxp_errtype >= sizeof(messages) / sizeof(*messages))
270 fxp_error_message = "unknown error code";
272 fxp_error_message = messages[fxp_errtype];
275 if (fxp_errtype == SSH_FX_OK)
277 else if (fxp_errtype == SSH_FX_EOF)
283 static void fxp_internal_error(char *msg)
285 fxp_error_message = msg;
289 const char *fxp_error(void)
291 return fxp_error_message;
294 int fxp_error_type(void)
300 * Perform exchange of init/version packets. Return 0 on failure.
304 struct sftp_packet *pktout, *pktin;
307 pktout = sftp_pkt_init(SSH_FXP_INIT);
308 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
313 fxp_internal_error("could not connect");
316 if (pktin->type != SSH_FXP_VERSION) {
317 fxp_internal_error("did not receive FXP_VERSION");
320 remotever = sftp_pkt_getuint32(pktin);
321 if (remotever > SFTP_PROTO_VERSION) {
323 ("remote protocol is more advanced than we support");
327 * In principle, this packet might also contain extension-
328 * string pairs. We should work through them and look for any
329 * we recognise. In practice we don't currently do so because
330 * we know we don't recognise _any_.
332 sftp_pkt_free(pktin);
338 * Canonify a pathname.
340 char *fxp_realpath(char *path)
342 struct sftp_packet *pktin, *pktout;
345 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
346 sftp_pkt_adduint32(pktout, 0x123); /* request id */
347 sftp_pkt_addstring_start(pktout);
348 sftp_pkt_addstring_str(pktout, path);
351 id = sftp_pkt_getuint32(pktin);
353 fxp_internal_error("request ID mismatch\n");
356 if (pktin->type == SSH_FXP_NAME) {
361 count = sftp_pkt_getuint32(pktin);
363 fxp_internal_error("REALPATH returned name count != 1\n");
366 sftp_pkt_getstring(pktin, &path, &len);
368 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
371 path = mkstr(path, len);
372 sftp_pkt_free(pktin);
375 fxp_got_status(pktin);
383 struct fxp_handle *fxp_open(char *path, int type)
385 struct sftp_packet *pktin, *pktout;
388 pktout = sftp_pkt_init(SSH_FXP_OPEN);
389 sftp_pkt_adduint32(pktout, 0x567); /* request id */
390 sftp_pkt_addstring(pktout, path);
391 sftp_pkt_adduint32(pktout, type);
392 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
395 id = sftp_pkt_getuint32(pktin);
397 fxp_internal_error("request ID mismatch\n");
400 if (pktin->type == SSH_FXP_HANDLE) {
402 struct fxp_handle *handle;
405 sftp_pkt_getstring(pktin, &hstring, &len);
407 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
410 handle = smalloc(sizeof(struct fxp_handle));
411 handle->hstring = mkstr(hstring, len);
413 sftp_pkt_free(pktin);
416 fxp_got_status(pktin);
424 struct fxp_handle *fxp_opendir(char *path)
426 struct sftp_packet *pktin, *pktout;
429 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
430 sftp_pkt_adduint32(pktout, 0x456); /* request id */
431 sftp_pkt_addstring(pktout, path);
434 id = sftp_pkt_getuint32(pktin);
436 fxp_internal_error("request ID mismatch\n");
439 if (pktin->type == SSH_FXP_HANDLE) {
441 struct fxp_handle *handle;
444 sftp_pkt_getstring(pktin, &hstring, &len);
446 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
449 handle = smalloc(sizeof(struct fxp_handle));
450 handle->hstring = mkstr(hstring, len);
452 sftp_pkt_free(pktin);
455 fxp_got_status(pktin);
463 void fxp_close(struct fxp_handle *handle)
465 struct sftp_packet *pktin, *pktout;
468 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
469 sftp_pkt_adduint32(pktout, 0x789); /* request id */
470 sftp_pkt_addstring_start(pktout);
471 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
474 id = sftp_pkt_getuint32(pktin);
476 fxp_internal_error("request ID mismatch\n");
479 fxp_got_status(pktin);
480 sfree(handle->hstring);
484 int fxp_mkdir(char *path)
486 struct sftp_packet *pktin, *pktout;
489 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
490 sftp_pkt_adduint32(pktout, 0x234); /* request id */
491 sftp_pkt_addstring_start(pktout);
492 sftp_pkt_addstring_data(pktout, path, strlen(path));
493 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
496 id = sftp_pkt_getuint32(pktin);
498 fxp_internal_error("request ID mismatch\n");
501 id = fxp_got_status(pktin);
508 int fxp_rmdir(char *path)
510 struct sftp_packet *pktin, *pktout;
513 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
514 sftp_pkt_adduint32(pktout, 0x345); /* request id */
515 sftp_pkt_addstring_start(pktout);
516 sftp_pkt_addstring_data(pktout, path, strlen(path));
519 id = sftp_pkt_getuint32(pktin);
521 fxp_internal_error("request ID mismatch\n");
524 id = fxp_got_status(pktin);
531 int fxp_rm(char *fname)
533 struct sftp_packet *pktin, *pktout;
536 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
537 sftp_pkt_adduint32(pktout, 0x678); /* request id */
538 sftp_pkt_addstring_start(pktout);
539 sftp_pkt_addstring_data(pktout, fname, strlen(fname));
542 id = sftp_pkt_getuint32(pktin);
544 fxp_internal_error("request ID mismatch\n");
547 id = fxp_got_status(pktin);
555 * Read from a file. Returns the number of bytes read, or -1 on an
556 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
557 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
558 * error indicator. It might even depend on the SFTP server.)
560 int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
563 struct sftp_packet *pktin, *pktout;
566 pktout = sftp_pkt_init(SSH_FXP_READ);
567 sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
568 sftp_pkt_addstring_start(pktout);
569 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
570 sftp_pkt_adduint64(pktout, offset);
571 sftp_pkt_adduint32(pktout, len);
574 id = sftp_pkt_getuint32(pktin);
576 fxp_internal_error("request ID mismatch");
579 if (pktin->type == SSH_FXP_DATA) {
583 sftp_pkt_getstring(pktin, &str, &rlen);
585 if (rlen > len || rlen < 0) {
586 fxp_internal_error("READ returned more bytes than requested");
590 memcpy(buffer, str, rlen);
594 fxp_got_status(pktin);
600 * Read from a directory.
602 struct fxp_names *fxp_readdir(struct fxp_handle *handle)
604 struct sftp_packet *pktin, *pktout;
607 pktout = sftp_pkt_init(SSH_FXP_READDIR);
608 sftp_pkt_adduint32(pktout, 0xABC); /* request id */
609 sftp_pkt_addstring_start(pktout);
610 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
613 id = sftp_pkt_getuint32(pktin);
615 fxp_internal_error("request ID mismatch\n");
618 if (pktin->type == SSH_FXP_NAME) {
619 struct fxp_names *ret;
621 ret = smalloc(sizeof(struct fxp_names));
622 ret->nnames = sftp_pkt_getuint32(pktin);
623 ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
624 for (i = 0; i < ret->nnames; i++) {
627 sftp_pkt_getstring(pktin, &str, &len);
628 ret->names[i].filename = mkstr(str, len);
629 sftp_pkt_getstring(pktin, &str, &len);
630 ret->names[i].longname = mkstr(str, len);
631 ret->names[i].attrs = sftp_pkt_getattrs(pktin);
635 fxp_got_status(pktin);
641 * Write to a file. Returns 0 on error, 1 on OK.
643 int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
646 struct sftp_packet *pktin, *pktout;
649 pktout = sftp_pkt_init(SSH_FXP_WRITE);
650 sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
651 sftp_pkt_addstring_start(pktout);
652 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
653 sftp_pkt_adduint64(pktout, offset);
654 sftp_pkt_addstring_start(pktout);
655 sftp_pkt_addstring_data(pktout, buffer, len);
658 id = sftp_pkt_getuint32(pktin);
660 fxp_internal_error("request ID mismatch\n");
663 fxp_got_status(pktin);
664 return fxp_errtype == SSH_FX_OK;
668 * Free up an fxp_names structure.
670 void fxp_free_names(struct fxp_names *names)
674 for (i = 0; i < names->nnames; i++) {
675 sfree(names->names[i].filename);
676 sfree(names->names[i].longname);