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");