#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <assert.h>
-#include <unistd.h>
#include "int64.h"
#include "sftp.h"
*/
static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt) {
- unsigned long value;
+ unsigned char value;
if (pkt->length - pkt->savedpos < 1)
return 0; /* arrgh, no way to decline (FIXME?) */
value = (unsigned char) pkt->data[pkt->savedpos];
}
/* ----------------------------------------------------------------------
- * Send and receive packet functions. FIXME: change for PuTTY.
+ * Send and receive packet functions.
*/
-int tossh, fromssh;
-int io_init(void) {
- int to[2], from[2];
- int pid;
-
- assert(pipe(to) == 0);
- assert(pipe(from) == 0);
- pid = fork();
- assert(pid >= 0);
- if (pid == 0) {
- /* We are child. Dup one end of each pipe to our std[io],
- * close other end, exec. */
- close(0); dup2(to[0], 0); close(to[1]);
- close(1); dup2(from[1], 1); close(from[0]);
- execl("/home/simon/src/openssh/openssh_cvs/prefix/bin/ssh", "ssh", "-2", "simon@localhost", "-s", "sftp", NULL);
- assert(0); /* bomb out if not */
- } else {
- /* We are parent. Close wrong end of each pipe, assign to glob vars. */
- close(to[0]); tossh = to[1];
- close(from[1]); fromssh = from[0];
- }
-}
-int io_finish(void) {
- int pid, status;
- close(tossh);
- close(fromssh);
- pid = wait(&status);
-}
int sftp_send(struct sftp_packet *pkt) {
+ int ret;
char x[4];
PUT_32BIT(x, pkt->length);
- assert(4 == write(tossh, x, 4));
- assert(pkt->length = write(tossh, pkt->data, pkt->length));
+ ret = (sftp_senddata(x, 4) &&
+ sftp_senddata(pkt->data, pkt->length));
sftp_pkt_free(pkt);
+ return ret;
}
struct sftp_packet *sftp_recv(void) {
struct sftp_packet *pkt;
char x[4];
- int p, ret;
- for (p = 0; p < 4 ;) {
- ret = read(fromssh, x+p, 4-p);
- assert(ret >= 0);
- if (ret == 0)
- return NULL;
- p += ret;
- }
+ if (!sftp_recvdata(x, 4))
+ return NULL;
pkt = smalloc(sizeof(struct sftp_packet));
pkt->savedpos = 0;
pkt->length = pkt->maxlen = GET_32BIT(x);
pkt->data = smalloc(pkt->length);
- for (p = 0; p < pkt->length ;) {
- ret = read(fromssh, pkt->data+p, pkt->length-p);
- assert(ret >= 0);
- if (ret == 0) {
- sftp_pkt_free(pkt);
- return NULL;
- }
- p += ret;
+ if (!sftp_recvdata(pkt->data, pkt->length)) {
+ sftp_pkt_free(pkt);
+ return NULL;
}
pkt->type = sftp_pkt_getbyte(pkt);
return -1;
}
-static int fxp_internal_error(char *msg) {
+static void fxp_internal_error(char *msg) {
fxp_error_message = msg;
fxp_errtype = -1;
}
sftp_send(pktout);
pktin = sftp_recv();
+ if (!pktin) {
+ fxp_internal_error("could not connect");
+ return 0;
+ }
if (pktin->type != SSH_FXP_VERSION) {
fxp_internal_error("did not receive FXP_VERSION");
return 0;
}
/*
- * Canonify a pathname. Concatenate the two given path elements
- * with a separating slash, unless the second is NULL.
+ * Canonify a pathname.
*/
-char *fxp_realpath(char *path, char *path2) {
+char *fxp_realpath(char *path) {
struct sftp_packet *pktin, *pktout;
int id;
sftp_pkt_adduint32(pktout, 0x123); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_str(pktout, path);
- if (path2) {
- sftp_pkt_addstring_str(pktout, "/");
- sftp_pkt_addstring_str(pktout, path2);
- }
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
return NULL;
}
if (pktin->type == SSH_FXP_HANDLE) {
- int count;
char *hstring;
struct fxp_handle *handle;
int len;
}
handle = smalloc(sizeof(struct fxp_handle));
handle->hstring = mkstr(hstring, len);
+ handle->hlen = len;
sftp_pkt_free(pktin);
return handle;
} else {
return NULL;
}
if (pktin->type == SSH_FXP_HANDLE) {
- int count;
char *hstring;
struct fxp_handle *handle;
int len;
}
handle = smalloc(sizeof(struct fxp_handle));
handle->hstring = mkstr(hstring, len);
+ handle->hlen = len;
sftp_pkt_free(pktin);
return handle;
} else {
pktout = sftp_pkt_init(SSH_FXP_CLOSE);
sftp_pkt_adduint32(pktout, 0x789); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
pktout = sftp_pkt_init(SSH_FXP_READ);
sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_adduint32(pktout, len);
sftp_send(pktout);
id = sftp_pkt_getuint32(pktin);
if (id != 0xBCD) {
fxp_internal_error("request ID mismatch");
- return;
+ return -1;
}
if (pktin->type == SSH_FXP_DATA) {
char *str;
pktout = sftp_pkt_init(SSH_FXP_READDIR);
sftp_pkt_adduint32(pktout, 0xABC); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
if (id != 0xABC) {
fxp_internal_error("request ID mismatch\n");
- return;
+ return NULL;
}
if (pktin->type == SSH_FXP_NAME) {
struct fxp_names *ret;
pktout = sftp_pkt_init(SSH_FXP_WRITE);
sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, buffer, len);
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
+ if (id != 0xDCB) {
+ fxp_internal_error("request ID mismatch\n");
+ return 0;
+ }
fxp_got_status(pktin);
return fxp_errtype == SSH_FX_OK;
}