Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_selinux.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2024 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that checks if SELinux is enabled.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "common.h"
28 #include "messages.h"
29 #include "npl_selinux.h"
30 #include "progname.h"
31 #include "progversion.h"
32 #include "system.h"
33 #include "xasprintf.h"
34
35 extern char *selinux_mnt;
36
37 static const char *program_copyright =
38 "Copyright (C) 2024 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
39
40 static struct option const longopts[] = {
41 {(char *) "permissive-is-allowed", no_argument, NULL, 'p'},
42 {(char *) "permissive-is-critical", no_argument, NULL, 'P'},
43 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
44 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
45 {NULL, 0, NULL, 0}
46 };
47
48 static _Noreturn void
49 usage (FILE * out)
50 {
51 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
52 fputs ("This plugin checks if SELinux is enabled.\n", out);
53 fputs (program_copyright, out);
54 fputs (USAGE_HEADER, out);
55 fprintf (out, " %s [--permissive-is-allowed|--permissive-is-critical]\n",
56 program_name);
57 fputs (USAGE_OPTIONS, out);
58 fputs (" -p --permissive-is-allowed permissive mode does not generate "
59 "a warning\n", out);
60 fputs (" -P --permissive-is-critical permissive mode is to be considered"
61 " critical\n", out);
62 fputs (USAGE_HELP, out);
63 fputs (USAGE_VERSION, out);
64 fputs (USAGE_NOTE, out);
65 fputs (" By default, permissive mode raises a warning.\n", out);
66 fputs (" Use the option -P to turn it into a critical error\n", out);
67 fputs (" or -p to consider it a valid configuration.\n", out);
68 fputs (USAGE_EXAMPLES, out);
69 fprintf (out, " %s\n", program_name);
70 fprintf (out, " %s --permissive-is-allowed\n", program_name);
71 fprintf (out, " %s --permissive-is-critical\n", program_name);
72
73 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
74 }
75
76 static _Noreturn void
77 print_version (void)
78 {
79 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
80 fputs (program_copyright, stdout);
81 fputs (GPLv3_DISCLAIMER, stdout);
82
83 exit (STATE_OK);
84 }
85
86 int
87 main (int argc, char **argv)
88 {
89 int c, is_enabled;
90 nagstatus status = STATE_OK,
91 permissive_status = STATE_WARNING;
92
93 set_program_name (argv[0]);
94
95 while ((c = getopt_long (argc, argv,
96 "p" GETOPT_HELP_VERSION_STRING,
97 longopts, NULL)) != -1)
98 {
99 switch (c)
100 {
101 default:
102 usage (stderr);
103 case 'P':
104 permissive_status = STATE_CRITICAL;
105 break;
106 case 'p':
107 permissive_status = STATE_OK;
108 break;
109
110 case_GETOPT_HELP_CHAR
111 case_GETOPT_VERSION_CHAR
112
113 }
114 }
115
116 is_enabled = is_selinux_enabled ();
117 char *status_str;
118 switch (is_enabled)
119 {
120 default:
121 status = STATE_CRITICAL;
122 status_str = "disabled";
123 break;
124 case 1:
125 status = permissive_status;
126 status_str = "disabled (permissive)";
127 break;
128 case 2:
129 status = STATE_OK;
130 status_str = "enabled (enforced)";
131 break;
132 }
133
134 printf ("%s %s - selinux %s%s | selinux_enabled=%d\n"
135 , program_name_short
136 , state_text (status)
137 , status_str
138 , selinux_mnt ? xasprintf (" (%s)", selinux_mnt) : ""
139 , status == STATE_OK ? 1 : 0);
140
141 return status;
142 }