]> git.sesse.net Git - betaftpd/blobdiff - ftpd.c
do_download(): Removed the check for f->upload :-D
[betaftpd] / ftpd.c
diff --git a/ftpd.c b/ftpd.c
index 473624e53ba55e8a115e8fdb3798dc343b9fe448..637469b9a2b4c98cc41220cf4fbe9209c5d45ec9 100644 (file)
--- a/ftpd.c
+++ b/ftpd.c
 #include <ascii.h>
 #endif
 
-/* Debug printing -- remove before 0.0.8 final */
-#if 1
-#define DPRINT(s)
-#else
-#define DPRINT(s) printf s 
-#endif
-
 #ifndef MAP_FAILED
 #define MAP_FAILED -1
 #endif
@@ -262,7 +255,6 @@ int vsnprintf(char *str, size_t n, const char *format, va_list ap)
  */
 int add_fd(const int fd, const int events)
 {
-       DPRINT(("add_fd(%d, %x)\n", fd, events));
 #if HAVE_POLL
        if (fd >= FD_MAX) {
                printf("add_fd(%d, %x): failed\n", fd, events);
@@ -289,7 +281,6 @@ int add_fd(const int fd, const int events)
  */
 void del_fd(const int fd)
 {
-       DPRINT(("del_fd(%d)\n", fd));
 #if HAVE_POLL
        if (fd >= FD_MAX)
                return;
@@ -365,7 +356,6 @@ struct conn *alloc_new_conn(const int sock)
 {
        const unsigned int one = 1;
        struct conn *c = (struct conn *)(malloc(sizeof(struct conn)));
-       DPRINT(("alloc_new_conn(%d)\n", sock));
 
        if (c == NULL) return c;
 
@@ -480,14 +470,11 @@ struct dcache *alloc_new_dcache()
  */
 void destroy_conn(struct conn * const c)
 {
-       DPRINT(("destroy_conn:\n"));
        if (c == NULL) return;
        del_fd(c->sock);
 
        destroy_ftran(c->transfer);
        remove_from_linked_list((struct list_element *)c);
-
-       DPRINT(("destroy_conn done.\n"));
 }
 
 /*
@@ -570,7 +557,7 @@ void destroy_dcache(struct dcache * const d)
 
 /*
  * process_all_clients():
- *             Processes all the control connections in active_clients
+ *             Processes all the _control_ connections in active_clients
  *             (normally returned from a select(), there are at max
  *             NUM_AC active connections in the set), sending them
  *             through to the command parser if a command has been
@@ -585,8 +572,6 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac)
        struct conn *c = NULL, *next = first_conn->next_conn;
        int checked_through = 0;
 
-       DPRINT(("process_all_clients: num_ac %d\n", num_ac));
-
        /* run through the linked list */
        while (next != NULL && checked_through < num_ac) {
                int bytes_avail;
@@ -594,14 +579,9 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac)
                c = next;
                next = c->next_conn;
 #if HAVE_POLL
-               if (fds[c->sock].revents & (POLLERR|POLLHUP|POLLNVAL)) {
-                       destroy_conn(c);
-                       continue;
-               }
-               if (!fds[c->sock].revents & POLLIN) {
+               if ((fds[c->sock].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) == 0) {
                        continue;
                }
-               DPRINT(("process_all_clients: fd %d has POLLIN set\n", c->sock));
 #else
                if (!FD_ISSET(c->sock, active_clients)) {
                        continue;
@@ -619,8 +599,10 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac)
                         * client has closed the socket. If we get a return value
                         * of -1 (error), we close the socket ourselves.
                         *
-                        * Just to be safe, we include this code for poll() as
-                        * well.
+                        * We do the same for poll(), even though we actually have
+                        * bits that tell us what is happening (in case of new 
+                        * input AND error/hangup at the same time, we do an
+                        * explicit check at the bottom of the loop as well).
                         */
                        destroy_conn(c);
                        continue;
@@ -635,6 +617,10 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac)
 
                c->buf_len += bytes_avail;
                parse_command(c);
+
+               if (fds[c->sock].revents & (POLLERR|POLLHUP|POLLNVAL)) {
+                        destroy_conn(c);
+                }
        }
        return checked_through;
 }
@@ -741,7 +727,7 @@ int process_all_sendfiles(fd_set * const active_clients, const int num_ac)
 int do_upload(struct ftran *f)
 {
        char upload_buf[16384];
-       int avail, size;
+       int size;
 #if WANT_ASCII
        /* keep buffer size small in ascii transfers 
           to prevent process stalling while filtering
@@ -804,11 +790,7 @@ int do_download(struct ftran *f)
         * Here we use a rather simplified sending `algorithm',
         * leaving most of the quirks to the system calls.
         */
-       if (f->dir_listing == 0
-#if WANT_UPLOAD
-               && f->upload == 0
-#endif
-       ) {
+       if (f->dir_listing == 0) {
                int err;
                size = f->size - f->pos;
 
@@ -1029,7 +1011,6 @@ int main(void)
 
 #if HAVE_POLL
                i = poll(fds, highest_fds + 1, 60000);
-               DPRINT(("poll returns %d\n", i));
 #if 0
                {
                        int j;
@@ -1252,7 +1233,6 @@ void numeric(struct conn * const c, const int numeric, const char * const format
        i = vsnprintf(buf, 256, fmt, args);
        va_end(args);
 
-       DPRINT((buf));
        err = send(c->sock, buf, i, 0);
        if (err == -1 && errno == EPIPE) {
                destroy_conn(c);
@@ -1540,11 +1520,12 @@ void clear_bad_fds(int * const server_sock)
  * dump_file(): Dumps a file on the control connection. Used for
  *             welcome messages and the likes. Note that outbuf
  *             is so big, to prevent any crashing from users creating
- *             weird .message files (like 1024 LFs)...
+ *             weird .message files (like 1024 LFs)... The size of
+ *             the file is limited to 1024 bytes (by truncation).
  */
 void dump_file(struct conn * const c, const int num, const char * const filename)
 {
-       char buf[1024], outbuf[8192];
+       char buf[1024], outbuf[5121];
        char *ptr = outbuf + 4;
        int i, j = -1;
 
@@ -1583,28 +1564,26 @@ void list_readmes(struct conn * const c)
        const time_t now = time(NULL);
        int i;
 
-       if (glob("README*", 0, NULL, &pglob) == 0) {
-               for (i = 0; i < pglob.gl_pathc; i++) {
-                       const char * const temp = pglob.gl_pathv[i];
-                       struct stat buf;
-                       char str[2048];
+       if (glob("README*", 0, NULL, &pglob) != 0) return;
 
-                       char *tm;
+       for (i = 0; i < pglob.gl_pathc; i++) {
+               struct stat buf;
+               char str[256];
+               char *tm;
 
-                       if (stat(temp, &buf) == -1) continue;
+               if (stat(pglob.gl_pathv[i], &buf) == -1) continue;
 
-                       /* remove trailing LF */
-                       tm = ctime(&buf.st_mtime);
-                       tm[strlen(tm) - 1] = 0;
+               /* remove trailing LF */
+               tm = ctime(&buf.st_mtime);
+               tm[strlen(tm) - 1] = 0;
 
-                       sprintf(str, "250-Please read the file %s\r\n"
-                                    "250-\tIt was last modified %s - %ld days ago\r\n",
-                               temp, tm,
-                               (now - buf.st_mtime) / 86400);
-                       send(c->sock, str, strlen(str), 0);
-               }
-               globfree(&pglob);
-       }       
+               snprintf(str, 256, "250-Please read the file %s\r\n"
+                                  "250-\tIt was last modified %s - %ld days ago\r\n",
+                       pglob.gl_pathv[i], tm,
+                       (now - buf.st_mtime) / 86400);
+               send(c->sock, str, strlen(str), 0);
+       }
+       globfree(&pglob);
 }
 #endif