/**
 * @brief Add days or seconds to an ISO time string.
 * @naslfn{isotime_add}
 *
 * This function adds days or seconds to an ISO time string and
 * returns the resulting time string.  The number of days or seconds
 * are given using the named parameters; if none are given nothing is
 * added; if both are given both additions are performed.  This
 * function won't work for dates before the Gregorian calendar switch.
 *
 * @nasluparam
 *
 * - An ISO time string
 *
 * @naslnparam
 *
 * - @a years An integer with the number of years to add to the timestamp.
 *
 * - @a days An integer with the number of days to add to the timestamp.
 *
 * - @a seconds An integer with the number of seconds to add to the
 *              timestamp.
 *
 * @naslret The resulting ISO time string or NULL if the provided ISO
 *          time string is not valid or the result would overflow
 *          (i.e. year > 9999).
 *
 * @param[in] lexic  Lexical context of the NASL interpreter.
 *
 * @return A tree cell.
 */
tree_cell *
nasl_isotime_add (lex_ctxt *lexic)
{
  tree_cell *retc;
  my_isotime_t timebuf;
  const char *string;
  int nyears, ndays, nseconds;

  string = get_str_var_by_num (lexic, 0);
  if (!string
      || get_var_size_by_num (lexic, 0) < ISOTIME_SIZE -1
      || check_isotime (string))
    return NULL;
  memcpy (timebuf, string, ISOTIME_SIZE -1);
  timebuf[ISOTIME_SIZE - 1] = 0;

  nyears = get_int_local_var_by_name (lexic, "years", 0);
  ndays = get_int_local_var_by_name (lexic, "days", 0);
  nseconds = get_int_local_var_by_name (lexic, "seconds", 0);

  if (nyears && add_years_to_isotime (timebuf, nyears))
    return NULL;
  if (ndays && add_days_to_isotime (timebuf, ndays))
    return NULL;
  if (nseconds && add_seconds_to_isotime (timebuf, nseconds))
    return NULL;
  /* If nothing was added, explicitly add 0 years.  */
  if (!nyears && !ndays && !nseconds && add_years_to_isotime (timebuf, 0))
    return NULL;

  retc = alloc_typed_cell (CONST_STR);
  retc->x.str_val = estrdup (timebuf);
  retc->size = strlen (timebuf);
  return retc;
}
tree_cell *
nasl_socket_negotiate_ssl (lex_ctxt * lexic)
{
  int soc, transport, ret;
  tree_cell *retc;


  soc = get_int_local_var_by_name (lexic, "socket", -1);
  transport = get_int_local_var_by_name (lexic, "transport",
                                         OPENVAS_ENCAPS_TLScustom);
  if (soc < 0)
    {
      nasl_perror (lexic, "socket_ssl_negotiate: Erroneous socket value %d\n",
                   soc);
      return NULL;
    }
  if (transport == -1)
    transport = OPENVAS_ENCAPS_TLScustom;
  else if (!IS_ENCAPS_SSL (transport))
    {
      nasl_perror (lexic, "socket_ssl_negotiate: Erroneous transport value %d\n",
                   transport);
      return NULL;
    }
  ret = socket_negotiate_ssl (soc, transport, lexic->script_infos);
  if (ret < 0)
    return NULL;

  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = ret;
  return retc;
}
/**
 * @brief Seek in file.
 */
