Exemple #1
0
int cgiMain(void){
    cgiFilePtr file;
    int targetFile;
    mode_t  mode;
    char name[128];
    char fileNameOnServer[64];
    char contentType[1024];
    char buffer[BufferLen];
    char *tmpStr=NULL;
    int size;
    int got,t;
    cgiHeaderContentType("text/html");
    //取得html页面中file元素的值,应该是文件在客户机上的路径名
    if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
        fprintf(stderr,"could not retrieve filename\n");
        goto FAIL;
    }  
    cgiFormFileSize("file", &size);
    //取得文件类型,不过本例中并未使用
    cgiFormFileContentType("file", contentType, sizeof(contentType));
    //目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
    if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
        fprintf(stderr,"could not open the file\n");
        goto FAIL;
    }
    t=-1; 
    //从路径名解析出用户文件名
    while(1){
        tmpStr=strstr(name+t+1,"\\");
        if(NULL==tmpStr)
            tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
        if(NULL!=tmpStr)
            t=(int)(tmpStr-name);
        else
            break;
    } 
    strcpy(fileNameOnServer,"/app/update/");
    strcat(fileNameOnServer,name+t+1);
    mode=S_IRWXU|S_IRGRP|S_IROTH;    
    //在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件    
    targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
    if(targetFile<0){
        fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);
        goto FAIL;
    }
    //从系统临时文件中读出文件内容,并放到刚创建的目标文件中 
    while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
        if(got>0)
            write(targetFile,buffer,got);    
    }
    cgiFormFileClose(file);
    close(targetFile);
    goto END; 
FAIL:
    fprintf(stderr,"Failed to upload");
    return 1;
