Esempio n. 1
0
static tree_cell * security_something(lex_ctxt * lexic, proto_post_something_t proto_post_func, post_something_t post_func)
{
 struct arglist * script_infos = lexic->script_infos;

 char* proto = get_str_local_var_by_name(lexic, "protocol");
 char* data = get_str_local_var_by_name(lexic, "data");
 int port = get_int_local_var_by_name(lexic, "port", -1); 
 char * dup = NULL;
 
 if(data != NULL)
 {
  int len = get_local_var_size_by_name(lexic, "data");
  int i;
  
  dup = nasl_strndup(data, len);
  for(i=0;i<len;i++)
   if(dup[i] == 0)dup[i]=' ';
 }

 /*if((arg_get_value(script_infos, "standalone")) != NULL)
 {
  if( data != NULL )
   fprintf(stdout, "%s\n", dup);
  else
   fprintf(stdout, "Success\n");
 }*/
   
 if(proto == NULL)
	 proto = get_str_local_var_by_name(lexic, "proto");

 if(port < 0)
	 port = get_int_var_by_num(lexic, 0, -1);

 
 if(dup != NULL)
 {
  if(proto == NULL)
   post_func(script_infos, port, dup);
  else
   proto_post_func(script_infos, port, proto, dup);

  efree(&dup);
  return FAKE_CELL;
 }
 
 if(proto == NULL)
  post_func(script_infos, port, NULL);
 else
  proto_post_func(script_infos, port, proto, NULL);
  

 return FAKE_CELL;
}
Esempio n. 2
0
File: exec.c Progetto: OPSF/uClinux
static char*
cell2str(lex_ctxt* lexic, tree_cell* c)
{
  char	* p;
  tree_cell	*c2;
  nasl_array	*a;

  if (c == NULL || c == FAKE_CELL)
    {
#if NASL_DEBUG > 0
      nasl_perror(lexic, "Cannot convert NULL or FAKE cell to string\n");
#endif
      return NULL;
    }
      
  switch(c->type)
    {
    case CONST_INT:
      p = malloc(16);
      if (p != NULL)
	snprintf(p, 16, "%d", c->x.i_val);
      return p;
      
    case CONST_STR:
    case CONST_DATA:
      if ( c->x.str_val == NULL)
	p = estrdup("");
      else
	p = nasl_strndup(c->x.str_val, c->size);
      return p;

    case REF_ARRAY:
    case DYN_ARRAY:
      a = c->x.ref_val;
      p = (char*)array2str(a);
      return estrdup(p);

    default:
      c2 = nasl_exec(lexic, c);
      p = cell2str(lexic, c2);
      deref_cell(c2);
      if (p == NULL)
	p = estrdup("");
      return p;
    }
}
Esempio n. 3
0
File: exec.c Progetto: OPSF/uClinux
char*
cell2str_and_size(lex_ctxt* lexic, tree_cell* c, int * sz)
{
  char	* p;
  tree_cell	*c2;

  if (c == NULL || c == FAKE_CELL)
    {
#if NASL_DEBUG > 0
      nasl_perror(lexic, "Cannot convert NULL or FAKE cell to string\n");
#endif
      return NULL;
    }
      
  switch(c->type)
    {
    case CONST_INT:
      p = malloc(16);
      if ( p == NULL ) 
        return NULL;
      snprintf(p, 16, "%d", c->x.i_val);
      if(sz != NULL)*sz = strlen(p);
      return p;
      
    case CONST_STR:
    case CONST_DATA:
      if ( c->x.str_val == NULL)
	p = estrdup("");
      else
	p = nasl_strndup(c->x.str_val, c->size);
      if(sz != NULL)*sz = c->size;
      return p;
      
    default:
      c2 = nasl_exec(lexic, c);
      p = cell2str_and_size(lexic, c2, sz);
      deref_cell(c2);
      if (p == NULL)
      	{
	p = estrdup("");
      	if(sz != NULL)*sz = 0;
	}
      return p;
    }
}
Esempio n. 4
0
tree_cell * nasl_telnet_init(lex_ctxt * lexic)
{
 int soc = get_int_var_by_num(lexic, 0, -1);
 int opts;				/* number of options recorded */
 unsigned char buffer[1024];
#define iac buffer[0]
#define code buffer[1]
#define option buffer[2]
 tree_cell * retc;
 int n = 0, n2;
 int lm = 0;

 if(soc <= 0 )
 {
 	nasl_perror(lexic, "Syntax error in the telnet_init() function\n");
	nasl_perror(lexic, "Correct syntax is : output = telnet_init(<socket>)\n");
 	return NULL;
}

 iac = 255;
 opts = 0;
 while(iac == 255)
 {
  n = read_stream_connection_min(soc, buffer, 3, 3);
  if((iac!=255)||(n<=0)||(n!=3))break;
  if((code == 251)||(code == 252))code = 254; /* WILL , WONT -> DON'T */
  else if((code == 253)||(code == 254))code = 252; /* DO,DONT -> WONT */
  write_stream_connection(soc, buffer,3);
  if ( lm == 0 )
  {
   code = 253;
   option = 0x22;
   write_stream_connection(soc, buffer,3);
   lm ++;
  }
  opts++;
  if (opts>100) break;
 }
 if (n <= 0)
  {
   if (opts == 0)
     return NULL;
   else
     n = 0;
  }

 if (opts>100)				/* remote telnet server is crazy */
  {
	nasl_perror(lexic, "More than 100 options received by telnet_init() function! exiting telnet_init.\n");
	return NULL;
  }

 n2 = read_stream_connection(soc, buffer + n,  sizeof(buffer) - n);
 if (n2 > 0)
   n += n2;
 retc = alloc_typed_cell(CONST_DATA);
 retc->size = n;
 retc->x.str_val = nasl_strndup(buffer, n);
#undef iac
#undef data
#undef option

  return retc;
}
Esempio n. 5
0
tree_cell * nasl_recv_line(lex_ctxt * lexic)
{
 int len = get_int_local_var_by_name(lexic, "length", -1);
 int soc = get_int_local_var_by_name(lexic, "socket", 0);
 int timeout = get_int_local_var_by_name(lexic, "timeout", -1);
 char * data;
 int new_len = 0;
 int n = 0;
 tree_cell * retc;
 time_t		t1 = 0;

 if(timeout < 0) timeout = lexic->recv_timeout;
 
 if(len == -1 || soc <= 0)
   {
     nasl_perror(lexic, "recv_line: missing or undefined parameter length or soc\n");
     return NULL;
   }

 if (timeout >= 0)	/* sycalls are much more expensive than simple tests */
   t1 = time(NULL);

 if ( fd_is_stream(soc) != 0 )
 {
  int bufsz = stream_get_buffer_sz ( soc );
  if ( bufsz <= 0 )
	stream_set_buffer(soc, len + 1 );
 }

 data = emalloc(len+1);
 for(;;)
 {
  int e = read_stream_connection_min(soc, data+n, 1, 1);
  if(e < 0)
    break;
  if(e == 0)
  {
   if( timeout >= 0 && time(NULL) - t1 < timeout)
  	continue;
    else 
  	break;
  }	
  n++;  
  if((data[n-1] == '\n') ||
     (n >= len))break;
 }
 
 
 
 if(n <= 0)
   {
     efree(&data);
     return NULL;
   }
 
 new_len = n;
 
 
  

 retc = alloc_tree_cell(0, NULL);
 retc->type = CONST_DATA;
 retc->size = new_len;
 retc->x.str_val = nasl_strndup(data, new_len);

 efree(&data);

 return retc;
}
Esempio n. 6
0
tree_cell * nasl_recv(lex_ctxt * lexic)
{
 char * data;
 int len = get_int_local_var_by_name(lexic, "length", -1);
 int min_len = get_int_local_var_by_name(lexic, "min", -1);
 int soc = get_int_local_var_by_name(lexic, "socket", 0);
 int to  = get_int_local_var_by_name(lexic, "timeout", lexic->recv_timeout);
 fd_set rd;
 struct timeval tv;
 int new_len = 0;
 tree_cell * retc;
 int type = -1, opt_len = sizeof(type);
 int e;
 

 if(len <= 0 || soc <= 0)
	 return NULL;

 if (to <= 0) to = 5;

 tv.tv_sec = to;
 tv.tv_usec = 0; 


 data = emalloc(len);
 if ( !fd_is_stream(soc) )
 	e = getsockopt(soc, SOL_SOCKET, SO_TYPE, &type, &opt_len);
  else
	e = -1;
 
 if(e == 0 && type == SOCK_DGRAM)
 {
 /*
  * As UDP packets may be lost, we retry up to 5 times
  */
 int retries = 5;
 int i;
 
 tv.tv_sec = to / retries;
 tv.tv_usec = (to % retries) *  100000;
 
 for(i=0;i<retries;i++)
 {
  FD_ZERO(&rd);
  FD_SET(soc, &rd);

  
  if(!to || select(soc+1, &rd, NULL, NULL, &tv)>0)
  {
   int e;
   e = recv(soc, data+new_len, len-new_len, 0);
  
   if(e <= 0)
   {
    if(!new_len)
    {
     efree(&data); 
     return NULL;
    }
    else break;
   }
   else new_len+=e;
   if(new_len >= len)break;
   break; /* UDP data is never fragmented */
  }
  else 
  {
   /* 
    * The packet may have been lost en route - we resend it
    */
   char * data;
   int len;
   
   data = get_udp_data(lexic->script_infos, soc, &len);
   if(data != NULL)send(soc, data, len, 0);
   tv.tv_sec = to / retries;
   tv.tv_usec = ( to % retries) * 100000;
   }
  }
 }
 else {	
 	int old = stream_set_timeout(soc, tv.tv_sec);
 	new_len = read_stream_connection_min(soc, data, min_len, len);
	stream_set_timeout(soc, old);
      }
 if(new_len > 0)
 {
  retc = alloc_tree_cell(0, NULL);
  retc->type = CONST_DATA;
  retc->x.str_val = nasl_strndup(data, new_len);
  retc->size = new_len;
  efree(&data);
  return retc;
 }
 else {
	 efree(&data);
	 return NULL;
  }
}