tree_cell *
nasl_file_seek (lex_ctxt * lexic)
{
  tree_cell *retc;
  int fd;
  int foffset;

  foffset = get_int_local_var_by_name (lexic, "offset", 0);
  fd = get_int_local_var_by_name (lexic, "fp", -1);
  if (fd < 0)
    {
      nasl_perror (lexic, "file_seek: need one arguments 'fp'\n");
      return NULL;
    }

  if (lseek (fd, foffset, SEEK_SET) < 0)
    {
      nasl_perror (lexic, "fseek: %s\n", strerror (errno));
      return NULL;
    }

  retc = alloc_typed_cell (CONST_INT);
  retc->x.i_val = 0;
  return retc;
}
tree_cell *
nasl_open_sock_tcp_bufsz (lex_ctxt * lexic, int bufsz)
{
  int soc = -1;
  struct arglist *script_infos = lexic->script_infos;
  int to, port;
  int transport = -1;
  const char *priority;
  tree_cell *retc;
  int type;

  to = get_int_local_var_by_name (lexic, "timeout", lexic->recv_timeout * 2);
  if (to < 0)
    to = 10;

  transport = get_int_local_var_by_name (lexic, "transport", -1);

  if (transport == OPENVAS_ENCAPS_TLScustom)
    {
      priority = get_str_local_var_by_name (lexic, "priority");
      if (!priority)
        priority = NULL;
      type = get_local_var_type_by_name (lexic, "priority");
      if (type != VAR2_STRING && type != VAR2_DATA)
        priority = NULL;
    }
  else
    priority = NULL;

  if (bufsz < 0)
    bufsz = get_int_local_var_by_name (lexic, "bufsz", 0);

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

  /* If "transport" has not been given, use auto detection if enabled
     in the KB. if "transport" has been given with a value of 0 force
     autodetection reagardless of what the KB tells.  */
  if (transport < 0)
    soc = open_stream_auto_encaps_ext (script_infos, port, to, 0);
  else if (transport == 0)
    soc = open_stream_auto_encaps_ext (script_infos, port, to, 1);
  else
    soc = open_stream_connection_ext (script_infos, port, transport, to,
                                      priority);
  if (bufsz > 0 && soc >= 0)
    {
      if (stream_set_buffer (soc, bufsz) < 0)
        nasl_perror (lexic, "stream_set_buffer: soc=%d,bufsz=%d\n", soc, bufsz);
    }

  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = soc < 0 ? 0 : soc;

  return retc;
}
Example #5
0
tree_cell * nasl_scanner_status(lex_ctxt * lexic)
{
 int current = get_int_local_var_by_name(lexic, "current", -1);
 int total   = get_int_local_var_by_name(lexic, "total", -1); 
 struct arglist * script_infos = lexic->script_infos;
 struct arglist * hostdata = arg_get_value(script_infos, "HOSTNAME");

 if(current != -1 && total != -1)
 {
  struct arglist * globs = arg_get_value(script_infos, "globals");			
      if (globs == NULL) return NULL;
      comm_send_status(globs, arg_get_value(hostdata, "NAME"), "portscan", current, total);
  }
 return FAKE_CELL;
} 
Example #6
0
tree_cell * replace_kb_item(lex_ctxt * lexic)
{
 struct arglist * script_infos = lexic->script_infos;
 char * name  = get_str_local_var_by_name(lexic, "name");
 int type = get_local_var_type_by_name(lexic, "value");
 
 if( name == NULL )
 {
  nasl_perror(lexic, "Syntax error with replace_kb_item() [null name]\n", name);
  return FAKE_CELL;
 }
 
 if(type == VAR2_INT)
 {
  int value = get_int_local_var_by_name(lexic, "value", -1);
  if ( value != -1 )plug_replace_key(script_infos, name, ARG_INT,(void*)value);
  else nasl_perror(lexic, "Syntax error with replace_kb_item(%s) [value=-1]\n", name); }
 else
 {
  char * value = get_str_local_var_by_name(lexic, "value");
  if( value == NULL )
  {
    nasl_perror(lexic, "Syntax error with replace_kb_item(%s) [null value]\n", name);
    return FAKE_CELL;
  }
  plug_replace_key(script_infos, name, ARG_STRING, value);
 }

 return FAKE_CELL;
}
Example #7
0
tree_cell * nasl_ftp_log_in(lex_ctxt * lexic)
{
 char * u, *p;
 int soc;
 tree_cell *retc;
 int res;

 soc = get_int_local_var_by_name(lexic, "socket", 0);
 if(soc <= 0)
	 return NULL;

 u = get_str_local_var_by_name(lexic, "user");
 if( u == NULL )
	 u = "";
 
 p = get_str_local_var_by_name(lexic, "pass");
 if( p == NULL )
	 p = "";

 res = ftp_log_in(soc, u, p) == 0;
 
 retc = alloc_tree_cell(0, NULL);
 retc->type = CONST_INT;
 retc->x.i_val = res;

 return retc;
}
tree_cell* nasl_ereg_replace(lex_ctxt* lexic)
{
 char * pattern = get_str_local_var_by_name(lexic, "pattern");
 char * replace = get_str_local_var_by_name(lexic, "replace");
 char * string  = get_str_local_var_by_name(lexic, "string");
 int    icase   = get_int_local_var_by_name(lexic, "icase", 0);
 char * r; 
 tree_cell * retc;
 
 if(pattern == NULL || replace == NULL)
 {
  nasl_perror(lexic, "Usage : ereg_replace(string:<string>, pattern:<pat>, replace:<replace>, icase:<TRUE|FALSE>\n");
  return NULL;
 }
 if (string == NULL)
  {
#if NASL_DEBUG > 1
   nasl_perror(lexic, "ereg_replace: string == NULL\n");
#endif
    return NULL;
  }

 r = _regreplace(pattern, replace, string, icase, 1);
 
 retc = alloc_tree_cell(0, NULL);
 retc->type = CONST_DATA;
 retc->size = strlen(r);
 retc->x.str_val =  r;

 return retc;
}
/*---------------------------------------------------------------------*/
tree_cell* nasl_crap(lex_ctxt* lexic)
{
  tree_cell	*retc;
  char		*data = get_str_local_var_by_name(lexic, "data");
  int		data_len = -1;
  int		len = get_int_local_var_by_name(lexic, "length", -1);
  int		len2 = get_int_var_by_num(lexic, 0, -1);


  if(len < 0 && len2 < 0)
    {
      nasl_perror(lexic, "crap: invalid or missing 'length' argument\n");
      return NULL;
    }
  if (len >= 0 && len2 >= 0)
    {
      nasl_perror(lexic, "crap: cannot set both unnamed and named 'length'\n");
      return NULL;
    }
  if (len < 0)
    len = len2;

  if( len == 0 )
  	return FAKE_CELL;
	
  if (data != NULL)
    {
      data_len = get_var_size_by_name(lexic, "data");
      if (data_len == 0)
	{
	  nasl_perror(lexic, "crap: invalid null 'data' parameter\n");
	  return NULL;
	}
    }

  retc = alloc_tree_cell(0, NULL);
  retc->type = CONST_DATA /*CONST_STR*/;
  retc->x.str_val = emalloc(len+1);
  retc->size = len;
  if (data == NULL)
    memset(retc->x.str_val, 'X', len);
  else
    {
      int	i,r;
      for(i = 0; i < len - data_len; i += data_len)
	memcpy(retc->x.str_val + i, data, data_len);
	
      if(data_len != 1)
       {
        if((r = (len % data_len)) > 0)
 	 memcpy(retc->x.str_val + (len - r), data, r);
	else
	 memcpy(retc->x.str_val + (len - data_len), data, data_len);
       }
      else 
         retc->x.str_val[ len - 1 ] = data[0];
    }
  retc->x.str_val[len] = '\0';
  return retc;
}
/**
 * @brief Read file
 */
