OpenVAS CLI  1.4.5
omp.c
Go to the documentation of this file.
1 /* OMP Command Line Interface
2  * $Id$
3  * Description: A command line client for the OpenVAS Management Protocol
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@greenbone.net>
7  * Michael Wiegand <michael.wiegand@greenbone.net>
8  *
9  * Copyright:
10  * Copyright (C) 2009, 2010, 2015, 2016 Greenbone Networks GmbH
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 
49 #define _GNU_SOURCE
50 
51 #include <assert.h>
52 #include <errno.h>
53 #include <glib.h>
54 #include <locale.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #ifndef _WIN32
59 #include <termios.h> /* for tcsetattr */
60 #endif
61 #include <unistd.h> /* for getpid */
62 
63 #include <openvas/misc/openvas_server.h>
64 #include <openvas/base/openvas_file.h>
65 #ifdef _WIN32
66 #include <winsock2.h>
67 #endif
68 #ifndef _WIN32
69 #include <openvas/misc/openvas_logging.h>
70 #endif
71 #include <openvas/omp/omp.h>
72 #include <openvas/omp/xml.h>
73 
77 #define OMP_PROGNAME "omp"
78 
82 #define OPENVASMD_ADDRESS "127.0.0.1"
83 
87 #define OPENVASMD_PORT 9390
88 
89 
93 #define SENDFILE_STR "SENDFILE"
94 
98 #define DEFAULT_PING_TIMEOUT 10
99 
100 /* Connection handling. */
101 
105 typedef struct
106 {
107  gnutls_session_t session;
108  int socket;
109  gchar *username;
110  gchar *password;
111  gchar *host_string;
112  gchar *port_string;
113  gint port;
114  gboolean use_certs;
118  gint timeout;
120 
133 static server_connection_t *
134 connection_from_file (const gchar * conf_file_path)
135 {
136  assert (conf_file_path);
137  GKeyFile *key_file = g_key_file_new ();
138  GError *error = NULL;
139  server_connection_t *connection = g_malloc0 (sizeof (*connection));
140 
141  /* Load key file. */
142  if (g_key_file_load_from_file (key_file, conf_file_path, 0, &error) == FALSE)
143  {
144  /* Be chatty about non trivial error (file does exist). */
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);
148 
149  g_error_free (error);
150  g_key_file_free (key_file);
151  return connection;
152  }
153 
154 #if 0
155  /* Check for completeness. */
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",
159  &error) == FALSE
160  || g_key_file_has_key (key_file, "Connection", "password",
161  &error) == FALSE)
162  {
163  g_warning ("Connection configuration file misses entrie(s): %s",
164  error->message);
165  g_error_free (error);
166  return NULL;
167  }
168 #endif
169 
170  /* Fill struct if any values found. */
171  connection->host_string =
172  g_key_file_get_string (key_file, "Connection", "host", NULL);
173  connection->port_string =
174  g_key_file_get_string (key_file, "Connection", "port", NULL);
175  connection->username =
176  g_key_file_get_string (key_file, "Connection", "username", NULL);
177  connection->password =
178  g_key_file_get_string (key_file, "Connection", "password", NULL);
179 
180  g_key_file_free (key_file);
181 
182  return connection;
183 }
184 
192 static int
193 manager_open (server_connection_t * connection)
194 {
195  if (connection->use_certs)
196  {
197  char *ca_pub = NULL, *key_pub = NULL, *key_priv = NULL;
198  gsize len = 0;
199  GError *error = NULL;
200 
201  if (!g_file_get_contents (connection->client_ca_cert_path, &ca_pub, &len, &error))
202  {
203  fprintf (stderr, "%s: %s\n", connection->client_ca_cert_path, error->message);
204  g_error_free (error);
205  return -1;
206  }
207  if (!g_file_get_contents (connection->client_cert_path, &key_pub, &len, &error))
208  {
209  fprintf (stderr, "%s: %s\n", connection->client_cert_path, error->message);
210  g_error_free (error);
211  g_free (ca_pub);
212  return -1;
213  }
214  if (!g_file_get_contents (connection->client_key_path, &key_priv, &len, &error))
215  {
216  fprintf (stderr, "%s: %s\n", connection->client_key_path, error->message);
217  g_error_free (error);
218  g_free (ca_pub);
219  g_free (key_pub);
220  return -1;
221  }
222  connection->socket = openvas_server_open_with_cert
223  (&connection->session, connection->host_string,
224  connection->port, ca_pub, key_pub, key_priv);
225  g_free (ca_pub);
226  g_free (key_pub);
227  g_free (key_priv);
228  }
229  else
230  connection->socket = openvas_server_open
231  (&connection->session, connection->host_string,
232  connection->port);
233 
234  if (connection->socket == -1)
235  {
236  fprintf (stderr, "Failed to acquire socket.\n");
237  return -1;
238  }
239 
240  if (connection->use_certs)
241  return 0;
242 
243  if (connection->username && connection->password)
244  {
245  if (omp_authenticate
246  (&connection->session, connection->username, connection->password))
247  {
248  openvas_server_close (connection->socket, connection->session);
249  fprintf (stderr, "Failed to authenticate.\n");
250  return -1;
251  }
252  }
253 
254  return 0;
255 }
256 
262 static int
263 manager_close (server_connection_t * server)
264 {
265  return openvas_server_close (server->socket, server->session);
266 }
267 
275 static int
276 print_tasks (entities_t tasks)
277 {
278  entity_t task;
279  while ((task = first_entity (tasks)))
280  {
281  if (strcmp (entity_name (task), "task") == 0)
282  {
283  entity_t entity, report;
284  entities_t reports;
285  const char *id, *name, *status, *progress;
286 
287  id = entity_attribute (task, "id");
288  if (id == NULL)
289  {
290  fprintf (stderr, "Failed to parse task ID.\n");
291  return -1;
292  }
293 
294  entity = entity_child (task, "name");
295  if (entity == NULL)
296  {
297  fprintf (stderr, "Failed to parse task name.\n");
298  return -1;
299  }
300  name = entity_text (entity);
301 
302  entity = entity_child (task, "status");
303  if (entity == NULL)
304  {
305  fprintf (stderr, "Failed to parse task status.\n");
306  return -1;
307  }
308  status = entity_text (entity);
309 
310  entity = entity_child (task, "progress");
311  if (entity == NULL)
312  {
313  fprintf (stderr, "Failed to parse task progress.\n");
314  return -1;
315  }
316  progress = entity_text (entity);
317 
318  printf ("%s %-7s", id, status);
319  if (strcmp (status, "Running") == 0)
320  printf (" %2s%% %s\n", progress, name);
321  else
322  printf (" %s\n", name);
323 
324  /* Print any reports indented under the task. */
325 
326  entity = entity_child (task, "reports");
327  if (entity == NULL)
328  {
329  tasks = next_entities (tasks);
330  continue;
331  }
332 
333  reports = entity->entities;
334  while ((report = first_entity (reports)))
335  {
336  if (strcmp (entity_name (report), "report") == 0)
337  {
338  entity_t result_count;
339  const char *id, *status, *holes, *infos, *logs, *warnings;
340  const char *time_stamp;
341 
342  id = entity_attribute (report, "id");
343  if (id == NULL)
344  {
345  fprintf (stderr, "Failed to parse report ID.\n");
346  return -1;
347  }
348 
349  entity = entity_child (report, "scan_run_status");
350  if (entity == NULL)
351  {
352  fprintf (stderr, "Failed to parse report status.\n");
353  return -1;
354  }
355  status = entity_text (entity);
356 
357  result_count = entity_child (report, "result_count");
358  if (result_count == NULL)
359  {
360  fprintf (stderr, "Failed to parse report result_count.\n");
361  return -1;
362  }
363 
364  entity = entity_child (result_count, "hole");
365  if (entity == NULL)
366  {
367  fprintf (stderr, "Failed to parse report hole.\n");
368  return -1;
369  }
370  holes = entity_text (entity);
371 
372  entity = entity_child (result_count, "info");
373  if (entity == NULL)
374  {
375  fprintf (stderr, "Failed to parse report info.\n");
376  return -1;
377  }
378  infos = entity_text (entity);
379 
380  entity = entity_child (result_count, "log");
381  if (entity == NULL)
382  {
383  fprintf (stderr, "Failed to parse report log.\n");
384  return -1;
385  }
386  logs = entity_text (entity);
387 
388  entity = entity_child (result_count, "warning");
389  if (entity == NULL)
390  {
391  fprintf (stderr, "Failed to parse report warning.\n");
392  return -1;
393  }
394  warnings = entity_text (entity);
395 
396  entity = entity_child (report, "timestamp");
397  if (entity == NULL)
398  {
399  fprintf (stderr, "Failed to parse report timestamp.\n");
400  return -1;
401  }
402  time_stamp = entity_text (entity);
403 
404  printf (" %s %-7s %2s %2s %2s %2s %s\n", id, status,
405  holes, warnings, infos, logs, time_stamp);
406  }
407  reports = next_entities (reports);
408  }
409  }
410  tasks = next_entities (tasks);
411  }
412  return 0;
413 }
414 
422 static int
423 print_configs (entities_t configs)
424 {
425  entity_t config;
426  while ((config = first_entity (configs)))
427  {
428  if (strcmp (entity_name (config), "config") == 0)
429  {
430  entity_t entity;
431  const char *id, *name;
432 
433  id = entity_attribute (config, "id");
434  if (id == NULL)
435  {
436  fprintf (stderr, "Failed to parse config ID.\n");
437  return -1;
438  }
439 
440  entity = entity_child (config, "name");
441  if (entity == NULL)
442  {
443  fprintf (stderr, "Failed to parse config name.\n");
444  return -1;
445  }
446  name = entity_text (entity);
447 
448  printf ("%s %s\n", id, name);
449  }
450  configs = next_entities (configs);
451  }
452  return 0;
453 }
454 
462 static int
463 print_targets (entities_t targets)
464 {
465  entity_t target;
466  while ((target = first_entity (targets)))
467  {
468  if (strcmp (entity_name (target), "target") == 0)
469  {
470  entity_t entity;
471  const char *id, *name;
472 
473  id = entity_attribute (target, "id");
474  if (id == NULL)
475  {
476  fprintf (stderr, "Failed to parse target ID.\n");
477  return -1;
478  }
479 
480  entity = entity_child (target, "name");
481  if (entity == NULL)
482  {
483  fprintf (stderr, "Failed to parse target name.\n");
484  return -1;
485  }
486  name = entity_text (entity);
487 
488  printf ("%s %s\n", id, name);
489  }
490  targets = next_entities (targets);
491  }
492  return 0;
493 }
494 
504 int
505 get_configs (gnutls_session_t* session, entity_t* status)
506 {
507  const char* status_code;
508  int ret;
509 
510  if (openvas_server_sendf (session, "<get_configs/>") == -1)
511  return -1;
512 
513  /* Read the response. */
514 
515  *status = NULL;
516  if (read_entity (session, status)) return -1;
517 
518  /* Check the response. */
519 
520  status_code = entity_attribute (*status, "status");
521  if (status_code == NULL)
522  {
523  free_entity (*status);
524  return -1;
525  }
526  if (strlen (status_code) == 0)
527  {
528  free_entity (*status);
529  return -1;
530  }
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;
535  return ret;
536 }
537 
538 
539 
540 /* Commands. */
541 
550 static int
551 manager_get_omp_version (server_connection_t * connection, gchar ** version_str)
552 {
553  entity_t entity, version;
554 
555  if (openvas_server_sendf (&(connection->session), "<get_version/>")
556  == -1)
557  {
558  manager_close (connection);
559  return -1;
560  }
561 
562  /* Read the response. */
563 
564  entity = NULL;
565  if (read_entity (&(connection->session), &entity))
566  {
567  fprintf (stderr, "Failed to read response.\n");
568  manager_close (connection);
569  return -1;
570  }
571 
572  version = entity_child (entity, "version");
573  if (version == NULL)
574  {
575  free_entity (entity);
576  fprintf (stderr, "Failed to parse version.\n");
577  manager_close (connection);
578  return -1;
579  }
580 
581  *version_str = g_strdup (entity_text(version));
582 
583  free_entity (entity);
584 
585  return 0;
586 }
587 
601 static int
602 manager_get_reports (server_connection_t * connection, gchar ** report_ids,
603  gchar * format, gchar * filter)
604 {
605  gchar *version = NULL;
606  gchar *default_format = NULL;
607  gchar *format_req_str = NULL;
608 
609  if (manager_get_omp_version (connection, &version))
610  {
611  fprintf (stderr, "Failed to determine OMP version.\n");
612  manager_close (connection);
613  return -1;
614  }
615 
616  if (strcmp (version, "1.0") == 0)
617  {
618  default_format = "XML";
619  format_req_str = "format";
620  }
621  else if (strcmp (version, "2.0") == 0)
622  {
623  default_format = "d5da9f67-8551-4e51-807b-b6a873d70e34";
624  format_req_str = "format_id";
625  }
626  else
627  {
628  default_format = "a994b278-1f62-11e1-96ac-406186ea4fc5";
629  format_req_str = "format_id";
630  }
631 
632  g_free (version);
633 
634  if (format == NULL || strcasecmp (format, default_format) == 0)
635  {
636  gchar *quoted_filter;
637  entity_t entity, report_xml;
638 
639  quoted_filter = filter ? g_strescape (filter, "") : NULL;
640 
641  if (openvas_server_sendf (&(connection->session),
642  "<get_reports"
643  " result_hosts_only=\"0\""
644  " first_result=\"0\""
645  " sort_field=\"ROWID\""
646  " sort_order=\"1\""
647  " %s=\"%s\""
648  "%s%s%s"
649  " report_id=\"%s\"/>",
650  format_req_str,
651  format ? format :
652  default_format,
653  quoted_filter ? " filter=\"" : "",
654  quoted_filter ? quoted_filter : "",
655  quoted_filter ? "\"" : "",
656  *report_ids))
657  {
658  fprintf (stderr, "Failed to get report.\n");
659  manager_close (connection);
660  g_free (quoted_filter);
661  return -1;
662  }
663  g_free (quoted_filter);
664 
665  if (read_entity (&connection->session, &entity)) {
666  fprintf (stderr, "Failed to get report.\n");
667  manager_close (connection);
668  return -1;
669  }
670 
671  report_xml = entity_child (entity, "report");
672  if (report_xml == NULL)
673  {
674  free_entity (entity);
675  fprintf (stderr, "Failed to get report.\n");
676  manager_close (connection);
677  return -1;
678  }
679 
680  print_entity (stdout, report_xml);
681  }
682  else
683  {
684  gchar *quoted_filter;
685  guchar *report = NULL;
686  gsize report_size = 0;
687  char first;
688  const char* status;
689  entity_t entity;
690 
691  quoted_filter = filter ? g_markup_escape_text (filter, -1) : NULL;
692 
693  if (openvas_server_sendf (&(connection->session),
694  "<get_reports %s=\"%s\" report_id=\"%s\" %s%s%s/>",
695  format_req_str,
696  format,
697  *report_ids,
698  quoted_filter ? " filter=\"" : "",
699  quoted_filter ? quoted_filter : "",
700  quoted_filter ? "\"" : ""))
701  {
702  g_free (quoted_filter);
703  fprintf (stderr, "Failed to get report.\n");
704  manager_close (connection);
705  return -1;
706  }
707  g_free (quoted_filter);
708 
709  /* Read the response. */
710 
711  entity = NULL;
712  if (read_entity (&connection->session, &entity))
713  {
714  fprintf (stderr, "Failed to get report.\n");
715  manager_close (connection);
716  return -1;
717  }
718 
719  /* Check the response. */
720 
721  status = entity_attribute (entity, "status");
722  if (status == NULL)
723  {
724  free_entity (entity);
725  fprintf (stderr, "Failed to get report.\n");
726  manager_close (connection);
727  return -1;
728  }
729  if (strlen (status) == 0)
730  {
731  free_entity (entity);
732  fprintf (stderr, "Failed to get report.\n");
733  manager_close (connection);
734  return -1;
735  }
736  first = status[0];
737  if (first == '2')
738  {
739  const char* report_64;
740  entity_t report_xml;
741 
742  report_xml = entity_child (entity, "report");
743  if (report_xml == NULL)
744  {
745  free_entity (entity);
746  fprintf (stderr, "Failed to get report.\n");
747  manager_close (connection);
748  return -1;
749  }
750 
751  report_64 = entity_text (report_xml);
752  if (strlen (report_64) == 0)
753  {
754  report = (guchar *) g_strdup ("");
755  report_size = 0;
756  }
757  else
758  {
759  report = g_base64_decode (report_64, &report_size);
760  }
761 
762  free_entity (entity);
763  }
764  else
765  {
766  free_entity (entity);
767  fprintf (stderr, "Failed to get report.\n");
768  manager_close (connection);
769  return -1;
770  }
771 
772  if (fwrite (report, 1, report_size, stdout) < report_size)
773  {
774  fprintf (stderr, "Failed to write entire report.\n");
775  manager_close (connection);
776  return -1;
777  }
778  g_free (report);
779  }
780 
781  return 0;
782 }
783 
791 static int
792 manager_get_report_formats (server_connection_t * connection)
793 {
794  entity_t entity, format;
795  entities_t formats;
796 
797  if (openvas_server_sendf (&(connection->session), "<get_report_formats/>")
798  == -1)
799  {
800  manager_close (connection);
801  return -1;
802  }
803 
804  /* Read the response. */
805 
806  entity = NULL;
807  if (read_entity (&(connection->session), &entity))
808  {
809  fprintf (stderr, "Failed to read response.\n");
810  manager_close (connection);
811  return -1;
812  }
813 
814  formats = entity->entities;
815  while ((format = first_entity (formats)))
816  {
817  if (strcmp (entity_name (format), "report_format") == 0)
818  {
819  const char *id;
820  entity_t name;
821 
822  id = entity_attribute (format, "id");
823  if (id == NULL)
824  {
825  free_entity (entity);
826  fprintf (stderr, "Failed to parse report format ID.\n");
827  manager_close (connection);
828  return -1;
829  }
830 
831  name = entity_child (format, "name");
832  if (name == NULL)
833  {
834  free_entity (entity);
835  fprintf (stderr, "Failed to parse report format name.\n");
836  manager_close (connection);
837  return -1;
838  }
839 
840  printf ("%s %s\n", id, entity_text (name));
841  }
842  formats = next_entities (formats);
843  }
844 
845  free_entity (entity);
846 
847  return 0;
848 }
849 
865 #ifndef _WIN32
866 ssize_t
867 read_password (char **lineptr, size_t *n, FILE *stream)
868 {
869  struct termios old, new;
870  int nread;
871 
872  /* Turn echoing off and fail if we can't. */
873  if (tcgetattr (fileno (stream), &old) != 0)
874  return -1;
875  new = old;
876  new.c_lflag &= ~ECHO;
877  if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
878  return -1;
879 
880  /* Read the password. */
881  nread = getline (lineptr, n, stream);
882 
883  /* Restore terminal. */
884  (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
885 
886  return nread;
887 }
888 #else
889 ssize_t
890 read_password (char **lineptr, size_t *n, FILE *stream)
891 {
892  HANDLE hConsoleHandle;
893  DWORD nread;
894  (void) stream;
895  unsigned int bufSize = 512;
896 
897  if (!lineptr || !n)
898  {
899  return -1;
900  }
901 
902  bufSize = *n ? *n : bufSize;
903 
904  hConsoleHandle = CreateFile ("CONIN$", GENERIC_READ | GENERIC_WRITE,
905  0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
906  NULL);
907 
908  if (hConsoleHandle == INVALID_HANDLE_VALUE)
909  return -1;
910 
911  if (!SetConsoleMode (hConsoleHandle, ENABLE_LINE_INPUT |
912  ENABLE_PROCESSED_INPUT))
913  return -1;
914 
915  /* Read the password */
916  {
917  wchar_t wcsbuf[bufSize];
918  int n2;
919  ReadConsoleW (hConsoleHandle, &wcsbuf, bufSize - 2, &nread, NULL);
920  CloseHandle (hConsoleHandle);
921 
922  if (nread <= 2)
923  return -1;
924 
925  /* Remove CR/LF */
926  wcsbuf[nread - 2] = '\n';
927  wcsbuf[nread - 1] = '\0';
928 
929  /* Convert to UTF-8 */
930  n2 = WideCharToMultiByte (CP_UTF8, 0, wcsbuf, nread - 1,
931  NULL, 0, NULL, NULL);
932 
933  if (n2 < 0)
934  return -1;
935 
936  *lineptr = g_malloc (n2 + 1);
937 
938  nread = WideCharToMultiByte (CP_UTF8, 0, wcsbuf, nread - 1,
939  *lineptr, n2, NULL, NULL);
940  if (nread < 0)
941  {
942  g_free (*lineptr);
943  return -1;
944  }
945  }
946 
947  return nread;
948 }
949 #endif
950 
951 
955 static void
956 my_gnutls_log_func (int level, const char *text)
957 {
958  fprintf (stderr, "[%d] (%d) %s", getpid (), level, text);
959  if (*text && text[strlen (text) -1] != '\n')
960  putc ('\n', stderr);
961 }
962 
963 static int
964 replace_send_file_xml (char **xml, const char *path)
965 {
966  char *s, *content;
967  if (!xml || !*xml || !path)
968  return 1;
969 
970  s = strstr (*xml, SENDFILE_STR);
971  if (!s)
972  {
973  fprintf (stderr, "%s not found in xml command.\n", SENDFILE_STR);
974  return 1;
975  }
976  content = openvas_file_as_base64 (path);
977  if (!content)
978  return 1;
979  *s = '\0';
980  s += strlen (SENDFILE_STR);
981  *xml = g_strdup_printf ("%s%s%s", *xml, content, s);
982  g_free (content);
983  return 0;
984 }
985 
986 /* Entry point. */
987 
988 int
989 main (int argc, char **argv)
990 {
991  server_connection_t *connection = NULL;
992  /* The return status of the command. */
993  int exit_status = -1;
994 
995  /* Global options. */
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;
1009  /* Shared command options. */
1010  static gchar *name = NULL;
1011  /* Command create-task. */
1012  static gboolean cmd_create_task = FALSE;
1013  static gchar *comment = NULL;
1014  static gchar *config = NULL;
1015  static gchar *target = NULL;
1016  /* Command delete-report. */
1017  static gboolean cmd_delete_report = FALSE;
1018  /* Command delete-task. */
1019  static gboolean cmd_delete_task = FALSE;
1020  /* Command get-report. */
1021  static gboolean cmd_get_report = FALSE;
1022  /* Command get-report-formats. */
1023  static gboolean cmd_get_report_formats = FALSE;
1024  /* Command get-omp-version. */
1025  static gboolean cmd_get_omp_version = FALSE;
1026  static gchar *format = NULL;
1027  /* Command get-tasks. */
1028  static gboolean cmd_get_tasks = FALSE;
1029  /* Command get-configs. */
1030  static gboolean cmd_get_configs = FALSE;
1031  /* Command get-targets. */
1032  static gboolean cmd_get_targets = FALSE;
1033  /* Command modify-task. */
1034  static gboolean cmd_modify_task = FALSE;
1035  static gboolean file = FALSE;
1036  /* Command start-task. */
1037  static gboolean cmd_start_task = FALSE;
1038  /* Filter string for get_reports */
1039  static gchar *filter = NULL;
1040  /* Command details */
1041  static gboolean cmd_details = FALSE;
1042  /* Command ping. */
1043  static gboolean cmd_ping = FALSE;
1044  static gint ping_timeout = DEFAULT_PING_TIMEOUT;
1045  /* Command given as XML. */
1046  static gchar *cmd_xml = NULL;
1047  /* The rest of the args. */
1048  static gchar **rest = NULL;
1049  /* Pretty print option. */
1050  static gboolean pretty_print = FALSE;
1051 
1052  GError *error = NULL;
1053 
1054  GOptionContext *option_context;
1055  static GOptionEntry option_entries[] = {
1056  /* Global options. */
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},
1083  /* Shared command options. */
1084  {"name", 'n', 0, G_OPTION_ARG_STRING, &name,
1085  "Name for create-task.",
1086  "<name>"},
1087  /* Command 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.",
1092  "<name>"},
1093  {"config", 'c', 0, G_OPTION_ARG_STRING, &config,
1094  "Config for create-task.",
1095  "<config>"},
1096  {"target", 't', 0, G_OPTION_ARG_STRING, &target,
1097  "Target for create-task.",
1098  "<target>"},
1099  /* Command delete-report. */
1100  {"delete-report", 'E', 0, G_OPTION_ARG_NONE, &cmd_delete_report,
1101  "Delete one or more reports.", NULL},
1102  /* Command delete-task. */
1103  {"delete-task", 'D', 0, G_OPTION_ARG_NONE, &cmd_delete_task,
1104  "Delete one or more tasks.", NULL},
1105  /* Command get-report. */
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.",
1112  "<format>"},
1113  {"filter", 0, 0, G_OPTION_ARG_STRING, &filter,
1114  "Filter string for get-report",
1115  "<string>"},
1116  /* Command get-tasks. */
1117  {"get-tasks", 'G', 0, G_OPTION_ARG_NONE, &cmd_get_tasks,
1118  "Get status of one, many or all tasks.", NULL},
1119  /* Command get-configs. */
1120  {"get-configs", 'g', 0, G_OPTION_ARG_NONE, &cmd_get_configs,
1121  "Get configs.", NULL},
1122  /* Command get-targets. */
1123  {"get-targets", 'T', 0, G_OPTION_ARG_NONE, &cmd_get_targets,
1124  "Get targets.", NULL},
1125  /* Pretty printing for "direct" xml (in combination with -X). */
1126  {"pretty-print", 'i', 0, G_OPTION_ARG_NONE, &pretty_print,
1127  "In combination with -X, pretty print the response.", NULL},
1128  /* Command start-task. */
1129  {"start-task", 'S', 0, G_OPTION_ARG_NONE, &cmd_start_task,
1130  "Start one or more tasks.", NULL},
1131  /* Command modify-task. */
1132  {"modify-task", 'M', 0, G_OPTION_ARG_NONE, &cmd_modify_task,
1133  "Modify a task.", NULL},
1134  /* Command ping. */
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},
1141  /* Command as XML. */
1142  {"xml", 'X', 0, G_OPTION_ARG_STRING, &cmd_xml,
1143  "XML command (e.g. \"<help/>\"). \"-\" to read from stdin.",
1144  "<command>"},
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,
1148  NULL, NULL},
1149  /* Enable details */
1150  {"details", 0, 0, G_OPTION_ARG_NONE, &cmd_details,
1151  "Enable detailed view.", NULL},
1152  {NULL, 0, 0, 0, NULL, NULL, NULL}
1153  };
1154 
1155  if (setlocale (LC_ALL, "") == NULL)
1156  {
1157  printf ("Failed to setlocale\n\n");
1158  exit (EXIT_FAILURE);
1159  }
1160 
1161  option_context =
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))
1165  {
1166  printf ("%s\n\n", error->message);
1167  exit (EXIT_FAILURE);
1168  }
1169 
1170  /* Check for Windows style help request */
1171  if (rest != NULL && *rest != NULL)
1172  {
1173  if (g_strstr_len (*rest, -1, "/?") != NULL)
1174  {
1175  printf ("%s", g_option_context_get_help (option_context, TRUE, NULL));
1176  exit (EXIT_SUCCESS);
1177  }
1178  }
1179 
1180  if (print_version)
1181  {
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");
1185  printf
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);
1189  }
1190 
1191  /* Check that one and at most one command option is present. */
1192  {
1193  int commands;
1194  commands =
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);
1200  if (commands == 0)
1201  {
1202  fprintf (stderr, "One command option must be present.\n");
1203  exit (EXIT_FAILURE);
1204  }
1205  if (commands > 1)
1206  {
1207  fprintf (stderr, "Only one command option must be present.\n");
1208  exit (EXIT_FAILURE);
1209  }
1210  }
1211 
1212  /* Setup the connection structure from the arguments and conf file.
1213  * Precedence of values is the following:
1214  * 1) command line argument (e.g. --port) 2) conf file 3) default */
1215 
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);
1220 
1221  if (manager_host_string != NULL)
1222  connection->host_string = manager_host_string;
1223  else if (connection->host_string == NULL)
1224  connection->host_string = OPENVASMD_ADDRESS;
1225 
1226  if (manager_port_string != NULL)
1227  connection->port = atoi (manager_port_string);
1228  else if (connection->port_string != NULL)
1229  connection->port = atoi (connection->port_string);
1230  else
1231  connection->port = OPENVASMD_PORT;
1232 
1233  if (connection->port <= 0 || connection->port >= 65536)
1234  {
1235  fprintf (stderr, "Manager port must be a number between 0 and 65536.\n");
1236  exit (EXIT_FAILURE);
1237  }
1238 
1239  connection->use_certs = use_certs;
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 ());
1244 
1245  if (client_cert_path != NULL)
1246  connection->client_cert_path = client_cert_path;
1247  else if (connection->client_cert_path == NULL)
1248  connection->client_cert_path = CLIENTCERT;
1249 
1250  if (client_key_path != NULL)
1251  connection->client_key_path = client_key_path;
1252  else if (connection->client_key_path == NULL)
1253  connection->client_key_path = CLIENTKEY;
1254 
1255  if (client_ca_cert_path != NULL)
1256  connection->client_ca_cert_path = client_ca_cert_path;
1257  else if (connection->client_ca_cert_path == NULL)
1258  connection->client_ca_cert_path = CACERT;
1259 
1260  if (ping_timeout < 0)
1261  ping_timeout = DEFAULT_PING_TIMEOUT;
1262  connection->timeout = ping_timeout;
1263 
1264  if (omp_password != NULL)
1265  connection->password = omp_password;
1266  else if (connection->password == NULL && !connection->use_certs && !cmd_ping
1267  && !cmd_get_omp_version)
1268  {
1269  gchar *pw = NULL;
1270  size_t n;
1271 
1272  printf ("Enter password: ");
1273  int ret = read_password (&pw, &n, stdin);
1274  printf ("\n");
1275 
1276  if (ret < 0)
1277  {
1278  fprintf (stderr, "Failed to read password from console!\n");
1279  exit (EXIT_FAILURE);
1280  }
1281 
1282  /* Remove the trailing newline character. */
1283  pw[ret - 1] = '\0';
1284 
1285  if (strlen (pw) > 0)
1286  connection->password = pw;
1287  else
1288  {
1289  fprintf (stderr, "Password must be set.\n");
1290  exit (EXIT_FAILURE);
1291  }
1292  }
1293 
1294  if (be_verbose)
1295  {
1296  const char *s;
1297 
1299  printf ("\nWARNING: Verbose mode may reveal passwords!\n\n");
1300  printf ("Will try to connect to host %s, port %d...\n",
1301  connection->host_string, connection->port);
1302 
1303  /* Enable GNUTLS debugging if the envvar, as used by the
1304  standard log functions, is set. */
1305  if ((s=getenv ("OPENVAS_GNUTLS_DEBUG")))
1306  {
1307  gnutls_global_set_log_function (my_gnutls_log_func);
1308  gnutls_global_set_log_level (atoi (s));
1309  }
1310  }
1311  else
1312  {
1313 #ifndef _WIN32
1314  g_log_set_default_handler (openvas_log_silent, NULL);
1315 #endif
1316  }
1317 
1318  /* Run the single command. */
1319 
1320  if (cmd_create_task)
1321  {
1322  char *id = NULL;
1323 
1324  if (manager_open (connection))
1325  exit (EXIT_FAILURE);
1326 
1327  if (omp_create_task
1328  (&(connection->session), name ? name : "unnamed task",
1329  config ? config : "Full and fast", target ? target : "Localhost",
1330  comment ? comment : "", &id))
1331  {
1332  fprintf (stderr, "Failed to create task.\n");
1333  manager_close (connection);
1334  exit (EXIT_FAILURE);
1335  }
1336 
1337  printf ("%s", id);
1338  putchar ('\n');
1339 
1340  manager_close (connection);
1341  exit_status = 0;
1342  }
1343  else if (cmd_delete_report)
1344  {
1345  gchar **point = rest;
1346 
1347  if (point == NULL || *point == NULL)
1348  {
1349  fprintf (stderr, "delete-report requires at least one argument.\n");
1350  exit (EXIT_FAILURE);
1351  }
1352 
1353  if (manager_open (connection))
1354  exit (EXIT_FAILURE);
1355 
1356  while (*point)
1357  {
1358  if (omp_delete_report (&(connection->session), *point))
1359  {
1360  fprintf (stderr, "Failed to delete report %s, exiting.\n",
1361  *point);
1362  manager_close (connection);
1363  exit (EXIT_FAILURE);
1364  }
1365  point++;
1366  }
1367 
1368  manager_close (connection);
1369  exit_status = 0;
1370  }
1371  else if (cmd_delete_task)
1372  {
1373  gchar **point = rest;
1374 
1375  if (point == NULL || *point == NULL)
1376  {
1377  fprintf (stderr, "delete-task requires at least one argument.\n");
1378  exit (EXIT_FAILURE);
1379  }
1380 
1381  if (manager_open (connection))
1382  exit (EXIT_FAILURE);
1383 
1384  while (*point)
1385  {
1386  if (omp_delete_task (&(connection->session), *point))
1387  {
1388  fprintf (stderr, "Failed to delete task.\n");
1389  manager_close (connection);
1390  exit (EXIT_FAILURE);
1391  }
1392  point++;
1393  }
1394 
1395  manager_close (connection);
1396  exit_status = 0;
1397  }
1398  else if (cmd_get_tasks)
1399  {
1400  gchar **point = rest;
1401  entity_t status;
1402 
1403  if (manager_open (connection))
1404  exit (EXIT_FAILURE);
1405 
1406  if (point)
1407  while (*point)
1408  {
1409  omp_get_task_opts_t opts;
1410 
1411  opts = omp_get_task_opts_defaults;
1412  opts.task_id = *point;
1413  opts.details = 1;
1414  opts.actions = "g";
1415 
1416  if (omp_get_task_ext (&(connection->session), opts, &status))
1417  {
1418  fprintf (stderr, "Failed to get status of task %s.\n", *point);
1419  manager_close (connection);
1420  exit (EXIT_FAILURE);
1421  }
1422  else
1423  {
1424  if (print_tasks (status->entities))
1425  {
1426  manager_close (connection);
1427  exit (EXIT_FAILURE);
1428  }
1429  }
1430 
1431  point++;
1432  }
1433  else
1434  {
1435  omp_get_tasks_opts_t opts;
1436 
1437  opts = omp_get_tasks_opts_defaults;
1438  if(cmd_details)
1439  opts.details = 1;
1440  else
1441  opts.details = 0;
1442  opts.filter = "permission=any owner=any rows=-1";
1443 
1444  if (omp_get_tasks_ext (&(connection->session), opts, &status))
1445  {
1446  fprintf (stderr, "Failed to get status of all tasks.\n");
1447  manager_close (connection);
1448  exit (EXIT_FAILURE);
1449  }
1450  if (print_tasks (status->entities))
1451  {
1452  manager_close (connection);
1453  exit (EXIT_FAILURE);
1454  }
1455  }
1456 
1457  manager_close (connection);
1458  exit_status = 0;
1459  }
1460  else if (cmd_get_configs)
1461  {
1462  entity_t status;
1463 
1464  if (manager_open (connection))
1465  exit (EXIT_FAILURE);
1466 
1467  if (get_configs (&(connection->session), &status))
1468  {
1469  fprintf (stderr, "Failed to get configs.\n");
1470  exit (EXIT_FAILURE);
1471  }
1472  if (print_configs (status->entities))
1473  {
1474  manager_close (connection);
1475  exit (EXIT_FAILURE);
1476  }
1477 
1478  manager_close (connection);
1479  exit_status = 0;
1480  }
1481  else if (cmd_get_targets)
1482  {
1483  entity_t status;
1484 
1485  if (manager_open (connection))
1486  exit (EXIT_FAILURE);
1487 
1488  if (omp_get_targets (&(connection->session), NULL, 0, 0, &status))
1489  {
1490  fprintf (stderr, "Failed to get targets.\n");
1491  exit (EXIT_FAILURE);
1492  }
1493  if (print_targets (status->entities))
1494  {
1495  manager_close (connection);
1496  exit (EXIT_FAILURE);
1497  }
1498 
1499  manager_close (connection);
1500  exit_status = 0;
1501  }
1502  else if (cmd_get_report)
1503  {
1504  gchar **report_ids = rest;
1505 
1506  if (report_ids == NULL || *report_ids == NULL)
1507  {
1508  fprintf (stderr, "get-report requires one argument.\n");
1509  exit (EXIT_FAILURE);
1510  }
1511 
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);
1517  }
1518  else if (cmd_get_report_formats)
1519  {
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);
1525  }
1526  else if (cmd_get_omp_version)
1527  {
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);
1533  g_free (version);
1534  if (exit_status == 0)
1535  manager_close (connection);
1536  }
1537  else if (cmd_ping)
1538  {
1539  if (manager_open (connection))
1540  {
1541  fprintf (stderr, "OMP ping failed: Failed to establish connection.\n");
1542  exit_status = 1;
1543  }
1544  else
1545  {
1546  int res;
1547  /* Returns 0 on success, 1 if manager closed connection, 2 on
1548  timeout, -1 on error */
1549  res = omp_ping (&(connection->session), connection->timeout);
1550  if (res == 0)
1551  {
1552  fprintf (stdout, "OMP ping was successful.\n");
1553  exit_status = 0;
1554  }
1555  else if (res == 1)
1556  {
1557  fprintf (stderr, "OMP ping failed: Server closed connection.\n");
1558  exit_status = 1;
1559  }
1560  else if (res == 2)
1561  {
1562  fprintf (stderr, "OMP ping failed: Timeout.\n");
1563  exit_status = 1;
1564  }
1565  else
1566  {
1567  fprintf (stderr, "OMP ping failed: Unknown error.\n");
1568  exit_status = 1;
1569  }
1570  }
1571  if (exit_status == 0)
1572  manager_close (connection);
1573  }
1574  else if (cmd_modify_task)
1575  {
1576  gchar **point = rest;
1577  gchar *content;
1578  gsize content_len;
1579 
1580  if (point == NULL || *point == NULL)
1581  {
1582  fprintf (stderr, "modify-task requires one argument.\n");
1583  exit (EXIT_FAILURE);
1584  }
1585 
1586  if (name == NULL)
1587  {
1588  fprintf (stderr,
1589  "modify-task requires the name option (path to file).\n");
1590  exit (EXIT_FAILURE);
1591  }
1592 
1593  if (file == FALSE)
1594  {
1595  fprintf (stderr, "modify-task requires the file option.\n");
1596  exit (EXIT_FAILURE);
1597  }
1598 
1599  if (file)
1600  {
1601  GIOChannel *stdin_channel;
1602 
1603  if (manager_open (connection))
1604  exit (EXIT_FAILURE);
1605  /* Mixing stream and file descriptor IO might lead to trouble. */
1606  error = NULL;
1607  stdin_channel = g_io_channel_unix_new (fileno (stdin));
1608  g_io_channel_read_to_end (stdin_channel, &content, &content_len,
1609  &error);
1610  g_io_channel_shutdown (stdin_channel, TRUE, NULL);
1611  g_io_channel_unref (stdin_channel);
1612  if (error)
1613  {
1614  fprintf (stderr, "failed to read from stdin: %s\n",
1615  error->message);
1616  g_error_free (error);
1617  exit (EXIT_FAILURE);
1618  }
1619 
1620 #if 0
1621 
1622  exit_status =
1623  manager_modify_task_file (connection, *point, name, content,
1624  content_len, error);
1625 #else
1626  if (omp_modify_task_file
1627  (&(connection->session), *point, name, content, content_len))
1628  {
1629  g_free (content);
1630  fprintf (stderr, "Failed to modify task.\n");
1631  manager_close (connection);
1632  exit (EXIT_FAILURE);
1633  }
1634 
1635  manager_close (connection);
1636  exit_status = 0;
1637 #endif
1638  }
1639  }
1640  else if (cmd_start_task)
1641  {
1642  gchar **point = rest;
1643 
1644  if (point == NULL || *point == NULL)
1645  {
1646  fprintf (stderr, "start-task requires at least one argument.\n");
1647  exit (EXIT_FAILURE);
1648  }
1649 
1650  if (manager_open (connection))
1651  exit (EXIT_FAILURE);
1652 
1653  while (*point)
1654  {
1655  char *report_id;
1656  if (omp_start_task_report
1657  (&(connection->session), *point, &report_id))
1658  {
1659  fprintf (stderr, "Failed to start task.\n");
1660  manager_close (connection);
1661  exit (EXIT_FAILURE);
1662  }
1663  printf ("%s\n", report_id);
1664  free (report_id);
1665  point++;
1666  }
1667  exit_status = 0;
1668 
1669  manager_close (connection);
1670  }
1671  else if (cmd_xml)
1672  {
1673  if (manager_open (connection))
1674  exit (EXIT_FAILURE);
1675 
1676  if (send_file_path)
1677  {
1678  char *new_xml = cmd_xml;
1679  if (replace_send_file_xml (&new_xml, send_file_path))
1680  exit (EXIT_FAILURE);
1681  g_free (cmd_xml);
1682  cmd_xml = new_xml;
1683  }
1684 
1686  if (prompt)
1687  {
1688  fprintf (stderr, "Connected, press a key to continue.\n");
1689  getchar ();
1690  }
1691 
1692  if (strcmp (cmd_xml, "-") == 0)
1693  {
1694  GError *error;
1695  gchar *content;
1696  gsize content_len;
1697  GIOChannel *stdin_channel;
1698 
1699  /* Mixing stream and file descriptor IO might lead to trouble. */
1700  error = NULL;
1701  stdin_channel = g_io_channel_unix_new (fileno (stdin));
1702  g_io_channel_read_to_end (stdin_channel, &content, &content_len,
1703  &error);
1704  g_io_channel_shutdown (stdin_channel, TRUE, NULL);
1705  g_io_channel_unref (stdin_channel);
1706  if (error)
1707  {
1708  fprintf (stderr, "Failed to read from stdin: %s\n",
1709  error->message);
1710  g_error_free (error);
1711  exit (EXIT_FAILURE);
1712  }
1713 
1714  g_free (cmd_xml);
1715  cmd_xml = content;
1716  }
1717 
1718  if (be_verbose)
1719  printf ("Sending to manager: %s\n", cmd_xml);
1720 
1721  if (openvas_server_sendf (&(connection->session), "%s", cmd_xml) == -1)
1722  {
1723  manager_close (connection);
1724  fprintf (stderr, "Failed to send_to_manager.\n");
1725  exit (EXIT_FAILURE);
1726  }
1727 
1728  /* Read the response. */
1729 
1730  entity_t entity = NULL;
1731  if (read_entity (&(connection->session), &entity))
1732  {
1733  fprintf (stderr, "Failed to read response.\n");
1734  manager_close (connection);
1735  exit (EXIT_FAILURE);
1736  }
1737 
1738  if (be_verbose)
1739  printf ("Got response:\n");
1740  if (pretty_print == FALSE)
1741  print_entity (stdout, entity);
1742  else
1743  print_entity_format (entity, GINT_TO_POINTER (2));
1744  printf ("\n");
1745 
1746  /* Cleanup. */
1747 
1749  if (prompt)
1750  {
1751  fprintf (stderr, "Press a key when done.\n");
1752  getchar ();
1753  }
1754 
1755  manager_close (connection);
1756  free_entity (entity);
1757 
1758  exit_status = 0;
1759  }
1760  else
1761  /* The option processing ensures that at least one command is present. */
1762  assert (0);
1763 
1764  /* Exit. */
1765 
1766  if (be_verbose)
1767  {
1768  if (exit_status)
1769  printf ("Command failed.\n");
1770  else
1771  printf ("Command completed successfully.\n");
1772  }
1773 
1774  exit (exit_status);
1775 }
ssize_t read_password(char **lineptr, size_t *n, FILE *stream)
Reads an entire line from a stream, suppressing character output.
Definition: omp.c:867
gchar * password
Password for user with which to connect.
Definition: omp.c:110
#define SENDFILE_STR
Substring to replace when using –send-file option.
Definition: omp.c:93
#define DEFAULT_PING_TIMEOUT
Default timeout value for OMP pings.
Definition: omp.c:98
int main(int argc, char **argv)
Definition: omp.c:989
gboolean use_certs
Use client certificates to authenticate.
Definition: omp.c:114
gchar * port_string
Server port string.
Definition: omp.c:112
Information needed to handle a connection to a server.
Definition: omp.c:105
int get_configs(gnutls_session_t *session, entity_t *status)
Get the list of scan configs.
Definition: omp.c:505
int socket
Socket to server.
Definition: omp.c:108
gchar * client_ca_cert_path
The file with the client ca certification.
Definition: omp.c:117
#define OPENVASMD_PORT
Default Manager port.
Definition: omp.c:87
#define OPENVASMD_ADDRESS
Default Manager (openvasmd) address.
Definition: omp.c:82
gchar * client_cert_path
The file with the client certification.
Definition: omp.c:115
gchar * username
Username with which to connect.
Definition: omp.c:109
gnutls_session_t session
GnuTLS Session to use.
Definition: omp.c:107
gint port
Port of server.
Definition: omp.c:113
gchar * host_string
Server host string.
Definition: omp.c:111
gint timeout
Timeout of request.
Definition: omp.c:118
gchar * client_key_path
The file with the client key.
Definition: omp.c:116