-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathgvmd.c
3432 lines (3005 loc) · 99.8 KB
/
gvmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2009-2022 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file gvmd.c
* @brief The Greenbone Vulnerability Manager daemon.
*
* This file defines the Greenbone Vulnerability Manager daemon. The Manager
* serves the Greenbone Management Protocol (GMP) to clients such as Greenbone
* Security Assistant (the web interface). The Manager and GMP give clients
* full access to an OpenVAS Scanner.
*
* The entry point to the daemon is the \ref main function. From there
* the references in the function documentation describe the flow of
* control in the program.
*/
/**
* \mainpage
*
* \section Introduction
* \verbinclude README.md
*
* \section manpages Manual Pages
* \subpage manpage
*
* \section Installation
* \verbinclude INSTALL.md
*
* \section Implementation
*
* The command line entry to the manager is defined in
* src/\ref gvmd.c. The manager is a GMP server.
*
* The GMP server is defined in src/\ref gmpd.c. The GMP library is defined
* in src/\ref gmp.c. The GMP library use the Manage library
* to manage credentials and tasks. The manage
* library is defined in src/\ref manage.c and src/\ref manage_sql.c .
*
* \subsection Forking
*
* The main daemon manager process will fork for every incoming connection and
* for every scheduled task.
*
* \section copying License Information
* \verbinclude COPYING
*/
/**
* \page manpage gvmd
* \htmlinclude doc/gvmd.html
*/
#include <locale.h>
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gnutls/gnutls.h>
#include <grp.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <execinfo.h>
#include <gvm/base/pidfile.h>
#include <gvm/base/pwpolicy.h>
#include <gvm/base/logging.h>
#include <bsd/unistd.h>
#include <gvm/base/gvm_sentry.h>
#include <gvm/util/fileutils.h>
#include <gvm/util/serverutils.h>
#include <gvm/util/ldaputils.h>
#include "debug_utils.h"
#include "ipc.h"
#include "manage.h"
#include "manage_sql_nvts.h"
#include "manage_sql_secinfo.h"
#include "manage_authentication.h"
#include "gmpd.h"
#include "utils.h"
#ifdef GIT_REV_AVAILABLE
#include "gitrevision.h"
#endif
#undef G_LOG_DOMAIN
/**
* @brief GLib log domain.
*/
#define G_LOG_DOMAIN "md main"
/**
* @brief The version number of this program.
*/
#ifndef GVMD_VERSION
#define GVMD_VERSION "-1"
#endif
/**
* @brief The name of the underlying Operating System.
*/
#ifndef GVM_OS_NAME
#define GVM_OS_NAME "-1"
#endif
/**
* @brief Location of scanner certificate.
*/
#ifndef SCANNERCERT
#define SCANNERCERT "/var/lib/openvas/CA/servercert.pem"
#endif
/**
* @brief Location of scanner certificate private key.
*/
#ifndef SCANNERKEY
#define SCANNERKEY "/var/lib/openvas/private/CA/serverkey.pem"
#endif
/**
* @brief Location of Certificate Authority certificate.
*/
#ifndef CACERT
#define CACERT "/var/lib/openvas/CA/cacert.pem"
#endif
/**
* @brief Location of client certificate.
*/
#ifndef CLIENTCERT
#define CLIENTCERT "/var/lib/openvas/CA/clientcert.pem"
#endif
/**
* @brief Location of client certificate private key.
*/
#ifndef CLIENTKEY
#define CLIENTKEY "/var/lib/openvas/private/CA/clientkey.pem"
#endif
/**
* @brief Manager port.
*
* Used if /etc/services "otp" and --port are missing.
*/
#define GVMD_PORT 9390
/**
* @brief Second argument to `listen'.
*/
#define MAX_CONNECTIONS 512
/**
* @brief Default value for client_watch_interval
*/
#define DEFAULT_CLIENT_WATCH_INTERVAL 1
/**
* @brief Default broker address
*/
#define DEFAULT_BROKER_ADDRESS "localhost:1883"
/**
* @brief Maximum number of frames in backtrace.
*
* For debugging backtrace in \ref handle_sigabrt and \ref handle_sigsegv.
*/
#define BA_SIZE 100
/**
* @brief Interval in seconds to check whether client connection was closed.
*/
static int client_watch_interval = DEFAULT_CLIENT_WATCH_INTERVAL;
/**
* @brief The socket accepting GMP connections from clients.
*/
static int manager_socket = -1;
/**
* @brief The optional, second socket accepting GMP connections from clients.
*/
static int manager_socket_2 = -1;
#if LOG
/**
* @brief The log stream.
*/
FILE* log_stream = NULL;
#endif
/**
* @brief Whether to use TLS for client connections.
*/
static int use_tls = 0;
/**
* @brief The client session.
*/
static gnutls_session_t client_session;
/**
* @brief The client credentials.
*/
static gnutls_certificate_credentials_t client_credentials;
/**
* @brief Database connection info.
*/
static db_conn_info_t database = { NULL, NULL, NULL, NULL };
/**
* @brief Is this process parent or child?
*/
static int is_parent = 1;
/**
* @brief Flag for signal handlers.
*/
volatile int termination_signal = 0;
/**
* @brief The address of the Scanner.
*/
static gchar **disabled_commands = NULL;
/**
* @brief Flag indicating that encrypted credentials are disabled.
*
* Setting this flag does not change any existing encrypted tuples but
* simply won't encrypt or decrypt anything. The variable is
* controlled by the command line option --disable-encrypted-credentials.
*/
gboolean disable_encrypted_credentials;
/**
* @brief Flag indicating that task scheduling is enabled.
*/
static gboolean scheduling_enabled;
/**
* @brief The GMP client's address.
*/
char client_address[INET6_ADDRSTRLEN];
/**
* @brief Signal mask to restore when going from blocked to normal signaling.
*/
static sigset_t *sigmask_normal = NULL;
/**
* @brief GnuTLS priorities.
*/
static gchar *priorities_option = "NORMAL";
/**
* @brief GnuTLS DH params file.
*/
static gchar *dh_params_option = NULL;
/**
* @brief Whether an NVT update is in progress.
*/
static int update_in_progress = 0;
/**
* @brief Whether a feed version check is in progress.
*/
static int feed_version_check_in_progress = 0;
/**
* @brief Logging parameters, as passed to setup_log_handlers.
*/
GSList *log_config = NULL;
/* Helpers. */
/**
* @brief Sets the GnuTLS priorities for a given session.
*
* @param[in] session Session for which to set the priorities.
* @param[in] priority Priority string.
*/
static void
set_gnutls_priority (gnutls_session_t *session, const char *priority)
{
const char *errp = NULL;
if (gnutls_priority_set_direct (*session, priority, &errp)
== GNUTLS_E_INVALID_REQUEST)
g_warning ("Invalid GnuTLS priority: %s", errp);
}
/**
* @brief Lock gvm-helping for an option.
*
* @param[in] lockfile_checking The gvm-checking lockfile.
*
* @return 0 success, -1 failed.
*/
static int
option_lock (lockfile_t *lockfile_checking)
{
static lockfile_t lockfile_helping;
if (lockfile_lock_shared_nb (&lockfile_helping, "gvm-helping"))
{
g_critical ("%s: Error getting helping lock", __func__);
return -1;
}
if (lockfile_unlock (lockfile_checking))
{
g_critical ("%s: Error releasing checking lock", __func__);
return -1;
}
return 0;
}
/* Forking, serving the client. */
/**
* @brief Connection watcher thread data.
*/
typedef struct {
gvm_connection_t *client_connection; ///< Client connection.
int connection_closed; ///< Whether connection is closed.
pthread_mutex_t mutex; ///< Mutex.
} connection_watcher_data_t;
/**
* @brief Create a new connection watcher thread data structure.
*
* @param[in] client_connection GVM connection to client to watch.
*
* @return Newly allocated watcher thread data.
*/
static connection_watcher_data_t*
connection_watcher_data_new (gvm_connection_t *client_connection)
{
connection_watcher_data_t *watcher_data;
watcher_data = g_malloc (sizeof (connection_watcher_data_t));
watcher_data->client_connection = client_connection;
watcher_data->connection_closed = 0;
pthread_mutex_init (&(watcher_data->mutex), NULL);
return watcher_data;
}
/**
* @brief Thread start routine watching the client connection.
*
* @param[in] data The connection data watcher struct.
*
* @return Always NULL.
*/
static void*
watch_client_connection (void* data)
{
int active;
connection_watcher_data_t *watcher_data;
gvm_connection_t *client_connection;
pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
watcher_data = (connection_watcher_data_t*) data;
client_connection = watcher_data->client_connection;
pthread_mutex_lock (&(watcher_data->mutex));
active = 1;
pthread_mutex_unlock (&(watcher_data->mutex));
while (active)
{
pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
sleep (client_watch_interval);
pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
pthread_mutex_lock (&(watcher_data->mutex));
if (watcher_data->connection_closed)
{
active = 0;
pthread_mutex_unlock (&(watcher_data->mutex));
continue;
}
int ret;
char buf[1];
errno = 0;
ret = recv (client_connection->socket, buf, 1, MSG_PEEK);
if (ret >= 0)
{
if (watcher_data->connection_closed == 0)
{
g_debug ("%s: Client connection closed", __func__);
sql_cancel ();
active = 0;
watcher_data->connection_closed = 1;
}
}
pthread_mutex_unlock (&(watcher_data->mutex));
}
return NULL;
}
/**
* @brief Serve the client.
*
* In all cases, close client_socket before returning.
*
* @param[in] server_socket The socket connected to the Manager.
* @param[in] client_connection The connection to the client.
*
* @return EXIT_SUCCESS on success, EXIT_FAILURE on failure.
*/
static int
serve_client (int server_socket, gvm_connection_t *client_connection)
{
pthread_t watch_thread;
connection_watcher_data_t *watcher_data;
if (server_socket > 0)
{
int optval;
optval = 1;
if (setsockopt (server_socket,
SOL_SOCKET, SO_KEEPALIVE,
&optval, sizeof (int)))
{
g_critical ("%s: failed to set SO_KEEPALIVE on scanner socket: %s",
__func__,
strerror (errno));
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
}
if (client_watch_interval)
{
watcher_data = connection_watcher_data_new (client_connection);
pthread_create (&watch_thread, NULL, watch_client_connection,
watcher_data);
}
else
{
watcher_data = NULL;
}
if (client_connection->tls
&& gvm_server_attach (client_connection->socket, &client_session))
{
g_debug ("%s: failed to attach client session to socket %i",
__func__,
client_connection->socket);
goto fail;
}
/* The socket must have O_NONBLOCK set, in case an "asynchronous network
* error" removes the data between `select' and `read'. */
if (fcntl (client_connection->socket, F_SETFL, O_NONBLOCK) == -1)
{
g_warning ("%s: failed to set real client socket flag: %s",
__func__,
strerror (errno));
goto fail;
}
/* Serve GMP. */
/* It's up to serve_gmp to gvm_server_free client_*. */
if (serve_gmp (client_connection, &database, disabled_commands))
goto server_fail;
if (watcher_data)
{
pthread_mutex_lock (&(watcher_data->mutex));
watcher_data->connection_closed = 1;
pthread_mutex_unlock (&(watcher_data->mutex));
pthread_cancel (watch_thread);
pthread_join (watch_thread, NULL);
g_free (watcher_data);
}
return EXIT_SUCCESS;
fail:
if (watcher_data)
{
pthread_mutex_lock (&(watcher_data->mutex));
gvm_connection_free (client_connection);
watcher_data->connection_closed = 1;
pthread_mutex_unlock (&(watcher_data->mutex));
}
else
{
gvm_connection_free (client_connection);
}
server_fail:
if (watcher_data)
{
pthread_mutex_lock (&(watcher_data->mutex));
watcher_data->connection_closed = 1;
pthread_mutex_unlock (&(watcher_data->mutex));
pthread_cancel (watch_thread);
pthread_join (watch_thread, NULL);
g_free (watcher_data);
}
return EXIT_FAILURE;
}
/**
* @brief Accept and fork.
*
* @param[in] server_socket Manager socket.
* @param[in] sigmask_current Sigmask to restore in child.
*
* Accept the client connection and fork a child process to serve the client.
* The child calls \ref serve_client to do the rest of the work.
*/
static void
accept_and_maybe_fork (int server_socket, sigset_t *sigmask_current)
{
/* Accept the client connection. */
pid_t pid;
int client_socket;
struct sockaddr_storage addr;
socklen_t addrlen = sizeof (addr);
while ((client_socket = accept (server_socket, (struct sockaddr *) &addr,
&addrlen))
== -1)
{
if (errno == EINTR)
continue;
if (errno == EAGAIN || errno == EWOULDBLOCK)
/* The connection is gone, return to select. */
return;
g_critical ("%s: failed to accept client connection: %s",
__func__,
strerror (errno));
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
sockaddr_as_str (&addr, client_address);
/* Fork a child to serve the client.
*
* Use the default handlers for termination signals in the child. This
* is required because the child calls 'system' and 'g_spawn_sync' in many
* places. As the child waits for the spawned command, the child will
* not return to any code that checks termination_signal, so the child
* can't use the signal handlers inherited from the main process. */
pid = fork_with_handlers ();
switch (pid)
{
case 0:
/* Child. */
{
int ret;
struct sigaction action;
gvm_connection_t client_connection;
init_sentry ();
is_parent = 0;
setproctitle ("Serving client");
/* Restore the sigmask that was blanked for pselect. */
pthread_sigmask (SIG_SETMASK, sigmask_current, NULL);
memset (&action, '\0', sizeof (action));
sigemptyset (&action.sa_mask);
action.sa_handler = SIG_DFL;
if (sigaction (SIGCHLD, &action, NULL) == -1)
{
g_critical ("%s: failed to set client SIGCHLD handler: %s",
__func__,
strerror (errno));
shutdown (client_socket, SHUT_RDWR);
close (client_socket);
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
/* The socket must have O_NONBLOCK set, in case an "asynchronous
* network error" removes the data between `select' and `read'.
*/
if (fcntl (client_socket, F_SETFL, O_NONBLOCK) == -1)
{
g_critical ("%s: failed to set client socket flag: %s",
__func__,
strerror (errno));
shutdown (client_socket, SHUT_RDWR);
close (client_socket);
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
/* Reopen the database (required after fork). */
cleanup_manage_process (FALSE);
memset (&client_connection, 0, sizeof (client_connection));
client_connection.tls = use_tls;
client_connection.socket = client_socket;
client_connection.session = client_session;
client_connection.credentials = client_credentials;
ret = serve_client (server_socket, &client_connection);
gvm_close_sentry ();
exit (ret);
}
case -1:
/* Parent when error, return to select. */
g_warning ("%s: failed to fork child: %s",
__func__,
strerror (errno));
close (client_socket);
break;
default:
/* Parent. Return to select. */
close (client_socket);
break;
}
}
/* Connection forker for scheduler. */
/**
* @brief Fork a child connected to the Manager.
*
* @param[in] client_connection Client connection.
* @param[in] uuid UUID of schedule user.
* @param[in] scheduler Whether this is for the scheduler.
*
* @return PID parent on success, 0 child on success, -1 error.
*/
static int
fork_connection_internal (gvm_connection_t *client_connection,
const gchar* uuid, int scheduler)
{
int pid, parent_client_socket, ret;
int sockets[2];
struct sigaction action;
gchar *auth_uuid;
/* Fork a child to use as scheduler/event client and server. */
/* This must 'fork' and not 'fork_with_handlers' so that the next fork can
* decide about handlers. */
pid = fork ();
switch (pid)
{
case 0:
/* Child. */
init_sentry ();
cleanup_manage_process (FALSE);
break;
case -1:
/* Parent when error. */
g_warning ("%s: fork: %s", __func__, strerror (errno));
return -1;
break;
default:
/* Parent. Return to caller. */
g_debug ("%s: %i forked %i", __func__, getpid (), pid);
return pid;
break;
}
/* This is now a child of the main Manager process. It forks again. The
* only case that returns is the process that the caller can use for GMP
* commands. The caller must exit this process.
*/
/* Restore the sigmask that was blanked for pselect. */
if (sigmask_normal)
pthread_sigmask (SIG_SETMASK, sigmask_normal, NULL);
/* Create a connected pair of sockets. */
if (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets))
{
g_warning ("%s: socketpair: %s", __func__, strerror (errno));
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
/* Split into a Manager client for the scheduler, and a Manager serving
* GMP to that client. */
is_parent = 0;
/* As with accept_and_maybe_fork, use the default handlers for termination
* signals in the child. This is required for signals to work when the
* child is waiting for spawns and forks. */
pid = fork_with_handlers ();
switch (pid)
{
case 0:
/* Child. Serve the scheduler GMP, then exit. */
init_sentry ();
setproctitle ("Serving GMP internally");
parent_client_socket = sockets[0];
memset (&action, '\0', sizeof (action));
sigemptyset (&action.sa_mask);
action.sa_handler = SIG_DFL;
if (sigaction (SIGCHLD, &action, NULL) == -1)
{
g_critical ("%s: failed to set client SIGCHLD handler: %s",
__func__,
strerror (errno));
shutdown (parent_client_socket, SHUT_RDWR);
close (parent_client_socket);
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
/* The socket must have O_NONBLOCK set, in case an "asynchronous
* network error" removes the data between `select' and `read'.
*/
if (fcntl (parent_client_socket, F_SETFL, O_NONBLOCK) == -1)
{
g_critical ("%s: failed to set client socket flag: %s",
__func__,
strerror (errno));
shutdown (parent_client_socket, SHUT_RDWR);
close (parent_client_socket);
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
/* Copy the given uuid, because the caller may have passed a
* reference to some session variable that will be reset by
* the process initialisation. */
auth_uuid = g_strdup (uuid);
init_gmpd_process (&database, disabled_commands);
/* Make any further authentications to this process succeed. This
* enables the scheduler to login as the owner of the scheduled
* task. */
manage_auth_allow_all (scheduler);
set_scheduled_user_uuid (auth_uuid);
g_free (auth_uuid);
/* For TLS, create a new session, because the parent may have been in
* the middle of using the old one. */
if (use_tls)
{
if (gvm_server_new (GNUTLS_SERVER,
CACERT,
SCANNERCERT,
SCANNERKEY,
&client_session,
&client_credentials))
{
g_critical ("%s: client server initialisation failed",
__func__);
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
set_gnutls_priority (&client_session, priorities_option);
if (dh_params_option
&& set_gnutls_dhparams (client_credentials, dh_params_option))
g_warning ("Couldn't set DH parameters from %s", dh_params_option);
}
/* Serve client. */
g_debug ("%s: serving GMP to client on socket %i",
__func__, parent_client_socket);
memset (client_connection, 0, sizeof (*client_connection));
client_connection->tls = use_tls;
client_connection->socket = parent_client_socket;
client_connection->session = client_session;
client_connection->credentials = client_credentials;
ret = serve_client (manager_socket, client_connection);
gvm_close_sentry ();
exit (ret);
break;
case -1:
/* Parent when error. */
g_warning ("%s: fork: %s", __func__, strerror (errno));
gvm_close_sentry ();
exit (EXIT_FAILURE);
break;
default:
/* Parent. */
g_debug ("%s: %i forked %i", __func__, getpid (), pid);
setproctitle ("Requesting GMP internally");
/* This process is returned as the child of
* fork_connection_for_scheduler so that the returned parent can wait
* on this process. */
if (scheduler)
{
/* When used for scheduling this parent process waits for the
* child. That means it does not use the loops which handle
* termination_signal. So we need to use the regular handlers
* for termination signals. */
setup_signal_handler (SIGTERM, SIG_DFL, 0);
setup_signal_handler (SIGINT, SIG_DFL, 0);
setup_signal_handler (SIGQUIT, SIG_DFL, 0);
}
/** @todo Give the parent time to prepare. */
gvm_sleep (5);
memset (client_connection, 0, sizeof (*client_connection));
client_connection->tls = use_tls;
client_connection->socket = sockets[1];
if (use_tls)
{
if (gvm_server_new (GNUTLS_CLIENT,
CACERT,
CLIENTCERT,
CLIENTKEY,
&client_connection->session,
&client_connection->credentials))
{
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
if (gvm_server_attach (client_connection->socket,
&client_connection->session))
{
gvm_close_sentry ();
exit (EXIT_FAILURE);
}
}
g_debug ("%s: all set to request GMP on socket %i",
__func__, client_connection->socket);
return 0;
break;
}
gvm_close_sentry ();
exit (EXIT_FAILURE);
return -1;
}
/**
* @brief Fork a child connected to the Manager.
*
* @param[in] client_connection Client connection.
* @param[in] uuid UUID of schedule user.
*
* @return PID parent on success, 0 child on success, -1 error.
*/
static int
fork_connection_for_scheduler (gvm_connection_t *client_connection,
const gchar* uuid)
{
return fork_connection_internal (client_connection, uuid, 1);
}
/**
* @brief Fork a child connected to the Manager.
*
* @param[in] client_connection Client connection.
* @param[in] uuid UUID of user.
*
* @return PID parent on success, 0 child on success, -1 error.
*/
static int
fork_connection_for_event (gvm_connection_t *client_connection,
const gchar* uuid)
{
return fork_connection_internal (client_connection, uuid, 0);
}
/* Maintenance functions. */
/**
* @brief Free logging configuration.
*/
static void
log_config_free ()
{
free_log_configuration (log_config);
log_config = NULL;
}
/**
* @brief Clean up for exit.
*
* Close sockets and streams.
*/
static void
cleanup ()
{
g_debug (" Cleaning up");
/** @todo These should happen via gmp, maybe with "cleanup_gmp ();". */
cleanup_manage_process (TRUE);
g_strfreev (disabled_commands);
if (manager_socket > -1)
{
close (manager_socket);
manager_socket = -1;
}
if (manager_socket_2 > -1)
{
close (manager_socket_2);
manager_socket_2 = -1;
}
#if LOG
if (log_stream != NULL)
{
if (fclose (log_stream))
g_critical ("%s: failed to close log stream: %s",
__func__,
strerror (errno));
}
#endif /* LOG */
g_debug (" Exiting");
if (log_config) log_config_free ();
/* Delete pidfile if this process is the parent. */
if (is_parent == 1) pidfile_remove (GVMD_PID_PATH);
}
/**
* @brief Handle a SIGABRT signal.
*
* @param[in] given_signal The signal that caused this function to run.
*/
static void
handle_sigabrt (int given_signal)
{
static int in_sigabrt = 0;
if (in_sigabrt) _exit (EXIT_FAILURE);
in_sigabrt = 1;
void *frames[BA_SIZE];
int frame_count, index;
char **frames_text;
/* Print a backtrace. */
frame_count = backtrace (frames, BA_SIZE);
frames_text = backtrace_symbols (frames, frame_count);
if (frames_text == NULL)
{
perror ("backtrace symbols");
frame_count = 0;