Code

Added support for --extra-opts in all C plugins (disabled by default, see configure...
[nagiosplug.git] / plugins / check_ldap.c
1 /*****************************************************************************
2
3 * Nagios check_ldap plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
7
8 * Last Modified: $Date$
9
10 * Description:
11
12 * This file contains the check_ldap plugin
13
14
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24
25 * You should have received a copy of the GNU General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 * $Id$
29
30 *****************************************************************************/
32 /* progname may be check_ldaps */
33 char *progname = "check_ldap";
34 const char *revision = "$Revision$";
35 const char *copyright = "2000-2008";
36 const char *email = "nagiosplug-devel@lists.sourceforge.net";
38 #include "common.h"
39 #include "netutils.h"
40 #include "utils.h"
42 #include <lber.h>
43 #include <ldap.h>
45 enum {
46         UNDEFINED = 0,
47 #ifdef HAVE_LDAP_SET_OPTION
48         DEFAULT_PROTOCOL = 2,
49 #endif
50         DEFAULT_PORT = 389
51 };
53 int process_arguments (int, char **);
54 int validate_arguments (void);
55 void print_help (void);
56 void print_usage (void);
58 char ld_defattr[] = "(objectclass=*)";
59 char *ld_attr = ld_defattr;
60 char *ld_host = NULL;
61 char *ld_base = NULL;
62 char *ld_passwd = NULL;
63 char *ld_binddn = NULL;
64 int ld_port = DEFAULT_PORT;
65 #ifdef HAVE_LDAP_SET_OPTION
66 int ld_protocol = DEFAULT_PROTOCOL;
67 #endif
68 #ifndef LDAP_OPT_SUCCESS
69 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
70 #endif
71 double warn_time = UNDEFINED;
72 double crit_time = UNDEFINED;
73 struct timeval tv;
74 int starttls = FALSE;
75 int ssl_on_connect = FALSE;
76 int verbose = 0;
78 /* for ldap tls */
80 char *SERVICE = "LDAP";
82 int
83 main (int argc, char *argv[])
84 {
86         LDAP *ld;
87         LDAPMessage *result;
89         /* should be    int result = STATE_UNKNOWN; */
90         
91         int status = STATE_UNKNOWN;
92         long microsec;
93         double elapsed_time;
94         
95         /* for ldap tls */
96         
97         int tls; 
98         int version=3;
100         setlocale (LC_ALL, "");
101         bindtextdomain (PACKAGE, LOCALEDIR);
102         textdomain (PACKAGE);
104         if (strstr(argv[0],"check_ldaps")) {
105                 asprintf (&progname, "check_ldaps");
106         }
108         /* Parse extra opts if any */
109         argv=np_extra_opts (&argc, argv, progname);
111         if (process_arguments (argc, argv) == ERROR)
112                 usage4 (_("Could not parse arguments"));
114         if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
115                 starttls = TRUE;
117         /* initialize alarm signal handling */
118         signal (SIGALRM, socket_timeout_alarm_handler);
120         /* set socket timeout */
121         alarm (socket_timeout);
123         /* get the start time */
124         gettimeofday (&tv, NULL);
126         /* initialize ldap */
127 #ifdef HAVE_LDAP_INIT
128         if (!(ld = ldap_init (ld_host, ld_port))) {
129                 printf ("Could not connect to the server at port %i\n", ld_port);
130                 return STATE_CRITICAL;
131         }
132 #else   
133         if (!(ld = ldap_open (ld_host, ld_port))) {
134                 if (verbose)
135                         ldap_perror(ld, "ldap_open");
136                 printf (_("Could not connect to the server at port %i\n"), ld_port);
137                 return STATE_CRITICAL;
138         }
139 #endif /* HAVE_LDAP_INIT */
140         
141 #ifdef HAVE_LDAP_SET_OPTION
142         /* set ldap options */
143         if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
144                         LDAP_OPT_SUCCESS ) {
145                 printf(_("Could not set protocol version %d\n"), ld_protocol);
146                 return STATE_CRITICAL;
147         }
148 #endif
150         if (ld_port == LDAPS_PORT || ssl_on_connect) {
151                 asprintf (&SERVICE, "LDAPS");
152 #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
153                 /* ldaps: set option tls */
154                 tls = LDAP_OPT_X_TLS_HARD;
155                 
156                 if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
157                 {
158                         if (verbose)
159                                 ldap_perror(ld, "ldaps_option");
160                         printf (_("Could not init TLS at port %i!\n"), ld_port);
161                         return STATE_CRITICAL;
162                 }
163 #else
164                 printf (_("TLS not supported by the libraries!\n"));
165                 return STATE_CRITICAL;
166 #endif /* LDAP_OPT_X_TLS */
167         } else if (starttls) {
168                 asprintf (&SERVICE, "LDAP-TLS");
169 #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
170                 /* ldap with startTLS: set option version */
171                 if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
172                 {
173                         if (version < LDAP_VERSION3)
174                         {
175                                 version = LDAP_VERSION3;
176                                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
177                         }
178                 }
179                 /* call start_tls */
180                 if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
181                 {
182                         if (verbose) 
183                                 ldap_perror(ld, "ldap_start_tls");
184                         printf (_("Could not init startTLS at port %i!\n"), ld_port);
185                         return STATE_CRITICAL;
186                 }
187 #else
188                 printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
189                 return STATE_CRITICAL;
190 #endif /* HAVE_LDAP_START_TLS_S */
191         }
192         
193         /* bind to the ldap server */
194         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
195                         LDAP_SUCCESS) {
196                 if (verbose)
197                         ldap_perror(ld, "ldap_bind");
198                 printf (_("Could not bind to the ldap-server\n"));
199                 return STATE_CRITICAL;
200         }
202         /* do a search of all objectclasses in the base dn */
203         if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
204                         != LDAP_SUCCESS) {
205                 if (verbose)
206                         ldap_perror(ld, "ldap_search");
207                 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
208                 return STATE_CRITICAL;
209         }
211         /* unbind from the ldap server */
212         ldap_unbind (ld);
214         /* reset the alarm handler */
215         alarm (0);
217         /* calcutate the elapsed time and compare to thresholds */
219         microsec = deltime (tv);
220         elapsed_time = (double)microsec / 1.0e6;
222         if (crit_time!=UNDEFINED && elapsed_time>crit_time)
223                 status = STATE_CRITICAL;
224         else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
225                 status = STATE_WARNING;
226         else
227                 status = STATE_OK;
229         /* print out the result */
230         printf (_("LDAP %s - %.3f seconds response time|%s\n"),
231                 state_text (status),
232                 elapsed_time,
233                 fperfdata ("time", elapsed_time, "s",
234                           (int)warn_time, warn_time,
235                           (int)crit_time, crit_time,
236                           TRUE, 0, FALSE, 0));
238         return status;
241 /* process command-line arguments */
242 int
243 process_arguments (int argc, char **argv)
245         int c;
247         int option = 0;
248         /* initialize the long option struct */
249         static struct option longopts[] = {
250                 {"help", no_argument, 0, 'h'},
251                 {"version", no_argument, 0, 'V'},
252                 {"timeout", required_argument, 0, 't'},
253                 {"host", required_argument, 0, 'H'},
254                 {"base", required_argument, 0, 'b'},
255                 {"attr", required_argument, 0, 'a'},
256                 {"bind", required_argument, 0, 'D'},
257                 {"pass", required_argument, 0, 'P'},
258 #ifdef HAVE_LDAP_SET_OPTION
259                 {"ver2", no_argument, 0, '2'},
260                 {"ver3", no_argument, 0, '3'},
261 #endif
262                 {"starttls", no_argument, 0, 'T'},
263                 {"ssl", no_argument, 0, 'S'},
264                 {"use-ipv4", no_argument, 0, '4'},
265                 {"use-ipv6", no_argument, 0, '6'},
266                 {"port", required_argument, 0, 'p'},
267                 {"warn", required_argument, 0, 'w'},
268                 {"crit", required_argument, 0, 'c'},
269                 {"verbose", no_argument, 0, 'v'},
270                 {0, 0, 0, 0}
271         };
273         if (argc < 2)
274                 return ERROR;
276         for (c = 1; c < argc; c++) {
277                 if (strcmp ("-to", argv[c]) == 0)
278                         strcpy (argv[c], "-t");
279         }
281         while (1) {
282                 c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
284                 if (c == -1 || c == EOF)
285                         break;
287                 switch (c) {
288                 case 'h':                                                                       /* help */
289                         print_help ();
290                         exit (STATE_OK);
291                 case 'V':                                                                       /* version */
292                         print_revision (progname, revision);
293                         exit (STATE_OK);
294                 case 't':                                                                       /* timeout period */
295                         if (!is_intnonneg (optarg))
296                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
297                         else
298                                 socket_timeout = atoi (optarg);
299                         break;
300                 case 'H':
301                         ld_host = optarg;
302                         break;
303                 case 'b':
304                         ld_base = optarg;
305                         break;
306                 case 'p':
307                         ld_port = atoi (optarg);
308                         break;
309                 case 'a':
310                         ld_attr = optarg;
311                         break;
312                 case 'D':
313                         ld_binddn = optarg;
314                         break;
315                 case 'P':
316                         ld_passwd = optarg;
317                         break;
318                 case 'w':
319                         warn_time = strtod (optarg, NULL);
320                         break;
321                 case 'c':
322                         crit_time = strtod (optarg, NULL);
323                         break;
324 #ifdef HAVE_LDAP_SET_OPTION
325                 case '2':
326                         ld_protocol = 2;
327                         break;
328                 case '3':
329                         ld_protocol = 3;
330                         break;
331 #endif
332                 case '4':
333                         address_family = AF_INET;
334                         break;
335                 case 'v':
336                         verbose++;
337                         break;
338                 case 'T':
339                         if (! ssl_on_connect)
340                                 starttls = TRUE;
341                         else
342                                 usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
343                         break;
344                 case 'S':
345                         if (! starttls) {
346                                 ssl_on_connect = TRUE;
347                                 ld_port = LDAPS_PORT;
348                         } else
349                                 usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
350                         break;
351                 case '6':
352 #ifdef USE_IPV6
353                         address_family = AF_INET6;
354 #else
355                         usage (_("IPv6 support not available\n"));
356 #endif
357                         break;
358                 default:
359                         usage5 ();
360                 }
361         }
363         c = optind;
364         if (ld_host == NULL && is_host(argv[c]))
365                 ld_host = strdup (argv[c++]);
367         if (ld_base == NULL && argv[c])
368                 ld_base = strdup (argv[c++]);
370         return validate_arguments ();
374 int
375 validate_arguments ()
377         if (ld_host==NULL || strlen(ld_host)==0)
378                 usage4 (_("Please specify the host name\n"));
380         if (ld_base==NULL || strlen(ld_base)==0)
381                 usage4 (_("Please specify the LDAP base\n"));
383         return OK;
387 void
388 print_help (void)
390         char *myport;
391         asprintf (&myport, "%d", DEFAULT_PORT);
393         print_revision (progname, revision);
395         printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
396         printf (COPYRIGHT, copyright, email);
398         printf ("\n\n");
400         print_usage ();
402         printf (_(UT_HELP_VRSN));
403         printf (_(UT_EXTRA_OPTS));
405         printf (_(UT_HOST_PORT), 'p', myport);
407         printf (_(UT_IPv46));
409         printf (" %s\n", "-a [--attr]");
410   printf ("    %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
411   printf (" %s\n", "-b [--base]");
412   printf ("    %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
413   printf (" %s\n", "-D [--bind]");
414   printf ("    %s\n", _("ldap bind DN (if required)"));
415   printf (" %s\n", "-P [--pass]");
416   printf ("    %s\n", _("ldap password (if required)"));
417   printf (" %s\n", "-T [--starttls]");
418   printf ("    %s\n", _("use starttls mechanism introduced in protocol version 3"));
419   printf (" %s\n", "-S [--ssl]");
420   printf ("    %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT);
422 #ifdef HAVE_LDAP_SET_OPTION
423         printf (" %s\n", "-2 [--ver2]");
424   printf ("    %s\n", _("use ldap protocol version 2"));
425   printf (" %s\n", "-3 [--ver3]");
426   printf ("    %s\n", _("use ldap protocol version 3"));
427   printf ("    (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL);
428 #endif
430         printf (_(UT_WARN_CRIT));
432         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
434         printf (_(UT_VERBOSE));
436         printf ("\n");
437         printf ("%s\n", _("Notes:"));
438         printf (" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
439         printf (_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT);
440         printf (" %s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
441         printf (" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
442         printf (" %s\n", _("to define the behaviour explicitly instead."));
443 #ifdef NP_EXTRA_OPTS
444         printf ("\n");
445         printf (_(UT_EXTRA_OPTS_NOTES));
446 #endif
448         printf (_(UT_SUPPORT));
451 /* todo
452 * add option -4 and -6 to the long manual
454 */
456 void
457 print_usage (void)
459   printf (_("Usage:"));
460         printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
461   printf ("\n       [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
462 #ifdef HAVE_LDAP_SET_OPTION
463                         "\n       [-2|-3] [-4|-6]"
464 #else
465                         ""
466 #endif
467                         );