Exemplo n.º 1
0
int cgiMain()
{
	int res=0;
	char rfid[IEEE_LEN+1]={0};
	int operatortype;
	int duration;

	char send_string[RF_RESPOND_TO_API_MAX_LEN]={0};

	char respond_string[RF_RESPOND_TO_API_MAX_LEN]={0};

	cgiFormResultType cgi_re;
	cgiHeaderContentType("application/json"); //MIME

	//获取rfid,state
	cgi_re = cgiFormString("rfid", rfid, IEEE_LEN + 1);
	cgi_re = cgiFormInteger("operatortype", &operatortype, -1);
	cgi_re = cgiFormInteger("param1", &duration, -1);
	rfid[IEEE_LEN]= '\0';

	//communication with RF_Daemon
	snprintf(send_string, RF_RESPOND_TO_API_MAX_LEN, "{\n	\"api\": \"RFWarningDevOperation\",\n	\"rfid\": \"%s\",\n	\"operatortype\": %d,\n	\"param1\": %d\n}", rfid,operatortype,duration);
	RF_DEBUG("%s\n", send_string);
	res = communicateWithRF(send_string, strlen(send_string)+1, respond_string);
//	if(res !=0) {
		fprintf(cgiOut,"{\n	\"status\": %d\n}\n", res);
//	}
//	fprintf(cgiOut, "%s\n", respond_string);

	return 0;
}
Exemplo n.º 2
0
int cgiMain()
{
	char send_cb_string[GL_CALLBACK_MAX_SIZE];
	int send_cb_len;

	linkage_st lnk_st;
	int enable_flag;

	cgiFormResultType cgi_re;
	int res=0;

	cgiHeaderContentType("application/json"); //MIME

	cgi_re = cgiFormInteger(FIELD_LID, &lnk_st.lnk_base.lid, 0);
	cgi_re = cgiFormString(FIELD_LNKNAME, lnk_st.lnk_base.lnkname, NAME_STRING_LEN + 1);
	lnk_st.lnk_base.lnkname[NAME_STRING_LEN] = '\0';
	cgi_re = cgiFormString(FIELD_TRGIEEE, lnk_st.lnk_base.trgieee, IEEE_LEN + 1);
	lnk_st.lnk_base.trgieee[IEEE_LEN] = '\0';
	cgi_re = cgiFormString(FIELD_TRGEP, lnk_st.lnk_base.trgep, 2 + 1);
	lnk_st.lnk_base.trgep[2] = '\0';
	cgi_re = cgiFormString(FIELD_TRGCND, lnk_st.lnk_base.trgcnd, TRIGGER_CONDITION_LEN + 1);
	lnk_st.lnk_base.trgcnd[TRIGGER_CONDITION_LEN] = '\0';
	cgi_re = cgiFormString(FIELD_LNKACT, lnk_st.lnk_base.lnkact, ACTPARA_LEN + 1);
	lnk_st.lnk_base.lnkact[ACTPARA_LEN] = '\0';
	cgi_re = cgiFormInteger(FIELD_ENABLE, &enable_flag, 0);
	lnk_st.lnk_base.enable = (char)enable_flag;

	res = gnerate_per_urlobj(&lnk_st.urlobject, lnk_st.lnk_base.lnkact);
	if(res <0){
		goto all_over;
	}
	res = gnerate_linkage_trgcnd_st(&lnk_st.lnk_condition, lnk_st.lnk_base.trgcnd);
	if(res <0){
		goto all_over;
	}
//	wrtie database
	res = db_init();
	if(res<0){
		goto all_over;
	}
	res = edit_linkage_db(&lnk_st);
	if(res<0){
		goto all_over;
	}
all_over:
	db_close();
    api_response(res, lnk_st.lnk_base.lid, &lnk_st.lnk_base);
    //push to cb_daemon
	if((send_cb_len = cJsonLinkage_callback(send_cb_string, SUBID_EDIT_LINK, res, lnk_st.lnk_base.lid, &lnk_st.lnk_base)) >=0) {
		//if push failed, not handle temporary
		push_to_CBDaemon(send_cb_string, send_cb_len);
	}
	return 0;
}
Exemplo n.º 3
0
/*
 * Function: doViewArtist
 * Parameters: (void)
 * Returns: (void)
 *
 * This function lets the user view the details of a specified Artist
 */
static void doViewArtist(void)
{
    int result = 0;
    int artistid = -1;

    /* if artistid field is set */
    result = cgiFormInteger("artistid", &artistid, -1);
    if (result != cgiFormSuccess) {
        /* Some sort of failure */
        artistid = -1;
    }
    else {
        /* Check artistid exists */
        if (getArtistExists(artistid) == FALSE) {
            fprintf(cgiOut,
                    "Artist [%d] does not exist in the database<br />\n",
                    artistid);
            fprintf(cgiOut,
                    "<a href=\"./?page=artist&amp;hash=%d\">[View All Artists]</a><br />\n",
                    _currUserLogon);
            return;
        }
    }

    if (artistid == -1) {
        /* print all artists */
        printAllArtists();
    }
    else {
        /* print specific artist */
        printSpecificArtist(artistid);
    }
}
Exemplo n.º 4
0
/*
 * api:	http://192.168.1.227/cgi-bin/rest/network/CallUploadDataProgram.cgi?datatype=1
 * */
