Code

12ea07135defce944055d7f55cc7365a0d0767fa
[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;
74 /* for ldap tls */
76 char *SERVICE = "LDAP";
78 int
79 main (int argc, char *argv[])
80 {
82         LDAP *ld;
83         LDAPMessage *result;
85         /* should be    int result = STATE_UNKNOWN; */
86         
87         int status = STATE_UNKNOWN;
88         long microsec;
89         double elapsed_time;
90         
91         /* for ldap tls */
92         
93         int tls; 
94         int version=3;
96         setlocale (LC_ALL, "");
97         bindtextdomain (PACKAGE, LOCALEDIR);
98         textdomain (PACKAGE);
100         if (strstr(argv[0],"check_ldaps")) {
101                 asprintf (&progname, "check_ldaps");
102         }
103         
104         if (process_arguments (argc, argv) == ERROR)
105                 usage4 (_("Could not parse arguments"));
107         /* initialize alarm signal handling */
108         signal (SIGALRM, socket_timeout_alarm_handler);
110         /* set socket timeout */
111         alarm (socket_timeout);
113         /* get the start time */
114         gettimeofday (&tv, NULL);
116         /* initialize ldap */
117 #ifdef HAVE_LDAP_INIT
118         if (!(ld = ldap_init (ld_host, ld_port))) {
119                 printf ("Could not connect to the server at port %i\n", ld_port);
120                 return STATE_CRITICAL;
121         }
122 #else   
123         if (!(ld = ldap_open (ld_host, ld_port))) {
124                 /*ldap_perror(ld, "ldap_open"); */
125                 printf (_("Could not connect to the server at port %i\n"), ld_port);
126                 return STATE_CRITICAL;
127         }
128 #endif /* HAVE_LDAP_INIT */
129         
130 #ifdef HAVE_LDAP_SET_OPTION
131         /* set ldap options */
132         if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
133                         LDAP_OPT_SUCCESS ) {
134                 printf(_("Could not set protocol version %d\n"), ld_protocol);
135                 return STATE_CRITICAL;
136         }
137 #endif
139         if (strstr(argv[0],"check_ldaps")) {
140         /* with TLS */
141                 if ( ld_port == LDAPS_PORT ) {
142                         asprintf (&SERVICE, "LDAPS");
143 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
144                         /* ldaps: set option tls */
145                         tls = LDAP_OPT_X_TLS_HARD;
146                         
147                         if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
148                         {
149                                 /*ldap_perror(ld, "ldaps_option"); */
150                                 printf (_("Could not init TLS at port %i!\n"), ld_port);
151                                 return STATE_CRITICAL;
152                 }
153 #else
154                         printf (_("TLS not supported by the libraries!\n"), ld_port);
155                         return STATE_CRITICAL;
156 #endif /* LDAP_OPT_X_TLS */
157                 } else {
158                         asprintf (&SERVICE, "LDAP-TLS");
159 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
160                         /* ldap with startTLS: set option version */
161                         if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
162                         {
163                                 if (version < LDAP_VERSION3)
164                                 {
165                                         version = LDAP_VERSION3;
166                                         ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
167                                 }
168                         }
169                         /* call start_tls */
170                         if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
171                         {
172                                 /*ldap_perror(ld, "ldap_start_tls"); */
173                                 printf (_("Could not init startTLS at port %i!\n"), ld_port);
174                                 return STATE_CRITICAL;
175                         }
176 #else
177                         printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
178                         return STATE_CRITICAL;
179 #endif /* HAVE_LDAP_START_TLS_S */
180                 }
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                 {"use-ipv4", no_argument, 0, '4'},
251                 {"use-ipv6", no_argument, 0, '6'},
252                 {"port", required_argument, 0, 'p'},
253                 {"warn", required_argument, 0, 'w'},
254                 {"crit", required_argument, 0, 'c'},
255                 {0, 0, 0, 0}
256         };
258         if (argc < 2)
259                 return ERROR;
261         for (c = 1; c < argc; c++) {
262                 if (strcmp ("-to", argv[c]) == 0)
263                         strcpy (argv[c], "-t");
264         }
266         while (1) {
267                 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
269                 if (c == -1 || c == EOF)
270                         break;
272                 switch (c) {
273                 case 'h':                                                                       /* help */
274                         print_help ();
275                         exit (STATE_OK);
276                 case 'V':                                                                       /* version */
277                         print_revision (progname, revision);
278                         exit (STATE_OK);
279                 case 't':                                                                       /* timeout period */
280                         if (!is_intnonneg (optarg))
281                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
282                         else
283                                 socket_timeout = atoi (optarg);
284                         break;
285                 case 'H':
286                         ld_host = optarg;
287                         break;
288                 case 'b':
289                         ld_base = optarg;
290                         break;
291                 case 'p':
292                         ld_port = atoi (optarg);
293                         break;
294                 case 'a':
295                         ld_attr = optarg;
296                         break;
297                 case 'D':
298                         ld_binddn = optarg;
299                         break;
300                 case 'P':
301                         ld_passwd = optarg;
302                         break;
303                 case 'w':
304                         warn_time = strtod (optarg, NULL);
305                         break;
306                 case 'c':
307                         crit_time = strtod (optarg, NULL);
308                         break;
309 #ifdef HAVE_LDAP_SET_OPTION
310                 case '2':
311                         ld_protocol = 2;
312                         break;
313                 case '3':
314                         ld_protocol = 3;
315                         break;
316 #endif
317                 case '4':
318                         address_family = AF_INET;
319                         break;
320                 case '6':
321 #ifdef USE_IPV6
322                         address_family = AF_INET6;
323 #else
324                         usage (_("IPv6 support not available\n"));
325 #endif
326                         break;
327                 default:
328                         usage5 ();
329                 }
330         }
332         c = optind;
333         if (ld_host == NULL && is_host(argv[c]))
334                 ld_host = strdup (argv[c++]);
336         if (ld_base == NULL && argv[c])
337                 ld_base = strdup (argv[c++]);
339         return validate_arguments ();
343 int
344 validate_arguments ()
346         if (ld_host==NULL || strlen(ld_host)==0)
347                 usage4 (_("Please specify the host name\n"));
349         if (ld_base==NULL || strlen(ld_base)==0)
350                 usage4 (_("Please specify the LDAP base\n"));
352         return OK;
356 void
357 print_help (void)
359         char *myport;
360         asprintf (&myport, "%d", DEFAULT_PORT);
362         print_revision (progname, revision);
364         printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
365         printf (COPYRIGHT, copyright, email);
367   printf ("\n\n");
368   
369         print_usage ();
371         printf (_(UT_HELP_VRSN));
373         printf (_(UT_HOST_PORT), 'p', myport);
375         printf (_(UT_IPv46));
377         printf (" %s\n", "-a [--attr]");
378   printf ("    %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
379   printf (" %s\n", "-b [--base]");
380   printf ("    %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
381   printf (" %s\n", "-D [--bind]");
382   printf ("    %s\n", _("ldap bind DN (if required)"));
383   printf (" %s\n", "-P [--pass]");
384   printf ("    %s\n", _("ldap password (if required)"));
386 #ifdef HAVE_LDAP_SET_OPTION
387         printf (" %s\n", "-2 [--ver2]");
388   printf ("    %s\n", _("use ldap protocol version 2"));
389   printf (" %s\n", "-3 [--ver3]");
390   printf ("    %s\n", _("use ldap protocol version 3"));
391   printf ("(default protocol version: %d)", DEFAULT_PROTOCOL);
392 #endif
394         printf (_(UT_WARN_CRIT));
396         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
398         printf (_(UT_VERBOSE));
400         printf (_(UT_SUPPORT));
403 /* todo
404 * add option -4 and -6 to the long manual
406 */
408 void
409 print_usage (void)
411   printf (_("Usage:"));
412         printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
413   printf ("\n       [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
414 #ifdef HAVE_LDAP_SET_OPTION
415                         "\n       [-2|-3] [-4|-6]"
416 #else
417                         ""
418 #endif
419                         );