x86: disable crashlog, ioremap of RAM does not work properly here
[openwrt.git] / package / uhttpd / src / uhttpd.c
index 6406e459ada1e2b25a28d3f77d2dddacee0e70ae..3563d91d16ef92ad12ed55cae69b410cc5aa2e84 100644 (file)
@@ -96,17 +96,18 @@ static void uh_config_parse(struct config *conf)
                                conf->error_handler = strdup(col1);
                        }
 #ifdef HAVE_CGI
-                       else if( (line[0] == '.') && (strchr(line, ':') != NULL) )
+                       else if( (line[0] == '*') && (strchr(line, ':') != NULL) )
                        {
-                               if( !(col1 = strchr(line, ':')) || (*col1++ = 0) ||
-                                   !(eol = strchr(col1, '\n')) || (*eol++  = 0) )
+                               if( !(col1 = strchr(line, '*')) || (*col1++ = 0) ||
+                                   !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
+                                   !(eol = strchr(col2, '\n')) || (*eol++  = 0) )
                                                continue;
 
-                               if( !uh_interpreter_add(line, col1) )
+                               if( !uh_interpreter_add(col1, col2) )
                                {
                                        fprintf(stderr,
                                                "Unable to add interpreter %s for extension %s: "
-                                               "Out of memory\n", col1, line
+                                               "Out of memory\n", col2, col1
                                        );
                                }
                        }
@@ -126,6 +127,8 @@ static int uh_socket_bind(
        int status;
        int bound = 0;
 
+       int tcp_ka_idl, tcp_ka_int, tcp_ka_cnt;
+
        struct listener *l = NULL;
        struct addrinfo *addrs = NULL, *p = NULL;
 
@@ -145,12 +148,29 @@ static int uh_socket_bind(
                }
 
                /* "address already in use" */
-               if( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1 )
+               if( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) )
                {
                        perror("setsockopt()");
                        goto error;
                }
 
+               /* TCP keep-alive */
+               if( conf->tcp_keepalive > 0 )
+               {
+                       tcp_ka_idl = 1;
+                       tcp_ka_cnt = 3;
+                       tcp_ka_int = conf->tcp_keepalive;
+
+                       if( setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) ||
+                           setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,  &tcp_ka_idl, sizeof(tcp_ka_idl)) ||
+                           setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int)) ||
+                           setsockopt(sock, SOL_TCP, TCP_KEEPCNT,   &tcp_ka_cnt, sizeof(tcp_ka_cnt)) )
+                       {
+                           fprintf(stderr, "Notice: Unable to enable TCP keep-alive: %s\n",
+                               strerror(errno));
+                       }
+               }
+
                /* required to get parallel v4 + v6 working */
                if( p->ai_family == AF_INET6 )
                {
@@ -324,8 +344,8 @@ static struct http_request * uh_http_header_parse(struct client *cl, char *buffe
                                hdrdata = &buffer[i+2];
                        }
 
-                       /* have no name and found [A-Z], start of name */
-                       else if( !hdrname && isalpha(buffer[i]) && isupper(buffer[i]) )
+                       /* have no name and found [A-Za-z], start of name */
+                       else if( !hdrname && isalpha(buffer[i]) )
                        {
                                hdrname = &buffer[i];
                        }
@@ -355,7 +375,6 @@ static struct http_request * uh_http_header_recv(struct client *cl)
        ssize_t blen = sizeof(buffer)-1;
        ssize_t rlen = 0;
 
-
        memset(buffer, 0, sizeof(buffer));
 
        while( blen > 0 )
@@ -371,41 +390,41 @@ static struct http_request * uh_http_header_recv(struct client *cl)
                if( select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0 )
                {
                        /* receive data */
-                       rlen = uh_tcp_peek(cl, bufptr, blen);
+                       ensure_out(rlen = uh_tcp_peek(cl, bufptr, blen));
 
-                       if( rlen > 0 )
+                       if( (idxptr = strfind(buffer, sizeof(buffer), "\r\n\r\n", 4)) )
                        {
-                               if( (idxptr = strfind(buffer, sizeof(buffer), "\r\n\r\n", 4)) )
-                               {
-                                       blen -= uh_tcp_recv(cl, bufptr, (int)(idxptr - bufptr) + 4);
+                               ensure_out(rlen = uh_tcp_recv(cl, bufptr,
+                                       (int)(idxptr - bufptr) + 4));
 
-                                       /* header read complete ... */
-                                       return uh_http_header_parse(cl, buffer, sizeof(buffer) - blen - 1);
-                               }
-                               else
-                               {
-                                       rlen = uh_tcp_recv(cl, bufptr, rlen);
-                                       blen -= rlen;
-                                       bufptr += rlen;
-                               }
+                               /* header read complete ... */
+                               blen -= rlen;
+                               return uh_http_header_parse(cl, buffer,
+                                       sizeof(buffer) - blen - 1);
                        }
                        else
                        {
-                               /* invalid request (unexpected eof/timeout) */
-                               uh_http_response(cl, 408, "Request Timeout");
-                               return NULL;
+                               ensure_out(rlen = uh_tcp_recv(cl, bufptr, rlen));
+
+                               /* unexpected eof - #7904 */
+                               if( rlen == 0 )
+                                       return NULL;
+
+                               blen -= rlen;
+                               bufptr += rlen;
                        }
                }
                else
                {
                        /* invalid request (unexpected eof/timeout) */
-                       uh_http_response(cl, 408, "Request Timeout");
                        return NULL;
                }
        }
 
        /* request entity too large */
        uh_http_response(cl, 413, "Request Entity Too Large");
