Patch to PSFTP: implement mkdir, rmdir, rm and scripting. Still to
[u/mdw/putty] / sftp.c
diff --git a/sftp.c b/sftp.c
index 51384be..4491349 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -4,8 +4,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <assert.h>
-#include <unistd.h>
 
 #include "int64.h"
 #include "sftp.h"
@@ -33,63 +33,77 @@ struct sftp_packet {
     int type;
 };
 
+static const char *fxp_error_message;
+static int fxp_errtype;
+
 /* ----------------------------------------------------------------------
  * SFTP packet construction functions.
  */
-static void sftp_pkt_ensure(struct sftp_packet *pkt, int length) {
+static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
+{
     if (pkt->maxlen < length) {
-        pkt->maxlen = length + 256;
+       pkt->maxlen = length + 256;
        pkt->data = srealloc(pkt->data, pkt->maxlen);
     }
 }
-static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len) {
+static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
+{
     pkt->length += len;
     sftp_pkt_ensure(pkt, pkt->length);
-    memcpy(pkt->data+pkt->length-len, data, len);
+    memcpy(pkt->data + pkt->length - len, data, len);
 }
-static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte) {
+static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
+{
     sftp_pkt_adddata(pkt, &byte, 1);
 }
-static struct sftp_packet *sftp_pkt_init(int pkt_type) {
+static struct sftp_packet *sftp_pkt_init(int pkt_type)
+{
     struct sftp_packet *pkt;
     pkt = smalloc(sizeof(struct sftp_packet));
     pkt->data = NULL;
     pkt->savedpos = -1;
     pkt->length = 0;
     pkt->maxlen = 0;
-    sftp_pkt_addbyte(pkt, (unsigned char)pkt_type);
+    sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
+    fxp_error_message = NULL;
     return pkt;
 }
-static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value) {
+static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
+{
     sftp_pkt_adddata(pkt, &value, 1);
 }
-static void sftp_pkt_adduint32(struct sftp_packet *pkt, unsigned long value) {
+static void sftp_pkt_adduint32(struct sftp_packet *pkt,
+                              unsigned long value)
+{
     unsigned char x[4];
     PUT_32BIT(x, value);
     sftp_pkt_adddata(pkt, x, 4);
 }
-static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value) {
+static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
+{
     unsigned char x[8];
     PUT_32BIT(x, value.hi);
-    PUT_32BIT(x+4, value.lo);
+    PUT_32BIT(x + 4, value.lo);
     sftp_pkt_adddata(pkt, x, 8);
 }
-static void sftp_pkt_addstring_start(struct sftp_packet *pkt) {
+static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
+{
     sftp_pkt_adduint32(pkt, 0);
     pkt->savedpos = pkt->length;
 }
-static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data) {
+static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
+{
     sftp_pkt_adddata(pkt, data, strlen(data));
-    PUT_32BIT(pkt->data + pkt->savedpos - 4,
-              pkt->length - pkt->savedpos);
+    PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
 }
 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
-                                   char *data, int len) {
+                                   char *data, int len)
+{
     sftp_pkt_adddata(pkt, data, len);
-    PUT_32BIT(pkt->data + pkt->savedpos - 4,
-              pkt->length - pkt->savedpos);
+    PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
 }
-static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data) {
+static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
+{
     sftp_pkt_addstring_start(pkt);
     sftp_pkt_addstring_str(pkt, data);
 }
@@ -98,35 +112,39 @@ static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data) {
  * SFTP packet decode functions.
  */
 
-static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt) {
-    unsigned long value;
+static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
+{
+    unsigned char value;
     if (pkt->length - pkt->savedpos < 1)
-        return 0;                      /* arrgh, no way to decline (FIXME?) */
+       return 0;                      /* arrgh, no way to decline (FIXME?) */
     value = (unsigned char) pkt->data[pkt->savedpos];
     pkt->savedpos++;
     return value;
 }
-static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt) {
+static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
+{
     unsigned long value;
     if (pkt->length - pkt->savedpos < 4)
-        return 0;                      /* arrgh, no way to decline (FIXME?) */
-    value = GET_32BIT(pkt->data+pkt->savedpos);
+       return 0;                      /* arrgh, no way to decline (FIXME?) */
+    value = GET_32BIT(pkt->data + pkt->savedpos);
     pkt->savedpos += 4;
     return value;
 }
 static void sftp_pkt_getstring(struct sftp_packet *pkt,
-                              char **p, int *length) {
+                              char **p, int *length)
+{
     *p = NULL;
     if (pkt->length - pkt->savedpos < 4)
-        return;
-    *length = GET_32BIT(pkt->data+pkt->savedpos);
+       return;
+    *length = GET_32BIT(pkt->data + pkt->savedpos);
     pkt->savedpos += 4;
     if (pkt->length - pkt->savedpos < *length)
-        return;
-    *p = pkt->data+pkt->savedpos;
+       return;
+    *p = pkt->data + pkt->savedpos;
     pkt->savedpos += *length;
 }
-static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) {
+static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
+{
     struct fxp_attrs ret;
     ret.flags = sftp_pkt_getuint32(pkt);
     if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
@@ -162,77 +180,41 @@ static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) {
     }
     return ret;
 }
-static void sftp_pkt_free(struct sftp_packet *pkt) {
-    if (pkt->data) sfree(pkt->data);
+static void sftp_pkt_free(struct sftp_packet *pkt)
+{
+    if (pkt->data)
+       sfree(pkt->data);
     sfree(pkt);
 }
 
 /* ----------------------------------------------------------------------
- * 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);
-       //execl("/root/ssh-research/ssh-2.4.0/apps/ssh/sftp-server2", "sftp-server2", NULL);
-       //execl("/usr/lib/sftp-server", "sftp-server", 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 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 *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);
@@ -244,8 +226,9 @@ struct sftp_packet *sftp_recv(void) {
  * String handling routines.
  */
 
-static char *mkstr(char *s, int len) {
-    char *p = smalloc(len+1);
+static char *mkstr(char *s, int len)
+{
+    char *p = smalloc(len + 1);
     memcpy(p, s, len);
     p[len] = '\0';
     return p;
@@ -255,15 +238,13 @@ static char *mkstr(char *s, int len) {
  * SFTP primitives.
  */
 
-static const char *fxp_error_message;
-static int fxp_errtype;
-
 /*
  * Deal with (and free) an FXP_STATUS packet. Return 1 if
  * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
  * Also place the status into fxp_errtype.
  */
-static int fxp_got_status(struct sftp_packet *pktin) {
+static int fxp_got_status(struct sftp_packet *pktin)
+{
     static const char *const messages[] = {
        /* SSH_FX_OK. The only time we will display a _message_ for this
         * is if we were expecting something other than FXP_STATUS on
@@ -285,8 +266,8 @@ static int fxp_got_status(struct sftp_packet *pktin) {
     } else {
        fxp_errtype = sftp_pkt_getuint32(pktin);
        if (fxp_errtype < 0 ||
-           fxp_errtype >= sizeof(messages)/sizeof(*messages))
-           fxp_error_message = "unknown error code";
+           fxp_errtype >= sizeof(messages) / sizeof(*messages))
+               fxp_error_message = "unknown error code";
        else
            fxp_error_message = messages[fxp_errtype];
     }
@@ -299,23 +280,27 @@ static int fxp_got_status(struct sftp_packet *pktin) {
        return -1;
 }
 
-static int fxp_internal_error(char *msg) {
+static void fxp_internal_error(char *msg)
+{
     fxp_error_message = msg;
     fxp_errtype = -1;
 }
 
-const char *fxp_error(void) {
+const char *fxp_error(void)
+{
     return fxp_error_message;
 }
 
-int fxp_error_type(void) {
+int fxp_error_type(void)
+{
     return fxp_errtype;
 }
 
 /*
  * Perform exchange of init/version packets. Return 0 on failure.
  */
-int fxp_init(void) {
+int fxp_init(void)
+{
     struct sftp_packet *pktout, *pktin;
     int remotever;
 
@@ -324,13 +309,18 @@ int fxp_init(void) {
     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;
     }
     remotever = sftp_pkt_getuint32(pktin);
     if (remotever > SFTP_PROTO_VERSION) {
-       fxp_internal_error("remote protocol is more advanced than we support");
+       fxp_internal_error
+           ("remote protocol is more advanced than we support");
        return 0;
     }
     /*
@@ -347,7 +337,8 @@ int fxp_init(void) {
 /*
  * Canonify a pathname.
  */
-char *fxp_realpath(char *path) {
+char *fxp_realpath(char *path)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -389,7 +380,8 @@ char *fxp_realpath(char *path) {
 /*
  * Open a file.
  */
-struct fxp_handle *fxp_open(char *path, int type) {
+struct fxp_handle *fxp_open(char *path, int type)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -406,7 +398,6 @@ struct fxp_handle *fxp_open(char *path, int type) {
        return NULL;
     }
     if (pktin->type == SSH_FXP_HANDLE) {
-       int count;
        char *hstring;
        struct fxp_handle *handle;
        int len;
@@ -430,7 +421,8 @@ struct fxp_handle *fxp_open(char *path, int type) {
 /*
  * Open a directory.
  */
-struct fxp_handle *fxp_opendir(char *path) {
+struct fxp_handle *fxp_opendir(char *path)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -445,7 +437,6 @@ struct fxp_handle *fxp_opendir(char *path) {
        return NULL;
     }
     if (pktin->type == SSH_FXP_HANDLE) {
-       int count;
        char *hstring;
        struct fxp_handle *handle;
        int len;
@@ -469,7 +460,8 @@ struct fxp_handle *fxp_opendir(char *path) {
 /*
  * Close a file/dir.
  */
-void fxp_close(struct fxp_handle *handle) {
+void fxp_close(struct fxp_handle *handle)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -489,13 +481,85 @@ void fxp_close(struct fxp_handle *handle) {
     sfree(handle);
 }
 
+int fxp_mkdir(char *path)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_MKDIR);
+    sftp_pkt_adduint32(pktout, 0x234); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, path, strlen(path));
+    sftp_pkt_adduint32(pktout, 0);     /* (FIXME) empty ATTRS structure */
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    id = sftp_pkt_getuint32(pktin);
+    if (id != 0x234) {
+       fxp_internal_error("request ID mismatch\n");
+       return 0;
+    }
+    id = fxp_got_status(pktin);
+    if (id != 1) {
+       return 0;
+    }
+    return 1;
+}
+
+int fxp_rmdir(char *path)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_RMDIR);
+    sftp_pkt_adduint32(pktout, 0x345); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, path, strlen(path));
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    id = sftp_pkt_getuint32(pktin);
+    if (id != 0x345) {
+       fxp_internal_error("request ID mismatch\n");
+       return 0;
+    }
+    id = fxp_got_status(pktin);
+    if (id != 1) {
+       return 0;
+    }
+    return 1;
+}
+
+int fxp_rm(char *fname)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_REMOVE);
+    sftp_pkt_adduint32(pktout, 0x678); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, fname, strlen(fname));
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    id = sftp_pkt_getuint32(pktin);
+    if (id != 0x678) {
+       fxp_internal_error("request ID mismatch\n");
+       return 0;
+    }
+    id = fxp_got_status(pktin);
+    if (id != 1) {
+       return 0;
+    }
+    return 1;
+}
+
 /*
  * Read from a file. Returns the number of bytes read, or -1 on an
  * error, or possibly 0 if EOF. (I'm not entirely sure whether it
  * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
  * error indicator. It might even depend on the SFTP server.)
  */
-int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
+int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
+            int len)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -510,7 +574,7 @@ int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
     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;
@@ -535,7 +599,8 @@ int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
 /*
  * Read from a directory.
  */
-struct fxp_names *fxp_readdir(struct fxp_handle *handle) {
+struct fxp_names *fxp_readdir(struct fxp_handle *handle)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -548,7 +613,7 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle) {
     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;
@@ -575,7 +640,9 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle) {
 /*
  * Write to a file. Returns 0 on error, 1 on OK.
  */
-int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
+int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
+             int len)
+{
     struct sftp_packet *pktin, *pktout;
     int id;
 
@@ -589,6 +656,10 @@ int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int 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;
 }
@@ -596,7 +667,8 @@ int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
 /*
  * Free up an fxp_names structure.
  */
-void fxp_free_names(struct fxp_names *names) {
+void fxp_free_names(struct fxp_names *names)
+{
     int i;
 
     for (i = 0; i < names->nnames; i++) {