Code

markup for translation
[nagiosplug.git] / plugins / check_ldap.c
1 /******************************************************************************
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 ******************************************************************************/
19 const char *progname = "check_ldap";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "config.h"
25 #include "common.h"
26 #include "netutils.h"
27 #include "utils.h"
29 #include <lber.h>
30 #include <ldap.h>
32 enum {
33         UNDEFINED = -1,
34 #ifdef HAVE_LDAP_SET_OPTION
35         DEFAULT_PROTOCOL = 2,
36 #endif
37         DEFAULT_PORT = 389
38 };
40 void
41 print_usage ()
42 {
43         printf (_("\
44 Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
45   [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
46 (Note: all times are in seconds.)\n"),
47                 progname, (HAVE_LDAP_SET_OPTION ? "[-2|-3] [-4|-6]" : ""));
48         printf (_(UT_HLP_VRS), progname, progname);
49 }
51 void
52 print_help ()
53 {
54         char *myport;
55         asprintf (&myport, "%d", DEFAULT_PORT);
57         print_revision (progname, revision);
59         printf (_("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"));
60         printf (_(COPYRIGHT), copyright, email);
62         print_usage ();
64         printf (_(UT_HELP_VRSN));
66         printf (_(UT_HOST_PORT), 'p', myport);
68         printf (_(UT_IPv46));
70         printf (_("\
71  -a [--attr]\n\
72     ldap attribute to search (default: \"(objectclass=*)\"\n\
73  -b [--base]\n\
74     ldap base (eg. ou=my unit, o=my org, c=at)\n\
75  -D [--bind]\n\
76     ldap bind DN (if required)\n\
77  -P [--pass]\n\
78     ldap password (if required)\n"));
80 #ifdef HAVE_LDAP_SET_OPTION
81         printf (_("\
82  -2 [--ver2]\n\
83      use ldap protocol version 2\n\
84  -3 [--ver3]\n\
85     use ldap protocol version 3\n\
86     (default protocol version: %d)\n"),
87                 DEFAULT_PROTOCOL);
88 #endif
90         printf (_(UT_WARN_CRIT));
92         printf (_(UT_SUPPORT));
93 }
94 \f
95 int process_arguments (int, char **);
96 int validate_arguments (void);
98 char ld_defattr[] = "(objectclass=*)";
99 char *ld_attr = ld_defattr;
100 char *ld_host = "";
101 char *ld_base = "";
102 char *ld_passwd = NULL;
103 char *ld_binddn = NULL;
104 unsigned int ld_port = DEFAULT_PORT;
105 #ifdef HAVE_LDAP_SET_OPTION
106 int ld_protocol = DEFAULT_PROTOCOL;
107 #endif
108 int warn_time = UNDEFINED;
109 int crit_time = UNDEFINED;
111 int
112 main (int argc, char *argv[])
115         LDAP *ld;
116         LDAPMessage *result;
118         int t_diff;
119         time_t time0, time1;
121         if (process_arguments (argc, argv) == ERROR)
122                 usage (_("check_ldap: could not parse arguments\n"));
124         /* initialize alarm signal handling */
125         signal (SIGALRM, socket_timeout_alarm_handler);
127         /* set socket timeout */
128         alarm (socket_timeout);
130         /* get the start time */
131         time (&time0);
133         /* initialize ldap */
134         if (!(ld = ldap_open (ld_host, ld_port))) {
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         }
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
148         /* bind to the ldap server */
149         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
150                         LDAP_SUCCESS) {
151                 /*ldap_perror(ld, "ldap_bind"); */
152                 printf (_("Could not bind to the ldap-server\n"));
153                 return STATE_CRITICAL;
154         }
156         /* do a search of all objectclasses in the base dn */
157         if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
158                         != LDAP_SUCCESS) {
159                 /*ldap_perror(ld, "ldap_search"); */
160                 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
161                 return STATE_CRITICAL;
162         }
164         /* unbind from the ldap server */
165         ldap_unbind (ld);
167         /* reset the alarm handler */
168         alarm (0);
170         /* get the finish time */
171         time (&time1);
173         /* calcutate the elapsed time and compare to thresholds */
174         t_diff = time1 - time0;
176         if (crit_time!=UNDEFINED && t_diff>=crit_time) {
177                 printf (_("LDAP CRITICAL - %i seconds response time\n"), t_diff);
178                 return STATE_CRITICAL;
179         }
181         if (warn_time!=UNDEFINED && t_diff>=warn_time) {
182                 printf (_("LDAP WARNING - %i seconds response time\n"), t_diff);
183                 return STATE_WARNING;
184         }
186         /* print out the result */
187         printf (_("LDAP OK - %i seconds response time\n"), t_diff);
189         return STATE_OK;
192 /* process command-line arguments */
193 int
194 process_arguments (int argc, char **argv)
196         int c;
198         int option_index = 0;
199         /* initialize the long option struct */
200         static struct option longopts[] = {
201                 {"help", no_argument, 0, 'h'},
202                 {"version", no_argument, 0, 'V'},
203                 {"timeout", required_argument, 0, 't'},
204                 {"host", required_argument, 0, 'H'},
205                 {"base", required_argument, 0, 'b'},
206                 {"attr", required_argument, 0, 'a'},
207                 {"bind", required_argument, 0, 'D'},
208                 {"pass", required_argument, 0, 'P'},
209 #ifdef HAVE_LDAP_SET_OPTION
210                 {"ver2", no_argument, 0, '2'},
211                 {"ver3", no_argument, 0, '3'},
212 #endif
213                 {"use-ipv4", no_argument, 0, '4'},
214                 {"use-ipv6", no_argument, 0, '6'},
215                 {"port", required_argument, 0, 'p'},
216                 {"warn", required_argument, 0, 'w'},
217                 {"crit", required_argument, 0, 'c'},
218                 {0, 0, 0, 0}
219         };
221         if (argc < 2)
222                 return ERROR;
224         for (c = 1; c < argc; c++) {
225                 if (strcmp ("-to", argv[c]) == 0)
226                         strcpy (argv[c], "-t");
227         }
229         while (1) {
230                 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option_index);
232                 if (c == -1 || c == EOF)
233                         break;
235                 switch (c) {
236                 case 'h':                                                                       /* help */
237                         print_help ();
238                         exit (STATE_OK);
239                 case 'V':                                                                       /* version */
240                         print_revision (progname, revision);
241                         exit (STATE_OK);
242                 case 't':                                                                       /* timeout period */
243                         if (!is_intnonneg (optarg))
244                                 usage2 (_("timeout interval must be a positive integer"), optarg);
245                         socket_timeout = atoi (optarg);
246                         break;
247                 case 'H':
248                         ld_host = optarg;
249                         break;
250                 case 'b':
251                         ld_base = optarg;
252                         break;
253                 case 'p':
254                         ld_port = atoi (optarg);
255                         break;
256                 case 'a':
257                         ld_attr = optarg;
258                         break;
259                 case 'D':
260                         ld_binddn = optarg;
261                         break;
262                 case 'P':
263                         ld_passwd = optarg;
264                         break;
265                 case 'w':
266                         warn_time = atoi (optarg);
267                         break;
268                 case 'c':
269                         crit_time = atoi (optarg);
270                         break;
271 #ifdef HAVE_LDAP_SET_OPTION
272                 case '2':
273                         ld_protocol = 2;
274                         break;
275                 case '3':
276                         ld_protocol = 3;
277                         break;
278 #endif
279                 case '4':
280                         address_family = AF_INET;
281                         break;
282                 case '6':
283 #ifdef USE_IPV6
284                         address_family = AF_INET6;
285 #else
286                         usage (_("IPv6 support not available\n"));
287 #endif
288                         break;
289                 default:
290                         usage (_("check_ldap: could not parse unknown arguments\n"));
291                         break;
292                 }
293         }
295         c = optind;
296         if (strlen(ld_host) == 0 && is_host(argv[c])) {
297                 asprintf (&ld_host, "%s", argv[c++]);
298         }
299         if (strlen(ld_base) == 0 && argv[c]) {
300                 asprintf (&ld_base, "%s", argv[c++]);
301         }
303         return validate_arguments ();
306 int
307 validate_arguments ()
309         if (strlen(ld_host) == 0)
310                 usage (_("please specify the host name\n"));
312         if (strlen(ld_base) == 0)
313                 usage (_("please specify the LDAP base\n"));
315         return OK;