CGI tried to execute using relative path

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2047 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2009-09-13 20:14:51 +00:00
parent 0e88518cb4
commit 26e1c2dd9b
4 changed files with 36 additions and 21 deletions

View File

@ -2149,9 +2149,9 @@ FAR httpd_server *httpd_initialize(FAR httpd_sockaddr *sa)
} }
#ifdef CONFIG_THTTPD_HOSTNAME #ifdef CONFIG_THTTPD_HOSTNAME
hs->hostname = strdup(CONFIG_THTTPD_HOSTNAME); hs->hostname = httpd_strdup(CONFIG_THTTPD_HOSTNAME);
#else #else
hs->hostname = strdup(httpd_ntoa(sa)); hs->hostname = httpd_strdup(httpd_ntoa(sa));
#endif #endif
nvdbg("hostname: %s\n", hs->hostname); nvdbg("hostname: %s\n", hs->hostname);

View File

@ -147,6 +147,25 @@ void httpd_free(FAR void *ptr)
} }
#endif #endif
#ifdef CONFIG_THTTPD_MEMDEBUG
FAR char *httpd_strdup(const char *str)
{
FAR char *newstr = strdup(str);
if (!newstr)
{
ndbg("strdup of %s failed\n", str);
}
else
{
nvdbg("strdup'ed %s\n", str);
g_nallocations++;
g_allocated += (strlen(str)+1);
}
httpd_memstats();
return newstr;
}
#endif
/* Helpers to implement dynamically allocated strings */ /* Helpers to implement dynamically allocated strings */
void httpd_realloc_str(char **pstr, size_t *maxsize, size_t size) void httpd_realloc_str(char **pstr, size_t *maxsize, size_t size)

View File

@ -41,6 +41,8 @@
****************************************************************************/ ****************************************************************************/
#include <nuttx/config.h> #include <nuttx/config.h>
#include <stdlib.h>
#include <string.h>
#include "config.h" #include "config.h"
#ifdef CONFIG_THTTPD #ifdef CONFIG_THTTPD
@ -55,10 +57,12 @@
extern FAR void *httpd_malloc(size_t nbytes); extern FAR void *httpd_malloc(size_t nbytes);
extern FAR void *httpd_realloc(FAR void *oldptr, size_t oldsize, size_t newsize); extern FAR void *httpd_realloc(FAR void *oldptr, size_t oldsize, size_t newsize);
extern void httpd_free(FAR void *ptr); extern void httpd_free(FAR void *ptr);
extern FAR char *httpd_strdup(const char *str);
#else #else
# define httpd_malloc(n) malloc(n) # define httpd_malloc(n) malloc(n)
# define httpd_realloc(p,o,n) realloc(p,n) # define httpd_realloc(p,o,n) realloc(p,n)
# define httpd_free(p) free(p) # define httpd_free(p) free(p)
# define httpd_strdup(s) strdup(s)
#endif #endif
/* Helpers to support allocations in multiples of a type size */ /* Helpers to support allocations in multiples of a type size */

View File

@ -46,6 +46,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <libgen.h>
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
@ -632,8 +633,8 @@ static int cgi_child(int argc, char **argv)
struct cgi_outbuffer_s hdr; struct cgi_outbuffer_s hdr;
struct fdwatch_s *fw; struct fdwatch_s *fw;
char *buffer; char *buffer;
char *binary;
char *directory; char *directory;
char *dupname;
boolean indone; boolean indone;
boolean outdone; boolean outdone;
int child; int child;
@ -741,28 +742,19 @@ static int cgi_child(int argc, char **argv)
} }
} }
/* Split the program into directory and binary, so we can chdir() to the /* chdir to the directory containing the binary. This isn't in the CGI 1.1
* program's own directory. This isn't in the CGI 1.1 spec, but it's what * spec, but it's what other HTTP servers do.
* other HTTP servers do.
*/ */
directory = strdup(hc->expnfilename); dupname = httpd_strdup(hc->expnfilename);
if (!directory) if (dupname)
{ {
binary = hc->expnfilename; /* ignore errors */ directory = dirname(dupname);
} if (directory)
else
{ {
binary = strrchr(directory, '/');
if (!binary)
{
binary = hc->expnfilename;
}
else
{
*binary++ = '\0';
(void)chdir(directory); /* ignore errors */ (void)chdir(directory); /* ignore errors */
} }
httpd_free(dupname);
} }
/* Allocate memory for buffering */ /* Allocate memory for buffering */
@ -794,7 +786,7 @@ static int cgi_child(int argc, char **argv)
/* Run the CGI program. */ /* Run the CGI program. */
nllvdbg("Starting CGI\n"); nllvdbg("Starting CGI\n");
child = exec(binary, (FAR const char **)argp, g_thttpdsymtab, g_thttpdnsymbols); child = exec(hc->expnfilename, (FAR const char **)argp, g_thttpdsymtab, g_thttpdnsymbols);
if (child < 0) if (child < 0)
{ {
/* Something went wrong. */ /* Something went wrong. */