1 /*****************************************************************************
3 * Monitoring check_ldap plugin
6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
10 * This file contains the check_ldap plugin
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *****************************************************************************/
29 /* progname may be check_ldaps */
30 char *progname
= "check_ldap";
31 const char *copyright
= "2000-2024";
32 const char *email
= "devel@monitoring-plugins.org";
39 #define LDAP_DEPRECATED 1
44 #ifdef HAVE_LDAP_SET_OPTION
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
55 char ld_defattr
[] = "(objectclass=*)";
56 char *ld_attr
= ld_defattr
;
59 char *ld_passwd
= NULL
;
60 char *ld_binddn
= NULL
;
62 #ifdef HAVE_LDAP_SET_OPTION
63 int ld_protocol
= DEFAULT_PROTOCOL
;
65 #ifndef LDAP_OPT_SUCCESS
66 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
68 double warn_time
= UNDEFINED
;
69 double crit_time
= UNDEFINED
;
70 thresholds
*entries_thresholds
= NULL
;
72 char* warn_entries
= NULL
;
73 char* crit_entries
= NULL
;
74 bool starttls
= false;
75 bool ssl_on_connect
= false;
80 char *SERVICE
= "LDAP";
83 main (int argc
, char *argv
[])
89 /* should be int result = STATE_UNKNOWN; */
91 int status
= STATE_UNKNOWN
;
100 int status_entries
= STATE_OK
;
103 setlocale (LC_ALL
, "");
104 bindtextdomain (PACKAGE
, LOCALEDIR
);
105 textdomain (PACKAGE
);
107 if (strstr(argv
[0],"check_ldaps")) {
108 xasprintf (&progname
, "check_ldaps");
111 /* Parse extra opts if any */
112 argv
=np_extra_opts (&argc
, argv
, progname
);
114 if (process_arguments (argc
, argv
) == ERROR
)
115 usage4 (_("Could not parse arguments"));
117 if (strstr(argv
[0],"check_ldaps") && ! starttls
&& ! ssl_on_connect
)
120 /* initialize alarm signal handling */
121 signal (SIGALRM
, socket_timeout_alarm_handler
);
123 /* set socket timeout */
124 alarm (socket_timeout
);
126 /* get the start time */
127 gettimeofday (&tv
, NULL
);
129 /* initialize ldap */
130 #ifdef HAVE_LDAP_INIT
131 if (!(ld
= ldap_init (ld_host
, ld_port
))) {
132 printf ("Could not connect to the server at port %i\n", ld_port
);
133 return STATE_CRITICAL
;
136 if (!(ld
= ldap_open (ld_host
, ld_port
))) {
138 ldap_perror(ld
, "ldap_open");
139 printf (_("Could not connect to the server at port %i\n"), ld_port
);
140 return STATE_CRITICAL
;
142 #endif /* HAVE_LDAP_INIT */
144 #ifdef HAVE_LDAP_SET_OPTION
145 /* set ldap options */
146 if (ldap_set_option (ld
, LDAP_OPT_PROTOCOL_VERSION
, &ld_protocol
) !=
148 printf(_("Could not set protocol version %d\n"), ld_protocol
);
149 return STATE_CRITICAL
;
153 if (ld_port
== LDAPS_PORT
|| ssl_on_connect
) {
154 xasprintf (&SERVICE
, "LDAPS");
155 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
156 /* ldaps: set option tls */
157 tls
= LDAP_OPT_X_TLS_HARD
;
159 if (ldap_set_option (ld
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
162 ldap_perror(ld
, "ldaps_option");
163 printf (_("Could not init TLS at port %i!\n"), ld_port
);
164 return STATE_CRITICAL
;
167 printf (_("TLS not supported by the libraries!\n"));
168 return STATE_CRITICAL
;
169 #endif /* LDAP_OPT_X_TLS */
170 } else if (starttls
) {
171 xasprintf (&SERVICE
, "LDAP-TLS");
172 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
173 /* ldap with startTLS: set option version */
174 if (ldap_get_option(ld
,LDAP_OPT_PROTOCOL_VERSION
, &version
) == LDAP_OPT_SUCCESS
)
176 if (version
< LDAP_VERSION3
)
178 version
= LDAP_VERSION3
;
179 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
183 if (ldap_start_tls_s(ld
, NULL
, NULL
) != LDAP_SUCCESS
)
186 ldap_perror(ld
, "ldap_start_tls");
187 printf (_("Could not init startTLS at port %i!\n"), ld_port
);
188 return STATE_CRITICAL
;
191 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
192 return STATE_CRITICAL
;
193 #endif /* HAVE_LDAP_START_TLS_S */
196 /* bind to the ldap server */
197 if (ldap_bind_s (ld
, ld_binddn
, ld_passwd
, LDAP_AUTH_SIMPLE
) !=
200 ldap_perror(ld
, "ldap_bind");
201 printf (_("Could not bind to the LDAP server\n"));
202 return STATE_CRITICAL
;
205 /* do a search of all objectclasses in the base dn */
206 if (ldap_search_s (ld
, ld_base
, (crit_entries
!=NULL
|| warn_entries
!=NULL
) ? LDAP_SCOPE_SUBTREE
: LDAP_SCOPE_BASE
, ld_attr
, NULL
, 0, &result
)
209 ldap_perror(ld
, "ldap_search");
210 printf (_("Could not search/find objectclasses in %s\n"), ld_base
);
211 return STATE_CRITICAL
;
212 } else if (crit_entries
!=NULL
|| warn_entries
!=NULL
) {
213 num_entries
= ldap_count_entries(ld
, result
);
216 /* unbind from the ldap server */
219 /* reset the alarm handler */
222 /* calculate the elapsed time and compare to thresholds */
224 microsec
= deltime (tv
);
225 elapsed_time
= (double)microsec
/ 1.0e6
;
227 if (crit_time
!=UNDEFINED
&& elapsed_time
>crit_time
)
228 status
= STATE_CRITICAL
;
229 else if (warn_time
!=UNDEFINED
&& elapsed_time
>warn_time
)
230 status
= STATE_WARNING
;
234 if(entries_thresholds
!= NULL
) {
236 printf ("entries found: %d\n", num_entries
);
237 print_thresholds("entry thresholds", entries_thresholds
);
239 status_entries
= get_status(num_entries
, entries_thresholds
);
240 if (status_entries
== STATE_CRITICAL
) {
241 status
= STATE_CRITICAL
;
242 } else if (status
!= STATE_CRITICAL
) {
243 status
= status_entries
;
247 /* print out the result */
248 if (crit_entries
!=NULL
|| warn_entries
!=NULL
) {
249 printf (_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"),
253 fperfdata ("time", elapsed_time
, "s",
254 (int)warn_time
, warn_time
,
255 (int)crit_time
, crit_time
,
257 sperfdata ("entries", (double)num_entries
, "",
260 true, 0.0, false, 0.0));
262 printf (_("LDAP %s - %.3f seconds response time|%s\n"),
265 fperfdata ("time", elapsed_time
, "s",
266 (int)warn_time
, warn_time
,
267 (int)crit_time
, crit_time
,
274 /* process command-line arguments */
276 process_arguments (int argc
, char **argv
)
281 /* initialize the long option struct */
282 static struct option longopts
[] = {
283 {"help", no_argument
, 0, 'h'},
284 {"version", no_argument
, 0, 'V'},
285 {"timeout", required_argument
, 0, 't'},
286 {"hostname", required_argument
, 0, 'H'},
287 {"base", required_argument
, 0, 'b'},
288 {"attr", required_argument
, 0, 'a'},
289 {"bind", required_argument
, 0, 'D'},
290 {"pass", required_argument
, 0, 'P'},
291 #ifdef HAVE_LDAP_SET_OPTION
292 {"ver2", no_argument
, 0, '2'},
293 {"ver3", no_argument
, 0, '3'},
295 {"starttls", no_argument
, 0, 'T'},
296 {"ssl", no_argument
, 0, 'S'},
297 {"use-ipv4", no_argument
, 0, '4'},
298 {"use-ipv6", no_argument
, 0, '6'},
299 {"port", required_argument
, 0, 'p'},
300 {"warn", required_argument
, 0, 'w'},
301 {"crit", required_argument
, 0, 'c'},
302 {"warn-entries", required_argument
, 0, 'W'},
303 {"crit-entries", required_argument
, 0, 'C'},
304 {"verbose", no_argument
, 0, 'v'},
311 for (c
= 1; c
< argc
; c
++) {
312 if (strcmp ("-to", argv
[c
]) == 0)
313 strcpy (argv
[c
], "-t");
317 c
= getopt_long (argc
, argv
, "hvV234TS6t:c:w:H:b:p:a:D:P:C:W:", longopts
, &option
);
319 if (c
== -1 || c
== EOF
)
325 exit (STATE_UNKNOWN
);
326 case 'V': /* version */
327 print_revision (progname
, NP_VERSION
);
328 exit (STATE_UNKNOWN
);
329 case 't': /* timeout period */
330 if (!is_intnonneg (optarg
))
331 usage2 (_("Timeout interval must be a positive integer"), optarg
);
333 socket_timeout
= atoi (optarg
);
342 ld_port
= atoi (optarg
);
354 warn_time
= strtod (optarg
, NULL
);
357 crit_time
= strtod (optarg
, NULL
);
360 warn_entries
= optarg
;
363 crit_entries
= optarg
;
365 #ifdef HAVE_LDAP_SET_OPTION
374 address_family
= AF_INET
;
380 if (! ssl_on_connect
)
383 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
387 ssl_on_connect
= true;
389 ld_port
= LDAPS_PORT
;
391 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
395 address_family
= AF_INET6
;
397 usage (_("IPv6 support not available\n"));
406 if (ld_host
== NULL
&& is_host(argv
[c
]))
407 ld_host
= strdup (argv
[c
++]);
409 if (ld_base
== NULL
&& argv
[c
])
410 ld_base
= strdup (argv
[c
++]);
413 ld_port
= DEFAULT_PORT
;
415 return validate_arguments ();
420 validate_arguments ()
422 if (ld_host
==NULL
|| strlen(ld_host
)==0)
423 usage4 (_("Please specify the host name\n"));
426 usage4 (_("Please specify the LDAP base\n"));
428 if (crit_entries
!=NULL
|| warn_entries
!=NULL
) {
429 set_thresholds(&entries_thresholds
,
430 warn_entries
, crit_entries
);
433 ld_passwd
= getenv("LDAP_PASSWORD");
443 xasprintf (&myport
, "%d", DEFAULT_PORT
);
445 print_revision (progname
, NP_VERSION
);
447 printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
448 printf (COPYRIGHT
, copyright
, email
);
454 printf (UT_HELP_VRSN
);
455 printf (UT_EXTRA_OPTS
);
457 printf (UT_HOST_PORT
, 'p', myport
);
461 printf (" %s\n", "-a [--attr]");
462 printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
463 printf (" %s\n", "-b [--base]");
464 printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
465 printf (" %s\n", "-D [--bind]");
466 printf (" %s\n", _("ldap bind DN (if required)"));
467 printf (" %s\n", "-P [--pass]");
468 printf (" %s\n", _("ldap password (if required, or set the password through environment variable 'LDAP_PASSWORD')"));
469 printf (" %s\n", "-T [--starttls]");
470 printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
471 printf (" %s\n", "-S [--ssl]");
472 printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT
);
474 #ifdef HAVE_LDAP_SET_OPTION
475 printf (" %s\n", "-2 [--ver2]");
476 printf (" %s\n", _("use ldap protocol version 2"));
477 printf (" %s\n", "-3 [--ver3]");
478 printf (" %s\n", _("use ldap protocol version 3"));
479 printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL
);
482 printf (UT_WARN_CRIT
);
484 printf (" %s\n", "-W [--warn-entries]");
485 printf (" %s\n", _("Number of found entries to result in warning status"));
486 printf (" %s\n", "-C [--crit-entries]");
487 printf (" %s\n", _("Number of found entries to result in critical status"));
489 printf (UT_CONN_TIMEOUT
, DEFAULT_SOCKET_TIMEOUT
);
494 printf ("%s\n", _("Notes:"));
495 printf (" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
496 printf (_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT
);
497 printf (" %s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
498 printf (" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
499 printf (" %s\n", _("to define the behaviour explicitly instead."));
500 printf (" %s\n", _("The parameters --warn-entries and --crit-entries are optional."));
508 printf ("%s\n", _("Usage:"));
509 printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname
);
510 printf ("\n [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
511 #ifdef HAVE_LDAP_SET_OPTION