+
+out:
        return NULL;
 }
 
@@ -493,7 +512,22 @@ static void uh_mainloop(struct config *conf, fd_set serv_fds, int max_fd)
 #ifdef HAVE_TLS
                                                        /* setup client tls context */
                                                        if( conf->tls )
-                                                               conf->tls_accept(cl);
+                                                       {
+                                                               if( conf->tls_accept(cl) < 1 )
+                                                               {
+                                                                       fprintf(stderr,
+                                                                               "tls_accept failed, "
+                                                                               "connection dropped\n");
+
+                                                                       /* close client socket */
+                                                                       close(new_fd);
+
+                                                                       /* remove from global client list */
+                                                                       uh_client_remove(new_fd);
+
+                                                                       continue;
+                                                               }
+                                                       }
 #endif
 
                                                        /* add client socket to global fdset */
@@ -552,7 +586,7 @@ static void uh_mainloop(struct config *conf, fd_set serv_fds, int max_fd)
                                                if( (pin = uh_path_lookup(cl, req->url)) != NULL )
                                                {
                                                        /* auth ok? */
-                                                       if( uh_auth_check(cl, req, pin) )
+                                                       if( !pin->redirected && uh_auth_check(cl, req, pin) )
                                                                uh_dispatch_request(cl, req, pin);
                                                }
 
@@ -601,11 +635,61 @@ static void uh_mainloop(struct config *conf, fd_set serv_fds, int max_fd)
 #endif
 }
 
