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)
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);
67 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
69 sftp_pkt_adddata(pkt, &value, 1);
71 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
76 sftp_pkt_adddata(pkt, x, 4);
78 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
81 PUT_32BIT(x, value.hi);
82 PUT_32BIT(x + 4, value.lo);
83 sftp_pkt_adddata(pkt, x, 8);
85 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
87 sftp_pkt_adduint32(pkt, 0);
88 pkt->savedpos = pkt->length;
90 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
92 sftp_pkt_adddata(pkt, data, strlen(data));
93 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
95 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
98 sftp_pkt_adddata(pkt, data, len);
99 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
101 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
103 sftp_pkt_addstring_start(pkt);
104 sftp_pkt_addstring_str(pkt, data);
107 /* ----------------------------------------------------------------------
108 * SFTP packet decode functions.
111 static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
114 if (pkt->length - pkt->savedpos < 1)
115 return 0; /* arrgh, no way to decline (FIXME?) */
116 value = (unsigned char) pkt->data[pkt->savedpos];
120 static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
123 if (pkt->length - pkt->savedpos < 4)
124 return 0; /* arrgh, no way to decline (FIXME?) */
125 value = GET_32BIT(pkt->data + pkt->savedpos);
129 static void sftp_pkt_getstring(struct sftp_packet *pkt,
130 char **p, int *length)
133 if (pkt->length - pkt->savedpos < 4)
135 *length = GET_32BIT(pkt->data + pkt->savedpos);
137 if (pkt->length - pkt->savedpos < *length)
139 *p = pkt->data + pkt->savedpos;
140 pkt->savedpos += *length;
142 static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
144 struct fxp_attrs ret;
145 ret.flags = sftp_pkt_getuint32(pkt);
146 if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
147 unsigned long hi, lo;
148 hi = sftp_pkt_getuint32(pkt);
149 lo = sftp_pkt_getuint32(pkt);
150 ret.size = uint64_make(hi, lo);
152 if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
153 ret.uid = sftp_pkt_getuint32(pkt);
154 ret.gid = sftp_pkt_getuint32(pkt);
156 if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
157 ret.permissions = sftp_pkt_getuint32(pkt);
159 if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
160 ret.atime = sftp_pkt_getuint32(pkt);
161 ret.mtime = sftp_pkt_getuint32(pkt);
163 if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
165 count = sftp_pkt_getuint32(pkt);
170 * We should try to analyse these, if we ever find one
173 sftp_pkt_getstring(pkt, &str, &len);
174 sftp_pkt_getstring(pkt, &str, &len);
179 static void sftp_pkt_free(struct sftp_packet *pkt)
186 /* ----------------------------------------------------------------------
187 * Send and receive packet functions.
189 int sftp_send(struct sftp_packet *pkt)
193 PUT_32BIT(x, pkt->length);
194 ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
198 struct sftp_packet *sftp_recv(void)
200 struct sftp_packet *pkt;
203 if (!sftp_recvdata(x, 4))
206 pkt = smalloc(sizeof(struct sftp_packet));
208 pkt->length = pkt->maxlen = GET_32BIT(x);
209 pkt->data = smalloc(pkt->length);
211 if (!sftp_recvdata(pkt->data, pkt->length)) {
216 pkt->type = sftp_pkt_getbyte(pkt);
221 /* ----------------------------------------------------------------------
222 * String handling routines.
225 static char *mkstr(char *s, int len)
227 char *p = smalloc(len + 1);
233 /* ----------------------------------------------------------------------
237 static const char *fxp_error_message;
238 static int fxp_errtype;
241 * Deal with (and free) an FXP_STATUS packet. Return 1 if
242 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
243 * Also place the status into fxp_errtype.
245 static int fxp_got_status(struct sftp_packet *pktin)
247 static const char *const messages[] = {
248 /* SSH_FX_OK. The only time we will display a _message_ for this
249 * is if we were expecting something other than FXP_STATUS on
250 * success, so this is actually an error message! */
251 "unexpected OK response",
253 "no such file or directory",
259 "operation unsupported",
262 if (pktin->type != SSH_FXP_STATUS) {
263 fxp_error_message = "expected FXP_STATUS packet";
266 fxp_errtype = sftp_pkt_getuint32(pktin);
267 if (fxp_errtype < 0 ||
268 fxp_errtype >= sizeof(messages) / sizeof(*messages))
269 fxp_error_message = "unknown error code";
271 fxp_error_message = messages[fxp_errtype];
274 if (fxp_errtype == SSH_FX_OK)
276 else if (fxp_errtype == SSH_FX_EOF)
282 static void fxp_internal_error(char *msg)
284 fxp_error_message = msg;
288 const char *fxp_error(void)
290 return fxp_error_message;
293 int fxp_error_type(void)
299 * Perform exchange of init/version packets. Return 0 on failure.
303 struct sftp_packet *pktout, *pktin;
306 pktout = sftp_pkt_init(SSH_FXP_INIT);
307 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
312 fxp_internal_error("could not connect");
315 if (pktin->type != SSH_FXP_VERSION) {
316 fxp_internal_error("did not receive FXP_VERSION");
319 remotever = sftp_pkt_getuint32(pktin);
320 if (remotever > SFTP_PROTO_VERSION) {
322 ("remote protocol is more advanced than we support");
326 * In principle, this packet might also contain extension-
327 * string pairs. We should work through them and look for any
328 * we recognise. In practice we don't currently do so because
329 * we know we don't recognise _any_.
331 sftp_pkt_free(pktin);
337 * Canonify a pathname.
339 char *fxp_realpath(char *path)
341 struct sftp_packet *pktin, *pktout;
344 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
345 sftp_pkt_adduint32(pktout, 0x123); /* request id */
346 sftp_pkt_addstring_start(pktout);
347 sftp_pkt_addstring_str(pktout, path);
350 id = sftp_pkt_getuint32(pktin);
352 fxp_internal_error("request ID mismatch\n");
355 if (pktin->type == SSH_FXP_NAME) {
360 count = sftp_pkt_getuint32(pktin);
362 fxp_internal_error("REALPATH returned name count != 1\n");
365 sftp_pkt_getstring(pktin, &path, &len);
367 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
370 path = mkstr(path, len);
371 sftp_pkt_free(pktin);
374 fxp_got_status(pktin);
382 struct fxp_handle *fxp_open(char *path, int type)
384 struct sftp_packet *pktin, *pktout;
387 pktout = sftp_pkt_init(SSH_FXP_OPEN);
388 sftp_pkt_adduint32(pktout, 0x567); /* request id */
389 sftp_pkt_addstring(pktout, path);
390 sftp_pkt_adduint32(pktout, type);
391 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
394 id = sftp_pkt_getuint32(pktin);
396 fxp_internal_error("request ID mismatch\n");
399 if (pktin->type == SSH_FXP_HANDLE) {
401 struct fxp_handle *handle;
404 sftp_pkt_getstring(pktin, &hstring, &len);
406 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
409 handle = smalloc(sizeof(struct fxp_handle));
410 handle->hstring = mkstr(hstring, len);
412 sftp_pkt_free(pktin);
415 fxp_got_status(pktin);
423 struct fxp_handle *fxp_opendir(char *path)
425 struct sftp_packet *pktin, *pktout;
428 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
429 sftp_pkt_adduint32(pktout, 0x456); /* request id */
430 sftp_pkt_addstring(pktout, path);
433 id = sftp_pkt_getuint32(pktin);
435 fxp_internal_error("request ID mismatch\n");
438 if (pktin->type == SSH_FXP_HANDLE) {
440 struct fxp_handle *handle;
443 sftp_pkt_getstring(pktin, &hstring, &len);
445 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
448 handle = smalloc(sizeof(struct fxp_handle));
449 handle->hstring = mkstr(hstring, len);
451 sftp_pkt_free(pktin);
454 fxp_got_status(pktin);
462 void fxp_close(struct fxp_handle *handle)
464 struct sftp_packet *pktin, *pktout;
467 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
468 sftp_pkt_adduint32(pktout, 0x789); /* request id */
469 sftp_pkt_addstring_start(pktout);
470 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
473 id = sftp_pkt_getuint32(pktin);
475 fxp_internal_error("request ID mismatch\n");
478 fxp_got_status(pktin);
479 sfree(handle->hstring);
484 * Read from a file. Returns the number of bytes read, or -1 on an
485 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
486 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
487 * error indicator. It might even depend on the SFTP server.)
489 int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
492 struct sftp_packet *pktin, *pktout;
495 pktout = sftp_pkt_init(SSH_FXP_READ);
496 sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
497 sftp_pkt_addstring_start(pktout);
498 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
499 sftp_pkt_adduint64(pktout, offset);
500 sftp_pkt_adduint32(pktout, len);
503 id = sftp_pkt_getuint32(pktin);
505 fxp_internal_error("request ID mismatch");
508 if (pktin->type == SSH_FXP_DATA) {
512 sftp_pkt_getstring(pktin, &str, &rlen);
514 if (rlen > len || rlen < 0) {
515 fxp_internal_error("READ returned more bytes than requested");
519 memcpy(buffer, str, rlen);
523 fxp_got_status(pktin);
529 * Read from a directory.
531 struct fxp_names *fxp_readdir(struct fxp_handle *handle)
533 struct sftp_packet *pktin, *pktout;
536 pktout = sftp_pkt_init(SSH_FXP_READDIR);
537 sftp_pkt_adduint32(pktout, 0xABC); /* request id */
538 sftp_pkt_addstring_start(pktout);
539 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
542 id = sftp_pkt_getuint32(pktin);
544 fxp_internal_error("request ID mismatch\n");
547 if (pktin->type == SSH_FXP_NAME) {
548 struct fxp_names *ret;
550 ret = smalloc(sizeof(struct fxp_names));
551 ret->nnames = sftp_pkt_getuint32(pktin);
552 ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
553 for (i = 0; i < ret->nnames; i++) {
556 sftp_pkt_getstring(pktin, &str, &len);
557 ret->names[i].filename = mkstr(str, len);
558 sftp_pkt_getstring(pktin, &str, &len);
559 ret->names[i].longname = mkstr(str, len);
560 ret->names[i].attrs = sftp_pkt_getattrs(pktin);
564 fxp_got_status(pktin);
570 * Write to a file. Returns 0 on error, 1 on OK.
572 int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
575 struct sftp_packet *pktin, *pktout;
578 pktout = sftp_pkt_init(SSH_FXP_WRITE);
579 sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
580 sftp_pkt_addstring_start(pktout);
581 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
582 sftp_pkt_adduint64(pktout, offset);
583 sftp_pkt_addstring_start(pktout);
584 sftp_pkt_addstring_data(pktout, buffer, len);
587 id = sftp_pkt_getuint32(pktin);
589 fxp_internal_error("request ID mismatch\n");
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)
603 for (i = 0; i < names->nnames; i++) {
604 sfree(names->names[i].filename);
605 sfree(names->names[i].longname);