[Openvas-commits] r4841 - in trunk/openvas-libraries: . nasl

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Sep 4 11:54:08 CEST 2009


Author: felix
Date: 2009-09-04 11:54:07 +0200 (Fri, 04 Sep 2009)
New Revision: 4841

Modified:
   trunk/openvas-libraries/ChangeLog
   trunk/openvas-libraries/nasl/nasl_cmd_exec.c
   trunk/openvas-libraries/nasl/nasl_debug.c
   trunk/openvas-libraries/nasl/nasl_var.c
   trunk/openvas-libraries/nasl/strutils.c
   trunk/openvas-libraries/nasl/strutils.h
Log:
* nasl/strutils.c, nasl/strutils.h, nasl/nasl_cmd_exec.c, nasl_debug.c:
Cosmetics, major reformatting.
Added todos about possible replacement by glib and move to other part of
openvas-libraries, doc.

* nasl/nasl_var.c: Removed unnecessary include


Modified: trunk/openvas-libraries/ChangeLog
===================================================================
--- trunk/openvas-libraries/ChangeLog	2009-09-04 09:39:34 UTC (rev 4840)
+++ trunk/openvas-libraries/ChangeLog	2009-09-04 09:54:07 UTC (rev 4841)
@@ -1,5 +1,14 @@
 2009-09-04  Felix Wolfsteller <felix.wolfsteller at intevation.de>
 
+	* nasl/strutils.c, nasl/strutils.h, nasl/nasl_cmd_exec.c, nasl_debug.c:
+	Cosmetics, major reformatting.
+	Added todos about possible replacement by glib and move to other part of
+	openvas-libraries, doc.
+	
+	* nasl/nasl_var.c: Removed unnecessary include
+
+2009-09-04  Felix Wolfsteller <felix.wolfsteller at intevation.de>
+
 	* misc/openvas_logging.c, misc/openvas_server.c,
 	misc/openvas_ssh_login.c: Added todos regarding library reorganisation.
 

Modified: trunk/openvas-libraries/nasl/nasl_cmd_exec.c
===================================================================
--- trunk/openvas-libraries/nasl/nasl_cmd_exec.c	2009-09-04 09:39:34 UTC (rev 4840)
+++ trunk/openvas-libraries/nasl/nasl_cmd_exec.c	2009-09-04 09:54:07 UTC (rev 4841)
@@ -16,9 +16,12 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  */
- /*
-  * This file contains all the "unsafe" functions found in NASL
-  */
+
+/**
+ * @brief
+ * This file contains all the "unsafe" functions found in NASL.
+ */
+
 #include <includes.h>
 
 #include "nasl_tree.h"
@@ -36,20 +39,22 @@
 static  void (*old_sig_t)() = NULL, (*old_sig_i)() = NULL, (*old_sig_c) = NULL;
 
 static void
-sig_h()
+sig_h ()
 {
   if (pid > 0)
     (void) kill(pid, SIGKILL);
 }
 
-static void	sig_c()
+static void
+sig_c ()
 {
   if (pid > 0)
     (void) waitpid(pid, NULL, WNOHANG);
 }
 
+/** @TODO Supspects to glib replacements, all path related stuff. */
 tree_cell*
-nasl_pread(lex_ctxt * lexic)
+nasl_pread (lex_ctxt * lexic)
 {
   tree_cell	*retc = NULL, *a;
   anon_nasl_var	*v;
@@ -59,24 +64,25 @@
   FILE		*fp;
   char		cwd[MAXPATHLEN], newdir[MAXPATHLEN];
 
- if ( check_authenticated(lexic) < 0 ) return NULL;
-  
+ if (check_authenticated(lexic) < 0)
+   return NULL;
+
   if (pid != 0)
     {
       nasl_perror(lexic, "nasl_pread is not reentrant!\n");
       return NULL;
     }
 
-  a = get_variable_by_name(lexic, "argv");
-  cmd = get_str_local_var_by_name(lexic, "cmd");
+  a = get_variable_by_name (lexic, "argv");
+  cmd = get_str_local_var_by_name (lexic, "cmd");
   if (cmd == NULL || a == NULL || (v = a->x.ref_val) == NULL)
     {
       nasl_perror(lexic, "pread() usage: cmd:..., argv:...\n");
       return NULL;
     }
 
-  nice = get_int_local_var_by_name(lexic, "nice", 0);
-  
+  nice = get_int_local_var_by_name (lexic, "nice", 0);
+
   if (v->var_type == VAR2_ARRAY)
     av = &v->v.v_arr;
   else
@@ -86,79 +92,81 @@
       return NULL;
     }
 