END:    
    printf("File \"%s\" has been uploaded",fileNameOnServer);
    return 0;
}
Exemple #2
0
void File()
{
    cgiFilePtr file;
    char name[1024];
    char contentType[1024];
    char buffer[1024];
    int size;
    int got;
    if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {
        printf("<p>No file was uploaded.<p>\n");
        return;
    }
    fprintf(cgiOut, "The filename submitted was: ");
    cgiHtmlEscape(name);
    fprintf(cgiOut, "<p>\n");
    cgiFormFileSize("file", &size);
    fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
    cgiFormFileContentType("file", contentType, sizeof(contentType));
    fprintf(cgiOut, "The alleged content type of the file was: ");
    cgiHtmlEscape(contentType);
    fprintf(cgiOut, "<p>\n");
    fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.<p>\n");
    fprintf(cgiOut, "The file's contents are shown here:<p>\n");
    if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
        fprintf(cgiOut, "Could not open the file.<p>\n");
        return;
    }
    fprintf(cgiOut, "<pre>\n");
    while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
            cgiFormSuccess)
    {
        cgiHtmlEscapeData(buffer, got);
    }
    fprintf(cgiOut, "</pre>\n");
    cgiFormFileClose(file);
}
Exemple #3
0
int cgiMain(void){
    cgiFilePtr file;
    int targetFile;
    mode_t  mode;
    char name[128];
    char fileNameOnServer[64];
    char contentType[1024];
    char buffer[BufferLen];
    char *tmpStr=NULL;
    int size;
    int got,t;
	 char cmd[256];
	long sessionId;            //会话ID
	char value[20];            //临时变量
	char username[40];         //用户名
	char actName[50];          //请求动作
	char msg[64];					//提示信息 
	int itemId = 0;				//网元ID 
	char cardtypes[128];
	char slots[128]; 
	 /* ==================基本校验 ====================== */
	
	/*  获取请求参数  */
	cgiFormString("sessionId",value,20);
	sessionId = atol(value);
	cgiFormString("username",username,40);
	cgiFormString("actName",actName,50);
	cgiFormString("QItemId",value,50);//网元ID
	itemId = atol(value);
	cgiFormString("cardtypes",cardtypes,128);
	cgiFormString("slots",slots,128);
	/* 检查session有效性  */
	//无效
	if(!isSessionValid(sessionId,username)){
		webLog("no");
		//无效session
		sprintf(msg,"未登录或会话已过期!");
		goto FAIL;
	}
	//有效
	else{
		webLog("yes");
		//更新session访问时间
		updateSessionService(sessionId);
	}
	
	/*  权限验证  */
	//权限不足
	if(!isAuthorityValid(actName,username)){
		//输出权限不足信息
		sprintf(msg,"您无此操作权限!");
		goto FAIL;
	}
	 
	
	//清除旧文件 
//	sprintf(cmd,"rm -f /backup/app.tar.gz ");
//	system(cmd);
	  
//	sprintf(cmd,"rm -rf %s/*",UPDATEDIR);
//	system(cmd);
	 
    //取得html页面中file元素的值,应该是文件在客户机上的路径名
    if (cgiFormFileName("updatePkg", name, sizeof(name)) !=cgiFormSuccess) {
        strcpy(msg,"系统出错:无法读取上传文件");
        goto FAIL;
    }  
    cgiFormFileSize("updatePkg", &size);
    if(size >30*1024*1024){
    	strcpy(msg,"不能上传大于30M的文件");
    	goto FAIL;
    }
    //取得文件类型,不过本例中并未使用
    cgiFormFileContentType("updatePkg", contentType, sizeof(contentType));
    //目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
    if (cgiFormFileOpen("updatePkg", &file) != cgiFormSuccess) {
        fprintf(stderr,"could not open the file\n");
        goto FAIL;
    }
    t=-1; 
    //从路径名解析出用户文件名
    while(1){
        tmpStr=strstr(name+t+1,"\\");
        if(NULL==tmpStr)
            tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
        if(NULL!=tmpStr)
            t=(int)(tmpStr-name);
        else
            break;
    } 
    strcpy(fileNameOnServer,"/app/webapp/httpd/html/file/");
    strcat(fileNameOnServer,name+t+1);
    mode=S_IRWXU|S_IRGRP|S_IROTH;  
    
    //删除同名文件 
    sprintf(cmd,"%s",fileNameOnServer);
    remove(cmd);
      
    //在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件    
    targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
    if(targetFile<0){
        fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);
        goto FAIL;
    }
    //从系统临时文件中读出文件内容,并放到刚创建的目标文件中 
    while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
        if(got>0)
            write(targetFile,buffer,got);    
    }
    cgiFormFileClose(file);
    close(targetFile);
    strcpy(msg,"升级文件上传成功!");
    
    
//    sprintf(cmd,"nohup /bin/Online &");
//	 system(cmd);
//	 sleep(1);
	 SendUpdateCmd(itemId,slots,cardtypes,name+t+1);
    goto END; 
FAIL:
   cgiHeaderContentType("text/html; charset=utf-8");
	printf("<html >\n");
	printf("<head> \n");
	printf("<title>Upload Information</title> \n");
	printf("</head> \n");
	printf("<body> \n");
	printf("<script type=\"text/javascript\">");
	//提示操作信息 
	printf("alert(\"%s\");",msg);	
	//提示信息后返回原页面 
	printf("window.location.href=\"/net/update.html\";");
	printf("</script>");
	printf("</body> \n");
	printf("</html> \n");
	return 0;
