Esempio n. 1
0
void ReadPOSTData(int sid) {
	char *pPostData;
	int rc=0;
	int x=0;

	if (conn[sid].PostData!=NULL) {
		free(conn[sid].PostData);
		conn[sid].PostData=NULL;
	}
	conn[sid].PostData=calloc(conn[sid].dat->in_ContentLength+1024, sizeof(char));
	if (conn[sid].PostData==NULL) {
		logerror("Memory allocation error while reading POST data.");
		closeconnect(sid, 1);
	}
	pPostData=conn[sid].PostData;
	/* reading beyond PostContentLength is required for IE5.5 and NS6 (HTTP 1.1) */
	do {
		rc=recv(conn[sid].socket, pPostData, 1024, 0);
		if (rc==-1) {
			closeconnect(sid, 1);
			return;
		}
		pPostData+=rc;
		x+=rc;
	} while ((rc==1024)||(x<conn[sid].dat->in_ContentLength));
	conn[sid].PostData[conn[sid].dat->in_ContentLength]='\0';
}
Esempio n. 2
0
void printerror(int sid, int status, char* title, char* text)
{
	send_header(sid, 0, 200, "OK", "1", "text/html", -1, -1);
	prints("<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\n", status, title);
	prints("<BODY BGCOLOR=#F0F0F0 TEXT=#000000 LINK=#0000FF ALINK=#0000FF VLINK=#0000FF>\n");
	prints("<H1>%d %s</H1>\n", status, title);
	prints("%s\n", text);
	prints("<HR>\n<ADDRESS>%s</ADDRESS>\n</BODY></HTML>\n", SERVER_NAME);
	conn[sid].dat->out_bodydone=1;
	flushbuffer(sid);
	closeconnect(sid, 1);
	return;
}
void test_gprs(void)
{
  rt_bool_t res = RT_TRUE; 
  res = sysconfig(); if(!res) return;
  
  res = poweron(); if(!res) return;
  rt_thread_delay(2000);
  
  res = gprsinit(); if(!res) return;
  
  res = msgsend("abc"); if(!res) return;
  res = msgreaddata(); if(!res) return;
 
  res = gprsconnect(); if(!res) return;
  res = gprssend("abc"); if(!res) return;
  res = gprsread();   if(!res) return;
  
  res = gprstp(); if(!res) return;
  res = gprstpoff(); if(!res) return;
  
  res = closeconnect(); if(!res) return;
  
  poweroff(); 
}
Esempio n. 4
0
int cgi_main()
{
#ifdef WIN32
	char *cgi_types[3][2]={
		{ ".php", "PHP.EXE" },
		{ ".pl",  "PERL.EXE" },
		{ NULL,   NULL }
	};
	DWORD exitcode=0;
	HANDLE hMyProcess=GetCurrentProcess();
	PROCESS_INFORMATION pi;
	STARTUPINFO si;
	char Command[512];
	char Environ[8192];
	char Path[255];
#else
	char *cgi_types[3][2]={
		{ ".php", "/usr/bin/php" },
		{ ".pl",  "/usr/bin/perl" },
		{ NULL,   NULL }
	};
	int pset1[2];
	int pset2[2];
#endif
	char *args[10];
	char *env[50];
	char cgifilename[255];
	char *extension;
	char szBuffer[BUFF_SIZE];
	pipe_fd local;
	pipe_fd remote;
	int sid=getsid();
	int nOutRead;
	int pid;
	unsigned int i;
	unsigned int n;

	memset(args, 0, sizeof(args));
	cgi_makeargs(sid, args);
	memset(env, 0, sizeof(env));
	cgi_makeenv(sid, env, args);
	snprintf(cgifilename, sizeof(cgifilename)-1, "%s", args[0]);
	for (i=0;i<10;i++) free(args[i]);
	n=0;
	if ((extension=strrchr(cgifilename, '.'))!=NULL) {
		for (i=0;cgi_types[i][0]!=NULL;i++) {
			if (strcmp(extension, cgi_types[i][0])==0) {
				args[n]=calloc(255, sizeof(char));
				snprintf(args[n], 254, "%s", cgi_types[i][1]);
				n++;
				break;
			}
		}
	}
	args[n]=calloc(255, sizeof(char));
	snprintf(args[n], 254, "%s", cgifilename);
#ifdef WIN32
	memset(Command, 0, sizeof(Command));
	memset(Environ, 0, sizeof(Environ));
	ZeroMemory(&pi, sizeof(pi));
	ZeroMemory(&si, sizeof(si));
	snprintf(Path, sizeof(Path)-1, "%s", cgifilename);
	if ((extension=strrchr(Path, '\\'))!=NULL) *extension='\0';
	if (args[1]==NULL) {
		snprintf(Command, sizeof(Command)-1, "%s", cgifilename);
	} else {
		//snprintf(Command, sizeof(Command)-1, "%s\\%s \"%s\"", config.server_cgi_bin_dir, cgi_types[i][1], cgifilename);
		snprintf(Command, sizeof(Command)-1, "%s\\%s \"%s\" > file.html", config.server_cgi_bin_dir, cgi_types[i][1], cgifilename);
	}

	logerror("Process Command: %s", Command);

	for (i=0, n=0;env[i]!=NULL;i++) {
		if (n+strlen(env[i])>sizeof(Environ)) break;
		n+=sprintf(&Environ[n], "%s", env[i]);
		n++;
	}
	if (!CreatePipe((HANDLE)&remote.in, (HANDLE)&local.out, NULL, BUFF_SIZE)) {
		for (i=0;i<10;i++) free(args[i]);
		for (i=0;i<50;i++) free(env[i]);
		printerror(sid, 500, "Internal Server Error", "Unable to create pipe.");
		return -1;
	}
	if (!CreatePipe((HANDLE)&local.in, (HANDLE)&remote.out, NULL, BUFF_SIZE)) {
		for (i=0;i<10;i++) free(args[i]);
		for (i=0;i<50;i++) free(env[i]);
		CloseHandle((HANDLE)remote.in);
		CloseHandle((HANDLE)local.out);
		printerror(sid, 500, "Internal Server Error", "Unable to create pipe.");
		return -1;
	}
	si.cb=sizeof(si);
	si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
	si.wShowWindow=SW_HIDE;
	si.hStdInput=(HANDLE)remote.in;
	si.hStdOutput=(HANDLE)remote.out;
	si.hStdError=(HANDLE)remote.out;
	if (!CreateProcess(NULL, Command, NULL, NULL, TRUE, CREATE_NO_WINDOW, Environ, Path, &si, &pi)) {
		for (i=0;i<10;i++) free(args[i]);
		for (i=0;i<50;i++) free(env[i]);
		CloseHandle((HANDLE)local.in);
		CloseHandle((HANDLE)local.out);
		CloseHandle((HANDLE)remote.in);
		CloseHandle((HANDLE)remote.out);
		logerror("CGI failed. [%s]", Command);
		printerror(sid, 500, "Internal Server Error", "There was a problem running the requested CGI.");
		return -1;
	}
	pid=pi.dwProcessId;
	CloseHandle(si.hStdInput);
	CloseHandle(si.hStdOutput);
#else
	if ((pipe(pset1)==-1) || (pipe(pset2)==-1)) {
		for (i=0;i<10;i++) free(args[i]);
		for (i=0;i<50;i++) free(env[i]);
		close(pset1[0]);
		close(pset1[1]);
		logerror("pipe() error");
		printerror(sid, 500, "Internal Server Error", "Unable to create pipe.");
		return -1;
	}
	local.in=pset1[0]; remote.out=pset1[1];
	remote.in=pset2[0]; local.out=pset2[1];
	logaccess(1, "Executing CGI [%s %s]", args[0], args[1]);
	pid=fork();
	if (pid<0) {
		logerror("fork() error");
		return -1;
	} else if (pid==0) {
		close(local.in);
		close(local.out);
		dup2(remote.in, fileno(stdin));
		dup2(remote.out, fileno(stdout));
//		if ((dup2(remote.in, fileno(stdin))!=0)||(dup2(remote.out, fileno(stdout))!=0)) {
//			logerror("dup2() error");
//			exit(0);
//		}
		execve(args[0], &args[0], &env[0]);
		logerror("execve() error [%s][%s]", args[0], args[1]);
		exit(0);
	} else {
		close(remote.in);
		close(remote.out);
	}
#endif
	sendfile(sid, "file.html");
	if (conn[sid].dat->in_ContentLength>0) {
#ifdef WIN32
		WriteFile((HANDLE)local.out, conn[sid].PostData, conn[sid].dat->in_ContentLength, &nOutRead, NULL);
#else
		write(local.out, conn[sid].PostData, conn[sid].dat->in_ContentLength);
#endif
	}
	conn[sid].dat->out_headdone=1;
	conn[sid].dat->out_status=200;
	if (strcasestr(conn[sid].dat->in_Protocol, "HTTP/1.1")!=NULL) {
		snprintf(conn[sid].dat->out_Protocol, sizeof(conn[sid].dat->out_Protocol)-1, "HTTP/1.1");
	} else {
		snprintf(conn[sid].dat->out_Protocol, sizeof(conn[sid].dat->out_Protocol)-1, "HTTP/1.0");
	}
	snprintf(conn[sid].dat->out_Connection, sizeof(conn[sid].dat->out_Connection)-1, "Close");
	prints("%s %d OK\r\n", conn[sid].dat->out_Protocol, conn[sid].dat->out_status);
	prints("Connection: %s\r\n", conn[sid].dat->out_Connection);
	flushbuffer(sid);
	do {
		memset(szBuffer, 0, sizeof(szBuffer));
#ifdef WIN32
		ReadFile((HANDLE)local.in, szBuffer, sizeof(szBuffer)-1, &nOutRead, NULL);
#else
		nOutRead=read(local.in, szBuffer, BUFF_SIZE-1);
#endif
		if (nOutRead>0)
		{
			if (RunAsCGI) {
				fwrite(szBuffer, sizeof(char), nOutRead, stdout);
			} else {
				fwrite(szBuffer, sizeof(char), nOutRead, stdout);
				send(conn[sid].socket, szBuffer, nOutRead, 0);
			}
		};
	} while (nOutRead>0);
	flushbuffer(sid);
	/* cleanup */
	for (i=0;i<10;i++) free(args[i]);
	for (i=0;i<50;i++) free(env[i]);
#ifdef WIN32
	GetExitCodeProcess(pi.hProcess, &exitcode);
	if (exitcode==STILL_ACTIVE) TerminateProcess(pi.hProcess, 1);
	CloseHandle(pi.hThread);
	CloseHandle(pi.hProcess);
	CloseHandle((HANDLE)local.in);
	CloseHandle((HANDLE)local.out);
#else
	close(local.in);
	close(local.out);
#endif
	conn[sid].dat->out_bodydone=1;
	flushbuffer(sid);
	closeconnect(sid, 1);
	return 0;
}
Esempio n. 5
0
int read_header(int sid)
{
	char line[2048];
	char *pTemp;
	time_t x;

	strncpy(conn[sid].dat->in_RemoteAddr, inet_ntoa(conn[sid].ClientAddr.sin_addr), sizeof(conn[sid].dat->in_RemoteAddr)-1);
	x=time((time_t*)0);
	do {
		memset(line, 0, sizeof(line));
		sgets(line, sizeof(line)-1, conn[sid].socket);
		striprn(line);
	} while ((strlen(line)==0)&&((time((time_t)0)-x)<30));
	if ((strlen(line)==0)&&((time((time_t)0)-x)>=30)) {
#ifdef DEBUG
logdata("\n[[[ KILLING IDLE KEEPALIVE ]]]\n");
#endif
		closeconnect(sid, 1);
	}
#ifdef DEBUG
logdata("\n[[[ STARTING REQUEST ]]]\n");
#endif
	if (strlen(line)==0)
		printerror(sid, 400, "Bad Request", "No Request Found.");
	if (sscanf(line, "%[^ ] %[^ ] %[^ ]", conn[sid].dat->in_RequestMethod, conn[sid].dat->in_RequestURI, conn[sid].dat->in_Protocol)!=3)
		printerror(sid, 400, "Bad Request", "Can't Parse Request.");
	pTemp=conn[sid].dat->in_RequestMethod;
	while (*pTemp) { *pTemp=toupper(*pTemp); pTemp++; };
	while (strlen(line)>0) {
		sgets(line, sizeof(line)-1, conn[sid].socket);
		while ((line[strlen(line)-1]=='\n')||(line[strlen(line)-1]=='\r')) line[strlen(line)-1]='\0';
		if (strncasecmp(line, "Connection: ", 12)==0)
			strncpy(conn[sid].dat->in_Connection, (char *)&line+12, sizeof(conn[sid].dat->in_Connection)-1);
		if (strncasecmp(line, "Content-Length: ", 16)==0) {
			conn[sid].dat->in_ContentLength=atoi((char *)&line+16);
			if (conn[sid].dat->in_ContentLength<0) {
				// Negative Content-Length?  If so, the client is either broken or malicious.
				// Thanks to <*****@*****.**> for spotting this one.
				logerror("ERROR: negative Content-Length of %d provided by client.", conn[sid].dat->in_ContentLength);
				conn[sid].dat->in_ContentLength=0;
			}
		}
		if (strncasecmp(line, "Cookie: ", 8)==0)
			strncpy(conn[sid].dat->in_Cookie, (char *)&line+8, sizeof(conn[sid].dat->in_Cookie)-1);
		if (strncasecmp(line, "Host: ", 6)==0)
			strncpy(conn[sid].dat->in_Host, (char *)&line+6, sizeof(conn[sid].dat->in_Host)-1);
		if (strncasecmp(line, "If-Modified-Since: ", 19)==0)
			strncpy(conn[sid].dat->in_IfModifiedSince, (char *)&line+19, sizeof(conn[sid].dat->in_IfModifiedSince)-1);
		if (strncasecmp(line, "User-Agent: ", 12)==0)
			strncpy(conn[sid].dat->in_UserAgent, (char *)&line+12, sizeof(conn[sid].dat->in_UserAgent)-1);
	}
	if ((strcmp(conn[sid].dat->in_RequestMethod, "GET")!=0)&&(strcmp(conn[sid].dat->in_RequestMethod, "POST")!=0)) {
		printerror(sid, 501, "Not Implemented", "That method is not implemented.");
		closeconnect(sid, 1);
		return -1;
	}
	if (strcmp(conn[sid].dat->in_RequestMethod, "POST")==0) {
		if (conn[sid].dat->in_ContentLength<MAX_POSTSIZE) {
			ReadPOSTData(sid);
		} else {
			// try to print an error : note the inbuffer being full may block us
			// FIXME: this is causing the children to segfault in win32
			printerror(sid, 413, "Bad Request", "Request entity too large.");
			logerror("%s - Large POST (>%d bytes) disallowed", conn[sid].dat->in_RemoteAddr, MAX_POSTSIZE);
			closeconnect(sid, 1);
			return -1;
		}
	}
	if (conn[sid].dat->in_RequestURI[0]!='/') {
		printerror(sid, 400, "Bad Request", "Bad filename.");
	}
	if (strchr(conn[sid].dat->in_RequestURI, '?')!=NULL) {
		strncpy(conn[sid].dat->in_QueryString, strchr(conn[sid].dat->in_RequestURI, '?')+1, sizeof(conn[sid].dat->in_QueryString)-1);
	}
	return 0;
}