int cgiMain()
{
	cgiFormResultType cgi_re;
	int res=0;
	int datatype;
	char cmdstring[100] = {0};

	cgiHeaderContentType("application/json"); //MIME
	cgi_re = cgiFormInteger("datatype", &datatype, -1);

	if(datatype ==-1)
		snprintf(cmdstring, sizeof(cmdstring), "%s", "/gl/bin/BaseDataUpload");
	else
		snprintf(cmdstring, sizeof(cmdstring), "%s %d", "/gl/bin/BaseDataUpload", datatype);

    fflush(stdout);
    pid_t	pid;
	if ((pid = fork()) > 0) {						//parent process
	}
	else
	{												//child process
		//重定向输入输出流
		freopen("/dev/null", "r", stdin);
		freopen("/dev/null", "w", stdout);
		freopen("/dev/null", "w", stderr);
		system(cmdstring);
		exit(0);
	}
	res =0;
	api_response(res, datatype, cmdstring);
	return 0;
}
Exemplo n.º 5
0
static int get_voltage(int sensor_idx)
{
	int value;
	char name[81];
	memset(name,0,sizeof(name));
	sprintf(name,"volt%d",sensor_idx);

	cgiFormInteger(name, &value, 0);
	return value;
}
Exemplo n.º 6
0
static int get_duration_ms(int group_idx,int serial_idx)
{
	int value;
	char name[81];
	memset(name,0,sizeof(name));
	sprintf(name,"duration%d_%d",group_idx,serial_idx+1);

	cgiFormInteger(name, &value, 0);
	return value;
}
Exemplo n.º 7
0
 int cgiMain()
{
	 char *filename = "ip_config.conf";
	 char ip[16];
	 unsigned short portt = 8887;
	 int fd;
	 int readnd = 0;
	 char buffer[512];
	 getip(filename, ip);
	 fd = tcp_init_client(ip, portt);


	 int passed, conditioner, mode;
	 struct replay_packet * replay;

	 struct common_packet request;

	 request.head.len = sizeof(request.data);
	 request.head.encrpyt = ENCRPYT_NO;

	 request.head.ki = KI_AIRCONDITIONER;

	 cgiHeaderContentType("text/html");
	 cgiFormInteger("conditioner", &conditioner, 0);
	 cgiFormInteger("mode", &mode, 0);
	 cgiFormInteger("passed", &passed, 0);
	 request.head.ttl = conditioner;
	 request.head.mo = mode;
	 request.head.extent = passed;

	 writen(fd, (void *)&request, sizeof(struct register_struct));
	 readnd = readn(fd, (void *)buffer, sizeof(struct replay_packet));
	 replay = (struct replay_packet *)buffer;

	 if (readnd == sizeof(struct replay_packet)) {
	 	if (replay->head.ki == KI_REPLAY) {
	 			fprintf(cgiOut, "conditioner=%d;statuss=%d", conditioner, replay->data);
	 	}
	 }
	 tcp_close(fd);
	 return 0;
}
Exemplo n.º 8
0
int
cgiMain()
{ 
	cgiHeaderContentType("text/html");
	
	int serverside = 0;
	cgiFormInteger("serverside", &serverside, 0);
	
	char function_call[50];
	int callback = 0;
	if (cgiFormString("function_call", &function_call[0], 50) == cgiFormSuccess)
		callback = 1;
	
	if(serverside) {
		/* Initialise the SEE library */
		SEE_init();
		
		/* Initialise an interpreter */
		SEE_interpreter_init(&g_interp_storage);
		g_interp = &g_interp_storage;
	
		/* Bring our native functions into the interpreter */
		SEE_CFUNCTION_PUTA(g_interp, g_interp->Global, "println", g_println, 1, 0);
		SEE_CFUNCTION_PUTA(g_interp, g_interp->Global, "include", g_include, 1, 0);
		SEE_CFUNCTION_PUTA(g_interp, g_interp->Global, "version", g_version, 1, 0);
		
		/* evaluate bootstrapping code, and then script, then transform events */
		evaluate_file(g_interp, &g_result, "/Users/alan/working/golf/boot/serverside.js");
		evaluate_file(g_interp, &g_result, cgiPathTranslated);
		
		/* this is dangerous lol.  should check to make sure the callback is a function */
		if(callback)
			evaluate_string(g_interp, &g_result, function_call);
		
		/* this converts elements with onclick event attributes into links */
		evaluate_file(g_interp, &g_result, "/Users/alan/working/golf/boot/transform.js");
		evaluate_string(g_interp, &g_result, "document.render();");
		
	} else {
		
		fprintf(cgiOut, "<html>\n<head>\n");
		fprintf(cgiOut, "<script type = \"text/javascript\">\n");
		fprintf(cgiOut, "function boot() {\n");
		print_file("/Users/alan/working/golf/boot/clientside.js");
		print_file(cgiPathTranslated);
		fprintf(cgiOut, "\n}\n");
		fprintf(cgiOut, "</script>\n</head>\n");
		fprintf(cgiOut, "<body onload = \"boot();\">\n");
		fprintf(cgiOut, "</body>\n</html>");
		
	}
	
	return EXIT_SUCCESS;
}
Exemplo n.º 9
0
static int get_power_status(int group_idx,int serial_idx,int power_idx)
{
	int value;
	char name[81];
	memset(name,0,sizeof(name));
	sprintf(name,"select%d_%d_%d",group_idx,serial_idx+1,power_idx+1);

	cgiFormInteger(name, &value, 0);
	if(0 == value)
		return 0;
	else
		return 10;
}
Exemplo n.º 10
0
int cgiMain()
{
	char send_cb_string[GL_CALLBACK_MAX_SIZE];
	int send_cb_len;

	cgiFormResultType cgi_re;
	int res;
	int id_value, do_status;

	cgiHeaderContentType("application/json"); //MIME

	char *ipaddr = getenv("REMOTE_ADDR");
	URLAPI_DEBUG("remote ip=%s\n",ipaddr);

	cgi_re = cgiFormInteger("tid", &id_value, 0);
	cgi_re = cgiFormInteger("enable", &do_status, 0);

	res = db_init();
	if(res <0) {
		goto all_over;
	}
	res = do_time_action_db(id_value, do_status);
	if(res <0) {
		goto all_over;
	}

all_over:
	db_close();
	api_response(res, id_value, do_status);

	if((send_cb_len = cJsonEnableTimeAction_callback(send_cb_string, ENABLE_TA_SUBID, res, id_value, do_status)) >=0) {
		push_to_CBDaemon(send_cb_string, send_cb_len);
		URLAPI_DEBUG("[time]=%ld,callback from [enableTimeAction]=%s\n", time(NULL), send_cb_string);
	}
	return 0;
}
Exemplo n.º 11
0
int cgiMain() {
	int sensor_num,sensor_idx;
	int i,j,k;
	char name[81];
	light_sensor *p_sensor ;

	cgiFormInteger("sensor_num", &sensor_num, 0);
	
	if(sensor_num > 100) sensor_num = 100;

	cgiWriteEnvironment("/CHANGE/THIS/PATH/capcgi.dat");
	cgiHeaderContentType("text/html");

	OutHead();
	OutBodyStart();

	if(0 == check_password())
		return 0;

	if(0 == sensor_num)
	{
		fprintf(cgiOut, "<p>数据出错,退出!!</p>\n");
		OutBodyEnd();	
		return 0;
	}
	else
	{
		pwsw_h.sensor_num= sensor_num;

		for(i=0;i<sensor_num;i++)
		{
			p_sensor = &pwsw_h.sensor[i];
			p_sensor->lux = get_lux(i+1);
			p_sensor->voltage = get_voltage(i+1);
		}
	}

	save_to_xml_file();
	fprintf(cgiOut, "<p>保存成功,请返回!</p>\n");
	fprintf(cgiOut, "&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" name=\"rest\" onclick=\"javascript:history.go(-1)\" value=\"重新载入\" />\n");

	OutBodyEnd();

	return 0;
}
Exemplo n.º 12
0
int cgiMain() {
	int sensor_num;
	
	sensor_num = 0;
	cgiFormInteger("sensor_num", &sensor_num, 0);

	pdlc_load_light_sensor_from_xml();

	if(sensor_num > 50) sensor_num = 50;
	
	if(0 == sensor_num)
	{
		sensor_num = pwsw_h.sensor_num;
	}
	else
	{
		pwsw_h.sensor_num = sensor_num;
	}

	cgiWriteEnvironment("/CHANGE/THIS/PATH/capcgi.dat");
	CookieSet();
	cgiHeaderContentType("text/html");

	OutHead();
	OutBodyStart();

	if(0 == check_password())
		return 0;

	//fprintf(cgiOut, "<p>&nbsp; sensor_num=%d -%d</p>\n",pwsw_h.sensor_num,sensor_num);
	
	OutBodyConfigForm(sensor_num);
	OutGroup(sensor_num);


	OutBodyEnd();

	return 0;
}
Exemplo n.º 13
0
int cgiMain()
{
	char send_cb_string[GL_CALLBACK_MAX_SIZE];
	int send_cb_len;

	cgiFormResultType cgi_re;
	int res;
	int sid;

	cgiHeaderContentType("application/json"); //MIME
	char *ipaddr = getenv("REMOTE_ADDR");
	SCENE_DEBUG("remote ip=%s\n",ipaddr);

	//read sid
	cgi_re = cgiFormInteger("sid", &sid, 0);

	res = db_init();
	if(res<0){
		goto all_over;
	}
	//delete database
	res = del_scene_db(sid);
all_over:
	db_close();
	//respond
    api_response(res, sid);

    //push to cb_daemon
#ifdef NO_CALLBAK_DEBUG
#else
	if((send_cb_len = cJsonDelDoScene_callback(send_cb_string, sid, DEL_SUBID_SCENE, res)) >=0) {
		push_to_CBDaemon(send_cb_string, send_cb_len);
		SCENE_DEBUG("[time]=%ld,callback=%s\n", time(NULL), send_cb_string);
	}
#endif
	return 0;
}
Exemplo n.º 14
0
int cgiMain() {
  /* Send the content type, letting the browser know this is HTML */
  cgiHeaderContentType("text/html");
  /* Top of the page */
  fprintf(cgiOut, "<HTML><HEAD>\n");
  fprintf(cgiOut, "<TITLE>renew_license</TITLE></HEAD>\n");
  fprintf(cgiOut, "<BODY><H1>renew_license</H1>\n");

  int unique_id;
  if(cgiFormInteger(CGI_INPUT_UNIQUE_ID, &unique_id, 
    CGI_INPUT_DEFAULT_UNIQUE_ID) != cgiFormSuccess) {
    fprintf(cgiOut, 
      "<P>ERROR: Missing or invalid input field <code>%s</code>.", 
      CGI_INPUT_UNIQUE_ID);
    goto the_end;  
  };

  char licensee_email[CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH];
  if(cgiFormStringNoNewlines(CGI_INPUT_LICENSEE_EMAIL, licensee_email,
    CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH) 
    != cgiFormSuccess) {
    fprintf(cgiOut, 
      "<P>ERROR: Missing or invalid input field <code>%s</code>.", 
      CGI_INPUT_LICENSEE_EMAIL);
    goto the_end;  
  };

  /* Connect to mySQL server */
  MYSQL *mysql = mysql_init(NULL);
  if(!mysql) {
    fprintf(cgiOut, 
      "<P>ERROR: Could not initialize mySQL handle."); 
    goto the_end;
  }
  if(!(mysql_real_connect(mysql, MYSQL_HOST, MYSQL_USERID, MYSQL_PASSWD,
    MYSQL_DB_NAME, 0, NULL, CLIENT_ODBC))) {
    fprintf(cgiOut, 
      "<P>ERROR: Could not connect to database."); 
    goto the_end;
  }
  /* Execute query */
  char query[1024];  
  snprintf(query, 1024, 
    "SELECT * FROM licenses WHERE unique_id=%d AND "
    "licensee_email='%s' AND "
    "valid_until < DATE_ADD(CURDATE(), INTERVAL 1 MONTH);\0", 
    unique_id, licensee_email); 
  if(mysql_query(mysql, query)) {
    fprintf(cgiOut, 
      "<P>ERROR: Problem executing query."); 
    goto before_the_end;
  };
  MYSQL_RES *result;  
  result = mysql_store_result(mysql);
  if(result) {
    /* mysql_num_rows() returns my_ulonglong==unsigned long */
    my_ulonglong num_records = mysql_num_rows(result);
    if(num_records == 0) {
      fprintf(cgiOut, 
      "<P>ERROR: Did not find license with "
      "number <code>%d</code>, " 
      "e-mail <code>%s</code>, expiring within 1 month. "
      "Re-check data validity and try again.", 
      unique_id, 
      licensee_email); 
      goto before_the_end;
    } 
    MYSQL_ROW row = mysql_fetch_row(result);
    fprintf(cgiOut, "Debug info (remove it from final build):<br>");
    fprintf(cgiOut, "<p>unique_id: <code>%s</code>", row[0]);
    fprintf(cgiOut, "<p>licensee_email: <code>%s</code>", row[2]);
    fprintf(cgiOut, "<p>valid_until: <code>%s</code>", row[7]);
    fprintf(cgiOut, "<p>notes: <code>%s</code>", 
      row[33] ? row[33] : "NULL");
    /* Update mySQL database with new expiry date and add note */
    snprintf(query, 1024, 
      "UPDATE licenses SET "
      "valid_until=DATE_ADD(NOW(), INTERVAL 1 YEAR), "
      "notes=CONCAT(COALESCE(notes, ''), '\nProlonged on ', CURDATE(), '.') "
      "WHERE unique_id=%d;", unique_id);
    fprintf(cgiOut, 
      "<P>Should execute the followin query now "
      "(uncomment next line in source to do so): <BR>"
      "<code>%s</code>\n", query);
    /* if(mysql_query(mysql, query)) {
      fprintf(cgiOut, "<P>ERROR: Problem executing query."); 
      goto before_the_end;
    } */
    /* Generate and email license */
    char command_line[COMMAND_LINE_BUFFER_LENGTH];
    snprintf(command_line, COMMAND_LINE_BUFFER_LENGTH, 
      "license_gen -m %d", unique_id);
    if(!(system(command_line))) {
      fprintf(cgiOut, 
      "<P>Your new license key (%s) was generated and "
      "e-mailed to your filed e-mail address (%s).",
      row[0], 
      row[2]);
    } else {
      fprintf(cgiOut, "<P>ERROR: Could not launch subshell." );
    }
    mysql_free_result(result);
  } else {
    fprintf(cgiOut, 
      "<P>ERROR: Problem fetching results from database."); 
    goto before_the_end;
  }

  /* Terminate mysql session */
before_the_end:
  mysql_close(mysql);

  /* Finish up the page */
the_end:
  fprintf(cgiOut, "</BODY></HTML>\n");
  return 0;
}
Exemplo n.º 15
0
int cgiMain() {

  static char      title[]           = "List of existing Certificates";
         char      sorting[16]       = "desc";
         char      certfilestr[225]  = "";
         FILE      *certfile         = NULL;
         BIO       *membio           = NULL;
         BIO       *outbio           = NULL;
         char      membio_buf[128]   = "";
         X509      *cert             = NULL;
         X509_NAME *certsubject      = NULL;
         ASN1_TIME *start_date       = NULL;
         ASN1_TIME *expiration_date  = NULL;
  struct tm        start_tm;
  struct tm        expiration_tm;
         time_t    now               = time(NULL);
         time_t    start             = time(NULL);
         time_t    expiration        = time(NULL);
         double    available_secs    = 0;
         double    remaining_secs    = 0;
  struct dirent    **certstore_files = NULL;
         int       pagenumber        = 1;
         int       certcounter       = 0;
         int       tempcounter       = 0;
         int       pagecounter       = 0;
         int       dispcounter       = 0;
         int       dispmaxlines      = 0;
         int       certvalidity      = 0;
         div_t     disp_calc;
         div_t     oddline_calc;
         double    percent           = 0;

         cert                       = X509_new();
         certsubject                = X509_NAME_new();

/* -------------------------------------------------------------------------- *
 * Get the list of .pem files from the cert directory                         *
 * ---------------------------------------------------------------------------*/
  certcounter = scandir(CACERTSTORE, &certstore_files, file_select, hexsort);
  if(certcounter<=0) int_error("Error: No certificate files found.");

/* -------------------------------------------------------------------------- *
 * calculate how many pages we get with MAXCERTDISPLAY                         *
 * ---------------------------------------------------------------------------*/

  if(certcounter<=MAXCERTDISPLAY) pagecounter = 1;
  else {
    disp_calc = div(certcounter, MAXCERTDISPLAY);
    /* if the count of certs divided by MAXCERTDISPLAY has no remainder */
    if(disp_calc.rem == 0) pagecounter = disp_calc.quot;
    /* with a remainder, we must prepare an extra page for the rest */
    else pagecounter = disp_calc.quot +1;
  }

/* -------------------------------------------------------------------------- *
 * Check if we have been subsequently called with a pagenumber & sort request *
 * ---------------------------------------------------------------------------*/

  if(cgiFormInteger("page", &pagenumber, 1) == cgiFormSuccess)
    if(pagenumber > pagecounter || pagenumber <=0)
      int_error("Error: Page does not exist.");

  if(cgiFormString("sort", sorting, sizeof(sorting)) != cgiFormSuccess)
      strncpy(sorting, "desc", sizeof(sorting));

/* -------------------------------------------------------------------------- *
 * now we know how many certs we have in total and we can build the page(s).  *
 * For every MAXCERTDISPLAY certs we start a new page and cycle through by    *
 * calling ourself with the requested certs in range.                         *
 * ---------------------------------------------------------------------------*/

  if(strcmp(sorting, "asc") == 0) {

    if(certcounter <= MAXCERTDISPLAY) {
       dispmaxlines = certcounter;
       tempcounter = 0;
    }
    else
      if(pagenumber == pagecounter &&
             ( pagecounter * MAXCERTDISPLAY) - certcounter != 0) {

        tempcounter = (pagecounter * MAXCERTDISPLAY) - MAXCERTDISPLAY;
        dispmaxlines = certcounter - ((pagecounter-1) * MAXCERTDISPLAY);
      }
      else {

        tempcounter = (pagenumber * MAXCERTDISPLAY) - MAXCERTDISPLAY;
        dispmaxlines = MAXCERTDISPLAY;
      }
  }

  if(strcmp(sorting, "desc") == 0) {

    if(certcounter <= MAXCERTDISPLAY) {
       dispmaxlines = certcounter;
       tempcounter = certcounter;
    }
    else
      if(pagenumber == pagecounter &&
             ( pagecounter * MAXCERTDISPLAY) - certcounter != 0) {

        tempcounter = certcounter - ((pagecounter-1) * MAXCERTDISPLAY);
        dispmaxlines = certcounter - ((pagecounter-1) * MAXCERTDISPLAY);
      }
      else {

       tempcounter = certcounter - (pagenumber*MAXCERTDISPLAY) + MAXCERTDISPLAY;
       dispmaxlines = MAXCERTDISPLAY;
      }
  }

/* -------------------------------------------------------------------------- *
 * start the html output                                                      *
 * ---------------------------------------------------------------------------*/

  outbio = BIO_new(BIO_s_file());
  BIO_set_fp(outbio, cgiOut, BIO_NOCLOSE);

  pagehead(title);

  //debugging only:
  //printf("Number of certs: %d\n", certcounter);
  //printf("Num tempcounter: %d\n", tempcounter);
  //printf("Number of pages: %d\n", pagecounter);
  //printf("Div Quotient: %d\n", disp_calc.quot);
  //printf("Div Remainder: %d\n", disp_calc.rem);
  //fprintf(cgiOut, "</BODY></HTML>\n");
  //exit(0);

/* -------------------------------------------------------------------------- *
 * start the form output                                                      *
 * ---------------------------------------------------------------------------*/

   fprintf(cgiOut, "<table>\n");
   fprintf(cgiOut, "<tr>\n");
   fprintf(cgiOut, "<th width=\"20\">");
   fprintf(cgiOut, "#");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "<th width=\"495\">");
   fprintf(cgiOut, "Certificate Subject Information");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "<th colspan=\"2\" width=\"60\">");
   fprintf(cgiOut, "Expiry");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "<th width=\"65\">");
   fprintf(cgiOut, "Action");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "</tr>\n");

  for(dispcounter=0; dispcounter < dispmaxlines; dispcounter++) {

    /* zero certificate values and flags */
    certvalidity = 0;
    percent = 0;
    available_secs = 0;
    remaining_secs = 0;
    cert = X509_new();
    certsubject = X509_NAME_new();

    if(strcmp(sorting, "desc") == 0) tempcounter--;

    snprintf(certfilestr, sizeof(certfilestr), "%s/%s",
                           CACERTSTORE, certstore_files[tempcounter]->d_name);

    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th rowspan=\"2\">");
    fprintf(cgiOut, "%d", tempcounter+1);
    fprintf(cgiOut, "</th>\n");

    oddline_calc = div(tempcounter+1, 2);
    if(oddline_calc.rem)
      fprintf(cgiOut, "<td rowspan=\"2\" class=\"odd\">");
    else
      fprintf(cgiOut, "<td rowspan=\"2\" class=\"even\">");

    if ( (certfile = fopen(certfilestr, "r")) != NULL) {
      PEM_read_X509(certfile, &cert, NULL, NULL);
      certsubject = X509_get_subject_name(cert);

      /* display the subject data, use the UTF-8 flag to show  *
       * Japanese Kanji, also needs the separator flag to work */
      X509_NAME_print_ex_fp(cgiOut, certsubject, 0,
         ASN1_STRFLGS_UTF8_CONVERT|XN_FLAG_SEP_CPLUS_SPC);

      /* store certificate start date for later eval */
      start_date = X509_get_notBefore(cert);

      /* store certificate expiration date for later eval */
      expiration_date = X509_get_notAfter(cert);

      /* check the start and end dates in the cert */
      if (X509_cmp_current_time (X509_get_notBefore (cert)) >= 0)
        /* flag the certificate as not valid yet */
        certvalidity = 0;
      else
      if (X509_cmp_current_time (X509_get_notAfter (cert)) <= 0)
        /* flag the certificate as expired */
        certvalidity = 0;
      else 
        /* flag the certificate is still valid */
        certvalidity = 1;

      fclose(certfile);
    }
    else 
       fprintf(cgiOut, "Error: Can't open certificate file %s for reading.",
                                                                 certfilestr);
    fprintf(cgiOut, "</td>\n");

    if(certvalidity == 0) {

      /* expiration bar display column */
      fprintf(cgiOut, "<th rowspan=\"2\">\n");
      fprintf(cgiOut, "<table class=\"led\">\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "</table>\n");
      fprintf(cgiOut, "</th>\n");

      /* remaining days before expiration column */
      fprintf(cgiOut, "<th class=\"exnok\" rowspan=\"2\">\n");
      fprintf(cgiOut, "Inval<br />Expd");
      fprintf(cgiOut, "</th>\n");
    }

    if(certvalidity == 1) {

      /* ------ START get the certificate lifetime in seconds ------ */
      /* copy the start date into a string */
      membio = BIO_new(BIO_s_mem());
      ASN1_TIME_print(membio, start_date);
      BIO_gets(membio, membio_buf, sizeof(membio_buf));
      BIO_free(membio);

      /* parse the start date string into a time struct */
      memset (&start_tm, '\0', sizeof(start_tm));
      strptime(membio_buf, "%h %d %T %Y %z", &start_tm);
      start = mktime(&start_tm);

      /* ------ START get the certificate remaining time in seconds ------ */
      /* copy the expiration date into a string */
      membio = BIO_new(BIO_s_mem());
      ASN1_TIME_print(membio, expiration_date);
      BIO_gets(membio, membio_buf, sizeof(membio_buf));
      BIO_free(membio);
  
      /* parse the expiration date string into a time struct */
      memset (&expiration_tm, '\0', sizeof(expiration_tm));
      strptime(membio_buf, "%h %d %T %Y %z", &expiration_tm);
  
      /* get the current time */
      now = time(NULL);
      expiration = mktime(&expiration_tm);
  
      /* get the time difference between expiration time and current time */
      remaining_secs = difftime(expiration, now);
      /* ------ END get the certificate remaining time in seconds ------ */

      /* get the time difference between start and expiration time */
      available_secs = difftime(expiration, start);
      /* ------ END get the certificate lifetime in seconds ------ */
  
      /* ------ START calculate percentage of lifetime left ------ */
      /* remaining_secs *100                                       */
      /* ------------------- = X, rounded down with floor()        */
      /* available_secs                                            */
      percent = floor((remaining_secs*100)/available_secs);
      /* ------ END calculate percentage of lifetime left   ------ */
  
      /* expiration bar display column */
      fprintf(cgiOut, "<th rowspan=\"2\">\n");
      fprintf(cgiOut, "<table class=\"led\">\n");
      if (percent >= 90) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#00FF00\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 80) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#00FF33\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 70) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#99FF33\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 60) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#FFFF00\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 50) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#FFCC00\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 40) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#FF9900\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 30) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#FF6600\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 20) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#FF3300\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 10) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=\"#FF0000\"></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "</table>\n");
      fprintf(cgiOut, "</th>\n");
  
      /* remaining days before expiration column */
      //fprintf(cgiOut, membio_buf);
      if (percent < 10) fprintf(cgiOut, "<th class=\"exnok\" rowspan=\"2\">\n");
      else fprintf(cgiOut, "<th class=\"exok\" rowspan=\"2\">\n");
      if(floor(remaining_secs/63072000) > 0) fprintf(cgiOut, "%.f<br />years", remaining_secs/31536000);
      else if(floor(remaining_secs/86400) > 0 ) fprintf(cgiOut, "%.f<br />days", remaining_secs/86400);
      else if(floor(remaining_secs/3600) > 0 ) fprintf(cgiOut, "%.f<br />hours", remaining_secs/3600);
      else if(floor(remaining_secs/60) > 0 ) fprintf(cgiOut, "%.f<br />mins", remaining_secs/60);
      else fprintf(cgiOut, "%.f<br />secs", remaining_secs);
      fprintf(cgiOut, "</th>\n");
    }

    /* action column */
    fprintf(cgiOut, "<th>");
    fprintf(cgiOut, "<form action=\"getcert.cgi\" method=\"post\">\n");
    fprintf(cgiOut, "<input type=\"hidden\" name=\"cfilename\" ");
    fprintf(cgiOut, "value=\"%s\" />\n", certstore_files[tempcounter]->d_name);
    fprintf(cgiOut, "<input type=\"hidden\" name=\"format\" value=\"text\" />\n");
    fprintf(cgiOut, "<input class=\"getcert\" type=\"submit\" value=\"Detail\" />\n");
    fprintf(cgiOut, "</form>\n");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th>\n");

    fprintf(cgiOut, "<form action=\"certrenew.cgi\" method=\"post\">\n");
    fprintf(cgiOut, "<input type=\"hidden\" name=\"cert-renew\" ");
    fprintf(cgiOut, "value=\"");
    PEM_write_bio_X509(outbio, cert);
    fprintf(cgiOut, "\" />\n");
    fprintf(cgiOut, "<input class=\"getcert\" type=\"submit\" value=\"Renew\" />\n");
    fprintf(cgiOut, "</form>\n");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "</tr>\n");

    if(strcmp(sorting, "asc") == 0) tempcounter++;
  }

  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th colspan=\"5\">");
  fprintf(cgiOut, "Total # of certs: %d | ", certcounter);
  fprintf(cgiOut, "Page %d of %d", pagenumber, pagecounter);
  fprintf(cgiOut, "</th>\n");
  fprintf(cgiOut, "</tr>\n");
  fprintf(cgiOut, "</table>\n");

  fprintf(cgiOut, "<p></p>\n");

  fprintf(cgiOut, "<table>\n");

  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th>\n");
  fprintf(cgiOut, "<form action=\"certstore.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"sort\" ");
  fprintf(cgiOut, "value=\"desc\" />\n");
  fprintf(cgiOut, "<input type=\"submit\" name=\"sort\"");
  fprintf(cgiOut, " value=\"Latest Certs first\" />\n");
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  fprintf(cgiOut, "<th>\n");
  fprintf(cgiOut, "<form action=\"certstore.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"sort\" ");
  fprintf(cgiOut, "value=\"asc\" />\n");
  fprintf(cgiOut, "<input type=\"submit\" name=\"sort\"");
  fprintf(cgiOut, " value=\"Oldest Certs first\" />\n");
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  // filler 1
  fprintf(cgiOut, "<th width=\"15\">");
  fprintf(cgiOut, "&nbsp;");
  fprintf(cgiOut, "</th>\n");

  // goto page 1
  fprintf(cgiOut, "<th width=\"5\">\n");
  fprintf(cgiOut, "<form action=\"certstore.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"submit\" value=\"&lt;&lt;\" />\n");
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  // goto page before
  fprintf(cgiOut, "<th width=\"5\">\n");
  fprintf(cgiOut, "<form action=\"certstore.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", certcounter);
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", pagecounter);
  fprintf(cgiOut, "<input type=\"hidden\" name=\"page\" ");
  fprintf(cgiOut, "value=\"");
  tempcounter = 0;
  if(pagenumber > 1) tempcounter = pagenumber - 1;
  else tempcounter = 1;
  fprintf(cgiOut, "%d", tempcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"submit\" value=\"&lt; 1\" />\n");
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  // goto page after
  fprintf(cgiOut, "<th width=\"5\">\n");
  fprintf(cgiOut, "<form action=\"certstore.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", certcounter);
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", pagecounter);
  fprintf(cgiOut, "<input type=\"hidden\" name=\"page\" ");
  fprintf(cgiOut, "value=\"");
  tempcounter = 0;
  if(pagecounter > pagenumber) tempcounter = pagenumber + 1;
  else tempcounter = pagecounter;
  fprintf(cgiOut, "%d", tempcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"submit\" value=\"1 &gt;\" />\n");
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  // goto last page
  fprintf(cgiOut, "<th width=\"5\">\n");
  fprintf(cgiOut, "<form action=\"certstore.cgi\" method=\"post\">");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", certcounter);
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", pagecounter);
  fprintf(cgiOut, "<input type=\"hidden\" name=\"page\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", pagecounter);
  fprintf(cgiOut, "<input type=\"submit\" value=\"&gt;&gt;\" />\n");
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  // goto page number
  fprintf(cgiOut, "<th width=\"120\">\n");
  fprintf(cgiOut, "<form class=\"setpage\" action=\"certstore.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", certcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", pagecounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input class=\"goto\" type=\"submit\" value=\"Goto\" />\n");
  fprintf(cgiOut, "<input class=\"page\" type=\"text\" name=\"page\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", pagecounter);
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");
  fprintf(cgiOut, "</tr>\n");
  fprintf(cgiOut, "</table>\n");