END:    
   cgiHeaderContentType("text/html; charset=utf-8");
	printf("<html >\n");
	printf("<head> \n");
	printf("<title>Upload Information</title> \n");
	printf("</head> \n");
	printf("<body> \n");
	printf("<script type=\"text/javascript\">");
	//提示操作信息 
	//printf("alert(\"%s\");",msg);	
	//提示信息后返回原页面 
	printf("window.location.href=\"/net/progress.html?itemId=%d\";",itemId);
	printf("</script>");
	printf("</body> \n");
	printf("</html> \n");
	return 0;
}
Exemple #4
0
int cgiMain(void){
    cgiFilePtr file;
    char fileNameOnServer[512]={0};
    char buffer[pic_max_len];
    int got = 0;
	int filelen = 0;
	Ctcp *conn_fileserver = NULL; 	
	char s_data[512]; 
	logo_recv_webclient_t data;
	uint32_t pic_type = 0;
	uint32_t branchtype = 0;
	logo_recv_fileServ_t* recv_data = NULL;
	log_init("/cgi/log/",(log_lvl_t)8,log_size,0,"");
	cgiHeaderContentType((char *)"text/html");
	  
	cgiFormString((char*)"session_key",s_data,512);
	if(strlen(s_data) == 0){
		CGI_ERROR_LOG("session_key error:len[%u] [%s]",strlen(s_data),s_data);
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,session_key_err);
		return FAIL;
	}
	parse_web_data(s_data,&data);
	
	if(data.cnt >CNT_MAX){
		CGI_ERROR_LOG("thumb cnt error[%u]",data.cnt);
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,thumb_cnt_err);
		return FAIL;
	}
	if(cgiFormFileName((char *)"file",fileNameOnServer, sizeof(fileNameOnServer)) !=cgiFormSuccess){   
		CGI_ERROR_LOG("could not retrieve filename, file: %s", fileNameOnServer);
		cgi_print_result(recv_data,"",pic_type,branchtype,FAIL,retrieve_filename_err);
		return FAIL;
    }
	char tfilename[128];
	if(cgiFormSuccess != cgiGetFilePath((char *)"file",tfilename,sizeof(tfilename))){
		CGI_ERROR_LOG("GET FILE PATH ERROR");
		cgi_print_result(recv_data,"",pic_type,branchtype,FAIL,retrieve_filename_err);
		return FAIL;
	}
	
	cgiFormFileSize((char *)"file", &filelen);
	if(filelen > pic_max_len){
		CGI_ERROR_LOG("file size is too big");
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,image_size_err);
		return FAIL;
	}

    if (cgiFormFileOpen((char *)"file", &file) != cgiFormSuccess) {
		CGI_ERROR_LOG("could not open the file");
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,open_file_err);
        return FAIL;
    }
	if(-1 == config_init("/opt/taomee/cgi_conf/bench.conf")){
		CGI_ERROR_LOG("read conf_file error");
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,unknown_err);
		return FAIL;
	}
	char *bind_ip = config_get_strval("bind_ip");
	if(data.ip != inet_addr(bind_ip)){
		struct in_addr addr1;
		memcpy(&addr1, &(data.ip), 4);
		CGI_ERROR_LOG("request is forbidden bind_ip[%d %s],req_ip[%u %s]",inet_addr(bind_ip),bind_ip,data.ip,inet_ntoa(addr1));
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,hostid_err);
		return FAIL;
	}
	time_t now_time; 
	now_time = time(NULL);  
	if((now_time - data.time) > 5*60){
		CGI_ERROR_LOG("upload timeout nowtime[%u] data.time[%u]",now_time,data.time);
		cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,timeout_err);
		return FAIL;
	}
	conn_fileserver = new Ctcp(config_get_strval("bind_fileserver_ip"),config_get_intval("bind_fileserver_port", 0),0,10);	
	if(cgiFormFileRead(file, buffer, pic_max_len, &got) ==cgiFormSuccess){
		if(got > 0){
			uint8_t filedate[8];
			memcpy(filedate,(uint8_t*)buffer,8);
			parse_uploadfile_type(&pic_type ,&branchtype,filedate);
			if(pic_type == 2){
				CGI_ERROR_LOG("image type error.");
				delete conn_fileserver;
				cgiFormFileClose(file);
				cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,image_type_err);
				config_exit();
				return FAIL;
			}
			if(NULL == cgi_create_image(branchtype, got, buffer)){
				CGI_ERROR_LOG("invalid file");
				delete conn_fileserver;
				cgiFormFileClose(file);
				cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,invalid_file);
				config_exit();
				return FAIL;
			}
			cgiFormFileClose(file);
			recv_data = connect_with_fileserver(buffer,conn_fileserver,&data,filelen,pic_type,branchtype,tfilename);
      		if(recv_data == NULL){
				CGI_ERROR_LOG("cgi connect fileserver error");
				delete conn_fileserver;
				cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,net_err);
				config_exit();
				return FAIL;
			}
			delete conn_fileserver;
			cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,SUCC,0);
			config_exit();
			return SUCC;
		}

	}
	CGI_ERROR_LOG("cgi read file error:got=%d",got);
	delete conn_fileserver;
	cgiFormFileClose(file);
	cgi_print_result(recv_data,fileNameOnServer,pic_type,branchtype,FAIL,read_file_err);
	config_exit();
	return FAIL;	
}
Exemple #5
0
int cgiMain(void){
	cgiFilePtr file;
	int targetFile;
	mode_t mode;
	char name[128];
	char nameForCheck[64];
	char fileNameOnServer[64];
	char contentType[1024];
	char buffer[BufferLen];
	char *tmpStr=NULL;
	int size;
	int got,t;

	//取得html页面中file元素的值--文件在客户机上的路径名
	if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
	}
	cgiFormFileSize("file", &size);
	//取得文件类型
	cgiFormFileContentType("file", contentType, sizeof(contentType));
	//目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件
	if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
		goto FAIL;
	}
	t=-1;
	//从路径名解析出用户文件名
	while(1){
		tmpStr=strstr(name+t+1,"\\");
		if(NULL==tmpStr)
			tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
		if(NULL!=tmpStr)
			t=(int)(tmpStr-name);
		else
			break;
	}
	strcpy(fileNameOnServer,name+t+1);
	mode=S_IRWXU|S_IRGRP|S_IROTH;
	//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件
	targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
	if(targetFile<0){
		goto FAIL;
	}
	//从系统临时文件中读出文件内容,并放到刚创建的目标文件中
	while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
		if(got>0)
			write(targetFile,buffer,got);
	}
	cgiFormFileClose(file);
	close(targetFile);
	goto END;

	FAIL:
		cgiHeaderLocation("../upgrade_error.html");
		system("rm /tmp/*");
		return 1;
	END:
		//升级动作
		if(cgiFormString("upfile1",nameForCheck,64)==cgiFormSuccess){
			system("mv /app/www/cgi-bin/rtu /app/");
			system("chmod 777 /app/rtu");
		}else if(cgiFormString("upfile2",nameForCheck,64)==cgiFormSuccess){
			system("");//预留,以后做好安装包再说
		}

		//以下为网页部分
		printf("Content-type: text/html\r\n\r\n");
		printf("<html>");
		printf("<head>");
		printf("    <meta charset=\"utf-8\" content=\"text/html\">");
		printf("    <title>");
		printf("        ctt-2000e动环设备");
		printf("    </title>");
		printf("</head>");
		printf("<body style=\"background:#1782dd\">");
		printf("<div id=\"all\" style=\"width:500px;margin:250px auto;color:#fff;font-size:30px;text-align:center;line-height:60px;\">");
		printf("    升级成功<br>系统将在 <i id=\"time\" style=\"font-size:45px;\">5</i> 秒后重启");
		printf("</div>");
		printf("<script type=\"text/javascript\">");
		printf("    var id_of_time=setInterval(setTime,1000);");
		printf("    setTimeout(close,5000);");
		printf("    function setTime(){");
		printf("        var lastTime=document.getElementById(\"time\");");
		printf("        var number=lastTime.innerHTML;");
		printf("        number=number-1;");
		printf("        lastTime.innerHTML=number;");
		printf("    }");
		printf("    function close(){");
		printf("	clearInterval(id_of_time);");
		printf("	var all=document.getElementById(\"all\");");
		printf("	all.innerHTML=\"系统重启中,请稍后点此重试连接\";");
		printf("	all.onclick=function(){");
		printf("	    window.open(\"http://\"+location.hostname+\":\"+location.port,\"_self\",\"\");");
		printf("        };");
		printf("    }");
		printf("</script>");
		printf("</body>");
		printf("</html>");

		system("rm /tmp/*");
		system("/sbin/reboot");
		return 0;
}