Code

more r1879 fixes
[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 #ifndef LDAP_OPT_SUCCESS
71 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
72 #endif
73 double warn_time = UNDEFINED;
74 double crit_time = UNDEFINED;
75 struct timeval tv;
76 int starttls = FALSE;
77 int ssl_on_connect = FALSE;
78 int verbose = 0;
80 /* for ldap tls */
82 char *SERVICE = "LDAP";
84 int
85 main (int argc, char *argv[])
86 {
88         LDAP *ld;
89         LDAPMessage *result;
91         /* should be    int result = STATE_UNKNOWN; */
92         
93         int status = STATE_UNKNOWN;
94         long microsec;
95         double elapsed_time;
96         
97         /* for ldap tls */
98         
99         int tls; 
100         int version=3;
102         setlocale (LC_ALL, "");
103         bindtextdomain (PACKAGE, LOCALEDIR);
104         textdomain (PACKAGE);
106         if (strstr(argv[0],"check_ldaps")) {
107                 asprintf (&progname, "check_ldaps");
108         }
109         
110         if (process_arguments (argc, argv) == ERROR)
111                 usage4 (_("Could not parse arguments"));
113         if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
114                 starttls = TRUE;
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;
130         }
131 #else   
132         if (!(ld = ldap_open (ld_host, ld_port))) {
133                 if (verbose)
134                         ldap_perror(ld, "ldap_open");
135                 printf (_("Could not connect to the server at port %i\n"), ld_port);
136                 return STATE_CRITICAL;
137         }
138 #endif /* HAVE_LDAP_INIT */
139         
140 #ifdef HAVE_LDAP_SET_OPTION
141         /* set ldap options */
142         if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
143                         LDAP_OPT_SUCCESS ) {
144                 printf(_("Could not set protocol version %d\n"), ld_protocol);
145                 return STATE_CRITICAL;
146         }
147 #endif
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;
154                 
155                 if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
156                 {
157                         if (verbose)
158                                 ldap_perror(ld, "ldaps_option");
159                         printf (_("Could not init TLS at port %i!\n"), ld_port);
160                         return STATE_CRITICAL;
161                 }
162 #else
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 )
171                 {
172                         if (version < LDAP_VERSION3)
173                         {
174                                 version = LDAP_VERSION3;
175                                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
176                         }
177                 }
178                 /* call start_tls */
179                 if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
180                 {
181                         if (verbose) 
182                                 ldap_perror(ld, "ldap_start_tls");
183                         printf (_("Could not init startTLS at port %i!\n"), ld_port);
184                         return STATE_CRITICAL;
185                 }
186 #else
187                 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
188                 return STATE_CRITICAL;
189 #endif /* HAVE_LDAP_START_TLS_S */
190         }
191         
192         /* bind to the ldap server */
193         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
194                         LDAP_SUCCESS) {
195                 if (verbose)
196                         ldap_perror(ld, "ldap_bind");
197                 printf (_("Could not bind to the ldap-server\n"));
198                 return STATE_CRITICAL;
199         }
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)
203                         != LDAP_SUCCESS) {
204                 if (verbose)
205                         ldap_perror(ld, "ldap_search");
206                 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
207                 return STATE_CRITICAL;
208         }
210         /* unbind from the ldap server */
211         ldap_unbind (ld);
213         /* reset the alarm handler */
214         alarm (0);
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;
225         else
226                 status = STATE_OK;
228         /* print out the result */
229         printf (_("LDAP %s - %.3f seconds response time|%s\n"),
230                 state_text (status),
231                 elapsed_time,
232                 fperfdata ("time", elapsed_time, "s",
233                           (int)warn_time, warn_time,
234                           (int)crit_time, crit_time,
235                           TRUE, 0, FALSE, 0));
237         return status;
240 /* process command-line arguments */
241 int
242 process_arguments (int argc, char **argv)
244         int c;
246         int option = 0;
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'},
260 #endif
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'},
269                 {0, 0, 0, 0}
270         };
272         if (argc < 2)
273                 return ERROR;
275         for (c = 1; c < argc; c++) {
276                 if (strcmp ("-to", argv[c]) == 0)
277                         strcpy (argv[c], "-t");
278         }
280         while (1) {
281                 c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
283                 if (c == -1 || c == EOF)
284                         break;
286                 switch (c) {
287                 case 'h':                                                                       /* help */
288                         print_help ();
289                         exit (STATE_OK);
290                 case 'V':                                                                       /* version */
291                         print_revision (progname, revision);
292                         exit (STATE_OK);
293                 case 't':                                                                       /* timeout period */
294                         if (!is_intnonneg (optarg))
295                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
296                         else
297                                 socket_timeout = atoi (optarg);
298                         break;
299                 case 'H':
300                         ld_host = optarg;
301                         break;
302                 case 'b':
303                         ld_base = optarg;
304                         break;
305                 case 'p':
306                         ld_port = atoi (optarg);
307                         break;
308                 case 'a':
309                         ld_attr = optarg;
310                         break;
311                 case 'D':
312                         ld_binddn = optarg;
313                         break;
314                 case 'P':
315                         ld_passwd = optarg;
316                         break;
317                 case 'w':
318                         warn_time = strtod (optarg, NULL);
319                         break;
320                 case 'c':
321                         crit_time = strtod (optarg, NULL);
322                         break;
323 #ifdef HAVE_LDAP_SET_OPTION
324                 case '2':
325                         ld_protocol = 2;
326                         break;
327                 case '3':
328                         ld_protocol = 3;
329                         break;
330 #endif
331                 case '4':
332                         address_family = AF_INET;
333                         break;
334                 case 'v':
335                         verbose++;
336                         break;
337                 case 'T':
338                         if (! ssl_on_connect)
339                                 starttls = TRUE;
340                         else
341                                 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
342                         break;
343                 case 'S':
344                         if (! starttls) {
345                                 ssl_on_connect = TRUE;
346                                 ld_port = LDAPS_PORT;
347                         } else
348                                 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
349                         break;
350                 case '6':
351 #ifdef USE_IPV6
352                         address_family = AF_INET6;
353 #else
354                         usage (_("IPv6 support not available\n"));
355 #endif
356                         break;
357                 default:
358                         usage5 ();
359                 }
360         }
362         c = optind;
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 ();
373 int
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"));
382         return OK;
386 void
387 print_help (void)
389         char *myport;
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);
397   printf ("\n\n");
398   
399         print_usage ();
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);
426 #endif
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));
444 /* todo
445 * add option -4 and -6 to the long manual
447 */
449 void
450 print_usage (void)
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
456                         "\n       [-2|-3] [-4|-6]"
457 #else
458                         ""
459 #endif
460                         );