+#ifdef HAVE_TLS
+static inline int uh_inittls(struct config *conf)
+{
+       /* library handle */
+       void *lib;
+
+       /* already loaded */
+       if( conf->tls != NULL )
+               return 0;
+
+       /* load TLS plugin */
+       if( ! (lib = dlopen("uhttpd_tls.so", RTLD_LAZY | RTLD_GLOBAL)) )
+       {
+               fprintf(stderr,
+                       "Notice: Unable to load TLS plugin - disabling SSL support! "
+                       "(Reason: %s)\n", dlerror()
+               );
+
+               return 1;
+       }
+       else
+       {
+               /* resolve functions */
+               if( !(conf->tls_init   = dlsym(lib, "uh_tls_ctx_init"))      ||
+                   !(conf->tls_cert   = dlsym(lib, "uh_tls_ctx_cert"))      ||
+                   !(conf->tls_key    = dlsym(lib, "uh_tls_ctx_key"))       ||
+                   !(conf->tls_free   = dlsym(lib, "uh_tls_ctx_free"))      ||
+                   !(conf->tls_accept = dlsym(lib, "uh_tls_client_accept")) ||
+                   !(conf->tls_close  = dlsym(lib, "uh_tls_client_close"))  ||
+                   !(conf->tls_recv   = dlsym(lib, "uh_tls_client_recv"))   ||
+                   !(conf->tls_send   = dlsym(lib, "uh_tls_client_send"))
+               ) {
+                       fprintf(stderr,
+                               "Error: Failed to lookup required symbols "
+                               "in TLS plugin: %s\n", dlerror()
+                       );
+                       exit(1);
+               }
+
+               /* init SSL context */
+               if( ! (conf->tls = conf->tls_init()) )
+               {
+                       fprintf(stderr, "Error: Failed to initalize SSL context\n");
+                       exit(1);
+               }
+       }
+
+       return 0;
+}
+#endif
 
 int main (int argc, char **argv)
 {
        /* master file descriptor list */
-       fd_set used_fds, serv_fds, read_fds;
+       fd_set serv_fds;
 
        /* working structs */
        struct addrinfo hints;
@@ -631,15 +715,12 @@ int main (int argc, char **argv)
        char bind[128];
        char *port = NULL;
 
-#if defined(HAVE_TLS) || defined(HAVE_LUA)
+#ifdef HAVE_LUA
        /* library handle */
        void *lib;
 #endif
 
-       /* clear the master and temp sets */
-       FD_ZERO(&used_fds);
        FD_ZERO(&serv_fds);
-       FD_ZERO(&read_fds);
 
        /* handle SIGPIPE, SIGINT, SIGTERM, SIGCHLD */
        sa.sa_flags = 0;
@@ -670,45 +751,9 @@ int main (int argc, char **argv)
        memset(&conf, 0, sizeof(conf));
        memset(bind, 0, sizeof(bind));
 
-#ifdef HAVE_TLS
-       /* load TLS plugin */
-       if( ! (lib = dlopen("uhttpd_tls.so", RTLD_LAZY | RTLD_GLOBAL)) )
-       {
-               fprintf(stderr,
-                       "Notice: Unable to load TLS plugin - disabling SSL support! "
-                       "(Reason: %s)\n", dlerror()
-               );
-       }
-       else
-       {
-               /* resolve functions */
-               if( !(conf.tls_init   = dlsym(lib, "uh_tls_ctx_init"))      ||
-                   !(conf.tls_cert   = dlsym(lib, "uh_tls_ctx_cert"))      ||
-                   !(conf.tls_key    = dlsym(lib, "uh_tls_ctx_key"))       ||
-                   !(conf.tls_free   = dlsym(lib, "uh_tls_ctx_free"))      ||
-                       !(conf.tls_accept = dlsym(lib, "uh_tls_client_accept")) ||
-                       !(conf.tls_close  = dlsym(lib, "uh_tls_client_close"))  ||
-                       !(conf.tls_recv   = dlsym(lib, "uh_tls_client_recv"))   ||
-                       !(conf.tls_send   = dlsym(lib, "uh_tls_client_send"))
-               ) {
-                       fprintf(stderr,
-                               "Error: Failed to lookup required symbols "
-                               "in TLS plugin: %s\n", dlerror()
-                       );
-                       exit(1);
-               }
-
-               /* init SSL context */
-               if( ! (conf.tls = conf.tls_init()) )
-               {
-                       fprintf(stderr, "Error: Failed to initalize SSL context\n");
-                       exit(1);
-               }
-       }
-#endif
 
        while( (opt = getopt(argc, argv,
-               "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:")) > 0
+               "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:A:")) > 0
        ) {
                switch(opt)
                {
@@ -734,7 +779,7 @@ int main (int argc, char **argv)
 #ifdef HAVE_TLS
                                if( opt == 's' )
                                {
-                                       if( !conf.tls )
+                                       if( uh_inittls(&conf) )
                                        {
                                                fprintf(stderr,
                                                        "Notice: TLS support is disabled, "
@@ -759,7 +804,7 @@ int main (int argc, char **argv)
 #ifdef HAVE_TLS
                        /* certificate */
                        case 'C':
-                               if( conf.tls )
+                               if( !uh_inittls(&conf) )
                                {
                                        if( conf.tls_cert(conf.tls, optarg) < 1 )
                                        {
@@ -775,7 +820,7 @@ int main (int argc, char **argv)
 
                        /* key */
                        case 'K':
-                               if( conf.tls )
+                               if( !uh_inittls(&conf) )
                                {
                                        if( conf.tls_key(conf.tls, optarg) < 1 )
                                        {
@@ -882,6 +927,11 @@ int main (int argc, char **argv)
                                conf.network_timeout = atoi(optarg);
                                break;
 
+                       /* tcp keep-alive */
+                       case 'A':
+                               conf.tcp_keepalive = atoi(optarg);
+                               break;
+
                        /* no fork */
                        case 'f':
                                nofork = 1;
@@ -891,8 +941,14 @@ int main (int argc, char **argv)
                        case 'd':
                                if( (port = malloc(strlen(optarg)+1)) != NULL )
                                {
+                                       /* "decode" plus to space to retain compat */
+                                       for (opt = 0; optarg[opt]; opt++)
+                                               if (optarg[opt] == '+')
+                                                       optarg[opt] = ' ';
+
                                        memset(port, 0, strlen(optarg)+1);
                                        uh_urldecode(port, strlen(optarg), optarg, strlen(optarg));
+
                                        printf("%s", port);
                                        free(port);
                                        exit(0);
@@ -1075,4 +1131,3 @@ int main (int argc, char **argv)
 
        return 0;
 }
-