/* ---------------------------------------------------------------------------*
 * end the html output                                                        *
 * ---------------------------------------------------------------------------*/
  pagefoot();

  BIO_free(outbio);
  return(0);
}
Exemplo n.º 16
0
int cgiMain(void) 
{    
	int ret;
	key_t key; 
	key_t key_cgi;
	int flag;

	int shm_id,sem_id, sem_id_cgi; 
	char *shm; 

	// 创建并关联共享内存
	key       = ftok("./vs_shm",0); 
	key_cgi  = ftok("./vs_shm_cgi",0); 
	shm_id  = shmget(key,SEGSIZE,0); 
	if(-1 == shm_id)
	{
		cgiHeaderContentType("text/html; charset=gb2312");
		puts("<html><head><title>share memeroy error</title><h1><font color='red'>create shared memory error</f><h1><html><head>"); 
		return -1; 
	} 

	shm = (char *)shmat(shm_id,0,0);
	if (-1 == (int)shm)
	{ 
		cgiHeaderContentType("text/html; charset=gb2312");
		puts(" attach shared memory error\n"); 
		return -1; 
	} 

	// 创建互斥锁
	sem_id     = sem_creat(key);                      // vs程序操作共享内存信号量
	sem_id_cgi = sem_creat(key_cgi);                  // 外部程序cgi操作共享内存信号量

	cgiFormInteger("flag", &flag, 1);
	if(1 == flag){				//getData
		cgi_rtv_get_action(shm);
		post_v(sem_id);             // 使能vs程序操作共享内存信号量
		wait_v(sem_id_cgi);

		ret = *(int *)shm;
		if(0 != ret )                          
		{
			char temp[32];
			sprintf(temp,"error is %d\n",ret);
			cgiHeaderContentType("text/html; charset=gb2312");
			puts("<html><head><title>rtv get error</title><h1><font color='red'>wrong username or password</f><h1>"); 
			puts(temp);
			return -1;    
		}
		
		cgi_rtv_puts(shm);

	}
	else if(0 == flag){
		cgi_rtv_set_action(shm);
		post_v(sem_id);             // 使能vs程序操作共享内存信号量
		wait_v(sem_id_cgi);

		ret = *(int *)shm;
		if(0 != ret )                          
		{
			char temp[32];
			sprintf(temp,"error is %d\n",ret);
			cgiHeaderContentType("text/html; charset=gb2312");
			puts("<html><head><title>login error</title><h1><font color='red'>wrong username or password</f><h1>"); 
			puts(temp);
			return -1;    
		}
		
		cgi_rtv_puts(shm);
	}

	shmdt(shm); 

	return 0; 
} 
Exemplo n.º 17
0
void cgi_rtv_set_action(char * shm){
	s_var_video *rtv_setting = (s_var_video *)(shm+sizeof(int)*3);

	*(int *)(shm+sizeof(int)) = ID_CGI_RTV_SET_ACTION;
	*(int *)(shm+sizeof(int)*2) = CGI_SET_DATA;

	memset(rtv_setting, 0, sizeof(s_var_video));

	cgiFormInteger("solution_x_1", &rtv_setting->solution_x_1, 352);
	cgiFormInteger("solution_y_1", &rtv_setting->solution_y_1, 288);
	cgiFormInteger("bright_1", &rtv_setting->bright_1, 20);
	cgiFormInteger("contrast_1", &rtv_setting->contrast_1, 30);
	cgiFormInteger("frame_interval_1", &rtv_setting->frame_interval_1, 0);
	
	cgiFormInteger("speed_1", &rtv_setting->speed_1, 64);
	cgiFormInteger("cache_time_1", &rtv_setting->cache_time_1, 10);

	cgiFormInteger("solution_x_2", &rtv_setting->solution_x_2, 352);
	cgiFormInteger("solution_y_2", &rtv_setting->solution_y_2, 288);
	cgiFormInteger("bright_2", &rtv_setting->bright_2, 20);
	cgiFormInteger("contrast_2", &rtv_setting->contrast_2, 30);
	cgiFormInteger("frame_interval_2", &rtv_setting->frame_interval_2, 0);
	
	cgiFormInteger("speed_2", &rtv_setting->speed_2, 64);
	cgiFormInteger("cache_time_2", &rtv_setting->cache_time_2, 10);
}
Exemplo n.º 18
0
void Frogs() {
	int frogsEaten;
	cgiFormInteger("frogs", &frogsEaten, 0);
	fprintf(cgiOut, "I have eaten %d frogs.<BR>\n", frogsEaten);
}
Exemplo n.º 19
0
int cgiMain() {
	int power_num,group_serial_num[5],group_num,group_idx;

	
	power_num = 8;
	group_num = 6;
	cgiFormInteger("power_num", &power_num, 0);
	cgiFormInteger("group_num", &group_num, 0);
	cgiFormInteger("group1_num", &group_serial_num[0], 0);
	cgiFormInteger("group2_num", &group_serial_num[1], 0);
	cgiFormInteger("group3_num", &group_serial_num[2], 0);
	cgiFormInteger("group4_num", &group_serial_num[3], 0);
	cgiFormInteger("group5_num", &group_serial_num[4], 0);

	pdlc_load_pwctrl_from_xml();

	if(power_num > 100) power_num = 100;
	if(group_num > 5) group_num = 5;
	if(group_serial_num[0] > 100) group_serial_num[0] = 100;
	if(group_serial_num[1] > 100) group_serial_num[1] = 100;
	if(group_serial_num[2] > 100) group_serial_num[2] = 100;
	if(group_serial_num[3] > 100) group_serial_num[3] = 100;
	if(group_serial_num[4] > 100) group_serial_num[4] = 100;
	
	if(0 == power_num)
	{
		power_num = pwsw_h.power_num;
		group_num = pwsw_h.group_num;
		group_serial_num[0] = pwsw_h.group[0].ctrl_serial_num;
		group_serial_num[1] = pwsw_h.group[1].ctrl_serial_num;
		group_serial_num[2] = pwsw_h.group[2].ctrl_serial_num;
		group_serial_num[3] = pwsw_h.group[3].ctrl_serial_num;
		group_serial_num[4] = pwsw_h.group[4].ctrl_serial_num;
	}
	else
	{
#if 1	
		pwsw_h.power_num = power_num;
		pwsw_h.group_num = group_num;
		pwsw_h.group[0].ctrl_serial_num = group_serial_num[0];
		pwsw_h.group[1].ctrl_serial_num = group_serial_num[1];
		pwsw_h.group[2].ctrl_serial_num = group_serial_num[2];
		pwsw_h.group[3].ctrl_serial_num = group_serial_num[3];
		pwsw_h.group[4].ctrl_serial_num = group_serial_num[4];
#endif
	}

	cgiWriteEnvironment("/CHANGE/THIS/PATH/capcgi.dat");
	CookieSet();
	cgiHeaderContentType("text/html");

	OutHead();
	OutBodyStart();

	if(0 == check_password())
		return 0;
	
	OutBodyConfigForm(power_num,group_num,group_serial_num[0],group_serial_num[1],
		group_serial_num[2],group_serial_num[3],group_serial_num[4]);

//	fprintf(cgiOut, "<p>power_num=%d group_num=%d [%d-%d-%d-%d-%d]</p>\n",
//		power_num,group_num,group_serial_num[0],group_serial_num[1],
//		group_serial_num[2],group_serial_num[3],group_serial_num[4]);

	for(group_idx=0;group_idx<pwsw_h.group_num;group_idx++)
	{
		OutGroup(pwsw_h.power_num,pwsw_h.group[group_idx].ctrl_serial_num,group_idx);
	}

	OutBodyEnd();

	return 0;
}
Exemplo n.º 20
0
int cgiMain()
{
	char send_cb_string[GL_CALLBACK_MAX_SIZE];
	int send_cb_len;

	scene_base_st scene_base;
	scene_action_stPtr scene_action;

	cgiFormResultType cgi_re;
	int res;

	cgiHeaderContentType("application/json"); //MIME
	char *ipaddr = getenv("REMOTE_ADDR");
	SCENE_DEBUG("remote ip=%s\n",ipaddr);

	//read name
	cgi_re = cgiFormString("scnname", scene_base.scnname, NAME_STRING_LEN + 1);

	//read action para
	cgi_re = cgiFormString("scnaction", scene_base.scnaction, SCENEACTION_MAX_LEN+1);

	//read index
	cgi_re = cgiFormInteger("scnindex", &scene_base.scnindex, 0);

	//read sid
	cgi_re = cgiFormInteger("sid", &scene_base.sid, 0);

//	printf("sid=%d\n",scene_base.sid);

	res = db_init();
	if(res<0){
		goto all_over;
	}
	//generate scene_action_st
	scene_action = scnaction_st_gener_malloc(scene_base.scnaction);							//malloc1
	if(scene_action == NULL){
		res = ERROR_GENERATE_URL_STRING;
		goto all_over;
	}

	//modify database
	res = modify_scene_db(&scene_base, scene_action);
	if(res<0){
		goto all_over;
	}

all_over:
	db_close();
    if(res<0){
    	scene_base_st *p;
    	p = &scene_base;
    	p = NULL;
    	api_error_res(res);
    }
    else{
    	api_response(&scene_base);
    }
    //push to cb_daemon
#ifdef NO_CALLBAK_DEBUG
#else
	if((send_cb_len = cJsonScene_callback(send_cb_string, &scene_base, NULL, NULL, 2, res)) >=0) {
		push_to_CBDaemon(send_cb_string, send_cb_len);
		SCENE_DEBUG("[time]=%ld,callback=%s\n", time(NULL), send_cb_string);
	}
#endif
    if(scene_action){
    	free(scene_action);																		//free1
    }
	return 0;
}
Exemplo n.º 21
0
int cgiMain() {

  static char      title[256]        = "";
  static char   subtitle[256]        = "";
         char      sorting[16]       = "desc";
         time_t    now               = time(NULL);
         time_t    start             = time(NULL);
         time_t    expiration        = time(NULL);
         double    available_secs    = 0;
         double    remaining_secs    = 0;
  struct dirent    **certstore_files = NULL;
         int       pagenumber        = 1;
         int       certcounter       = 0;
         int       tempcounter       = 0;
         int       pagecounter       = 0;
         int       dispcounter       = 0;
         int       dispmaxlines      = 0;
         int       certvalidity      = 0;
         div_t     disp_calc;
         div_t     oddline_calc;
         double    percent           = 0;

         cert                        = X509_new();
         certsubject                 = X509_NAME_new();
	 char      **form_data       = NULL;  /* string array for query data */

  /* get the current time */
  now = time(NULL);

/* ------------------------------------------------------------------------- *
 * If we are called without arguments, we display the cert search criteria   *
 * ------------------------------------------------------------------------- */
  if (cgiFormEntries(&form_data) != cgiFormSuccess)
    int_error("Error: Could not retrieve CGI form data.");
  if(form_data[0] == NULL) {

    start_tm = *gmtime(&now);

    snprintf(title, sizeof(title), "Search existing Certificates");
    pagehead(title);
    fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"get\">");
    fprintf(cgiOut, "<table>");

    /* Search for Subject String */
    fprintf(cgiOut, "<tr><th colspan=\"5\">Search by Name</th></tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th class=\"cnt\" rowspan=\"2\">\n");
    fprintf(cgiOut, "<input type=\"radio\" value=\"dn\" name=\"search\" />");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "Distinguished Name Field:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    fprintf(cgiOut, "<select name=\"field\">");
    fprintf(cgiOut, "<option value=\"countryName\">Country</option>");
    fprintf(cgiOut, "<option value=\"stateOrProvinceName\">State</option>");
    fprintf(cgiOut, "<option value=\"localityName\">Location</option>");
    fprintf(cgiOut, "<option value=\"organizationName\">Organisation</option>");
    fprintf(cgiOut, "<option value=\"organizationalUnitName\">Department</option>");
    fprintf(cgiOut, "<option value=\"emailAddress\">E-Mail Addr</option>");
    fprintf(cgiOut, "<option selected=\"selected\" value=\"commonName\">Common Name</option>");
    fprintf(cgiOut, "<option value=\"surname\">Surname</option>");
    fprintf(cgiOut, "<option value=\"givenName\">Given Name</option>");
    fprintf(cgiOut, "</select>");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "Search String<br />[20 chars max]:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    fprintf(cgiOut, "<input type=\"text\" size=\"15\" name=\"dnvalue\" value=\"changeme.com\" />");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<td class=\"desc\" colspan=\"4\">\n");
    fprintf(cgiOut, "Search for certificates that have the given string in the selected DN field. ");
    fprintf(cgiOut, "The search is case sensitive, so results for country=us can be different from country=US and country=Us.");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "</tr>\n");

    /* Search for Expiration Date */
    fprintf(cgiOut, "<tr><th colspan=\"5\">Search by Expiration Date</th></tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th class=\"cnt\" rowspan=\"2\">\n");
    fprintf(cgiOut, "<input type=\"radio\" value=\"exp\" name=\"search\" checked=\"checked\" />");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "Expiration Date is<br />between Start Date:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    strftime(membio_buf, sizeof(membio_buf), "%d.%m.%Y", &start_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"9\" name=\"exp_startdate\" value=\"%s\" /> ", membio_buf);
    strftime(membio_buf, sizeof(membio_buf), "%H:%M", &start_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"3\" name=\"exp_starttime\" value=\"%s\" />", membio_buf);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "and End Date<br />[default 90 days]:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    /* set second time 3 months (90 days) into the future: 86400s/d*90d=7776000s */
    expiration = now + 7776000;
    expiration_tm = *gmtime(&expiration);
    strftime(membio_buf, sizeof(membio_buf), "%d.%m.%Y", &expiration_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"9\" name=\"exp_enddate\" value=\"%s\" /> ", membio_buf);
    strftime(membio_buf, sizeof(membio_buf), "%H:%M", &expiration_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"3\" name=\"exp_endtime\" value=\"%s\" />", membio_buf);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<td class=\"desc\" colspan=\"4\">\n");
    fprintf(cgiOut, "Search for certificates that expire(d) between the selected start and end date. ");
    fprintf(cgiOut, "By default, the search is pre-set to find certificates that expire in the next 3 months.");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "</tr>\n");

    /* Search for Enabled Date */
    fprintf(cgiOut, "<tr><th colspan=\"5\">Search by Creation Date</th></tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th class=\"cnt\" rowspan=\"2\">\n");
    fprintf(cgiOut, "<input type=\"radio\" value=\"ena\" name=\"search\" />");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "Enabled Date is<br />between Start Date:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    /* set second time 3 months (90 days) into the past: 86400s/d*90d=7776000s */
    expiration = now - 7776000;
    expiration_tm = *gmtime(&expiration);
    strftime(membio_buf, sizeof(membio_buf), "%d.%m.%Y", &expiration_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"9\" name=\"ena_startdate\" value=\"%s\" /> ", membio_buf);
    strftime(membio_buf, sizeof(membio_buf), "%H:%M", &expiration_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"3\" name=\"ena_starttime\" value=\"%s\" />", membio_buf);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "and End Date<br />[default now]:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    strftime(membio_buf, sizeof(membio_buf), "%d.%m.%Y", &start_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"9\" name=\"ena_enddate\" value=\"%s\" /> ", membio_buf);
    strftime(membio_buf, sizeof(membio_buf), "%H:%M", &start_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"3\" name=\"ena_endtime\" value=\"%s\" />", membio_buf);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<td class=\"desc\" colspan=\"4\">\n");
    fprintf(cgiOut, "Search for certificates that become valid between the selected start and end date. ");
    fprintf(cgiOut, "By default, the search is pre-set to show certificates created in the past 3 months.");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "</tr>\n");

    /* Search for Revocation Date */
    fprintf(cgiOut, "<tr><th colspan=\"5\">Search by Revocation Date</th></tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th class=\"cnt\" rowspan=\"2\">\n");
    fprintf(cgiOut, "<input type=\"radio\" value=\"rev\" name=\"search\" />");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "Revocation Date is<br />between Start Date:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    /* set second time 3 months (90 days) into the past: 86400s/d*90d=7776000s */
    expiration = now - 7776000;
    expiration_tm = *gmtime(&expiration);
    strftime(membio_buf, sizeof(membio_buf), "%d.%m.%Y", &expiration_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"9\" name=\"rev_startdate\" value=\"%s\" /> ", membio_buf);
    strftime(membio_buf, sizeof(membio_buf), "%H:%M", &expiration_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"3\" name=\"rev_starttime\" value=\"%s\"/>", membio_buf);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "and End Date<br />[now]:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    strftime(membio_buf, sizeof(membio_buf), "%d.%m.%Y", &start_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"9\" name=\"rev_enddate\" value=\"%s\" /> ", membio_buf);
    strftime(membio_buf, sizeof(membio_buf), "%H:%M", &start_tm);
    fprintf(cgiOut, "<input type=\"text\" size=\"3\" name=\"rev_endtime\" value=\"%s\" />", membio_buf);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<td class=\"desc\" colspan=\"4\">\n");
    fprintf(cgiOut, "Search for certificates that have been revoked between the selected start and end date. ");
    fprintf(cgiOut, " By default, the search is pre-set to show certificates revoked in the past 3 months.");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "</tr>\n");

    /* Search for Serial Number */
    fprintf(cgiOut, "<tr><th colspan=\"5\">Search by Serial Number</th></tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th class=\"cnt\" rowspan=\"2\">\n");
    fprintf(cgiOut, "<input type=\"radio\" value=\"ser\" name=\"search\" />");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "Serial Number is<br />between Start Serial:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    fprintf(cgiOut, "<input type=\"text\" size=\"14\" name=\"startserial\" ");
    fprintf(cgiOut, "value=\"%s\" style=\"text-align:right;\" />", startserstr);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td class=\"type\">\n");
    fprintf(cgiOut, "and End Serial<br />[max 10e11]:");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "<td class=\"center\">\n");
    fprintf(cgiOut, "<input type=\"text\" size=\"14\" name=\"endserial\" ");
    fprintf(cgiOut, "value=\"%s\" style=\"text-align:right;\" />", endserstr);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<td class=\"desc\" colspan=\"4\">\n");
    fprintf(cgiOut, "Search for certificates whose serial number is between the given ");
    fprintf(cgiOut, "start and end serial number in decimal format. To find a particular certificate, set start and end serial to be equal.");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "</tr>\n");

    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th colspan=\"5\">");
    fprintf(cgiOut, "<input type=\"submit\" value=\"Search Certificates\" />");
    fprintf(cgiOut, "</th>");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "</table>\n");
    fprintf(cgiOut, "</form>\n");
    pagefoot();

  }
  else {
  
  /* ------------------------------------------------------------------- *
   * check if we got the CGI form data                                   *
   * --------------------------------------------------------------------*/
    if ( cgiFormString("search", search, sizeof(search))
                                                     != cgiFormSuccess )
      int_error("Error retrieving CGI form search type.");
    else {
      if (strcmp(search, "dn") == 0) {
        if ( cgiFormString("field", field, sizeof(field))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form DN search field information.");

        if ( cgiFormString("dnvalue", dnvalue, sizeof(dnvalue))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form DN search dnvalue information.");
        snprintf(title, sizeof(title), "Search Certs by Subject");
        snprintf(subtitle, sizeof(subtitle), "Certificates with DN %s=%s", field, dnvalue);
      }
      else if (strcmp(search, "exp") == 0) {
        if ( cgiFormString("exp_startdate", exp_startdate, sizeof(exp_startdate))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form expiration start date.");

        if ( cgiFormString("exp_starttime", exp_starttime, sizeof(exp_starttime))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form expiration start time.");

        if ( cgiFormString("exp_enddate", exp_enddate, sizeof(exp_enddate))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form expiration end date.");

        if ( cgiFormString("exp_endtime", exp_endtime, sizeof(exp_endtime))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form expiration end time.");

        strncat(exp_startstr, exp_startdate, sizeof(exp_startstr)-1);
        strncat(exp_startstr, " ", 1); /* add a space between date and time */
        strncat(exp_startstr, exp_starttime, sizeof(exp_startstr)-strlen(exp_startstr)-1);
        strncat(exp_endstr, exp_enddate, sizeof(exp_endstr)-1);
        strncat(exp_endstr, " ", 1); /* add a space between date and time */
        strncat(exp_endstr, exp_endtime, sizeof(exp_endstr)-strlen(exp_endstr)-1);
        snprintf(title, sizeof(title), "Search Certs by Expiration");
        snprintf(subtitle, sizeof(subtitle), "Certificates with expiration between %s and %s", exp_startstr, exp_endstr);
      }
      else if (strcmp(search, "ena") == 0) {
        if ( cgiFormString("ena_startdate", ena_startdate, sizeof(ena_startdate))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable start date.");

        if ( cgiFormString("ena_starttime", ena_starttime, sizeof(ena_starttime))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable start time.");

        if ( cgiFormString("ena_enddate", ena_enddate, sizeof(ena_enddate))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable end date.");

        if ( cgiFormString("ena_endtime", ena_endtime, sizeof(ena_endtime))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable end time.");

        strncat(ena_startstr, ena_startdate, sizeof(ena_startstr)-1);
        strncat(ena_startstr, " ", 1); /* add a space between date and time */
        strncat(ena_startstr, ena_starttime, sizeof(ena_startstr)-strlen(ena_startstr)-1);
        strncat(ena_endstr, ena_enddate, sizeof(ena_endstr)-1);
        strncat(ena_endstr, " ", 1); /* add a space between date and time */
        strncat(ena_endstr, ena_endtime, sizeof(ena_endstr)-strlen(ena_endstr)-1);
        snprintf(title, sizeof(title), "Search Certs by Start Date");
        snprintf(subtitle, sizeof(subtitle), "Certificates with start date between %s and %s", ena_startstr, ena_endstr);
      }
      else if (strcmp(search, "rev") == 0) {
        if ( cgiFormString("rev_startdate", rev_startdate, sizeof(rev_startdate))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable start date.");

        if ( cgiFormString("rev_starttime", rev_starttime, sizeof(rev_starttime))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable start time.");

        if ( cgiFormString("rev_enddate", rev_enddate, sizeof(rev_enddate))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable end date.");

        if ( cgiFormString("rev_endtime", rev_endtime, sizeof(rev_endtime))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form enable end time.");

        strncat(rev_startstr, rev_startdate, sizeof(rev_startstr)-1);
        strncat(rev_startstr, " ", 1); /* add a space between date and time */
        strncat(rev_startstr, rev_starttime, sizeof(rev_startstr)-strlen(rev_startstr)-1);
        strncat(rev_endstr, rev_enddate, sizeof(rev_endstr)-1);
        strncat(rev_endstr, " ", 1); /* add a space between date and time */
        strncat(rev_endstr, rev_endtime, sizeof(rev_endstr)-strlen(rev_endstr)-1);
        snprintf(title, sizeof(title), "Search Revoked Certificates");
        snprintf(subtitle, sizeof(subtitle), "Certificates revoked between %s and %s", rev_startstr, rev_endstr);
      }
      else if (strcmp(search, "ser") == 0) {
        if ( cgiFormString("startserial", startserstr, sizeof(startserstr))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form start serial value.");

        if ( cgiFormString("endserial", endserstr, sizeof(endserstr))
                                                     != cgiFormSuccess )
        int_error("Error retrieving CGI form end serial value.");
        snprintf(title, sizeof(title), "Search Certs by Serial Number");
        snprintf(subtitle, sizeof(subtitle), "Certificates with serial number between %s and %s", startserstr, endserstr);
      }
      else int_error("Error CGI form retrieving a valid search type.");
    }

/* -------------------------------------------------------------------------- *
 * We got CGI arguments, first we get a list of .pem files from the cert dir  *
 * ---------------------------------------------------------------------------*/
  certcounter = scandir(CACERTSTORE, &certstore_files, file_select, hexsort);
  // It can happen that our search does not return any certs. This is not an error.
  //if(certcounter<=0) int_error("Error: No certificate files found.");

/* -------------------------------------------------------------------------- *
 * calculate how many pages we get with MAXCERTDISPLAY                         *
 * ---------------------------------------------------------------------------*/

  if(certcounter<=MAXCERTDISPLAY) pagecounter = 1;
  else {
    disp_calc = div(certcounter, MAXCERTDISPLAY);
    /* if the count of certs divided by MAXCERTDISPLAY has no remainder */
    if(disp_calc.rem == 0) pagecounter = disp_calc.quot;
    /* with a remainder, we must prepare an extra page for the rest */
    else pagecounter = disp_calc.quot +1;
  }

/* -------------------------------------------------------------------------- *
 * Check if we have been subsequently called with a pagenumber & sort request *
 * ---------------------------------------------------------------------------*/

  if(cgiFormInteger("page", &pagenumber, 1) == cgiFormSuccess)
    if(pagenumber > pagecounter || pagenumber <=0)
      int_error("Error: Page does not exist.");

  if(cgiFormString("sort", sorting, sizeof(sorting)) != cgiFormSuccess)
      strncpy(sorting, "desc", sizeof(sorting));

/* -------------------------------------------------------------------------- *
 * now we know how many certs we have in total and we can build the page(s).  *
 * For every MAXCERTDISPLAY certs we start a new page and cycle through by    *
 * calling ourself with the requested certs in range.                         *
 * ---------------------------------------------------------------------------*/

  if(strcmp(sorting, "asc") == 0) {

    if(certcounter <= MAXCERTDISPLAY) {
       dispmaxlines = certcounter;
       tempcounter = 0;
    }
    else
      if(pagenumber == pagecounter &&
             ( pagecounter * MAXCERTDISPLAY) - certcounter != 0) {

        tempcounter = (pagecounter * MAXCERTDISPLAY) - MAXCERTDISPLAY;
        dispmaxlines = certcounter - ((pagecounter-1) * MAXCERTDISPLAY);
      }
      else {

        tempcounter = (pagenumber * MAXCERTDISPLAY) - MAXCERTDISPLAY;
        dispmaxlines = MAXCERTDISPLAY;
      }
  }

  if(strcmp(sorting, "desc") == 0) {

    if(certcounter <= MAXCERTDISPLAY) {
       dispmaxlines = certcounter;
       tempcounter = certcounter;
    }
    else
      if(pagenumber == pagecounter &&
             ( pagecounter * MAXCERTDISPLAY) - certcounter != 0) {

        tempcounter = certcounter - ((pagecounter-1) * MAXCERTDISPLAY);
        dispmaxlines = certcounter - ((pagecounter-1) * MAXCERTDISPLAY);
      }
      else {

       tempcounter = certcounter - (pagenumber*MAXCERTDISPLAY) + MAXCERTDISPLAY;
       dispmaxlines = MAXCERTDISPLAY;
      }
  }

/* -------------------------------------------------------------------------- *
 * start the html output                                                      *
 * ---------------------------------------------------------------------------*/

  pagehead(title);

  //debugging only:
  //printf("Number of certs: %d\n", certcounter);
  //printf("Num tempcounter: %d\n", tempcounter);
  //printf("Number of pages: %d\n", pagecounter);
  //printf("Div Quotient: %d\n", disp_calc.quot);
  //printf("Div Remainder: %d\n", disp_calc.rem);
  //fprintf(cgiOut, "</BODY></HTML>\n");
  //exit(0);

/* -------------------------------------------------------------------------- *
 * start the form output                                                      *
 * ---------------------------------------------------------------------------*/

   fprintf(cgiOut, "<h3>%s</h3>\n", subtitle);
   fprintf(cgiOut, "<p></p>\n");
   fprintf(cgiOut, "<table>\n");
   fprintf(cgiOut, "<tr>\n");
   fprintf(cgiOut, "<th width=\"20\">");
   fprintf(cgiOut, "#");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "<th width=\"495\">");
   fprintf(cgiOut, "Certificate Subject Information");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "<th width=\"60\" colspan=\"2\">");
   fprintf(cgiOut, "Expires");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "<th width=\"65\">");
   fprintf(cgiOut, "Action");
   fprintf(cgiOut, "</th>\n");
   fprintf(cgiOut, "</tr>\n");

  /* if our search did not return any certs, we display a note instead */
  if(certcounter<=0) {
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<td class=\"even\" colspan=\"5\">");
    fprintf(cgiOut, "Could not find any certificates for the given search criteria.");
    fprintf(cgiOut, "</td>\n");
    fprintf(cgiOut, "</tr>\n");
  }

  for(dispcounter=0; dispcounter < dispmaxlines; dispcounter++) {

    /* zero certificate values and flags */
    certvalidity = 0;
    percent = 0;
    available_secs = 0;
    remaining_secs = 0;
    cert = X509_new();
    certsubject = X509_NAME_new();

    if(strcmp(sorting, "desc") == 0) tempcounter--;

    snprintf(certfilestr, sizeof(certfilestr), "%s/%s",
                           CACERTSTORE, certstore_files[tempcounter]->d_name);

    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th rowspan=\"2\">");
    fprintf(cgiOut, "%d", tempcounter+1);
    fprintf(cgiOut, "</th>\n");

    oddline_calc = div(tempcounter+1, 2);
    if(oddline_calc.rem)
      fprintf(cgiOut, "<td rowspan=\"2\" class=\"odd\">");
    else
      fprintf(cgiOut, "<td rowspan=\"2\" class=\"even\">");

    if ( (certfile = fopen(certfilestr, "r")) != NULL) {
      PEM_read_X509(certfile, &cert, NULL, NULL);

      /* ---------------------------------------------------------- *
       * Display the subject data. Use the UTF-8 flag to show       *
       * Japanese Kanji. This also needs the separator flag to work *
       * ---------------------------------------------------------- */
      certsubject = X509_get_subject_name(cert);
      X509_NAME_print_ex_fp(cgiOut, certsubject, 0,
         ASN1_STRFLGS_UTF8_CONVERT|XN_FLAG_SEP_CPLUS_SPC);

      /* store certificate start date for later eval */
      start_date = X509_get_notBefore(cert);

      /* store certificate expiration date for later eval */
      expiration_date = X509_get_notAfter(cert);

      /* check the start and end dates in the cert */
      if (X509_cmp_current_time (X509_get_notBefore (cert)) >= 0)
        /* flag the certificate as not valid yet */
        certvalidity = 0;
      else
      if (X509_cmp_current_time (X509_get_notAfter (cert)) <= 0)
        /* flag the certificate as expired */
        certvalidity = 0;
      else 
        /* flag the certificate is still valid */
        certvalidity = 1;

      fclose(certfile);
    }
    else 
       fprintf(cgiOut, "Error: Can't open certificate file %s for reading.",
                                                                 certfilestr);
    fprintf(cgiOut, "</td>\n");

    if(certvalidity == 0) {

      /* expiration bar display column */
      fprintf(cgiOut, "<th rowspan=\"2\">\n");
      fprintf(cgiOut, "<table class=\"led\">\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "</table>\n");
      fprintf(cgiOut, "</th>\n");

      /* remaining days before expiration column */
      fprintf(cgiOut, "<th class=\"exnok\" rowspan=\"2\">");
      fprintf(cgiOut, "Inval.<br />/Expd");
      fprintf(cgiOut, "</th>\n");
    }

    if(certvalidity == 1) {

      /* ------ START get the certificate lifetime in seconds ------ */
      /* copy the start date into a string */
      membio = BIO_new(BIO_s_mem());
      ASN1_TIME_print(membio, start_date);
      BIO_gets(membio, membio_buf, sizeof(membio_buf));
      BIO_free(membio);

      /* parse the start date string into a time struct */
      memset (&start_tm, '\0', sizeof(start_tm));
      strptime(membio_buf, "%h %d %T %Y %z", &start_tm);
      start = mktime(&start_tm);

      /* ------ START get the certificate remaining time in seconds ------ */
      /* copy the expiration date into a string */
      membio = BIO_new(BIO_s_mem());
      ASN1_TIME_print(membio, expiration_date);
      BIO_gets(membio, membio_buf, sizeof(membio_buf));
      BIO_free(membio);
  
      /* parse the expiration date string into a time struct */
      memset (&expiration_tm, '\0', sizeof(expiration_tm));
      strptime(membio_buf, "%h %d %T %Y %z", &expiration_tm);
  
      /* get the current time */
      expiration = mktime(&expiration_tm);
  
      /* get the time difference between expiration time and current time */
      remaining_secs = difftime(expiration, now);
      /* ------ END get the certificate remaining time in seconds ------ */

      /* get the time difference between start and expiration time */
      available_secs = difftime(expiration, start);
      /* ------ END get the certificate lifetime in seconds ------ */
  
      /* ------ START calculate percentage of lifetime left ------ */
      /* remaining_secs *100                                       */
      /* ------------------- = X, rounded down with floor()        */
      /* available_secs                                            */
      percent = floor((remaining_secs*100)/available_secs);
      /* ------ END calculate percentage of lifetime left   ------ */
  
      /* expiration bar display column */
      fprintf(cgiOut, "<th rowspan=\"2\">");
      fprintf(cgiOut, "<table class=\"led\">\n");
      if (percent >= 90) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#00FF00></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 80) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#00FF33></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 70) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#99FF33></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 60) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#FFFF00></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 50) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#FFCC00></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 40) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#FF9900></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 30) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#FF6600></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 20) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#FF3300></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      if (percent >= 10) fprintf(cgiOut, "  <tr><td class=\"led\" bgcolor=#FF0000></td></tr>\n");
      else fprintf(cgiOut, "  <tr><td class=\"led-off\"></td></tr>\n");
      fprintf(cgiOut, "</table>\n");
      fprintf(cgiOut, "</th>");
  
      /* remaining days before expiration column */
      //fprintf(cgiOut, membio_buf);
      if (percent < 10) fprintf(cgiOut, "<th class=\"exnok\" rowspan=\"2\">\n");
      else fprintf(cgiOut, "<th class=\"exok\" rowspan=\"2\">\n");
      if(floor(remaining_secs/63072000) > 0) fprintf(cgiOut, "%.f<br />years", remaining_secs/31536000);
      else if(floor(remaining_secs/86400) > 0 ) fprintf(cgiOut, "%.f<br />days", remaining_secs/86400);
      else if(floor(remaining_secs/3600) > 0 ) fprintf(cgiOut, "%.f<br />hours", remaining_secs/3600);
      else if(floor(remaining_secs/60) > 0 ) fprintf(cgiOut, "%.f<br />mins", remaining_secs/60);
      else fprintf(cgiOut, "%.f<br />secs", remaining_secs);
      fprintf(cgiOut, "</th>\n");
    }

    /* action column */
    fprintf(cgiOut, "<th>");
    fprintf(cgiOut, "<form action=\"getcert.cgi\" method=\"post\">\n");
    fprintf(cgiOut, "<input type=\"hidden\" name=\"cfilename\" ");
    fprintf(cgiOut, "value=\"%s\" />\n", certstore_files[tempcounter]->d_name);
    fprintf(cgiOut, "<input type=\"hidden\" name=\"format\" value=\"pem\" />\n");
    fprintf(cgiOut, "<input class=\"getcert\" type=\"submit\" value=\"Detail\" />\n");
    fprintf(cgiOut, "</form>\n");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "</tr>\n");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th>");
    fprintf(cgiOut, "<form action=\"getcert.cgi\" method=\"post\">\n");
    fprintf(cgiOut, "<input type=\"hidden\" name=\"cfilename\" ");
    fprintf(cgiOut, "value=\"%s\" />\n", certstore_files[tempcounter]->d_name);
    fprintf(cgiOut, "<input type=\"hidden\" name=\"format\" value=\"text\" />\n");
    fprintf(cgiOut, "<input class=\"getcert\" type=\"submit\" value=\"Renew\" />\n");
    fprintf(cgiOut, "</form>");
    fprintf(cgiOut, "</th>\n");
    fprintf(cgiOut, "</tr>\n");

    if(strcmp(sorting, "asc") == 0) tempcounter++;
  }


  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th colspan=\"5\">");
  fprintf(cgiOut, "Total # of certs: %d | ", certcounter);
  fprintf(cgiOut, "Page %d of %d", pagenumber, pagecounter);
  fprintf(cgiOut, "</th>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "</table>\n");

  fprintf(cgiOut, "<p></p>\n");

  fprintf(cgiOut, "<table>\n");

  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th>");
  fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"post\">");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"sort\" ");
  fprintf(cgiOut, "value=\"desc\" />\n");
  resubmit();
  fprintf(cgiOut, "<input type=\"submit\" name=\"sort\"");
  fprintf(cgiOut, " value=\"Latest Certs first\" />");
  fprintf(cgiOut, "</form>");
  fprintf(cgiOut, "</th>\n");

  fprintf(cgiOut, "<th>");
  fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"post\">");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"sort\" ");
  fprintf(cgiOut, "value=\"asc\">\n");
  resubmit();
  fprintf(cgiOut, "<input type=\"submit\" name=\"sort\"");
  fprintf(cgiOut, " value=\"Oldest Certs first\">");
  fprintf(cgiOut, "</form>");
  fprintf(cgiOut, "</th>\n");

  // filler 1
  fprintf(cgiOut, "<th width=\"15\">");
  fprintf(cgiOut, "&nbsp;");
  fprintf(cgiOut, "</th>\n");

  // goto page 1
  fprintf(cgiOut, "<th width=\"5\">");
  fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"post\">");
  resubmit();
  fprintf(cgiOut, "<input type=\"submit\" value=\"&lt;&lt;\" />");
  fprintf(cgiOut, "</form>");
  fprintf(cgiOut, "</th>\n");

  // goto page before
  fprintf(cgiOut, "<th width=\"5\">");
  fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"post\">");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", certcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", pagecounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"page\" ");
  fprintf(cgiOut, "value=\"");
  tempcounter = 0;
  if(pagenumber > 1) tempcounter = pagenumber - 1;
  else tempcounter = 1;
  fprintf(cgiOut, "%d", tempcounter);
  fprintf(cgiOut, "\" />\n");
  resubmit();
  fprintf(cgiOut, "<input type=\"submit\" value=\"&lt; 1\">");
  fprintf(cgiOut, "</form>");
  fprintf(cgiOut, "</th>\n");

  // goto page after
  fprintf(cgiOut, "<th width=\"5\">");
  fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"post\">");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", certcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", pagecounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"page\" ");
  fprintf(cgiOut, "value=\"");
  tempcounter = 0;
  if(pagecounter > pagenumber) tempcounter = pagenumber + 1;
  else tempcounter = pagecounter;
  fprintf(cgiOut, "%d", tempcounter);
  fprintf(cgiOut, "\" />\n");
  resubmit();
  fprintf(cgiOut, "<input type=\"submit\" value=\"1 &gt;\" />");
  fprintf(cgiOut, "</form>");
  fprintf(cgiOut, "</th>\n");

  // goto last page
  fprintf(cgiOut, "<th width=\"5\">");
  fprintf(cgiOut, "<form action=\"certsearch.cgi\" method=\"post\">");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", certcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", pagecounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"page\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", pagecounter);
  fprintf(cgiOut, "\" />\n");
  resubmit();
  fprintf(cgiOut, "<input type=\"submit\" value=\"&gt;&gt;\" />");
  fprintf(cgiOut, "</form>");
  fprintf(cgiOut, "</th>\n");

  // goto page number
  fprintf(cgiOut, "<th width=\"120\">\n");
  fprintf(cgiOut, "<form class=\"setpage\" action=\"certsearch.cgi\" method=\"post\">\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"certcounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", certcounter);
  fprintf(cgiOut, "\" />\n");
  fprintf(cgiOut, "<input type=\"hidden\" name=\"pagecounter\" ");
  fprintf(cgiOut, "value=\"");
  fprintf(cgiOut, "%d", pagecounter);
  fprintf(cgiOut, "\" />\n");
  resubmit();
  fprintf(cgiOut, "<input class=\"goto\" type=\"submit\" value=\"Goto\" />\n");
  fprintf(cgiOut, "&nbsp; &nbsp;");
  fprintf(cgiOut, "<input class=\"page\" type=\"text\" name=\"page\" ");
  fprintf(cgiOut, "value=\"%d\" />\n", pagecounter);
  fprintf(cgiOut, "</form>\n");
  fprintf(cgiOut, "</th>\n");

  fprintf(cgiOut, "</tr>\n");
  fprintf(cgiOut, "</table>\n");

/* ---------------------------------------------------------------------------*
 * end the html output                                                        *
 * ---------------------------------------------------------------------------*/

  pagefoot();
}
  return(0);
}
Exemplo n.º 22
0
int cgiMain() {
	int power_num,group_serial_num[5],group_num,group_idx;
	int i,j,k;
	char name[81];
	ctrl_group *p_group ;

	cgiFormInteger("power_num", &power_num, 0);
	cgiFormInteger("group_num", &group_num, 0);
	memset(name,0,sizeof(name));
	cgiFormStringNoNewlines("serial_num", name, 81);
	get_serial_num(name,&group_serial_num);
	
	if(power_num > 100) power_num = 100;
	if(group_num > 5) group_num = 5;
	if(group_serial_num[0] > 100) group_serial_num[0] = 100;
	if(group_serial_num[1] > 100) group_serial_num[1] = 100;
	if(group_serial_num[2] > 100) group_serial_num[2] = 100;
	if(group_serial_num[3] > 100) group_serial_num[3] = 100;
	if(group_serial_num[4] > 100) group_serial_num[4] = 100;


	cgiWriteEnvironment("/CHANGE/THIS/PATH/capcgi.dat");
	cgiHeaderContentType("text/html");

	OutHead();
	OutBodyStart();

	if(0 == check_password())
		return 0;

	fprintf(cgiOut, "<p>power_num=%d group_num=%d [%d-%d-%d-%d-%d]</p>\n",
		power_num,group_num,group_serial_num[0],group_serial_num[1],
		group_serial_num[2],group_serial_num[3],group_serial_num[4]);

	if(0 == power_num)
	{
		fprintf(cgiOut, "<p>数据出错,退出!!</p>\n");
		OutBodyEnd();	
		return 0;
	}
	else
	{
		pwsw_h.power_num = power_num;
		pwsw_h.group_num = group_num;
		pwsw_h.group[0].ctrl_serial_num = group_serial_num[0];
		pwsw_h.group[1].ctrl_serial_num = group_serial_num[1];
		pwsw_h.group[2].ctrl_serial_num = group_serial_num[2];
		pwsw_h.group[3].ctrl_serial_num = group_serial_num[3];
		pwsw_h.group[4].ctrl_serial_num = group_serial_num[4];

		for(i=0;i<group_num;i++)
		{
			p_group = &pwsw_h.group[i];
			p_group->projector_on = 0;
			p_group->ctrl_serial_num = group_serial_num[i];
			get_start_time(i,&p_group->start_time);
			get_end_time(i,&p_group->end_time);
			for(j=0;j<p_group->ctrl_serial_num;j++)
			{
				p_group->serial_info[j].duration_ms = get_duration_ms(i,j);
				for(k=0;k<power_num;k++)
				{
					p_group->serial_info[j].power_status[k]= get_power_status(i,j,k);
				}
				
			}
		}
	}

	//Out_pwsw_xml();
	save_to_xml_file();
	fprintf(cgiOut, "<p>保存成功,请返回!</p>\n");
	fprintf(cgiOut, "&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" name=\"rest\" onclick=\"javascript:history.go(-1)\" value=\"重新载入\" />\n");

	OutBodyEnd();

	return 0;
}
Exemplo n.º 23
0
int cgiMain() {

  static char 	title[] = "Create a new Scan Configuration";
  char		ip[16];
  int		port;
  char		user[81];
  char		pass[81];
  char		cert[81];
  char		encr[81];
  int		prefs_counter = 0;
  int		rules_counter = 0;
  int		pdeps_counter = 0;
  int		even_counter = 0;
  int		odd_counter = 0;
  SSL		*ssl;
  int 		i = 0;
  char          altcolor[16] = "class=\"odd\"";

#ifdef DEBUG
  char          error_string[255] = "";

  if(! (debugfile = fopen(DEBUGFILE, "w"))) {
    snprintf(error_string, sizeof(error_string),
           "Cannot open debug file %s for writing.", DEBUGFILE);
    int_error(error_string);
  }
#endif

/* -------------------------------------------------------------------------- *
 * check if we got called from scantemplates to create a  new template file   *
 * ---------------------------------------------------------------------------*/

 cgiFormString("template", templatefilestr, sizeof(templatefilestr));

/* -------------------------------------------------------------------------- *
 * check if we got all information to make a scan server connection           *
 * ---------------------------------------------------------------------------*/

  if ( cgiFormString("ip", ip, sizeof(ip)) != cgiFormSuccess )
    int_error("Error retrieving OpenVAS server IP address.");

  if ( cgiFormInteger("port", &port, SCANNER_PORT) != cgiFormSuccess )
    int_error("Error retrieving OpenVAS server port number.");

  if ( port <= 0 || port > 65535 )
    int_error("Error OpenVAS server port number not in a valid port range.");

  if ( cgiFormString("encr", encr, sizeof(encr)) != cgiFormSuccess )
    int_error("Error retrieving OpenVAS server protocol encryption type.");

  if ( cgiFormString("user", user, sizeof(user)) != cgiFormSuccess )
    int_error("Error retrieving OpenVAS remote user name.");

  if ( cgiFormString("pass", pass, sizeof(pass)) != cgiFormSuccess )
    int_error("Error retrieving OpenVAS remote user password.");

  if ( cgiFormString("cert", cert, sizeof(cert)) != cgiFormSuccess )
    int_error("Error retrieving OpenVAS client certificate name.");
  
/* -------------------------------------------------------------------------- *
 * make a SSL connection to the OpenVAS Server.                               *
 * ---------------------------------------------------------------------------*/

  ssl = scanner_connect(ip, port, encr, cert);

/* -------------------------------------------------------------------------- *
 * Handle the login to the OpenVAS Server.                                    *
 * ---------------------------------------------------------------------------*/

  scanner_login(ssl, user, pass);

/* -------------------------------------------------------------------------- *
 * Get the list of plugins from the OpenVAS Server and                        *
 * create the list of categories.                                             *
 * ---------------------------------------------------------------------------*/

  plugs_counter = 0;
  plugs_counter = scanner_getplugs(ssl);
  if (plugs_counter == 0)
     int_error("Error: Could not receive plugins from OpenVAS server.");

  scanner_getgroups();

/* -------------------------------------------------------------------------- *
 * Get the preferences list from the OpenVAS Server.                          *
 * ---------------------------------------------------------------------------*/

  prefs_counter = scanner_getprefs(ssl);
  if (prefs_counter == 0)
     int_error("Could not receive preferences from OpenVAS server.");

/* -------------------------------------------------------------------------- *
 * Get the rules list from the OpenVAS Server.                                *
 * ---------------------------------------------------------------------------*/

  rules_counter = scanner_getrules(ssl);
  /* it isn't unusual to have no rules so the count can be zero. */

/* -------------------------------------------------------------------------- *
 * Get the preferences dependency list from the OpenVAS Server.               *
 * ---------------------------------------------------------------------------*/

  pdeps_counter = scanner_getpdeps(ssl);

/* -------------------------------------------------------------------------- *
 * start the html output                                                      *
 * ---------------------------------------------------------------------------*/

  pagehead(title, NULL, cgiOut);

/* -------------------------------------------------------------------------- *
 * start the form output                                                      *
 * ---------------------------------------------------------------------------*/

  if(strcmp(templatefilestr, "create") == 0)
    fprintf(cgiOut, "<form action=\"scanverify.cgi\" method=\"post\">");
  else
    fprintf(cgiOut, "<form action=\"scanprocess.cgi\" method=\"post\">");

  fprintf(cgiOut, "<table width=\"100%%\">");
  fprintf(cgiOut, "<tr>\n");


  if(strcmp(templatefilestr, "create") == 0) {
    fprintf(cgiOut, "<th colspan=2>");
    fprintf(cgiOut, "New OpenVAS Scan Template");
    fprintf(cgiOut, "</th>");
    fprintf(cgiOut, "</tr>");
    fprintf(cgiOut, "<td bgcolor=CFCFCF align=\"center\" bordercolor=CFCFCF");
    fprintf(cgiOut, " width=270>");
    fprintf(cgiOut, "Configuration Name:");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td align=\"center\">");
    fprintf(cgiOut, "<input type=text name=s-name size=35 maxlength=35>");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>");
    fprintf(cgiOut, "<tr>\n");
    fprintf(cgiOut, "<th colspan=2>");
    fprintf(cgiOut, "<input type=\"submit\" value=\"Save Template\">");
    fprintf(cgiOut, "</th>");
  }
  else {
    fprintf(cgiOut, "<th colspan=4>");
    fprintf(cgiOut, "Scan Target IP Address and optional Login Credentials");
    fprintf(cgiOut, "</th>");
    fprintf(cgiOut, "</tr>\n");

    fprintf(cgiOut, "<tr>");
    fprintf(cgiOut, "<td bgcolor=CFCFCF align=\"center\">");
    fprintf(cgiOut, "IP Address:");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td align=\"center\">");
    fprintf(cgiOut, "<input type=text name=t-ip size=15 maxlength=15 value=%s>",
            DEFAULT_TARGET_IP);
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td bgcolor=CFCFCF align=\"center\">");
    fprintf(cgiOut, "Credentials:");
    fprintf(cgiOut, "</td>");

    fprintf(cgiOut, "<td align=\"center\">");
   fprintf(cgiOut, "<select name=\"c-type\">");
   fprintf(cgiOut, "<option value=\"none\" selected>No Credentials</option>");
   fprintf(cgiOut, "<option value=\"ssh-pass\">SSH Passphrase</option>");
   fprintf(cgiOut, "<option value=\"smb-pass\">SMB User Login</option></select>");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");

    fprintf(cgiOut, "<tr>");
    fprintf(cgiOut, "<td bgcolor=CFCFCF align=\"center\">");
    fprintf(cgiOut, "User Name:");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td align=\"center\">");
    fprintf(cgiOut, "<input type=text name=c-user size=15 maxlength=15>");
    fprintf(cgiOut, "</td>");

    fprintf(cgiOut, "<td bgcolor=CFCFCF align=\"center\">");
    fprintf(cgiOut, "Passphrase:");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "<td align=\"center\">");
    fprintf(cgiOut, "<input type=password name=c-pass size=18 maxlength=30>");
    fprintf(cgiOut, "</td>");
    fprintf(cgiOut, "</tr>\n");

    fprintf(cgiOut, "<tr>");
    fprintf(cgiOut, "<th colspan=4>");
    fprintf(cgiOut, "<input type=\"submit\" value=\"Start Scan\">");
    fprintf(cgiOut, "</th>");
  }
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "</table>");

  /* forward login information to either scanprocess.cgi or scanverify.cgi */
  fprintf(cgiOut,"<input type=hidden name=ip value=%s>",ip);
  fprintf(cgiOut,"<input type=hidden name=port value=%d>",port);
  fprintf(cgiOut,"<input type=hidden name=encr value=%s>",encr);
  fprintf(cgiOut,"<input type=hidden name=user value=%s>",user);

  /* Escape the password string, it can contain HTML reserved chars like '>' */
  fprintf(cgiOut,"<input type=hidden name=pass value=");
  cgiHtmlEscape(pass);
  fprintf(cgiOut,">");

  fprintf(cgiOut,"<input type=hidden name=cert value=%s>",cert);

  fprintf(cgiOut, "<p>");
  fprintf(cgiOut, "<table width=\"100%%\">");
  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th colspan=6>");
  fprintf(cgiOut, "OpenVAS Scanner Plugin Family List");
  fprintf(cgiOut, "</th>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "<tr>");

  for(i=0; i<famly_counter; i++) {

    fprintf(cgiOut, "<td bgcolor=CFCFCF align=\"center\" bordercolor=CFCFCF>");
    fprintf(cgiOut, "<input type=checkbox name=\"%s\">", famlylist[i].name);
    fprintf(cgiOut, "</td>");

    if( i == 1 || (i % 2) != 0 ) {
      if ( odd_counter == 1 || (odd_counter % 2) != 0 )
        strncpy(altcolor, "class=\"odd\"", sizeof(altcolor));
      else
        strncpy(altcolor, "class=\"even\"", sizeof(altcolor));
      odd_counter++;
    } else {
      if ( even_counter == 1 || (even_counter % 2) != 0 )
        strncpy(altcolor, "class=\"odd\"", sizeof(altcolor));
      else
        strncpy(altcolor, "class=\"even\"", sizeof(altcolor));
      even_counter++;
    }

    fprintf(cgiOut, "<td %s>", altcolor);
    fprintf(cgiOut, "%s", famlylist[i].name);
    fprintf(cgiOut, "</td>");

    fprintf(cgiOut, "<td %s style=\"text-align: right;\">", altcolor);
    fprintf(cgiOut, "%d", famlylist[i].plugscount);
    fprintf(cgiOut, "</td>");

   /* we want to display 2 columns of 3 cells (checkbox | Family Name | *
    * Plugin Count) to shorten the length of the overall Family list.   */
    if( i == 1 || (i % 2) != 0 ) fprintf(cgiOut, "</tr><tr>\n");

  }

  /* if famly_counter is a uneven number we miss a cell at the last row *
   * so we better insert a "dummy".                                     */
  if( (famly_counter % 2) != 0 ) {
     fprintf(cgiOut, "<td bgcolor=CFCFCF bordercolor=CFCFCF>&nbsp;</td>\n");
     fprintf(cgiOut, "<td bgcolor=FFFFFF colspan=2>&nbsp;</td>\n");
  }
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "<tr>");
  fprintf(cgiOut, "<th colspan=6>");
  fprintf(cgiOut, "Total: %d Plugins", plugs_counter );
  fprintf(cgiOut, " in %d Families.", famly_counter );
  fprintf(cgiOut, "</th>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "</table>\n");
  fprintf(cgiOut, "</form>");

  fprintf(cgiOut, "<p>");
  fprintf(cgiOut, "<table width=\"100%%\">");
  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th colspan=2>");
  fprintf(cgiOut, "INOVASC Client");
  fprintf(cgiOut, "</th>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "<tr>");
  fprintf(cgiOut, "<td align=\"center\" bgcolor=\"#CFCFCF\"");
  fprintf(cgiOut, "bordercolor=\"#CFCFCF\" width=180>");
  fprintf(cgiOut, "Version:");
  fprintf(cgiOut, "</td>");
  fprintf(cgiOut, "<td align=\"center\"bgcolor=\"#FFFFFF\">");
  fprintf(cgiOut, "%s\n", CLIENT_SW_VERSION);
  fprintf(cgiOut, "</td>");
  fprintf(cgiOut, "</tr>\n");
  fprintf(cgiOut, "<tr>");
  fprintf(cgiOut, "<td align=\"center\" bgcolor=CFCFCF bordercolor=CFCFCF>");
  fprintf(cgiOut, "Copyright:");
  fprintf(cgiOut, "</td>");
  fprintf(cgiOut, "<td align=\"center\"bgcolor=\"#FFFFFF\">");
  fprintf(cgiOut, "%s\n", CLIENT_COPYRIGHTS);
  fprintf(cgiOut, "</td>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "<tr>");
  fprintf(cgiOut, "<td align=\"center\" bgcolor=CFCFCF bordercolor=CFCFCF>");
  fprintf(cgiOut, "Status:");
  fprintf(cgiOut, "</td>");
  fprintf(cgiOut, "<td align=\"center\"bgcolor=\"#FFFFFF\">");
  fprintf(cgiOut, "<b>%s</b> login to server <b>%s</b> successful.",
                   user, ip);
  fprintf(cgiOut, "</td>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "<tr>\n");
  fprintf(cgiOut, "<th colspan=2>");
  fprintf(cgiOut, "&nbsp;");
  fprintf(cgiOut, "</th>");
  fprintf(cgiOut, "</tr>");
  fprintf(cgiOut, "</table>\n");

/* -------------------------------------------------------------------------- *
 * end the html output                                                        *
 * ---------------------------------------------------------------------------*/

  pagefoot(NULL);
#ifdef DEBUG
  if(debugfile != NULL) fclose(debugfile);
#endif
  fclose(cgiOut);
  return(0);
}