Code

a2f0dee6551a28c6a276e3af5f40303805089c83
[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;
76 /* for ldap tls */
78 char *SERVICE = "LDAP";
80 int
81 main (int argc, char *argv[])
82 {
84         LDAP *ld;
85         LDAPMessage *result;
87         /* should be    int result = STATE_UNKNOWN; */
88         
89         int status = STATE_UNKNOWN;
90         long microsec;
91         double elapsed_time;
92         
93         /* for ldap tls */
94         
95         int tls; 
96         int version=3;
98         setlocale (LC_ALL, "");
99         bindtextdomain (PACKAGE, LOCALEDIR);
100         textdomain (PACKAGE);
102         if (strstr(argv[0],"check_ldaps")) {
103                 asprintf (&progname, "check_ldaps");
104                 starttls = TRUE;
105         }
106         
107         if (process_arguments (argc, argv) == ERROR)
108                 usage4 (_("Could not parse arguments"));
110         /* initialize alarm signal handling */
111         signal (SIGALRM, socket_timeout_alarm_handler);
113         /* set socket timeout */
114         alarm (socket_timeout);
116         /* get the start time */
117         gettimeofday (&tv, NULL);
119         /* initialize ldap */
120 #ifdef HAVE_LDAP_INIT
121         if (!(ld = ldap_init (ld_host, ld_port))) {
122                 printf ("Could not connect to the server at port %i\n", ld_port);
123                 return STATE_CRITICAL;
124         }
125 #else   
126         if (!(ld = ldap_open (ld_host, ld_port))) {
127                 /*ldap_perror(ld, "ldap_open"); */
128                 printf (_("Could not connect to the server at port %i\n"), ld_port);
129                 return STATE_CRITICAL;
130         }
131 #endif /* HAVE_LDAP_INIT */
132         
133 #ifdef HAVE_LDAP_SET_OPTION
134         /* set ldap options */
135         if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
136                         LDAP_OPT_SUCCESS ) {
137                 printf(_("Could not set protocol version %d\n"), ld_protocol);
138                 return STATE_CRITICAL;
139         }
140 #endif
142         if (ld_port == LDAPS_PORT || ssl_on_connect) {
143                 asprintf (&SERVICE, "LDAPS");
144 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
145                 /* ldaps: set option tls */
146                 tls = LDAP_OPT_X_TLS_HARD;
147                 
148                 if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
149                 {
150                         /*ldap_perror(ld, "ldaps_option"); */
151                         printf (_("Could not init TLS at port %i!\n"), ld_port);
152                         return STATE_CRITICAL;
153                 }
154 #else
155                 printf (_("TLS not supported by the libraries!\n"));
156                 return STATE_CRITICAL;
157 #endif /* LDAP_OPT_X_TLS */
158         } else if (starttls) {
159                 asprintf (&SERVICE, "LDAP-TLS");
160 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
161                 /* ldap with startTLS: set option version */
162                 if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
163                 {
164                         if (version < LDAP_VERSION3)
165                         {
166                                 version = LDAP_VERSION3;
167                                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
168                         }
169                 }
170                 /* call start_tls */
171                 if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
172                 {
173                         /*ldap_perror(ld, "ldap_start_tls"); */
174                         printf (_("Could not init startTLS at port %i!\n"), ld_port);
175                         return STATE_CRITICAL;
176                 }
177 #else
178                 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
179                 return STATE_CRITICAL;
180 #endif /* HAVE_LDAP_START_TLS_S */
181         }
182         
183         /* bind to the ldap server */
184         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
185                         LDAP_SUCCESS) {
186                 /*ldap_perror(ld, "ldap_bind"); */
187                 printf (_("Could not bind to the ldap-server\n"));
188                 return STATE_CRITICAL;
189         }
191         /* do a search of all objectclasses in the base dn */
192         if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
193                         != LDAP_SUCCESS) {
194                 /*ldap_perror(ld, "ldap_search"); */
195                 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
196                 return STATE_CRITICAL;
197         }
199         /* unbind from the ldap server */
200         ldap_unbind (ld);
202         /* reset the alarm handler */
203         alarm (0);
205         /* calcutate the elapsed time and compare to thresholds */
207         microsec = deltime (tv);
208         elapsed_time = (double)microsec / 1.0e6;
210         if (crit_time!=UNDEFINED && elapsed_time>crit_time)
211                 status = STATE_CRITICAL;
212         else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
213                 status = STATE_WARNING;
214         else
215                 status = STATE_OK;
217         /* print out the result */
218         printf (_("LDAP %s - %.3f seconds response time|%s\n"),
219                 state_text (status),
220                 elapsed_time,
221                 fperfdata ("time", elapsed_time, "s",
222                           (int)warn_time, warn_time,
223                           (int)crit_time, crit_time,
224                           TRUE, 0, FALSE, 0));
226         return status;
229 /* process command-line arguments */
230 int
231 process_arguments (int argc, char **argv)
233         int c;
235         int option = 0;
236         /* initialize the long option struct */
237         static struct option longopts[] = {
238                 {"help", no_argument, 0, 'h'},
239                 {"version", no_argument, 0, 'V'},
240                 {"timeout", required_argument, 0, 't'},
241                 {"host", required_argument, 0, 'H'},
242                 {"base", required_argument, 0, 'b'},
243                 {"attr", required_argument, 0, 'a'},
244                 {"bind", required_argument, 0, 'D'},
245                 {"pass", required_argument, 0, 'P'},
246 #ifdef HAVE_LDAP_SET_OPTION
247                 {"ver2", no_argument, 0, '2'},
248                 {"ver3", no_argument, 0, '3'},
249 #endif
250                 {"starttls", no_argument, 0, 'T'},
251                 {"ssl", no_argument, 0, 'S'},
252                 {"use-ipv4", no_argument, 0, '4'},
253                 {"use-ipv6", no_argument, 0, '6'},
254                 {"port", required_argument, 0, 'p'},
255                 {"warn", required_argument, 0, 'w'},
256                 {"crit", required_argument, 0, 'c'},
257                 {0, 0, 0, 0}
258         };
260         if (argc < 2)
261                 return ERROR;
263         for (c = 1; c < argc; c++) {
264                 if (strcmp ("-to", argv[c]) == 0)
265                         strcpy (argv[c], "-t");
266         }
268         while (1) {
269                 c = getopt_long (argc, argv, "hV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
271                 if (c == -1 || c == EOF)
272                         break;
274                 switch (c) {
275                 case 'h':                                                                       /* help */
276                         print_help ();
277                         exit (STATE_OK);
278                 case 'V':                                                                       /* version */
279                         print_revision (progname, revision);
280                         exit (STATE_OK);
281                 case 't':                                                                       /* timeout period */
282                         if (!is_intnonneg (optarg))
283                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
284                         else
285                                 socket_timeout = atoi (optarg);
286                         break;
287                 case 'H':
288                         ld_host = optarg;
289                         break;
290                 case 'b':
291                         ld_base = optarg;
292                         break;
293                 case 'p':
294                         ld_port = atoi (optarg);
295                         break;
296                 case 'a':
297                         ld_attr = optarg;
298                         break;
299                 case 'D':
300                         ld_binddn = optarg;
301                         break;
302                 case 'P':
303                         ld_passwd = optarg;
304                         break;
305                 case 'w':
306                         warn_time = strtod (optarg, NULL);
307                         break;
308                 case 'c':
309                         crit_time = strtod (optarg, NULL);
310                         break;
311 #ifdef HAVE_LDAP_SET_OPTION
312                 case '2':
313                         ld_protocol = 2;
314                         break;
315                 case '3':
316                         ld_protocol = 3;
317                         break;
318 #endif
319                 case '4':
320                         address_family = AF_INET;
321                         break;
322                 case 'T':
323                         if (! ssl_on_connect)
324                                 starttls = TRUE;
325                         else
326                                 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
327                         break;
328                 case 'S':
329                         if (! starttls) {
330                                 ssl_on_connect = TRUE;
331                                 ld_port = LDAPS_PORT;
332                         } else
333                                 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
334                         break;
335                 case '6':
336 #ifdef USE_IPV6
337                         address_family = AF_INET6;
338 #else
339                         usage (_("IPv6 support not available\n"));
340 #endif
341                         break;
342                 default:
343                         usage5 ();
344                 }
345         }
347         c = optind;
348         if (ld_host == NULL && is_host(argv[c]))
349                 ld_host = strdup (argv[c++]);
351         if (ld_base == NULL && argv[c])
352                 ld_base = strdup (argv[c++]);
354         return validate_arguments ();
358 int
359 validate_arguments ()
361         if (ld_host==NULL || strlen(ld_host)==0)
362                 usage4 (_("Please specify the host name\n"));
364         if (ld_base==NULL || strlen(ld_base)==0)
365                 usage4 (_("Please specify the LDAP base\n"));
367         return OK;
371 void
372 print_help (void)
374         char *myport;
375         asprintf (&myport, "%d", DEFAULT_PORT);
377         print_revision (progname, revision);
379         printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
380         printf (COPYRIGHT, copyright, email);
382   printf ("\n\n");
383   
384         print_usage ();
386         printf (_(UT_HELP_VRSN));
388         printf (_(UT_HOST_PORT), 'p', myport);
390         printf (_(UT_IPv46));
392         printf (" %s\n", "-a [--attr]");
393   printf ("    %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
394   printf (" %s\n", "-b [--base]");
395   printf ("    %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
396   printf (" %s\n", "-D [--bind]");
397   printf ("    %s\n", _("ldap bind DN (if required)"));
398   printf (" %s\n", "-P [--pass]");
399   printf ("    %s\n", _("ldap password (if required)"));
400   printf (" %s\n", "-T [--starttls]");
401   printf ("    %s\n", _("use starttls mechanism introduced in protocol version 3"));
402   printf (" %s\n", "-S [--ssl]");
403   printf ("    %s\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to %s"), LDAPS_PORT);
405 #ifdef HAVE_LDAP_SET_OPTION
406         printf (" %s\n", "-2 [--ver2]");
407   printf ("    %s\n", _("use ldap protocol version 2"));
408   printf (" %s\n", "-3 [--ver3]");
409   printf ("    %s\n", _("use ldap protocol version 3"));
410   printf ("    (default protocol version: %d)\n", DEFAULT_PROTOCOL);
411 #endif
413         printf (_(UT_WARN_CRIT));
415         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
417         printf (_(UT_VERBOSE));
419         printf ("\n%s\n", _("Note:"));
420         printf ("%s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
421         printf (_("implied (using default port %i) unless --port=636 is specified. In that case %s"), DEFAULT_PORT, "\n");
422         printf ("%s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
423         printf ("%s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
424         printf ("%s\n", _("to define the behaviour explicitly instead."));
426         printf (_(UT_SUPPORT));
429 /* todo
430 * add option -4 and -6 to the long manual
432 */
434 void
435 print_usage (void)
437   printf (_("Usage:"));
438         printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
439   printf ("\n       [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
440 #ifdef HAVE_LDAP_SET_OPTION
441                         "\n       [-2|-3] [-4|-6]"
442 #else
443                         ""
444 #endif
445                         );