63 #include <openvas/misc/openvas_server.h> 64 #include <openvas/base/openvas_file.h> 69 #include <openvas/misc/openvas_logging.h> 71 #include <openvas/omp/omp.h> 72 #include <openvas/omp/xml.h> 77 #define OMP_PROGNAME "omp" 82 #define OPENVASMD_ADDRESS "127.0.0.1" 87 #define OPENVASMD_PORT 9390 93 #define SENDFILE_STR "SENDFILE" 98 #define DEFAULT_PING_TIMEOUT 10 134 connection_from_file (
const gchar * conf_file_path)
136 assert (conf_file_path);
137 GKeyFile *key_file = g_key_file_new ();
138 GError *error = NULL;
142 if (g_key_file_load_from_file (key_file, conf_file_path, 0, &error) == FALSE)
145 if (g_file_test (conf_file_path, G_FILE_TEST_EXISTS))
146 g_warning (
"Could not load connection configuration from %s: %s",
147 conf_file_path, error->message);
149 g_error_free (error);
150 g_key_file_free (key_file);
156 if (g_key_file_has_key (key_file,
"Connection",
"host", &error) == FALSE
157 || g_key_file_has_key (key_file,
"Connection",
"port", &error) == FALSE
158 || g_key_file_has_key (key_file,
"Connection",
"username",
160 || g_key_file_has_key (key_file,
"Connection",
"password",
163 g_warning (
"Connection configuration file misses entrie(s): %s",
165 g_error_free (error);
172 g_key_file_get_string (key_file,
"Connection",
"host", NULL);
174 g_key_file_get_string (key_file,
"Connection",
"port", NULL);
176 g_key_file_get_string (key_file,
"Connection",
"username", NULL);
178 g_key_file_get_string (key_file,
"Connection",
"password", NULL);
180 g_key_file_free (key_file);
197 char *ca_pub = NULL, *key_pub = NULL, *key_priv = NULL;
199 GError *error = NULL;
204 g_error_free (error);
207 if (!g_file_get_contents (connection->
client_cert_path, &key_pub, &len, &error))
210 g_error_free (error);
214 if (!g_file_get_contents (connection->
client_key_path, &key_priv, &len, &error))
216 fprintf (stderr,
"%s: %s\n", connection->
client_key_path, error->message);
217 g_error_free (error);
222 connection->
socket = openvas_server_open_with_cert
224 connection->
port, ca_pub, key_pub, key_priv);
230 connection->
socket = openvas_server_open
234 if (connection->
socket == -1)
236 fprintf (stderr,
"Failed to acquire socket.\n");
248 openvas_server_close (connection->
socket, connection->
session);
249 fprintf (stderr,
"Failed to authenticate.\n");
265 return openvas_server_close (server->
socket, server->
session);
276 print_tasks (entities_t tasks)
279 while ((task = first_entity (tasks)))
281 if (strcmp (entity_name (task),
"task") == 0)
283 entity_t entity, report;
285 const char *id, *name, *status, *progress;
287 id = entity_attribute (task,
"id");
290 fprintf (stderr,
"Failed to parse task ID.\n");
294 entity = entity_child (task,
"name");
297 fprintf (stderr,
"Failed to parse task name.\n");
300 name = entity_text (entity);
302 entity = entity_child (task,
"status");
305 fprintf (stderr,
"Failed to parse task status.\n");
308 status = entity_text (entity);
310 entity = entity_child (task,
"progress");
313 fprintf (stderr,
"Failed to parse task progress.\n");
316 progress = entity_text (entity);
318 printf (
"%s %-7s",
id, status);
319 if (strcmp (status,
"Running") == 0)
320 printf (
" %2s%% %s\n", progress, name);
322 printf (
" %s\n", name);
326 entity = entity_child (task,
"reports");
329 tasks = next_entities (tasks);
333 reports = entity->entities;
334 while ((report = first_entity (reports)))
336 if (strcmp (entity_name (report),
"report") == 0)
338 entity_t result_count;
339 const char *id, *status, *holes, *infos, *logs, *warnings;
340 const char *time_stamp;
342 id = entity_attribute (report,
"id");
345 fprintf (stderr,
"Failed to parse report ID.\n");
349 entity = entity_child (report,
"scan_run_status");
352 fprintf (stderr,
"Failed to parse report status.\n");
355 status = entity_text (entity);
357 result_count = entity_child (report,
"result_count");
358 if (result_count == NULL)
360 fprintf (stderr,
"Failed to parse report result_count.\n");
364 entity = entity_child (result_count,
"hole");
367 fprintf (stderr,
"Failed to parse report hole.\n");
370 holes = entity_text (entity);
372 entity = entity_child (result_count,
"info");
375 fprintf (stderr,
"Failed to parse report info.\n");
378 infos = entity_text (entity);
380 entity = entity_child (result_count,
"log");
383 fprintf (stderr,
"Failed to parse report log.\n");
386 logs = entity_text (entity);
388 entity = entity_child (result_count,
"warning");
391 fprintf (stderr,
"Failed to parse report warning.\n");
394 warnings = entity_text (entity);
396 entity = entity_child (report,
"timestamp");
399 fprintf (stderr,
"Failed to parse report timestamp.\n");
402 time_stamp = entity_text (entity);
404 printf (
" %s %-7s %2s %2s %2s %2s %s\n",
id, status,
405 holes, warnings, infos, logs, time_stamp);
407 reports = next_entities (reports);
410 tasks = next_entities (tasks);
423 print_configs (entities_t configs)
426 while ((config = first_entity (configs)))
428 if (strcmp (entity_name (config),
"config") == 0)
431 const char *id, *name;
433 id = entity_attribute (config,
"id");
436 fprintf (stderr,
"Failed to parse config ID.\n");
440 entity = entity_child (config,
"name");
443 fprintf (stderr,
"Failed to parse config name.\n");
446 name = entity_text (entity);
448 printf (
"%s %s\n",
id, name);
450 configs = next_entities (configs);
463 print_targets (entities_t targets)
466 while ((target = first_entity (targets)))
468 if (strcmp (entity_name (target),
"target") == 0)
471 const char *id, *name;
473 id = entity_attribute (target,
"id");
476 fprintf (stderr,
"Failed to parse target ID.\n");
480 entity = entity_child (target,
"name");
483 fprintf (stderr,
"Failed to parse target name.\n");
486 name = entity_text (entity);
488 printf (
"%s %s\n",
id, name);
490 targets = next_entities (targets);
507 const char* status_code;
510 if (openvas_server_sendf (session,
"<get_configs/>") == -1)
516 if (read_entity (session, status))
return -1;
520 status_code = entity_attribute (*status,
"status");
521 if (status_code == NULL)
523 free_entity (*status);
526 if (strlen (status_code) == 0)
528 free_entity (*status);
531 if (status_code[0] ==
'2')
return 0;
532 ret = (int) strtol (status_code, NULL, 10);
533 free_entity (*status);
534 if (errno == ERANGE)
return -1;
553 entity_t entity, version;
555 if (openvas_server_sendf (&(connection->
session),
"<get_version/>")
558 manager_close (connection);
565 if (read_entity (&(connection->
session), &entity))
567 fprintf (stderr,
"Failed to read response.\n");
568 manager_close (connection);
572 version = entity_child (entity,
"version");
575 free_entity (entity);
576 fprintf (stderr,
"Failed to parse version.\n");
577 manager_close (connection);
581 *version_str = g_strdup (entity_text(version));
583 free_entity (entity);
603 gchar * format, gchar * filter)
605 gchar *version = NULL;
606 gchar *default_format = NULL;
607 gchar *format_req_str = NULL;
609 if (manager_get_omp_version (connection, &version))
611 fprintf (stderr,
"Failed to determine OMP version.\n");
612 manager_close (connection);
616 if (strcmp (version,
"1.0") == 0)
618 default_format =
"XML";
619 format_req_str =
"format";
621 else if (strcmp (version,
"2.0") == 0)
623 default_format =
"d5da9f67-8551-4e51-807b-b6a873d70e34";
624 format_req_str =
"format_id";
628 default_format =
"a994b278-1f62-11e1-96ac-406186ea4fc5";
629 format_req_str =
"format_id";
634 if (format == NULL || strcasecmp (format, default_format) == 0)
636 gchar *quoted_filter;
637 entity_t entity, report_xml;
639 quoted_filter = filter ? g_strescape (filter,
"") : NULL;
641 if (openvas_server_sendf (&(connection->
session),
643 " result_hosts_only=\"0\"" 644 " first_result=\"0\"" 645 " sort_field=\"ROWID\"" 649 " report_id=\"%s\"/>",
653 quoted_filter ?
" filter=\"" :
"",
654 quoted_filter ? quoted_filter :
"",
655 quoted_filter ?
"\"" :
"",
658 fprintf (stderr,
"Failed to get report.\n");
659 manager_close (connection);
660 g_free (quoted_filter);
663 g_free (quoted_filter);
665 if (read_entity (&connection->
session, &entity)) {
666 fprintf (stderr,
"Failed to get report.\n");
667 manager_close (connection);
671 report_xml = entity_child (entity,
"report");
672 if (report_xml == NULL)
674 free_entity (entity);
675 fprintf (stderr,
"Failed to get report.\n");
676 manager_close (connection);
680 print_entity (stdout, report_xml);
684 gchar *quoted_filter;
685 guchar *report = NULL;
686 gsize report_size = 0;
691 quoted_filter = filter ? g_markup_escape_text (filter, -1) : NULL;
693 if (openvas_server_sendf (&(connection->
session),
694 "<get_reports %s=\"%s\" report_id=\"%s\" %s%s%s/>",
698 quoted_filter ?
" filter=\"" :
"",
699 quoted_filter ? quoted_filter :
"",
700 quoted_filter ?
"\"" :
""))
702 g_free (quoted_filter);
703 fprintf (stderr,
"Failed to get report.\n");
704 manager_close (connection);
707 g_free (quoted_filter);
712 if (read_entity (&connection->
session, &entity))
714 fprintf (stderr,
"Failed to get report.\n");
715 manager_close (connection);
721 status = entity_attribute (entity,
"status");
724 free_entity (entity);
725 fprintf (stderr,
"Failed to get report.\n");
726 manager_close (connection);
729 if (strlen (status) == 0)
731 free_entity (entity);
732 fprintf (stderr,
"Failed to get report.\n");
733 manager_close (connection);
739 const char* report_64;
742 report_xml = entity_child (entity,
"report");
743 if (report_xml == NULL)
745 free_entity (entity);
746 fprintf (stderr,
"Failed to get report.\n");
747 manager_close (connection);
751 report_64 = entity_text (report_xml);
752 if (strlen (report_64) == 0)
754 report = (guchar *) g_strdup (
"");
759 report = g_base64_decode (report_64, &report_size);
762 free_entity (entity);
766 free_entity (entity);
767 fprintf (stderr,
"Failed to get report.\n");
768 manager_close (connection);
772 if (fwrite (report, 1, report_size, stdout) < report_size)
774 fprintf (stderr,
"Failed to write entire report.\n");
775 manager_close (connection);
794 entity_t entity, format;
797 if (openvas_server_sendf (&(connection->
session),
"<get_report_formats/>")
800 manager_close (connection);
807 if (read_entity (&(connection->
session), &entity))
809 fprintf (stderr,
"Failed to read response.\n");
810 manager_close (connection);
814 formats = entity->entities;
815 while ((format = first_entity (formats)))
817 if (strcmp (entity_name (format),
"report_format") == 0)
822 id = entity_attribute (format,
"id");
825 free_entity (entity);
826 fprintf (stderr,
"Failed to parse report format ID.\n");
827 manager_close (connection);
831 name = entity_child (format,
"name");
834 free_entity (entity);
835 fprintf (stderr,
"Failed to parse report format name.\n");
836 manager_close (connection);
840 printf (
"%s %s\n",
id, entity_text (name));
842 formats = next_entities (formats);
845 free_entity (entity);
869 struct termios old, new;
873 if (tcgetattr (fileno (stream), &old) != 0)
876 new.c_lflag &= ~ECHO;
877 if (tcsetattr (fileno (stream), TCSAFLUSH, &
new) != 0)
881 nread = getline (lineptr, n, stream);
884 (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
892 HANDLE hConsoleHandle;
895 unsigned int bufSize = 512;
902 bufSize = *n ? *n : bufSize;
904 hConsoleHandle = CreateFile (
"CONIN$", GENERIC_READ | GENERIC_WRITE,
905 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
908 if (hConsoleHandle == INVALID_HANDLE_VALUE)
911 if (!SetConsoleMode (hConsoleHandle, ENABLE_LINE_INPUT |
912 ENABLE_PROCESSED_INPUT))
917 wchar_t wcsbuf[bufSize];
919 ReadConsoleW (hConsoleHandle, &wcsbuf, bufSize - 2, &nread, NULL);
920 CloseHandle (hConsoleHandle);
926 wcsbuf[nread - 2] =
'\n';
927 wcsbuf[nread - 1] =
'\0';
930 n2 = WideCharToMultiByte (CP_UTF8, 0, wcsbuf, nread - 1,
931 NULL, 0, NULL, NULL);
936 *lineptr = g_malloc (n2 + 1);
938 nread = WideCharToMultiByte (CP_UTF8, 0, wcsbuf, nread - 1,
939 *lineptr, n2, NULL, NULL);
956 my_gnutls_log_func (
int level,
const char *text)
958 fprintf (stderr,
"[%d] (%d) %s", getpid (), level, text);
959 if (*text && text[strlen (text) -1] !=
'\n')
964 replace_send_file_xml (
char **xml,
const char *path)
967 if (!xml || !*xml || !path)
973 fprintf (stderr,
"%s not found in xml command.\n",
SENDFILE_STR);
976 content = openvas_file_as_base64 (path);
981 *xml = g_strdup_printf (
"%s%s%s", *xml, content, s);
993 int exit_status = -1;
996 static gboolean prompt = FALSE;
997 static gboolean print_version = FALSE;
998 static gboolean be_verbose = FALSE;
999 static gboolean use_certs = FALSE;
1000 static gchar *client_cert_path = NULL;
1001 static gchar *client_key_path = NULL;
1002 static gchar *client_ca_cert_path = NULL;
1003 static gchar *conf_file_path = NULL;
1004 static gchar *send_file_path = NULL;
1005 static gchar *manager_host_string = NULL;
1006 static gchar *manager_port_string = NULL;
1007 static gchar *omp_username = NULL;
1008 static gchar *omp_password = NULL;
1010 static gchar *name = NULL;
1012 static gboolean cmd_create_task = FALSE;
1013 static gchar *comment = NULL;
1014 static gchar *config = NULL;
1015 static gchar *target = NULL;
1017 static gboolean cmd_delete_report = FALSE;
1019 static gboolean cmd_delete_task = FALSE;
1021 static gboolean cmd_get_report = FALSE;
1023 static gboolean cmd_get_report_formats = FALSE;
1025 static gboolean cmd_get_omp_version = FALSE;
1026 static gchar *format = NULL;
1028 static gboolean cmd_get_tasks = FALSE;
1030 static gboolean cmd_get_configs = FALSE;
1032 static gboolean cmd_get_targets = FALSE;
1034 static gboolean cmd_modify_task = FALSE;
1035 static gboolean file = FALSE;
1037 static gboolean cmd_start_task = FALSE;
1039 static gchar *filter = NULL;
1041 static gboolean cmd_details = FALSE;
1043 static gboolean cmd_ping = FALSE;
1046 static gchar *cmd_xml = NULL;
1048 static gchar **rest = NULL;
1050 static gboolean pretty_print = FALSE;
1052 GError *error = NULL;
1054 GOptionContext *option_context;
1055 static GOptionEntry option_entries[] = {
1057 {
"host",
'h', 0, G_OPTION_ARG_STRING, &manager_host_string,
1058 "Connect to manager on host <host>",
"<host>"},
1059 {
"port",
'p', 0, G_OPTION_ARG_STRING, &manager_port_string,
1060 "Use port number <number>",
"<number>"},
1061 {
"version",
'V', 0, G_OPTION_ARG_NONE, &print_version,
1062 "Print version.", NULL},
1063 {
"verbose",
'v', 0, G_OPTION_ARG_NONE, &be_verbose,
1064 "Verbose messages (WARNING: may reveal passwords).", NULL},
1065 {
"use-certs", 0, 0, G_OPTION_ARG_NONE, &use_certs,
1066 "Use client certificates to authenticate.", NULL},
1067 {
"client-cert", 0, 0, G_OPTION_ARG_FILENAME, &client_cert_path,
1068 "Client certificate. Default: " CLIENTCERT,
"<cert-file>"},
1069 {
"client-key", 0, 0, G_OPTION_ARG_FILENAME, &client_key_path,
1070 "Client key. Default: " CLIENTKEY,
"<key-file>"},
1071 {
"client-ca-cert", 0, 0, G_OPTION_ARG_FILENAME, &client_ca_cert_path,
1072 "Client CA certificate. Default: " CACERT,
"<cert-file>"},
1073 {
"username",
'u', 0, G_OPTION_ARG_STRING, &omp_username,
1074 "OMP username",
"<username>"},
1075 {
"password",
'w', 0, G_OPTION_ARG_STRING, &omp_password,
1076 "OMP password",
"<password>"},
1077 {
"config-file", 0, 0, G_OPTION_ARG_FILENAME, &conf_file_path,
1078 "Configuration file for connection parameters.",
"<config-file>"},
1079 {
"prompt",
'P', 0, G_OPTION_ARG_NONE, &prompt,
1080 "Prompt to exit.", NULL},
1081 {
"get-omp-version",
'O', 0, G_OPTION_ARG_NONE, &cmd_get_omp_version,
1082 "Print OMP version.", NULL},
1084 {
"name",
'n', 0, G_OPTION_ARG_STRING, &name,
1085 "Name for create-task.",
1088 {
"create-task",
'C', 0, G_OPTION_ARG_NONE, &cmd_create_task,
1089 "Create a task.", NULL},
1090 {
"comment",
'm', 0, G_OPTION_ARG_STRING, &comment,
1091 "Comment for create-task.",
1093 {
"config",
'c', 0, G_OPTION_ARG_STRING, &config,
1094 "Config for create-task.",
1096 {
"target",
't', 0, G_OPTION_ARG_STRING, &target,
1097 "Target for create-task.",
1100 {
"delete-report",
'E', 0, G_OPTION_ARG_NONE, &cmd_delete_report,
1101 "Delete one or more reports.", NULL},
1103 {
"delete-task",
'D', 0, G_OPTION_ARG_NONE, &cmd_delete_task,
1104 "Delete one or more tasks.", NULL},
1106 {
"get-report",
'R', 0, G_OPTION_ARG_NONE, &cmd_get_report,
1107 "Get report of one task.", NULL},
1108 {
"get-report-formats",
'F', 0, G_OPTION_ARG_NONE, &cmd_get_report_formats,
1109 "Get report formats. (OMP 2.0 only)", NULL},
1110 {
"format",
'f', 0, G_OPTION_ARG_STRING, &format,
1111 "Format for get-report.",
1113 {
"filter", 0, 0, G_OPTION_ARG_STRING, &filter,
1114 "Filter string for get-report",
1117 {
"get-tasks",
'G', 0, G_OPTION_ARG_NONE, &cmd_get_tasks,
1118 "Get status of one, many or all tasks.", NULL},
1120 {
"get-configs",
'g', 0, G_OPTION_ARG_NONE, &cmd_get_configs,
1121 "Get configs.", NULL},
1123 {
"get-targets",
'T', 0, G_OPTION_ARG_NONE, &cmd_get_targets,
1124 "Get targets.", NULL},
1126 {
"pretty-print",
'i', 0, G_OPTION_ARG_NONE, &pretty_print,
1127 "In combination with -X, pretty print the response.", NULL},
1129 {
"start-task",
'S', 0, G_OPTION_ARG_NONE, &cmd_start_task,
1130 "Start one or more tasks.", NULL},
1132 {
"modify-task",
'M', 0, G_OPTION_ARG_NONE, &cmd_modify_task,
1133 "Modify a task.", NULL},
1135 {
"ping", 0, 0, G_OPTION_ARG_NONE, &cmd_ping,
1136 "Ping OMP server", NULL},
1137 {
"timeout",
't', 0, G_OPTION_ARG_INT, &ping_timeout,
1138 "Wait <number> seconds for OMP ping response",
"<number>"},
1139 {
"file", 0, 0, G_OPTION_ARG_NONE, &file,
1140 "Add text in stdin as file on task.", NULL},
1142 {
"xml",
'X', 0, G_OPTION_ARG_STRING, &cmd_xml,
1143 "XML command (e.g. \"<help/>\"). \"-\" to read from stdin.",
1145 {
"send-file", 0, 0, G_OPTION_ARG_FILENAME, &send_file_path,
1146 "Replace SENDFILE in xml with base64 of file.",
"<file>"},
1147 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &rest,
1150 {
"details", 0, 0, G_OPTION_ARG_NONE, &cmd_details,
1151 "Enable detailed view.", NULL},
1152 {NULL, 0, 0, 0, NULL, NULL, NULL}
1155 if (setlocale (LC_ALL,
"") == NULL)
1157 printf (
"Failed to setlocale\n\n");
1158 exit (EXIT_FAILURE);
1162 g_option_context_new (
"- OpenVAS OMP Command Line Interface");
1163 g_option_context_add_main_entries (option_context, option_entries, NULL);
1164 if (!g_option_context_parse (option_context, &argc, &argv, &error))
1166 printf (
"%s\n\n", error->message);
1167 exit (EXIT_FAILURE);
1171 if (rest != NULL && *rest != NULL)
1173 if (g_strstr_len (*rest, -1,
"/?") != NULL)
1175 printf (
"%s", g_option_context_get_help (option_context, TRUE, NULL));
1176 exit (EXIT_SUCCESS);
1182 printf (
"OMP Command Line Interface %s\n", OPENVASCLI_VERSION);
1183 printf (
"Copyright (C) 2010-2016 Greenbone Networks GmbH\n");
1184 printf (
"License GPLv2+: GNU GPL version 2 or later\n");
1186 (
"This is free software: you are free to change and redistribute it.\n" 1187 "There is NO WARRANTY, to the extent permitted by law.\n\n");
1188 exit (EXIT_SUCCESS);
1195 (int) cmd_create_task + (
int) cmd_delete_report + (int) cmd_delete_task +
1196 (
int) cmd_get_report + (int) cmd_get_report_formats +
1197 (
int) cmd_get_tasks + (int) cmd_modify_task + (
int) cmd_start_task +
1198 (int) cmd_get_targets + (
int) cmd_get_omp_version + (int) cmd_get_configs +
1199 (
int) cmd_ping + (int) (cmd_xml != NULL);
1202 fprintf (stderr,
"One command option must be present.\n");
1203 exit (EXIT_FAILURE);
1207 fprintf (stderr,
"Only one command option must be present.\n");
1208 exit (EXIT_FAILURE);
1216 if (conf_file_path == NULL)
1217 conf_file_path = g_build_filename (g_get_home_dir (),
"omp.config", NULL);
1218 connection = connection_from_file (conf_file_path);
1219 g_free (conf_file_path);
1221 if (manager_host_string != NULL)
1226 if (manager_port_string != NULL)
1227 connection->
port = atoi (manager_port_string);
1233 if (connection->
port <= 0 || connection->
port >= 65536)
1235 fprintf (stderr,
"Manager port must be a number between 0 and 65536.\n");
1236 exit (EXIT_FAILURE);
1240 if (omp_username != NULL)
1241 connection->
username = omp_username;
1242 else if (connection->
username == NULL)
1243 connection->
username = g_strdup (g_get_user_name ());
1245 if (client_cert_path != NULL)
1250 if (client_key_path != NULL)
1255 if (client_ca_cert_path != NULL)
1260 if (ping_timeout < 0)
1262 connection->
timeout = ping_timeout;
1264 if (omp_password != NULL)
1265 connection->
password = omp_password;
1267 && !cmd_get_omp_version)
1272 printf (
"Enter password: ");
1278 fprintf (stderr,
"Failed to read password from console!\n");
1279 exit (EXIT_FAILURE);
1285 if (strlen (pw) > 0)
1289 fprintf (stderr,
"Password must be set.\n");
1290 exit (EXIT_FAILURE);
1299 printf (
"\nWARNING: Verbose mode may reveal passwords!\n\n");
1300 printf (
"Will try to connect to host %s, port %d...\n",
1305 if ((s=getenv (
"OPENVAS_GNUTLS_DEBUG")))
1307 gnutls_global_set_log_function (my_gnutls_log_func);
1308 gnutls_global_set_log_level (atoi (s));
1314 g_log_set_default_handler (openvas_log_silent, NULL);
1320 if (cmd_create_task)
1324 if (manager_open (connection))
1325 exit (EXIT_FAILURE);
1328 (&(connection->
session), name ? name :
"unnamed task",
1329 config ? config :
"Full and fast", target ? target :
"Localhost",
1330 comment ? comment :
"", &
id))
1332 fprintf (stderr,
"Failed to create task.\n");
1333 manager_close (connection);
1334 exit (EXIT_FAILURE);
1340 manager_close (connection);
1343 else if (cmd_delete_report)
1345 gchar **point = rest;
1347 if (point == NULL || *point == NULL)
1349 fprintf (stderr,
"delete-report requires at least one argument.\n");
1350 exit (EXIT_FAILURE);
1353 if (manager_open (connection))
1354 exit (EXIT_FAILURE);
1358 if (omp_delete_report (&(connection->
session), *point))
1360 fprintf (stderr,
"Failed to delete report %s, exiting.\n",
1362 manager_close (connection);
1363 exit (EXIT_FAILURE);
1368 manager_close (connection);
1371 else if (cmd_delete_task)
1373 gchar **point = rest;
1375 if (point == NULL || *point == NULL)
1377 fprintf (stderr,
"delete-task requires at least one argument.\n");
1378 exit (EXIT_FAILURE);
1381 if (manager_open (connection))
1382 exit (EXIT_FAILURE);
1386 if (omp_delete_task (&(connection->
session), *point))
1388 fprintf (stderr,
"Failed to delete task.\n");
1389 manager_close (connection);
1390 exit (EXIT_FAILURE);
1395 manager_close (connection);
1398 else if (cmd_get_tasks)
1400 gchar **point = rest;
1403 if (manager_open (connection))
1404 exit (EXIT_FAILURE);
1409 omp_get_task_opts_t opts;
1411 opts = omp_get_task_opts_defaults;
1412 opts.task_id = *point;
1416 if (omp_get_task_ext (&(connection->
session), opts, &status))
1418 fprintf (stderr,
"Failed to get status of task %s.\n", *point);
1419 manager_close (connection);
1420 exit (EXIT_FAILURE);
1424 if (print_tasks (status->entities))
1426 manager_close (connection);
1427 exit (EXIT_FAILURE);
1435 omp_get_tasks_opts_t opts;
1437 opts = omp_get_tasks_opts_defaults;
1442 opts.filter =
"permission=any owner=any rows=-1";
1444 if (omp_get_tasks_ext (&(connection->
session), opts, &status))
1446 fprintf (stderr,
"Failed to get status of all tasks.\n");
1447 manager_close (connection);
1448 exit (EXIT_FAILURE);
1450 if (print_tasks (status->entities))
1452 manager_close (connection);
1453 exit (EXIT_FAILURE);
1457 manager_close (connection);
1460 else if (cmd_get_configs)
1464 if (manager_open (connection))
1465 exit (EXIT_FAILURE);
1469 fprintf (stderr,
"Failed to get configs.\n");
1470 exit (EXIT_FAILURE);
1472 if (print_configs (status->entities))
1474 manager_close (connection);
1475 exit (EXIT_FAILURE);
1478 manager_close (connection);
1481 else if (cmd_get_targets)
1485 if (manager_open (connection))
1486 exit (EXIT_FAILURE);
1488 if (omp_get_targets (&(connection->
session), NULL, 0, 0, &status))
1490 fprintf (stderr,
"Failed to get targets.\n");
1491 exit (EXIT_FAILURE);
1493 if (print_targets (status->entities))
1495 manager_close (connection);
1496 exit (EXIT_FAILURE);
1499 manager_close (connection);
1502 else if (cmd_get_report)
1504 gchar **report_ids = rest;
1506 if (report_ids == NULL || *report_ids == NULL)
1508 fprintf (stderr,
"get-report requires one argument.\n");
1509 exit (EXIT_FAILURE);
1512 if (manager_open (connection))
1513 exit (EXIT_FAILURE);
1514 exit_status = manager_get_reports (connection, report_ids, format, filter);
1515 if (exit_status == 0)
1516 manager_close (connection);
1518 else if (cmd_get_report_formats)
1520 if (manager_open (connection))
1521 exit (EXIT_FAILURE);
1522 exit_status = manager_get_report_formats (connection);
1523 if (exit_status == 0)
1524 manager_close (connection);
1526 else if (cmd_get_omp_version)
1528 gchar *version = NULL;
1529 if (manager_open (connection))
1530 exit (EXIT_FAILURE);
1531 exit_status = manager_get_omp_version (connection, &version);
1532 printf (
"Version: %s\n", version);
1534 if (exit_status == 0)
1535 manager_close (connection);
1539 if (manager_open (connection))
1541 fprintf (stderr,
"OMP ping failed: Failed to establish connection.\n");
1552 fprintf (stdout,
"OMP ping was successful.\n");
1557 fprintf (stderr,
"OMP ping failed: Server closed connection.\n");
1562 fprintf (stderr,
"OMP ping failed: Timeout.\n");
1567 fprintf (stderr,
"OMP ping failed: Unknown error.\n");
1571 if (exit_status == 0)
1572 manager_close (connection);
1574 else if (cmd_modify_task)
1576 gchar **point = rest;
1580 if (point == NULL || *point == NULL)
1582 fprintf (stderr,
"modify-task requires one argument.\n");
1583 exit (EXIT_FAILURE);
1589 "modify-task requires the name option (path to file).\n");
1590 exit (EXIT_FAILURE);
1595 fprintf (stderr,
"modify-task requires the file option.\n");
1596 exit (EXIT_FAILURE);
1601 GIOChannel *stdin_channel;
1603 if (manager_open (connection))
1604 exit (EXIT_FAILURE);
1607 stdin_channel = g_io_channel_unix_new (fileno (stdin));
1608 g_io_channel_read_to_end (stdin_channel, &content, &content_len,
1610 g_io_channel_shutdown (stdin_channel, TRUE, NULL);
1611 g_io_channel_unref (stdin_channel);
1614 fprintf (stderr,
"failed to read from stdin: %s\n",
1616 g_error_free (error);
1617 exit (EXIT_FAILURE);
1623 manager_modify_task_file (connection, *point, name, content,
1624 content_len, error);
1626 if (omp_modify_task_file
1627 (&(connection->
session), *point, name, content, content_len))
1630 fprintf (stderr,
"Failed to modify task.\n");
1631 manager_close (connection);
1632 exit (EXIT_FAILURE);
1635 manager_close (connection);
1640 else if (cmd_start_task)
1642 gchar **point = rest;
1644 if (point == NULL || *point == NULL)
1646 fprintf (stderr,
"start-task requires at least one argument.\n");
1647 exit (EXIT_FAILURE);
1650 if (manager_open (connection))
1651 exit (EXIT_FAILURE);
1656 if (omp_start_task_report
1657 (&(connection->
session), *point, &report_id))
1659 fprintf (stderr,
"Failed to start task.\n");
1660 manager_close (connection);
1661 exit (EXIT_FAILURE);
1663 printf (
"%s\n", report_id);
1669 manager_close (connection);
1673 if (manager_open (connection))
1674 exit (EXIT_FAILURE);
1678 char *new_xml = cmd_xml;
1679 if (replace_send_file_xml (&new_xml, send_file_path))
1680 exit (EXIT_FAILURE);
1688 fprintf (stderr,
"Connected, press a key to continue.\n");
1692 if (strcmp (cmd_xml,
"-") == 0)
1697 GIOChannel *stdin_channel;
1701 stdin_channel = g_io_channel_unix_new (fileno (stdin));
1702 g_io_channel_read_to_end (stdin_channel, &content, &content_len,
1704 g_io_channel_shutdown (stdin_channel, TRUE, NULL);
1705 g_io_channel_unref (stdin_channel);
1708 fprintf (stderr,
"Failed to read from stdin: %s\n",
1710 g_error_free (error);
1711 exit (EXIT_FAILURE);
1719 printf (
"Sending to manager: %s\n", cmd_xml);
1721 if (openvas_server_sendf (&(connection->
session),
"%s", cmd_xml) == -1)
1723 manager_close (connection);
1724 fprintf (stderr,
"Failed to send_to_manager.\n");
1725 exit (EXIT_FAILURE);
1730 entity_t entity = NULL;
1731 if (read_entity (&(connection->
session), &entity))
1733 fprintf (stderr,
"Failed to read response.\n");
1734 manager_close (connection);
1735 exit (EXIT_FAILURE);
1739 printf (
"Got response:\n");
1740 if (pretty_print == FALSE)
1741 print_entity (stdout, entity);
1743 print_entity_format (entity, GINT_TO_POINTER (2));
1751 fprintf (stderr,
"Press a key when done.\n");
1755 manager_close (connection);
1756 free_entity (entity);
1769 printf (
"Command failed.\n");
1771 printf (
"Command completed successfully.\n");
ssize_t read_password(char **lineptr, size_t *n, FILE *stream)
Reads an entire line from a stream, suppressing character output.
gchar * password
Password for user with which to connect.
#define SENDFILE_STR
Substring to replace when using –send-file option.
#define DEFAULT_PING_TIMEOUT
Default timeout value for OMP pings.
int main(int argc, char **argv)
gboolean use_certs
Use client certificates to authenticate.
gchar * port_string
Server port string.
Information needed to handle a connection to a server.
int get_configs(gnutls_session_t *session, entity_t *status)
Get the list of scan configs.
int socket
Socket to server.
gchar * client_ca_cert_path
The file with the client ca certification.
#define OPENVASMD_PORT
Default Manager port.
#define OPENVASMD_ADDRESS
Default Manager (openvasmd) address.
gchar * client_cert_path
The file with the client certification.
gchar * username
Username with which to connect.
gnutls_session_t session
GnuTLS Session to use.
gchar * host_string
Server host string.
gint timeout
Timeout of request.
gchar * client_key_path
The file with the client key.