Code

a0b739cc573af96e7bd5ce36258f59b974667e6b
[nagiosplug.git] / plugins / check_ldap.c
1 /******************************************************************************
2 *
3 * Nagios check_ldap plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_ldap plugin
13 *
14 * License Information:
15 *
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.
20 *
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.
25 *
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.
30  $Id$
31  
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";
40 #include "common.h"
41 #include "netutils.h"
42 #include "utils.h"
44 #include <lber.h>
45 #include <ldap.h>
47 enum {
48         UNDEFINED = 0,
49 #ifdef HAVE_LDAP_SET_OPTION
50         DEFAULT_PROTOCOL = 2,
51 #endif
52         DEFAULT_PORT = 389
53 };
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;
62 char *ld_host = NULL;
63 char *ld_base = NULL;
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;
69 #endif
70 double warn_time = UNDEFINED;
71 double crit_time = UNDEFINED;
72 struct timeval tv;
73 int starttls = FALSE;
74 int ssl_on_connect = FALSE;
75 int verbose = 0;
77 /* for ldap tls */
79 char *SERVICE = "LDAP";
81 int
82 main (int argc, char *argv[])
83 {
85         LDAP *ld;
86         LDAPMessage *result;
88         /* should be    int result = STATE_UNKNOWN; */
89         
90         int status = STATE_UNKNOWN;
91         long microsec;
92         double elapsed_time;
93         
94         /* for ldap tls */
95         
96         int tls; 
97         int version=3;
99         setlocale (LC_ALL, "");
100         bindtextdomain (PACKAGE, LOCALEDIR);
101         textdomain (PACKAGE);
103         if (strstr(argv[0],"check_ldaps")) {
104                 asprintf (&progname, "check_ldaps");
105         }
106         
107         if (process_arguments (argc, argv) == ERROR)
108                 usage4 (_("Could not parse arguments"));
110         if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
111                 starttls = TRUE;
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;
127         }
128 #else   
129         if (!(ld = ldap_open (ld_host, ld_port))) {
130                 if (verbose)
131                         ldap_perror(ld, "ldap_open");
132                 printf (_("Could not connect to the server at port %i\n"), ld_port);
133                 return STATE_CRITICAL;
134         }
135 #endif /* HAVE_LDAP_INIT */
136         
137 #ifdef HAVE_LDAP_SET_OPTION
138         /* set ldap options */
139         if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
140                         LDAP_OPT_SUCCESS ) {
141                 printf(_("Could not set protocol version %d\n"), ld_protocol);
142                 return STATE_CRITICAL;
143         }
144 #endif
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;
151                 
152                 if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
153                 {
154                         if (verbose)
155                                 ldap_perror(ld, "ldaps_option");
156                         printf (_("Could not init TLS at port %i!\n"), ld_port);
157                         return STATE_CRITICAL;
158                 }
159 #else
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 )
168                 {
169                         if (version < LDAP_VERSION3)
170                         {
171                                 version = LDAP_VERSION3;
172                                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
173                         }
174                 }
175                 /* call start_tls */
176                 if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
177                 {
178                         if (verbose) 
179                                 ldap_perror(ld, "ldap_start_tls");
180                         printf (_("Could not init startTLS at port %i!\n"), ld_port);
181                         return STATE_CRITICAL;
182                 }
183 #else
184                 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
185                 return STATE_CRITICAL;
186 #endif /* HAVE_LDAP_START_TLS_S */
187         }
188         
189         /* bind to the ldap server */
190         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
191                         LDAP_SUCCESS) {
192                 if (verbose)
193                         ldap_perror(ld, "ldap_bind");
194                 printf (_("Could not bind to the ldap-server\n"));
195                 return STATE_CRITICAL;
196         }
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)
200                         != LDAP_SUCCESS) {
201                 if (verbose)
202                         ldap_perror(ld, "ldap_search");
203                 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
204                 return STATE_CRITICAL;
205         }
207         /* unbind from the ldap server */
208         ldap_unbind (ld);
210         /* reset the alarm handler */
211         alarm (0);
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;
222         else
223                 status = STATE_OK;
225         /* print out the result */
226         printf (_("LDAP %s - %.3f seconds response time|%s\n"),
227                 state_text (status),
228                 elapsed_time,
229                 fperfdata ("time", elapsed_time, "s",
230                           (int)warn_time, warn_time,
231                           (int)crit_time, crit_time,
232                           TRUE, 0, FALSE, 0));
234         return status;
237 /* process command-line arguments */
238 int
239 process_arguments (int argc, char **argv)
241         int c;
243         int option = 0;
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'},
257 #endif
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'},
266                 {0, 0, 0, 0}
267         };
269         if (argc < 2)
270                 return ERROR;
272         for (c = 1; c < argc; c++) {
273                 if (strcmp ("-to", argv[c]) == 0)
274                         strcpy (argv[c], "-t");
275         }
277         while (1) {
278                 c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
280                 if (c == -1 || c == EOF)
281                         break;
283                 switch (c) {
284                 case 'h':                                                                       /* help */
285                         print_help ();
286                         exit (STATE_OK);
287                 case 'V':                                                                       /* version */
288                         print_revision (progname, revision);
289                         exit (STATE_OK);
290                 case 't':                                                                       /* timeout period */
291                         if (!is_intnonneg (optarg))
292                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
293                         else
294                                 socket_timeout = atoi (optarg);
295                         break;
296                 case 'H':
297                         ld_host = optarg;
298                         break;
299                 case 'b':
300                         ld_base = optarg;
301                         break;
302                 case 'p':
303                         ld_port = atoi (optarg);
304                         break;
305                 case 'a':
306                         ld_attr = optarg;
307                         break;
308                 case 'D':
309                         ld_binddn = optarg;
310                         break;
311                 case 'P':
312                         ld_passwd = optarg;
313                         break;
314                 case 'w':
315                         warn_time = strtod (optarg, NULL);
316                         break;
317                 case 'c':
318                         crit_time = strtod (optarg, NULL);
319                         break;
320 #ifdef HAVE_LDAP_SET_OPTION
321                 case '2':
322                         ld_protocol = 2;
323                         break;
324                 case '3':
325                         ld_protocol = 3;
326                         break;
327 #endif
328                 case '4':
329                         address_family = AF_INET;
330                         break;
331                 case 'v':
332                         verbose++;
333                         break;
334                 case 'T':
335                         if (! ssl_on_connect)
336                                 starttls = TRUE;
337                         else
338                                 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
339                         break;
340                 case 'S':
341                         if (! starttls) {
342                                 ssl_on_connect = TRUE;
343                                 ld_port = LDAPS_PORT;
344                         } else
345                                 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
346                         break;
347                 case '6':
348 #ifdef USE_IPV6
349                         address_family = AF_INET6;
350 #else
351                         usage (_("IPv6 support not available\n"));
352 #endif
353                         break;
354                 default:
355                         usage5 ();
356                 }
357         }
359         c = optind;
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 ();
370 int
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"));
379         return OK;
383 void
384 print_help (void)
386         char *myport;
387         asprintf (&myport, "%d", DEFAULT_PORT);
389         print_revision (progname, revision);
391         printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
392         printf (COPYRIGHT, copyright, email);
394   printf ("\n\n");
395   
396         print_usage ();
398         printf (_(UT_HELP_VRSN));
400         printf (_(UT_HOST_PORT), 'p', myport);
402         printf (_(UT_IPv46));
404         printf (" %s\n", "-a [--attr]");
405   printf ("    %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
406   printf (" %s\n", "-b [--base]");
407   printf ("    %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
408   printf (" %s\n", "-D [--bind]");
409   printf ("    %s\n", _("ldap bind DN (if required)"));
410   printf (" %s\n", "-P [--pass]");
411   printf ("    %s\n", _("ldap password (if required)"));
412   printf (" %s\n", "-T [--starttls]");
413   printf ("    %s\n", _("use starttls mechanism introduced in protocol version 3"));
414   printf (" %s\n", "-S [--ssl]");
415   printf ("    %s\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to %s"), LDAPS_PORT);
417 #ifdef HAVE_LDAP_SET_OPTION
418         printf (" %s\n", "-2 [--ver2]");
419   printf ("    %s\n", _("use ldap protocol version 2"));
420   printf (" %s\n", "-3 [--ver3]");
421   printf ("    %s\n", _("use ldap protocol version 3"));
422   printf ("    (default protocol version: %d)\n", DEFAULT_PROTOCOL);
423 #endif
425         printf (_(UT_WARN_CRIT));
427         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
429         printf (_(UT_VERBOSE));
431         printf ("\n%s\n", _("Note:"));
432         printf ("%s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
433         printf (_("implied (using default port %i) unless --port=636 is specified. In that case %s"), DEFAULT_PORT, "\n");
434         printf ("%s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
435         printf ("%s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
436         printf ("%s\n", _("to define the behaviour explicitly instead."));
438         printf (_(UT_SUPPORT));
441 /* todo
442 * add option -4 and -6 to the long manual
444 */
446 void
447 print_usage (void)
449   printf (_("Usage:"));
450         printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
451   printf ("\n       [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
452 #ifdef HAVE_LDAP_SET_OPTION
453                         "\n       [-2|-3] [-4|-6]"
454 #else
455                         ""
456 #endif
457                         );