1 /*****************************************************************************
3 * Nagios check_ldap plugin
6 * Copyright (c) 2000-2008 Nagios 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-2008";
32 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
43 #ifdef HAVE_LDAP_SET_OPTION
49 int process_arguments (int, char **);
50 int validate_arguments (void);
51 void print_help (void);
52 void print_usage (void);
54 char ld_defattr
[] = "(objectclass=*)";
55 char *ld_attr
= ld_defattr
;
58 char *ld_passwd
= NULL
;
59 char *ld_binddn
= NULL
;
60 int ld_port
= DEFAULT_PORT
;
61 #ifdef HAVE_LDAP_SET_OPTION
62 int ld_protocol
= DEFAULT_PROTOCOL
;
64 #ifndef LDAP_OPT_SUCCESS
65 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
67 double warn_time
= UNDEFINED
;
68 double crit_time
= UNDEFINED
;
71 int ssl_on_connect
= FALSE
;
76 char *SERVICE
= "LDAP";
79 main (int argc
, char *argv
[])
85 /* should be int result = STATE_UNKNOWN; */
87 int status
= STATE_UNKNOWN
;
96 setlocale (LC_ALL
, "");
97 bindtextdomain (PACKAGE
, LOCALEDIR
);
100 if (strstr(argv
[0],"check_ldaps")) {
101 asprintf (&progname
, "check_ldaps");
104 /* Parse extra opts if any */
105 argv
=np_extra_opts (&argc
, argv
, progname
);
107 if (process_arguments (argc
, argv
) == ERROR
)
108 usage4 (_("Could not parse arguments"));
110 if (strstr(argv
[0],"check_ldaps") && ! starttls
&& ! ssl_on_connect
)
113 /* initialize alarm signal handling */
114 signal (SIGALRM
, socket_timeout_alarm_handler
);
116 /* set socket timeout */
117 alarm (socket_timeout
);
119 /* get the start time */
120 gettimeofday (&tv
, NULL
);
122 /* initialize ldap */
123 #ifdef HAVE_LDAP_INIT
124 if (!(ld
= ldap_init (ld_host
, ld_port
))) {
125 printf ("Could not connect to the server at port %i\n", ld_port
);
126 return STATE_CRITICAL
;
129 if (!(ld
= ldap_open (ld_host
, ld_port
))) {
131 ldap_perror(ld
, "ldap_open");
132 printf (_("Could not connect to the server at port %i\n"), ld_port
);
133 return STATE_CRITICAL
;
135 #endif /* HAVE_LDAP_INIT */
137 #ifdef HAVE_LDAP_SET_OPTION
138 /* set ldap options */
139 if (ldap_set_option (ld
, LDAP_OPT_PROTOCOL_VERSION
, &ld_protocol
) !=
141 printf(_("Could not set protocol version %d\n"), ld_protocol
);
142 return STATE_CRITICAL
;
146 if (ld_port
== LDAPS_PORT
|| ssl_on_connect
) {
147 asprintf (&SERVICE
, "LDAPS");
148 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
149 /* ldaps: set option tls */
150 tls
= LDAP_OPT_X_TLS_HARD
;
152 if (ldap_set_option (ld
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
155 ldap_perror(ld
, "ldaps_option");
156 printf (_("Could not init TLS at port %i!\n"), ld_port
);
157 return STATE_CRITICAL
;
160 printf (_("TLS not supported by the libraries!\n"));
161 return STATE_CRITICAL
;
162 #endif /* LDAP_OPT_X_TLS */
163 } else if (starttls
) {
164 asprintf (&SERVICE
, "LDAP-TLS");
165 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
166 /* ldap with startTLS: set option version */
167 if (ldap_get_option(ld
,LDAP_OPT_PROTOCOL_VERSION
, &version
) == LDAP_OPT_SUCCESS
)
169 if (version
< LDAP_VERSION3
)
171 version
= LDAP_VERSION3
;
172 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
176 if (ldap_start_tls_s(ld
, NULL
, NULL
) != LDAP_SUCCESS
)
179 ldap_perror(ld
, "ldap_start_tls");
180 printf (_("Could not init startTLS at port %i!\n"), ld_port
);
181 return STATE_CRITICAL
;
184 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
185 return STATE_CRITICAL
;
186 #endif /* HAVE_LDAP_START_TLS_S */
189 /* bind to the ldap server */
190 if (ldap_bind_s (ld
, ld_binddn
, ld_passwd
, LDAP_AUTH_SIMPLE
) !=
193 ldap_perror(ld
, "ldap_bind");
194 printf (_("Could not bind to the LDAP server\n"));
195 return STATE_CRITICAL
;
198 /* do a search of all objectclasses in the base dn */
199 if (ldap_search_s (ld
, ld_base
, LDAP_SCOPE_BASE
, ld_attr
, NULL
, 0, &result
)
202 ldap_perror(ld
, "ldap_search");
203 printf (_("Could not search/find objectclasses in %s\n"), ld_base
);
204 return STATE_CRITICAL
;
207 /* unbind from the ldap server */
210 /* reset the alarm handler */
213 /* calcutate the elapsed time and compare to thresholds */
215 microsec
= deltime (tv
);
216 elapsed_time
= (double)microsec
/ 1.0e6
;
218 if (crit_time
!=UNDEFINED
&& elapsed_time
>crit_time
)
219 status
= STATE_CRITICAL
;
220 else if (warn_time
!=UNDEFINED
&& elapsed_time
>warn_time
)
221 status
= STATE_WARNING
;
225 /* print out the result */
226 printf (_("LDAP %s - %.3f seconds response time|%s\n"),
229 fperfdata ("time", elapsed_time
, "s",
230 (int)warn_time
, warn_time
,
231 (int)crit_time
, crit_time
,
237 /* process command-line arguments */
239 process_arguments (int argc
, char **argv
)
244 /* initialize the long option struct */
245 static struct option longopts
[] = {
246 {"help", no_argument
, 0, 'h'},
247 {"version", no_argument
, 0, 'V'},
248 {"timeout", required_argument
, 0, 't'},
249 {"host", required_argument
, 0, 'H'},
250 {"base", required_argument
, 0, 'b'},
251 {"attr", required_argument
, 0, 'a'},
252 {"bind", required_argument
, 0, 'D'},
253 {"pass", required_argument
, 0, 'P'},
254 #ifdef HAVE_LDAP_SET_OPTION
255 {"ver2", no_argument
, 0, '2'},
256 {"ver3", no_argument
, 0, '3'},
258 {"starttls", no_argument
, 0, 'T'},
259 {"ssl", no_argument
, 0, 'S'},
260 {"use-ipv4", no_argument
, 0, '4'},
261 {"use-ipv6", no_argument
, 0, '6'},
262 {"port", required_argument
, 0, 'p'},
263 {"warn", required_argument
, 0, 'w'},
264 {"crit", required_argument
, 0, 'c'},
265 {"verbose", no_argument
, 0, 'v'},
272 for (c
= 1; c
< argc
; c
++) {
273 if (strcmp ("-to", argv
[c
]) == 0)
274 strcpy (argv
[c
], "-t");
278 c
= getopt_long (argc
, argv
, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts
, &option
);
280 if (c
== -1 || c
== EOF
)
287 case 'V': /* version */
288 print_revision (progname
, NP_VERSION
);
290 case 't': /* timeout period */
291 if (!is_intnonneg (optarg
))
292 usage2 (_("Timeout interval must be a positive integer"), optarg
);
294 socket_timeout
= atoi (optarg
);
303 ld_port
= atoi (optarg
);
315 warn_time
= strtod (optarg
, NULL
);
318 crit_time
= strtod (optarg
, NULL
);
320 #ifdef HAVE_LDAP_SET_OPTION
329 address_family
= AF_INET
;
335 if (! ssl_on_connect
)
338 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
342 ssl_on_connect
= TRUE
;
343 ld_port
= LDAPS_PORT
;
345 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
349 address_family
= AF_INET6
;
351 usage (_("IPv6 support not available\n"));
360 if (ld_host
== NULL
&& is_host(argv
[c
]))
361 ld_host
= strdup (argv
[c
++]);
363 if (ld_base
== NULL
&& argv
[c
])
364 ld_base
= strdup (argv
[c
++]);
366 return validate_arguments ();
371 validate_arguments ()
373 if (ld_host
==NULL
|| strlen(ld_host
)==0)
374 usage4 (_("Please specify the host name\n"));
376 if (ld_base
==NULL
|| strlen(ld_base
)==0)
377 usage4 (_("Please specify the LDAP base\n"));
387 asprintf (&myport
, "%d", DEFAULT_PORT
);
389 print_revision (progname
, NP_VERSION
);
391 printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
392 printf (COPYRIGHT
, copyright
, email
);
398 printf (_(UT_HELP_VRSN
));
399 printf (_(UT_EXTRA_OPTS
));
401 printf (_(UT_HOST_PORT
), 'p', myport
);
403 printf (_(UT_IPv46
));
405 printf (" %s\n", "-a [--attr]");
406 printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
407 printf (" %s\n", "-b [--base]");
408 printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
409 printf (" %s\n", "-D [--bind]");
410 printf (" %s\n", _("ldap bind DN (if required)"));
411 printf (" %s\n", "-P [--pass]");
412 printf (" %s\n", _("ldap password (if required)"));
413 printf (" %s\n", "-T [--starttls]");
414 printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
415 printf (" %s\n", "-S [--ssl]");
416 printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT
);
418 #ifdef HAVE_LDAP_SET_OPTION
419 printf (" %s\n", "-2 [--ver2]");
420 printf (" %s\n", _("use ldap protocol version 2"));
421 printf (" %s\n", "-3 [--ver3]");
422 printf (" %s\n", _("use ldap protocol version 3"));
423 printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL
);
426 printf (_(UT_WARN_CRIT
));
428 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
430 printf (_(UT_VERBOSE
));
433 printf ("%s\n", _("Notes:"));
434 printf (" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
435 printf (_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT
);
436 printf (" %s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
437 printf (" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
438 printf (" %s\n", _("to define the behaviour explicitly instead."));
441 printf (_(UT_EXTRA_OPTS_NOTES
));
444 printf (_(UT_SUPPORT
));
448 * add option -4 and -6 to the long manual
455 printf (_("Usage:"));
456 printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname
);
457 printf ("\n [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
458 #ifdef HAVE_LDAP_SET_OPTION