-  cd = get_int_local_var_by_name(lexic, "cd", 0);
+  cd = get_int_local_var_by_name (lexic, "cd", 0);
 
   cwd[0] = '\0';
   if (cd)
     {
-      char	*p;
+      char *p;
 
       if (cmd[0] == '/')
-	{
-	  strncpy(newdir, cmd, sizeof(newdir)-1); /* Flawfinder: ignore
+        {
+          strncpy (newdir, cmd, sizeof(newdir)-1); /* Flawfinder: ignore
                                                      (\0-termination taken
                                                      care of) */
-	  p = strrchr(newdir, '/');
-	  if (p != newdir) *p = '\0';
-	}
+          p = strrchr (newdir, '/');
+          if (p != newdir)
+            *p = '\0';
+        }
       else
-	{
-	  p = find_in_path(cmd, 0);
-	  if (p != NULL)
-	    strncpy(newdir, p, sizeof(newdir)-1); /* Flawfinder: ignore
+        {
+          p = find_in_path (cmd, 0);
+          if (p != NULL)
+            strncpy (newdir, p, sizeof(newdir)-1); /* Flawfinder: ignore
                                                      (\0-termination taken
                                                      care of) */
-	  else
-	    {
-	      nasl_perror(lexic, "pread: '%s' not found in $PATH\n", cmd);
-	      return NULL;
-	    }
-	  
-	}
+          else
+            {
+              nasl_perror (lexic, "pread: '%s' not found in $PATH\n", cmd);
+              return NULL;
+            }
+
+        }
       newdir[sizeof(newdir)-1] = '\0';
 
       if (getcwd(cwd, sizeof(cwd)) == NULL)
-	{
-	  nasl_perror(lexic, "pread(): getcwd: %s\n", strerror(errno));
-	  *cwd = '\0';
-	}
+        {
+          nasl_perror(lexic, "pread(): getcwd: %s\n", strerror(errno));
+          *cwd = '\0';
+        }
 
       if (chdir(newdir) < 0)
-	{
-	  nasl_perror(lexic, "pread: could not chdir to %s\n", newdir);
-	  return NULL;
-	}
+        {
+          nasl_perror(lexic, "pread: could not chdir to %s\n", newdir);
+          return NULL;
+        }
       if (cmd[0] != '/' && strlen(newdir) + strlen(cmd) + 1 < sizeof(newdir))
-	{
-	  strcat(newdir, "/"); /* Flawfinder: ignore (if-command above checks
+        {
+          strcat(newdir, "/"); /* Flawfinder: ignore (if-command above checks
                                   for size) */
-	  strcat(newdir, cmd); /* Flawfinder: ignore (if-command above checks
+          strcat(newdir, cmd); /* Flawfinder: ignore (if-command above checks
                                   for size) */
-	  cmd = newdir;
-	}
+          cmd = newdir;
+        }
     }
 
   if (av->hash_elt != NULL)
-    nasl_perror(lexic, "pread: named elements in 'cmd' are ignored!\n");
+    nasl_perror (lexic, "pread: named elements in 'cmd' are ignored!\n");
   n = av->max_idx;
   args = emalloc(sizeof(char**) * (n+2)); /* Last arg is NULL */
-  for (j= 0, i = 0; i < n; i ++)
+  for (j = 0, i = 0; i < n; i ++)
     {
-      str = (char*)var2str(av->num_elt[i]);
+      str = (char*) var2str(av->num_elt[i]);
       if (str != NULL)
-	args[j++] = estrdup(str);
+        args[j++] = estrdup(str);
     }
   args[j++] = NULL;
 
-  old_sig_t = signal(SIGTERM, sig_h);
-  old_sig_i = signal(SIGINT, sig_h);
-  old_sig_c = signal(SIGCHLD, sig_c);
+  old_sig_t = signal (SIGTERM, sig_h);
+  old_sig_i = signal (SIGINT,  sig_h);
+  old_sig_c = signal (SIGCHLD, sig_c);
 
-  fp = nessus_popen4((const char*)cmd, args, &pid, nice);
+  fp = nessus_popen4 ((const char*)cmd, args, &pid, nice);
 
   for (i = 0; i < n; i ++)
