passive mode
[oftp] / src / ftp.c
index 806c766..0a0c721 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -22,6 +22,8 @@
 
 #define TIMEOUT        15
 
+static void free_dir(struct ftp_dirent *dir);
+
 static int newconn(struct ftp *ftp);
 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
 static int handle_control(struct ftp *ftp);
@@ -29,6 +31,7 @@ static int handle_data(struct ftp *ftp, int s);
 static void proc_control(struct ftp *ftp, const char *buf);
 
 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls);
+static int cproc_pasv(struct ftp *ftp, int code, const char *buf, void *cls);
 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls);
 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls);
 
@@ -44,6 +47,10 @@ struct ftp *ftp_alloc(void)
                return 0;
        }
        ftp->ctl = ftp->data = ftp->lis = -1;
+       ftp->passive = 1;
+
+       ftp->dirent[0] = darr_alloc(0, sizeof *ftp->dirent[0]);
+       ftp->dirent[1] = darr_alloc(0, sizeof *ftp->dirent[1]);
        return ftp;
 }
 
@@ -54,6 +61,18 @@ void ftp_free(struct ftp *ftp)
        if(ftp->ctl >= 0) {
                ftp_close(ftp);
        }
+
+       free_dir(ftp->dirent[0]);
+       free_dir(ftp->dirent[1]);
+}
+
+static void free_dir(struct ftp_dirent *dir)
+{
+       int i;
+       for(i=0; i<darr_size(dir); i++) {
+               free(dir[i].name);
+       }
+       darr_free(dir);
 }
 
 int ftp_connect(struct ftp *ftp, const char *hostname, int port)
@@ -133,6 +152,10 @@ static void exec_op(struct ftp *ftp, int op, const char *arg)
                ftp_chdir(ftp, arg);
                break;
 
+       case FTP_CDUP:
+               ftp_chdir(ftp, "..");
+               break;
+
        case FTP_MKDIR:
                ftp_mkdir(ftp, arg);
                break;
@@ -309,6 +332,22 @@ static int ftp_active(struct ftp *ftp)
        return 0;
 }
 