tree_cell *
nasl_file_read (lex_ctxt * lexic)
{
  tree_cell *retc;
  char *buf;
  int fd;
  int flength;
  int n;

  fd = get_int_local_var_by_name (lexic, "fp", -1);
  if (fd < 0)
    {
      nasl_perror (lexic, "file_read: need file pointer argument\n");
      return NULL;
    }

  flength = get_int_local_var_by_name (lexic, "length", 0);

  buf = emalloc (flength + 1);
  if (buf == NULL)
    {
      nasl_perror (lexic, "file_read: cannot malloc %d bytes\n", flength);
      goto error;
    }

  for (n = 0; n < flength;)
    {
      int e;
      errno = 0;
      e = read (fd, buf + n, flength - n);
      if (e < 0 && errno == EINTR)
        continue;
      else if (e <= 0)
        break;
      else
        n += e;
    }

  retc = alloc_typed_cell (CONST_DATA);
  retc->size = n;
  retc->x.str_val = buf;
  return retc;
error:
  efree (&buf);
  return NULL;
}
Example #11
0
tree_cell*
nasl_mktime(lex_ctxt* lexic)
{
  struct tm	tm;
  tree_cell	*retc;
  time_t	tictac;

  tm.tm_sec = get_int_local_var_by_name(lexic, "sec", 0); /* seconds */
  tm.tm_min = get_int_local_var_by_name(lexic, "min", 0); /* minutes */
  tm.tm_hour = get_int_local_var_by_name(lexic, "hour", 0); /* hours */
  tm.tm_mday = get_int_local_var_by_name(lexic, "mday", 0); /* day of the month */
  tm.tm_mon = get_int_local_var_by_name(lexic, "mon", 1); /* month */
  tm.tm_mon -= 1;
  tm.tm_year = get_int_local_var_by_name(lexic, "year", 0); /* year */
  if (tm.tm_year >= 1900) tm.tm_year -= 1900;    
  tm.tm_isdst = get_int_local_var_by_name(lexic, "isdst", -1); /* daylight saving time */
  errno = 0;
  tictac = mktime(&tm);
  if (tictac == (time_t)(-1))
    {
      nasl_perror(lexic, "mktime(sec=%02d min=%02d hour=%02d mday=%02d mon=%02d year=%04d isdst=%d): %s\n", 
		  tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday, 
		  tm.tm_mon+1, tm.tm_year+1900, tm.tm_isdst,
		  errno ? strerror(errno): "invalid value?");
      return NULL;
    }
  retc = alloc_typed_cell(CONST_INT);
  retc->x.i_val = tictac;
  return retc;
}
tree_cell *
nasl_send (lex_ctxt * lexic)
{
  int soc = get_int_local_var_by_name (lexic, "socket", 0);
  char *data = get_str_local_var_by_name (lexic, "data");
  int option = get_int_local_var_by_name (lexic, "option", 0);
  int length = get_int_local_var_by_name (lexic, "length", 0);
  int data_length = get_var_size_by_name (lexic, "data");
  int n;
  tree_cell *retc;
  int type;
  unsigned int type_len = sizeof (type);


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

  if (length <= 0 || length > data_length)
    length = data_length;


  if (!fd_is_stream (soc)
      && getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0
      && type == SOCK_DGRAM)
    {
      n = send (soc, data, length, option);
      add_udp_data (lexic->script_infos, soc, data, length);
    }
  else
    n = nsend (soc, data, length, option);

  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = n;

  return retc;
}
Example #13
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;
}
Example #14
0
tree_cell * nasl_scanner_add_port(lex_ctxt * lexic)
{ 
 struct arglist * script_infos = lexic->script_infos;

 int port = get_int_local_var_by_name(lexic, "port", -1);
 char* proto = get_str_local_var_by_name(lexic, "proto");

 if(port >= 0)
 {
  scanner_add_port(script_infos, port, proto?proto:"tcp");
 }

 return FAKE_CELL;
}
Example #15
0
tree_cell * nasl_open_sock_tcp_bufsz(lex_ctxt * lexic, int bufsz)
{
 int soc = -1;
 struct arglist *  script_infos = lexic->script_infos;
 int	to, port, transport = -1;
 tree_cell * retc;
 char *host;

 to = get_int_local_var_by_name(lexic, "timeout", lexic->recv_timeout*2);
 if(to <= 0)
 	to = 10;
	
 transport = get_int_local_var_by_name(lexic, "transport", -1);
 if (bufsz < 0)
   bufsz = get_int_local_var_by_name(lexic, "bufsz", 0);
  
 port = get_int_var_by_num(lexic, 0, -1);
 if(port < 0)
	 return NULL;
 
 if(transport < 0)
   soc =  open_stream_auto_encaps(script_infos, port, to);
 else
   soc  = open_stream_connection(script_infos, port, transport, to);

 if (bufsz > 0 && soc >= 0 )
 {
   if (stream_set_buffer(soc, bufsz) < 0)
     nasl_perror(lexic, "stream_set_buffer: soc=%d,bufsz=%d\n", soc, bufsz);
 }

  retc = alloc_tree_cell(0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = soc < 0 ? 0 : soc;

  return retc;
}
tree_cell *
nasl_socket_get_ssl_ciphersuite (lex_ctxt * lexic)
{
  int soc, result;
  tree_cell *retc;

  soc = get_int_local_var_by_name (lexic, "socket", -1);
  result = socket_get_ssl_ciphersuite (soc);
  if (result < 0)
    return NULL;
  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = result;
  return retc;
}
tree_cell *
nasl_socket_get_ssl_version (lex_ctxt * lexic)
{
  int soc;
  openvas_encaps_t version;
  tree_cell *retc;

  soc = get_int_local_var_by_name (lexic, "socket", -1);
  version = socket_get_ssl_version (soc);
  if (version < 0)
    return NULL;
  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = version;
  return retc;
}
/*
 * This function returns an array
 */
tree_cell*
nasl_eregmatch(lex_ctxt* lexic)
{
  char		*pattern = get_str_local_var_by_name(lexic, "pattern");
  char		*string  = get_str_local_var_by_name(lexic, "string");
  int		icase    = get_int_local_var_by_name(lexic, "icase", 0);
  int		copt = 0, i;
  tree_cell	*retc;
  regex_t	re;
  regmatch_t	subs[NS];
  anon_nasl_var	v;
  nasl_array	*a;


  if(icase != 0)
    copt = REG_ICASE;
 
  if(pattern == NULL || string == NULL)
    return NULL;

  nasl_re_set_syntax(RE_SYNTAX_POSIX_EGREP);
  if(nasl_regcomp(&re, pattern, REG_EXTENDED|copt))
    {
      nasl_perror(lexic, "regmatch() : regcomp() failed\n");
      return NULL;
    }
  
  if(nasl_regexec(&re, string, (size_t)NS, subs, 0) != 0)
    return NULL;
      
  retc = alloc_tree_cell(0, NULL);
  retc->type = DYN_ARRAY;
  retc->x.ref_val = a = emalloc(sizeof(nasl_array));

  for (i = 0; i < NS; i ++)
    if (subs[i].rm_so != -1)
      {
	v.var_type = VAR2_DATA;
	v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
	v.v.v_str.s_val = string + subs[i].rm_so;
	(void) add_var_to_list(a, i, &v);
      }

  nasl_regfree(&re);
  return retc;
}
Example #19
0
tree_cell * nasl_ftp_get_pasv_address(lex_ctxt * lexic)
{
 int soc; 
 struct sockaddr_in addr;
 tree_cell * retc; 

 soc = get_int_local_var_by_name(lexic, "socket", 0);
 if(soc <= 0)
	 return NULL;

 bzero(&addr, sizeof(addr));
 ftp_get_pasv_address(soc, &addr);

 retc = alloc_tree_cell(0, NULL);
 retc->type = CONST_INT;
 retc->x.i_val = ntohs(addr.sin_port);
 return retc;
}
tree_cell *
nasl_socket_get_ssl_compression (lex_ctxt * lexic)
{
  int soc;
  tree_cell *retc;

  soc = get_int_local_var_by_name (lexic, "socket", -1);
  if (soc < 0)
    {
      nasl_perror (lexic, "socket_get_cert: Erroneous socket value %d\n",
                   soc);
      return NULL;
    }
  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = socket_get_ssl_compression (soc);
  return retc;
}
Example #21
0
tree_cell*
nasl_localtime(lex_ctxt* lexic)
{
  tree_cell		*retc;
  struct tm		*ptm;
  time_t		tictac;
  int			utc;
  nasl_array		*a;
  anon_nasl_var		v;


  tictac = get_int_var_by_num(lexic, 0, 0);
  if (tictac == 0)
    tictac = time(NULL);
  utc = get_int_local_var_by_name(lexic, "utc", 0);
  
  if (utc)
    ptm = gmtime(&tictac);
  else
    ptm = localtime(&tictac);

  if (ptm == NULL)
    {
      nasl_perror(lexic, "localtime(%d,utc=%d): %s\n", tictac, utc, strerror(errno));
      return NULL;
    }

  retc = alloc_typed_cell(DYN_ARRAY);
  retc->x.ref_val = a = emalloc(sizeof(nasl_array));
  memset(&v, 0, sizeof(v));
  v.var_type = VAR2_INT;

  v.v.v_int = ptm->tm_sec; add_var_to_array(a, "sec", &v); /* seconds */
  v.v.v_int = ptm->tm_min; add_var_to_array(a, "min", &v); /* minutes */
  v.v.v_int = ptm->tm_hour; add_var_to_array(a, "hour", &v); /* hours */
  v.v.v_int = ptm->tm_mday; add_var_to_array(a, "mday", &v); /* day of the month */
  v.v.v_int = ptm->tm_mon+1; add_var_to_array(a, "mon", &v); /* month */
  v.v.v_int = ptm->tm_year+1900; add_var_to_array(a, "year", &v); /* year */
  v.v.v_int = ptm->tm_wday; add_var_to_array(a, "wday", &v); /* day of the week */
  v.v.v_int = ptm->tm_yday+1; add_var_to_array(a, "yday", &v); /* day in the year */
  v.v.v_int = ptm->tm_isdst; add_var_to_array(a, "isdst", &v); /* daylight saving time */

  return retc;
}
/**
 * @brief Write file
 */
tree_cell *
nasl_file_write (lex_ctxt * lexic)
{
  tree_cell *retc;
  char *content;
  int len;
  int fd;
  int n;

  content = get_str_local_var_by_name (lexic, "data");
  fd = get_int_local_var_by_name (lexic, "fp", -1);
  if (content == NULL || fd < 0)
    {
      nasl_perror (lexic, "file_write: need two arguments 'fp' and 'data'\n");
      return NULL;
    }
  len = get_var_size_by_name (lexic, "data");


  for (n = 0; n < len;)
    {
      int e;
      errno = 0;
      e = write (fd, content + n, len - n);
      if (e < 0 && errno == EINTR)
        continue;
      else if (e <= 0)
        {
          nasl_perror (lexic, "file_write: write() failed - %s\n",
                       strerror (errno));
          break;
        }
      else
        n += e;
    }



  retc = alloc_typed_cell (CONST_INT);
  retc->x.i_val = n;
  return retc;
}
Example #23
0
tree_cell * nasl_shared_socket_register( lex_ctxt * lexic )
{
  char * name = get_str_local_var_by_name(lexic, "name");
  int    soc  = get_int_local_var_by_name(lexic, "socket", -1);
  struct arglist * script_infos = lexic->script_infos;
  int type, opt_len = sizeof(type);

 if ( name == NULL || soc < 0 )
 { 
  fprintf(stderr, "Usage: shared_socket_register(name:<name>, socket:<soc>)\n");
  return NULL;
 }

 if ( strncmp(name, SECRET_SOCKET_PREFIX, strlen(SECRET_SOCKET_PREFIX)) == 0 &&
      check_authenticated(lexic) < 0 ) return NULL;


 shared_socket_register(script_infos, soc, name);
 return FAKE_CELL;
}
tree_cell* nasl_ereg(lex_ctxt* lexic)
{
 char * pattern = get_str_local_var_by_name(lexic, "pattern");
 char * string = get_str_local_var_by_name(lexic, "string");
 int	icase       = get_int_local_var_by_name(lexic, "icase", 0);
 char * s;
 int copt = 0;
 tree_cell * retc;
 regex_t re;
 
 if(icase != 0)
 	copt = REG_ICASE;
 
 if(pattern == NULL || string == NULL)
 	return NULL;

 nasl_re_set_syntax(RE_SYNTAX_POSIX_EGREP);
 if(nasl_regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|copt))
  {
   nasl_perror(lexic, "ereg() : regcomp() failed\n");
   return NULL;
  }
  
  retc = alloc_tree_cell(0, NULL);
  retc->type = CONST_INT;
  string = estrdup(string);
  s = strchr(string, '\n');
  if ( s != NULL ) s[0] = '\0';
  if (s != string )
  {
  if(nasl_regexec(&re, string, 0, NULL, 0) == 0)
    retc->x.i_val = 1;
  else
    retc->x.i_val = 0;
  }
 else retc->x.i_val = 0;
 
 efree(&string);
 nasl_regfree(&re);
 return retc;
}
Example #25
0
tree_cell * set_kb_item(lex_ctxt * lexic)
{
 struct arglist * script_infos = lexic->script_infos;
 char * name  = get_str_local_var_by_name(lexic, "name");
 int type = get_local_var_type_by_name(lexic, "value");
 
 if( name == NULL )
 {
  nasl_perror(lexic, "Syntax error with set_kb_item() [null name]\n", name);
  return FAKE_CELL;
 }
 
 if (! lexic->authenticated && 
     strncmp(name, SECRET_KB_PREFIX, sizeof(SECRET_KB_PREFIX) - 1) == 0)
 {
  nasl_perror(lexic, "Only signed scripts can set a Secret/ KB entry\n");
  return FAKE_CELL;
 }


 if(type == VAR2_INT)
 {
  int value = get_int_local_var_by_name(lexic, "value", -1);
  if ( value != -1 )plug_set_key(script_infos, name, ARG_INT,(void*)value);
  else nasl_perror(lexic, "Syntax error with set_kb_item() [value=-1]\n", name); }
 else
 {
  char * value = get_str_local_var_by_name(lexic, "value");
  if( value == NULL )
  {
    nasl_perror(lexic, "Syntax error with set_kb_item() [null value]\n", name);
    return FAKE_CELL;
  }
  plug_set_key(script_infos, name, ARG_STRING, value);
 }

 return FAKE_CELL;
}
tree_cell *
nasl_socket_get_cert (lex_ctxt * lexic)
{
  int soc, cert_len = 0;
  tree_cell *retc;
  void *cert;

  soc = get_int_local_var_by_name (lexic, "socket", -1);
  if (soc < 0)
    {
      nasl_perror (lexic, "socket_get_cert: Erroneous socket value %d\n",
                   soc);
      return NULL;
    }
  socket_get_cert (soc, &cert, &cert_len);
  if (cert_len <= 0)
    return NULL;
  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_DATA;
  retc->x.str_val = cert;
  retc->size = cert_len;
  return retc;
}
tree_cell*
nasl_match(lex_ctxt* lexic)
{
  char		*pattern = get_str_local_var_by_name(lexic, "pattern");
  char		*string  = get_str_local_var_by_name(lexic, "string");
  int		icase     = get_int_local_var_by_name(lexic, "icase", 0);
  tree_cell	*retc;
  
  if (pattern == NULL)
    {
      nasl_perror(lexic, "nasl_match: parameter 'pattern' missing\n");
      pattern = "";
    }
  if (string == NULL)
    {
      nasl_perror(lexic, "nasl_match: parameter 'string' missing\n");
      string = "";
    }

  retc = alloc_tree_cell(0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = str_match(string, pattern, icase);
  return retc;
}
tree_cell *
nasl_socket_get_ssl_session_id (lex_ctxt * lexic)
{
  int soc;
  size_t sid_len = 0;
  tree_cell *retc;
  void *sid;

  soc = get_int_local_var_by_name (lexic, "socket", -1);
  if (soc < 0)
    {
      nasl_perror (lexic, "socket_get_cert: Erroneous socket value %d\n",
                   soc);
      return NULL;
    }
  socket_get_ssl_session_id (soc, &sid, &sid_len);
  if (sid == NULL || sid_len == 0)
    return NULL;
  retc = alloc_tree_cell (0, NULL);
  retc->type = CONST_DATA;
  retc->x.str_val = sid;
  retc->size = sid_len;
  return retc;
}
Example #29
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;
}
Example #30
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;
  }
}