/openvas-client-3.0.3/openvas/cli.c
# · C · 636 lines · 482 code · 74 blank · 80 comment · 73 complexity · bc6b0fc17c344034de9db5c8c5ef459b MD5 · raw file
- /* OpenVAS Client
- * Copyright (C) 1998 - 2001 Renaud Deraison
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * cli.c - Command Line Interface manager
- *
- * modified by Axel Nennker <axel@nennker.de> 20020418
- * do not need gtk here
- * removed gcc -Wall complaints, NULL pointer checks
- */
- #include <includes.h>
- #include "globals.h"
- #ifdef LIBOPENVAS_OLD_INCLUDE_PATH
- #include <openvas/plugutils.h> /* for addslashes */
- #include <openvas/network.h> /* for openvas_get_socket_from_connection,
- close_stream_connection */
- #else
- #include <openvas/misc/plugutils.h> /* for addslashes */
- #include <openvas/misc/network.h> /* for openvas_get_socket_from_connection,
- close_stream_connection */
- #endif
- #include "openvas-client.h"
- #include "context.h"
- #include "preferences.h"
- #include "parser.h"
- #include "parseutils.h"
- #include "openvas_plugin.h"
- #include "cli.h"
- #undef USE_GTK
- #include "read_target_file.h"
- #include "nbe_output.h"
- #include "text_output.h"
- #include "latex_output.h"
- #include "xml_output_ng.h"
- #include "html_output.h"
- #include "html_graph_output.h"
- #include "attack.h"
- #include "auth.h"
- #include "comm.h"
- #include "backend.h"
- static struct cli_args * g_cli = NULL;
- /*---------------------------------------------------
- Private functions
- -----------------------------------------------------*/
- static int
- is_server_present (int soc)
- {
- fd_set rd;
- struct timeval tv = {2,0};
- int fd = openvas_get_socket_from_connection(soc);
- FD_ZERO(&rd);
- FD_SET(fd, &rd);
- if(select(fd+1, &rd, NULL, NULL, &tv) > 0)
- {
- int len = -1;
- ioctl(fd, FIONREAD, &len);
- if(!len){
- fprintf(stderr, "Communication closed by server\n");
- return 0;
- }
- }
- return 1;
- }
- void
- cli_sigterm (int s)
- {
- cli_report(g_cli);
- exit(5);
- }
- /**
- * @brief Monitor the test - read data from the client, and process it.
- */
- static void
- cli_test_monitor (struct cli_args * cli)
- {
- int type, finished = 0;
- static char buf [16384], msg [16384];
- int backend = backend_init(NULL);
- signal(SIGTERM, cli_sigterm);
- g_cli = cli;
- cli->backend = backend;
- while(!finished)
- {
- /* I don't think buf[0] == 0 is a case that will happen, but just
- * to be safe, as it was the previous semantics
- */
- if(network_gets(Context->socket, buf, sizeof(buf) - 1) < 0 || buf[0] == '\0')
- {
- if(!is_server_present(Context->socket))
- {
- fprintf(stderr, "OpenVAS-Client: The server abruptly shut the communication down - the test may be incomplete\n");
- finished = 1;
- }
- continue;
- }
- buf[strlen(buf)-1]=0;
- if((type = parse_server_message(Context, buf, backend, msg))==MSG_BYE)
- finished = 1;
- if(cli->verbose)
- {
- switch(type)
- {
- case MSG_STAT2:
- {
- char * hostname;
- char * action;
- char * current;
- int max;
- char * plug = NULL;
- parse_scanner_short_status(&(buf[2]), &hostname, &action, ¤t, &max);
- if(!strcmp(action, "portscan"))plug="";
- printf ("%s|%s|%s|%d\n", action,hostname,current,max);
- efree(&hostname);
- efree (&action);
- efree(¤t);
- }
- break;
- case MSG_STAT:
- {
- char * hostname;
- char * action;
- char* current;
- int max;
- parse_scanner_status(buf, &hostname, &action, ¤t, &max);
- printf ("%s|%s|%s|%d|foo\n", action,hostname,current,max);
- efree(&hostname);
- efree (&action);
- efree(¤t);
- }
- break;
- case MSG_FINISHED:
- printf("finished|%s||||\n", msg);
- break;
- }
- }
- fflush(stdout);
- msg[0] = '\0';
- }
- }
- /*---------------------------------------------------
- CLI arguments management
- ----------------------------------------------------*/
- struct cli_args *
- cli_args_new()
- {
- return emalloc(sizeof(struct cli_args));
- }
- void
- cli_args_server (struct cli_args * args, char * server)
- {
- if (args->server)
- free(args->server);
- args->server = strdup (server);
- }
- void
- cli_args_port (struct cli_args * args, int port)
- {
- args->port = port;
- }
- void
- cli_args_login (struct cli_args * args, char * login)
- {
- if (args->login)
- free (args->login);
- args->login = strdup (login);
- }
- void
- cli_args_password (struct cli_args * args, char * pwd)
- {
- if (args->password)
- free (args->password);
- args->password = strdup (pwd);
- }
- void
- cli_args_target (struct cli_args * args, char * target)
- {
- if (args->target)
- free (args->target);
- args->target = strdup (target);
- }
- void
- cli_args_results (struct cli_args * args, char * results)
- {
- char* ftype;
- if(args->results)free(args->results);
- args->results = strdup(results);
- if(args->extension)free(args->extension);
- /* choose output file type based on fname */
- ftype = strrchr(args->results, '.');
- if(!ftype)
- {
- if(args->results[strlen(args->results)-1]=='/')
- {
- args->results[strlen(args->results)-1]=0;
- ftype = "html_pie";
- }
- else
- {
- ftype = "nbe";
- }
- }
- else
- ftype++;
- args->extension = strdup(ftype);
- }
- void
- cli_args_verbose (struct cli_args * args, int verbose)
- {
- args->verbose = verbose;
- }
- /**
- * Sets the output function pointer in an cli_args.
- * The output function is chosen according to the type, and currently one of
- * - backend_to_nbe (type: "nbe", NULL)
- * - arglist_to_html_graph ("html_graph", "html_pie")
- * - arglist_to_html ("html")
- * - arglist_to_latex ("tex", "latex")
- * - arglist_to_text ("txt", "txt")
- * - backend_to_xml_ng ("xml")
- *
- * @param args The cli_args of interest.
- * @param type Sets the type of output created when calling the output function
- * ("xml", "nbe" etc).
- * @see backend_to_nbe
- * @see backend_to_xml_ng
- * @see arglist_to_latex
- * @see arglist_to_html
- * @see arglist_to_html_graph
- * @see arglist_to_text
- */
- void
- cli_args_output (struct cli_args * args, char * type)
- {
- char * ftype = args->extension;
- if(type)ftype = type;
- if(!ftype)
- {
- args->output = (output_func_t)backend_to_nbe;
- args->backend_output_func = 1;
- return;
- }
- #ifndef NO_GDCHART
- if(!strncmp(ftype, "html_pie", 8)||
- !strncmp(ftype, "html_graph", 10)) {
- args->output = arglist_to_html_graph;
- }
- else
- #endif /* NO_GDCHART */
- if (!strncmp(ftype, "html", 4)) {
- args->output = arglist_to_html;
- } else if (!strcmp(ftype, "latex") ||
- !strcmp(ftype, "tex")) {
- args->output = arglist_to_latex;
- } else if(!strcmp(ftype, "txt")||
- !strcmp(ftype, "text")) {
- args->output = arglist_to_text;
- }
- else if(!strcmp(ftype, "nbe"))
- {
- args->output = (output_func_t)backend_to_nbe;
- args->backend_output_func = 1;
- }
- else if(!strcmp(ftype, "xml"))
- {
- args->output = (output_func_t)backend_to_xml_ng;
- args->backend_output_func = 1;
- }
- else {
- fprintf(stderr, "'%s' is not a valid report type\n", ftype);
- exit(1);
- }
- }
- /*---------------------------------------------------------
- * Auditing now
- *--------------------------------------------------------*/
- int
- cli_connect_to_scanner (struct cli_args * cli)
- {
- const char * err;
- prefs_set_string(Context, "nessusd_host", cli->server);
- prefs_set_int(Context, "nessusd_port", cli->port);
- prefs_set_string(Context, "nessusd_user", cli->login);
- Context->passwd = cli->password;
- err = connect_to_scanner(Context);
- if(err)
- {
- fprintf(stderr, "OpenVAS-Client : %s\n", err);
- return -1;
- }
- return 0;
- }
- /**
- * @return -1 If targetfile could not be converted to a list or attack failed,
- * 0 otherwise.
- */
- int
- cli_test_network (struct cli_args * cli)
- {
- /* If we fail to turn the target file into a list then
- * We should _NOT_ try to attack anything */
- char * target_list = target_file_to_list(cli->target);
- if (target_list == NULL) {
- /* report the error */
- fprintf(stderr, "OpenVAS-Client : error turning targetfile (%s) to list\n", cli->target);
- return -1;
- }
- if(attack_host(target_list, Context))
- {
- cli_test_monitor(cli);
- return 0;
- }
- else
- return -1;
- }
- /**
- * @brief Exports a report with the output function set.
- */
- void
- cli_report (struct cli_args * cli)
- {
- if(!cli->backend_output_func)
- cli->output(backend_convert(cli->backend), cli->results);
- else
- cli->output((struct arglist*)GSIZE_TO_POINTER(cli->backend), cli->results);
- }
- /* Only difference to addslashes from addslashes.c is that single "'"s are escaped, too.
- Should verify if that is of any use and merge. */
- static char*
- sql_addslashes (char *in)
- {
- char * ret;
- char * out;
- if ( in == NULL ) return NULL;
- out = malloc(strlen(in) * 2 + 1);
- bzero(out, strlen(in) * 2 + 1);
- ret = out;
- while(in[0])
- {
- if(in[0] == '\\')
- {
- out[0] = '\\'; out++;
- out[0] = '\\'; out++;
- }
- else if(in[0] == '\n')
- {
- out[0] = '\\'; out++;
- out[0] = 'n'; out++;
- }
- else if(in[0] == '\r')
- {
- out[0] = '\\'; out++;
- out[0] = 'r'; out++;
- }
- else if(in[0] == '\'')
- {
- out[0] = '\\'; out++;
- out[0] = '\''; out++;
- }
- else {
- out[0] = in[0];
- out++;
- }
- in++;
- }
- return realloc(ret, strlen(ret) + 1);
- }
- /**
- * @brief Prints SQL statements to insert plugin informations into a plugins
- * @brief table to stdout.
- */
- static void
- _cli_sql_dump_plugins (struct openvas_plugin * p)
- {
- while (p != NULL )
- {
- char * m, * n;
- printf("INSERT INTO plugins VALUES ('%s', ", p->oid);
- m = sql_addslashes(nvti_name(p->ni));
- printf("'%s', ", m);
- efree(&m);
- m = sql_addslashes(nvti_family(p->ni));
- printf("'%s', ", m);
- efree(&m);
- m = sql_addslashes(p->category);
- printf("'%s', ", m);
- efree(&m);
- m = sql_addslashes(nvti_copyright(p->ni));
- printf("'%s', ", m);
- efree(&m);
- m = sql_addslashes(nvti_summary(p->ni));
- printf("'%s', ", m);
- efree(&m);
- n = nvti_description(p->ni);
- m = sql_addslashes(n);
- printf("'%s',", m);
- efree(&m);
- m = sql_addslashes(nvti_version(p->ni));
- printf("'%s',", m);
- efree(&m);
- if( nvti_cve(p->ni) != NULL )
- {
- m = sql_addslashes(nvti_cve(p->ni));
- printf("'%s',", m);
- efree(&m);
- }
- else printf("'',");
- if(nvti_bid(p->ni) != NULL)
- {
- m = sql_addslashes(nvti_bid(p->ni));
- printf("'%s',", m);
- efree(&m);
- }
- else printf("'',");
- if(nvti_xref(p->ni) != NULL)
- {
- m = sql_addslashes(nvti_xref(p->ni));
- printf("'%s');\n", m);
- efree(&m);
- }
- else printf("'');\n");
- p = p->next;
- }
- }
- static void
- _cli_dump_plugins (struct openvas_plugin * p)
- {
- while( p != NULL )
- {
- char * var = p->oid;
- char * n;
- printf("%s|", var);
- var = addslashes(nvti_family(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_name(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(p->category);
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_copyright(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_summary(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_version(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_cve(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_bid(p->ni));
- printf("%s|", var);
- efree(&var);
- var = addslashes(nvti_xref(p->ni));
- printf("%s|", var);
- efree(&var);
- n = nvti_description(p->ni);
- var = addslashes(n);
- printf("%s\n", var);
- efree(&var);
- p = p->next;
- }
- }
- /**
- * @brief Prints SQL statements to delete and recreate a plugins table and fill
- * @brief it with plugin information to stdout.
- */
- void
- cli_sql_dump_plugins (struct cli_args * cli)
- {
- printf("DROP TABLE IF EXISTS plugins;\n");
- printf("CREATE TABLE plugins (\n");
- printf(" oid varchar(50) NOT NULL,\n");
- printf(" name varchar(255),\n");
- printf(" family varchar(255),\n");
- printf(" category varchar(255),\n");
- printf(" copyright varchar(255),\n");
- printf(" summary varchar(255),\n");
- printf(" description blob,\n");
- printf(" version varchar(255),\n");
- printf(" cve_id varchar(255),\n");
- printf(" bugtraq_id varchar(255),\n");
- printf(" xref blob,\n");
- printf(" primary key (oid));\n");
- _cli_sql_dump_plugins(Context->plugins);
- _cli_sql_dump_plugins(Context->scanners);
- }
- void
- cli_dump_plugins (struct cli_args * cli)
- {
- _cli_dump_plugins (Context->plugins);
- _cli_dump_plugins (Context->scanners);
- }
- static void
- _cli_dump_pprefs ()
- {
- struct arglist * p = arg_get_value(Context->prefs, "PLUGINS_PREFS");
- if(!p)
- return;
- while(p->next)
- {
- switch(p->type)
- {
- case ARG_STRING:
- printf("%s = %s\n", p->name, (char*)p->value);
- break;
- case ARG_INT:
- printf("%s = %s\n", p->name, p->value ? "yes":"no");
- break;
- default:
- break;
- }
- p = p->next;
- }
- }
- void
- cli_sql_dump_prefs (struct cli_args * cli)
- {
- cli_dump_prefs (cli);
- }
- void
- cli_dump_prefs (struct cli_args * cli)
- {
- struct arglist * p = arg_get_value(Context->prefs, "SERVER_PREFS");
- if(!p)
- return;
- while(p->next)
- {
- switch(p->type)
- {
- case ARG_INT :
- printf("%s = %d\n", p->name, (int)GPOINTER_TO_SIZE(p->value));
- break;
- case ARG_STRING:
- printf("%s = %s\n", p->name, (char*)p->value);
- break;
- }
- p = p->next;
- }
- if(Context->plugins)_cli_dump_pprefs(Context->plugins);
- if(Context->scanners)_cli_dump_pprefs(Context->scanners);
- return;
- }
- int
- cli_close_connection (struct cli_args * cli)
- {
- return close_stream_connection(Context->socket);
- }