1 /*****************************************************************************
3 * Nagios check_ldap plugin
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_ldap plugin
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 /* progname may be check_ldaps */
33 char *progname
= "check_ldap";
34 const char *revision
= "$Revision$";
35 const char *copyright
= "2000-2008";
36 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
47 #ifdef HAVE_LDAP_SET_OPTION
53 int process_arguments (int, char **);
54 int validate_arguments (void);
55 void print_help (void);
56 void print_usage (void);
58 char ld_defattr
[] = "(objectclass=*)";
59 char *ld_attr
= ld_defattr
;
62 char *ld_passwd
= NULL
;
63 char *ld_binddn
= NULL
;
64 int ld_port
= DEFAULT_PORT
;
65 #ifdef HAVE_LDAP_SET_OPTION
66 int ld_protocol
= DEFAULT_PROTOCOL
;
68 #ifndef LDAP_OPT_SUCCESS
69 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
71 double warn_time
= UNDEFINED
;
72 double crit_time
= UNDEFINED
;
75 int 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 setlocale (LC_ALL
, "");
101 bindtextdomain (PACKAGE
, LOCALEDIR
);
102 textdomain (PACKAGE
);
104 if (strstr(argv
[0],"check_ldaps")) {
105 asprintf (&progname
, "check_ldaps");
108 /* Parse extra opts if any */
109 argv
=np_extra_opts (&argc
, argv
, progname
);
111 if (process_arguments (argc
, argv
) == ERROR
)
112 usage4 (_("Could not parse arguments"));
114 if (strstr(argv
[0],"check_ldaps") && ! starttls
&& ! ssl_on_connect
)
117 /* initialize alarm signal handling */
118 signal (SIGALRM
, socket_timeout_alarm_handler
);
120 /* set socket timeout */
121 alarm (socket_timeout
);
123 /* get the start time */
124 gettimeofday (&tv
, NULL
);
126 /* initialize ldap */
127 #ifdef HAVE_LDAP_INIT
128 if (!(ld
= ldap_init (ld_host
, ld_port
))) {
129 printf ("Could not connect to the server at port %i\n", ld_port
);
130 return STATE_CRITICAL
;
133 if (!(ld
= ldap_open (ld_host
, ld_port
))) {
135 ldap_perror(ld
, "ldap_open");
136 printf (_("Could not connect to the server at port %i\n"), ld_port
);
137 return STATE_CRITICAL
;
139 #endif /* HAVE_LDAP_INIT */
141 #ifdef HAVE_LDAP_SET_OPTION
142 /* set ldap options */
143 if (ldap_set_option (ld
, LDAP_OPT_PROTOCOL_VERSION
, &ld_protocol
) !=
145 printf(_("Could not set protocol version %d\n"), ld_protocol
);
146 return STATE_CRITICAL
;
150 if (ld_port
== LDAPS_PORT
|| ssl_on_connect
) {
151 asprintf (&SERVICE
, "LDAPS");
152 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
153 /* ldaps: set option tls */
154 tls
= LDAP_OPT_X_TLS_HARD
;
156 if (ldap_set_option (ld
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
159 ldap_perror(ld
, "ldaps_option");
160 printf (_("Could not init TLS at port %i!\n"), ld_port
);
161 return STATE_CRITICAL
;
164 printf (_("TLS not supported by the libraries!\n"));
165 return STATE_CRITICAL
;
166 #endif /* LDAP_OPT_X_TLS */
167 } else if (starttls
) {
168 asprintf (&SERVICE
, "LDAP-TLS");
169 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
170 /* ldap with startTLS: set option version */
171 if (ldap_get_option(ld
,LDAP_OPT_PROTOCOL_VERSION
, &version
) == LDAP_OPT_SUCCESS
)
173 if (version
< LDAP_VERSION3
)
175 version
= LDAP_VERSION3
;
176 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
180 if (ldap_start_tls_s(ld
, NULL
, NULL
) != LDAP_SUCCESS
)
183 ldap_perror(ld
, "ldap_start_tls");
184 printf (_("Could not init startTLS at port %i!\n"), ld_port
);
185 return STATE_CRITICAL
;
188 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
189 return STATE_CRITICAL
;
190 #endif /* HAVE_LDAP_START_TLS_S */
193 /* bind to the ldap server */
194 if (ldap_bind_s (ld
, ld_binddn
, ld_passwd
, LDAP_AUTH_SIMPLE
) !=
197 ldap_perror(ld
, "ldap_bind");
198 printf (_("Could not bind to the ldap-server\n"));
199 return STATE_CRITICAL
;
202 /* do a search of all objectclasses in the base dn */
203 if (ldap_search_s (ld
, ld_base
, LDAP_SCOPE_BASE
, ld_attr
, NULL
, 0, &result
)
206 ldap_perror(ld
, "ldap_search");
207 printf (_("Could not search/find objectclasses in %s\n"), ld_base
);
208 return STATE_CRITICAL
;
211 /* unbind from the ldap server */
214 /* reset the alarm handler */
217 /* calcutate the elapsed time and compare to thresholds */
219 microsec
= deltime (tv
);
220 elapsed_time
= (double)microsec
/ 1.0e6
;
222 if (crit_time
!=UNDEFINED
&& elapsed_time
>crit_time
)
223 status
= STATE_CRITICAL
;
224 else if (warn_time
!=UNDEFINED
&& elapsed_time
>warn_time
)
225 status
= STATE_WARNING
;
229 /* print out the result */
230 printf (_("LDAP %s - %.3f seconds response time|%s\n"),
233 fperfdata ("time", elapsed_time
, "s",
234 (int)warn_time
, warn_time
,
235 (int)crit_time
, crit_time
,
241 /* process command-line arguments */
243 process_arguments (int argc
, char **argv
)
248 /* initialize the long option struct */
249 static struct option longopts
[] = {
250 {"help", no_argument
, 0, 'h'},
251 {"version", no_argument
, 0, 'V'},
252 {"timeout", required_argument
, 0, 't'},
253 {"host", required_argument
, 0, 'H'},
254 {"base", required_argument
, 0, 'b'},
255 {"attr", required_argument
, 0, 'a'},
256 {"bind", required_argument
, 0, 'D'},
257 {"pass", required_argument
, 0, 'P'},
258 #ifdef HAVE_LDAP_SET_OPTION
259 {"ver2", no_argument
, 0, '2'},
260 {"ver3", no_argument
, 0, '3'},
262 {"starttls", no_argument
, 0, 'T'},
263 {"ssl", no_argument
, 0, 'S'},
264 {"use-ipv4", no_argument
, 0, '4'},
265 {"use-ipv6", no_argument
, 0, '6'},
266 {"port", required_argument
, 0, 'p'},
267 {"warn", required_argument
, 0, 'w'},
268 {"crit", required_argument
, 0, 'c'},
269 {"verbose", no_argument
, 0, 'v'},
276 for (c
= 1; c
< argc
; c
++) {
277 if (strcmp ("-to", argv
[c
]) == 0)
278 strcpy (argv
[c
], "-t");
282 c
= getopt_long (argc
, argv
, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts
, &option
);
284 if (c
== -1 || c
== EOF
)
291 case 'V': /* version */
292 print_revision (progname
, revision
);
294 case 't': /* timeout period */
295 if (!is_intnonneg (optarg
))
296 usage2 (_("Timeout interval must be a positive integer"), optarg
);
298 socket_timeout
= atoi (optarg
);
307 ld_port
= atoi (optarg
);
319 warn_time
= strtod (optarg
, NULL
);
322 crit_time
= strtod (optarg
, NULL
);
324 #ifdef HAVE_LDAP_SET_OPTION
333 address_family
= AF_INET
;
339 if (! ssl_on_connect
)
342 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
346 ssl_on_connect
= TRUE
;
347 ld_port
= LDAPS_PORT
;
349 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
353 address_family
= AF_INET6
;
355 usage (_("IPv6 support not available\n"));
364 if (ld_host
== NULL
&& is_host(argv
[c
]))
365 ld_host
= strdup (argv
[c
++]);
367 if (ld_base
== NULL
&& argv
[c
])
368 ld_base
= strdup (argv
[c
++]);
370 return validate_arguments ();
375 validate_arguments ()
377 if (ld_host
==NULL
|| strlen(ld_host
)==0)
378 usage4 (_("Please specify the host name\n"));
380 if (ld_base
==NULL
|| strlen(ld_base
)==0)
381 usage4 (_("Please specify the LDAP base\n"));
391 asprintf (&myport
, "%d", DEFAULT_PORT
);
393 print_revision (progname
, revision
);
395 printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
396 printf (COPYRIGHT
, copyright
, email
);
402 printf (_(UT_HELP_VRSN
));
403 printf (_(UT_EXTRA_OPTS
));
405 printf (_(UT_HOST_PORT
), 'p', myport
);
407 printf (_(UT_IPv46
));
409 printf (" %s\n", "-a [--attr]");
410 printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
411 printf (" %s\n", "-b [--base]");
412 printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
413 printf (" %s\n", "-D [--bind]");
414 printf (" %s\n", _("ldap bind DN (if required)"));
415 printf (" %s\n", "-P [--pass]");
416 printf (" %s\n", _("ldap password (if required)"));
417 printf (" %s\n", "-T [--starttls]");
418 printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
419 printf (" %s\n", "-S [--ssl]");
420 printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT
);
422 #ifdef HAVE_LDAP_SET_OPTION
423 printf (" %s\n", "-2 [--ver2]");
424 printf (" %s\n", _("use ldap protocol version 2"));
425 printf (" %s\n", "-3 [--ver3]");
426 printf (" %s\n", _("use ldap protocol version 3"));
427 printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL
);
430 printf (_(UT_WARN_CRIT
));
432 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
434 printf (_(UT_VERBOSE
));
437 printf ("%s\n", _("Notes:"));
438 printf (" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
439 printf (_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT
);
440 printf (" %s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
441 printf (" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
442 printf (" %s\n", _("to define the behaviour explicitly instead."));
445 printf (_(UT_EXTRA_OPTS_NOTES
));
448 printf (_(UT_SUPPORT
));
452 * add option -4 and -6 to the long manual
459 printf (_("Usage:"));
460 printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname
);
461 printf ("\n [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
462 #ifdef HAVE_LDAP_SET_OPTION