X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fftp.c;h=cf76a6b1f82b02c55462508a5ca2f2c5ebc4eb5b;hb=214a070238552de6167bbf506cbc23006969a182;hp=6a29a776bb10e85b070b65cc598b40beb639bbf4;hpb=a606b2eade34c3d8e662e94a32af960b5d2911f1;p=oftp diff --git a/src/ftp.c b/src/ftp.c index 6a29a77..cf76a6b 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -18,9 +20,21 @@ #include #define closesocket close #define fcntlsocket fcntl +#else +#define select select_s + +#ifndef O_NONBLOCK +#define O_NONBLOCK 0x2000 +#endif +#ifndef F_SETFL +#define F_GETFL 101 +#define F_SETFL 102 +#endif #endif -#define TIMEOUT 5 +#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, ...); @@ -29,6 +43,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 +59,9 @@ struct ftp *ftp_alloc(void) return 0; } ftp->ctl = ftp->data = ftp->lis = -1; + ftp->passive = 1; + + ftp->dirent = darr_alloc(0, sizeof *ftp->dirent); return ftp; } @@ -54,6 +72,17 @@ void ftp_free(struct ftp *ftp) if(ftp->ctl >= 0) { ftp_close(ftp); } + + free_dir(ftp->dirent); +} + +static void free_dir(struct ftp_dirent *dir) +{ + int i; + for(i=0; iqhead) { + if(!ftp->busy && !ftp->qhead) { exec_op(ftp, op, arg); return 0; } @@ -195,19 +228,27 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg) if(!(fop = malloc(sizeof *fop))) { return -1; } - if(!(fop->arg = strdup(arg))) { - free(fop); - return -1; + if(arg) { + if(!(fop->arg = strdup(arg))) { + free(fop); + return -1; + } + } else { + fop->arg = 0; } fop->op = op; fop->next = 0; - ftp->qtail->next = fop; - ftp->qtail = fop; + if(ftp->qhead) { + ftp->qtail->next = fop; + ftp->qtail = fop; + } else { + ftp->qhead = ftp->qtail = fop; + } return 0; } -int ftp_waitresp(struct ftp *ftp, time_t timeout) +int ftp_waitresp(struct ftp *ftp, long timeout) { fd_set rdset; struct timeval tv; @@ -251,7 +292,7 @@ int ftp_waitresp(struct ftp *ftp, time_t timeout) static int ftp_active(struct ftp *ftp) { struct sockaddr_in sa = {0}; - socklen_t len; + int len; unsigned long addr; unsigned short port; @@ -268,9 +309,11 @@ static int ftp_active(struct ftp *ftp) return -1; } + len = sizeof sa; + getsockname(ftp->ctl, (struct sockaddr*)&sa, &len); + sa.sin_family = AF_INET; sa.sin_port = 0; - sa.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(ftp->lis, (struct sockaddr*)&sa, sizeof sa) == -1) { errmsg("ftp_active: failed to bind listening socket\n"); @@ -281,7 +324,7 @@ static int ftp_active(struct ftp *ftp) listen(ftp->lis, 1); len = sizeof sa; - if(getsockname(ftp->ctl, (struct sockaddr*)&sa, &len) == -1) { + if(getsockname(ftp->lis, (struct sockaddr*)&sa, &len) == -1) { errmsg("ftp_active: failed to retrieve listening socket address\n"); closesocket(ftp->lis); ftp->lis = -1; @@ -290,6 +333,7 @@ static int ftp_active(struct ftp *ftp) addr = ntohl(sa.sin_addr.s_addr); port = ntohs(sa.sin_port); + infomsg("listening on %s port %d\n", inet_ntoa(sa.sin_addr), port); sendcmd(ftp, "PORT %d,%d,%d,%d,%d,%d", addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff, port >> 8, port & 0xff); @@ -298,6 +342,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) { @@ -327,11 +387,13 @@ static int sendcmd(struct ftp *ftp, const char *fmt, ...) return -1; } + ftp->busy = 1; + va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); + infomsg("send: %s\n", buf); strcat(buf, "\r\n"); - return send(ftp->ctl, buf, strlen(buf), 0); } @@ -340,7 +402,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; @@ -355,7 +423,9 @@ static int handle_control(struct ftp *ftp) end = start + rd; buf = ftp->crecv; for(i=0; icrecv && buf < end) { ftp->num_crecv = end - buf; memmove(ftp->crecv, buf, ftp->num_crecv); + } else { + ftp->num_crecv = 0; } } return 0; @@ -418,8 +490,14 @@ static int respcode(const char *resp) static void proc_control(struct ftp *ftp, const char *buf) { int code; + char *end; while(*buf && isspace(*buf)) buf++; + if((end = strchr(buf, '\r'))) { + *end = 0; + } + + infomsg("recv: %s\n", buf); if((code = respcode(buf)) == 0) { warnmsg("ignoring invalid response: %s\n", buf); @@ -434,12 +512,14 @@ static void proc_control(struct ftp *ftp, const char *buf) if(ftp->cproc) { if(ftp->cproc(ftp, code, buf, ftp->cproc_cls) <= 0) { ftp->cproc = 0; + ftp->busy = 0; /* execute next operation if there's one queued */ exec_queued(ftp); } return; } + ftp->busy = 0; switch(code) { case 220: @@ -463,9 +543,6 @@ static void proc_control(struct ftp *ftp, const char *buf) 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; @@ -485,7 +562,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; } @@ -505,6 +586,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; @@ -514,6 +615,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; @@ -573,6 +678,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 = ((unsigned int)addr[0] << 24) | ((unsigned int)addr[1] << 16) | + ((unsigned int)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); + closesocket(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; @@ -588,15 +750,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->curdir = strdup_nf(dirname); + ftp->modified = FTP_MOD_DIR; 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) @@ -612,19 +781,89 @@ static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls) return 0; } +#define SKIP_FIELD(p) \ + do { \ + while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \ + while(*(p) && *(p) != '\n' && isspace(*(p))) (p)++; \ + } while(0) + +static int parse_dirent(struct ftp_dirent *ent, const char *line) +{ + int len; + const char *ptr = line; + const char *end; + + if(!(end = strchr(line, '\r')) && !(end = strchr(line, '\n'))) { + return -1; + } + + if(line[0] == 'd') { + ent->type = FTP_DIR; + } else { + ent->type = FTP_FILE; + } + + SKIP_FIELD(ptr); /* skip mode */ + SKIP_FIELD(ptr); /* skip links */ + SKIP_FIELD(ptr); /* skip owner */ + SKIP_FIELD(ptr); /* skip group */ + + if(ent->type == FTP_FILE) { + ent->size = atoi(ptr); + } + SKIP_FIELD(ptr); /* skip size */ + SKIP_FIELD(ptr); /* skip month */ + SKIP_FIELD(ptr); /* skip day */ + SKIP_FIELD(ptr); /* skip year */ + + if(ptr >= end) return -1; + + len = end - ptr; + ent->name = malloc(len + 1); + memcpy(ent->name, ptr, len); + ent->name[len] = 0; + + return 0; +} + +int ftp_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 */ - /* TODO */ - rbuf->buf[rbuf->size] = 0; - fprintf(stderr, "%s\n", rbuf->buf); + char *ptr = rbuf->buf; + char *end = rbuf->buf + rbuf->size; + struct ftp_dirent ent; + + darr_clear(ftp->dirent); + + while(ptr < end) { + if(parse_dirent(&ent, ptr) != -1) { + darr_push(ftp->dirent, &ent); + } + while(ptr < end && *ptr != '\n' && *ptr != '\r') ptr++; + while(ptr < end && (*ptr == '\r' || *ptr == '\n')) ptr++; + } + ftp->modified |= FTP_MOD_DIR; free(rbuf->buf); free(rbuf); ftp->dproc = 0; + + num = darr_size(ftp->dirent); + qsort(ftp->dirent, num, sizeof *ftp->dirent, ftp_direntcmp); return; } @@ -643,3 +882,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) +{ + return ftp->curdir; +} + +int ftp_num_dirent(struct ftp *ftp) +{ + return darr_size(ftp->dirent); +} + +struct ftp_dirent *ftp_dirent(struct ftp *ftp, int idx) +{ + return ftp->dirent + idx; +}