31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <sys/param.h> 50 #include "../base/nvticache.h" 51 #include "../base/kb.h" 63 new = g_strconcat (
nvti_xref (n),
", ",
name,
":", value, NULL);
65 new = g_strconcat (
name,
":", value, NULL);
78 new = g_strconcat (
nvti_tag (n),
"|",
name,
"=", value, NULL);
80 new = g_strconcat (
name,
"=", value, NULL);
97 new = g_strdup_printf (
"%s, %s", old, depname);
109 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
133 char port_s[255], *kbstr;
134 const char *prange =
prefs_get (
"port_range");
140 if (!strcmp (proto,
"udp"))
143 kbstr =
"Host/udp_scanned";
148 kbstr =
"Host/scanned";
152 if (kb_item_get_int (
kb, kbstr) <= 0)
153 return unscanned_ports_as_closed (port_type);
159 return unscanned_ports_as_closed (port_type);
164 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
165 return kb_item_get_int (
kb, port_s) > 0;
207 return g_strdup (hinfos->
fqdn);
210 return g_strdup (hinfos->
fqdn);
236 mark_successful_plugin (
const char *
oid,
struct arglist *desc)
240 bzero (data,
sizeof (data));
241 snprintf (data,
sizeof (data),
"Success/%s",
oid);
246 mark_post (
const char *
oid,
struct arglist *desc,
const char *action,
249 char entry_name[255];
251 if (strlen (action) > (
sizeof (entry_name) - 20))
254 snprintf (entry_name,
sizeof (entry_name),
"SentData/%s/%s",
oid, action);
271 const char *action,
const char *what)
274 const char *prepend_tags, *append_tags;
275 char *buffer, *data, **nvti_tags = NULL;
284 action_str = g_string_new (
"");
287 action_str = g_string_new (action);
288 g_string_append (action_str,
"\n");
291 prepend_tags =
prefs_get (
"result_prepend_tags");
292 append_tags =
prefs_get (
"result_append_tags");
294 if (prepend_tags || append_tags)
297 nvti_tags = g_strsplit (tags,
"|", 0);
305 if (nvti_tags != NULL)
307 if (prepend_tags != NULL)
309 gchar **tags = g_strsplit (prepend_tags,
",", 0);
313 while (tags[i] != NULL)
317 tag_prefix = g_strconcat (tags[i],
"=", NULL);
318 while (nvti_tags[j] != NULL && tag_value == NULL)
320 if (g_str_has_prefix (nvti_tags[j], tag_prefix))
322 tag_value = g_strstr_len (nvti_tags[j], -1,
"=");
328 if (tag_value != NULL)
330 tag_value = tag_value + 1;
331 gchar *tag_line = g_strdup_printf (
"%s:\n%s\n\n", tags[i],
333 g_string_prepend (action_str, tag_line);
342 if (append_tags != NULL)
344 gchar **tags = g_strsplit (append_tags,
",", 0);
349 while (tags[i] != NULL)
353 tag_prefix = g_strconcat (tags[i],
"=", NULL);
354 while (nvti_tags[j] != NULL && tag_value == NULL)
356 if (g_str_has_prefix (nvti_tags[j], tag_prefix))
358 tag_value = g_strstr_len (nvti_tags[j], -1,
"=");
364 if (tag_value != NULL)
366 tag_value = tag_value + 1;
367 gchar *tag_line = g_strdup_printf (
"%s:\n%s\n\n", tags[i],
369 g_string_append (action_str, tag_line);
379 len = action_str->len;
380 buffer = g_malloc0 (1024 + len + 1);
388 snprintf (idbuffer,
sizeof (idbuffer),
"<|> %s ",
oid);
392 snprintf (buffer, 1024 + len,
393 "SERVER <|> %s <|> %s <|> %d/%s <|> %s %s<|> SERVER\n",
395 action_str->str, idbuffer);
398 snprintf (buffer, 1024 + len,
399 "SERVER <|> %s <|> %s <|> general/%s <|> %s %s<|> SERVER\n", what,
403 mark_post (
oid, desc, what, action);
406 data = g_convert (buffer, -1,
"UTF-8",
"ISO_8859-1", NULL, &length, NULL);
411 mark_successful_plugin (
oid, desc);
414 g_string_free (action_str, TRUE);
419 const char *proto,
const char *action)
436 const char *proto,
const char *action)
452 const char *proto,
const char *action)
479 char *plug_name, *cname, *retval = NULL;
488 cname = g_strdup (
name);
494 char *t = prefs->
name;
498 if (a && b && b[1] ==
':')
500 b += 2 *
sizeof (char);
501 if (!strcmp (cname, b))
505 if (!strcmp (t, plug_name))
508 retval = g_strdup (prefs->
value);
520 GSList *tmp, *nprefs;
533 g_slist_free_full (nprefs, (
void (*) (
void *))
nvtpref_free);
554 long contentsize = 0;
557 GError *error = NULL;
565 if (contentsize <= 0)
569 g_file_open_tmp (
"openvassd-file-upload.XXXXXX", &tmpfilename, &error);
573 " file for %s: %s", filename, error->message);
574 g_error_free (error);
579 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
582 " temporary file for %s: %s", filename, error->message);
583 g_error_free (error);
606 const char *identifier)
618 return g_hash_table_lookup (trans, identifier);
650 filesize_str = g_hash_table_lookup (trans, identifier);
651 if (filesize_str == NULL)
654 return atol (filesize_str);
668 kb_item_add_int (
kb,
name, GPOINTER_TO_SIZE (
value));
675 (
int) GPOINTER_TO_SIZE (
value));
691 kb_item_set_int (
kb,
name, GPOINTER_TO_SIZE (
value));
698 (
int) GPOINTER_TO_SIZE (
value));
720 static int _plug_get_key_son = 0;
723 plug_get_key_sighand_term ()
725 int son = _plug_get_key_son;
730 _plug_get_key_son = 0;
736 plug_get_key_sigchld ()
744 sig_n (
int signo,
void (*fnc) (
int))
750 sigemptyset (&sa.sa_mask);
751 sigaction (signo, &sa, (
struct sigaction *) 0);
755 sig_term (
void (*fcn) ())
757 sig_n (SIGTERM, fcn);
761 sig_chld (
void (*fcn) ())
763 sig_n (SIGCHLD, fcn);
770 struct kb_item *res = NULL, *res_list;
780 res = kb_item_get_all (
kb,
name);
785 if (res->
next == NULL || single)
792 ret = g_memdup (&res->
v_int, sizeof (res->
v_int));
798 ret = g_strdup (res->
v_str);
806 sig_chld (plug_get_key_sigchld);
812 socketpair (AF_UNIX, SOCK_STREAM, 0, sockpair);
813 if ((pid = fork ()) == 0)
827 arg_set_value (globals,
"global_socket", GSIZE_TO_POINTER (sockpair[1]));
829 srand48 (getpid () + getppid () + time (NULL));
835 ret = g_memdup (&res->
v_int, sizeof (res->
v_int));
841 ret = g_strdup (res->
v_str);
849 __func__, strerror (errno));
862 _plug_get_key_son = pid;
863 sig_term (plug_get_key_sighand_term);
875 FD_SET (sockpair[0], &rd);
876 e = select (sockpair[0] + 1, &rd, NULL, NULL, &tv);
878 while (e < 0 && errno == EINTR);
888 waitpid (pid, &status, WNOHANG);
889 _plug_get_key_son = 0;
921 int open21 = 0, open80 = 0;
922 #define MAX_CANDIDATES 16 924 int num_candidates = 0;
926 k = res = kb_item_get_pattern (
kb,
"Ports/tcp/*");
936 s = res->
name +
sizeof (
"Ports/tcp/") - 1;
944 candidates[num_candidates++] = ret;
954 if (num_candidates != 0)
955 return candidates[lrand48 () % num_candidates];
980 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
995 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1005 plug_set_ssl_item (
struct arglist *args,
char *item,
char *itemfname)
1008 snprintf (s,
sizeof (s),
"SSL/%s", item);
1015 plug_set_ssl_item (args,
"cert", cert);
1021 plug_set_ssl_item (args,
"key", key);
1027 plug_set_ssl_item (args,
"password", key);
1037 plug_set_ssl_item (args,
"CA", key);
1043 char *buf = getenv (
"PATH"), *pbuf, *p1, *p2;
1044 static char cmd[MAXPATHLEN];
1045 int len = strlen (
name);
1047 if (len >= MAXPATHLEN)
1054 while (*pbuf !=
'\0')
1056 for (p1 = pbuf, p2 = cmd; *p1 !=
':' && *p1 !=
'\0';)
1065 if (cmd[0] !=
'/' && safe)
1067 if (p2 - cmd + 1 + len >= MAXPATHLEN)
1071 snprintf (p2, MAXPATHLEN,
"/%s",
name);
1072 if (access (cmd, X_OK) == 0)
1075 if (stat (cmd, &st) < 0)
1077 else if (S_ISREG (st.st_mode))
void proto_post_error(const char *oid, struct arglist *desc, int port, const char *proto, const char *action)
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
void proto_post_alarm(const char *oid, struct arglist *desc, int port, const char *proto, const char *action)
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
gchar * nvti_xref(const nvti_t *n)
Get the xref's.
void plug_replace_key(struct arglist *args, char *name, int type, void *value)
void kb_item_free(struct kb_item *)
Release a KB item (or a list).
void plug_set_dep(struct arglist *desc, const char *depname)
void nvticache_reset()
Reset connection to KB. To be called after a fork().
void plug_set_tag(struct arglist *desc, char *name, char *value)
int arg_set_value(struct arglist *arglst, const char *name, void *value)
int port_in_port_ranges(int pnum, port_protocol_t ptype, array_t *pranges)
Checks if a port num is in port ranges array.
void host_add_port_proto(struct arglist *args, int portnum, char *proto)
nvtpref_t * nvtpref_new(gchar *name, gchar *type, gchar *dflt)
Create a new nvtpref structure filled with the given values.
void post_error(const char *oid, struct arglist *desc, int port, const char *action)
void plug_set_key(struct arglist *args, char *name, int type, const void *value)
void * plug_get_key(struct arglist *args, char *name, int *type, int single)
The structure of a information record that corresponds to a NVT.
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
void plug_set_ssl_CA_file(struct arglist *args, char *key)
int host_get_port_state(struct arglist *plugdata, int portnum)
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Protos and data structures for NVT Information data sets.
gchar * nvti_tag(const nvti_t *n)
Get the tag.
void array_free(GPtrArray *array)
Free global array value.
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
char * plug_get_host_fqdn(struct arglist *desc)
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
void proto_post_wrapped(const char *oid, struct arglist *desc, int port, const char *proto, const char *action, const char *what)
Post a security message (e.g. LOG, NOTE, WARNING ...).
const char * get_plugin_preference_fname(struct arglist *desc, const char *filename)
Get the file name of a plugins preference that is of type "file".
char * plug_get_host_ip_str(struct arglist *desc)
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
void plug_set_port_transport(struct arglist *args, int port, int tr)
void plug_set_ssl_pem_password(struct arglist *args, char *key)
kb_t plug_get_kb(struct arglist *args)
void post_log(const char *oid, struct arglist *desc, int port, const char *action)
Post a log message about a tcp port.
#define INTERNAL_COMM_CTRL_FINISHED
int internal_recv(int soc, char **data, int *data_sz, int *msg_type)
Top-level KB. This is to be inherited by KB implementations.
char * addr6_as_str(const struct in6_addr *addr6)
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
int nvti_set_xref(nvti_t *n, const gchar *xref)
Set the xrefs of a NVT.
port_protocol_t
Possible port types.
#define INTERNAL_COMM_MSG_TYPE_DATA
struct in6_addr * plug_get_host_ip(struct arglist *desc)
void post_alarm(const char *oid, struct arglist *desc, int port, const char *action)
void plug_set_xref(struct arglist *desc, char *name, char *value)
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
char * find_in_path(char *name, int safe)
void scanner_add_port(struct arglist *args, int port, char *proto)
int nvticache_initialized(void)
Return whether the nvt cache is initialized.
#define INTERNAL_COMM_MSG_TYPE_CTRL
char * get_plugin_preference_file_content(struct arglist *desc, const char *identifier)
Get the file contents of a plugins preference that is of type "file".
struct timeval timeval(unsigned long val)
GSList * nvticache_get_prefs(const char *oid)
Get the prefs from a plugin OID.
struct arglist * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
int host_get_port_state_udp(struct arglist *plugdata, int portnum)
int arg_get_value_int(struct arglist *args, const char *name)
int internal_send(int soc, char *data, int msg_type)
void add_plugin_preference(struct arglist *desc, const char *name, const char *type, const char *defaul)
int host_get_port_state_proto(struct arglist *plugdata, int portnum, char *proto)
void plug_set_ssl_cert(struct arglist *args, char *cert)
void plug_set_ssl_key(struct arglist *args, char *key)
The structure for a preference of a NVT.
unsigned int plug_get_host_open_port(struct arglist *desc)
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
const char * plug_get_hostname(struct arglist *desc)
char * nvticache_get_name(const char *oid)
Get the name from a plugin OID.
long get_plugin_preference_file_size(struct arglist *desc, const char *identifier)
Get the file size of a plugins preference that is of type "file".
void proto_post_log(const char *oid, struct arglist *desc, int port, const char *proto, const char *action)
Post a log message.
void * arg_get_value(struct arglist *args, const char *name)
int plug_get_port_transport(struct arglist *args, int port)
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
char * nvticache_get_tags(const char *oid)
Get the tags from a plugin OID.
char * get_plugin_preference(const char *oid, const char *name)
array_t * port_range_ranges(const char *port_range)
Create a range array from a port_range string.