1 /******************************************************************************
3 * Nagios check_ldap plugin
6 * Copyright (c) 2000-2006 nagios-plugins team
8 * Last Modified: $Date$
12 * This file contains the check_ldap plugin
14 * License Information:
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 ******************************************************************************/
34 /* progname may be check_ldaps */
35 char *progname
= "check_ldap";
36 const char *revision
= "$Revision$";
37 const char *copyright
= "2000-2006";
38 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
49 #ifdef HAVE_LDAP_SET_OPTION
55 int process_arguments (int, char **);
56 int validate_arguments (void);
57 void print_help (void);
58 void print_usage (void);
60 char ld_defattr
[] = "(objectclass=*)";
61 char *ld_attr
= ld_defattr
;
64 char *ld_passwd
= NULL
;
65 char *ld_binddn
= NULL
;
66 int ld_port
= DEFAULT_PORT
;
67 #ifdef HAVE_LDAP_SET_OPTION
68 int ld_protocol
= DEFAULT_PROTOCOL
;
70 #ifndef LDAP_OPT_SUCCESS
71 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
73 double warn_time
= UNDEFINED
;
74 double crit_time
= UNDEFINED
;
77 int ssl_on_connect
= FALSE
;
82 char *SERVICE
= "LDAP";
85 main (int argc
, char *argv
[])
91 /* should be int result = STATE_UNKNOWN; */
93 int status
= STATE_UNKNOWN
;
102 setlocale (LC_ALL
, "");
103 bindtextdomain (PACKAGE
, LOCALEDIR
);
104 textdomain (PACKAGE
);
106 if (strstr(argv
[0],"check_ldaps")) {
107 asprintf (&progname
, "check_ldaps");
110 if (process_arguments (argc
, argv
) == ERROR
)
111 usage4 (_("Could not parse arguments"));
113 if (strstr(argv
[0],"check_ldaps") && ! starttls
&& ! ssl_on_connect
)
116 /* initialize alarm signal handling */
117 signal (SIGALRM
, socket_timeout_alarm_handler
);
119 /* set socket timeout */
120 alarm (socket_timeout
);
122 /* get the start time */
123 gettimeofday (&tv
, NULL
);
125 /* initialize ldap */
126 #ifdef HAVE_LDAP_INIT
127 if (!(ld
= ldap_init (ld_host
, ld_port
))) {
128 printf ("Could not connect to the server at port %i\n", ld_port
);
129 return STATE_CRITICAL
;
132 if (!(ld
= ldap_open (ld_host
, ld_port
))) {
134 ldap_perror(ld
, "ldap_open");
135 printf (_("Could not connect to the server at port %i\n"), ld_port
);
136 return STATE_CRITICAL
;
138 #endif /* HAVE_LDAP_INIT */
140 #ifdef HAVE_LDAP_SET_OPTION
141 /* set ldap options */
142 if (ldap_set_option (ld
, LDAP_OPT_PROTOCOL_VERSION
, &ld_protocol
) !=
144 printf(_("Could not set protocol version %d\n"), ld_protocol
);
145 return STATE_CRITICAL
;
149 if (ld_port
== LDAPS_PORT
|| ssl_on_connect
) {
150 asprintf (&SERVICE
, "LDAPS");
151 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
152 /* ldaps: set option tls */
153 tls
= LDAP_OPT_X_TLS_HARD
;
155 if (ldap_set_option (ld
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
158 ldap_perror(ld
, "ldaps_option");
159 printf (_("Could not init TLS at port %i!\n"), ld_port
);
160 return STATE_CRITICAL
;
163 printf (_("TLS not supported by the libraries!\n"));
164 return STATE_CRITICAL
;
165 #endif /* LDAP_OPT_X_TLS */
166 } else if (starttls
) {
167 asprintf (&SERVICE
, "LDAP-TLS");
168 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
169 /* ldap with startTLS: set option version */
170 if (ldap_get_option(ld
,LDAP_OPT_PROTOCOL_VERSION
, &version
) == LDAP_OPT_SUCCESS
)
172 if (version
< LDAP_VERSION3
)
174 version
= LDAP_VERSION3
;
175 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
179 if (ldap_start_tls_s(ld
, NULL
, NULL
) != LDAP_SUCCESS
)
182 ldap_perror(ld
, "ldap_start_tls");
183 printf (_("Could not init startTLS at port %i!\n"), ld_port
);
184 return STATE_CRITICAL
;
187 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
188 return STATE_CRITICAL
;
189 #endif /* HAVE_LDAP_START_TLS_S */
192 /* bind to the ldap server */
193 if (ldap_bind_s (ld
, ld_binddn
, ld_passwd
, LDAP_AUTH_SIMPLE
) !=
196 ldap_perror(ld
, "ldap_bind");
197 printf (_("Could not bind to the ldap-server\n"));
198 return STATE_CRITICAL
;
201 /* do a search of all objectclasses in the base dn */
202 if (ldap_search_s (ld
, ld_base
, LDAP_SCOPE_BASE
, ld_attr
, NULL
, 0, &result
)
205 ldap_perror(ld
, "ldap_search");
206 printf (_("Could not search/find objectclasses in %s\n"), ld_base
);
207 return STATE_CRITICAL
;
210 /* unbind from the ldap server */
213 /* reset the alarm handler */
216 /* calcutate the elapsed time and compare to thresholds */
218 microsec
= deltime (tv
);
219 elapsed_time
= (double)microsec
/ 1.0e6
;
221 if (crit_time
!=UNDEFINED
&& elapsed_time
>crit_time
)
222 status
= STATE_CRITICAL
;
223 else if (warn_time
!=UNDEFINED
&& elapsed_time
>warn_time
)
224 status
= STATE_WARNING
;
228 /* print out the result */
229 printf (_("LDAP %s - %.3f seconds response time|%s\n"),
232 fperfdata ("time", elapsed_time
, "s",
233 (int)warn_time
, warn_time
,
234 (int)crit_time
, crit_time
,
240 /* process command-line arguments */
242 process_arguments (int argc
, char **argv
)
247 /* initialize the long option struct */
248 static struct option longopts
[] = {
249 {"help", no_argument
, 0, 'h'},
250 {"version", no_argument
, 0, 'V'},
251 {"timeout", required_argument
, 0, 't'},
252 {"host", required_argument
, 0, 'H'},
253 {"base", required_argument
, 0, 'b'},
254 {"attr", required_argument
, 0, 'a'},
255 {"bind", required_argument
, 0, 'D'},
256 {"pass", required_argument
, 0, 'P'},
257 #ifdef HAVE_LDAP_SET_OPTION
258 {"ver2", no_argument
, 0, '2'},
259 {"ver3", no_argument
, 0, '3'},
261 {"starttls", no_argument
, 0, 'T'},
262 {"ssl", no_argument
, 0, 'S'},
263 {"use-ipv4", no_argument
, 0, '4'},
264 {"use-ipv6", no_argument
, 0, '6'},
265 {"port", required_argument
, 0, 'p'},
266 {"warn", required_argument
, 0, 'w'},
267 {"crit", required_argument
, 0, 'c'},
268 {"verbose", no_argument
, 0, 'v'},
275 for (c
= 1; c
< argc
; c
++) {
276 if (strcmp ("-to", argv
[c
]) == 0)
277 strcpy (argv
[c
], "-t");
281 c
= getopt_long (argc
, argv
, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts
, &option
);
283 if (c
== -1 || c
== EOF
)
290 case 'V': /* version */
291 print_revision (progname
, revision
);
293 case 't': /* timeout period */
294 if (!is_intnonneg (optarg
))
295 usage2 (_("Timeout interval must be a positive integer"), optarg
);
297 socket_timeout
= atoi (optarg
);
306 ld_port
= atoi (optarg
);
318 warn_time
= strtod (optarg
, NULL
);
321 crit_time
= strtod (optarg
, NULL
);
323 #ifdef HAVE_LDAP_SET_OPTION
332 address_family
= AF_INET
;
338 if (! ssl_on_connect
)
341 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
345 ssl_on_connect
= TRUE
;
346 ld_port
= LDAPS_PORT
;
348 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
352 address_family
= AF_INET6
;
354 usage (_("IPv6 support not available\n"));
363 if (ld_host
== NULL
&& is_host(argv
[c
]))
364 ld_host
= strdup (argv
[c
++]);
366 if (ld_base
== NULL
&& argv
[c
])
367 ld_base
= strdup (argv
[c
++]);
369 return validate_arguments ();
374 validate_arguments ()
376 if (ld_host
==NULL
|| strlen(ld_host
)==0)
377 usage4 (_("Please specify the host name\n"));
379 if (ld_base
==NULL
|| strlen(ld_base
)==0)
380 usage4 (_("Please specify the LDAP base\n"));
390 asprintf (&myport
, "%d", DEFAULT_PORT
);
392 print_revision (progname
, revision
);
394 printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
395 printf (COPYRIGHT
, copyright
, email
);
401 printf (_(UT_HELP_VRSN
));
403 printf (_(UT_HOST_PORT
), 'p', myport
);
405 printf (_(UT_IPv46
));
407 printf (" %s\n", "-a [--attr]");
408 printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
409 printf (" %s\n", "-b [--base]");
410 printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
411 printf (" %s\n", "-D [--bind]");
412 printf (" %s\n", _("ldap bind DN (if required)"));
413 printf (" %s\n", "-P [--pass]");
414 printf (" %s\n", _("ldap password (if required)"));
415 printf (" %s\n", "-T [--starttls]");
416 printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
417 printf (" %s\n", "-S [--ssl]");
418 printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT
);
420 #ifdef HAVE_LDAP_SET_OPTION
421 printf (" %s\n", "-2 [--ver2]");
422 printf (" %s\n", _("use ldap protocol version 2"));
423 printf (" %s\n", "-3 [--ver3]");
424 printf (" %s\n", _("use ldap protocol version 3"));
425 printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL
);
428 printf (_(UT_WARN_CRIT
));
430 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
432 printf (_(UT_VERBOSE
));
434 printf ("\n%s\n", _("Note:"));
435 printf ("%s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
436 printf (_("implied (using default port %i) unless --port=636 is specified. In that case %s"), DEFAULT_PORT
, "\n");
437 printf ("%s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
438 printf ("%s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
439 printf ("%s\n", _("to define the behaviour explicitly instead."));
441 printf (_(UT_SUPPORT
));
445 * add option -4 and -6 to the long manual
452 printf (_("Usage:"));
453 printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname
);
454 printf ("\n [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
455 #ifdef HAVE_LDAP_SET_OPTION