-    efree(&args[i]);
-  efree(&args);
+    efree (&args[i]);
+  efree (&args);
+
   if (fp != NULL)
     {
       sz = 0; str = emalloc(1);
@@ -197,9 +205,9 @@
       retc->size = sz;
     }
 
-  signal(SIGINT, old_sig_i);
-  signal(SIGTERM, old_sig_t);
-  signal(SIGCHLD, old_sig_c);
+  signal (SIGINT,  old_sig_i);
+  signal (SIGTERM, old_sig_t);
+  signal (SIGCHLD, old_sig_c);
 
   return retc;
 }
@@ -229,7 +237,10 @@
 /*
  * Not a command, but dangerous anyway
  */
-
+/**
+ * @brief Read file if check_authenticated.
+ * @ingroup nasl_implement 
+ */
 tree_cell*
 nasl_fread(lex_ctxt * lexic)
 {
@@ -241,8 +252,8 @@
   int		alen, len, n;
   FILE		*fp;
 
- if ( check_authenticated(lexic) < 0 ) return NULL;
-    
+ if (check_authenticated(lexic) < 0) return NULL;
+
   fname = get_str_var_by_num(lexic, 0);
   if (fname == NULL)
     {
@@ -322,7 +333,7 @@
   retc->x.str_val = buf;
   fclose(fp);
   return retc;
-  
+
  error:
   efree(&buf);
   fclose(fp);
@@ -332,14 +343,17 @@
 /*
  * Not a command, but dangerous anyway
  */
-
+/**
+ * @brief Unlink file if check_authenticated.
+ * @ingroup nasl_implement
+ */
 tree_cell*
 nasl_unlink(lex_ctxt * lexic)
 {
   char		*fname;
 
  if (check_authenticated(lexic) < 0) return NULL;
-    
+
   fname = get_str_var_by_num(lexic, 0);
   if (fname == NULL)
     {
@@ -357,7 +371,9 @@
 }
 
 /* Definitely dangerous too */
-
+/**
+ * @brief Write file if check_authenticated.
+ */
 tree_cell*
 nasl_fwrite(lex_ctxt * lexic)
 {
@@ -373,7 +389,7 @@
       nasl_perror(lexic, "fwrite may only be called by an authenticated script\n");
       return NULL;
     }
-    
+
   content = get_str_local_var_by_name(lexic, "data");
   fname = get_str_local_var_by_name(lexic, "file");
   if (content == NULL || fname == NULL)
@@ -382,7 +398,7 @@
       return NULL;
     }
   len = get_var_size_by_name(lexic, "data");
-  
+
   if (lstat(fname, &lstat_info) == -1) {
     if (errno != ENOENT) {
       nasl_perror(lexic, "fwrite: %s: %s\n", fname, strerror(errno));
@@ -471,7 +487,10 @@
  *  File access functions : Dangerous
  */
 
-
+/**
+ * @brief Stat file if check_authenticated.
+ * @ingroup nasl_implement
+ */
 tree_cell*
 nasl_file_stat(lex_ctxt * lexic)
 {
@@ -496,7 +515,9 @@
   return retc;
 }
 
-
+/**
+ * @brief Open file if check_authenticated.
+ */
 tree_cell*
 nasl_file_open(lex_ctxt * lexic)
 {
@@ -567,7 +588,9 @@
   return retc;
 }
 
-
+/**
+ * @brief Close file if check_authenticated.
+ */
 tree_cell*
 nasl_file_close(lex_ctxt * lexic)
 {
@@ -595,7 +618,9 @@
 }
 
 
-
+/**
+ * @brief Read file if check_authenticated.
+ */
 tree_cell*
 nasl_file_read(lex_ctxt * lexic)
 {
@@ -605,9 +630,8 @@
   int           flength;
   int           n;
 
-  if ( check_authenticated(lexic) < 0 ) return NULL;
+  if (check_authenticated(lexic) < 0) return NULL;
 
-
  
   fd = get_int_local_var_by_name(lexic, "fp", -1);
   if (fd < 0 )
@@ -645,7 +669,9 @@
 }
 
 
-
+/**
+ * @brief Write file if check_authenticated.
+ */
 tree_cell*
 nasl_file_write(lex_ctxt * lexic)
 {
@@ -655,7 +681,7 @@
   int		fd;
   int 		n;
 
-  if ( check_authenticated(lexic) < 0 ) return NULL;
+  if (check_authenticated(lexic) < 0) return NULL;
 
   content = get_str_local_var_by_name(lexic, "data");
   fd = get_int_local_var_by_name(lexic, "fp", -1);
@@ -688,19 +714,20 @@
   return retc;
 }
 
-
+/**
+ * @brief Seek in file if check_authenticated.
+ */
 tree_cell*
-nasl_file_seek(lex_ctxt * lexic)
+nasl_file_seek (lex_ctxt * lexic)
 {
   tree_cell	*retc;
   int		fd;
   int           foffset;
 
-  if ( check_authenticated(lexic) < 0 ) return NULL;
+  if (check_authenticated(lexic) < 0)
+    return NULL;
 
 
-
-
   foffset = get_int_local_var_by_name(lexic, "offset", 0);
   fd = get_int_local_var_by_name(lexic, "fp", -1);
   if (fd < 0 )

Modified: trunk/openvas-libraries/nasl/nasl_debug.c
===================================================================
--- trunk/openvas-libraries/nasl/nasl_debug.c	2009-09-04 09:39:34 UTC (rev 4840)
+++ trunk/openvas-libraries/nasl/nasl_debug.c	2009-09-04 09:54:07 UTC (rev 4841)
@@ -57,39 +57,50 @@
  va_end(param);
 }
 
-
-int nasl_trace_enabled()
+/**
+ * @brief Checks if the nasl_trace_fp is set.
+ *
+ * @return 0 if nasl_trace_fp == NULL, 1 otherwise.
+ */
+int
+nasl_trace_enabled ()
 {
- if( nasl_trace_fp == NULL )
+ if (nasl_trace_fp == NULL)
    return 0;
- else 
-  return 1;  
+ else
+   return 1;
 }
 
-
-void nasl_trace(lex_ctxt * lexic, char * msg, ...)
+/**
+ * @brief Prints debug message in printf fashion to nasl_trace_fp if it exists.
+ *
+ * Like @ref nasl_perror, but to the nasl_trace_fp.
+ */
+void
+nasl_trace (lex_ctxt * lexic, char * msg, ...)
 {
  va_list param;
  char debug_message[4096];
  char *script_name = "", *p;
-  
- if(nasl_trace_fp == NULL)
-	 return;
- va_start(param, msg);
- 
- if( lexic != NULL )
- {
-  script_name = arg_get_value(lexic->script_infos, "script_name");
-  if(script_name == NULL)
-   script_name = "";
- }
- 
- vsnprintf(debug_message, sizeof(debug_message), msg, param);
+
+ if (nasl_trace_fp == NULL)
+  return;
+ va_start (param, msg);
+
+ if (lexic != NULL)
+  {
+    script_name = arg_get_value (lexic->script_infos, "script_name");
+    if (script_name == NULL)
+    script_name = "";
+  }
+
+ vsnprintf (debug_message, sizeof(debug_message), msg, param);
  for (p = debug_message; *p != '\0'; p ++)
    ;
  if (p == debug_message || p[-1] != '\n')
-   fprintf(nasl_trace_fp, "[%d](%s) %s\n",getpid(), script_name, debug_message); 
+   fprintf (nasl_trace_fp, "[%d](%s) %s\n", getpid(), script_name, debug_message);
  else
-   fprintf(nasl_trace_fp, "[%d](%s) %s",getpid(), script_name, debug_message); 
- va_end(param);
+   fprintf (nasl_trace_fp, "[%d](%s) %s", getpid(), script_name, debug_message);
+
+ va_end (param);
 }

Modified: trunk/openvas-libraries/nasl/nasl_var.c
===================================================================
--- trunk/openvas-libraries/nasl/nasl_var.c	2009-09-04 09:39:34 UTC (rev 4840)
+++ trunk/openvas-libraries/nasl/nasl_var.c	2009-09-04 09:54:07 UTC (rev 4841)
@@ -25,7 +25,7 @@
 #include "nasl_var.h"
 #include "nasl_lex_ctxt.h"
 #include "exec.h"
-
+#include "system.h"
 #include "nasl_debug.h"
 
 #ifndef NASL_DEBUG

Modified: trunk/openvas-libraries/nasl/strutils.c
===================================================================
--- trunk/openvas-libraries/nasl/strutils.c	2009-09-04 09:39:34 UTC (rev 4840)
+++ trunk/openvas-libraries/nasl/strutils.c	2009-09-04 09:54:07 UTC (rev 4841)
@@ -20,35 +20,42 @@
 
 #include "strutils.h"
 
-char * nasl_strndup(char * str, int length)
+/**
+ * @TODO These functions are not necessarily nasl-specific and thus subject to
+ *       be moved (e.g. to misc).
+ */
+
+char *
+nasl_strndup (char * str, int length)
 {
- char * ret = emalloc(length + 1);
- bcopy(str, ret, length);
- return ret; 
+  char * ret = emalloc (length + 1);
+  bcopy (str, ret, length);
+  return ret;
 }
 
-
-int str_match(const char* string, const char* pattern, int icase)
+/** @TODO partly replacable by g_pattern_match function (when not icase) */
+int
+str_match (const char* string, const char* pattern, int icase)
 {
   while (*pattern != '\0')
     {
       if (*pattern == '?')
-	{
-	  if (*string == '\0')
-	    return 0;
-	}
+        {
+          if (*string == '\0')
+            return 0;
+        }
       else if (*pattern == '*')
-	{
-	  const char	*p = string;
-	  do
-	    if (str_match(p, pattern+1, icase))
-	      return 1;
-	  while (*p ++ != '\0');
-	  return 0;
-	}
-      else if ((icase && (tolower(*pattern) != tolower(*string))) ||
-	       (! icase && (*pattern != *string)))
-	return 0;
+        {
+          const char *p = string;
+          do
+            if (str_match (p, pattern + 1, icase))
+              return 1;
+          while (*p ++ != '\0');
+          return 0;
+        }
+      else if ( (icase && (tolower (*pattern) != tolower (*string))) ||
+                (! icase && (*pattern != *string)))
+        return 0;
       pattern ++; string ++;
     }
   return *string == '\0';
@@ -56,34 +63,35 @@
 
 
 
-/*
- * Slow replacement for memmem()
+/**
+ * @brief Slow replacement for memmem.
  */
-void * nasl_memmem(haystack, hl_len, needle, n_len)
- const void *  haystack;
- size_t hl_len;
- const void * needle;
- size_t n_len;
+void *
+nasl_memmem (const void * haystack, size_t hl_len,
+             const void * needle, size_t n_len)
 {
- char * hs = (char*)haystack;
- char * nd = (char*)needle;
- int i;
+  char * hs = (char*) haystack;
+  char * nd = (char*) needle;
+  int i;
 
- if ( hl_len < n_len )
-	return NULL;
+  if (hl_len < n_len)
+    return NULL;
 
- for(i=0;i<=hl_len-n_len;i++)
- {
-  if(hs[i] == nd[0])
-  { 
-   int flag = 1;
-   int j;
-   for(j = 1;j < n_len; j++)if(hs[i+j] != nd[j] ){ flag=0;break; }
-   if(flag != 0)
-   	return( hs + i );
-  }
- }
- return(NULL);
+  for (i = 0 ; i <= hl_len - n_len ; i++)
+    {
+      if (hs[i] == nd[0])
+        {
+          int flag = 1;
+          int j;
+          for (j = 1; j < n_len; j++)
+            if (hs[i+j] != nd[j])
+              {
+                flag = 0;
+                break;
+              }
+          if (flag != 0)
+            return (hs + i);
+        }
+    }
+  return (NULL);
 }
-
-

Modified: trunk/openvas-libraries/nasl/strutils.h
===================================================================
--- trunk/openvas-libraries/nasl/strutils.h	2009-09-04 09:39:34 UTC (rev 4840)
+++ trunk/openvas-libraries/nasl/strutils.h	2009-09-04 09:54:07 UTC (rev 4841)
@@ -27,9 +27,12 @@
  * do so, delete this exception statement from your version.
  *
  */
+
 #ifndef NASL2_STRUTILS_H__
 #define NASL2_STRUTILS_H__
-char* nasl_strndup(char*, int);
-int str_match(const char *, const char *, int);
-void * nasl_memmem(const void *, size_t, const void *, size_t);
+
+char * nasl_strndup (char*, int);
+int str_match (const char *, const char *, int);
+void * nasl_memmem (const void *, size_t, const void *, size_t);
+
 #endif



More information about the Openvas-commits mailing list