+static int ftp_passive(struct ftp *ftp)
+{
+       if(ftp->lis >= 0) {
+               closesocket(ftp->lis);
+               ftp->lis = -1;
+       }
+       if(ftp->data >= 0) {
+               closesocket(ftp->data);
+               ftp->data = -1;
+       }
+
+       sendcmd(ftp, "PASV");
+       ftp->cproc = cproc_pasv;
+       return 0;
+}
+
 int ftp_handle(struct ftp *ftp, int s)
 {
        if(s == ftp->ctl) {
@@ -353,7 +392,13 @@ static int handle_control(struct ftp *ftp)
        int i, sz, rd;
        char *buf, *start, *end;
 
-       while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) {
+       for(;;) {
+               if((sz = sizeof ftp->crecv - ftp->num_crecv) <= 0) {
+                       /* discard buffer */
+                       warnmsg("discard buffer\n");
+                       sz = sizeof ftp->crecv;
+                       ftp->num_crecv = 0;
+               }
                start = ftp->crecv + ftp->num_crecv;
                if((rd = recv(ftp->ctl, start, sz, 0)) == -1) {
                        if(errno == EINTR) continue;
@@ -368,7 +413,9 @@ static int handle_control(struct ftp *ftp)
                end = start + rd;
                buf = ftp->crecv;
                for(i=0; i<rd; i++) {
-                       if(start[i] == '\n') {
+                       if(start[i] == '\r') {
+                               start[i] = 0;
+                       } else if(start[i] == '\n') {
                                start[i] = 0;
                                proc_control(ftp, buf);
                                buf = start + i + 1;
@@ -377,6 +424,8 @@ static int handle_control(struct ftp *ftp)
                if(buf != ftp->crecv && buf < end) {
                        ftp->num_crecv = end - buf;
                        memmove(ftp->crecv, buf, ftp->num_crecv);
+               } else {
+                       ftp->num_crecv = 0;
                }
        }
        return 0;
@@ -460,6 +509,7 @@ static void proc_control(struct ftp *ftp, const char *buf)
                }
                return;
        }
+       ftp->busy = 0;
 
        switch(code) {
        case 220:
@@ -479,14 +529,10 @@ static void proc_control(struct ftp *ftp, const char *buf)
                errmsg("login failed\n");
                break;
        }
-       ftp->busy = 0;
 }
 
 static int newconn(struct ftp *ftp)
 {
-       if(ftp_active(ftp) == -1) {
-               return -1;
-       }
        ftp_queue(ftp, FTP_PWD, 0);
        ftp_queue(ftp, FTP_LIST, 0);
        return 0;
@@ -506,7 +552,11 @@ int ftp_pwd(struct ftp *ftp)
 
 int ftp_chdir(struct ftp *ftp, const char *dirname)
 {
-       sendcmd(ftp, "cwd %s", dirname);
+       if(strcmp(dirname, "..") == 0) {
+               sendcmd(ftp, "cdup");
+       } else {
+               sendcmd(ftp, "cwd %s", dirname);
+       }
        ftp->cproc = cproc_cwd;
        return 0;
 }
@@ -526,6 +576,26 @@ int ftp_delete(struct ftp *ftp, const char *fname)
        return -1;
 }
 
+static int prepare_data(struct ftp *ftp)
+{
+       if(ftp->data >= 0) {
+               return 0;
+       }
+
+       if(ftp->passive) {
+               ftp_passive(ftp);
+               ftp_waitresp(ftp, TIMEOUT);
+       } else {
+               if(ftp_active(ftp) == -1) {
+                       return -1;
+               }
+       }
+       if(ftp->data == -1) {
+               return -1;
+       }
+       return 0;
+}
+
 struct recvbuf {
        char *buf;
        long size, bufsz;
@@ -535,6 +605,10 @@ int ftp_list(struct ftp *ftp)
 {
        struct recvbuf *rbuf;
 
+       if(prepare_data(ftp)) {
+               return -1;
+       }
+
        if(!(rbuf = malloc(sizeof *rbuf))) {
                errmsg("failed to allocate receive buffer\n");
                return -1;
@@ -594,6 +668,63 @@ static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
+static int cproc_pasv(struct ftp *ftp, int code, const char *buf, void *cls)
+{
+       const char *str;
+       unsigned int addr[6];
+       struct sockaddr_in sa;
+       unsigned int ipaddr;
+       int port;
+
+       if(code != 227) {
+               errmsg("ftp_passive failed\n");
+               goto nopasv;
+       }
+
+       str = buf;
+       while(*str) {
+               if(sscanf(str, "%u,%u,%u,%u,%u,%u", addr, addr + 1, addr + 2, addr + 3,
+                                       addr + 4, addr + 5) == 6) {
+                       break;
+               }
+               str++;
+       }
+       if(!*str || (addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) >= 256) {
+               errmsg("ftp_passive: failed to parse response: %s\n", buf);
+               goto nopasv;
+       }
+       port = (addr[4] << 8) | addr[5];
+       ipaddr = ((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
+               ((uint32_t)addr[2] << 8) | addr[3];
+
+       if((ftp->data = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+               fprintf(stderr, "ftp_passive: failed to allocate socket\n");
+               goto nopasv;
+       }
+
+       infomsg("passive mode: %d.%d.%d.%d port %d\n", addr[0], addr[1], addr[2], addr[3],
+                       port);
+
+       sa.sin_family = AF_INET;
+       sa.sin_addr.s_addr = htonl(ipaddr);
+       sa.sin_port = htons(port);
+
+       if(connect(ftp->data, (struct sockaddr*)&sa, sizeof sa) == -1) {
+               errmsg("ftp_passive: failed to connect to %s:%p\n", inet_ntoa(sa.sin_addr), port);
+               close(ftp->data);
+               ftp->data = -1;
+               goto nopasv;
+       }
+
+       ftp->status = FTP_CONN_PASV;
+       return 0;
+
+nopasv:
+       ftp->passive = 0;
+       ftp_active(ftp);
+       return 0;
+}
+
 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
 {
        char *dirname;
@@ -609,15 +740,22 @@ static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
                return -1;
        }
 
-       free(ftp->curdir_rem);
-       ftp->curdir_rem = strdup_nf(dirname);
-       ftp->modified = 1;
+       free(ftp->curdir[FTP_REMOTE]);
+       ftp->curdir[FTP_REMOTE] = strdup_nf(dirname);
+       ftp->modified = FTP_MOD_REMDIR;
        return 0;
 }
 
 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls)
 {
-       return -1;
+       if(code != 250) {
+               warnmsg("cwd failed\n");
+               return -1;
+       }
+
+       ftp_queue(ftp, FTP_PWD, 0);
+       ftp_queue(ftp, FTP_LIST, 0);
+       return 0;
 }
 
 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
@@ -633,19 +771,6 @@ static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
-static void free_dirlist(struct ftp_dirent *list)
-{
-       struct ftp_dirent *tmp;
-
-       while(list) {
-               tmp = list;
-               list = list->next;
-
-               free(tmp->name);
-               free(tmp);
-       }
-}
-
 #define SKIP_FIELD(p) \
        do { \
                while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \
@@ -691,30 +816,32 @@ static int parse_dirent(struct ftp_dirent *ent, const char *line)
        return 0;
 }
 
+static int direntcmp(const void *a, const void *b)
+{
+       const struct ftp_dirent *da = a, *db = b;
+
+       if(da->type == db->type) {
+               return strcmp(da->name, db->name);
+       }
+       return da->type == FTP_DIR ? -1 : 1;
+}
+
 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
 {
+       int num;
        struct recvbuf *rbuf = cls;
 
        if(sz == 0) {
                /* EOF condition, we got the whole list, update directory entries */
                char *ptr = rbuf->buf;
                char *end = rbuf->buf + rbuf->size;
-               struct ftp_dirent *tail = 0;
-               struct ftp_dirent *ent;
+               struct ftp_dirent ent;
 
-               free_dirlist(ftp->dent_rem);
-               ftp->dent_rem = 0;
+               darr_clear(ftp->dirent[FTP_REMOTE]);
 
                while(ptr < end) {
-                       ent = malloc_nf(sizeof *ent);
-                       if(parse_dirent(ent, ptr) != -1) {
-                               ent->next = 0;
-                               if(!tail) {
-                                       ftp->dent_rem = tail = ent;
-                               } else {
-                                       tail->next = ent;
-                                       tail = ent;
-                               }
+                       if(parse_dirent(&ent, ptr) != -1) {
+                               darr_push(ftp->dirent[FTP_REMOTE], &ent);
                        }
                        while(ptr < end && *ptr != '\n' && *ptr != '\r') ptr++;
                        while(ptr < end && (*ptr == '\r' || *ptr == '\n')) ptr++;
@@ -724,6 +851,9 @@ static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
                free(rbuf->buf);
                free(rbuf);
                ftp->dproc = 0;
+
+               num = darr_size(ftp->dirent[FTP_REMOTE]);
+               qsort(ftp->dirent[FTP_REMOTE], num, sizeof *ftp->dirent[FTP_REMOTE], direntcmp);
                return;
        }
 
@@ -742,3 +872,18 @@ static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
        memcpy(rbuf->buf + rbuf->size, buf, sz);
        rbuf->size += sz;
 }
+
+const char *ftp_curdir(struct ftp *ftp, int whichdir)
+{
+       return ftp->curdir[whichdir];
+}
+
+int ftp_num_dirent(struct ftp *ftp, int whichdir)
+{
+       return darr_size(ftp->dirent[whichdir]);
+}
+
+struct ftp_dirent *ftp_dirent(struct ftp *ftp, int whichdir, int idx)
+{
+       return ftp->dirent[whichdir] + idx;
+}